|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* rdjpgcom.c
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* Copyright (C) 1994-1997, Thomas G. Lane.
|
|
kusano |
7d535a |
* Modified 2009 by Bill Allombert, Guido Vollbeding.
|
|
kusano |
7d535a |
* This file is part of the Independent JPEG Group's software.
|
|
kusano |
7d535a |
* For conditions of distribution and use, see the accompanying README file.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* This file contains a very simple stand-alone application that displays
|
|
kusano |
7d535a |
* the text in COM (comment) markers in a JFIF file.
|
|
kusano |
7d535a |
* This may be useful as an example of the minimum logic needed to parse
|
|
kusano |
7d535a |
* JPEG markers.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#define JPEG_CJPEG_DJPEG /* to get the command-line config symbols */
|
|
kusano |
7d535a |
#include "jinclude.h" /* get auto-config symbols, <stdio.h> */</stdio.h>
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifdef HAVE_LOCALE_H
|
|
kusano |
7d535a |
#include <locale.h> /* Bill Allombert: use locale for isprint */</locale.h>
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
#include <ctype.h> /* to declare isupper(), tolower() */</ctype.h>
|
|
kusano |
7d535a |
#ifdef USE_SETMODE
|
|
kusano |
7d535a |
#include <fcntl.h> /* to declare setmode()'s parameter macros */</fcntl.h>
|
|
kusano |
7d535a |
/* If you have setmode() but not <io.h>, just delete this line: */</io.h>
|
|
kusano |
7d535a |
#include <io.h> /* to declare setmode() */</io.h>
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifdef USE_CCOMMAND /* command-line reader for Macintosh */
|
|
kusano |
7d535a |
#ifdef __MWERKS__
|
|
kusano |
7d535a |
#include <sioux.h> /* Metrowerks needs this */</sioux.h>
|
|
kusano |
7d535a |
#include <console.h> /* ... and this */</console.h>
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
#ifdef THINK_C
|
|
kusano |
7d535a |
#include <console.h> /* Think declares it here */</console.h>
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
|
|
kusano |
7d535a |
#define READ_BINARY "r"
|
|
kusano |
7d535a |
#else
|
|
kusano |
7d535a |
#ifdef VMS /* VMS is very nonstandard */
|
|
kusano |
7d535a |
#define READ_BINARY "rb", "ctx=stm"
|
|
kusano |
7d535a |
#else /* standard ANSI-compliant case */
|
|
kusano |
7d535a |
#define READ_BINARY "rb"
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#ifndef EXIT_FAILURE /* define exit() codes if not provided */
|
|
kusano |
7d535a |
#define EXIT_FAILURE 1
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
#ifndef EXIT_SUCCESS
|
|
kusano |
7d535a |
#ifdef VMS
|
|
kusano |
7d535a |
#define EXIT_SUCCESS 1 /* VMS is very nonstandard */
|
|
kusano |
7d535a |
#else
|
|
kusano |
7d535a |
#define EXIT_SUCCESS 0
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* These macros are used to read the input file.
|
|
kusano |
7d535a |
* To reuse this code in another application, you might need to change these.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static FILE * infile; /* input JPEG file */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Return next input byte, or EOF if no more */
|
|
kusano |
7d535a |
#define NEXTBYTE() getc(infile)
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Error exit handler */
|
|
kusano |
7d535a |
#define ERREXIT(msg) (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE))
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Read one byte, testing for EOF */
|
|
kusano |
7d535a |
static int
|
|
kusano |
7d535a |
read_1_byte (void)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int c;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
c = NEXTBYTE();
|
|
kusano |
7d535a |
if (c == EOF)
|
|
kusano |
7d535a |
ERREXIT("Premature EOF in JPEG file");
|
|
kusano |
7d535a |
return c;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Read 2 bytes, convert to unsigned int */
|
|
kusano |
7d535a |
/* All 2-byte quantities in JPEG markers are MSB first */
|
|
kusano |
7d535a |
static unsigned int
|
|
kusano |
7d535a |
read_2_bytes (void)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int c1, c2;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
c1 = NEXTBYTE();
|
|
kusano |
7d535a |
if (c1 == EOF)
|
|
kusano |
7d535a |
ERREXIT("Premature EOF in JPEG file");
|
|
kusano |
7d535a |
c2 = NEXTBYTE();
|
|
kusano |
7d535a |
if (c2 == EOF)
|
|
kusano |
7d535a |
ERREXIT("Premature EOF in JPEG file");
|
|
kusano |
7d535a |
return (((unsigned int) c1) << 8) + ((unsigned int) c2);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* JPEG markers consist of one or more 0xFF bytes, followed by a marker
|
|
kusano |
7d535a |
* code byte (which is not an FF). Here are the marker codes of interest
|
|
kusano |
7d535a |
* in this program. (See jdmarker.c for a more complete list.)
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#define M_SOF0 0xC0 /* Start Of Frame N */
|
|
kusano |
7d535a |
#define M_SOF1 0xC1 /* N indicates which compression process */
|
|
kusano |
7d535a |
#define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */
|
|
kusano |
7d535a |
#define M_SOF3 0xC3
|
|
kusano |
7d535a |
#define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */
|
|
kusano |
7d535a |
#define M_SOF6 0xC6
|
|
kusano |
7d535a |
#define M_SOF7 0xC7
|
|
kusano |
7d535a |
#define M_SOF9 0xC9
|
|
kusano |
7d535a |
#define M_SOF10 0xCA
|
|
kusano |
7d535a |
#define M_SOF11 0xCB
|
|
kusano |
7d535a |
#define M_SOF13 0xCD
|
|
kusano |
7d535a |
#define M_SOF14 0xCE
|
|
kusano |
7d535a |
#define M_SOF15 0xCF
|
|
kusano |
7d535a |
#define M_SOI 0xD8 /* Start Of Image (beginning of datastream) */
|
|
kusano |
7d535a |
#define M_EOI 0xD9 /* End Of Image (end of datastream) */
|
|
kusano |
7d535a |
#define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
|
|
kusano |
7d535a |
#define M_APP0 0xE0 /* Application-specific marker, type N */
|
|
kusano |
7d535a |
#define M_APP12 0xEC /* (we don't bother to list all 16 APPn's) */
|
|
kusano |
7d535a |
#define M_COM 0xFE /* COMment */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Find the next JPEG marker and return its marker code.
|
|
kusano |
7d535a |
* We expect at least one FF byte, possibly more if the compressor used FFs
|
|
kusano |
7d535a |
* to pad the file.
|
|
kusano |
7d535a |
* There could also be non-FF garbage between markers. The treatment of such
|
|
kusano |
7d535a |
* garbage is unspecified; we choose to skip over it but emit a warning msg.
|
|
kusano |
7d535a |
* NB: this routine must not be used after seeing SOS marker, since it will
|
|
kusano |
7d535a |
* not deal correctly with FF/00 sequences in the compressed image data...
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int
|
|
kusano |
7d535a |
next_marker (void)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int c;
|
|
kusano |
7d535a |
int discarded_bytes = 0;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Find 0xFF byte; count and skip any non-FFs. */
|
|
kusano |
7d535a |
c = read_1_byte();
|
|
kusano |
7d535a |
while (c != 0xFF) {
|
|
kusano |
7d535a |
discarded_bytes++;
|
|
kusano |
7d535a |
c = read_1_byte();
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
/* Get marker code byte, swallowing any duplicate FF bytes. Extra FFs
|
|
kusano |
7d535a |
* are legal as pad bytes, so don't count them in discarded_bytes.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
do {
|
|
kusano |
7d535a |
c = read_1_byte();
|
|
kusano |
7d535a |
} while (c == 0xFF);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (discarded_bytes != 0) {
|
|
kusano |
7d535a |
fprintf(stderr, "Warning: garbage data found in JPEG file\n");
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return c;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Read the initial marker, which should be SOI.
|
|
kusano |
7d535a |
* For a JFIF file, the first two bytes of the file should be literally
|
|
kusano |
7d535a |
* 0xFF M_SOI. To be more general, we could use next_marker, but if the
|
|
kusano |
7d535a |
* input file weren't actually JPEG at all, next_marker might read the whole
|
|
kusano |
7d535a |
* file and then return a misleading error message...
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int
|
|
kusano |
7d535a |
first_marker (void)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int c1, c2;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
c1 = NEXTBYTE();
|
|
kusano |
7d535a |
c2 = NEXTBYTE();
|
|
kusano |
7d535a |
if (c1 != 0xFF || c2 != M_SOI)
|
|
kusano |
7d535a |
ERREXIT("Not a JPEG file");
|
|
kusano |
7d535a |
return c2;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Most types of marker are followed by a variable-length parameter segment.
|
|
kusano |
7d535a |
* This routine skips over the parameters for any marker we don't otherwise
|
|
kusano |
7d535a |
* want to process.
|
|
kusano |
7d535a |
* Note that we MUST skip the parameter segment explicitly in order not to
|
|
kusano |
7d535a |
* be fooled by 0xFF bytes that might appear within the parameter segment;
|
|
kusano |
7d535a |
* such bytes do NOT introduce new markers.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void
|
|
kusano |
7d535a |
skip_variable (void)
|
|
kusano |
7d535a |
/* Skip over an unknown or uninteresting variable-length marker */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
unsigned int length;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Get the marker parameter length count */
|
|
kusano |
7d535a |
length = read_2_bytes();
|
|
kusano |
7d535a |
/* Length includes itself, so must be at least 2 */
|
|
kusano |
7d535a |
if (length < 2)
|
|
kusano |
7d535a |
ERREXIT("Erroneous JPEG marker length");
|
|
kusano |
7d535a |
length -= 2;
|
|
kusano |
7d535a |
/* Skip over the remaining bytes */
|
|
kusano |
7d535a |
while (length > 0) {
|
|
kusano |
7d535a |
(void) read_1_byte();
|
|
kusano |
7d535a |
length--;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Process a COM marker.
|
|
kusano |
7d535a |
* We want to print out the marker contents as legible text;
|
|
kusano |
7d535a |
* we must guard against non-text junk and varying newline representations.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void
|
|
kusano |
7d535a |
process_COM (int raw)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
unsigned int length;
|
|
kusano |
7d535a |
int ch;
|
|
kusano |
7d535a |
int lastch = 0;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Bill Allombert: set locale properly for isprint */
|
|
kusano |
7d535a |
#ifdef HAVE_LOCALE_H
|
|
kusano |
7d535a |
setlocale(LC_CTYPE, "");
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Get the marker parameter length count */
|
|
kusano |
7d535a |
length = read_2_bytes();
|
|
kusano |
7d535a |
/* Length includes itself, so must be at least 2 */
|
|
kusano |
7d535a |
if (length < 2)
|
|
kusano |
7d535a |
ERREXIT("Erroneous JPEG marker length");
|
|
kusano |
7d535a |
length -= 2;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
while (length > 0) {
|
|
kusano |
7d535a |
ch = read_1_byte();
|
|
kusano |
7d535a |
if (raw) {
|
|
kusano |
7d535a |
putc(ch, stdout);
|
|
kusano |
7d535a |
/* Emit the character in a readable form.
|
|
kusano |
7d535a |
* Nonprintables are converted to \nnn form,
|
|
kusano |
7d535a |
* while \ is converted to \\.
|
|
kusano |
7d535a |
* Newlines in CR, CR/LF, or LF form will be printed as one newline.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
} else if (ch == '\r') {
|
|
kusano |
7d535a |
printf("\n");
|
|
kusano |
7d535a |
} else if (ch == '\n') {
|
|
kusano |
7d535a |
if (lastch != '\r')
|
|
kusano |
7d535a |
printf("\n");
|
|
kusano |
7d535a |
} else if (ch == '\\') {
|
|
kusano |
7d535a |
printf("\\\\");
|
|
kusano |
7d535a |
} else if (isprint(ch)) {
|
|
kusano |
7d535a |
putc(ch, stdout);
|
|
kusano |
7d535a |
} else {
|
|
kusano |
7d535a |
printf("\\%03o", ch);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
lastch = ch;
|
|
kusano |
7d535a |
length--;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
printf("\n");
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Bill Allombert: revert to C locale */
|
|
kusano |
7d535a |
#ifdef HAVE_LOCALE_H
|
|
kusano |
7d535a |
setlocale(LC_CTYPE, "C");
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Process a SOFn marker.
|
|
kusano |
7d535a |
* This code is only needed if you want to know the image dimensions...
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void
|
|
kusano |
7d535a |
process_SOFn (int marker)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
unsigned int length;
|
|
kusano |
7d535a |
unsigned int image_height, image_width;
|
|
kusano |
7d535a |
int data_precision, num_components;
|
|
kusano |
7d535a |
const char * process;
|
|
kusano |
7d535a |
int ci;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
length = read_2_bytes(); /* usual parameter length count */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
data_precision = read_1_byte();
|
|
kusano |
7d535a |
image_height = read_2_bytes();
|
|
kusano |
7d535a |
image_width = read_2_bytes();
|
|
kusano |
7d535a |
num_components = read_1_byte();
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
switch (marker) {
|
|
kusano |
7d535a |
case M_SOF0: process = "Baseline"; break;
|
|
kusano |
7d535a |
case M_SOF1: process = "Extended sequential"; break;
|
|
kusano |
7d535a |
case M_SOF2: process = "Progressive"; break;
|
|
kusano |
7d535a |
case M_SOF3: process = "Lossless"; break;
|
|
kusano |
7d535a |
case M_SOF5: process = "Differential sequential"; break;
|
|
kusano |
7d535a |
case M_SOF6: process = "Differential progressive"; break;
|
|
kusano |
7d535a |
case M_SOF7: process = "Differential lossless"; break;
|
|
kusano |
7d535a |
case M_SOF9: process = "Extended sequential, arithmetic coding"; break;
|
|
kusano |
7d535a |
case M_SOF10: process = "Progressive, arithmetic coding"; break;
|
|
kusano |
7d535a |
case M_SOF11: process = "Lossless, arithmetic coding"; break;
|
|
kusano |
7d535a |
case M_SOF13: process = "Differential sequential, arithmetic coding"; break;
|
|
kusano |
7d535a |
case M_SOF14: process = "Differential progressive, arithmetic coding"; break;
|
|
kusano |
7d535a |
case M_SOF15: process = "Differential lossless, arithmetic coding"; break;
|
|
kusano |
7d535a |
default: process = "Unknown"; break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
printf("JPEG image is %uw * %uh, %d color components, %d bits per sample\n",
|
|
kusano |
7d535a |
image_width, image_height, num_components, data_precision);
|
|
kusano |
7d535a |
printf("JPEG process: %s\n", process);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (length != (unsigned int) (8 + num_components * 3))
|
|
kusano |
7d535a |
ERREXIT("Bogus SOF marker length");
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
for (ci = 0; ci < num_components; ci++) {
|
|
kusano |
7d535a |
(void) read_1_byte(); /* Component ID code */
|
|
kusano |
7d535a |
(void) read_1_byte(); /* H, V sampling factors */
|
|
kusano |
7d535a |
(void) read_1_byte(); /* Quantization table number */
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Parse the marker stream until SOS or EOI is seen;
|
|
kusano |
7d535a |
* display any COM markers.
|
|
kusano |
7d535a |
* While the companion program wrjpgcom will always insert COM markers before
|
|
kusano |
7d535a |
* SOFn, other implementations might not, so we scan to SOS before stopping.
|
|
kusano |
7d535a |
* If we were only interested in the image dimensions, we would stop at SOFn.
|
|
kusano |
7d535a |
* (Conversely, if we only cared about COM markers, there would be no need
|
|
kusano |
7d535a |
* for special code to handle SOFn; we could treat it like other markers.)
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int
|
|
kusano |
7d535a |
scan_JPEG_header (int verbose, int raw)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int marker;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Expect SOI at start of file */
|
|
kusano |
7d535a |
if (first_marker() != M_SOI)
|
|
kusano |
7d535a |
ERREXIT("Expected SOI marker first");
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Scan miscellaneous markers until we reach SOS. */
|
|
kusano |
7d535a |
for (;;) {
|
|
kusano |
7d535a |
marker = next_marker();
|
|
kusano |
7d535a |
switch (marker) {
|
|
kusano |
7d535a |
/* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be,
|
|
kusano |
7d535a |
* treated as SOFn. C4 in particular is actually DHT.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
case M_SOF0: /* Baseline */
|
|
kusano |
7d535a |
case M_SOF1: /* Extended sequential, Huffman */
|
|
kusano |
7d535a |
case M_SOF2: /* Progressive, Huffman */
|
|
kusano |
7d535a |
case M_SOF3: /* Lossless, Huffman */
|
|
kusano |
7d535a |
case M_SOF5: /* Differential sequential, Huffman */
|
|
kusano |
7d535a |
case M_SOF6: /* Differential progressive, Huffman */
|
|
kusano |
7d535a |
case M_SOF7: /* Differential lossless, Huffman */
|
|
kusano |
7d535a |
case M_SOF9: /* Extended sequential, arithmetic */
|
|
kusano |
7d535a |
case M_SOF10: /* Progressive, arithmetic */
|
|
kusano |
7d535a |
case M_SOF11: /* Lossless, arithmetic */
|
|
kusano |
7d535a |
case M_SOF13: /* Differential sequential, arithmetic */
|
|
kusano |
7d535a |
case M_SOF14: /* Differential progressive, arithmetic */
|
|
kusano |
7d535a |
case M_SOF15: /* Differential lossless, arithmetic */
|
|
kusano |
7d535a |
if (verbose)
|
|
kusano |
7d535a |
process_SOFn(marker);
|
|
kusano |
7d535a |
else
|
|
kusano |
7d535a |
skip_variable();
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
case M_SOS: /* stop before hitting compressed data */
|
|
kusano |
7d535a |
return marker;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
case M_EOI: /* in case it's a tables-only JPEG stream */
|
|
kusano |
7d535a |
return marker;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
case M_COM:
|
|
kusano |
7d535a |
process_COM(raw);
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
case M_APP12:
|
|
kusano |
7d535a |
/* Some digital camera makers put useful textual information into
|
|
kusano |
7d535a |
* APP12 markers, so we print those out too when in -verbose mode.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
if (verbose) {
|
|
kusano |
7d535a |
printf("APP12 contains:\n");
|
|
kusano |
7d535a |
process_COM(raw);
|
|
kusano |
7d535a |
} else
|
|
kusano |
7d535a |
skip_variable();
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
default: /* Anything else just gets skipped */
|
|
kusano |
7d535a |
skip_variable(); /* we assume it has a parameter count... */
|
|
kusano |
7d535a |
break;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
} /* end loop */
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Command line parsing code */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static const char * progname; /* program name for error messages */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static void
|
|
kusano |
7d535a |
usage (void)
|
|
kusano |
7d535a |
/* complain about bad command line */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
fprintf(stderr, "rdjpgcom displays any textual comments in a JPEG file.\n");
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
fprintf(stderr, "Usage: %s [switches] [inputfile]\n", progname);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
fprintf(stderr, "Switches (names may be abbreviated):\n");
|
|
kusano |
7d535a |
fprintf(stderr, " -raw Display non-printable characters in comments (unsafe)\n");
|
|
kusano |
7d535a |
fprintf(stderr, " -verbose Also display dimensions of JPEG image\n");
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
exit(EXIT_FAILURE);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
static int
|
|
kusano |
7d535a |
keymatch (char * arg, const char * keyword, int minchars)
|
|
kusano |
7d535a |
/* Case-insensitive matching of (possibly abbreviated) keyword switches. */
|
|
kusano |
7d535a |
/* keyword is the constant keyword (must be lower case already), */
|
|
kusano |
7d535a |
/* minchars is length of minimum legal abbreviation. */
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
register int ca, ck;
|
|
kusano |
7d535a |
register int nmatched = 0;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
while ((ca = *arg++) != '\0') {
|
|
kusano |
7d535a |
if ((ck = *keyword++) == '\0')
|
|
kusano |
7d535a |
return 0; /* arg longer than keyword, no good */
|
|
kusano |
7d535a |
if (isupper(ca)) /* force arg to lcase (assume ck is already) */
|
|
kusano |
7d535a |
ca = tolower(ca);
|
|
kusano |
7d535a |
if (ca != ck)
|
|
kusano |
7d535a |
return 0; /* no good */
|
|
kusano |
7d535a |
nmatched++; /* count matched characters */
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
/* reached end of argument; fail if it's too short for unique abbrev */
|
|
kusano |
7d535a |
if (nmatched < minchars)
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
return 1; /* A-OK */
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* The main program.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int
|
|
kusano |
7d535a |
main (int argc, char **argv)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
int argn;
|
|
kusano |
7d535a |
char * arg;
|
|
kusano |
7d535a |
int verbose = 0, raw = 0;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* On Mac, fetch a command line. */
|
|
kusano |
7d535a |
#ifdef USE_CCOMMAND
|
|
kusano |
7d535a |
argc = ccommand(&argv);
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
progname = argv[0];
|
|
kusano |
7d535a |
if (progname == NULL || progname[0] == 0)
|
|
kusano |
7d535a |
progname = "rdjpgcom"; /* in case C library doesn't provide it */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Parse switches, if any */
|
|
kusano |
7d535a |
for (argn = 1; argn < argc; argn++) {
|
|
kusano |
7d535a |
arg = argv[argn];
|
|
kusano |
7d535a |
if (arg[0] != '-')
|
|
kusano |
7d535a |
break; /* not switch, must be file name */
|
|
kusano |
7d535a |
arg++; /* advance over '-' */
|
|
kusano |
7d535a |
if (keymatch(arg, "verbose", 1)) {
|
|
kusano |
7d535a |
verbose++;
|
|
kusano |
7d535a |
} else if (keymatch(arg, "raw", 1)) {
|
|
kusano |
7d535a |
raw = 1;
|
|
kusano |
7d535a |
} else
|
|
kusano |
7d535a |
usage();
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Open the input file. */
|
|
kusano |
7d535a |
/* Unix style: expect zero or one file name */
|
|
kusano |
7d535a |
if (argn < argc-1) {
|
|
kusano |
7d535a |
fprintf(stderr, "%s: only one input file\n", progname);
|
|
kusano |
7d535a |
usage();
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (argn < argc) {
|
|
kusano |
7d535a |
if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
|
|
kusano |
7d535a |
fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
|
|
kusano |
7d535a |
exit(EXIT_FAILURE);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
} else {
|
|
kusano |
7d535a |
/* default input file is stdin */
|
|
kusano |
7d535a |
#ifdef USE_SETMODE /* need to hack file mode? */
|
|
kusano |
7d535a |
setmode(fileno(stdin), O_BINARY);
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
#ifdef USE_FDOPEN /* need to re-open in binary mode? */
|
|
kusano |
7d535a |
if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
|
|
kusano |
7d535a |
fprintf(stderr, "%s: can't open stdin\n", progname);
|
|
kusano |
7d535a |
exit(EXIT_FAILURE);
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
#else
|
|
kusano |
7d535a |
infile = stdin;
|
|
kusano |
7d535a |
#endif
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Scan the JPEG headers. */
|
|
kusano |
7d535a |
(void) scan_JPEG_header(verbose, raw);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* All done. */
|
|
kusano |
7d535a |
exit(EXIT_SUCCESS);
|
|
kusano |
7d535a |
return 0; /* suppress no-return-value warnings */
|
|
kusano |
7d535a |
}
|