]> Creatis software - gdcm.git/blob - src/gdcmJpeg2000.cxx
COMP: Remove call to problematic fprintf
[gdcm.git] / src / gdcmJpeg2000.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmJpeg2000.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/24 21:08:59 $
7   Version:   $Revision: 1.31 $
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 bool gdcm_read_JPEG2000_file (void* raw, char *inputdata, size_t inputlength)
40 {
41   j2k_image_t img;
42   j2k_cp_t cp;
43
44   // default blindly copied
45   cp.layer=0;
46   cp.reduce=0;
47   cp.decod_format=-1;
48   cp.cod_format=-1;
49
50   cp.cod_format=J2K_CFMT;
51   cp.decod_format = PGX_DFMT;
52   int len = inputlength;
53   unsigned char *src = (unsigned char*)inputdata;
54
55   // Decompression
56   if (!j2k_decode(src, len, &img, &cp)) {
57     std::cerr << "ERROR -> j2k_to_image: failed to decode image!\n";
58     return false;
59   }
60
61   // Copy buffer
62   for (int compno = 0; compno < img.numcomps; compno++)
63   {
64     j2k_comp_t *comp = &img.comps[compno];
65     int nbytes = 0;
66
67     int w = img.comps[compno].w;
68     int wr = int_ceildivpow2(img.comps[compno].w, img.comps[compno].factor);
69
70     //int h = img.comps[compno].h;
71     int hr = int_ceildivpow2(img.comps[compno].h, img.comps[compno].factor);
72
73     if (comp->prec <= 8)
74       {
75       nbytes = 1;
76       uint8_t *data8 = (uint8_t*)raw;
77       for (int i = 0; i < wr * hr; i++) 
78         {
79         int v = img.comps[compno].data[i / wr * w + i % wr];
80         *data8++ = (uint8_t)v;
81         }
82       }
83     else if (comp->prec <= 16)
84       {
85       nbytes = 2;
86       uint16_t *data16 = (uint16_t*)raw;
87       for (int i = 0; i < wr * hr; i++) 
88         {
89         int v = img.comps[compno].data[i / wr * w + i % wr];
90         *data16++ = (uint16_t)v;
91         }
92       }
93     else
94       {
95       nbytes = 4;
96       uint32_t *data32 = (uint32_t*)raw;
97       for (int i = 0; i < wr * hr; i++) 
98         {
99         int v = img.comps[compno].data[i / wr * w + i % wr];
100         *data32++ = (uint32_t)v;
101         }
102       }
103     free(img.comps[compno].data);
104   }
105
106
107   // Free remaining structures
108   j2k_dec_release();
109   // FIXME
110   delete[] inputdata;
111
112   return true;
113 }
114
115 #if 0
116 bool gdcm_read_JASPER_file (void* raw, char *inputdata, size_t inputlength)
117 {
118 #if 0
119   std::cerr << "Inputlenght=" << inputlength << std::endl;
120   std::ofstream out("/tmp/jpeg2000.jpc", std::ios::binary);
121   out.write((char*)inputdata,inputlength);
122   out.close();
123 #endif
124   jas_init(); //important...
125   jas_stream_t *jasStream = 
126     jas_stream_memopen((char *)inputdata, inputlength);
127     
128   int fmtid;
129   if ((fmtid = jas_image_getfmt(jasStream)) < 0) 
130     {
131     gdcmErrorMacro("unknown image format");
132     return false;
133     }
134
135   // Decode the image. 
136   jas_image_t *jasImage /* = NULL*/; // Useless assignation
137   if (!(jasImage = jas_image_decode(jasStream, fmtid, 0))) 
138     {
139     gdcmErrorMacro("cannot decode image");
140     return false;
141     }
142
143   // close the stream. 
144   jas_stream_close(jasStream);
145   int numcmpts = jas_image_numcmpts(jasImage);
146   int width = jas_image_cmptwidth(jasImage, 0);
147   int height = jas_image_cmptheight(jasImage, 0);
148   int prec = jas_image_cmptprec(jasImage, 0);
149   int i, j, k;
150
151   // The following should serioulsy be rewritten I cannot believe we need to
152   // do a per pixel decompression, there should be a way to read a full
153   // scanline...
154   if (prec == 8)
155     {
156     uint8_t *data8 = (uint8_t*)raw;
157     for ( i = 0; i < height; i++)
158       for ( j = 0; j < width; j++)
159         for ( k= 0; k < numcmpts; k++)
160           *data8++ = (uint8_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
161     }
162   else if (prec <= 16)
163     {
164     uint16_t *data16 = (uint16_t*)raw;
165     for ( i = 0; i < height; i++) 
166       for ( j = 0; j < width; j++) 
167         for ( k= 0; k < numcmpts; k++)
168           *data16++ = (uint16_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
169     }
170   else if (prec <= 32)
171     {
172     uint32_t *data32 = (uint32_t*)raw;
173     for ( i = 0; i < height; i++) 
174       for ( j = 0; j < width; j++) 
175         for ( k= 0; k < numcmpts; k++)
176           *data32++ = (uint32_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
177     }
178
179   jas_image_destroy(jasImage);
180   jas_image_clearfmts();
181
182   //FIXME
183   //delete the jpeg temp buffer
184 #if 0
185   std::ofstream rawout("/tmp/jpeg2000.raw");
186   rawout.write((char*)raw,height*width*numcmpts*((prec+4)/8));
187   rawout.close();
188 #endif
189   delete[] inputdata;
190
191   return true;
192 }
193 #endif
194
195 //-----------------------------------------------------------------------------
196 } // end namespace gdcm
197