View Full Version : How to load JPEG file for OpenGL
07-11-2003, 11:46 PM
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.
07-12-2003, 03:27 AM
Qt (http://www.trolltech.com/) , Image Magick (http://www.imagemagick.org/) and libJPEG (http://www.ijg.org/) come to mind. I'm sure there are plenty of other solutions, including QuickTime (http://developer.apple.com/quicktime) if you don't want Linux compatibility (which I assume you do, given the board http://www.opengl.org/discussion_boards/ubb/wink.gif)
[This message has been edited by OneSadCookie (edited 07-12-2003).]
07-12-2003, 06:03 AM
Try the Developer's Image Library at http://sourceforge.net/projects/openil/. The even have some nice opengl utility functions like loading a texture from a jpg in a single call.
07-12-2003, 08:12 PM
I'm very glad to hear your advice!
I intend to reserch libJPEG and DevIL.
And I will be happy coder ;-)
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.
07-14-2003, 12:39 PM
jpeglib is the way to go.
Here is my code
//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
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);
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
08-02-2003, 10:12 PM
SDL is the easiest way to load up a jpeg, using SDL_image, because you can just say
there is an sdl/linux tutorial on nehe.com if you like sdl get programming linux games, but not linux game programming. Otherwise learn qt
Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.