Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

rdjpgcom.c File Reference

#include "jinclude.h"
#include <ctype.h>

Go to the source code of this file.


Defines

#define JPEG_CJPEG_DJPEG
#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
#define M_SOF1   0xC1
#define M_SOF2   0xC2
#define M_SOF3   0xC3
#define M_SOF5   0xC5
#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
#define M_EOI   0xD9
#define M_SOS   0xDA
#define M_APP0   0xE0
#define M_APP12   0xEC
#define M_COM   0xFE

Functions

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

Variables

FILE * infile
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.

Referenced by main(), and usage().

#define EXIT_SUCCESS   0
 

Definition at line 51 of file rdjpgcom.c.

Referenced by main().

#define JPEG_CJPEG_DJPEG
 

Definition at line 14 of file rdjpgcom.c.

#define M_APP0   0xE0
 

Definition at line 122 of file rdjpgcom.c.

#define M_APP12   0xEC
 

Definition at line 123 of file rdjpgcom.c.

Referenced by scan_JPEG_header().

#define M_COM   0xFE
 

Definition at line 124 of file rdjpgcom.c.

Referenced by scan_JPEG_header().

#define M_EOI   0xD9
 

Definition at line 120 of file rdjpgcom.c.

Referenced by scan_JPEG_header().

#define M_SOF0   0xC0
 

Definition at line 106 of file rdjpgcom.c.

Referenced by process_SOFn(), and scan_JPEG_header().

#define M_SOF1   0xC1
 

Definition at line 107 of file rdjpgcom.c.

Referenced by process_SOFn(), and scan_JPEG_header().

#define M_SOF10   0xCA
 

Definition at line 114 of file rdjpgcom.c.

Referenced by process_SOFn(), and scan_JPEG_header().

#define M_SOF11   0xCB
 

Definition at line 115 of file rdjpgcom.c.

Referenced by process_SOFn(), and scan_JPEG_header().

#define M_SOF13   0xCD
 

Definition at line 116 of file rdjpgcom.c.

Referenced by process_SOFn(), and scan_JPEG_header().

#define M_SOF14   0xCE
 

Definition at line 117 of file rdjpgcom.c.

Referenced by process_SOFn(), and scan_JPEG_header().

#define M_SOF15   0xCF
 

Definition at line 118 of file rdjpgcom.c.

Referenced by process_SOFn(), and scan_JPEG_header().

#define M_SOF2   0xC2
 

Definition at line 108 of file rdjpgcom.c.

Referenced by process_SOFn(), and scan_JPEG_header().

#define M_SOF3   0xC3
 

Definition at line 109 of file rdjpgcom.c.

Referenced by process_SOFn(), and scan_JPEG_header().

#define M_SOF5   0xC5
 

Definition at line 110 of file rdjpgcom.c.

Referenced by process_SOFn(), and scan_JPEG_header().

#define M_SOF6   0xC6
 

Definition at line 111 of file rdjpgcom.c.

Referenced by process_SOFn(), and scan_JPEG_header().

#define M_SOF7   0xC7
 

Definition at line 112 of file rdjpgcom.c.

Referenced by process_SOFn(), and scan_JPEG_header().

#define M_SOF9   0xC9
 

Definition at line 113 of file rdjpgcom.c.

Referenced by process_SOFn(), and scan_JPEG_header().

#define M_SOI   0xD8
 

Definition at line 119 of file rdjpgcom.c.

Referenced by first_marker(), and scan_JPEG_header().

#define M_SOS   0xDA
 

Definition at line 121 of file rdjpgcom.c.

Referenced by scan_JPEG_header().

 
#define NEXTBYTE      getc(infile)
 

Definition at line 64 of file rdjpgcom.c.

Referenced by copy_rest_of_file(), first_marker(), read_1_byte(), and read_2_bytes().

#define READ_BINARY   "rb"
 

Definition at line 40 of file rdjpgcom.c.

Referenced by main().


Function Documentation

int first_marker void    [static]
 

Definition at line 173 of file rdjpgcom.c.

References ERREXIT, M_SOI, and NEXTBYTE.

Referenced by read_markers(), and scan_JPEG_header().

00174 {
00175   int c1, c2;
00176 
00177   c1 = NEXTBYTE();
00178   c2 = NEXTBYTE();
00179   if (c1 != 0xFF || c2 != M_SOI)
00180     ERREXIT("Not a JPEG file");
00181   return c2;
00182 }

int keymatch char *    arg,
const char *    keyword,
int    minchars
[static]
 

Definition at line 408 of file rdjpgcom.c.

References arg, keyword, and minchars.

00412 {
00413   register int ca, ck;
00414   register int nmatched = 0;
00415 
00416   while ((ca = *arg++) != '\0') {
00417     if ((ck = *keyword++) == '\0')
00418       return 0;                 /* arg longer than keyword, no good */
00419     if (isupper(ca))            /* force arg to lcase (assume ck is already) */
00420       ca = tolower(ca);
00421     if (ca != ck)
00422       return 0;                 /* no good */
00423     nmatched++;                 /* count matched characters */
00424   }
00425   /* reached end of argument; fail if it's too short for unique abbrev */
00426   if (nmatched < minchars)
00427     return 0;
00428   return 1;                     /* A-OK */
00429 }

int main int    argc,
char **    argv
 

Definition at line 437 of file rdjpgcom.c.

References arg, argc, EXIT_FAILURE, EXIT_SUCCESS, fdopen(), infile, keymatch(), O_BINARY, progname, READ_BINARY, scan_JPEG_header(), and usage().

00438 {
00439   int argn;
00440   char * arg;
00441   int verbose = 0;
00442 
00443   /* On Mac, fetch a command line. */
00444 #ifdef USE_CCOMMAND
00445   argc = ccommand(&argv);
00446 #endif
00447 
00448   progname = argv[0];
00449   if (progname == NULL || progname[0] == 0)
00450     progname = "rdjpgcom";      /* in case C library doesn't provide it */
00451 
00452   /* Parse switches, if any */
00453   for (argn = 1; argn < argc; argn++) {
00454     arg = argv[argn];
00455     if (arg[0] != '-')
00456       break;                    /* not switch, must be file name */
00457     arg++;                      /* advance over '-' */
00458     if (keymatch(arg, "verbose", 1)) {
00459       verbose++;
00460     } else
00461       usage();
00462   }
00463 
00464   /* Open the input file. */
00465   /* Unix style: expect zero or one file name */
00466   if (argn < argc-1) {
00467     fprintf(stderr, "%s: only one input file\n", progname);
00468     usage();
00469   }
00470   if (argn < argc) {
00471     if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
00472       fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
00473       exit(EXIT_FAILURE);
00474     }
00475   } else {
00476     /* default input file is stdin */
00477 #ifdef USE_SETMODE              /* need to hack file mode? */
00478     setmode(fileno(stdin), O_BINARY);
00479 #endif
00480 #ifdef USE_FDOPEN               /* need to re-open in binary mode? */
00481     if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
00482       fprintf(stderr, "%s: can't open stdin\n", progname);
00483       exit(EXIT_FAILURE);
00484     }
00485 #else
00486     infile = stdin;
00487 #endif
00488   }
00489 
00490   /* Scan the JPEG headers. */
00491   (void) scan_JPEG_header(verbose);
00492 
00493   /* All done. */
00494   exit(EXIT_SUCCESS);
00495   return 0;                     /* suppress no-return-value warnings */
00496 }

int next_marker void    [static]
 

Definition at line 138 of file rdjpgcom.c.

References c, and read_1_byte().

Referenced by jpeg_resync_to_restart(), read_markers(), read_restart_marker(), and scan_JPEG_header().

00139 {
00140   int c;
00141   int discarded_bytes = 0;
00142 
00143   /* Find 0xFF byte; count and skip any non-FFs. */
00144   c = read_1_byte();
00145   while (c != 0xFF) {
00146     discarded_bytes++;
00147     c = read_1_byte();
00148   }
00149   /* Get marker code byte, swallowing any duplicate FF bytes.  Extra FFs
00150    * are legal as pad bytes, so don't count them in discarded_bytes.
00151    */
00152   do {
00153     c = read_1_byte();
00154   } while (c == 0xFF);
00155 
00156   if (discarded_bytes != 0) {
00157     fprintf(stderr, "Warning: garbage data found in JPEG file\n");
00158   }
00159 
00160   return c;
00161 }

void process_COM void    [static]
 

Definition at line 221 of file rdjpgcom.c.

References ERREXIT, lastch, read_1_byte(), and read_2_bytes().

Referenced by scan_JPEG_header().

00222 {
00223   unsigned int length;
00224   int ch;
00225   int lastch = 0;
00226 
00227   /* Get the marker parameter length count */
00228   length = read_2_bytes();
00229   /* Length includes itself, so must be at least 2 */
00230   if (length < 2)
00231     ERREXIT("Erroneous JPEG marker length");
00232   length -= 2;
00233 
00234   while (length > 0) {
00235     ch = read_1_byte();
00236     /* Emit the character in a readable form.
00237      * Nonprintables are converted to \nnn form,
00238      * while \ is converted to \\.
00239      * Newlines in CR, CR/LF, or LF form will be printed as one newline.
00240      */
00241     if (ch == '\r') {
00242       printf("\n");
00243     } else if (ch == '\n') {
00244       if (lastch != '\r')
00245         printf("\n");
00246     } else if (ch == '\\') {
00247       printf("\\\\");
00248     } else if (isprint(ch)) {
00249       putc(ch, stdout);
00250     } else {
00251       printf("\\%03o", ch);
00252     }
00253     lastch = ch;
00254     length--;
00255   }
00256   printf("\n");
00257 }

void process_SOFn int    marker [static]
 

Definition at line 266 of file rdjpgcom.c.

References ERREXIT, image_height, image_width, M_SOF0, M_SOF1, M_SOF10, M_SOF11, M_SOF13, M_SOF14, M_SOF15, M_SOF2, M_SOF3, M_SOF5, M_SOF6, M_SOF7, M_SOF9, marker, read_1_byte(), and read_2_bytes().

Referenced by scan_JPEG_header().

00267 {
00268   unsigned int length;
00269   unsigned int image_height, image_width;
00270   int data_precision, num_components;
00271   const char * process;
00272   int ci;
00273 
00274   length = read_2_bytes();      /* usual parameter length count */
00275 
00276   data_precision = read_1_byte();
00277   image_height = read_2_bytes();
00278   image_width = read_2_bytes();
00279   num_components = read_1_byte();
00280 
00281   switch (marker) {
00282   case M_SOF0:  process = "Baseline";  break;
00283   case M_SOF1:  process = "Extended sequential";  break;
00284   case M_SOF2:  process = "Progressive";  break;
00285   case M_SOF3:  process = "Lossless";  break;
00286   case M_SOF5:  process = "Differential sequential";  break;
00287   case M_SOF6:  process = "Differential progressive";  break;
00288   case M_SOF7:  process = "Differential lossless";  break;
00289   case M_SOF9:  process = "Extended sequential, arithmetic coding";  break;
00290   case M_SOF10: process = "Progressive, arithmetic coding";  break;
00291   case M_SOF11: process = "Lossless, arithmetic coding";  break;
00292   case M_SOF13: process = "Differential sequential, arithmetic coding";  break;
00293   case M_SOF14: process = "Differential progressive, arithmetic coding"; break;
00294   case M_SOF15: process = "Differential lossless, arithmetic coding";  break;
00295   default:      process = "Unknown";  break;
00296   }
00297 
00298   printf("JPEG image is %uw * %uh, %d color components, %d bits per sample\n",
00299          image_width, image_height, num_components, data_precision);
00300   printf("JPEG process: %s\n", process);
00301 
00302   if (length != (unsigned int) (8 + num_components * 3))
00303     ERREXIT("Bogus SOF marker length");
00304 
00305   for (ci = 0; ci < num_components; ci++) {
00306     (void) read_1_byte();       /* Component ID code */
00307     (void) read_1_byte();       /* H, V sampling factors */
00308     (void) read_1_byte();       /* Quantization table number */
00309   }
00310 }

int read_1_byte void    [static]
 

Definition at line 73 of file rdjpgcom.c.

References c, ERREXIT, and NEXTBYTE.

Referenced by copy_variable(), next_marker(), process_COM(), process_SOFn(), and skip_variable().

00074 {
00075   int c;
00076 
00077   c = NEXTBYTE();
00078   if (c == EOF)
00079     ERREXIT("Premature EOF in JPEG file");
00080   return c;
00081 }

unsigned int read_2_bytes void    [static]
 

Definition at line 86 of file rdjpgcom.c.

References ERREXIT, and NEXTBYTE.

Referenced by copy_variable(), process_COM(), process_SOFn(), and skip_variable().

00087 {
00088   int c1, c2;
00089 
00090   c1 = NEXTBYTE();
00091   if (c1 == EOF)
00092     ERREXIT("Premature EOF in JPEG file");
00093   c2 = NEXTBYTE();
00094   if (c2 == EOF)
00095     ERREXIT("Premature EOF in JPEG file");
00096   return (((unsigned int) c1) << 8) + ((unsigned int) c2);
00097 }

int scan_JPEG_header int    verbose [static]
 

Definition at line 324 of file rdjpgcom.c.

References ERREXIT, first_marker(), M_APP12, M_COM, M_EOI, M_SOF0, M_SOF1, M_SOF10, M_SOF11, M_SOF13, M_SOF14, M_SOF15, M_SOF2, M_SOF3, M_SOF5, M_SOF6, M_SOF7, M_SOF9, M_SOI, M_SOS, marker, next_marker(), process_COM(), process_SOFn(), and skip_variable().

00325 {
00326   int marker;
00327 
00328   /* Expect SOI at start of file */
00329   if (first_marker() != M_SOI)
00330     ERREXIT("Expected SOI marker first");
00331 
00332   /* Scan miscellaneous markers until we reach SOS. */
00333   for (;;) {
00334     marker = next_marker();
00335     switch (marker) {
00336       /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be,
00337        * treated as SOFn.  C4 in particular is actually DHT.
00338        */
00339     case M_SOF0:                /* Baseline */
00340     case M_SOF1:                /* Extended sequential, Huffman */
00341     case M_SOF2:                /* Progressive, Huffman */
00342     case M_SOF3:                /* Lossless, Huffman */
00343     case M_SOF5:                /* Differential sequential, Huffman */
00344     case M_SOF6:                /* Differential progressive, Huffman */
00345     case M_SOF7:                /* Differential lossless, Huffman */
00346     case M_SOF9:                /* Extended sequential, arithmetic */
00347     case M_SOF10:               /* Progressive, arithmetic */
00348     case M_SOF11:               /* Lossless, arithmetic */
00349     case M_SOF13:               /* Differential sequential, arithmetic */
00350     case M_SOF14:               /* Differential progressive, arithmetic */
00351     case M_SOF15:               /* Differential lossless, arithmetic */
00352       if (verbose)
00353         process_SOFn(marker);
00354       else
00355         skip_variable();
00356       break;
00357 
00358     case M_SOS:                 /* stop before hitting compressed data */
00359       return marker;
00360 
00361     case M_EOI:                 /* in case it's a tables-only JPEG stream */
00362       return marker;
00363 
00364     case M_COM:
00365       process_COM();
00366       break;
00367 
00368     case M_APP12:
00369       /* Some digital camera makers put useful textual information into
00370        * APP12 markers, so we print those out too when in -verbose mode.
00371        */
00372       if (verbose) {
00373         printf("APP12 contains:\n");
00374         process_COM();
00375       } else
00376         skip_variable();
00377       break;
00378 
00379     default:                    /* Anything else just gets skipped */
00380       skip_variable();          /* we assume it has a parameter count... */
00381       break;
00382     }
00383   } /* end loop */
00384 }

void skip_variable void    [static]
 

Definition at line 195 of file rdjpgcom.c.

References ERREXIT, read_1_byte(), and read_2_bytes().

Referenced by read_markers(), and scan_JPEG_header().

00197 {
00198   unsigned int length;
00199 
00200   /* Get the marker parameter length count */
00201   length = read_2_bytes();
00202   /* Length includes itself, so must be at least 2 */
00203   if (length < 2)
00204     ERREXIT("Erroneous JPEG marker length");
00205   length -= 2;
00206   /* Skip over the remaining bytes */
00207   while (length > 0) {
00208     (void) read_1_byte();
00209     length--;
00210   }
00211 }

void usage void    [static]
 

Definition at line 393 of file rdjpgcom.c.

References EXIT_FAILURE, and progname.

00395 {
00396   fprintf(stderr, "rdjpgcom displays any textual comments in a JPEG file.\n");
00397 
00398   fprintf(stderr, "Usage: %s [switches] [inputfile]\n", progname);
00399 
00400   fprintf(stderr, "Switches (names may be abbreviated):\n");
00401   fprintf(stderr, "  -verbose    Also display dimensions of JPEG image\n");
00402 
00403   exit(EXIT_FAILURE);
00404 }

Variable Documentation

FILE* infile [static]
 

Definition at line 61 of file rdjpgcom.c.

Referenced by main().

const char* progname [static]
 

Definition at line 389 of file rdjpgcom.c.

Referenced by main(), and usage().

 

Powered by Plone

This site conforms to the following standards: