]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
* Add the Copy method in all datas
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/29 12:48:47 $
7   Version:   $Revision: 1.71 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmElementSet.h"
20 #include "gdcmDebug.h"
21 #include "gdcmSeqEntry.h"
22 #include "gdcmDataEntry.h"
23
24 namespace gdcm 
25 {
26 //-----------------------------------------------------------------------------
27 // Constructor / Destructor
28 /**
29  * \brief   Constructor for a given ElementSet
30  */
31 ElementSet::ElementSet() 
32           : DocEntrySet()
33 {
34 }
35
36 /**
37  * \brief   Canonical destructor.
38  */
39 ElementSet::~ElementSet() 
40 {
41    ClearEntry();
42 }
43
44 //-----------------------------------------------------------------------------
45 // Public
46 /**
47   * \brief   Writes the Header Entries (Dicom Elements)
48   *          from the H Table
49   * @param fp ofstream to write to  
50   * @param filetype filetype
51   */ 
52 void ElementSet::WriteContent(std::ofstream *fp, FileType filetype)
53 {
54    for (TagDocEntryHT::const_iterator i = TagHT.begin(); 
55                                      i != TagHT.end(); 
56                                     ++i)
57    { 
58        // depending on the gdcm::Document type 
59        // (gdcm::File; gdcm::DicomDir, (more to come ?)
60        // some groups *cannot* be present.
61        // We hereby protect gdcm for writting stupid things
62        // if they were found in the original document. 
63        if ( !MayIWrite( (i->second)->GetGroup() ) ) 
64           continue;
65       // Skip 'Group Length' element, since it may be wrong.
66       //       except for Group 0002 
67        if ( (i->second)->GetElement() == 0x0000 
68          && (i->second)->GetGroup() != 0x0002 )
69           continue; 
70        i->second->WriteContent(fp, filetype);
71    } 
72 }
73
74 /**
75  * \brief   add a new Dicom Element pointer to the H Table
76  * @param   newEntry entry to add
77  */
78 bool ElementSet::AddEntry(DocEntry *newEntry)
79 {
80    const TagKey &key = newEntry->GetKey();
81
82    if ( TagHT.count(key) == 1 )
83    {
84       gdcmWarningMacro( "Key already present: " << key );
85       return false;
86    }
87    else
88    {
89       TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
90       newEntry->Register();
91       return true;
92    }
93 }
94
95 /**
96  * \brief   Clear the hash table from given entry AND delete the entry.
97  * @param   entryToRemove Entry to remove AND delete.
98  */
99 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
100 {
101    const TagKey &key = entryToRemove->GetKey();
102    if ( TagHT.count(key) == 1 )
103    {
104       TagHT.erase(key);
105       entryToRemove->Unregister();
106       return true;
107    }
108
109    gdcmWarningMacro( "Key not present : " << key);
110    return false ;
111 }
112
113 /**
114  * \brief   delete all entries in the ElementSet
115  */
116 void ElementSet::ClearEntry()
117 {
118    for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
119    {
120       if ( cc->second )
121       {
122          cc->second->Unregister();
123       }
124    }
125    TagHT.clear();
126 }
127
128 /**
129  * \brief   Get the first entry while visiting *the* 'zero level' DocEntrySet
130  *              (DocEntries out of any Sequence)
131  * \return  The first DocEntry if found, otherwhise NULL
132  */
133 DocEntry *ElementSet::GetFirstEntry()
134 {
135    ItTagHT = TagHT.begin();
136    if (ItTagHT != TagHT.end())
137       return  ItTagHT->second;
138    return NULL;
139 }
140
141 /**
142  * \brief   Get the next entry while visiting *the* 'zero level' DocEntrySet
143  *              (DocEntries out of any Sequence) 
144  * \note : meaningfull only if GetFirstEntry already called 
145  * \return  The next DocEntry if found, otherwhise NULL
146  */
147 DocEntry *ElementSet::GetNextEntry()
148 {
149    gdcmAssertMacro (ItTagHT != TagHT.end());
150
151    ++ItTagHT;
152    if (ItTagHT != TagHT.end())
153       return  ItTagHT->second;
154    return NULL;
155 }
156
157 /**
158  * \brief  retrieves a Dicom Element using (group, element)
159  * @param   group  Group number of the searched Dicom Element 
160  * @param   elem Element number of the searched Dicom Element 
161  * @return  
162  */
163 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem) 
164 {
165    TagKey key = DictEntry::TranslateToKey(group, elem);
166    TagDocEntryHT::iterator it = TagHT.find(key);
167
168    if ( it!=TagHT.end() )
169       return it->second;
170    return NULL;
171 }
172
173 /**
174  * \brief Copies all the attributes from an other DocEntrySet 
175  * @param set entry to copy from
176  * @remarks The contained DocEntries a not copied, only referenced
177  */
178 void ElementSet::Copy(DocEntrySet *set)
179 {
180    // Remove all previous entries
181    ClearEntry();
182
183    DocEntrySet::Copy(set);
184
185    ElementSet *eltSet = dynamic_cast<ElementSet *>(set);
186    if( eltSet )
187    {
188       TagHT = eltSet->TagHT;
189       for(ItTagHT = TagHT.begin();ItTagHT != TagHT.end();++ItTagHT)
190       {
191          (ItTagHT->second)->Register();
192       }
193    }
194 }
195
196 //-----------------------------------------------------------------------------
197 // Protected
198
199 //-----------------------------------------------------------------------------
200 // Private
201
202 //-----------------------------------------------------------------------------
203 // Print
204 /**
205   * \brief   Prints the Header Entries (Dicom Elements) from the H Table
206   * @param os ostream to write to  
207   * @param indent Indentation string to be prepended during printing
208   */ 
209 void ElementSet::Print(std::ostream &os, std::string const & )
210 {
211    // Let's change the 'warning value' for Pixel Data,
212    // to avoid human reader to be confused by 'gdcm::NotLoaded'.   
213    DataEntry *pixelElement = GetDataEntry(0x7fe0,0x0010);
214    if ( pixelElement != 0 )
215    {
216       pixelElement->SetFlag( DataEntry::FLAG_PIXELDATA );
217    }
218
219    for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
220    {
221       DocEntry *entry = i->second;
222
223       entry->SetPrintLevel(PrintLevel);
224       entry->Print(os);   
225
226       if ( dynamic_cast<SeqEntry *>(entry) )
227       {
228          // Avoid the newline for a sequence:
229          continue;
230       }
231       os << std::endl;
232    }
233 }
234
235 //-----------------------------------------------------------------------------
236 } // end namespace gdcm