From 41645833232bc39b90f5319e7e3531ec2917d987 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 16 Dec 2018 08:52:32 +1100 Subject: [PATCH] Add a src arg to std/gif's copy_to_image_buffer --- release/c/wuffs-unsupported-snapshot.h | 58 ++++++++++++-------------- std/gif/decode_gif.wuffs | 35 +++++++--------- 2 files changed, 42 insertions(+), 51 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index c7f885ba..61ec39df 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -2759,7 +2759,6 @@ struct wuffs_gif__decoder__struct { wuffs_base__range_ie_u32 f_dirty_y; uint64_t f_compressed_ri; uint64_t f_compressed_wi; - uint64_t f_uncompressed_ri; uint64_t f_uncompressed_wi; uint8_t f_compressed[4096]; uint8_t f_uncompressed[4096]; @@ -7536,7 +7535,8 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, static wuffs_base__status // wuffs_gif__decoder__copy_to_image_buffer(wuffs_gif__decoder* self, - wuffs_base__pixel_buffer* a_pb); + wuffs_base__pixel_buffer* a_pb, + wuffs_base__slice_u8 a_src); // ---------------- Initializer Implementations @@ -9508,10 +9508,22 @@ wuffs_gif__decoder__decode_id_part1(wuffs_gif__decoder* self, iop_v_w = o_0_iop_v_w; io1_v_w = o_0_io1_v_w; } - status = wuffs_gif__decoder__copy_to_image_buffer(self, a_dst); + if (self->private_impl.f_uncompressed_wi > 4096) { + status = wuffs_gif__error__internal_error_inconsistent_ri_wi; + goto exit; + } + status = wuffs_gif__decoder__copy_to_image_buffer( + self, a_dst, + wuffs_base__slice_u8__subslice_j( + ((wuffs_base__slice_u8){ + .ptr = self->private_impl.f_uncompressed, + .len = 4096, + }), + self->private_impl.f_uncompressed_wi)); if (status) { goto exit; } + self->private_impl.f_uncompressed_wi = 0; if (wuffs_base__status__is_ok(v_z)) { self->private_impl.f_previous_lzw_decode_ended_abruptly = false; if (v_block_size > 0) { @@ -9589,12 +9601,14 @@ exit: static wuffs_base__status // wuffs_gif__decoder__copy_to_image_buffer(wuffs_gif__decoder* self, - wuffs_base__pixel_buffer* a_pb) { + wuffs_base__pixel_buffer* a_pb, + wuffs_base__slice_u8 a_src) { wuffs_base__status status = NULL; wuffs_base__slice_u8 v_dst; wuffs_base__slice_u8 v_src; uint64_t v_n; + uint64_t v_src_ri; uint32_t v_bytes_per_pixel; uint32_t v_pixfmt; wuffs_base__table_u8 v_tab; @@ -9604,26 +9618,16 @@ wuffs_gif__decoder__copy_to_image_buffer(wuffs_gif__decoder* self, v_dst = ((wuffs_base__slice_u8){}); v_src = ((wuffs_base__slice_u8){}); v_n = 0; + v_src_ri = 0; v_bytes_per_pixel = 1; v_pixfmt = wuffs_base__pixel_buffer__pixel_format(a_pb); if ((v_pixfmt == 1107331208) || (v_pixfmt == 1375766664)) { v_bytes_per_pixel = 4; } v_tab = wuffs_base__pixel_buffer__plane(a_pb, 0); - if (self->private_impl.f_uncompressed_wi > 4096) { - status = wuffs_gif__error__internal_error_inconsistent_ri_wi; - goto exit; - } 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); + while (v_src_ri < ((uint64_t)(a_src.len))) { + v_src = wuffs_base__slice_u8__subslice_i(a_src, v_src_ri); if (self->private_impl.f_dst_y >= self->private_impl.f_frame_rect_y1) { status = wuffs_gif__error__too_much_pixel_data; goto exit; @@ -9646,8 +9650,7 @@ label_0_continue:; .len = 1024, }), v_src); - wuffs_base__u64__sat_add_indirect(&self->private_impl.f_uncompressed_ri, - v_n); + wuffs_base__u64__sat_add_indirect(&v_src_ri, v_n); wuffs_base__u32__sat_add_indirect(&self->private_impl.f_dst_x, ((uint32_t)((v_n & 4294967295)))); self->private_impl.f_dirty_y = wuffs_base__range_ie_u32__unite( @@ -9672,20 +9675,16 @@ label_0_continue:; } goto label_0_continue; } - if (self->private_impl.f_uncompressed_wi == - self->private_impl.f_uncompressed_ri) { + if (((uint64_t)(a_src.len)) == v_src_ri) { goto label_0_break; - } else if (self->private_impl.f_uncompressed_wi < - self->private_impl.f_uncompressed_ri) { + } else if (((uint64_t)(a_src.len)) < v_src_ri) { status = wuffs_gif__error__internal_error_inconsistent_ri_wi; goto exit; } v_n = ((uint64_t)( (self->private_impl.f_frame_rect_x1 - self->private_impl.f_dst_x))); - v_n = wuffs_base__u64__min(v_n, (self->private_impl.f_uncompressed_wi - - self->private_impl.f_uncompressed_ri)); - wuffs_base__u64__sat_add_indirect(&self->private_impl.f_uncompressed_ri, - v_n); + v_n = wuffs_base__u64__min(v_n, (((uint64_t)(a_src.len)) - v_src_ri)); + wuffs_base__u64__sat_add_indirect(&v_src_ri, v_n); wuffs_base__u32__sat_add_indirect(&self->private_impl.f_dst_x, ((uint32_t)((v_n & 4294967295)))); if (self->private_impl.f_frame_rect_x1 <= self->private_impl.f_dst_x) { @@ -9704,16 +9703,13 @@ label_0_continue:; } goto label_0_continue; } - if (self->private_impl.f_uncompressed_ri != - self->private_impl.f_uncompressed_wi) { + if (v_src_ri != ((uint64_t)(a_src.len))) { status = wuffs_gif__error__internal_error_inconsistent_ri_wi; goto exit; } goto label_0_break; } label_0_break:; - self->private_impl.f_uncompressed_ri = 0; - self->private_impl.f_uncompressed_wi = 0; goto exit; exit: return status; diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 949bbee7..351d26a8 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -109,7 +109,6 @@ pub struct decoder?( compressed_ri base.u64, compressed_wi base.u64, - uncompressed_ri base.u64, uncompressed_wi base.u64, compressed array[4096] base.u8, @@ -670,7 +669,11 @@ pri func decoder.decode_id_part1!??(dst ptr base.pixel_buffer, src base.io_reade } } - this.copy_to_image_buffer!?(pb:args.dst) + if this.uncompressed_wi > 4096 { + return "?internal error: inconsistent ri/wi" + } + this.copy_to_image_buffer!?(pb:args.dst, src:this.uncompressed[:this.uncompressed_wi]) + this.uncompressed_wi = 0 if z.is_ok() { this.previous_lzw_decode_ended_abruptly = false @@ -694,11 +697,12 @@ pri func decoder.decode_id_part1!??(dst ptr base.pixel_buffer, src base.io_reade // TODO: check for "not enough pixel data". } -pri func decoder.copy_to_image_buffer!?(pb ptr base.pixel_buffer) { +pri func decoder.copy_to_image_buffer!?(pb ptr base.pixel_buffer, src slice base.u8) { // TODO: don't assume a packed pixel format. var dst slice base.u8 var src slice base.u8 var n base.u64 + var src_ri base.u64 // TODO: a Wuffs (not just C) name for the WUFFS_BASE__PIXEL_FORMAT__ETC // magic pixfmt constants. Also, support more formats. @@ -709,15 +713,8 @@ pri func decoder.copy_to_image_buffer!?(pb ptr base.pixel_buffer) { } var tab table base.u8 = args.pb.plane(p:0) - - if this.uncompressed_wi > 4096 { - return "?internal error: inconsistent ri/wi" - } - while this.uncompressed_wi > this.uncompressed_ri, - inv this.uncompressed_wi <= 4096, - { - assert this.uncompressed_ri < this.uncompressed_wi via "a < b: b > a"() - src = this.uncompressed[this.uncompressed_ri:this.uncompressed_wi] + while src_ri < args.src.length() { + src = args.src[src_ri:] if this.dst_y >= this.frame_rect_y1 { return "?too much pixel data" @@ -737,7 +734,7 @@ pri func decoder.copy_to_image_buffer!?(pb ptr base.pixel_buffer) { } n = this.swizzler.swizzle_packed!(dst:dst, dst_palette:this.dst_palette[:], src:src) - this.uncompressed_ri ~sat+= n + src_ri ~sat+= n this.dst_x ~sat+= (n & 0xFFFFFFFF) as base.u32 this.dirty_y = this.dirty_y.unite(r:this.util.make_range_ie_u32( @@ -755,9 +752,9 @@ pri func decoder.copy_to_image_buffer!?(pb ptr base.pixel_buffer) { continue } - if this.uncompressed_wi == this.uncompressed_ri { + if args.src.length() == src_ri { break - } else if this.uncompressed_wi < this.uncompressed_ri { + } else if args.src.length() < src_ri { return "?internal error: inconsistent ri/wi" } @@ -766,9 +763,9 @@ pri func decoder.copy_to_image_buffer!?(pb ptr base.pixel_buffer) { // Set n to the number of pixels (i.e. the number of bytes) to skip. n = (this.frame_rect_x1 - this.dst_x) as base.u64 - n = n.min(x:this.uncompressed_wi - this.uncompressed_ri) + n = n.min(x:args.src.length() - src_ri) - this.uncompressed_ri ~sat+= n + src_ri ~sat+= n this.dst_x ~sat+= (n & 0xFFFFFFFF) as base.u32 if this.frame_rect_x1 <= this.dst_x { @@ -781,11 +778,9 @@ pri func decoder.copy_to_image_buffer!?(pb ptr base.pixel_buffer) { continue } - if this.uncompressed_ri != this.uncompressed_wi { + if src_ri != args.src.length() { return "?internal error: inconsistent ri/wi" } break } - this.uncompressed_ri = 0 - this.uncompressed_wi = 0 }