rdjpgcom.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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_APP0 0xE0 /* Application-specific marker, type N */
  103. #define M_APP12 0xEC /* (we don't bother to list all 16 APPn's) */
  104. #define M_COM 0xFE /* COMment */
  105. /*
  106. * Find the next JPEG marker and return its marker code.
  107. * We expect at least one FF byte, possibly more if the compressor used FFs
  108. * to pad the file.
  109. * There could also be non-FF garbage between markers. The treatment of such
  110. * garbage is unspecified; we choose to skip over it but emit a warning msg.
  111. * NB: this routine must not be used after seeing SOS marker, since it will
  112. * not deal correctly with FF/00 sequences in the compressed image data...
  113. */
  114. static int
  115. next_marker(void)
  116. {
  117. int c;
  118. int discarded_bytes = 0;
  119. /* Find 0xFF byte; count and skip any non-FFs. */
  120. c = read_1_byte();
  121. while (c != 0xFF) {
  122. discarded_bytes++;
  123. c = read_1_byte();
  124. }
  125. /* Get marker code byte, swallowing any duplicate FF bytes. Extra FFs
  126. * are legal as pad bytes, so don't count them in discarded_bytes.
  127. */
  128. do {
  129. c = read_1_byte();
  130. } while (c == 0xFF);
  131. if (discarded_bytes != 0) {
  132. fprintf(stderr, "Warning: garbage data found in JPEG file\n");
  133. }
  134. return c;
  135. }
  136. /*
  137. * Read the initial marker, which should be SOI.
  138. * For a JFIF file, the first two bytes of the file should be literally
  139. * 0xFF M_SOI. To be more general, we could use next_marker, but if the
  140. * input file weren't actually JPEG at all, next_marker might read the whole
  141. * file and then return a misleading error message...
  142. */
  143. static int
  144. first_marker(void)
  145. {
  146. int c1, c2;
  147. c1 = NEXTBYTE();
  148. c2 = NEXTBYTE();
  149. if (c1 != 0xFF || c2 != M_SOI)
  150. ERREXIT("Not a JPEG file");
  151. return c2;
  152. }
  153. /*
  154. * Most types of marker are followed by a variable-length parameter segment.
  155. * This routine skips over the parameters for any marker we don't otherwise
  156. * want to process.
  157. * Note that we MUST skip the parameter segment explicitly in order not to
  158. * be fooled by 0xFF bytes that might appear within the parameter segment;
  159. * such bytes do NOT introduce new markers.
  160. */
  161. static void
  162. skip_variable(void)
  163. /* Skip over an unknown or uninteresting variable-length marker */
  164. {
  165. unsigned int length;
  166. /* Get the marker parameter length count */
  167. length = read_2_bytes();
  168. /* Length includes itself, so must be at least 2 */
  169. if (length < 2)
  170. ERREXIT("Erroneous JPEG marker length");
  171. length -= 2;
  172. /* Skip over the remaining bytes */
  173. while (length > 0) {
  174. (void)read_1_byte();
  175. length--;
  176. }
  177. }
  178. /*
  179. * Process a COM marker.
  180. * We want to print out the marker contents as legible text;
  181. * we must guard against non-text junk and varying newline representations.
  182. */
  183. static void
  184. process_COM(int raw)
  185. {
  186. unsigned int length;
  187. int ch;
  188. int lastch = 0;
  189. /* Bill Allombert: set locale properly for isprint */
  190. #ifdef HAVE_LOCALE_H
  191. setlocale(LC_CTYPE, "");
  192. #endif
  193. /* Get the marker parameter length count */
  194. length = read_2_bytes();
  195. /* Length includes itself, so must be at least 2 */
  196. if (length < 2)
  197. ERREXIT("Erroneous JPEG marker length");
  198. length -= 2;
  199. while (length > 0) {
  200. ch = read_1_byte();
  201. if (raw) {
  202. putc(ch, stdout);
  203. /* Emit the character in a readable form.
  204. * Nonprintables are converted to \nnn form,
  205. * while \ is converted to \\.
  206. * Newlines in CR, CR/LF, or LF form will be printed as one newline.
  207. */
  208. } else if (ch == '\r') {
  209. printf("\n");
  210. } else if (ch == '\n') {
  211. if (lastch != '\r')
  212. printf("\n");
  213. } else if (ch == '\\') {
  214. printf("\\\\");
  215. } else if (isprint(ch)) {
  216. putc(ch, stdout);
  217. } else {
  218. printf("\\%03o", ch);
  219. }
  220. lastch = ch;
  221. length--;
  222. }
  223. printf("\n");
  224. /* Bill Allombert: revert to C locale */
  225. #ifdef HAVE_LOCALE_H
  226. setlocale(LC_CTYPE, "C");
  227. #endif
  228. }
  229. /*
  230. * Process a SOFn marker.
  231. * This code is only needed if you want to know the image dimensions...
  232. */
  233. static void
  234. process_SOFn(int marker)
  235. {
  236. unsigned int length;
  237. unsigned int image_height, image_width;
  238. int data_precision, num_components;
  239. const char *process;
  240. int ci;
  241. length = read_2_bytes(); /* usual parameter length count */
  242. data_precision = read_1_byte();
  243. image_height = read_2_bytes();
  244. image_width = read_2_bytes();
  245. num_components = read_1_byte();
  246. switch (marker) {
  247. case M_SOF0: process = "Baseline"; break;
  248. case M_SOF1: process = "Extended sequential"; break;
  249. case M_SOF2: process = "Progressive"; break;
  250. case M_SOF3: process = "Lossless"; break;
  251. case M_SOF5: process = "Differential sequential"; break;
  252. case M_SOF6: process = "Differential progressive"; break;
  253. case M_SOF7: process = "Differential lossless"; break;
  254. case M_SOF9: process = "Extended sequential, arithmetic coding"; break;
  255. case M_SOF10: process = "Progressive, arithmetic coding"; break;
  256. case M_SOF11: process = "Lossless, arithmetic coding"; break;
  257. case M_SOF13: process = "Differential sequential, arithmetic coding"; break;
  258. case M_SOF14:
  259. process = "Differential progressive, arithmetic coding"; break;
  260. case M_SOF15: process = "Differential lossless, arithmetic coding"; break;
  261. default: process = "Unknown"; break;
  262. }
  263. printf("JPEG image is %uw * %uh, %d color components, %d bits per sample\n",
  264. image_width, image_height, num_components, data_precision);
  265. printf("JPEG process: %s\n", process);
  266. if (length != (unsigned int)(8 + num_components * 3))
  267. ERREXIT("Bogus SOF marker length");
  268. for (ci = 0; ci < num_components; ci++) {
  269. (void)read_1_byte(); /* Component ID code */
  270. (void)read_1_byte(); /* H, V sampling factors */
  271. (void)read_1_byte(); /* Quantization table number */
  272. }
  273. }
  274. /*
  275. * Parse the marker stream until SOS or EOI is seen;
  276. * display any COM markers.
  277. * While the companion program wrjpgcom will always insert COM markers before
  278. * SOFn, other implementations might not, so we scan to SOS before stopping.
  279. * If we were only interested in the image dimensions, we would stop at SOFn.
  280. * (Conversely, if we only cared about COM markers, there would be no need
  281. * for special code to handle SOFn; we could treat it like other markers.)
  282. */
  283. static int
  284. scan_JPEG_header(int verbose, int raw)
  285. {
  286. int marker;
  287. /* Expect SOI at start of file */
  288. if (first_marker() != M_SOI)
  289. ERREXIT("Expected SOI marker first");
  290. /* Scan miscellaneous markers until we reach SOS. */
  291. for (;;) {
  292. marker = next_marker();
  293. switch (marker) {
  294. /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be,
  295. * treated as SOFn. C4 in particular is actually DHT.
  296. */
  297. case M_SOF0: /* Baseline */
  298. case M_SOF1: /* Extended sequential, Huffman */
  299. case M_SOF2: /* Progressive, Huffman */
  300. case M_SOF3: /* Lossless, Huffman */
  301. case M_SOF5: /* Differential sequential, Huffman */
  302. case M_SOF6: /* Differential progressive, Huffman */
  303. case M_SOF7: /* Differential lossless, Huffman */
  304. case M_SOF9: /* Extended sequential, arithmetic */
  305. case M_SOF10: /* Progressive, arithmetic */
  306. case M_SOF11: /* Lossless, arithmetic */
  307. case M_SOF13: /* Differential sequential, arithmetic */
  308. case M_SOF14: /* Differential progressive, arithmetic */
  309. case M_SOF15: /* Differential lossless, arithmetic */
  310. if (verbose)
  311. process_SOFn(marker);
  312. else
  313. skip_variable();
  314. break;
  315. case M_SOS: /* stop before hitting compressed data */
  316. return marker;
  317. case M_EOI: /* in case it's a tables-only JPEG stream */
  318. return marker;
  319. case M_COM:
  320. process_COM(raw);
  321. break;
  322. case M_APP12:
  323. /* Some digital camera makers put useful textual information into
  324. * APP12 markers, so we print those out too when in -verbose mode.
  325. */
  326. if (verbose) {
  327. printf("APP12 contains:\n");
  328. process_COM(raw);
  329. } else
  330. skip_variable();
  331. break;
  332. default: /* Anything else just gets skipped */
  333. skip_variable(); /* we assume it has a parameter count... */
  334. break;
  335. }
  336. } /* end loop */
  337. }
  338. /* Command line parsing code */
  339. static const char *progname; /* program name for error messages */
  340. static void
  341. usage(void)
  342. /* complain about bad command line */
  343. {
  344. fprintf(stderr, "rdjpgcom displays any textual comments in a JPEG file.\n");
  345. fprintf(stderr, "Usage: %s [switches] [inputfile]\n", progname);
  346. fprintf(stderr, "Switches (names may be abbreviated):\n");
  347. fprintf(stderr, " -raw Display non-printable characters in comments (unsafe)\n");
  348. fprintf(stderr, " -verbose Also display dimensions of JPEG image\n");
  349. exit(EXIT_FAILURE);
  350. }
  351. static int
  352. keymatch(char *arg, const char *keyword, int minchars)
  353. /* Case-insensitive matching of (possibly abbreviated) keyword switches. */
  354. /* keyword is the constant keyword (must be lower case already), */
  355. /* minchars is length of minimum legal abbreviation. */
  356. {
  357. register int ca, ck;
  358. register int nmatched = 0;
  359. while ((ca = *arg++) != '\0') {
  360. if ((ck = *keyword++) == '\0')
  361. return 0; /* arg longer than keyword, no good */
  362. if (isupper(ca)) /* force arg to lcase (assume ck is already) */
  363. ca = tolower(ca);
  364. if (ca != ck)
  365. return 0; /* no good */
  366. nmatched++; /* count matched characters */
  367. }
  368. /* reached end of argument; fail if it's too short for unique abbrev */
  369. if (nmatched < minchars)
  370. return 0;
  371. return 1; /* A-OK */
  372. }
  373. /*
  374. * The main program.
  375. */
  376. int
  377. main(int argc, char **argv)
  378. {
  379. int argn;
  380. char *arg;
  381. int verbose = 0, raw = 0;
  382. /* On Mac, fetch a command line. */
  383. #ifdef USE_CCOMMAND
  384. argc = ccommand(&argv);
  385. #endif
  386. progname = argv[0];
  387. if (progname == NULL || progname[0] == 0)
  388. progname = "rdjpgcom"; /* in case C library doesn't provide it */
  389. /* Parse switches, if any */
  390. for (argn = 1; argn < argc; argn++) {
  391. arg = argv[argn];
  392. if (arg[0] != '-')
  393. break; /* not switch, must be file name */
  394. arg++; /* advance over '-' */
  395. if (keymatch(arg, "verbose", 1)) {
  396. verbose++;
  397. } else if (keymatch(arg, "raw", 1)) {
  398. raw = 1;
  399. } else
  400. usage();
  401. }
  402. /* Open the input file. */
  403. /* Unix style: expect zero or one file name */
  404. if (argn < argc - 1) {
  405. fprintf(stderr, "%s: only one input file\n", progname);
  406. usage();
  407. }
  408. if (argn < argc) {
  409. if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
  410. fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
  411. exit(EXIT_FAILURE);
  412. }
  413. } else {
  414. /* default input file is stdin */
  415. #ifdef USE_SETMODE /* need to hack file mode? */
  416. setmode(fileno(stdin), O_BINARY);
  417. #endif
  418. #ifdef USE_FDOPEN /* need to re-open in binary mode? */
  419. if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
  420. fprintf(stderr, "%s: can't open stdin\n", progname);
  421. exit(EXIT_FAILURE);
  422. }
  423. #else
  424. infile = stdin;
  425. #endif
  426. }
  427. /* Scan the JPEG headers. */
  428. (void)scan_JPEG_header(verbose, raw);
  429. /* All done. */
  430. exit(EXIT_SUCCESS);
  431. return 0; /* suppress no-return-value warnings */
  432. }