From ca9015bd2d41f9242462a8aafd981a897ae0ef9d Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 14 Sep 2019 08:35:50 +1000 Subject: [PATCH] Have std/gif replicate interlaced rows --- release/c/wuffs-unsupported-snapshot.c | 29 +++++++ std/gif/decode_gif.wuffs | 29 +++++++ test/c/std/gif.c | 73 ++++++++++++++++++ .../hippopotamus.interlaced.truncated.gif | Bin 0 -> 1024 bytes 4 files changed, 131 insertions(+) create mode 100644 test/data/hippopotamus.interlaced.truncated.gif diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 5fc393ad..779802c5 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -8561,6 +8561,12 @@ static const uint8_t // 1, 2, 4, 8, 8, }; +static const uint8_t // + wuffs_gif__interlace_count[5] // + WUFFS_BASE__POTENTIALLY_UNUSED = { + 0, 0, 1, 3, 7, +}; + static const uint8_t // wuffs_gif__animexts1dot0[11] // WUFFS_BASE__POTENTIALLY_UNUSED = { @@ -11043,6 +11049,9 @@ wuffs_gif__decoder__copy_to_image_buffer(wuffs_gif__decoder* self, wuffs_base__table_u8 v_tab = {0}; uint64_t v_i = 0; uint64_t v_j = 0; + uint32_t v_replicate_count = 0; + wuffs_base__slice_u8 v_replicate_dst = {0}; + wuffs_base__slice_u8 v_replicate_src = {0}; v_pixfmt_channels = (wuffs_base__pixel_buffer__pixel_format(a_pb) & 65535); if (v_pixfmt_channels == 34952) { @@ -11090,6 +11099,26 @@ label_0_continue:; } if (self->private_impl.f_frame_rect_x1 <= self->private_impl.f_dst_x) { self->private_impl.f_dst_x = self->private_impl.f_frame_rect_x0; + if (self->private_impl.f_interlace == 0) { + wuffs_base__u32__sat_add_indirect(&self->private_impl.f_dst_y, 1); + goto label_0_continue; + } + if ((self->private_impl.f_num_decoded_frames_value == 0) && + !self->private_impl.f_gc_has_transparent_index && + (self->private_impl.f_interlace > 1)) { + v_replicate_count = ((uint32_t)( + wuffs_gif__interlace_count[self->private_impl.f_interlace])); + v_replicate_src = + wuffs_base__table_u8__row(v_tab, self->private_impl.f_dst_y); + while (v_replicate_count > 0) { + v_replicate_dst = wuffs_base__table_u8__row( + v_tab, wuffs_base__u32__sat_add(self->private_impl.f_dst_y, + v_replicate_count)); + wuffs_base__slice_u8__copy_from_slice(v_replicate_dst, + v_replicate_src); + v_replicate_count -= 1; + } + } wuffs_base__u32__sat_add_indirect( &self->private_impl.f_dst_y, ((uint32_t)( diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 9c46e989..377db629 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -118,6 +118,7 @@ pub const quirk_reject_empty_palette base.u32 = (0xF8586 << 10) | 6 // to that maximum value (and therefore outside the frame rect). pri const interlace_start array[5] base.u32 = [0xFFFFFFFF, 1, 2, 4, 0] pri const interlace_delta array[5] base.u8 = [1, 2, 4, 8, 8] +pri const interlace_count array[5] base.u8 = [0, 0, 1, 3, 7] pub struct decoder?( width base.u32, @@ -1112,6 +1113,9 @@ pri func decoder.copy_to_image_buffer!(pb ptr base.pixel_buffer, src slice base. var tab table base.u8 var i base.u64 var j base.u64 + var replicate_count base.u32 + var replicate_dst slice base.u8 + var replicate_src slice base.u8 // TODO: a Wuffs (not just C) name for the WUFFS_BASE__PIXEL_FORMAT__ETC // magic pixfmt constants. Also, support more formats. @@ -1162,6 +1166,31 @@ pri func decoder.copy_to_image_buffer!(pb ptr base.pixel_buffer, src slice base. if this.frame_rect_x1 <= this.dst_x { this.dst_x = this.frame_rect_x0 + if this.interlace == 0 { + this.dst_y ~sat+= 1 + continue + } + + // For the first frame of an interlaced, non-transparent GIF, + // replicate the early passes' rows. For example, when such an + // image is downloaded over a slow network, this produces a richer + // intermediate image while waiting for the complete image. + // + // Some other GIF implementations call this progressive display or + // a "Haeberli inspired" technique. + if (this.num_decoded_frames_value == 0) and + (not this.gc_has_transparent_index) and + (this.interlace > 1) { + + replicate_count = interlace_count[this.interlace] as base.u32 + replicate_src = tab.row(y:this.dst_y) + while replicate_count > 0 { + replicate_dst = tab.row(y:this.dst_y ~sat+ replicate_count) + replicate_dst.copy_from_slice!(s:replicate_src) + replicate_count -= 1 + } + } + this.dst_y ~sat+= interlace_delta[this.interlace] as base.u32 while (this.interlace > 0) and (this.dst_y >= this.frame_rect_y1) { this.interlace -= 1 diff --git a/test/c/std/gif.c b/test/c/std/gif.c index e003235b..a8f0bd97 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -1255,6 +1255,78 @@ const char* test_wuffs_gif_decode_input_is_a_png() { return NULL; } +const char* test_wuffs_gif_decode_interlaced_truncated() { + CHECK_FOCUS(__func__); + + wuffs_base__io_buffer src = ((wuffs_base__io_buffer){ + .data = global_src_slice, + }); + + const char* status = + read_file(&src, "test/data/hippopotamus.interlaced.truncated.gif"); + if (status) { + return status; + } + + wuffs_gif__decoder dec; + status = wuffs_gif__decoder__initialize( + &dec, sizeof dec, WUFFS_VERSION, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED); + if (status) { + RETURN_FAIL("initialize: \"%s\"", status); + } + wuffs_base__image_config ic = ((wuffs_base__image_config){}); + + status = wuffs_gif__decoder__decode_image_config(&dec, &ic, &src); + if (status) { + RETURN_FAIL("decode_image_config: \"%s\"", status); + } + if (wuffs_base__pixel_config__width(&ic.pixcfg) != 36) { + RETURN_FAIL("width: got %" PRIu32 ", want 36", + wuffs_base__pixel_config__width(&ic.pixcfg)); + } + if (wuffs_base__pixel_config__height(&ic.pixcfg) != 28) { + RETURN_FAIL("height: got %" PRIu32 ", want 28", + wuffs_base__pixel_config__height(&ic.pixcfg)); + } + if (wuffs_base__pixel_config__pixel_format(&ic.pixcfg) != + WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY) { + RETURN_FAIL("pixel_format: got 0x%08" PRIX32 ", want 0x%08" PRIX32, + wuffs_base__pixel_config__pixel_format(&ic.pixcfg), + WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY); + } + const int num_pixel_indexes = 36 * 28 * 1; + + wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){}); + status = wuffs_base__pixel_buffer__set_from_slice(&pb, &ic.pixcfg, + global_pixel_slice); + if (status) { + return status; + } + uint8_t* pixel_ptr = wuffs_base__pixel_buffer__plane(&pb, 0).ptr; + memset(pixel_ptr, 0xEE, num_pixel_indexes); + + if (pixel_ptr[num_pixel_indexes - 1] != 0xEE) { + RETURN_FAIL("final pixel index, before: got 0x%02X, want 0x%02X", + pixel_ptr[num_pixel_indexes - 1], 0xEE); + } + + status = wuffs_gif__decoder__decode_frame(&dec, &pb, &src, global_work_slice, + NULL); + if (status != wuffs_base__suspension__short_read) { + RETURN_FAIL("decode_frame: got \"%s\", want \"%s\"", status, + wuffs_base__suspension__short_read); + } + + // Even though the source GIF data was truncated, replicating the interlaced + // rows should have set the bottom right pixel. + if (pixel_ptr[num_pixel_indexes - 1] == 0xEE) { + RETURN_FAIL("final pixel index, after: got 0x%02X, want not 0x%02X", + pixel_ptr[num_pixel_indexes - 1], 0xEE); + } + return NULL; +} + const char* do_test_wuffs_gif_decode_metadata(bool full) { wuffs_base__io_buffer src = ((wuffs_base__io_buffer){ .data = global_src_slice, @@ -2252,6 +2324,7 @@ proc tests[] = { test_wuffs_gif_decode_input_is_a_gif_many_medium_reads, // test_wuffs_gif_decode_input_is_a_gif_many_small_reads, // test_wuffs_gif_decode_input_is_a_png, // + test_wuffs_gif_decode_interlaced_truncated, // test_wuffs_gif_decode_metadata_empty, // test_wuffs_gif_decode_metadata_full, // test_wuffs_gif_decode_missing_two_src_bytes, // diff --git a/test/data/hippopotamus.interlaced.truncated.gif b/test/data/hippopotamus.interlaced.truncated.gif new file mode 100644 index 0000000000000000000000000000000000000000..15bf87a68bb3478e105a3663281d1c0f5f67d23f GIT binary patch literal 1024 zcmZ?wbhEHbRAG=|_|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