+ // 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;
+ std::string ImageLocation = GetPubElValByName("Image Location");
+ if ( ImageLocation == GDCM_UNFOUND ) {
+ grPixel = 0x7fe0;
+ } else {
+ grPixel = (guint16) atoi( ImageLocation.c_str() );
+ }
+ if (grPixel != 0x7fe0)
+ // This is a kludge for old dirty Philips imager.
+ numPixel = 0x1010;
+ else
+ numPixel = 0x0010;
+
+ gdcmElValue* PixelElement = PubElValSet.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::GetDictEntryByNumber(guint16 group,
+ guint16 element) {
+ gdcmDictEntry * found = (gdcmDictEntry*)0;
+ if (!RefPubDict && !RefShaDict) {
+ dbg.Verbose(0, "gdcmHeader::GetDictEntry",
+ "we SHOULD have a default dictionary");
+ }
+ if (RefPubDict) {
+ found = RefPubDict->GetTagByNumber(group, element);
+ if (found)
+ return found;
+ }
+ if (RefShaDict) {
+ found = RefShaDict->GetTagByNumber(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(std::string Name) {
+ gdcmDictEntry * found = (gdcmDictEntry*)0;
+ if (!RefPubDict && !RefShaDict) {
+ dbg.Verbose(0, "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 ("gdcm::Unfound") otherwise.
+ */
+std::string gdcmHeader::GetPubElValByNumber(guint16 group, guint16 element) {
+ return PubElValSet.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 ("gdcm::Unfound") otherwise.
+ */
+std::string gdcmHeader::GetPubElValRepByNumber(guint16 group, guint16 element) {
+ gdcmElValue* elem = PubElValSet.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 ("gdcm::Unfound") otherwise.
+ */
+std::string gdcmHeader::GetPubElValByName(std::string TagName) {
+ return PubElValSet.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 ("gdcm::Unfound") otherwise.
+ */
+std::string gdcmHeader::GetPubElValRepByName(std::string TagName) {
+ gdcmElValue* elem = PubElValSet.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 ("gdcm::Unfound") otherwise.
+ */
+std::string gdcmHeader::GetShaElValByNumber(guint16 group, guint16 element) {
+ return ShaElValSet.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 ("gdcm::Unfound") otherwise.
+ */
+std::string gdcmHeader::GetShaElValRepByNumber(guint16 group, guint16 element) {
+ gdcmElValue* elem = ShaElValSet.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 ("gdcm::Unfound") otherwise.
+ */
+std::string gdcmHeader::GetShaElValByName(std::string TagName) {
+ return ShaElValSet.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 ("gdcm::Unfound") otherwise.
+ */
+std::string gdcmHeader::GetShaElValRepByName(std::string TagName) {
+ gdcmElValue* elem = ShaElValSet.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 ("gdcm::Unfound") otherwise.
+ */
+std::string gdcmHeader::GetElValByNumber(guint16 group, guint16 element) {
+ std::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 ("gdcm::Unfound") otherwise.
+ */
+std::string gdcmHeader::GetElValRepByNumber(guint16 group, guint16 element) {
+ std::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 ("gdcm::Unfound") otherwise.
+ */
+std::string gdcmHeader::GetElValByName(std::string TagName) {
+ std::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 ("gdcm::Unfound") otherwise.
+ */
+std::string gdcmHeader::GetElValRepByName(std::string TagName) {
+ std::string pub = GetPubElValRepByName(TagName);
+ if (pub.length())
+ return pub;
+ return GetShaElValRepByName(TagName);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Accesses an existing gdcmElValue in the PubElValSet 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(std::string content, guint16 group,
+ guint16 element)
+
+//TODO : homogeneiser les noms : SetPubElValByNumber qui appelle PubElValSet.SetElValueByNumber
+// pourquoi pas SetPubElValueByNumber ??
+{
+
+ return ( PubElValSet.SetElValueByNumber (content, group, element) );
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Accesses an existing gdcmElValue in the PubElValSet 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(std::string content, std::string TagName) {
+ return ( PubElValSet.SetElValueByName (content, TagName) );
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Accesses an existing gdcmElValue in the PubElValSet of this instance
+ * through it's (group, element) and modifies it's length with
+ * the given value.
+ * \warning Use with extreme caution.
+ * @param length new length to substitute with
+ * @param group group of the ElVal to modify
+ * @param element element of the ElVal to modify
+ * @return 1 on success, 0 otherwise.
+ */
+
+int gdcmHeader::SetPubElValLengthByNumber(guint32 length, guint16 group,
+ guint16 element) {
+ return ( PubElValSet.SetElValueLengthByNumber (length, group, element) );
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Accesses an existing gdcmElValue in the ShaElValSet 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
+ * @return 1 on success, 0 otherwise.
+ */
+int gdcmHeader::SetShaElValByNumber(std::string content,
+ guint16 group, guint16 element) {
+ return ( ShaElValSet.SetElValueByNumber (content, group, element) );
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Accesses an existing gdcmElValue in the ShaElValSet of this instance
+ * through tag name and modifies it's content with the given value.
+ * @param content new value to substitute with
+ * @param ShadowTagName name of the tag to be modified
+ */
+int gdcmHeader::SetShaElValByName(std::string content, std::string ShadowTagName) {
+ return ( ShaElValSet.SetElValueByName (content, ShadowTagName) );
+}
+
+/**
+ * \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);
+ PubElValSet.Add(newElValue);
+ }
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief This predicate, based on hopefully reasonnable heuristics,
+ * decides whether or not the current gdcmHeader was properly parsed
+ * and contains the mandatory information for being considered as
+ * a well formed and usable image.
+ * @return true when gdcmHeader is the one of a reasonable Dicom file,
+ * false otherwise.
+ */
+bool gdcmHeader::IsReadable(void) {
+ if ( GetElValByName("Image Dimensions") != GDCM_UNFOUND
+ && atoi(GetElValByName("Image Dimensions").c_str()) > 4 ) {
+ return false;
+ }
+ if ( GetElValByName("Bits Allocated") == GDCM_UNFOUND )
+ return false;
+ if ( GetElValByName("Bits Stored") == GDCM_UNFOUND )
+ return false;
+ if ( GetElValByName("High Bit") == GDCM_UNFOUND )
+ return false;
+ if ( GetElValByName("Pixel Representation") == GDCM_UNFOUND )
+ return false;
+ return true;
+}
+
+/**
+ * \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.
+ * \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(std::string NewTagName, std::string VR) {
+ gdcmElValue* NewElVal = (gdcmElValue*)0;
+ guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
+ guint32 FreeElem = 0;
+ gdcmDictEntry* NewEntry = (gdcmDictEntry*)0;
+
+ FreeElem = PubElValSet.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);
+ PubElValSet.Add(NewElVal);
+ return NewElVal;