cjpeg.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. Program cjpeg;
  2. { Original: cjpeg.c ; Copyright (C) 1991-1996, Thomas G. Lane. }
  3. { This file contains a command-line user interface for the JPEG compressor. }
  4. { Two different command line styles are permitted, depending on the
  5. compile-time switch TWO_FILE_COMMANDLINE:
  6. cjpeg [options] inputfile outputfile
  7. cjpeg [options] [inputfile]
  8. In the second style, output is always to standard output, which you'd
  9. normally redirect to a file or pipe to some other program. Input is
  10. either from a named file or from standard input (typically redirected).
  11. The second style is convenient on Unix but is unhelpful on systems that
  12. don't support pipes. Also, you MUST use the first style if your system
  13. doesn't do binary I/O to stdin/stdout.
  14. To simplify script writing, the "-outfile" switch is provided. The syntax
  15. cjpeg [options] -outfile outputfile inputfile
  16. works regardless of which command line style is used. }
  17. {$I jconfig.inc}
  18. {$undef PPM_SUPPORTED}
  19. uses
  20. jmorecfg,
  21. cdjpeg, { Common decls for cjpeg/djpeg applications }
  22. {jversion,} { for version message }
  23. jpeglib,
  24. jerror,
  25. jinclude, JDataDst,
  26. JcAPImin, JcAPIstd, JcParam,
  27. {$ifdef TARGA_SUPPORTED} rdtarga, {$endif}
  28. {$ifdef BMP_SUPPORTED} rdbmp, {$endif}
  29. {$ifdef EXT_SWITCH} rdswitch, {$endif}
  30. {cderror,}
  31. jdeferr;
  32. { This routine determines what format the input file is,
  33. and selects the appropriate input-reading module.
  34. To determine which family of input formats the file belongs to,
  35. we may look only at the first byte of the file, since C does not
  36. guarantee that more than one character can be pushed back with ungetc.
  37. Looking at additional bytes would require one of these approaches:
  38. 1) assume we can fseek() the input file (fails for piped input);
  39. 2) assume we can push back more than one character (works in
  40. some C implementations, but unportable);
  41. 3) provide our own buffering (breaks input readers that want to use
  42. stdio directly, such as the RLE library);
  43. or 4) don't put back the data, and modify the input_init methods to assume
  44. they start reading after the start of file (also breaks RLE library).
  45. #1 is attractive for MS-DOS but is untenable on Unix.
  46. The most portable solution for file types that can't be identified by their
  47. first byte is to make the user tell us what they are. This is also the
  48. only approach for "raw" file types that contain only arbitrary values.
  49. We presently apply this method for Targa files. Most of the time Targa
  50. files start with $00, so we recognize that case. Potentially, however,
  51. a Targa file could start with any byte value (byte 0 is the length of the
  52. seldom-used ID field), so we provide a switch to force Targa input mode. }
  53. var
  54. is_targa : boolean; { records user -targa switch }
  55. function GetFirstChar(cinfo : j_compress_ptr;
  56. fptr : fileptr) : char;
  57. var
  58. c : char;
  59. begin
  60. if JFREAD(fptr, @c, 1) <> 1 then
  61. ERREXIT(j_common_ptr(cinfo), JERR_INPUT_EMPTY);
  62. {$ifndef delphi_stream}
  63. Seek(fptr^, 0); { Nomssi: probably not portable }
  64. {$else}
  65. Fptr^.Seek(0,0);
  66. {$endif}
  67. if (IOresult <> 0) then
  68. ERREXIT(j_common_ptr(cinfo), JERR_UNGETC_FAILED);
  69. GetFirstChar := c;
  70. end;
  71. {LOCAL}
  72. function select_file_type (cinfo : j_compress_ptr;
  73. var infile : FILE) : cjpeg_source_ptr;
  74. var
  75. c : char;
  76. begin
  77. if (is_targa) then
  78. begin
  79. {$ifdef TARGA_SUPPORTED}
  80. select_file_type := jinit_read_targa(cinfo);
  81. exit;
  82. {$else}
  83. ERREXIT(j_common_ptr(cinfo), JERR_TGA_NOTCOMP);
  84. {$endif}
  85. end;
  86. c := GetFirstChar(cinfo, @infile);
  87. select_file_type := NIL; { suppress compiler warnings }
  88. case c of
  89. {$ifdef BMP_SUPPORTED}
  90. 'B': select_file_type := jinit_read_bmp(cinfo);
  91. {$endif}
  92. {$ifdef GIF_SUPPORTED}
  93. 'G': select_file_type := jinit_read_gif(cinfo);
  94. {$endif}
  95. {$ifdef PPM_SUPPORTED}
  96. 'P': select_file_type := jinit_read_ppm(cinfo);
  97. {$endif}
  98. {$ifdef RLE_SUPPORTED}
  99. 'R': select_file_type := jinit_read_rle(cinfo);
  100. {$endif}
  101. {$ifdef TARGA_SUPPORTED}
  102. char($00): select_file_type := jinit_read_targa(cinfo);
  103. {$endif}
  104. else
  105. ERREXIT(j_common_ptr(cinfo), JERR_UNKNOWN_FORMAT);
  106. end;
  107. end;
  108. { Argument-parsing code.
  109. The switch parser is designed to be useful with DOS-style command line
  110. syntax, ie, intermixed switches and file names, where only the switches
  111. to the left of a given file name affect processing of that file.
  112. The main program in this file doesn't actually use this capability... }
  113. var
  114. progname, { program name for error messages }
  115. outfilename : string[79]; { for -outfile switch }
  116. {LOCAL}
  117. procedure usage;
  118. { complain about bad command line }
  119. begin
  120. Write(output, 'usage: ', progname, ' [switches] ');
  121. {$ifdef TWO_FILE_COMMANDLINE}
  122. WriteLn(output, 'inputfile outputfile');
  123. {$else}
  124. WriteLn(output, '[inputfile]');
  125. {$endif}
  126. WriteLn(output, 'Switches (names may be abbreviated):');
  127. WriteLn(output, ' -quality N Compression quality (0..100; 5-95 is useful range)');
  128. WriteLn(output, ' -grayscale Create monochrome JPEG file');
  129. {$ifdef ENTROPY_OPT_SUPPORTED}
  130. WriteLn(output, ' -optimize Optimize Huffman table (smaller file, but slow compression)');
  131. {$endif}
  132. {$ifdef C_PROGRESSIVE_SUPPORTED}
  133. WriteLn(output, ' -progressive Create progressive JPEG file');
  134. {$endif}
  135. {$ifdef TARGA_SUPPORTED}
  136. WriteLn(output, ' -targa Input file is Targa format (usually not needed)');
  137. {$endif}
  138. WriteLn(output, 'Switches for advanced users:');
  139. {$ifdef DCT_ISLOW_SUPPORTED}
  140. if (JDCT_DEFAULT = JDCT_ISLOW) then
  141. WriteLn(output, ' -dct int Use integer DCT method (default)')
  142. else
  143. WriteLn(output, ' -dct int Use integer DCT method');
  144. {$endif}
  145. {$ifdef DCT_IFAST_SUPPORTED}
  146. if (JDCT_DEFAULT = JDCT_IFAST) then
  147. WriteLn(output, ' -dct fast Use fast integer DCT (less accurate) (default)')
  148. else
  149. WriteLn(output, ' -dct fast Use fast integer DCT (less accurate)');
  150. {$endif}
  151. {$ifdef DCT_FLOAT_SUPPORTED}
  152. if (JDCT_DEFAULT = JDCT_FLOAT) then
  153. WriteLn(output, ' -dct float Use floating-point DCT method (default)')
  154. else
  155. WriteLn(output, ' -dct float Use floating-point DCT method');
  156. {$endif}
  157. WriteLn(output, ' -restart N Set restart interval in rows, or in blocks with B');
  158. {$ifdef INPUT_SMOOTHING_SUPPORTED}
  159. WriteLn(output, ' -smooth N Smooth dithered input (N=1..100 is strength)');
  160. {$endif}
  161. WriteLn(output, ' -maxmemory N Maximum memory to use (in kbytes)');
  162. WriteLn(output, ' -outfile name Specify name for output file');
  163. WriteLn(output, ' -verbose or -debug Emit debug output');
  164. {$IFDEF EXT_SWITCH}
  165. WriteLn(output, 'Switches for wizards:');
  166. {$ifdef C_ARITH_CODING_SUPPORTED}
  167. WriteLn(output, ' -arithmetic Use arithmetic coding');
  168. {$endif}
  169. WriteLn(output, ' -baseline Force baseline output');
  170. WriteLn(output, ' -qtables file Use quantization tables given in file');
  171. WriteLn(output, ' -qslots N[,...] Set component quantization tables');
  172. WriteLn(output, ' -sample HxV[,...] Set component sampling factors');
  173. {$ifdef C_MULTISCAN_FILES_SUPPORTED}
  174. WriteLn(output, ' -scans file Create multi-scan JPEG per script file');
  175. {$endif}
  176. {$ENDIF}
  177. Halt(EXIT_FAILURE);
  178. end;
  179. {LOCAL}
  180. function parse_switches (cinfo : j_compress_ptr;
  181. last_file_arg_seen : int;
  182. for_real : boolean) : int;
  183. { Parse optional switches.
  184. Returns argv[] index of first file-name argument (== argc if none).
  185. Any file names with indexes <= last_file_arg_seen are ignored;
  186. they have presumably been processed in a previous iteration.
  187. (Pass 0 for last_file_arg_seen on the first or only iteration.)
  188. for_real is FALSE on the first (dummy) pass; we may skip any expensive
  189. processing. }
  190. var
  191. argn,
  192. argc : int;
  193. arg : string;
  194. var
  195. value : int;
  196. code : integer;
  197. var
  198. quality : int; { -quality parameter }
  199. q_scale_factor : int; { scaling percentage for -qtables }
  200. force_baseline : boolean;
  201. simple_progressive : boolean;
  202. qtablefile, { saves -qtables filename if any }
  203. qslotsarg, { saves -qslots parm if any }
  204. samplearg, { saves -sample parm if any }
  205. scansarg : string; { saves -scans parm if any }
  206. var
  207. lval : long;
  208. ch : char;
  209. const
  210. printed_version : boolean = FALSE;
  211. begin
  212. qtablefile := '';
  213. qslotsarg := '';
  214. samplearg := '';
  215. scansarg := '';
  216. { Set up default JPEG parameters. }
  217. { Note that default -quality level need not, and does not,
  218. match the default scaling for an explicit -qtables argument. }
  219. quality := 75; { default -quality value }
  220. q_scale_factor := 100; { default to no scaling for -qtables }
  221. force_baseline := FALSE; { by default, allow 16-bit quantizers }
  222. simple_progressive := FALSE;
  223. is_targa := FALSE;
  224. outfilename := '';
  225. cinfo^.err^.trace_level := 0;
  226. { Scan command line options, adjust parameters }
  227. argn := 0;
  228. argc := ParamCount;
  229. while argn < argc do
  230. begin
  231. Inc(argn);
  232. arg := ParamStr(argn);
  233. if (arg[1] <> '-') then
  234. begin
  235. { Not a switch, must be a file name argument }
  236. if (argn <= last_file_arg_seen) then
  237. begin
  238. outfilename := ''; { -outfile applies to just one input file }
  239. continue; { ignore this name if previously processed }
  240. end;
  241. break; { else done parsing switches }
  242. end;
  243. {Inc(arg); - advance past switch marker character }
  244. if (keymatch(arg, '-arithmetic', 2)) then
  245. begin
  246. { Use arithmetic coding. }
  247. {$ifdef C_ARITH_CODING_SUPPORTED}
  248. cinfo^.arith_code := TRUE;
  249. {$else}
  250. WriteLn(output, progname, ': sorry, arithmetic coding not supported');
  251. Halt(EXIT_FAILURE);
  252. {$endif}
  253. end
  254. else
  255. if (keymatch(arg, '-baseline', 2)) then
  256. begin
  257. { Force baseline output (8-bit quantizer values). }
  258. force_baseline := TRUE;
  259. end
  260. else
  261. if (keymatch(arg, '-dct', 3)) then
  262. begin
  263. { Select DCT algorithm. }
  264. Inc(argn);
  265. if (argn >= argc) then { advance to next argument }
  266. usage;
  267. if (keymatch(ParamStr(argn), 'int', 1)) then
  268. begin
  269. cinfo^.dct_method := JDCT_ISLOW;
  270. end
  271. else
  272. if (keymatch(ParamStr(argn), 'fast', 2)) then
  273. begin
  274. cinfo^.dct_method := JDCT_IFAST;
  275. end
  276. else
  277. if (keymatch(ParamStr(argn), 'float', 2)) then
  278. begin
  279. cinfo^.dct_method := JDCT_FLOAT;
  280. end
  281. else
  282. usage;
  283. end
  284. else
  285. if keymatch(arg, '-debug', 2) or keymatch(arg, '-verbose', 2) then
  286. begin
  287. { Enable debug printouts. }
  288. { On first -d, print version identification }
  289. if (not printed_version) then
  290. begin
  291. WriteLn(output, 'Independent JPEG Group''s CJPEG, version ', JVERSION);
  292. WriteLn(output, JCOPYRIGHT);
  293. WriteLn(output, JNOTICE);
  294. printed_version := TRUE;
  295. end;
  296. Inc(cinfo^.err^.trace_level);
  297. end
  298. else
  299. if (keymatch(arg, '-grayscale', 3)) or (keymatch(arg, '-greyscale',3)) then
  300. begin
  301. { Force a monochrome JPEG file to be generated. }
  302. jpeg_set_colorspace(cinfo, JCS_GRAYSCALE);
  303. end
  304. else
  305. if (keymatch(arg, '-maxmemory', 4)) then
  306. begin
  307. ch := 'x';
  308. Inc(argn);
  309. if (argn >= argc) then { advance to next argument }
  310. usage;
  311. arg := ParamStr(argn);
  312. if (length(arg) > 1) and (arg[length(arg)] in ['m','M']) then
  313. begin
  314. ch := arg[length(arg)];
  315. arg := Copy(arg, 1, Length(arg)-1);
  316. end;
  317. Val(arg, lval, code);
  318. if (code <> 1) then
  319. usage;
  320. if (ch = 'm') or (ch = 'M') then
  321. lval := lval * long(1000);
  322. cinfo^.mem^.max_memory_to_use := lval * long(1000);
  323. end
  324. else
  325. if (keymatch(arg, '-optimize', 2)) or (keymatch(arg, '-optimise', 2)) then
  326. begin
  327. { Enable entropy parm optimization. }
  328. {$ifdef ENTROPY_OPT_SUPPORTED}
  329. cinfo^.optimize_coding := TRUE;
  330. {$else}
  331. WriteLn(output, progname, ': sorry, entropy optimization was not compiled');
  332. exit(EXIT_FAILURE);
  333. {$endif}
  334. end
  335. else
  336. if (keymatch(arg, '-outfile', 5)) then
  337. begin
  338. { Set output file name. }
  339. Inc(argn);
  340. if (argn >= argc) then { advance to next argument }
  341. usage;
  342. outfilename := ParamStr(argn); { save it away for later use }
  343. end
  344. else
  345. if (keymatch(arg, '-progressive', 2)) then
  346. begin
  347. { Select simple progressive mode. }
  348. {$ifdef C_PROGRESSIVE_SUPPORTED}
  349. simple_progressive := TRUE;
  350. { We must postpone execution until num_components is known. }
  351. {$else}
  352. WriteLn(output, progname, ': sorry, progressive output was not compiled');
  353. Halt(EXIT_FAILURE);
  354. {$endif}
  355. end
  356. else
  357. if (keymatch(arg, '-quality', 2)) then
  358. begin
  359. { Quality factor (quantization table scaling factor). }
  360. Inc(argn);
  361. if (argn >= argc) then { advance to next argument }
  362. usage;
  363. Val(ParamStr(argn), quality, code);
  364. if code <> 0 then
  365. usage;
  366. { Change scale factor in case -qtables is present. }
  367. q_scale_factor := jpeg_quality_scaling(quality);
  368. end
  369. else
  370. if (keymatch(arg, '-qslots', 3)) then
  371. begin
  372. { Quantization table slot numbers. }
  373. Inc(argn);
  374. if (argn >= argc) then { advance to next argument }
  375. usage;
  376. qslotsarg := ParamStr(argn);
  377. { Must delay setting qslots until after we have processed any
  378. colorspace-determining switches, since jpeg_set_colorspace sets
  379. default quant table numbers. }
  380. end
  381. else
  382. if (keymatch(arg, '-qtables', 3)) then
  383. begin
  384. { Quantization tables fetched from file. }
  385. Inc(argn);
  386. if (argn >= argc) then { advance to next argument }
  387. usage;
  388. qtablefile := ParamStr(argn);
  389. { We postpone actually reading the file in case -quality comes later. }
  390. end
  391. else
  392. if (keymatch(arg, '-restart', 2)) then
  393. begin
  394. { Restart interval in MCU rows (or in MCUs with 'b'). }
  395. ch := 'x';
  396. Inc(argn);
  397. if (argn >= argc) then { advance to next argument }
  398. usage;
  399. arg := ParamStr(argn);
  400. if (length(arg) > 1) and (arg[length(arg)] in ['b','B']) then
  401. begin
  402. ch := arg[length(arg)];
  403. arg := Copy(arg, 1, Length(arg)-1);
  404. end;
  405. Val(arg, lval, Code);
  406. if (code <> 1) then
  407. usage;
  408. if (lval < 0) or (lval > long(65535)) then
  409. usage;
  410. if (ch = 'b') or (ch = 'B') then
  411. begin
  412. cinfo^.restart_interval := uInt (lval);
  413. cinfo^.restart_in_rows := 0; { else prior '-restart n' overrides me }
  414. end
  415. else
  416. begin
  417. cinfo^.restart_in_rows := int (lval);
  418. { restart_interval will be computed during startup }
  419. end;
  420. end
  421. else
  422. if (keymatch(arg, '-sample', 3)) then
  423. begin
  424. { Set sampling factors. }
  425. Inc(argn);
  426. if (argn >= argc) then { advance to next argument }
  427. usage;
  428. samplearg := ParamStr(argn);
  429. { Must delay setting sample factors until after we have processed any
  430. colorspace-determining switches, since jpeg_set_colorspace sets
  431. default sampling factors. }
  432. end
  433. else
  434. if (keymatch(arg, '-scans', 3)) then
  435. begin
  436. { Set scan script. }
  437. {$ifdef C_MULTISCAN_FILES_SUPPORTED}
  438. Inc(argn);
  439. if (argn >= argc) then { advance to next argument }
  440. usage;
  441. scansarg := ParamStr(argn);
  442. { We must postpone reading the file in case -progressive appears. }
  443. {$else}
  444. WriteLn(output, progname, ': sorry, multi-scan output was not compiled');
  445. Halt(EXIT_FAILURE);
  446. {$endif}
  447. end
  448. else
  449. if (keymatch(arg, '-smooth', 3)) then
  450. begin
  451. { Set input smoothing factor. }
  452. Inc(argn);
  453. if (argn >= argc) then { advance to next argument }
  454. usage;
  455. Val(ParamStr(argn), value, code);
  456. if (value < 0) or (value > 100)
  457. or (code <> 0) then
  458. usage;
  459. cinfo^.smoothing_factor := value;
  460. end
  461. else
  462. if (keymatch(arg, '-targa', 2)) then
  463. begin
  464. { Input file is Targa format. }
  465. is_targa := TRUE;
  466. end
  467. else
  468. begin
  469. usage; { bogus switch }
  470. end;
  471. end;
  472. { Post-switch-scanning cleanup }
  473. if (for_real) then
  474. begin
  475. { Set quantization tables for selected quality. }
  476. { Some or all may be overridden if -qtables is present. }
  477. jpeg_set_quality(cinfo, quality, force_baseline);
  478. {$IFDEF EXT_SWITCH}
  479. if (qtablefile <> '') then { process -qtables if it was present }
  480. if (not read_quant_tables(cinfo, qtablefile,
  481. q_scale_factor, force_baseline)) then
  482. usage;
  483. if (qslotsarg <> '') then { process -qslots if it was present }
  484. if (not set_quant_slots(cinfo, qslotsarg)) then
  485. usage;
  486. if (samplearg <> '') then { process -sample if it was present }
  487. if (not set_sample_factors(cinfo, samplearg)) then
  488. usage;
  489. {$ENDIF}
  490. {$ifdef C_PROGRESSIVE_SUPPORTED}
  491. if (simple_progressive) then { process -progressive; -scans can override }
  492. jpeg_simple_progression(cinfo);
  493. {$endif}
  494. {$IFDEF EXT_SWITCH}
  495. {$ifdef C_MULTISCAN_FILES_SUPPORTED}
  496. if (scansarg <> '') then { process -scans if it was present }
  497. if (not read_scan_script(cinfo, scansarg)) then
  498. usage;
  499. {$endif}
  500. {$ENDIF}
  501. end;
  502. parse_switches := argn; { return index of next arg (file name) }
  503. end;
  504. { The main program. }
  505. var
  506. cinfo : jpeg_compress_struct;
  507. jerr : jpeg_error_mgr;
  508. {$ifdef PROGRESS_REPORT}
  509. progress : cdjpeg_progress_mgr;
  510. {$endif}
  511. file_index : int;
  512. src_mgr : cjpeg_source_ptr;
  513. input_file : FILE;
  514. output_file : FILE;
  515. num_scanlines : JDIMENSION;
  516. var
  517. argc : int;
  518. begin
  519. argc := ParamCount;
  520. progname := ParamStr(0);
  521. { Initialize the JPEG compression object with default error handling. }
  522. cinfo.err := jpeg_std_error(jerr);
  523. jpeg_create_compress(@cinfo);
  524. { Add some application-specific error messages (from cderror.h) }
  525. {jerr.addon_message_table := cdjpeg_message_table;}
  526. jerr.first_addon_message := JMSG_FIRSTADDONCODE;
  527. jerr.last_addon_message := JMSG_LASTADDONCODE;
  528. { Now safe to enable signal catcher. }
  529. {$ifdef NEED_SIGNAL_CATCHER}
  530. enable_signal_catcher(j_common_ptr ( @cinfo);
  531. {$endif}
  532. { Initialize JPEG parameters.
  533. Much of this may be overridden later.
  534. In particular, we don't yet know the input file's color space,
  535. but we need to provide some value for jpeg_set_defaults() to work. }
  536. cinfo.in_color_space := JCS_RGB; { arbitrary guess }
  537. jpeg_set_defaults(@cinfo);
  538. { Scan command line to find file names.
  539. It is convenient to use just one switch-parsing routine, but the switch
  540. values read here are ignored; we will rescan the switches after opening
  541. the input file. }
  542. file_index := parse_switches(@cinfo, 0, FALSE);
  543. {$ifdef TWO_FILE_COMMANDLINE}
  544. { Must have either -outfile switch or explicit output file name }
  545. if (outfilename = '') then
  546. begin
  547. if (file_index <> argc-2+1) then
  548. begin
  549. WriteLn(output, progname, ': must name one input and one output file');
  550. usage;
  551. end;
  552. outfilename := ParamStr(file_index+1);
  553. end
  554. else
  555. begin
  556. if (file_index <> argc-1) then
  557. begin
  558. WriteLn(output, progname, ': must name one input and one output file');
  559. usage;
  560. end;
  561. end;
  562. {$else}
  563. { Unix style: expect zero or one file name }
  564. if (file_index < argc-1) then
  565. begin
  566. WriteLn(output, progname, ': only one input file');
  567. usage;
  568. end;
  569. {$endif} { TWO_FILE_COMMANDLINE }
  570. { Open the input file. }
  571. if (file_index < argc) then
  572. begin
  573. Assign(input_file, ParamStr(file_index));
  574. {$push}{$I-}
  575. Reset(input_file, 1);
  576. {$pop}
  577. if (IOresult <> 0) then
  578. begin
  579. WriteLn(output, progname, ': can''t open ', ParamStr(file_index));
  580. Halt(EXIT_FAILURE);
  581. end;
  582. end
  583. else
  584. begin
  585. WriteLn(output, progname, ': no input file');
  586. Halt(EXIT_FAILURE);
  587. end;
  588. { Open the output file. }
  589. if (outfilename <> '') then
  590. begin
  591. Assign(output_file, outfilename);
  592. {$push}{$I-}
  593. Reset(output_file, 1);
  594. {$pop}
  595. if (IOresult = 0) then
  596. begin
  597. WriteLn(output, outfilename, ': already exists.');
  598. close(output_file);
  599. Halt(EXIT_FAILURE);
  600. end;
  601. {$push}{$I-}
  602. ReWrite(output_file, 1);
  603. {$pop}
  604. if (IOresult <> 0) then
  605. begin
  606. WriteLn(output, progname, ': can''t create ', outfilename);
  607. Halt(EXIT_FAILURE);
  608. end;
  609. end
  610. else
  611. begin
  612. WriteLn(output, progname, ': no output file');
  613. Halt(EXIT_FAILURE);
  614. end;
  615. {$ifdef PROGRESS_REPORT}
  616. start_progress_monitor(j_common_ptr (@cinfo), @progress);
  617. {$endif}
  618. { Figure out the input file format, and set up to read it. }
  619. src_mgr := select_file_type(@cinfo, input_file);
  620. src_mgr^.input_file := @input_file;
  621. { Read the input file header to obtain file size & colorspace. }
  622. src_mgr^.start_input (@cinfo, src_mgr);
  623. { Now that we know input colorspace, fix colorspace-dependent defaults }
  624. jpeg_default_colorspace(@cinfo);
  625. { Adjust default compression parameters by re-parsing the options }
  626. file_index := parse_switches(@cinfo, 0, TRUE);
  627. { Specify data destination for compression }
  628. jpeg_stdio_dest(@cinfo, @output_file);
  629. { Start compressor }
  630. jpeg_start_compress(@cinfo, TRUE);
  631. { Process data }
  632. while (cinfo.next_scanline < cinfo.image_height) do
  633. begin
  634. num_scanlines := src_mgr^.get_pixel_rows (@cinfo, src_mgr);
  635. {void} jpeg_write_scanlines(@cinfo, src_mgr^.buffer, num_scanlines);
  636. end;
  637. { Finish compression and release memory }
  638. src_mgr^.finish_input (@cinfo, src_mgr);
  639. jpeg_finish_compress(@cinfo);
  640. jpeg_destroy_compress(@cinfo);
  641. { Close files, if we opened them }
  642. close(input_file);
  643. close(output_file);
  644. {$ifdef PROGRESS_REPORT}
  645. end_progress_monitor(j_common_ptr (@cinfo));
  646. {$endif}
  647. { All done. }
  648. if jerr.num_warnings <> 0 then
  649. Halt(EXIT_WARNING)
  650. else
  651. Halt(EXIT_SUCCESS);
  652. end.