How to load JPEG file for OpenGL


I want to load .jpg/.jpeg files for OpenGL application.How can I load?

If you know any advice, I’m very glad to hear your advice.


Qt , Image Magick and libJPEG come to mind. I’m sure there are plenty of other solutions, including QuickTime if you don’t want Linux compatibility (which I assume you do, given the board )

[This message has been edited by OneSadCookie (edited 07-12-2003).]

Try the Developer’s Image Library at The even have some nice opengl utility functions like loading a texture from a jpg in a single call.

Happy coding

I’m very glad to hear your advice!

I intend to reserch libJPEG and DevIL.

And I will be happy coder :wink:
Thank you very much.

I would suggest using libjpeg. Its standard on most linux distro’s so when you put it out you wont have another dep and it works really well with jpegs. Ill post my code when I get home from work. Try googling it theres lots of examples of how to do it with OGL.

jpeglib is the way to go.

Here is my code

#include <jpeglib.h>
#include <jerror.h>
//if the jpeglib stuff isnt after I think stdlib then it wont work just put it at the end
unsigned long x;
unsigned long y;
unsigned short int bpp; //bits per pixels unsigned short int
BYTE* data; //the data of the image
UINT ID; //the id ogl gives it
unsigned long size; //length of the file
int channels; //the channels of the image 3 = RGA 4 = RGBA
GLuint type;

bool LoadJPEG(char* FileName, bool Fast = true)
FILE* file = fopen(FileName, “rb”); //open the file
struct jpeg_decompress_struct info; //the jpeg decompress info
struct jpeg_error_mgr err; //the error handler

info.err = jpeg_std_error(&err); //tell the jpeg decompression handler to send the errors to err
jpeg_create_decompress(&info); //sets info to all the default stuff

//if the jpeg file didnt load exit
fprintf(stderr, “Error reading JPEG file %s!!!”, FileName);
return false;

jpeg_stdio_src(&info, file); //tell the jpeg lib the file we’er reading

jpeg_read_header(&info, TRUE); //tell it to start reading it

//if it wants to be read fast or not
info.do_fancy_upsampling = FALSE;

jpeg_start_decompress(&info); //decompress the file

//set the x and y
x = info.output_width;
y = info.output_height;
channels = info.num_components;

type = GL_RGB;

if(channels == 4)
type = GL_RGBA;

bpp = channels * 8;

size = x * y * 3;

//read turn the uncompressed data into something ogl can read
data = new BYTE[size]; //setup data for the data its going to be handling

BYTE* p1 = data;
BYTE** p2 = &p1;
int numlines = 0;

while(info.output_scanline < info.output_height)
numlines = jpeg_read_scanlines(&info, p2, 1);
*p2 += numlines * 3 * info.output_width;

jpeg_finish_decompress(&info); //finish decompressing this file

fclose(file); //close the file

return true;

SDL is the easiest way to load up a jpeg, using SDL_image, because you can just say
SDL_Surface *img;
there is an sdl/linux tutorial on if you like sdl get programming linux games, but not linux game programming. Otherwise learn qt

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.