码迷,mamicode.com
首页 > 系统相关 > 详细

linux 下 OpenGL 读取 JPG, PNG, TAG 纹理数据

时间:2014-12-30 11:41:37      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

实际读取图片的代码已经上传到我的资源里面; 下面贴出使用例子:

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


linux 下 OpenGL 读取 JPG, PNG, TAG 纹理数据

标签:

原文地址:http://blog.csdn.net/zangle260/article/details/42263963

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!