Decoding of some PNG files generated by GDAL2Tiles (raster map tile generator) fails with the error message "not enough pixel data". Most other decoders can read these files without any issues.
Even though the error message suggests otherwise, the problematic files actually contain too much data. Following the expected IDAT chunks containing all of the image data, there is an additional zero-length IDAT chunk.
According to the PNG specification, zero-length IDAT chunks are valid. Trailing garbage may not be valid, but the spec explicitly allows ignoring of errors that have no effect on the processing of the image.
The error message "not enough pixel data" is generated after the decoder successfully decodes the image: it then encounters the trailing zero-length IDAT chunk and attempts to decode it as if it were the first IDAT chunk in the image.
No error is generated when the decoder encounters trailing IDAT chunks that contain actual image data, as long as that image data can be decoded with the PNG header (and palette) already seen. This allows users to craft images that trick the decoder into decoding a second image appended after the first.
See the image below, which most decoders will show as plain white, while image/png will happily reveal the gopher hidden within:
Decoding of some PNG files generated by GDAL2Tiles (raster map tile generator) fails with the error message "not enough pixel data". Most other decoders can read these files without any issues.
Even though the error message suggests otherwise, the problematic files actually contain too much data. Following the expected IDAT chunks containing all of the image data, there is an additional zero-length IDAT chunk.
According to the PNG specification, zero-length IDAT chunks are valid. Trailing garbage may not be valid, but the spec explicitly allows ignoring of errors that have no effect on the processing of the image.
The error message "not enough pixel data" is generated after the decoder successfully decodes the image: it then encounters the trailing zero-length IDAT chunk and attempts to decode it as if it were the first IDAT chunk in the image.
No error is generated when the decoder encounters trailing IDAT chunks that contain actual image data, as long as that image data can be decoded with the PNG header (and palette) already seen. This allows users to craft images that trick the decoder into decoding a second image appended after the first.
See the image below, which most decoders will show as plain white, while image/png will happily reveal the gopher hidden within:
CL with proposed fix following.