OpenGL JPEG texture loader using IJG libjpeg

Textures are inevitable in graphics programming. JPEG textures can save you lots of disk space but they're not easy to decode and use in your app. Luckily for us, there's a free library that does the nasty work, it's written by the Independent JPEG Group.
This library is not easy to use for beginners at programming who are learning OpenGL, so I've written a small example of it's usage.

Configure your workspace (Windows):

GLUT:
I am using MSFT Visual C++ 2008 Express Edition, you can get a free copy here.
Download GLUT for Win32 binaries from here, unzip it and you'll find glut.h, glut32.lib, glut32.dll. Copy

  • glut32.dll => "C:\Windows\"
  • glut32.lib => "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\lib\"
  • glut.h => "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\GL\" (create the folder GL)

Actual file path might vary depending on your installation.

IJG jpeg lib:
Download the Windows format package jpegsr8.zip (current version is 8 ATM) from IJG.org and unzip it.
Rename jconfig.vc to jconfig.h.

These are the files we will need to add to our project (I've attached them (jpeg-8_needed_files.zip) to the article so you wont need to copy each one):

jaricom.c jcapimin.c jcapistd.c jcarith.c jccoefct.c jccolor.c jcdctmgr.c jchuff.c jcinit.c jcmainct.c jcmarker.c jcmaster.c jcomapi.c jconfig.h jcparam.c jcprepct.c jcsample.c jctrans.c jdapimin.c jdapistd.c jdarith.c jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c jdct.h jddctmgr.c jdhuff.c jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c jdpostct.c jdsample.c jdtrans.c jerror.c jerror.h jfdctflt.c jfdctfst.c jfdctint.c jidctflt.c jidctfst.c jidctint.c jinclude.h jmemmgr.c jmemnobs.c jmemsys.h jmorecfg.h jpegint.h jpeglib.h jquant1.c jquant2.c jutils.c jversion.h

For more information about each file consult the filelist.txt text file.

Configure your workspace (Linux (deb based)):

GLUT:

apt-get install glut3 glut3-dev libglut3

IJG jpeg lib:
Download the Unix format package jpegsrc.v8.tar.gz (current version is 8 ATM) from IJG.org, extract the archive and do:

./configure
make
make install

This will install the library on your system. More in install.txt.

The code:

I've attached (jpegloader.zip) the complete source code of an application that displays a jpeg image on the screen. I will explain a little about it now.

IJG's lib jpeg comes with an example.c that shows how to use the library, I have modified it to make it load a jpeg image and pass it to OpenGL, the file name now is jpegloader.c
Usage:

    unsigned int uiv1Texture[1];
    glEnable(GL_TEXTURE_2D);
    //reading the jpeg file
    read_JPEG_file("texture/image1.jpg", uiv1Texture); 
    glBindTexture(GL_TEXTURE_2D, uiv1Texture[0]);
    //dont forget to glDeleteTextures(1, uiv1Texture);

A good article on OpenGL textures www.nullterminator.net/gltexture.html, it has a RAW file loader too.



Thanks for reading, if you like this article or find it useful please leave a comment, rate it and/or share it with your friends.

Trackback URL for this post:

http://www.efkhoury.com/trackback/7
AttachmentSize
jpeg-8_needed_files.zip297.96 KB
jpegloader.zip315.95 KB