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