0
votes

First a bit of context. I need to flip a PNG image. I get the info per byte (in order) and I have to flip it as a stream. I manage to divide and parse the chunks. But when I have the IDAT chunk data I don't know what to do with it to flip it.

INFO from the IHDR: Bit depth = 08 (8 bits per sample) and colour type = 06. The image is 800 x 600 = 480000

The IDAT size is 179502 so that is 0.374 of the total of pixel size (weird).

If I flip the pixels (all the IDAT data as it is) I get an image that when I open it just displays a transparent image. I also tried using 4 bytes = 1 pixel as I have RGB + alpha but still no good result. (Always a transparent image... that is also very wierd... I was hoping random images, not transparent images).

I have read that the info is compressed, so my plan is to decompress it, flip the RGB array and then compress again, but... how can I decompress and compress it again? I can't find the info/algorithm to do that.

I also find the 0x78 hex almost always after the IDAT part, so I have deduced that the compress pixel array does not start directly after the IDAT but some bytes after... If so... How is it organized? is it only pixel array directy or does it have some info before and/or after in the IDAT chunk data?

1
Hope I was clear enough with my question and I ask forgiveness for my English that is not perfect.Agustin Barrachina

1 Answers

1
votes

The IDAT chunk(s) include the pixel data of the image, prefiltered and compressed. To get the RGB you'd need to:

  • concatenate all the IDAT chunks
  • uncompress the stream (ZLIB)
  • unfilter the filter applied to each row
  • according to the pixel format of the image you might have the RGB components in each byte, or perhaps in words, or perhaps in some bits (and perhaps they are not RGB components, but indexes into a Palette, or grayscale, or with Alpha...)

In short, you need to decode a PNG image, which is not trivial. There are many PNG decoders out there, so I don't know why you'd want to reivent the wheel. If you really want to do that yourself, then you need to read the details of the PNG standard.