1 /*=========================================================================
4 Module: $RCSfile: gdcmJpeg.cxx,v $
6 Date: $Date: 2004/12/12 17:21:08 $
7 Version: $Revision: 1.29 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
21 DICOM provides a mechanism for supporting the use of JPEG Image Compression
22 through the Encapsulated Format (see PS 3.3 of the DICOM Standard).
23 Annex A defines a number of Transfer Syntaxes which reference
24 the JPEG Standard and provide a number of lossless (bit preserving)
25 and lossy compression schemes.
26 In order to facilitate interoperability of implementations conforming
27 to the DICOM Standard which elect to use one or more
28 of the Transfer Syntaxes for JPEG Image Compression, the following policy is specified:
30 Any implementation which conforms to the DICOM Standard and has elected
31 to support any one of the Transfer Syntaxes for lossless JPEG Image Compression,
32 shall support the following lossless compression:
33 The subset (first-order horizontal prediction [Selection Value 1) of JPEG Process 14
34 (DPCM, non-hierarchical with Huffman coding) (see Annex F of the DICOM Standard).
36 Any implementation which conforms to the DICOM Standard and has elected
37 to support any one of the Transfer Syntaxes for 8-bit lossy JPEG Image Compression,
38 shall support the JPEG Baseline Compression (coding Process 1).
40 Any implementation which conforms to the DICOM Standard and has elected
41 to support any one of the Transfer Syntaxes for 12-bit lossy JPEG Image Compression,
42 shall support the JPEG Compression Process 4.
44 Note: The DICOM conformance statement shall differentiate between implementations
45 that can simply receive JPEG encoded images and those that can receive and process
46 JPEG encoded images (see PS 3.2 of the DICOM Standard).
48 The use of the DICOM Encapsulated Format to support JPEG Compressed Pixel Data
49 implies that the Data Elements which are related to the Native Format Pixel Data encoding
50 (e.g. Bits Allocated, Bits Stored, High Bit, Pixel Representation, Rows, Columns, etc.)
51 shall contain values which are consistent with the characteristics
52 of the uncompressed pixel data from which the compressed Data Stream was derived.
53 The Pixel Data characteristics included in the JPEG Interchange Format
54 shall be used to decode the compressed data stream.
56 Run Length Encoding Compression
58 DICOM provides a mechanism for supporting the use of Run Length Encoding (RLE)
59 Compression which is a byte oriented lossless compression scheme through
60 the encapsulated Format (see PS 3.3 of this Standard).
61 Annex G of the DICOM Standard defines RLE Compression and its Transfer Syntax.
63 Note: The RLE Compression algorithm described in Annex G
64 of the DICOM Standard is the compression used in
65 the TIFF 6.0 specification known as the "PackBits" scheme.
67 The use of the DICOM Encapsulated Format to support RLE Compressed Pixel Data
68 implies that the Data Elements which are related to the Native Format Pixel Data encoding (
69 e.g. Bits Allocated, Bits Stored, High Bit, Pixel Representation, Rows, Columns, etc.)
70 shall contain values which are consistent with the characteristics
71 of the uncompressed pixel data from which the compressed data is derived
75 * <setjmp.h> is used for the optional error recovery mechanism shown in
76 * the second part of the example.
80 * Include file for users of JPEG library.
81 * You will need to have included system headers that define at least
82 * the typedefs FILE and size_t before you can include jpeglib.h.
83 * (stdio.h is sufficient on ANSI-conforming systems.)
84 * You may also wish to include "jerror.h".
89 #include "jdatasrc.cxx"
90 #include "jdatadst.cxx"
94 /******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/
96 /* This half of the example shows how to feed data into the JPEG compressor.
97 * We present a minimal version that does not worry about refinements such
98 * as error recovery (the JPEG code will just exit() if it gets an error).
102 * IMAGE DATA FORMATS:
104 * The standard input image format is a rectangular array of pixels, with
105 * each pixel having the same number of "component" values (color channels).
106 * Each pixel row is an array of JSAMPLEs (which typically are unsigned chars).
107 * If you are working with color data, then the color values for each pixel
108 * must be adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit
111 * For this example, we'll assume that this data structure matches the way
112 * our application has stored the image in memory, so we can just pass a
113 * pointer to our image buffer. In particular, let's say that the image is
114 * RGB color and is described by:
118 //extern JSAMPLE * image_buffer; /* Points to large array of R,G,B-order data */
119 //extern int image_height; /* Number of rows in image */
120 //extern int image_width; /* Number of columns in image */
125 * Sample routine for JPEG compression. We assume that the target file name
126 * and a compression quality factor are passed in.
131 * \brief routine for JPEG decompression
132 * @param fp pointer to an already open file descriptor
133 * 8 significant bits per pixel
134 * @param im_buf Points to array (of R,G,B-order) data to compress
135 * @param quality compression quality
136 * @param image_height Number of rows in image
137 * @param image_width Number of columns in image
138 * @return 1 on success, 0 on error
141 bool gdcm_write_JPEG_file (std::ofstream* fp, void* im_buf,
142 int image_width, int image_height, int quality)
145 JSAMPLE* image_buffer = (JSAMPLE*) im_buf;
147 /* This struct contains the JPEG compression parameters and pointers to
148 * working space (which is allocated as needed by the JPEG library).
149 * It is possible to have several such structures, representing multiple
150 * compression/decompression processes, in existence at once. We refer
151 * to any one struct (and its associated working data) as a "JPEG object".
153 struct jpeg_compress_struct cinfo;
154 /* This struct represents a JPEG error handler. It is declared separately
155 * because applications often want to supply a specialized error handler
156 * (see the second half of this file for an example). But here we just
157 * take the easy way out and use the standard error handler, which will
158 * print a message on stderr and call exit() if compression fails.
159 * Note that this struct must live as long as the main JPEG parameter
160 * struct, to avoid dangling-pointer problems.
162 struct jpeg_error_mgr jerr;
164 //FILE* outfile; /* target FILE* /
165 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
166 int row_stride; /* physical row width in image buffer */
168 /* Step 1: allocate and initialize JPEG compression object */
170 /* We have to set up the error handler first, in case the initialization
171 * step fails. (Unlikely, but it could happen if you are out of memory.)
172 * This routine fills in the contents of struct jerr, and returns jerr's
173 * address which we place into the link field in cinfo.
175 cinfo.err = jpeg_std_error(&jerr);
176 /* Now we can initialize the JPEG compression object. */
177 jpeg_create_compress(&cinfo);
179 /* Step 2: specify data destination (eg, a file) */
180 /* Note: steps 2 and 3 can be done in either order. */
182 /* Here we use the library-supplied code to send compressed data to a
183 * stdio stream. You can also write your own code to do something else.
184 * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
185 * requires it in order to write binary files.
187 // if ((outfile = fopen(filename, "wb")) == NULL) {
188 // fprintf(stderr, "can't open %s\n", filename);
192 jpeg_stdio_dest(&cinfo, fp);
194 /* Step 3: set parameters for compression */
196 /* First we supply a description of the input image.
197 * Four fields of the cinfo struct must be filled in:
199 cinfo.image_width = image_width;/* image width and height, in pixels */
200 cinfo.image_height = image_height;
201 cinfo.input_components = 3; /* # of color components per pixel */
202 cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
203 /* Now use the library's routine to set default compression parameters.
204 * (You must set at least cinfo.in_color_space before calling this,
205 * since the defaults depend on the source color space.)
207 jpeg_set_defaults(&cinfo);
208 /* Now you can set any non-default parameters you wish to.
209 * Here we just illustrate the use of quality (quantization table) scaling:
211 jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
213 /* Step 4: Start compressor */
215 /* TRUE ensures that we will write a complete interchange-JPEG file.
216 * Pass TRUE unless you are very sure of what you're doing.
218 jpeg_start_compress(&cinfo, TRUE);
220 /* Step 5: while (scan lines remain to be written) */
221 /* jpeg_write_scanlines(...); */
223 /* Here we use the library's state variable cinfo.next_scanline as the
224 * loop counter, so that we don't have to keep track ourselves.
225 * To keep things simple, we pass one scanline per call; you can pass
226 * more if you wish, though.
228 row_stride = image_width * 3;/* JSAMPLEs per row in image_buffer */
230 while (cinfo.next_scanline < cinfo.image_height) {
231 /* jpeg_write_scanlines expects an array of pointers to scanlines.
232 * Here the array is only one element long, but you could pass
233 * more than one scanline at a time if that's more convenient.
235 row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
237 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
240 /* Step 6: Finish compression */
242 jpeg_finish_compress(&cinfo);
244 /* After finish_compress, we can close the output file. */
246 // fclose(fp); --> the caller will close (multiframe treatement)
248 /* Step 7: release JPEG compression object */
250 /* This is an important step since it will release a good deal of memory. */
251 jpeg_destroy_compress(&cinfo);
253 /* And we're done! */
263 * In the above loop, we ignored the return value of jpeg_write_scanlines,
264 * which is the number of scanlines actually written. We could get away
265 * with this because we were only relying on the value of cinfo.next_scanline,
266 * which will be incremented correctly. If you maintain additional loop
267 * variables then you should be careful to increment them properly.
268 * Actually, for output to a stdio stream you needn't worry, because
269 * then jpeg_write_scanlines will write all the lines passed (or else exit
270 * with a fatal error). Partial writes can only occur if you use a data
271 * destination module that can demand suspension of the compressor.
272 * (If you don't know what that's for, you don't need it.)
274 * If the compressor requires full-image buffers (for entropy-coding
275 * optimization or a multi-scan JPEG file), it will create temporary
276 * files for anything that doesn't fit within the maximum-memory setting.
277 * (Note that temp files are NOT needed if you use the default parameters.)
278 * On some systems you may need to set up a signal handler to ensure that
279 * temporary files are deleted if the program is interrupted. See libjpeg.doc.
281 * Scanlines MUST be supplied in top-to-bottom order if you want your JPEG
282 * files to be compatible with everyone else's. If you cannot readily read
283 * your data in that order, you'll need an intermediate array to hold the
284 * image. See rdtarga.c or rdbmp.c for examples of handling bottom-to-top
285 * source data using the JPEG code's internal virtual-array mechanisms.
290 /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
292 /* This half of the example shows how to read data from the JPEG decompressor.
293 * It's a bit more refined than the above, in that we show:
294 * (a) how to modify the JPEG library's standard error-reporting behavior;
295 * (b) how to allocate workspace using the library's memory manager.
297 * Just to make this example a little different from the first one, we'll
298 * assume that we do not intend to put the whole image into an in-memory
299 * buffer, but to send it line-by-line someplace else. We need a one-
300 * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
301 * memory manager allocate it for us. This approach is actually quite useful
302 * because we don't need to remember to deallocate the buffer separately: it
303 * will go away automatically when the JPEG object is cleaned up.
309 * The JPEG library's standard error handler (jerror.c) is divided into
310 * several "methods" which you can override individually. This lets you
311 * adjust the behavior without duplicating a lot of code, which you might
312 * have to update with each future release.
314 * Our example here shows how to override the "error_exit" method so that
315 * control is returned to the library's caller when a fatal error occurs,
316 * rather than calling exit() as the standard error_exit method does.
318 * We use C's setjmp/longjmp facility to return control. This means that the
319 * routine which calls the JPEG library must first execute a setjmp() call to
320 * establish the return point. We want the replacement error_exit to do a
321 * longjmp(). But we need to make the setjmp buffer accessible to the
322 * error_exit routine. To do this, we make a private extension of the
323 * standard JPEG error handler object. (If we were using C++, we'd say we
324 * were making a subclass of the regular error handler.)
326 * Here's the extended error handler struct:
329 //-----------------------------------------------------------------------------
330 struct my_error_mgr {
331 struct jpeg_error_mgr pub; /* "public" fields */
332 jmp_buf setjmp_buffer; /* for return to caller */
335 //-----------------------------------------------------------------------------
336 typedef struct my_error_mgr* my_error_ptr;
339 * Here's the routine that will replace the standard error_exit method:
341 METHODDEF(void) my_error_exit (j_common_ptr cinfo) {
342 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
343 my_error_ptr myerr = (my_error_ptr) cinfo->err;
345 /* Always display the message. */
346 /* We could postpone this until after returning, if we chose. */
347 (*cinfo->err->output_message) (cinfo);
349 /* Return control to the setjmp point */
350 longjmp(myerr->setjmp_buffer, 1);
353 //-----------------------------------------------------------------------------
355 * Sample routine for JPEG decompression. We assume that the source file name
356 * is passed in. We want to return 1 on success, 0 on error.
360 * \brief routine for JPEG decompression
361 * @param fp pointer to an already open file descriptor
362 * 8 significant bits per pixel
363 * @param image_buffer to receive uncompressed pixels
364 * @return 1 on success, 0 on error
367 bool gdcm_read_JPEG_file ( std::ifstream* fp, void* image_buffer )
371 /* This struct contains the JPEG decompression parameters and pointers to
372 * working space (which is allocated as needed by the JPEG library).
374 struct jpeg_decompress_struct cinfo;
376 /* -------------- inside, we found :
377 * JDIMENSION image_width; // input image width
378 * JDIMENSION image_height; // input image height
379 * int input_components; // nb of color components in input image
380 * J_COLOR_SPACE in_color_space; // colorspace of input image
381 * double input_gamma; // image gamma of input image
384 /* We use our private extension JPEG error handler.
385 * Note that this struct must live as long as the main JPEG parameter
386 * struct, to avoid dangling-pointer problems.
388 struct my_error_mgr jerr;
391 JSAMPARRAY buffer;/* Output row buffer */
395 // typedef unsigned char JSAMPLE;
396 // typedef JSAMPLE FAR *JSAMPROW;/* ptr to one image row of pixel samples. */
397 // typedef JSAMPROW *JSAMPARRAY;/* ptr to some rows (a 2-D sample array) */
398 // typedef JSAMPARRAY *JSAMPIMAGE;/* a 3-D sample array: top index is color */
400 int row_stride;/* physical row width in output buffer */
402 #ifdef GDCM_JPG_DEBUG
403 printf("entree dans File::gdcm_read_JPEG_file (i.e. 8), depuis gdcmJpeg\n");
404 #endif //GDCM_JPG_DEBUG
406 /* In this example we want to open the input file before doing anything else,
407 * so that the setjmp() error recovery below can assume the file is open.
408 * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
409 * requires it in order to read binary files.
412 /* Step 1: allocate and initialize JPEG decompression object */
413 #ifdef GDCM_JPG_DEBUG
414 printf("Entree Step 1\n");
415 #endif //GDCM_JPG_DEBUG
417 /* We set up the normal JPEG error routines, then override error_exit. */
419 cinfo.err = jpeg_std_error(&jerr.pub);
420 jerr.pub.error_exit = my_error_exit;
422 /* Establish the setjmp return context for my_error_exit to use. */
423 if (setjmp(jerr.setjmp_buffer))
425 /* If we get here, the JPEG code has signaled an error.
426 * We need to clean up the JPEG object, close the input file, and return.
428 jpeg_destroy_decompress(&cinfo);
431 /* Now we can initialize the JPEG decompression object. */
432 jpeg_create_decompress(&cinfo);
434 /* Step 2: specify data source (eg, a file) */
435 #ifdef GDCM_JPG_DEBUG
436 printf("Entree Step 2\n");
437 #endif //GDCM_JPG_DEBUG
439 jpeg_stdio_src(&cinfo, fp);
441 /* Step 3: read file parameters with jpeg_read_header() */
442 #ifdef GDCM_JPG_DEBUG
443 printf("Entree Step 3\n");
444 #endif //GDCM_JPG_DEBUG
446 (void) jpeg_read_header(&cinfo, TRUE);
448 /* We can ignore the return value from jpeg_read_header since
449 * (a) suspension is not possible with the stdio data source, and
450 * (b) we passed TRUE to reject a tables-only JPEG file as an error.
451 * See libjpeg.doc for more info.
454 // prevent the library from performing any color space conversion
455 if( cinfo.process == JPROC_LOSSLESS )
457 cinfo.jpeg_color_space = JCS_UNKNOWN;
458 cinfo.out_color_space = JCS_UNKNOWN;
462 #ifdef GDCM_JPG_DEBUG
463 printf("--------------Header contents :----------------\n");
464 printf("image_width %d image_height %d\n",
465 cinfo.image_width , cinfo.image_height);
466 printf("bits of precision in image data %d \n",
467 cinfo.output_components);
468 printf("nb of color components returned %d \n",
469 cinfo.data_precision);
470 #endif //GDCM_JPG_DEBUG
474 * JDIMENSION image_width; // input image width
475 * JDIMENSION image_height; // input image height
476 * int output_components; // # of color components returned
477 * J_COLOR_SPACE in_color_space; // colorspace of input image
478 * double input_gamma; // image gamma of input image
479 * int data_precision; // bits of precision in image data
482 /* Step 4: set parameters for decompression */
483 #ifdef GDCM_JPG_DEBUG
484 printf("Entree Step 4\n");
485 #endif //GDCM_JPG_DEBUG
486 /* In this example, we don't need to change any of the defaults set by
487 * jpeg_read_header(), so we do nothing here.
490 /* Step 5: Start decompressor */
491 #ifdef GDCM_JPG_DEBUG
492 printf("Entree Step 5\n");
493 #endif //GDCM_JPG_DEBUG
495 (void) jpeg_start_decompress(&cinfo);
496 /* We can ignore the return value since suspension is not possible
497 * with the stdio data source.
500 /* We may need to do some setup of our own at this point before reading
501 * the data. After jpeg_start_decompress() we have the correct scaled
502 * output image dimensions available, as well as the output colormap
503 * if we asked for color quantization.
504 * In this example, we need to make an output work buffer of the right size.
507 /* JSAMPLEs per row in output buffer */
508 row_stride = cinfo.output_width * cinfo.output_components*2;
510 #ifdef GDCM_JPG_DEBUG
511 printf ("cinfo.output_width %d cinfo.output_components %d row_stride %d\n",
512 cinfo.output_width, cinfo.output_components,row_stride);
513 #endif //GDCM_JPG_DEBUG
515 /* Make a one-row-high sample array that will go away when done with image */
516 buffer = (*cinfo.mem->alloc_sarray)
517 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
519 /* Step 6: while (scan lines remain to be read) */
520 #ifdef GDCM_JPG_DEBUG
521 printf("Entree Step 6\n");
522 #endif //GDCM_JPG_DEBUG
523 /* jpeg_read_scanlines(...); */
525 /* Here we use the library's state variable cinfo.output_scanline as the
526 * loop counter, so that we don't have to keep track ourselves.
528 #ifdef GDCM_JPG_DEBUG
529 printf ("cinfo.output_height %d cinfo.output_width %d\n",
530 cinfo.output_height,cinfo.output_width);
531 #endif //GDCM_JPG_DEBUG
532 pimage=(char *)image_buffer;
534 int bufsize = cinfo.output_width * cinfo.output_components;
535 size_t rowsize = bufsize * sizeof(JSAMPLE);
537 while (cinfo.output_scanline < cinfo.output_height) {
538 /* jpeg_read_scanlines expects an array of pointers to scanlines.
539 * Here the array is only one element long, but you could ask for
540 * more than one scanline at a time if that's more convenient.
543 //printf( "scanlines: %d\n",cinfo.output_scanline);
544 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
545 memcpy( pimage, *buffer,rowsize);
549 /* Step 7: Finish decompression */
550 #ifdef GDCM_JPG_DEBUG
551 printf("Entree Step 7\n");
552 #endif //GDCM_JPG_DEBUG
554 (void) jpeg_finish_decompress(&cinfo);
556 /* We can ignore the return value since suspension is not possible
557 * with the stdio data source.
560 /* Step 8: Release JPEG decompression object */
562 #ifdef GDCM_JPG_DEBUG
563 printf("Entree Step 8\n");
564 #endif //GDCM_JPG_DEBUG
566 /* This is an important step since it will release a good deal of memory. */
568 jpeg_destroy_decompress(&cinfo);
570 /* After finish_decompress, we can close the input file.
571 * Here we postpone it until after no more JPEG errors are possible,
572 * so as to simplify the setjmp error logic above. (Actually, I don't
573 * think that jpeg_destroy can do an error exit, but why assume anything...)
576 /* At this point you may want to check to see whether any corrupt-data
577 * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
580 /* And we're done! */
589 * In the above code, we ignored the return value of jpeg_read_scanlines,
590 * which is the number of scanlines actually read. We could get away with
591 * this because we asked for only one line at a time and we weren't using
592 * a suspending data source. See libjpeg.doc for more info.
594 * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
595 * we should have done it beforehand to ensure that the space would be
596 * counted against the JPEG max_memory setting. In some systems the above
597 * code would risk an out-of-memory error. However, in general we don't
598 * know the output image dimensions before jpeg_start_decompress(), unless we
599 * call jpeg_calc_output_dimensions(). See libjpeg.doc for more about this.
601 * Scanlines are returned in the same order as they appear in the JPEG file,
602 * which is standardly top-to-bottom. If you must emit data bottom-to-top,
603 * you can use one of the virtual arrays provided by the JPEG memory manager
604 * to invert the data. See wrbmp.c for an example.
606 * As with compression, some operating modes may require temporary files.
607 * On some systems you may need to set up a signal handler to ensure that
608 * temporary files are deleted if the program is interrupted. See libjpeg.doc.
611 //----------------------------------------------------------------------------
615 * \brief routine for JPEG decompression from a memory buffer.
616 * routine for JPEG decompression from a memory buffer. This routine
617 * only reads one JPEG image at a time, but returns information about
618 * how many bytes have been consumed from the \c input_buffer, and
619 * how many bytes have been written into the output \c image_buffer.
621 * @param input_buffer pointer to a memory buffer containing the jpeg
623 * @param buflen length of the memory buffer.
624 * @param image_buffer pointer to the location where the decompressed
625 * image will be filled.
626 * @param howManyRead returns how many bytes have been consumed from the
628 * @param howManyWritten returns how many bytes have been written into
629 * the output image_buffer.
630 * @return 1 on success, 0 on error
633 bool gdcm_read_JPEG_memory ( const JOCTET* input_buffer, const size_t buflen,
635 size_t *howManyRead, size_t *howManyWritten)
637 char* pimage=(char *)image_buffer;
638 JOCTET* input = (JOCTET*) input_buffer;
640 /* This struct contains the JPEG decompression parameters and pointers to
641 * working space (which is allocated as needed by the JPEG library).
643 struct jpeg_decompress_struct cinfo;
645 /* -------------- inside, we found :
646 * JDIMENSION image_width; // input image width
647 * JDIMENSION image_height; // input image height
648 * int input_components; // nb of color components in input image
649 * J_COLOR_SPACE in_color_space; // colorspace of input image
650 * double input_gamma; // image gamma of input image
653 /* We use our private extension JPEG error handler.
654 * Note that this struct must live as long as the main JPEG parameter
655 * struct, to avoid dangling-pointer problems.
657 struct my_error_mgr jerr;
660 JSAMPARRAY buffer;/* Output row buffer */
664 // typedef unsigned char JSAMPLE;
665 // typedef JSAMPLE FAR *JSAMPROW;/* ptr to one image row of pixel samples. */
666 // typedef JSAMPROW *JSAMPARRAY;/* ptr to some rows (a 2-D sample array) */
667 // typedef JSAMPARRAY *JSAMPIMAGE;/* a 3-D sample array: top index is color */
669 int row_stride;/* physical row width in output buffer */
671 #ifdef GDCM_JPG_DEBUG
672 printf("entree dans File::gdcm_read_JPEG_file (i.e. 8), depuis gdcmJpeg\n");
673 #endif //GDCM_JPG_DEBUG
675 /* In this example we want to open the input file before doing anything else,
676 * so that the setjmp() error recovery below can assume the file is open.
677 * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
678 * requires it in order to read binary files.
681 /* Step 1: allocate and initialize JPEG decompression object */
682 #ifdef GDCM_JPG_DEBUG
683 printf("Entree Step 1\n");
684 #endif //GDCM_JPG_DEBUG
686 /* We set up the normal JPEG error routines, then override error_exit. */
688 cinfo.err = jpeg_std_error(&jerr.pub);
689 jerr.pub.error_exit = my_error_exit;
691 /* Establish the setjmp return context for my_error_exit to use. */
692 if (setjmp(jerr.setjmp_buffer))
694 /* If we get here, the JPEG code has signaled an error.
695 * We need to clean up the JPEG object, close the input file, and return.
697 jpeg_destroy_decompress(&cinfo);
699 *howManyRead += input - input_buffer;
700 *howManyWritten += pimage - (char *)image_buffer;
704 /* Now we can initialize the JPEG decompression object. */
705 jpeg_create_decompress(&cinfo);
707 /* Step 2: specify data source (eg, a file) */
708 #ifdef GDCM_JPG_DEBUG
709 printf("Entree Step 2\n");
710 #endif //GDCM_JPG_DEBUG
712 jpeg_memory_src(&cinfo, input, buflen);
714 /* Step 3: read file parameters with jpeg_read_header() */
715 #ifdef GDCM_JPG_DEBUG
716 printf("Entree Step 3\n");
717 #endif //GDCM_JPG_DEBUG
719 (void) jpeg_read_header(&cinfo, TRUE);
721 /* We can ignore the return value from jpeg_read_header since
722 * (a) suspension is not possible with the stdio data source, and
723 * (b) we passed TRUE to reject a tables-only JPEG file as an error.
724 * See libjpeg.doc for more info.
727 // prevent the library from performing any color space conversion
728 if( cinfo.process == JPROC_LOSSLESS )
730 cinfo.jpeg_color_space = JCS_UNKNOWN;
731 cinfo.out_color_space = JCS_UNKNOWN;
734 #ifdef GDCM_JPG_DEBUG
735 printf("--------------Header contents :----------------\n");
736 printf("image_width %d image_height %d\n",
737 cinfo.image_width , cinfo.image_height);
738 printf("bits of precision in image data %d \n",
739 cinfo.output_components);
740 printf("nb of color components returned %d \n",
741 cinfo.data_precision);
742 #endif //GDCM_JPG_DEBUG
746 * JDIMENSION image_width; // input image width
747 * JDIMENSION image_height; // input image height
748 * int output_components; // # of color components returned
749 * J_COLOR_SPACE in_color_space; // colorspace of input image
750 * double input_gamma; // image gamma of input image
751 * int data_precision; // bits of precision in image data
754 /* Step 4: set parameters for decompression */
755 #ifdef GDCM_JPG_DEBUG
756 printf("Entree Step 4\n");
757 #endif //GDCM_JPG_DEBUG
758 /* In this example, we don't need to change any of the defaults set by
759 * jpeg_read_header(), so we do nothing here.
762 /* Step 5: Start decompressor */
763 #ifdef GDCM_JPG_DEBUG
764 printf("Entree Step 5\n");
765 #endif //GDCM_JPG_DEBUG
767 (void) jpeg_start_decompress(&cinfo);
768 /* We can ignore the return value since suspension is not possible
769 * with the stdio data source.
772 /* We may need to do some setup of our own at this point before reading
773 * the data. After jpeg_start_decompress() we have the correct scaled
774 * output image dimensions available, as well as the output colormap
775 * if we asked for color quantization.
776 * In this example, we need to make an output work buffer of the right size.
779 /* JSAMPLEs per row in output buffer */
780 row_stride = cinfo.output_width * cinfo.output_components*2;
782 #ifdef GDCM_JPG_DEBUG
783 printf ("cinfo.output_width %d cinfo.output_components %d row_stride %d\n",
784 cinfo.output_width, cinfo.output_components,row_stride);
785 #endif //GDCM_JPG_DEBUG
787 /* Make a one-row-high sample array that will go away when done with image */
788 buffer = (*cinfo.mem->alloc_sarray)
789 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
792 /* Step 6: while (scan lines remain to be read) */
793 #ifdef GDCM_JPG_DEBUG
794 printf("Entree Step 6\n");
795 #endif //GDCM_JPG_DEBUG
796 /* jpeg_read_scanlines(...); */
798 /* Here we use the library's state variable cinfo.output_scanline as the
799 * loop counter, so that we don't have to keep track ourselves.
801 #ifdef GDCM_JPG_DEBUG
802 printf ("cinfo.output_height %d cinfo.output_width %d\n",
803 cinfo.output_height,cinfo.output_width);
804 #endif //GDCM_JPG_DEBUG
806 int bufsize = cinfo.output_width * cinfo.output_components;
807 size_t rowsize = bufsize * sizeof(JSAMPLE);
809 while (cinfo.output_scanline < cinfo.output_height) {
810 /* jpeg_read_scanlines expects an array of pointers to scanlines.
811 * Here the array is only one element long, but you could ask for
812 * more than one scanline at a time if that's more convenient.
815 //printf( "scanlines: %d\n",cinfo.output_scanline);
816 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
817 memcpy( pimage, *buffer,rowsize);
821 /* Step 7: Finish decompression */
822 #ifdef GDCM_JPG_DEBUG
823 printf("Entree Step 7\n");
824 #endif //GDCM_JPG_DEBUG
826 input = (JOCTET *)cinfo.src->next_input_byte;
828 (void) jpeg_finish_decompress(&cinfo);
830 /* We can ignore the return value since suspension is not possible
831 * with the stdio data source.
834 /* Step 8: Release JPEG decompression object */
836 #ifdef GDCM_JPG_DEBUG
837 printf("Entree Step 8\n");
838 #endif //GDCM_JPG_DEBUG
840 /* This is an important step since it will release a good deal of memory. */
842 jpeg_destroy_decompress(&cinfo);
845 /* After finish_decompress, we can close the input file.
846 * Here we postpone it until after no more JPEG errors are possible,
847 * so as to simplify the setjmp error logic above. (Actually, I don't
848 * think that jpeg_destroy can do an error exit, but why assume anything...)
851 /* At this point you may want to check to see whether any corrupt-data
852 * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
855 /* And we're done! */
856 *howManyRead += input - input_buffer;
857 *howManyWritten += pimage - (char *)image_buffer;
862 } // end namespace gdcm