Skip to main content
GameDev.net gamedev.net
🔒 Locked

JPEG reading

Started by baxtorz Mar 31, 2004 at 5:00 PM 3 replies 900+ views
Original Post
baxtorz
baxtorz
hi I am a new programmer.....anyway. I want to read a .jpeg file into memory. How would I go about doing this. Below is how its done with a bitmap image. AUX_RGBImageRec *pBitmap = NULL; pBitmap = auxDIBImageLoad(strFileName); is there a way in opengl to do this. thanks
Ostsol
Ostsol
Nope. Bitmaps are the only format supported by GLAUX -- though people have been moving away from that library for quite a while, now. One library that I''ve heard good things about is OpenIL:

http://openil.sourceforge.net/

I''ve never used it myself, though. . .
-Ostsol
_the_phantom_
_the_phantom_
OpenIL is now know as DevIL (after the maker was told to change the name), the address is still right however

I''ve used it, its good and certainly get the job done
Promit
Promit
Now, if you want to use JPEGs you are going to use libjpeg. You don''t want to write jpeg loading yourself. Trust me.

Once you have libjpeg set up, code similar to this will load a jpeg for OpenGL:

struct jpeg_decompress_struct Info;
FILE* pFile;
unsigned char* RawData;

pFile = fopen( FileName, "rb" );
if( pFile == NULL )
{
Debug( "Failed to load texture %s\n", FileName );
return false;
}

jpeg_error_mgr jerr;

Info.err = jpeg_std_error( &jerr );

jpeg_create_decompress( &Info );
jpeg_stdio_src( &Info, pFile );
jpeg_read_header( &Info, TRUE );

jpeg_start_decompress( &Info );

m_BitDepth = Info.num_components;
m_Width = Info.image_width;
m_Height = Info.image_height;

int RowSpan = m_Width * m_BitDepth;
RawData = new unsigned char[RowSpan * m_Height];

unsigned char** RowPtr = new unsigned char*[m_Height];

unsigned int i;
for( i = 0; i < m_Height; i++ )
{
RowPtr[i] = &(RawData[i * RowSpan]);
}

int RowsRead = 0;
while( Info.output_scanline < Info.output_height )
{
RowsRead += jpeg_read_scanlines( &Info, &RowPtr[RowsRead], Info.output_height - RowsRead );
}

delete[] RowPtr;

jpeg_finish_decompress( &Info );

jpeg_destroy_decompress( &Info );

fclose( pFile );

//Create the texture

glGenTextures( 1, &m_Index );
glBindTexture( GL_TEXTURE_2D, m_Index );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR );

//load the tex

if( UseMipMaps )
{
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR );
gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGB, m_Width, m_Height, GL_RGB, GL_UNSIGNED_BYTE, RawData );
}
else
{
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, m_Width, m_Height, 0,
GL_RGB, GL_UNSIGNED_BYTE, RawData );
}
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

delete[] RawData;
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
BriTeg
BriTeg
Search for "IPicture" on msdn. IPicture is part of Microsoft''s platform SDK, and handles loading .bmp, .wmf, .jpg, .ico and I think .gif, which can then be used for textures for OpenGL, DirectX, GDI, etc. There''s some sample code on NeHe''s site.
Brianmiserere nostri <font color=#BC

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.