diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 70224373..c39e0ac6 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -9805,6 +9805,9 @@ struct wuffs_tga__decoder__struct { uint16_t f_header_y_origin; uint8_t f_header_pixel_depth; uint8_t f_header_image_descriptor; + bool f_opaque; + uint32_t f_src_bytes_per_pixel; + uint32_t f_src_pixfmt; uint64_t f_frame_config_io_position; wuffs_base__pixel_swizzler f_swizzler; @@ -9818,9 +9821,15 @@ struct wuffs_tga__decoder__struct { uint64_t scratch; } s_decode_image_config[1]; struct { + uint64_t v_dst_bytes_per_pixel; uint32_t v_dst_x; uint32_t v_dst_y; uint8_t v_src[4]; + uint64_t v_mark; + uint32_t v_num_pixels32; + uint32_t v_lit_length; + uint64_t v_num_dst_bytes; + uint64_t scratch; } s_decode_frame[1]; } private_data; @@ -42402,6 +42411,32 @@ wuffs_tga__decoder__decode_image_config( status = wuffs_base__make_status(wuffs_tga__error__bad_header); goto exit; } + if ((self->private_impl.f_header_image_type | 8) == 9) { + self->private_impl.f_src_pixfmt = 2164295816; + } else if ((self->private_impl.f_header_image_type | 8) == 10) { + if (self->private_impl.f_header_pixel_depth == 16) { + self->private_impl.f_src_pixfmt = 2164295816; + } else if (self->private_impl.f_header_pixel_depth == 24) { + self->private_impl.f_src_bytes_per_pixel = 3; + self->private_impl.f_src_pixfmt = 2147485832; + self->private_impl.f_opaque = true; + } else if (self->private_impl.f_header_pixel_depth == 32) { + self->private_impl.f_src_bytes_per_pixel = 4; + self->private_impl.f_src_pixfmt = 2164295816; + } else { + status = wuffs_base__make_status(wuffs_tga__error__unsupported_tga_file); + goto exit; + } + } else { + if (self->private_impl.f_header_pixel_depth == 8) { + self->private_impl.f_src_bytes_per_pixel = 1; + self->private_impl.f_src_pixfmt = 536870920; + self->private_impl.f_opaque = true; + } else { + status = wuffs_base__make_status(wuffs_tga__error__unsupported_tga_file); + goto exit; + } + } { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(18); if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { @@ -42430,7 +42465,7 @@ wuffs_tga__decoder__decode_image_config( if (a_dst != NULL) { wuffs_base__image_config__set( a_dst, - 2164295816, + self->private_impl.f_src_pixfmt, 0, self->private_impl.f_width, self->private_impl.f_height, @@ -42542,9 +42577,9 @@ wuffs_tga__decoder__decode_frame_config( 0, self->private_impl.f_frame_config_io_position, 0, - true, + self->private_impl.f_opaque, false, - 0); + 4278190080); } self->private_impl.f_call_sequence = 4; @@ -42609,20 +42644,49 @@ wuffs_tga__decoder__decode_frame( uint32_t v_dst_y = 0; wuffs_base__table_u8 v_tab = {0}; wuffs_base__slice_u8 v_dst = {0}; + uint64_t v_dst_start = 0; uint8_t v_src[4] = {0}; + uint64_t v_mark = 0; + uint64_t v_num_pixels64 = 0; + uint32_t v_num_pixels32 = 0; + uint32_t v_lit_length = 0; + uint64_t v_num_dst_bytes = 0; + uint32_t v_num_src_bytes = 0; + + const uint8_t* iop_a_src = NULL; + const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + if (a_src) { + io0_a_src = a_src->data.ptr; + io1_a_src = io0_a_src + a_src->meta.ri; + iop_a_src = io1_a_src; + io2_a_src = io0_a_src + a_src->meta.wi; + } uint32_t coro_susp_point = self->private_impl.p_decode_frame[0]; if (coro_susp_point) { + v_dst_bytes_per_pixel = self->private_data.s_decode_frame[0].v_dst_bytes_per_pixel; v_dst_x = self->private_data.s_decode_frame[0].v_dst_x; v_dst_y = self->private_data.s_decode_frame[0].v_dst_y; memcpy(v_src, self->private_data.s_decode_frame[0].v_src, sizeof(v_src)); + v_mark = self->private_data.s_decode_frame[0].v_mark; + v_num_pixels32 = self->private_data.s_decode_frame[0].v_num_pixels32; + v_lit_length = self->private_data.s_decode_frame[0].v_lit_length; + v_num_dst_bytes = self->private_data.s_decode_frame[0].v_num_dst_bytes; } switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; if (self->private_impl.f_call_sequence < 4) { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); status = wuffs_tga__decoder__decode_frame_config(self, NULL, a_src); + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } if (status.repr) { goto suspend; } @@ -42634,7 +42698,7 @@ wuffs_tga__decoder__decode_frame( v_status = wuffs_base__pixel_swizzler__prepare(&self->private_impl.f_swizzler, wuffs_base__pixel_buffer__pixel_format(a_dst), wuffs_base__pixel_buffer__palette(a_dst), - wuffs_base__utility__make_pixel_format(2164295816), + wuffs_base__utility__make_pixel_format(self->private_impl.f_src_pixfmt), wuffs_base__utility__empty_slice_u8(), a_blend); if ( ! wuffs_base__status__is_ok(&v_status)) { @@ -42654,23 +42718,66 @@ wuffs_tga__decoder__decode_frame( goto exit; } v_dst_bytes_per_pixel = ((uint64_t)((v_dst_bits_per_pixel / 8))); - v_src[0] = 127; - v_src[1] = 0; - v_src[2] = 255; - v_src[3] = 255; if ((self->private_impl.f_header_image_descriptor & 32) == 0) { v_dst_y = ((uint32_t)(self->private_impl.f_height - 1)); } + v_lit_length = self->private_impl.f_width; + label__resume__continue:; while (true) { v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0); while (v_dst_y < self->private_impl.f_height) { v_dst = wuffs_base__table_u8__row_u32(v_tab, v_dst_y); + v_dst_start = (((uint64_t)(v_dst_x)) * v_dst_bytes_per_pixel); + if (v_dst_start <= ((uint64_t)(v_dst.len))) { + v_dst = wuffs_base__slice_u8__subslice_i(v_dst, v_dst_start); + } else { + v_dst = wuffs_base__utility__empty_slice_u8(); + } while (v_dst_x < self->private_impl.f_width) { - wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, v_dst, wuffs_base__pixel_buffer__palette(a_dst), wuffs_base__make_slice_u8(v_src, 4)); - if (v_dst_bytes_per_pixel <= ((uint64_t)(v_dst.len))) { - v_dst = wuffs_base__slice_u8__subslice_i(v_dst, v_dst_bytes_per_pixel); + if (self->private_impl.f_src_bytes_per_pixel > 0) { + if (v_lit_length > 0) { + v_mark = ((uint64_t)(iop_a_src - io0_a_src)); + v_num_pixels64 = (((uint64_t)(io2_a_src - iop_a_src)) / ((uint64_t)(self->private_impl.f_src_bytes_per_pixel))); + v_num_pixels32 = ((uint32_t)(wuffs_base__u64__min(v_num_pixels64, ((uint64_t)(v_lit_length))))); + v_num_dst_bytes = (((uint64_t)(v_num_pixels32)) * v_dst_bytes_per_pixel); + v_num_src_bytes = (v_num_pixels32 * self->private_impl.f_src_bytes_per_pixel); + self->private_data.s_decode_frame[0].scratch = v_num_src_bytes; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + if (self->private_data.s_decode_frame[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) { + self->private_data.s_decode_frame[0].scratch -= ((uint64_t)(io2_a_src - iop_a_src)); + iop_a_src = io2_a_src; + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + iop_a_src += self->private_data.s_decode_frame[0].scratch; + wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, v_dst, wuffs_base__pixel_buffer__palette(a_dst), wuffs_base__io__since(v_mark, ((uint64_t)(iop_a_src - io0_a_src)), io0_a_src)); + if (v_num_dst_bytes <= ((uint64_t)(v_dst.len))) { + v_dst = wuffs_base__slice_u8__subslice_i(v_dst, v_num_dst_bytes); + } else { + v_dst = wuffs_base__utility__empty_slice_u8(); + } + v_dst_x += v_num_pixels32; + v_lit_length = (((uint32_t)(v_lit_length - v_num_pixels32)) & 65535); + if (v_lit_length > 0) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3); + goto label__resume__continue; + } + } + } else { + if (v_lit_length > 0) { + v_src[0] = 127; + v_src[1] = 0; + v_src[2] = 255; + v_src[3] = 255; + wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, v_dst, wuffs_base__pixel_buffer__palette(a_dst), wuffs_base__make_slice_u8(v_src, 4)); + if (v_dst_bytes_per_pixel <= ((uint64_t)(v_dst.len))) { + v_dst = wuffs_base__slice_u8__subslice_i(v_dst, v_dst_bytes_per_pixel); + } + v_dst_x += 1; + v_lit_length -= 1; + } } - v_dst_x += 1; } v_dst_x = 0; if ((self->private_impl.f_header_image_descriptor & 32) == 0) { @@ -42678,6 +42785,7 @@ wuffs_tga__decoder__decode_frame( } else { v_dst_y += 1; } + v_lit_length = self->private_impl.f_width; } goto label__resume__break; } @@ -42693,12 +42801,21 @@ wuffs_tga__decoder__decode_frame( suspend: self->private_impl.p_decode_frame[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 3 : 0; + self->private_data.s_decode_frame[0].v_dst_bytes_per_pixel = v_dst_bytes_per_pixel; self->private_data.s_decode_frame[0].v_dst_x = v_dst_x; self->private_data.s_decode_frame[0].v_dst_y = v_dst_y; memcpy(self->private_data.s_decode_frame[0].v_src, v_src, sizeof(v_src)); + self->private_data.s_decode_frame[0].v_mark = v_mark; + self->private_data.s_decode_frame[0].v_num_pixels32 = v_num_pixels32; + self->private_data.s_decode_frame[0].v_lit_length = v_lit_length; + self->private_data.s_decode_frame[0].v_num_dst_bytes = v_num_dst_bytes; goto exit; exit: + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + if (wuffs_base__status__is_error(&status)) { self->private_impl.magic = WUFFS_BASE__DISABLED; } diff --git a/std/tga/decode_tga.wuffs b/std/tga/decode_tga.wuffs index b17d4678..bb0039b7 100644 --- a/std/tga/decode_tga.wuffs +++ b/std/tga/decode_tga.wuffs @@ -18,8 +18,8 @@ pub status "#unsupported TGA file" pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0 pub struct decoder? implements base.image_decoder( - width : base.u32, - height : base.u32, + width : base.u32[..= 0xFFFF], + height : base.u32[..= 0xFFFF], // Call sequence states: // - 0x00: initial state. @@ -59,6 +59,11 @@ pub struct decoder? implements base.image_decoder( header_pixel_depth : base.u8, header_image_descriptor : base.u8, + opaque : base.bool, + + src_bytes_per_pixel : base.u32[..= 4], + src_pixfmt : base.u32, + frame_config_io_position : base.u64, swizzler : base.pixel_swizzler, @@ -137,6 +142,35 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_ return "#bad header" } + if (this.header_image_type | 8) == 0x09 { + // TODO: base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL + this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL + + } else if (this.header_image_type | 8) == 0x0A { + if this.header_pixel_depth == 0x10 { + // TODO: implement. + this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL + } else if this.header_pixel_depth == 0x18 { + this.src_bytes_per_pixel = 3 + this.src_pixfmt = base.PIXEL_FORMAT__BGR + this.opaque = true + } else if this.header_pixel_depth == 0x20 { + this.src_bytes_per_pixel = 4 + this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL + } else { + return "#unsupported TGA file" + } + + } else { + if this.header_pixel_depth == 0x08 { + this.src_bytes_per_pixel = 1 + this.src_pixfmt = base.PIXEL_FORMAT__Y + this.opaque = true + } else { + return "#unsupported TGA file" + } + } + this.header_image_descriptor = args.src.read_u8?() if (this.header_image_descriptor & 0x10) <> 0 { // We don't support right-to-left, only left-to-right pixel order. @@ -154,7 +188,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_ if args.dst <> nullptr { args.dst.set!( - pixfmt: base.PIXEL_FORMAT__BGRA_NONPREMUL, + pixfmt: this.src_pixfmt, pixsub: 0, width: this.width, height: this.height, @@ -189,9 +223,9 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_ index: 0, io_position: this.frame_config_io_position, disposal: 0, - opaque_within_bounds: true, + opaque_within_bounds: this.opaque, overwrite_instead_of_blend: false, - background_color: 0x0000_0000) + background_color: 0xFF00_0000) } this.call_sequence = 4 @@ -206,7 +240,14 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, var dst_y : base.u32 var tab : table base.u8 var dst : slice base.u8 + var dst_start : base.u64 var src : array[4] base.u8 + var mark : base.u64 + var num_pixels64 : base.u64 + var num_pixels32 : base.u32[..= 0xFFFF] + var lit_length : base.u32[..= 0xFFFF] + var num_dst_bytes : base.u64[..= 0x1F_FFE0] + var num_src_bytes : base.u32[..= 0x3_FFFC] if this.call_sequence < 4 { this.decode_frame_config?(dst: nullptr, src: args.src) @@ -219,7 +260,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, status = this.swizzler.prepare!( dst_pixfmt: args.dst.pixel_format(), dst_palette: args.dst.palette(), - src_pixfmt: this.util.make_pixel_format(repr: base.PIXEL_FORMAT__BGRA_NONPREMUL), + src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt), src_palette: this.util.empty_slice_u8(), blend: args.blend) if not status.is_ok() { @@ -235,34 +276,67 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, } dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64 - // TODO: don't hard-code reddish purple. - src[0] = 0x7F - src[1] = 0x00 - src[2] = 0xFF - src[3] = 0xFF - if (this.header_image_descriptor & 0x20) == 0 { // Bottom-to-top. dst_y = this.height ~mod- 1 } + lit_length = this.width - // TODO: add "continue.resume" lines after short reads. while.resume true { tab = args.dst.plane(p: 0) while dst_y < this.height { dst = tab.row_u32(y: dst_y) + dst_start = (dst_x as base.u64) * dst_bytes_per_pixel + if dst_start <= dst.length() { + dst = dst[dst_start ..] + } else { + dst = this.util.empty_slice_u8() + } while dst_x < this.width { assert dst_x < 0xFFFF_FFFF via "a < b: a < c; c <= b"(c: this.width) - // TODO: be more efficient than swizzling one pixel at a time. - this.swizzler.swizzle_interleaved_from_slice!( - dst: dst, dst_palette: args.dst.palette(), src: src[..]) + if this.src_bytes_per_pixel > 0 { + if lit_length > 0 { + mark = args.src.mark() + num_pixels64 = args.src.length() / (this.src_bytes_per_pixel as base.u64) + num_pixels32 = num_pixels64.min(a: lit_length as base.u64) as base.u32 + num_dst_bytes = (num_pixels32 as base.u64) * dst_bytes_per_pixel + num_src_bytes = num_pixels32 * this.src_bytes_per_pixel + args.src.skip_u32?(n: num_src_bytes) + this.swizzler.swizzle_interleaved_from_slice!( + dst: dst, + dst_palette: args.dst.palette(), + src: args.src.since(mark: mark)) + if num_dst_bytes <= dst.length() { + dst = dst[num_dst_bytes ..] + } else { + dst = this.util.empty_slice_u8() + } + dst_x ~mod+= num_pixels32 + lit_length = (lit_length ~mod- num_pixels32) & 0xFFFF + if lit_length > 0 { + yield? base."$short read" + continue.resume + } + } - if dst_bytes_per_pixel <= dst.length() { - dst = dst[dst_bytes_per_pixel ..] + } else { + if lit_length > 0 { + // TODO: don't hard-code reddish purple. + src[0] = 0x7F + src[1] = 0x00 + src[2] = 0xFF + src[3] = 0xFF + // TODO: be more efficient than swizzling one pixel at a time. + this.swizzler.swizzle_interleaved_from_slice!( + dst: dst, dst_palette: args.dst.palette(), src: src[..]) + if dst_bytes_per_pixel <= dst.length() { + dst = dst[dst_bytes_per_pixel ..] + } + dst_x += 1 + lit_length -= 1 + } } - - dst_x += 1 } endwhile dst_x = 0 @@ -271,6 +345,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, } else { // Top-to-bottom. dst_y ~mod+= 1 } + lit_length = this.width } endwhile break.resume } endwhile.resume diff --git a/test/3pdata/nia-checksums-of-tgasuite.txt b/test/3pdata/nia-checksums-of-tgasuite.txt index 1f08ed4b..3897f2fe 100644 --- a/test/3pdata/nia-checksums-of-tgasuite.txt +++ b/test/3pdata/nia-checksums-of-tgasuite.txt @@ -1,6 +1,6 @@ # Generated by script/print-nia-checksums.sh -3da6493d test/3pdata/tgasuite/ubw8.tga +9fbe866a test/3pdata/tgasuite/ubw8.tga 3da6493d test/3pdata/tgasuite/ucm8.tga 3da6493d test/3pdata/tgasuite/utc16.tga -3da6493d test/3pdata/tgasuite/utc24.tga -3da6493d test/3pdata/tgasuite/utc32.tga +5d75ef41 test/3pdata/tgasuite/utc24.tga +79781cfb test/3pdata/tgasuite/utc32.tga diff --git a/test/c/std/tga.c b/test/c/std/tga.c index 26faaf30..0e0f6fbd 100644 --- a/test/c/std/tga.c +++ b/test/c/std/tga.c @@ -79,7 +79,7 @@ test_wuffs_tga_decode_interface() { WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED)); return do_test__wuffs_base__image_decoder( wuffs_tga__decoder__upcast_as__wuffs_base__image_decoder(&dec), - "test/data/bricks-color.tga", 0, SIZE_MAX, 160, 120, 0xFFFF007F); + "test/data/bricks-color.tga", 0, SIZE_MAX, 160, 120, 0xFF022460); } // ---------------- Mimic Tests diff --git a/test/nia-checksums-of-data.txt b/test/nia-checksums-of-data.txt index 07141704..6dddb445 100644 --- a/test/nia-checksums-of-data.txt +++ b/test/nia-checksums-of-data.txt @@ -26,7 +26,7 @@ e08a7cc8 test/data/artificial-png/exif.png e08a7cc8 test/data/artificial-png/key-value-pairs.png 076cb375 test/data/bricks-color.bmp 076cb375 test/data/bricks-color.png -0a9502ff test/data/bricks-color.tga +076cb375 test/data/bricks-color.tga f36c2e80 test/data/bricks-dither.bmp f36c2e80 test/data/bricks-dither.gif f36c2e80 test/data/bricks-dither.no-ancillary.png @@ -35,7 +35,7 @@ c2bce675 test/data/bricks-gray.bmp c2bce675 test/data/bricks-gray.gif c2bce675 test/data/bricks-gray.no-ancillary.png c2bce675 test/data/bricks-gray.png -0a9502ff test/data/bricks-gray.tga +0319cb08 test/data/bricks-gray.tga 7aebfe6c test/data/bricks-nodither.bmp 7aebfe6c test/data/bricks-nodither.gif 7aebfe6c test/data/bricks-nodither.png