Commit 834acf62 authored by Luis Penaranda's avatar Luis Penaranda

read jpeg files

parent 9308b92b
...@@ -280,12 +280,56 @@ void OpenGLCanvas::read_config_file(){ ...@@ -280,12 +280,56 @@ void OpenGLCanvas::read_config_file(){
emit max_fov_changed((int)fov_max); 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){ 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; 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); glPixelStorei(GL_UNPACK_ALIGNMENT,1);
GLuint tex; GLuint tex;
glGenTextures(1,&tex); glGenTextures(1,&tex);
...@@ -555,29 +599,25 @@ void OpenGLCanvas::define_triangle_indices(unsigned int * indices, int m, int n) ...@@ -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; struct pam inpam;
pm_init(progname, 0); pm_init(progname, 0);
FILE *in_file; FILE *in_file;
FOPEN_RO(in_file,texturePath) FOPEN_RO(in_file,texturePath)
#ifdef PAM_STRUCT_SIZE #ifdef PAM_STRUCT_SIZE
pnm_readpaminit(in_file,&inpam,PAM_STRUCT_SIZE(tuple_type)); pnm_readpaminit(in_file,&inpam,PAM_STRUCT_SIZE(tuple_type));
#else #else
pnm_readpaminit(in_file,&inpam,sizeof(struct pam)); pnm_readpaminit(in_file,&inpam,sizeof(struct pam));
#endif #endif
image_size_x=inpam.width; image_size_x=inpam.width;
image_size_y=inpam.height; image_size_y=inpam.height;
int size = image_size_x*image_size_y*inpam.depth*inpam.bytes_per_sample; int size = image_size_x*image_size_y*inpam.depth*inpam.bytes_per_sample;
pm_close(in_file); pm_close(in_file);
//fprintf(stderr,"size=%d\n",size);
return size; return size;
} }
void OpenGLCanvas::readTextureBytes(const char * const progname, void OpenGLCanvas::pnmReadTextureBytes(const char * const progname,
const char * texturePath, const char * texturePath,
unsigned char * textureBytes, unsigned char * textureBytes,
int * outImageWidth, int * outImageWidth,
...@@ -618,6 +658,56 @@ void OpenGLCanvas::readTextureBytes(const char * const progname, ...@@ -618,6 +658,56 @@ void OpenGLCanvas::readTextureBytes(const char * const progname,
pm_close(in_file); 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_scanline<cinfo.output_height){
--scanline;
jpeg_read_scanlines(&cinfo,row_pointer,1);
for(int i=0;i<image_size_x*depth;++i){
textureBytes[scanline*image_size_x*depth+i]=row_pointer[0][i];
}
}
*outImageWidth=cinfo.output_width;
*outImageHeight=cinfo.output_height;
fclose(in_file);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
}
void OpenGLCanvas::resizeGL(int w, int h){ void OpenGLCanvas::resizeGL(int w, int h){
if(w>h) if(w>h)
glViewport(0,(h-w)/2,w,w); glViewport(0,(h-w)/2,w,w);
......
...@@ -41,6 +41,14 @@ extern "C" { ...@@ -41,6 +41,14 @@ extern "C" {
#include <pam.h> #include <pam.h>
} }
#include <jpeglib.h>
enum FileType{
PNM,
JPEG,
UNKNOWN
};
class OpenGLCanvas : public QGLWidget class OpenGLCanvas : public QGLWidget
{ {
Q_OBJECT Q_OBJECT
...@@ -58,8 +66,10 @@ protected: ...@@ -58,8 +66,10 @@ protected:
void load_sphere_mesh(float *positions, int m, int n); void load_sphere_mesh(float *positions, int m, int n);
float calculate_extent(float fov_rads); float calculate_extent(float fov_rads);
void define_triangle_indices(unsigned int * indices, int m, int n); void define_triangle_indices(unsigned int * indices, int m, int n);
int getTextureSize(const char * const progname, const char * texturePath); int pnmGetTextureSize(const char *const progname, const char *texturePath);
void readTextureBytes(const char * const progname, const char * texturePath,unsigned char * textureBytes,int * outImageWidth, int * outImageHeight); 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); char *textFileRead(char *fn);
void setShaders(); void setShaders();
void mousePressEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event);
...@@ -85,6 +95,8 @@ public slots: ...@@ -85,6 +95,8 @@ public slots:
void slotTimer(); void slotTimer();
private: private:
enum FileType get_file_type(const char*);
double fov; double fov;
double fov_max; // the \phi_{max} on the technote double fov_max; // the \phi_{max} on the technote
double scale; double scale;
......
...@@ -8,13 +8,13 @@ QT += core gui opengl ...@@ -8,13 +8,13 @@ QT += core gui opengl
mac { mac {
LIBS += -L/opt/local/lib LIBS += -L/opt/local/lib
LIBS += -lpng -lpnm LIBS += -lpng -lpnm -ljpeg
LIBS += -framework Carbon -framework OpenGL -framework GLUT LIBS += -framework Carbon -framework OpenGL -framework GLUT
INCLUDEPATH += /opt/local/include/netpbm/ \ INCLUDEPATH += /opt/local/include/netpbm/ \
/opt/local/include/ /opt/local/include/
} else:unix { } else:unix {
#LIBS += -lm -lpng -lpetsc -lglut -lGL -lnetpbm #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 # GLee is required when the version of opengl is old
#SOURCES += GLee.c #SOURCES += GLee.c
#HEADERS += GLee.h #HEADERS += GLee.h
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment