GDCM 2.2.1

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

gdcmDict.h
Go to the documentation of this file.
1 /*=========================================================================
2
3 Program: GDCM (Grassroots DICOM). A DICOM library
4
5 Copyright (c) 2006-2011 Mathieu Malaterre
6 All rights reserved.
7 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notice for more information.
12
13 =========================================================================*/
14 #ifndef GDCMDICT_H
15 #define GDCMDICT_H
16
17 #include "gdcmTypes.h"
18 #include "gdcmTag.h"
19 #include "gdcmPrivateTag.h"
20 #include "gdcmDictEntry.h"
21 #include "gdcmSystem.h"
22
23 #include <iostream>
24 #include <iomanip>
25 #include <map>
26
27 /*
28 * FIXME / TODO
29 * I need to seriously rewrite this mess. a class template should work for both a public
30 * and a private dict
31 */
32
33 namespace gdcm
34 {
35 // Data Element Tag
44 class GDCM_EXPORT Dict
45 {
46 public:
47 typedef std::map<Tag, DictEntry> MapDictEntry;
48 typedef MapDictEntry::iterator Iterator;
49 typedef MapDictEntry::const_iterator ConstIterator;
50 //static DictEntry GroupLengthDictEntry; // = DictEntry("Group Length",VR::UL,VM::VM1);
51
52 Dict():DictInternal() {
53 assert( DictInternal.empty() );
54 }
55
56 friend std::ostream& operator<<(std::ostream& _os, const Dict &_val);
57
58 ConstIterator Begin() const { return DictInternal.begin(); }
59 ConstIterator End() const { return DictInternal.end(); }
60
61 bool IsEmpty() const { return DictInternal.empty(); }
62 void AddDictEntry(const Tag &tag, const DictEntry &de)
63 {
64 #ifndef NDEBUG
65 MapDictEntry::size_type s = DictInternal.size();
66 #endif
67 DictInternal.insert(
68 MapDictEntry::value_type(tag, de));
69 assert( s < DictInternal.size() );
70 }
71
72 const DictEntry &GetDictEntry(const Tag &tag) const
73 {
74 MapDictEntry::const_iterator it =
75 DictInternal.find(tag);
76 if (it == DictInternal.end())
77 {
78 #ifdef UNKNOWNPUBLICTAG
79 // test.acr
80 if( tag != Tag(0x28,0x15)
81 && tag != Tag(0x28,0x16)
82 && tag != Tag(0x28,0x199)
83 // gdcmData/TheralysGDCM1.dcm
84 && tag != Tag(0x20,0x1)
85 // gdcmData/0019004_Baseline_IMG1.dcm
86 && tag != Tag(0x8348,0x339)
87 && tag != Tag(0xb5e8,0x338)
88 // gdcmData/dicomdir_Acusson_WithPrivate_WithSR
89 && tag != Tag(0x40,0xa125)
90 )
91 {
92 assert( 0 && "Impossible" );
93 }
94 #endif
95 it = DictInternal.find( Tag(0xffff,0xffff) );
96 return it->second;
97 }
98 assert( DictInternal.count(tag) == 1 );
99 return it->second;
100 }
101
103 const char *GetKeywordFromTag(Tag const & tag) const
104 {
105 MapDictEntry::const_iterator it =
106 DictInternal.find(tag);
107 if (it == DictInternal.end())
108 {
109 return NULL;
110 }
111 assert( DictInternal.count(tag) == 1 );
112 return it->second.GetKeyword();
113 }
114
119 const DictEntry &GetDictEntryByKeyword(const char *keyword, Tag & tag) const
120 {
121 MapDictEntry::const_iterator it =
122 DictInternal.begin();
123 if( keyword )
124 {
125 for(; it != DictInternal.end(); ++it)
126 {
127 if( strcmp( keyword, it->second.GetKeyword() ) == 0 )
128 {
129 // Found a match !
130 tag = it->first;
131 break;
132 }
133 }
134 }
135 else
136 {
137 it = DictInternal.end();
138 }
139 if (it == DictInternal.end())
140 {
141 tag = Tag(0xffff,0xffff);
142 it = DictInternal.find( tag );
143 return it->second;
144 }
145 assert( DictInternal.count(tag) == 1 );
146 return it->second;
147 }
148
152 const DictEntry &GetDictEntryByName(const char *name, Tag & tag) const
153 {
154 MapDictEntry::const_iterator it =
155 DictInternal.begin();
156 if( name )
157 {
158 for(; it != DictInternal.end(); ++it)
159 {
160 if( strcmp( name, it->second.GetName() ) == 0 )
161 {
162 // Found a match !
163 tag = it->first;
164 break;
165 }
166 }
167 }
168 else
169 {
170 it = DictInternal.end();
171 }
172 if (it == DictInternal.end())
173 {
174 tag = Tag(0xffff,0xffff);
175 it = DictInternal.find( tag );
176 return it->second;
177 }
178 assert( DictInternal.count(tag) == 1 );
179 return it->second;
180 }
181
182 protected:
183 friend class Dicts;
184 void LoadDefault();
185
186 private:
187 Dict &operator=(const Dict &_val); // purposely not implemented
188 Dict(const Dict &_val); // purposely not implemented
189
190 MapDictEntry DictInternal;
191 };
192 //-----------------------------------------------------------------------------
193 inline std::ostream& operator<<(std::ostream& os, const Dict &val)
194 {
195 Dict::MapDictEntry::const_iterator it = val.DictInternal.begin();
196 for(;it != val.DictInternal.end(); ++it)
197 {
198 const Tag &t = it->first;
199 const DictEntry &de = it->second;
200 os << t << " " << de << '\n';
201 }
202
203 return os;
204 }
205
206 // TODO
207 // For private dict, element < 0x10 should automatically defined:
208 // Name = "Private Creator"
209 // ValueRepresentation = LO
210 // ValueMultiplicity = 1
211 // Owner = ""
212
216 class GDCM_EXPORT PrivateDict
217 {
218 typedef std::map<PrivateTag, DictEntry> MapDictEntry;
219 friend std::ostream& operator<<(std::ostream& os, const PrivateDict &val);
220 public:
221 PrivateDict() {}
222 ~PrivateDict() {}
223 void AddDictEntry(const PrivateTag &tag, const DictEntry &de)
224 {
225 #ifndef NDEBUG
226 MapDictEntry::size_type s = DictInternal.size();
227 #endif
228 DictInternal.insert(
229 MapDictEntry::value_type(tag, de));
230 // The following code should only be used when manually constructing a Private.xml file by hand
231 // it will get rid of VR::UN duplicate (ie. if a VR != VR::Un can be found)
232 #if defined(NDEBUG) && 0
233 if( s == DictInternal.size() )
234 {
235 MapDictEntry::iterator it =
236 DictInternal.find(tag);
237 assert( it != DictInternal.end() );
238 DictEntry &duplicate = it->second;
239 assert( de.GetVR() == VR::UN || duplicate.GetVR() == VR::UN );
240 assert( de.GetVR() != duplicate.GetVR() );
241 if( duplicate.GetVR() == VR::UN )
242 {
243 assert( de.GetVR() != VR::UN );
244 duplicate.SetVR( de.GetVR() );
245 duplicate.SetVM( de.GetVM() );
246 assert( GetDictEntry(tag).GetVR() != VR::UN );
247 assert( GetDictEntry(tag).GetVR() == de.GetVR() );
248 assert( GetDictEntry(tag).GetVM() == de.GetVM() );
249 }
250 return;
251 }
252 #endif
253 assert( s < DictInternal.size() /*&& std::cout << tag << "," << de << std::endl*/ );
254 }
257 bool RemoveDictEntry(const PrivateTag &tag)
258 {
259 MapDictEntry::size_type s =
260 DictInternal.erase(tag);
261 assert( s == 1 || s == 0 );
262 return s == 1;
263 }
264 bool FindDictEntry(const PrivateTag &tag) const
265 {
266 MapDictEntry::const_iterator it =
267 DictInternal.find(tag);
268 if (it == DictInternal.end())
269 {
270 return false;
271 }
272 return true;
273 }
274 const DictEntry &GetDictEntry(const PrivateTag &tag) const
275 {
276 // if 0x10 -> return Private Creator
277 MapDictEntry::const_iterator it =
278 DictInternal.find(tag);
279 if (it == DictInternal.end())
280 {
281 //assert( 0 && "Impossible" );
282 it = DictInternal.find( PrivateTag(0xffff,0xffff,"GDCM Private Sentinel" ) );
283 assert (it != DictInternal.end());
284 return it->second;
285 }
286 assert( DictInternal.count(tag) == 1 );
287 return it->second;
288 }
289
290
291 void PrintXML() const
292 {
293 MapDictEntry::const_iterator it = DictInternal.begin();
294 std::cout << "<dict edition=\"2008\">\n";
295 for(;it != DictInternal.end(); ++it)
296 {
297 const PrivateTag &t = it->first;
298 const DictEntry &de = it->second;
299 std::cout << " <entry group=\"" << std::hex << std::setw(4)
300 << std::setfill('0') << t.GetGroup() << "\"" <<
301 " element=\"xx" << std::setw(2) << std::setfill('0')<< t.GetElement() << "\"" << " vr=\""
302 << de.GetVR() << "\" vm=\"" << de.GetVM() << "\" owner=\""
303 << t.GetOwner();
304 const char *name = de.GetName();
305 if( *name == 0 )
306 {
307 std::cout << "\"/>\n";
308 }
309 else
310 {
311 std::cout << "\" name=\"" << de.GetName() << "\"/>\n";
312 }
313 }
314 std::cout << "</dict>\n";
315 }
316
317 bool IsEmpty() const { return DictInternal.empty(); }
318 protected:
319 friend class Dicts;
320 void LoadDefault();
321
322 private:
323 PrivateDict &operator=(const PrivateDict &_val); // purposely not implemented
324 PrivateDict(const PrivateDict &_val); // purposely not implemented
325
326 MapDictEntry DictInternal;
327 };
328 //-----------------------------------------------------------------------------
329 inline std::ostream& operator<<(std::ostream& os, const PrivateDict &val)
330 {
331 PrivateDict::MapDictEntry::const_iterator it = val.DictInternal.begin();
332 for(;it != val.DictInternal.end(); ++it)
333 {
334 const PrivateTag &t = it->first;
335 const DictEntry &de = it->second;
336 os << t << " " << de << '\n';
337 }
338
339 return os;
340 }
341
342 } // end namespace gdcm
343
344 #endif //GDCMDICT_H