00001 /************************************************************************* 00002 * $Id: gdcmJpeg.c,v 1.1 2005/09/09 08:22:24 bellet Exp $ 00003 ************************************************************************** 00004 This software is governed by the CeCILL license under French law and 00005 abiding by the rules of distribution of free software. You can use, 00006 modify and/ or redistribute the software under the terms of the CeCILL 00007 license as circulated by CEA, CNRS and INRIA at the following URL 00008 "http://www.cecill.info". 00009 00010 As a counterpart to the access to the source code and rights to copy, 00011 modify and redistribute granted by the license, users are provided only 00012 with a limited warranty and the software's author, the holder of the 00013 economic rights, and the successive licensors have only limited 00014 liability. 00015 00016 In this respect, the user's attention is drawn to the risks associated 00017 with loading, using, modifying and/or developing or reproducing the 00018 software by the user in light of its specific status of free software, 00019 that may mean that it is complicated to manipulate, and that also 00020 therefore means that it is reserved for developers and experienced 00021 professionals having in-depth computer knowledge. Users are therefore 00022 encouraged to load and test the software's suitability as regards their 00023 requirements in conditions enabling the security of their systems and/or 00024 data to be ensured and, more generally, to use and operate it in the 00025 same conditions as regards security. 00026 00027 The fact that you are presently reading this means that you have had 00028 knowledge of the CeCILL license and that you accept its terms. 00029 00030 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de 00031 l'Image). All rights reserved. See License.txt for details. 00032 00033 Version 1.0 05/09/2005 00034 *************************************************************************/ 00035 00036 #include <stdio.h> 00037 #include <string.h> 00038 00039 /* 00040 * Include file for users of JPEG library. 00041 * You will need to have included system headers that define at least 00042 * the typedefs FILE and size_t before you can include jpeglib.h. 00043 * (stdio.h is sufficient on ANSI-conforming systems.) 00044 * You may also wish to include "jerror.h". 00045 */ 00046 00047 #include "jpeglib.h" 00048 00049 /* 00050 * <setjmp.h> is used for the optional error recovery mechanism shown in 00051 * the second part of the example. 00052 */ 00053 00054 #include <setjmp.h> 00055 00056 00057 /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/ 00058 00059 /* This half of the example shows how to read data from the JPEG decompressor. 00060 * It's a bit more refined than the above, in that we show: 00061 * (a) how to modify the JPEG library's standard error-reporting behavior; 00062 * (b) how to allocate workspace using the library's memory manager. 00063 * 00064 * Just to make this example a little different from the first one, we'll 00065 * assume that we do not intend to put the whole image into an in-memory 00066 * buffer, but to send it line-by-line someplace else. We need a one- 00067 * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG 00068 * memory manager allocate it for us. This approach is actually quite useful 00069 * because we don't need to remember to deallocate the buffer separately: it 00070 * will go away automatically when the JPEG object is cleaned up. 00071 */ 00072 00073 00074 /* 00075 * ERROR HANDLING: 00076 * 00077 * The JPEG library's standard error handler (jerror.c) is divided into 00078 * several "methods" which you can override individually. This lets you 00079 * adjust the behavior without duplicating a lot of code, which you might 00080 * have to update with each future release. 00081 * 00082 * Our example here shows how to override the "error_exit" method so that 00083 * control is returned to the library's caller when a fatal error occurs, 00084 * rather than calling exit() as the standard error_exit method does. 00085 * 00086 * We use C's setjmp/longjmp facility to return control. This means that the 00087 * routine which calls the JPEG library must first execute a setjmp() call to 00088 * establish the return point. We want the replacement error_exit to do a 00089 * longjmp(). But we need to make the setjmp buffer accessible to the 00090 * error_exit routine. To do this, we make a private extension of the 00091 * standard JPEG error handler object. (If we were using C++, we'd say we 00092 * were making a subclass of the regular error handler.) 00093 * 00094 * Here's the extended error handler struct: 00095 */ 00096 00097 struct my_error_mgr { 00098 struct jpeg_error_mgr pub; /* "public" fields */ 00099 jmp_buf setjmp_buffer; /* for return to caller */ 00100 }; 00101 00102 typedef struct my_error_mgr * my_error_ptr; 00103 00104 /* 00105 * Here's the routine that will replace the standard error_exit method: 00106 */ 00107 00108 METHODDEF(void) 00109 my_error_exit (j_common_ptr cinfo) 00110 { 00111 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */ 00112 my_error_ptr myerr = (my_error_ptr) cinfo->err; 00113 00114 /* Always display the message. */ 00115 /* We could postpone this until after returning, if we chose. */ 00116 (*cinfo->err->output_message) (cinfo); 00117 00118 /* Return control to the setjmp point */ 00119 longjmp(myerr->setjmp_buffer, 1); 00120 } 00121 00122 00123 /* 00124 * Sample routine for JPEG decompression. We assume that the source file name 00125 * is passed in. We want to return 1 on success, 0 on error. 00126 */ 00127 00128 /* 00129 //GLOBAL(int) 00130 int 00131 gdcmFile::gdcm_read_JPEG_file (void * image_buffer) { 00132 */ 00133 00134 int gdcm_read_JPEG_file (void * image_buffer, FILE * fp) { 00135 char *pimage; 00136 00137 /* This struct contains the JPEG decompression parameters and pointers to 00138 * working space (which is allocated as needed by the JPEG library). 00139 */ 00140 struct jpeg_decompress_struct cinfo; 00141 /* We use our private extension JPEG error handler. 00142 * Note that this struct must live as long as the main JPEG parameter 00143 * struct, to avoid dangling-pointer problems. 00144 */ 00145 struct my_error_mgr jerr; 00146 /* More stuff */ 00147 00148 JSAMPARRAY buffer; /* Output row buffer */ 00149 00150 // rappel : 00151 // typedef unsigned char JSAMPLE; 00152 // typedef JSAMPLE FAR *JSAMPROW; /* ptr to one image row of pixel samples. */ 00153 // typedef JSAMPROW *JSAMPARRAY; /* ptr to some rows (a 2-D sample array) */ 00154 // typedef JSAMPARRAY *JSAMPIMAGE; /* a 3-D sample array: top index is color */ 00155 00156 00157 int row_stride; /* physical row width in output buffer */ 00158 00159 /* In this example we want to open the input file before doing anything else, 00160 * so that the setjmp() error recovery below can assume the file is open. 00161 * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that 00162 * requires it in order to read binary files. 00163 */ 00164 00165 00166 printf("entree dans gdcmFile::gdcm_read_JPEG_file, depuis lidido-jpeg\n"); 00167 00168 /* Step 1: allocate and initialize JPEG decompression object */ 00169 00170 printf("Entree Step 1\n"); 00171 00172 /* We set up the normal JPEG error routines, then override error_exit. */ 00173 cinfo.err = jpeg_std_error(&jerr.pub); 00174 jerr.pub.error_exit = my_error_exit; 00175 /* Establish the setjmp return context for my_error_exit to use. */ 00176 if (setjmp(jerr.setjmp_buffer)) { 00177 /* If we get here, the JPEG code has signaled an error. 00178 * We need to clean up the JPEG object, close the input file, and return. 00179 */ 00180 jpeg_destroy_decompress(&cinfo); 00181 00182 return 0; 00183 } 00184 /* Now we can initialize the JPEG decompression object. */ 00185 jpeg_create_decompress(&cinfo); 00186 00187 /* Step 2: specify data source (eg, a file) */ 00188 printf("Entree Step 2\n"); 00189 jpeg_stdio_src(&cinfo, fp); 00190 00191 /* Step 3: read file parameters with jpeg_read_header() */ 00192 00193 printf("Entree Step 3\n"); 00194 00195 (void) jpeg_read_header(&cinfo, TRUE); 00196 /* We can ignore the return value from jpeg_read_header since 00197 * (a) suspension is not possible with the stdio data source, and 00198 * (b) we passed TRUE to reject a tables-only JPEG file as an error. 00199 * See libjpeg.doc for more info. 00200 */ 00201 00202 /* Step 4: set parameters for decompression */ 00203 00204 printf("Entree Step 4\n"); 00205 00206 /* In this example, we don't need to change any of the defaults set by 00207 * jpeg_read_header(), so we do nothing here. 00208 */ 00209 00210 /* Step 5: Start decompressor */ 00211 00212 printf("Entree Step 5\n"); 00213 00214 (void) jpeg_start_decompress(&cinfo); 00215 /* We can ignore the return value since suspension is not possible 00216 * with the stdio data source. 00217 */ 00218 00219 /* We may need to do some setup of our own at this point before reading 00220 * the data. After jpeg_start_decompress() we have the correct scaled 00221 * output image dimensions available, as well as the output colormap 00222 * if we asked for color quantization. 00223 * In this example, we need to make an output work buffer of the right size. 00224 */ 00225 /* JSAMPLEs per row in output buffer */ 00226 row_stride = cinfo.output_width * cinfo.output_components; 00227 /* Make a one-row-high sample array that will go away when done with image */ 00228 buffer = (*cinfo.mem->alloc_sarray) 00229 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); 00230 00231 /* Step 6: while (scan lines remain to be read) */ 00232 /* jpeg_read_scanlines(...); */ 00233 00234 /* Here we use the library's state variable cinfo.output_scanline as the 00235 * loop counter, so that we don't have to keep track ourselves. 00236 */ 00237 00238 printf("Entree Step 6\n"); 00239 00240 printf ("cinfo.output_height %d cinfo.output_width %d\n",cinfo.output_height,cinfo.output_width); 00241 00242 pimage=(char *)image_buffer; 00243 00244 00245 while (cinfo.output_scanline < cinfo.output_height) { 00246 /* jpeg_read_scanlines expects an array of pointers to scanlines. 00247 * Here the array is only one element long, but you could ask for 00248 * more than one scanline at a time if that's more convenient. 00249 */ 00250 00251 // l'image est deja allouée (et passée en param) 00252 // on ecrit directement les pixels 00253 // (on DEVRAIT pouvoir) 00254 00255 //(void) jpeg_read_scanlines(&cinfo, pimage, 1); 00256 00257 (void) jpeg_read_scanlines(&cinfo, buffer, 1); 00258 memcpy( pimage, buffer[0],row_stride*2 ); // FIXME : *2 car 16 bits?!? 00259 //printf("cinfo.output_scanline %d\n",cinfo.output_scanline ); 00260 00261 pimage+=row_stride*2; // FIXME : *2 car 16 bits?!? 00262 } 00263 00264 /* Step 7: Finish decompression */ 00265 00266 printf("Entree Step 7\n"); 00267 00268 (void) jpeg_finish_decompress(&cinfo); 00269 /* We can ignore the return value since suspension is not possible 00270 * with the stdio data source. 00271 */ 00272 00273 /* Step 8: Release JPEG decompression object */ 00274 00275 printf("Entree Step 8\n"); 00276 00277 /* This is an important step since it will release a good deal of memory. */ 00278 jpeg_destroy_decompress(&cinfo); 00279 00280 /* After finish_decompress, we can close the input file. 00281 * Here we postpone it until after no more JPEG errors are possible, 00282 * so as to simplify the setjmp error logic above. (Actually, I don't 00283 * think that jpeg_destroy can do an error exit, but why assume anything...) 00284 */ 00285 00286 /* At this point you may want to check to see whether any corrupt-data 00287 * warnings occurred (test whether jerr.pub.num_warnings is nonzero). 00288 */ 00289 00290 /* And we're done! */ 00291 return 1; 00292 } 00293 00294 00295 /* 00296 * SOME FINE POINTS: 00297 * 00298 * In the above code, we ignored the return value of jpeg_read_scanlines, 00299 * which is the number of scanlines actually read. We could get away with 00300 * this because we asked for only one line at a time and we weren't using 00301 * a suspending data source. See libjpeg.doc for more info. 00302 * 00303 * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress(); 00304 * we should have done it beforehand to ensure that the space would be 00305 * counted against the JPEG max_memory setting. In some systems the above 00306 * code would risk an out-of-memory error. However, in general we don't 00307 * know the output image dimensions before jpeg_start_decompress(), unless we 00308 * call jpeg_calc_output_dimensions(). See libjpeg.doc for more about this. 00309 * 00310 * Scanlines are returned in the same order as they appear in the JPEG file, 00311 * which is standardly top-to-bottom. If you must emit data bottom-to-top, 00312 * you can use one of the virtual arrays provided by the JPEG memory manager 00313 * to invert the data. See wrbmp.c for an example. 00314 * 00315 * As with compression, some operating modes may require temporary files. 00316 * On some systems you may need to set up a signal handler to ensure that 00317 * temporary files are deleted if the program is interrupted. See libjpeg.doc. 00318 */ 00319