In linux, OpenGL reads JPG, PNG, TAG texture data, and opengltag
The code for actually reading images has been uploaded to my resources. The following is an example (the code is not rigorous and can be taken together ):
Unsigned char * esLoadJPG (const char * fileName, int * width, int * height, int * size)
{
FILE * f = fopen (fileName, "rb ");
Fseek (f, 0, SEEK_END );
* Size = ftell (f );
Fseek (f, 0, SEEK_SET );
Unsigned char * data = (unsigned char *) malloc (* size );
Fread (data, 1, * size, f );
Includecoder dec (data, * size );
Dec. init ();
Dec. decodeJpeg ();
* Width = dec. getW ();
* Height = dec. getH ();
* Size = dec. getSize ();
Unsigned char * buffer = (unsigned char *) malloc (* size );
Memcpy (buffer, dec. getbmp data (), * size );
Return buffer;
}
Unsigned char * esLoadTGA (const char * fileName, int * width, int * height, int * size)
{
Unsigned char * buffer = NULL;
FILE * f;
Unsigned char tgaheader [12];
Unsigned char attributes [6];
Unsigned int imagesize;
F = fopen (fileName, "rb ");
If (f = NULL) return NULL;
If (fread (& tgaheader, sizeof (tgaheader), 1, f) = 0)
{
Fclose (f );
Return NULL;
}
If (fread (attributes, sizeof (attributes), 1, f) = 0)
{
Fclose (f );
Return 0;
}
* Width = attributes [1] * 256 + attributes [0];
* Height = attributes [3] * 256 + attributes [2];
Imagesize = attributes [4]/8 ** width ** height;
* Size = imagesize;
Buffer = (unsigned char *) malloc (imagesize );
If (buffer = NULL)
{
Fclose (f );
Return 0;
}
If (fread (buffer, 1, imagesize, f )! = Imagesize)
{
Free (buffer );
Return NULL;
}
Fclose (f );
Return buffer;
}
Unsigned char * esLoadPNG (const char * fileName, int * width, int * height, int * size)
{
FILE * f = fopen (fileName, "rb ");
Fseek (f, 0, SEEK_END );
* Size = ftell (f );
Fseek (f, 0, SEEK_SET );
Unsigned char * data = (unsigned char *) malloc (* size );
Fread (data, 1, * size, f );
PngDecoder dec (data, * size );
Dec. init ();
Dec. decoderPng ();
* Width = dec. getW ();
* Height = dec. getH ();
* Size = dec. getSize ();
Unsigned char * buffer = (unsigned char *) malloc (* size );
Memcpy (buffer, dec. getbmp data (), * size );
Return buffer;
}