I've been trying to figure this out by reverse engineering a .png file I created in GIMP. It's 4x4 pixels. My goal is to decode the raw pixels out of the file with the intent of reversing this to encode.
Here is a full hex dump of the file:
89504E47 0D0A1A0A 0000000D 49484452 00000004 00000004
08020000 00269309 29000000 3F494441 54081D01 3400CBFF
01CC96B1 134FE120 C0CECDF1 5101FFA5 60000000 000000E0
403201DF E59286DF 6D000000 00000004 EDB11F00 2E007A21
93EDB11F 3063136F 4733525A 00000000 49454E44 AE426082
According to the spec, we start with the PNG signature which is the first 8 bytes.
89504E47 0D0A1A0A
We then have repeating "chunk" structures, this file has 3 "chunks", the header (IHDR), the image data (IDAT) and then the end "chunk" (IEND).
Each chunk is arranged into: the first 4 bytes for the length of the chunk data, the next 4 bytes for the type of data, then n-bytes for the actual data and then 4 bytes for the cyclic redundancy check (CRC) of the type of data and actual data sections.
Following this through...
0000000D
Is the chunk's data length (13 bytes).
49484452
Is the chunk's type (IHDR).
00000004 00000004 08020000 00
Is the chunk's data (4 bytes width, height; 1 byte bit depth, colour type, compression method, filter method, interlace method).
269309 29
Is the CRC of the data and type (managed to get the code to work this out from here.
000000 3F
Is the next chunk's data length (63 bytes).
494441 54
Is the chunk's type (IDAT).
081D01 3400CBFF 01CC96B1 134FE120 C0CECDF1 5101FFA5 60000000 000000E0 403201DF E59286DF 6D000000 00000004 EDB11F00 2E007A21 93EDB11F 3063136F
Is the chunk's actual data (the image data compressed and filtered).
So my actual question is how do I decode this last section into raw pixels?
According to the spec, I must first decompress the data (INFLATE?) and then unfilter it (??) to be left with scanlines of pixels (my goal).
If this could be explained in pseudocode that would be amazing! Otherwise I'm familiar with Swift and less so with C...