1 /*=========================================================================
4 Module: $RCSfile: gdcmElementSet.cxx,v $
6 Date: $Date: 2007/07/26 08:36:49 $
7 Version: $Revision: 1.77 $
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, bool dummy)
54 bool insideMetaElements = false;
55 bool yetOutsideMetaElements = false;
57 for (TagDocEntryHT::const_iterator i = TagHT.begin();
61 int group = (i->second)->GetGroup();
63 if (yetOutsideMetaElements==false && group == 0x0002)
64 insideMetaElements = true;
66 if (insideMetaElements == true && group != 0x0002)
68 yetOutsideMetaElements = true;
69 insideMetaElements = false;
72 // depending on the gdcm::Document type
73 // (gdcm::File; gdcm::DicomDir, (more to come ?)
74 // some groups *cannot* be present.
75 // We hereby protect gdcm for writting stupid things
76 // if they were found in the original document.
77 if ( !MayIWrite( group ) )
80 // Skip 'Group Length' element, since it may be wrong.
81 // except for Group 0x0002
82 // ( keep it as well for Group 0x0008 of ACR Files,
83 // since some ACR readers *need* it )
85 if ( (i->second)->GetElement() != 0x0000
87 ( (i->second)->GetGroup() == 0x0002
88 ||( (filetype == ACR || filetype == ACR_LIBIDO ) && (i->second)->GetGroup() == 0x0008 ) )
91 // There are DocEntries, written recursively
92 i->second->WriteContent(fp, filetype, insideMetaElements );
98 * \brief add a new Dicom Element pointer to the H Table
99 * @param newEntry entry to add
101 bool ElementSet::AddEntry(DocEntry *newEntry)
103 const TagKey &key = newEntry->GetKey();
105 if ( TagHT.count(key) == 1 )
107 gdcmWarningMacro( "Key already present: " << key );
112 TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
113 newEntry->Register();
119 * \brief Clear the hash table from given entry AND delete the entry.
120 * @param entryToRemove Entry to remove AND delete.
122 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
124 const TagKey &key = entryToRemove->GetKey();
125 if ( TagHT.count(key) == 1 )
128 entryToRemove->Unregister();
132 gdcmWarningMacro( "Key not present : " << key);
137 * \brief delete all entries in the ElementSet
139 void ElementSet::ClearEntry()
141 for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
145 cc->second->Unregister();
152 * \brief Get the first entry while visiting *the* 'zero level' DocEntrySet
153 * (DocEntries out of any Sequence)
154 * \return The first DocEntry if found, otherwhise NULL
156 DocEntry *ElementSet::GetFirstEntry()
158 ItTagHT = TagHT.begin();
159 if (ItTagHT != TagHT.end())
160 return ItTagHT->second;
165 * \brief Get the next entry while visiting *the* 'zero level' DocEntrySet
166 * (DocEntries out of any Sequence)
167 * \note : meaningfull only if GetFirstEntry already called
168 * \return The next DocEntry if found, otherwhise NULL
170 DocEntry *ElementSet::GetNextEntry()
172 gdcmAssertMacro (ItTagHT != TagHT.end());
175 if (ItTagHT != TagHT.end())
176 return ItTagHT->second;
181 * \brief retrieves a Dicom Element using (group, element)
182 * @param group Group number of the searched Dicom Element
183 * @param elem Element number of the searched Dicom Element
186 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem)
188 TagKey key = DictEntry::TranslateToKey(group, elem);
189 TagDocEntryHT::iterator it = TagHT.find(key);
191 if ( it!=TagHT.end() )
197 * \brief Copies all the attributes from an other DocEntrySet
198 * @param set entry to copy from
199 * @remarks The contained DocEntries a not copied, only referenced
201 void ElementSet::Copy(DocEntrySet *set)
203 // Remove all previous entries
206 DocEntrySet::Copy(set);
208 ElementSet *eltSet = dynamic_cast<ElementSet *>(set);
211 TagHT = eltSet->TagHT;
212 for(ItTagHT = TagHT.begin();ItTagHT != TagHT.end();++ItTagHT)
214 (ItTagHT->second)->Register();
220 * \brief Checks whether *all* the DataEntries of the group have all
221 * the same type for VR (ImplicitVR or ExplicitVR)
222 * @param group group number to be checked
223 * @return 1:ImplicitVR 2:ExplicitVR -1:NotCoherent
225 int ElementSet::IsVRCoherent( uint16_t group )
227 uint16_t currentGroup;
230 for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
232 currentGroup = cc->second->GetGroup();
234 if ( currentGroup < group )
236 if ( currentGroup > group )
238 // currentGroup == group
241 if (cc->second->IsImplicitVR() )
249 if (cc->second->IsImplicitVR() )
250 currentCodeVR = 1; //Implicit
252 currentCodeVR = 2; // Explicit
254 if ( currentCodeVR == codeVR )
257 return -1; // -1 : not coherent
264 //-----------------------------------------------------------------------------
267 //-----------------------------------------------------------------------------
270 //-----------------------------------------------------------------------------
273 * \brief Prints the Header Entries (Dicom Elements) from the H Table
274 * @param os ostream to write to
275 * @param indent Indentation string to be prepended during printing
277 void ElementSet::Print(std::ostream &os, std::string const & )
279 // Let's change the 'warning value' for Pixel Data,
280 // to avoid human reader to be confused by 'gdcm::NotLoaded'.
281 DataEntry *pixelElement = GetDataEntry(0x7fe0,0x0010);
282 if ( pixelElement != 0 )
284 pixelElement->SetFlag( DataEntry::FLAG_PIXELDATA );
287 for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
289 DocEntry *entry = i->second;
291 entry->SetPrintLevel(PrintLevel);
294 if ( dynamic_cast<SeqEntry*>(entry) )
296 // Avoid the newline for a sequence:
303 //-----------------------------------------------------------------------------
304 } // end namespace gdcm