| 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 |
#include "cdjpeg.h" |
| 27 |
#include "jversion.h" |
| 28 |
|
| 29 |
#ifdef USE_CCOMMAND |
| 30 |
#ifdef __MWERKS__ |
| 31 |
#include <SIOUX.h> |
| 32 |
#include <console.h> |
| 33 |
#endif |
| 34 |
#ifdef THINK_C |
| 35 |
#include <console.h> |
| 36 |
#endif |
| 37 |
#endif |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#define JMESSAGE(code,string) string , |
| 43 |
|
| 44 |
static const char * const cdjpeg_message_table[] = { |
| 45 |
#include "cderror.h" |
| 46 |
NULL |
| 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 |
static boolean is_targa; |
| 77 |
|
| 78 |
|
| 79 |
LOCAL(cjpeg_source_ptr) |
| 80 |
select_file_type (j_compress_ptr cinfo, FILE * infile) |
| 81 |
{ |
| 82 |
int c; |
| 83 |
|
| 84 |
if (is_targa) { |
| 85 |
#ifdef TARGA_SUPPORTED |
| 86 |
return jinit_read_targa(cinfo); |
| 87 |
#else |
| 88 |
ERREXIT(cinfo, JERR_TGA_NOTCOMP); |
| 89 |
#endif |
| 90 |
} |
| 91 |
|
| 92 |
if ((c = getc(infile)) == EOF) |
| 93 |
ERREXIT(cinfo, JERR_INPUT_EMPTY); |
| 94 |
if (ungetc(c, infile) == EOF) |
| 95 |
ERREXIT(cinfo, JERR_UNGETC_FAILED); |
| 96 |
|
| 97 |
switch (c) { |
| 98 |
#ifdef BMP_SUPPORTED |
| 99 |
case 'B': |
| 100 |
return jinit_read_bmp(cinfo); |
| 101 |
#endif |
| 102 |
#ifdef GIF_SUPPORTED |
| 103 |
case 'G': |
| 104 |
return jinit_read_gif(cinfo); |
| 105 |
#endif |
| 106 |
#ifdef PPM_SUPPORTED |
| 107 |
case 'P': |
| 108 |
return jinit_read_ppm(cinfo); |
| 109 |
#endif |
| 110 |
#ifdef RLE_SUPPORTED |
| 111 |
case 'R': |
| 112 |
return jinit_read_rle(cinfo); |
| 113 |
#endif |
| 114 |
#ifdef TARGA_SUPPORTED |
| 115 |
case 0x00: |
| 116 |
return jinit_read_targa(cinfo); |
| 117 |
#endif |
| 118 |
default: |
| 119 |
ERREXIT(cinfo, JERR_UNKNOWN_FORMAT); |
| 120 |
break; |
| 121 |
} |
| 122 |
|
| 123 |
return NULL; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
static const char * progname; |
| 137 |
static char * outfilename; |
| 138 |
|
| 139 |
|
| 140 |
LOCAL(void) |
| 141 |
usage (void) |
| 142 |
|
| 143 |
{ |
| 144 |
fprintf(stderr, "usage: %s [switches] ", progname); |
| 145 |
#ifdef TWO_FILE_COMMANDLINE |
| 146 |
fprintf(stderr, "inputfile outputfile\n"); |
| 147 |
#else |
| 148 |
fprintf(stderr, "[inputfile]\n"); |
| 149 |
#endif |
| 150 |
|
| 151 |
fprintf(stderr, "Switches (names may be abbreviated):\n"); |
| 152 |
fprintf(stderr, " -quality N Compression quality (0..100; 5-95 is useful range)\n"); |
| 153 |
fprintf(stderr, " -grayscale Create monochrome JPEG file\n"); |
| 154 |
#ifdef ENTROPY_OPT_SUPPORTED |
| 155 |
fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n"); |
| 156 |
#endif |
| 157 |
#ifdef C_PROGRESSIVE_SUPPORTED |
| 158 |
fprintf(stderr, " -progressive Create progressive JPEG file\n"); |
| 159 |
#endif |
| 160 |
#ifdef TARGA_SUPPORTED |
| 161 |
fprintf(stderr, " -targa Input file is Targa format (usually not needed)\n"); |
| 162 |
#endif |
| 163 |
fprintf(stderr, "Switches for advanced users:\n"); |
| 164 |
#ifdef DCT_ISLOW_SUPPORTED |
| 165 |
fprintf(stderr, " -dct int Use integer DCT method%s\n", |
| 166 |
(JDCT_DEFAULT == JDCT_ISLOW ? " (default)" : "")); |
| 167 |
#endif |
| 168 |
#ifdef DCT_IFAST_SUPPORTED |
| 169 |
fprintf(stderr, " -dct fast Use fast integer DCT (less accurate)%s\n", |
| 170 |
(JDCT_DEFAULT == JDCT_IFAST ? " (default)" : "")); |
| 171 |
#endif |
| 172 |
#ifdef DCT_FLOAT_SUPPORTED |
| 173 |
fprintf(stderr, " -dct float Use floating-point DCT method%s\n", |
| 174 |
(JDCT_DEFAULT == JDCT_FLOAT ? " (default)" : "")); |
| 175 |
#endif |
| 176 |
fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n"); |
| 177 |
#ifdef INPUT_SMOOTHING_SUPPORTED |
| 178 |
fprintf(stderr, " -smooth N Smooth dithered input (N=1..100 is strength)\n"); |
| 179 |
#endif |
| 180 |
fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n"); |
| 181 |
fprintf(stderr, " -outfile name Specify name for output file\n"); |
| 182 |
fprintf(stderr, " -verbose or -debug Emit debug output\n"); |
| 183 |
fprintf(stderr, "Switches for wizards:\n"); |
| 184 |
#ifdef C_ARITH_CODING_SUPPORTED |
| 185 |
fprintf(stderr, " -arithmetic Use arithmetic coding\n"); |
| 186 |
#endif |
| 187 |
fprintf(stderr, " -baseline Force baseline quantization tables\n"); |
| 188 |
fprintf(stderr, " -qtables file Use quantization tables given in file\n"); |
| 189 |
fprintf(stderr, " -qslots N[,...] Set component quantization tables\n"); |
| 190 |
fprintf(stderr, " -sample HxV[,...] Set component sampling factors\n"); |
| 191 |
#ifdef C_MULTISCAN_FILES_SUPPORTED |
| 192 |
fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n"); |
| 193 |
#endif |
| 194 |
exit(EXIT_FAILURE); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
LOCAL(int) |
| 199 |
parse_switches (j_compress_ptr cinfo, int argc, char **argv, |
| 200 |
int last_file_arg_seen, boolean for_real) |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
{ |
| 210 |
int argn; |
| 211 |
char * arg; |
| 212 |
int quality; |
| 213 |
int q_scale_factor; |
| 214 |
boolean force_baseline; |
| 215 |
boolean simple_progressive; |
| 216 |
char * qtablefile = NULL; |
| 217 |
char * qslotsarg = NULL; |
| 218 |
char * samplearg = NULL; |
| 219 |
char * scansarg = NULL; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
quality = 75; |
| 226 |
q_scale_factor = 100; |
| 227 |
force_baseline = FALSE; |
| 228 |
simple_progressive = FALSE; |
| 229 |
is_targa = FALSE; |
| 230 |
outfilename = NULL; |
| 231 |
cinfo->err->trace_level = 0; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
for (argn = 1; argn < argc; argn++) { |
| 236 |
arg = argv[argn]; |
| 237 |
if (*arg != '-') { |
| 238 |
|
| 239 |
if (argn <= last_file_arg_seen) { |
| 240 |
outfilename = NULL; |
| 241 |
continue; |
| 242 |
} |
| 243 |
break; |
| 244 |
} |
| 245 |
arg++; |
| 246 |
|
| 247 |
if (keymatch(arg, "arithmetic", 1)) { |
| 248 |
|
| 249 |
#ifdef C_ARITH_CODING_SUPPORTED |
| 250 |
cinfo->arith_code = TRUE; |
| 251 |
#else |
| 252 |
fprintf(stderr, "%s: sorry, arithmetic coding not supported\n", |
| 253 |
progname); |
| 254 |
exit(EXIT_FAILURE); |
| 255 |
#endif |
| 256 |
|
| 257 |
} else if (keymatch(arg, "baseline", 1)) { |
| 258 |
|
| 259 |
force_baseline = TRUE; |
| 260 |
|
| 261 |
} else if (keymatch(arg, "dct", 2)) { |
| 262 |
|
| 263 |
if (++argn >= argc) |
| 264 |
usage(); |
| 265 |
if (keymatch(argv[argn], "int", 1)) { |
| 266 |
cinfo->dct_method = JDCT_ISLOW; |
| 267 |
} else if (keymatch(argv[argn], "fast", 2)) { |
| 268 |
cinfo->dct_method = JDCT_IFAST; |
| 269 |
} else if (keymatch(argv[argn], "float", 2)) { |
| 270 |
cinfo->dct_method = JDCT_FLOAT; |
| 271 |
} else |
| 272 |
usage(); |
| 273 |
|
| 274 |
} else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) { |
| 275 |
|
| 276 |
|
| 277 |
static boolean printed_version = FALSE; |
| 278 |
|
| 279 |
if (! printed_version) { |
| 280 |
fprintf(stderr, "Independent JPEG Group's CJPEG, version %s\n%s\n", |
| 281 |
JVERSION, JCOPYRIGHT); |
| 282 |
printed_version = TRUE; |
| 283 |
} |
| 284 |
cinfo->err->trace_level++; |
| 285 |
|
| 286 |
} else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) { |
| 287 |
|
| 288 |
jpeg_set_colorspace(cinfo, JCS_GRAYSCALE); |
| 289 |
|
| 290 |
} else if (keymatch(arg, "maxmemory", 3)) { |
| 291 |
|
| 292 |
long lval; |
| 293 |
char ch = 'x'; |
| 294 |
|
| 295 |
if (++argn >= argc) |
| 296 |
usage(); |
| 297 |
if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
| 298 |
usage(); |
| 299 |
if (ch == 'm' || ch == 'M') |
| 300 |
lval *= 1000L; |
| 301 |
cinfo->mem->max_memory_to_use = lval * 1000L; |
| 302 |
|
| 303 |
} else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) { |
| 304 |
|
| 305 |
#ifdef ENTROPY_OPT_SUPPORTED |
| 306 |
cinfo->optimize_coding = TRUE; |
| 307 |
#else |
| 308 |
fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n", |
| 309 |
progname); |
| 310 |
exit(EXIT_FAILURE); |
| 311 |
#endif |
| 312 |
|
| 313 |
} else if (keymatch(arg, "outfile", 4)) { |
| 314 |
|
| 315 |
if (++argn >= argc) |
| 316 |
usage(); |
| 317 |
outfilename = argv[argn]; |
| 318 |
|
| 319 |
} else if (keymatch(arg, "progressive", 1)) { |
| 320 |
|
| 321 |
#ifdef C_PROGRESSIVE_SUPPORTED |
| 322 |
simple_progressive = TRUE; |
| 323 |
|
| 324 |
#else |
| 325 |
fprintf(stderr, "%s: sorry, progressive output was not compiled\n", |
| 326 |
progname); |
| 327 |
exit(EXIT_FAILURE); |
| 328 |
#endif |
| 329 |
|
| 330 |
} else if (keymatch(arg, "quality", 1)) { |
| 331 |
|
| 332 |
if (++argn >= argc) |
| 333 |
usage(); |
| 334 |
if (sscanf(argv[argn], "%d", &quality) != 1) |
| 335 |
usage(); |
| 336 |
|
| 337 |
q_scale_factor = jpeg_quality_scaling(quality); |
| 338 |
|
| 339 |
} else if (keymatch(arg, "qslots", 2)) { |
| 340 |
|
| 341 |
if (++argn >= argc) |
| 342 |
usage(); |
| 343 |
qslotsarg = argv[argn]; |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
} else if (keymatch(arg, "qtables", 2)) { |
| 350 |
|
| 351 |
if (++argn >= argc) |
| 352 |
usage(); |
| 353 |
qtablefile = argv[argn]; |
| 354 |
|
| 355 |
|
| 356 |
} else if (keymatch(arg, "restart", 1)) { |
| 357 |
|
| 358 |
long lval; |
| 359 |
char ch = 'x'; |
| 360 |
|
| 361 |
if (++argn >= argc) |
| 362 |
usage(); |
| 363 |
if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1) |
| 364 |
usage(); |
| 365 |
if (lval < 0 || lval > 65535L) |
| 366 |
usage(); |
| 367 |
if (ch == 'b' || ch == 'B') { |
| 368 |
cinfo->restart_interval = (unsigned int) lval; |
| 369 |
cinfo->restart_in_rows = 0; |
| 370 |
} else { |
| 371 |
cinfo->restart_in_rows = (int) lval; |
| 372 |
|
| 373 |
} |
| 374 |
|
| 375 |
} else if (keymatch(arg, "sample", 2)) { |
| 376 |
|
| 377 |
if (++argn >= argc) |
| 378 |
usage(); |
| 379 |
samplearg = argv[argn]; |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
} else if (keymatch(arg, "scans", 2)) { |
| 386 |
|
| 387 |
#ifdef C_MULTISCAN_FILES_SUPPORTED |
| 388 |
if (++argn >= argc) |
| 389 |
usage(); |
| 390 |
scansarg = argv[argn]; |
| 391 |
|
| 392 |
#else |
| 393 |
fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n", |
| 394 |
progname); |
| 395 |
exit(EXIT_FAILURE); |
| 396 |
#endif |
| 397 |
|
| 398 |
} else if (keymatch(arg, "smooth", 2)) { |
| 399 |
|
| 400 |
int val; |
| 401 |
|
| 402 |
if (++argn >= argc) |
| 403 |
usage(); |
| 404 |
if (sscanf(argv[argn], "%d", &val) != 1) |
| 405 |
usage(); |
| 406 |
if (val < 0 || val > 100) |
| 407 |
usage(); |
| 408 |
cinfo->smoothing_factor = val; |
| 409 |
|
| 410 |
} else if (keymatch(arg, "targa", 1)) { |
| 411 |
|
| 412 |
is_targa = TRUE; |
| 413 |
|
| 414 |
} else { |
| 415 |
usage(); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
if (for_real) { |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
jpeg_set_quality(cinfo, quality, force_baseline); |
| 426 |
|
| 427 |
if (qtablefile != NULL) |
| 428 |
if (! read_quant_tables(cinfo, qtablefile, |
| 429 |
q_scale_factor, force_baseline)) |
| 430 |
usage(); |
| 431 |
|
| 432 |
if (qslotsarg != NULL) |
| 433 |
if (! set_quant_slots(cinfo, qslotsarg)) |
| 434 |
usage(); |
| 435 |
|
| 436 |
if (samplearg != NULL) |
| 437 |
if (! set_sample_factors(cinfo, samplearg)) |
| 438 |
usage(); |
| 439 |
|
| 440 |
#ifdef C_PROGRESSIVE_SUPPORTED |
| 441 |
if (simple_progressive) |
| 442 |
jpeg_simple_progression(cinfo); |
| 443 |
#endif |
| 444 |
|
| 445 |
#ifdef C_MULTISCAN_FILES_SUPPORTED |
| 446 |
if (scansarg != NULL) |
| 447 |
if (! read_scan_script(cinfo, scansarg)) |
| 448 |
usage(); |
| 449 |
#endif |
| 450 |
} |
| 451 |
|
| 452 |
return argn; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
int |
| 461 |
main (int argc, char **argv) |
| 462 |
{ |
| 463 |
struct jpeg_compress_struct cinfo; |
| 464 |
struct jpeg_error_mgr jerr; |
| 465 |
#ifdef PROGRESS_REPORT |
| 466 |
struct cdjpeg_progress_mgr progress; |
| 467 |
#endif |
| 468 |
int file_index; |
| 469 |
cjpeg_source_ptr src_mgr; |
| 470 |
FILE * input_file; |
| 471 |
FILE * output_file; |
| 472 |
JDIMENSION num_scanlines; |
| 473 |
|
| 474 |
|
| 475 |
#ifdef USE_CCOMMAND |
| 476 |
argc = ccommand(&argv); |
| 477 |
#endif |
| 478 |
|
| 479 |
progname = argv[0]; |
| 480 |
if (progname == NULL || progname[0] == 0) |
| 481 |
progname = "cjpeg"; |
| 482 |
|
| 483 |
|
| 484 |
cinfo.err = jpeg_std_error(&jerr); |
| 485 |
jpeg_create_compress(&cinfo); |
| 486 |
|
| 487 |
jerr.addon_message_table = cdjpeg_message_table; |
| 488 |
jerr.first_addon_message = JMSG_FIRSTADDONCODE; |
| 489 |
jerr.last_addon_message = JMSG_LASTADDONCODE; |
| 490 |
|
| 491 |
|
| 492 |
#ifdef NEED_SIGNAL_CATCHER |
| 493 |
enable_signal_catcher((j_common_ptr) &cinfo); |
| 494 |
#endif |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
cinfo.in_color_space = JCS_RGB; |
| 503 |
jpeg_set_defaults(&cinfo); |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
file_index = parse_switches(&cinfo, argc, argv, 0, FALSE); |
| 512 |
|
| 513 |
#ifdef TWO_FILE_COMMANDLINE |
| 514 |
|
| 515 |
if (outfilename == NULL) { |
| 516 |
if (file_index != argc-2) { |
| 517 |
fprintf(stderr, "%s: must name one input and one output file\n", |
| 518 |
progname); |
| 519 |
usage(); |
| 520 |
} |
| 521 |
outfilename = argv[file_index+1]; |
| 522 |
} else { |
| 523 |
if (file_index != argc-1) { |
| 524 |
fprintf(stderr, "%s: must name one input and one output file\n", |
| 525 |
progname); |
| 526 |
usage(); |
| 527 |
} |
| 528 |
} |
| 529 |
#else |
| 530 |
|
| 531 |
if (file_index < argc-1) { |
| 532 |
fprintf(stderr, "%s: only one input file\n", progname); |
| 533 |
usage(); |
| 534 |
} |
| 535 |
#endif |
| 536 |
|
| 537 |
|
| 538 |
if (file_index < argc) { |
| 539 |
if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) { |
| 540 |
fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]); |
| 541 |
exit(EXIT_FAILURE); |
| 542 |
} |
| 543 |
} else { |
| 544 |
|
| 545 |
input_file = read_stdin(); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
if (outfilename != NULL) { |
| 550 |
if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) { |
| 551 |
fprintf(stderr, "%s: can't open %s\n", progname, outfilename); |
| 552 |
exit(EXIT_FAILURE); |
| 553 |
} |
| 554 |
} else { |
| 555 |
|
| 556 |
output_file = write_stdout(); |
| 557 |
} |
| 558 |
|
| 559 |
#ifdef PROGRESS_REPORT |
| 560 |
start_progress_monitor((j_common_ptr) &cinfo, &progress); |
| 561 |
#endif |
| 562 |
|
| 563 |
|
| 564 |
src_mgr = select_file_type(&cinfo, input_file); |
| 565 |
src_mgr->input_file = input_file; |
| 566 |
|
| 567 |
|
| 568 |
(*src_mgr->start_input) (&cinfo, src_mgr); |
| 569 |
|
| 570 |
|
| 571 |
jpeg_default_colorspace(&cinfo); |
| 572 |
|
| 573 |
|
| 574 |
file_index = parse_switches(&cinfo, argc, argv, 0, TRUE); |
| 575 |
|
| 576 |
|
| 577 |
jpeg_stdio_dest(&cinfo, output_file); |
| 578 |
|
| 579 |
|
| 580 |
jpeg_start_compress(&cinfo, TRUE); |
| 581 |
|
| 582 |
|
| 583 |
while (cinfo.next_scanline < cinfo.image_height) { |
| 584 |
num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr); |
| 585 |
(void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines); |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
(*src_mgr->finish_input) (&cinfo, src_mgr); |
| 590 |
jpeg_finish_compress(&cinfo); |
| 591 |
jpeg_destroy_compress(&cinfo); |
| 592 |
|
| 593 |
|
| 594 |
if (input_file != stdin) |
| 595 |
fclose(input_file); |
| 596 |
if (output_file != stdout) |
| 597 |
fclose(output_file); |
| 598 |
|
| 599 |
#ifdef PROGRESS_REPORT |
| 600 |
end_progress_monitor((j_common_ptr) &cinfo); |
| 601 |
#endif |
| 602 |
|
| 603 |
|
| 604 |
exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS); |
| 605 |
return 0; |
| 606 |
} |