dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / platform / tests_image_codec_plugin_luce.lucb
#==============================================================================================
#
# tests_image_codec_plugin_luce - the ImageCodecPlugin over luce-gif, luce-bmp and luce-ico:
# fixtures, animated GIFs streamed to AnimatedDecodedImageData, icon entries, errors
#
# DESCRIPTION:
# Ladybird's Tests/LibGfx/TestImageDecoder.cpp cases for BMP, ICO, CUR and GIF, run
# through the plugin on LibGfx's test inputs (tests/libgfx, without the 590 KB
# top-down.bmp, for which a small top-down bitmap stands in), and synthetic images
# written by the local oracle's gen_cases.py (luce-browser-tools/oracles/
# luce-browser-engine/image_codecs): a six-frame GIF on a 2x1 canvas exercising every
# disposal, a transparent index and browser delays; icons with a PNG and a BMP entry and
# with three BMP entries; a BMP holding a PNG. Every frame's pixels are checked as an
# FNV-1a hash of its premultiplied BGRA8888 pixels against what Ladybird's ImageDecoder
# makes of the same file (the oracle's expected.txt), except where we differ on
# purpose: the loop count is the plays browsers make (Ladybird answers the count
# written), an icon's largest entry is chosen even when it has no more bits than a
# smaller one, and a BMP holding a PNG decodes.
#
#==============================================================================================
# mark: Helpers =================================================================================
## The bytes of a LibGfx test input (tests/libgfx/test-inputs).
func image_codec_test_input(path: str) -> ak.ByteBuffer:
var full = ak.string_builder_new()
ak.string_builder_append(&full, "tests/libgfx/test-inputs/")
ak.string_builder_append(&full, path)
return core_mapped_file_map(ak.string_builder_string_view(&full)) else trap("the test input reads")
## The FNV-1a hash of a bitmap's pixels, each premultiplied BGRA8888 pixel's bytes from the lowest (the oracle's hash).
func image_codec_test_hash(bitmap: const gfx.Bitmap*) -> u32:
var hash: u32 = 2166136261
for y: i32 in 0..<gfx.bitmap_height(bitmap):
let row = gfx.bitmap_scanline_const(bitmap, y) else trap("a row")
for x in 0..<(usize)gfx.bitmap_width(bitmap):
for byte: u32 in 0..<4:
hash ^= (row[x] >> (8 * byte)) & 0xff
hash = hash *% 16777619
return hash
## The pixel at (x, y) of a bitmap (premultiplied BGRA8888, as a u32).
func image_codec_test_pixel(bitmap: const gfx.Bitmap*, x: i32, y: i32) -> u32:
let row = gfx.bitmap_scanline_const(bitmap, y) else trap("a row")
return row[(usize)x]
## Frame `index` of a decoded image.
func image_codec_test_frame(image: const PlatformDecodedImage*, index: usize) -> gfx.Bitmap*:
return ak.vector_at_const(&image.frames, index).bitmap else trap("a decoded frame has a bitmap")
## Whether frame `index` of a decoded image is `width` x `height` with the pixel hash `hash`.
func image_codec_test_frame_is(image: const PlatformDecodedImage*, index: usize, width: i32, height: i32, hash: u32) -> bool:
let bitmap = image_codec_test_frame(image, index)
return gfx.bitmap_width(bitmap) == width and gfx.bitmap_height(bitmap) == height and image_codec_test_hash(bitmap) == hash
## Decodes a still LibGfx test input: one frame, not animated, shown for `duration` ms, played `loop_count` times.
func image_codec_test_still(path: str, duration: u32, loop_count: u32) -> PlatformDecodedImage:
let bytes = image_codec_test_input(path)
let image = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&bytes)) else trap("the test input decodes")
assert(image.frame_count == 1 and not image.is_animated and image.session_id == 0 and image.loop_count == loop_count)
assert(ak.vector_size(&image.frames) == 1 and ak.vector_at_const(&image.frames, 0).duration == (usize)duration)
assert(gfx.bitmap_format(image_codec_test_frame(&image, 0)) == gfx.BitmapFormat.bgra8888)
assert(gfx.bitmap_alpha_type(image_codec_test_frame(&image, 0)) == gfx.AlphaType.premultiplied)
return image
## Whether decoding `bytes` fails with `code`.
func image_codec_test_fails(bytes: const u8[], code: ErrorCode) -> bool:
let image = luce_image_codec_plugin_decode(bytes) catch failure:
return failure.code == code
_ = image
return false
# mark: Synthetic images (the oracle's gen_cases.py) ============================================
## A 2x1 GIF of six 1x1 frames over a palette of black, red, green and blue, looping twice
## (NETSCAPE2.0, its count at bytes 41 and 42): red at (0, 0) kept for 5 hundredths; green at
## (1, 0) cleared after, delay 0; blue at (0, 0) restored after, delay 1; blue at (1, 0), delay
## 20; green at (0, 0), delay 3; the transparent index at (1, 0), delay 7.
let image_codec_test_animation_loop2_gif: u8[183] = [0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x02, 0x00, 0x01, 0x00, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00,
0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x21, 0xFF, 0x0B, 0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E,
0x30, 0x03, 0x01, 0x02, 0x00, 0x00, 0x21, 0xF9, 0x04, 0x04, 0x05, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x4C, 0x01, 0x00, 0x21, 0xF9, 0x04, 0x08, 0x00, 0x00, 0x00, 0x00, 0x2C,
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x54, 0x01, 0x00, 0x21, 0xF9, 0x04, 0x0C, 0x01,
0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x5C, 0x01, 0x00, 0x21,
0xF9, 0x04, 0x04, 0x14, 0x00, 0x00, 0x00, 0x2C, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02,
0x5C, 0x01, 0x00, 0x21, 0xF9, 0x04, 0x04, 0x03, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
0x00, 0x00, 0x02, 0x02, 0x54, 0x01, 0x00, 0x21, 0xF9, 0x04, 0x05, 0x07, 0x00, 0x00, 0x00, 0x2C, 0x01, 0x00, 0x00,
0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3B]
## An icon of a 1x1 32-bit BMP entry (blue 16, green 32, red 48, alpha 128) and a 3x1 PNG entry (loader_test_rgba_png).
let image_codec_test_ico_png_and_bmp_ico: u8[164] = [0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x20, 0x00, 0x30, 0x00, 0x00, 0x00, 0x26,
0x00, 0x00, 0x00, 0x03, 0x01, 0x00, 0x00, 0x01, 0x00, 0x20, 0x00, 0x4E, 0x00, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00,
0x28, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x10, 0x20, 0x30, 0x80, 0x00, 0x00, 0x00, 0x00, 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00,
0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00,
0x00, 0x1B, 0xE0, 0x14, 0xB4, 0x00, 0x00, 0x00, 0x15, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0x38, 0x91, 0x62,
0xD4, 0xC0, 0x25, 0x22, 0xF7, 0xFF, 0xFF, 0xFF, 0xFF, 0x0C, 0x00, 0x23, 0xAF, 0x06, 0x17, 0xF4, 0x45, 0xF0, 0x3C,
0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82]
## An icon of three BMP entries: 2x1 at 24 bits (red, green), 2x1 at 32 bits (blue, white at alpha 128), 1x1 at 32 bits.
let image_codec_test_ico_bmp_entries_ico: u8[206] = [0x00, 0x00, 0x01, 0x00, 0x03, 0x00, 0x02, 0x01, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x34, 0x00, 0x00, 0x00, 0x36,
0x00, 0x00, 0x00, 0x02, 0x01, 0x00, 0x00, 0x01, 0x00, 0x20, 0x00, 0x34, 0x00, 0x00, 0x00, 0x6A, 0x00, 0x00, 0x00,
0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x20, 0x00, 0x30, 0x00, 0x00, 0x00, 0x9E, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00,
0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00]
## A 2x2 24-bit top-down BMP (a negative height): red, green over blue, white.
let image_codec_test_top_down_bmp: u8[70] = [0x42, 0x4D, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x02, 0x00,
0x00, 0x00, 0xFE, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00,
0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00]
## A BI_PNG bitmap holding loader_test_rgba_png.
let image_codec_test_png_in_bmp: u8[132] = [0x42, 0x4D, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x4E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00,
0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1B,
0xE0, 0x14, 0xB4, 0x00, 0x00, 0x00, 0x15, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0x38, 0x91, 0x62, 0xD4, 0xC0, 0x25, 0x22,
0xF7, 0xFF, 0xFF, 0xFF, 0xFF, 0x0C, 0x00, 0x23, 0xAF, 0x06, 0x17, 0xF4, 0x45, 0xF0, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45,
0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82]
## The canvases the animation's frames show, as premultiplied BGRA8888 pixels (Ladybird's agree).
let image_codec_test_animation_canvases: u32[12] = [0xffff0000, 0, 0xffff0000, 0xff00ff00, 0xff0000ff, 0, 0xffff0000, 0xff0000ff, 0xff00ff00, 0xff0000ff,
0xff00ff00, 0xff0000ff]
## The frames' durations as browsers play them (a delay of 0 or 1 hundredths is 100 ms).
let image_codec_test_animation_durations: u32[6] = [50, 100, 100, 200, 30, 70]
## Whether `bitmap` shows canvas `index` of the animation.
func image_codec_test_animation_frame_is(bitmap: const gfx.Bitmap*, index: usize) -> bool:
return (gfx.bitmap_width(bitmap) == 2 and gfx.bitmap_height(bitmap) == 1 and image_codec_test_pixel(bitmap, 0, 0) == image_codec_test_animation_canvases[index * 2] and image_codec_test_pixel(bitmap, 1, 0) == image_codec_test_animation_canvases[index * 2 + 1])
## The animation with the loop extension's count set to `count`.
func image_codec_test_animation_looping(count: u8) -> u8[183]:
var bytes = image_codec_test_animation_loop2_gif
bytes[41] = count
return bytes
## The animation without its loop extension (bytes 25 to 43).
func image_codec_test_animation_without_loop() -> u8[164]:
var bytes: u8[164] = ---
memory.copy(bytes, image_codec_test_animation_loop2_gif, 25)
memory.copy(bytes[25..], image_codec_test_animation_loop2_gif[44..], 139)
return bytes
## LibGfx's test_gif_without_global_color_table: a 1x1 GIF with only a local palette, its pixel red.
let image_codec_test_gif_local_palette: u8[35] = [0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x01, 0x00, 0x80, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x02, 0x02, 0x4C, 0x01, 0x00, 0x3B]
# mark: Choosing the decoder ====================================================================
test "the ImageCodecPlugin: MimeSniff's image patterns choose the decoder; image/bmp, image/gif and both icon types are supported":
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
let gif = image_codec_test_input("gif/download-animation.gif")
assert(luce_image_codec_sniff(ak.byte_buffer_bytes_const(&gif)) == LuceImageCodecFormat.gif)
assert(luce_image_codec_sniff(image_codec_test_gif_local_palette) == LuceImageCodecFormat.gif)
let bmp = image_codec_test_input("bmp/rgba32-1.bmp")
assert(luce_image_codec_sniff(ak.byte_buffer_bytes_const(&bmp)) == LuceImageCodecFormat.bmp)
let icon = image_codec_test_input("ico/serenity.ico")
assert(luce_image_codec_sniff(ak.byte_buffer_bytes_const(&icon)) == LuceImageCodecFormat.ico)
let cursor = image_codec_test_input("cur/cursor.cur")
assert(luce_image_codec_sniff(ak.byte_buffer_bytes_const(&cursor)) == LuceImageCodecFormat.ico)
# LibGfx's test_not_ico: a PNG is no icon.
assert(luce_image_codec_sniff(loader_test_rgba_png) == LuceImageCodecFormat.png)
assert(luce_image_codec_sniff(b"RIFF\x00\x00\x00\x00WEBPVP8 ") == LuceImageCodecFormat.webp)
assert(luce_image_codec_sniff(b"\x00\x00\x00\x1CftypavifXXXX") == LuceImageCodecFormat.avif)
assert(luce_image_codec_sniff(b"\x00\x00\x00\x1Cftypavis") == LuceImageCodecFormat.avif)
assert(luce_image_codec_sniff(b"\x00\x00\x00\x1Cftypmif1") == LuceImageCodecFormat.unknown)
assert(luce_image_codec_sniff(b"not an image") == LuceImageCodecFormat.unknown)
# The declared types reach the same decoders.
assert(luce_image_codec_format_for_essence(ak.sv("image/gif")) == LuceImageCodecFormat.gif)
assert(luce_image_codec_format_for_essence(ak.sv("image/bmp")) == LuceImageCodecFormat.bmp)
assert(luce_image_codec_format_for_essence(ak.sv("image/x-icon")) == LuceImageCodecFormat.ico)
assert(luce_image_codec_format_for_essence(ak.sv("image/vnd.microsoft.icon")) == LuceImageCodecFormat.ico)
assert(luce_image_codec_format_for_essence(ak.sv("Image/VND.Microsoft.Icon")) == LuceImageCodecFormat.ico)
assert(luce_image_codec_format_for_essence(ak.sv("image/svg+xml")) == LuceImageCodecFormat.unknown)
assert(html_is_supported_image_type(ak.sv("image/gif")) and html_is_supported_image_type(ak.sv("image/bmp")))
assert(html_is_supported_image_type(ak.sv("image/x-icon")) and html_is_supported_image_type(ak.sv("image/vnd.microsoft.icon")))
# mark: BMP =====================================================================================
test "the ImageCodecPlugin: BMP files (LibGfx's test_bmp*), a top-down bitmap, a BMP holding a PNG":
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
let rgba = image_codec_test_still("bmp/rgba32-1.bmp", 0, 0)
assert(image_codec_test_frame_is(&rgba, 0, 127, 64, 0xf7e7aba3))
let one_bit = image_codec_test_still("bmp/bitmap.bmp", 0, 0)
assert(image_codec_test_frame_is(&one_bit, 0, 399, 400, 0x03ebbbb5))
assert(image_codec_test_pixel(image_codec_test_frame(&one_bit, 0), 0, 0) == 0xffffffff)
let too_many_colors = image_codec_test_still("bmp/too-many-palette-colors.bmp", 0, 0)
assert(image_codec_test_frame_is(&too_many_colors, 0, 2, 2, 0x50c0069d))
let v4 = image_codec_test_still("bmp/2x2x32_v4.bmp", 0, 0)
assert(image_codec_test_frame_is(&v4, 0, 2, 2, 0x50c0069d))
assert(image_codec_test_pixel(image_codec_test_frame(&v4, 0), 0, 0) == 0xffff0000)
let os2 = image_codec_test_still("bmp/os2_3bpc.bmp", 0, 0)
assert(image_codec_test_frame_is(&os2, 0, 300, 200, 0x29017f4c))
assert(image_codec_test_pixel(image_codec_test_frame(&os2, 0), 150, 100) == 0xff000000)
assert(image_codec_test_pixel(image_codec_test_frame(&os2, 0), 152, 100) == 0xffffffff)
# test_bmp_top_down's file is 590 KB; a 2x2 top-down bitmap: red, green over blue, white.
let top_down = luce_image_codec_plugin_decode(image_codec_test_top_down_bmp) else trap("the top-down BMP decodes")
assert(image_codec_test_frame_is(&top_down, 0, 2, 2, 0x26043aad))
assert(image_codec_test_pixel(image_codec_test_frame(&top_down, 0), 0, 0) == 0xffff0000)
# A BI_PNG bitmap is its PNG (LibGfx finds no decoder for it).
let png_in_bmp = luce_image_codec_plugin_decode(image_codec_test_png_in_bmp) else trap("the BMP's PNG decodes")
let png_bitmap = image_codec_test_frame(&png_in_bmp, 0)
assert(gfx.bitmap_width(png_bitmap) == 3 and image_codec_test_pixel(png_bitmap, 0, 0) == 0x80643219 and image_codec_test_pixel(png_bitmap, 2, 0) == 0)
# Damaged bitmaps fail: one cut short in its headers, one whose BI_PNG holds no PNG.
let truncated = image_codec_test_input("bmp/rgba32-1.bmp")
assert(image_codec_test_fails(ak.byte_buffer_bytes_const(&truncated)[..<20], bmp_codec.corrupt))
var not_png = image_codec_test_png_in_bmp
not_png[54] = 0
assert(image_codec_test_fails(not_png, luce_image_codec_unsupported))
# mark: ICO and CUR =============================================================================
test "the ImageCodecPlugin: icons and cursors (LibGfx's test_cur, test_*ico*), the entry chosen, PNG entries":
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
let cursor = image_codec_test_still("cur/cursor.cur", 0, 0)
assert(image_codec_test_frame_is(&cursor, 0, 32, 32, 0x1dfbece1))
let cursor_bitmap = image_codec_test_frame(&cursor, 0)
assert(image_codec_test_pixel(cursor_bitmap, 0, 0) == 0 and image_codec_test_pixel(cursor_bitmap, 2, 2) == 0xff000000)
assert(image_codec_test_pixel(cursor_bitmap, 8, 8) == 0xffffffff)
let serenity = image_codec_test_still("ico/serenity.ico", 0, 0)
assert(image_codec_test_frame_is(&serenity, 0, 16, 16, 0x13f0fdd2))
assert(image_codec_test_pixel(image_codec_test_frame(&serenity, 0), 0, 0) == 0)
assert(image_codec_test_pixel(image_codec_test_frame(&serenity, 0), 7, 4) == 0xffa10000)
let youtube = image_codec_test_still("ico/yt-favicon.ico", 0, 0)
assert(image_codec_test_frame_is(&youtube, 0, 16, 16, 0x3c919117))
assert(image_codec_test_pixel(image_codec_test_frame(&youtube, 0), 14, 14) == 0xffea0000)
assert(image_codec_test_pixel(image_codec_test_frame(&youtube, 0), 13, 15) == 0xffff0a0f)
let maskless = image_codec_test_still("ico/malformed_maskless.ico", 0, 0)
assert(image_codec_test_frame_is(&maskless, 0, 16, 16, 0x13f0fdd2))
# test_ico_malformed_frame: the directories read, the entries do not decode.
for path in ["ico/oss-fuzz-testcase-62541.ico", "ico/oss-fuzz-testcase-63177.ico", "ico/oss-fuzz-testcase-63357.ico", "bmp/oss-fuzz-testcase-62541.bmp"]:
let bytes = image_codec_test_input(path)
let image: PlatformDecodedImage? = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&bytes)) catch:
recover none
assert(image == none)
# Of 2x1 at 24 bits, 2x1 at 32 bits and 1x1, the 2x1 at 32 bits (blue, half-transparent white).
let entries = luce_image_codec_plugin_decode(image_codec_test_ico_bmp_entries_ico) else trap("the icon decodes")
assert(image_codec_test_frame_is(&entries, 0, 2, 1, 0x0c741bc3))
assert(image_codec_test_pixel(image_codec_test_frame(&entries, 0), 0, 0) == 0xff0000ff)
assert(image_codec_test_pixel(image_codec_test_frame(&entries, 0), 1, 0) == 0x80808080)
# A 1x1 BMP entry and a 3x1 PNG entry of as many bits: the larger, decoded as a PNG (Ladybird keeps
# the first, which has as many bits).
let png_entry = luce_image_codec_plugin_decode(image_codec_test_ico_png_and_bmp_ico) else trap("the icon decodes")
let png_bitmap = image_codec_test_frame(&png_entry, 0)
assert(gfx.bitmap_width(png_bitmap) == 3 and gfx.bitmap_height(png_bitmap) == 1)
assert(image_codec_test_pixel(png_bitmap, 0, 0) == 0x80643219 and image_codec_test_pixel(png_bitmap, 1, 0) == 0xff0a141e)
# A PNG entry whose header cannot be read comes last: the BMP entry is chosen.
var unreadable_png = image_codec_test_ico_png_and_bmp_ico
unreadable_png[100] = 0
let bmp_entry = luce_image_codec_plugin_decode(unreadable_png) else trap("the icon decodes")
assert(image_codec_test_frame_is(&bmp_entry, 0, 1, 1, 0xeec8fc45))
assert(image_codec_test_pixel(image_codec_test_frame(&bmp_entry, 0), 0, 0) == 0x80181008)
# An icon of no images, and one whose chosen PNG entry's pixels are damaged.
assert(image_codec_test_fails(b"\x00\x00\x01\x00\x00\x00", ico_codec.corrupt))
var broken_png = image_codec_test_ico_png_and_bmp_ico
broken_png[131] = 0xFF
let broken: PlatformDecodedImage? = luce_image_codec_plugin_decode(broken_png) catch:
recover none
assert(broken == none)
# mark: GIF =====================================================================================
test "the ImageCodecPlugin: GIF files (LibGfx's test_gif*), every frame composited with its duration and the loop count":
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
# test_gif: three frames of 400 ms, looping forever.
let download_bytes = image_codec_test_input("gif/download-animation.gif")
let download = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&download_bytes)) else trap("the GIF decodes")
assert(download.is_animated and download.frame_count == 3 and download.loop_count == 0 and download.session_id == 0)
assert(ak.vector_size(&download.frames) == 3 and ak.vector_at_const(&download.frames, 1).duration == 400)
assert(*ak.vector_at_const(&download.all_durations, 2) == 400)
assert(image_codec_test_frame_is(&download, 0, 292, 32, 0x5974dc75))
assert(image_codec_test_frame_is(&download, 1, 292, 32, 0x2a523f97))
assert(image_codec_test_frame_is(&download, 2, 292, 32, 0xb0a3d89f))
# test_corrupted_gif: the frame decoded before the damage.
let corrupted = image_codec_test_still("gif/corrupted.gif", 100, 1)
assert(image_codec_test_frame_is(&corrupted, 0, 88, 31, 0xea5dc90d))
# test_gif_empty_lzw_data: a transparent pixel; test_gif_without_global_color_table: red.
let minimal = image_codec_test_still("gif/minimal-1x1.gif", 100, 1)
assert(image_codec_test_frame_is(&minimal, 0, 1, 1, 0x4b95f515))
let local_palette = luce_image_codec_plugin_decode(image_codec_test_gif_local_palette) else trap("the GIF decodes")
assert(image_codec_test_pixel(image_codec_test_frame(&local_palette, 0), 0, 0) == 0xffff0000)
# Every disposal, a transparent index and browser delays, in order.
let animation = luce_image_codec_plugin_decode(image_codec_test_animation_loop2_gif) else trap("the GIF decodes")
assert(animation.is_animated and animation.frame_count == 6 and ak.vector_size(&animation.frames) == 6)
for index: usize in 0..<6:
assert(image_codec_test_animation_frame_is(image_codec_test_frame(&animation, index), index))
assert(ak.vector_at_const(&animation.frames, index).duration == (usize)image_codec_test_animation_durations[index])
assert(*ak.vector_at_const(&animation.all_durations, index) == image_codec_test_animation_durations[index])
# The loop count is how many times browsers play it: the count written and the first play, 0
# forever, once without the extension (Ladybird answers 2, 0 and 1).
assert(animation.loop_count == 3)
let forever = luce_image_codec_plugin_decode(image_codec_test_animation_looping(0)) else trap("the GIF decodes")
assert(forever.loop_count == 0)
let once = luce_image_codec_plugin_decode(image_codec_test_animation_without_loop()) else trap("the GIF decodes")
assert(once.loop_count == 1 and once.frame_count == 6)
# Not a GIF past its signature, and a screen descriptor with no image.
assert(image_codec_test_fails(b"GIF89a", gif_codec.corrupt))
assert(image_codec_test_fails(b"GIF89a\x01\x00\x01\x00\x00\x00\x00\x3B", gif_codec.corrupt))
# mark: Color spaces ============================================================================
## Whether a decoded image's color space has these transfer function { g, a, b, c, d, e, f }
## and row-major XYZ D50 matrix bits; all zero for none (sRGB).
func image_codec_test_color_space_is(image: const PlatformDecodedImage*, bits: u32[16]) -> bool:
var color_space = image.color_space
var none_bits: u32[16]
let found = *gfx.color_space_color_space_sk_sp(&color_space) else return bits == none_bits
let curve = found.transfer_function
let m = found.to_xyzd50
let values: f32[16] = [curve.g, curve.a, curve.b, curve.c, curve.d, curve.e, curve.f, m.vals[0][0], m.vals[0][1], m.vals[0][2], m.vals[1][0], m.vals[1][1], m.vals[1][2], m.vals[2][0], m.vals[2][1], m.vals[2][2]]
for i in 0..<(usize)16:
if values[i].bits() != bits[i]:
return false
return true
## The bits of no color space (sRGB).
let image_codec_test_no_color_space: u32[16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
## The bytes of a test of tests/libweb (a path under it).
func image_codec_test_libweb(path: str) -> ak.ByteBuffer:
var full = ak.string_builder_new()
ak.string_builder_append(&full, "tests/libweb/")
ak.string_builder_append(&full, path)
return core_mapped_file_map(ak.string_builder_string_view(&full)) else trap("the test image reads")
test "the ImageCodecPlugin: color spaces as ImageDecoder::color_space makes them: cICP, then ICC profiles":
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
# Gfx::ColorSpace::load_from_icc_bytes's results, printed by Skia (luce-browser-tools/
# oracles/luce-color/icc, skia_oracle's `ladybird` lines): sRGB's curve and the PNG's own
# matrix (Display P3's for icc-v2.png; sRGB's with red and green swapped for WPT's
# swap-990000-iCCP.png); a JPEG whose profile is sRGB's is Skia's sRGB.
let v2 = image_codec_test_still("icc/icc-v2.png", 0, 0)
assert(image_codec_test_color_space_is(&v2, [0x4019999a, 0x3f72a76e, 0x3d558919, 0x3d9e8391, 0x3d25aee6, 0, 0, 0x3f03df00, 0x3e957e00, 0x3e20e000, 0x3e76fc00, 0x3f313700, 0x3d885800, 0xba8a0000, 0x3d2b9000, 0x3f48b900]))
let v4 = image_codec_test_still("icc/icc-v4.jpg", 0, 0)
assert(image_codec_test_color_space_is(&v4, [0x4019999a, 0x3f72a76e, 0x3d558919, 0x3d9e8391, 0x3d25aee6, 0, 0, 0x3edf4400, 0x3ec53200, 0x3e128000, 0x3e63d400, 0x3f378500, 0x3d784000, 0x3c640000, 0x3dc6d000, 0x3f36cf00]))
let swap_bytes = image_codec_test_libweb("Ref/input/wpt-import/css/css-color/support/swap-990000-iCCP.png")
let swap = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&swap_bytes)) else trap("the swapped PNG decodes")
assert(image_codec_test_color_space_is(&swap, [0x4019999a, 0x3f72a76e, 0x3d558919, 0x3d9e8391, 0x3d25aee6, 0, 0, 0x3ec53200, 0x3edf4400, 0x3e128000, 0x3f378500, 0x3e63d400, 0x3d784000, 0x3dc6d000, 0x3c640000, 0x3f36cf00]))
# The cICP chunk wins over the iCCP chunk (here a profile skcms refuses): Display P3's
# primaries (12) with sRGB's curve (13), as ColorSpace::from_cicp builds them.
let cicp_bytes = image_codec_test_libweb("Ref/input/wpt-import/png/support/cICP-and-iCCP.png")
let cicp = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&cicp_bytes)) else trap("the cICP PNG decodes")
# (The bits the donor's from_cicp gives, as luce-browser-render's from_cicp test pins them.)
assert(image_codec_test_color_space_is(&cicp, [0x4019999a, 0x3f72a76e, 0x3d558919, 0x3d9e8391, 0x3d25aee6, 0, 0, 0x3f03ddbc, 0x3e957c67, 0x3e20ecbe, 0x3e76f860, 0x3f313668, 0x3d885c11, 0xba898c54, 0x3d2b8c34, 0x3f48ccfa]))
# Matrix coefficients other than identity, or a studio range, are refused: sRGB.
var refused = luce_image_codec_cicp_color_space(png_codec.Cicp(color_primaries = 12, transfer_function = 13, matrix_coefficients = 1, video_full_range = 1))
assert(*gfx.color_space_color_space_sk_sp(&refused) == none)
var studio = luce_image_codec_cicp_color_space(png_codec.Cicp(color_primaries = 12, transfer_function = 13, matrix_coefficients = 0, video_full_range = 0))
assert(*gfx.color_space_color_space_sk_sp(&studio) == none)
# No profile, and an empty APP2 profile, are sRGB (no color space).
let empty = image_codec_test_still("icc/gradient_empty_icc.jpg", 0, 0)
assert(image_codec_test_color_space_is(&empty, image_codec_test_no_color_space))
let gif = image_codec_test_still("gif/minimal-1x1.gif", 100, 1)
assert(image_codec_test_color_space_is(&gif, image_codec_test_no_color_space))
## Stores `value` little-endian at `at`.
func image_codec_test_put32(at: usize, value: u32, data: u8[]):
for i in 0..<(usize)4:
data[at + i] = (u8)((value >> (u32)(8 * i)) & 0xff)
## A 1x1 24-bit BMP with a BITMAPV5HEADER of color space type `cs_type`, its pixel blue, and
## `profile` after the pixels at the offset the header gives (`profile_data`, from the DIB
## header, or the real one when 0).
func image_codec_test_v5_bmp(cs_type: u32, profile: const u8[], profile_data: u32) -> ak.ByteBuffer:
let pixels_at: usize = 14 + 124
let profile_at = pixels_at + 4
var bytes = ak.byte_buffer_create_zeroed(profile_at + profile.length) else trap("out of memory")
let data = ak.byte_buffer_bytes(&bytes)
data[0] = 'B'
data[1] = 'M'
image_codec_test_put32(2, (u32)data.length, data)
image_codec_test_put32(10, (u32)pixels_at, data)
image_codec_test_put32(14, 124, data)
image_codec_test_put32(18, 1, data)
image_codec_test_put32(22, 1, data)
data[26] = 1
data[28] = 24
image_codec_test_put32(14 + 56, cs_type, data)
image_codec_test_put32(14 + 112, profile_data if profile_data != 0 else (u32)(profile_at - 14), data)
image_codec_test_put32(14 + 116, (u32)profile.length, data)
data[pixels_at] = 0xff
for i in 0..<profile.length:
data[profile_at + i] = profile[i]
return bytes
test "the ImageCodecPlugin: a BMP's embedded profile (BITMAPV5HEADER, PROFILE_EMBEDDED) is its color space":
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
# The profile of WPT's swap-990000-iCCP.png (sRGB with red and green swapped).
let png_bytes = image_codec_test_libweb("Ref/input/wpt-import/css/css-color/support/swap-990000-iCCP.png")
let found = png_codec.info(ak.byte_buffer_bytes_const(&png_bytes)) else trap("the PNG reads")
var profile: u8[]? = none
with memory.heap:
profile = png_codec.icc_profile(&found) else none
let icc = profile else trap("the PNG has a profile")
defer free(icc) in memory.heap
let swapped: u32[16] = [0x4019999a, 0x3f72a76e, 0x3d558919, 0x3d9e8391, 0x3d25aee6, 0, 0, 0x3ec53200, 0x3edf4400, 0x3e128000, 0x3f378500, 0x3e63d400, 0x3d784000, 0x3dc6d000, 0x3c640000, 0x3f36cf00]
let embedded = image_codec_test_v5_bmp(0x4D424544, icc, 0)
let image = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&embedded)) else trap("the BMP decodes")
assert(image_codec_test_color_space_is(&image, swapped))
assert(image_codec_test_pixel(image_codec_test_frame(&image, 0), 0, 0) == 0xff0000ff)
# LINKED ('LINK') and sRGB ('sRGB') types, and a profile past the end of the file, are sRGB.
let types: u32[2] = [0x4C494E4B, 0x73524742]
for cs_type in types:
let other = image_codec_test_v5_bmp(cs_type, icc, 0)
let decoded = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&other)) else trap("the BMP decodes")
assert(image_codec_test_color_space_is(&decoded, image_codec_test_no_color_space))
let outside = image_codec_test_v5_bmp(0x4D424544, icc, 1000)
let decoded = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&outside)) else trap("the BMP decodes")
assert(image_codec_test_color_space_is(&decoded, image_codec_test_no_color_space))
# mark: Unsupported formats =====================================================================
test "the ImageCodecPlugin: AVIF and unknown data fail as unsupported, a damaged WebP as corrupt":
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
assert(image_codec_test_fails(b"RIFF\x00\x00\x00\x00WEBPVP8 ", webp_codec.corrupt))
assert(image_codec_test_fails(b"\x00\x00\x00\x1CftypavifXXXX", luce_image_codec_unsupported))
assert(image_codec_test_fails(b"not an image", luce_image_codec_unsupported))
assert(image_codec_test_fails(b"", luce_image_codec_unsupported))
# mark: Streaming animations ====================================================================
## What decode_image's handlers and the plugin's animation callbacks heard.
struct ImageCodecTestLog:
var resolved: i32
var rejected: i32
var image: PlatformDecodedImage
var deliveries: i32
var delivered_session: i64
var delivered: ak.Vector[gfx.Bitmap*]
var failures: i32
static func on_resolved(context: void*?, image: PlatformDecodedImage*):
let log = (ImageCodecTestLog*)(context else trap("no log"))
log.resolved += 1
log.image = *image
static func on_rejected(context: void*?, error_: ak.AkError*):
let log = (ImageCodecTestLog*)(context else trap("no log"))
log.rejected += 1
static func on_frames(context: void*?, session_id: i64, bitmaps: ak.Vector[gfx.Bitmap*]):
let log = (ImageCodecTestLog*)(context else trap("no log"))
log.deliveries += 1
log.delivered_session = session_id
log.delivered = bitmaps
static func on_failed(context: void*?, session_id: i64):
let log = (ImageCodecTestLog*)(context else trap("no log"))
log.failures += 1
## Decodes `bytes` through the installed plugin's decode_image, running the event loop until it settles.
func image_codec_test_decode_image(event_loop: LoaderTestEventLoop*, log: ImageCodecTestLog*, bytes: const u8[]):
let resolved = ak.Function1[PlatformDecodedImage*, unit](call = ImageCodecTestLog.on_resolved, context = log)
let rejected = ak.Function1[ak.AkError*, unit](call = ImageCodecTestLog.on_rejected, context = log)
_ = platform_image_codec_plugin_decode_image(platform_image_codec_plugin_the(), bytes, resolved, rejected)
loader_test_drain(event_loop)
test "the ImageCodecPlugin: an animated GIF is streamed: its first frames, every duration, frames on request, stopped sessions":
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
var event_loop = LoaderTestEventLoop()
loader_test_install_event_loop(&event_loop)
defer loader_test_remove_event_loop()
platform_s_the = none
luce_image_codec_plugin_install()
defer platform_s_the = none
let plugin = platform_image_codec_plugin_the()
let log = new ImageCodecTestLog(image = PlatformDecodedImage(color_space = luce_image_codec_srgb())) else trap("out of memory")
plugin.on_animation_frames_decoded = ak.Function2[i64, ak.Vector[gfx.Bitmap*], unit](call = ImageCodecTestLog.on_frames, context = log)
plugin.on_animation_decode_failed = ak.Function1[i64, unit](call = ImageCodecTestLog.on_failed, context = log)
# The first answer: a session, the first four frames, all six durations, the loop count.
image_codec_test_decode_image(&event_loop, log, image_codec_test_animation_loop2_gif)
assert(log.resolved == 1 and log.rejected == 0)
let session_id = log.image.session_id
assert(session_id == 1 and log.image.is_animated and log.image.frame_count == 6 and log.image.loop_count == 3)
assert(ak.vector_size(&log.image.frames) == 4 and ak.vector_size(&log.image.all_durations) == 6)
for index: usize in 0..<4:
assert(image_codec_test_animation_frame_is(image_codec_test_frame(&log.image, index), index))
assert(ak.vector_at_const(&log.image.frames, index).duration == (usize)image_codec_test_animation_durations[index])
for index: usize in 0..<6:
assert(*ak.vector_at_const(&log.image.all_durations, index) == image_codec_test_animation_durations[index])
# A second animation gets the next session; a still GIF and a PNG get none.
image_codec_test_decode_image(&event_loop, log, image_codec_test_animation_without_loop())
assert(log.resolved == 2 and log.image.session_id == 2 and log.image.loop_count == 1)
image_codec_test_decode_image(&event_loop, log, image_codec_test_gif_local_palette)
assert(log.resolved == 3 and log.image.session_id == 0 and not log.image.is_animated and ak.vector_size(&log.image.frames) == 1)
image_codec_test_decode_image(&event_loop, log, loader_test_rgba_png)
assert(log.resolved == 4 and log.image.session_id == 0)
image_codec_test_decode_image(&event_loop, log, b"GIF89a")
assert(log.resolved == 4 and log.rejected == 1)
# Frames 4 and 5 (of four asked for), decoded on the next turn.
platform_image_codec_plugin_request_animation_frames(plugin, session_id, 4, 4)
assert(log.deliveries == 0)
loader_test_drain(&event_loop)
assert(log.deliveries == 1 and log.delivered_session == session_id and ak.vector_size(&log.delivered) == 2)
assert(image_codec_test_animation_frame_is(*ak.vector_at_const(&log.delivered, 0), 4))
assert(image_codec_test_animation_frame_is(*ak.vector_at_const(&log.delivered, 1), 5))
# Going back renders from the first frame again: frames 1 and 2.
platform_image_codec_plugin_request_animation_frames(plugin, session_id, 1, 2)
loader_test_drain(&event_loop)
assert(log.deliveries == 2 and ak.vector_size(&log.delivered) == 2)
assert(image_codec_test_animation_frame_is(*ak.vector_at_const(&log.delivered, 0), 1))
assert(image_codec_test_animation_frame_is(*ak.vector_at_const(&log.delivered, 1), 2))
# A start past the last frame, or an unknown session, is ignored.
platform_image_codec_plugin_request_animation_frames(plugin, session_id, 6, 4)
platform_image_codec_plugin_request_animation_frames(plugin, 99, 0, 4)
loader_test_drain(&event_loop)
assert(log.deliveries == 2 and log.failures == 0)
# Stopping the session cancels a request not yet decoded, and later ones are ignored.
platform_image_codec_plugin_request_animation_frames(plugin, session_id, 0, 1)
platform_image_codec_plugin_stop_animation_decode(plugin, session_id)
loader_test_drain(&event_loop)
platform_image_codec_plugin_request_animation_frames(plugin, session_id, 0, 1)
platform_image_codec_plugin_stop_animation_decode(plugin, session_id)
loader_test_drain(&event_loop)
assert(log.deliveries == 2 and log.failures == 0)
# The other session still answers.
platform_image_codec_plugin_request_animation_frames(plugin, 2, 5, 1)
loader_test_drain(&event_loop)
assert(log.deliveries == 3 and log.delivered_session == 2 and ak.vector_size(&log.delivered) == 1)
assert(image_codec_test_animation_frame_is(*ak.vector_at_const(&log.delivered, 0), 5))
test "the ImageCodecPlugin feeds AnimatedDecodedImageData: frames requested as the animation advances arrive in its pool":
# The plugin outlives the VM, whose collection finalizes the data (stopping its session).
platform_s_the = none
luce_image_codec_plugin_install()
defer platform_s_the = none
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
let realm = web_test_create_realm(vm)
var event_loop = LoaderTestEventLoop()
loader_test_install_event_loop(&event_loop)
defer loader_test_remove_event_loop()
let log = new ImageCodecTestLog(image = PlatformDecodedImage(color_space = luce_image_codec_srgb())) else trap("out of memory")
image_codec_test_decode_image(&event_loop, log, image_codec_test_animation_loop2_gif)
let result = &log.image
assert(result.session_id != 0)
# As SharedResourceRequest's handle_successful_bitmap_decode does.
var initial_bitmaps: ak.Vector[gfx.Bitmap*]
for frame in ak.vector_span(&result.frames):
ak.vector_append_t(&initial_bitmaps, frame.bitmap else trap("a decoded frame has a bitmap"))
let size = gfx.bitmap_size(ak.vector_first(&result.frames).bitmap else trap("a bitmap"))
let data = html_animated_decoded_image_data_create(realm, result.session_id, result.frame_count, result.loop_count, size, result.color_space,
result.all_durations, initial_bitmaps)
let base = data.html_decoded_image_data()
assert(html_decoded_image_data_frame_count(base) == 6 and html_decoded_image_data_loop_count(base) == 3)
assert(html_decoded_image_data_frame_duration(base, 3) == 200 and html_decoded_image_data_frame_duration(base, 5) == 70)
# Frame 1: frames 2 and 3 are ahead, fewer than four: frames from 4 are requested and arrive.
assert(html_decoded_image_data_notify_frame_advanced(base, 1) == 1)
assert(data.m_request_in_flight and data.m_last_requested_start_frame == 4)
loader_test_drain(&event_loop)
assert(not data.m_request_in_flight)
for index: usize in 0..<6:
assert(html_animated_decoded_image_data_find_slot(data, (u32)index) != none)
let frame5 = html_decoded_image_data_bitmap(base, 5, gfx.IntSize()) else trap("frame 5")
assert(image_codec_test_animation_frame_is(gfx.immutable_bitmap_bitmap(frame5) else trap("a bitmap"), 5))