1 /*=========================================================================
4 Module: $RCSfile: gdcmJpeg2000.cxx,v $
6 Date: $Date: 2005/11/28 15:20:33 $
7 Version: $Revision: 1.34 $
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 gdcmStaticErrorMacro( "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];
67 int w = img.comps[compno].w;
68 int wr = int_ceildivpow2(img.comps[compno].w, img.comps[compno].factor);
70 //int h = img.comps[compno].h;
71 int hr = int_ceildivpow2(img.comps[compno].h, img.comps[compno].factor);
75 uint8_t *data8 = (uint8_t*)raw;
76 for (int i = 0; i < wr * hr; i++)
78 int v = img.comps[compno].data[i / wr * w + i % wr];
79 *data8++ = (uint8_t)v;
82 else if (comp->prec <= 16)
84 uint16_t *data16 = (uint16_t*)raw;
85 for (int i = 0; i < wr * hr; i++)
87 int v = img.comps[compno].data[i / wr * w + i % wr];
88 *data16++ = (uint16_t)v;
93 uint32_t *data32 = (uint32_t*)raw;
94 for (int i = 0; i < wr * hr; i++)
96 int v = img.comps[compno].data[i / wr * w + i % wr];
97 *data32++ = (uint32_t)v;
100 free(img.comps[compno].data);
103 // Free remaining structures
112 bool gdcm_read_JASPER_file (void* raw, char *inputdata, size_t inputlength)
115 std::cerr << "Inputlenght=" << inputlength << std::endl;
116 std::ofstream out("/tmp/jpeg2000.jpc", std::ios::binary);
117 out.write((char*)inputdata,inputlength);
120 jas_init(); //important...
121 jas_stream_t *jasStream =
122 jas_stream_memopen((char *)inputdata, inputlength);
125 if ((fmtid = jas_image_getfmt(jasStream)) < 0)
127 gdcmErrorMacro("unknown image format");
132 jas_image_t *jasImage /* = NULL*/; // Useless assignation
133 if (!(jasImage = jas_image_decode(jasStream, fmtid, 0)))
135 gdcmErrorMacro("cannot decode image");
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);
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
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 ));
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 ));
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 ));
175 jas_image_destroy(jasImage);
176 jas_image_clearfmts();
179 //delete the jpeg temp buffer
181 std::ofstream rawout("/tmp/jpeg2000.raw");
182 rawout.write((char*)raw,height*width*numcmpts*((prec+4)/8));
191 //-----------------------------------------------------------------------------
192 } // end namespace gdcm