[Solved] Problem viewing certain PNG files

Bugs found in XnView Classic. Please report only one bug per topic!

Moderators: XnTriq, xnview

Post Reply
User avatar
XnTriq
Moderator & Librarian
Posts: 5574
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

[Solved] Problem viewing certain PNG files

Post by XnTriq » Sat Jan 18, 2020 9:00 am

TweakPNG's validity check gives these PNGs a clean bill of health, and I can open them in Photoshop, Firefox etc., but XnView Classic/MP and MS Paint don't like them:

User avatar
xnview
Author of XnView
Posts: 33337
Joined: Mon Oct 13, 2003 7:31 am
Location: France
Contact:

Re: Problem viewing certain PNG files

Post by xnview » Fri Jan 24, 2020 1:09 pm

LIBPNG has this error "IDAT: invalid distance too far back"
Pierre.

User avatar
XnTriq
Moderator & Librarian
Posts: 5574
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Problem viewing certain PNG files

Post by XnTriq » Fri Jan 24, 2020 1:45 pm

xnview wrote:
Fri Jan 24, 2020 1:09 pm
LIBPNG has this error "IDAT: invalid distance too far back"
Thanks for checking, Pierre!

http://www.libpng.org/pub/png/libpng-manual.txt wrote:Libpng-1.6.0 through 1.6.2 used the CMF bytes at the beginning of the IDAT stream to set the size of the sliding window for reading instead of using the default 32-kbyte sliding window size. It was discovered that there are hundreds of PNG files in the wild that have incorrect CMF bytes that caused zlib to issue the "invalid distance too far back" error and reject the file.
Libpng-1.6.3 and later calculate their own safe CMF from the image dimensions, provide a way to revert to the libpng-1.5.x behavior (ignoring the CMF bytes and using a 32-kbyte sliding window), by using

Code: Select all

png_set_option(png_ptr, PNG_MAXIMUM_INFLATE_WINDOW,
    PNG_OPTION_ON);
and provide a tool (contrib/tools/pngfix) for rewriting a PNG file while optimizing the CMF bytes in its IDAT chunk correctly.
pngfix --help wrote:pngfix checks each PNG file on the command line for errors. By default errors arenot output and the program just returns an exit code and prints a summary.
With the --quiet (-q) option the summaries are suppressed too and the program only outputs unexpected errors (internal errors and file open errors).
Various known problems in PNG files are fixed while the file is being read
The exit code says what problems were fixed. In particular the zlib error:
  • "invalid distance too far back"
caused by an incorrect optimization of a zlib stream is fixed in any compressed chunk in which it is encountered. An integrity problem of the PNG stream caused by a bug in libpng which wrote an incorrect chunk length is also fixed. Chunk CRC errors are automatically fixed up.

Setting one of the "OUTPUT" options causes the possibly modified file to be written to a new file.

Notice that some PNG files with the zlib optimization problem can still be read by libpng under some circumstances. This program will still detect and, if requested, correct the error.

The program will reliably process all files on the command line unless either an invalid argument causes the usage message (this message) to be produced or the program crashes.

The summary lines describe issues encountered with the zlib compressed stream of a chunk.
http://www.libpng.org/pub/png/apps/pngcheck.html wrote:pngcheck verifies the integrity of PNG, JNG and MNG files (by checking the internal 32-bit CRCs [checksums] and decompressing the image data); it can optionally dump almost all of the chunk-level information in the image in human-readable form. For example, it can be used to print the basic statistics about an image (dimensions, bit depth, etc.); to list the color and transparency info in its palette (assuming it has one); or to extract the embedded text annotations. This is a command-line program with batch capabilities.

[…]

Also included (since version 2.1.0) are two helper utilities:
  • pngsplit - break a PNG, MNG or JNG image into constituent chunks (numbered for easy reassembly)
  • png-fix-IDAT-windowsize - fix minor zlib-header breakage caused by libpng 1.2.6

Posts on the subject by Glenn Randers-Pehrson (one of the original designers of the PNG format and a co-founder of the PNG Development Group) who – as I just learned – passed away in October of 2018:
https://bbs.archlinux.org/viewtopic.php?pid=1371253#p1371253 wrote:The "too far back" error report was introduced in libpng-1.6.0 and removed from libpng-1.6.3. So, please upgrade to libpng-1.6.3 or later (current version is 1.6.8).
https://www.sourceforge.net/p/libpng/bugs/222/#74f4 wrote:This is a known issue. You can use libpng's "contrib/tools/pngfix", "pngcrush", or any libng15-based application to repair such invalid PNG files. The problem is that the zlib datastream looks back further than the beginning of the sliding window while decompressing.
https://www.github.com/glennrp/libpng/issues/183#issuecomment-341529492 wrote:As you reported, and according to "pngcheck -v" it contains the "too far back" error in the IDAT (zlib) data. You can use pngfix to repair it. pngfix comes with the libpng16 distribution, and you run

Code: Select all

pngfix --out=problem-fixed.png problem.png
to repair it.

User avatar
XnTriq
Moderator & Librarian
Posts: 5574
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: [Solved] Problem viewing certain PNG files

Post by XnTriq » Fri Jan 24, 2020 3:30 pm

How to repair b0rked PNGs like pdfwriter.png with png-fix-IDAT-windowsize:

pngcheck.exe -vt pdfwriter.png

Code: Select all

File: pdfwriter.png (14173 bytes)
  chunk IHDR at offset 0x0000c, length 13
    509 x 257 image, 24-bit RGB, non-interlaced
  chunk IDAT at offset 0x00025, length 8192
    zlib: deflated, 4K window, maximum compression
    zlib: inflate error = -3 (data error)
ERRORS DETECTED in pdfwriter.png

png-fix-IDAT-windowsize.exe -verbose pdfwriter.png

Code: Select all

png-fix-IDAT-windowsize, version 0.5 of 1 August 2005, by Greg Roelofs.
  This software is licensed under the GNU General Public License.
  There is NO warranty.
  Compiled with zlib 1.2.3; using zlib 1.2.3.

pdfwriter.png:
  509x257, 24-bit, non-interlaced, RGB (type 2) image
    IDAT chunk (0x49444154), length 8192, at file offset 37 (0x00025)

No errors detected while fixing 1 PNG image.

pngcheck.exe -vt pdfwriter-fixed.png

Code: Select all

File: pdfwriter-fixed.png (14173 bytes)
  chunk IHDR at offset 0x0000c, length 13
    509 x 257 image, 24-bit RGB, non-interlaced
  chunk IDAT at offset 0x00025, length 8192
    zlib: deflated, 32K window, default compression
  chunk IDAT at offset 0x02031, length 5912
  chunk IEND at offset 0x03755, length 0
No errors detected in pdfwriter-fixed.png (4 chunks, 96.4% compression).

pdfwriter.png
pdfwriter.png (13.84 KiB) Viewed 792 times
pdfwriter-fixed.png
pdfwriter-fixed.png (13.84 KiB) Viewed 792 times

Post Reply