open Imgmeta let test_format_construction () = Alcotest.(check int) "eleven format constructors" 11 (List.length [ PNG; JPEG; GIF; WebP; HEIF; AVIF; TIFF; JXL; BMP; ICO; QOI ]) ;; let test_record_fields () = let m = { format = PNG; width = 10; height = 20; depth = 8; orientation = 1 } in Alcotest.(check int) "width" 10 m.width; Alcotest.(check int) "height" 20 m.height; Alcotest.(check int) "depth" 8 m.depth; Alcotest.(check int) "orientation" 1 m.orientation ;; let test_pp_error_unknown () = let buf = Buffer.create 64 in let fmt = Format.formatter_of_buffer buf in pp_error fmt Unknown_format; Format.pp_print_flush fmt (); Alcotest.(check string) "msg" "unknown_format" (Buffer.contents buf) ;; let test_exception_carries_error () = let raised = try raise (Imgmeta_error (Malformed "bad header")) with | Imgmeta_error (Malformed s) -> String.equal s "bad header" | _ -> false in Alcotest.(check bool) "raises and matches" true raised ;; let test_reader_bytes_read () = let r = Imgmeta.Reader.of_bytes (Bytes.of_string "abcdef") in let chunk = Imgmeta.Reader.read r ~len:3 in Alcotest.(check string) "first 3" "abc" (Bytes.to_string chunk); Alcotest.(check int) "pos advanced" 3 (Imgmeta.Reader.pos r) ;; let test_reader_bytes_read_at () = let r = Imgmeta.Reader.of_bytes (Bytes.of_string "abcdef") in let chunk = Imgmeta.Reader.read_at r ~pos:2 ~len:3 in Alcotest.(check string) "from offset 2" "cde" (Bytes.to_string chunk) ;; let test_reader_bytes_size () = let r = Imgmeta.Reader.of_bytes (Bytes.of_string "abcdef") in Alcotest.(check (option int)) "size" (Some 6) (Imgmeta.Reader.size r) ;; let test_reader_bytes_truncated () = let r = Imgmeta.Reader.of_bytes (Bytes.of_string "abc") in let raised = try let _ = Imgmeta.Reader.read r ~len:10 in false with | Imgmeta_error Truncated -> true in Alcotest.(check bool) "raises truncated when over reading" true raised ;; let with_temp_file content f = let path = Filename.temp_file "imgmeta_" ".bin" in Out_channel.with_open_bin path (fun oc -> Out_channel.output_string oc content); Fun.protect ~finally:(fun () -> Sys.remove path) (fun () -> f path) ;; let test_reader_file_read () = with_temp_file "hello world" (fun path -> match Imgmeta.Reader.of_file path with | Error _ -> Alcotest.fail "of_file failed" | Ok r -> let head = Imgmeta.Reader.read r ~len:5 in Alcotest.(check string) "first 5" "hello" (Bytes.to_string head); let tail = Imgmeta.Reader.read_at r ~pos:6 ~len:5 in Alcotest.(check string) "tail" "world" (Bytes.to_string tail); Imgmeta.Reader.close r) ;; let test_reader_in_channel_read () = with_temp_file "abcdef" (fun path -> In_channel.with_open_bin path (fun ic -> match Imgmeta.Reader.of_in_channel ic with | Error _ -> Alcotest.fail "of_in_channel failed" | Ok r -> let chunk = Imgmeta.Reader.read r ~len:3 in Alcotest.(check string) "head" "abc" (Bytes.to_string chunk))) ;; let bytes_of_hex hex = let clean = String.concat "" (String.split_on_char ' ' hex) in let len = String.length clean / 2 in let b = Bytes.create len in for i = 0 to len - 1 do let hi = clean.[i * 2] in let lo = clean.[(i * 2) + 1] in let v c = match c with | '0' .. '9' -> Char.code c - Char.code '0' | 'a' .. 'f' -> Char.code c - Char.code 'a' + 10 | _ -> failwith "bad hex" in Bytes.set_uint8 b i ((v hi * 16) + v lo) done; b ;; let tiff_with_entries ~endian entries = let n = List.length entries in let buf = Bytes.create (8 + 2 + (12 * n) + 4) in let set_u16 = if endian = `LE then Bytes.set_uint16_le else Bytes.set_uint16_be in let set_u32 b o v = if endian = `LE then Bytes.set_int32_le b o (Int32.of_int v) else Bytes.set_int32_be b o (Int32.of_int v) in Bytes.set_uint8 buf 0 (if endian = `LE then 0x49 else 0x4d); Bytes.set_uint8 buf 1 (if endian = `LE then 0x49 else 0x4d); set_u16 buf 2 0x002a; set_u32 buf 4 8; set_u16 buf 8 n; List.iteri (fun i (tag, ty, count, value_lo16) -> let off = 10 + (i * 12) in set_u16 buf off tag; set_u16 buf (off + 2) ty; set_u32 buf (off + 4) count; set_u16 buf (off + 8) value_lo16; set_u16 buf (off + 10) 0) entries; set_u32 buf (10 + (12 * n)) 0; buf ;; let test_exif_orientation_le () = let tiff = tiff_with_entries ~endian:`LE [ 0x0112, 3, 1, 6 ] in Alcotest.(check int) "le orientation 6" 6 (Imgmeta.Formats.Exif.parse_orientation tiff) ;; let test_exif_orientation_be () = let tiff = tiff_with_entries ~endian:`BE [ 0x0112, 3, 1, 8 ] in Alcotest.(check int) "be orientation 8" 8 (Imgmeta.Formats.Exif.parse_orientation tiff) ;; let test_exif_missing_orientation () = let tiff = tiff_with_entries ~endian:`LE [ 0x0100, 3, 1, 256 ] in Alcotest.(check int) "missing tag returns 1" 1 (Imgmeta.Formats.Exif.parse_orientation tiff) ;; let test_exif_invalid_magic () = let tiff = tiff_with_entries ~endian:`LE [ 0x0112, 3, 1, 6 ] in Bytes.set_uint8 tiff 2 0xff; Alcotest.(check int) "bad magic returns 1" 1 (Imgmeta.Formats.Exif.parse_orientation tiff) ;; let test_exif_empty () = Alcotest.(check int) "empty returns 1" 1 (Imgmeta.Formats.Exif.parse_orientation Bytes.empty) ;; let test_exif_out_of_range () = let tiff = tiff_with_entries ~endian:`LE [ 0x0112, 3, 1, 9 ] in Alcotest.(check int) "value > 8 returns 1" 1 (Imgmeta.Formats.Exif.parse_orientation tiff) ;; let check_magic name hex expected = let b = bytes_of_hex hex in Alcotest.(check (option string)) name (Some (Imgmeta.format_to_string expected)) (Option.map Imgmeta.format_to_string (Imgmeta.Magic.of_bytes b)) ;; let test_magic_png () = check_magic "png" "89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52" PNG ;; let test_magic_jpeg () = check_magic "jpeg" "ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01" JPEG ;; let test_magic_gif () = check_magic "gif" "47 49 46 38 39 61 0a 00 0a 00 80 00 00 ff ff ff" GIF ;; let test_magic_webp () = check_magic "webp" "52 49 46 46 24 00 00 00 57 45 42 50 56 50 38 20" WebP ;; let test_magic_heif () = check_magic "heif" "00 00 00 18 66 74 79 70 68 65 69 63 00 00 00 00" HEIF ;; let test_magic_avif () = check_magic "avif" "00 00 00 18 66 74 79 70 61 76 69 66 00 00 00 00" AVIF ;; let test_magic_unknown () = let b = bytes_of_hex "00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff" in Alcotest.(check (option string)) "unknown returns none" None (Option.map Imgmeta.format_to_string (Imgmeta.Magic.of_bytes b)) ;; let png_chunk buf ty body = let len = Bytes.length body in let len_bytes = Bytes.create 4 in Bytes.set_int32_be len_bytes 0 (Int32.of_int len); Buffer.add_bytes buf len_bytes; Buffer.add_string buf ty; Buffer.add_bytes buf body; Buffer.add_string buf "\x00\x00\x00\x00" ;; let png_header ~width ~height ~depth ~color_type = let buf = Buffer.create 64 in Buffer.add_string buf "\x89PNG\r\n\x1a\n"; let ihdr = Bytes.create 13 in Bytes.set_int32_be ihdr 0 (Int32.of_int width); Bytes.set_int32_be ihdr 4 (Int32.of_int height); Bytes.set_uint8 ihdr 8 depth; Bytes.set_uint8 ihdr 9 color_type; Bytes.set_uint8 ihdr 10 0; Bytes.set_uint8 ihdr 11 0; Bytes.set_uint8 ihdr 12 0; png_chunk buf "IHDR" ihdr; Buffer.to_bytes buf ;; let png_with_exif ~width ~height ~depth ~color_type ~orientation = let buf = Buffer.create 96 in Buffer.add_string buf "\x89PNG\r\n\x1a\n"; let ihdr = Bytes.create 13 in Bytes.set_int32_be ihdr 0 (Int32.of_int width); Bytes.set_int32_be ihdr 4 (Int32.of_int height); Bytes.set_uint8 ihdr 8 depth; Bytes.set_uint8 ihdr 9 color_type; Bytes.set_uint8 ihdr 10 0; Bytes.set_uint8 ihdr 11 0; Bytes.set_uint8 ihdr 12 0; png_chunk buf "IHDR" ihdr; png_chunk buf "eXIf" (tiff_with_entries ~endian:`LE [ 0x0112, 3, 1, orientation ]); Buffer.to_bytes buf ;; let load_bytes path = In_channel.with_open_bin path (fun ic -> let len = In_channel.length ic |> Int64.to_int in let buf = Bytes.create len in match In_channel.really_input ic buf 0 len with | Some () -> buf | None -> failwith "short read") ;; let test_png_synthesized () = let data = png_header ~width:200 ~height:100 ~depth:8 ~color_type:2 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Png.read_metadata r with | Error e -> Alcotest.failf "expected ok, got %a" Imgmeta.pp_error e | Ok m -> Alcotest.(check int) "width" 200 m.width; Alcotest.(check int) "height" 100 m.height; Alcotest.(check int) "depth" 8 m.depth ;; let test_png_synthesized_16bit () = let data = png_header ~width:2 ~height:2 ~depth:16 ~color_type:6 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Png.read_metadata r with | Ok m -> Alcotest.(check int) "depth 16" 16 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_png_orientation_swap () = let data = png_with_exif ~width:200 ~height:100 ~depth:8 ~color_type:2 ~orientation:6 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Png.read_metadata r with | Ok m -> Alcotest.(check int) "swapped width" 100 m.width; Alcotest.(check int) "swapped height" 200 m.height; Alcotest.(check int) "orientation" 6 m.orientation | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_png_orientation_no_swap () = let data = png_with_exif ~width:200 ~height:100 ~depth:8 ~color_type:2 ~orientation:3 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Png.read_metadata r with | Ok m -> Alcotest.(check int) "unswapped width" 200 m.width; Alcotest.(check int) "unswapped height" 100 m.height; Alcotest.(check int) "orientation" 3 m.orientation | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_png_fixture () = let data = load_bytes "fixtures/nix.png" in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Png.read_metadata r with | Error e -> Alcotest.failf "fixtures/nix.png failed %a" Imgmeta.pp_error e | Ok m -> Alcotest.(check int) "width" 320 m.width; Alcotest.(check int) "height" 320 m.height; Alcotest.(check int) "depth" 8 m.depth ;; let gif_header ~width ~height ~color_res = let buf = Buffer.create 32 in Buffer.add_string buf "GIF89a"; let dim = Bytes.create 4 in Bytes.set_uint16_le dim 0 width; Bytes.set_uint16_le dim 2 height; Buffer.add_bytes buf dim; let packed = ((color_res - 1) land 0b111) lsl 4 in Buffer.add_char buf (Char.chr packed); Buffer.add_char buf '\x00'; Buffer.add_char buf '\x00'; Buffer.to_bytes buf ;; let test_gif_synthesized () = let data = gif_header ~width:64 ~height:48 ~color_res:8 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Gif.read_metadata r with | Ok m -> Alcotest.(check int) "width" 64 m.width; Alcotest.(check int) "height" 48 m.height; Alcotest.(check int) "depth" 8 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let jpeg_header ~width ~height ~precision = let buf = Buffer.create 32 in Buffer.add_string buf "\xff\xd8"; Buffer.add_string buf "\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00"; Buffer.add_string buf "\xff\xc0"; let len_bytes = Bytes.create 2 in Bytes.set_uint16_be len_bytes 0 17; Buffer.add_bytes buf len_bytes; Buffer.add_char buf (Char.chr precision); let dims = Bytes.create 4 in Bytes.set_uint16_be dims 0 height; Bytes.set_uint16_be dims 2 width; Buffer.add_bytes buf dims; Buffer.add_char buf '\x03'; for _ = 1 to 9 do Buffer.add_char buf '\x00' done; Buffer.to_bytes buf ;; let jpeg_with_exif ~width ~height ~precision ~orientation = let buf = Buffer.create 128 in Buffer.add_string buf "\xff\xd8"; let exif_tiff = tiff_with_entries ~endian:`LE [ 0x0112, 3, 1, orientation ] in let body = Bytes.cat (Bytes.of_string "Exif\x00\x00") exif_tiff in let app1_len = 2 + Bytes.length body in Buffer.add_string buf "\xff\xe1"; let len_bytes = Bytes.create 2 in Bytes.set_uint16_be len_bytes 0 app1_len; Buffer.add_bytes buf len_bytes; Buffer.add_bytes buf body; Buffer.add_string buf "\xff\xc0"; let sof_len = Bytes.create 2 in Bytes.set_uint16_be sof_len 0 17; Buffer.add_bytes buf sof_len; Buffer.add_char buf (Char.chr precision); let dims = Bytes.create 4 in Bytes.set_uint16_be dims 0 height; Bytes.set_uint16_be dims 2 width; Buffer.add_bytes buf dims; Buffer.add_char buf '\x03'; for _ = 1 to 9 do Buffer.add_char buf '\x00' done; Buffer.to_bytes buf ;; let test_jpeg_orientation_swap () = let data = jpeg_with_exif ~width:320 ~height:240 ~precision:8 ~orientation:6 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Jpeg.read_metadata r with | Ok m -> Alcotest.(check int) "swapped width" 240 m.width; Alcotest.(check int) "swapped height" 320 m.height; Alcotest.(check int) "orientation" 6 m.orientation | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_jpeg_orientation_no_swap () = let data = jpeg_with_exif ~width:320 ~height:240 ~precision:8 ~orientation:2 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Jpeg.read_metadata r with | Ok m -> Alcotest.(check int) "unswapped width" 320 m.width; Alcotest.(check int) "orientation" 2 m.orientation | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_jpeg_synthesized () = let data = jpeg_header ~width:320 ~height:240 ~precision:8 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Jpeg.read_metadata r with | Ok m -> Alcotest.(check int) "width" 320 m.width; Alcotest.(check int) "height" 240 m.height; Alcotest.(check int) "depth" 8 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_jpeg_fixture () = let data = load_bytes "fixtures/nix.jpeg" in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Jpeg.read_metadata r with | Error e -> Alcotest.failf "fixtures/nix.jpeg failed %a" Imgmeta.pp_error e | Ok m -> Alcotest.(check int) "width" 320 m.width; Alcotest.(check int) "height" 320 m.height; Alcotest.(check int) "depth" 8 m.depth ;; let riff_header chunks = let body = Buffer.create 64 in Buffer.add_string body "WEBP"; List.iter (fun (ty, data) -> Buffer.add_string body ty; let len = Bytes.create 4 in Bytes.set_int32_le len 0 (Int32.of_int (Bytes.length data)); Buffer.add_bytes body len; Buffer.add_bytes body data; if Bytes.length data mod 2 = 1 then Buffer.add_char body '\x00') chunks; let out = Buffer.create 96 in Buffer.add_string out "RIFF"; let size = Bytes.create 4 in Bytes.set_int32_le size 0 (Int32.of_int (Buffer.length body)); Buffer.add_bytes out size; Buffer.add_buffer out body; Buffer.to_bytes out ;; let webp_vp8x_body ~width ~height ~flags = let body = Bytes.create 10 in Bytes.fill body 0 10 '\x00'; Bytes.set_uint8 body 0 flags; Bytes.set_uint8 body 4 ((width - 1) land 0xff); Bytes.set_uint8 body 5 (((width - 1) lsr 8) land 0xff); Bytes.set_uint8 body 6 (((width - 1) lsr 16) land 0xff); Bytes.set_uint8 body 7 ((height - 1) land 0xff); Bytes.set_uint8 body 8 (((height - 1) lsr 8) land 0xff); Bytes.set_uint8 body 9 (((height - 1) lsr 16) land 0xff); body ;; let webp_vp8x ~width ~height = riff_header [ "VP8X", webp_vp8x_body ~width ~height ~flags:0 ] ;; let webp_vp8x_with_exif ~width ~height ~orientation = let vp8x = webp_vp8x_body ~width ~height ~flags:0x08 in let exif = tiff_with_entries ~endian:`LE [ 0x0112, 3, 1, orientation ] in riff_header [ "VP8X", vp8x; "EXIF", exif ] ;; let webp_vp8l ~width ~height = let body = Bytes.create 5 in Bytes.set_uint8 body 0 0x2f; let wm1 = width - 1 in let hm1 = height - 1 in Bytes.set_uint8 body 1 (wm1 land 0xff); Bytes.set_uint8 body 2 ((wm1 lsr 8) land 0x3f lor ((hm1 land 0x03) lsl 6)); Bytes.set_uint8 body 3 ((hm1 lsr 2) land 0xff); Bytes.set_uint8 body 4 ((hm1 lsr 10) land 0x0f); riff_header [ "VP8L", body ] ;; let test_webp_vp8x () = let data = webp_vp8x ~width:512 ~height:256 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Webp.read_metadata r with | Ok m -> Alcotest.(check int) "width" 512 m.width; Alcotest.(check int) "height" 256 m.height; Alcotest.(check int) "depth" 8 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_webp_orientation_swap () = let data = webp_vp8x_with_exif ~width:512 ~height:256 ~orientation:6 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Webp.read_metadata r with | Ok m -> Alcotest.(check int) "swapped width" 256 m.width; Alcotest.(check int) "swapped height" 512 m.height; Alcotest.(check int) "orientation" 6 m.orientation | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_webp_orientation_no_swap () = let data = webp_vp8x_with_exif ~width:512 ~height:256 ~orientation:2 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Webp.read_metadata r with | Ok m -> Alcotest.(check int) "unswapped width" 512 m.width; Alcotest.(check int) "orientation" 2 m.orientation | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_webp_vp8l () = let data = webp_vp8l ~width:16 ~height:16 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Webp.read_metadata r with | Ok m -> Alcotest.(check int) "width" 16 m.width; Alcotest.(check int) "height" 16 m.height | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let isobmff_box ty body = let buf = Buffer.create (8 + Bytes.length body) in let len = Bytes.create 4 in Bytes.set_int32_be len 0 (Int32.of_int (8 + Bytes.length body)); Buffer.add_bytes buf len; Buffer.add_string buf ty; Buffer.add_bytes buf body; Buffer.to_bytes buf ;; let test_isobmff_walk_top_level () = let a = isobmff_box "ftyp" (Bytes.of_string "heic") in let b = isobmff_box "free" (Bytes.of_string "xxx") in let data = Bytes.cat a b in let r = Imgmeta.Reader.of_bytes data in let found = ref [] in Imgmeta.Formats.Isobmff.walk_top r (fun box -> found := box.Imgmeta.Formats.Isobmff.kind :: !found); Alcotest.(check (list string)) "two boxes" [ "free"; "ftyp" ] !found ;; let test_isobmff_find_top () = let a = isobmff_box "ftyp" (Bytes.of_string "heic") in let b = isobmff_box "free" (Bytes.of_string "xx") in let data = Bytes.cat a b in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Isobmff.find_top r "free" with | None -> Alcotest.fail "expected free box" | Some box -> Alcotest.(check string) "found free" "free" box.kind ;; let isobmff_full_box ty body = let inner = Bytes.create (4 + Bytes.length body) in Bytes.fill inner 0 4 '\x00'; Bytes.blit body 0 inner 4 (Bytes.length body); isobmff_box ty inner ;; let ispe_body ~width ~height = let b = Bytes.create 8 in Bytes.set_int32_be b 0 (Int32.of_int width); Bytes.set_int32_be b 4 (Int32.of_int height); b ;; let pixi_body ~depth = let b = Bytes.create 4 in Bytes.set_uint8 b 0 3; Bytes.set_uint8 b 1 depth; Bytes.set_uint8 b 2 depth; Bytes.set_uint8 b 3 depth; b ;; let heif_file ~width ~height ~depth = let ftyp = isobmff_box "ftyp" (Bytes.of_string "heic\x00\x00\x00\x00mif1") in let ispe = isobmff_full_box "ispe" (ispe_body ~width ~height) in let pixi = isobmff_full_box "pixi" (pixi_body ~depth) in let ipco = isobmff_box "ipco" (Bytes.cat ispe pixi) in let iprp = isobmff_box "iprp" ipco in let meta = isobmff_full_box "meta" iprp in Bytes.cat ftyp meta ;; let heif_file_with_irot ~width ~height ~depth ~irot = let ftyp = isobmff_box "ftyp" (Bytes.of_string "heic\x00\x00\x00\x00mif1") in let ispe = isobmff_full_box "ispe" (ispe_body ~width ~height) in let pixi = isobmff_full_box "pixi" (pixi_body ~depth) in let irot_box = isobmff_box "irot" (Bytes.make 1 (Char.chr (irot land 0x3))) in let ipco = isobmff_box "ipco" (Bytes.cat (Bytes.cat ispe pixi) irot_box) in let iprp = isobmff_box "iprp" ipco in let meta = isobmff_full_box "meta" iprp in Bytes.cat ftyp meta ;; let test_heif_irot_swap () = let data = heif_file_with_irot ~width:1920 ~height:1080 ~depth:8 ~irot:1 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Heif.read_metadata r with | Ok m -> Alcotest.(check int) "swapped width" 1080 m.width; Alcotest.(check int) "swapped height" 1920 m.height; Alcotest.(check int) "orientation" 8 m.orientation | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_heif_irot_180 () = let data = heif_file_with_irot ~width:1920 ~height:1080 ~depth:8 ~irot:2 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Heif.read_metadata r with | Ok m -> Alcotest.(check int) "unswapped width" 1920 m.width; Alcotest.(check int) "orientation" 3 m.orientation | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let iinf_body entries = let inner = Buffer.create 16 in let count_bytes = Bytes.create 2 in Bytes.set_uint16_be count_bytes 0 (List.length entries); Buffer.add_bytes inner count_bytes; List.iter (fun (id, ty, name) -> let payload = Buffer.create 16 in let v_and_flags = Bytes.create 4 in Bytes.set_uint8 v_and_flags 0 2; Buffer.add_bytes payload v_and_flags; let id_bytes = Bytes.create 2 in Bytes.set_uint16_be id_bytes 0 id; Buffer.add_bytes payload id_bytes; Buffer.add_bytes payload (Bytes.create 2); Buffer.add_string payload ty; Buffer.add_string payload name; Buffer.add_char payload '\x00'; Buffer.add_bytes inner (isobmff_box "infe" (Buffer.to_bytes payload))) entries; Buffer.to_bytes inner ;; let iloc_body_v0 ~items = let buf = Buffer.create 32 in Buffer.add_char buf (Char.chr 0x44); Buffer.add_char buf '\x00'; let count_bytes = Bytes.create 2 in Bytes.set_uint16_be count_bytes 0 (List.length items); Buffer.add_bytes buf count_bytes; List.iter (fun (id, offset, length) -> let id_bytes = Bytes.create 2 in Bytes.set_uint16_be id_bytes 0 id; Buffer.add_bytes buf id_bytes; Buffer.add_bytes buf (Bytes.create 2); let ec_bytes = Bytes.create 2 in Bytes.set_uint16_be ec_bytes 0 1; Buffer.add_bytes buf ec_bytes; let off_bytes = Bytes.create 4 in Bytes.set_int32_be off_bytes 0 (Int32.of_int offset); Buffer.add_bytes buf off_bytes; let len_bytes = Bytes.create 4 in Bytes.set_int32_be len_bytes 0 (Int32.of_int length); Buffer.add_bytes buf len_bytes) items; Buffer.to_bytes buf ;; let heif_file_with_exif_item ~width ~height ~depth ~orientation = let ftyp = isobmff_box "ftyp" (Bytes.of_string "heic\x00\x00\x00\x00mif1") in let ispe = isobmff_full_box "ispe" (ispe_body ~width ~height) in let pixi = isobmff_full_box "pixi" (pixi_body ~depth) in let ipco = isobmff_box "ipco" (Bytes.cat ispe pixi) in let iprp = isobmff_box "iprp" ipco in let iinf = isobmff_full_box "iinf" (iinf_body [ 2, "Exif", "Exif" ]) in let tiff = tiff_with_entries ~endian:`LE [ 0x0112, 3, 1, orientation ] in let exif_payload = Bytes.cat (Bytes.create 4) tiff in let exif_len = Bytes.length exif_payload in let placeholder = isobmff_full_box "iloc" (iloc_body_v0 ~items:[ 2, 0, exif_len ]) in let meta_without_iloc = Bytes.cat iinf iprp in let meta_iloc_offset = Bytes.length ftyp + 8 + 4 + Bytes.length meta_without_iloc in let exif_offset = meta_iloc_offset + Bytes.length placeholder in let iloc = isobmff_full_box "iloc" (iloc_body_v0 ~items:[ 2, exif_offset, exif_len ]) in let meta = isobmff_full_box "meta" (Bytes.cat meta_without_iloc iloc) in Bytes.cat (Bytes.cat ftyp meta) exif_payload ;; let test_heif_exif_item_swap () = let data = heif_file_with_exif_item ~width:1920 ~height:1080 ~depth:8 ~orientation:6 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Heif.read_metadata r with | Ok m -> Alcotest.(check int) "swapped width" 1080 m.width; Alcotest.(check int) "swapped height" 1920 m.height; Alcotest.(check int) "orientation" 6 m.orientation | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let heif_file_irot_and_exif ~width ~height ~depth ~irot ~exif_orientation = let ftyp = isobmff_box "ftyp" (Bytes.of_string "heic\x00\x00\x00\x00mif1") in let ispe = isobmff_full_box "ispe" (ispe_body ~width ~height) in let pixi = isobmff_full_box "pixi" (pixi_body ~depth) in let irot_box = isobmff_box "irot" (Bytes.make 1 (Char.chr (irot land 0x3))) in let ipco = isobmff_box "ipco" (Bytes.cat (Bytes.cat ispe pixi) irot_box) in let iprp = isobmff_box "iprp" ipco in let iinf = isobmff_full_box "iinf" (iinf_body [ 2, "Exif", "Exif" ]) in let tiff = tiff_with_entries ~endian:`LE [ 0x0112, 3, 1, exif_orientation ] in let exif_payload = Bytes.cat (Bytes.create 4) tiff in let exif_len = Bytes.length exif_payload in let placeholder = isobmff_full_box "iloc" (iloc_body_v0 ~items:[ 2, 0, exif_len ]) in let meta_without_iloc = Bytes.cat iinf iprp in let meta_iloc_offset = Bytes.length ftyp + 8 + 4 + Bytes.length meta_without_iloc in let exif_offset = meta_iloc_offset + Bytes.length placeholder in let iloc = isobmff_full_box "iloc" (iloc_body_v0 ~items:[ 2, exif_offset, exif_len ]) in let meta = isobmff_full_box "meta" (Bytes.cat meta_without_iloc iloc) in Bytes.cat (Bytes.cat ftyp meta) exif_payload ;; let test_heif_irot_wins_over_exif () = let data = heif_file_irot_and_exif ~width:1920 ~height:1080 ~depth:8 ~irot:2 ~exif_orientation:6 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Heif.read_metadata r with | Ok m -> Alcotest.(check int) "irot 2 wins not exif 6 width" 1920 m.width; Alcotest.(check int) "irot 2 wins not exif 6 orientation" 3 m.orientation | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_heif_synthesized () = let data = heif_file ~width:1920 ~height:1080 ~depth:10 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Heif.read_metadata r with | Ok m -> Alcotest.(check int) "width" 1920 m.width; Alcotest.(check int) "height" 1080 m.height; Alcotest.(check int) "depth" 10 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_heif_fixture () = let data = load_bytes "fixtures/nix.heic" in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Heif.read_metadata r with | Error e -> Alcotest.failf "fixtures/nix.heic failed %a" Imgmeta.pp_error e | Ok m -> Alcotest.(check int) "width" 320 m.width; Alcotest.(check int) "height" 320 m.height; Alcotest.(check int) "depth" 8 m.depth ;; let avif_file ~width ~height ~depth = let ftyp = isobmff_box "ftyp" (Bytes.of_string "avif\x00\x00\x00\x00mif1") in let ispe = isobmff_full_box "ispe" (ispe_body ~width ~height) in let pixi = isobmff_full_box "pixi" (pixi_body ~depth) in let ipco = isobmff_box "ipco" (Bytes.cat ispe pixi) in let iprp = isobmff_box "iprp" ipco in let meta = isobmff_full_box "meta" iprp in Bytes.cat ftyp meta ;; let test_avif_synthesized () = let data = avif_file ~width:800 ~height:600 ~depth:10 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Avif.read_metadata r with | Ok m -> Alcotest.(check int) "width" 800 m.width; Alcotest.(check int) "height" 600 m.height; Alcotest.(check int) "depth" 10 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_avif_rotated_fixture () = match Imgmeta.of_file "fixtures/sandwich.avif" with | Ok m -> Alcotest.(check int) "display width" 4284 m.width; Alcotest.(check int) "display height" 5712 m.height; Alcotest.(check int) "orientation" 6 m.orientation | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_public_of_bytes () = let data = png_header ~width:10 ~height:20 ~depth:8 ~color_type:2 in match Imgmeta.of_bytes data with | Ok m -> Alcotest.(check int) "width" 10 m.width | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_public_of_bytes_exn_raises () = let raised = try let _ = Imgmeta.of_bytes_exn (Bytes.of_string "garbage") in false with | Imgmeta_error _ -> true in Alcotest.(check bool) "raises" true raised ;; let test_public_of_file () = match Imgmeta.of_file "fixtures/nix.png" with | Ok m -> Alcotest.(check int) "w" 320 m.width; Alcotest.(check int) "h" 320 m.height; Alcotest.(check int) "d" 8 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_public_of_in_channel () = In_channel.with_open_bin "fixtures/nix.heic" (fun ic -> match Imgmeta.of_in_channel ic with | Ok m -> Alcotest.(check int) "w" 320 m.width; Alcotest.(check int) "h" 320 m.height | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e) ;; let test_public_detect_format () = let data = png_header ~width:1 ~height:1 ~depth:8 ~color_type:2 in Alcotest.(check (option string)) "detects png" (Some "png") (Option.map Imgmeta.format_to_string (Imgmeta.detect_format data)) ;; let equal_meta a b = a.Imgmeta.format = b.Imgmeta.format && a.width = b.width && a.height = b.height && a.depth = b.depth && a.orientation = b.orientation ;; let load_three_from_path path = let from_file = Imgmeta.of_file_exn path in let from_bytes = Imgmeta.of_bytes_exn (load_bytes path) in let from_chan = In_channel.with_open_bin path Imgmeta.of_in_channel_exn in from_file, from_bytes, from_chan ;; let check_three_equal path = let a, b, c = load_three_from_path path in Alcotest.(check bool) "file equals bytes" true (equal_meta a b); Alcotest.(check bool) "file equals chan" true (equal_meta a c) ;; let test_cross_source_png () = check_three_equal "fixtures/nix.png" let test_cross_source_jpeg () = check_three_equal "fixtures/nix.jpeg" let test_cross_source_heic () = check_three_equal "fixtures/nix.heic" let test_cross_source_avif_rotated () = check_three_equal "fixtures/sandwich.avif" let expect_error name data = match Imgmeta.of_bytes data with | Ok _ -> Alcotest.failf "%s expected error but got ok" name | Error _ -> () ;; let test_negative_unknown () = expect_error "garbage" (Bytes.of_string "this is not an image at all") ;; let test_negative_truncated_png () = let data = png_header ~width:1 ~height:1 ~depth:8 ~color_type:2 in let short = Bytes.sub data 0 10 in expect_error "png truncated" short ;; let test_negative_malformed_jpeg () = expect_error "jpeg bad marker" (Bytes.of_string "\xff\xd8\xab\xcd") ;; let test_negative_truncated_webp () = expect_error "webp truncated" (Bytes.of_string "RIFF\x00") ;; let test_negative_truncated_heif () = expect_error "heif truncated" (Bytes.of_string "\x00\x00\x00\x18ftyphei") ;; let png_with_raw_chunk ~ty ~declared_len ~payload = let buf = Buffer.create 96 in Buffer.add_bytes buf (png_header ~width:8 ~height:8 ~depth:8 ~color_type:2); let len_bytes = Bytes.create 4 in Bytes.set_int32_be len_bytes 0 (Int32.of_int declared_len); Buffer.add_bytes buf len_bytes; Buffer.add_string buf ty; Buffer.add_string buf payload; Buffer.to_bytes buf ;; let webp_with_raw_chunk ~ty ~declared_len ~payload = let body = Buffer.create 64 in Buffer.add_string body "WEBP"; Buffer.add_string body ty; let len_bytes = Bytes.create 4 in Bytes.set_int32_le len_bytes 0 (Int32.of_int declared_len); Buffer.add_bytes body len_bytes; Buffer.add_string body payload; let out = Buffer.create 96 in Buffer.add_string out "RIFF"; let size = Bytes.create 4 in Bytes.set_int32_le size 0 (Int32.of_int (Buffer.length body)); Buffer.add_bytes out size; Buffer.add_buffer out body; Buffer.to_bytes out ;; (* these two would spin forever before the walkers required forward progress. a regression shows up as the suite hanging rather than as a failed check *) let test_hardening_png_negative_chunk_length () = let data = png_with_raw_chunk ~ty:"junk" ~declared_len:(-12) ~payload:(String.make 16 '\x00') in match Imgmeta.of_bytes data with | Ok m -> Alcotest.(check int) "width still read" 8 m.width | Error _ -> () ;; let test_hardening_webp_negative_chunk_length () = let data = webp_with_raw_chunk ~ty:"JUNK" ~declared_len:(-8) ~payload:(String.make 16 '\x00') in match Imgmeta.of_bytes data with | Ok _ -> Alcotest.fail "expected an error for a negative riff chunk length" | Error _ -> () ;; (* a length from the file used to reach Bytes.sub and Bytes.create unchecked and raise Invalid_argument out of an API that returns a result *) let test_hardening_png_negative_exif_length () = let data = png_with_raw_chunk ~ty:"eXIf" ~declared_len:(-1) ~payload:(String.make 16 '\x00') in let outcome = try `Returned (Imgmeta.of_bytes data) with | e -> `Raised (Printexc.to_string e) in match outcome with | `Raised msg -> Alcotest.failf "of_bytes raised %s" msg | `Returned (Error _) -> () | `Returned (Ok m) -> Alcotest.(check int) "width still read" 8 m.width ;; let test_hardening_png_oversized_exif_length () = let data = png_with_raw_chunk ~ty:"eXIf" ~declared_len:(256 * 1024 * 1024) ~payload:(String.make 16 '\x00') in let outcome = try `Returned (Imgmeta.of_bytes data) with | e -> `Raised (Printexc.to_string e) in match outcome with | `Raised msg -> Alcotest.failf "of_bytes raised %s" msg | `Returned (Error _) -> () | `Returned (Ok m) -> Alcotest.(check int) "width still read" 8 m.width; Alcotest.(check int) "orientation falls back" 1 m.orientation ;; let test_public_of_in_channel_pipe () = let read_fd, write_fd = Unix.pipe () in let data = png_header ~width:64 ~height:32 ~depth:8 ~color_type:2 in let oc = Unix.out_channel_of_descr write_fd in Out_channel.output_bytes oc data; Out_channel.close oc; let ic = Unix.in_channel_of_descr read_fd in Fun.protect ~finally:(fun () -> In_channel.close ic) (fun () -> match Imgmeta.of_in_channel ic with | Ok m -> Alcotest.(check int) "width over a pipe" 64 m.width; Alcotest.(check int) "height over a pipe" 32 m.height | Error e -> Alcotest.failf "non-seekable channel %a" Imgmeta.pp_error e) ;; let test_hardening_of_file_survives_bad_length () = let path = Filename.temp_file "imgmeta_test" ".png" in Fun.protect ~finally:(fun () -> Sys.remove path) (fun () -> let data = png_with_raw_chunk ~ty:"eXIf" ~declared_len:(-1) ~payload:(String.make 16 '\x00') in Out_channel.with_open_bin path (fun oc -> Out_channel.output_bytes oc data); let outcome = try `Returned (Imgmeta.of_file path) with | e -> `Raised (Printexc.to_string e) in match outcome with | `Raised msg -> Alcotest.failf "of_file raised %s" msg | `Returned _ -> ()) ;; let ftyp_box ~major ~compatible = Bytes.of_string (major ^ "\x00\x00\x00\x00" ^ String.concat "" compatible) |> isobmff_box "ftyp" ;; let test_magic_avif_compatible_brand () = let data = ftyp_box ~major:"mif1" ~compatible:[ "mif1"; "avif"; "miaf" ] in Alcotest.(check (option string)) "avif named only in the compatible brands" (Some "avif") (Option.map Imgmeta.format_to_string (Imgmeta.Magic.of_bytes data)) ;; let test_magic_heif_compatible_brand () = let data = ftyp_box ~major:"mif1" ~compatible:[ "mif1"; "heic" ] in Alcotest.(check (option string)) "heif without an avif brand" (Some "heif") (Option.map Imgmeta.format_to_string (Imgmeta.Magic.of_bytes data)) ;; let pitm_body ~item_id = let b = Bytes.create 2 in Bytes.set_uint16_be b 0 item_id; b ;; let ipma_body ~item_id ~indices = let buf = Buffer.create 16 in let count = Bytes.create 4 in Bytes.set_int32_be count 0 1l; Buffer.add_bytes buf count; let id = Bytes.create 2 in Bytes.set_uint16_be id 0 item_id; Buffer.add_bytes buf id; Buffer.add_char buf (Char.chr (List.length indices)); List.iter (fun i -> Buffer.add_char buf (Char.chr i)) indices; Buffer.to_bytes buf ;; (* a thumbnail listed ahead of the primary item is exactly the shape that made the first ispe under ipco the wrong answer *) let heif_file_with_thumbnail ~thumb ~primary ~depth = let ftyp = ftyp_box ~major:"heic" ~compatible:[ "mif1"; "heic" ] in let tw, th = thumb in let pw, ph = primary in let ispe_thumb = isobmff_full_box "ispe" (ispe_body ~width:tw ~height:th) in let ispe_primary = isobmff_full_box "ispe" (ispe_body ~width:pw ~height:ph) in let pixi = isobmff_full_box "pixi" (pixi_body ~depth) in let ipco = isobmff_box "ipco" (Bytes.concat Bytes.empty [ ispe_thumb; ispe_primary; pixi ]) in let ipma = isobmff_full_box "ipma" (ipma_body ~item_id:2 ~indices:[ 0x82; 0x03 ]) in let iprp = isobmff_box "iprp" (Bytes.cat ipco ipma) in let pitm = isobmff_full_box "pitm" (pitm_body ~item_id:2) in let meta = isobmff_full_box "meta" (Bytes.cat pitm iprp) in Bytes.cat ftyp meta ;; let test_heif_ipma_selects_primary_item () = let data = heif_file_with_thumbnail ~thumb:(160, 120) ~primary:(4032, 3024) ~depth:8 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Heif.read_metadata r with | Ok m -> Alcotest.(check int) "primary width not the thumbnail" 4032 m.width; Alcotest.(check int) "primary height not the thumbnail" 3024 m.height; Alcotest.(check int) "depth" 8 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let webp_vp8_lossy ~width ~height = let body = Bytes.create 10 in Bytes.fill body 0 10 '\x00'; Bytes.set_uint8 body 3 0x9d; Bytes.set_uint8 body 4 0x01; Bytes.set_uint8 body 5 0x2a; Bytes.set_uint16_le body 6 (width land 0x3fff); Bytes.set_uint16_le body 8 (height land 0x3fff); riff_header [ "VP8 ", body ] ;; let test_webp_vp8_lossy () = let data = webp_vp8_lossy ~width:320 ~height:240 in let r = Imgmeta.Reader.of_bytes data in match Imgmeta.Formats.Webp.read_metadata r with | Ok m -> Alcotest.(check int) "width" 320 m.width; Alcotest.(check int) "height" 240 m.height; Alcotest.(check int) "depth" 8 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let tiff_type_short = 3 let tiff_type_long = 4 (* entries are (tag, type, count, value). a SHORT value sits in the first two bytes of the four byte field under both byte orders *) let tiff_file ~endian ~entries ~trailing = let n = List.length entries in let head_len = 8 + 2 + (n * 12) + 4 in let buf = Bytes.make (head_len + Bytes.length trailing) '\x00' in let set_u16 = if endian = `LE then Bytes.set_uint16_le else Bytes.set_uint16_be in let set_u32 b o v = if endian = `LE then Bytes.set_int32_le b o (Int32.of_int v) else Bytes.set_int32_be b o (Int32.of_int v) in Bytes.blit_string (if endian = `LE then "II" else "MM") 0 buf 0 2; set_u16 buf 2 0x002a; set_u32 buf 4 8; set_u16 buf 8 n; List.iteri (fun i (tag, ty, count, value) -> let off = 10 + (i * 12) in set_u16 buf off tag; set_u16 buf (off + 2) ty; set_u32 buf (off + 4) count; if ty = tiff_type_short then set_u16 buf (off + 8) value else set_u32 buf (off + 8) value) entries; set_u32 buf (10 + (n * 12)) 0; Bytes.blit trailing 0 buf head_len (Bytes.length trailing); buf ;; let tiff_simple ~endian ~width ~height ~bits = tiff_file ~endian ~entries: [ 0x0100, tiff_type_long, 1, width ; 0x0101, tiff_type_long, 1, height ; 0x0102, tiff_type_short, 1, bits ] ~trailing:Bytes.empty ;; let test_tiff_little_endian () = let r = Imgmeta.Reader.of_bytes (tiff_simple ~endian:`LE ~width:640 ~height:400 ~bits:8) in match Imgmeta.Formats.Tiff.read_metadata r with | Ok m -> Alcotest.(check int) "width" 640 m.width; Alcotest.(check int) "height" 400 m.height; Alcotest.(check int) "depth" 8 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_tiff_big_endian () = let r = Imgmeta.Reader.of_bytes (tiff_simple ~endian:`BE ~width:1024 ~height:768 ~bits:16) in match Imgmeta.Formats.Tiff.read_metadata r with | Ok m -> Alcotest.(check int) "width" 1024 m.width; Alcotest.(check int) "height" 768 m.height; Alcotest.(check int) "depth" 16 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; (* three samples do not fit the value field, which then holds an offset. this is what an ordinary rgb tiff writes *) let test_tiff_out_of_line_bits_per_sample () = let trailing = Bytes.create 6 in List.iter (fun i -> Bytes.set_uint16_le trailing (i * 2) 16) [ 0; 1; 2 ]; let entries = [ 0x0100, tiff_type_long, 1, 640 ; 0x0101, tiff_type_long, 1, 400 ; 0x0102, tiff_type_short, 3, 8 + 2 + (3 * 12) + 4 ] in let r = Imgmeta.Reader.of_bytes (tiff_file ~endian:`LE ~entries ~trailing) in match Imgmeta.Formats.Tiff.read_metadata r with | Ok m -> Alcotest.(check int) "depth from the out of line array" 16 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_tiff_orientation_swap () = let entries = [ 0x0100, tiff_type_long, 1, 400 ; 0x0101, tiff_type_long, 1, 640 ; 0x0102, tiff_type_short, 1, 8 ; 0x0112, tiff_type_short, 1, 6 ] in let r = Imgmeta.Reader.of_bytes (tiff_file ~endian:`LE ~entries ~trailing:Bytes.empty) in match Imgmeta.Formats.Tiff.read_metadata r with | Ok m -> Alcotest.(check int) "swapped width" 640 m.width; Alcotest.(check int) "swapped height" 400 m.height; Alcotest.(check int) "orientation" 6 m.orientation | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let bmp_info ~width ~height ~bits = let buf = Bytes.make 54 '\x00' in Bytes.blit_string "BM" 0 buf 0 2; Bytes.set_int32_le buf 2 (Int32.of_int 54); Bytes.set_int32_le buf 10 (Int32.of_int 54); Bytes.set_int32_le buf 14 (Int32.of_int 40); Bytes.set_int32_le buf 18 (Int32.of_int width); Bytes.set_int32_le buf 22 (Int32.of_int height); Bytes.set_uint16_le buf 26 1; Bytes.set_uint16_le buf 28 bits; buf ;; let bmp_core ~width ~height ~bits = let buf = Bytes.make 26 '\x00' in Bytes.blit_string "BM" 0 buf 0 2; Bytes.set_int32_le buf 2 (Int32.of_int 26); Bytes.set_int32_le buf 10 (Int32.of_int 26); Bytes.set_int32_le buf 14 (Int32.of_int 12); Bytes.set_uint16_le buf 18 width; Bytes.set_uint16_le buf 20 height; Bytes.set_uint16_le buf 22 1; Bytes.set_uint16_le buf 24 bits; buf ;; let test_bmp_info_header () = let r = Imgmeta.Reader.of_bytes (bmp_info ~width:640 ~height:400 ~bits:24) in match Imgmeta.Formats.Bmp.read_metadata r with | Ok m -> Alcotest.(check int) "width" 640 m.width; Alcotest.(check int) "height" 400 m.height; Alcotest.(check int) "bits per channel from 24 bpp" 8 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; (* a negative height marks a top-down bitmap and must not flip the sign of the reported dimension *) let test_bmp_top_down () = let r = Imgmeta.Reader.of_bytes (bmp_info ~width:64 ~height:(-32) ~bits:32) in match Imgmeta.Formats.Bmp.read_metadata r with | Ok m -> Alcotest.(check int) "width" 64 m.width; Alcotest.(check int) "height" 32 m.height | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_bmp_core_header () = let r = Imgmeta.Reader.of_bytes (bmp_core ~width:16 ~height:8 ~bits:1) in match Imgmeta.Formats.Bmp.read_metadata r with | Ok m -> Alcotest.(check int) "width" 16 m.width; Alcotest.(check int) "height" 8 m.height; Alcotest.(check int) "depth" 1 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let ico_file entries = let n = List.length entries in let buf = Bytes.make (6 + (n * 16)) '\x00' in Bytes.set_uint16_le buf 2 1; Bytes.set_uint16_le buf 4 n; List.iteri (fun i (w, h, bits) -> let off = 6 + (i * 16) in Bytes.set_uint8 buf off (w land 0xff); Bytes.set_uint8 buf (off + 1) (h land 0xff); Bytes.set_uint16_le buf (off + 6) bits) entries; buf ;; let test_ico_largest_entry () = let r = Imgmeta.Reader.of_bytes (ico_file [ 16, 16, 32; 48, 48, 32; 32, 32, 32 ]) in match Imgmeta.Formats.Ico.read_metadata r with | Ok m -> Alcotest.(check int) "largest width" 48 m.width; Alcotest.(check int) "largest height" 48 m.height; Alcotest.(check int) "bits per channel from 32 bpp" 8 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; (* a stored dimension of zero means 256, the largest an icon entry can describe *) let test_ico_256_is_encoded_as_zero () = let r = Imgmeta.Reader.of_bytes (ico_file [ 16, 16, 32; 256, 256, 32 ]) in match Imgmeta.Formats.Ico.read_metadata r with | Ok m -> Alcotest.(check int) "width" 256 m.width; Alcotest.(check int) "height" 256 m.height | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let qoi_header ~width ~height ~channels = let buf = Bytes.make 14 '\x00' in Bytes.blit_string "qoif" 0 buf 0 4; Bytes.set_int32_be buf 4 (Int32.of_int width); Bytes.set_int32_be buf 8 (Int32.of_int height); Bytes.set_uint8 buf 12 channels; buf ;; let test_qoi_header () = let r = Imgmeta.Reader.of_bytes (qoi_header ~width:800 ~height:600 ~channels:4) in match Imgmeta.Formats.Qoi.read_metadata r with | Ok m -> Alcotest.(check int) "width" 800 m.width; Alcotest.(check int) "height" 600 m.height; Alcotest.(check int) "depth" 8 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; (* real cjxl 0.12.0 output for a 96x48 image, kept as bytes because a hand written bit stream would only test this parser against its own encoder *) let jxl_raw_hex = "ff 0a cb 07 00 13 88 02 00 fc 00 b5 9f 20 00 00 15 2a a3 8c 1b bc 9c eb f9 f2 43 87 \ c5 b4 8d eb 0c 6d b5 6d 61 09 63 b3 bd 30 48 08 80 1a 31 c6 e3 c5 c8 46 93 78 60 00 \ 08 0c 00 86 30 e9 34 21 00 88 02 a8 e1 68 78 95 24 00" ;; let jxl_container_hex = "00 00 00 0c 4a 58 4c 20 0d 0a 87 0a 00 00 00 14 66 74 79 70 6a 78 6c 20 00 00 00 00 \ 6a 78 6c 20 00 00 00 52 6a 78 6c 63 ff 0a cb 07 00 13 88 02 00 fc 00 b5 9f 20 00 00 \ 15 2a a3 8c 1b bc 9c eb f9 f2 43 87 c5 b4 8d eb 0c 6d b5 6d 61 09 63 b3 bd 30 48 08 \ 80 1a 31 c6 e3 c5 c8 46 93 78 60 00 08 0c 00 86 30 e9 34 21 00 88 02 a8 e1 68 78 95 \ 24 00" ;; let check_jxl name hex = let r = Imgmeta.Reader.of_bytes (bytes_of_hex hex) in match Imgmeta.Formats.Jxl.read_metadata r with | Ok m -> Alcotest.(check int) (name ^ " width") 96 m.width; Alcotest.(check int) (name ^ " height") 48 m.height; Alcotest.(check int) (name ^ " depth") 8 m.depth | Error e -> Alcotest.failf "%s %a" name Imgmeta.pp_error e ;; (* an animation sets extra_fields, which puts the intrinsic size, preview and animation headers between the size header and the bit depth. cjxl 0.12.0 output for a three frame 120x60 animation, truncated to the header window *) let jxl_animation_hex = "ff 0a d8 71 41 00 12 8a 4b 02 00 13 50 01 10 00 24 01 67 13 28 01 00 50 a1 32 ca b8 \ c1 cb b9 9e 2f 3f 74 58 4c db b8 ce d0 56 db 16 96 24 8a 8c 63 93 74 02 87 90 02 3b \ 10 27 4e 8c 71 e2 4c 0c" ;; let test_jxl_animation_reaches_bit_depth () = let r = Imgmeta.Reader.of_bytes (bytes_of_hex jxl_animation_hex) in match Imgmeta.Formats.Jxl.read_metadata r with | Ok m -> Alcotest.(check int) "width" 120 m.width; Alcotest.(check int) "height" 60 m.height; Alcotest.(check int) "depth read past the optional headers" 8 m.depth | Error e -> Alcotest.failf "%a" Imgmeta.pp_error e ;; let test_jxl_bare_codestream () = check_jxl "bare codestream" jxl_raw_hex let test_jxl_container () = check_jxl "container" jxl_container_hex let test_magic_new_formats () = let check name data expected = Alcotest.(check (option string)) name (Some (Imgmeta.format_to_string expected)) (Option.map Imgmeta.format_to_string (Imgmeta.Magic.of_bytes data)) in check "tiff le" (tiff_simple ~endian:`LE ~width:8 ~height:8 ~bits:8) TIFF; check "tiff be" (tiff_simple ~endian:`BE ~width:8 ~height:8 ~bits:8) TIFF; check "bmp" (bmp_info ~width:8 ~height:8 ~bits:24) BMP; check "ico" (ico_file [ 16, 16, 32 ]) ICO; check "qoi" (qoi_header ~width:8 ~height:8 ~channels:3) QOI; check "jxl bare" (bytes_of_hex jxl_raw_hex) JXL; check "jxl container" (bytes_of_hex jxl_container_hex) JXL ;; (* an all zero directory count is the shape most likely to collide with the weak ico signature *) let test_magic_rejects_empty_ico_directory () = Alcotest.(check (option string)) "an empty directory is not an icon" None (Option.map Imgmeta.format_to_string (Imgmeta.Magic.of_bytes (bytes_of_hex "00 00 01 00 00 00 00 00"))) ;; let () = Alcotest.run "imgmeta" [ ( "types" , [ Alcotest.test_case "format constructors" `Quick test_format_construction ; Alcotest.test_case "record fields" `Quick test_record_fields ; Alcotest.test_case "pp_error unknown_format" `Quick test_pp_error_unknown ; Alcotest.test_case "exception carries error" `Quick test_exception_carries_error ] ) ; ( "reader" , [ Alcotest.test_case "bytes read" `Quick test_reader_bytes_read ; Alcotest.test_case "bytes read_at" `Quick test_reader_bytes_read_at ; Alcotest.test_case "bytes size" `Quick test_reader_bytes_size ; Alcotest.test_case "bytes truncated" `Quick test_reader_bytes_truncated ; Alcotest.test_case "file read" `Quick test_reader_file_read ; Alcotest.test_case "in_channel read" `Quick test_reader_in_channel_read ] ) ; ( "magic" , [ Alcotest.test_case "png" `Quick test_magic_png ; Alcotest.test_case "jpeg" `Quick test_magic_jpeg ; Alcotest.test_case "gif" `Quick test_magic_gif ; Alcotest.test_case "webp" `Quick test_magic_webp ; Alcotest.test_case "heif" `Quick test_magic_heif ; Alcotest.test_case "avif" `Quick test_magic_avif ; Alcotest.test_case "unknown" `Quick test_magic_unknown ; Alcotest.test_case "new format signatures" `Quick test_magic_new_formats ; Alcotest.test_case "empty ico directory rejected" `Quick test_magic_rejects_empty_ico_directory ; Alcotest.test_case "avif via compatible brand" `Quick test_magic_avif_compatible_brand ; Alcotest.test_case "heif via compatible brand" `Quick test_magic_heif_compatible_brand ] ) ; ( "png" , [ Alcotest.test_case "synthesized 8 bit rgb" `Quick test_png_synthesized ; Alcotest.test_case "synthesized 16 bit rgba" `Quick test_png_synthesized_16bit ; Alcotest.test_case "fixture 320x320 rgba" `Quick test_png_fixture ; Alcotest.test_case "orientation 6 swap" `Quick test_png_orientation_swap ; Alcotest.test_case "orientation 3 no swap" `Quick test_png_orientation_no_swap ] ) ; "gif", [ Alcotest.test_case "synthesized 64x48" `Quick test_gif_synthesized ] ; ( "jpeg" , [ Alcotest.test_case "synthesized 320x240" `Quick test_jpeg_synthesized ; Alcotest.test_case "fixture 320x320 with exif" `Quick test_jpeg_fixture ; Alcotest.test_case "orientation 6 swap" `Quick test_jpeg_orientation_swap ; Alcotest.test_case "orientation 2 no swap" `Quick test_jpeg_orientation_no_swap ] ) ; ( "webp" , [ Alcotest.test_case "synthesized vp8x 512x256" `Quick test_webp_vp8x ; Alcotest.test_case "synthesized vp8l 16x16" `Quick test_webp_vp8l ; Alcotest.test_case "orientation 6 swap" `Quick test_webp_orientation_swap ; Alcotest.test_case "orientation 2 no swap" `Quick test_webp_orientation_no_swap ; Alcotest.test_case "synthesized vp8 lossy 320x240" `Quick test_webp_vp8_lossy ] ) ; ( "isobmff" , [ Alcotest.test_case "walk top level" `Quick test_isobmff_walk_top_level ; Alcotest.test_case "find top" `Quick test_isobmff_find_top ] ) ; ( "heif" , [ Alcotest.test_case "synthesized 1920x1080 10bit" `Quick test_heif_synthesized ; Alcotest.test_case "fixture 320x320 8bit" `Quick test_heif_fixture ; Alcotest.test_case "irot 1 90 ccw swap" `Quick test_heif_irot_swap ; Alcotest.test_case "irot 2 180 no swap" `Quick test_heif_irot_180 ; Alcotest.test_case "exif item swap" `Quick test_heif_exif_item_swap ; Alcotest.test_case "irot wins over exif" `Quick test_heif_irot_wins_over_exif ; Alcotest.test_case "ipma selects the primary item" `Quick test_heif_ipma_selects_primary_item ] ) ; ( "avif" , [ Alcotest.test_case "synthesized 800x600 10bit" `Quick test_avif_synthesized ; Alcotest.test_case "rotated fixture orientation 6" `Quick test_avif_rotated_fixture ] ) ; ( "exif" , [ Alcotest.test_case "le orientation 6" `Quick test_exif_orientation_le ; Alcotest.test_case "be orientation 8" `Quick test_exif_orientation_be ; Alcotest.test_case "missing tag" `Quick test_exif_missing_orientation ; Alcotest.test_case "invalid magic" `Quick test_exif_invalid_magic ; Alcotest.test_case "empty bytes" `Quick test_exif_empty ; Alcotest.test_case "out of range" `Quick test_exif_out_of_range ] ) ; ( "public" , [ Alcotest.test_case "of_bytes png" `Quick test_public_of_bytes ; Alcotest.test_case "of_bytes_exn raises" `Quick test_public_of_bytes_exn_raises ; Alcotest.test_case "of_file fixture png" `Quick test_public_of_file ; Alcotest.test_case "of_in_channel fixture heic" `Quick test_public_of_in_channel ; Alcotest.test_case "detect_format png" `Quick test_public_detect_format ; Alcotest.test_case "of_in_channel over a pipe" `Quick test_public_of_in_channel_pipe ] ) ; ( "cross_source" , [ Alcotest.test_case "png all three equal" `Quick test_cross_source_png ; Alcotest.test_case "jpeg all three equal" `Quick test_cross_source_jpeg ; Alcotest.test_case "heic all three equal" `Quick test_cross_source_heic ; Alcotest.test_case "rotated avif all three equal" `Quick test_cross_source_avif_rotated ] ) ; ( "negative" , [ Alcotest.test_case "unknown garbage" `Quick test_negative_unknown ; Alcotest.test_case "png truncated" `Quick test_negative_truncated_png ; Alcotest.test_case "jpeg malformed marker" `Quick test_negative_malformed_jpeg ; Alcotest.test_case "webp truncated" `Quick test_negative_truncated_webp ; Alcotest.test_case "heif truncated" `Quick test_negative_truncated_heif ] ) ; ( "tiff" , [ Alcotest.test_case "little endian 640x400" `Quick test_tiff_little_endian ; Alcotest.test_case "big endian 1024x768 16 bit" `Quick test_tiff_big_endian ; Alcotest.test_case "out of line bits per sample" `Quick test_tiff_out_of_line_bits_per_sample ; Alcotest.test_case "orientation 6 swap" `Quick test_tiff_orientation_swap ] ) ; ( "jxl" , [ Alcotest.test_case "bare codestream 96x48" `Quick test_jxl_bare_codestream ; Alcotest.test_case "isobmff container 96x48" `Quick test_jxl_container ; Alcotest.test_case "animation reaches the bit depth" `Quick test_jxl_animation_reaches_bit_depth ] ) ; ( "bmp" , [ Alcotest.test_case "info header 640x400 24bpp" `Quick test_bmp_info_header ; Alcotest.test_case "top down negative height" `Quick test_bmp_top_down ; Alcotest.test_case "core header 16x8 1bpp" `Quick test_bmp_core_header ] ) ; ( "ico" , [ Alcotest.test_case "largest directory entry wins" `Quick test_ico_largest_entry ; Alcotest.test_case "256 encoded as zero" `Quick test_ico_256_is_encoded_as_zero ] ) ; "qoi", [ Alcotest.test_case "header 800x600" `Quick test_qoi_header ] ; ( "hardening" , [ Alcotest.test_case "png negative chunk length" `Quick test_hardening_png_negative_chunk_length ; Alcotest.test_case "webp negative chunk length" `Quick test_hardening_webp_negative_chunk_length ; Alcotest.test_case "png negative exif length" `Quick test_hardening_png_negative_exif_length ; Alcotest.test_case "png oversized exif length" `Quick test_hardening_png_oversized_exif_length ; Alcotest.test_case "of_file survives a bad length" `Quick test_hardening_of_file_survives_bad_length ] ) ] ;;