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