5 // for jpeglib defined BITS_IN_JSAMPLE
6 #include "jpeg/libijg8/jBitsInJsample.h"
7 // FIXME : find something else when both
8 // libJpeg8 and libJpeg12 will be active
13 DICOM provides a mechanism for supporting the use of JPEG Image Compression
14 through the Encapsulated Format (see PS 3.3 of the DICOM Standard).
15 Annex A defines a number of Transfer Syntaxes which reference
16 the JPEG Standard and provide a number of lossless (bit preserving)
17 and lossy compression schemes.
18 In order to facilitate interoperability of implementations conforming
19 to the DICOM Standard which elect to use one or more
20 of the Transfer Syntaxes for JPEG Image Compression, the following policy is specified:
22 Any implementation which conforms to the DICOM Standard and has elected
23 to support any one of the Transfer Syntaxes for lossless JPEG Image Compression,
24 shall support the following lossless compression:
25 The subset (first-order horizontal prediction [Selection Value 1) of JPEG Process 14
26 (DPCM, non-hierarchical with Huffman coding) (see Annex F of the DICOM Standard).
28 Any implementation which conforms to the DICOM Standard and has elected
29 to support any one of the Transfer Syntaxes for 8-bit lossy JPEG Image Compression,
30 shall support the JPEG Baseline Compression (coding Process 1).
32 Any implementation which conforms to the DICOM Standard and has elected
33 to support any one of the Transfer Syntaxes for 12-bit lossy JPEG Image Compression,
34 shall support the JPEG Compression Process 4.
36 Note: The DICOM conformance statement shall differentiate between implementations
37 that can simply receive JPEG encoded images and those that can receive and process
38 JPEG encoded images (see PS 3.2 of the DICOM Standard).
40 The use of the DICOM Encapsulated Format to support JPEG Compressed Pixel Data
41 implies that the Data Elements which are related to the Native Format Pixel Data encoding
42 (e.g. Bits Allocated, Bits Stored, High Bit, Pixel Representation, Rows, Columns, etc.)
43 shall contain values which are consistent with the characteristics
44 of the uncompressed pixel data from which the compressed Data Stream was derived.
45 The Pixel Data characteristics included in the JPEG Interchange Format
46 shall be used to decode the compressed data stream.
48 Run Length Encoding Compression
50 DICOM provides a mechanism for supporting the use of Run Length Encoding (RLE)
51 Compression which is a byte oriented lossless compression scheme through
52 the encapsulated Format (see PS 3.3 of this Standard).
53 Annex G of the DICOM Standard defines RLE Compression and its Transfer Syntax.
55 Note: The RLE Compression algorithm described in Annex G
56 of the DICOM Standard is the compression used in
57 the TIFF 6.0 specification known as the "PackBits" scheme.
59 The use of the DICOM Encapsulated Format to support RLE Compressed Pixel Data
60 implies that the Data Elements which are related to the Native Format Pixel Data encoding (
61 e.g. Bits Allocated, Bits Stored, High Bit, Pixel Representation, Rows, Columns, etc.)
62 shall contain values which are consistent with the characteristics
63 of the uncompressed pixel data from which the compressed data is derived
67 * <setjmp.h> is used for the optional error recovery mechanism shown in
68 * the second part of the example.
72 * Include file for users of JPEG library.
73 * You will need to have included system headers that define at least
74 * the typedefs FILE and size_t before you can include jpeglib.h.
75 * (stdio.h is sufficient on ANSI-conforming systems.)
76 * You may also wish to include "jerror.h".
84 /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
86 /* This half of the example shows how to read data from the JPEG decompressor.
87 * It's a bit more refined than the above, in that we show:
88 * (a) how to modify the JPEG library's standard error-reporting behavior;
89 * (b) how to allocate workspace using the library's memory manager.
91 * Just to make this example a little different from the first one, we'll
92 * assume that we do not intend to put the whole image into an in-memory
93 * buffer, but to send it line-by-line someplace else. We need a one-
94 * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
95 * memory manager allocate it for us. This approach is actually quite useful
96 * because we don't need to remember to deallocate the buffer separately: it
97 * will go away automatically when the JPEG object is cleaned up.
103 * The JPEG library's standard error handler (jerror.c) is divided into
104 * several "methods" which you can override individually. This lets you
105 * adjust the behavior without duplicating a lot of code, which you might
106 * have to update with each future release.
108 * Our example here shows how to override the "error_exit" method so that
109 * control is returned to the library's caller when a fatal error occurs,
110 * rather than calling exit() as the standard error_exit method does.
112 * We use C's setjmp/longjmp facility to return control. This means that the
113 * routine which calls the JPEG library must first execute a setjmp() call to
114 * establish the return point. We want the replacement error_exit to do a
115 * longjmp(). But we need to make the setjmp buffer accessible to the
116 * error_exit routine. To do this, we make a private extension of the
117 * standard JPEG error handler object. (If we were using C++, we'd say we
118 * were making a subclass of the regular error handler.)
120 * Here's the extended error handler struct:
123 struct my_error_mgr {
124 struct jpeg_error_mgr pub; /* "public" fields */
125 jmp_buf setjmp_buffer; /* for return to caller */
128 typedef struct my_error_mgr * my_error_ptr;
131 * Here's the routine that will replace the standard error_exit method:
135 my_error_exit (j_common_ptr cinfo) {
136 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
137 my_error_ptr myerr = (my_error_ptr) cinfo->err;
139 /* Always display the message. */
140 /* We could postpone this until after returning, if we chose. */
141 (*cinfo->err->output_message) (cinfo);
143 /* Return control to the setjmp point */
144 longjmp(myerr->setjmp_buffer, 1);
149 * Sample routine for JPEG decompression. We assume that the source file name
150 * is passed in. We want to return 1 on success, 0 on error.
156 gdcmFile::gdcm_read_JPEG_file (void * image_buffer) {
160 /* This struct contains the JPEG decompression parameters and pointers to
161 * working space (which is allocated as needed by the JPEG library).
164 struct jpeg_decompress_struct cinfo;
166 /* -------------- inside, we found :
167 JDIMENSION image_width; // input image width
168 JDIMENSION image_height; // input image height
169 int input_components; // nb of color components in input image
170 J_COLOR_SPACE in_color_space; // colorspace of input image
171 double input_gamma; // image gamma of input image
174 /* We use our private extension JPEG error handler.
175 * Note that this struct must live as long as the main JPEG parameter
176 * struct, to avoid dangling-pointer problems.
178 struct my_error_mgr jerr;
181 JSAMPARRAY buffer; /* Output row buffer */
185 // typedef unsigned char JSAMPLE;
186 // typedef JSAMPLE FAR *JSAMPROW; /* ptr to one image row of pixel samples. */
187 // typedef JSAMPROW *JSAMPARRAY; /* ptr to some rows (a 2-D sample array) */
188 // typedef JSAMPARRAY *JSAMPIMAGE; /* a 3-D sample array: top index is color */
191 int row_stride; /* physical row width in output buffer */
193 if (DEBUG) printf("entree dans gdcmFile::gdcm_read_JPEG_file, depuis gdcmJpeg\n");
196 /* In this example we want to open the input file before doing anything else,
197 * so that the setjmp() error recovery below can assume the file is open.
198 * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
199 * requires it in order to read binary files.
202 /* Step 1: allocate and initialize JPEG decompression object */
204 if (DEBUG)printf("Entree Step 1\n");
206 /* We set up the normal JPEG error routines, then override error_exit. */
208 cinfo.err = jpeg_std_error(&jerr.pub);
209 jerr.pub.error_exit = my_error_exit;
211 /* Establish the setjmp return context for my_error_exit to use. */
213 if (setjmp(jerr.setjmp_buffer)) {
214 /* If we get here, the JPEG code has signaled an error.
215 * We need to clean up the JPEG object, close the input file, and return.
217 jpeg_destroy_decompress(&cinfo);
220 /* Now we can initialize the JPEG decompression object. */
221 jpeg_create_decompress(&cinfo);
223 /* Step 2: specify data source (eg, a file) */
225 if (DEBUG) printf("Entree Step 2\n");
227 jpeg_stdio_src(&cinfo, fp);
229 /* Step 3: read file parameters with jpeg_read_header() */
231 if (DEBUG) printf("Entree Step 3\n");
233 (void) jpeg_read_header(&cinfo, TRUE);
235 /* We can ignore the return value from jpeg_read_header since
236 * (a) suspension is not possible with the stdio data source, and
237 * (b) we passed TRUE to reject a tables-only JPEG file as an error.
238 * See libjpeg.doc for more info.
242 printf("--------------Header contents :----------------\n");
243 printf("image_width %d image_height %d\n",
244 cinfo.image_width , cinfo.image_height);
245 printf("bits of precision in image data %d \n",
246 cinfo.output_components);
247 printf("nb of color components returned %d \n",
248 cinfo.data_precision);
253 JDIMENSION image_width; // input image width
254 JDIMENSION image_height; // input image height
255 int output_components; // # of color components returned
256 J_COLOR_SPACE in_color_space; // colorspace of input image
257 double input_gamma; // image gamma of input image
258 int data_precision; // bits of precision in image data
262 /* Step 4: set parameters for decompression */
264 if (DEBUG) printf("Entree Step 4\n");
266 /* In this example, we don't need to change any of the defaults set by
267 * jpeg_read_header(), so we do nothing here.
270 /* Step 5: Start decompressor */
272 if (DEBUG) printf("Entree Step 5\n");
274 (void) jpeg_start_decompress(&cinfo);
275 /* We can ignore the return value since suspension is not possible
276 * with the stdio data source.
279 /* We may need to do some setup of our own at this point before reading
280 * the data. After jpeg_start_decompress() we have the correct scaled
281 * output image dimensions available, as well as the output colormap
282 * if we asked for color quantization.
283 * In this example, we need to make an output work buffer of the right size.
286 /* JSAMPLEs per row in output buffer */
287 row_stride = cinfo.output_width * cinfo.output_components;
289 if (DEBUG) printf ("cinfo.output_width %d cinfo.output_components %d row_stride %d\n",
290 cinfo.output_width, cinfo.output_components,row_stride);
292 /* Make a one-row-high sample array that will go away when done with image */
293 buffer = (*cinfo.mem->alloc_sarray)
294 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
296 /* Step 6: while (scan lines remain to be read) */
298 if (DEBUG) printf("Entree Step 6\n");
300 /* jpeg_read_scanlines(...); */
302 /* Here we use the library's state variable cinfo.output_scanline as the
303 * loop counter, so that we don't have to keep track ourselves.
306 if (DEBUG) printf ("cinfo.output_height %d cinfo.output_width %d\n",
307 cinfo.output_height,cinfo.output_width);
309 pimage=(char *)image_buffer;
312 while (cinfo.output_scanline < cinfo.output_height) {
313 /* jpeg_read_scanlines expects an array of pointers to scanlines.
314 * Here the array is only one element long, but you could ask for
315 * more than one scanline at a time if that's more convenient.
318 // l'image est deja allouée (et passée en param)
319 // on ecrit directement les pixels
320 // (on DEVRAIT pouvoir)
322 //(void) jpeg_read_scanlines(&cinfo, pimage, 1);
324 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
326 if ( BITS_IN_JSAMPLE == 8) {
327 memcpy( pimage, buffer[0],row_stride);
330 memcpy( pimage, buffer[0],row_stride*2 ); // FIXME : *2 car 16 bits?!?
331 pimage+=row_stride*2; // FIXME : *2 car 16 bits?!?
335 /* Step 7: Finish decompression */
337 if (DEBUG) printf("Entree Step 7\n");
339 (void) jpeg_finish_decompress(&cinfo);
340 /* We can ignore the return value since suspension is not possible
341 * with the stdio data source.
344 /* Step 8: Release JPEG decompression object */
346 if (DEBUG) printf("Entree Step 8\n");
348 /* This is an important step since it will release a good deal of memory. */
350 jpeg_destroy_decompress(&cinfo);
352 /* After finish_decompress, we can close the input file.
353 * Here we postpone it until after no more JPEG errors are possible,
354 * so as to simplify the setjmp error logic above. (Actually, I don't
355 * think that jpeg_destroy can do an error exit, but why assume anything...)
358 /* At this point you may want to check to see whether any corrupt-data
359 * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
362 /* And we're done! */
370 * In the above code, we ignored the return value of jpeg_read_scanlines,
371 * which is the number of scanlines actually read. We could get away with
372 * this because we asked for only one line at a time and we weren't using
373 * a suspending data source. See libjpeg.doc for more info.
375 * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
376 * we should have done it beforehand to ensure that the space would be
377 * counted against the JPEG max_memory setting. In some systems the above
378 * code would risk an out-of-memory error. However, in general we don't
379 * know the output image dimensions before jpeg_start_decompress(), unless we
380 * call jpeg_calc_output_dimensions(). See libjpeg.doc for more about this.
382 * Scanlines are returned in the same order as they appear in the JPEG file,
383 * which is standardly top-to-bottom. If you must emit data bottom-to-top,
384 * you can use one of the virtual arrays provided by the JPEG memory manager
385 * to invert the data. See wrbmp.c for an example.
387 * As with compression, some operating modes may require temporary files.
388 * On some systems you may need to set up a signal handler to ensure that
389 * temporary files are deleted if the program is interrupted. See libjpeg.doc.