Search code examples
pngdeflate

Writing uncompress PNG one/first Deflate block OK second things go wrong


For complex reasons I want to save images using the .PNG standard but without compression. (About the same as here: write png quickly)

I find that if I have one Deflate block in my image, tools like Windows paint, gimp etc. can read the image and it comes out OK. But as soon as I put in a second Deflate block the output image is not what I expected.

The big difference between my code and the "write-png-quickly" is that my first Deflate block is NOT 65535 bytes. But If I read the Deflate standard my format is allowed. I made a test image 4 pixels wide and 2 lines high. Here is a binary dump of what I produce:

0x0000 89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 
0x0010 00 00 00 04 00 00 00 02 08 02 00 00 00 F0 CA EA 
0x0020 34 00 00 00 2A 49 44 41 54 08 1D 00 00 0D FF F2 
0x0030 00 01 02 FF 01 02 FF 01 02 FF 01 02 FF 01 00 0D 
0x0040 FF F2 00 01 02 FF 01 02 FF 01 02 FF 01 02 FF 60 
0x0050 FA 08 11 2F 23 C9 3A 00 00 00 00 49 45 4E 44 AE 
0x0060 42 60 82 

This is the first Deflate: starts with 00 then size and ~size (2 bytes) then the 13 bytes:

0x002B 00 00 0D FF F2 00 01 02 FF 01 02 FF 01 02 FF 01 02 FF

This is the second Deflate: starts with 01 then again 13 bytes:

0x003D 01 00 0D FF F2 00 01 02 FF 01 02 FF 01 02 FF 01 02 FF

The fact that it all works with one Deflate block give me reason to believe that the rest like CRC, Adler checksum is OK.


Solution

  • Those tools must not be properly checking for errors, since your single stored block is messed up as well.

    The lengths need to be in little-endian order. You have them in big-endian order. So the first block should start with: 00 0D 00 F2 FF.