rdjpgcom.c 14 KB

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