• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
derek_of_bodom@hotmail.com

How to read PNG images without a library?

27 posts in this topic

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

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

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

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

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

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?
0

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?
0

Share this post


Link to post
Share on other sites
[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?
0

Share this post


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

Share this post


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

Share this post


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

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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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