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