+/**
+ * \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;
+
+ // we save the initial values of the following
+ // in order to be able to restore the header in a disk-consistent state
+ // (if user asks twice to get the pixels from disk)
+
+ if ( PixelRead == -1 ) // File was never "read" before
+ {
+ InitialSpp = Header->GetEntryByNumber(0x0028,0x0002);
+ InitialPhotInt = Header->GetEntryByNumber(0x0028,0x0004);
+ InitialPlanConfig = Header->GetEntryByNumber(0x0028,0x0006);
+ InitialBitsAllocated = Header->GetEntryByNumber(0x0028,0x0100);
+ }
+ else // File was already "read", the following *may* have been modified
+ // we restore them to be in a disk-consistent state
+ {
+ // FIXME : What happened with the LUTs ?
+ Header->SetEntryByNumber(InitialSpp,0x0028,0x0002);
+ Header->SetEntryByNumber(InitialPhotInt,0x0028,0x0004);
+ Header->SetEntryByNumber(InitialPlanConfig,0x0028,0x0006);
+ Header->SetEntryByNumber(InitialBitsAllocated,0x0028,0x0100);
+ }
+
+ 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++;
+ }
+ }