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