Back to index

plt-scheme  4.2.1
Defines | Functions | Variables
wrjpgcom.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 WRITE_BINARY   "wb"
#define EXIT_FAILURE   1
#define EXIT_SUCCESS   0
#define MAX_COM_LENGTH   65000L /* must be <= 65533 in any case */
#define NEXTBYTE()   getc(infile)
#define PUTBYTE(x)   putc((x), outfile)
#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_COM   0xFE /* COMment */

Functions

voidmalloc ()
static int read_1_byte (void)
static unsigned int read_2_bytes (void)
static void write_1_byte (int c)
static void write_2_bytes (unsigned int val)
static void write_marker (int marker)
static void copy_rest_of_file (void)
static int next_marker (void)
static int first_marker (void)
static void copy_variable (void)
static void skip_variable (void)
static int scan_JPEG_header (int keep_COM)
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 FILE * outfile
static const char * progname

Define Documentation

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

Definition at line 87 of file wrjpgcom.c.

#define EXIT_FAILURE   1

Definition at line 51 of file wrjpgcom.c.

#define EXIT_SUCCESS   0

Definition at line 57 of file wrjpgcom.c.

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

Definition at line 14 of file wrjpgcom.c.

#define M_COM   0xFE /* COMment */

Definition at line 173 of file wrjpgcom.c.

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

Definition at line 171 of file wrjpgcom.c.

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

Definition at line 157 of file wrjpgcom.c.

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

Definition at line 158 of file wrjpgcom.c.

#define M_SOF10   0xCA

Definition at line 165 of file wrjpgcom.c.

#define M_SOF11   0xCB

Definition at line 166 of file wrjpgcom.c.

#define M_SOF13   0xCD

Definition at line 167 of file wrjpgcom.c.

#define M_SOF14   0xCE

Definition at line 168 of file wrjpgcom.c.

#define M_SOF15   0xCF

Definition at line 169 of file wrjpgcom.c.

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

Definition at line 159 of file wrjpgcom.c.

#define M_SOF3   0xC3

Definition at line 160 of file wrjpgcom.c.

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

Definition at line 161 of file wrjpgcom.c.

#define M_SOF6   0xC6

Definition at line 162 of file wrjpgcom.c.

#define M_SOF7   0xC7

Definition at line 163 of file wrjpgcom.c.

#define M_SOF9   0xC9

Definition at line 164 of file wrjpgcom.c.

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

Definition at line 170 of file wrjpgcom.c.

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

Definition at line 172 of file wrjpgcom.c.

#define MAX_COM_LENGTH   65000L /* must be <= 65533 in any case */

Definition at line 66 of file wrjpgcom.c.

#define NEXTBYTE ( )    getc(infile)

Definition at line 78 of file wrjpgcom.c.

#define PUTBYTE (   x)    putc((x), outfile)

Definition at line 83 of file wrjpgcom.c.

#define READ_BINARY   "rb"

Definition at line 45 of file wrjpgcom.c.

#define WRITE_BINARY   "wb"

Definition at line 46 of file wrjpgcom.c.


Function Documentation

static void copy_rest_of_file ( void  ) [static]

Definition at line 142 of file wrjpgcom.c.

{
  int c;

  while ((c = NEXTBYTE()) != EOF)
    PUTBYTE(c);
}

Here is the caller graph for this function:

static void copy_variable ( void  ) [static]

Definition at line 244 of file wrjpgcom.c.

{
  unsigned int length;

  /* Get the marker parameter length count */
  length = read_2_bytes();
  write_2_bytes(length);
  /* 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) {
    write_1_byte(read_1_byte());
    length--;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int first_marker ( void  ) [static]

Definition at line 222 of file wrjpgcom.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 384 of file wrjpgcom.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 413 of file wrjpgcom.c.

{
  int argn;
  char * arg;
  int keep_COM = 1;
  char * comment_arg = NULL;
  FILE * comment_file = NULL;
  unsigned int comment_length = 0;
  int marker;

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

  progname = argv[0];
  if (progname == NULL || progname[0] == 0)
    progname = "wrjpgcom";  /* 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, "replace", 1)) {
      keep_COM = 0;
    } else if (keymatch(arg, "cfile", 2)) {
      if (++argn >= argc) usage();
      if ((comment_file = fopen(argv[argn], "r")) == NULL) {
       fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
       exit(EXIT_FAILURE);
      }
    } else if (keymatch(arg, "comment", 1)) {
      if (++argn >= argc) usage();
      comment_arg = argv[argn];
      /* If the comment text starts with '"', then we are probably running
       * under MS-DOG and must parse out the quoted string ourselves.  Sigh.
       */
      if (comment_arg[0] == '"') {
       comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH);
       if (comment_arg == NULL)
         ERREXIT("Insufficient memory");
       strcpy(comment_arg, argv[argn]+1);
       for (;;) {
         comment_length = (unsigned int) strlen(comment_arg);
         if (comment_length > 0 && comment_arg[comment_length-1] == '"') {
           comment_arg[comment_length-1] = '\0'; /* zap terminating quote */
           break;
         }
         if (++argn >= argc)
           ERREXIT("Missing ending quote mark");
         strcat(comment_arg, " ");
         strcat(comment_arg, argv[argn]);
       }
      }
      comment_length = (unsigned int) strlen(comment_arg);
    } else
      usage();
  }

  /* Cannot use both -comment and -cfile. */
  if (comment_arg != NULL && comment_file != NULL)
    usage();
  /* If there is neither -comment nor -cfile, we will read the comment text
   * from stdin; in this case there MUST be an input JPEG file name.
   */
  if (comment_arg == NULL && comment_file == NULL && argn >= argc)
    usage();

  /* Open the input file. */
  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
  }

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

  /* Collect comment text from comment_file or stdin, if necessary */
  if (comment_arg == NULL) {
    FILE * src_file;
    int c;

    comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH);
    if (comment_arg == NULL)
      ERREXIT("Insufficient memory");
    comment_length = 0;
    src_file = (comment_file != NULL ? comment_file : stdin);
    while ((c = getc(src_file)) != EOF) {
      if (comment_length >= (unsigned int) MAX_COM_LENGTH) {
       fprintf(stderr, "Comment text may not exceed %u bytes\n",
              (unsigned int) MAX_COM_LENGTH);
       exit(EXIT_FAILURE);
      }
      comment_arg[comment_length++] = (char) c;
    }
    if (comment_file != NULL)
      fclose(comment_file);
  }

  /* Copy JPEG headers until SOFn marker;
   * we will insert the new comment marker just before SOFn.
   * This (a) causes the new comment to appear after, rather than before,
   * existing comments; and (b) ensures that comments come after any JFIF
   * or JFXX markers, as required by the JFIF specification.
   */
  marker = scan_JPEG_header(keep_COM);
  /* Insert the new COM marker, but only if nonempty text has been supplied */
  if (comment_length > 0) {
    write_marker(M_COM);
    write_2_bytes(comment_length + 2);
    while (comment_length > 0) {
      write_1_byte(*comment_arg++);
      comment_length--;
    }
  }
  /* Duplicate the remainder of the source file.
   * Note that any COM markers occuring after SOF will not be touched.
   */
  write_marker(marker);
  copy_rest_of_file();

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

Here is the call graph for this function:

void* malloc ( )
static int next_marker ( void  ) [static]

Definition at line 187 of file wrjpgcom.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 int read_1_byte ( void  ) [static]

Definition at line 92 of file wrjpgcom.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 105 of file wrjpgcom.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  keep_COM) [static]

Definition at line 289 of file wrjpgcom.c.

{
  int marker;

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

  /* Scan miscellaneous markers until we reach SOFn. */
  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 */
      return marker;

    case M_SOS:                    /* should not see compressed data before SOF */
      ERREXIT("SOS without prior SOFn");
      break;

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

    case M_COM:                    /* Existing COM: conditionally discard */
      if (keep_COM) {
       write_marker(marker);
       copy_variable();
      } else {
       skip_variable();
      }
      break;

    default:                /* Anything else just gets copied */
      write_marker(marker);
      copy_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 264 of file wrjpgcom.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 351 of file wrjpgcom.c.

{
  fprintf(stderr, "wrjpgcom inserts a textual comment in a JPEG file.\n");
  fprintf(stderr, "You can add to or replace any existing comment(s).\n");

  fprintf(stderr, "Usage: %s [switches] ", progname);
#ifdef TWO_FILE_COMMANDLINE
  fprintf(stderr, "inputfile outputfile\n");
#else
  fprintf(stderr, "[inputfile]\n");
#endif

  fprintf(stderr, "Switches (names may be abbreviated):\n");
  fprintf(stderr, "  -replace         Delete any existing comments\n");
  fprintf(stderr, "  -comment \"text\"  Insert comment with given text\n");
  fprintf(stderr, "  -cfile name      Read comment from named file\n");
  fprintf(stderr, "Notice that you must put quotes around the comment text\n");
  fprintf(stderr, "when you use -comment.\n");
  fprintf(stderr, "If you do not give either -comment or -cfile on the command line,\n");
  fprintf(stderr, "then the comment text is read from standard input.\n");
  fprintf(stderr, "It can be multiple lines, up to %u characters total.\n",
         (unsigned int) MAX_COM_LENGTH);
#ifndef TWO_FILE_COMMANDLINE
  fprintf(stderr, "You must specify an input JPEG file name when supplying\n");
  fprintf(stderr, "comment text from standard input.\n");
#endif

  exit(EXIT_FAILURE);
}
static void write_1_byte ( int  c) [static]

Definition at line 122 of file wrjpgcom.c.

{
  PUTBYTE(c);
}

Here is the caller graph for this function:

static void write_2_bytes ( unsigned int  val) [static]

Definition at line 128 of file wrjpgcom.c.

{
  PUTBYTE((val >> 8) & 0xFF);
  PUTBYTE(val & 0xFF);
}

Here is the caller graph for this function:

static void write_marker ( int  marker) [static]

Definition at line 135 of file wrjpgcom.c.

{
  PUTBYTE(0xFF);
  PUTBYTE(marker);
}

Here is the caller graph for this function:


Variable Documentation

FILE* infile [static]

Definition at line 75 of file wrjpgcom.c.

FILE* outfile [static]

Definition at line 80 of file wrjpgcom.c.

const char* progname [static]

Definition at line 347 of file wrjpgcom.c.