Linux under OpenGL read JPG, PNG, TAG texture data

Source: Internet
Author: User
Tags fread

The code that actually reads the picture has already been uploaded into my resources, and the following example is posted:

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);
Jpegdecoder 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.getbmpdata (), *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] * + attributes[0];
*height = attributes[3] * + 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.getbmpdata (), *size);
return buffer;
}


Linux under OpenGL read JPG, PNG, TAG texture data

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.