1 /*=========================================================================
4 Module: $RCSfile: gdcmJpeg12.cxx,v $
6 Date: $Date: 2004/10/12 04:35:46 $
7 Version: $Revision: 1.21 $
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 =========================================================================*/
22 * <setjmp.h> is used for the optional error recovery mechanism shown in
23 * the second part of the example.
27 * Include file for users of JPEG library.
28 * You will need to have included system headers that define at least
29 * the typedefs FILE and size_t before you can include jpeglib.h.
30 * (stdio.h is sufficient on ANSI-conforming systems.)
31 * You may also wish to include "jerror.h".
35 #include "src/jpeg/libijg12/jconfig.h"
36 #include "src/jpeg/libijg12/jpeglib.h"
41 /******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/
47 bool gdcm_write_JPEG_file12 (FILE* fp, void* im_buff,
48 int image_width, int image_heigh, int quality)
59 /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
61 /* This half of the example shows how to read data from the JPEG decompressor.
62 * It's a bit more refined than the above, in that we show:
63 * (a) how to modify the JPEG library's standard error-reporting behavior;
64 * (b) how to allocate workspace using the library's memory manager.
66 * Just to make this example a little different from the first one, we'll
67 * assume that we do not intend to put the whole image into an in-memory
68 * buffer, but to send it line-by-line someplace else. We need a one-
69 * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
70 * memory manager allocate it for us. This approach is actually quite useful
71 * because we don't need to remember to deallocate the buffer separately: it
72 * will go away automatically when the JPEG object is cleaned up.
78 * The JPEG library's standard error handler (jerror.c) is divided into
79 * several "methods" which you can override individually. This lets you
80 * adjust the behavior without duplicating a lot of code, which you might
81 * have to update with each future release.
83 * Our example here shows how to override the "error_exit" method so that
84 * control is returned to the library's caller when a fatal error occurs,
85 * rather than calling exit() as the standard error_exit method does.
87 * We use C's setjmp/longjmp facility to return control. This means that the
88 * routine which calls the JPEG library must first execute a setjmp() call to
89 * establish the return point. We want the replacement error_exit to do a
90 * longjmp(). But we need to make the setjmp buffer accessible to the
91 * error_exit routine. To do this, we make a private extension of the
92 * standard JPEG error handler object. (If we were using C++, we'd say we
93 * were making a subclass of the regular error handler.)
95 * Here's the extended error handler struct:
98 //-----------------------------------------------------------------------------
100 struct jpeg_error_mgr pub; /* "public" fields */
101 jmp_buf setjmp_buffer; /* for return to caller */
104 //-----------------------------------------------------------------------------
105 typedef struct my_error_mgr * my_error_ptr;
108 * Here's the routine that will replace the standard error_exit method:
110 METHODDEF(void) my_error_exit (j_common_ptr cinfo)
112 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
113 my_error_ptr myerr = (my_error_ptr) cinfo->err;
115 /* Always display the message. */
116 /* We could postpone this until after returning, if we chose. */
117 (*cinfo->err->output_message) (cinfo);
119 /* Return control to the setjmp point */
120 longjmp(myerr->setjmp_buffer, 1);
124 //-----------------------------------------------------------------------------
126 * Sample routine for JPEG decompression. We assume that the source file name
127 * is passed in. We want to return 1 on success, 0 on error.
132 * \brief routine for JPEG decompression
133 * @param fp pointer to an already open file descriptor
134 * 12 significant bits per pixel
135 * @param image_buffer to receive uncompressed pixels
136 * @return 1 on success, 0 on error
139 bool gdcm_read_JPEG_file12 ( FILE* fp,void* image_buffer )
143 /* This struct contains the JPEG decompression parameters and pointers to
144 * working space (which is allocated as needed by the JPEG library).
146 struct jpeg_decompress_struct cinfo;
148 /* -------------- inside, we found :
149 * JDIMENSION image_width; // input image width
150 * JDIMENSION image_height; // input image height
151 * int input_components; // nb of color components in input image
152 * J_COLOR_SPACE in_color_space;// colorspace of input image
153 * double input_gamma; // image gamma of input image
156 /* We use our private extension JPEG error handler.
157 * Note that this struct must live as long as the main JPEG parameter
158 * struct, to avoid dangling-pointer problems.
160 struct my_error_mgr jerr;
163 JSAMPARRAY buffer;/* Output row buffer */
167 // typedef unsigned char JSAMPLE;
168 // typedef JSAMPLE FAR *JSAMPROW;/* ptr to one image row of pixel samples. */
169 // typedef JSAMPROW *JSAMPARRAY;/* ptr to some rows (a 2-D sample array) */
170 // typedef JSAMPARRAY *JSAMPIMAGE;/* a 3-D sample array: top index is color */
172 int row_stride;/* physical row width in output buffer */
173 #ifdef GDCM_JPG_DEBUG
174 printf("entree dans File::gdcm_read_JPEG_file12, depuis gdcmJpeg\n");
175 #endif //GDCM_JPG_DEBUG
177 /* In this example we want to open the input file before doing anything else,
178 * so that the setjmp() error recovery below can assume the file is open.
179 * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
180 * requires it in order to read binary files.
183 /* Step 1: allocate and initialize JPEG decompression object */
184 #ifdef GDCM_JPG_DEBUG
185 printf("Entree Step 1\n");
186 #endif //GDCM_JPG_DEBUG
188 /* We set up the normal JPEG error routines, then override error_exit. */
190 cinfo.err = jpeg_std_error(&jerr.pub);
191 jerr.pub.error_exit = my_error_exit;
193 /* Establish the setjmp return context for my_error_exit to use. */
194 if (setjmp(jerr.setjmp_buffer)) {
195 /* If we get here, the JPEG code has signaled an error.
196 * We need to clean up the JPEG object, close the input file, and return.
198 jpeg_destroy_decompress(&cinfo);
202 /* Now we can initialize the JPEG decompression object. */
203 jpeg_create_decompress(&cinfo);
205 /* Step 2: specify data source (eg, a file) */
206 #ifdef GDCM_JPG_DEBUG
207 printf("Entree Step 2\n");
208 #endif //GDCM_JPG_DEBUG
209 jpeg_stdio_src(&cinfo, fp);
211 /* Step 3: read file parameters with jpeg_read_header() */
212 #ifdef GDCM_JPG_DEBUG
213 printf("Entree Step 3\n");
214 #endif //GDCM_JPG_DEBUG
215 (void) jpeg_read_header(&cinfo, TRUE);
217 /* We can ignore the return value from jpeg_read_header since
218 * (a) suspension is not possible with the stdio data source, and
219 * (b) we passed TRUE to reject a tables-only JPEG file as an error.
220 * See libjpeg.doc for more info.
223 #ifdef GDCM_JPG_DEBUG
224 printf("--------------Header contents :----------------\n");
225 printf("image_width %d image_height %d\n",
226 cinfo.image_width , cinfo.image_height);
227 printf("bits of precision in image data %d \n",
228 cinfo.output_components);
229 printf("nb of color components returned %d \n",
230 cinfo.data_precision);
231 #endif //GDCM_JPG_DEBUG
235 * JDIMENSION image_width; // input image width
236 * JDIMENSION image_height; // input image height
237 * int output_components; // # of color components returned
238 * J_COLOR_SPACE in_color_space; // colorspace of input image
239 * double input_gamma; // image gamma of input image
240 * int data_precision; // bits of precision in image data
243 /* Step 4: set parameters for decompression */
244 #ifdef GDCM_JPG_DEBUG
245 printf("Entree Step 4\n");
246 #endif //GDCM_JPG_DEBUG
248 /* In this example, we don't need to change any of the defaults set by
249 * jpeg_read_header(), so we do nothing here.
252 /* Step 5: Start decompressor */
253 #ifdef GDCM_JPG_DEBUG
254 printf("Entree Step 5\n");
255 #endif //GDCM_JPG_DEBUG
257 (void) jpeg_start_decompress(&cinfo);
258 /* We can ignore the return value since suspension is not possible
259 * with the stdio data source.
262 /* We may need to do some setup of our own at this point before reading
263 * the data. After jpeg_start_decompress() we have the correct scaled
264 * output image dimensions available, as well as the output colormap
265 * if we asked for color quantization.
266 * In this example, we need to make an output work buffer of the right size.
269 /* JSAMPLEs per row in output buffer */
270 row_stride = cinfo.output_width * cinfo.output_components;
272 #ifdef GDCM_JPG_DEBUG
273 printf ("cinfo.output_width %d cinfo.output_components %d row_stride %d\n",
274 cinfo.output_width, cinfo.output_components,row_stride);
275 #endif //GDCM_JPG_DEBUG
277 /* Make a one-row-high sample array that will go away when done with image */
278 buffer = (*cinfo.mem->alloc_sarray)
279 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
281 /* Step 6: while (scan lines remain to be read) */
282 #ifdef GDCM_JPG_DEBUG
283 printf("Entree Step 6\n");
284 #endif //GDCM_JPG_DEBUG
286 /* jpeg_read_scanlines(...); */
288 /* Here we use the library's state variable cinfo.output_scanline as the
289 * loop counter, so that we don't have to keep track ourselves.
292 #ifdef GDCM_JPG_DEBUG
293 printf ("cinfo.output_height %d cinfo.output_width %d\n",
294 cinfo.output_height,cinfo.output_width);
295 #endif //GDCM_JPG_DEBUG
297 pimage=(char *)image_buffer;
299 while (cinfo.output_scanline < cinfo.output_height) {
300 /* jpeg_read_scanlines expects an array of pointers to scanlines.
301 * Here the array is only one element long, but you could ask for
302 * more than one scanline at a time if that's more convenient.
305 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
307 if ( BITS_IN_JSAMPLE == 8) {
308 memcpy( pimage, buffer[0],row_stride);
311 memcpy( pimage, buffer[0],row_stride*2 ); // FIXME : *2 car 16 bits?!?
312 pimage+=row_stride*2; // FIXME : *2 car 16 bits?!?
316 /* Step 7: Finish decompression */
317 #ifdef GDCM_JPG_DEBUG
318 printf("Entree Step 7\n");
319 #endif //GDCM_JPG_DEBUG
320 (void) jpeg_finish_decompress(&cinfo);
321 /* We can ignore the return value since suspension is not possible
322 * with the stdio data source.
325 /* Step 8: Release JPEG decompression object */
326 #ifdef GDCM_JPG_DEBUG
327 printf("Entree Step 8\n");
328 #endif //GDCM_JPG_DEBUG
330 /* This is an important step since it will release a good deal of memory. */
331 jpeg_destroy_decompress(&cinfo);
333 /* After finish_decompress, we can close the input file.
334 * Here we postpone it until after no more JPEG errors are possible,
335 * so as to simplify the setjmp error logic above. (Actually, I don't
336 * think that jpeg_destroy can do an error exit, but why assume anything...)
339 /* At this point you may want to check to see whether any corrupt-data
340 * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
343 /* And we're done! */
351 * In the above code, we ignored the return value of jpeg_read_scanlines,
352 * which is the number of scanlines actually read. We could get away with
353 * this because we asked for only one line at a time and we weren't using
354 * a suspending data source. See libjpeg.doc for more info.
356 * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
357 * we should have done it beforehand to ensure that the space would be
358 * counted against the JPEG max_memory setting. In some systems the above
359 * code would risk an out-of-memory error. However, in general we don't
360 * know the output image dimensions before jpeg_start_decompress(), unless we
361 * call jpeg_calc_output_dimensions(). See libjpeg.doc for more about this.
363 * Scanlines are returned in the same order as they appear in the JPEG file,
364 * which is standardly top-to-bottom. If you must emit data bottom-to-top,
365 * you can use one of the virtual arrays provided by the JPEG memory manager
366 * to invert the data. See wrbmp.c for an example.
368 * As with compression, some operating modes may require temporary files.
369 * On some systems you may need to set up a signal handler to ensure that
370 * temporary files are deleted if the program is interrupted. See libjpeg.doc.
373 //-----------------------------------------------------------------------------
375 } // end namespace gdcm