dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / platform / tests_image_codec_plugin_luce_webp.lucb
#==============================================================================================
#
# tests_image_codec_plugin_luce_webp - the ImageCodecPlugin over luce-webp and luce-png's
# animations: WebP stills and animations, animated PNGs, their streaming
#
# DESCRIPTION:
# Ladybird's Tests/LibGfx/TestImageDecoder.cpp cases for WebP and APNG (test_webp_*,
# test_apng*), run through the plugin on LibGfx's test inputs (tests/libgfx), and
# synthetic images made by luce-webp's and luce-png's fixture generators
# (tests/image_codecs). 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 (luce-browser-tools/oracles/luce-browser-engine/image_codecs), with its durations,
# loop counts and animation flags: all agree. Big frames are made with the VM's heap
# current, as WebContent makes them, so the collector keeps them.
#
#==============================================================================================
# mark: Helpers =================================================================================
## The bytes of a synthetic test image (tests/image_codecs).
func image_codec_test_synthetic(name: str) -> ak.ByteBuffer:
var full = ak.string_builder_new()
ak.string_builder_append(&full, "tests/image_codecs/")
ak.string_builder_append(&full, name)
return core_mapped_file_map(ak.string_builder_string_view(&full)) else trap("the test image reads")
## A decoded image as Ladybird's ImageDecoder reports it: its frame count, whether it is animated,
## its loop count, and each frame's size, duration and pixel hash.
struct ImageCodecTestExpected:
var frame_count: u32
var animated: bool
var loop_count: u32
var width: i32
var height: i32
var durations: const u32[]
var hashes: const u32[]
## Whether `image` is what Ladybird's ImageDecoder makes of the same file.
func image_codec_test_matches(image: const PlatformDecodedImage*, expected: ImageCodecTestExpected) -> bool:
if image.frame_count != expected.frame_count or image.is_animated != expected.animated or image.loop_count != expected.loop_count:
return false
if ak.vector_size(&image.frames) != expected.hashes.length:
return false
for index in 0..<expected.hashes.length:
if ak.vector_at_const(&image.frames, index).duration != (usize)expected.durations[index]:
return false
if not image_codec_test_frame_is(image, index, expected.width, expected.height, expected.hashes[index]):
return false
return true
## Decodes `bytes` (a whole image, every frame) and checks it against Ladybird's.
func image_codec_test_decodes_as(bytes: const u8[], expected: ImageCodecTestExpected) -> bool:
let image = luce_image_codec_plugin_decode(bytes) else return false
return image_codec_test_matches(&image, expected)
let image_codec_test_zero_duration: u32[1] = [0]
## The LibGfx WebP inputs, their sizes, and Ladybird's hashes of their one frame.
let image_codec_test_webp_names: str[19] = ["catdog-alert-13-alpha-used-false", "catdog-alert-13", "catdog-alert-2", "catdog-alert-3", "catdog-alert-8", "extended-lossless", "extended-lossy-uncompressed-alpha", "extended-lossy", "Qpalette", "semi-transparent-pixel", "simple-vp8",
"simple-vp8l-alpha-used-false", "simple-vp8l", "smolkling-gradient-alpha", "smolkling-horizontal-alpha", "smolkling-vertical-alpha", "smolkling", "width11-height11-colors15", "width11-height11-colors2"]
let image_codec_test_webp_sizes: i32[38] = [32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 417, 223, 417, 223, 417, 223, 256, 256, 1, 1, 240, 240, 386, 395, 386, 395, 264, 264, 264, 264, 264, 264, 264, 264, 11, 11, 11, 11]
let image_codec_test_webp_hashes: u32[19] = [0xaf3936cb, 0x9571d2e3, 0xeebbcca5, 0x5f5a8123, 0x70899422, 0xe0f6a548, 0x6096401a, 0x54da0a15, 0x51d47dc5, 0x40e05c15, 0xfe71cfeb, 0x4e3d1615, 0x2a448280, 0x48211e6a, 0x44db85ee, 0x312ae708, 0xc3f634ed, 0x6f3df1cd, 0x5a0723d8]
## extended-lossless-animated.webp's eight frames, as Ladybird's ImageDecoder gives them.
let image_codec_test_webp_animation_hashes: u32[8] = [0x52474d15, 0x584ecd25, 0xca62a0cd, 0xd65c7abd, 0x82ecbe75, 0x3f931785, 0x0019e22d, 0x462d21bd]
let image_codec_test_hundred_ms: u32[8] = [100, 100, 100, 100, 100, 100, 100, 100]
## The synthetic animations' frames, as Ladybird's ImageDecoder gives them.
let image_codec_test_anim_lossy_hashes: u32[5] = [0x4507d319, 0x6f755370, 0x68e50fee, 0x108015e7, 0x9859999b]
let image_codec_test_anim_lossy_durations: u32[5] = [20, 40, 60, 80, 100]
let image_codec_test_dispose_bgnd_hashes: u32[5] = [0x1bbb9d06, 0x9ef6f615, 0x7154d486, 0xc552862b, 0xe08628af]
let image_codec_test_second: u32[5] = [1000, 1000, 1000, 1000, 1000]
let image_codec_test_transparent_dispose_hashes: u32[3] = [0x934d6813, 0x7fff9493, 0x934d6813]
let image_codec_test_transparent_dispose_durations: u32[3] = [100, 500, 500]
let image_codec_test_keep_over_hashes: u32[5] = [0x2dcc26dd, 0x1c36baa9, 0x03daef9d, 0x186b810e, 0x8be7f0fe]
let image_codec_test_previous_hashes: u32[5] = [0x2dcc26dd, 0x17514499, 0x9a16ee9d, 0xea6ad5b8, 0xa59a0bcb]
let image_codec_test_background_hashes: u32[5] = [0x2dcc26dd, 0x17514499, 0x9a16ee9d, 0xee9f7421, 0x4011f955]
let image_codec_test_pillow_durations: u32[5] = [30, 1, 1, 100, 250]
let image_codec_test_offsets_hashes: u32[3] = [0x52cf3d72, 0xb5970b36, 0x7720c37b]
let image_codec_test_offsets_durations: u32[3] = [1, 50, 1]
let image_codec_test_hidden_hashes: u32[2] = [0x11cba6bc, 0xd276ed61]
let image_codec_test_hidden_durations: u32[2] = [100, 100]
let image_codec_test_apng_one_second: u32[1] = [1000]
let image_codec_test_apng_one_frame_hash: u32[1] = [0x098c9dc5]
let image_codec_test_apng_blend_hash: u32[1] = [0x7d0ce3c5]
# mark: WebP ====================================================================================
test "the ImageCodecPlugin: WebP files (LibGfx's test_webp_*), as Ladybird's ImageDecoder decodes them":
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
let allocator: memory.Allocator = vm_heap(vm)
with allocator:
image_codec_test_webp_files()
func image_codec_test_webp_files():
for index in 0..<image_codec_test_webp_names.length:
var buffer: u8[128]
let name = format(buffer, f"webp/{image_codec_test_webp_names[index]}.webp") else trap("a test input's name")
let bytes = image_codec_test_input(name)
let hash: u32[1] = [image_codec_test_webp_hashes[index]]
let expected = ImageCodecTestExpected(frame_count = 1, animated = false, loop_count = 0, width = image_codec_test_webp_sizes[index * 2], height = image_codec_test_webp_sizes[index * 2 + 1], durations = image_codec_test_zero_duration, hashes = hash)
assert(image_codec_test_decodes_as(ak.byte_buffer_bytes_const(&bytes), expected))
# test_webp_simple_lossy's pixels: libwebp's, converted to premultiplied BGRA.
let vp8_bytes = image_codec_test_input("webp/simple-vp8.webp")
let vp8 = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&vp8_bytes)) else trap("simple-vp8.webp decodes")
assert(image_codec_test_pixel(image_codec_test_frame(&vp8, 0), 120, 232) == 0xfff1eff0)
assert(image_codec_test_pixel(image_codec_test_frame(&vp8, 0), 198, 202) == 0xff7aaad5)
# The alpha-used bit clear: drawn opaque (Ladybird's BGRx8888), the stored alpha unused.
let unused_bytes = image_codec_test_input("webp/simple-vp8l-alpha-used-false.webp")
let unused = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&unused_bytes)) else trap("the file decodes")
assert(image_codec_test_pixel(image_codec_test_frame(&unused, 0), 0, 0) == 0xff000000)
# test_webp_extended_lossless_animated: eight frames of 100 ms, looping 42 times.
let animated_bytes = image_codec_test_input("webp/extended-lossless-animated.webp")
let animated = ImageCodecTestExpected(frame_count = 8, animated = true, loop_count = 42, width = 990, height = 1050, durations = image_codec_test_hundred_ms, hashes = image_codec_test_webp_animation_hashes)
assert(image_codec_test_decodes_as(ak.byte_buffer_bytes_const(&animated_bytes), animated))
test "the ImageCodecPlugin: WebP animations (blending, disposal, key frames) and ICC profiles":
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
let lossy_bytes = image_codec_test_synthetic("anim-lossy.webp")
let lossy = ImageCodecTestExpected(frame_count = 5, animated = true, loop_count = 0, width = 48, height = 36, durations = image_codec_test_anim_lossy_durations, hashes = image_codec_test_anim_lossy_hashes)
assert(image_codec_test_decodes_as(ak.byte_buffer_bytes_const(&lossy_bytes), lossy))
let disposed_bytes = image_codec_test_synthetic("gif-dispose_bgnd.webp")
let disposed = ImageCodecTestExpected(frame_count = 5, animated = true, loop_count = 0, width = 100, height = 100, durations = image_codec_test_second, hashes = image_codec_test_dispose_bgnd_hashes)
assert(image_codec_test_decodes_as(ak.byte_buffer_bytes_const(&disposed_bytes), disposed))
let transparent_bytes = image_codec_test_synthetic("gif-transparent_dispose.webp")
let transparent = ImageCodecTestExpected(frame_count = 3, animated = true, loop_count = 1, width = 3, height = 1, durations = image_codec_test_transparent_dispose_durations, hashes = image_codec_test_transparent_dispose_hashes)
assert(image_codec_test_decodes_as(ak.byte_buffer_bytes_const(&transparent_bytes), transparent))
# The ICCP chunk's profile gives the color space (from Ladybird's p3-v4.icc); the pixels are
# left as they are. Without one, sRGB.
let tagged_bytes = image_codec_test_synthetic("meta-icc-set.webp")
let tagged = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&tagged_bytes)) else trap("the WebP decodes")
assert(image_codec_test_frame_is(&tagged, 0, 97, 61, 0x4140d463))
assert(not image_codec_test_color_space_is(&tagged, image_codec_test_no_color_space))
let plain_bytes = image_codec_test_input("webp/simple-vp8.webp")
let plain = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&plain_bytes)) else trap("the WebP decodes")
assert(image_codec_test_color_space_is(&plain, image_codec_test_no_color_space))
# A file whose chunks WebPMux refuses (the alpha flag cleared beside an ALPH chunk) is refused,
# as Ladybird's decode_webp_header refuses it; a truncated one too.
let lossy_alpha = image_codec_test_input("webp/extended-lossy.webp")
var changed = ak.byte_buffer_copy_span_u8(ak.byte_buffer_bytes_const(&lossy_alpha)) else trap("out of memory")
let changed_bytes = ak.byte_buffer_bytes(&changed)
changed_bytes[20] = changed_bytes[20] & ~(u8)0x10
assert(image_codec_test_fails(changed_bytes, webp_codec.corrupt))
assert(image_codec_test_fails(ak.byte_buffer_bytes_const(&lossy_alpha)[..<1000], webp_codec.corrupt))
# mark: Animated PNG ============================================================================
test "the ImageCodecPlugin: animated PNGs (LibGfx's test_apng*), composited as Ladybird's PNG loader composites them":
let vm = web_test_create_vm()
defer web_test_destroy_vm(vm)
# test_apng: one frame after a hidden default image, 1000 ms; not animated.
let one_bytes = image_codec_test_input("png/apng-1-frame.png")
let one = ImageCodecTestExpected(frame_count = 1, animated = false, loop_count = 0, width = 128, height = 64, durations = image_codec_test_apng_one_second, hashes = image_codec_test_apng_one_frame_hash)
assert(image_codec_test_decodes_as(ak.byte_buffer_bytes_const(&one_bytes), one))
let one_image = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&one_bytes)) else trap("the APNG decodes")
assert(image_codec_test_pixel(image_codec_test_frame(&one_image, 0), 64, 32) == 0xff75fc4c)
# test_apng_idat_not_affecting_next_frame: the hidden image is not drawn under the frame.
let blend_bytes = image_codec_test_input("png/apng-blend.png")
let blend = ImageCodecTestExpected(frame_count = 1, animated = false, loop_count = 0, width = 100, height = 100, durations = image_codec_test_apng_one_second, hashes = image_codec_test_apng_blend_hash)
assert(image_codec_test_decodes_as(ak.byte_buffer_bytes_const(&blend_bytes), blend))
# Every disposal and blend, regions, delays (a numerator of 0 is 1 ms), the loop count.
let over_bytes = image_codec_test_synthetic("pillow-keep-over.png")
let over = ImageCodecTestExpected(frame_count = 5, animated = true, loop_count = 3, width = 40, height = 30, durations = image_codec_test_pillow_durations, hashes = image_codec_test_keep_over_hashes)
assert(image_codec_test_decodes_as(ak.byte_buffer_bytes_const(&over_bytes), over))
let previous_bytes = image_codec_test_synthetic("pillow-previous.png")
let previous = ImageCodecTestExpected(frame_count = 5, animated = true, loop_count = 3, width = 40, height = 30, durations = image_codec_test_pillow_durations, hashes = image_codec_test_previous_hashes)
assert(image_codec_test_decodes_as(ak.byte_buffer_bytes_const(&previous_bytes), previous))
let background_bytes = image_codec_test_synthetic("pillow-background.png")
let background = ImageCodecTestExpected(frame_count = 5, animated = true, loop_count = 3, width = 40, height = 30, durations = image_codec_test_pillow_durations, hashes = image_codec_test_background_hashes)
assert(image_codec_test_decodes_as(ak.byte_buffer_bytes_const(&background_bytes), background))
let offsets_bytes = image_codec_test_synthetic("hand-offsets-over.png")
let offsets = ImageCodecTestExpected(frame_count = 3, animated = true, loop_count = 2, width = 16, height = 12, durations = image_codec_test_offsets_durations, hashes = image_codec_test_offsets_hashes)
assert(image_codec_test_decodes_as(ak.byte_buffer_bytes_const(&offsets_bytes), offsets))
let hidden_bytes = image_codec_test_synthetic("hand-hidden-default.png")
let hidden = ImageCodecTestExpected(frame_count = 2, animated = true, loop_count = 0, width = 9, height = 7, durations = image_codec_test_hidden_durations, hashes = image_codec_test_hidden_hashes)
assert(image_codec_test_decodes_as(ak.byte_buffer_bytes_const(&hidden_bytes), hidden))
# PNGImageDecoderPlugin::create's fallback: the first frame as a still image when a later one
# is missing, a transparent canvas when the first does not decode.
let missing_bytes = image_codec_test_synthetic("hand-missing-frames.png")
let missing = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&missing_bytes)) else trap("the damaged APNG decodes")
assert(not missing.is_animated and missing.frame_count == 1)
assert(ak.vector_at_const(&missing.frames, 0).duration == 100 and image_codec_test_frame_is(&missing, 0, 8, 8, 0xc4b4e03e))
let broken_bytes = image_codec_test_synthetic("hand-crc-idat.png")
let broken = luce_image_codec_plugin_decode(ak.byte_buffer_bytes_const(&broken_bytes)) else trap("the damaged APNG decodes")
assert(not broken.is_animated and broken.frame_count == 1 and image_codec_test_frame_is(&broken, 0, 8, 8, 0xe6a1d1c5))
# mark: Streaming ===============================================================================
test "the ImageCodecPlugin: animated WebPs and PNGs are streamed as GIFs are: first frames, durations, frames on request":
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)
# A WebP animation: the first four frames, all five durations, the loop count.
let webp_bytes = image_codec_test_synthetic("anim-lossy.webp")
image_codec_test_decode_image(&event_loop, log, ak.byte_buffer_bytes_const(&webp_bytes))
assert(log.resolved == 1 and log.image.session_id == 1 and log.image.is_animated and log.image.frame_count == 5 and log.image.loop_count == 0)
assert(ak.vector_size(&log.image.frames) == 4 and ak.vector_size(&log.image.all_durations) == 5)
for index in 0..<(usize)4:
assert(image_codec_test_frame_is(&log.image, index, 48, 36, image_codec_test_anim_lossy_hashes[index]))
assert(*ak.vector_at_const(&log.image.all_durations, 4) == 100)
platform_image_codec_plugin_request_animation_frames(plugin, 1, 4, 4)
loader_test_drain(&event_loop)
assert(log.deliveries == 1 and ak.vector_size(&log.delivered) == 1)
assert(image_codec_test_hash(*ak.vector_at_const(&log.delivered, 0)) == image_codec_test_anim_lossy_hashes[4])
# Back to frame 1: decoded again from the first.
platform_image_codec_plugin_request_animation_frames(plugin, 1, 1, 1)
loader_test_drain(&event_loop)
assert(log.deliveries == 2 and image_codec_test_hash(*ak.vector_at_const(&log.delivered, 0)) == image_codec_test_anim_lossy_hashes[1])
# An APNG of five frames: a session; one of a single frame, or one that falls back, none.
let apng_bytes = image_codec_test_synthetic("pillow-previous.png")
image_codec_test_decode_image(&event_loop, log, ak.byte_buffer_bytes_const(&apng_bytes))
assert(log.resolved == 2 and log.image.session_id == 2 and log.image.frame_count == 5 and log.image.loop_count == 3)
assert(*ak.vector_at_const(&log.image.all_durations, 1) == 1)
platform_image_codec_plugin_request_animation_frames(plugin, 2, 3, 2)
loader_test_drain(&event_loop)
assert(log.deliveries == 3 and ak.vector_size(&log.delivered) == 2)
assert(image_codec_test_hash(*ak.vector_at_const(&log.delivered, 1)) == image_codec_test_previous_hashes[4])
let still_bytes = image_codec_test_input("png/apng-1-frame.png")
image_codec_test_decode_image(&event_loop, log, ak.byte_buffer_bytes_const(&still_bytes))
assert(log.resolved == 3 and log.image.session_id == 0 and not log.image.is_animated)
let missing_bytes = image_codec_test_synthetic("hand-missing-frames.png")
image_codec_test_decode_image(&event_loop, log, ak.byte_buffer_bytes_const(&missing_bytes))
assert(log.resolved == 4 and log.image.session_id == 0 and log.image.frame_count == 1)
assert(log.failures == 0)