views:

408

answers:

4

Hi All,

How can I use zlib library to decompress a PNG file? I have no idea how to read a PNG file. When I opened a PNG file in TextPad(a text editor), the data was not at all understandable. Is it because a PNG file is compressed?

Any help is greatly appreciated. I need to read a PNG file using a C program and gcc compiler.

Regards, darkie

+3  A: 

Why not use libpng? The PNG file format is fairly simple, but there are many different possible variations and encoding methods and it can be fairly tedious to ensure you cover all of the cases. Something like libpng handles all the conversion and stuff for you automatically.

Dean Harding
Hi, Because I have been told to use standard C libraries, I cannot use libpng library?
darkie15
Told by whom? Is this homework? If so, you should add a tag for 'homework'.
Dean Harding
Ok done that. Can you please guide me now?
darkie15
@darkie15: The zlib library is not any more a "standard C library" than libpng is. If this is really the assignment, I think you are supposed to use `fread` and friends?
Thomas
Hi Thomas, I did not get you. what do you mean by 'friends'??
darkie15
I said the format is fairly simple, but for a beginner (which you seem to be) it's going to take a while... I would start with the specification document (http://www.libpng.org/pub/png/spec/1.1/PNG-Contents.html) which is pretty good and goes into a *lot* of detail. If you have any more (specific) questions regarding the specification, feel free to ask more questions here.
Dean Harding
Thanks codeka. I have a question: When I open up the PNG file in a text editor, the data is not at all human readable. Is it because the PNG file is compressed right now? Can you please help me understand this?
darkie15
A: 

You should probably read up on how a binary file-format works and use a hex-editor instead of a text-editor to look at the files. Generally you should use libpng to handle png-files as stated earlier but if you want to decode it yourself you have alot of reading to do.

I recommend reading this http://www.libpng.org/pub/png/book/chapter13.html

Kingdom of Fish
Hi, I have been told only to use standard C libraries and zlib is the only one that is being additionally provided. Can you please give me some pointers ?
darkie15
The link I gave is a detailed description on how libpng decompresses png. My guess is that zlib should be used on the raw data after you taken out the headers and such.
Kingdom of Fish
+1  A: 

If this is a homework assignment and you really are only restricted to the standard C library, you to be looking at the official PNG file format specification: http://www.w3.org/TR/PNG/. However, are you sure you really need to be decoding the PNG file? If all you need to do is display it somehow, you're headed on the wrong path.

It will be rather complex and time consuming to write a decoder for any general PNG file, but not too bad for simple ones. In fact, because the PNG format allows for pieces of it to be compressed, to do it with only standard C libraries would require you to implement gzip decompress (a reasonable homework assignment for a mid-level undergrad course, but my guess is that you would have spent a lot of discussing compression algoirthms before this was assigned to you)

However, it isn't terribly difficult if you restrict yourself to non-compressed, non-interlaced PNG files. I wrote a decoder once in Python that handled only the easy cases in a couple of hours, so I'm sure it'll be doable in C.

smehmood
Hi smehmood, Thank you for the information. But I need to read a PNG file using standard C functions, decompress the file and then print the information of the file in human readable format. Information to be printed includes: structure of the PNG file, signature of PNG file, type and length of each chunk.
darkie15
What you've described is very doable. You just need to read the PNG file format specification I provided, and it will tell you how the file is laid out, and where all the information is stored.
smehmood
A: 

I've code once a basic Java library for reading/writing PNG files: http://code.google.com/p/pngj/

It does not support palleted images, but apart from that is fairly complete, simple and the code has no external dependencies (i.e. it only uses the standard JSE API, which includes zip decompression). And the code is available. I guess you could port it to C with not much effort.

leonbloy