Menu Home

jpeg转换OpenGL Texture


jpeg转换OpenGL Texture

#include <stdio.h>
#include <unistd.h>
#include <jpeglib.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glut.h>

void LoadJpgTextureGL(char *file)
{
FILE *jpegFile;
int sizebuf;
unsigned char* buffer;
unsigned char* texturebuf;
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);

if((jpegfile = fopen(file,"rb"))==NULL)
{
    perror("fopen jpeg");
    return; 
}
jpeg_stdio_src(&cinfo,jpefile);
jpeg_read_header(&cinfo,TRUE);
jpeg_start_decompress(&cinfo);

sizebuf = cinfo.output_width * cinfo.output_components;
buffer = (unsigned char *)malloc(sizebuf);
texturebuf = (unsigned char *)malloc(sizebuf * cinfo.output_height);
unsigned char *tempjpg = texturebuf;
while(cinfo.output_scanline < cinfo.output_height)
{
    jpeg_read_scanlines(&cinfo,&buffer,1);
    memcpy(texturebuf,buffer,sizebuf);
    texturebuf +=sizebuf;
}
texturebuf = tempjpg;
/*do something .eg: glTexImage2D()*/

jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);

free(buffer);
fclose(jpegfile);
}

Categories: C/C++ linux opengl 图形学

Tagged as:

lnmcc

Leave a Reply

Your email address will not be published.