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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
|
/*
* jpeg - This file provides interface routines to connect xloadimage to
* the jpeg decoding routines taken from the Independent JPEG Group's
* free JPEG software. See jpeg.README for more information.
*
* This code is based on example.c from the IJG v4 distribution.
* 1998/08/19: Change for IJG v6.0a. dump Progressive JPEG support.
*/
#include "image.h" /* xloadimage declarations */
#ifdef HAVE_LIBJPEG
#include "options.h"
#include <jpeglib.h>
#include <jerror.h>
#include <setjmp.h> /* need setjmp/longjmp */
#undef DEBUG
/* #define DEBUG */
#undef debug
#ifdef DEBUG
# define debug(xx) fprintf(stderr,xx)
#else
# define debug(xx)
#endif
static Image *image; /* xloadimage image being returned */
static char *filename;
static jmp_buf setjmp_buffer; /* for return to caller */
ZFILE * zinput_file; /* tells input routine where to read JPEG */
static JOCTET jpeg_read_buff[1024 * 16];
/*
* source manager
*/
void init_source( j_decompress_ptr cinfo)
{
debug("init_source()");
}
boolean fill_input_buffer( j_decompress_ptr cinfo)
{
struct jpeg_source_mgr *src = cinfo->src;
debug("fill_input_buffer()");
#ifdef DEBUG
fprintf( stderr,"fill_input_buffer(): %d ",src->bytes_in_buffer);
#endif
src->next_input_byte = jpeg_read_buff;
src->bytes_in_buffer = zread(zinput_file,
jpeg_read_buff, sizeof(jpeg_read_buff));
if(src->bytes_in_buffer <= 0){
WARNMS(cinfo, JWRN_JPEG_EOF);
jpeg_read_buff[0] = 0xFF;
jpeg_read_buff[1] = JPEG_EOI;
src->bytes_in_buffer = 2;
}
return TRUE;
}
void skip_input_data( j_decompress_ptr cinfo, long num_bytes)
{
int rest;
struct jpeg_source_mgr *src = cinfo->src;
debug("skip_input_data()");
#ifdef DEBUG
fprintf(stderr,": %ld,%d ", num_bytes, src->bytes_in_buffer);
#endif
if( num_bytes < 1) return;
rest = src->bytes_in_buffer;
if( num_bytes < rest) {
src->next_input_byte += num_bytes;
src->bytes_in_buffer -= num_bytes;;
return;
}
num_bytes -= rest;
while( num_bytes--) {
zgetc(zinput_file);
}
fill_input_buffer(cinfo);
}
boolean resync_to_restart( j_decompress_ptr cinfo, int desired)
{
return jpeg_resync_to_restart( cinfo, desired);
}
void term_source( j_decompress_ptr cinfo)
{
debug("term_source()");
}
/*
* error manager
*/
static void
output_message ( j_common_ptr cominfo)
{
char buf[JMSG_LENGTH_MAX];
(*cominfo->err->format_message)(cominfo, buf);
fprintf(stderr, "jpegLoad: %s - %s\n", filename, buf);
}
static void error_exit (j_common_ptr cominfo)
{
output_message( cominfo);
longjmp(setjmp_buffer, 1); /* return control to outer routine */
}
static void
jpegInfo (cinfo)
j_decompress_ptr cinfo;
{
/* Create display of image parameters */
printf("%s is a %dx%d JPEG image, color space ", filename,
cinfo->image_width, cinfo->image_height);
switch (cinfo->jpeg_color_space) {
case JCS_GRAYSCALE:
printf("Grayscale");
break;
case JCS_RGB:
printf("RGB");
break;
case JCS_YCbCr:
printf("YCbCr");
break;
case JCS_CMYK:
printf("CMYK");
break;
case JCS_YCCK:
printf("YCCK");
break;
case JCS_UNKNOWN:
default:
printf("Unknown");
break;
}
printf(", %d comp%s,", cinfo->num_components,
(cinfo->num_components - 1) ? "s" : "");
if (cinfo->progressive_mode)
printf(" Progressive,");
if (cinfo->arith_code)
printf(" Arithmetic coding.\n");
else
printf(" Huffman coding.\n");
}
/* Main control routine for loading */
Image *
jpegLoad (fullname, name, verbose)
char *fullname, *name;
unsigned int verbose;
{
struct jpeg_decompress_struct cinfo;
struct jpeg_source_mgr src_mgr;
struct jpeg_error_mgr err_mgr;
int i, row_stride;
byte *bufp;
zinput_file = zopen(fullname); /* Open the input file */
if (zinput_file == NULL)
return NULL;
filename = name; /* copy parms to static vars */
image = NULL; /* in case we fail before creating image */
jpeg_create_decompress(&cinfo);
src_mgr.init_source = init_source;
src_mgr.fill_input_buffer = fill_input_buffer;
src_mgr.skip_input_data = skip_input_data;
src_mgr.resync_to_restart = resync_to_restart;
src_mgr.term_source = term_source;
cinfo.src = &src_mgr; /* links to method structs */
err_mgr.error_exit = error_exit; /* supply error-exit routine */
err_mgr.output_message = output_message;
err_mgr.trace_level = 0; /* default = no tracing */
err_mgr.num_warnings = 0; /* no warnings emitted yet */
cinfo.err = jpeg_std_error(&err_mgr);
src_mgr.bytes_in_buffer = 0;
fill_input_buffer( &cinfo);
/* Quick check to see if file starts with JPEG SOI marker */
if(jpeg_read_buff[0] != 0xFF || jpeg_read_buff[1] != 0xD8) {
zclose(zinput_file);
return NULL;
}
/* prepare setjmp context for possible exit from error_exit */
if (setjmp(setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error. */
/* Return as much of the image as we could get. */
jpeg_destroy_decompress(&cinfo);
zclose(zinput_file);
return image;
}
jpeg_read_header(&cinfo, TRUE);
if (verbose) jpegInfo(&cinfo);
/* Turn off caching beyond this point of the file */
znocache(zinput_file);
jpeg_start_decompress(&cinfo);
switch (cinfo.out_color_space) {
case JCS_GRAYSCALE:
image = newRGBImage(cinfo.image_width,cinfo.image_height,8);
image->title = dupString(filename);
/* set a linear map */
for(i=0;i<256;i++) {
*(image->rgb.red + i) =
*(image->rgb.green + i) =
*(image->rgb.blue + i) = i << 8;
}
image->rgb.used = 256;
break;
case JCS_RGB:
image = newTrueImage(cinfo.image_width,cinfo.image_height);
image->title = dupString(filename);
break;
default:
image = NULL;
ERREXITS(&cinfo, 1, "Cannot cope with JPEG image colorspace");
}
row_stride = cinfo.output_width * cinfo.output_components;
bufp = image->data;
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines(&cinfo, &bufp, 1);
bufp += row_stride;
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
zclose(zinput_file); /* Done, close the input file */
return image;
}
/*
Main control routine for identifying JPEG without loading
return 0: Not jpeg file.
*/
int
jpegIdent (fullname, name)
char *fullname, *name;
{
struct jpeg_decompress_struct cinfo;
struct jpeg_source_mgr src_mgr;
struct jpeg_error_mgr err_mgr;
zinput_file = zopen(fullname); /* Open the input file */
if (zinput_file == NULL)
return 0;
filename = name; /* copy parms to static vars */
jpeg_create_decompress(&cinfo);
src_mgr.init_source = init_source;
src_mgr.fill_input_buffer = fill_input_buffer;
src_mgr.skip_input_data = skip_input_data;
src_mgr.resync_to_restart = resync_to_restart;
src_mgr.term_source = term_source;
cinfo.src = &src_mgr; /* links to method structs */
err_mgr.error_exit = error_exit; /* supply error-exit routine */
err_mgr.output_message = output_message;
err_mgr.trace_level = 0; /* default = no tracing */
err_mgr.num_warnings = 0; /* no warnings emitted yet */
cinfo.err = jpeg_std_error(&err_mgr);
src_mgr.bytes_in_buffer = 0;
fill_input_buffer( &cinfo);
/* Quick check to see if file starts with JPEG SOI marker */
if(jpeg_read_buff[0] != 0xFF || jpeg_read_buff[1] != 0xD8) {
jpeg_destroy_decompress(&cinfo);
zclose(zinput_file);
return 0;
}
/* prepare setjmp context for expected exit via longjmp */
if (setjmp(setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error. */
/* Return 0 since error in the headers means the image is unloadable. */
jpeg_destroy_decompress(&cinfo);
zclose(zinput_file);
return 0;
}
jpeg_read_header(&cinfo, TRUE);
jpegInfo(&cinfo);
/* Turn off caching beyond this point of the file */
znocache(zinput_file);
jpeg_destroy_decompress(&cinfo);
zclose(zinput_file);
return 1;
}
/*
* Dump Jpeg
*/
/* parse options passed to jpegDump
*/
static void parseOptions(j_compress_ptr cinfo, char *options, int verbose)
{
char *name, *value;
while (getNextTypeOption(&options, &name, &value) > 0) {
if (!strncmp("arithmetic", name, strlen(name))) {
/* Use arithmetic coding. */
#ifdef C_ARITH_CODING_SUPPORTED
if (verbose)
printf(" Using arithmetic coding.\n");
cinfo->arith_code = TRUE;
#else
fprintf(stderr, "jpegDump: sorry, arithmetic coding not supported\n");
}
#endif
else if (!strncmp("grayscale", name, strlen(name)) ||
!strncmp("greyscale", name, strlen(name)) ||
!strncmp("monochrome", name, strlen(name))) {
/* Force a monochrome JPEG file to be generated. */
if (verbose)
printf(" Creating a grayscale/monochrome file.\n");
jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
}
else if (!strncmp("nointerleave", name, strlen(name))) {
/* Create noninterleaved file. */
#ifdef C_MULTISCAN_FILES_SUPPORTED
if (verbose)
printf(" Creating a noninterleaved file.\n");
cinfo->interleave = FALSE;
#else
fprintf(stderr, "jpegDump: sorry, multiple-scan support was not compiled\n");
#endif
}
else if (!strncmp("progressive", name, strlen(name))) {
/* Enable progressive JPEG. */
if (verbose)
printf(" Progressive JPEG.\n");
jpeg_simple_progression (cinfo);
}
else if (!strncmp("optimize", name, strlen(name)) ||
!strncmp("optimise", name, strlen(name))) {
/* Enable entropy parm optimization. */
if (verbose)
printf(" Optimizing entropy.\n");
cinfo->optimize_coding = TRUE;
}
else if (!strncmp("quality", name, strlen(name))) {
/* Quality factor (quantization table scaling factor). */
int val;
if (!value || (sscanf(value, "%d", &val) != 1))
fprintf(stderr, "jpegDump: quality: Invalid quality factor specified.\n");
else {
/* Set quantization tables (will be overridden if -qtables also given).
* Note: we make force_baseline FALSE.
* This means non-baseline JPEG files can be created with low Q values.
* To ensure only baseline files are generated, pass TRUE instead.
*/
if (verbose)
printf(" Using a quality factor of %d.\n", val);
jpeg_set_quality(cinfo, val, FALSE);
#if 0
/* Change scale factor in case -qtables is present. */
q_scale_factor = j_quality_scaling(val);
#endif
}
}
#if 0
else if (!strncmp("qtables", name, strlen(name))) {
/* Quantization tables fetched from file. */
if (!value || !*value)
fprintf(stderr, "jpegDump: No file specified for quantization tables.\n");
else {
if (verbose)
printf(" Using quantization tables loaded from %s.\n", qtablefile);
qtablefile = value;
}
/* we postpone actually reading the file in case -quality comes later */
}
#endif
else if (!strncmp("restart", name, strlen(name))) {
/* Restart interval in MCU rows (or in MCUs with 'b'). */
long lval;
char ch = 'x';
if (!value || (sscanf(value, "%ld%c", &lval, &ch) < 1) ||
(lval < 0) || (lval > 65535L)) {
fprintf(stderr, "jpegDump: restart: Invalid restart interval specified.\n");
continue;
}
if (verbose)
printf(" Using a restart interval of %s.\n", value);
if (ch == 'b' || ch == 'B')
cinfo->restart_interval = (UINT16) lval;
else
cinfo->restart_in_rows = (int) lval;
}
#if 0
else if (!strncmp("sample", name, strlen(name))) {
/* Set sampling factors. */
if (!value)
fprintf(stderr, "jpegDump: sample: No sampling factors specified.\n");
if (verbose)
printf(" Using sampling factors of %s.\n", value);
set_sample_factors(cinfo, value);
}
#endif
else if (!strncmp("smooth", name, strlen(name))) {
/* Set input smoothing factor. */
int val;
if (!value || (sscanf(value, "%d", &val) != 1) ||
(val < 0) || (val > 100))
fprintf(stderr, "jpegDump: smooth: Invalid smoothing factor specified.\n");
else {
if (verbose)
printf(" Using a smoothing factor of %d.\n", val);
cinfo->smoothing_factor = val;
}
}
else
fprintf(stderr, "jpegDump: Unknown option '%s'.\n", name);
}
}
/* this reads a single raster line
*/
byte *current_row;
unsigned int bytes_per_row;
byte *read_row(Image *image, byte *pixel_rows)
{
int x;
int pixlen;
byte *src_row_ptr = current_row;
byte *dest_row_ptr = pixel_rows;
Pixel pixval;
byte mask;
switch (image->type) {
case IBITMAP:
mask = 0x80;
for (x = 0; x < image->width; x++) {
pixval = ((*src_row_ptr & mask) > 0 ? 1 : 0);
/* we use the "red" color value under the assumption that they
* are all equal. that can be wrong if the user used -foreground
* or -background. I don't care right now.
*/
*dest_row_ptr++ = image->rgb.red[pixval] >> 8;
mask >>= 1;
if (mask == 0) {
mask = 0x80;
src_row_ptr++;
}
}
break;
case IRGB:
/* this expands the pixel value into its components
*/
pixlen = image->pixlen;
for (x = 0; x < image->width; x++) {
pixval = memToVal(src_row_ptr, pixlen);
*dest_row_ptr++ = image->rgb.red[pixval] >> 8;
*dest_row_ptr++ = image->rgb.green[pixval] >> 8;
*dest_row_ptr++ = image->rgb.blue[pixval] >> 8;
src_row_ptr += pixlen;
}
break;
case ITRUE:
return current_row;
break;
}
return pixel_rows;
}
void jpegDump(Image *image, char *options, char *file, int verbose)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * outfile; /* target file */
JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
if ((outfile = fopen(file, "w")) == NULL) {
perror(file);
return;
}
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = image->width;
cinfo.image_height = image->height;
/* set # of color components per pixel & colospace fo input image */
switch (image->type) {
case IBITMAP:
bytes_per_row = (image->width / 8) + (image->width % 8 ? 1 : 0);
cinfo.input_components = 1;
cinfo.in_color_space = JCS_GRAYSCALE;
row_pointer[0] = lmalloc( cinfo.image_width * cinfo.input_components);
break;
case IRGB:
bytes_per_row = image->width * image->pixlen;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
row_pointer[0] = lmalloc( cinfo.image_width * cinfo.input_components);
break;
case ITRUE:
bytes_per_row = image->width * image->pixlen;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
row_pointer[0] = image->data;
break;
}
/* Now use the library's routine to set default compression parameters.
* (You must set at least cinfo.in_color_space before calling this,
* since the defaults depend on the source color space.)
*/
jpeg_set_defaults(&cinfo);
/* Now you can set any non-default parameters you wish to.
* Here we just illustrate the use of quality (quantization table) scaling:
*/
jpeg_set_quality(&cinfo, 75, TRUE /* limit to baseline-JPEG values */);
if( cinfo.in_color_space == JCS_GRAYSCALE)
jpeg_set_colorspace(&cinfo, JCS_GRAYSCALE);
parseOptions(&cinfo, options, verbose);
jpeg_start_compress(&cinfo, TRUE);
current_row = image->data;
while (cinfo.next_scanline < cinfo.image_height) {
/* jpeg_write_scanlines expects an array of pointers to scanlines.
* Here the array is only one element long, but you could pass
* more than one scanline at a time if that's more convenient.
*/
row_pointer[0] = read_row(image, row_pointer[0]);
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
current_row += bytes_per_row;
}
jpeg_finish_compress(&cinfo);
fclose(outfile);
jpeg_destroy_compress(&cinfo);
if (image->type == IBITMAP || image->type == IRGB) {
lfree( row_pointer[0]);
}
}
#else /* !HAVE_LIBJPEG */
static int unused;
#endif /* !HAVE_LIBJPEG */
|