现在的位置: 首页 > 操作系统 > 正文

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

2019年11月12日 操作系统 ⁄ 共 1806字 ⁄ 字号 评论关闭

实际读取图片的代码已经上传到我的资源里面; 下面贴出使用例子(代码不严谨,凑合看):

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

抱歉!评论已关闭.