How to read PNG images without a library?

Started by
26 comments, last by FLeBlanc 11 years, 7 months ago
I don't want to rely on a library to load PNG images, and I want to know how I can write the code myself. I found http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html, but I'm finding it hard to understand how to go about writing the code to load the pixel data from a PNG file. Any help would be greatly appreciated, thanks.
Advertisement
but I'm finding it hard to understand how to go about writing the code to load the pixel data from a PNG file.[/quote]

It's complicated, but since it doesn't change, other people did the hard parts and they provide them in a form of library.

Any help would be greatly appreciated, thanks.[/quote]

Use a library.

Alternatively, use any post-C, post-C++ language, those come with image loading functionality built-in.


Parsing PNG is not an important or challenging problem. It's not something worth solving today.

but I'm finding it hard to understand how to go about writing the code to load the pixel data from a PNG file.


It's complicated, but since it doesn't change, other people did the hard parts and they provide them in a form of library.

Any help would be greatly appreciated, thanks.[/quote]

Use a library.

Alternatively, use any post-C, post-C++ language, those come with image loading functionality built-in.


Parsing PNG is not an important or challenging problem. It's not something worth solving today.
[/quote]

The reason I don't want to use a library is because I want to know how to do it myself. I'm sure I could probably write some code using the resource I provided, but I can't be certain it would work. I don't want to go through the trouble of writing code only to find that I did something wrong. I was hoping maybe someone had some experience in this.
You could look at stb_image for sample code - I'm not sure how complete it's loader is (I just use a library) but it may help you get an idea of what's involved.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


You could look at stb_image for sample code - I'm not sure how complete it's loader is (I just use a library) but it may help you get an idea of what's involved.


Thank you. I'll definitely try to look through it when I have the time. From the looks of it, there's a lot going on in there, so for now I'll just use a library for my project just to simplify things.
I doubt you're going to find a whole pile of experienced coders who are going to come in here and say "I did this, and here is how you can do it too." Why? Because experienced coders understand the principle that Antheus was trying to get across: don't waste time reinventing the wheel. What is your purpose in this exercise? Is it to accomplish the task of loading images? If so, use a library. It's code that is bug-free, tested and stable. Or is your purpose to become a better programmer? In that case, there are better ways of doing it than solving a problem that is already robustly solved. Write a game instead. The experience you get writing a game will help you better understand how to load a PNG file, only the cool thing is that you will also come to realize the needlessness of writing yet another PNG loader.

Additionally, if you don't want to go through all the trouble of writing code only to find that you did something wrong, maybe it's time to find another hobby. That is, right there, the very essence of what it means to learn to program and become a better programmer. You make mistakes, you learn from them, you try something else. If you want to skip the hard parts or have someone else do the hard work for you, you are never, ever going to be an experienced programmer.
I apologize for seeming like I'm trying to have someone do my work for me, I really am. I'm just kinda looking for someone to point me in the right direction since Google doesn't yield very many results. I am currently making a game, and just for fun, I wanted to try to separate myself from libraries as much as possible. I would be better at doing this myself, but I have a learning disability (ADHD), so it's extremely hard for me to concentrate enough to figure out what I'm doing.

I know I seem like an inexperienced coder, but that's not the case. I'm just trying to learn something I've never done that seems like it should be simple enough. Regardless, it shall be a project for another day, I should be able to use a library to do what I need to do (hopefully).
Short version: Bite the bullet, and use libraries.

Long(er) version: Are you writing a game, or writing an engine? If your answer is the former, why re-create the wheel by doing what has already been done? If your answer is the latter, well... carry on.

someone to point me in the right direction since Google doesn't yield very many results.


W3C has formal PNG specification.

but I have a learning disability (ADHD), so it's extremely hard for me to concentrate enough to figure out what I'm doing.[/quote]

If that is accurate, then implementing any specification or standard is beyond your capabilities. Such work is tedious, boring and incredibly mundane. You go over spec, line after line, transcribing it into language of your choice. After each step, write a test case, perhaps with example provided by spec. And so on and on. After some 4 weeks of work, you can start testing the spec against reference examples. Then, once deployed in real world, the bugs start showing up. Each of those may or may not be reproducible, may be hardware specific, may be transient. So after many years, the library starts approaching what may be called "complete".

Rendering PNGs is trivial in browsers and most languages today because they all use libpng, which has gone through above process during the last 10-15 years.

However, even if implementing standard, notice how much it relies on other documents. Functionality for those is provided by existing libraries as well.

Short version: Bite the bullet, and use libraries.

Long(er) version: Are you writing a game, or writing an engine? If your answer is the former, why re-create the wheel by doing what has already been done? If your answer is the latter, well... carry on.

I am writing an engine. It's not so hard, most of it is pretty easy for me since I've done it already, but there are things such as loading PNG images that aren't so easy. I'm thinking of just using a library to create a tool to convert images to my own custom format so they will be easier to load. The thing about the game I'm going to be making is that it needs to be very customizable.

[quote name='Nyxenon' timestamp='1333386412' post='4927553']
someone to point me in the right direction since Google doesn't yield very many results.


W3C has formal PNG specification.

but I have a learning disability (ADHD), so it's extremely hard for me to concentrate enough to figure out what I'm doing.[/quote]

If that is accurate, then implementing any specification or standard is beyond your capabilities. Such work is tedious, boring and incredibly mundane. You go over spec, line after line, transcribing it into language of your choice. After each step, write a test case, perhaps with example provided by spec. And so on and on. After some 4 weeks of work, you can start testing the spec against reference examples. Then, once deployed in real world, the bugs start showing up. Each of those may or may not be reproducible, may be hardware specific, may be transient. So after many years, the library starts approaching what may be called "complete".

Rendering PNGs is trivial in browsers and most languages today because they all use libpng, which has gone through above process during the last 10-15 years.

However, even if implementing standard, notice how much it relies on other documents. Functionality for those is provided by existing libraries as well.
[/quote]

It bewilders me that something that seems as simple as a PNG image would be so complex. Are there any other file formats that are more simple than PNG besides the raw format?

This topic is closed to new replies.

Advertisement