From a68492824004a76c59e2e12e2611414e59c0c1b3 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 20 Nov 2021 15:39:07 +1100 Subject: [PATCH] Let std/png allow PLTE for RGB, RGBA color types --- release/c/wuffs-unsupported-snapshot.c | 29 +++++++++++++---------- std/png/decode_png.wuffs | 12 ++++++++-- test/3pdata/nia-checksums-of-pngsuite.txt | 2 ++ 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index a8aeb09a..5275a315 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -37927,20 +37927,25 @@ wuffs_png__decoder__decode_other_chunk( WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; if (self->private_impl.f_chunk_type == 1163152464) { - if (self->private_impl.f_seen_plte || (self->private_impl.f_color_type != 3)) { + if (self->private_impl.f_seen_plte) { + status = wuffs_base__make_status(wuffs_png__error__bad_chunk); + goto exit; + } else if (self->private_impl.f_color_type == 3) { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + status = wuffs_png__decoder__decode_plte(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 == 2) || (self->private_impl.f_color_type == 6)) { + } else { 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(1); - status = wuffs_png__decoder__decode_plte(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_plte = true; } else if ((self->private_impl.f_chunk_type & 32) == 0) { diff --git a/std/png/decode_png.wuffs b/std/png/decode_png.wuffs index 54e17e7f..a18d8a0c 100644 --- a/std/png/decode_png.wuffs +++ b/std/png/decode_png.wuffs @@ -535,10 +535,18 @@ pri func decoder.choose_filter_implementations!() { pri func decoder.decode_other_chunk?(src: base.io_reader) { if this.chunk_type == 'PLTE'le { - if this.seen_plte or (this.color_type <> 3) { + if this.seen_plte { + return "#bad chunk" + } else if this.color_type == 3 { + // Color type 3 means paletted. + this.decode_plte?(src: args.src) + } else if (this.color_type == 2) or (this.color_type == 6) { + // Color types 2 and 6 means RGB and RGBA. In these cases, the PLTE + // chunk is merely a hint, like a sPLT "suggested palette" chunk. + // We ignore it. + } else { return "#bad chunk" } - this.decode_plte?(src: args.src) this.seen_plte = true } else if (this.chunk_type & ANCILLARY_BIT) == 0 { diff --git a/test/3pdata/nia-checksums-of-pngsuite.txt b/test/3pdata/nia-checksums-of-pngsuite.txt index f8c7184e..bad2c8ea 100644 --- a/test/3pdata/nia-checksums-of-pngsuite.txt +++ b/test/3pdata/nia-checksums-of-pngsuite.txt @@ -101,6 +101,8 @@ bb956205 test/3pdata/pngsuite/oi4n0g16.png 84bc9be8 test/3pdata/pngsuite/oi4n2c16.png bb956205 test/3pdata/pngsuite/oi9n0g16.png 84bc9be8 test/3pdata/pngsuite/oi9n2c16.png +84bc9be8 test/3pdata/pngsuite/pp0n2c16.png +91dbb0d0 test/3pdata/pngsuite/pp0n6a08.png 024cf410 test/3pdata/pngsuite/ps1n0g08.png 84bc9be8 test/3pdata/pngsuite/ps1n2c16.png 024cf410 test/3pdata/pngsuite/ps2n0g08.png