Back to index

plt-scheme  4.2.1
Defines | Functions | Variables
rdjpgcom.c File Reference
#include "jinclude.h"
#include <ctype.h>

Go to the source code of this file.

Defines

#define JPEG_CJPEG_DJPEG   /* to get the command-line config symbols */
#define READ_BINARY   "rb"
#define EXIT_FAILURE   1
#define EXIT_SUCCESS   0
#define NEXTBYTE()   getc(infile)
#define ERREXIT(msg)   (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE))
#define M_SOF0   0xC0 /* Start Of Frame N */
#define M_SOF1   0xC1 /* N indicates which compression process */
#define M_SOF2   0xC2 /* Only SOF0-SOF2 are now in common use */
#define M_SOF3   0xC3
#define M_SOF5   0xC5 /* NB: codes C4 and CC are NOT SOF markers */
#define M_SOF6   0xC6
#define M_SOF7   0xC7
#define M_SOF9   0xC9
#define M_SOF10   0xCA
#define M_SOF11   0xCB
#define M_SOF13   0xCD
#define M_SOF14   0xCE
#define M_SOF15   0xCF
#define M_SOI   0xD8 /* Start Of Image (beginning of datastream) */
#define M_EOI   0xD9 /* End Of Image (end of datastream) */
#define M_SOS   0xDA /* Start Of Scan (begins compressed data) */
#define M_APP0   0xE0 /* Application-specific marker, type N */
#define M_APP12   0xEC /* (we don't bother to list all 16 APPn's) */
#define M_COM   0xFE /* COMment */

Functions

static int read_1_byte (void)
static unsigned int read_2_bytes (void)
static int next_marker (void)
static int first_marker (void)
static void skip_variable (void)
static void process_COM (void)
static void process_SOFn (int marker)
static int scan_JPEG_header (int verbose)
static void usage (void)
static int keymatch (char *arg, const char *keyword, int minchars)
int main (int argc, char **argv)

Variables

static FILE * infile
static const char * progname

Define Documentation

#define ERREXIT (   msg)    (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE))

Definition at line 68 of file rdjpgcom.c.

#define EXIT_FAILURE   1

Definition at line 45 of file rdjpgcom.c.

#define EXIT_SUCCESS   0

Definition at line 51 of file rdjpgcom.c.

#define JPEG_CJPEG_DJPEG   /* to get the command-line config symbols */

Definition at line 14 of file rdjpgcom.c.

#define M_APP0   0xE0 /* Application-specific marker, type N */

Definition at line 122 of file rdjpgcom.c.

#define M_APP12   0xEC /* (we don't bother to list all 16 APPn's) */

Definition at line 123 of file rdjpgcom.c.

#define M_COM   0xFE /* COMment */

Definition at line 124 of file rdjpgcom.c.

#define M_EOI   0xD9 /* End Of Image (end of datastream) */

Definition at line 120 of file rdjpgcom.c.

#define M_SOF0   0xC0 /* Start Of Frame N */

Definition at line 106 of file rdjpgcom.c.

#define M_SOF1   0xC1 /* N indicates which compression process */

Definition at line 107 of file rdjpgcom.c.

#define M_SOF10   0xCA

Definition at line 114 of file rdjpgcom.c.

#define M_SOF11   0xCB

Definition at line 115 of file rdjpgcom.c.

#define M_SOF13   0xCD

Definition at line 116 of file rdjpgcom.c.

#define M_SOF14   0xCE

Definition at line 117 of file rdjpgcom.c.

#define M_SOF15   0xCF

Definition at line 118 of file rdjpgcom.c.

#define M_SOF2   0xC2 /* Only SOF0-SOF2 are now in common use */

Definition at line 108 of file rdjpgcom.c.

#define M_SOF3   0xC3

Definition at line 109 of file rdjpgcom.c.

#define M_SOF5   0xC5 /* NB: codes C4 and CC are NOT SOF markers */

Definition at line 110 of file rdjpgcom.c.

#define M_SOF6   0xC6

Definition at line 111 of file rdjpgcom.c.

#define M_SOF7   0xC7

Definition at line 112 of file rdjpgcom.c.

#define M_SOF9   0xC9

Definition at line 113 of file rdjpgcom.c.

#define M_SOI   0xD8 /* Start Of Image (beginning of datastream) */

Definition at line 119 of file rdjpgcom.c.

#define M_SOS   0xDA /* Start Of Scan (begins compressed data) */

Definition at line 121 of file rdjpgcom.c.

#define NEXTBYTE ( )    getc(infile)

Definition at line 64 of file rdjpgcom.c.

#define READ_BINARY   "rb"

Definition at line 40 of file rdjpgcom.c.


Function Documentation

static int first_marker ( void  ) [static]

Definition at line 173 of file rdjpgcom.c.

{
  int c1, c2;

  c1 = NEXTBYTE();
  c2 = NEXTBYTE();
  if (c1 != 0xFF || c2 != M_SOI)
    ERREXIT("Not a JPEG file");
  return c2;
}
static int keymatch ( char *  arg,
const char *  keyword,
int  minchars 
) [static]

Definition at line 408 of file rdjpgcom.c.

{
  register int ca, ck;
  register int nmatched = 0;

  while ((ca = *arg++) != '\0') {
    if ((ck = *keyword++) == '\0')
      return 0;                    /* arg longer than keyword, no good */
    if (isupper(ca))        /* force arg to lcase (assume ck is already) */
      ca = tolower(ca);
    if (ca != ck)
      return 0;                    /* no good */
    nmatched++;                    /* count matched characters */
  }
  /* reached end of argument; fail if it's too short for unique abbrev */
  if (nmatched < minchars)
    return 0;
  return 1;                 /* A-OK */
}
int main ( int  argc,
char **  argv 
)

Definition at line 437 of file rdjpgcom.c.

{
  int argn;
  char * arg;
  int verbose = 0;

  /* On Mac, fetch a command line. */
#ifdef USE_CCOMMAND
  argc = ccommand(&argv);
#endif

  progname = argv[0];
  if (progname == NULL || progname[0] == 0)
    progname = "rdjpgcom";  /* in case C library doesn't provide it */

  /* Parse switches, if any */
  for (argn = 1; argn < argc; argn++) {
    arg = argv[argn];
    if (arg[0] != '-')
      break;                /* not switch, must be file name */
    arg++;                  /* advance over '-' */
    if (keymatch(arg, "verbose", 1)) {
      verbose++;
    } else
      usage();
  }

  /* Open the input file. */
  /* Unix style: expect zero or one file name */
  if (argn < argc-1) {
    fprintf(stderr, "%s: only one input file\n", progname);
    usage();
  }
  if (argn < argc) {
    if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
      fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
      exit(EXIT_FAILURE);
    }
  } else {
    /* default input file is stdin */
#ifdef USE_SETMODE          /* need to hack file mode? */
    setmode(fileno(stdin), O_BINARY);
#endif
#ifdef USE_FDOPEN           /* need to re-open in binary mode? */
    if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
      fprintf(stderr, "%s: can't open stdin\n", progname);
      exit(EXIT_FAILURE);
    }
#else
    infile = stdin;
#endif
  }

  /* Scan the JPEG headers. */
  (void) scan_JPEG_header(verbose);

  /* All done. */
  exit(EXIT_SUCCESS);
  return 0;                 /* suppress no-return-value warnings */
}

Here is the call graph for this function:

static int next_marker ( void  ) [static]

Definition at line 138 of file rdjpgcom.c.

{
  int c;
  int discarded_bytes = 0;

  /* Find 0xFF byte; count and skip any non-FFs. */
  c = read_1_byte();
  while (c != 0xFF) {
    discarded_bytes++;
    c = read_1_byte();
  }
  /* Get marker code byte, swallowing any duplicate FF bytes.  Extra FFs
   * are legal as pad bytes, so don't count them in discarded_bytes.
   */
  do {
    c = read_1_byte();
  } while (c == 0xFF);

  if (discarded_bytes != 0) {
    fprintf(stderr, "Warning: garbage data found in JPEG file\n");
  }

  return c;
}

Here is the call graph for this function:

static void process_COM ( void  ) [static]

Definition at line 221 of file rdjpgcom.c.

{
  unsigned int length;
  int ch;
  int lastch = 0;

  /* Get the marker parameter length count */
  length = read_2_bytes();
  /* Length includes itself, so must be at least 2 */
  if (length < 2)
    ERREXIT("Erroneous JPEG marker length");
  length -= 2;

  while (length > 0) {
    ch = read_1_byte();
    /* Emit the character in a readable form.
     * Nonprintables are converted to \nnn form,
     * while \ is converted to \\.
     * Newlines in CR, CR/LF, or LF form will be printed as one newline.
     */
    if (ch == '\r') {
      printf("\n");
    } else if (ch == '\n') {
      if (lastch != '\r')
       printf("\n");
    } else if (ch == '\\') {
      printf("\\\\");
    } else if (isprint(ch)) {
      putc(ch, stdout);
    } else {
      printf("\\%03o", ch);
    }
    lastch = ch;
    length--;
  }
  printf("\n");
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void process_SOFn ( int  marker) [static]

Definition at line 266 of file rdjpgcom.c.

{
  unsigned int length;
  unsigned int image_height, image_width;
  int data_precision, num_components;
  const char * process;
  int ci;

  length = read_2_bytes();  /* usual parameter length count */

  data_precision = read_1_byte();
  image_height = read_2_bytes();
  image_width = read_2_bytes();
  num_components = read_1_byte();

  switch (marker) {
  case M_SOF0:       process = "Baseline";  break;
  case M_SOF1:       process = "Extended sequential";  break;
  case M_SOF2:       process = "Progressive";  break;
  case M_SOF3:       process = "Lossless";  break;
  case M_SOF5:       process = "Differential sequential";  break;
  case M_SOF6:       process = "Differential progressive";  break;
  case M_SOF7:       process = "Differential lossless";  break;
  case M_SOF9:       process = "Extended sequential, arithmetic coding";  break;
  case M_SOF10:      process = "Progressive, arithmetic coding";  break;
  case M_SOF11:      process = "Lossless, arithmetic coding";  break;
  case M_SOF13:      process = "Differential sequential, arithmetic coding";  break;
  case M_SOF14:      process = "Differential progressive, arithmetic coding"; break;
  case M_SOF15:      process = "Differential lossless, arithmetic coding";  break;
  default:    process = "Unknown";  break;
  }

  printf("JPEG image is %uw * %uh, %d color components, %d bits per sample\n",
        image_width, image_height, num_components, data_precision);
  printf("JPEG process: %s\n", process);

  if (length != (unsigned int) (8 + num_components * 3))
    ERREXIT("Bogus SOF marker length");

  for (ci = 0; ci < num_components; ci++) {
    (void) read_1_byte();   /* Component ID code */
    (void) read_1_byte();   /* H, V sampling factors */
    (void) read_1_byte();   /* Quantization table number */
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int read_1_byte ( void  ) [static]

Definition at line 73 of file rdjpgcom.c.

{
  int c;

  c = NEXTBYTE();
  if (c == EOF)
    ERREXIT("Premature EOF in JPEG file");
  return c;
}

Here is the caller graph for this function:

static unsigned int read_2_bytes ( void  ) [static]

Definition at line 86 of file rdjpgcom.c.

{
  int c1, c2;

  c1 = NEXTBYTE();
  if (c1 == EOF)
    ERREXIT("Premature EOF in JPEG file");
  c2 = NEXTBYTE();
  if (c2 == EOF)
    ERREXIT("Premature EOF in JPEG file");
  return (((unsigned int) c1) << 8) + ((unsigned int) c2);
}

Here is the caller graph for this function:

static int scan_JPEG_header ( int  verbose) [static]

Definition at line 324 of file rdjpgcom.c.

{
  int marker;

  /* Expect SOI at start of file */
  if (first_marker() != M_SOI)
    ERREXIT("Expected SOI marker first");

  /* Scan miscellaneous markers until we reach SOS. */
  for (;;) {
    marker = next_marker();
    switch (marker) {
      /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be,
       * treated as SOFn.  C4 in particular is actually DHT.
       */
    case M_SOF0:            /* Baseline */
    case M_SOF1:            /* Extended sequential, Huffman */
    case M_SOF2:            /* Progressive, Huffman */
    case M_SOF3:            /* Lossless, Huffman */
    case M_SOF5:            /* Differential sequential, Huffman */
    case M_SOF6:            /* Differential progressive, Huffman */
    case M_SOF7:            /* Differential lossless, Huffman */
    case M_SOF9:            /* Extended sequential, arithmetic */
    case M_SOF10:           /* Progressive, arithmetic */
    case M_SOF11:           /* Lossless, arithmetic */
    case M_SOF13:           /* Differential sequential, arithmetic */
    case M_SOF14:           /* Differential progressive, arithmetic */
    case M_SOF15:           /* Differential lossless, arithmetic */
      if (verbose)
       process_SOFn(marker);
      else
       skip_variable();
      break;

    case M_SOS:                    /* stop before hitting compressed data */
      return marker;

    case M_EOI:                    /* in case it's a tables-only JPEG stream */
      return marker;

    case M_COM:
      process_COM();
      break;

    case M_APP12:
      /* Some digital camera makers put useful textual information into
       * APP12 markers, so we print those out too when in -verbose mode.
       */
      if (verbose) {
       printf("APP12 contains:\n");
       process_COM();
      } else
       skip_variable();
      break;

    default:                /* Anything else just gets skipped */
      skip_variable();             /* we assume it has a parameter count... */
      break;
    }
  } /* end loop */
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void skip_variable ( void  ) [static]

Definition at line 195 of file rdjpgcom.c.

{
  unsigned int length;

  /* Get the marker parameter length count */
  length = read_2_bytes();
  /* Length includes itself, so must be at least 2 */
  if (length < 2)
    ERREXIT("Erroneous JPEG marker length");
  length -= 2;
  /* Skip over the remaining bytes */
  while (length > 0) {
    (void) read_1_byte();
    length--;
  }
}

Here is the call graph for this function:

static void usage ( void  ) [static]

Definition at line 393 of file rdjpgcom.c.

{
  fprintf(stderr, "rdjpgcom displays any textual comments in a JPEG file.\n");

  fprintf(stderr, "Usage: %s [switches] [inputfile]\n", progname);

  fprintf(stderr, "Switches (names may be abbreviated):\n");
  fprintf(stderr, "  -verbose    Also display dimensions of JPEG image\n");

  exit(EXIT_FAILURE);
}

Variable Documentation

FILE* infile [static]

Definition at line 61 of file rdjpgcom.c.

const char* progname [static]

Definition at line 389 of file rdjpgcom.c.