From 9514b12c7f0a36a4bbab178d0d5fcb1b87a34011 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 21 Jul 2018 21:45:33 +1000 Subject: [PATCH] Spin decode_frame_config out of decode_frame --- example/gifplayer/gifplayer.c | 24 +- fuzz/c/std/gif_fuzzer.c | 5 +- release/c/wuffs-unsupported-snapshot.h | 327 +++++++++++++++++++------ std/gif/decode_gif.wuffs | 146 +++++++---- test/c/std/gif.c | 99 ++++++-- 5 files changed, 438 insertions(+), 163 deletions(-) diff --git a/example/gifplayer/gifplayer.c b/example/gifplayer/gifplayer.c index 43982299..a1c13323 100644 --- a/example/gifplayer/gifplayer.c +++ b/example/gifplayer/gifplayer.c @@ -309,13 +309,25 @@ const char* play() { } while (1) { - // TODO: add Wuffs API so that we only make the copy if needed: if the - // upcoming frame is WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS. - memcpy(prev_dst_buffer, dst_buffer, dst_len); - wuffs_base__frame_config fc = ((wuffs_base__frame_config){}); - wuffs_base__status s = wuffs_gif__decoder__decode_frame( - &dec, &fc, &pb, src_reader, ((wuffs_base__slice_u8){})); + wuffs_base__status s = + wuffs_gif__decoder__decode_frame_config(&dec, &fc, src_reader); + if (s) { + if (s == WUFFS_BASE__SUSPENSION_END_OF_DATA) { + break; + } + return wuffs_gif__status__string(s); + } + + switch (wuffs_base__frame_config__disposal(&fc)) { + case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS: { + memcpy(prev_dst_buffer, dst_buffer, dst_len); + break; + } + } + + s = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, + ((wuffs_base__slice_u8){}), 0, 0); if (s) { if (s == WUFFS_BASE__SUSPENSION_END_OF_DATA) { break; diff --git a/fuzz/c/std/gif_fuzzer.c b/fuzz/c/std/gif_fuzzer.c index d5a26e47..06a5a78c 100644 --- a/fuzz/c/std/gif_fuzzer.c +++ b/fuzz/c/std/gif_fuzzer.c @@ -91,9 +91,8 @@ const char* fuzz(wuffs_base__io_reader src_reader, uint32_t hash) { bool seen_ok = false; while (true) { - wuffs_base__frame_config fc = ((wuffs_base__frame_config){}); - s = wuffs_gif__decoder__decode_frame(&dec, &fc, &pb, src_reader, - ((wuffs_base__slice_u8){})); + s = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, + ((wuffs_base__slice_u8){}), 0, 0); if (s) { if ((s == WUFFS_BASE__SUSPENSION_END_OF_DATA) && seen_ok) { s = WUFFS_BASE__STATUS_OK; diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index d4fd9527..59040d89 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -2527,6 +2527,7 @@ typedef struct { bool f_has_full_global_palette; bool f_has_full_palette; bool f_use_local_palette; + uint8_t f_which_palette; uint8_t f_interlace; bool f_seen_num_loops; uint32_t f_num_loops; @@ -2554,6 +2555,13 @@ typedef struct { uint32_t v_num_loops; bool v_ffio; } c_decode_image_config[1]; + struct { + uint32_t coro_susp_point; + } c_decode_frame_config[1]; + struct { + uint32_t coro_susp_point; + uint8_t v_lw; + } c_skip_frame[1]; struct { uint32_t coro_susp_point; } c_decode_frame[1]; @@ -2620,10 +2628,13 @@ typedef struct { wuffs_base__io_reader a_src); inline uint64_t frame_count(); inline wuffs_base__range_ii_u64 work_buffer_size(); - inline wuffs_base__status decode_frame(wuffs_base__frame_config* a_dst0, - wuffs_base__pixel_buffer* a_dst1, + inline wuffs_base__status decode_frame_config(wuffs_base__frame_config* a_dst, + wuffs_base__io_reader a_src); + inline wuffs_base__status decode_frame(wuffs_base__pixel_buffer* a_dst, wuffs_base__io_reader a_src, - wuffs_base__slice_u8 a_work_buffer); + wuffs_base__slice_u8 a_work_buffer, + uint32_t a_pixbuf_origin_x, + uint32_t a_pixbuf_origin_y); #endif // __cplusplus } wuffs_gif__decoder; @@ -2652,12 +2663,18 @@ wuffs_gif__decoder__frame_count(wuffs_gif__decoder* self); WUFFS_BASE__MAYBE_STATIC wuffs_base__range_ii_u64 // wuffs_gif__decoder__work_buffer_size(wuffs_gif__decoder* self); +WUFFS_BASE__MAYBE_STATIC wuffs_base__status // +wuffs_gif__decoder__decode_frame_config(wuffs_gif__decoder* self, + wuffs_base__frame_config* a_dst, + wuffs_base__io_reader a_src); + WUFFS_BASE__MAYBE_STATIC wuffs_base__status // wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self, - wuffs_base__frame_config* a_dst0, - wuffs_base__pixel_buffer* a_dst1, + wuffs_base__pixel_buffer* a_dst, wuffs_base__io_reader a_src, - wuffs_base__slice_u8 a_work_buffer); + wuffs_base__slice_u8 a_work_buffer, + uint32_t a_pixbuf_origin_x, + uint32_t a_pixbuf_origin_y); // ---------------- C++ Convenience Methods @@ -2687,12 +2704,19 @@ wuffs_gif__decoder::work_buffer_size() { } inline wuffs_base__status // -wuffs_gif__decoder::decode_frame(wuffs_base__frame_config* a_dst0, - wuffs_base__pixel_buffer* a_dst1, +wuffs_gif__decoder::decode_frame_config(wuffs_base__frame_config* a_dst, + wuffs_base__io_reader a_src) { + return wuffs_gif__decoder__decode_frame_config(this, a_dst, a_src); +} + +inline wuffs_base__status // +wuffs_gif__decoder::decode_frame(wuffs_base__pixel_buffer* a_dst, wuffs_base__io_reader a_src, - wuffs_base__slice_u8 a_work_buffer) { - return wuffs_gif__decoder__decode_frame(this, a_dst0, a_dst1, a_src, - a_work_buffer); + wuffs_base__slice_u8 a_work_buffer, + uint32_t a_pixbuf_origin_x, + uint32_t a_pixbuf_origin_y) { + return wuffs_gif__decoder__decode_frame(this, a_dst, a_src, a_work_buffer, + a_pixbuf_origin_x, a_pixbuf_origin_y); } #endif // __cplusplus @@ -6628,6 +6652,10 @@ static const uint8_t wuffs_gif__netscape2dot0[11] = { // ---------------- Private Function Prototypes +static wuffs_base__status // +wuffs_gif__decoder__skip_frame(wuffs_gif__decoder* self, + wuffs_base__io_reader a_src); + static wuffs_base__status // wuffs_gif__decoder__decode_up_to_id_part1(wuffs_gif__decoder* self, wuffs_base__io_reader a_src); @@ -6662,8 +6690,7 @@ wuffs_gif__decoder__decode_id_part0(wuffs_gif__decoder* self, static wuffs_base__status // wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, - wuffs_base__frame_config* a_dst0, - wuffs_base__pixel_buffer* a_dst1, + wuffs_base__pixel_buffer* a_dst, wuffs_base__io_reader a_src); static wuffs_base__status // @@ -6824,14 +6851,12 @@ wuffs_gif__decoder__work_buffer_size(wuffs_gif__decoder* self) { 0); } -// -------- func gif.decoder.decode_frame +// -------- func gif.decoder.decode_frame_config WUFFS_BASE__MAYBE_STATIC wuffs_base__status // -wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self, - wuffs_base__frame_config* a_dst0, - wuffs_base__pixel_buffer* a_dst1, - wuffs_base__io_reader a_src, - wuffs_base__slice_u8 a_work_buffer) { +wuffs_gif__decoder__decode_frame_config(wuffs_gif__decoder* self, + wuffs_base__frame_config* a_dst, + wuffs_base__io_reader a_src) { if (!self) { return WUFFS_BASE__ERROR_BAD_RECEIVER; } @@ -6842,7 +6867,194 @@ wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self, if (self->private_impl.status < 0) { return self->private_impl.status; } - if (!a_dst0 || !a_dst1) { + wuffs_base__status status = WUFFS_BASE__STATUS_OK; + + uint32_t coro_susp_point = + self->private_impl.c_decode_frame_config[0].coro_susp_point; + if (coro_susp_point) { + } else { + } + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + if (!self->private_impl.f_end_of_data) { + if (self->private_impl.f_call_sequence == 0) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + status = wuffs_gif__decoder__decode_image_config(self, NULL, a_src); + if (status) { + goto suspend; + } + } else if (self->private_impl.f_call_sequence == 1) { + } else if (self->private_impl.f_call_sequence == 2) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + status = wuffs_gif__decoder__skip_frame(self, a_src); + if (status) { + goto suspend; + } + } else { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); + status = wuffs_gif__decoder__decode_up_to_id_part1(self, a_src); + if (status) { + goto suspend; + } + } + } + if (self->private_impl.f_end_of_data) { + while (true) { + status = WUFFS_BASE__SUSPENSION_END_OF_DATA; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(4); + } + } + if (self->private_impl.f_use_local_palette || + self->private_impl.f_gc_has_transparent_index) { + self->private_impl.f_which_palette = 1; + } else if (!self->private_impl.f_previous_use_global_palette) { + self->private_impl.f_which_palette = 0; + } else { + self->private_impl.f_which_palette = 2; + } + self->private_impl.f_previous_use_global_palette = + !(self->private_impl.f_use_local_palette || + self->private_impl.f_gc_has_transparent_index); + if (a_dst != NULL) { + 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_gc_duration, true, + self->private_impl.f_gc_disposal, + (self->private_impl.f_which_palette != 2)); + } + self->private_impl.f_call_sequence = 2; + + goto ok; + ok: + self->private_impl.c_decode_frame_config[0].coro_susp_point = 0; + goto exit; + } + + goto suspend; +suspend: + self->private_impl.c_decode_frame_config[0].coro_susp_point = coro_susp_point; + + goto exit; +exit: + self->private_impl.status = status; + return status; +} + +// -------- func gif.decoder.skip_frame + +static wuffs_base__status // +wuffs_gif__decoder__skip_frame(wuffs_gif__decoder* self, + wuffs_base__io_reader a_src) { + wuffs_base__status status = WUFFS_BASE__STATUS_OK; + + uint8_t v_lw; + + uint8_t* ioptr_src = NULL; + uint8_t* iobounds0orig_src = NULL; + uint8_t* iobounds1_src = NULL; + WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(iobounds0orig_src); + WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(iobounds1_src); + if (a_src.private_impl.buf) { + ioptr_src = a_src.private_impl.buf->ptr + a_src.private_impl.buf->ri; + if (!a_src.private_impl.bounds[0]) { + a_src.private_impl.bounds[0] = ioptr_src; + a_src.private_impl.bounds[1] = + a_src.private_impl.buf->ptr + a_src.private_impl.buf->wi; + } + iobounds0orig_src = a_src.private_impl.bounds[0]; + iobounds1_src = a_src.private_impl.bounds[1]; + } + + uint32_t coro_susp_point = self->private_impl.c_skip_frame[0].coro_susp_point; + if (coro_susp_point) { + v_lw = self->private_impl.c_skip_frame[0].v_lw; + } else { + } + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + if (WUFFS_BASE__UNLIKELY(ioptr_src == iobounds1_src)) { + goto short_read_src; + } + uint8_t t_0 = *ioptr_src++; + v_lw = t_0; + } + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + if (a_src.private_impl.buf) { + a_src.private_impl.buf->ri = ioptr_src - a_src.private_impl.buf->ptr; + } + status = wuffs_gif__decoder__skip_blocks(self, a_src); + if (a_src.private_impl.buf) { + ioptr_src = a_src.private_impl.buf->ptr + a_src.private_impl.buf->ri; + } + if (status) { + goto suspend; + } + self->private_impl.f_seen_graphic_control = false; + self->private_impl.f_gc_has_transparent_index = false; + self->private_impl.f_gc_transparent_index = 0; + self->private_impl.f_gc_disposal = 0; + self->private_impl.f_gc_duration = 0; + wuffs_base__u64__sat_add_indirect(&self->private_impl.f_frame_count_value, + 1); + self->private_impl.f_call_sequence = 3; + + goto ok; + ok: + self->private_impl.c_skip_frame[0].coro_susp_point = 0; + goto exit; + } + + goto suspend; +suspend: + self->private_impl.c_skip_frame[0].coro_susp_point = coro_susp_point; + self->private_impl.c_skip_frame[0].v_lw = v_lw; + + goto exit; +exit: + if (a_src.private_impl.buf) { + a_src.private_impl.buf->ri = ioptr_src - a_src.private_impl.buf->ptr; + } + + return status; + +short_read_src: + if (wuffs_base__io_reader__is_eof(a_src)) { + status = WUFFS_BASE__ERROR_UNEXPECTED_EOF; + goto exit; + } + status = WUFFS_BASE__SUSPENSION_SHORT_READ; + goto suspend; +} + +// -------- func gif.decoder.decode_frame + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status // +wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self, + wuffs_base__pixel_buffer* a_dst, + wuffs_base__io_reader a_src, + wuffs_base__slice_u8 a_work_buffer, + uint32_t a_pixbuf_origin_x, + uint32_t a_pixbuf_origin_y) { + if (!self) { + return WUFFS_BASE__ERROR_BAD_RECEIVER; + } + if (self->private_impl.magic != WUFFS_BASE__MAGIC) { + self->private_impl.status = + WUFFS_BASE__ERROR_CHECK_WUFFS_VERSION_NOT_CALLED; + } + if (self->private_impl.status < 0) { + return self->private_impl.status; + } + if (!a_dst) { self->private_impl.status = WUFFS_BASE__ERROR_BAD_ARGUMENT; return WUFFS_BASE__ERROR_BAD_ARGUMENT; } @@ -6856,37 +7068,26 @@ wuffs_gif__decoder__decode_frame(wuffs_gif__decoder* self, switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence == 0) { + if (self->private_impl.f_call_sequence != 2) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); - status = wuffs_gif__decoder__decode_image_config(self, NULL, a_src); + status = wuffs_gif__decoder__decode_frame_config(self, NULL, a_src); if (status) { goto suspend; } } - if (!self->private_impl.f_end_of_data) { - if (self->private_impl.f_call_sequence == 1) { - self->private_impl.f_call_sequence = 2; - } else { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); - status = wuffs_gif__decoder__decode_up_to_id_part1(self, a_src); - if (status) { - goto suspend; - } - } - } - if (self->private_impl.f_end_of_data) { - while (true) { - status = WUFFS_BASE__SUSPENSION_END_OF_DATA; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3); - } - } - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); - status = wuffs_gif__decoder__decode_id_part1(self, a_dst0, a_dst1, a_src); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + status = wuffs_gif__decoder__decode_id_part1(self, a_dst, a_src); if (status) { goto suspend; } + self->private_impl.f_seen_graphic_control = false; + self->private_impl.f_gc_has_transparent_index = false; + self->private_impl.f_gc_transparent_index = 0; + self->private_impl.f_gc_disposal = 0; + self->private_impl.f_gc_duration = 0; wuffs_base__u64__sat_add_indirect(&self->private_impl.f_frame_count_value, 1); + self->private_impl.f_call_sequence = 3; goto ok; ok: @@ -8120,8 +8321,7 @@ short_read_src: static wuffs_base__status // wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, - wuffs_base__frame_config* a_dst0, - wuffs_base__pixel_buffer* a_dst1, + wuffs_base__pixel_buffer* a_dst, wuffs_base__io_reader a_src) { wuffs_base__status status = WUFFS_BASE__STATUS_OK; @@ -8135,7 +8335,6 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(ioptr_w); WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(iobounds1_w); wuffs_base__status v_z; - wuffs_base__slice_u8 v_palette; uint8_t* ioptr_src = NULL; uint8_t* iobounds0orig_src = NULL; @@ -8160,10 +8359,8 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, v_block_size = self->private_impl.c_decode_id_part1[0].v_block_size; v_w = ((wuffs_base__io_writer){}); v_z = self->private_impl.c_decode_id_part1[0].v_z; - v_palette = ((wuffs_base__slice_u8){}); } else { v_w = ((wuffs_base__io_writer){}); - v_palette = ((wuffs_base__slice_u8){}); } switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; @@ -8189,6 +8386,11 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, (4 * ((uint32_t)(self->private_impl.f_gc_transparent_index))) + 3)] = 0; } + wuffs_base__pixel_buffer__set_palette( + a_dst, + ((wuffs_base__slice_u8){.ptr = self->private_impl.f_palettes[( + self->private_impl.f_which_palette & 1)], + .len = 1024})); if (self->private_impl.f_previous_lzw_decode_ended_abruptly) { (memset(&self->private_impl.f_lzw, 0, sizeof((wuffs_lzw__decoder){})), wuffs_lzw__decoder__check_wuffs_version(&self->private_impl.f_lzw, @@ -8273,7 +8475,7 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, } if ((v_z == 0) || (v_z == WUFFS_BASE__SUSPENSION_SHORT_WRITE)) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); - status = wuffs_gif__decoder__copy_to_image_buffer(self, a_dst1); + status = wuffs_gif__decoder__copy_to_image_buffer(self, a_dst); if (status) { goto suspend; } @@ -8295,40 +8497,11 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, if (self->private_impl.f_uncompressed_ri != self->private_impl.f_uncompressed_wi) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); - status = wuffs_gif__decoder__copy_to_image_buffer(self, a_dst1); + status = wuffs_gif__decoder__copy_to_image_buffer(self, a_dst); if (status) { goto suspend; } } - v_palette = ((wuffs_base__slice_u8){}); - if (self->private_impl.f_use_local_palette || - self->private_impl.f_gc_has_transparent_index) { - v_palette = ((wuffs_base__slice_u8){ - .ptr = self->private_impl.f_palettes[1], .len = 1024}); - } else if (!self->private_impl.f_previous_use_global_palette) { - v_palette = ((wuffs_base__slice_u8){ - .ptr = self->private_impl.f_palettes[0], .len = 1024}); - } - wuffs_base__frame_config__update( - a_dst0, - 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_gc_duration, true, - self->private_impl.f_gc_disposal, (((uint64_t)(v_palette.len)) != 0)); - if (((uint64_t)(v_palette.len)) != 0) { - wuffs_base__pixel_buffer__set_palette(a_dst1, v_palette); - } - self->private_impl.f_previous_use_global_palette = - !(self->private_impl.f_use_local_palette || - self->private_impl.f_gc_has_transparent_index); - self->private_impl.f_seen_graphic_control = false; - self->private_impl.f_gc_has_transparent_index = false; - self->private_impl.f_gc_transparent_index = 0; - self->private_impl.f_gc_disposal = 0; - self->private_impl.f_gc_duration = 0; goto ok; ok: diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 44c3f2df..36f7242b 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -50,12 +50,28 @@ pub struct decoder?( // - 0: initial state. // - 1: image config decoded, including the first frame's bounds, but not // the first frame's pixels. - // - 2: frame decoded. + // - 2: frame config decoded. + // - 3: frame decoded. // - // Call sequence state transitions: - // - 0 -> 1: via decode_image_config. - // - 1 -> 2: via decode_frame. - // - 2 -> 2: via decode_frame. + // State transitions: + // + // - 0 -> 1: via IC + // - 0 -> 2: via FC with implicit IC + // - 0 -> 3: via F with implicit IC and FC + // + // - 1 -> 2: via FC + // - 1 -> 3: via F with implicit FC + // + // - 2 -> 2: via FC with implicit F + // - 2 -> 3: via F + // + // - 3 -> 2: via FC + // - 3 -> 3: via F with implicit FC + // + // Where: + // - F is decode_frame, implicit means skip_frame + // - FC is decode_frame_config, implicit means nullptr in.dst + // - IC is decode_image_config, implicit means nullptr in.dst call_sequence base.u8, end_of_data base.bool, @@ -66,6 +82,8 @@ pub struct decoder?( has_full_palette base.bool, use_local_palette base.bool, + which_palette base.u8[..2], + // interlace indexes the interlace_start and interlace_delta arrays. interlace base.u8[..4], @@ -145,31 +163,87 @@ pub func decoder.work_buffer_size()(ret base.range_ii_u64) { return this.util.make_range_ii_u64(min_incl:0, max_incl:0) } -// TODO: take pixbuf_origin_x and pixbuf_origin_y args. -pub func decoder.decode_frame?(dst0 ptr base.frame_config, dst1 ptr base.pixel_buffer, src base.io_reader, work_buffer slice base.u8)() { - if this.call_sequence == 0 { - this.decode_image_config?(dst:nullptr, src:in.src) - } - +pub func decoder.decode_frame_config?(dst nptr base.frame_config, src base.io_reader)() { if not this.end_of_data { - if this.call_sequence == 1 { - // TODO: memset dst's pixel_buffer (and palette) to zero? - this.call_sequence = 2 + if this.call_sequence == 0 { + this.decode_image_config?(dst:nullptr, src:in.src) + } else if this.call_sequence == 1 { + // No-op. + } else if this.call_sequence == 2 { + this.skip_frame?(src:in.src) } else { this.decode_up_to_id_part1?(src:in.src) } } - // This is a new "if", not an "else", because calling - // this.decode_up_to_id_part1 can modify this.end_of_data. + // This is a new "if", not an "else", because the calls above can modify + // this.end_of_data. if this.end_of_data { while true { yield suspension "end of data" } } - this.decode_id_part1?(dst0:in.dst0, dst1:in.dst1, src:in.src) + if this.use_local_palette or this.gc_has_transparent_index { + this.which_palette = 1 + } else if not this.previous_use_global_palette { + // This frame uses the global palette but the previous one didn't. + this.which_palette = 0 + } else { + // Both this frame and the previous one used the global palette. No + // palette update is necessary. + this.which_palette = 2 + } + this.previous_use_global_palette = + not (this.use_local_palette or this.gc_has_transparent_index) + + if in.dst != nullptr { + in.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), + duration:this.gc_duration, blend:true, disposal:this.gc_disposal, + palette_changed:this.which_palette != 2) + } + + this.call_sequence = 2 +} + +pri func decoder.skip_frame?(src base.io_reader)() { + var lw base.u8 = in.src.read_u8?() + this.skip_blocks?(src:in.src) + + // The Image Descriptor is mandatory, but the Graphic Control extension is + // optional. Reset the GC related fields for the next decode_frame call. + this.seen_graphic_control = false + this.gc_has_transparent_index = false + this.gc_transparent_index = 0 + this.gc_disposal = 0 + this.gc_duration = 0 + this.frame_count_value ~sat+= 1 + this.call_sequence = 3 +} + +// TODO: honor pixbuf_origin_x and pixbuf_origin_y args. +pub func decoder.decode_frame?(dst ptr base.pixel_buffer, src base.io_reader, work_buffer slice base.u8, pixbuf_origin_x base.u32, pixbuf_origin_y base.u32)() { + if this.call_sequence != 2 { + this.decode_frame_config?(dst:nullptr, src:in.src) + } + + this.decode_id_part1?(dst:in.dst, src:in.src) + + // The Image Descriptor is mandatory, but the Graphic Control extension is + // optional. Reset the GC related fields for the next decode_frame call. + this.seen_graphic_control = false + this.gc_has_transparent_index = false + this.gc_transparent_index = 0 + this.gc_disposal = 0 + this.gc_duration = 0 + + this.frame_count_value ~sat+= 1 + this.call_sequence = 3 } pri func decoder.decode_up_to_id_part1?(src base.io_reader)() { @@ -455,7 +529,7 @@ pri func decoder.decode_id_part0?(src base.io_reader)() { } } -pri func decoder.decode_id_part1?(dst0 ptr base.frame_config, dst1 ptr base.pixel_buffer, src base.io_reader)() { +pri func decoder.decode_id_part1?(dst ptr base.pixel_buffer, src base.io_reader)() { if this.gc_has_transparent_index { if not this.use_local_palette { this.palettes[1][:].copy_from_slice(s:this.palettes[0][:]) @@ -467,6 +541,7 @@ pri func decoder.decode_id_part1?(dst0 ptr base.frame_config, dst1 ptr base.pixe this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 2] = 0x00 this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 3] = 0x00 } + in.dst.set_palette!(palette:this.palettes[this.which_palette & 1][:]) // Other GIF implementations accept GIF files that aren't completely spec // compliant. For example, the test/data/gifplayer-muybridge.gif file @@ -519,7 +594,7 @@ pri func decoder.decode_id_part1?(dst0 ptr base.frame_config, dst1 ptr base.pixe } if z.is_ok() or (z == suspension "short write") { - this.copy_to_image_buffer?(pb:in.dst1) + this.copy_to_image_buffer?(pb:in.dst) if z == suspension "short write" { continue:inner @@ -536,41 +611,10 @@ pri func decoder.decode_id_part1?(dst0 ptr base.frame_config, dst1 ptr base.pixe } if this.uncompressed_ri != this.uncompressed_wi { - this.copy_to_image_buffer?(pb:in.dst1) + this.copy_to_image_buffer?(pb:in.dst) } // TODO: check for "not enough pixel data". - - var palette slice base.u8 - if this.use_local_palette or this.gc_has_transparent_index { - palette = this.palettes[1][:] - } else if not this.previous_use_global_palette { - // This frame uses the global palette but the previous one didn't. - palette = this.palettes[0][:] - } else { - // Both this frame and the previous one used the global palette. No - // palette update is necessary. - } - in.dst0.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), - duration:this.gc_duration, blend:true, disposal:this.gc_disposal, - palette_changed:palette.length() != 0) - if palette.length() != 0 { - in.dst1.set_palette!(palette:palette) - } - - this.previous_use_global_palette = not (this.use_local_palette or this.gc_has_transparent_index) - - // The Image Descriptor is mandatory, but the Graphic Control extension is - // optional. Reset the GC related fields for the next decode_frame call. - this.seen_graphic_control = false - this.gc_has_transparent_index = false - this.gc_transparent_index = 0 - this.gc_disposal = 0 - this.gc_duration = 0 } pri func decoder.copy_to_image_buffer?(pb ptr base.pixel_buffer)() { diff --git a/test/c/std/gif.c b/test/c/std/gif.c index cad7d158..43ee7ad8 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -237,14 +237,20 @@ const char* wuffs_gif_decode(wuffs_base__io_buffer* dst, } while (true) { - s = wuffs_gif__decoder__decode_frame(&dec, &fc, &pb, src_reader, - ((wuffs_base__slice_u8){})); - if (s == WUFFS_BASE__SUSPENSION_END_OF_DATA) { - break; + s = wuffs_gif__decoder__decode_frame_config(&dec, &fc, src_reader); + if (s) { + if (s == WUFFS_BASE__SUSPENSION_END_OF_DATA) { + break; + } + return wuffs_gif__status__string(s); } + + s = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, + ((wuffs_base__slice_u8){}), 0, 0); if (s) { return wuffs_gif__status__string(s); } + const char* msg = copy_to_io_buffer_from_pixel_buffer( dst, &pb, wuffs_base__frame_config__bounds(&fc)); if (msg) { @@ -270,7 +276,9 @@ bool do_test_wuffs_gif_decode(const char* filename, wuffs_gif__decoder dec = ((wuffs_gif__decoder){}); wuffs_gif__decoder__check_wuffs_version(&dec, sizeof dec, WUFFS_VERSION); + wuffs_base__frame_config fc = ((wuffs_base__frame_config){}); 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); @@ -330,17 +338,42 @@ bool do_test_wuffs_gif_decode(const char* filename, } size_t old_ri = src.ri; - wuffs_base__frame_config fc = ((wuffs_base__frame_config){}); + wuffs_base__status status = + wuffs_gif__decoder__decode_frame_config(&dec, &fc, src_reader); + + if (status == WUFFS_BASE__STATUS_OK) { + break; + } + if (status != WUFFS_BASE__SUSPENSION_SHORT_READ) { + FAIL("decode_frame_config: got %" PRIi32 " (%s), want %" PRIi32 " (%s)", + status, wuffs_gif__status__string(status), + WUFFS_BASE__SUSPENSION_SHORT_READ, + wuffs_gif__status__string(WUFFS_BASE__SUSPENSION_SHORT_READ)); + return false; + } + + if (src.ri < old_ri) { + FAIL("read index src.ri went backwards"); + return false; + } + if (src.ri == old_ri) { + FAIL("no progress was made"); + return false; + } + } + + while (true) { + num_iters++; + wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src); + if (rlimit) { + set_reader_limit(&src_reader, rlimit); + } + size_t old_ri = src.ri; + wuffs_base__status status = wuffs_gif__decoder__decode_frame( - &dec, &fc, &pb, src_reader, ((wuffs_base__slice_u8){})); + &dec, &pb, src_reader, ((wuffs_base__slice_u8){}), 0, 0); if (status == WUFFS_BASE__STATUS_OK) { - const char* msg = copy_to_io_buffer_from_pixel_buffer( - &got, &pb, wuffs_base__frame_config__bounds(&fc)); - if (msg) { - FAIL("%s", msg); - return false; - } break; } if (status != WUFFS_BASE__SUSPENSION_SHORT_READ) { @@ -360,14 +393,23 @@ bool do_test_wuffs_gif_decode(const char* filename, } } + { + const char* msg = copy_to_io_buffer_from_pixel_buffer( + &got, &pb, wuffs_base__frame_config__bounds(&fc)); + if (msg) { + FAIL("%s", msg); + return false; + } + } + if (rlimit) { - if (num_iters <= 1) { - FAIL("num_iters: got %d, want > 1", num_iters); + if (num_iters <= 2) { + FAIL("num_iters: got %d, want > 2", num_iters); return false; } } else { - if (num_iters != 1) { - FAIL("num_iters: got %d, want 1", num_iters); + if (num_iters != 2) { + FAIL("num_iters: got %d, want 2", num_iters); return false; } } @@ -400,9 +442,8 @@ bool do_test_wuffs_gif_decode(const char* filename, return false; } wuffs_base__io_reader src_reader = wuffs_base__io_buffer__reader(&src); - wuffs_base__frame_config fc = ((wuffs_base__frame_config){}); wuffs_base__status status = wuffs_gif__decoder__decode_frame( - &dec, &fc, &pb, src_reader, ((wuffs_base__slice_u8){})); + &dec, &pb, src_reader, ((wuffs_base__slice_u8){}), 0, 0); if (status != WUFFS_BASE__SUSPENSION_END_OF_DATA) { FAIL("decode_frame: got %" PRIi32 " (%s), want %" PRIi32 " (%s)", status, wuffs_gif__status__string(status), @@ -457,7 +498,7 @@ bool do_test_wuffs_gif_decode_animated( const char* filename, uint32_t want_num_loops, uint32_t want_num_frames, - wuffs_base__rect_ie_u32* want_dirty_rects) { + wuffs_base__rect_ie_u32* want_frame_config_bounds) { wuffs_base__io_buffer src = {.ptr = global_src_buffer, .len = BUFFER_SIZE}; if (!read_file(&src, filename)) { @@ -498,17 +539,24 @@ bool do_test_wuffs_gif_decode_animated( uint32_t i; for (i = 0; i < want_num_frames; i++) { wuffs_base__frame_config fc = ((wuffs_base__frame_config){}); - status = wuffs_gif__decoder__decode_frame(&dec, &fc, &pb, src_reader, - ((wuffs_base__slice_u8){})); + status = wuffs_gif__decoder__decode_frame_config(&dec, &fc, src_reader); + if (status != WUFFS_BASE__STATUS_OK) { + FAIL("decode_frame_config #%" PRIu32 ": got %" PRIi32 " (%s)", i, status, + wuffs_gif__status__string(status)); + return false; + } + + status = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, + ((wuffs_base__slice_u8){}), 0, 0); if (status != WUFFS_BASE__STATUS_OK) { FAIL("decode_frame #%" PRIu32 ": got %" PRIi32 " (%s)", i, status, wuffs_gif__status__string(status)); return false; } - if (want_dirty_rects) { + if (want_frame_config_bounds) { wuffs_base__rect_ie_u32 got = wuffs_base__frame_config__bounds(&fc); - wuffs_base__rect_ie_u32 want = want_dirty_rects[i]; + 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 @@ -522,9 +570,8 @@ bool do_test_wuffs_gif_decode_animated( } // There should be no more frames. - wuffs_base__frame_config fc = ((wuffs_base__frame_config){}); - status = wuffs_gif__decoder__decode_frame(&dec, &fc, &pb, src_reader, - ((wuffs_base__slice_u8){})); + status = wuffs_gif__decoder__decode_frame(&dec, &pb, src_reader, + ((wuffs_base__slice_u8){}), 0, 0); if (status != WUFFS_BASE__SUSPENSION_END_OF_DATA) { FAIL("decode_frame: got %" PRIi32 " (%s), want %" PRIi32 " (%s)", status, wuffs_gif__status__string(status), WUFFS_BASE__SUSPENSION_END_OF_DATA,