1 /*=========================================================================
4 Module: $RCSfile: gdcmElementSet.cxx,v $
6 Date: $Date: 2007/05/23 14:18:10 $
7 Version: $Revision: 1.76 $
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"
24 namespace GDCM_NAME_SPACE
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 ExplicitVR/ImplicitVR/ACR/ACR_LIBIDO/JPEG/JPEG2000/...
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() ) )
66 // Skip 'Group Length' element, since it may be wrong.
67 // except for Group 0x0002
68 // ( keep it as well for Group 0x0008 of ACR Files,
69 // since some ACR readers *need* it )
71 if ( (i->second)->GetElement() != 0x0000
73 ( (i->second)->GetGroup() == 0x0002
74 ||( (filetype == ACR || filetype == ACR_LIBIDO ) && (i->second)->GetGroup() == 0x0008 ) )
77 i->second->WriteContent(fp, filetype);
83 * \brief add a new Dicom Element pointer to the H Table
84 * @param newEntry entry to add
86 bool ElementSet::AddEntry(DocEntry *newEntry)
88 const TagKey &key = newEntry->GetKey();
90 if ( TagHT.count(key) == 1 )
92 gdcmWarningMacro( "Key already present: " << key );
97 TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
104 * \brief Clear the hash table from given entry AND delete the entry.
105 * @param entryToRemove Entry to remove AND delete.
107 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
109 const TagKey &key = entryToRemove->GetKey();
110 if ( TagHT.count(key) == 1 )
113 entryToRemove->Unregister();
117 gdcmWarningMacro( "Key not present : " << key);
122 * \brief delete all entries in the ElementSet
124 void ElementSet::ClearEntry()
126 for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
130 cc->second->Unregister();
137 * \brief Get the first entry while visiting *the* 'zero level' DocEntrySet
138 * (DocEntries out of any Sequence)
139 * \return The first DocEntry if found, otherwhise NULL
141 DocEntry *ElementSet::GetFirstEntry()
143 ItTagHT = TagHT.begin();
144 if (ItTagHT != TagHT.end())
145 return ItTagHT->second;
150 * \brief Get the next entry while visiting *the* 'zero level' DocEntrySet
151 * (DocEntries out of any Sequence)
152 * \note : meaningfull only if GetFirstEntry already called
153 * \return The next DocEntry if found, otherwhise NULL
155 DocEntry *ElementSet::GetNextEntry()
157 gdcmAssertMacro (ItTagHT != TagHT.end());
160 if (ItTagHT != TagHT.end())
161 return ItTagHT->second;
166 * \brief retrieves a Dicom Element using (group, element)
167 * @param group Group number of the searched Dicom Element
168 * @param elem Element number of the searched Dicom Element
171 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem)
173 TagKey key = DictEntry::TranslateToKey(group, elem);
174 TagDocEntryHT::iterator it = TagHT.find(key);
176 if ( it!=TagHT.end() )
182 * \brief Copies all the attributes from an other DocEntrySet
183 * @param set entry to copy from
184 * @remarks The contained DocEntries a not copied, only referenced
186 void ElementSet::Copy(DocEntrySet *set)
188 // Remove all previous entries
191 DocEntrySet::Copy(set);
193 ElementSet *eltSet = dynamic_cast<ElementSet *>(set);
196 TagHT = eltSet->TagHT;
197 for(ItTagHT = TagHT.begin();ItTagHT != TagHT.end();++ItTagHT)
199 (ItTagHT->second)->Register();
205 * \brief Checks whether *all* the DataEntries of the group have all
206 * the same type for VR (ImplicitVR or ExplicitVR)
207 * @param group group number to be checked
208 * @return 1:ImplicitVR 2:ExplicitVR -1:NotCoherent
210 int ElementSet::IsVRCoherent( uint16_t group )
212 uint16_t currentGroup;
215 for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
217 currentGroup = cc->second->GetGroup();
219 if ( currentGroup < group )
221 if ( currentGroup > group )
223 // currentGroup == group
226 if (cc->second->IsImplicitVR() )
234 if (cc->second->IsImplicitVR() )
235 currentCodeVR = 1; //Implicit
237 currentCodeVR = 2; // Explicit
239 if ( currentCodeVR == codeVR )
242 return -1; // -1 : not coherent
249 //-----------------------------------------------------------------------------
252 //-----------------------------------------------------------------------------
255 //-----------------------------------------------------------------------------
258 * \brief Prints the Header Entries (Dicom Elements) from the H Table
259 * @param os ostream to write to
260 * @param indent Indentation string to be prepended during printing
262 void ElementSet::Print(std::ostream &os, std::string const & )
264 // Let's change the 'warning value' for Pixel Data,
265 // to avoid human reader to be confused by 'gdcm::NotLoaded'.
266 DataEntry *pixelElement = GetDataEntry(0x7fe0,0x0010);
267 if ( pixelElement != 0 )
269 pixelElement->SetFlag( DataEntry::FLAG_PIXELDATA );
272 for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
274 DocEntry *entry = i->second;
276 entry->SetPrintLevel(PrintLevel);
279 if ( dynamic_cast<SeqEntry*>(entry) )
281 // Avoid the newline for a sequence:
288 //-----------------------------------------------------------------------------
289 } // end namespace gdcm