Jump to content

  • Log In with Google      Sign In   
  • Create Account


How to read PNG images without a library?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • This topic is locked This topic is locked
27 replies to this topic

#1 Nyxenon   Members   -  Reputation: 104

Like
0Likes
Like

Posted 02 April 2012 - 10:33 AM

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.

Sponsor:

#2 Antheus   Members   -  Reputation: 2393

Like
5Likes
Like

Posted 02 April 2012 - 10:39 AM

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.


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.

#3 Nyxenon   Members   -  Reputation: 104

Like
0Likes
Like

Posted 02 April 2012 - 10:41 AM

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.


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.


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.

#4 mhagain   Crossbones+   -  Reputation: 7413

Like
3Likes
Like

Posted 02 April 2012 - 10:44 AM

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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#5 Nyxenon   Members   -  Reputation: 104

Like
0Likes
Like

Posted 02 April 2012 - 10:50 AM

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.

#6 FLeBlanc   Crossbones+   -  Reputation: 3081

Like
3Likes
Like

Posted 02 April 2012 - 10:53 AM

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.

#7 Nyxenon   Members   -  Reputation: 104

Like
0Likes
Like

Posted 02 April 2012 - 11:06 AM

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).

#8 stormwarestudios   Members   -  Reputation: 215

Like
0Likes
Like

Posted 02 April 2012 - 11:14 AM

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.

#9 Antheus   Members   -  Reputation: 2393

Like
4Likes
Like

Posted 02 April 2012 - 11:16 AM

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.


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.

#10 Nyxenon   Members   -  Reputation: 104

Like
0Likes
Like

Posted 02 April 2012 - 11:22 AM

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.


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.


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.


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?

#11 Telastyn   Crossbones+   -  Reputation: 3718

Like
3Likes
Like

Posted 02 April 2012 - 11:29 AM

The reason I don't want to use a library is because I want to know how to do it myself.


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.

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.


Tough nuts, that's how you learn.

I was hoping maybe someone had some experience in this.


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.

#12 Nyxenon   Members   -  Reputation: 104

Like
0Likes
Like

Posted 02 April 2012 - 11:34 AM


The reason I don't want to use a library is because I want to know how to do it myself.


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.

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.


Tough nuts, that's how you learn.

I was hoping maybe someone had some experience in this.


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.


Definitely tough words to bite, but it's probably what I want to hear.
I started writing some code to try 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?

#13 SimonForsman   Crossbones+   -  Reputation: 5752

Like
3Likes
Like

Posted 02 April 2012 - 11:52 AM

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?


TGA is fairly straightforward, as is BMP.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#14 Telastyn   Crossbones+   -  Reputation: 3718

Like
0Likes
Like

Posted 02 April 2012 - 11:57 AM

Nothing I read said that something like that was proper, and it doesn't make sense.


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

#15 Antheus   Members   -  Reputation: 2393

Like
4Likes
Like

Posted 02 April 2012 - 12:05 PM

TGA is fairly straightforward, as is BMP.


It's only 30 pages.

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.

#16 SimonForsman   Crossbones+   -  Reputation: 5752

Like
0Likes
Like

Posted 02 April 2012 - 02:05 PM

TGA is fairly straightforward, as is BMP.

It's only 30 pages. 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.


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.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#17 yckx   Prime Members   -  Reputation: 1163

Like
4Likes
Like

Posted 02 April 2012 - 05:03 PM

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..

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.

#18 Antheus   Members   -  Reputation: 2393

Like
6Likes
Like

Posted 02 April 2012 - 05:14 PM

10 years ago, writing a library like this was a necessity. Existing libraries were hard to come by, there was no meaningful code sharing, reputable reference implementations were hard to come by or were part of reference packages, perhaps available for large fee to standardization committees. Heck, internet access was a problem.

So people in a pinch did the next best thing. They looked up wotsit.org, quickly parsed a few fields, tweaked until it worked. Maybe posted as a tutorial or sample code somewhere. Perhaps it got reused.

Result of the era was a ton of minimal readers/parsers that barely got the job done.

Then web exploded. Standard-compliant implementations became much more important, reuse became the norm, licenses were relaxed, patents expired. These libraries made their way into standard libraries, OSes and browsers. Suddenly getting vast exposure, their correctness was suddenly tested daily by 1 billion users. And patches made it back to source.


Regardless how one feels about cost vs. effort, these is actual engineering at its best. Without some really specific requirements in some adequately demanding project (corporate or hobby development is not it), the case for NIH is over.

These are solved problems. C and C++ remain as clumsy as ever in integrating third-party libraries. There are probably a lot of benign warnings that will crop up. But these libraries work, in real world on real data.

What do they buy you? Trillions of hours of QA. Billions of parsed images, from valid to invalid. Millions of users. Tens of thousands of hardware configurations. Hundreds of compilers.

Cost of replicating this effort is sufficient to cover entire economic debt of every country today. And it's available for free. Isn't it worth spending 3 hours downloading and integrating this one library?


Impressive, isn't it.

#19 alnite   Crossbones+   -  Reputation: 2038

Like
1Likes
Like

Posted 02 April 2012 - 05:51 PM

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 some 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.

#20 Krohm   Crossbones+   -  Reputation: 2960

Like
1Likes
Like

Posted 03 April 2012 - 12:52 AM

I am writing an engine. It's not so hard, most of it is pretty easy for me

You just made my day man. I still don't understand why cannot you just focus on another problem.

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.

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS