00001 /*========================================================================= 00002 00003 Program: gdcm 00004 Module: $RCSfile: gdcmElementSet.cxx,v $ 00005 Language: C++ 00006 Date: $Date: 2005/11/29 12:48:47 $ 00007 Version: $Revision: 1.71 $ 00008 00009 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de 00010 l'Image). All rights reserved. See Doc/License.txt or 00011 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details. 00012 00013 This software is distributed WITHOUT ANY WARRANTY; without even 00014 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00015 PURPOSE. See the above copyright notices for more information. 00016 00017 =========================================================================*/ 00018 00019 #include "gdcmElementSet.h" 00020 #include "gdcmDebug.h" 00021 #include "gdcmSeqEntry.h" 00022 #include "gdcmDataEntry.h" 00023 00024 namespace gdcm 00025 { 00026 //----------------------------------------------------------------------------- 00027 // Constructor / Destructor 00031 ElementSet::ElementSet() 00032 : DocEntrySet() 00033 { 00034 } 00035 00039 ElementSet::~ElementSet() 00040 { 00041 ClearEntry(); 00042 } 00043 00044 //----------------------------------------------------------------------------- 00045 // Public 00052 void ElementSet::WriteContent(std::ofstream *fp, FileType filetype) 00053 { 00054 for (TagDocEntryHT::const_iterator i = TagHT.begin(); 00055 i != TagHT.end(); 00056 ++i) 00057 { 00058 // depending on the gdcm::Document type 00059 // (gdcm::File; gdcm::DicomDir, (more to come ?) 00060 // some groups *cannot* be present. 00061 // We hereby protect gdcm for writting stupid things 00062 // if they were found in the original document. 00063 if ( !MayIWrite( (i->second)->GetGroup() ) ) 00064 continue; 00065 // Skip 'Group Length' element, since it may be wrong. 00066 // except for Group 0002 00067 if ( (i->second)->GetElement() == 0x0000 00068 && (i->second)->GetGroup() != 0x0002 ) 00069 continue; 00070 i->second->WriteContent(fp, filetype); 00071 } 00072 } 00073 00078 bool ElementSet::AddEntry(DocEntry *newEntry) 00079 { 00080 const TagKey &key = newEntry->GetKey(); 00081 00082 if ( TagHT.count(key) == 1 ) 00083 { 00084 gdcmWarningMacro( "Key already present: " << key ); 00085 return false; 00086 } 00087 else 00088 { 00089 TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry)); 00090 newEntry->Register(); 00091 return true; 00092 } 00093 } 00094 00099 bool ElementSet::RemoveEntry( DocEntry *entryToRemove) 00100 { 00101 const TagKey &key = entryToRemove->GetKey(); 00102 if ( TagHT.count(key) == 1 ) 00103 { 00104 TagHT.erase(key); 00105 entryToRemove->Unregister(); 00106 return true; 00107 } 00108 00109 gdcmWarningMacro( "Key not present : " << key); 00110 return false ; 00111 } 00112 00116 void ElementSet::ClearEntry() 00117 { 00118 for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc) 00119 { 00120 if ( cc->second ) 00121 { 00122 cc->second->Unregister(); 00123 } 00124 } 00125 TagHT.clear(); 00126 } 00127 00133 DocEntry *ElementSet::GetFirstEntry() 00134 { 00135 ItTagHT = TagHT.begin(); 00136 if (ItTagHT != TagHT.end()) 00137 return ItTagHT->second; 00138 return NULL; 00139 } 00140 00147 DocEntry *ElementSet::GetNextEntry() 00148 { 00149 gdcmAssertMacro (ItTagHT != TagHT.end()); 00150 00151 ++ItTagHT; 00152 if (ItTagHT != TagHT.end()) 00153 return ItTagHT->second; 00154 return NULL; 00155 } 00156 00163 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem) 00164 { 00165 TagKey key = DictEntry::TranslateToKey(group, elem); 00166 TagDocEntryHT::iterator it = TagHT.find(key); 00167 00168 if ( it!=TagHT.end() ) 00169 return it->second; 00170 return NULL; 00171 } 00172 00178 void ElementSet::Copy(DocEntrySet *set) 00179 { 00180 // Remove all previous entries 00181 ClearEntry(); 00182 00183 DocEntrySet::Copy(set); 00184 00185 ElementSet *eltSet = dynamic_cast<ElementSet *>(set); 00186 if( eltSet ) 00187 { 00188 TagHT = eltSet->TagHT; 00189 for(ItTagHT = TagHT.begin();ItTagHT != TagHT.end();++ItTagHT) 00190 { 00191 (ItTagHT->second)->Register(); 00192 } 00193 } 00194 } 00195 00196 //----------------------------------------------------------------------------- 00197 // Protected 00198 00199 //----------------------------------------------------------------------------- 00200 // Private 00201 00202 //----------------------------------------------------------------------------- 00203 // Print 00209 void ElementSet::Print(std::ostream &os, std::string const & ) 00210 { 00211 // Let's change the 'warning value' for Pixel Data, 00212 // to avoid human reader to be confused by 'gdcm::NotLoaded'. 00213 DataEntry *pixelElement = GetDataEntry(0x7fe0,0x0010); 00214 if ( pixelElement != 0 ) 00215 { 00216 pixelElement->SetFlag( DataEntry::FLAG_PIXELDATA ); 00217 } 00218 00219 for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i) 00220 { 00221 DocEntry *entry = i->second; 00222 00223 entry->SetPrintLevel(PrintLevel); 00224 entry->Print(os); 00225 00226 if ( dynamic_cast<SeqEntry *>(entry) ) 00227 { 00228 // Avoid the newline for a sequence: 00229 continue; 00230 } 00231 os << std::endl; 00232 } 00233 } 00234 00235 //----------------------------------------------------------------------------- 00236 } // end namespace gdcm