In linux, OpenGL reads JPG, PNG, TAG texture data, and opengltag

Source: Internet
Author: User

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;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.