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  

example.c File Reference

#include <stdio.h>
#include "jpeglib.h"
#include <setjmp.h>

Go to the source code of this file.


Data Structures

struct  my_error_mgr

Typedefs

typedef my_error_mgrmy_error_ptr

Functions

 write_JPEG_file (char *filename, int quality)
 my_error_exit (j_common_ptr cinfo)
 read_JPEG_file (char *filename)

Variables

JSAMPLEimage_buffer
int image_height
int image_width

Typedef Documentation

typedef struct my_error_mgr* my_error_ptr
 

Definition at line 256 of file jpeg-6b/example.c.


Function Documentation

my_error_exit j_common_ptr    cinfo
 

Definition at line 263 of file jpeg-6b/example.c.

References my_error_mgr::setjmp_buffer.

Referenced by read_JPEG_file().

00264 {
00265   /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
00266   my_error_ptr myerr = (my_error_ptr) cinfo->err;
00267 
00268   /* Always display the message. */
00269   /* We could postpone this until after returning, if we chose. */
00270   (*cinfo->err->output_message) (cinfo);
00271 
00272   /* Return control to the setjmp point */
00273   longjmp(myerr->setjmp_buffer, 1);
00274 }

read_JPEG_file char *    filename
 

Definition at line 284 of file jpeg-6b/example.c.

References jpeg_create_decompress, jpeg_destroy_decompress(), jpeg_finish_decompress(), jpeg_read_header(), jpeg_read_scanlines(), jpeg_start_decompress(), jpeg_std_error(), jpeg_stdio_src(), JPOOL_IMAGE, JSAMPARRAY, my_error_exit(), jpeg_decompress_struct::output_components, jpeg_decompress_struct::output_width, my_error_mgr::pub, and my_error_mgr::setjmp_buffer.

00285 {
00286   /* This struct contains the JPEG decompression parameters and pointers to
00287    * working space (which is allocated as needed by the JPEG library).
00288    */
00289   struct jpeg_decompress_struct cinfo;
00290   /* We use our private extension JPEG error handler.
00291    * Note that this struct must live as long as the main JPEG parameter
00292    * struct, to avoid dangling-pointer problems.
00293    */
00294   struct my_error_mgr jerr;
00295   /* More stuff */
00296   FILE * infile;                /* source file */
00297   JSAMPARRAY buffer;            /* Output row buffer */
00298   int row_stride;               /* physical row width in output buffer */
00299 
00300   /* In this example we want to open the input file before doing anything else,
00301    * so that the setjmp() error recovery below can assume the file is open.
00302    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
00303    * requires it in order to read binary files.
00304    */
00305 
00306   if ((infile = fopen(filename, "rb")) == NULL) {
00307     fprintf(stderr, "can't open %s\n", filename);
00308     return 0;
00309   }
00310 
00311   /* Step 1: allocate and initialize JPEG decompression object */
00312 
00313   /* We set up the normal JPEG error routines, then override error_exit. */
00314   cinfo.err = jpeg_std_error(&jerr.pub);
00315   jerr.pub.error_exit = my_error_exit;
00316   /* Establish the setjmp return context for my_error_exit to use. */
00317   if (setjmp(jerr.setjmp_buffer)) {
00318     /* If we get here, the JPEG code has signaled an error.
00319      * We need to clean up the JPEG object, close the input file, and return.
00320      */
00321     jpeg_destroy_decompress(&cinfo);
00322     fclose(infile);
00323     return 0;
00324   }
00325   /* Now we can initialize the JPEG decompression object. */
00326   jpeg_create_decompress(&cinfo);
00327 
00328   /* Step 2: specify data source (eg, a file) */
00329 
00330   jpeg_stdio_src(&cinfo, infile);
00331 
00332   /* Step 3: read file parameters with jpeg_read_header() */
00333 
00334   (void) jpeg_read_header(&cinfo, TRUE);
00335   /* We can ignore the return value from jpeg_read_header since
00336    *   (a) suspension is not possible with the stdio data source, and
00337    *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
00338    * See libjpeg.doc for more info.
00339    */
00340 
00341   /* Step 4: set parameters for decompression */
00342 
00343   /* In this example, we don't need to change any of the defaults set by
00344    * jpeg_read_header(), so we do nothing here.
00345    */
00346 
00347   /* Step 5: Start decompressor */
00348 
00349   (void) jpeg_start_decompress(&cinfo);
00350   /* We can ignore the return value since suspension is not possible
00351    * with the stdio data source.
00352    */
00353 
00354   /* We may need to do some setup of our own at this point before reading
00355    * the data.  After jpeg_start_decompress() we have the correct scaled
00356    * output image dimensions available, as well as the output colormap
00357    * if we asked for color quantization.
00358    * In this example, we need to make an output work buffer of the right size.
00359    */ 
00360   /* JSAMPLEs per row in output buffer */
00361   row_stride = cinfo.output_width * cinfo.output_components;
00362   /* Make a one-row-high sample array that will go away when done with image */
00363   buffer = (*cinfo.mem->alloc_sarray)
00364                 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
00365 
00366   /* Step 6: while (scan lines remain to be read) */
00367   /*           jpeg_read_scanlines(...); */
00368 
00369   /* Here we use the library's state variable cinfo.output_scanline as the
00370    * loop counter, so that we don't have to keep track ourselves.
00371    */
00372   while (cinfo.output_scanline < cinfo.output_height) {
00373     /* jpeg_read_scanlines expects an array of pointers to scanlines.
00374      * Here the array is only one element long, but you could ask for
00375      * more than one scanline at a time if that's more convenient.
00376      */
00377     (void) jpeg_read_scanlines(&cinfo, buffer, 1);
00378     /* Assume put_scanline_someplace wants a pointer and sample count. */
00379     put_scanline_someplace(buffer[0], row_stride);
00380   }
00381 
00382   /* Step 7: Finish decompression */
00383 
00384   (void) jpeg_finish_decompress(&cinfo);
00385   /* We can ignore the return value since suspension is not possible
00386    * with the stdio data source.
00387    */
00388 
00389   /* Step 8: Release JPEG decompression object */
00390 
00391   /* This is an important step since it will release a good deal of memory. */
00392   jpeg_destroy_decompress(&cinfo);
00393 
00394   /* After finish_decompress, we can close the input file.
00395    * Here we postpone it until after no more JPEG errors are possible,
00396    * so as to simplify the setjmp error logic above.  (Actually, I don't
00397    * think that jpeg_destroy can do an error exit, but why assume anything...)
00398    */
00399   fclose(infile);
00400 
00401   /* At this point you may want to check to see whether any corrupt-data
00402    * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
00403    */
00404 
00405   /* And we're done! */
00406   return 1;
00407 }

write_JPEG_file char *    filename,
int    quality
 

Definition at line 72 of file jpeg-6b/example.c.

References image_buffer, image_height, jpeg_compress_struct::image_height, image_width, jpeg_compress_struct::image_width, jpeg_compress_struct::in_color_space, jpeg_compress_struct::input_components, JCS_RGB, jpeg_create_compress, jpeg_destroy_compress(), jpeg_finish_compress(), jpeg_set_defaults(), jpeg_set_quality(), jpeg_start_compress(), jpeg_std_error(), jpeg_stdio_dest(), jpeg_write_scanlines(), JSAMPROW, jpeg_compress_struct::next_scanline, and quality.

00073 {
00074   /* This struct contains the JPEG compression parameters and pointers to
00075    * working space (which is allocated as needed by the JPEG library).
00076    * It is possible to have several such structures, representing multiple
00077    * compression/decompression processes, in existence at once.  We refer
00078    * to any one struct (and its associated working data) as a "JPEG object".
00079    */
00080   struct jpeg_compress_struct cinfo;
00081   /* This struct represents a JPEG error handler.  It is declared separately
00082    * because applications often want to supply a specialized error handler
00083    * (see the second half of this file for an example).  But here we just
00084    * take the easy way out and use the standard error handler, which will
00085    * print a message on stderr and call exit() if compression fails.
00086    * Note that this struct must live as long as the main JPEG parameter
00087    * struct, to avoid dangling-pointer problems.
00088    */
00089   struct jpeg_error_mgr jerr;
00090   /* More stuff */
00091   FILE * outfile;               /* target file */
00092   JSAMPROW row_pointer[1];      /* pointer to JSAMPLE row[s] */
00093   int row_stride;               /* physical row width in image buffer */
00094 
00095   /* Step 1: allocate and initialize JPEG compression object */
00096 
00097   /* We have to set up the error handler first, in case the initialization
00098    * step fails.  (Unlikely, but it could happen if you are out of memory.)
00099    * This routine fills in the contents of struct jerr, and returns jerr's
00100    * address which we place into the link field in cinfo.
00101    */
00102   cinfo.err = jpeg_std_error(&jerr);
00103   /* Now we can initialize the JPEG compression object. */
00104   jpeg_create_compress(&cinfo);
00105 
00106   /* Step 2: specify data destination (eg, a file) */
00107   /* Note: steps 2 and 3 can be done in either order. */
00108 
00109   /* Here we use the library-supplied code to send compressed data to a
00110    * stdio stream.  You can also write your own code to do something else.
00111    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
00112    * requires it in order to write binary files.
00113    */
00114   if ((outfile = fopen(filename, "wb")) == NULL) {
00115     fprintf(stderr, "can't open %s\n", filename);
00116     exit(1);
00117   }
00118   jpeg_stdio_dest(&cinfo, outfile);
00119 
00120   /* Step 3: set parameters for compression */
00121 
00122   /* First we supply a description of the input image.
00123    * Four fields of the cinfo struct must be filled in:
00124    */
00125   cinfo.image_width = image_width;      /* image width and height, in pixels */
00126   cinfo.image_height = image_height;
00127   cinfo.input_components = 3;           /* # of color components per pixel */
00128   cinfo.in_color_space = JCS_RGB;       /* colorspace of input image */
00129   /* Now use the library's routine to set default compression parameters.
00130    * (You must set at least cinfo.in_color_space before calling this,
00131    * since the defaults depend on the source color space.)
00132    */
00133   jpeg_set_defaults(&cinfo);
00134   /* Now you can set any non-default parameters you wish to.
00135    * Here we just illustrate the use of quality (quantization table) scaling:
00136    */
00137   jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
00138 
00139   /* Step 4: Start compressor */
00140 
00141   /* TRUE ensures that we will write a complete interchange-JPEG file.
00142    * Pass TRUE unless you are very sure of what you're doing.
00143    */
00144   jpeg_start_compress(&cinfo, TRUE);
00145 
00146   /* Step 5: while (scan lines remain to be written) */
00147   /*           jpeg_write_scanlines(...); */
00148 
00149   /* Here we use the library's state variable cinfo.next_scanline as the
00150    * loop counter, so that we don't have to keep track ourselves.
00151    * To keep things simple, we pass one scanline per call; you can pass
00152    * more if you wish, though.
00153    */
00154   row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
00155 
00156   while (cinfo.next_scanline < cinfo.image_height) {
00157     /* jpeg_write_scanlines expects an array of pointers to scanlines.
00158      * Here the array is only one element long, but you could pass
00159      * more than one scanline at a time if that's more convenient.
00160      */
00161     row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
00162     (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
00163   }
00164 
00165   /* Step 6: Finish compression */
00166 
00167   jpeg_finish_compress(&cinfo);
00168   /* After finish_compress, we can close the output file. */
00169   fclose(outfile);
00170 
00171   /* Step 7: release JPEG compression object */
00172 
00173   /* This is an important step since it will release a good deal of memory. */
00174   jpeg_destroy_compress(&cinfo);
00175 
00176   /* And we're done! */
00177 }

Variable Documentation

JSAMPLE* image_buffer
 

Definition at line 61 of file jpeg-6b/example.c.

Referenced by write_JPEG_file().

int image_height
 

Definition at line 62 of file jpeg-6b/example.c.

Referenced by process_SOFn(), and write_JPEG_file().

int image_width
 

Definition at line 63 of file jpeg-6b/example.c.

Referenced by process_SOFn(), and write_JPEG_file().

 

Powered by Plone

This site conforms to the following standards: