Code I wrote using the JPEG library manual (info found on MSDN I believe). I also strongly believe something similar is on nehe.gamedev.net (OpenGL graphics tutorials). This loads the pertinent data utilizing the jpeg library (jpeg.h + jpeg.lib in my project) and returns a texture.
Code:
UINT TextureManager::LoadJPG(char * FileName)
{
FILE *pFile = fopen(FileName, "rb");
if(!pFile) //texture doesn't exist
{
// trace << "cannot find texture: " << FileName << "\n";
return NULL;
}
jpeg_decompress_struct jpg;
jpeg_error_mgr jerr;
jpg.err = jpeg_std_error(&jerr); //for some dumb reason we really need this
int channels, width, height;
jpeg_create_decompress(&jpg);
jpeg_stdio_src(&jpg, pFile);
jpeg_read_header(&jpg, 1);
jpeg_start_decompress(&jpg);
channels = jpg.num_components;
width = jpg.image_width;
height = jpg.image_height;
int rowsize = jpg.image_width * jpg.num_components;
byte *data = new byte [rowsize * height];
byte** t = new byte*[height];
for (int i = 0; i < height; i++)
t[height-i-1] = &(data[i * rowsize]); //reads from 0 to height-1
//want to work backwards so he start from top and works down
i = 0;
while (jpg.output_scanline < jpg.output_height) //jpeg library modifies these behind the scenes
{
i += jpeg_read_scanlines(&jpg,&t[i], jpg.output_height - i);
}
delete[] t;
jpeg_finish_decompress(&jpg);
jpeg_destroy_decompress(&jpg);
GLenum type;
if(channels == 3)
{
type = GL_RGB;
}
if(channels == 4)
{
type = GL_RGBA;
}
return GenerateInternalTexture(data, channels, width, height, type);
}
I hope that may help some.