Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1402112 Posts in 68038 Topics- by 61627 Members - Latest Member: Spytrever

August 15, 2022, 01:22:42 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)PNG texture loading in OpenGL
Pages: [1]
Print
Author Topic: PNG texture loading in OpenGL  (Read 24306 times)
mechacrash
Level 1
*


It's almost like I'm actually being productive!


View Profile
« on: December 03, 2009, 06:07:34 AM »

I am looking at implementing a PNG loader into my program but I don't know how to. I have searched google a lot and everything uses a png library. I really dont want to use one so I was wondering if someone could link me somewhere where I could get the code for loading pngs.

I realise it's not going to be easy or a short method to decompress but I don't want to use a library or any extra .dlls if possible.
Logged

brog
Level 7
**



View Profile WWW
« Reply #1 on: December 03, 2009, 06:37:20 AM »

read the PNG spec and code it yourself.
or, you know, just use a library.
Logged
Saint
Level 3
***



View Profile WWW
« Reply #2 on: December 03, 2009, 07:34:31 AM »

You could use stb_image; http://www.ohloh.net/p/7147 or http://nothings.org/

It's not a library, just a c file that you can jam directly into your project with the rest of your code. It's also public domain so you can use it however you want.

Or if you desperately want to write your own, download stb_image and read the code. Also, there is probably documentation on any other formats you might like over at Wotsit ( http://www.wotsit.org/ ), but this means you will have to write your own zlib unpacker as well.
« Last Edit: December 03, 2009, 07:37:40 AM by Saint » Logged
LemonScented
Level 7
**



View Profile
« Reply #3 on: December 03, 2009, 08:32:55 AM »

I use PicoPNG: http://members.gamedev.net/lode/projects/LodePNG/picopng.cpp although you might also consider the slightly less cryptic LodePNG by the same author: http://members.gamedev.net/lode/projects/LodePNG/

No extra libraries or DLLs to link to, just a couple of files you should be able to drop straight into a project and get working right away. Best not to spend too much time trying to work out HOW that code works, mind - just trust that it does!  Wink
Logged

mcc
Level 10
*****


glitch


View Profile WWW
« Reply #4 on: December 03, 2009, 09:16:18 AM »

I haven't used the PicoPNG version, but LodePNG is fantastic, can also create PNGs, is easy to use and easy to integrate. Like LemonScented says t's not exactly a library, LodePNG like PicoPNG is a single source code file + header so you can just add it to your project. You can use it like this:

Code:
LodePNG_Decoder decoder;
LodePNG_Decoder_init(&decoder);
unsigned char* buffer;
unsigned char* image;
size_t buffersize, imagesize;

LodePNG_loadFile(&buffer, &buffersize, filename); /*load the image file with given filename*/

if ( !buffer || buffersize <= 0 ){
printf("Couldn't open file: %s\n", filename);
return;
}

LodePNG_decode(&decoder, &image, &imagesize, buffer, buffersize); /*decode the png*/

int width = decoder.infoPng.width; height = decoder.infoPng.height;

init(width, height);
uint32_t *image2 = (uint32_t *)image;
for(int x = 0; x < decoder.infoPng.width; x++) {
for(int y = 0; y < decoder.infoPng.height; y++) {
uint32_t pixel = htonl(image2[y * decoder.infoPng.width + x]);
// DO WHATEVER HERE
}
}

free(buffer); free(image);
Logged

My projects:<br />Games: Jumpman Retro-futuristic platforming iJumpman iPhone version Drumcircle PC+smartphone music toy<br />More: RUN HELLO
Ivan
Owl Country
Level 10
*


alright, let's see what we can see


View Profile
« Reply #5 on: December 03, 2009, 09:33:17 AM »

Just use libpng itself, its like 10 lines of code to read in RBGA pixel data.

Here's some really old code of mine to read it, so you can use that as a start:
http://code.google.com/p/owlcountry/source/browse/trunk/pngLoad.c
Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
slembcke
Level 3
***



View Profile WWW
« Reply #6 on: December 03, 2009, 09:37:33 AM »

http://files.slembcke.net/misc/PNGTexture.tar.gz

Shows how to load a PNG as an RGBA texture using the official library (supports every format of PNG file), set up a GLUT window and load the texture. All in 150 lines of code. You can statically link against libPNG without any licensing issues, so I don't see why you wouldn't want to do that really...
Logged

Scott - Howling Moon Software Chipmunk Physics Library - A fast and lightweight 2D physics engine.
Sos
Level 8
***


I make bad games


View Profile WWW
« Reply #7 on: December 03, 2009, 11:51:53 AM »

i see you need some fresh SOIL for your opengl game

try here: http://www.lonesock.net/soil.html

simple opengl image loader, it's the simplest ever.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic