]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
Typo
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/03/23 15:05:11 $
7   Version:   $Revision: 1.75 $
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    ExplicitVR/ImplicitVR/ACR/ACR_LIBIDO/JPEG/JPEG2000/...
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   
66       // Skip 'Group Length' element, since it may be wrong.
67       //       except for Group 0x0002
68       // ( keep it as well for Group 0x0008 of ACR Files, 
69       //  since some ACR readers *need* it )
70       
71        if ( (i->second)->GetElement() != 0x0000 
72            || 
73             (  (i->second)->GetGroup() == 0x0002 
74              ||( (filetype == ACR || filetype == ACR_LIBIDO ) && (i->second)->GetGroup() == 0x0008 ) )
75         )
76        {
77              i->second->WriteContent(fp, filetype);
78        }             
79    } 
80 }
81
82 /**
83  * \brief   add a new Dicom Element pointer to the H Table
84  * @param   newEntry entry to add
85  */
86 bool ElementSet::AddEntry(DocEntry *newEntry)
87 {
88    const TagKey &key = newEntry->GetKey();
89
90    if ( TagHT.count(key) == 1 )
91    {
92       gdcmWarningMacro( "Key already present: " << key );
93       return false;
94    }
95    else
96    {
97       TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
98       newEntry->Register();
99       return true;
100    }
101 }
102
103 /**
104  * \brief   Clear the hash table from given entry AND delete the entry.
105  * @param   entryToRemove Entry to remove AND delete.
106  */
107 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
108 {
109    const TagKey &key = entryToRemove->GetKey();
110    if ( TagHT.count(key) == 1 )
111    {
112       TagHT.erase(key);
113       entryToRemove->Unregister();
114       return true;
115    }
116
117    gdcmWarningMacro( "Key not present : " << key);
118    return false ;
119 }
120
121 /**
122  * \brief   delete all entries in the ElementSet
123  */
124 void ElementSet::ClearEntry()
125 {
126    for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
127    {
128       if ( cc->second )
129       {
130          cc->second->Unregister();
131       }
132    }
133    TagHT.clear();
134 }
135
136 /**
137  * \brief   Get the first entry while visiting *the* 'zero level' DocEntrySet
138  *              (DocEntries out of any Sequence)
139  * \return  The first DocEntry if found, otherwhise NULL
140  */
141 DocEntry *ElementSet::GetFirstEntry()
142 {
143    ItTagHT = TagHT.begin();
144    if (ItTagHT != TagHT.end())
145       return  ItTagHT->second;
146    return NULL;
147 }
148
149 /**
150  * \brief   Get the next entry while visiting *the* 'zero level' DocEntrySet
151  *              (DocEntries out of any Sequence) 
152  * \note : meaningfull only if GetFirstEntry already called 
153  * \return  The next DocEntry if found, otherwhise NULL
154  */
155 DocEntry *ElementSet::GetNextEntry()
156 {
157    gdcmAssertMacro (ItTagHT != TagHT.end());
158
159    ++ItTagHT;
160    if (ItTagHT != TagHT.end())
161       return  ItTagHT->second;
162    return NULL;
163 }
164
165 /**
166  * \brief  retrieves a Dicom Element using (group, element)
167  * @param   group  Group number of the searched Dicom Element 
168  * @param   elem Element number of the searched Dicom Element 
169  * @return  
170  */
171 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem) 
172 {
173    TagKey key = DictEntry::TranslateToKey(group, elem);
174    TagDocEntryHT::iterator it = TagHT.find(key);
175
176    if ( it!=TagHT.end() )
177       return it->second;
178    return NULL;
179 }
180
181 /**
182  * \brief Copies all the attributes from an other DocEntrySet 
183  * @param set entry to copy from
184  * @remarks The contained DocEntries a not copied, only referenced
185  */
186 void ElementSet::Copy(DocEntrySet *set)
187 {
188    // Remove all previous entries
189    ClearEntry();
190
191    DocEntrySet::Copy(set);
192
193    ElementSet *eltSet = dynamic_cast<ElementSet *>(set);
194    if( eltSet )
195    {
196       TagHT = eltSet->TagHT;
197       for(ItTagHT = TagHT.begin();ItTagHT != TagHT.end();++ItTagHT)
198       {
199          (ItTagHT->second)->Register();
200       }
201    }
202 }
203
204 /**
205  * \brief Checks whether *all* the DataEntries of the group have all
206  *        the same type for VR (ImplicitVR or ExplicitVR) 
207  * @param group group number to be checked
208  * @return 1:ImplicitVR 2:ExplicitVR -1:NotCoherent 
209  */
210 int ElementSet::IsVRCoherent( uint16_t group )
211 {
212    uint16_t currentGroup;
213    int codeVR = -1;
214    int currentCodeVR;
215    for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
216    {
217       currentGroup = cc->second->GetGroup();
218
219       if ( currentGroup < group )
220          continue;   
221       if ( currentGroup > group )
222          break;
223       // currentGroup == group
224       if (codeVR == -1)
225       {
226          if (cc->second->IsImplicitVR() )
227             codeVR = 1;
228          else 
229             codeVR = 2;
230          continue;
231       }
232       else
233       {
234          if (cc->second->IsImplicitVR() )
235             currentCodeVR = 1; //Implicit
236          else 
237             currentCodeVR = 2; // Explicit  
238   
239          if ( currentCodeVR == codeVR )
240            continue;
241          else
242             return -1;    // -1 : not coherent 
243       }
244    }   
245    return codeVR;
246 }
247
248
249 //-----------------------------------------------------------------------------
250 // Protected
251
252 //-----------------------------------------------------------------------------
253 // Private
254
255 //-----------------------------------------------------------------------------
256 // Print
257 /**
258   * \brief   Prints the Header Entries (Dicom Elements) from the H Table
259   * @param os ostream to write to  
260   * @param indent Indentation string to be prepended during printing
261   */ 
262 void ElementSet::Print(std::ostream &os, std::string const & )
263 {
264    // Let's change the 'warning value' for Pixel Data,
265    // to avoid human reader to be confused by 'gdcm::NotLoaded'.   
266    DataEntry *pixelElement = GetDataEntry(0x7fe0,0x0010);
267    if ( pixelElement != 0 )
268    {
269       pixelElement->SetFlag( DataEntry::FLAG_PIXELDATA );
270    }
271
272    for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
273    {
274       DocEntry *entry = i->second;
275
276       entry->SetPrintLevel(PrintLevel);
277       entry->Print(os);   
278
279       if ( dynamic_cast<SeqEntry*>(entry) )
280       {
281          // Avoid the newline for a sequence:
282          continue;
283       }
284       os << std::endl;
285    }
286 }
287
288 //-----------------------------------------------------------------------------
289 } // end namespace gdcm