From 834acf62e7fe2eb86477cc1857b51dba82d617f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Pe=C3=B1aranda?= Date: Mon, 15 Apr 2013 14:44:07 -0300 Subject: [PATCH] read jpeg files --- openglcanvas.cpp | 110 +++++++++++++++++++++++++++++++++++++++---- openglcanvas.h | 16 ++++++- pano_interface_1.pro | 4 +- 3 files changed, 116 insertions(+), 14 deletions(-) diff --git a/openglcanvas.cpp b/openglcanvas.cpp index 0dcc452..8be5b01 100644 --- a/openglcanvas.cpp +++ b/openglcanvas.cpp @@ -280,12 +280,56 @@ void OpenGLCanvas::read_config_file(){ emit max_fov_changed((int)fov_max); } +// The file type is determined from the file extension. +enum FileType OpenGLCanvas::get_file_type(const char *filename){ + // find the last '.' in the file name + int dotposition=-1; + for(int i=strlen(filename)-1;i>=0;--i){ + if(filename[i]=='.'){ + dotposition=i; + break; + } + } + // if '.' was not found, the whole string is considered extension + char *ext=(char*)filename+dotposition+1; // file extension + if((ext[0]=='p'||ext[0]=='P')&& + (ext[1]=='n'||ext[1]=='b'||ext[1]=='g'||ext[1]=='p'|| + ext[1]=='N'||ext[1]=='B'||ext[1]=='G'||ext[1]=='P')&& + (ext[2]=='m'||ext[2]=='M')){ + return PNM; + } + if((ext[0]=='j'||ext[0]=='J')&& + (ext[1]=='p'||ext[1]=='P')&& + ((strlen(ext)==3&&(ext[2]=='g'||ext[2]=='G'))|| + (strlen(ext)==4&&(ext[2]=='e'||ext[2]=='E')&& + (ext[3]=='g'||ext[3]=='G')))){ + return JPEG; + } + return UNKNOWN; +} + void OpenGLCanvas::load_image(const char *new_image){ - const char * const progname=(char*)(PROGNAME); - int textureSize=getTextureSize(progname,new_image); - unsigned char * textureBytes=(unsigned char*)malloc(textureSize); int width,height; - readTextureBytes(progname,new_image,textureBytes,&width,&height); + const char * const progname=(char*)(PROGNAME); + unsigned char *textureBytes=NULL; + int textureSize; + switch(get_file_type(new_image)){ + case JPEG: + fprintf(stderr,"input image has JPEG format\n"); + textureSize=jpgGetTextureSize(new_image); + textureBytes=(unsigned char*)malloc(textureSize); + jpgReadTextureBytes(new_image,textureBytes,&width,&height); + break; + case PNM: + fprintf(stderr,"input image has PNM format\n"); + textureSize=pnmGetTextureSize(progname,new_image); + textureBytes=(unsigned char*)malloc(textureSize); + pnmReadTextureBytes(progname,new_image,textureBytes,&width,&height); + break; + default: // UNKNOWN + fprintf(stderr,"%s: unknown file format\n",new_image); + exit(-1); + } glPixelStorei(GL_UNPACK_ALIGNMENT,1); GLuint tex; glGenTextures(1,&tex); @@ -555,29 +599,25 @@ void OpenGLCanvas::define_triangle_indices(unsigned int * indices, int m, int n) } -int OpenGLCanvas::getTextureSize(const char * const progname, const char * texturePath) +int OpenGLCanvas::pnmGetTextureSize(const char * const progname, const char * texturePath) { struct pam inpam; pm_init(progname, 0); FILE *in_file; FOPEN_RO(in_file,texturePath) - #ifdef PAM_STRUCT_SIZE pnm_readpaminit(in_file,&inpam,PAM_STRUCT_SIZE(tuple_type)); #else pnm_readpaminit(in_file,&inpam,sizeof(struct pam)); #endif - image_size_x=inpam.width; image_size_y=inpam.height; int size = image_size_x*image_size_y*inpam.depth*inpam.bytes_per_sample; pm_close(in_file); - - //fprintf(stderr,"size=%d\n",size); return size; } -void OpenGLCanvas::readTextureBytes(const char * const progname, +void OpenGLCanvas::pnmReadTextureBytes(const char * const progname, const char * texturePath, unsigned char * textureBytes, int * outImageWidth, @@ -618,6 +658,56 @@ void OpenGLCanvas::readTextureBytes(const char * const progname, pm_close(in_file); } +int OpenGLCanvas::jpgGetTextureSize(const char *texturePath) +{ + FILE *in_file; + FOPEN_RO(in_file,texturePath) + struct jpeg_decompress_struct cinfo; + struct jpeg_error_mgr jerr; + cinfo.err=jpeg_std_error(&jerr); + jpeg_create_decompress(&cinfo); + jpeg_stdio_src(&cinfo,in_file); + jpeg_read_header(&cinfo,0); + jpeg_start_decompress(&cinfo); + image_size_x=cinfo.output_width; + image_size_y=cinfo.output_height; + int size=image_size_x*image_size_y*cinfo.num_components; + fclose(in_file); + return size; +} + +void OpenGLCanvas::jpgReadTextureBytes(const char *texturePath, + unsigned char *textureBytes, + int *outImageWidth, + int *outImageHeight) +{ + FILE *in_file; + FOPEN_RO(in_file,texturePath); + struct jpeg_decompress_struct cinfo; + struct jpeg_error_mgr jerr; + cinfo.err=jpeg_std_error(&jerr); + JSAMPROW row_pointer[1]; + jpeg_create_decompress(&cinfo); + jpeg_stdio_src(&cinfo,in_file); + jpeg_read_header(&cinfo,0); + jpeg_start_decompress(&cinfo); + int depth=cinfo.num_components; + unsigned long scanline=image_size_y; + row_pointer[0]=(unsigned char*)malloc(image_size_x*depth); + while(cinfo.output_scanlineh) glViewport(0,(h-w)/2,w,w); diff --git a/openglcanvas.h b/openglcanvas.h index 2ee2083..da6c950 100644 --- a/openglcanvas.h +++ b/openglcanvas.h @@ -41,6 +41,14 @@ extern "C" { #include } +#include + +enum FileType{ + PNM, + JPEG, + UNKNOWN +}; + class OpenGLCanvas : public QGLWidget { Q_OBJECT @@ -58,8 +66,10 @@ protected: void load_sphere_mesh(float *positions, int m, int n); float calculate_extent(float fov_rads); void define_triangle_indices(unsigned int * indices, int m, int n); - int getTextureSize(const char * const progname, const char * texturePath); - void readTextureBytes(const char * const progname, const char * texturePath,unsigned char * textureBytes,int * outImageWidth, int * outImageHeight); + int pnmGetTextureSize(const char *const progname, const char *texturePath); + void pnmReadTextureBytes(const char *const progname, const char *texturePath,unsigned char *textureBytes,int *outImageWidth,int *outImageHeight); + int jpgGetTextureSize(const char *texturePath); + void jpgReadTextureBytes(const char *texturePath,unsigned char *textureBytes,int *outImageWidth,int *outImageHeight); char *textFileRead(char *fn); void setShaders(); void mousePressEvent(QMouseEvent *event); @@ -85,6 +95,8 @@ public slots: void slotTimer(); private: + enum FileType get_file_type(const char*); + double fov; double fov_max; // the \phi_{max} on the technote double scale; diff --git a/pano_interface_1.pro b/pano_interface_1.pro index 23fa05b..51ccf4f 100644 --- a/pano_interface_1.pro +++ b/pano_interface_1.pro @@ -8,13 +8,13 @@ QT += core gui opengl mac { LIBS += -L/opt/local/lib -LIBS += -lpng -lpnm +LIBS += -lpng -lpnm -ljpeg LIBS += -framework Carbon -framework OpenGL -framework GLUT INCLUDEPATH += /opt/local/include/netpbm/ \ /opt/local/include/ } else:unix { #LIBS += -lm -lpng -lpetsc -lglut -lGL -lnetpbm -LIBS += -lm -lglut -lGL -lGLEW -lnetpbm +LIBS += -lm -lglut -lGL -lGLEW -lnetpbm -ljpeg # GLee is required when the version of opengl is old #SOURCES += GLee.c #HEADERS += GLee.h -- GitLab