00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "cdjpeg.h"
00014 #include "jversion.h"
00015
00016 #ifdef USE_CCOMMAND
00017 #ifdef __MWERKS__
00018 #include <SIOUX.h>
00019 #endif
00020 #ifdef THINK_C
00021 #include <console.h>
00022 #endif
00023 #endif
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 static const char * progname;
00036 static char * outfilename;
00037
00038
00039 LOCAL void
00040 usage (void)
00041
00042 {
00043 fprintf(stderr, "usage: %s [switches] ", progname);
00044 #ifdef TWO_FILE_COMMANDLINE
00045 fprintf(stderr, "inputfile outputfile\n");
00046 #else
00047 fprintf(stderr, "[inputfile]\n");
00048 #endif
00049
00050 fprintf(stderr, "Switches (names may be abbreviated):\n");
00051 #ifdef ENTROPY_OPT_SUPPORTED
00052 fprintf(stderr, " -optimize Optimize Huffman table (smaller file, but slow compression)\n");
00053 #endif
00054 #ifdef C_PROGRESSIVE_SUPPORTED
00055 fprintf(stderr, " -progressive Create progressive JPEG file\n");
00056 #endif
00057 fprintf(stderr, "Switches for advanced users:\n");
00058 fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
00059 fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
00060 fprintf(stderr, " -outfile name Specify name for output file\n");
00061 fprintf(stderr, " -verbose or -debug Emit debug output\n");
00062 fprintf(stderr, "Switches for wizards:\n");
00063 #ifdef C_ARITH_CODING_SUPPORTED
00064 fprintf(stderr, " -arithmetic Use arithmetic coding\n");
00065 #endif
00066 #ifdef C_MULTISCAN_FILES_SUPPORTED
00067 fprintf(stderr, " -scans file Create multi-scan JPEG per script file\n");
00068 #endif
00069 exit(EXIT_FAILURE);
00070 }
00071
00072
00073 LOCAL int
00074 parse_switches (j_compress_ptr cinfo, int argc, char **argv,
00075 int last_file_arg_seen, boolean for_real)
00076
00077
00078
00079
00080
00081
00082
00083
00084 {
00085 int argn;
00086 char * arg;
00087 boolean simple_progressive;
00088 char * scansarg = NULL;
00089
00090
00091 simple_progressive = FALSE;
00092 outfilename = NULL;
00093 cinfo->err->trace_level = 0;
00094
00095
00096
00097 for (argn = 1; argn < argc; argn++) {
00098 arg = argv[argn];
00099 if (*arg != '-') {
00100
00101 if (argn <= last_file_arg_seen) {
00102 outfilename = NULL;
00103 continue;
00104 }
00105 break;
00106 }
00107 arg++;
00108
00109 if (keymatch(arg, "arithmetic", 1)) {
00110
00111 #ifdef C_ARITH_CODING_SUPPORTED
00112 cinfo->arith_code = TRUE;
00113 #else
00114 fprintf(stderr, "%s: sorry, arithmetic coding not supported\n",
00115 progname);
00116 exit(EXIT_FAILURE);
00117 #endif
00118
00119 } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
00120
00121
00122 static boolean printed_version = FALSE;
00123
00124 if (! printed_version) {
00125 fprintf(stderr, "Independent JPEG Group's JPEGTRAN, version %s\n%s\n",
00126 JVERSION, JCOPYRIGHT);
00127 printed_version = TRUE;
00128 }
00129 cinfo->err->trace_level++;
00130
00131 } else if (keymatch(arg, "maxmemory", 3)) {
00132
00133 long lval;
00134 char ch = 'x';
00135
00136 if (++argn >= argc)
00137 usage();
00138 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
00139 usage();
00140 if (ch == 'm' || ch == 'M')
00141 lval *= 1000L;
00142 cinfo->mem->max_memory_to_use = lval * 1000L;
00143
00144 } else if (keymatch(arg, "optimize", 1) || keymatch(arg, "optimise", 1)) {
00145
00146 #ifdef ENTROPY_OPT_SUPPORTED
00147 cinfo->optimize_coding = TRUE;
00148 #else
00149 fprintf(stderr, "%s: sorry, entropy optimization was not compiled\n",
00150 progname);
00151 exit(EXIT_FAILURE);
00152 #endif
00153
00154 } else if (keymatch(arg, "outfile", 4)) {
00155
00156 if (++argn >= argc)
00157 usage();
00158 outfilename = argv[argn];
00159
00160 } else if (keymatch(arg, "progressive", 1)) {
00161
00162 #ifdef C_PROGRESSIVE_SUPPORTED
00163 simple_progressive = TRUE;
00164
00165 #else
00166 fprintf(stderr, "%s: sorry, progressive output was not compiled\n",
00167 progname);
00168 exit(EXIT_FAILURE);
00169 #endif
00170
00171 } else if (keymatch(arg, "restart", 1)) {
00172
00173 long lval;
00174 char ch = 'x';
00175
00176 if (++argn >= argc)
00177 usage();
00178 if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
00179 usage();
00180 if (lval < 0 || lval > 65535L)
00181 usage();
00182 if (ch == 'b' || ch == 'B') {
00183 cinfo->restart_interval = (unsigned int) lval;
00184 cinfo->restart_in_rows = 0;
00185 } else {
00186 cinfo->restart_in_rows = (int) lval;
00187
00188 }
00189
00190 } else if (keymatch(arg, "scans", 2)) {
00191
00192 #ifdef C_MULTISCAN_FILES_SUPPORTED
00193 if (++argn >= argc)
00194 usage();
00195 scansarg = argv[argn];
00196
00197 #else
00198 fprintf(stderr, "%s: sorry, multi-scan output was not compiled\n",
00199 progname);
00200 exit(EXIT_FAILURE);
00201 #endif
00202
00203 } else {
00204 usage();
00205 }
00206 }
00207
00208
00209
00210 if (for_real) {
00211
00212 #ifdef C_PROGRESSIVE_SUPPORTED
00213 if (simple_progressive)
00214 jpeg_simple_progression(cinfo);
00215 #endif
00216
00217 #ifdef C_MULTISCAN_FILES_SUPPORTED
00218 if (scansarg != NULL)
00219 if (! read_scan_script(cinfo, scansarg))
00220 usage();
00221 #endif
00222 }
00223
00224 return argn;
00225 }
00226
00227
00228
00229
00230
00231
00232 GLOBAL int
00233 main (int argc, char **argv)
00234 {
00235 struct jpeg_decompress_struct srcinfo;
00236 struct jpeg_compress_struct dstinfo;
00237 struct jpeg_error_mgr jsrcerr, jdsterr;
00238 #ifdef PROGRESS_REPORT
00239 struct cdjpeg_progress_mgr progress;
00240 #endif
00241 jvirt_barray_ptr * coef_arrays;
00242 int file_index;
00243 FILE * input_file;
00244 FILE * output_file;
00245
00246
00247 #ifdef USE_CCOMMAND
00248 argc = ccommand(&argv);
00249 #endif
00250
00251 progname = argv[0];
00252 if (progname == NULL || progname[0] == 0)
00253 progname = "jpegtran";
00254
00255
00256 srcinfo.err = jpeg_std_error(&jsrcerr);
00257 jpeg_create_decompress(&srcinfo);
00258
00259 dstinfo.err = jpeg_std_error(&jdsterr);
00260 jpeg_create_compress(&dstinfo);
00261
00262
00263
00264
00265 #ifdef NEED_SIGNAL_CATCHER
00266 enable_signal_catcher((j_common_ptr) &srcinfo);
00267 #endif
00268
00269
00270
00271
00272
00273
00274
00275 file_index = parse_switches(&dstinfo, argc, argv, 0, FALSE);
00276 jsrcerr.trace_level = jdsterr.trace_level;
00277
00278 #ifdef TWO_FILE_COMMANDLINE
00279
00280 if (outfilename == NULL) {
00281 if (file_index != argc-2) {
00282 fprintf(stderr, "%s: must name one input and one output file\n",
00283 progname);
00284 usage();
00285 }
00286 outfilename = argv[file_index+1];
00287 } else {
00288 if (file_index != argc-1) {
00289 fprintf(stderr, "%s: must name one input and one output file\n",
00290 progname);
00291 usage();
00292 }
00293 }
00294 #else
00295
00296 if (file_index < argc-1) {
00297 fprintf(stderr, "%s: only one input file\n", progname);
00298 usage();
00299 }
00300 #endif
00301
00302
00303 if (file_index < argc) {
00304 if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
00305 fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
00306 exit(EXIT_FAILURE);
00307 }
00308 } else {
00309
00310 input_file = read_stdin();
00311 }
00312
00313
00314 if (outfilename != NULL) {
00315 if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
00316 fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
00317 exit(EXIT_FAILURE);
00318 }
00319 } else {
00320
00321 output_file = write_stdout();
00322 }
00323
00324 #ifdef PROGRESS_REPORT
00325 start_progress_monitor((j_common_ptr) &dstinfo, &progress);
00326 #endif
00327
00328
00329 jpeg_stdio_src(&srcinfo, input_file);
00330
00331
00332 (void) jpeg_read_header(&srcinfo, TRUE);
00333
00334
00335 coef_arrays = jpeg_read_coefficients(&srcinfo);
00336
00337
00338 jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
00339
00340
00341 file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);
00342
00343
00344 jpeg_stdio_dest(&dstinfo, output_file);
00345
00346
00347 jpeg_write_coefficients(&dstinfo, coef_arrays);
00348
00349
00350
00351
00352 jpeg_finish_compress(&dstinfo);
00353 jpeg_destroy_compress(&dstinfo);
00354 (void) jpeg_finish_decompress(&srcinfo);
00355 jpeg_destroy_decompress(&srcinfo);
00356
00357
00358 if (input_file != stdin)
00359 fclose(input_file);
00360 if (output_file != stdout)
00361 fclose(output_file);
00362
00363 #ifdef PROGRESS_REPORT
00364 end_progress_monitor((j_common_ptr) &dstinfo);
00365 #endif
00366
00367
00368 exit(jsrcerr.num_warnings + jdsterr.num_warnings ?EXIT_WARNING:EXIT_SUCCESS);
00369 return 0;
00370 }