1 /*=========================================================================
4 Module: $RCSfile: gdcmElementSet.cxx,v $
6 Date: $Date: 2008/05/19 09:25:42 $
7 Version: $Revision: 1.80 $
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;
56 (void)dummy2;(void)dummy;
58 for (TagDocEntryHT::const_iterator i = TagHT.begin();
62 int group = (i->second)->GetGroup();
64 if (yetOutsideMetaElements==false && group == 0x0002)
65 insideMetaElements = true;
67 if (insideMetaElements == true && group != 0x0002)
69 yetOutsideMetaElements = true;
70 insideMetaElements = false;
73 // depending on the gdcm::Document type
74 // (gdcm::File; gdcm::DicomDir, (more to come ?)
75 // some groups *cannot* be present.
76 // We hereby protect gdcm for writting stupid things
77 // if they were found in the original document.
78 if ( !MayIWrite( group ) )
81 // Skip 'Group Length' element, since it may be wrong.
82 // except for Group 0x0002
83 // ( keep it as well for Group 0x0008 of ACR Files,
84 // since some ACR readers *need* it )
86 if ( (i->second)->GetElement() != 0x0000
88 ( (i->second)->GetGroup() == 0x0002
89 ||( (filetype == ACR || filetype == ACR_LIBIDO ) && (i->second)->GetGroup() == 0x0008 ) )
92 // There are DocEntries, written recursively
93 // false : we are outside any Sequence
94 i->second->WriteContent(fp, filetype, insideMetaElements, false );
100 * \brief add a new Dicom Element pointer to the H Table
101 * @param newEntry entry to add
103 bool ElementSet::AddEntry(DocEntry *newEntry)
105 const TagKey &key = newEntry->GetKey();
107 if ( TagHT.count(key) == 1 )
109 gdcmWarningMacro( "Key already present: " << key );
114 TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
115 newEntry->Register();
121 * \brief Clear the hash table from given entry AND delete the entry.
122 * @param entryToRemove Entry to remove AND delete.
124 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
126 const TagKey &key = entryToRemove->GetKey();
127 if ( TagHT.count(key) == 1 )
130 entryToRemove->Unregister();
134 gdcmWarningMacro( "Key not present : " << key);
139 * \brief delete all entries in the ElementSet
141 void ElementSet::ClearEntry()
143 for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
147 cc->second->Unregister();
154 * \brief Get the first entry while visiting *the* 'zero level' DocEntrySet
155 * (DocEntries out of any Sequence)
156 * \return The first DocEntry if found, otherwhise NULL
158 DocEntry *ElementSet::GetFirstEntry()
160 ItTagHT = TagHT.begin();
161 if (ItTagHT != TagHT.end())
162 return ItTagHT->second;
167 * \brief Get the next entry while visiting *the* 'zero level' DocEntrySet
168 * (DocEntries out of any Sequence)
169 * \note : meaningfull only if GetFirstEntry already called
170 * \return The next DocEntry if found, otherwhise NULL
172 DocEntry *ElementSet::GetNextEntry()
174 gdcmAssertMacro (ItTagHT != TagHT.end());
177 if (ItTagHT != TagHT.end())
178 return ItTagHT->second;
183 * \brief retrieves a Dicom Element using (group, element)
184 * @param group Group number of the searched Dicom Element
185 * @param elem Element number of the searched Dicom Element
188 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem)
190 TagKey key = DictEntry::TranslateToKey(group, elem);
191 TagDocEntryHT::iterator it = TagHT.find(key);
193 if ( it!=TagHT.end() )
199 * \brief Copies all the attributes from an other DocEntrySet
200 * @param set entry to copy from
201 * @remarks The contained DocEntries a not copied, only referenced
203 void ElementSet::Copy(DocEntrySet *set)
205 // Remove all previous entries
208 DocEntrySet::Copy(set);
210 ElementSet *eltSet = dynamic_cast<ElementSet *>(set);
213 TagHT = eltSet->TagHT;
214 for(ItTagHT = TagHT.begin();ItTagHT != TagHT.end();++ItTagHT)
216 (ItTagHT->second)->Register();
222 * \brief Checks whether *all* the DataEntries of the group have all
223 * the same type for VR (ImplicitVR or ExplicitVR)
224 * @param group group number to be checked
225 * @return 1:ImplicitVR 2:ExplicitVR -1:NotCoherent
227 int ElementSet::IsVRCoherent( uint16_t group )
229 uint16_t currentGroup;
232 for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
234 currentGroup = cc->second->GetGroup();
236 if ( currentGroup < group )
238 if ( currentGroup > group )
240 // currentGroup == group
243 if (cc->second->IsImplicitVR() )
251 if (cc->second->IsImplicitVR() )
252 currentCodeVR = 1; //Implicit
254 currentCodeVR = 2; // Explicit
256 if ( currentCodeVR == codeVR )
259 return -1; // -1 : not coherent
266 //-----------------------------------------------------------------------------
269 //-----------------------------------------------------------------------------
272 //-----------------------------------------------------------------------------
275 * \brief Prints the Header Entries (Dicom Elements) from the H Table
276 * @param os ostream to write to
277 * @param indent Indentation string to be prepended during printing
279 void ElementSet::Print(std::ostream &os, std::string const & )
281 // Let's change the 'warning value' for Pixel Data,
282 // to avoid human reader to be confused by 'gdcm::NotLoaded'.
283 DataEntry *pixelElement = GetDataEntry(0x7fe0,0x0010);
284 if ( pixelElement != 0 )
286 pixelElement->SetFlag( DataEntry::FLAG_PIXELDATA );
289 for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
291 DocEntry *entry = i->second;
293 entry->SetPrintLevel(PrintLevel);
296 if ( dynamic_cast<SeqEntry*>(entry) )
298 // Avoid the newline for a sequence:
305 //-----------------------------------------------------------------------------
306 } // end namespace gdcm