rdjpgcom.c 14 KB

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