00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "gdcmElementSet.h"
00020 #include "gdcmDebug.h"
00021 #include "gdcmSeqEntry.h"
00022 #include "gdcmDataEntry.h"
00023
00024 namespace GDCM_NAME_SPACE
00025 {
00026
00027
00031 ElementSet::ElementSet()
00032 : DocEntrySet()
00033 {
00034 }
00035
00039 ElementSet::~ElementSet()
00040 {
00041 ClearEntry();
00042 }
00043
00044
00045
00052 void ElementSet::WriteContent(std::ofstream *fp, FileType filetype, bool dummy)
00053 {
00054 bool insideMetaElements = false;
00055 bool yetOutsideMetaElements = false;
00056
00057 for (TagDocEntryHT::const_iterator i = TagHT.begin();
00058 i != TagHT.end();
00059 ++i)
00060 {
00061 int group = (i->second)->GetGroup();
00062
00063 if (yetOutsideMetaElements==false && group == 0x0002)
00064 insideMetaElements = true;
00065
00066 if (insideMetaElements == true && group != 0x0002)
00067 {
00068 yetOutsideMetaElements = true;
00069 insideMetaElements = false;
00070 }
00071
00072
00073
00074
00075
00076
00077 if ( !MayIWrite( group ) )
00078 continue;
00079
00080
00081
00082
00083
00084
00085 if ( (i->second)->GetElement() != 0x0000
00086 ||
00087 ( (i->second)->GetGroup() == 0x0002
00088 ||( (filetype == ACR || filetype == ACR_LIBIDO ) && (i->second)->GetGroup() == 0x0008 ) )
00089 )
00090 {
00091
00092 i->second->WriteContent(fp, filetype, insideMetaElements );
00093 }
00094 }
00095 }
00096
00101 bool ElementSet::AddEntry(DocEntry *newEntry)
00102 {
00103 const TagKey &key = newEntry->GetKey();
00104
00105 if ( TagHT.count(key) == 1 )
00106 {
00107 gdcmWarningMacro( "Key already present: " << key );
00108 return false;
00109 }
00110 else
00111 {
00112 TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
00113 newEntry->Register();
00114 return true;
00115 }
00116 }
00117
00122 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
00123 {
00124 const TagKey &key = entryToRemove->GetKey();
00125 if ( TagHT.count(key) == 1 )
00126 {
00127 TagHT.erase(key);
00128 entryToRemove->Unregister();
00129 return true;
00130 }
00131
00132 gdcmWarningMacro( "Key not present : " << key);
00133 return false ;
00134 }
00135
00139 void ElementSet::ClearEntry()
00140 {
00141 for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
00142 {
00143 if ( cc->second )
00144 {
00145 cc->second->Unregister();
00146 }
00147 }
00148 TagHT.clear();
00149 }
00150
00156 DocEntry *ElementSet::GetFirstEntry()
00157 {
00158 ItTagHT = TagHT.begin();
00159 if (ItTagHT != TagHT.end())
00160 return ItTagHT->second;
00161 return NULL;
00162 }
00163
00170 DocEntry *ElementSet::GetNextEntry()
00171 {
00172 gdcmAssertMacro (ItTagHT != TagHT.end());
00173
00174 ++ItTagHT;
00175 if (ItTagHT != TagHT.end())
00176 return ItTagHT->second;
00177 return NULL;
00178 }
00179
00186 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem)
00187 {
00188 TagKey key = DictEntry::TranslateToKey(group, elem);
00189 TagDocEntryHT::iterator it = TagHT.find(key);
00190
00191 if ( it!=TagHT.end() )
00192 return it->second;
00193 return NULL;
00194 }
00195
00201 void ElementSet::Copy(DocEntrySet *set)
00202 {
00203
00204 ClearEntry();
00205
00206 DocEntrySet::Copy(set);
00207
00208 ElementSet *eltSet = dynamic_cast<ElementSet *>(set);
00209 if( eltSet )
00210 {
00211 TagHT = eltSet->TagHT;
00212 for(ItTagHT = TagHT.begin();ItTagHT != TagHT.end();++ItTagHT)
00213 {
00214 (ItTagHT->second)->Register();
00215 }
00216 }
00217 }
00218
00225 int ElementSet::IsVRCoherent( uint16_t group )
00226 {
00227 uint16_t currentGroup;
00228 int codeVR = -1;
00229 int currentCodeVR;
00230 for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
00231 {
00232 currentGroup = cc->second->GetGroup();
00233
00234 if ( currentGroup < group )
00235 continue;
00236 if ( currentGroup > group )
00237 break;
00238
00239 if (codeVR == -1)
00240 {
00241 if (cc->second->IsImplicitVR() )
00242 codeVR = 1;
00243 else
00244 codeVR = 2;
00245 continue;
00246 }
00247 else
00248 {
00249 if (cc->second->IsImplicitVR() )
00250 currentCodeVR = 1;
00251 else
00252 currentCodeVR = 2;
00253
00254 if ( currentCodeVR == codeVR )
00255 continue;
00256 else
00257 return -1;
00258 }
00259 }
00260 return codeVR;
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00277 void ElementSet::Print(std::ostream &os, std::string const & )
00278 {
00279
00280
00281 DataEntry *pixelElement = GetDataEntry(0x7fe0,0x0010);
00282 if ( pixelElement != 0 )
00283 {
00284 pixelElement->SetFlag( DataEntry::FLAG_PIXELDATA );
00285 }
00286
00287 for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
00288 {
00289 DocEntry *entry = i->second;
00290
00291 entry->SetPrintLevel(PrintLevel);
00292 entry->Print(os);
00293
00294 if ( dynamic_cast<SeqEntry*>(entry) )
00295 {
00296
00297 continue;
00298 }
00299 os << std::endl;
00300 }
00301 }
00302
00303
00304 }