1 /*=========================================================================
4 Module: $RCSfile: gdcmJpeg2000.cxx,v $
6 Date: $Date: 2005/10/24 21:21:09 $
7 Version: $Revision: 1.32 $
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.
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.
17 =========================================================================*/
18 #include "gdcmFileHelper.h"
19 #include "gdcmDebug.h"
30 //-----------------------------------------------------------------------------
32 * \brief routine for JPEG decompression
34 * @param inputdata inputdata
35 * @param inputlength inputlength
36 * @return 1 on success, 0 on error
39 bool gdcm_read_JPEG2000_file (void* raw, char *inputdata, size_t inputlength)
44 // default blindly copied
50 cp.cod_format=J2K_CFMT;
51 cp.decod_format = PGX_DFMT;
52 int len = inputlength;
53 unsigned char *src = (unsigned char*)inputdata;
56 if (!j2k_decode(src, len, &img, &cp))
58 gdcmErrorMacro( "ERROR -> j2k_to_image: failed to decode image!" );
63 for (int compno = 0; compno < img.numcomps; compno++)
65 j2k_comp_t *comp = &img.comps[compno];
68 int w = img.comps[compno].w;
69 int wr = int_ceildivpow2(img.comps[compno].w, img.comps[compno].factor);
71 //int h = img.comps[compno].h;
72 int hr = int_ceildivpow2(img.comps[compno].h, img.comps[compno].factor);
77 uint8_t *data8 = (uint8_t*)raw;
78 for (int i = 0; i < wr * hr; i++)
80 int v = img.comps[compno].data[i / wr * w + i % wr];
81 *data8++ = (uint8_t)v;
84 else if (comp->prec <= 16)
87 uint16_t *data16 = (uint16_t*)raw;
88 for (int i = 0; i < wr * hr; i++)
90 int v = img.comps[compno].data[i / wr * w + i % wr];
91 *data16++ = (uint16_t)v;
97 uint32_t *data32 = (uint32_t*)raw;
98 for (int i = 0; i < wr * hr; i++)
100 int v = img.comps[compno].data[i / wr * w + i % wr];
101 *data32++ = (uint32_t)v;
104 free(img.comps[compno].data);
107 // Free remaining structures
116 bool gdcm_read_JASPER_file (void* raw, char *inputdata, size_t inputlength)
119 std::cerr << "Inputlenght=" << inputlength << std::endl;
120 std::ofstream out("/tmp/jpeg2000.jpc", std::ios::binary);
121 out.write((char*)inputdata,inputlength);
124 jas_init(); //important...
125 jas_stream_t *jasStream =
126 jas_stream_memopen((char *)inputdata, inputlength);
129 if ((fmtid = jas_image_getfmt(jasStream)) < 0)
131 gdcmErrorMacro("unknown image format");
136 jas_image_t *jasImage /* = NULL*/; // Useless assignation
137 if (!(jasImage = jas_image_decode(jasStream, fmtid, 0)))
139 gdcmErrorMacro("cannot decode image");
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);
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
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 ));
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 ));
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 ));
179 jas_image_destroy(jasImage);
180 jas_image_clearfmts();
183 //delete the jpeg temp buffer
185 std::ofstream rawout("/tmp/jpeg2000.raw");
186 rawout.write((char*)raw,height*width*numcmpts*((prec+4)/8));
195 //-----------------------------------------------------------------------------
196 } // end namespace gdcm