Jun 27, 2009

Using FreeImage in Ubuntu

In my last post I mentioned I wanted to output a visualization of the time it takes to render various parts of my fractal generator. To do this I used FreeImage, so here is a little tutorial on how to use the library in Ubuntu.

To install, just install the libfreeimage-dev package, either through Synaptic or using:
sudo apt-get install libfreeimage-dev
Once you've got that installed, you can link to it from your C/C++ programs.

Here's some simple code to output a blue background to a bitmap:
#include <FreeImage.h>
#include <stdlib.h>

int main(){
FreeImage_Initialise();
atexit(FreeImage_DeInitialise);

// create the bitmap object
FIBITMAP * bitmap = FreeImage_Allocate(200, 200, 32); // allocate a 200x200 pixel image, with 32-bit colour

// create the blue colour
RGBQUAD blue;
blue.rgbBlue = 255;

for (int i = 0; i < 200; i++){
for (int j = 0; j < 200; j++){
// draw a blue pixel at (i, j)
FreeImage_SetPixelColor(bitmap, i, j, &blue);
}
}

// save it as output.bmp
FreeImage_Save(FIF_BMP, bitmap, "output.bmp");

// deallocate memory
FreeImage_Unload(bitmap);
}
Then to compile:
g++ image.cpp -o image -lfreeimage

1 comment:

Anonymous said...

Thank you soo much for this tutorial.
It just worked as a charm!