Sign in to follow this  
Lode

PNG decoder updated

Recommended Posts

I updated my png decoder, it changed quite a lot since 2005. LodePNG is a PNG image decoder that doesn't use libPNG or any other libraries (no external dependencies), and is intented for games to load 32-bit RGBA textures stored as PNG files. This is better than using BMP files, because the PNG files are compressed (lossless), and support an alpha channel for your textures. The code is now more optimized, and in the documentation (which is in the header file), a fully working example is included. LodePNG is BSD licensed and easy to use. The files (lodepng.cpp and lodepng.h) can be found here: http://student.kuleuven.be/~m0216922/pngloader/ If you think you can use it, please try it out! Let me know if you find any bugs, possible abuses, or ways to improve the code. Thanks! (maybe I'll also make an encoder with similar style in the future) [Edited by - Lode on June 7, 2006 8:37:32 PM]

Share this post


Link to post
Share on other sites
My compliments again as I already mailed you, it's nice to have some focused and unbloated png loading code, haven't found anything like this. It works well for me.

Have you made any changes except for api and documentation since the last release (which was in april I think)?

Share this post


Link to post
Share on other sites
Quote:
Original post by DeadXorAliveHave you made any changes except for api and documentation since the last release (which was in april I think)?


A few small details in the code have been improved, but nothing important.

Share this post


Link to post
Share on other sites
I hope it's ok to bump this thread, but I updated it quite a lot more since the last time, and I added an example to it that can show png images in an SDL window with a console command.

Also, I made a comparision about the easyness-of-use between libpng and LodePNG, which I thought was somewhat funny:

HOW TO READ A PNG FILE WITH LIBPNG



// variables
unsigned char buf[4];
png_structp png_ptr;
png_infop info_ptr;
png_uint_32 width, height, scanline;
int bit_depth, color_type, interlace_type, row;
// open the file
fp = fopen( filename, "rb" );
// read signature
if ( fread( buf, 1, 4, fp ) != 4 ) printf("couldn't read");
// check if valid png
if (png_sig_cmp( buf, (png_size_t)0, 4 ) ) printf("not a png?");
// create the png reading structure, errors go to stderr
png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
if (png_ptr == NULL)
{
fclose(fp);
return;
}
// allocate info struct
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
fclose(fp);
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
return;
}
// set error handling
if (setjmp(png_ptr->jmpbuf))
{
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(fp);
return;
}
// set input method
png_init_io(png_ptr, fp);
// tell libpng we have already read some bytes
png_set_sig_bytes(png_ptr, 4 );
// read all info
png_read_info(png_ptr, info_ptr);
// get some characteristics of the file
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
&interlace_type, NULL, NULL);
// get size of scanline
scanline = png_get_rowbytes( png_ptr, info_ptr );
// allocate texture memory
location = new (unsigned char)[scanline * height];
// read the image line by line into the user's buffer
for (row = 0; row < height; row++)
{
png_read_row( png_ptr, (unsigned char *)(location+(row*scanline)), NULL );
}
// finish reading the file
png_read_end(png_ptr, info_ptr);
// get palette, if any
if (info_ptr->palette)
{
palette = new (unsigned char)[768];
memcpy( palette, info_ptr->palette, 768 );
}
// free png structures
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
// close, exit
fclose(fp);


HOW TO DO THE SAME WITH LODEPNG:



//declare variables
std::vector<unsigned char> buffer; //this will contain the file
std::vector<unsigned char> image; //this will contain the pixel data
std::vector<unsigned long> info; //this will contain the information

//all variables are ready, now load the file and decode the PNG
LodePNG::loadFile(filename, buffer); //use filename of existing PNG file
LodePNG::decodePNG32(buffer, image, info); //decode the PNG, it'll be in image, converted to raw RGBA format

//check error to see if everything went ok. If not, display the error value.
if(LodePNG::hasError()) cout << "\nerror: " << LodePNG::getError() << "\n";



Actually, I'd like to ask why the way to load PNG files with libpng is so complex? Is this way the best functionality - ease of use tradeoff?

Share this post


Link to post
Share on other sites
Sign in to follow this