+ gdcmDictEntry * NewTag = GetDictEntryByName(Name);
+ if (!NewTag)
+ NewTag = new gdcmDictEntry(0xffff, 0xffff, "LO", "Unknown", Name);
+
+ gdcmElValue* NewElVal = new gdcmElValue(NewTag);
+ if (!NewElVal) {
+ dbg.Verbose(1, "gdcmHeader::ObtainElValueByName",
+ "failed to allocate gdcmElValue");
+ return (gdcmElValue*)0;
+ }
+ return NewElVal;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Read the next tag but WITHOUT loading it's value
+ * @return On succes the newly created ElValue, NULL on failure.
+ */
+gdcmElValue * gdcmHeader::ReadNextElement(void) {
+ guint16 g;
+ guint16 n;
+ gdcmElValue * NewElVal;
+
+ g = ReadInt16();
+ n = ReadInt16();
+ if (errno == 1)
+ // We reached the EOF (or an error occured) and header parsing
+ // has to be considered as finished.
+ return (gdcmElValue *)0;
+
+ NewElVal = NewElValueByKey(g, n);
+ FindVR(NewElVal);
+ FindLength(NewElVal);
+ if (errno == 1)
+ // Call it quits
+ return (gdcmElValue *)0;
+ NewElVal->SetOffset(ftell(fp));
+ return NewElVal;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Apply some heuristics to predict wether the considered
+ * element value contains/represents an integer or not.
+ * @param ElVal The element value on which to apply the predicate.
+ * @return The result of the heuristical predicate.
+ */
+bool gdcmHeader::IsAnInteger(gdcmElValue * ElVal) {
+ guint16 group = ElVal->GetGroup();
+ guint16 element = ElVal->GetElement();
+ string vr = ElVal->GetVR();
+ guint32 length = ElVal->GetLength();
+
+ // When we have some semantics on the element we just read, and if we
+ // a priori know we are dealing with an integer, then we shall be
+ // able to swap it's element value properly.
+ if ( element == 0 ) { // This is the group length of the group
+ if (length == 4)
+ return true;
+ else
+ dbg.Error("gdcmHeader::IsAnInteger",
+ "Erroneous Group Length element length.");
+ }
+
+ /*
+ // on le traite tt de même (VR peut donner l'info)
+ // faire qq chose + ruse (pas de test si pas de VR)
+ if ( group % 2 != 0 )
+ // We only have some semantics on documented elements, which are
+ // the even ones.
+ return false;
+ */
+
+ /*
+ if ( (length != 4) && ( length != 2) )
+ // Swapping only make sense on integers which are 2 or 4 bytes long.
+
+ // En fait, pour les entiers de 'Value Multiplicity' supérieur a 1
+ // la longueur n'est pas forcement 2 ou 4
+ // ET il faudra swapper.
+ return false;
+ */
+
+ if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
+ return true;
+
+
+ // est-ce encore utile?
+ // mieux vaut modifier le source du Dicom Dictionnaty
+ // et remplacer pour ces 2 cas RET par US
+
+ if ( (group == 0x0028) && (element == 0x0005) )
+ // The "Image Dimensions" tag is retained from ACR/NEMA and contains
+ // the number of dimensions of the contained object (1 for Signal,
+ // 2 for Image, 3 for Volume, 4 for Sequence).
+ return true;
+
+ if ( (group == 0x0028) && (element == 0x0200) )
+ // This tag is retained from ACR/NEMA
+ return true;
+
+ return false;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Recover the offset (from the beginning of the file) of the pixels.
+ */
+size_t gdcmHeader::GetPixelOffset(void) {
+ // If this file complies with the norm we should encounter the
+ // "Image Location" tag (0x0028, 0x0200). This tag contains the
+ // the group that contains the pixel data (hence the "Pixel Data"
+ // is found by indirection through the "Image Location").
+ // Inside the group pointed by "Image Location" the searched element
+ // is conventionally the element 0x0010 (when the norm is respected).
+ // When the "Image Location" is absent we default to group 0x7fe0.
+ guint16 grPixel;
+ guint16 numPixel;
+ string ImageLocation = GetPubElValByName("Image Location");
+ if ( ImageLocation == "gdcm::Unfound" ) {
+ grPixel = 0x7fe0;
+ } else {
+ grPixel = (guint16) atoi( ImageLocation.c_str() );
+ }
+ if (grPixel != 0x7fe0)
+ // FIXME is this still necessary ?
+ // Now, this looks like an old dirty fix for Philips imager
+ numPixel = 0x1010;
+ else
+ numPixel = 0x0010;
+ gdcmElValue* PixelElement = PubElVals.GetElementByNumber(grPixel, numPixel);
+ if (PixelElement)
+ return PixelElement->GetOffset();
+ else
+ return 0;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches both the public and the shadow dictionary (when they
+ * exist) for the presence of the DictEntry with given
+ * group and element. The public dictionary has precedence on the
+ * shadow one.
+ * @param group group of the searched DictEntry
+ * @param element element of the searched DictEntry
+ * @return Corresponding DictEntry when it exists, NULL otherwise.
+ */
+gdcmDictEntry * gdcmHeader::GetDictEntryByKey(guint16 group, guint16 element) {
+ gdcmDictEntry * found = (gdcmDictEntry*)0;
+ if (!RefPubDict && !RefShaDict) {
+ dbg.Verbose(0, "FIXME in gdcmHeader::GetDictEntry",
+ "we SHOULD have a default dictionary");
+ }
+ if (RefPubDict) {
+ found = RefPubDict->GetTagByKey(group, element);
+ if (found)
+ return found;
+ }
+ if (RefShaDict) {
+ found = RefShaDict->GetTagByKey(group, element);
+ if (found)
+ return found;
+ }
+ return found;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches both the public and the shadow dictionary (when they
+ * exist) for the presence of the DictEntry with given name.
+ * The public dictionary has precedence on the shadow one.
+ * @param Name name of the searched DictEntry
+ * @return Corresponding DictEntry when it exists, NULL otherwise.
+ */
+gdcmDictEntry * gdcmHeader::GetDictEntryByName(string Name) {
+ gdcmDictEntry * found = (gdcmDictEntry*)0;
+ if (!RefPubDict && !RefShaDict) {
+ dbg.Verbose(0, "FIXME in gdcmHeader::GetDictEntry",
+ "we SHOULD have a default dictionary");
+ }
+ if (RefPubDict) {
+ found = RefPubDict->GetTagByName(Name);
+ if (found)
+ return found;
+ }
+ if (RefShaDict) {
+ found = RefShaDict->GetTagByName(Name);
+ if (found)
+ return found;
+ }
+ return found;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches within the public dictionary for element value of
+ * a given tag.
+ * @param group Group of the researched tag.
+ * @param element Element of the researched tag.
+ * @return Corresponding element value when it exists, and the string
+ * "gdcm::Unfound" otherwise.
+ */
+string gdcmHeader::GetPubElValByNumber(guint16 group, guint16 element) {
+ return PubElVals.GetElValueByNumber(group, element);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches within the public dictionary for element value
+ * representation of a given tag.
+ *
+ * Obtaining the VR (Value Representation) might be needed by caller
+ * to convert the string typed content to caller's native type
+ * (think of C++ vs Python). The VR is actually of a higher level
+ * of semantics than just the native C++ type.
+ * @param group Group of the researched tag.
+ * @param element Element of the researched tag.
+ * @return Corresponding element value representation when it exists,
+ * and the string "gdcm::Unfound" otherwise.
+ */
+string gdcmHeader::GetPubElValRepByNumber(guint16 group, guint16 element) {
+ gdcmElValue* elem = PubElVals.GetElementByNumber(group, element);
+ if ( !elem )
+ return "gdcm::Unfound";
+ return elem->GetVR();
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches within the public dictionary for element value of
+ * a given tag.
+ * @param TagName name of the researched element.
+ * @return Corresponding element value when it exists, and the string
+ * "gdcm::Unfound" otherwise.
+ */
+string gdcmHeader::GetPubElValByName(string TagName) {
+ return PubElVals.GetElValueByName(TagName);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches within the elements parsed with the public dictionary for
+ * the element value representation of a given tag.
+ *
+ * Obtaining the VR (Value Representation) might be needed by caller
+ * to convert the string typed content to caller's native type
+ * (think of C++ vs Python). The VR is actually of a higher level
+ * of semantics than just the native C++ type.
+ * @param TagName name of the researched element.
+ * @return Corresponding element value representation when it exists,
+ * and the string "gdcm::Unfound" otherwise.
+ */
+string gdcmHeader::GetPubElValRepByName(string TagName) {
+ gdcmElValue* elem = PubElVals.GetElementByName(TagName);
+ if ( !elem )
+ return "gdcm::Unfound";
+ return elem->GetVR();
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches within elements parsed with the SHADOW dictionary
+ * for the element value of a given tag.
+ * @param group Group of the researched tag.
+ * @param element Element of the researched tag.
+ * @return Corresponding element value representation when it exists,
+ * and the string "gdcm::Unfound" otherwise.
+ */
+string gdcmHeader::GetShaElValByNumber(guint16 group, guint16 element) {
+ return ShaElVals.GetElValueByNumber(group, element);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches within the elements parsed with the SHADOW dictionary
+ * for the element value representation of a given tag.
+ *
+ * Obtaining the VR (Value Representation) might be needed by caller
+ * to convert the string typed content to caller's native type
+ * (think of C++ vs Python). The VR is actually of a higher level
+ * of semantics than just the native C++ type.
+ * @param group Group of the researched tag.
+ * @param element Element of the researched tag.
+ * @return Corresponding element value representation when it exists,
+ * and the string "gdcm::Unfound" otherwise.
+ */
+string gdcmHeader::GetShaElValRepByNumber(guint16 group, guint16 element) {
+ gdcmElValue* elem = ShaElVals.GetElementByNumber(group, element);
+ if ( !elem )
+ return "gdcm::Unfound";
+ return elem->GetVR();
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches within the elements parsed with the shadow dictionary
+ * for an element value of given tag.
+ * @param TagName name of the researched element.
+ * @return Corresponding element value when it exists, and the string
+ * "gdcm::Unfound" otherwise.
+ */
+string gdcmHeader::GetShaElValByName(string TagName) {
+ return ShaElVals.GetElValueByName(TagName);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches within the elements parsed with the shadow dictionary for
+ * the element value representation of a given tag.
+ *
+ * Obtaining the VR (Value Representation) might be needed by caller
+ * to convert the string typed content to caller's native type
+ * (think of C++ vs Python). The VR is actually of a higher level
+ * of semantics than just the native C++ type.
+ * @param TagName name of the researched element.
+ * @return Corresponding element value representation when it exists,
+ * and the string "gdcm::Unfound" otherwise.
+ */
+string gdcmHeader::GetShaElValRepByName(string TagName) {
+ gdcmElValue* elem = ShaElVals.GetElementByName(TagName);
+ if ( !elem )
+ return "gdcm::Unfound";
+ return elem->GetVR();
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches within elements parsed with the public dictionary
+ * and then within the elements parsed with the shadow dictionary
+ * for the element value of a given tag.
+ * @param group Group of the researched tag.
+ * @param element Element of the researched tag.
+ * @return Corresponding element value representation when it exists,
+ * and the string "gdcm::Unfound" otherwise.
+ */
+string gdcmHeader::GetElValByNumber(guint16 group, guint16 element) {
+ string pub = GetPubElValByNumber(group, element);
+ if (pub.length())
+ return pub;
+ return GetShaElValByNumber(group, element);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches within elements parsed with the public dictionary
+ * and then within the elements parsed with the shadow dictionary
+ * for the element value representation of a given tag.
+ *
+ * Obtaining the VR (Value Representation) might be needed by caller
+ * to convert the string typed content to caller's native type
+ * (think of C++ vs Python). The VR is actually of a higher level
+ * of semantics than just the native C++ type.
+ * @param group Group of the researched tag.
+ * @param element Element of the researched tag.
+ * @return Corresponding element value representation when it exists,
+ * and the string "gdcm::Unfound" otherwise.
+ */
+string gdcmHeader::GetElValRepByNumber(guint16 group, guint16 element) {
+ string pub = GetPubElValRepByNumber(group, element);
+ if (pub.length())
+ return pub;
+ return GetShaElValRepByNumber(group, element);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches within elements parsed with the public dictionary
+ * and then within the elements parsed with the shadow dictionary
+ * for the element value of a given tag.
+ * @param TagName name of the researched element.
+ * @return Corresponding element value when it exists,
+ * and the string "gdcm::Unfound" otherwise.
+ */
+string gdcmHeader::GetElValByName(string TagName) {
+ string pub = GetPubElValByName(TagName);
+ if (pub.length())
+ return pub;
+ return GetShaElValByName(TagName);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches within elements parsed with the public dictionary
+ * and then within the elements parsed with the shadow dictionary
+ * for the element value representation of a given tag.
+ *
+ * Obtaining the VR (Value Representation) might be needed by caller
+ * to convert the string typed content to caller's native type
+ * (think of C++ vs Python). The VR is actually of a higher level
+ * of semantics than just the native C++ type.
+ * @param TagName name of the researched element.
+ * @return Corresponding element value representation when it exists,
+ * and the string "gdcm::Unfound" otherwise.
+ */
+string gdcmHeader::GetElValRepByName(string TagName) {
+ string pub = GetPubElValRepByName(TagName);
+ if (pub.length())
+ return pub;
+ return GetShaElValRepByName(TagName);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Accesses an existing gdcmElValue in the PubElVals of this instance
+ * through it's (group, element) and modifies it's content with
+ * the given value.
+ * @param content new value to substitute with
+ * @param group group of the ElVal to modify
+ * @param element element of the ElVal to modify
+ */
+int gdcmHeader::SetPubElValByNumber(string content, guint16 group,
+ guint16 element)
+{
+ return ( PubElVals.SetElValueByNumber (content, group, element) );
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Accesses an existing gdcmElValue in the PubElVals of this instance
+ * through tag name and modifies it's content with the given value.
+ * @param content new value to substitute with
+ * @param TagName name of the tag to be modified
+ */
+int gdcmHeader::SetPubElValByName(string content, string TagName) {
+ return ( PubElVals.SetElValueByName (content, TagName) );
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Accesses an existing gdcmElValue in the PubElVals of this instance
+ * through it's (group, element) and modifies it's length with
+ * the given value.
+ * NOT FOR BOZOs !
+ * @param contents new length to substitute with
+ * @param group group of the ElVal to modify
+ * @param element element of the ElVal to modify
+ */
+int gdcmHeader::SetPubElValLengthByNumber(guint32 lgr, guint16 group,
+ guint16 element)
+{
+ return ( PubElVals.SetElValueLengthByNumber (lgr, group, element) );
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Accesses an existing gdcmElValue in the ShaElVals of this instance
+ * through it's (group, element) and modifies it's content with
+ * the given value.
+ * @param content new value to substitute with
+ * @param group group of the ElVal to modify
+ * @param element element of the ElVal to modify
+ */
+int gdcmHeader::SetShaElValByNumber(string content,
+ guint16 group, guint16 element)
+{
+ return ( ShaElVals.SetElValueByNumber (content, group, element) );
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Accesses an existing gdcmElValue in the ShaElVals of this instance
+ * through tag name and modifies it's content with the given value.
+ * @param content new value to substitute with
+ * @param TagName name of the tag to be modified
+ */
+int gdcmHeader::SetShaElValByName(string content, string TagName) {
+ return ( ShaElVals.SetElValueByName (content, TagName) );
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Parses the header of the file but WITHOUT loading element values.
+ */
+void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
+ gdcmElValue * newElValue = (gdcmElValue *)0;
+
+ rewind(fp);
+ CheckSwap();
+ while ( (newElValue = ReadNextElement()) ) {
+ SkipElementValue(newElValue);
+ PubElVals.Add(newElValue);
+ }
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Once the header is parsed add some gdcm convenience/helper elements
+ * in the gdcmElValSet. For example add:
+ * - gdcmImageType which is an entry containing a short for the
+ * type of image and whose value ranges in
+ * I8 (unsigned 8 bit image)
+ * I16 (unsigned 8 bit image)
+ * IS16 (signed 8 bit image)
+ * - gdcmXsize, gdcmYsize, gdcmZsize whose values are respectively
+ * the ones of the official DICOM fields Rows, Columns and Planes.
+ */
+void gdcmHeader::AddAndDefaultElements(void) {
+ gdcmElValue* NewElVal = (gdcmElValue*)0;
+ string NewVal;
+
+ NewElVal = NewManualElValToPubDict("gdcmXSize", "US");
+ if (!NewElVal) return;
+ NewVal = GetElValByName("Rows");
+ if (NewVal != "gdcm::Unfound")
+ NewElVal->SetValue(NewVal);
+ else
+ NewElVal->SetValue("0");
+
+
+ NewElVal = NewManualElValToPubDict("gdcmYSize", "US");
+ if (!NewElVal) return;
+ NewVal = GetElValByName("Columns");
+ if (NewVal != "gdcm::Unfound")
+ NewElVal->SetValue(NewVal);
+ else
+ NewElVal->SetValue("0");
+
+
+ NewElVal = NewManualElValToPubDict("gdcmZSize", "US");
+ if (!NewElVal) return;
+ NewVal = GetElValByName("Planes");
+ if (NewVal != "gdcm::Unfound")
+ NewElVal->SetValue(NewVal);
+ else
+ NewElVal->SetValue("0");
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Small utility function that creates a new manually crafted
+ * (as opposed as read from the file) gdcmElValue with user
+ * specified name and adds it to the public tag hash table.
+ * Refer to gdcmHeader::AddAndDefaultElements for a typical usage.
+ * \note A fake TagKey is generated so the PubDict can keep it's coherence.
+ * @param NewTagName The name to be given to this new tag.
+ * @param VR The Value Representation to be given to this new tag.
+ * @ return The newly hand crafted Element Value.
+ */
+gdcmElValue* gdcmHeader::NewManualElValToPubDict(string NewTagName, string VR) {
+ gdcmElValue* NewElVal = (gdcmElValue*)0;
+ guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
+ guint32 FreeElem = 0;
+ gdcmDictEntry* NewEntry = (gdcmDictEntry*)0;
+
+ FreeElem = PubElVals.GenerateFreeTagKeyInGroup(StuffGroup);
+ if (FreeElem == UINT32_MAX) {
+ dbg.Verbose(1, "gdcmHeader::NewManualElValToPubDict",
+ "Group 0xffff in Public Dict is full");
+ return (gdcmElValue*)0;
+ }
+ NewEntry = new gdcmDictEntry(StuffGroup, FreeElem,
+ VR, "GDCM", NewTagName);
+ NewElVal = new gdcmElValue(NewEntry);
+ PubElVals.Add(NewElVal);
+ return NewElVal;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Loads the element values of all the elements present in the
+ * public tag based hash table.
+ */
+void gdcmHeader::LoadElements(void) {
+ rewind(fp);
+ TagElValueHT ht = PubElVals.GetTagHt();
+ for (TagElValueHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) {
+ LoadElementValue(tag->second);
+ }
+}
+
+void gdcmHeader::PrintPubElVal(ostream & os) {
+ PubElVals.Print(os);
+}
+
+void gdcmHeader::PrintPubDict(ostream & os) {
+ RefPubDict->Print(os);