linux 下 OpenGL 读取 JPG, PNG, TAG 纹理数据
实际读取图片的代码已经上传到我的资源里面; 下面贴出使用例子:
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] * 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.getbmpData(), *size);
return buffer;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。