+ return PixelData;
+}
+
+/**
+ * \ingroup gdcmFile
+ * \brief Copies at most MaxSize bytes of pixel data to caller's
+ * memory space.
+ * \warning This function was designed to avoid people that want to build
+ * a volume from an image stack to need first to get the image pixels
+ * and then move them to the volume area.
+ * It's absolutely useless for any VTK user since vtk chooses
+ * to invert the lines of an image, that is the last line comes first
+ * (for some axis related reasons?). Hence he will have
+ * to load the image line by line, starting from the end.
+ * VTK users hace to call GetImageData
+ * \warning DOES NOT transform the Grey Plane + Palette Color (if any)
+ * into a single RGB Pixels Plane
+ * the (VTK) user will manage the palettes
+ *
+ * @param destination Address (in caller's memory space) at which the
+ * pixel data should be copied
+ * @param maxSize Maximum number of bytes to be copied. When MaxSize
+ * is not sufficient to hold the pixel data the copy is not
+ * executed (i.e. no partial copy).
+ * @return On success, the number of bytes actually copied. Zero on
+ * failure e.g. MaxSize is lower than necessary.
+ */
+size_t gdcmFile::GetImageDataIntoVectorRaw (void *destination, size_t maxSize)
+{
+ int nb, nbu, highBit, sign;
+ PixelRead = 1 ; // PixelRaw
+
+ if ( ImageDataSize > maxSize )
+ {
+ dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
+ "than caller's expected MaxSize");
+ return (size_t)0;
+ }
+
+ ReadPixelData( destination );
+
+ // Number of Bits Allocated for storing a Pixel
+ std::string str_nb = Header->GetEntryByNumber(0x0028,0x0100);
+ if ( str_nb == GDCM_UNFOUND )
+ {
+ nb = 16;
+ }
+ else
+ {
+ nb = atoi( str_nb.c_str() );
+ // FIXME
+ // From reading SetPixelDataSizeFromHeader, it seems 12 should be treated
+ // separately, correct ?
+ }
+
+ // Number of Bits actually used
+ std::string str_nbu = Header->GetEntryByNumber(0x0028,0x0101);
+ if ( str_nbu == GDCM_UNFOUND )
+ {
+ nbu = nb;
+ }
+ else
+ {
+ nbu = atoi( str_nbu.c_str() );
+ }
+
+ // High Bit Position
+ std::string str_highBit = Header->GetEntryByNumber(0x0028,0x0102);
+ if ( str_highBit == GDCM_UNFOUND )
+ {
+ highBit = nb - 1;
+ }
+ else
+ {
+ highBit = atoi( str_highBit.c_str() );
+ }
+
+ // Pixel sign
+ // 0 = Unsigned
+ // 1 = Signed
+ std::string str_sign = Header->GetEntryByNumber(0x0028,0x0103);
+ if ( str_sign == GDCM_UNFOUND )
+ {
+ sign = 0; // default is unsigned
+ }
+ else
+ {
+ sign = atoi( str_sign.c_str() );
+ }
+
+ // re arange bytes inside the integer (processor endianity)
+ if ( nb != 8 )
+ {
+ SwapZone(destination, Header->GetSwapCode(), ImageDataSize, nb);
+ }
+
+ // to avoid pb with some xmedcon breakers images
+ if ( nb == 16 && nbu < nb && sign == 0)
+ {
+ int l = (int)(ImageDataSize / (nb/8));
+ uint16_t *deb = (uint16_t *)destination;
+ for(int i = 0; i<l; i++)
+ {
+ if( *deb == 0xffff )
+ {
+ *deb = 0;
+ }
+ deb++;
+ }
+ }
+
+ // re arange bits inside the bytes
+ if ( nbu != nb )
+ {
+ int l = (int)(ImageDataSize / (nb/8));
+ if ( nb == 16 )
+ {
+ uint16_t mask = 0xffff;
+ mask = mask >> (nb-nbu);
+ uint16_t *deb = (uint16_t *)destination;
+ for(int i = 0; i<l; i++)
+ {
+ *deb = (*deb >> (nbu - highBit - 1)) & mask;
+ deb++;
+ }
+ }
+ else if ( nb == 32 )
+ {
+ uint32_t mask = 0xffffffff;
+ mask = mask >> (nb - nbu);
+ uint32_t *deb = (uint32_t *)destination;
+ for(int i = 0; i<l; i++)
+ {
+ *deb = (*deb >> (nbu - highBit - 1)) & mask;
+ deb++;
+ }
+ }
+ else
+ {
+ dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: weird image");
+ return 0;
+ }
+ }
+// DO NOT remove this code commented out.
+// Nobody knows what's expecting you ...
+// Just to 'see' what was actually read on disk :-(
+
+// FILE * f2;
+// f2 = fopen("SpuriousFile.RAW","wb");
+// fwrite(destination,ImageDataSize,1,f2);
+// fclose(f2);
+
+ // Deal with the color
+ // -------------------
+
+ std::string str_PhotometricInterpretation =
+ Header->GetEntryByNumber(0x0028,0x0004);
+
+ if ( str_PhotometricInterpretation == "MONOCHROME1 "
+ || str_PhotometricInterpretation == "MONOCHROME2 " )
+ {
+ return ImageDataSize;
+ }
+
+ // Planar configuration = 0 : Pixels are already RGB
+ // Planar configuration = 1 : 3 planes : R, G, B
+ // Planar configuration = 2 : 1 gray Plane + 3 LUT
+
+ // Well ... supposed to be !
+ // See US-PAL-8-10x-echo.dcm: PlanarConfiguration=0,
+ // PhotometricInterpretation=PALETTE COLOR
+ // and heuristic has to be found :-(
+
+ int planConf = Header->GetPlanarConfiguration(); // 0028,0006
+
+ // Whatever Planar Configuration is,
+ // "PALETTE COLOR " implies that we deal with the palette.
+ if ( str_PhotometricInterpretation == "PALETTE COLOR ")
+ {
+ planConf = 2;
+ }
+
+ switch ( planConf )
+ {
+ case 0:
+ // Pixels are already RGB
+ break;
+ case 1:
+ if (str_PhotometricInterpretation == "YBR_FULL")
+ {
+ // Warning : YBR_FULL_422 acts as RGB
+ // : we need to make RGB Pixels from Planes Y,cB,cR
+
+ // to see the tricks about YBR_FULL, YBR_FULL_422,
+ // YBR_PARTIAL_422, YBR_ICT, YBR_RCT have a look at :
+ // ftp://medical.nema.org/medical/dicom/final/sup61_ft.pdf
+ // and be *very* affraid
+ //
+ int l = Header->GetXSize() * Header->GetYSize();
+ int nbFrames = Header->GetZSize();
+
+ uint8_t* newDest = new uint8_t[ImageDataSize];
+ uint8_t* x = newDest;
+ uint8_t* a = (uint8_t*)destination;
+ uint8_t* b = a + l;
+ uint8_t* c = b + l;
+ double R,G,B;
+
+ /// \todo : Replace by the 'well known' integer computation
+ /// counterpart
+ /// see http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
+ /// for code optimisation
+
+ for (int i = 0; i < nbFrames; i++)
+ {
+ for (int j = 0; j < l; j++)
+ {
+ R = 1.164 *(*a-16) + 1.596 *(*c -128) + 0.5;
+ G = 1.164 *(*a-16) - 0.813 *(*c -128) - 0.392 *(*b -128) + 0.5;
+ B = 1.164 *(*a-16) + 2.017 *(*b -128) + 0.5;
+
+ if (R < 0.0) R = 0.0;
+ if (G < 0.0) G = 0.0;
+ if (B < 0.0) B = 0.0;
+ if (R > 255.0) R = 255.0;
+ if (G > 255.0) G = 255.0;
+ if (B > 255.0) B = 255.0;
+
+ *(x++) = (uint8_t)R;
+ *(x++) = (uint8_t)G;
+ *(x++) = (uint8_t)B;
+ a++; b++; c++;
+ }
+ }
+ memmove(destination, newDest, ImageDataSize);
+ delete[] newDest;
+ }
+ else
+ {
+ // need to make RGB Pixels from R,G,B Planes
+ // (all the Frames at a time)
+
+ int l = Header->GetXSize() * Header->GetYSize() * Header->GetZSize();
+
+ uint8_t *newDest = new uint8_t[ImageDataSize];
+ uint8_t *x = newDest;
+ uint8_t *a = (uint8_t *)destination;
+ uint8_t *b = a + l;
+ uint8_t *c = b + l;
+
+ for (int j = 0; j < l; j++)
+ {
+ *(x++) = *(a++);
+ *(x++) = *(b++);
+ *(x++) = *(c++);
+ }
+ memmove(destination, newDest, ImageDataSize);
+ delete[] newDest;
+ }
+ break;
+ case 2:
+ // Palettes were found
+ // Let the user deal with them !
+ return ImageDataSize;
+ }
+ // now, it's an RGB image
+ // Lets's write it in the Header
+
+ // CreateOrReplaceIfExist ?
+
+ std::string spp = "3"; // Samples Per Pixel
+ std::string rgb = "RGB "; // Photometric Interpretation
+ std::string planConfig = "0"; // Planar Configuration
+
+ Header->SetEntryByNumber(spp,0x0028,0x0002);
+ Header->SetEntryByNumber(rgb,0x0028,0x0004);
+ Header->SetEntryByNumber(planConfig,0x0028,0x0006);
+
+ /// \todo Drop Palette Color out of the Header?
+ return ImageDataSize;
+}