1 /*=========================================================================
4 Module: $RCSfile: gdcmElementSet.cxx,v $
6 Date: $Date: 2005/10/27 11:39:34 $
7 Version: $Revision: 1.70 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #include "gdcmElementSet.h"
20 #include "gdcmDebug.h"
21 #include "gdcmSeqEntry.h"
22 #include "gdcmDataEntry.h"
26 //-----------------------------------------------------------------------------
27 // Constructor / Destructor
29 * \brief Constructor for a given ElementSet
31 ElementSet::ElementSet()
37 * \brief Canonical destructor.
39 ElementSet::~ElementSet()
44 //-----------------------------------------------------------------------------
47 * \brief Writes the Header Entries (Dicom Elements)
49 * @param fp ofstream to write to
50 * @param filetype filetype
52 void ElementSet::WriteContent(std::ofstream *fp, FileType filetype)
54 for (TagDocEntryHT::const_iterator i = TagHT.begin();
58 // depending on the gdcm::Document type
59 // (gdcm::File; gdcm::DicomDir, (more to come ?)
60 // some groups *cannot* be present.
61 // We hereby protect gdcm for writting stupid things
62 // if they were found in the original document.
63 if ( !MayIWrite( (i->second)->GetGroup() ) )
65 // Skip 'Group Length' element, since it may be wrong.
66 // except for Group 0002
67 if ( (i->second)->GetElement() == 0x0000
68 && (i->second)->GetGroup() != 0x0002 )
70 i->second->WriteContent(fp, filetype);
75 * \brief add a new Dicom Element pointer to the H Table
76 * @param newEntry entry to add
78 bool ElementSet::AddEntry(DocEntry *newEntry)
80 const TagKey &key = newEntry->GetKey();
82 if ( TagHT.count(key) == 1 )
84 gdcmWarningMacro( "Key already present: " << key );
89 TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
96 * \brief Clear the hash table from given entry AND delete the entry.
97 * @param entryToRemove Entry to remove AND delete.
99 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
101 const TagKey &key = entryToRemove->GetKey();
102 if ( TagHT.count(key) == 1 )
105 entryToRemove->Unregister();
109 gdcmWarningMacro( "Key not present : " << key);
114 * \brief delete all entries in the ElementSet
116 void ElementSet::ClearEntry()
118 for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
122 cc->second->Unregister();
129 * \brief Get the first entry while visiting *the* 'zero level' DocEntrySet
130 * (DocEntries out of any Sequence)
131 * \return The first DocEntry if found, otherwhise NULL
133 DocEntry *ElementSet::GetFirstEntry()
135 ItTagHT = TagHT.begin();
136 if (ItTagHT != TagHT.end())
137 return ItTagHT->second;
142 * \brief Get the next entry while visiting *the* 'zero level' DocEntrySet
143 * (DocEntries out of any Sequence)
144 * \note : meaningfull only if GetFirstEntry already called
145 * \return The next DocEntry if found, otherwhise NULL
147 DocEntry *ElementSet::GetNextEntry()
149 gdcmAssertMacro (ItTagHT != TagHT.end());
152 if (ItTagHT != TagHT.end())
153 return ItTagHT->second;
158 * \brief retrieves a Dicom Element using (group, element)
159 * @param group Group number of the searched Dicom Element
160 * @param elem Element number of the searched Dicom Element
163 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem)
165 TagKey key = DictEntry::TranslateToKey(group, elem);
166 TagDocEntryHT::iterator it = TagHT.find(key);
168 if ( it!=TagHT.end() )
173 //-----------------------------------------------------------------------------
176 //-----------------------------------------------------------------------------
179 //-----------------------------------------------------------------------------
182 * \brief Prints the Header Entries (Dicom Elements) from the H Table
183 * @param os ostream to write to
184 * @param indent Indentation string to be prepended during printing
186 void ElementSet::Print(std::ostream &os, std::string const & )
188 // Let's change the 'warning value' for Pixel Data,
189 // to avoid human reader to be confused by 'gdcm::NotLoaded'.
190 DataEntry *pixelElement = GetDataEntry(0x7fe0,0x0010);
191 if ( pixelElement != 0 )
193 pixelElement->SetFlag( DataEntry::FLAG_PIXELDATA );
196 for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
198 DocEntry *entry = i->second;
200 entry->SetPrintLevel(PrintLevel);
203 if ( dynamic_cast<SeqEntry *>(entry) )
205 // Avoid the newline for a sequence:
212 //-----------------------------------------------------------------------------
213 } // end namespace gdcm