Sign in to follow this  
derek_of_bodom@hotmail.com

How to read PNG images without a library?

Recommended Posts

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 [url="http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html"]http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html[/url], 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.

Share this post


Link to post
Share on other sites
[quote name='Antheus' timestamp='1333384759' post='4927542']
[quote]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.

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

[url="http://stackoverflow.com/questions/4237405/lightweight-library-to-read-in-png-images-c"]Use a library[/url].

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.

Share this post


Link to post
Share on other sites
mhagain    13430
You could look at [url="http://nothings.org/stb_image.c"]stb_image[/url] 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.

Share this post


Link to post
Share on other sites
[quote name='mhagain' timestamp='1333385041' post='4927546']
You could look at [url="http://nothings.org/stb_image.c"]stb_image[/url] 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.
[/quote]

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.

Share this post


Link to post
Share on other sites
FLeBlanc    3141
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Antheus    2409
[quote name='Nyxenon' timestamp='1333386412' post='4927553']
someone to point me in the right direction since Google doesn't yield very many results.[/quote]

[url="http://www.w3.org/TR/PNG/"]W3C has formal PNG specification.[/url]

[quote]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.

Share this post


Link to post
Share on other sites
[quote name='stormwarestudios' timestamp='1333386880' post='4927557']
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.
[/quote]
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='Antheus' timestamp='1333386999' post='4927561']
[quote name='Nyxenon' timestamp='1333386412' post='4927553']
someone to point me in the right direction since Google doesn't yield very many results.[/quote]

[url="http://www.w3.org/TR/PNG/"]W3C has formal PNG specification.[/url]

[quote]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?

Share this post


Link to post
Share on other sites
Telastyn    3777
[quote name='Nyxenon' timestamp='1333384869' post='4927544']
The reason I don't want to use a library is because I want to know how to do it myself.
[/quote]

You find the specification, you spend a lot of time and effort to make something that doesn't work. Then you fix it until it works.

[quote]
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.
[/quote]

Tough nuts, that's how you learn.

[quote]
I was hoping maybe someone had some experience in this.
[/quote]

We do. We went through the painful process so that we have the experience to know that 'use a library' is fantastic advice. If you think that experience is valuable, then go through that painful process yourself.

Share this post


Link to post
Share on other sites
[quote name='Telastyn' timestamp='1333387767' post='4927564']
[quote name='Nyxenon' timestamp='1333384869' post='4927544']
The reason I don't want to use a library is because I want to know how to do it myself.
[/quote]

You find the specification, you spend a lot of time and effort to make something that doesn't work. Then you fix it until it works.

[quote]
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.
[/quote]

Tough nuts, that's how you learn.

[quote]
I was hoping maybe someone had some experience in this.
[/quote]

We do. We went through the painful process so that we have the experience to know that 'use a library' is fantastic advice. If you think that experience is valuable, then go through that painful process yourself.
[/quote]

Definitely tough words to bite, but it's probably what I want to hear.
I started writing some code to [i]try[/i] to read a PNG image, and when reading the signature, the first four bytes were correct, but the following four bytes were "10 204 204 204". Nothing I read said that something like that was proper, and it doesn't make sense. According to one thing that I read, it said the first four bytes determine if it is a PNG, then the next 4 bytes are CR-LF (or something like that). Is it safe to just skip those four bytes, or are the important in some way?

Share this post


Link to post
Share on other sites
SimonForsman    7642
[quote name='Nyxenon' timestamp='1333387330' post='4927562']
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?
[/quote]

TGA is fairly straightforward, as is BMP.

Share this post


Link to post
Share on other sites
Telastyn    3777
[quote name='Nyxenon' timestamp='1333388050' post='4927566']
Nothing I read said that something like that was proper, and it doesn't make sense.
[/quote]

Yeah. Did I forget to mention that other programmers made mistakes when [i]they [/i]implemented the specification and that you'll encounter situations like that?

Share this post


Link to post
Share on other sites
Antheus    2409
[quote name='SimonForsman' timestamp='1333389159' post='4927574']

TGA is fairly straightforward, as is BMP.
[/quote]

It's only [url="http://www.dca.fee.unicamp.br/~martino/disciplinas/ea978/tgaffs.pdf"]30 pages[/url].

Problem is, since TGA used to be percevied as "simple" and since standards weren't respected or enforced, it's a bit "vague". Just like most specs, simple non-conformant and non-validated implementations add to the clutter.

Windows Photo Viewer, for example, cannot properly render .tga files, causing me considerable pain when dealing with certain type of government forms provided in tga.

BMP isn't simple either - due to different versions, same problems as above, as well as multiple raster modes, various incompatible yet BMP-labeled formats full support isn't trivial. For most part, Windows BMP is defined by whatever WinAPI loads.


Standards are written and defined by people, so complexity is people-related issues, not technical aspects.

Share this post


Link to post
Share on other sites
SimonForsman    7642
[quote name='Antheus' timestamp='1333389905' post='4927577'] [quote name='SimonForsman' timestamp='1333389159' post='4927574'] TGA is fairly straightforward, as is BMP. [/quote] It's only [url="http://www.dca.fee.unicamp.br/~martino/disciplinas/ea978/tgaffs.pdf"]30 pages[/url]. Problem is, since TGA used to be percevied as "simple" and since standards weren't respected or enforced, it's a bit "vague". Just like most specs, simple non-conformant and non-validated implementations add to the clutter. Windows Photo Viewer, for example, cannot properly render .tga files, causing me considerable pain when dealing with certain type of government forms provided in tga. BMP isn't simple either - due to different versions, same problems as above, as well as multiple raster modes, various incompatible yet BMP-labeled formats full support isn't trivial. For most part, Windows BMP is defined by whatever WinAPI loads. Standards are written and defined by people, so complexity is people-related issues, not technical aspects. [/quote]

That is true, i don't think the OP really needs 100% standards compliance though, being able to load the most common variations (the ones your tools produce really) on the format is usually enough for games.

Share this post


Link to post
Share on other sites
yckx    1298
[quote name='Antheus' timestamp='1333386999' post='4927561']
Quote
but I have a learning disability (ADHD), so it's extremely hard for me to concentrate enough to figure out what I'm doing.

If that is accurate, then implementing any specification or standard is beyond your capabilities..
[/quote]
Fie on that Realm of thought! ADHD is certainly a disadvantage, but it's by no means insurmountable. It merely increases the challenge. Sure, there is no quantifiable reason to write your own PNG reader, but the OP saw it as a challenge, and I presume expects he would derive satisfaction from the accomplishment, regardless of its impracticality. Lots of people with dyslexia still manage to read. Lots of people with dyscalculia manage to do math. And lots of people with ADHD manage to do all kinds of boring tasks.

As to the OP's question, I've not written a PNG reader, and I doubt many people on this forum have, due to existing libraries, so you're unlikely to get general advice on tricky spots. But if you have specific trouble you can't manage to get past after a reasonable effort, post your relevant code and we'll do our best to help you find and fix the bug.

Share this post


Link to post
Share on other sites
alnite    3438
You do realize that PNG comes in many different flavors depending on bit depth, transparency, and such? Have you ever ran across an API or library that claims it can read PNG but realized that it can only read [i]some[/i] PNGs? If you really want to learn the structure of PNGs, have several sample of PNG images (8bit, 24bit, 32bit), with and without transparency, open up a PNG documentation, and try parse those images mentally using a hex editor.

Maybe you are doing it for yourself, but I can almost guarantee that at the end of it, you'd feel that you'd just wasted your time as the other library did it better than your version.

Share this post


Link to post
Share on other sites
Krohm    5031
[quote name='Nyxenon' timestamp='1333387330' post='4927562']
I am writing an engine. It's not so hard, most of it is pretty easy for me[/quote]You just made my day man. I still don't understand why cannot you just focus on another problem.

[quote name='Antheus' timestamp='1333389905' post='4927577']
Problem is, since TGA used to be percevied as "simple" and since standards weren't respected or enforced, it's a bit "vague". Just like most specs, simple non-conformant and non-validated implementations add to the clutter.[/quote]I completely agree with that. I strongly suggest against .tga nowadays, it's just outdated. RLE compression is pretty much a joke. They have been superceded by png as far as I understand.

Share this post


Link to post
Share on other sites
[quote name='Krohm' timestamp='1333435972' post='4927806']
[quote name='Nyxenon' timestamp='1333387330' post='4927562']
I am writing an engine. It's not so hard, most of it is pretty easy for me[/quote]I still don't understand why cannot you just focus on another problem.
[/quote]

Well, it's mostly because I want to get to work on the renderer, and I want to use a texture for testing. I COULD just save a texture to the raw format, and load it that way, but eventually I'd need to load PNGs. I've decided to just use XNA to write a test renderer to see how well it performs so I can optimize it, then I'll write it in C++.

Share this post


Link to post
Share on other sites
JTippetts    12970
Wait, you're going to write a test renderer in XNA, optimize it, then write it in C++? For the love of all that's holy, why? Why bother with the "write it in C++" step at all, and undo all that effort you just spent to optimize it for XNA?

It seems to me like you ought to sit down and think about exactly what it is you want to accomplish. You seem to be heading down a path that's going to waste some time. First of all, given what I've read in this thread so far, you simply do not know enough about the language(s) to optimize anything. Optimization is often a trap in and of itself, since beginners rarely write code that challenges modern hardware, and if they do it's more along the lines of "choose a more optimal algorithm" than it is "optimize this algorithm". Second of all, if you do want to write a renderer, then the place to start is not with PNG loading code. This is a bad place to start. As many others have indicated, loading PNGs is a solved problem. The work is done for you. That is many, many lines of code that [i]you do not have to write[/i]. Why in the name of Pete would you waste your time writing PNG loading code, rather than writing the renderer that you want to write?

Allow me to tell you a story. Once upon a time, I needed to write some code to pack a bunch of sprite rectangles tightly into a texture. So I googled rectangle packing, and I stumbled upon some algorithms. I knew just enough about the languages at the time to be able to adapt the code to my needs without fully understanding what it was doing. Eventually, I got to the point where I did understand it, but not by studying that code. I got there by writing game code, learning more about the language as I went. I somehow knew that studying rectangle packing code until I understood it completely was not the optimal route to becoming a better programmer. I also understood that spending a whole bunch of time writing my own packing code, when there was packing code readily available, was not going to get me closer to finishing my game. It would just be a waste of time, and I waste enough time as it is.

Share this post


Link to post
Share on other sites
Satharis    2449
Really after everyone is essentially repeating the same mantra, I'm not sure why you're so interested in writing a PNG loader. Does it PROVE something to yourself? To anyone else? What are you trying to get out of it? Knowledge? Knowledge for what? How often are you going to need to write a custom PNG loader if your lifetime, even with a professional job?

As has been said-the virtue of being a good coder is knowing not to reinvent the wheel, you're almost insulting the work of the people that spent all their time creating these libraries and releasing them for us to use. Put them to use, carry their torch on to the next step in the race and make something [i]new[/i]. If you were wanting to write a PNG loader just to write a PNG loader, to devote all your time to making a wonderful PNG loader for others to use or something like that, I'm sure you'd have more support. As is, you seem to just be wanting to write one to prove you can, even though you [u]shouldn't.[/u]

Really I see this sort of thing come up with new coders all the time, and I tell them exactly what the truth is: Do you have to make EVERYTHING yourself? Did you build the stove you cook your food on in the morning? The bed you sleep on? The house you live in? Have you ever made anything and used a tool? A screwdriver or a hammer someone else made? In retrospect you're doing exactly that, you're assuming people will look down on you or something if you don't do the work of the world and make everything from scratch.

Keep in mind, the last paragraph doesn't necessarily apply to your level of skill or thinking or such like that, I don't know enough about you to comment on that. The point was that I have that come up so often I just have to point out what I respond with, if only to give a glimpse at the reasoning behind what everyone is telling you.

Share this post


Link to post
Share on other sites
mhagain    13430
[quote name='Krohm' timestamp='1333435972' post='4927806']
I strongly suggest against .tga nowadays, it's just outdated. RLE compression is pretty much a joke. They have been superceded by png as far as I understand.
[/quote]
TGA is fine for some use cases. So long as you stick to 32-bit uncompressed it's going to go directly into any reasonable texture creation function call without any CPU-side pre-processing required. Yes, the files are larger, but if you're in a postion where they're already on the HD then file size is really the least of your worries. PNG offers good compression without quality loss for sure, but they're slow to load and so can lead to a poorer end-user experience.

The formats I'd personally recommend are DDS or TGA. TGA during content creation or when DXT compression artefacts are too objectionable, DDS for everything else. Any other format is really not necessary and only creates extra unwanted work in your program.

Share this post


Link to post
Share on other sites
Krohm    5031
As far as I recall, uncompressed PNGs load just as fast as TGA.
I sort of agree however with your rationale and yes, I understand setting the compression bar on each step might be a small workflow hiccup.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this