rdjpgcom.pas 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. Program rdjpgcom;
  2. { This file contains a very simple stand-alone application that displays
  3. the text in COM (comment) markers in a JFIF file.
  4. This may be useful as an example of the minimum logic needed to parse
  5. JPEG markers. }
  6. { Original: Copyright (C) 1994-1995, Thomas G. Lane. }
  7. uses
  8. Objects;
  9. const
  10. EXIT_FAILURE = 1;
  11. EXIT_SUCCESS = 0;
  12. type
  13. int = integer;
  14. uInt = word;
  15. { These macros are used to read the input file.
  16. To reuse this code in another application, you might need to change these. }
  17. var
  18. infile : TBufStream; { input JPEG file }
  19. { Return next input byte, or EOF if no more }
  20. function NEXTBYTE : byte;
  21. var
  22. c : Byte;
  23. begin
  24. infile.Read(c, 1);
  25. NEXTBYTE := c;
  26. end;
  27. { Error exit handler }
  28. procedure ERREXIT(msg : string);
  29. begin
  30. WriteLn(output, msg);
  31. Halt(EXIT_FAILURE);
  32. end;
  33. { Read one byte, testing for EOF }
  34. function read_1_byte : int;
  35. var
  36. c : int;
  37. begin
  38. c := NEXTBYTE;
  39. if (infile.Status <> stOK) then
  40. ERREXIT('Premature EOF in JPEG file');
  41. read_1_byte := c;
  42. end;
  43. { Read 2 bytes, convert to unsigned int }
  44. { All 2-byte quantities in JPEG markers are MSB first }
  45. function read_2_bytes : uInt;
  46. var
  47. c : word;
  48. begin
  49. infile.Read(c, 2);
  50. c := Swap(c);
  51. if (infile.Status <> stOK) then
  52. ERREXIT('Premature EOF in JPEG file');
  53. read_2_bytes := c;
  54. end;
  55. { JPEG markers consist of one or more 0xFF bytes, followed by a marker
  56. code byte (which is not an FF). Here are the marker codes of interest
  57. in this program. (See jdmarker.c for a more complete list.) }
  58. const
  59. M_SOF0 = $C0; { Start Of Frame N }
  60. M_SOF1 = $C1; { N indicates which compression process }
  61. M_SOF2 = $C2; { Only SOF0-SOF2 are now in common use }
  62. M_SOF3 = $C3;
  63. M_SOF5 = $C5; { NB: codes C4 and CC are NOT SOF markers }
  64. M_SOF6 = $C6;
  65. M_SOF7 = $C7;
  66. M_SOF9 = $C9;
  67. M_SOF10 = $CA;
  68. M_SOF11 = $CB;
  69. M_SOF13 = $CD;
  70. M_SOF14 = $CE;
  71. M_SOF15 = $CF;
  72. M_SOI = $D8; { Start Of Image (beginning of datastream) }
  73. M_EOI = $D9; { End Of Image (end of datastream) }
  74. M_SOS = $DA; { Start Of Scan (begins compressed data) }
  75. M_COM = $FE; { COMment }
  76. { Find the next JPEG marker and return its marker code.
  77. We expect at least one FF byte, possibly more if the compressor used FFs
  78. to pad the file.
  79. There could also be non-FF garbage between markers. The treatment of such
  80. garbage is unspecified; we choose to skip over it but emit a warning msg.
  81. NB: this routine must not be used after seeing SOS marker, since it will
  82. not deal correctly with FF/00 sequences in the compressed image data... }
  83. function next_marker : int;
  84. var
  85. c : int;
  86. discarded_bytes : int;
  87. begin
  88. discarded_bytes := 0;
  89. { Find 0xFF byte; count and skip any non-FFs. }
  90. c := read_1_byte;
  91. while (c <> $FF) do
  92. begin
  93. Inc(discarded_bytes);
  94. c := read_1_byte;
  95. end;
  96. { Get marker code byte, swallowing any duplicate FF bytes. Extra FFs
  97. are legal as pad bytes, so don't count them in discarded_bytes. }
  98. repeat
  99. c := read_1_byte;
  100. until (c <> $FF);
  101. if (discarded_bytes <> 0) then
  102. begin
  103. WriteLn(output, 'Warning: garbage data found in JPEG file');
  104. end;
  105. next_marker := c;
  106. end;
  107. { Read the initial marker, which should be SOI.
  108. For a JFIF file, the first two bytes of the file should be literally
  109. $FF M_SOI. To be more general, we could use next_marker, but if the
  110. input file weren't actually JPEG at all, next_marker might read the whole
  111. file and then return a misleading error message... }
  112. function first_marker : int;
  113. var
  114. c1, c2 : int;
  115. begin
  116. c1 := NEXTBYTE;
  117. c2 := NEXTBYTE;
  118. if (c1 <> $FF) or (c2 <> M_SOI) then
  119. ERREXIT('Not a JPEG file');
  120. first_marker := c2;
  121. end;
  122. { Most types of marker are followed by a variable-length parameter segment.
  123. This routine skips over the parameters for any marker we don't otherwise
  124. want to process.
  125. Note that we MUST skip the parameter segment explicitly in order not to
  126. be fooled by $FF bytes that might appear within the parameter segment;
  127. such bytes do NOT introduce new markers. }
  128. procedure skip_variable;
  129. { Skip over an unknown or uninteresting variable-length marker }
  130. var
  131. length : uInt;
  132. begin
  133. { Get the marker parameter length count }
  134. length := read_2_bytes;
  135. { Length includes itself, so must be at least 2 }
  136. if (length < 2) then
  137. ERREXIT('Erroneous JPEG marker length');
  138. Dec(length, 2);
  139. { Skip over the remaining bytes }
  140. while (length > 0) do
  141. begin
  142. read_1_byte;
  143. Dec(length);
  144. end;
  145. end;
  146. { Process a COM marker.
  147. We want to print out the marker contents as legible text;
  148. we must guard against random junk and varying newline representations. }
  149. procedure process_COM;
  150. const
  151. CR = 13;
  152. LF = 10;
  153. var
  154. length : uInt;
  155. comment : string;
  156. lastch : byte;
  157. begin
  158. comment := '';
  159. { Get the marker parameter length count }
  160. length := read_2_bytes;
  161. { Length includes itself, so must be at least 2 }
  162. if (length < 2) then
  163. ERREXIT('Erroneous JPEG marker length');
  164. Dec(length, 2);
  165. comment := '';
  166. while (length > 0) do
  167. begin
  168. comment := comment + char(read_1_byte);
  169. Dec(length);
  170. end;
  171. WriteLn(comment);
  172. end;
  173. { Process a SOFn marker.
  174. This code is only needed if you want to know the image dimensions... }
  175. procedure process_SOFn (marker : int);
  176. var
  177. length : uInt;
  178. image_height, image_width : uInt;
  179. data_precision, num_components : int;
  180. process : string;
  181. ci: int;
  182. begin
  183. length := read_2_bytes; { usual parameter length count }
  184. data_precision := read_1_byte;
  185. image_height := read_2_bytes;
  186. image_width := read_2_bytes;
  187. num_components := read_1_byte;
  188. case marker of
  189. M_SOF0: process := 'Baseline';
  190. M_SOF1: process := 'Extended sequential';
  191. M_SOF2: process := 'Progressive';
  192. M_SOF3: process := 'Lossless';
  193. M_SOF5: process := 'Differential sequential';
  194. M_SOF6: process := 'Differential progressive';
  195. M_SOF7: process := 'Differential lossless';
  196. M_SOF9: process := 'Extended sequential, arithmetic coding';
  197. M_SOF10: process := 'Progressive, arithmetic coding';
  198. M_SOF11: process := 'Lossless, arithmetic coding';
  199. M_SOF13: process := 'Differential sequential, arithmetic coding';
  200. M_SOF14: process := 'Differential progressive, arithmetic coding';
  201. M_SOF15: process := 'Differential lossless, arithmetic coding';
  202. else
  203. process := 'Unknown';
  204. end;
  205. WriteLn('JPEG image is ',image_width,'w * ',image_height,'h, ',
  206. num_components, ' color components, ',data_precision,
  207. ' bits per sample');
  208. WriteLn('JPEG process: ', process);
  209. if (length <> uInt(8 + num_components * 3)) then
  210. ERREXIT('Bogus SOF marker length');
  211. for ci := 0 to pred(num_components) do
  212. begin
  213. read_1_byte; { Component ID code }
  214. read_1_byte; { H, V sampling factors }
  215. read_1_byte; { Quantization table number }
  216. end;
  217. end;
  218. { Parse the marker stream until SOS or EOI is seen;
  219. display any COM markers.
  220. While the companion program wrjpgcom will always insert COM markers before
  221. SOFn, other implementations might not, so we scan to SOS before stopping.
  222. If we were only interested in the image dimensions, we would stop at SOFn.
  223. (Conversely, if we only cared about COM markers, there would be no need
  224. for special code to handle SOFn; we could treat it like other markers.) }
  225. function scan_JPEG_header (verbose : boolean) : int;
  226. var
  227. marker : int;
  228. begin
  229. { Expect SOI at start of file }
  230. if (first_marker <> M_SOI) then
  231. ERREXIT('Expected SOI marker first');
  232. { Scan miscellaneous markers until we reach SOS. }
  233. repeat
  234. marker := next_marker;
  235. case marker of
  236. M_SOF0, { Baseline }
  237. M_SOF1, { Extended sequential, Huffman }
  238. M_SOF2, { Progressive, Huffman }
  239. M_SOF3, { Lossless, Huffman }
  240. M_SOF5, { Differential sequential, Huffman }
  241. M_SOF6, { Differential progressive, Huffman }
  242. M_SOF7, { Differential lossless, Huffman }
  243. M_SOF9, { Extended sequential, arithmetic }
  244. M_SOF10, { Progressive, arithmetic }
  245. M_SOF11, { Lossless, arithmetic }
  246. M_SOF13, { Differential sequential, arithmetic }
  247. M_SOF14, { Differential progressive, arithmetic }
  248. M_SOF15: { Differential lossless, arithmetic }
  249. if (verbose) then
  250. process_SOFn(marker)
  251. else
  252. skip_variable;
  253. M_SOS: { stop before hitting compressed data }
  254. begin
  255. scan_JPEG_header := marker;
  256. exit;
  257. end;
  258. M_EOI: { in case it's a tables-only JPEG stream }
  259. begin
  260. scan_JPEG_header := marker;
  261. exit;
  262. end;
  263. M_COM:
  264. process_COM;
  265. else { Anything else just gets skipped }
  266. skip_variable; { we assume it has a parameter count... }
  267. end;
  268. until false; { end loop }
  269. end;
  270. { Command line parsing code }
  271. var
  272. progname : string[79]; { program name for error messages }
  273. procedure usage;
  274. { complain about bad command line }
  275. begin
  276. WriteLn(output, 'rdjpgcom displays any textual comments in a JPEG file.');
  277. WriteLn(output, 'Usage: ',progname,' [switches] [inputfile]');
  278. WriteLn(output, 'Switches (names may be abbreviated):');
  279. WriteLn(output, ' -verbose Also display dimensions of JPEG image');
  280. Halt(EXIT_FAILURE);
  281. end;
  282. { The main program. }
  283. var
  284. verbose : boolean;
  285. argn : int;
  286. arg : string;
  287. begin
  288. verbose := FALSE;
  289. progname := ParamStr(0);
  290. if (progname = '') then
  291. progname := 'rdjpgcom'; { in case C library doesn't provide it }
  292. { Parse switches, if any }
  293. for argn := 1 to Pred(ParamCount) do
  294. begin
  295. arg := ParamStr(argn);
  296. if arg[1] = '-' then
  297. if (Pos(arg, '-verbose') > 0) then
  298. begin
  299. verbose := TRUE;
  300. end
  301. else
  302. usage;
  303. end;
  304. { Open the input file. }
  305. arg := ParamStr(ParamCount);
  306. if not infile.Init(arg, stOpenRead, 4096) then
  307. begin
  308. WriteLn(output, 'can''t open ', arg);
  309. Halt(EXIT_FAILURE);
  310. end;
  311. { Scan the JPEG headers. }
  312. scan_JPEG_header(verbose);
  313. infile.done;
  314. { All done. }
  315. Halt(EXIT_SUCCESS);
  316. end.