]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
--> Optional Element 0x0000 (group length) is no longer written.
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/26 09:15:19 $
7   Version:   $Revision: 1.69 $
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    
66       // Skip 'Group Length' element, since it may be wrong.
67       //       except for Group 0002 
68        if ( (i->second)->GetElement() == 0x0000 
69          && (i->second)->GetElement() != 0x0002 )
70           continue;
71       
72        i->second->WriteContent(fp, filetype);
73    } 
74 }
75
76 /**
77  * \brief   add a new Dicom Element pointer to the H Table
78  * @param   newEntry entry to add
79  */
80 bool ElementSet::AddEntry(DocEntry *newEntry)
81 {
82    const TagKey &key = newEntry->GetKey();
83
84    if ( TagHT.count(key) == 1 )
85    {
86       gdcmWarningMacro( "Key already present: " << key );
87       return false;
88    }
89    else
90    {
91       TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
92       newEntry->Register();
93       return true;
94    }
95 }
96
97 /**
98  * \brief   Clear the hash table from given entry AND delete the entry.
99  * @param   entryToRemove Entry to remove AND delete.
100  */
101 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
102 {
103    const TagKey &key = entryToRemove->GetKey();
104    if ( TagHT.count(key) == 1 )
105    {
106       TagHT.erase(key);
107       entryToRemove->Unregister();
108       return true;
109    }
110
111    gdcmWarningMacro( "Key not present : " << key);
112    return false ;
113 }
114
115 /**
116  * \brief   delete all entries in the ElementSet
117  */
118 void ElementSet::ClearEntry()
119 {
120    for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
121    {
122       if ( cc->second )
123       {
124          cc->second->Unregister();
125       }
126    }
127    TagHT.clear();
128 }
129
130 /**
131  * \brief   Get the first entry while visiting *the* 'zero level' DocEntrySet
132  *              (DocEntries out of any Sequence)
133  * \return  The first DocEntry if found, otherwhise NULL
134  */
135 DocEntry *ElementSet::GetFirstEntry()
136 {
137    ItTagHT = TagHT.begin();
138    if (ItTagHT != TagHT.end())
139       return  ItTagHT->second;
140    return NULL;
141 }
142
143 /**
144  * \brief   Get the next entry while visiting *the* 'zero level' DocEntrySet
145  *              (DocEntries out of any Sequence) 
146  * \note : meaningfull only if GetFirstEntry already called 
147  * \return  The next DocEntry if found, otherwhise NULL
148  */
149 DocEntry *ElementSet::GetNextEntry()
150 {
151    gdcmAssertMacro (ItTagHT != TagHT.end());
152
153    ++ItTagHT;
154    if (ItTagHT != TagHT.end())
155       return  ItTagHT->second;
156    return NULL;
157 }
158
159 /**
160  * \brief  retrieves a Dicom Element using (group, element)
161  * @param   group  Group number of the searched Dicom Element 
162  * @param   elem Element number of the searched Dicom Element 
163  * @return  
164  */
165 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem) 
166 {
167    TagKey key = DictEntry::TranslateToKey(group, elem);
168    TagDocEntryHT::iterator it = TagHT.find(key);
169
170    if ( it!=TagHT.end() )
171       return it->second;
172    return NULL;
173 }
174
175 //-----------------------------------------------------------------------------
176 // Protected
177
178 //-----------------------------------------------------------------------------
179 // Private
180
181 //-----------------------------------------------------------------------------
182 // Print
183 /**
184   * \brief   Prints the Header Entries (Dicom Elements) from the H Table
185   * @param os ostream to write to  
186   * @param indent Indentation string to be prepended during printing
187   */ 
188 void ElementSet::Print(std::ostream &os, std::string const & )
189 {
190    // Let's change the 'warning value' for Pixel Data,
191    // to avoid human reader to be confused by 'gdcm::NotLoaded'.   
192    DataEntry *pixelElement = GetDataEntry(0x7fe0,0x0010);
193    if ( pixelElement != 0 )
194    {
195       pixelElement->SetFlag( DataEntry::FLAG_PIXELDATA );
196    }
197
198    for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
199    {
200       DocEntry *entry = i->second;
201
202       entry->SetPrintLevel(PrintLevel);
203       entry->Print(os);   
204
205       if ( dynamic_cast<SeqEntry *>(entry) )
206       {
207          // Avoid the newline for a sequence:
208          continue;
209       }
210       os << std::endl;
211    }
212 }
213
214 //-----------------------------------------------------------------------------
215 } // end namespace gdcm