]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
Add the method int ElementSet::IsVRCoherent( uint16_t group )
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/01/31 11:39:47 $
7   Version:   $Revision: 1.72 $
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  * \brief Checks whether *all* the DataEntries of the group have all
198  *        the same type for VR (ImplicitVR or ExplicitVR) 
199  * @param group group number to be checked
200  * @return 1:ImplicitVR 2:ExplicitVR -1:NotCoherent 
201  */
202 int ElementSet::IsVRCoherent( uint16_t group )
203 {
204    uint16_t currentGroup;
205    int codeVR = -1;
206    int currentCodeVR;
207    for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
208    {
209       currentGroup = cc->second->GetGroup();
210
211       if ( currentGroup < group )
212          continue;   
213       if ( currentGroup > group )
214          break;
215       // currentGroup == group
216       if (codeVR == -1)
217       {
218          if (cc->second->IsImplicitVR() )
219             codeVR = 1;
220          else 
221             codeVR = 2;
222          continue;
223       }
224       else
225       {
226          if (cc->second->IsImplicitVR() )
227             currentCodeVR = 1; //Implicit
228          else 
229             currentCodeVR = 2; // Explicit  
230   
231          if ( currentCodeVR == codeVR )
232            continue;
233          else
234             return -1;    // -1 : not coherent 
235       }
236    }   
237    return codeVR;
238 }
239
240
241 //-----------------------------------------------------------------------------
242 // Protected
243
244 //-----------------------------------------------------------------------------
245 // Private
246
247 //-----------------------------------------------------------------------------
248 // Print
249 /**
250   * \brief   Prints the Header Entries (Dicom Elements) from the H Table
251   * @param os ostream to write to  
252   * @param indent Indentation string to be prepended during printing
253   */ 
254 void ElementSet::Print(std::ostream &os, std::string const & )
255 {
256    // Let's change the 'warning value' for Pixel Data,
257    // to avoid human reader to be confused by 'gdcm::NotLoaded'.   
258    DataEntry *pixelElement = GetDataEntry(0x7fe0,0x0010);
259    if ( pixelElement != 0 )
260    {
261       pixelElement->SetFlag( DataEntry::FLAG_PIXELDATA );
262    }
263
264    for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
265    {
266       DocEntry *entry = i->second;
267
268       entry->SetPrintLevel(PrintLevel);
269       entry->Print(os);   
270
271       if ( dynamic_cast<SeqEntry *>(entry) )
272       {
273          // Avoid the newline for a sequence:
274          continue;
275       }
276       os << std::endl;
277    }
278 }
279
280 //-----------------------------------------------------------------------------
281 } // end namespace gdcm