1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/* Copyright (C) 2000-2011 by George Williams */
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <basics.h>
#ifdef _NO_LIBJPEG
static int a_file_must_define_something=0; /* ANSI says so */
#elif !defined(_STATIC_LIBJPEG) && !defined(NODYNAMIC) /* I don't know how to deal with dynamic libs on mac OS/X, hence this */
#include <dynamic.h>
#undef HAVE_STDLIB_H /* Defined by libtool, redefined by jconfig.h, annoying warning msg */
#include <sys/types.h>
#include <stdio.h>
#include <jpeglib.h>
#include <jerror.h>
#include <setjmp.h>
#include "gimage.h"
static DL_CONST void *libjpeg=NULL;
static struct jpeg_error_mgr *(*_jpeg_std_error)(struct jpeg_error_mgr *);
static void (*_jpeg_destroy_decompress)(j_decompress_ptr);
static void (*_jpeg_create_decompress)(j_decompress_ptr,int,size_t);
static void (*_jpeg_stdio_src)(j_decompress_ptr, FILE *);
static int (*_jpeg_read_header)(j_decompress_ptr, boolean);
static boolean (*_jpeg_start_decompress)(j_decompress_ptr);
static boolean (*_jpeg_read_scanlines)(j_decompress_ptr, JSAMPARRAY, JDIMENSION);
static boolean (*_jpeg_finish_decompress)(j_decompress_ptr);
static int loadjpeg() {
char *err;
libjpeg = dlopen("libjpeg" SO_EXT,RTLD_LAZY);
if ( libjpeg==NULL ) {
fprintf(stderr,"%s\n", dlerror());
return( 0 );
}
_jpeg_std_error = (struct jpeg_error_mgr *(*)(struct jpeg_error_mgr *))
dlsym(libjpeg,"jpeg_std_error");
_jpeg_destroy_decompress = (void (*)(j_decompress_ptr))
dlsym(libjpeg,"jpeg_destroy_decompress");
_jpeg_create_decompress = (void (*)(j_decompress_ptr,int,size_t))
dlsym(libjpeg,"jpeg_CreateDecompress");
_jpeg_stdio_src = (void (*)(j_decompress_ptr, FILE *))
dlsym(libjpeg,"jpeg_stdio_src");
_jpeg_read_header = (int (*)(j_decompress_ptr,boolean))
dlsym(libjpeg,"jpeg_read_header");
_jpeg_start_decompress = (boolean (*)(j_decompress_ptr))
dlsym(libjpeg,"jpeg_start_decompress");
_jpeg_read_scanlines = (boolean (*)(j_decompress_ptr, JSAMPARRAY, JDIMENSION))
dlsym(libjpeg,"jpeg_read_scanlines");
_jpeg_finish_decompress = (boolean (*)(j_decompress_ptr))
dlsym(libjpeg,"jpeg_finish_decompress");
if ( _jpeg_std_error && _jpeg_destroy_decompress && _jpeg_create_decompress &&
_jpeg_stdio_src && _jpeg_read_header && _jpeg_start_decompress &&
_jpeg_read_scanlines && _jpeg_finish_decompress )
return( 1 );
dlclose(libjpeg);
err = dlerror();
if ( err==NULL )
err = "Couldn't load needed symbol from libjpeg.so";
fprintf(stderr,"%s\n", err);
return( 0 );
}
/******************************************************************************/
struct jpegState {
struct jpeg_decompress_struct *cinfo;
int state;
struct _GImage *base;
JSAMPLE *buffer;
int scanpos;
};
struct my_error_mgr {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
int padding[8]; /* On my solaris box jmp_buf is the wrong size */
};
typedef struct my_error_mgr * my_error_ptr;
METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
{
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
my_error_ptr myerr = (my_error_ptr) cinfo->err;
/* Always display the message. */
/* We could postpone this until after returning, if we chose. */
(*cinfo->err->output_message) (cinfo);
/* Return control to the setjmp point */
longjmp(myerr->setjmp_buffer, 1);
}
/* jpeg routines use 24 bit pixels, xvt routines pad out to 32 */
static void transferBufferToImage(struct jpegState *js,int ypos) {
struct jpeg_decompress_struct *cinfo = js->cinfo;
JSAMPLE *pt, *end;
Color *ppt;
ppt = (Color *) (js->base->data+ypos*js->base->bytes_per_line);
for ( pt = js->buffer, end = pt+3*cinfo->image_width; pt<end; ) {
register int r,g,b;
r = *(pt++); g= *(pt++); b= *(pt++);
*(ppt++) = COLOR_CREATE(r,g,b);
}
}
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 );
}
#else
#include <sys/types.h>
#include <stdio.h>
#include <jpeglib.h>
#include <jerror.h>
#include <setjmp.h>
#include "gimage.h"
/******************************************************************************/
struct jpegState {
struct jpeg_decompress_struct *cinfo;
int state;
struct _GImage *base;
JSAMPLE *buffer;
int scanpos;
};
struct my_error_mgr {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
int padding[8]; /* On my solaris box jmp_buf is the wrong size */
};
typedef struct my_error_mgr * my_error_ptr;
METHODDEF(void)
my_error_exit (j_common_ptr cinfo)
{
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
my_error_ptr myerr = (my_error_ptr) cinfo->err;
/* Always display the message. */
/* We could postpone this until after returning, if we chose. */
(*cinfo->err->output_message) (cinfo);
/* Return control to the setjmp point */
longjmp(myerr->setjmp_buffer, 1);
}
/* jpeg routines use 24 bit pixels, xvt routines pad out to 32 */
static void transferBufferToImage(struct jpegState *js,int ypos) {
struct jpeg_decompress_struct *cinfo = js->cinfo;
JSAMPLE *pt, *end;
Color *ppt;
ppt = (Color *) (js->base->data+ypos*js->base->bytes_per_line);
for ( pt = js->buffer, end = pt+3*cinfo->image_width; pt<end; ) {
register int r,g,b;
r = *(pt++); g= *(pt++); b= *(pt++);
*(ppt++) = COLOR_CREATE(r,g,b);
}
}
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;
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_CreateDecompress(&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 ((infile = fopen(filename, "rb")) == NULL) {
fprintf(stderr,"can't open %s\n", filename);
return( NULL );
}
ret = GImageRead_Jpeg(infile);
fclose(infile);
return( ret );
}
#endif
|