Main Page | File List | Related Pages

gdcmJpeg12.cxx

00001 // gdcmJpeg12.cxx
00002 //-----------------------------------------------------------------------------
00003 #include <stdio.h>
00004 #include "gdcmFile.h"
00005 
00006 #define BITS_IN_JSAMPLE 12
00007 
00008 // BITS_IN_JSAMPLE is a compile time defined options.
00009 // We need both 8 an 12;
00010 // To avoid renaming *all* the Jpeg functions,
00011 // we hard code the 'brain damaged liker' option.
00012 // For all the functions, we shall have the 8 and 12 version
00013 // (8 with the 'long' name, 12 with the 'short' name)
00014 
00015 #define jpeg_read_header        jReadHeader
00016 #define my_error_exit           myErrorExit
00017 #define jpeg_destroy_decompress jDestDecompress
00018 #define jpeg_stdio_src          jStdSrc
00019 #define jpeg_read_header        jReadHeader
00020 #define jpeg_read_scanlines     jReadScanlines
00021 #define jpeg_finish_decompress  jFinDecompress
00022 #define jpeg_create_decompress  jCreaDecompress
00023 
00024 // -----------------
00025 #define jpeg_std_error          jStdError
00026 #define jpeg_CreateCompress     jCreaCompress
00027 #define jpeg_CreateDecompress   jCreaDecompress
00028 #define jpeg_destroy_compress   jDestCompress
00029 #define jpeg_destroy_decompress jDestDecompress
00030 #define jpeg_stdio_dest         jStdDest
00031 #define jpeg_stdio_src          jStdSrc
00032 #define jpeg_set_defaults       jSetDefaults
00033 #define jpeg_set_colorspace     jSetColorspace
00034 #define jpeg_default_colorspace jDefColorspace
00035 #define jpeg_set_quality        jSetQuality
00036 #define jpeg_set_linear_quality jSetLQuality
00037 #define jpeg_add_quant_table    jAddQuantTable
00038 #define jpeg_quality_scaling    jQualityScaling
00039 #define jpeg_simple_progression jSimProgress
00040 #define jpeg_suppress_tables    jSuppressTables
00041 #define jpeg_alloc_quant_table  jAlcQTable
00042 #define jpeg_alloc_huff_table   jAlcHTable
00043 #define jpeg_start_compress     jStrtCompress
00044 #define jpeg_write_scanlines    jWrtScanlines
00045 #define jpeg_finish_compress    jFinCompress
00046 #define jpeg_write_raw_data     jWrtRawData
00047 #define jpeg_write_marker       jWrtMarker
00048 #define jpeg_write_m_header     jWrtMHeader
00049 #define jpeg_write_m_byte       jWrtMByte
00050 #define jpeg_write_tables       jWrtTables
00051 #define jpeg_read_header        jReadHeader
00052 #define jpeg_start_decompress   jStrtDecompress
00053 #define jpeg_read_scanlines     jReadScanlines
00054 #define jpeg_finish_decompress  jFinDecompress
00055 #define jpeg_read_raw_data      jReadRawData
00056 #define jpeg_has_multiple_scans jHasMultScn
00057 #define jpeg_start_output       jStrtOutput
00058 #define jpeg_finish_output      jFinOutput
00059 #define jpeg_input_complete     jInComplete
00060 #define jpeg_new_colormap       jNewCMap
00061 #define jpeg_consume_input      jConsumeInput
00062 #define jpeg_calc_output_dimensions     jCalcDimensions
00063 #define jpeg_save_markers       jSaveMarkers
00064 #define jpeg_set_marker_processor       jSetMarker
00065 #define jpeg_read_coefficients  jReadCoefs
00066 #define jpeg_write_coefficients jWrtCoefs
00067 #define jpeg_copy_critical_parameters   jCopyCrit
00068 #define jpeg_abort_compress     jAbrtCompress
00069 #define jpeg_abort_decompress   jAbrtDecompress
00070 #define jpeg_abort              jAbort
00071 #define jpeg_destroy            jDestroy
00072 #define jpeg_resync_to_restart  jResyncRestart
00073 
00074 #define DEBUG 0
00075 
00076 /*
00077  * <setjmp.h> is used for the optional error recovery mechanism shown in
00078  * the second part of the example.
00079  */
00080 
00081 /*
00082  * Include file for users of JPEG library.
00083  * You will need to have included system headers that define at least
00084  * the typedefs FILE and size_t before you can include jpeglib.h.
00085  * (stdio.h is sufficient on ANSI-conforming systems.)
00086  * You may also wish to include "jerror.h".
00087  */
00088 
00089 extern "C" {
00090 #include "jpeg/libijg12/jpeglib12.h"
00091 #include <setjmp.h>
00092 }
00093 
00094 /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
00095 
00096 /* This half of the example shows how to read data from the JPEG decompressor.
00097  * It's a bit more refined than the above, in that we show:
00098  *   (a) how to modify the JPEG library's standard error-reporting behavior;
00099  *   (b) how to allocate workspace using the library's memory manager.
00100  *
00101  * Just to make this example a little different from the first one, we'll
00102  * assume that we do not intend to put the whole image into an in-memory
00103  * buffer, but to send it line-by-line someplace else.  We need a one-
00104  * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
00105  * memory manager allocate it for us.  This approach is actually quite useful
00106  * because we don't need to remember to deallocate the buffer separately: it
00107  * will go away automatically when the JPEG object is cleaned up.
00108  */
00109 
00110 /*
00111  * ERROR HANDLING:
00112  *
00113  * The JPEG library's standard error handler (jerror.c) is divided into
00114  * several "methods" which you can override individually.  This lets you
00115  * adjust the behavior without duplicating a lot of code, which you might
00116  * have to update with each future release.
00117  *
00118  * Our example here shows how to override the "error_exit" method so that
00119  * control is returned to the library's caller when a fatal error occurs,
00120  * rather than calling exit() as the standard error_exit method does.
00121  *
00122  * We use C's setjmp/longjmp facility to return control.  This means that the
00123  * routine which calls the JPEG library must first execute a setjmp() call to
00124  * establish the return point.  We want the replacement error_exit to do a
00125  * longjmp().  But we need to make the setjmp buffer accessible to the
00126  * error_exit routine.  To do this, we make a private extension of the
00127  * standard JPEG error handler object.  (If we were using C++, we'd say we
00128  * were making a subclass of the regular error handler.)
00129  *
00130  * Here's the extended error handler struct:
00131  */
00132 
00133 //-----------------------------------------------------------------------------
00134 struct my_error_mgr {
00135   struct jpeg_error_mgr pub;    /* "public" fields */
00136   jmp_buf setjmp_buffer;        /* for return to caller */
00137 };
00138 
00139 typedef struct my_error_mgr * my_error_ptr;
00140 
00141 //-----------------------------------------------------------------------------
00142 /*
00143  * Here's the routine that will replace the standard error_exit method:
00144  */
00145 METHODDEF(void)
00146 my_error_exit (j_common_ptr cinfo) {
00147   /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
00148   my_error_ptr myerr = (my_error_ptr) cinfo->err;
00149 
00150   /* Always display the message. */
00151   /* We could postpone this until after returning, if we chose. */
00152   (*cinfo->err->output_message) (cinfo);
00153 
00154   /* Return control to the setjmp point */
00155   longjmp(myerr->setjmp_buffer, 1);
00156 }
00157 
00158 
00159 //-----------------------------------------------------------------------------
00160 /*
00161  * Sample routine for JPEG decompression.  We assume that the source file name
00162  * is passed in.  We want to return 1 on success, 0 on error.
00163  */
00164 bool gdcmFile::gdcm_read_JPEG_file12 (FILE *fp,void * image_buffer) {
00165    char *pimage;
00166 
00167    /* This struct contains the JPEG decompression parameters and pointers to
00168     * working space (which is allocated as needed by the JPEG library).
00169     */
00170 
00171    struct jpeg_decompress_struct cinfo;
00172   
00173    /* -------------- inside, we found :
00174     * JDIMENSION image_width;   // input image width 
00175     * JDIMENSION image_height;  // input image height 
00176     * int input_components;             // nb of color components in input image 
00177     * J_COLOR_SPACE in_color_space;     // colorspace of input image 
00178     * double input_gamma;               // image gamma of input image 
00179     * -------------- */
00180   
00181    /* We use our private extension JPEG error handler.
00182     * Note that this struct must live as long as the main JPEG parameter
00183     * struct, to avoid dangling-pointer problems.
00184     */
00185    struct my_error_mgr jerr;
00186    /* More stuff */
00187 
00188    JSAMPARRAY buffer;           /* Output row buffer */
00189 
00190    // rappel :
00191    // ------
00192    // typedef unsigned char JSAMPLE;
00193    // typedef JSAMPLE FAR *JSAMPROW;    /* ptr to one image row of pixel samples. */
00194    // typedef JSAMPROW *JSAMPARRAY;     /* ptr to some rows (a 2-D sample array) */
00195    // typedef JSAMPARRAY *JSAMPIMAGE;   /* a 3-D sample array: top index is color */
00196 
00197 
00198    int row_stride;              /* physical row width in output buffer */
00199   
00200    if (DEBUG) printf("entree dans gdcmFile::gdcm_read_JPEG_file12, depuis gdcmJpeg\n");
00201 
00202 
00203    /* In this example we want to open the input file before doing anything else,
00204     * so that the setjmp() error recovery below can assume the file is open.
00205     * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
00206     * requires it in order to read binary files.
00207     */
00208 
00209    /* Step 1: allocate and initialize JPEG decompression object */
00210    if (DEBUG)printf("Entree Step 1\n");
00211 
00212    /* We set up the normal JPEG error routines, then override error_exit. */
00213 
00214    cinfo.err = jpeg_std_error(&jerr.pub);
00215    jerr.pub.error_exit = my_error_exit;
00216 
00217    /* Establish the setjmp return context for my_error_exit to use. */
00218    if (setjmp(jerr.setjmp_buffer)) {
00219       /* If we get here, the JPEG code has signaled an error.
00220        * We need to clean up the JPEG object, close the input file, and return.
00221        */
00222       jpeg_destroy_decompress(&cinfo);
00223       return(false);
00224    }
00225 
00226    /* Now we can initialize the JPEG decompression object. */
00227    jpeg_create_decompress(&cinfo);
00228 
00229    /* Step 2: specify data source (eg, a file) */
00230    if (DEBUG) printf("Entree Step 2\n");
00231    jpeg_stdio_src(&cinfo, fp);
00232 
00233    /* Step 3: read file parameters with jpeg_read_header() */
00234    if (DEBUG) printf("Entree Step 3\n");
00235    (void) jpeg_read_header(&cinfo, TRUE);
00236 
00237    /* We can ignore the return value from jpeg_read_header since
00238     *   (a) suspension is not possible with the stdio data source, and
00239     *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
00240     * See libjpeg.doc for more info.
00241     */
00242 
00243    if (DEBUG) {   
00244       printf("--------------Header contents :----------------\n");
00245       printf("image_width %d image_height %d\n", 
00246                               cinfo.image_width , cinfo.image_height);
00247       printf("bits of precision in image data  %d \n", 
00248                               cinfo.output_components);
00249       printf("nb of color components returned  %d \n", 
00250                               cinfo.data_precision);
00251    }
00252 
00253 
00254    /*
00255     * JDIMENSION image_width;   // input image width 
00256     * JDIMENSION image_height;  // input image height 
00257     * int output_components;    // # of color components returned 
00258     * J_COLOR_SPACE in_color_space;     // colorspace of input image 
00259     * double input_gamma;               // image gamma of input image
00260     * int data_precision;               // bits of precision in image data 
00261     */
00262 
00263    /* Step 4: set parameters for decompression */
00264    if (DEBUG) printf("Entree Step 4\n");
00265 
00266    /* In this example, we don't need to change any of the defaults set by
00267     * jpeg_read_header(), so we do nothing here.
00268     */
00269 
00270    /* Step 5: Start decompressor */
00271    if (DEBUG) printf("Entree Step 5\n");
00272 
00273    (void) jpeg_start_decompress(&cinfo);
00274    /* We can ignore the return value since suspension is not possible
00275     * with the stdio data source.
00276     */
00277 
00278    /* We may need to do some setup of our own at this point before reading
00279     * the data.  After jpeg_start_decompress() we have the correct scaled
00280     * output image dimensions available, as well as the output colormap
00281     * if we asked for color quantization.
00282     * In this example, we need to make an output work buffer of the right size.
00283     */ 
00284 
00285    /* JSAMPLEs per row in output buffer */
00286    row_stride = cinfo.output_width * cinfo.output_components;
00287   
00288    if (DEBUG) 
00289       printf ("cinfo.output_width %d cinfo.output_components %d  row_stride %d\n",
00290               cinfo.output_width, cinfo.output_components,row_stride);
00291         
00292    /* Make a one-row-high sample array that will go away when done with image */
00293    buffer = (*cinfo.mem->alloc_sarray)
00294            ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
00295 
00296    /* Step 6: while (scan lines remain to be read) */
00297    if (DEBUG)  printf("Entree Step 6\n"); 
00298 
00299    /*           jpeg_read_scanlines(...); */
00300 
00301    /* Here we use the library's state variable cinfo.output_scanline as the
00302     * loop counter, so that we don't have to keep track ourselves.
00303     */
00304 
00305    if (DEBUG)  printf ("cinfo.output_height %d  cinfo.output_width %d\n",
00306                    cinfo.output_height,cinfo.output_width);
00307 
00308    pimage=(char *)image_buffer;
00309 
00310    while (cinfo.output_scanline < cinfo.output_height) {
00311       /* jpeg_read_scanlines expects an array of pointers to scanlines.
00312        * Here the array is only one element long, but you could ask for
00313        * more than one scanline at a time if that's more convenient.
00314        */
00315 
00316       (void) jpeg_read_scanlines(&cinfo, buffer, 1);
00317 
00318       if ( BITS_IN_JSAMPLE == 8) {
00319          memcpy( pimage, buffer[0],row_stride); 
00320          pimage+=row_stride;
00321       } else {
00322          memcpy( pimage, buffer[0],row_stride*2 ); // FIXME : *2  car 16 bits?!?
00323          pimage+=row_stride*2;                     // FIXME : *2 car 16 bits?!?     
00324       }
00325    }
00326  
00327   /* Step 7: Finish decompression */
00328    if (DEBUG)  printf("Entree Step 7\n");
00329   (void) jpeg_finish_decompress(&cinfo);
00330    /* We can ignore the return value since suspension is not possible
00331     * with the stdio data source.
00332     */
00333 
00334    /* Step 8: Release JPEG decompression object */
00335    if (DEBUG) printf("Entree Step 8\n");
00336 
00337    /* This is an important step since it will release a good deal of memory. */
00338    jpeg_destroy_decompress(&cinfo);
00339 
00340    /* After finish_decompress, we can close the input file.
00341     * Here we postpone it until after no more JPEG errors are possible,
00342     * so as to simplify the setjmp error logic above.  (Actually, I don't
00343     * think that jpeg_destroy can do an error exit, but why assume anything...)
00344     */
00345 
00346    /* At this point you may want to check to see whether any corrupt-data
00347     * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
00348     */
00349 
00350    /* And we're done! */
00351 
00352    return(true);
00353 }
00354 
00355 /*
00356  * SOME FINE POINTS:
00357  *
00358  * In the above code, we ignored the return value of jpeg_read_scanlines,
00359  * which is the number of scanlines actually read.  We could get away with
00360  * this because we asked for only one line at a time and we weren't using
00361  * a suspending data source.  See libjpeg.doc for more info.
00362  *
00363  * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
00364  * we should have done it beforehand to ensure that the space would be
00365  * counted against the JPEG max_memory setting.  In some systems the above
00366  * code would risk an out-of-memory error.  However, in general we don't
00367  * know the output image dimensions before jpeg_start_decompress(), unless we
00368  * call jpeg_calc_output_dimensions().  See libjpeg.doc for more about this.
00369  *
00370  * Scanlines are returned in the same order as they appear in the JPEG file,
00371  * which is standardly top-to-bottom.  If you must emit data bottom-to-top,
00372  * you can use one of the virtual arrays provided by the JPEG memory manager
00373  * to invert the data.  See wrbmp.c for an example.
00374  *
00375  * As with compression, some operating modes may require temporary files.
00376  * On some systems you may need to set up a signal handler to ensure that
00377  * temporary files are deleted if the program is interrupted.  See libjpeg.doc.
00378  */
00379 
00380 //-----------------------------------------------------------------------------

Generated on Mon Feb 14 16:13:44 2005 for gdcm by doxygen 1.3.6