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