Implementing a Basic PNG reader the handmade way

godratio
These kinds of post and writes up should be put somehwhere along with the learning materials being developed.


Created a feature request for the team and I to work on.
Can't get this working at all. 'decompressed_png' and 'image' are just 0s after sh_zlib_decompress and sh_png_defilter.
brothir
Can't get this working at all. 'decompressed_png' and 'image' are just 0s after sh_zlib_decompress and sh_png_defilter.


hey, really sorry for the late reply, did you copy paste the code or write your own? if its a copy paste, can you tell me how to reproduce the error? I wanna fix it. last time I ran this code, it was working when I compiled it, I made sure of it many times. and I just ran it again, and it works, I compiled it on Visual Studio 2015 and I haven't tried GNU C compilers so if you used that please tell me.

Hello,

Well I need some suggestions in order to continue debugging the proper way!

First of all I tried to install ImageWatch on Visual Studio 2017 15.3.0.
It did not work out, even though I have changed extension.vsixmanifest to "support" the 2017 version, because it is currently unavailable for the latest version of Visual Studio.

However I managed to get the reader working in my project. The issue appears when I try to draw it with OpenGL on the screen. In order to see if I have not made any mistakes. The "picturesque" alphabet appears:

https://imgur.com/a/n3LAE

According to the color type it says "2" that translated from PNG Doc means it is RGB triplet.
So I used the following OpenGL call...
1
2
  
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, Image); <-----


Now after I see that something obviously is wrong. I am looking for possible suggestions where the issue might be.
Is it something wrong in how I set up the texture? Or it is decompression and filtering?

Also I want to examine the raw data... Is it possible to writefile() and then read it with another program somehow without adding a specific header say BMP or whatever.

Or I have to examine it byte by byte in hex after using libpng and compare manually both results?

Appreciate your help!





Enyo Enev

According to the color type it says "2" that translated from PNG Doc means it is RGB triplet.


The implementation I provided in the tutorial doesn't take into account color types other than 0 and images that might have more than one IDATA chunk, both are very simple to implement:
  1. Color types other than 0 will need changes in the de-filtering code, its basically related to "how many bytes" you should move by when you defilter.
  2. an image could contain (n) chunks, thus the best way to extract them is to loop until you hit IEND, if an image contains more than one IDATA chunk, you must extract them and combine them to one contiguous chunk for the decompressor.
  3. I have both of those "almost" implemented in my own local copy but haven't fully tested extensively and don't have any writing to go along with it.
Thank you! Saved me a lot of time...

I just misunderstood the zlib type with the color type. HOW could I ?!? :D

#define Z_HUFFMAN_ONLY 2

Have a nice day.

Edited by Enyo Enev on
glad you made it work :D