1 /*=========================================================================
4 Module: $RCSfile: gdcmElementSet.cxx,v $
6 Date: $Date: 2007/10/19 14:47:18 $
7 Version: $Revision: 1.79 $
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, bool dummy2)
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 // false : we are outside any Sequence
93 i->second->WriteContent(fp, filetype, insideMetaElements, false );
99 * \brief add a new Dicom Element pointer to the H Table
100 * @param newEntry entry to add
102 bool ElementSet::AddEntry(DocEntry *newEntry)
104 const TagKey &key = newEntry->GetKey();
106 if ( TagHT.count(key) == 1 )
108 gdcmWarningMacro( "Key already present: " << key );
113 TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
114 newEntry->Register();
120 * \brief Clear the hash table from given entry AND delete the entry.
121 * @param entryToRemove Entry to remove AND delete.
123 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
125 const TagKey &key = entryToRemove->GetKey();
126 if ( TagHT.count(key) == 1 )
129 entryToRemove->Unregister();
133 gdcmWarningMacro( "Key not present : " << key);
138 * \brief delete all entries in the ElementSet
140 void ElementSet::ClearEntry()
142 for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
146 cc->second->Unregister();
153 * \brief Get the first entry while visiting *the* 'zero level' DocEntrySet
154 * (DocEntries out of any Sequence)
155 * \return The first DocEntry if found, otherwhise NULL
157 DocEntry *ElementSet::GetFirstEntry()
159 ItTagHT = TagHT.begin();
160 if (ItTagHT != TagHT.end())
161 return ItTagHT->second;
166 * \brief Get the next entry while visiting *the* 'zero level' DocEntrySet
167 * (DocEntries out of any Sequence)
168 * \note : meaningfull only if GetFirstEntry already called
169 * \return The next DocEntry if found, otherwhise NULL
171 DocEntry *ElementSet::GetNextEntry()
173 gdcmAssertMacro (ItTagHT != TagHT.end());
176 if (ItTagHT != TagHT.end())
177 return ItTagHT->second;
182 * \brief retrieves a Dicom Element using (group, element)
183 * @param group Group number of the searched Dicom Element
184 * @param elem Element number of the searched Dicom Element
187 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem)
189 TagKey key = DictEntry::TranslateToKey(group, elem);
190 TagDocEntryHT::iterator it = TagHT.find(key);
192 if ( it!=TagHT.end() )
198 * \brief Copies all the attributes from an other DocEntrySet
199 * @param set entry to copy from
200 * @remarks The contained DocEntries a not copied, only referenced
202 void ElementSet::Copy(DocEntrySet *set)
204 // Remove all previous entries
207 DocEntrySet::Copy(set);
209 ElementSet *eltSet = dynamic_cast<ElementSet *>(set);
212 TagHT = eltSet->TagHT;
213 for(ItTagHT = TagHT.begin();ItTagHT != TagHT.end();++ItTagHT)
215 (ItTagHT->second)->Register();
221 * \brief Checks whether *all* the DataEntries of the group have all
222 * the same type for VR (ImplicitVR or ExplicitVR)
223 * @param group group number to be checked
224 * @return 1:ImplicitVR 2:ExplicitVR -1:NotCoherent
226 int ElementSet::IsVRCoherent( uint16_t group )
228 uint16_t currentGroup;
231 for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
233 currentGroup = cc->second->GetGroup();
235 if ( currentGroup < group )
237 if ( currentGroup > group )
239 // currentGroup == group
242 if (cc->second->IsImplicitVR() )
250 if (cc->second->IsImplicitVR() )
251 currentCodeVR = 1; //Implicit
253 currentCodeVR = 2; // Explicit
255 if ( currentCodeVR == codeVR )
258 return -1; // -1 : not coherent
265 //-----------------------------------------------------------------------------
268 //-----------------------------------------------------------------------------
271 //-----------------------------------------------------------------------------
274 * \brief Prints the Header Entries (Dicom Elements) from the H Table
275 * @param os ostream to write to
276 * @param indent Indentation string to be prepended during printing
278 void ElementSet::Print(std::ostream &os, std::string const & )
280 // Let's change the 'warning value' for Pixel Data,
281 // to avoid human reader to be confused by 'gdcm::NotLoaded'.
282 DataEntry *pixelElement = GetDataEntry(0x7fe0,0x0010);
283 if ( pixelElement != 0 )
285 pixelElement->SetFlag( DataEntry::FLAG_PIXELDATA );
288 for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
290 DocEntry *entry = i->second;
292 entry->SetPrintLevel(PrintLevel);
295 if ( dynamic_cast<SeqEntry*>(entry) )
297 // Avoid the newline for a sequence:
304 //-----------------------------------------------------------------------------
305 } // end namespace gdcm