Yesterday, for a small project of mine, I needed to implement a small utility to deform jpeg images from command line. The idea was to use texture mapping for deformations, so I needed a way to manipulate the jpeg images at the pixel level.

The first thing I tried was the imagemagik utility. I’ve browsed their documentation but I couldn’t find anything close to what I needed. I wanted to be able to draw textured mapped triangles into a picture. I knew that the Allegro library that I’ve used back in the old DOS programming days could do the texturing part. The bad thing that I found out is that it does not know how to handle jpeg files 🙁

Then I googled the Internet for a free and easy to use graphic library and I found libGD. The good thing is that it can handle jpeg files. The bad thing is that it cannot do the texturing part. The next thing I had in mind was to use both libraries but this would mean to add lots of dependencies to my small application for a simple texture mapping function. So I decided to write my own texture mapping function to remember the good old days. I will not describe how texture mapping works as there are plenty of tutorials on the internet for this.

Here is how you can use the gd library to process jpeg images:

int main (int argc, char *argv[])
gdImagePtr image;
FILE *in,*out;/*open the existing jpeg file */

in = fopen (argv[1], “rb”);
image = gdImageCreateFromJpeg (in);
fclose (in);

/* do the actual image processing
image->sx and image->sy gives the image resolution
image->tpixels[y][x] to access the truecolor pixels directly (int RGB)

/*saving the new jpeg file */
out = fopen (argv[2], “wb”);
gdImageJpeg(image,out,95/*compression level*/);

For more information see the libGD Documentation.
Have fun!

1 Comment

  • omg :O vali from the ex m48 bik network?
    nice stuff you got here.
    cheers dude.