From fe66a2aed2c6c2f3237f2515bbd54fa4fb24b18f Mon Sep 17 00:00:00 2001 From: mushus <6138982+Mushus@users.noreply.github.com> Date: Sun, 19 Feb 2023 00:52:15 +0900 Subject: [PATCH] [bugfix] Keep png transparency (#1522) * keep png transparency * rewrite to switch case --- internal/media/png-stripper.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/internal/media/png-stripper.go b/internal/media/png-stripper.go index e23ac2cd..485f4c93 100644 --- a/internal/media/png-stripper.go +++ b/internal/media/png-stripper.go @@ -78,9 +78,29 @@ import ( "io" ) -// chunkTypeAncillaryBit is whether the first byte of a big-endian uint32 chunk -// type (the first of four ASCII letters) is lower-case. -const chunkTypeAncillaryBit = 0x20000000 +const ( + chunkTypeIHDR = 0x49484452 + chunkTypePLTE = 0x504C5445 + chunkTypeIDAT = 0x49444154 + chunkTypeIEND = 0x49454E44 + chunkTypeTRNS = 0x74524e53 +) + +func isNecessaryChunkType(chunkType uint32) bool { + switch chunkType { + case chunkTypeIHDR: + return true + case chunkTypePLTE: + return true + case chunkTypeIDAT: + return true + case chunkTypeIEND: + return true + case chunkTypeTRNS: + return true + } + return false +} // pngAncillaryChunkStripper wraps another io.Reader to strip ancillary chunks, // if the data is in the PNG file format. If the data isn't PNG, it is passed @@ -179,7 +199,7 @@ func (r *pngAncillaryChunkStripper) Read(p []byte) (int, error) { // byte trailer, a checksum. r.pending = int64(binary.BigEndian.Uint32(r.buffer[:4])) + 4 chunkType := binary.BigEndian.Uint32(r.buffer[4:]) - r.discard = (chunkType & chunkTypeAncillaryBit) != 0 + r.discard = !isNecessaryChunkType(chunkType) if r.discard { r.rIndex = r.wIndex }