2 //-----------------------------------------------------------------------------
6 #define BITS_IN_JSAMPLE 12
9 #define GDCM_jpr_DEBUG 0
13 * <setjmp.h> is used for the optional error recovery mechanism shown in
14 * the second part of the example.
18 * Include file for users of JPEG library.
19 * You will need to have included system headers that define at least
20 * the typedefs FILE and size_t before you can include jpeglib.h.
21 * (stdio.h is sufficient on ANSI-conforming systems.)
22 * You may also wish to include "jerror.h".
26 #include "src/jpeg/libijg12/jconfig.h"
27 #include "src/jpeg/libijg12/jpeglib.h"
28 //#include "jconfig12.h"
31 /******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/
37 bool gdcm_write_JPEG_file12 (FILE* fp, void* im_buff,
38 int image_width, int image_heigh, int quality)
49 /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
51 /* This half of the example shows how to read data from the JPEG decompressor.
52 * It's a bit more refined than the above, in that we show:
53 * (a) how to modify the JPEG library's standard error-reporting behavior;
54 * (b) how to allocate workspace using the library's memory manager.
56 * Just to make this example a little different from the first one, we'll
57 * assume that we do not intend to put the whole image into an in-memory
58 * buffer, but to send it line-by-line someplace else. We need a one-
59 * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
60 * memory manager allocate it for us. This approach is actually quite useful
61 * because we don't need to remember to deallocate the buffer separately: it
62 * will go away automatically when the JPEG object is cleaned up.
68 * The JPEG library's standard error handler (jerror.c) is divided into
69 * several "methods" which you can override individually. This lets you
70 * adjust the behavior without duplicating a lot of code, which you might
71 * have to update with each future release.
73 * Our example here shows how to override the "error_exit" method so that
74 * control is returned to the library's caller when a fatal error occurs,
75 * rather than calling exit() as the standard error_exit method does.
77 * We use C's setjmp/longjmp facility to return control. This means that the
78 * routine which calls the JPEG library must first execute a setjmp() call to
79 * establish the return point. We want the replacement error_exit to do a
80 * longjmp(). But we need to make the setjmp buffer accessible to the
81 * error_exit routine. To do this, we make a private extension of the
82 * standard JPEG error handler object. (If we were using C++, we'd say we
83 * were making a subclass of the regular error handler.)
85 * Here's the extended error handler struct:
88 //-----------------------------------------------------------------------------
90 struct jpeg_error_mgr pub; /* "public" fields */
91 jmp_buf setjmp_buffer; /* for return to caller */
94 //-----------------------------------------------------------------------------
95 typedef struct my_error_mgr * my_error_ptr;
98 * Here's the routine that will replace the standard error_exit method:
100 METHODDEF(void) my_error_exit (j_common_ptr cinfo) {
101 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
102 my_error_ptr myerr = (my_error_ptr) cinfo->err;
104 /* Always display the message. */
105 /* We could postpone this until after returning, if we chose. */
106 (*cinfo->err->output_message) (cinfo);
108 /* Return control to the setjmp point */
109 longjmp(myerr->setjmp_buffer, 1);
113 //-----------------------------------------------------------------------------
115 * Sample routine for JPEG decompression. We assume that the source file name
116 * is passed in. We want to return 1 on success, 0 on error.
121 * \brief routine for JPEG decompression
122 * @param fp pointer to an already open file descriptor
123 * 12 significant bits per pixel
124 * @param image_buffer to receive uncompressed pixels
125 * @return 1 on success, 0 on error
128 bool gdcmFile::gdcm_read_JPEG_file12 (FILE* fp,void* image_buffer) {
131 /* This struct contains the JPEG decompression parameters and pointers to
132 * working space (which is allocated as needed by the JPEG library).
134 struct jpeg_decompress_struct cinfo;
136 /* -------------- inside, we found :
137 * JDIMENSION image_width; // input image width
138 * JDIMENSION image_height; // input image height
139 * int input_components; // nb of color components in input image
140 * J_COLOR_SPACE in_color_space;// colorspace of input image
141 * double input_gamma; // image gamma of input image
144 /* We use our private extension JPEG error handler.
145 * Note that this struct must live as long as the main JPEG parameter
146 * struct, to avoid dangling-pointer problems.
148 struct my_error_mgr jerr;
151 JSAMPARRAY buffer;/* Output row buffer */
155 // typedef unsigned char JSAMPLE;
156 // typedef JSAMPLE FAR *JSAMPROW;/* ptr to one image row of pixel samples. */
157 // typedef JSAMPROW *JSAMPARRAY;/* ptr to some rows (a 2-D sample array) */
158 // typedef JSAMPARRAY *JSAMPIMAGE;/* a 3-D sample array: top index is color */
160 int row_stride;/* physical row width in output buffer */
161 #ifdef GDCM_JPG_DEBUG
162 printf("entree dans gdcmFile::gdcm_read_JPEG_file12, depuis gdcmJpeg\n");
163 #endif //GDCM_JPG_DEBUG
165 /* In this example we want to open the input file before doing anything else,
166 * so that the setjmp() error recovery below can assume the file is open.
167 * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
168 * requires it in order to read binary files.
171 /* Step 1: allocate and initialize JPEG decompression object */
172 #ifdef GDCM_JPG_DEBUG
173 printf("Entree Step 1\n");
174 #endif //GDCM_JPG_DEBUG
176 /* We set up the normal JPEG error routines, then override error_exit. */
178 cinfo.err = jpeg_std_error(&jerr.pub);
179 jerr.pub.error_exit = my_error_exit;
181 /* Establish the setjmp return context for my_error_exit to use. */
182 if (setjmp(jerr.setjmp_buffer)) {
183 /* If we get here, the JPEG code has signaled an error.
184 * We need to clean up the JPEG object, close the input file, and return.
186 jpeg_destroy_decompress(&cinfo);
190 /* Now we can initialize the JPEG decompression object. */
191 jpeg_create_decompress(&cinfo);
193 /* Step 2: specify data source (eg, a file) */
194 #ifdef GDCM_JPG_DEBUG
195 printf("Entree Step 2\n");
196 #endif //GDCM_JPG_DEBUG
197 jpeg_stdio_src(&cinfo, fp);
199 /* Step 3: read file parameters with jpeg_read_header() */
200 #ifdef GDCM_JPG_DEBUG
201 printf("Entree Step 3\n");
202 #endif //GDCM_JPG_DEBUG
203 (void) jpeg_read_header(&cinfo, TRUE);
205 /* We can ignore the return value from jpeg_read_header since
206 * (a) suspension is not possible with the stdio data source, and
207 * (b) we passed TRUE to reject a tables-only JPEG file as an error.
208 * See libjpeg.doc for more info.
211 #ifdef GDCM_JPG_DEBUG
212 printf("--------------Header contents :----------------\n");
213 printf("image_width %d image_height %d\n",
214 cinfo.image_width , cinfo.image_height);
215 printf("bits of precision in image data %d \n",
216 cinfo.output_components);
217 printf("nb of color components returned %d \n",
218 cinfo.data_precision);
219 #endif //GDCM_JPG_DEBUG
223 * JDIMENSION image_width; // input image width
224 * JDIMENSION image_height; // input image height
225 * int output_components; // # of color components returned
226 * J_COLOR_SPACE in_color_space; // colorspace of input image
227 * double input_gamma; // image gamma of input image
228 * int data_precision; // bits of precision in image data
231 /* Step 4: set parameters for decompression */
232 #ifdef GDCM_JPG_DEBUG
233 printf("Entree Step 4\n");
234 #endif //GDCM_JPG_DEBUG
236 /* In this example, we don't need to change any of the defaults set by
237 * jpeg_read_header(), so we do nothing here.
240 /* Step 5: Start decompressor */
241 #ifdef GDCM_JPG_DEBUG
242 printf("Entree Step 5\n");
243 #endif //GDCM_JPG_DEBUG
245 (void) jpeg_start_decompress(&cinfo);
246 /* We can ignore the return value since suspension is not possible
247 * with the stdio data source.
250 /* We may need to do some setup of our own at this point before reading
251 * the data. After jpeg_start_decompress() we have the correct scaled
252 * output image dimensions available, as well as the output colormap
253 * if we asked for color quantization.
254 * In this example, we need to make an output work buffer of the right size.
257 /* JSAMPLEs per row in output buffer */
258 row_stride = cinfo.output_width * cinfo.output_components;
260 #ifdef GDCM_JPG_DEBUG
261 printf ("cinfo.output_width %d cinfo.output_components %d row_stride %d\n",
262 cinfo.output_width, cinfo.output_components,row_stride);
263 #endif //GDCM_JPG_DEBUG
265 /* Make a one-row-high sample array that will go away when done with image */
266 buffer = (*cinfo.mem->alloc_sarray)
267 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
269 /* Step 6: while (scan lines remain to be read) */
270 #ifdef GDCM_JPG_DEBUG
271 printf("Entree Step 6\n");
272 #endif //GDCM_JPG_DEBUG
274 /* jpeg_read_scanlines(...); */
276 /* Here we use the library's state variable cinfo.output_scanline as the
277 * loop counter, so that we don't have to keep track ourselves.
280 #ifdef GDCM_JPG_DEBUG
281 printf ("cinfo.output_height %d cinfo.output_width %d\n",
282 cinfo.output_height,cinfo.output_width);
283 #endif //GDCM_JPG_DEBUG
285 pimage=(char *)image_buffer;
287 while (cinfo.output_scanline < cinfo.output_height) {
288 /* jpeg_read_scanlines expects an array of pointers to scanlines.
289 * Here the array is only one element long, but you could ask for
290 * more than one scanline at a time if that's more convenient.
293 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
295 if ( BITS_IN_JSAMPLE == 8) {
296 memcpy( pimage, buffer[0],row_stride);
299 memcpy( pimage, buffer[0],row_stride*2 ); // FIXME : *2 car 16 bits?!?
300 pimage+=row_stride*2; // FIXME : *2 car 16 bits?!?
304 /* Step 7: Finish decompression */
305 #ifdef GDCM_JPG_DEBUG
306 printf("Entree Step 7\n");
307 #endif //GDCM_JPG_DEBUG
308 (void) jpeg_finish_decompress(&cinfo);
309 /* We can ignore the return value since suspension is not possible
310 * with the stdio data source.
313 /* Step 8: Release JPEG decompression object */
314 #ifdef GDCM_JPG_DEBUG
315 printf("Entree Step 8\n");
316 #endif //GDCM_JPG_DEBUG
318 /* This is an important step since it will release a good deal of memory. */
319 jpeg_destroy_decompress(&cinfo);
321 /* After finish_decompress, we can close the input file.
322 * Here we postpone it until after no more JPEG errors are possible,
323 * so as to simplify the setjmp error logic above. (Actually, I don't
324 * think that jpeg_destroy can do an error exit, but why assume anything...)
327 /* At this point you may want to check to see whether any corrupt-data
328 * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
331 /* And we're done! */
339 * In the above code, we ignored the return value of jpeg_read_scanlines,
340 * which is the number of scanlines actually read. We could get away with
341 * this because we asked for only one line at a time and we weren't using
342 * a suspending data source. See libjpeg.doc for more info.
344 * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
345 * we should have done it beforehand to ensure that the space would be
346 * counted against the JPEG max_memory setting. In some systems the above
347 * code would risk an out-of-memory error. However, in general we don't
348 * know the output image dimensions before jpeg_start_decompress(), unless we
349 * call jpeg_calc_output_dimensions(). See libjpeg.doc for more about this.
351 * Scanlines are returned in the same order as they appear in the JPEG file,
352 * which is standardly top-to-bottom. If you must emit data bottom-to-top,
353 * you can use one of the virtual arrays provided by the JPEG memory manager
354 * to invert the data. See wrbmp.c for an example.
356 * As with compression, some operating modes may require temporary files.
357 * On some systems you may need to set up a signal handler to ensure that
358 * temporary files are deleted if the program is interrupted. See libjpeg.doc.
361 //-----------------------------------------------------------------------------