foliojs / png.js

A (animated) PNG decoder in JavaScript for the HTML5 canvas element and Node.js
http://devongovett.github.com/png.js
MIT License
489 stars 92 forks source link

Extraneous data after IEND not detected #17

Closed ain closed 10 years ago

ain commented 10 years ago

Since the library also detects corrupt files in terms of being incomplete, I'd expect it to also detect any corrupt images that have data succeeding IEND and breaking image minification tools for example, e.g. grunt-contrib-imagemin with

Error: * Processing: app/images/layout/sticky_0.png
    Warning: Extraneous data found after IEND
    125x66 pixels, 4x8 bits/pixel, RGB+alpha
    Reducing image to 2x8 bits/pixel, grayscale+alpha
    Recoverable errors found in input. Rerun OptiPNG with -fix enabled.
    Error: Previous error(s) not fixed

Example image: extraneous

devongovett commented 10 years ago

IEND is supposed to mark the end of the image, so this library doesn't parse beyond that point at all. You could imagine cases where a PNG image is embedded in another file, and there is valid data in that file after the PNG image ends. We wouldn't want the parser to throw an error in that case. Thus, I think that kind of out of bounds checking is out of scope of this library.

If you want to do a manual check as a user of the library, however, you could check whether png.pos < png.data.length after parsing is complete to see if there is more data that hasn't been parsed.

ain commented 10 years ago

Fair enough, I can understand the considerations, thanks for the reply.

P.S. the above comparison is a bit faulty so I resorted to a slightly different condition for the trailing overhead business: pngcheck.js:41.