diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index ef8bfdc9..b56a086a 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -1213,8 +1213,9 @@ typedef int32_t wuffs_gif__status; #define WUFFS_GIF__ERROR_TOO_MUCH_PIXEL_DATA -1105848314 // 0xBE161806 #define WUFFS_GIF__ERROR_INTERNAL_ERROR_INCONSISTENT_RI_WI \ -1105848313 // 0xBE161807 -#define WUFFS_GIF__ERROR_LZW_CODE_IS_OUT_OF_RANGE -1105848312 // 0xBE161808 -#define WUFFS_GIF__ERROR_LZW_PREFIX_CHAIN_IS_CYCLICAL -1105848311 // 0xBE161809 +#define WUFFS_GIF__ERROR_TODO_UNSUPPORTED_INTERLACING -1105848312 // 0xBE161808 +#define WUFFS_GIF__ERROR_LZW_CODE_IS_OUT_OF_RANGE -1105848311 // 0xBE161809 +#define WUFFS_GIF__ERROR_LZW_PREFIX_CHAIN_IS_CYCLICAL -1105848310 // 0xBE16180A bool wuffs_gif__status__is_error(wuffs_gif__status s); @@ -1355,9 +1356,9 @@ typedef struct { uint32_t v_num_palette_entries; uint32_t v_i; uint8_t v_lw; + bool v_write_to_ib_instead_of_w; uint64_t v_block_size; wuffs_gif__status v_z; - bool v_write_to_ib_instead_of_w; uint64_t v_n_copied; } c_decode_id_part1[1]; } private_impl; @@ -1946,7 +1947,7 @@ bool wuffs_gif__status__is_error(wuffs_gif__status s) { return s < 0; } -const char* wuffs_gif__status__strings[10] = { +const char* wuffs_gif__status__strings[11] = { "gif: bad block", "gif: bad extension label", "gif: bad graphic control", @@ -1955,6 +1956,7 @@ const char* wuffs_gif__status__strings[10] = { "gif: not enough pixel data", "gif: too much pixel data", "gif: internal error: inconsistent ri/wi", + "gif: TODO: unsupported interlacing", "gif: LZW code is out of range", "gif: LZW prefix chain is cyclical", }; @@ -1969,7 +1971,7 @@ const char* wuffs_gif__status__string(wuffs_gif__status s) { break; case wuffs_gif__packageid: a = wuffs_gif__status__strings; - n = 10; + n = 11; break; } uint32_t i = s & 0xFF; @@ -3314,6 +3316,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1( uint32_t v_num_palette_entries; uint32_t v_i; uint8_t v_lw; + bool v_write_to_ib_instead_of_w; uint64_t v_block_size; wuffs_base__io_writer v_w; wuffs_base__io_buffer u_w; @@ -3323,7 +3326,6 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1( WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(ioptr_w); WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(iobounds1_w); wuffs_gif__status v_z; - bool v_write_to_ib_instead_of_w; wuffs_base__slice_u8 v_pass_through; uint64_t v_n_copied; wuffs_base__slice_u8 v_palette; @@ -3372,18 +3374,18 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1( self->private_impl.c_decode_id_part1[0].v_num_palette_entries; v_i = self->private_impl.c_decode_id_part1[0].v_i; v_lw = self->private_impl.c_decode_id_part1[0].v_lw; + v_write_to_ib_instead_of_w = + self->private_impl.c_decode_id_part1[0].v_write_to_ib_instead_of_w; 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_write_to_ib_instead_of_w = - self->private_impl.c_decode_id_part1[0].v_write_to_ib_instead_of_w; v_pass_through = ((wuffs_base__slice_u8){}); v_n_copied = self->private_impl.c_decode_id_part1[0].v_n_copied; v_palette = ((wuffs_base__slice_u8){}); } else { v_use_local_palette = false; - v_w = ((wuffs_base__io_writer){}); v_write_to_ib_instead_of_w = false; + v_w = ((wuffs_base__io_writer){}); v_pass_through = ((wuffs_base__slice_u8){}); v_palette = ((wuffs_base__slice_u8){}); } @@ -3457,6 +3459,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1( } wuffs_gif__lzw_decoder__set_literal_width(&self->private_impl.f_lzw, ((uint32_t)(v_lw))); + v_write_to_ib_instead_of_w = false; self->private_impl.f_previous_lzw_decode_ended_abruptly = true; while (true) { { @@ -3519,7 +3522,6 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1( a_src = o_0_a_src; } if ((v_z == 0) || (v_z == WUFFS_GIF__SUSPENSION_SHORT_WRITE)) { - v_write_to_ib_instead_of_w = false; if (!v_write_to_ib_instead_of_w) { while (self->private_impl.f_uncompressed_ri < self->private_impl.f_uncompressed_wi) { @@ -3565,6 +3567,15 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1( label_1_break:; } label_0_break:; + if ((self->private_impl.f_uncompressed_ri != + self->private_impl.f_uncompressed_wi) && + v_write_to_ib_instead_of_w) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10); + status = wuffs_gif__decoder__copy_to_image_buffer(self, a_ib); + if (status) { + goto suspend; + } + } v_palette = ((wuffs_base__slice_u8){}); if (v_use_local_palette) { v_palette = ((wuffs_base__slice_u8){ @@ -3599,10 +3610,10 @@ suspend: v_num_palette_entries; self->private_impl.c_decode_id_part1[0].v_i = v_i; self->private_impl.c_decode_id_part1[0].v_lw = v_lw; - self->private_impl.c_decode_id_part1[0].v_block_size = v_block_size; - self->private_impl.c_decode_id_part1[0].v_z = v_z; self->private_impl.c_decode_id_part1[0].v_write_to_ib_instead_of_w = v_write_to_ib_instead_of_w; + self->private_impl.c_decode_id_part1[0].v_block_size = v_block_size; + self->private_impl.c_decode_id_part1[0].v_z = v_z; self->private_impl.c_decode_id_part1[0].v_n_copied = v_n_copied; goto exit; @@ -3693,6 +3704,15 @@ static wuffs_gif__status wuffs_gif__decoder__copy_to_image_buffer( self->private_impl.f_dst_y = ((uint32_t)(((v_i / ((uint64_t)(v_tab.width))) & 4294967295))); } + if (self->private_impl.f_uncompressed_ri == + self->private_impl.f_uncompressed_wi) { + self->private_impl.f_uncompressed_ri = 0; + self->private_impl.f_uncompressed_wi = 0; + } else if (self->private_impl.f_uncompressed_ri > + self->private_impl.f_uncompressed_wi) { + status = WUFFS_GIF__ERROR_INTERNAL_ERROR_INCONSISTENT_RI_WI; + goto exit; + } status = WUFFS_GIF__STATUS_OK; goto ok; } @@ -3709,12 +3729,23 @@ label_0_continue:; goto exit; } if (self->private_impl.f_interlace) { + status = WUFFS_GIF__ERROR_TODO_UNSUPPORTED_INTERLACING; + goto exit; } else { v_dst = wuffs_base__table_u8__row(v_tab, self->private_impl.f_dst_y); } if (((uint64_t)(self->private_impl.f_dst_x)) < ((uint64_t)(v_dst.len))) { - v_dst = wuffs_base__slice_u8__subslice_i( - v_dst, ((uint64_t)(self->private_impl.f_dst_x))); + if ((((uint64_t)(self->private_impl.f_dst_x)) <= + ((uint64_t)(self->private_impl.f_dst_x1))) && + (((uint64_t)(self->private_impl.f_dst_x1)) <= + ((uint64_t)(v_dst.len)))) { + v_dst = wuffs_base__slice_u8__subslice_ij( + v_dst, ((uint64_t)(self->private_impl.f_dst_x)), + ((uint64_t)(self->private_impl.f_dst_x1))); + } else { + v_dst = wuffs_base__slice_u8__subslice_i( + v_dst, ((uint64_t)(self->private_impl.f_dst_x))); + } v_n = ((uint32_t)( (wuffs_base__slice_u8__copy_from_slice(v_dst, v_src) & 4294967295))); v_new_ri = @@ -3728,8 +3759,11 @@ label_0_continue:; self->private_impl.f_dst_y += 1; goto label_0_continue; } - if (self->private_impl.f_uncompressed_wi <= + if (self->private_impl.f_uncompressed_wi == self->private_impl.f_uncompressed_ri) { + goto label_0_break; + } else if (self->private_impl.f_uncompressed_wi < + self->private_impl.f_uncompressed_ri) { status = WUFFS_GIF__ERROR_INTERNAL_ERROR_INCONSISTENT_RI_WI; goto exit; } @@ -3753,6 +3787,8 @@ label_0_continue:; goto label_0_break; } label_0_break:; + self->private_impl.f_uncompressed_ri = 0; + self->private_impl.f_uncompressed_wi = 0; goto ok; ok: diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index 01e898d2..a595e81c 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -1213,8 +1213,9 @@ typedef int32_t wuffs_gif__status; #define WUFFS_GIF__ERROR_TOO_MUCH_PIXEL_DATA -1105848314 // 0xBE161806 #define WUFFS_GIF__ERROR_INTERNAL_ERROR_INCONSISTENT_RI_WI \ -1105848313 // 0xBE161807 -#define WUFFS_GIF__ERROR_LZW_CODE_IS_OUT_OF_RANGE -1105848312 // 0xBE161808 -#define WUFFS_GIF__ERROR_LZW_PREFIX_CHAIN_IS_CYCLICAL -1105848311 // 0xBE161809 +#define WUFFS_GIF__ERROR_TODO_UNSUPPORTED_INTERLACING -1105848312 // 0xBE161808 +#define WUFFS_GIF__ERROR_LZW_CODE_IS_OUT_OF_RANGE -1105848311 // 0xBE161809 +#define WUFFS_GIF__ERROR_LZW_PREFIX_CHAIN_IS_CYCLICAL -1105848310 // 0xBE16180A bool wuffs_gif__status__is_error(wuffs_gif__status s); @@ -1355,9 +1356,9 @@ typedef struct { uint32_t v_num_palette_entries; uint32_t v_i; uint8_t v_lw; + bool v_write_to_ib_instead_of_w; uint64_t v_block_size; wuffs_gif__status v_z; - bool v_write_to_ib_instead_of_w; uint64_t v_n_copied; } c_decode_id_part1[1]; } private_impl; diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index b17021b7..bd79023a 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -24,6 +24,8 @@ pub error "too much pixel data" pri error "internal error: inconsistent ri/wi" +pub error "TODO: unsupported interlacing" + pub struct decoder?( width base.u32, height base.u32, @@ -418,6 +420,10 @@ pri func decoder.decode_id_part1?(ib ptr base.image_buffer, dst base.io_writer, } this.lzw.set_literal_width!(lw:lw as base.u32) + // TODO: delete the write_to_ib_instead_of_w hack. It should always be + // true. + var write_to_ib_instead_of_w base.bool = false + this.previous_lzw_decode_ended_abruptly = true while true { var block_size base.u64 = in.src.read_u8?() as base.u64 @@ -448,10 +454,6 @@ pri func decoder.decode_id_part1?(ib ptr base.image_buffer, dst base.io_writer, // // TODO: in.dst should be a ptr base.image_buffer and we should // composite the pixels instead of just copying them. - // - // TODO: delete the write_to_ib_instead_of_w hack. It should - // always be true. - var write_to_ib_instead_of_w base.bool = false if not write_to_ib_instead_of_w { while this.uncompressed_ri < this.uncompressed_wi { var pass_through slice base.u8 = this.uncompressed[this.uncompressed_ri:this.uncompressed_wi] @@ -484,6 +486,10 @@ pri func decoder.decode_id_part1?(ib ptr base.image_buffer, dst base.io_writer, } } + if (this.uncompressed_ri != this.uncompressed_wi) and write_to_ib_instead_of_w { + this.copy_to_image_buffer?(ib:in.ib) + } + // TODO: check for "not enough pixel data". var palette slice base.u8 @@ -560,6 +566,13 @@ pri func decoder.copy_to_image_buffer?(ib ptr base.image_buffer)() { this.dst_x = ((i % tab.width()) & 0xFFFFFFFF) as base.u32 this.dst_y = ((i / tab.width()) & 0xFFFFFFFF) as base.u32 } + + if this.uncompressed_ri == this.uncompressed_wi { + this.uncompressed_ri = 0 + this.uncompressed_wi = 0 + } else if this.uncompressed_ri > this.uncompressed_wi { + return error "internal error: inconsistent ri/wi" + } return } @@ -576,14 +589,19 @@ pri func decoder.copy_to_image_buffer?(ib ptr base.image_buffer)() { // in.ib's bounds. if this.interlace { - // TODO. + return error "TODO: unsupported interlacing" } else { dst = tab.row(y:this.dst_y) } if (this.dst_x as base.u64) < dst.length() { - // TODO: drop the "as base.u64" on the next line? - dst = dst[this.dst_x as base.u64:] + if ((this.dst_x as base.u64) <= (this.dst_x1 as base.u64)) and + ((this.dst_x1 as base.u64) <= dst.length()) { + // TODO: drop the "as base.u64"s on the next line? + dst = dst[this.dst_x as base.u64:this.dst_x1 as base.u64] + } else { + dst = dst[this.dst_x as base.u64:] + } n = (dst.copy_from_slice(s:src) & 0xFFFFFFFF) as base.u32 new_ri = this.uncompressed_ri ~sat+ n @@ -597,13 +615,15 @@ pri func decoder.copy_to_image_buffer?(ib ptr base.image_buffer)() { continue } - // Second, skip over src for that part of this.frame_rect that is - // outside in.ib's bounds. This second step should be infrequent. - - if this.uncompressed_wi <= this.uncompressed_ri { + if this.uncompressed_wi == this.uncompressed_ri { + break + } else if this.uncompressed_wi < this.uncompressed_ri { return error "internal error: inconsistent ri/wi" } + // Second, skip over src for that part of this.frame_rect that is + // outside in.ib's bounds. This second step should be infrequent. + // Set n to the number of pixels (i.e. the number of bytes) to skip. n = this.dst_x1 - this.dst_x n = n.min(x:this.uncompressed_wi - this.uncompressed_ri) @@ -623,4 +643,6 @@ pri func decoder.copy_to_image_buffer?(ib ptr base.image_buffer)() { } break } + this.uncompressed_ri = 0 + this.uncompressed_wi = 0 } diff --git a/test/c/std/gif.c b/test/c/std/gif.c index d0000568..d9fa45d9 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -355,6 +355,9 @@ void bench_wuffs_lzw_decode_100k() { // ---------------- GIF Tests +// TODO: delete this. +const bool write_to_ib_instead_of_w = false; + const char* wuffs_gif_decode(wuffs_base__io_buffer* dst, wuffs_base__io_buffer* src) { wuffs_gif__decoder dec; @@ -370,7 +373,11 @@ const char* wuffs_gif_decode(wuffs_base__io_buffer* dst, } // TODO: check wuffs_base__image_buffer__set_from_slice errors? wuffs_base__image_buffer__set_from_slice( - &ib, ic, wuffs_base__io_buffer__writable(dst)); + &ib, ic, + ((wuffs_base__slice_u8){ + .ptr = global_pixel_buffer, + .len = WUFFS_TESTLIB_ARRAY_SIZE(global_pixel_buffer), + })); while (true) { s = wuffs_gif__decoder__decode_frame(&dec, &ib, dst_writer, src_reader); if (s == WUFFS_GIF__SUSPENSION_END_OF_DATA) { @@ -379,6 +386,12 @@ const char* wuffs_gif_decode(wuffs_base__io_buffer* dst, if (s) { return wuffs_gif__status__string(s); } + if (write_to_ib_instead_of_w) { + const char* msg = copy_to_io_buffer_from_image_buffer(dst, &ib); + if (msg) { + return msg; + } + } } return NULL; } @@ -435,7 +448,11 @@ bool do_test_wuffs_gif_decode(const char* filename, } // TODO: check wuffs_base__image_buffer__set_from_slice errors? wuffs_base__image_buffer__set_from_slice( - &ib, ic, wuffs_base__io_buffer__writable(&got)); + &ib, ic, + ((wuffs_base__slice_u8){ + .ptr = global_pixel_buffer, + .len = WUFFS_TESTLIB_ARRAY_SIZE(global_pixel_buffer), + })); } int num_iters = 0; @@ -455,6 +472,13 @@ bool do_test_wuffs_gif_decode(const char* filename, wuffs_gif__status status = wuffs_gif__decoder__decode_frame(&dec, &ib, got_writer, src_reader); if (status == WUFFS_GIF__STATUS_OK) { + if (write_to_ib_instead_of_w) { + const char* msg = copy_to_io_buffer_from_image_buffer(&got, &ib); + if (msg) { + FAIL("%s", msg); + return false; + } + } break; } if ((status != WUFFS_GIF__SUSPENSION_SHORT_READ) && @@ -604,7 +628,11 @@ bool do_test_wuffs_gif_decode_animated( } // TODO: check wuffs_base__image_buffer__set_from_slice errors? wuffs_base__image_buffer__set_from_slice( - &ib, ic, wuffs_base__io_buffer__writable(&got)); + &ib, ic, + ((wuffs_base__slice_u8){ + .ptr = global_pixel_buffer, + .len = WUFFS_TESTLIB_ARRAY_SIZE(global_pixel_buffer), + })); uint32_t i; for (i = 0; i < want_num_frames; i++) { @@ -800,6 +828,11 @@ bool do_test_mimic_gif_decode(const char* filename) { return true; } +void test_mimic_gif_decode_animated_small() { + CHECK_FOCUS(__func__); + do_test_mimic_gif_decode("../../data/animated-red-blue.gif"); +} + void test_mimic_gif_decode_bricks_dither() { CHECK_FOCUS(__func__); do_test_mimic_gif_decode("../../data/bricks-dither.gif"); @@ -969,6 +1002,7 @@ proc tests[] = { #ifdef WUFFS_MIMIC + test_mimic_gif_decode_animated_small, // test_mimic_gif_decode_bricks_dither, // test_mimic_gif_decode_bricks_gray, // test_mimic_gif_decode_bricks_nodither, // diff --git a/test/c/testlib/testlib.c b/test/c/testlib/testlib.c index b48d8e6f..c59d767b 100644 --- a/test/c/testlib/testlib.c +++ b/test/c/testlib/testlib.c @@ -27,6 +27,7 @@ uint8_t global_got_buffer[BUFFER_SIZE]; uint8_t global_want_buffer[BUFFER_SIZE]; uint8_t global_src_buffer[BUFFER_SIZE]; +uint8_t global_pixel_buffer[BUFFER_SIZE]; uint8_t global_palette_buffer[PALETTE_BUFFER_SIZE]; char fail_msg[65536] = {0}; @@ -330,6 +331,31 @@ void set_writer_limit(wuffs_base__io_writer* o, uint64_t limit) { } } +const char* copy_to_io_buffer_from_image_buffer(wuffs_base__io_buffer* dst, + wuffs_base__image_buffer* src) { + // TODO: don't assume 1 plane or WUFFS_BASE__PIXEL_SUBSAMPLING__NONE. + uint32_t p; + for (p = 0; p < 1; p++) { + wuffs_base__table_u8 tab = wuffs_base__image_buffer__plane(src, p); + wuffs_base__rect_ie_u32 r = wuffs_base__image_buffer__dirty_rect(src); + uint32_t y; + for (y = r.min_inclusive_y; y < r.max_exclusive_y; y++) { + wuffs_base__slice_u8 row = wuffs_base__table_u8__row(tab, y); + if ((r.min_inclusive_x >= r.max_exclusive_x) || + (r.max_exclusive_x > row.len)) { + break; + } + uint32_t n = r.max_exclusive_x - r.min_inclusive_x; + if (n > (dst->len - dst->wi)) { + return "copy_to_io_buffer_from_image_buffer: dst buffer is too small"; + } + memmove(dst->ptr + dst->wi, row.ptr + r.min_inclusive_x, n); + dst->wi += n; + } + } + return NULL; +} + bool read_file(wuffs_base__io_buffer* dst, const char* path) { if (!dst || !path) { FAIL("read_file: NULL argument"); @@ -444,7 +470,8 @@ bool io_buffers_equal(const char* prefix, } char* msg = fail_msg; size_t i; - for (i = 0; (i < got->wi) && (i < want->wi); i++) { + size_t n = got->wi < want->wi ? got->wi : want->wi; + for (i = 0; i < n; i++) { if (got->ptr[i] != want->ptr[i]) { break; } @@ -453,7 +480,7 @@ bool io_buffers_equal(const char* prefix, INCR_FAIL(msg, "%sio_buffers_equal: wi: got %zu, want %zu.\n", prefix, got->wi, want->wi); } else if (i < got->wi) { - INCR_FAIL(msg, "%sio_buffers_equal:\n", prefix); + INCR_FAIL(msg, "%sio_buffers_equal: wi=%zu:\n", prefix, n); } else { return true; }