From 11c8917a4c468eaf004eac54ed8422b297154791 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 8 Sep 2018 08:02:47 +1000 Subject: [PATCH] Clip a frame's bounds to the image bounds --- release/c/wuffs-unsupported-snapshot.h | 13 +- std/gif/decode_gif.wuffs | 8 +- test/c/std/gif.c | 151 +++++++++++++++--- .../artificial/gif-frame-out-of-bounds.gif | Bin 110 -> 132 bytes ...rame-out-of-bounds.gif.make-artificial.txt | 6 + 5 files changed, 150 insertions(+), 28 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index 62316702..155f1fdc 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -6604,10 +6604,15 @@ wuffs_gif__decoder__decode_frame_config(wuffs_gif__decoder* self, wuffs_base__frame_config__update( a_dst, wuffs_base__utility__make_rect_ie_u32( - &self->private_impl.f_util, self->private_impl.f_frame_rect_x0, - self->private_impl.f_frame_rect_y0, - self->private_impl.f_frame_rect_x1, - self->private_impl.f_frame_rect_y1), + &self->private_impl.f_util, + wuffs_base__u32__min(self->private_impl.f_frame_rect_x0, + self->private_impl.f_width), + wuffs_base__u32__min(self->private_impl.f_frame_rect_y0, + self->private_impl.f_height), + wuffs_base__u32__min(self->private_impl.f_frame_rect_x1, + self->private_impl.f_width), + wuffs_base__u32__min(self->private_impl.f_frame_rect_y1, + self->private_impl.f_height)), self->private_impl.f_gc_duration, self->private_impl.f_num_decoded_frame_configs_value, self->private_impl.f_frame_config_io_position, v_blend, diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 6b3d04d3..e31a61d6 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -206,10 +206,10 @@ pub func decoder.decode_frame_config!??(dst nptr base.frame_config, src base.io_ if args.dst != nullptr { args.dst.update!(bounds:this.util.make_rect_ie_u32( - min_incl_x:this.frame_rect_x0, - min_incl_y:this.frame_rect_y0, - max_excl_x:this.frame_rect_x1, - max_excl_y:this.frame_rect_y1), + min_incl_x:this.frame_rect_x0.min(x:this.width), + min_incl_y:this.frame_rect_y0.min(x:this.height), + max_excl_x:this.frame_rect_x1.min(x:this.width), + max_excl_y:this.frame_rect_y1.min(x:this.height)), duration:this.gc_duration, index:this.num_decoded_frame_configs_value, io_position:this.frame_config_io_position, diff --git a/test/c/std/gif.c b/test/c/std/gif.c index 8242ad5d..3300d597 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -202,7 +202,7 @@ const char* wuffs_gif_decode(wuffs_base__io_buffer* dst, if (z) { return z; } - wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){}); + wuffs_base__image_config ic = ((wuffs_base__image_config){}); wuffs_base__frame_config fc = ((wuffs_base__frame_config){}); wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(src); @@ -210,6 +210,8 @@ const char* wuffs_gif_decode(wuffs_base__io_buffer* dst, if (z) { return z; } + + wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){}); z = wuffs_base__pixel_buffer__set_from_slice( &pb, &ic.pixcfg, ((wuffs_base__slice_u8){ @@ -515,7 +517,6 @@ bool do_test_wuffs_gif_decode_animated( return false; } - wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){}); wuffs_base__image_config ic = ((wuffs_base__image_config){}); wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src); @@ -540,6 +541,8 @@ bool do_test_wuffs_gif_decode_animated( wuffs_base__image_config__num_loops(&ic), want_num_loops); return false; } + + wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){}); z = wuffs_base__pixel_buffer__set_from_slice( &pb, &ic.pixcfg, ((wuffs_base__slice_u8){ @@ -560,26 +563,26 @@ bool do_test_wuffs_gif_decode_animated( return false; } - z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, work_buffer, - NULL); - if (z) { - FAIL("decode_frame #%" PRIu32 ": got \"%s\"", i, z); - return false; - } - if (want_frame_config_bounds) { wuffs_base__rect_ie_u32 got = wuffs_base__frame_config__bounds(&fc); wuffs_base__rect_ie_u32 want = want_frame_config_bounds[i]; if (!wuffs_base__rect_ie_u32__equals(&got, want)) { - FAIL("decode_frame #%" PRIu32 ": bounds: got (%" PRIu32 ", %" PRIu32 - ")-(%" PRIu32 ", %" PRIu32 "), want (%" PRIu32 ", %" PRIu32 - ")-(%" PRIu32 ", %" PRIu32 ")", + FAIL("decode_frame_config #%" PRIu32 ": bounds: got (%" PRIu32 + ", %" PRIu32 ")-(%" PRIu32 ", %" PRIu32 "), want (%" PRIu32 + ", %" PRIu32 ")-(%" PRIu32 ", %" PRIu32 ")", i, got.min_incl_x, got.min_incl_y, got.max_excl_x, got.max_excl_y, want.min_incl_x, want.min_incl_y, want.max_excl_x, want.max_excl_y); return false; } } + + z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, work_buffer, + NULL); + if (z) { + FAIL("decode_frame #%" PRIu32 ": got \"%s\"", i, z); + return false; + } } // There should be no more frames, no matter how many times we call @@ -622,7 +625,7 @@ void test_wuffs_gif_decode_animated_small() { // wire format is 0x0002, but that value means "repeat 2 times after the // first play", so the total number of loops is 3. const uint32_t want_num_loops = 3; - wuffs_base__rect_ie_u32 want_rects[4] = { + wuffs_base__rect_ie_u32 want_frame_config_bounds[4] = { make_rect_ie_u32(0, 0, 64, 48), make_rect_ie_u32(15, 31, 52, 40), make_rect_ie_u32(15, 0, 64, 40), @@ -630,7 +633,8 @@ void test_wuffs_gif_decode_animated_small() { }; do_test_wuffs_gif_decode_animated( "../../data/animated-red-blue.gif", want_num_loops, - WUFFS_TESTLIB_ARRAY_SIZE(want_rects), want_rects); + WUFFS_TESTLIB_ARRAY_SIZE(want_frame_config_bounds), + want_frame_config_bounds); } void test_wuffs_gif_decode_frame_out_of_bounds() { @@ -661,16 +665,123 @@ void test_wuffs_gif_decode_frame_out_of_bounds() { // test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt for // more discussion. - if (wuffs_base__pixel_config__width(&ic.pixcfg) != 4) { - FAIL("width: got %" PRIu32 ", want 4", - wuffs_base__pixel_config__width(&ic.pixcfg)); + const uint32_t width = 4; + const uint32_t height = 2; + + if (wuffs_base__pixel_config__width(&ic.pixcfg) != width) { + FAIL("width: got %" PRIu32 ", want %" PRIu32, + wuffs_base__pixel_config__width(&ic.pixcfg), width); return; } - if (wuffs_base__pixel_config__height(&ic.pixcfg) != 2) { - FAIL("height: got %" PRIu32 ", want 2", - wuffs_base__pixel_config__height(&ic.pixcfg)); + if (wuffs_base__pixel_config__height(&ic.pixcfg) != height) { + FAIL("height: got %" PRIu32 ", want %" PRIu32, + wuffs_base__pixel_config__height(&ic.pixcfg), height); return; } + + wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){}); + z = wuffs_base__pixel_buffer__set_from_slice( + &pb, &ic.pixcfg, + ((wuffs_base__slice_u8){ + .ptr = global_pixel_buffer, + .len = WUFFS_TESTLIB_ARRAY_SIZE(global_pixel_buffer), + })); + if (z) { + FAIL("set_from_slice: \"%s\"", z); + return; + } + + uint64_t work_buffer_size = + wuffs_base__image_config__work_buffer_size(&ic).max_incl; + if (work_buffer_size > BUFFER_SIZE) { + FAIL("work buffer size is too large"); + return; + } + wuffs_base__slice_u8 work_buffer = ((wuffs_base__slice_u8){ + .ptr = global_work_buffer, + .len = work_buffer_size, + }); + + // See test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt + // for the want_frame_config_bounds and want_pixel_indexes source. + + wuffs_base__rect_ie_u32 want_frame_config_bounds[4] = { + make_rect_ie_u32(1, 0, 4, 1), + make_rect_ie_u32(0, 1, 2, 2), + make_rect_ie_u32(0, 2, 1, 2), + make_rect_ie_u32(2, 0, 4, 2), + }; + + const char* want_pixel_indexes[4] = { + ".123....", + "....89..", + "........", + "..45..89", + }; + + uint32_t i; + for (i = 0; true; i++) { + { + wuffs_base__frame_config fc = ((wuffs_base__frame_config){}); + z = wuffs_gif__decoder__decode_frame_config(&dec, &fc, src_reader); + if (i == WUFFS_TESTLIB_ARRAY_SIZE(want_frame_config_bounds)) { + if (z != wuffs_base__warning__end_of_data) { + FAIL("decode_frame_config #%" PRIu32 ": got \"%s\"", i, z); + return; + } + break; + } + if (z) { + FAIL("decode_frame_config #%" PRIu32 ": got \"%s\"", i, z); + return; + } + + wuffs_base__rect_ie_u32 got = wuffs_base__frame_config__bounds(&fc); + wuffs_base__rect_ie_u32 want = want_frame_config_bounds[i]; + if (!wuffs_base__rect_ie_u32__equals(&got, want)) { + FAIL("decode_frame_config #%" PRIu32 ": bounds: got (%" PRIu32 + ", %" PRIu32 ")-(%" PRIu32 ", %" PRIu32 "), want (%" PRIu32 + ", %" PRIu32 ")-(%" PRIu32 ", %" PRIu32 ")", + i, got.min_incl_x, got.min_incl_y, got.max_excl_x, got.max_excl_y, + want.min_incl_x, want.min_incl_y, want.max_excl_x, + want.max_excl_y); + return; + } + } + + { + wuffs_base__table_u8 p = wuffs_base__pixel_buffer__plane(&pb, 0); + uint32_t y, x; + for (y = 0; y < height; y++) { + for (x = 0; x < width; x++) { + p.ptr[(y * p.stride) + x] = 0; + } + } + + z = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, work_buffer, + NULL); + if (z) { + FAIL("decode_frame #%" PRIu32 ": got \"%s\"", i, z); + return; + } + + char got[(width * height) + 1]; + for (y = 0; y < height; y++) { + for (x = 0; x < width; x++) { + uint8_t index = 0x0F & p.ptr[(y * p.stride) + x]; + got[(y * width) + x] = index ? ('0' + index) : '.'; + } + } + got[width * height] = 0; + + const char* want = want_pixel_indexes[i]; + if (memcmp(got, want, width * height)) { + FAIL("decode_frame #%" PRIu32 ": got \"%s\", want \"%s\"", i, got, + want); + return; + } + } + } } void test_wuffs_gif_decode_input_is_a_gif() { diff --git a/test/data/artificial/gif-frame-out-of-bounds.gif b/test/data/artificial/gif-frame-out-of-bounds.gif index 1a0b69f6e5a3aab9ecae8c20212ffe4516fe2441..df0baeef3ef108386143a49b1ea69aefa42ec070 100644 GIT binary patch delta 29 kcmd06VVsaFuEWH@z`(-5%)r3HxnxH3q^LDJ#Tgl_0bHL3tpET3 delta 7 OcmZo+%$tzQXbk`flL9RO diff --git a/test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt b/test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt index 741e165a..aa2fb462 100644 --- a/test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt +++ b/test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt @@ -6,6 +6,7 @@ # - frame #0 has bounds (1, 0) - (4, 1). # - frame #1 has bounds (0, 1) - (2, 3). # - frame #2 has bounds (0, 4) - (1, 5). +# - frame #3 has bounds (2, 0) - (6, 3). # # Here are some (but not all) possible decoding policies: # @@ -88,4 +89,9 @@ frame { } lzw 4 0x0E +frame { + frameLeftTopWidthHeight 2 0 4 3 +} +lzw 4 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F + trailer