+ // Copy buffer
+ for (int compno = 0; compno < img.numcomps; compno++)
+ {
+ j2k_comp_t *comp = &img.comps[compno];
+ int nbytes = 0;
+
+ int w = img.comps[compno].w;
+ int wr = int_ceildivpow2(img.comps[compno].w, img.comps[compno].factor);
+
+ //int h = img.comps[compno].h;
+ int hr = int_ceildivpow2(img.comps[compno].h, img.comps[compno].factor);
+
+ if (comp->prec <= 8)
+ {
+ nbytes = 1;
+ uint8_t *data8 = (uint8_t*)raw;
+ for (int i = 0; i < wr * hr; i++)
+ {
+ int v = img.comps[compno].data[i / wr * w + i % wr];
+ *data8++ = (uint8_t)v;
+ }
+ }
+ else if (comp->prec <= 16)
+ {
+ nbytes = 2;
+ uint16_t *data16 = (uint16_t*)raw;
+ for (int i = 0; i < wr * hr; i++)
+ {
+ int v = img.comps[compno].data[i / wr * w + i % wr];
+ *data16++ = (uint16_t)v;
+ }
+ }
+ else
+ {
+ nbytes = 4;
+ uint32_t *data32 = (uint32_t*)raw;
+ for (int i = 0; i < wr * hr; i++)
+ {
+ int v = img.comps[compno].data[i / wr * w + i % wr];
+ *data32++ = (uint32_t)v;
+ }
+ }
+ free(img.comps[compno].data);
+ }
+
+ // Free remaining structures
+ j2k_dec_release();
+ // FIXME
+ delete[] inputdata;
+
+ return true;
+}
+
+#if 0
+bool gdcm_read_JASPER_file (void* raw, char *inputdata, size_t inputlength)
+{
+#if 0
+ std::cerr << "Inputlenght=" << inputlength << std::endl;
+ std::ofstream out("/tmp/jpeg2000.jpc", std::ios::binary);
+ out.write((char*)inputdata,inputlength);
+ out.close();
+#endif
+ jas_init(); //important...
+ jas_stream_t *jasStream =
+ jas_stream_memopen((char *)inputdata, inputlength);
+
+ int fmtid;
+ if ((fmtid = jas_image_getfmt(jasStream)) < 0)
+ {
+ gdcmErrorMacro("unknown image format");
+ return false;
+ }
+
+ // Decode the image.
+ jas_image_t *jasImage /* = NULL*/; // Useless assignation
+ if (!(jasImage = jas_image_decode(jasStream, fmtid, 0)))
+ {
+ gdcmErrorMacro("cannot decode image");
+ return false;
+ }
+
+ // close the stream.
+ jas_stream_close(jasStream);
+ int numcmpts = jas_image_numcmpts(jasImage);
+ int width = jas_image_cmptwidth(jasImage, 0);
+ int height = jas_image_cmptheight(jasImage, 0);
+ int prec = jas_image_cmptprec(jasImage, 0);
+ int i, j, k;