From c997066a2a373c90fcb5616adb86c3282207ad40 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 15 Jan 2022 12:38:21 +1100 Subject: [PATCH] Have std/tga decode color-mapped images Updates #67 --- release/c/wuffs-unsupported-snapshot.c | 144 +++++++++++++++++++++- std/tga/decode_tga.wuffs | 64 +++++++++- test/3pdata/nia-checksums-of-tgasuite.txt | 2 +- test/nia-checksums-of-data.txt | 2 +- 4 files changed, 200 insertions(+), 12 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index c39e0ac6..5f6ae9a2 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -9817,7 +9817,11 @@ struct wuffs_tga__decoder__struct { } private_impl; struct { + uint8_t f_dst_palette[1024]; + uint8_t f_src_palette[1024]; + struct { + uint32_t v_i; uint64_t scratch; } s_decode_image_config[1]; struct { @@ -42131,6 +42135,10 @@ wuffs_tga__decoder__decode_image_config( self->private_impl.active_coroutine = 0; wuffs_base__status status = wuffs_base__make_status(NULL); + uint32_t v_c = 0; + uint32_t v_c5 = 0; + uint32_t v_i = 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; @@ -42143,6 +42151,9 @@ wuffs_tga__decoder__decode_image_config( } uint32_t coro_susp_point = self->private_impl.p_decode_image_config[0]; + if (coro_susp_point) { + v_i = self->private_data.s_decode_image_config[0].v_i; + } switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; @@ -42412,7 +42423,9 @@ wuffs_tga__decoder__decode_image_config( goto exit; } if ((self->private_impl.f_header_image_type | 8) == 9) { - self->private_impl.f_src_pixfmt = 2164295816; + self->private_impl.f_src_bytes_per_pixel = 1; + self->private_impl.f_src_pixfmt = 2164523016; + self->private_impl.f_opaque = ((self->private_impl.f_header_color_map_entry_size != 15) || (self->private_impl.f_header_color_map_entry_size != 24)); } 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; @@ -42460,6 +42473,122 @@ wuffs_tga__decoder__decode_image_config( } iop_a_src += self->private_data.s_decode_image_config[0].scratch; if (self->private_impl.f_header_color_map_type != 0) { + while (v_i < ((uint32_t)(self->private_impl.f_header_color_map_length))) { + if (self->private_impl.f_header_color_map_entry_size == 24) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(20); + uint32_t t_12; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 3)) { + t_12 = ((uint32_t)(wuffs_base__peek_u24le__no_bounds_check(iop_a_src))); + iop_a_src += 3; + } else { + self->private_data.s_decode_image_config[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(21); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; + uint32_t num_bits_12 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_12; + if (num_bits_12 == 16) { + t_12 = ((uint32_t)(*scratch)); + break; + } + num_bits_12 += 8; + *scratch |= ((uint64_t)(num_bits_12)) << 56; + } + } + v_c = t_12; + } + self->private_data.f_src_palette[(((v_i & 255) * 4) + 0)] = ((uint8_t)(((v_c >> 0) & 255))); + self->private_data.f_src_palette[(((v_i & 255) * 4) + 1)] = ((uint8_t)(((v_c >> 8) & 255))); + self->private_data.f_src_palette[(((v_i & 255) * 4) + 2)] = ((uint8_t)(((v_c >> 16) & 255))); + self->private_data.f_src_palette[(((v_i & 255) * 4) + 3)] = 255; + } else if (self->private_impl.f_header_color_map_entry_size == 32) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(22); + uint32_t t_13; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_13 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_image_config[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(23); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; + uint32_t num_bits_13 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_13; + if (num_bits_13 == 24) { + t_13 = ((uint32_t)(*scratch)); + break; + } + num_bits_13 += 8; + *scratch |= ((uint64_t)(num_bits_13)) << 56; + } + } + v_c = t_13; + } + self->private_data.f_src_palette[(((v_i & 255) * 4) + 0)] = ((uint8_t)(((v_c >> 0) & 255))); + self->private_data.f_src_palette[(((v_i & 255) * 4) + 1)] = ((uint8_t)(((v_c >> 8) & 255))); + self->private_data.f_src_palette[(((v_i & 255) * 4) + 2)] = ((uint8_t)(((v_c >> 16) & 255))); + self->private_data.f_src_palette[(((v_i & 255) * 4) + 3)] = ((uint8_t)(((v_c >> 24) & 255))); + } else { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(24); + uint32_t t_14; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 2)) { + t_14 = ((uint32_t)(wuffs_base__peek_u16le__no_bounds_check(iop_a_src))); + iop_a_src += 2; + } else { + self->private_data.s_decode_image_config[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(25); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; + uint32_t num_bits_14 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_14; + if (num_bits_14 == 8) { + t_14 = ((uint32_t)(*scratch)); + break; + } + num_bits_14 += 8; + *scratch |= ((uint64_t)(num_bits_14)) << 56; + } + } + v_c = t_14; + } + v_c5 = (31 & (v_c >> 0)); + self->private_data.f_src_palette[(((v_i & 255) * 4) + 0)] = ((uint8_t)(((v_c5 << 3) | (v_c5 >> 2)))); + v_c5 = (31 & (v_c >> 5)); + self->private_data.f_src_palette[(((v_i & 255) * 4) + 1)] = ((uint8_t)(((v_c5 << 3) | (v_c5 >> 2)))); + v_c5 = (31 & (v_c >> 10)); + self->private_data.f_src_palette[(((v_i & 255) * 4) + 2)] = ((uint8_t)(((v_c5 << 3) | (v_c5 >> 2)))); + self->private_data.f_src_palette[(((v_i & 255) * 4) + 3)] = 255; + } + v_i += 1; + } + while (v_i < 256) { + self->private_data.f_src_palette[((v_i * 4) + 0)] = 0; + self->private_data.f_src_palette[((v_i * 4) + 1)] = 0; + self->private_data.f_src_palette[((v_i * 4) + 2)] = 0; + self->private_data.f_src_palette[((v_i * 4) + 3)] = 255; + v_i += 1; + } } self->private_impl.f_frame_config_io_position = wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src))); if (a_dst != NULL) { @@ -42484,6 +42613,7 @@ wuffs_tga__decoder__decode_image_config( suspend: self->private_impl.p_decode_image_config[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 1 : 0; + self->private_data.s_decode_image_config[0].v_i = v_i; goto exit; exit: @@ -42643,8 +42773,10 @@ wuffs_tga__decoder__decode_frame( uint32_t v_dst_x = 0; uint32_t v_dst_y = 0; wuffs_base__table_u8 v_tab = {0}; + wuffs_base__slice_u8 v_dst_palette = {0}; wuffs_base__slice_u8 v_dst = {0}; uint64_t v_dst_start = 0; + wuffs_base__slice_u8 v_src_palette = {0}; uint8_t v_src[4] = {0}; uint64_t v_mark = 0; uint64_t v_num_pixels64 = 0; @@ -42695,11 +42827,14 @@ wuffs_tga__decoder__decode_frame( status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; } + if (self->private_impl.f_header_color_map_type != 0) { + v_src_palette = wuffs_base__make_slice_u8(self->private_data.f_src_palette, 1024); + } 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__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024)), wuffs_base__utility__make_pixel_format(self->private_impl.f_src_pixfmt), - wuffs_base__utility__empty_slice_u8(), + v_src_palette, a_blend); if ( ! wuffs_base__status__is_ok(&v_status)) { status = v_status; @@ -42725,6 +42860,7 @@ wuffs_tga__decoder__decode_frame( label__resume__continue:; while (true) { v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0); + v_dst_palette = wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024)); 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); @@ -42750,7 +42886,7 @@ wuffs_tga__decoder__decode_frame( 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)); + wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, v_dst, v_dst_palette, 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 { diff --git a/std/tga/decode_tga.wuffs b/std/tga/decode_tga.wuffs index bb0039b7..e01702d6 100644 --- a/std/tga/decode_tga.wuffs +++ b/std/tga/decode_tga.wuffs @@ -68,12 +68,19 @@ pub struct decoder? implements base.image_decoder( swizzler : base.pixel_swizzler, util : base.utility, +)( + dst_palette : array[4 * 256] base.u8, + src_palette : array[4 * 256] base.u8, ) pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) { } pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) { + var c : base.u32 + var c5 : base.u32[..= 0x1F] + var i : base.u32 + if this.call_sequence <> 0 { return base."#bad call sequence" } @@ -143,8 +150,11 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_ } if (this.header_image_type | 8) == 0x09 { - // TODO: base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL - this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL + this.src_bytes_per_pixel = 1 + this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL + this.opaque = + (this.header_color_map_entry_size <> 0x0F) or + (this.header_color_map_entry_size <> 0x18) } else if (this.header_image_type | 8) == 0x0A { if this.header_pixel_depth == 0x10 { @@ -180,8 +190,43 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_ // Skip the Image ID. args.src.skip_u32?(n: this.header_id_length as base.u32) + // Read the color map. if this.header_color_map_type <> 0 { - // TODO: read the color map. + while i < (this.header_color_map_length as base.u32) { + assert i <= 0xFFFF via "a <= b: a <= c; c <= b"(c: this.header_color_map_length as base.u32) + if this.header_color_map_entry_size == 0x18 { + c = args.src.read_u24le_as_u32?() + this.src_palette[((i & 0xFF) * 4) + 0] = ((c >> 0x00) & 0xFF) as base.u8 + this.src_palette[((i & 0xFF) * 4) + 1] = ((c >> 0x08) & 0xFF) as base.u8 + this.src_palette[((i & 0xFF) * 4) + 2] = ((c >> 0x10) & 0xFF) as base.u8 + this.src_palette[((i & 0xFF) * 4) + 3] = 0xFF + } else if this.header_color_map_entry_size == 0x20 { + c = args.src.read_u32le?() + this.src_palette[((i & 0xFF) * 4) + 0] = ((c >> 0x00) & 0xFF) as base.u8 + this.src_palette[((i & 0xFF) * 4) + 1] = ((c >> 0x08) & 0xFF) as base.u8 + this.src_palette[((i & 0xFF) * 4) + 2] = ((c >> 0x10) & 0xFF) as base.u8 + this.src_palette[((i & 0xFF) * 4) + 3] = ((c >> 0x18) & 0xFF) as base.u8 + } else { + // Expand 15-bit or 16-bit color map entries. + c = args.src.read_u16le_as_u32?() + c5 = 0x1F & (c >> 0) + this.src_palette[((i & 0xFF) * 4) + 0] = ((c5 << 3) | (c5 >> 2)) as base.u8 + c5 = 0x1F & (c >> 5) + this.src_palette[((i & 0xFF) * 4) + 1] = ((c5 << 3) | (c5 >> 2)) as base.u8 + c5 = 0x1F & (c >> 10) + this.src_palette[((i & 0xFF) * 4) + 2] = ((c5 << 3) | (c5 >> 2)) as base.u8 + // TODO: can the alpha value be zero (BGRA5551)? + this.src_palette[((i & 0xFF) * 4) + 3] = 0xFF + } + i += 1 + } endwhile + while i < 0x100 { + this.src_palette[(i * 4) + 0] = 0x00 + this.src_palette[(i * 4) + 1] = 0x00 + this.src_palette[(i * 4) + 2] = 0x00 + this.src_palette[(i * 4) + 3] = 0xFF + i += 1 + } endwhile } this.frame_config_io_position = args.src.position() @@ -239,8 +284,10 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, var dst_x : base.u32 var dst_y : base.u32 var tab : table base.u8 + var dst_palette : slice base.u8 var dst : slice base.u8 var dst_start : base.u64 + var src_palette : slice base.u8 var src : array[4] base.u8 var mark : base.u64 var num_pixels64 : base.u64 @@ -257,11 +304,14 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, return base."@end of data" } + if this.header_color_map_type <> 0 { + src_palette = this.src_palette[..] + } status = this.swizzler.prepare!( dst_pixfmt: args.dst.pixel_format(), - dst_palette: args.dst.palette(), + dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]), src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt), - src_palette: this.util.empty_slice_u8(), + src_palette: src_palette, blend: args.blend) if not status.is_ok() { return status @@ -283,6 +333,8 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, while.resume true { tab = args.dst.plane(p: 0) + dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..]) + while dst_y < this.height { dst = tab.row_u32(y: dst_y) dst_start = (dst_x as base.u64) * dst_bytes_per_pixel @@ -305,7 +357,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, args.src.skip_u32?(n: num_src_bytes) this.swizzler.swizzle_interleaved_from_slice!( dst: dst, - dst_palette: args.dst.palette(), + dst_palette: dst_palette, src: args.src.since(mark: mark)) if num_dst_bytes <= dst.length() { dst = dst[num_dst_bytes ..] diff --git a/test/3pdata/nia-checksums-of-tgasuite.txt b/test/3pdata/nia-checksums-of-tgasuite.txt index 3897f2fe..e60c6c1e 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 9fbe866a test/3pdata/tgasuite/ubw8.tga -3da6493d test/3pdata/tgasuite/ucm8.tga +5d75ef41 test/3pdata/tgasuite/ucm8.tga 3da6493d test/3pdata/tgasuite/utc16.tga 5d75ef41 test/3pdata/tgasuite/utc24.tga 79781cfb test/3pdata/tgasuite/utc32.tga diff --git a/test/nia-checksums-of-data.txt b/test/nia-checksums-of-data.txt index 6dddb445..555bca46 100644 --- a/test/nia-checksums-of-data.txt +++ b/test/nia-checksums-of-data.txt @@ -39,7 +39,7 @@ c2bce675 test/data/bricks-gray.png 7aebfe6c test/data/bricks-nodither.bmp 7aebfe6c test/data/bricks-nodither.gif 7aebfe6c test/data/bricks-nodither.png -0a9502ff test/data/bricks-nodither.tga +7aebfe6c test/data/bricks-nodither.tga ae2e547d test/data/bricks-nodither.wbmp b47730a2 test/data/checkerboard.gamma1dot0.png b47730a2 test/data/checkerboard.gamma2dot2.png