Monday 2 March 2009

How to load the PNG image?

hmm, to load the PNG image, most people use libpng. But, wait.. But WAIT!! That library is “NOT SAFE FOR DIRECT USE”. The API is Awful, yeah.. It's Right. If you don’t believe me, spend a day with it, struggle through it, THEN check out OpenIL. This is an example of how DIFFICULT using libpng directly is.

#include 
#include

#undef _UNICODE
#include "il.h"

#pragma comment( lib, "DevIL.lib" )

// Wow. DevIL is amazing.

// From http://gpwiki.org/index.php/DevIL:Tutorials:Basics

// The library consists of three sub-libraries:
// * IL - main DevIL library. It allows you to load and save images to files. Every function in this library have 'il' prefixed to their name.
// * ILU - this library contains functions for altering images. Every function in this library have 'ilu' prefixed to their name.
// * ILUT - this library connects DevIL with OpenGL. Every function in this library have 'ilut' prefixed to their name.

int main()
{
ilInit();
printf("DevIL has been initialized\n");

// Loading an image
ILboolean result = ilLoadImage( "4px.png" ) ;

if( result == true )
{
printf("the image loaded successfully\n");
}
else
{
printf("The image failed to load\n" ) ;

ILenum err = ilGetError() ;
printf( "the error %d\n", err );
printf( "string is %s\n", ilGetString( err ) );
}

int size = ilGetInteger( IL_IMAGE_SIZE_OF_DATA ) ;
printf("Data size: %d\n", size );
ILubyte * bytes = ilGetData() ;

for( int i = 0 ; i < size; i++ )
{
// see we should see the byte data of the image now.
printf( "%d\n", bytes[ i ] );
}
}

0 comments: