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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑不溜秋的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值