Skip navigation

This example just shows how to use DevIL to open, create, and save an image

Great tutorial here too

Pretty neat.

#undef _UNICODE

#include <stdio.h>
#include <stdlib.h>

#include <windows.h>


#include "il.h"
#include "ilu.h"  // for image creation and manipulation funcs.

#pragma comment( lib, "DevIL.lib" )
#pragma comment( lib, "ILU.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 ] );
  }


  // Other info.
  int bpp = ilGetInteger( IL_IMAGE_BITS_PER_PIXEL ) ;
  
  printf("bpp: %d   IL_IMAGE_FORMAT: %d\n",
    
    bpp,
    ilGetInteger( IL_IMAGE_FORMAT )
    
  ) ;


  ilClearColour( 0, 255, 0, 255 ) ;




  // generate image data as just array of bytes.

  // get width and height of your desktop
  int width  = GetSystemMetrics( SM_CXVIRTUALSCREEN ) ;
  int height = GetSystemMetrics( SM_CYVIRTUALSCREEN ) ;

  int bytesToUsePerPixel = 3 ;  // RGB
  // we coulda used 4, if we wanted a "transparency" component,
  // but we don't want this png to use trans.

  // duh, the size of a byte is obviously ONE BYTE,
  // but this is just a formality to PERHAPS
  // make the code a bit more clear.
  int sizeOfByte = sizeof( unsigned char ) ;

  // So this computation gets the size
  // that the image data array should be.
  // (width*height)   *   (#bytes to use per pixel) really.
  int theSize = width * height * sizeOfByte * bytesToUsePerPixel ;

  unsigned char * imData =(unsigned char*)malloc( theSize ) ;

  for( int i = 0 ; i < theSize ; i++ )
  {
    imData[ i ] = i % 255 ;
  }
  
  
  // Let's make an image now.
  // the next line "generates an image" within the
  // DevIL STATE MACHINE.

  // DevIL is cool because it has this concept of
  // your "currently selected image".

  // So, when you call all the DevIL library functions,
  // (such as ilTexImage to change an images properties),
  // notice how there's no passing of any big data struct,
  // pointer to an image, or anything like that.
  
  // That's because the ONLY reference YOU get
  // as the user of the OpenIL API IS this INT
  // value:
  ILuint imageID = ilGenImage() ;

  // there.  We just created a NEW image (which will start out
  // to being 1x1 pixels or something like that)
  // our REFERENCE to this 1x1 pixel image IS
  // the integer number imageID.

  // NOW, we "select-in" the image we just generated
  // as the one we're currently working on.
  ilBindImage( imageID ) ;

  // From HENCE FORTH (until we call ilBindImage() again)
  // WE ARE WORKING ON "imageID".  ANY calls to
  // ilTexImage, or ilGetInteger, or WHATEVER are in
  // reference to the LAST IMAGE WE LAST BOUND.

  // So the new image we just created is rather.. empty.
  printf("New image!  width=%d,  height=%d,  bpp=%d\n",

    ilGetInteger( IL_IMAGE_WIDTH ),
    ilGetInteger( IL_IMAGE_HEIGHT ),
    ilGetInteger( IL_IMAGE_BPP )
    );

  printf("About to tex up your image\n");
  ilTexImage(
    
    width,
    height,
    
    1,  // OpenIL supports 3d textures!  but we don't want it to be 3d.  so
    // we just set this to be 1
    
    3,  // 3 channels:  one for R , one for G, one for B
    
    IL_RGB,  // duh, yeah use rgb!  coulda been rgba if we wanted trans
    
    IL_UNSIGNED_BYTE,  // the type of data the imData array contains (next)
    
    imData  // and the array of bytes represneting the actual image data
    
  ) ;

  printf("Your image was texxed\n");

  printf("Now width=%d,  height=%d,  bpp=%d\n",

    ilGetInteger( IL_IMAGE_WIDTH ),
    ilGetInteger( IL_IMAGE_HEIGHT ),
    ilGetInteger( IL_IMAGE_BPP )
    );

  // allow openIL to overwrite the file
  // we created last time
  ilEnable(IL_FILE_OVERWRITE);
 
  // actually save out as png
  ilSave( IL_PNG, "output.png" ) ;

  // now try saving as jpg
  ilSave( IL_JPG, "output.jpg" ) ;
  
  // now save as bmp
  ilSave( IL_BMP, "output.bmp" ) ;


  // Look at the file sizes!  For this TYPE
  // of image, png is the best because its
  // flat colors repeated.

  // On my mahcin eht png is 94KB and the BMP is 11000kB!
  // the jpg comes out at 3000kb.

  // PNG RULES!!

  // lets give another example where jpg will
  // be better...

  // cahnge im data to being just pure noise
  for( int i = 0 ; i < theSize ; i++ )
  {
    imData[ i ] = rand()%255 ;
  }


  // now set the data in the image with this new data array
  ilSetData( imData ) ;

  // save again

  // actually save out as png
  ilSave( IL_PNG, "outputNoise.png" ) ;

  // now try saving as jpg
  ilSave( IL_JPG, "outputNoise.jpg" ) ;
  
  // now save as bmp
  ilSave( IL_BMP, "outputNoise.bmp" ) ;


  // Wowo!   So for this last test, PNG was
  // worse than jpg and bmp!  that's remarkable.
  // I actually didn't know that coudl happen.

  printf("and done");

}

As usual, download the code from esnips! (thanks esnips!)

Advertisements

One Comment


One Trackback/Pingback

  1. […] How to use OpenIL to generate and save an image | Bobobobo’s … – This example just shows how to use DevIL to open, create, and save an image Great tutorial here too. Pretty neat. #undef _UNICODE #include #include #include #include “il.h” #include “ilu.h” // for image creation and manipulation funcs. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: