+ rewind(fp);
+ TagElValueHT ht = PubElValSet.GetTagHt();
+ for (TagElValueHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) {
+ LoadElementValue(tag->second);
+ }
+ // Load 'non string' values
+ rewind(fp);
+ string PhotometricInterpretation = GetPubElValByNumber(0x0028,0x0004);
+ if( PhotometricInterpretation == "PALETTE COLOR " ){
+ LoadElementVoidArea(0x0028,0x1200); // gray LUT
+ LoadElementVoidArea(0x0028,0x1201); // R LUT
+ LoadElementVoidArea(0x0028,0x1202); // G LUT
+ LoadElementVoidArea(0x0028,0x1203); // B LUT
+
+ LoadElementVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data
+ LoadElementVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data
+ LoadElementVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data
+ }
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief
+ * @return
+ */
+void gdcmHeader::PrintPubElVal(std::ostream & os) {
+ PubElValSet.Print(os);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief
+ * @return
+ */
+void gdcmHeader::PrintPubDict(std::ostream & os) {
+ RefPubDict->Print(os);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief
+ * @return
+ */
+int gdcmHeader::Write(FILE * fp, FileType type) {
+ return PubElValSet.Write(fp, type);
+}
+
+//
+// ------------------------ 'non string' elements related functions
+//
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Loads (from disk) the element content
+ * when a string is not suitable
+ */
+void * gdcmHeader::LoadElementVoidArea(guint16 Group, guint16 Elem) {
+ gdcmElValue * Element= PubElValSet.GetElementByNumber(Group, Elem);
+ if ( !Element )
+ return NULL;
+ size_t o =(size_t)Element->GetOffset();
+ fseek(fp, o, SEEK_SET);
+ int l=Element->GetLength();
+ void * a = malloc(l);
+ if(!a) {
+ cout << "Big Broblem (LoadElementVoidArea, malloc) "
+ << hex << Group << " " << Elem << "\n";
+ return NULL;
+ }
+ int res = PubElValSet.SetVoidAreaByNumber(a, Group, Elem);
+ // TODO check the result
+ size_t l2 = fread(a, 1, l ,fp);
+ if(l != l2) {
+ cout << "Big Broblem (LoadElementVoidArea, fread) "
+ << hex << Group << " " << Elem << "\n";
+ free(a);
+ return NULL;
+ }
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Gets (from Header) the offset of a 'non string' element value
+ * \ (LoadElementValue has already be executed)
+ * @param Group
+ * @param Elem
+ * @return File Offset of the Element Value
+ */
+ size_t gdcmHeader::GetPubElValOffsetByNumber(guint16 Group, guint16 Elem) {
+ gdcmElValue* elValue = PubElValSet.GetElementByNumber(Group, Elem);
+ if (!elValue) {
+ dbg.Verbose(1, "gdcmHeader::GetElValueByNumber",
+ "failed to Locate gdcmElValue");
+ return (size_t)0;
+ }
+ return elValue->GetOffset();
+}
+
+/**
+ * \ingroup gdcmHeader
+* \brief Gets (from Header) a 'non string' element value
+ * \ (LoadElementValue has already be executed)
+ * @param Group
+ * @param Elem
+ * @return Pointer to the 'non string' area
+
+ */
+ void * gdcmHeader::GetPubElValVoidAreaByNumber(guint16 Group, guint16 Elem) {
+ gdcmElValue* elValue = PubElValSet.GetElementByNumber(Group, Elem);
+ if (!elValue) {
+ dbg.Verbose(1, "gdcmHeader::GetElValueByNumber",
+ "failed to Locate gdcmElValue");
+ return (NULL);
+ }
+ return elValue->GetVoidArea();
+}
+
+
+//
+// =============================================================================
+// Heuristics based accessors
+//==============================================================================
+//
+
+// TODO : move to an other file.
+
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Retrieve the number of columns of image.
+ * @return The encountered size when found, 0 by default.
+ */
+int gdcmHeader::GetXSize(void) {
+ // We cannot check for "Columns" because the "Columns" tag is present
+ // both in IMG (0028,0011) and OLY (6000,0011) sections of the dictionary.
+ string StrSize = GetPubElValByNumber(0x0028,0x0011);
+ if (StrSize == "gdcm::Unfound")
+ return 0;
+ return atoi(StrSize.c_str());
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Retrieve the number of lines of image.
+ * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
+ * @return The encountered size when found, 1 by default.
+ */
+int gdcmHeader::GetYSize(void) {
+ // We cannot check for "Rows" because the "Rows" tag is present
+ // both in IMG (0028,0010) and OLY (6000,0010) sections of the dictionary.
+ string StrSize = GetPubElValByNumber(0x0028,0x0010);
+ if (StrSize != "gdcm::Unfound")
+ return atoi(StrSize.c_str());
+ if ( IsDicomV3() )
+ return 0;
+ else
+ // The Rows (0028,0010) entry is optional for ACR/NEMA. It might
+ // hence be a signal (1d image). So we default to 1:
+ return 1;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Retrieve the number of planes of volume or the number
+ * of frames of a multiframe.
+ * \warning When present we consider the "Number of Frames" as the third
+ * dimension. When absent we consider the third dimension as
+ * being the "Planes" tag content.
+ * @return The encountered size when found, 1 by default.
+ */
+int gdcmHeader::GetZSize(void) {
+ // Both in DicomV3 and ACR/Nema the consider the "Number of Frames"
+ // as the third dimension.
+ string StrSize = GetPubElValByNumber(0x0028,0x0008);
+ if (StrSize != "gdcm::Unfound")
+ return atoi(StrSize.c_str());
+
+ // We then consider the "Planes" entry as the third dimension [we
+ // cannot retrieve by name since "Planes tag is present both in
+ // IMG (0028,0012) and OLY (6000,0012) sections of the dictionary].
+ StrSize = GetPubElValByNumber(0x0028,0x0012);
+ if (StrSize != "gdcm::Unfound")
+ return atoi(StrSize.c_str());
+ return 1;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Retrieve the number of Bits Stored
+ * (as opposite to number of Bits Allocated)
+ *
+ * @return The encountered number of Bits Stored, 0 by default.
+ */
+int gdcmHeader::GetBitsStored(void) {
+ string StrSize = GetPubElValByNumber(0x0028,0x0101);
+ if (StrSize == "gdcm::Unfound")
+ return 1;
+ return atoi(StrSize.c_str());
+}
+
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Retrieve the number of Samples Per Pixel
+ * (1 : gray level, 3 : RGB)
+ *
+ * @return The encountered number of Samples Per Pixel, 1 by default.
+ */
+int gdcmHeader::GetSamplesPerPixel(void) {
+ string StrSize = GetPubElValByNumber(0x0028,0x0002);
+ if (StrSize == "gdcm::Unfound")
+ return 1; // Well, it's supposed to be mandatory ...
+ return atoi(StrSize.c_str());
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Retrieve the Planar Configuration for RGB images
+ * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
+ *
+ * @return The encountered Planar Configuration, 0 by default.
+ */
+int gdcmHeader::GetPlanarConfiguration(void) {
+ string StrSize = GetPubElValByNumber(0x0028,0x0006);
+ if (StrSize == "gdcm::Unfound")
+ return 0;
+ return atoi(StrSize.c_str());
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Return the size (in bytes) of a single pixel of data.
+ * @return The size in bytes of a single pixel of data.
+ *
+ */
+int gdcmHeader::GetPixelSize(void) {
+ string PixelType = GetPixelType();
+ if (PixelType == "8U" || PixelType == "8S")
+ return 1;
+ if (PixelType == "16U" || PixelType == "16S")
+ return 2;
+ if (PixelType == "32U" || PixelType == "32S")
+ return 4;
+ dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
+ return 0;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Build the Pixel Type of the image.
+ * Possible values are:
+ * - 8U unsigned 8 bit,
+ * - 8S signed 8 bit,
+ * - 16U unsigned 16 bit,
+ * - 16S signed 16 bit,
+ * - 32U unsigned 32 bit,
+ * - 32S signed 32 bit,
+ * \warning 12 bit images appear as 16 bit.
+ * @return
+ */
+string gdcmHeader::GetPixelType(void) {
+ string BitsAlloc;
+ BitsAlloc = GetElValByName("Bits Allocated");
+ if (BitsAlloc == "gdcm::Unfound") {
+ dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
+ BitsAlloc = string("16");
+ }
+ if (BitsAlloc == "12")
+ BitsAlloc = string("16");
+
+ string Signed;
+ Signed = GetElValByName("Pixel Representation");
+ if (Signed == "gdcm::Unfound") {
+ dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
+ BitsAlloc = string("0");
+ }
+ if (Signed == "0")
+ Signed = string("U");
+ else
+ Signed = string("S");
+
+ return( BitsAlloc + Signed);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief gets the info from 0028,0030 : Pixel Spacing
+ * \ else 1.
+ * @return X dimension of a pixel
+ */
+float gdcmHeader::GetXSpacing(void) {
+ float xspacing, yspacing;
+ string StrSpacing = GetPubElValByNumber(0x0028,0x0030);
+
+ if (StrSpacing == "gdcm::Unfound") {
+ dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
+ return 1.;
+ }
+ if( sscanf( StrSpacing.c_str(), "%f\\%f", &xspacing, &yspacing) != 2)
+ return 0.;
+ //else
+ return xspacing;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief gets the info from 0028,0030 : Pixel Spacing
+ * \ else 1.
+ * @return Y dimension of a pixel
+ */
+float gdcmHeader::GetYSpacing(void) {
+ float xspacing, yspacing;
+ string StrSpacing = GetPubElValByNumber(0x0028,0x0030);
+
+ if (StrSpacing == "gdcm::Unfound") {
+ dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
+ return 1.;
+ }
+ if( sscanf( StrSpacing.c_str(), "%f\\%f", &xspacing, &yspacing) != 2)
+ return 0.;
+ if (yspacing == 0.) {
+ dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
+ // seems to be a bug in the header ...
+ sscanf( StrSpacing.c_str(), "%f\\0\\%f", &xspacing, &yspacing);
+ }
+ return yspacing;
+}
+
+
+/**
+ *\ingroup gdcmHeader
+ *\brief gets the info from 0018,0088 : Space Between Slices
+ *\ else from 0018,0050 : Slice Thickness
+ *\ else 1.
+ * @return Z dimension of a voxel-to be
+ */
+float gdcmHeader::GetZSpacing(void) {
+ // TODO : translate into English
+ // Spacing Between Slices : distance entre le milieu de chaque coupe
+ // Les coupes peuvent etre :
+ // jointives (Spacing between Slices = Slice Thickness)
+ // chevauchantes (Spacing between Slices < Slice Thickness)
+ // disjointes (Spacing between Slices > Slice Thickness)
+ // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
+ // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
+ // Si le Spacing Between Slices est absent,
+ // on suppose que les coupes sont jointives
+
+ string StrSpacingBSlices = GetPubElValByNumber(0x0018,0x0088);
+
+ if (StrSpacingBSlices == "gdcm::Unfound") {
+ dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
+ string StrSliceThickness = GetPubElValByNumber(0x0018,0x0050);
+ if (StrSliceThickness == "gdcm::Unfound")
+ return 1.;
+ else
+ // if no 'Spacing Between Slices' is found,
+ // we assume slices join together
+ // (no overlapping, no interslice gap)
+ // if they don't, we're fucked up
+ return atof(StrSliceThickness.c_str());
+ } else {
+ return atof(StrSpacingBSlices.c_str());
+ }
+}
+
+//
+// Image Position Patient (0020,0032):
+// If not found (ACR_NEMA) we try Image Position (0020,0030)
+// If not found (ACR-NEMA), we consider Slice Location (0020,1041)
+// or Location (0020,0050)
+// as the Z coordinate,
+// 0. for all the coordinates if nothing is found
+
+// TODO : find a way to inform the caller nothing was found
+// TODO : How to tell the caller a wrong number of values was found?
+
+/**
+ * \ingroup gdcmHeader
+ * \brief gets the info from 0020,0032 : Image Position Patient
+ *\ else from 0020,0030 : Image Position (RET)
+ *\ else 0.
+ * @return up-left image corner position
+ */
+float gdcmHeader::GetXImagePosition(void) {
+ float xImPos, yImPos, zImPos;
+ string StrImPos = GetPubElValByNumber(0x0020,0x0032);
+
+ if (StrImPos == "gdcm::Unfound") {
+ dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position Patient (0020,0032)");
+ StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
+ if (StrImPos == "gdcm::Unfound") {
+ dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position (RET) (0020,0030)");
+ // How to tell the caller nothing was found ?
+ return 0.;
+ }
+ }
+ if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
+ return 0.;
+ return xImPos;