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