From da0d51d802379cd44edf60040301d71ecd723623 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 19 Apr 2024 21:31:02 +1000 Subject: [PATCH] std/png: ignore tRNS chunks for color types 4, 6 Updates #146 --- doc/changelog.md | 1 + release/c/wuffs-unsupported-snapshot.c | 26 ++++++++++++++------------ std/png/decode_png.wuffs | 13 +++++++++++-- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/doc/changelog.md b/doc/changelog.md index c5577582..f8c706cf 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -33,6 +33,7 @@ The LICENSE has changed from a single license (Apache 2) to a dual license - Fixed `PIXEL_FORMAT__YA_{NON,}PREMUL` constant values. - Generated constants now default to unsigned. - Halved the sizeof `wuffs_foo__bar::unique_ptr`. +- Have `std/png` ignore tRNS chunks for color types 4 (YA) and 6 (RGBA). - Let `std/png` decode PNG color type 4 to `PIXEL_FORMAT__YA_NONPREMUL` (two channels) instead of `PIXEL_FORMAT__BGRA_NONPREMUL` (four channels). - Removed the `std/gif -> std/lzw` dependency. diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 360d2778..a3b37c04 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -62137,20 +62137,22 @@ wuffs_png__decoder__decode_other_chunk( self->private_impl.f_seen_srgb = true; } } else if (self->private_impl.f_chunk_type == 1397641844u) { - if (self->private_impl.f_seen_trns || (self->private_impl.f_color_type > 3u) || ((self->private_impl.f_color_type == 3u) && ! self->private_impl.f_seen_plte)) { + if (self->private_impl.f_seen_trns || ((self->private_impl.f_color_type == 3u) && ! self->private_impl.f_seen_plte)) { status = wuffs_base__make_status(wuffs_png__error__bad_chunk); goto exit; - } - if (a_src) { - a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); - } - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9); - status = wuffs_png__decoder__decode_trns(self, a_src); - if (a_src) { - iop_a_src = a_src->data.ptr + a_src->meta.ri; - } - if (status.repr) { - goto suspend; + } else if (self->private_impl.f_color_type > 3u) { + } else { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9); + status = wuffs_png__decoder__decode_trns(self, a_src); + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } + if (status.repr) { + goto suspend; + } } self->private_impl.f_seen_trns = true; } diff --git a/std/png/decode_png.wuffs b/std/png/decode_png.wuffs index d18de834..c4785acd 100644 --- a/std/png/decode_png.wuffs +++ b/std/png/decode_png.wuffs @@ -617,11 +617,20 @@ pri func decoder.decode_other_chunk?(src: base.io_reader, framy: base.bool) { } } else if this.chunk_type == 'tRNS'le { - if this.seen_trns or (this.color_type > 3) or + if this.seen_trns or ((this.color_type == 3) and (not this.seen_plte)) { return "#bad chunk" + } else if this.color_type > 3 { + // No-op, ignoring the chunk. The PNG spec section "11.3.2.1 + // tRNS Transparency" says that "A tRNS chunk shall not appear + // for colour types 4 and 6, since a full alpha channel is + // already present in those cases." However, some real-world + // PNG images (incorrectly) have these. libpng treats this as a + // "benign error", not a fatal one. See + // https://github.com/google/wuffs/issues/146 + } else { + this.decode_trns?(src: args.src) } - this.decode_trns?(src: args.src) this.seen_trns = true } }