PDA

View Full Version : How to load JPEG file for OpenGL



mdive
07-11-2003, 10:46 PM
Hello.

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.

Thanks.

OneSadCookie
07-12-2003, 02: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).]

azcoder
07-12-2003, 05: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.

Happy coding
;-)

mdive
07-12-2003, 07: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.

07-14-2003, 05:20 AM
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.

nukem
07-14-2003, 11:39 AM
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(&amp;err); //tell the jpeg decompression handler to send the errors to err
jpeg_create_decompress(&amp;info); //sets info to all the default stuff

//if the jpeg file didnt load exit
if(!file)
{
fprintf(stderr, "Error reading JPEG file %s!!!", FileName);
LoadBlackWhiteBorder();
return false;
}

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

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

//if it wants to be read fast or not
if(Fast)
{
info.do_fancy_upsampling = FALSE;
}

jpeg_start_decompress(&amp;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 = &amp;p1;
int numlines = 0;

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

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

fclose(file); //close the file

return true;
}

scriptlord
08-02-2003, 09:12 PM
SDL is the easiest way to load up a jpeg, using SDL_image, because you can just say
SDL_Surface *img;
img=IMG_Load("something.jpg");
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