diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 7a3d475a..ea7a5143 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -9312,6 +9312,7 @@ struct wuffs_png__decoder__struct { bool f_seen_srgb; bool f_seen_trns; bool f_zlib_is_dirty; + uint64_t f_remap_transparency; uint32_t f_dst_pixfmt; uint32_t f_src_pixfmt; uint32_t f_chunk_type; @@ -9383,8 +9384,9 @@ struct wuffs_png__decoder__struct { uint64_t scratch; } s_decode_plte[1]; struct { - uint32_t v_num_entries; uint32_t v_i; + uint32_t v_n; + uint64_t scratch; } s_decode_trns[1]; struct { uint32_t v_checksum_have; @@ -37579,7 +37581,7 @@ wuffs_png__decoder__decode_ihdr( self->private_impl.f_filter_distance = 0; wuffs_png__decoder__assign_filter_distance(self); if (self->private_impl.f_filter_distance == 0) { - status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); + status = wuffs_base__make_status(wuffs_png__error__bad_header); goto exit; } self->private_impl.f_overall_workbuf_length = (((uint64_t)(self->private_impl.f_height)) * (1 + wuffs_png__decoder__calculate_bytes_per_row(self, self->private_impl.f_width))); @@ -37610,7 +37612,9 @@ static wuffs_base__empty_struct wuffs_png__decoder__assign_filter_distance( wuffs_png__decoder* self) { if (self->private_impl.f_depth < 8) { - if (self->private_impl.f_color_type == 0) { + if ((self->private_impl.f_depth != 1) && (self->private_impl.f_depth != 2) && (self->private_impl.f_depth != 4)) { + return wuffs_base__make_empty_struct(); + } else if (self->private_impl.f_color_type == 0) { self->private_impl.f_dst_pixfmt = 536870920; self->private_impl.f_src_pixfmt = 536870920; } else if (self->private_impl.f_color_type == 3) { @@ -38539,8 +38543,9 @@ wuffs_png__decoder__decode_trns( wuffs_base__io_buffer* a_src) { wuffs_base__status status = wuffs_base__make_status(NULL); - uint32_t v_num_entries = 0; uint32_t v_i = 0; + uint32_t v_n = 0; + uint64_t v_u = 0; const uint8_t* iop_a_src = NULL; const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; @@ -38555,42 +38560,143 @@ wuffs_png__decoder__decode_trns( uint32_t coro_susp_point = self->private_impl.p_decode_trns[0]; if (coro_susp_point) { - v_num_entries = self->private_data.s_decode_trns[0].v_num_entries; v_i = self->private_data.s_decode_trns[0].v_i; + v_n = self->private_data.s_decode_trns[0].v_n; } switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_chunk_length > 256) { - status = wuffs_base__make_status(wuffs_png__error__bad_chunk); - goto exit; - } - v_num_entries = ((uint32_t)(self->private_impl.f_chunk_length)); - while (v_i < v_num_entries) { - { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); - if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { - status = wuffs_base__make_status(wuffs_base__suspension__short_read); - goto suspend; - } - uint8_t t_0 = *iop_a_src++; - self->private_data.f_src_palette[((4 * v_i) + 3)] = t_0; - } - v_i += 1; - } - if (self->private_impl.f_color_type == 3) { - self->private_impl.f_dst_pixfmt = 2164523016; - self->private_impl.f_src_pixfmt = 2164523016; - } else { + if (self->private_impl.f_color_type == 0) { self->private_impl.choosy_filter_and_swizzle = ( &wuffs_png__decoder__filter_and_swizzle_tricky); - if (self->private_impl.f_color_type == 0) { - self->private_impl.f_dst_pixfmt = 2164295816; - self->private_impl.f_src_pixfmt = 2164308923; - } else if (self->private_impl.f_color_type == 2) { + if (self->private_impl.f_depth <= 8) { self->private_impl.f_dst_pixfmt = 2164295816; + self->private_impl.f_src_pixfmt = 2164295816; + } else { + self->private_impl.f_dst_pixfmt = 2164308923; self->private_impl.f_src_pixfmt = 2164308923; } + if (self->private_impl.f_chunk_length != 2) { + status = wuffs_base__make_status(wuffs_png__error__bad_chunk); + goto exit; + } + self->private_impl.f_chunk_length = 0; + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + uint64_t t_0; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 2)) { + t_0 = ((uint64_t)(wuffs_base__peek_u16be__no_bounds_check(iop_a_src))); + iop_a_src += 2; + } else { + self->private_data.s_decode_trns[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + 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_trns[0].scratch; + uint32_t num_bits_0 = ((uint32_t)(*scratch & 0xFF)); + *scratch >>= 8; + *scratch <<= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_0); + if (num_bits_0 == 8) { + t_0 = ((uint64_t)(*scratch >> 48)); + break; + } + num_bits_0 += 8; + *scratch |= ((uint64_t)(num_bits_0)); + } + } + v_u = t_0; + } + if (self->private_impl.f_depth <= 1) { + self->private_impl.f_remap_transparency = (((v_u & 1) * 16777215) | 4278190080); + } else if (self->private_impl.f_depth <= 2) { + self->private_impl.f_remap_transparency = (((v_u & 3) * 5592405) | 4278190080); + } else if (self->private_impl.f_depth <= 4) { + self->private_impl.f_remap_transparency = (((v_u & 15) * 1118481) | 4278190080); + } else if (self->private_impl.f_depth <= 8) { + self->private_impl.f_remap_transparency = (((v_u & 255) * 65793) | 4278190080); + } else { + self->private_impl.f_remap_transparency = ((v_u * 4295032833) | 18446462598732840960u); + } + } else if (self->private_impl.f_color_type == 2) { + self->private_impl.choosy_filter_and_swizzle = ( + &wuffs_png__decoder__filter_and_swizzle_tricky); + if (self->private_impl.f_depth <= 8) { + self->private_impl.f_dst_pixfmt = 2164295816; + self->private_impl.f_src_pixfmt = 2164295816; + } else { + self->private_impl.f_dst_pixfmt = 2164308923; + self->private_impl.f_src_pixfmt = 2164308923; + } + if (self->private_impl.f_chunk_length != 6) { + status = wuffs_base__make_status(wuffs_png__error__bad_chunk); + goto exit; + } + self->private_impl.f_chunk_length = 0; + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); + uint64_t t_1; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 6)) { + t_1 = ((uint64_t)(wuffs_base__peek_u48be__no_bounds_check(iop_a_src))); + iop_a_src += 6; + } else { + self->private_data.s_decode_trns[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); + 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_trns[0].scratch; + uint32_t num_bits_1 = ((uint32_t)(*scratch & 0xFF)); + *scratch >>= 8; + *scratch <<= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_1); + if (num_bits_1 == 40) { + t_1 = ((uint64_t)(*scratch >> 16)); + break; + } + num_bits_1 += 8; + *scratch |= ((uint64_t)(num_bits_1)); + } + } + v_u = t_1; + } + if (self->private_impl.f_depth <= 8) { + self->private_impl.f_remap_transparency = ((255 & (v_u >> 0)) | + (65280 & (v_u >> 8)) | + (16711680 & (v_u >> 16)) | + 4278190080); + } else { + self->private_impl.f_remap_transparency = (v_u | 18446462598732840960u); + } + } else if (self->private_impl.f_color_type == 3) { + self->private_impl.f_dst_pixfmt = 2164523016; + self->private_impl.f_src_pixfmt = 2164523016; + if (self->private_impl.f_chunk_length > 256) { + status = wuffs_base__make_status(wuffs_png__error__bad_chunk); + goto exit; + } + v_n = ((uint32_t)(self->private_impl.f_chunk_length)); + self->private_impl.f_chunk_length = 0; + while (v_i < v_n) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_2 = *iop_a_src++; + self->private_data.f_src_palette[((4 * v_i) + 3)] = t_2; + } + v_i += 1; + } + } else { + status = wuffs_base__make_status(wuffs_png__error__bad_chunk); + goto exit; } goto ok; @@ -38602,8 +38708,8 @@ wuffs_png__decoder__decode_trns( goto suspend; suspend: self->private_impl.p_decode_trns[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; - self->private_data.s_decode_trns[0].v_num_entries = v_num_entries; self->private_data.s_decode_trns[0].v_i = v_i; + self->private_data.s_decode_trns[0].v_n = v_n; goto exit; exit: @@ -39571,14 +39677,6 @@ wuffs_png__decoder__filter_and_swizzle_tricky( if (self->private_impl.f_depth >= 8) { v_src_bytes_per_pixel = (((uint64_t)(WUFFS_PNG__NUM_CHANNELS[self->private_impl.f_color_type])) * ((uint64_t)((self->private_impl.f_depth >> 3)))); } - v_bits_unpacked[0] = 255; - v_bits_unpacked[1] = 255; - v_bits_unpacked[2] = 255; - v_bits_unpacked[3] = 255; - v_bits_unpacked[4] = 255; - v_bits_unpacked[5] = 255; - v_bits_unpacked[6] = 255; - v_bits_unpacked[7] = 255; v_y = ((uint32_t)(WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][5])); while (v_y < self->private_impl.f_height) { v_dst = wuffs_base__table_u8__row(v_tab, v_y); @@ -39622,6 +39720,44 @@ wuffs_png__decoder__filter_and_swizzle_tricky( v_s = wuffs_base__slice_u8__subslice_i(v_s, 2); wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, wuffs_base__slice_u8__subslice_i(v_dst, v_i), v_dst_palette, wuffs_base__make_slice_u8(v_bits_unpacked, 4)); } + } else if (((uint32_t)((self->private_impl.f_remap_transparency & 4294967295))) != 0) { + if (self->private_impl.f_color_type == 0) { + if (1 <= ((uint64_t)(v_s.len))) { + v_bits_unpacked[0] = v_s.ptr[0]; + v_bits_unpacked[1] = v_s.ptr[0]; + v_bits_unpacked[2] = v_s.ptr[0]; + v_bits_unpacked[3] = 255; + v_s = wuffs_base__slice_u8__subslice_i(v_s, 1); + if (((uint32_t)((self->private_impl.f_remap_transparency & 4294967295))) == ((((uint32_t)(v_bits_unpacked[0])) << 0) | + (((uint32_t)(v_bits_unpacked[1])) << 8) | + (((uint32_t)(v_bits_unpacked[2])) << 16) | + (((uint32_t)(v_bits_unpacked[3])) << 24))) { + v_bits_unpacked[0] = 0; + v_bits_unpacked[1] = 0; + v_bits_unpacked[2] = 0; + v_bits_unpacked[3] = 0; + } + wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, wuffs_base__slice_u8__subslice_i(v_dst, v_i), v_dst_palette, wuffs_base__make_slice_u8(v_bits_unpacked, 4)); + } + } else { + if (3 <= ((uint64_t)(v_s.len))) { + v_bits_unpacked[0] = v_s.ptr[2]; + v_bits_unpacked[1] = v_s.ptr[1]; + v_bits_unpacked[2] = v_s.ptr[0]; + v_bits_unpacked[3] = 255; + v_s = wuffs_base__slice_u8__subslice_i(v_s, 3); + if (((uint32_t)((self->private_impl.f_remap_transparency & 4294967295))) == ((((uint32_t)(v_bits_unpacked[0])) << 0) | + (((uint32_t)(v_bits_unpacked[1])) << 8) | + (((uint32_t)(v_bits_unpacked[2])) << 16) | + (((uint32_t)(v_bits_unpacked[3])) << 24))) { + v_bits_unpacked[0] = 0; + v_bits_unpacked[1] = 0; + v_bits_unpacked[2] = 0; + v_bits_unpacked[3] = 0; + } + wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, wuffs_base__slice_u8__subslice_i(v_dst, v_i), v_dst_palette, wuffs_base__make_slice_u8(v_bits_unpacked, 4)); + } + } } else if (v_src_bytes_per_pixel <= ((uint64_t)(v_s.len))) { wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, wuffs_base__slice_u8__subslice_i(v_dst, v_i), v_dst_palette, wuffs_base__slice_u8__subslice_j(v_s, v_src_bytes_per_pixel)); v_s = wuffs_base__slice_u8__subslice_i(v_s, v_src_bytes_per_pixel); @@ -39647,7 +39783,23 @@ wuffs_png__decoder__filter_and_swizzle_tricky( v_bits_unpacked[0] = ((uint8_t)((v_bits_packed >> v_shift) * v_multiplier)); v_bits_packed = ((uint8_t)(v_bits_packed << self->private_impl.f_depth)); v_packs_remaining = ((uint8_t)(v_packs_remaining - 1)); - wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, wuffs_base__slice_u8__subslice_i(v_dst, v_i), v_dst_palette, wuffs_base__make_slice_u8(v_bits_unpacked, 1)); + if (((uint32_t)((self->private_impl.f_remap_transparency & 4294967295))) != 0) { + v_bits_unpacked[1] = v_bits_unpacked[0]; + v_bits_unpacked[2] = v_bits_unpacked[0]; + v_bits_unpacked[3] = 255; + if (((uint32_t)((self->private_impl.f_remap_transparency & 4294967295))) == ((((uint32_t)(v_bits_unpacked[0])) << 0) | + (((uint32_t)(v_bits_unpacked[1])) << 8) | + (((uint32_t)(v_bits_unpacked[2])) << 16) | + (((uint32_t)(v_bits_unpacked[3])) << 24))) { + v_bits_unpacked[0] = 0; + v_bits_unpacked[1] = 0; + v_bits_unpacked[2] = 0; + v_bits_unpacked[3] = 0; + } + wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, wuffs_base__slice_u8__subslice_i(v_dst, v_i), v_dst_palette, wuffs_base__make_slice_u8(v_bits_unpacked, 4)); + } else { + wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, wuffs_base__slice_u8__subslice_i(v_dst, v_i), v_dst_palette, wuffs_base__make_slice_u8(v_bits_unpacked, 1)); + } } v_x += (((uint32_t)(1)) << WUFFS_PNG__INTERLACING[self->private_impl.f_interlace_pass][0]); } @@ -39663,7 +39815,26 @@ wuffs_png__decoder__filter_and_swizzle_tricky( v_bits_unpacked[3] = v_s.ptr[0]; v_bits_unpacked[4] = v_s.ptr[1]; v_bits_unpacked[5] = v_s.ptr[0]; + v_bits_unpacked[6] = 255; + v_bits_unpacked[7] = 255; v_s = wuffs_base__slice_u8__subslice_i(v_s, 2); + if (self->private_impl.f_remap_transparency == ((((uint64_t)(v_bits_unpacked[0])) << 0) | + (((uint64_t)(v_bits_unpacked[1])) << 8) | + (((uint64_t)(v_bits_unpacked[2])) << 16) | + (((uint64_t)(v_bits_unpacked[3])) << 24) | + (((uint64_t)(v_bits_unpacked[4])) << 32) | + (((uint64_t)(v_bits_unpacked[5])) << 40) | + (((uint64_t)(v_bits_unpacked[6])) << 48) | + (((uint64_t)(v_bits_unpacked[7])) << 56))) { + v_bits_unpacked[0] = 0; + v_bits_unpacked[1] = 0; + v_bits_unpacked[2] = 0; + v_bits_unpacked[3] = 0; + v_bits_unpacked[4] = 0; + v_bits_unpacked[5] = 0; + v_bits_unpacked[6] = 0; + v_bits_unpacked[7] = 0; + } } } else if (self->private_impl.f_color_type == 2) { if (6 <= ((uint64_t)(v_s.len))) { @@ -39673,7 +39844,26 @@ wuffs_png__decoder__filter_and_swizzle_tricky( v_bits_unpacked[3] = v_s.ptr[2]; v_bits_unpacked[4] = v_s.ptr[1]; v_bits_unpacked[5] = v_s.ptr[0]; + v_bits_unpacked[6] = 255; + v_bits_unpacked[7] = 255; v_s = wuffs_base__slice_u8__subslice_i(v_s, 6); + if (self->private_impl.f_remap_transparency == ((((uint64_t)(v_bits_unpacked[0])) << 0) | + (((uint64_t)(v_bits_unpacked[1])) << 8) | + (((uint64_t)(v_bits_unpacked[2])) << 16) | + (((uint64_t)(v_bits_unpacked[3])) << 24) | + (((uint64_t)(v_bits_unpacked[4])) << 32) | + (((uint64_t)(v_bits_unpacked[5])) << 40) | + (((uint64_t)(v_bits_unpacked[6])) << 48) | + (((uint64_t)(v_bits_unpacked[7])) << 56))) { + v_bits_unpacked[0] = 0; + v_bits_unpacked[1] = 0; + v_bits_unpacked[2] = 0; + v_bits_unpacked[3] = 0; + v_bits_unpacked[4] = 0; + v_bits_unpacked[5] = 0; + v_bits_unpacked[6] = 0; + v_bits_unpacked[7] = 0; + } } } else if (self->private_impl.f_color_type == 4) { if (4 <= ((uint64_t)(v_s.len))) { diff --git a/std/png/decode_png.wuffs b/std/png/decode_png.wuffs index 3c958376..d421d95d 100644 --- a/std/png/decode_png.wuffs +++ b/std/png/decode_png.wuffs @@ -98,6 +98,17 @@ pub struct decoder? implements base.image_decoder( zlib_is_dirty : base.bool, + // remap_transparency, if non-zero, is the 32-bit or 64-bit (depending on + // this.depth) argb_nonpremul color that is nominally opaque but remapped + // to transparent black. + // + // "Remapped" is an unofficial term. This is where the IHDR color type is 0 + // (Y) or 2 (RGB) but there's also a tRNS chunk. + // + // PNG transparency that isn't "remapped" is color type 3 (indexed) with + // tRNS, color type 4 (YA) or color type 6 (RGBA). + remap_transparency : base.u64, + dst_pixfmt : base.u32, src_pixfmt : base.u32, @@ -311,7 +322,7 @@ pri func decoder.decode_ihdr?(src: base.io_reader) { this.filter_distance = 0 this.assign_filter_distance!() if this.filter_distance == 0 { - return "#unsupported PNG file" + return "#bad header" } this.overall_workbuf_length = (this.height as base.u64) * (1 + this.calculate_bytes_per_row(width: this.width)) @@ -320,7 +331,9 @@ pri func decoder.decode_ihdr?(src: base.io_reader) { pri func decoder.assign_filter_distance!() { if this.depth < 8 { - if this.color_type == 0 { + if (this.depth <> 1) and (this.depth <> 2) and (this.depth <> 4) { + return nothing + } else if this.color_type == 0 { this.dst_pixfmt = base.PIXEL_FORMAT__Y this.src_pixfmt = base.PIXEL_FORMAT__Y } else if this.color_type == 3 { @@ -607,33 +620,78 @@ pri func decoder.decode_srgb?(src: base.io_reader) { } pri func decoder.decode_trns?(src: base.io_reader) { - var num_entries : base.u32[..= 256] - var i : base.u32 + var i : base.u32 + var n : base.u32[..= 256] + var u : base.u64 - if this.chunk_length > 256 { - return "#bad chunk" - } - num_entries = this.chunk_length as base.u32 - - while i < num_entries { - assert i < 256 via "a < b: a < c; c <= b"(c: num_entries) - this.src_palette[(4 * i) + 3] = args.src.read_u8?() - i += 1 - } endwhile - - if this.color_type == 3 { - this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL - this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL - } else { + if this.color_type == 0 { choose filter_and_swizzle = [filter_and_swizzle_tricky] - if this.color_type == 0 { - // TODO: PIXEL_FORMAT__YA_NONPREMUL - this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL - this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE - } else if this.color_type == 2 { + if this.depth <= 8 { this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL + this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL + } else { + this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE } + if this.chunk_length <> 2 { + return "#bad chunk" + } + this.chunk_length = 0 + + u = args.src.read_u16be_as_u64?() + if this.depth <= 1 { + this.remap_transparency = ((u & 0x01) * 0xFF_FFFF) | 0xFF00_0000 + } else if this.depth <= 2 { + this.remap_transparency = ((u & 0x03) * 0x55_5555) | 0xFF00_0000 + } else if this.depth <= 4 { + this.remap_transparency = ((u & 0x0F) * 0x11_1111) | 0xFF00_0000 + } else if this.depth <= 8 { + this.remap_transparency = ((u & 0xFF) * 0x01_0101) | 0xFF00_0000 + } else { + this.remap_transparency = (u * 0x0001_0001_0001) | 0xFFFF_0000_0000_0000 + } + + } else if this.color_type == 2 { + choose filter_and_swizzle = [filter_and_swizzle_tricky] + if this.depth <= 8 { + this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL + this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL + } else { + this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE + this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE + } + if this.chunk_length <> 6 { + return "#bad chunk" + } + this.chunk_length = 0 + + u = args.src.read_u48be_as_u64?() + if this.depth <= 8 { + this.remap_transparency = + (0x0000_00FF & (u >> 0)) | + (0x0000_FF00 & (u >> 8)) | + (0x00FF_0000 & (u >> 16)) | + 0xFF00_0000 + } else { + this.remap_transparency = u | 0xFFFF_0000_0000_0000 + } + + } else if this.color_type == 3 { + this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL + this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL + if this.chunk_length > 256 { + return "#bad chunk" + } + n = this.chunk_length as base.u32 + this.chunk_length = 0 + while i < n { + assert i < 256 via "a < b: a < c; c <= b"(c: n) + this.src_palette[(4 * i) + 3] = args.src.read_u8?() + i += 1 + } endwhile + + } else { + return "#bad chunk" } } diff --git a/std/png/decode_swizzle_tricky.wuffs b/std/png/decode_swizzle_tricky.wuffs index 44dd36c1..653e0c8c 100644 --- a/std/png/decode_swizzle_tricky.wuffs +++ b/std/png/decode_swizzle_tricky.wuffs @@ -55,15 +55,6 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf: ((this.depth >> 3) as base.u64) } - bits_unpacked[0] = 0xFF - bits_unpacked[1] = 0xFF - bits_unpacked[2] = 0xFF - bits_unpacked[3] = 0xFF - bits_unpacked[4] = 0xFF - bits_unpacked[5] = 0xFF - bits_unpacked[6] = 0xFF - bits_unpacked[7] = 0xFF - y = INTERLACING[this.interlace_pass][5] as base.u32 while y < this.height { assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.height) @@ -118,6 +109,54 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf: dst_palette: dst_palette, src: bits_unpacked[.. 4]) } + + } else if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) <> 0 { + if this.color_type == 0 { + if 1 <= s.length() { + bits_unpacked[0] = s[0] + bits_unpacked[1] = s[0] + bits_unpacked[2] = s[0] + bits_unpacked[3] = 0xFF + s = s[1 ..] + if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == ( + ((bits_unpacked[0] as base.u32) << 0) | + ((bits_unpacked[1] as base.u32) << 8) | + ((bits_unpacked[2] as base.u32) << 16) | + ((bits_unpacked[3] as base.u32) << 24)) { + bits_unpacked[0] = 0 + bits_unpacked[1] = 0 + bits_unpacked[2] = 0 + bits_unpacked[3] = 0 + } + this.swizzler.swizzle_interleaved_from_slice!( + dst: dst[i ..], + dst_palette: dst_palette, + src: bits_unpacked[.. 4]) + } + } else { + if 3 <= s.length() { + bits_unpacked[0] = s[2] + bits_unpacked[1] = s[1] + bits_unpacked[2] = s[0] + bits_unpacked[3] = 0xFF + s = s[3 ..] + if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == ( + ((bits_unpacked[0] as base.u32) << 0) | + ((bits_unpacked[1] as base.u32) << 8) | + ((bits_unpacked[2] as base.u32) << 16) | + ((bits_unpacked[3] as base.u32) << 24)) { + bits_unpacked[0] = 0 + bits_unpacked[1] = 0 + bits_unpacked[2] = 0 + bits_unpacked[3] = 0 + } + this.swizzler.swizzle_interleaved_from_slice!( + dst: dst[i ..], + dst_palette: dst_palette, + src: bits_unpacked[.. 4]) + } + } + } else if src_bytes_per_pixel <= s.length() { this.swizzler.swizzle_interleaved_from_slice!( dst: dst[i ..], @@ -153,10 +192,31 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf: bits_packed = bits_packed ~mod<< this.depth packs_remaining = packs_remaining ~mod- 1 - this.swizzler.swizzle_interleaved_from_slice!( - dst: dst[i ..], - dst_palette: dst_palette, - src: bits_unpacked[.. 1]) + if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) <> 0 { + bits_unpacked[1] = bits_unpacked[0] + bits_unpacked[2] = bits_unpacked[0] + bits_unpacked[3] = 0xFF + if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == ( + ((bits_unpacked[0] as base.u32) << 0) | + ((bits_unpacked[1] as base.u32) << 8) | + ((bits_unpacked[2] as base.u32) << 16) | + ((bits_unpacked[3] as base.u32) << 24)) { + bits_unpacked[0] = 0 + bits_unpacked[1] = 0 + bits_unpacked[2] = 0 + bits_unpacked[3] = 0 + } + this.swizzler.swizzle_interleaved_from_slice!( + dst: dst[i ..], + dst_palette: dst_palette, + src: bits_unpacked[.. 4]) + + } else { + this.swizzler.swizzle_interleaved_from_slice!( + dst: dst[i ..], + dst_palette: dst_palette, + src: bits_unpacked[.. 1]) + } } x += (1 as base.u32) << INTERLACING[this.interlace_pass][0] } endwhile @@ -176,8 +236,29 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf: bits_unpacked[3] = s[0] bits_unpacked[4] = s[1] bits_unpacked[5] = s[0] + bits_unpacked[6] = 0xFF + bits_unpacked[7] = 0xFF s = s[2 ..] + if this.remap_transparency == ( + ((bits_unpacked[0] as base.u64) << 0) | + ((bits_unpacked[1] as base.u64) << 8) | + ((bits_unpacked[2] as base.u64) << 16) | + ((bits_unpacked[3] as base.u64) << 24) | + ((bits_unpacked[4] as base.u64) << 32) | + ((bits_unpacked[5] as base.u64) << 40) | + ((bits_unpacked[6] as base.u64) << 48) | + ((bits_unpacked[7] as base.u64) << 56)) { + bits_unpacked[0] = 0 + bits_unpacked[1] = 0 + bits_unpacked[2] = 0 + bits_unpacked[3] = 0 + bits_unpacked[4] = 0 + bits_unpacked[5] = 0 + bits_unpacked[6] = 0 + bits_unpacked[7] = 0 + } } + } else if this.color_type == 2 { if 6 <= s.length() { bits_unpacked[0] = s[5] @@ -186,8 +267,29 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf: bits_unpacked[3] = s[2] bits_unpacked[4] = s[1] bits_unpacked[5] = s[0] + bits_unpacked[6] = 0xFF + bits_unpacked[7] = 0xFF s = s[6 ..] + if this.remap_transparency == ( + ((bits_unpacked[0] as base.u64) << 0) | + ((bits_unpacked[1] as base.u64) << 8) | + ((bits_unpacked[2] as base.u64) << 16) | + ((bits_unpacked[3] as base.u64) << 24) | + ((bits_unpacked[4] as base.u64) << 32) | + ((bits_unpacked[5] as base.u64) << 40) | + ((bits_unpacked[6] as base.u64) << 48) | + ((bits_unpacked[7] as base.u64) << 56)) { + bits_unpacked[0] = 0 + bits_unpacked[1] = 0 + bits_unpacked[2] = 0 + bits_unpacked[3] = 0 + bits_unpacked[4] = 0 + bits_unpacked[5] = 0 + bits_unpacked[6] = 0 + bits_unpacked[7] = 0 + } } + } else if this.color_type == 4 { if 4 <= s.length() { bits_unpacked[0] = s[1] @@ -200,6 +302,7 @@ pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf: bits_unpacked[7] = s[2] s = s[4 ..] } + } else { if 8 <= s.length() { bits_unpacked[0] = s[5] diff --git a/test/nia-checksums-of-data.txt b/test/nia-checksums-of-data.txt index 65cfa28f..5500e274 100644 --- a/test/nia-checksums-of-data.txt +++ b/test/nia-checksums-of-data.txt @@ -1,6 +1,6 @@ # Generated by script/print-nia-checksums.sh d3bb0beb test/data/DCI-P3-D65.icc -c4a44cef test/data/animated-red-blue.apng +646e081a test/data/animated-red-blue.apng 76e40058 test/data/animated-red-blue.gif f9a10dac test/data/artificial/gif-background-color.gif 36e7d9c7 test/data/artificial/gif-empty-palette.gif @@ -54,7 +54,7 @@ a80781a1 test/data/hippopotamus.interlaced.truncated.gif 21e17862 test/data/hippopotamus.regular.gif dcbb225a test/data/hippopotamus.regular.png 2dccfe56 test/data/muybridge-frame-000.wbmp -fc4777aa test/data/muybridge.apng +3a4145fa test/data/muybridge.apng db2733f5 test/data/muybridge.gif bf7e8c96 test/data/pjw-thumbnail.bmp 73d48c08 test/data/pjw-thumbnail.gif