+ * \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
+ * file (gdcmHeader only deals with the ... header)
+ * Opens (in read only and when possible) an existing file and checks
+ * for DICOM compliance. Returns NULL on failure.
+ * It will be up to the user to load the pixels into memory
+ * (see GetImageData, GetImageDataRaw)
+ * \note the in-memory representation of all available tags found in
+ * the DICOM header is post-poned to first header information access.
+ * This avoid a double parsing of public part of the header when
+ * one sets an a posteriori shadow dictionary (efficiency can be
+ * seen as a side effect).
+ * @param filename file to be opened for parsing
+ */
+gdcmFile::gdcmFile(std::string const & filename )
+{
+ Header = new gdcmHeader( filename );
+ SelfHeader = true;
+ SaveInitialValues();
+}
+
+/**
+ * \ingroup gdcmFile
+ * \brief canonical destructor
+ * \note If the gdcmHeader was created by the gdcmFile constructor,
+ * it is destroyed by the gdcmFile
+ */
+gdcmFile::~gdcmFile()
+{
+ if( SelfHeader )
+ {
+ delete Header;
+ }
+ Header = 0;
+
+ DeleteInitialValues();
+}
+
+/**
+ * \ingroup gdcmFile
+ * \brief Sets some initial values for the Constructor
+ * \warning not end user intended
+ */
+void gdcmFile::SaveInitialValues()
+{
+
+ PixelRead = -1; // no ImageData read yet.
+ LastAllocatedPixelDataLength = 0;
+ Pixel_Data = 0;
+
+ InitialSpp = "";
+ InitialPhotInt = "";
+ InitialPlanConfig = "";
+ InitialBitsAllocated = "";
+ InitialHighBit = "";
+
+ InitialRedLUTDescr = 0;
+ InitialGreenLUTDescr = 0;
+ InitialBlueLUTDescr = 0;
+ InitialRedLUTData = 0;
+ InitialGreenLUTData = 0;
+ InitialBlueLUTData = 0;
+
+ if ( Header->IsReadable() )
+ {
+ SetPixelDataSizeFromHeader();
+
+ // the following values *may* be modified
+ // by gdcmFile::GetImageDataIntoVectorRaw
+ // we save their initial value.
+ InitialSpp = Header->GetEntryByNumber(0x0028,0x0002);
+ InitialPhotInt = Header->GetEntryByNumber(0x0028,0x0004);
+ InitialPlanConfig = Header->GetEntryByNumber(0x0028,0x0006);
+
+ InitialBitsAllocated = Header->GetEntryByNumber(0x0028,0x0100);
+ InitialHighBit = Header->GetEntryByNumber(0x0028,0x0102);
+
+ // the following entries *may* be removed from the H table
+ // (NOT deleted ...) by gdcmFile::GetImageDataIntoVectorRaw
+ // we keep a pointer on them.
+ InitialRedLUTDescr = Header->GetDocEntryByNumber(0x0028,0x1101);
+ InitialGreenLUTDescr = Header->GetDocEntryByNumber(0x0028,0x1102);
+ InitialBlueLUTDescr = Header->GetDocEntryByNumber(0x0028,0x1103);
+
+ InitialRedLUTData = Header->GetDocEntryByNumber(0x0028,0x1201);
+ InitialGreenLUTData = Header->GetDocEntryByNumber(0x0028,0x1202);
+ InitialBlueLUTData = Header->GetDocEntryByNumber(0x0028,0x1203);
+ }
+}
+
+/**
+ * \ingroup gdcmFile
+ * \brief restores some initial values
+ * \warning not end user intended
+ */
+void gdcmFile::RestoreInitialValues()
+{
+ if ( Header->IsReadable() )
+ {
+ // the following values *may* have been modified
+ // by gdcmFile::GetImageDataIntoVectorRaw
+ // we restore their initial value.
+ if ( InitialSpp != "")
+ Header->SetEntryByNumber(InitialSpp,0x0028,0x0002);
+ if ( InitialPhotInt != "")
+ Header->SetEntryByNumber(InitialPhotInt,0x0028,0x0004);
+ if ( InitialPlanConfig != "")
+
+ Header->SetEntryByNumber(InitialPlanConfig,0x0028,0x0006);
+ if ( InitialBitsAllocated != "")
+ Header->SetEntryByNumber(InitialBitsAllocated,0x0028,0x0100);
+ if ( InitialHighBit != "")
+ Header->SetEntryByNumber(InitialHighBit,0x0028,0x0102);
+
+ // the following entries *may* be have been removed from the H table
+ // (NOT deleted ...) by gdcmFile::GetImageDataIntoVectorRaw
+ // we restore them.
+
+ if (InitialRedLUTDescr)
+ Header->AddEntry(InitialRedLUTDescr);
+ if (InitialGreenLUTDescr)
+ Header->AddEntry(InitialGreenLUTDescr);
+ if (InitialBlueLUTDescr)
+ Header->AddEntry(InitialBlueLUTDescr);
+
+ if (InitialRedLUTData)
+ Header->AddEntry(InitialBlueLUTDescr);
+ if (InitialGreenLUTData)
+ Header->AddEntry(InitialGreenLUTData);
+ if (InitialBlueLUTData)
+ Header->AddEntry(InitialBlueLUTData);
+ }
+}
+
+/**
+ * \ingroup gdcmFile
+ * \brief delete initial values (il they were saved)
+ * of InitialLutDescriptors and InitialLutData
+ */
+void gdcmFile::DeleteInitialValues()
+{
+
+// InitialLutDescriptors and InitialLutData
+// will have to be deleted if the don't belong any longer
+// to the Header H table when the header is deleted...
+
+ if ( InitialRedLUTDescr )
+ delete InitialRedLUTDescr;
+
+ if ( InitialGreenLUTDescr )
+ delete InitialGreenLUTDescr;
+
+ if ( InitialBlueLUTDescr )
+ delete InitialBlueLUTDescr;
+
+ if ( InitialRedLUTData )
+ delete InitialRedLUTData;
+
+ if ( InitialGreenLUTData != NULL)
+ delete InitialGreenLUTData;
+
+ if ( InitialBlueLUTData != NULL)
+ delete InitialBlueLUTData;
+}
+
+/**
+ * \ingroup gdcmFile
+ * \brief drop palette related initial values -if any-
+ * (InitialLutDescriptors and InitialLutData)
+ * out of header, to make it consistent with the Pixel_Data
+ * as it's loaded in memory
+ */
+
+//FIXME : Should be nice, if we could let it here.
+// will be moved to PixelData class
+// Now, the job is done in gdcmHeader.cxx
+
+ /*
+void gdcmFile::DropInitialValues()
+{
+ gdcmHeader* h=GetHeader();
+ if ( GetEntryByNumber(0x0028,0x0002).c_str()[0] == '3' )
+ {
+ // if SamplesPerPixel = 3, sure we don't need any LUT !
+ // Drop 0028|1101, 0028|1102, 0028|1103
+ // Drop 0028|1201, 0028|1202, 0028|1203
+
+ gdcmDocEntry* e = h->GetDocEntryByNumber(0x0028,0x01101);
+ if (e)
+ {
+ h->RemoveEntryNoDestroy(e);
+ }
+ e = h->GetDocEntryByNumber(0x0028,0x1102);
+ if (e)
+ {
+ h->RemoveEntryNoDestroy(e);
+ }
+ e = h->GetDocEntryByNumber(0x0028,0x1103);
+ if (e)
+ {
+ h->RemoveEntryNoDestroy(e);
+ }
+ e = h->GetDocEntryByNumber(0x0028,0x01201);
+ if (e)
+ {
+ h->RemoveEntryNoDestroy(e);
+ }
+ e = h->GetDocEntryByNumber(0x0028,0x1202);
+ if (e)
+ {
+ h->RemoveEntryNoDestroy(e);
+ }
+ e = h->GetDocEntryByNumber(0x0028,0x1203);
+ if (e)
+ {
+ h->RemoveEntryNoDestroy(e);
+ }
+ }
+}
+*/
+
+//-----------------------------------------------------------------------------
+// Print
+
+//-----------------------------------------------------------------------------
+// Public
+
+/**
+ * \ingroup gdcmFile
+ * \brief computes the length (in bytes) we must ALLOCATE to receive the
+ * image(s) pixels (multiframes taken into account)
+ * \warning : it is NOT the group 7FE0 length
+ * (no interest for compressed images).
+ * \warning : not end user intended ?
+ */
+void gdcmFile::SetPixelDataSizeFromHeader()
+{
+ // see PS 3.3-2003 : C.7.6.3.2.1
+ //
+ // MONOCHROME1
+ // MONOCHROME2
+ // PALETTE COLOR
+ // RGB
+ // HSV (Retired)
+ // ARGB (Retired)
+ // CMYK (Retired)
+ // YBR_FULL
+ // YBR_FULL_422 (no LUT, no Palette)
+ // YBR_PARTIAL_422
+ // YBR_ICT
+ // YBR_RCT
+
+ // LUT's
+ // ex : gdcm-US-ALOKA-16.dcm
+ // 0028|1221 [OW] [Segmented Red Palette Color Lookup Table Data]
+ // 0028|1222 [OW] [Segmented Green Palette Color Lookup Table Data]
+ // 0028|1223 [OW] [Segmented Blue Palette Color Lookup Table Data]
+
+ // ex : OT-PAL-8-face.dcm
+ // 0028|1201 [US] [Red Palette Color Lookup Table Data]
+ // 0028|1202 [US] [Green Palette Color Lookup Table Data]
+ // 0028|1203 [US] [Blue Palette Color Lookup Table Data]
+
+ // Number of "Bits Allocated"
+ int nb;
+ std::string str_nb = Header->GetEntryByNumber(0x0028,0x0100);
+
+ if ( str_nb == GDCM_UNFOUND )
+ {
+ nb = 16;
+ }
+ else
+ {
+ nb = atoi( str_nb.c_str() );
+ if (nb == 12)
+ {
+ nb =16;
+ }
+ }
+ ImageDataSize =
+ ImageDataSizeRaw = Header->GetXSize() * Header->GetYSize()
+ * Header->GetZSize() * (nb/8) * Header->GetSamplesPerPixel();
+ std::string str_PhotometricInterpretation =
+ Header->GetEntryByNumber(0x0028,0x0004);
+
+ // if ( str_PhotometricInterpretation == "PALETTE COLOR " ),
+ // pb when undealt Segmented Palette Color
+
+ if ( Header->HasLUT() )
+ {
+ ImageDataSize *= 3;
+ }
+}
+
+/**
+ * \ingroup gdcmFile
+ * \brief - Allocates necessary memory,
+ * - Reads the pixels from disk (uncompress if necessary),
+ * - Transforms YBR pixels, if any, into RGB pixels
+ * - Transforms 3 planes R, G, B, if any, into a single RGB Plane
+ * - Transforms single Grey plane + 3 Palettes into a RGB Plane
+ * - Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
+ * @return Pointer to newly allocated pixel data.
+ * NULL if alloc fails
+ */
+void *gdcmFile::GetImageData()
+{
+ // FIXME (Mathieu)
+ // I need to deallocate Pixel_Data before doing any allocation:
+
+ if ( Pixel_Data )
+ if ( LastAllocatedPixelDataLength != ImageDataSize )
+ free(Pixel_Data);
+ if ( !Pixel_Data )
+ Pixel_Data = new uint8_t[ImageDataSize];
+
+ if ( Pixel_Data )
+ {
+ LastAllocatedPixelDataLength = ImageDataSize;
+
+ // we load the pixels (and transform grey level + LUT into RGB)
+ GetImageDataIntoVector(Pixel_Data, ImageDataSize);
+
+ // We say the value *is* loaded.
+ GetHeader()->SetEntryByNumber( GDCM_BINLOADED,
+ GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
+
+ // Will be 7fe0, 0010 in standard case
+ GetHeader()->SetEntryVoidAreaByNumber( Pixel_Data,
+ GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
+ }
+ PixelRead = 0; // no PixelRaw
+
+ return Pixel_Data;
+}
+
+/**
+ * \ingroup gdcmFile
+ * \brief
+ * Read the pixels from disk (uncompress if necessary),
+ * Transforms YBR pixels, if any, into RGB pixels
+ * Transforms 3 planes R, G, B, if any, into a single RGB Plane
+ * Transforms single Grey plane + 3 Palettes into a RGB Plane
+ * Copies at most MaxSize bytes of pixel data to caller allocated
+ * memory space.
+ * \warning This function allows people that want to build a volume
+ * from an image stack *not to* have, 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 have to call GetImageData
+ *
+ * @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::GetImageDataIntoVector (void* destination, size_t maxSize)
+{
+ GetImageDataIntoVectorRaw (destination, maxSize);
+ PixelRead = 0 ; // =0 : no ImageDataRaw
+ if ( !Header->HasLUT() )
+ {
+ return ImageDataSize;
+ }
+
+ // from Lut R + Lut G + Lut B
+ uint8_t *newDest = new uint8_t[ImageDataSize];
+ uint8_t *a = (uint8_t *)destination;
+ uint8_t *lutRGBA = Header->GetLUTRGBA();
+
+ if ( lutRGBA )
+ {
+ int j;
+ // move Gray pixels to temp area
+ memmove(newDest, destination, ImageDataSizeRaw);
+ for (size_t i=0; i<ImageDataSizeRaw; ++i)
+ {
+ // Build RGB Pixels
+ j = newDest[i]*4;
+ *a++ = lutRGBA[j];
+ *a++ = lutRGBA[j+1];
+ *a++ = lutRGBA[j+2];
+ }
+ delete[] newDest;
+
+ // now, it's an RGB image
+ // Lets's write it in the Header
+
+ // FIXME : Better use CreateOrReplaceIfExist ?
+
+ std::string spp = "3"; // Samples Per Pixel
+ Header->SetEntryByNumber(spp,0x0028,0x0002);
+ std::string rgb = "RGB "; // Photometric Interpretation
+ Header->SetEntryByNumber(rgb,0x0028,0x0004);
+ std::string planConfig = "0"; // Planar Configuration
+ Header->SetEntryByNumber(planConfig,0x0028,0x0006);
+
+ }
+ else // GetLUTRGBA() failed
+ {
+ // (gdcm-US-ALOKA-16.dcm), contains Segmented xxx Palette Color
+ // that are *more* than 65535 long ?!?
+ // No idea how to manage such an image !
+ // Need to make RGB Pixels (?) from grey Pixels (?!) and Gray Lut (!?!)
+ // It seems that *no Dicom Viewer* has any idea :-(
+
+ std::string photomInterp = "MONOCHROME1 "; // Photometric Interpretation
+ Header->SetEntryByNumber(photomInterp,0x0028,0x0004);
+ }
+
+ /// \todo Drop Palette Color out of the Header?
+ return ImageDataSize;
+}
+
+/**
+ * \ingroup gdcmFile
+ * \brief Allocates necessary memory,
+ * Transforms YBR pixels (if any) into RGB pixels
+ * Transforms 3 planes R, G, B (if any) into a single RGB Plane
+ * Copies the pixel data (image[s]/volume[s]) to newly allocated zone.
+ * DOES NOT transform Grey plane + 3 Palettes into a RGB Plane
+ * @return Pointer to newly allocated pixel data.
+ * \ NULL if alloc fails