GImage *GImageRead_Jpeg(FILE *infile) { GImage *ret; struct _GImage *base; struct jpeg_decompress_struct cinfo; struct my_error_mgr jerr; JSAMPLE *rows[1]; struct jpegState js; int ypos; if ( libjpeg==NULL ) if ( !loadjpeg()) return( NULL ); cinfo.err = _jpeg_std_error(&jerr.pub); jerr.pub.error_exit = my_error_exit; if (setjmp(jerr.setjmp_buffer)) { _jpeg_destroy_decompress(&cinfo); return( NULL ); } _jpeg_create_decompress(&cinfo,JPEG_LIB_VERSION,(size_t) sizeof(struct jpeg_decompress_struct)); _jpeg_stdio_src(&cinfo, infile); (void) _jpeg_read_header(&cinfo, TRUE); if ( cinfo.jpeg_color_space == JCS_GRAYSCALE ) cinfo.out_color_space = JCS_RGB; ret = GImageCreate(it_true,cinfo.image_width, cinfo.image_height); if ( ret==NULL ) { _jpeg_destroy_decompress(&cinfo); return( NULL ); } base = ret->u.image; (void) _jpeg_start_decompress(&cinfo); rows[0] = galloc(3*cinfo.image_width); js.cinfo = &cinfo; js.base = base; js.buffer = rows[0]; while (cinfo.output_scanline < cinfo.output_height) { ypos = cinfo.output_scanline; (void) _jpeg_read_scanlines(&cinfo, rows, 1); transferBufferToImage(&js,ypos); } (void) _jpeg_finish_decompress(&cinfo); _jpeg_destroy_decompress(&cinfo); gfree(rows[0]); return( ret ); }
GImage *GImageReadJpeg(char *filename) { GImage *ret; FILE * infile; /* source file */ if ( libjpeg==NULL ) if ( !loadjpeg()) return( NULL ); if ((infile = fopen(filename, "rb")) == NULL) { fprintf(stderr,"can't open %s\n", filename); return( NULL ); } ret = GImageRead_Jpeg(infile); fclose(infile); return( ret ); }
static int loadimage(const char *path, img_t *image) { lprintf("loadimage %s\n", path); const char *p = strrchr(path, '.'); if(!p) { lprintf("%s has no extension\n"); return -1; } const char *fn = strrchr(path, '/'); if(fn) fn++; else fn = path; strcpy(image->filename, fn); if(!strcasecmp(p, ".jpeg") || !strcasecmp(p, ".jpg")) return loadjpeg(path, image); if(!strcasecmp(p, ".png")) return loadpng(path, image); lprintf("Unrecognized extension\n"); return -1; }