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  

djpeg.c File Reference

#include "cdjpeg.h"
#include "jversion.h"
#include <ctype.h>
#include "cderror.h"

Go to the source code of this file.


Defines

#define JMESSAGE(code, string)   string ,
#define DEFAULT_FMT   FMT_PPM

Enumerations

enum  IMAGE_FORMATS {
  FMT_BMP, FMT_GIF, FMT_OS2, FMT_PPM,
  FMT_RLE, FMT_TARGA, FMT_TIFF
}

Functions

 usage (void)
 parse_switches (j_decompress_ptr cinfo, int argc, char **argv, int last_file_arg_seen, boolean for_real)
 jpeg_getc (j_decompress_ptr cinfo)
 print_text_marker (j_decompress_ptr cinfo)
int main (int argc, char **argv)

Variables

const char *const cdjpeg_message_table []
IMAGE_FORMATS requested_fmt
const char * progname
char * outfilename

Define Documentation

#define DEFAULT_FMT   FMT_PPM
 

Definition at line 202 of file djpeg.c.

#define JMESSAGE code,
string       string ,
 

Definition at line 44 of file djpeg.c.


Enumeration Type Documentation

enum IMAGE_FORMATS
 

Enumeration values:
FMT_BMP 
FMT_GIF 
FMT_OS2 
FMT_PPM 
FMT_RLE 
FMT_TARGA 
FMT_TIFF 

Definition at line 191 of file djpeg.c.

00195                                  {
00196       /* BMP output format. */
00197       requested_fmt = FMT_BMP;
00198 
00199     } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||

Function Documentation

jpeg_getc j_decompress_ptr    cinfo
 

Definition at line 486 of file djpeg.c.

References outfilename.

00487          {
00488     if (file_index != argc-1) {
00489       fprintf(stderr, "%s: must name one input and one output file\n",
00490               progname);
00491       usage();
00492     }
00493   }
00494 #else
00495   /* Unix style: expect zero or one file name */
00496   if (file_index < argc-1) {
00497     fprintf(stderr, "%s: only one input file\n", progname);

int main int    argc,
char **    argv
 

Definition at line 556 of file djpeg.c.

References FMT_RLE, and FMT_TARGA.

00560               :
00561     dest_mgr = jinit_write_rle(&cinfo);
00562     break;
00563 #endif
00564 #ifdef TARGA_SUPPORTED
00565   case FMT_TARGA:
00566     dest_mgr = jinit_write_targa(&cinfo);
00567     break;
00568 #endif
00569   default:
00570     ERREXIT(&cinfo, JERR_UNSUPPORTED_FORMAT);
00571     break;
00572   }
00573   dest_mgr->output_file = output_file;
00574 
00575   /* Start decompressor */
00576   (void) jpeg_start_decompress(&cinfo);
00577 
00578   /* Write output file header */
00579   (*dest_mgr->start_output) (&cinfo, dest_mgr);
00580 
00581   /* Process data */
00582   while (cinfo.output_scanline < cinfo.output_height) {
00583     num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
00584                                         dest_mgr->buffer_height);
00585     (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines);
00586   }
00587 
00588 #ifdef PROGRESS_REPORT
00589   /* Hack: count final pass as done in case finish_output does an extra pass.
00590    * The library won't have updated completed_passes.
00591    */
00592   progress.pub.completed_passes = progress.pub.total_passes;
00593 #endif
00594 
00595   /* Finish decompression and release memory.
00596    * I must do it in this order because output module has allocated memory
00597    * of lifespan JPOOL_IMAGE; it needs to finish before releasing memory.
00598    */
00599   (*dest_mgr->finish_output) (&cinfo, dest_mgr);
00600   (void) jpeg_finish_decompress(&cinfo);
00601   jpeg_destroy_decompress(&cinfo);
00602 
00603   /* Close files, if we opened them */
00604   if (input_file != stdin)
00605     fclose(input_file);
00606   if (output_file != stdout)
00607     fclose(output_file);
00608 
00609 #ifdef PROGRESS_REPORT
00610   end_progress_monitor((j_common_ptr) &cinfo);
00611 #endif
00612 
00613   /* All done. */
00614   exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
00615   return 0;                     /* suppress no-return-value warnings */
}

parse_switches j_decompress_ptr    cinfo,
int    argc,
char **    argv,
int    last_file_arg_seen,
boolean    for_real
 

Definition at line 294 of file djpeg.c.

References L.

00299                                              {
00300       /* Suppress fancy upsampling */
00301       cinfo->do_fancy_upsampling = FALSE;
00302 
00303     } else if (keymatch(arg, "onepass", 3)) {
00304       /* Use fast one-pass quantization. */
00305       cinfo->two_pass_quantize = FALSE;
00306 
00307     } else if (keymatch(arg, "os2", 3)) {
00308       /* BMP output format (OS/2 flavor). */
00309       requested_fmt = FMT_OS2;
00310 
00311     } else if (keymatch(arg, "outfile", 4)) {
00312       /* Set output file name. */
00313       if (++argn >= argc)       /* advance to next argument */
00314         usage();
00315       outfilename = argv[argn]; /* save it away for later use */
00316 
00317     } else if (keymatch(arg, "pnm", 1) || keymatch(arg, "ppm", 1)) {
00318       /* PPM/PGM output format. */
00319       requested_fmt = FMT_PPM;
00320 
00321     } else if (keymatch(arg, "rle", 1)) {
00322       /* RLE output format. */
00323       requested_fmt = FMT_RLE;
00324 
00325     } else if (keymatch(arg, "scale", 1)) {
00326       /* Scale the output image by a fraction M/N. */
00327       if (++argn >= argc)       /* advance to next argument */
00328         usage();
00329       if (sscanf(argv[argn], "%d/%d",
00330                  &cinfo->scale_num, &cinfo->scale_denom) != 2)
00331         usage();
00332 
00333     } else if (keymatch(arg, "targa", 1)) {
00334       /* Targa output format. */
00335       requested_fmt = FMT_TARGA;
00336 
00337     } else {
00338       usage();                  /* bogus switch */
00339     }
00340   }
00341 
00342   return argn;                  /* return index of next arg (file name) */
00343 }
00344 
00345 
00346 /*
00347  * Marker processor for COM and interesting APPn markers.
00348  * This replaces the library's built-in processor, which just skips the marker.
00349  * We want to print out the marker as text, to the extent possible.
00350  * Note this code relies on a non-suspending data source.
00351  */
00352 
00353 LOCAL(unsigned int)
00354 jpeg_getc (j_decompress_ptr cinfo)
00355 /* Read next byte */
00356 {
00357   struct jpeg_source_mgr * datasrc = cinfo->src;
00358 
00359   if (datasrc->bytes_in_buffer == 0) {
00360     if (! (*datasrc->fill_input_buffer) (cinfo))
00361       ERREXIT(cinfo, JERR_CANT_SUSPEND);
00362   }
00363   datasrc->bytes_in_buffer--;
00364   return GETJOCTET(*datasrc->next_input_byte++);
00365 }
00366 
00367 
00368 METHODDEF(boolean)
00369 print_text_marker (j_decompress_ptr cinfo)
00370 {
00371   boolean traceit = (cinfo->err->trace_level >= 1);
00372   INT32 length;
00373   unsigned int ch;
00374   unsigned int lastch = 0;
00375 
00376   length = jpeg_getc(cinfo) << 8;
00377   length += jpeg_getc(cinfo);
00378   length -= 2;                  /* discount the length word itself */
00379 
00380   if (traceit) {
00381     if (cinfo->unread_marker == JPEG_COM)
00382       fprintf(stderr, "Comment, length %ld:\n", (long) length);
00383     else                        /* assume it is an APPn otherwise */
00384       fprintf(stderr, "APP%d, length %ld:\n",
00385               cinfo->unread_marker - JPEG_APP0, (long) length);
00386   }
00387 
00388   while (--length >= 0) {
00389     ch = jpeg_getc(cinfo);
00390     if (traceit) {
00391       /* Emit the character in a readable form.
00392        * Nonprintables are converted to \nnn form,
00393        * while \ is converted to \\.
00394        * Newlines in CR, CR/LF, or LF form will be printed as one newline.
00395        */
00396       if (ch == '\r') {
00397         fprintf(stderr, "\n");
00398       } else if (ch == '\n') {
00399         if (lastch != '\r')
00400           fprintf(stderr, "\n");
00401       } else if (ch == '\\') {
00402         fprintf(stderr, "\\\\");
00403       } else if (isprint(ch)) {
00404         putc(ch, stderr);
00405       } else {
00406         fprintf(stderr, "\\%03o", ch);
00407       }
00408       lastch = ch;
00409     }
00410   }
00411 
00412   if (traceit)
00413     fprintf(stderr, "\n");
00414 
00415   return TRUE;
00416 }
00417 
00418 
00419 /*
00420  * The main program.
00421  */
00422 
00423 int
00424 main (int argc, char **argv)
00425 {
00426   struct jpeg_decompress_struct cinfo;
00427   struct jpeg_error_mgr jerr;
00428 #ifdef PROGRESS_REPORT
00429   struct cdjpeg_progress_mgr progress;
00430 #endif
00431   int file_index;
00432   djpeg_dest_ptr dest_mgr = NULL;
00433   FILE * input_file;
00434   FILE * output_file;
00435   JDIMENSION num_scanlines;
00436 
00437   /* On Mac, fetch a command line. */
00438 #ifdef USE_CCOMMAND
00439   argc = ccommand(&argv);
00440 #endif
00441 
00442   progname = argv[0];
00443   if (progname == NULL || progname[0] == 0)
00444     progname = "djpeg";         /* in case C library doesn't provide it */
00445 
00446   /* Initialize the JPEG decompression object with default error handling. */
00447   cinfo.err = jpeg_std_error(&jerr);
00448   jpeg_create_decompress(&cinfo);
00449   /* Add some application-specific error messages (from cderror.h) */
00450   jerr.addon_message_table = cdjpeg_message_table;
00451   jerr.first_addon_message = JMSG_FIRSTADDONCODE;
00452   jerr.last_addon_message = JMSG_LASTADDONCODE;
00453 
00454   /* Insert custom marker processor for COM and APP12.
00455    * APP12 is used by some digital camera makers for textual info,
00456    * so we provide the ability to display it as text.
00457    * If you like, additional APPn marker types can be selected for display,
00458    * but don't try to override APP0 or APP14 this way (see libjpeg.doc).
00459    */
00460   jpeg_set_marker_processor(&cinfo, JPEG_COM, print_text_marker);
00461   jpeg_set_marker_processor(&cinfo, JPEG_APP0+12, print_text_marker);
00462 
00463   /* Now safe to enable signal catcher. */
00464 #ifdef NEED_SIGNAL_CATCHER
00465   enable_signal_catcher((j_common_ptr) &cinfo);
00466 #endif
00467 
00468   /* Scan command line to find file names. */
00469   /* It is convenient to use just one switch-parsing routine, but the switch
00470    * values read here are ignored; we will rescan the switches after opening
00471    * the input file.
00472    * (Exception: tracing level set here controls verbosity for COM markers
00473    * found during jpeg_read_header...)
00474    */
00475 

print_text_marker j_decompress_ptr    cinfo
 

Definition at line 501 of file djpeg.c.

References argc, and progname.

00503                          {
00504     if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
00505       fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
00506       exit(EXIT_FAILURE);
00507     }
00508   } else {
00509     /* default input file is stdin */
00510     input_file = read_stdin();
00511   }
00512 
00513   /* Open the output file. */
00514   if (outfilename != NULL) {
00515     if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
00516       fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
00517       exit(EXIT_FAILURE);
00518     }
00519   } else {
00520     /* default output file is stdout */
00521     output_file = write_stdout();
00522   }
00523 
00524 #ifdef PROGRESS_REPORT
00525   start_progress_monitor((j_common_ptr) &cinfo, &progress);
00526 #endif
00527 
00528   /* Specify data source for decompression */
00529   jpeg_stdio_src(&cinfo, input_file);
00530 
00531   /* Read file header, set default decompression parameters */
00532   (void) jpeg_read_header(&cinfo, TRUE);
00533 
00534   /* Adjust default decompression parameters by re-parsing the options */
00535   file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
00536 
00537   /* Initialize the output module now to let it override any crucial
00538    * option settings (for instance, GIF wants to force color quantization).
00539    */
00540   switch (requested_fmt) {
00541 #ifdef BMP_SUPPORTED
00542   case FMT_BMP:
00543     dest_mgr = jinit_write_bmp(&cinfo, FALSE);
00544     break;
00545   case FMT_OS2:
00546     dest_mgr = jinit_write_bmp(&cinfo, TRUE);
00547     break;
00548 #endif

usage void   
 

Definition at line 222 of file djpeg.c.

00224                                            {
00225       /* Select dithering algorithm. */
00226       if (++argn >= argc)       /* advance to next argument */
00227         usage();
00228       if (keymatch(argv[argn], "fs", 2)) {
00229         cinfo->dither_mode = JDITHER_FS;
00230       } else if (keymatch(argv[argn], "none", 2)) {
00231         cinfo->dither_mode = JDITHER_NONE;
00232       } else if (keymatch(argv[argn], "ordered", 2)) {
00233         cinfo->dither_mode = JDITHER_ORDERED;
00234       } else
00235         usage();
00236 
00237     } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
00238       /* Enable debug printouts. */
00239       /* On first -d, print version identification */
00240       static boolean printed_version = FALSE;
00241 
00242       if (! printed_version) {
00243         fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
00244                 JVERSION, JCOPYRIGHT);
00245         printed_version = TRUE;
00246       }
00247       cinfo->err->trace_level++;
00248 
00249     } else if (keymatch(arg, "fast", 1)) {
00250       /* Select recommended processing options for quick-and-dirty output. */
00251       cinfo->two_pass_quantize = FALSE;
00252       cinfo->dither_mode = JDITHER_ORDERED;
00253       if (! cinfo->quantize_colors) /* don't override an earlier -colors */
00254         cinfo->desired_number_of_colors = 216;
00255       cinfo->dct_method = JDCT_FASTEST;
00256       cinfo->do_fancy_upsampling = FALSE;
00257 
00258     } else if (keymatch(arg, "gif", 1)) {
00259       /* GIF output format. */
00260       requested_fmt = FMT_GIF;
00261 
00262     } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
00263       /* Force monochrome output. */
00264       cinfo->out_color_space = JCS_GRAYSCALE;
00265 
00266     } else if (keymatch(arg, "map", 3)) {
00267       /* Quantize to a color map taken from an input file. */
00268       if (++argn >= argc)       /* advance to next argument */
00269         usage();
00270       if (for_real) {           /* too expensive to do twice! */
00271 #ifdef QUANT_2PASS_SUPPORTED    /* otherwise can't quantize to supplied map */
00272         FILE * mapfile;
00273 
00274         if ((mapfile = fopen(argv[argn], READ_BINARY)) == NULL) {
00275           fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
00276           exit(EXIT_FAILURE);
00277         }
00278         read_color_map(cinfo, mapfile);
00279         fclose(mapfile);
00280         cinfo->quantize_colors = TRUE;
00281 #else
00282         ERREXIT(cinfo, JERR_NOT_COMPILED);
00283 #endif
00284       }
00285 
00286     } else if (keymatch(arg, "maxmemory", 3)) {
00287       /* Maximum memory in Kb (or Mb with 'm'). */
00288       long lval;
00289       char ch = 'x';
00290 

Variable Documentation

const char* const cdjpeg_message_table[] [static]
 

Definition at line 46 of file djpeg.c.

char* outfilename [static]
 

Definition at line 218 of file djpeg.c.

Referenced by jpeg_getc().

const char* progname [static]
 

Definition at line 217 of file djpeg.c.

Referenced by print_text_marker().

IMAGE_FORMATS requested_fmt [static]
 

Definition at line 205 of file djpeg.c.

 

Powered by Plone

This site conforms to the following standards: