]> Creatis software - gdcm.git/blob - src/gdcmJpeg2000.cxx
COMP: Fix warning about unused var... cant remember what I was trying to do, maybe...
[gdcm.git] / src / gdcmJpeg2000.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmJpeg2000.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/04 15:20:13 $
7   Version:   $Revision: 1.33 $
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    {
58       gdcmErrorMacro( "ERROR -> j2k_to_image: failed to decode image!" );
59       return false;
60    }
61  
62    // Copy buffer
63    for (int compno = 0; compno < img.numcomps; compno++)
64    {
65       j2k_comp_t *comp = &img.comps[compno];
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          uint8_t *data8 = (uint8_t*)raw;
76          for (int i = 0; i < wr * hr; i++) 
77          {
78             int v = img.comps[compno].data[i / wr * w + i % wr];
79             *data8++ = (uint8_t)v;
80          }
81       }
82       else if (comp->prec <= 16)
83       {
84          uint16_t *data16 = (uint16_t*)raw;
85          for (int i = 0; i < wr * hr; i++) 
86          {
87             int v = img.comps[compno].data[i / wr * w + i % wr];
88             *data16++ = (uint16_t)v;
89          }
90       }
91       else
92       {
93          uint32_t *data32 = (uint32_t*)raw;
94          for (int i = 0; i < wr * hr; i++) 
95          {
96             int v = img.comps[compno].data[i / wr * w + i % wr];
97             *data32++ = (uint32_t)v;
98          }
99       }
100       free(img.comps[compno].data);
101    }
102  
103    // Free remaining structures
104    j2k_dec_release();
105    // FIXME
106    delete[] inputdata;
107  
108    return true;
109 }
110
111 #if 0
112 bool gdcm_read_JASPER_file (void* raw, char *inputdata, size_t inputlength)
113 {
114 #if 0
115   std::cerr << "Inputlenght=" << inputlength << std::endl;
116   std::ofstream out("/tmp/jpeg2000.jpc", std::ios::binary);
117   out.write((char*)inputdata,inputlength);
118   out.close();
119 #endif
120   jas_init(); //important...
121   jas_stream_t *jasStream = 
122     jas_stream_memopen((char *)inputdata, inputlength);
123     
124   int fmtid;
125   if ((fmtid = jas_image_getfmt(jasStream)) < 0) 
126     {
127     gdcmErrorMacro("unknown image format");
128     return false;
129     }
130
131   // Decode the image. 
132   jas_image_t *jasImage /* = NULL*/; // Useless assignation
133   if (!(jasImage = jas_image_decode(jasStream, fmtid, 0))) 
134     {
135     gdcmErrorMacro("cannot decode image");
136     return false;
137     }
138
139   // close the stream. 
140   jas_stream_close(jasStream);
141   int numcmpts = jas_image_numcmpts(jasImage);
142   int width = jas_image_cmptwidth(jasImage, 0);
143   int height = jas_image_cmptheight(jasImage, 0);
144   int prec = jas_image_cmptprec(jasImage, 0);
145   int i, j, k;
146
147   // The following should serioulsy be rewritten I cannot believe we need to
148   // do a per pixel decompression, there should be a way to read a full
149   // scanline...
150   if (prec == 8)
151     {
152     uint8_t *data8 = (uint8_t*)raw;
153     for ( i = 0; i < height; i++)
154       for ( j = 0; j < width; j++)
155         for ( k= 0; k < numcmpts; k++)
156           *data8++ = (uint8_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
157     }
158   else if (prec <= 16)
159     {
160     uint16_t *data16 = (uint16_t*)raw;
161     for ( i = 0; i < height; i++) 
162       for ( j = 0; j < width; j++) 
163         for ( k= 0; k < numcmpts; k++)
164           *data16++ = (uint16_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
165     }
166   else if (prec <= 32)
167     {
168     uint32_t *data32 = (uint32_t*)raw;
169     for ( i = 0; i < height; i++) 
170       for ( j = 0; j < width; j++) 
171         for ( k= 0; k < numcmpts; k++)
172           *data32++ = (uint32_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
173     }
174
175   jas_image_destroy(jasImage);
176   jas_image_clearfmts();
177
178   //FIXME
179   //delete the jpeg temp buffer
180 #if 0
181   std::ofstream rawout("/tmp/jpeg2000.raw");
182   rawout.write((char*)raw,height*width*numcmpts*((prec+4)/8));
183   rawout.close();
184 #endif
185   delete[] inputdata;
186
187   return true;
188 }
189 #endif
190
191 //-----------------------------------------------------------------------------
192 } // end namespace gdcm
193