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 00000000..15bf87a6 Binary files /dev/null and b/test/data/hippopotamus.interlaced.truncated.gif differ