rdjpgcom.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * rdjpgcom.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1994-1997, Thomas G. Lane.
  6. * Modified 2009 by Bill Allombert, Guido Vollbeding.
  7. * It was modified by The libjpeg-turbo Project to include only code relevant
  8. * to libjpeg-turbo.
  9. * For conditions of distribution and use, see the accompanying README.ijg
  10. * file.
  11. *
  12. * This file contains a very simple stand-alone application that displays
  13. * the text in COM (comment) markers in a JFIF file.
  14. * This may be useful as an example of the minimum logic needed to parse
  15. * JPEG markers.
  16. */
  17. #define JPEG_CJPEG_DJPEG /* to get the command-line config symbols */
  18. #include "jinclude.h" /* get auto-config symbols, <stdio.h> */
  19. #ifdef HAVE_LOCALE_H
  20. #include <locale.h> /* Bill Allombert: use locale for isprint */
  21. #endif
  22. #include <ctype.h> /* to declare isupper(), tolower() */
  23. #ifdef USE_SETMODE
  24. #include <fcntl.h> /* to declare setmode()'s parameter macros */
  25. /* If you have setmode() but not <io.h>, just delete this line: */
  26. #include <io.h> /* to declare setmode() */
  27. #endif
  28. #ifdef USE_CCOMMAND /* command-line reader for Macintosh */
  29. #ifdef __MWERKS__
  30. #include <SIOUX.h> /* Metrowerks needs this */
  31. #include <console.h> /* ... and this */
  32. #endif
  33. #ifdef THINK_C
  34. #include <console.h> /* Think declares it here */
  35. #endif
  36. #endif
  37. #ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
  38. #define READ_BINARY "r"
  39. #else
  40. #define READ_BINARY "rb"
  41. #endif
  42. #ifndef EXIT_FAILURE /* define exit() codes if not provided */
  43. #define EXIT_FAILURE 1
  44. #endif
  45. #ifndef EXIT_SUCCESS
  46. #define EXIT_SUCCESS 0
  47. #endif
  48. /*
  49. * These macros are used to read the input file.
  50. * To reuse this code in another application, you might need to change these.
  51. */
  52. static FILE *infile; /* input JPEG file */
  53. /* Return next input byte, or EOF if no more */
  54. #define NEXTBYTE() getc(infile)
  55. /* Error exit handler */
  56. #define ERREXIT(msg) (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE))
  57. /* Read one byte, testing for EOF */
  58. static int
  59. read_1_byte(void)
  60. {
  61. int c;
  62. c = NEXTBYTE();
  63. if (c == EOF)
  64. ERREXIT("Premature EOF in JPEG file");
  65. return c;
  66. }
  67. /* Read 2 bytes, convert to unsigned int */
  68. /* All 2-byte quantities in JPEG markers are MSB first */
  69. static unsigned int
  70. read_2_bytes(void)
  71. {
  72. int c1, c2;
  73. c1 = NEXTBYTE();
  74. if (c1 == EOF)
  75. ERREXIT("Premature EOF in JPEG file");
  76. c2 = NEXTBYTE();
  77. if (c2 == EOF)
  78. ERREXIT("Premature EOF in JPEG file");
  79. return (((unsigned int)c1) << 8) + ((unsigned int)c2);
  80. }
  81. /*
  82. * JPEG markers consist of one or more 0xFF bytes, followed by a marker
  83. * code byte (which is not an FF). Here are the marker codes of interest
  84. * in this program. (See jdmarker.c for a more complete list.)
  85. */
  86. #define M_SOF0 0xC0 /* Start Of Frame N */
  87. #define M_SOF1 0xC1 /* N indicates which compression process */
  88. #define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */
  89. #define M_SOF3 0xC3
  90. #define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */
  91. #define M_SOF6 0xC6
  92. #define M_SOF7 0xC7
  93. #define M_SOF9 0xC9
  94. #define M_SOF10 0xCA
  95. #define M_SOF11 0xCB
  96. #define M_SOF13 0xCD
  97. #define M_SOF14 0xCE
  98. #define M_SOF15 0xCF
  99. #define M_SOI 0xD8 /* Start Of Image (beginning of datastream) */
  100. #define M_EOI 0xD9 /* End Of Image (end of datastream) */
  101. #define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
  102. #define M_APP12 0xEC /* (we don't bother to list all 16 APPn's) */
  103. #define M_COM 0xFE /* COMment */
  104. /*
  105. * Find the next JPEG marker and return its marker code.
  106. * We expect at least one FF byte, possibly more if the compressor used FFs
  107. * to pad the file.
  108. * There could also be non-FF garbage between markers. The treatment of such
  109. * garbage is unspecified; we choose to skip over it but emit a warning msg.
  110. * NB: this routine must not be used after seeing SOS marker, since it will
  111. * not deal correctly with FF/00 sequences in the compressed image data...
  112. */
  113. static int
  114. next_marker(void)
  115. {
  116. int c;
  117. int discarded_bytes = 0;
  118. /* Find 0xFF byte; count and skip any non-FFs. */
  119. c = read_1_byte();
  120. while (c != 0xFF) {
  121. discarded_bytes++;
  122. c = read_1_byte();
  123. }
  124. /* Get marker code byte, swallowing any duplicate FF bytes. Extra FFs
  125. * are legal as pad bytes, so don't count them in discarded_bytes.
  126. */
  127. do {
  128. c = read_1_byte();
  129. } while (c == 0xFF);
  130. if (discarded_bytes != 0) {
  131. fprintf(stderr, "Warning: garbage data found in JPEG file\n");
  132. }
  133. return c;
  134. }
  135. /*
  136. * Read the initial marker, which should be SOI.
  137. * For a JFIF file, the first two bytes of the file should be literally
  138. * 0xFF M_SOI. To be more general, we could use next_marker, but if the
  139. * input file weren't actually JPEG at all, next_marker might read the whole
  140. * file and then return a misleading error message...
  141. */
  142. static int
  143. first_marker(void)
  144. {
  145. int c1, c2;
  146. c1 = NEXTBYTE();
  147. c2 = NEXTBYTE();
  148. if (c1 != 0xFF || c2 != M_SOI)
  149. ERREXIT("Not a JPEG file");
  150. return c2;
  151. }
  152. /*
  153. * Most types of marker are followed by a variable-length parameter segment.
  154. * This routine skips over the parameters for any marker we don't otherwise
  155. * want to process.
  156. * Note that we MUST skip the parameter segment explicitly in order not to
  157. * be fooled by 0xFF bytes that might appear within the parameter segment;
  158. * such bytes do NOT introduce new markers.
  159. */
  160. static void
  161. skip_variable(void)
  162. /* Skip over an unknown or uninteresting variable-length marker */
  163. {
  164. unsigned int length;
  165. /* Get the marker parameter length count */
  166. length = read_2_bytes();
  167. /* Length includes itself, so must be at least 2 */
  168. if (length < 2)
  169. ERREXIT("Erroneous JPEG marker length");
  170. length -= 2;
  171. /* Skip over the remaining bytes */
  172. while (length > 0) {
  173. (void)read_1_byte();
  174. length--;
  175. }
  176. }
  177. /*
  178. * Process a COM marker.
  179. * We want to print out the marker contents as legible text;
  180. * we must guard against non-text junk and varying newline representations.
  181. */
  182. static void
  183. process_COM(int raw)
  184. {
  185. unsigned int length;
  186. int ch;
  187. int lastch = 0;
  188. /* Bill Allombert: set locale properly for isprint */
  189. #ifdef HAVE_LOCALE_H
  190. setlocale(LC_CTYPE, "");
  191. #endif
  192. /* Get the marker parameter length count */
  193. length = read_2_bytes();
  194. /* Length includes itself, so must be at least 2 */
  195. if (length < 2)
  196. ERREXIT("Erroneous JPEG marker length");
  197. length -= 2;
  198. while (length > 0) {
  199. ch = read_1_byte();
  200. if (raw) {
  201. putc(ch, stdout);
  202. /* Emit the character in a readable form.
  203. * Nonprintables are converted to \nnn form,
  204. * while \ is converted to \\.
  205. * Newlines in CR, CR/LF, or LF form will be printed as one newline.
  206. */
  207. } else if (ch == '\r') {
  208. printf("\n");
  209. } else if (ch == '\n') {
  210. if (lastch != '\r')
  211. printf("\n");
  212. } else if (ch == '\\') {
  213. printf("\\\\");
  214. } else if (isprint(ch)) {
  215. putc(ch, stdout);
  216. } else {
  217. printf("\\%03o", ch);
  218. }
  219. lastch = ch;
  220. length--;
  221. }
  222. printf("\n");
  223. /* Bill Allombert: revert to C locale */
  224. #ifdef HAVE_LOCALE_H
  225. setlocale(LC_CTYPE, "C");
  226. #endif
  227. }
  228. /*
  229. * Process a SOFn marker.
  230. * This code is only needed if you want to know the image dimensions...
  231. */
  232. static void
  233. process_SOFn(int marker)
  234. {
  235. unsigned int length;
  236. unsigned int image_height, image_width;
  237. int data_precision, num_components;
  238. const char *process;
  239. int ci;
  240. length = read_2_bytes(); /* usual parameter length count */
  241. data_precision = read_1_byte();
  242. image_height = read_2_bytes();
  243. image_width = read_2_bytes();
  244. num_components = read_1_byte();
  245. switch (marker) {
  246. case M_SOF0: process = "Baseline"; break;
  247. case M_SOF1: process = "Extended sequential"; break;
  248. case M_SOF2: process = "Progressive"; break;
  249. case M_SOF3: process = "Lossless"; break;
  250. case M_SOF5: process = "Differential sequential"; break;
  251. case M_SOF6: process = "Differential progressive"; break;
  252. case M_SOF7: process = "Differential lossless"; break;
  253. case M_SOF9: process = "Extended sequential, arithmetic coding"; break;
  254. case M_SOF10: process = "Progressive, arithmetic coding"; break;
  255. case M_SOF11: process = "Lossless, arithmetic coding"; break;
  256. case M_SOF13: process = "Differential sequential, arithmetic coding"; break;
  257. case M_SOF14:
  258. process = "Differential progressive, arithmetic coding"; break;
  259. case M_SOF15: process = "Differential lossless, arithmetic coding"; break;
  260. default: process = "Unknown"; break;
  261. }
  262. printf("JPEG image is %uw * %uh, %d color components, %d bits per sample\n",
  263. image_width, image_height, num_components, data_precision);
  264. printf("JPEG process: %s\n", process);
  265. if (length != (unsigned int)(8 + num_components * 3))
  266. ERREXIT("Bogus SOF marker length");
  267. for (ci = 0; ci < num_components; ci++) {
  268. (void)read_1_byte(); /* Component ID code */
  269. (void)read_1_byte(); /* H, V sampling factors */
  270. (void)read_1_byte(); /* Quantization table number */
  271. }
  272. }
  273. /*
  274. * Parse the marker stream until SOS or EOI is seen;
  275. * display any COM markers.
  276. * While the companion program wrjpgcom will always insert COM markers before
  277. * SOFn, other implementations might not, so we scan to SOS before stopping.
  278. * If we were only interested in the image dimensions, we would stop at SOFn.
  279. * (Conversely, if we only cared about COM markers, there would be no need
  280. * for special code to handle SOFn; we could treat it like other markers.)
  281. */
  282. static int
  283. scan_JPEG_header(int verbose, int raw)
  284. {
  285. int marker;
  286. /* Expect SOI at start of file */
  287. if (first_marker() != M_SOI)
  288. ERREXIT("Expected SOI marker first");
  289. /* Scan miscellaneous markers until we reach SOS. */
  290. for (;;) {
  291. marker = next_marker();
  292. switch (marker) {
  293. /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be,
  294. * treated as SOFn. C4 in particular is actually DHT.
  295. */
  296. case M_SOF0: /* Baseline */
  297. case M_SOF1: /* Extended sequential, Huffman */
  298. case M_SOF2: /* Progressive, Huffman */
  299. case M_SOF3: /* Lossless, Huffman */
  300. case M_SOF5: /* Differential sequential, Huffman */
  301. case M_SOF6: /* Differential progressive, Huffman */
  302. case M_SOF7: /* Differential lossless, Huffman */
  303. case M_SOF9: /* Extended sequential, arithmetic */
  304. case M_SOF10: /* Progressive, arithmetic */
  305. case M_SOF11: /* Lossless, arithmetic */
  306. case M_SOF13: /* Differential sequential, arithmetic */
  307. case M_SOF14: /* Differential progressive, arithmetic */
  308. case M_SOF15: /* Differential lossless, arithmetic */
  309. if (verbose)
  310. process_SOFn(marker);
  311. else
  312. skip_variable();
  313. break;
  314. case M_SOS: /* stop before hitting compressed data */
  315. return marker;
  316. case M_EOI: /* in case it's a tables-only JPEG stream */
  317. return marker;
  318. case M_COM:
  319. process_COM(raw);
  320. break;
  321. case M_APP12:
  322. /* Some digital camera makers put useful textual information into
  323. * APP12 markers, so we print those out too when in -verbose mode.
  324. */
  325. if (verbose) {
  326. printf("APP12 contains:\n");
  327. process_COM(raw);
  328. } else
  329. skip_variable();
  330. break;
  331. default: /* Anything else just gets skipped */
  332. skip_variable(); /* we assume it has a parameter count... */
  333. break;
  334. }
  335. } /* end loop */
  336. }
  337. /* Command line parsing code */
  338. static const char *progname; /* program name for error messages */
  339. static void
  340. usage(void)
  341. /* complain about bad command line */
  342. {
  343. fprintf(stderr, "rdjpgcom displays any textual comments in a JPEG file.\n");
  344. fprintf(stderr, "Usage: %s [switches] [inputfile]\n", progname);
  345. fprintf(stderr, "Switches (names may be abbreviated):\n");
  346. fprintf(stderr, " -raw Display non-printable characters in comments (unsafe)\n");
  347. fprintf(stderr, " -verbose Also display dimensions of JPEG image\n");
  348. exit(EXIT_FAILURE);
  349. }
  350. static int
  351. keymatch(char *arg, const char *keyword, int minchars)
  352. /* Case-insensitive matching of (possibly abbreviated) keyword switches. */
  353. /* keyword is the constant keyword (must be lower case already), */
  354. /* minchars is length of minimum legal abbreviation. */
  355. {
  356. register int ca, ck;
  357. register int nmatched = 0;
  358. while ((ca = *arg++) != '\0') {
  359. if ((ck = *keyword++) == '\0')
  360. return 0; /* arg longer than keyword, no good */
  361. if (isupper(ca)) /* force arg to lcase (assume ck is already) */
  362. ca = tolower(ca);
  363. if (ca != ck)
  364. return 0; /* no good */
  365. nmatched++; /* count matched characters */
  366. }
  367. /* reached end of argument; fail if it's too short for unique abbrev */
  368. if (nmatched < minchars)
  369. return 0;
  370. return 1; /* A-OK */
  371. }
  372. /*
  373. * The main program.
  374. */
  375. int
  376. main(int argc, char **argv)
  377. {
  378. int argn;
  379. char *arg;
  380. int verbose = 0, raw = 0;
  381. /* On Mac, fetch a command line. */
  382. #ifdef USE_CCOMMAND
  383. argc = ccommand(&argv);
  384. #endif
  385. progname = argv[0];
  386. if (progname == NULL || progname[0] == 0)
  387. progname = "rdjpgcom"; /* in case C library doesn't provide it */
  388. /* Parse switches, if any */
  389. for (argn = 1; argn < argc; argn++) {
  390. arg = argv[argn];
  391. if (arg[0] != '-')
  392. break; /* not switch, must be file name */
  393. arg++; /* advance over '-' */
  394. if (keymatch(arg, "verbose", 1)) {
  395. verbose++;
  396. } else if (keymatch(arg, "raw", 1)) {
  397. raw = 1;
  398. } else
  399. usage();
  400. }
  401. /* Open the input file. */
  402. /* Unix style: expect zero or one file name */
  403. if (argn < argc - 1) {
  404. fprintf(stderr, "%s: only one input file\n", progname);
  405. usage();
  406. }
  407. if (argn < argc) {
  408. if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
  409. fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
  410. exit(EXIT_FAILURE);
  411. }
  412. } else {
  413. /* default input file is stdin */
  414. #ifdef USE_SETMODE /* need to hack file mode? */
  415. setmode(fileno(stdin), O_BINARY);
  416. #endif
  417. #ifdef USE_FDOPEN /* need to re-open in binary mode? */
  418. if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
  419. fprintf(stderr, "%s: can't open stdin\n", progname);
  420. exit(EXIT_FAILURE);
  421. }
  422. #else
  423. infile = stdin;
  424. #endif
  425. }
  426. /* Scan the JPEG headers. */
  427. (void)scan_JPEG_header(verbose, raw);
  428. /* All done. */
  429. exit(EXIT_SUCCESS);
  430. return 0; /* suppress no-return-value warnings */
  431. }