When decoding paletted PNG images, image/png.Decode makes an effort to handle the case where the PNG IDAT (pixel data) section refers to a palette entry index beyond the end of the palette (see src/png/reader.go).
However, in the case where the PNG PLTE section contains exactly 255 colors, but the PNG IDAT section contains references to 256 colors, the result of image/png.Decode is an invalid PalettedImage and nil error. The invalid Image panics when Image.At is invoked for pixels that reference the 256th color.
As the above example shows, if the PNG PLTE contains 254 (or fewer) colors the decode image is valid and extra palette entries, initialize to black, for the out-of-range pixels.
I believe the problem is that readImagePass in png/reader.go should only skip palette size extension when the palette has 256 colors, not 255.
The text was updated successfully, but these errors were encountered: