]> Creatis software - gdcm.git/blob - src/gdcmJpeg2000.cxx
COMP: Make bcc55 happy
[gdcm.git] / src / gdcmJpeg2000.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmJpeg2000.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/01/24 20:48:10 $
7   Version:   $Revision: 1.38 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmFileHelper.h"
19 #include "gdcmDebug.h"
20
21 #include <iostream>
22 #include <fstream>
23
24 extern "C" {
25   #include <openjpeg.h>
26 }
27
28 namespace gdcm 
29 {
30 //-----------------------------------------------------------------------------
31  /**
32  * \brief   routine for JPEG decompression 
33  * @param raw raw
34  * @param inputdata inputdata
35  * @param inputlength inputlength 
36  * @return 1 on success, 0 on error
37  */
38
39 /**
40 sample error callback expecting a FILE* client object
41 */
42 void error_callback(const char *msg, void *) {
43   std::cerr << "Error in gdcmopenjpeg" << msg << std::endl;
44 }
45 /**
46 sample warning callback expecting a FILE* client object
47 */
48 void warning_callback(const char *msg, void *) {
49   std::cerr << "Warning in gdcmopenjpeg" << msg << std::endl;
50 }
51 /**
52 sample debug callback expecting no client object
53 */
54 void info_callback(const char *msg, void *) {
55   std::cerr << "Info in gdcmopenjpeg" << msg << std::endl;
56 }
57
58 #define J2K_CFMT 0
59 #define JP2_CFMT 1
60 #define JPT_CFMT 2
61 #define MJ2_CFMT 3
62 #define PXM_DFMT 0
63 #define PGX_DFMT 1
64 #define BMP_DFMT 2
65 #define YUV_DFMT 3
66 /*
67  * Divide an integer by a power of 2 and round upwards.
68  *
69  * a divided by 2^b
70  */
71 static int int_ceildivpow2(int a, int b) {
72   return (a + (1 << b) - 1) >> b;
73 }
74
75 /*
76  * The following function was copy paste from j2k_to_image.c with part from convert.c
77  */
78 bool gdcm_read_JPEG2000_file (void* raw, char *inputdata, size_t inputlength)
79 {
80   opj_dparameters_t parameters;  /* decompression parameters */
81   opj_event_mgr_t event_mgr;    /* event manager */
82   opj_image_t *image = NULL;
83   opj_dinfo_t* dinfo = NULL;  /* handle to a decompressor */
84   opj_cio_t *cio = NULL;
85   unsigned char *src = (unsigned char*)inputdata; 
86   int file_length = inputlength;
87
88   /* configure the event callbacks (not required) */
89   memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
90   event_mgr.error_handler = error_callback;
91   event_mgr.warning_handler = warning_callback;
92   event_mgr.info_handler = info_callback;
93
94   /* set decoding parameters to default values */
95   opj_set_default_decoder_parameters(&parameters);
96  
97    // default blindly copied
98    parameters.cp_layer=0;
99    parameters.cp_reduce=0;
100 //   parameters.decod_format=-1;
101 //   parameters.cod_format=-1;
102
103       /* JPEG-2000 codestream */
104     parameters.decod_format = J2K_CFMT;
105     assert(parameters.decod_format == J2K_CFMT);
106   parameters.cod_format = PGX_DFMT;
107   assert(parameters.cod_format == PGX_DFMT);
108
109       /* get a decoder handle */
110       dinfo = opj_create_decompress(CODEC_J2K);
111       
112       /* catch events using our callbacks and give a local context */
113       opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, NULL);      
114
115       /* setup the decoder decoding parameters using user parameters */
116       opj_setup_decoder(dinfo, &parameters);
117
118       /* open a byte stream */
119       cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
120
121       /* decode the stream and fill the image structure */
122       image = opj_decode(dinfo, cio);
123       if(!image) {
124         opj_destroy_decompress(dinfo);
125         opj_cio_close(cio);
126         return 1;
127       }
128       
129       /* close the byte stream */
130       opj_cio_close(cio);
131
132   /* free the memory containing the code-stream */
133   delete[] src;  //FIXME
134   src = NULL;
135
136
137    // Copy buffer
138    for (int compno = 0; compno < image->numcomps; compno++)
139    {
140       opj_image_comp_t *comp = &image->comps[compno];
141   
142       int w = image->comps[compno].w;
143       int wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
144   
145       //int h = image.comps[compno].h;
146       int hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
147   
148       if (comp->prec <= 8)
149       {
150          uint8_t *data8 = (uint8_t*)raw;
151          for (int i = 0; i < wr * hr; i++) 
152          {
153             int v = image->comps[compno].data[i / wr * w + i % wr];
154             *data8++ = (uint8_t)v;
155          }
156       }
157       else if (comp->prec <= 16)
158       {
159          uint16_t *data16 = (uint16_t*)raw;
160          for (int i = 0; i < wr * hr; i++) 
161          {
162             int v = image->comps[compno].data[i / wr * w + i % wr];
163             *data16++ = (uint16_t)v;
164          }
165       }
166       else
167       {
168          uint32_t *data32 = (uint32_t*)raw;
169          for (int i = 0; i < wr * hr; i++) 
170          {
171             int v = image->comps[compno].data[i / wr * w + i % wr];
172             *data32++ = (uint32_t)v;
173          }
174       }
175       //free(image.comps[compno].data);
176    }
177  
178
179   /* free remaining structures */
180   if(dinfo) {
181     opj_destroy_decompress(dinfo);
182   }
183
184   /* free image data structure */
185   opj_image_destroy(image);
186
187   return true;
188 }
189
190 #if 0
191 // For openjpeg 0.97
192 bool gdcm_read_JPEG2000_file (void* raw, char *inputdata, size_t inputlength)
193 {
194    j2k_image_t img;
195    j2k_cp_t cp;
196  
197    // default blindly copied
198    cp.layer=0;
199    cp.reduce=0;
200    cp.decod_format=-1;
201    cp.cod_format=-1;
202  
203    cp.cod_format=J2K_CFMT;
204    cp.decod_format = PGX_DFMT;
205    int len = inputlength;
206    unsigned char *src = (unsigned char*)inputdata;
207  
208    // Decompression
209    if (!j2k_decode(src, len, &img, &cp))
210    {
211       gdcmStaticErrorMacro( "ERROR -> j2k_to_image: failed to decode image!" );
212       return false;
213    }
214  
215    // Copy buffer
216    for (int compno = 0; compno < img.numcomps; compno++)
217    {
218       j2k_comp_t *comp = &img.comps[compno];
219   
220       int w = img.comps[compno].w;
221       int wr = int_ceildivpow2(img.comps[compno].w, img.comps[compno].factor);
222   
223       //int h = img.comps[compno].h;
224       int hr = int_ceildivpow2(img.comps[compno].h, img.comps[compno].factor);
225   
226       if (comp->prec <= 8)
227       {
228          uint8_t *data8 = (uint8_t*)raw;
229          for (int i = 0; i < wr * hr; i++) 
230          {
231             int v = img.comps[compno].data[i / wr * w + i % wr];
232             *data8++ = (uint8_t)v;
233          }
234       }
235       else if (comp->prec <= 16)
236       {
237          uint16_t *data16 = (uint16_t*)raw;
238          for (int i = 0; i < wr * hr; i++) 
239          {
240             int v = img.comps[compno].data[i / wr * w + i % wr];
241             *data16++ = (uint16_t)v;
242          }
243       }
244       else
245       {
246          uint32_t *data32 = (uint32_t*)raw;
247          for (int i = 0; i < wr * hr; i++) 
248          {
249             int v = img.comps[compno].data[i / wr * w + i % wr];
250             *data32++ = (uint32_t)v;
251          }
252       }
253       free(img.comps[compno].data);
254    }
255  
256    // Free remaining structures
257    j2k_dec_release();
258    // FIXME
259    delete[] inputdata;
260  
261    return true;
262 }
263 #endif
264
265 #if 0
266 bool gdcm_read_JASPER_file (void* raw, char *inputdata, size_t inputlength)
267 {
268 #if 0
269   std::cerr << "Inputlenght=" << inputlength << std::endl;
270   std::ofstream out("/tmp/jpeg2000.jpc", std::ios::binary);
271   out.write((char*)inputdata,inputlength);
272   out.close();
273 #endif
274   jas_init(); //important...
275   jas_stream_t *jasStream = 
276     jas_stream_memopen((char *)inputdata, inputlength);
277     
278   int fmtid;
279   if ((fmtid = jas_image_getfmt(jasStream)) < 0) 
280     {
281     gdcmErrorMacro("unknown image format");
282     return false;
283     }
284
285   // Decode the image. 
286   jas_image_t *jasImage /* = NULL*/; // Useless assignation
287   if (!(jasImage = jas_image_decode(jasStream, fmtid, 0))) 
288     {
289     gdcmErrorMacro("cannot decode image");
290     return false;
291     }
292
293   // close the stream. 
294   jas_stream_close(jasStream);
295   int numcmpts = jas_image_numcmpts(jasImage);
296   int width = jas_image_cmptwidth(jasImage, 0);
297   int height = jas_image_cmptheight(jasImage, 0);
298   int prec = jas_image_cmptprec(jasImage, 0);
299   int i, j, k;
300
301   // The following should serioulsy be rewritten I cannot believe we need to
302   // do a per pixel decompression, there should be a way to read a full
303   // scanline...
304   if (prec == 8)
305     {
306     uint8_t *data8 = (uint8_t*)raw;
307     for ( i = 0; i < height; i++)
308       for ( j = 0; j < width; j++)
309         for ( k= 0; k < numcmpts; k++)
310           *data8++ = (uint8_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
311     }
312   else if (prec <= 16)
313     {
314     uint16_t *data16 = (uint16_t*)raw;
315     for ( i = 0; i < height; i++) 
316       for ( j = 0; j < width; j++) 
317         for ( k= 0; k < numcmpts; k++)
318           *data16++ = (uint16_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
319     }
320   else if (prec <= 32)
321     {
322     uint32_t *data32 = (uint32_t*)raw;
323     for ( i = 0; i < height; i++) 
324       for ( j = 0; j < width; j++) 
325         for ( k= 0; k < numcmpts; k++)
326           *data32++ = (uint32_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
327     }
328
329   jas_image_destroy(jasImage);
330   jas_image_clearfmts();
331
332   //FIXME
333   //delete the jpeg temp buffer
334 #if 0
335   std::ofstream rawout("/tmp/jpeg2000.raw");
336   rawout.write((char*)raw,height*width*numcmpts*((prec+4)/8));
337   rawout.close();
338 #endif
339   delete[] inputdata;
340
341   return true;
342 }
343 #endif
344
345 //-----------------------------------------------------------------------------
346 } // end namespace gdcm
347