4 #define BITS_IN_JSAMPLE 12
6 // BITS_IN_JSAMPLE is a compile time defined options.
7 // We need both 8 an 12;
8 // To avoid renaming *all* the Jpeg functions,
9 // we hard code the 'brain damaged liker' option.
10 // For all the functions, we shall have the 8 and 12 version
11 // (8 with the 'long' name, 12 with the 'short' name)
13 #define jpeg_read_header jReadHeader
14 #define my_error_exit myErrorExit
15 #define jpeg_destroy_decompress jDestDecompress
16 #define jpeg_stdio_src jStdSrc
17 #define jpeg_read_header jReadHeader
18 #define jpeg_read_scanlines jReadScanlines
19 #define jpeg_finish_decompress jFinDecompress
20 #define jpeg_create_decompress jCreaDecompress
23 #define jpeg_std_error jStdError
24 #define jpeg_CreateCompress jCreaCompress
25 #define jpeg_CreateDecompress jCreaDecompress
26 #define jpeg_destroy_compress jDestCompress
27 #define jpeg_destroy_decompress jDestDecompress
28 #define jpeg_stdio_dest jStdDest
29 #define jpeg_stdio_src jStdSrc
30 #define jpeg_set_defaults jSetDefaults
31 #define jpeg_set_colorspace jSetColorspace
32 #define jpeg_default_colorspace jDefColorspace
33 #define jpeg_set_quality jSetQuality
34 #define jpeg_set_linear_quality jSetLQuality
35 #define jpeg_add_quant_table jAddQuantTable
36 #define jpeg_quality_scaling jQualityScaling
37 #define jpeg_simple_progression jSimProgress
38 #define jpeg_suppress_tables jSuppressTables
39 #define jpeg_alloc_quant_table jAlcQTable
40 #define jpeg_alloc_huff_table jAlcHTable
41 #define jpeg_start_compress jStrtCompress
42 #define jpeg_write_scanlines jWrtScanlines
43 #define jpeg_finish_compress jFinCompress
44 #define jpeg_write_raw_data jWrtRawData
45 #define jpeg_write_marker jWrtMarker
46 #define jpeg_write_m_header jWrtMHeader
47 #define jpeg_write_m_byte jWrtMByte
48 #define jpeg_write_tables jWrtTables
49 #define jpeg_read_header jReadHeader
50 #define jpeg_start_decompress jStrtDecompress
51 #define jpeg_read_scanlines jReadScanlines
52 #define jpeg_finish_decompress jFinDecompress
53 #define jpeg_read_raw_data jReadRawData
54 #define jpeg_has_multiple_scans jHasMultScn
55 #define jpeg_start_output jStrtOutput
56 #define jpeg_finish_output jFinOutput
57 #define jpeg_input_complete jInComplete
58 #define jpeg_new_colormap jNewCMap
59 #define jpeg_consume_input jConsumeInput
60 #define jpeg_calc_output_dimensions jCalcDimensions
61 #define jpeg_save_markers jSaveMarkers
62 #define jpeg_set_marker_processor jSetMarker
63 #define jpeg_read_coefficients jReadCoefs
64 #define jpeg_write_coefficients jWrtCoefs
65 #define jpeg_copy_critical_parameters jCopyCrit
66 #define jpeg_abort_compress jAbrtCompress
67 #define jpeg_abort_decompress jAbrtDecompress
68 #define jpeg_abort jAbort
69 #define jpeg_destroy jDestroy
70 #define jpeg_resync_to_restart jResyncRestart
78 * <setjmp.h> is used for the optional error recovery mechanism shown in
79 * the second part of the example.
83 * Include file for users of JPEG library.
84 * You will need to have included system headers that define at least
85 * the typedefs FILE and size_t before you can include jpeglib.h.
86 * (stdio.h is sufficient on ANSI-conforming systems.)
87 * You may also wish to include "jerror.h".
91 #include "jpeg/libijg12/jpeglib12.h"
95 /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
97 /* This half of the example shows how to read data from the JPEG decompressor.
98 * It's a bit more refined than the above, in that we show:
99 * (a) how to modify the JPEG library's standard error-reporting behavior;
100 * (b) how to allocate workspace using the library's memory manager.
102 * Just to make this example a little different from the first one, we'll
103 * assume that we do not intend to put the whole image into an in-memory
104 * buffer, but to send it line-by-line someplace else. We need a one-
105 * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
106 * memory manager allocate it for us. This approach is actually quite useful
107 * because we don't need to remember to deallocate the buffer separately: it
108 * will go away automatically when the JPEG object is cleaned up.
114 * The JPEG library's standard error handler (jerror.c) is divided into
115 * several "methods" which you can override individually. This lets you
116 * adjust the behavior without duplicating a lot of code, which you might
117 * have to update with each future release.
119 * Our example here shows how to override the "error_exit" method so that
120 * control is returned to the library's caller when a fatal error occurs,
121 * rather than calling exit() as the standard error_exit method does.
123 * We use C's setjmp/longjmp facility to return control. This means that the
124 * routine which calls the JPEG library must first execute a setjmp() call to
125 * establish the return point. We want the replacement error_exit to do a
126 * longjmp(). But we need to make the setjmp buffer accessible to the
127 * error_exit routine. To do this, we make a private extension of the
128 * standard JPEG error handler object. (If we were using C++, we'd say we
129 * were making a subclass of the regular error handler.)
131 * Here's the extended error handler struct:
134 struct my_error_mgr {
135 struct jpeg_error_mgr pub; /* "public" fields */
136 jmp_buf setjmp_buffer; /* for return to caller */
139 typedef struct my_error_mgr * my_error_ptr;
142 * Here's the routine that will replace the standard error_exit method:
146 my_error_exit (j_common_ptr cinfo) {
147 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
148 my_error_ptr myerr = (my_error_ptr) cinfo->err;
150 /* Always display the message. */
151 /* We could postpone this until after returning, if we chose. */
152 (*cinfo->err->output_message) (cinfo);
154 /* Return control to the setjmp point */
155 longjmp(myerr->setjmp_buffer, 1);
160 * Sample routine for JPEG decompression. We assume that the source file name
161 * is passed in. We want to return 1 on success, 0 on error.
167 gdcmFile::gdcm_read_JPEG_file12 (void * image_buffer) {
171 /* This struct contains the JPEG decompression parameters and pointers to
172 * working space (which is allocated as needed by the JPEG library).
175 struct jpeg_decompress_struct cinfo;
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
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.
189 struct my_error_mgr jerr;
192 JSAMPARRAY buffer; /* Output row buffer */
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 */
202 int row_stride; /* physical row width in output buffer */
204 if (DEBUG) printf("entree dans gdcmFile::gdcm_read_JPEG_file12, depuis gdcmJpeg\n");
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.
213 /* Step 1: allocate and initialize JPEG decompression object */
215 if (DEBUG)printf("Entree Step 1\n");
217 /* We set up the normal JPEG error routines, then override error_exit. */
219 cinfo.err = jpeg_std_error(&jerr.pub);
220 jerr.pub.error_exit = my_error_exit;
222 /* Establish the setjmp return context for my_error_exit to use. */
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.
228 jpeg_destroy_decompress(&cinfo);
231 /* Now we can initialize the JPEG decompression object. */
232 jpeg_create_decompress(&cinfo);
234 /* Step 2: specify data source (eg, a file) */
236 if (DEBUG) printf("Entree Step 2\n");
238 jpeg_stdio_src(&cinfo, fp);
240 /* Step 3: read file parameters with jpeg_read_header() */
242 if (DEBUG) printf("Entree Step 3\n");
244 (void) jpeg_read_header(&cinfo, TRUE);
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.
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);
264 JDIMENSION image_width; // input image width
265 JDIMENSION image_height; // input image height
266 int output_components; // # of color components returned
267 J_COLOR_SPACE in_color_space; // colorspace of input image
268 double input_gamma; // image gamma of input image
269 int data_precision; // bits of precision in image data
273 /* Step 4: set parameters for decompression */
275 if (DEBUG) printf("Entree Step 4\n");
277 /* In this example, we don't need to change any of the defaults set by
278 * jpeg_read_header(), so we do nothing here.
281 /* Step 5: Start decompressor */
283 if (DEBUG) printf("Entree Step 5\n");
285 (void) jpeg_start_decompress(&cinfo);
286 /* We can ignore the return value since suspension is not possible
287 * with the stdio data source.
290 /* We may need to do some setup of our own at this point before reading
291 * the data. After jpeg_start_decompress() we have the correct scaled
292 * output image dimensions available, as well as the output colormap
293 * if we asked for color quantization.
294 * In this example, we need to make an output work buffer of the right size.
297 /* JSAMPLEs per row in output buffer */
298 row_stride = cinfo.output_width * cinfo.output_components;
300 if (DEBUG) printf ("cinfo.output_width %d cinfo.output_components %d row_stride %d\n",
301 cinfo.output_width, cinfo.output_components,row_stride);
303 /* Make a one-row-high sample array that will go away when done with image */
304 buffer = (*cinfo.mem->alloc_sarray)
305 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
307 /* Step 6: while (scan lines remain to be read) */
309 if (DEBUG) printf("Entree Step 6\n");
311 /* jpeg_read_scanlines(...); */
313 /* Here we use the library's state variable cinfo.output_scanline as the
314 * loop counter, so that we don't have to keep track ourselves.
317 if (DEBUG) printf ("cinfo.output_height %d cinfo.output_width %d\n",
318 cinfo.output_height,cinfo.output_width);
320 pimage=(char *)image_buffer;
322 while (cinfo.output_scanline < cinfo.output_height) {
323 /* jpeg_read_scanlines expects an array of pointers to scanlines.
324 * Here the array is only one element long, but you could ask for
325 * more than one scanline at a time if that's more convenient.
328 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
330 if ( BITS_IN_JSAMPLE == 8) {
331 memcpy( pimage, buffer[0],row_stride);
334 memcpy( pimage, buffer[0],row_stride*2 ); // FIXME : *2 car 16 bits?!?
335 pimage+=row_stride*2; // FIXME : *2 car 16 bits?!?
339 /* Step 7: Finish decompression */
341 if (DEBUG) printf("Entree Step 7\n");
343 (void) jpeg_finish_decompress(&cinfo);
344 /* We can ignore the return value since suspension is not possible
345 * with the stdio data source.
348 /* Step 8: Release JPEG decompression object */
350 if (DEBUG) printf("Entree Step 8\n");
352 /* This is an important step since it will release a good deal of memory. */
354 jpeg_destroy_decompress(&cinfo);
356 /* After finish_decompress, we can close the input file.
357 * Here we postpone it until after no more JPEG errors are possible,
358 * so as to simplify the setjmp error logic above. (Actually, I don't
359 * think that jpeg_destroy can do an error exit, but why assume anything...)
362 /* At this point you may want to check to see whether any corrupt-data
363 * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
366 /* And we're done! */
374 * In the above code, we ignored the return value of jpeg_read_scanlines,
375 * which is the number of scanlines actually read. We could get away with
376 * this because we asked for only one line at a time and we weren't using
377 * a suspending data source. See libjpeg.doc for more info.
379 * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
380 * we should have done it beforehand to ensure that the space would be
381 * counted against the JPEG max_memory setting. In some systems the above
382 * code would risk an out-of-memory error. However, in general we don't
383 * know the output image dimensions before jpeg_start_decompress(), unless we
384 * call jpeg_calc_output_dimensions(). See libjpeg.doc for more about this.
386 * Scanlines are returned in the same order as they appear in the JPEG file,
387 * which is standardly top-to-bottom. If you must emit data bottom-to-top,
388 * you can use one of the virtual arrays provided by the JPEG memory manager
389 * to invert the data. See wrbmp.c for an example.
391 * As with compression, some operating modes may require temporary files.
392 * On some systems you may need to set up a signal handler to ensure that
393 * temporary files are deleted if the program is interrupted. See libjpeg.doc.