diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index f5a25850..127933ff 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -1204,13 +1204,17 @@ typedef int32_t wuffs_gif__status; #define WUFFS_GIF__ERROR_INVALID_CALL_SEQUENCE -2147483637 // 0x8000000B #define WUFFS_GIF__SUSPENSION_END_OF_DATA 12 // 0x0000000C -#define WUFFS_GIF__ERROR_BAD_BLOCK -1105848320 // 0xBE161800 -#define WUFFS_GIF__ERROR_BAD_EXTENSION_LABEL -1105848319 // 0xBE161801 -#define WUFFS_GIF__ERROR_BAD_GRAPHIC_CONTROL -1105848318 // 0xBE161802 -#define WUFFS_GIF__ERROR_BAD_HEADER -1105848317 // 0xBE161803 -#define WUFFS_GIF__ERROR_BAD_LITERAL_WIDTH -1105848316 // 0xBE161804 -#define WUFFS_GIF__ERROR_LZW_CODE_IS_OUT_OF_RANGE -1105848315 // 0xBE161805 -#define WUFFS_GIF__ERROR_LZW_PREFIX_CHAIN_IS_CYCLICAL -1105848314 // 0xBE161806 +#define WUFFS_GIF__ERROR_BAD_BLOCK -1105848320 // 0xBE161800 +#define WUFFS_GIF__ERROR_BAD_EXTENSION_LABEL -1105848319 // 0xBE161801 +#define WUFFS_GIF__ERROR_BAD_GRAPHIC_CONTROL -1105848318 // 0xBE161802 +#define WUFFS_GIF__ERROR_BAD_HEADER -1105848317 // 0xBE161803 +#define WUFFS_GIF__ERROR_BAD_LITERAL_WIDTH -1105848316 // 0xBE161804 +#define WUFFS_GIF__ERROR_NOT_ENOUGH_PIXEL_DATA -1105848315 // 0xBE161805 +#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 bool wuffs_gif__status__is_error(wuffs_gif__status s); @@ -1285,6 +1289,9 @@ typedef struct { wuffs_base__rect_ie_u32 f_frame_rect; uint32_t f_dst_x; uint32_t f_dst_y; + uint32_t f_dst_x0; + uint32_t f_dst_x1; + uint32_t f_dst_y1; uint32_t f_uncompressed_ri; uint32_t f_uncompressed_wi; uint8_t f_uncompressed[4096]; @@ -1939,12 +1946,15 @@ bool wuffs_gif__status__is_error(wuffs_gif__status s) { return s < 0; } -const char* wuffs_gif__status__strings[7] = { +const char* wuffs_gif__status__strings[10] = { "gif: bad block", "gif: bad extension label", "gif: bad graphic control", "gif: bad header", "gif: bad literal width", + "gif: not enough pixel data", + "gif: too much pixel data", + "gif: internal error: inconsistent ri/wi", "gif: LZW code is out of range", "gif: LZW prefix chain is cyclical", }; @@ -1959,7 +1969,7 @@ const char* wuffs_gif__status__string(wuffs_gif__status s) { break; case wuffs_gif__packageid: a = wuffs_gif__status__strings; - n = 7; + n = 10; break; } uint32_t i = s & 0xFF; @@ -3189,6 +3199,7 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part0( &self->private_impl.f_frame_rect, v_frame_y); self->private_impl.f_dst_x = v_frame_x; self->private_impl.f_dst_y = v_frame_y; + self->private_impl.f_dst_x0 = v_frame_x; { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); uint16_t t_5; @@ -3251,6 +3262,8 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part0( &self->private_impl.f_frame_rect, v_frame_x); wuffs_base__rect_ie_u32__set_max_exclusive_y( &self->private_impl.f_frame_rect, v_frame_y); + self->private_impl.f_dst_x1 = v_frame_x; + self->private_impl.f_dst_y1 = v_frame_y; if (self->private_impl.f_call_sequence == 0) { self->private_impl.f_width = wuffs_base__u32__max(self->private_impl.f_width, v_frame_x); @@ -3620,18 +3633,81 @@ static wuffs_gif__status wuffs_gif__decoder__copy_to_image_buffer( wuffs_gif__status status = WUFFS_GIF__STATUS_OK; wuffs_base__slice_u8 v_dst; + wuffs_base__slice_u8 v_src; wuffs_base__table_u8 v_tab; + uint32_t v_n; + uint32_t v_new_ri; v_dst = ((wuffs_base__slice_u8){}); + v_src = ((wuffs_base__slice_u8){}); v_tab = wuffs_base__image_buffer__plane(a_ib, 0); - if (((uint64_t)(v_tab.width)) == ((uint64_t)(v_tab.stride))) { + if (!self->private_impl.f_interlace && + (((uint64_t)(v_tab.width)) == ((uint64_t)(v_tab.stride)))) { v_dst = wuffs_base__table_u8__linearize(v_tab); if (((uint64_t)(v_dst.len)) == 0) { } status = WUFFS_GIF__STATUS_OK; goto ok; } - v_dst = wuffs_base__table_u8__row(v_tab, self->private_impl.f_dst_y); + v_n = 0; + v_new_ri = 0; +label_0_continue:; + while (self->private_impl.f_uncompressed_wi > + self->private_impl.f_uncompressed_ri) { + v_src = wuffs_base__slice_u8__subslice_ij( + ((wuffs_base__slice_u8){.ptr = self->private_impl.f_uncompressed, + .len = 4096}), + self->private_impl.f_uncompressed_ri, + self->private_impl.f_uncompressed_wi); + if (self->private_impl.f_dst_y >= self->private_impl.f_dst_y1) { + status = WUFFS_GIF__ERROR_TOO_MUCH_PIXEL_DATA; + goto exit; + } + if (self->private_impl.f_interlace) { + } 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))); + v_n = ((uint32_t)( + (wuffs_base__slice_u8__copy_from_slice(v_dst, v_src) & 4294967295))); + v_new_ri = + wuffs_base__u32__sat_add(v_n, self->private_impl.f_uncompressed_ri); + self->private_impl.f_uncompressed_ri = + wuffs_base__u32__min(v_new_ri, 4096); + wuffs_base__u32__sat_add_indirect(&self->private_impl.f_dst_x, v_n); + } + if (self->private_impl.f_dst_x1 <= self->private_impl.f_dst_x) { + self->private_impl.f_dst_x = self->private_impl.f_dst_x0; + self->private_impl.f_dst_y += 1; + goto label_0_continue; + } + if (self->private_impl.f_uncompressed_wi <= + self->private_impl.f_uncompressed_ri) { + status = WUFFS_GIF__ERROR_INTERNAL_ERROR_INCONSISTENT_RI_WI; + goto exit; + } + v_n = (self->private_impl.f_dst_x1 - self->private_impl.f_dst_x); + v_n = wuffs_base__u32__min(v_n, (self->private_impl.f_uncompressed_wi - + self->private_impl.f_uncompressed_ri)); + v_new_ri = + wuffs_base__u32__sat_add(v_n, self->private_impl.f_uncompressed_ri); + self->private_impl.f_uncompressed_ri = wuffs_base__u32__min(v_new_ri, 4096); + wuffs_base__u32__sat_add_indirect(&self->private_impl.f_dst_x, v_n); + if (self->private_impl.f_dst_x1 <= self->private_impl.f_dst_x) { + self->private_impl.f_dst_x = self->private_impl.f_dst_x0; + self->private_impl.f_dst_y += 1; + goto label_0_continue; + } + if (self->private_impl.f_uncompressed_ri != + self->private_impl.f_uncompressed_wi) { + status = WUFFS_GIF__ERROR_INTERNAL_ERROR_INCONSISTENT_RI_WI; + goto exit; + } + goto label_0_break; + } +label_0_break:; goto ok; ok: diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index a464582a..f544e4a1 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -1204,13 +1204,17 @@ typedef int32_t wuffs_gif__status; #define WUFFS_GIF__ERROR_INVALID_CALL_SEQUENCE -2147483637 // 0x8000000B #define WUFFS_GIF__SUSPENSION_END_OF_DATA 12 // 0x0000000C -#define WUFFS_GIF__ERROR_BAD_BLOCK -1105848320 // 0xBE161800 -#define WUFFS_GIF__ERROR_BAD_EXTENSION_LABEL -1105848319 // 0xBE161801 -#define WUFFS_GIF__ERROR_BAD_GRAPHIC_CONTROL -1105848318 // 0xBE161802 -#define WUFFS_GIF__ERROR_BAD_HEADER -1105848317 // 0xBE161803 -#define WUFFS_GIF__ERROR_BAD_LITERAL_WIDTH -1105848316 // 0xBE161804 -#define WUFFS_GIF__ERROR_LZW_CODE_IS_OUT_OF_RANGE -1105848315 // 0xBE161805 -#define WUFFS_GIF__ERROR_LZW_PREFIX_CHAIN_IS_CYCLICAL -1105848314 // 0xBE161806 +#define WUFFS_GIF__ERROR_BAD_BLOCK -1105848320 // 0xBE161800 +#define WUFFS_GIF__ERROR_BAD_EXTENSION_LABEL -1105848319 // 0xBE161801 +#define WUFFS_GIF__ERROR_BAD_GRAPHIC_CONTROL -1105848318 // 0xBE161802 +#define WUFFS_GIF__ERROR_BAD_HEADER -1105848317 // 0xBE161803 +#define WUFFS_GIF__ERROR_BAD_LITERAL_WIDTH -1105848316 // 0xBE161804 +#define WUFFS_GIF__ERROR_NOT_ENOUGH_PIXEL_DATA -1105848315 // 0xBE161805 +#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 bool wuffs_gif__status__is_error(wuffs_gif__status s); @@ -1285,6 +1289,9 @@ typedef struct { wuffs_base__rect_ie_u32 f_frame_rect; uint32_t f_dst_x; uint32_t f_dst_y; + uint32_t f_dst_x0; + uint32_t f_dst_x1; + uint32_t f_dst_y1; uint32_t f_uncompressed_ri; uint32_t f_uncompressed_wi; uint8_t f_uncompressed[4096]; diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index cbafdf3a..b927546b 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -188,6 +188,7 @@ const ( ) var SliceFuncs = []string{ + // TODO: should copy_from_slice be a ! method? "T1.copy_from_slice(s T1)(ret u64)", "T1.length()(ret u64)", "T1.prefix(up_to u64)(ret T1)", diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 90a8f27c..cead0961 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -19,6 +19,10 @@ pub error "bad extension label" pub error "bad graphic control" pub error "bad header" pub error "bad literal width" +pub error "not enough pixel data" +pub error "too much pixel data" + +pri error "internal error: inconsistent ri/wi" pub struct decoder?( width base.u32, @@ -60,6 +64,10 @@ pub struct decoder?( // The dst_etc fields are the output cursor during copy_to_image_buffer. dst_x base.u32, dst_y base.u32, + // TODO: these are just frame_rect fields. + dst_x0 base.u32, + dst_x1 base.u32, + dst_y1 base.u32, uncompressed_ri base.u32[..4096], uncompressed_wi base.u32[..4096], @@ -335,11 +343,14 @@ pri func decoder.decode_id_part0?(src base.io_reader)() { this.frame_rect.set_min_inclusive_y!(y:frame_y) this.dst_x = frame_x this.dst_y = frame_y + this.dst_x0 = frame_x frame_x ~mod+= in.src.read_u16le?() as base.u32 frame_y ~mod+= in.src.read_u16le?() as base.u32 this.frame_rect.set_max_exclusive_x!(x:frame_x) this.frame_rect.set_max_exclusive_y!(y:frame_y) + this.dst_x1 = frame_x + this.dst_y1 = frame_y // Set the image's overall width and height to be the maximum of the // nominal image width and height (given in the Logical Screen Descriptor) @@ -473,6 +484,8 @@ pri func decoder.decode_id_part1?(ib ptr base.image_buffer, dst base.io_writer, } } + // TODO: check for "not enough pixel data". + var palette slice base.u8 if use_local_palette { palette = this.palettes[1][:] @@ -499,8 +512,10 @@ pri func decoder.decode_id_part1?(ib ptr base.image_buffer, dst base.io_writer, pri func decoder.copy_to_image_buffer?(ib ptr base.image_buffer)() { // TODO: don't assume a packed pixel format. var dst slice base.u8 + var src slice base.u8 + var tab table base.u8 = in.ib.plane(p:0) - if tab.width() == tab.stride() { + if (not this.interlace) and (tab.width() == tab.stride()) { dst = tab.linearize() if dst.length() == 0 { // TODO. @@ -508,5 +523,67 @@ pri func decoder.copy_to_image_buffer?(ib ptr base.image_buffer)() { return } - dst = tab.row(y:this.dst_y) + var n base.u32 + // TODO: we shouldn't need this temporary variable. + var new_ri base.u32 + while this.uncompressed_wi > this.uncompressed_ri { + assert this.uncompressed_ri < this.uncompressed_wi via "a < b: b > a"() + src = this.uncompressed[this.uncompressed_ri:this.uncompressed_wi] + + if this.dst_y >= this.dst_y1 { + return error "too much pixel data" + } + assert this.dst_y < 0xFFFFFFFF via "a < b: a < c; c <= b"(c:this.dst_y1) + + // First, copy from src to that part of this.frame_rect that is inside + // in.ib's bounds. + + if this.interlace { + // TODO. + } 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:] + n = (dst.copy_from_slice(s:src) & 0xFFFFFFFF) as base.u32 + + new_ri = n ~sat+ this.uncompressed_ri + this.uncompressed_ri = new_ri.min(x:4096) + this.dst_x ~sat+= n + } + + if this.dst_x1 <= this.dst_x { + this.dst_x = this.dst_x0 + this.dst_y += 1 + 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 { + return error "internal error: inconsistent ri/wi" + } + + // 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) + + new_ri = n ~sat+ this.uncompressed_ri + this.uncompressed_ri = new_ri.min(x:4096) + this.dst_x ~sat+= n + + if this.dst_x1 <= this.dst_x { + this.dst_x = this.dst_x0 + this.dst_y += 1 + continue + } + + if this.uncompressed_ri != this.uncompressed_wi { + return error "internal error: inconsistent ri/wi" + } + break + } }