cdjpeg.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. Unit CdJpeg;
  2. { OriginaL : cdjpeg.h+cdjpeg.c ; Copyright (C) 1994-1996, Thomas G. Lane.
  3. This file contains common support routines used by the IJG application
  4. programs (cjpeg, djpeg, jpegtran).
  5. This file contains common declarations for the sample applications
  6. cjpeg and djpeg. It is NOT used by the core JPEG library. }
  7. {$define JPEG_CJPEG_DJPEG} { define proper options in jconfig.h }
  8. {$define JPEG_INTERNAL_OPTIONS} { cjpeg.c,djpeg.c need to see xxx_SUPPORTED }
  9. interface
  10. {$I jconfig.inc}
  11. uses
  12. jmorecfg, jinclude, jpeglib,
  13. jdeferr,
  14. {cderror,} { get application-specific error codes }
  15. jerror; { get library error codes too }
  16. const
  17. EXIT_SUCCESS = 0;
  18. EXIT_FAILURE = 1;
  19. EXIT_WARNING = 2;
  20. type { Nomssi }
  21. BGRptr = ^BGRtype;
  22. BGRtype = packed record
  23. b,g,r : byte;
  24. end;
  25. type { Nomssi }
  26. RGBptr = ^RGBtype;
  27. RGBtype = packed record
  28. r,g,b : JSAMPLE;
  29. end;
  30. { Object interface for cjpeg's source file decoding modules }
  31. type
  32. cjpeg_source_ptr = ^cjpeg_source_struct;
  33. cjpeg_source_struct = record
  34. start_input : procedure (cinfo : j_compress_ptr;
  35. sinfo : cjpeg_source_ptr);
  36. get_pixel_rows : function (cinfo : j_compress_ptr;
  37. sinfo : cjpeg_source_ptr) : JDIMENSION;
  38. finish_input : procedure (cinfo : j_compress_ptr;
  39. sinfo : cjpeg_source_ptr);
  40. input_file : FILEptr;
  41. buffer : JSAMPARRAY;
  42. buffer_height : JDIMENSION;
  43. end;
  44. { Object interface for djpeg's output file encoding modules }
  45. type
  46. djpeg_dest_ptr = ^djpeg_dest_struct;
  47. djpeg_dest_struct = record
  48. { start_output is called after jpeg_start_decompress finishes.
  49. The color map will be ready at this time, if one is needed. }
  50. start_output : procedure (cinfo : j_decompress_ptr;
  51. dinfo : djpeg_dest_ptr);
  52. { Emit the specified number of pixel rows from the buffer. }
  53. put_pixel_rows : procedure (cinfo : j_decompress_ptr;
  54. dinfo : djpeg_dest_ptr;
  55. rows_supplied : JDIMENSION);
  56. { Finish up at the end of the image. }
  57. finish_output : procedure (cinfo : j_decompress_ptr;
  58. dinfo : djpeg_dest_ptr);
  59. { Target file spec; filled in by djpeg.c after object is created. }
  60. output_file : FILEptr;
  61. { Output pixel-row buffer. Created by module init or start_output.
  62. Width is cinfo^.output_width * cinfo^.output_components;
  63. height is buffer_height. }
  64. buffer : JSAMPARRAY;
  65. buffer_height : JDIMENSION;
  66. end;
  67. { cjpeg/djpeg may need to perform extra passes to convert to or from
  68. the source/destination file format. The JPEG library does not know
  69. about these passes, but we'd like them to be counted by the progress
  70. monitor. We use an expanded progress monitor object to hold the
  71. additional pass count. }
  72. type
  73. cd_progress_ptr = ^cdjpeg_progress_mgr;
  74. cdjpeg_progress_mgr = record
  75. pub : jpeg_progress_mgr; { fields known to JPEG library }
  76. completed_extra_passes : int; { extra passes completed }
  77. total_extra_passes : int; { total extra }
  78. { last printed percentage stored here to avoid multiple printouts }
  79. percent_done : int;
  80. end;
  81. {GLOBAL}
  82. procedure enable_signal_catcher (cinfo : j_common_ptr);
  83. { Case-insensitive matching of possibly-abbreviated keyword switches.
  84. keyword is the constant keyword (must be lower case already),
  85. minchars is length of minimum legal abbreviation. }
  86. {GLOBAL}
  87. function keymatch (arg : string;
  88. const keyword : string;
  89. minchars : int) : boolean;
  90. {$ifdef PROGRESS_REPORT}
  91. {GLOBAL}
  92. procedure start_progress_monitor (cinfo : j_common_ptr;
  93. progress : cd_progress_ptr);
  94. {GLOBAL}
  95. procedure end_progress_monitor (cinfo : j_common_ptr);
  96. {$endif}
  97. implementation
  98. {GLOBAL}
  99. procedure enable_signal_catcher (cinfo : j_common_ptr);
  100. begin
  101. RunError(255); { not translated - Jacques Nomssi }
  102. end;
  103. { Optional progress monitor: display a percent-done figure on stderr. }
  104. {$ifdef PROGRESS_REPORT}
  105. {METHODDEF}
  106. procedure progress_monitor (cinfo : j_common_ptr); far;
  107. var
  108. prog : cd_progress_ptr;
  109. total_passes : int;
  110. percent_done : int;
  111. begin
  112. prog := cd_progress_ptr (cinfo^.progress);
  113. total_passes := prog^.pub.total_passes + prog^.total_extra_passes;
  114. percent_done := int (prog^.pub.pass_counter*Long(100) div prog^.pub.pass_limit);
  115. if (percent_done <> prog^.percent_done) then
  116. begin
  117. prog^.percent_done := percent_done;
  118. if (total_passes > 1) then
  119. Write(output, #13'Pass ',
  120. prog^.pub.completed_passes + prog^.completed_extra_passes + 1,
  121. '/',total_passes,': ',percent_done:3,'% ')
  122. else
  123. Write(#13' ', percent_done,'% ');
  124. {fflush(stderr);}
  125. end;
  126. end;
  127. {GLOBAL}
  128. procedure start_progress_monitor (cinfo : j_common_ptr;
  129. progress : cd_progress_ptr);
  130. begin
  131. { Enable progress display, unless trace output is on }
  132. if (cinfo^.err^.trace_level = 0) then
  133. begin
  134. progress^.pub.progress_monitor := progress_monitor;
  135. progress^.completed_extra_passes := 0;
  136. progress^.total_extra_passes := 0;
  137. progress^.percent_done := -1;
  138. cinfo^.progress := @progress^.pub;
  139. end;
  140. end;
  141. {GLOBAL}
  142. procedure end_progress_monitor (cinfo : j_common_ptr);
  143. begin
  144. { Clear away progress display }
  145. if (cinfo^.err^.trace_level = 0) then
  146. begin
  147. WriteLn(#13' '#13);
  148. {fflush(stderr);}
  149. end;
  150. end;
  151. {$endif}
  152. { Case-insensitive matching of possibly-abbreviated keyword switches.
  153. keyword is the constant keyword (must be lower case already),
  154. minchars is length of minimum legal abbreviation. }
  155. {GLOBAL}
  156. function keymatch (arg : string;
  157. const keyword : string;
  158. minchars : int) : boolean;
  159. var
  160. {register} i : int;
  161. ca, ck : char;
  162. {register} nmatched : int;
  163. begin
  164. nmatched := 0;
  165. i := 1;
  166. if length(arg) > length(keyword) then
  167. begin
  168. keymatch := FALSE; { arg longer than keyword, no good }
  169. exit;
  170. end;
  171. while (i <= length(arg)) do
  172. begin
  173. ca := UpCase(arg[i]);
  174. ck := UpCase(keyword[i]);
  175. Inc(i);
  176. if (ca <> ck) then
  177. begin
  178. keymatch := FALSE; { no good }
  179. exit;
  180. end;
  181. Inc(nmatched); { count matched characters }
  182. end;
  183. { reached end of argument; fail if it's too short for unique abbrev }
  184. keymatch := (nmatched >= minchars);
  185. end;
  186. {$IFDEF std I/O}
  187. { Routines to establish binary I/O mode for stdin and stdout.
  188. Non-Unix systems often require some hacking to get out of text mode. }
  189. {GLOBAL}
  190. function read_stdin : FILEptr;
  191. var
  192. input_file : FILEptr;
  193. begin
  194. input_file := @input;
  195. {$ifdef USE_SETMODE} { need to hack file mode? }
  196. setmode(fileno(stdin), O_BINARY);
  197. {$endif}
  198. {$ifdef USE_FDOPEN} { need to re-open in binary mode? }
  199. if ((input_file = fdopen(fileno(stdin), READ_BINARY)) = NIL) then
  200. begin
  201. WriteLn(stderr, 'Cannot reopen stdin');
  202. Halt(EXIT_FAILURE);
  203. end;
  204. {$endif}
  205. read_stdin := input_file;
  206. end;
  207. {GLOBAL}
  208. function write_stdout : FILEptr;
  209. var
  210. output_file : FILEptr;
  211. begin
  212. output_file := @output;
  213. {$ifdef USE_SETMODE} { need to hack file mode? }
  214. setmode(fileno(stdout), O_BINARY);
  215. {$endif}
  216. {$ifdef USE_FDOPEN} { need to re-open in binary mode? }
  217. if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) = NIL) then
  218. begin
  219. WriteLn(stderr, 'Cannot reopen stdout');
  220. Halt(EXIT_FAILURE);
  221. end;
  222. {$endif}
  223. write_stdout := output_file;
  224. end;
  225. {$ENDIF}
  226. end.