hexpngzlib

Issue with a hand-made PNG file


I have been trying for some time do create a PNG file "from scratch", that is to say to manipulate the data byte per byte in order to create a viable file.

During this quest of mine I have encountered a strange error, whenever the raw pixel data, along with the filter type byte, reaches a length of 256 the image fails to render and all the pixels become black. Just to clarify this doesn't seem to happen with other sizes and I have successfully created images "by hand".

Here is the HEX data of the image color coded to visualize the different chunks: HEX data

Just so that we are on the same page I am going the write an explanation for the contents of every chunk.

PNG INITIAL BYTES

IHDR CHUNK

IDAT CHUNK

IEND CHUNK

I tried to change the color type and the bit depth but as long as the sum of the compressed, actually not compressed, data amounts to 256 the file doesn't render correctly and just replaces every pixel with black ones. I tried every possible window, from 256 to 32768, and nothing changed.

Maybe I need to include some ancillary chunk like sRGB, even tough I have no idea why that should help.


Solution

  • 0x80 this byte represents the fact that this is the last compressed data chunk to process

    No, it does not. The deflate bits start at the bottom, not the top. To do what you describe, that byte needs to be 0x01.

    0x01 0x00 the value of these bytes is the length of the compressed data (256)

    No, it is not. The length is represented in little-endian order, and so 256 would be 0x00 0x01.

    I have successfully created images "by hand".

    Note that just because an image renders does not mean that the PNG file is correct. Software that is trying to display PNG images can choose to be quite liberal in accepting erroneous input. Instead, consider using something like pngcheck to validate your manually-constructed PNG files. Your previous attempts undoubtedly had errors as well. What happened is that you eventually exceeded the ability of the PNG decoder to make some sort of sense of what you were giving it.