]> Creatis software - gdcm.git/blob - src/gdcmJpeg12.cxx
Remove using namespace std;
[gdcm.git] / src / gdcmJpeg12.cxx
1 //This is needed when compiling in debug mode
2 #ifdef _MSC_VER
3 // 'identifier' : class 'type' needs to have dll-interface to be used by
4 // clients of class 'type2'
5 #pragma warning ( disable : 4251 )
6 // 'identifier' : identifier was truncated to 'number' characters in the
7 // debug information
8 #pragma warning ( disable : 4786 )
9 #endif //_MSC_VER
10
11 #include <stdio.h>
12 #include "gdcmFile.h"
13
14 #define BITS_IN_JSAMPLE 12
15
16 // BITS_IN_JSAMPLE is a compile time defined options.
17 // We need both 8 an 12;
18 // To avoid renaming *all* the Jpeg functions,
19 // we hard code the 'brain damaged liker' option.
20 // For all the functions, we shall have the 8 and 12 version
21 // (8 with the 'long' name, 12 with the 'short' name)
22
23 #define jpeg_read_header        jReadHeader
24 #define my_error_exit           myErrorExit
25 #define jpeg_destroy_decompress jDestDecompress
26 #define jpeg_stdio_src          jStdSrc
27 #define jpeg_read_header        jReadHeader
28 #define jpeg_read_scanlines     jReadScanlines
29 #define jpeg_finish_decompress  jFinDecompress
30 #define jpeg_create_decompress  jCreaDecompress
31
32 // -----------------
33 #define jpeg_std_error          jStdError
34 #define jpeg_CreateCompress     jCreaCompress
35 #define jpeg_CreateDecompress   jCreaDecompress
36 #define jpeg_destroy_compress   jDestCompress
37 #define jpeg_destroy_decompress jDestDecompress
38 #define jpeg_stdio_dest         jStdDest
39 #define jpeg_stdio_src          jStdSrc
40 #define jpeg_set_defaults       jSetDefaults
41 #define jpeg_set_colorspace     jSetColorspace
42 #define jpeg_default_colorspace jDefColorspace
43 #define jpeg_set_quality        jSetQuality
44 #define jpeg_set_linear_quality jSetLQuality
45 #define jpeg_add_quant_table    jAddQuantTable
46 #define jpeg_quality_scaling    jQualityScaling
47 #define jpeg_simple_progression jSimProgress
48 #define jpeg_suppress_tables    jSuppressTables
49 #define jpeg_alloc_quant_table  jAlcQTable
50 #define jpeg_alloc_huff_table   jAlcHTable
51 #define jpeg_start_compress     jStrtCompress
52 #define jpeg_write_scanlines    jWrtScanlines
53 #define jpeg_finish_compress    jFinCompress
54 #define jpeg_write_raw_data     jWrtRawData
55 #define jpeg_write_marker       jWrtMarker
56 #define jpeg_write_m_header     jWrtMHeader
57 #define jpeg_write_m_byte       jWrtMByte
58 #define jpeg_write_tables       jWrtTables
59 #define jpeg_read_header        jReadHeader
60 #define jpeg_start_decompress   jStrtDecompress
61 #define jpeg_read_scanlines     jReadScanlines
62 #define jpeg_finish_decompress  jFinDecompress
63 #define jpeg_read_raw_data      jReadRawData
64 #define jpeg_has_multiple_scans jHasMultScn
65 #define jpeg_start_output       jStrtOutput
66 #define jpeg_finish_output      jFinOutput
67 #define jpeg_input_complete     jInComplete
68 #define jpeg_new_colormap       jNewCMap
69 #define jpeg_consume_input      jConsumeInput
70 #define jpeg_calc_output_dimensions     jCalcDimensions
71 #define jpeg_save_markers       jSaveMarkers
72 #define jpeg_set_marker_processor       jSetMarker
73 #define jpeg_read_coefficients  jReadCoefs
74 #define jpeg_write_coefficients jWrtCoefs
75 #define jpeg_copy_critical_parameters   jCopyCrit
76 #define jpeg_abort_compress     jAbrtCompress
77 #define jpeg_abort_decompress   jAbrtDecompress
78 #define jpeg_abort              jAbort
79 #define jpeg_destroy            jDestroy
80 #define jpeg_resync_to_restart  jResyncRestart
81
82 // -----------------
83
84 #define DEBUG 0
85
86
87 /*
88  * <setjmp.h> is used for the optional error recovery mechanism shown in
89  * the second part of the example.
90  */
91
92 /*
93  * Include file for users of JPEG library.
94  * You will need to have included system headers that define at least
95  * the typedefs FILE and size_t before you can include jpeglib.h.
96  * (stdio.h is sufficient on ANSI-conforming systems.)
97  * You may also wish to include "jerror.h".
98  */
99
100 extern "C" {
101 #include "jpeg/libijg12/jpeglib12.h"
102 #include <setjmp.h>
103 }
104
105 /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
106
107 /* This half of the example shows how to read data from the JPEG decompressor.
108  * It's a bit more refined than the above, in that we show:
109  *   (a) how to modify the JPEG library's standard error-reporting behavior;
110  *   (b) how to allocate workspace using the library's memory manager.
111  *
112  * Just to make this example a little different from the first one, we'll
113  * assume that we do not intend to put the whole image into an in-memory
114  * buffer, but to send it line-by-line someplace else.  We need a one-
115  * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
116  * memory manager allocate it for us.  This approach is actually quite useful
117  * because we don't need to remember to deallocate the buffer separately: it
118  * will go away automatically when the JPEG object is cleaned up.
119  */
120
121 /*
122  * ERROR HANDLING:
123  *
124  * The JPEG library's standard error handler (jerror.c) is divided into
125  * several "methods" which you can override individually.  This lets you
126  * adjust the behavior without duplicating a lot of code, which you might
127  * have to update with each future release.
128  *
129  * Our example here shows how to override the "error_exit" method so that
130  * control is returned to the library's caller when a fatal error occurs,
131  * rather than calling exit() as the standard error_exit method does.
132  *
133  * We use C's setjmp/longjmp facility to return control.  This means that the
134  * routine which calls the JPEG library must first execute a setjmp() call to
135  * establish the return point.  We want the replacement error_exit to do a
136  * longjmp().  But we need to make the setjmp buffer accessible to the
137  * error_exit routine.  To do this, we make a private extension of the
138  * standard JPEG error handler object.  (If we were using C++, we'd say we
139  * were making a subclass of the regular error handler.)
140  *
141  * Here's the extended error handler struct:
142  */
143
144 struct my_error_mgr {
145   struct jpeg_error_mgr pub;    /* "public" fields */
146   jmp_buf setjmp_buffer;        /* for return to caller */
147 };
148
149 typedef struct my_error_mgr * my_error_ptr;
150
151 /*
152  * Here's the routine that will replace the standard error_exit method:
153  */
154
155 METHODDEF(void)
156 my_error_exit (j_common_ptr cinfo) {
157   /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
158   my_error_ptr myerr = (my_error_ptr) cinfo->err;
159
160   /* Always display the message. */
161   /* We could postpone this until after returning, if we chose. */
162   (*cinfo->err->output_message) (cinfo);
163
164   /* Return control to the setjmp point */
165   longjmp(myerr->setjmp_buffer, 1);
166 }
167
168
169 /*
170  * Sample routine for JPEG decompression.  We assume that the source file name
171  * is passed in.  We want to return 1 on success, 0 on error.
172  */
173
174
175 //GLOBAL(int)
176 int
177 gdcmFile::gdcm_read_JPEG_file12 (void * image_buffer) {
178
179 char *pimage;
180
181   /* This struct contains the JPEG decompression parameters and pointers to
182    * working space (which is allocated as needed by the JPEG library).
183    */
184    
185   struct jpeg_decompress_struct cinfo;
186   
187   /* -------------- inside, we found :
188   JDIMENSION image_width;       // input image width 
189   JDIMENSION image_height;      // input image height 
190   int input_components;         // nb of color components in input image 
191   J_COLOR_SPACE in_color_space; // colorspace of input image 
192   double input_gamma;           // image gamma of input image 
193      -------------- */
194   
195   /* We use our private extension JPEG error handler.
196    * Note that this struct must live as long as the main JPEG parameter
197    * struct, to avoid dangling-pointer problems.
198    */
199   struct my_error_mgr jerr;
200   /* More stuff */
201  
202   JSAMPARRAY buffer;            /* Output row buffer */
203   
204   // rappel :
205   // ------
206   // typedef unsigned char JSAMPLE;
207   // typedef JSAMPLE FAR *JSAMPROW;     /* ptr to one image row of pixel samples. */
208   // typedef JSAMPROW *JSAMPARRAY;      /* ptr to some rows (a 2-D sample array) */
209   // typedef JSAMPARRAY *JSAMPIMAGE;    /* a 3-D sample array: top index is color */
210   
211  
212   int row_stride;               /* physical row width in output buffer */
213   
214  if (DEBUG) printf("entree dans gdcmFile::gdcm_read_JPEG_file12, depuis gdcmJpeg\n");
215
216
217   /* In this example we want to open the input file before doing anything else,
218    * so that the setjmp() error recovery below can assume the file is open.
219    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
220    * requires it in order to read binary files.
221    */
222
223   /* Step 1: allocate and initialize JPEG decompression object */
224   
225   if (DEBUG)printf("Entree Step 1\n");
226
227   /* We set up the normal JPEG error routines, then override error_exit. */
228   
229   cinfo.err = jpeg_std_error(&jerr.pub);
230   jerr.pub.error_exit = my_error_exit;
231   
232   /* Establish the setjmp return context for my_error_exit to use. */
233   
234   if (setjmp(jerr.setjmp_buffer)) {
235     /* If we get here, the JPEG code has signaled an error.
236      * We need to clean up the JPEG object, close the input file, and return.
237      */
238     jpeg_destroy_decompress(&cinfo);
239     return 0;
240   }
241   /* Now we can initialize the JPEG decompression object. */
242   jpeg_create_decompress(&cinfo);
243
244   /* Step 2: specify data source (eg, a file) */
245   
246 if (DEBUG) printf("Entree Step 2\n");
247
248   jpeg_stdio_src(&cinfo, fp);
249
250   /* Step 3: read file parameters with jpeg_read_header() */
251
252  if (DEBUG) printf("Entree Step 3\n");
253
254   (void) jpeg_read_header(&cinfo, TRUE);
255    
256   /* We can ignore the return value from jpeg_read_header since
257    *   (a) suspension is not possible with the stdio data source, and
258    *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
259    * See libjpeg.doc for more info.
260    */
261
262 if (DEBUG) {   
263         printf("--------------Header contents :----------------\n");
264         printf("image_width %d image_height %d\n", 
265                                 cinfo.image_width , cinfo.image_height);
266         printf("bits of precision in image data  %d \n", 
267                                 cinfo.output_components);
268         printf("nb of color components returned  %d \n", 
269                                 cinfo.data_precision);
270 }
271
272
273 /*
274   JDIMENSION image_width;       // input image width 
275   JDIMENSION image_height;      // input image height 
276   int output_components;        // # of color components returned 
277   J_COLOR_SPACE in_color_space; // colorspace of input image 
278   double input_gamma;           // image gamma of input image
279   int data_precision;           // bits of precision in image data 
280  
281 */
282
283   /* Step 4: set parameters for decompression */
284   
285  if (DEBUG) printf("Entree Step 4\n");
286
287   /* In this example, we don't need to change any of the defaults set by
288    * jpeg_read_header(), so we do nothing here.
289    */
290
291   /* Step 5: Start decompressor */
292   
293  if (DEBUG) printf("Entree Step 5\n");
294
295   (void) jpeg_start_decompress(&cinfo);
296   /* We can ignore the return value since suspension is not possible
297    * with the stdio data source.
298    */
299    
300   /* We may need to do some setup of our own at this point before reading
301    * the data.  After jpeg_start_decompress() we have the correct scaled
302    * output image dimensions available, as well as the output colormap
303    * if we asked for color quantization.
304    * In this example, we need to make an output work buffer of the right size.
305    */ 
306    
307   /* JSAMPLEs per row in output buffer */
308   row_stride = cinfo.output_width * cinfo.output_components;
309   
310  if (DEBUG) printf ("cinfo.output_width %d cinfo.output_components %d  row_stride %d\n",
311         cinfo.output_width, cinfo.output_components,row_stride);
312         
313   /* Make a one-row-high sample array that will go away when done with image */
314   buffer = (*cinfo.mem->alloc_sarray)
315                 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
316
317   /* Step 6: while (scan lines remain to be read) */
318   
319  if (DEBUG)  printf("Entree Step 6\n"); 
320
321   /*           jpeg_read_scanlines(...); */
322
323   /* Here we use the library's state variable cinfo.output_scanline as the
324    * loop counter, so that we don't have to keep track ourselves.
325    */
326    
327  if (DEBUG)  printf ("cinfo.output_height %d  cinfo.output_width %d\n",
328                         cinfo.output_height,cinfo.output_width);
329  
330   pimage=(char *)image_buffer;
331    
332   while (cinfo.output_scanline < cinfo.output_height) {
333     /* jpeg_read_scanlines expects an array of pointers to scanlines.
334      * Here the array is only one element long, but you could ask for
335      * more than one scanline at a time if that's more convenient.
336      */
337     
338      (void) jpeg_read_scanlines(&cinfo, buffer, 1);
339       
340      if ( BITS_IN_JSAMPLE == 8) {
341          memcpy( pimage, buffer[0],row_stride); 
342          pimage+=row_stride;
343      } else {
344          memcpy( pimage, buffer[0],row_stride*2 ); // FIXME : *2  car 16 bits?!?
345          pimage+=row_stride*2;                     // FIXME : *2 car 16 bits?!?     
346      }
347   }
348  
349   /* Step 7: Finish decompression */
350   
351 if (DEBUG)  printf("Entree Step 7\n");
352
353   (void) jpeg_finish_decompress(&cinfo);
354   /* We can ignore the return value since suspension is not possible
355    * with the stdio data source.
356    */
357
358   /* Step 8: Release JPEG decompression object */
359   
360 if (DEBUG) printf("Entree Step 8\n");
361
362   /* This is an important step since it will release a good deal of memory. */
363   
364   jpeg_destroy_decompress(&cinfo);
365
366   /* After finish_decompress, we can close the input file.
367    * Here we postpone it until after no more JPEG errors are possible,
368    * so as to simplify the setjmp error logic above.  (Actually, I don't
369    * think that jpeg_destroy can do an error exit, but why assume anything...)
370    */
371
372   /* At this point you may want to check to see whether any corrupt-data
373    * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
374    */
375
376   /* And we're done! */
377   
378   return 1;
379 }
380
381 /*
382  * SOME FINE POINTS:
383  *
384  * In the above code, we ignored the return value of jpeg_read_scanlines,
385  * which is the number of scanlines actually read.  We could get away with
386  * this because we asked for only one line at a time and we weren't using
387  * a suspending data source.  See libjpeg.doc for more info.
388  *
389  * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
390  * we should have done it beforehand to ensure that the space would be
391  * counted against the JPEG max_memory setting.  In some systems the above
392  * code would risk an out-of-memory error.  However, in general we don't
393  * know the output image dimensions before jpeg_start_decompress(), unless we
394  * call jpeg_calc_output_dimensions().  See libjpeg.doc for more about this.
395  *
396  * Scanlines are returned in the same order as they appear in the JPEG file,
397  * which is standardly top-to-bottom.  If you must emit data bottom-to-top,
398  * you can use one of the virtual arrays provided by the JPEG memory manager
399  * to invert the data.  See wrbmp.c for an example.
400  *
401  * As with compression, some operating modes may require temporary files.
402  * On some systems you may need to set up a signal handler to ensure that
403  * temporary files are deleted if the program is interrupted.  See libjpeg.doc.
404  */
405  
406
407
408
409
410  
411