00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "gdcmFileHelper.h"
00019 #include "gdcmDebug.h"
00020
00021 #include <iostream>
00022 #include <fstream>
00023
00024 extern "C" {
00025 #include <openjpeg.h>
00026 }
00027
00028 namespace gdcm
00029 {
00030
00039 bool gdcm_read_JPEG2000_file (void* raw, char *inputdata, size_t inputlength)
00040 {
00041 j2k_image_t img;
00042 j2k_cp_t cp;
00043
00044
00045 cp.layer=0;
00046 cp.reduce=0;
00047 cp.decod_format=-1;
00048 cp.cod_format=-1;
00049
00050 cp.cod_format=J2K_CFMT;
00051 cp.decod_format = PGX_DFMT;
00052 int len = inputlength;
00053 unsigned char *src = (unsigned char*)inputdata;
00054
00055
00056 if (!j2k_decode(src, len, &img, &cp))
00057 {
00058 gdcmStaticErrorMacro( "ERROR -> j2k_to_image: failed to decode image!" );
00059 return false;
00060 }
00061
00062
00063 for (int compno = 0; compno < img.numcomps; compno++)
00064 {
00065 j2k_comp_t *comp = &img.comps[compno];
00066
00067 int w = img.comps[compno].w;
00068 int wr = int_ceildivpow2(img.comps[compno].w, img.comps[compno].factor);
00069
00070
00071 int hr = int_ceildivpow2(img.comps[compno].h, img.comps[compno].factor);
00072
00073 if (comp->prec <= 8)
00074 {
00075 uint8_t *data8 = (uint8_t*)raw;
00076 for (int i = 0; i < wr * hr; i++)
00077 {
00078 int v = img.comps[compno].data[i / wr * w + i % wr];
00079 *data8++ = (uint8_t)v;
00080 }
00081 }
00082 else if (comp->prec <= 16)
00083 {
00084 uint16_t *data16 = (uint16_t*)raw;
00085 for (int i = 0; i < wr * hr; i++)
00086 {
00087 int v = img.comps[compno].data[i / wr * w + i % wr];
00088 *data16++ = (uint16_t)v;
00089 }
00090 }
00091 else
00092 {
00093 uint32_t *data32 = (uint32_t*)raw;
00094 for (int i = 0; i < wr * hr; i++)
00095 {
00096 int v = img.comps[compno].data[i / wr * w + i % wr];
00097 *data32++ = (uint32_t)v;
00098 }
00099 }
00100 free(img.comps[compno].data);
00101 }
00102
00103
00104 j2k_dec_release();
00105
00106 delete[] inputdata;
00107
00108 return true;
00109 }
00110
00111 #if 0
00112 bool gdcm_read_JASPER_file (void* raw, char *inputdata, size_t inputlength)
00113 {
00114 #if 0
00115 std::cerr << "Inputlenght=" << inputlength << std::endl;
00116 std::ofstream out("/tmp/jpeg2000.jpc", std::ios::binary);
00117 out.write((char*)inputdata,inputlength);
00118 out.close();
00119 #endif
00120 jas_init();
00121 jas_stream_t *jasStream =
00122 jas_stream_memopen((char *)inputdata, inputlength);
00123
00124 int fmtid;
00125 if ((fmtid = jas_image_getfmt(jasStream)) < 0)
00126 {
00127 gdcmErrorMacro("unknown image format");
00128 return false;
00129 }
00130
00131
00132 jas_image_t *jasImage ;
00133 if (!(jasImage = jas_image_decode(jasStream, fmtid, 0)))
00134 {
00135 gdcmErrorMacro("cannot decode image");
00136 return false;
00137 }
00138
00139
00140 jas_stream_close(jasStream);
00141 int numcmpts = jas_image_numcmpts(jasImage);
00142 int width = jas_image_cmptwidth(jasImage, 0);
00143 int height = jas_image_cmptheight(jasImage, 0);
00144 int prec = jas_image_cmptprec(jasImage, 0);
00145 int i, j, k;
00146
00147
00148
00149
00150 if (prec == 8)
00151 {
00152 uint8_t *data8 = (uint8_t*)raw;
00153 for ( i = 0; i < height; i++)
00154 for ( j = 0; j < width; j++)
00155 for ( k= 0; k < numcmpts; k++)
00156 *data8++ = (uint8_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
00157 }
00158 else if (prec <= 16)
00159 {
00160 uint16_t *data16 = (uint16_t*)raw;
00161 for ( i = 0; i < height; i++)
00162 for ( j = 0; j < width; j++)
00163 for ( k= 0; k < numcmpts; k++)
00164 *data16++ = (uint16_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
00165 }
00166 else if (prec <= 32)
00167 {
00168 uint32_t *data32 = (uint32_t*)raw;
00169 for ( i = 0; i < height; i++)
00170 for ( j = 0; j < width; j++)
00171 for ( k= 0; k < numcmpts; k++)
00172 *data32++ = (uint32_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
00173 }
00174
00175 jas_image_destroy(jasImage);
00176 jas_image_clearfmts();
00177
00178
00179
00180 #if 0
00181 std::ofstream rawout("/tmp/jpeg2000.raw");
00182 rawout.write((char*)raw,height*width*numcmpts*((prec+4)/8));
00183 rawout.close();
00184 #endif
00185 delete[] inputdata;
00186
00187 return true;
00188 }
00189 #endif
00190
00191
00192 }
00193