···1+open Js_of_ocaml
2+3+let post_result id text =
4+ ignore (Js.Unsafe.global##postMessage (Js.string (id ^ ":" ^ text)))
5+6+let () =
7+ try
8+ (* Create a 4x4 test image: red/green/blue/white quadrants *)
9+ let w = 4 and h = 4 in
10+ let data = Bigarray.Array1.create Bigarray.int8_unsigned Bigarray.c_layout (w * h * 4) in
11+ for y = 0 to h - 1 do
12+ for x = 0 to w - 1 do
13+ let off = (y * w + x) * 4 in
14+ let r, g, b =
15+ if y < 2 && x < 2 then (255, 0, 0)
16+ else if y < 2 then (0, 255, 0)
17+ else if x < 2 then (0, 0, 255)
18+ else (255, 255, 255)
19+ in
20+ Bigarray.Array1.set data off r;
21+ Bigarray.Array1.set data (off + 1) g;
22+ Bigarray.Array1.set data (off + 2) b;
23+ Bigarray.Array1.set data (off + 3) 255
24+ done
25+ done;
26+ let img = Viz.{ data; width = w; height = h } in
27+28+ (* Test 1: PNG encoding produces valid data *)
29+ let png = Viz.png_of_rgba img in
30+ post_result "png-result"
31+ (Printf.sprintf "OK: %d bytes, magic=0x%02x%c%c%c"
32+ (String.length png) (Char.code png.[0]) png.[1] png.[2] png.[3]);
33+34+ (* Test 2: data URL generation *)
35+ let url = Viz_jsoo.to_data_url img in
36+ let prefix = "data:image/png;base64," in
37+ let has_prefix = String.length url > String.length prefix &&
38+ String.sub url 0 (String.length prefix) = prefix in
39+ post_result "url-result"
40+ (if has_prefix then Printf.sprintf "OK: %d chars" (String.length url)
41+ else "FAIL: bad prefix");
42+43+ (* Post the data URL for the page to display as an image *)
44+ post_result "img-src" url;
45+46+ post_result "status" "ALL PASSED"
47+ with exn ->
48+ post_result "status" (Printf.sprintf "FAILED: %s" (Printexc.to_string exn))