GDCM 3.2.1

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 gdcm_assert( DictInternal.empty() );
54 }
55 Dict &operator=(const Dict &_val) = delete;
56 Dict(const Dict &_val) = delete;
57
58
59 friend std::ostream& operator<<(std::ostream& _os, const Dict &_val);
60
61 ConstIterator Begin() const { return DictInternal.begin(); }
62 ConstIterator End() const { return DictInternal.end(); }
63
64 bool IsEmpty() const { return DictInternal.empty(); }
65 void AddDictEntry(const Tag &tag, const DictEntry &de)
66 {
67#ifndef NDEBUG
68 MapDictEntry::size_type s = DictInternal.size();
69#endif
70 DictInternal.insert(
71 MapDictEntry::value_type(tag, de));
72#ifndef NDEBUG
73 gdcm_assert( s < DictInternal.size() );
74#endif
75 }
76
77 const DictEntry &GetDictEntry(const Tag &tag) const
78 {
79 MapDictEntry::const_iterator it =
80 DictInternal.find(tag);
81 if (it == DictInternal.end())
82 {
83#ifdef UNKNOWNPUBLICTAG
84 // test.acr
85 if( tag != Tag(0x28,0x15)
86 && tag != Tag(0x28,0x16)
87 && tag != Tag(0x28,0x199)
88 // gdcmData/TheralysGDCM1.dcm
89 && tag != Tag(0x20,0x1)
90 // gdcmData/0019004_Baseline_IMG1.dcm
91 && tag != Tag(0x8348,0x339)
92 && tag != Tag(0xb5e8,0x338)
93 // gdcmData/dicomdir_Acusson_WithPrivate_WithSR
94 && tag != Tag(0x40,0xa125)
95 )
96 {
97 gdcm_assert( 0 && "Impossible" );
98 }
99#endif
100 it = DictInternal.find( Tag(0xffff,0xffff) );
101 return it->second;
102 }
103 gdcm_assert( DictInternal.count(tag) == 1 );
104 return it->second;
105 }
106
108 const char *GetKeywordFromTag(Tag const & tag) const
109 {
110 MapDictEntry::const_iterator it =
111 DictInternal.find(tag);
112 if (it == DictInternal.end())
113 {
114 return nullptr;
115 }
116 gdcm_assert( DictInternal.count(tag) == 1 );
117 return it->second.GetKeyword();
118 }
119
124 const DictEntry &GetDictEntryByKeyword(const char *keyword, Tag & tag) const
125 {
126 MapDictEntry::const_iterator it =
127 DictInternal.begin();
128 if( keyword )
129 {
130 for(; it != DictInternal.end(); ++it)
131 {
132 if( strcmp( keyword, it->second.GetKeyword() ) == 0 )
133 {
134 // Found a match !
135 tag = it->first;
136 break;
137 }
138 }
139 }
140 else
141 {
142 it = DictInternal.end();
143 }
144 if (it == DictInternal.end())
145 {
146 tag = Tag(0xffff,0xffff);
147 it = DictInternal.find( tag );
148 return it->second;
149 }
150 gdcm_assert( DictInternal.count(tag) == 1 );
151 return it->second;
152 }
153
157 const DictEntry &GetDictEntryByName(const char *name, Tag & tag) const
158 {
159 MapDictEntry::const_iterator it =
160 DictInternal.begin();
161 if( name )
162 {
163 for(; it != DictInternal.end(); ++it)
164 {
165 if( strcmp( name, it->second.GetName() ) == 0 )
166 {
167 // Found a match !
168 tag = it->first;
169 break;
170 }
171 }
172 }
173 else
174 {
175 it = DictInternal.end();
176 }
177 if (it == DictInternal.end())
178 {
179 tag = Tag(0xffff,0xffff);
180 it = DictInternal.find( tag );
181 return it->second;
182 }
183 gdcm_assert( DictInternal.count(tag) == 1 );
184 return it->second;
185 }
186
187 protected:
188 friend class Dicts;
189 void LoadDefault();
190
191 private:
192 MapDictEntry DictInternal;
193};
194//-----------------------------------------------------------------------------
195 inline std::ostream& operator<<(std::ostream& os, const Dict &val)
196{
197 Dict::MapDictEntry::const_iterator it = val.DictInternal.begin();
198 for(;it != val.DictInternal.end(); ++it)
199 {
200 const Tag &t = it->first;
201 const DictEntry &de = it->second;
202 os << t << " " << de << '\n';
203 }
204
205 return os;
206}
207
208// TODO
209// For private dict, element < 0x10 should automatically defined:
210// Name = "Private Creator"
211// ValueRepresentation = LO
212// ValueMultiplicity = 1
213// Owner = ""
214
218 class GDCM_EXPORT PrivateDict
219{
220 typedef std::map<PrivateTag, DictEntry> MapDictEntry;
221 friend std::ostream& operator<<(std::ostream& os, const PrivateDict &val);
222 public:
223 PrivateDict() = default;
224 ~PrivateDict() = default;
225 void AddDictEntry(const PrivateTag &tag, const DictEntry &de)
226 {
227#ifndef NDEBUG
228 MapDictEntry::size_type s = DictInternal.size();
229#endif
230 DictInternal.insert(
231 MapDictEntry::value_type(tag, de));
232// The following code should only be used when manually constructing a Private.xml file by hand
233// it will get rid of VR::UN duplicate (ie. if a VR != VR::Un can be found)
234#if defined(NDEBUG) && 0
235 if( s == DictInternal.size() )
236 {
237 MapDictEntry::iterator it =
238 DictInternal.find(tag);
239 gdcm_assert( it != DictInternal.end() );
240 DictEntry &duplicate = it->second;
241 gdcm_assert( de.GetVR() == VR::UN || duplicate.GetVR() == VR::UN );
242 gdcm_assert( de.GetVR() != duplicate.GetVR() );
243 if( duplicate.GetVR() == VR::UN )
244 {
245 gdcm_assert( de.GetVR() != VR::UN );
246 duplicate.SetVR( de.GetVR() );
247 duplicate.SetVM( de.GetVM() );
248 gdcm_assert( GetDictEntry(tag).GetVR() != VR::UN );
249 gdcm_assert( GetDictEntry(tag).GetVR() == de.GetVR() );
250 gdcm_assert( GetDictEntry(tag).GetVM() == de.GetVM() );
251 }
252 return;
253 }
254#endif
255#ifndef NDEBUG
256 gdcm_assert( s < DictInternal.size() /*&& std::cout << tag << "," << de << std::endl*/ );
257#endif
258 }
259
261 bool RemoveDictEntry(const PrivateTag &tag)
262 {
263 MapDictEntry::size_type s =
264 DictInternal.erase(tag);
265 gdcm_assert( s == 1 || s == 0 );
266 return s == 1;
267 }
268 bool FindDictEntry(const PrivateTag &tag) const
269 {
270 MapDictEntry::const_iterator it =
271 DictInternal.find(tag);
272 if (it == DictInternal.end())
273 {
274 return false;
275 }
276 return true;
277 }
278 const DictEntry &GetDictEntry(const PrivateTag &tag) const
279 {
280 // if 0x10 -> return Private Creator
281 MapDictEntry::const_iterator it =
282 DictInternal.find(tag);
283 if (it == DictInternal.end())
284 {
285 //gdcm_assert( 0 && "Impossible" );
286 it = DictInternal.find( PrivateTag(0xffff,0xffff,"GDCM Private Sentinel" ) );
287 assert (it != DictInternal.end());
288 return it->second;
289 }
290 gdcm_assert( DictInternal.count(tag) == 1 );
291 return it->second;
292 }
293
294
295 void PrintXML() const
296 {
297 MapDictEntry::const_iterator it = DictInternal.begin();
298 std::cout << "<dict edition=\"2008\">\n";
299 for(;it != DictInternal.end(); ++it)
300 {
301 const PrivateTag &t = it->first;
302 const DictEntry &de = it->second;
303 std::cout << " <entry group=\"" << std::hex << std::setw(4)
304 << std::setfill('0') << t.GetGroup() << "\"" <<
305 " element=\"xx" << std::setw(2) << std::setfill('0')<< t.GetElement() << "\"" << " vr=\""
306 << de.GetVR() << "\" vm=\"" << de.GetVM() << "\" owner=\""
307 << t.GetOwner();
308 const char *name = de.GetName();
309 if( *name == 0 )
310 {
311 std::cout << "\"/>\n";
312 }
313 else
314 {
315 std::cout << "\" name=\"" << de.GetName() << "\"/>\n";
316 }
317 }
318 std::cout << "</dict>\n";
319 }
320
321 bool IsEmpty() const { return DictInternal.empty(); }
322 protected:
323 friend class Dicts;
324 void LoadDefault();
325
326 private:
327 PrivateDict &operator=(const PrivateDict &_val) = delete;
328 PrivateDict(const PrivateDict &_val) = delete;
329
330 MapDictEntry DictInternal;
331};
332//-----------------------------------------------------------------------------
333 inline std::ostream& operator<<(std::ostream& os, const PrivateDict &val)
334{
335 PrivateDict::MapDictEntry::const_iterator it = val.DictInternal.begin();
336 for(;it != val.DictInternal.end(); ++it)
337 {
338 const PrivateTag &t = it->first;
339 const DictEntry &de = it->second;
340 os << t << " " << de << '\n';
341 }
342
343 return os;
344}
345
346} // end namespace gdcm
347
348#endif //GDCMDICT_H
gdcm::DictEntry
Class to represent an Entry in the Dict.
Definition gdcmDictEntry.h:37
gdcm::DictEntry::SetVR
void SetVR(const VR &vr)
Definition gdcmDictEntry.h:54
gdcm::DictEntry::GetVR
const VR & GetVR() const
Set/Get VR.
Definition gdcmDictEntry.h:53
gdcm::DictEntry::SetVM
void SetVM(VM const &vm)
Definition gdcmDictEntry.h:60
gdcm::DictEntry::GetName
const char * GetName() const
Set/Get Name.
Definition gdcmDictEntry.h:63
gdcm::DictEntry::GetVM
const VM & GetVM() const
Set/Get VM.
Definition gdcmDictEntry.h:59
gdcm::Dict::Dict
Dict(const Dict &_val)=delete
gdcm::Dict::operator<<
friend std::ostream & operator<<(std::ostream &_os, const Dict &_val)
Definition gdcmDict.h:195
gdcm::Dict::End
ConstIterator End() const
Definition gdcmDict.h:62
gdcm::Dict::GetDictEntry
const DictEntry & GetDictEntry(const Tag &tag) const
Definition gdcmDict.h:77
gdcm::Dict::operator=
Dict & operator=(const Dict &_val)=delete
gdcm::Dict::IsEmpty
bool IsEmpty() const
Definition gdcmDict.h:64
gdcm::Dict::Iterator
MapDictEntry::iterator Iterator
Definition gdcmDict.h:48
gdcm::Dict::Dicts
friend class Dicts
Definition gdcmDict.h:188
gdcm::Dict::GetKeywordFromTag
const char * GetKeywordFromTag(Tag const &tag) const
Function to return the Keyword from a Tag.
Definition gdcmDict.h:108
gdcm::Dict::Dict
Dict()
Definition gdcmDict.h:52
gdcm::Dict::GetDictEntryByName
const DictEntry & GetDictEntryByName(const char *name, Tag &tag) const
Definition gdcmDict.h:157
gdcm::Dict::ConstIterator
MapDictEntry::const_iterator ConstIterator
Definition gdcmDict.h:49
gdcm::Dict::Begin
ConstIterator Begin() const
Definition gdcmDict.h:61
gdcm::Dict::AddDictEntry
void AddDictEntry(const Tag &tag, const DictEntry &de)
Definition gdcmDict.h:65
gdcm::Dict::LoadDefault
void LoadDefault()
gdcm::Dict::GetDictEntryByKeyword
const DictEntry & GetDictEntryByKeyword(const char *keyword, Tag &tag) const
Definition gdcmDict.h:124
gdcm::Dict::MapDictEntry
std::map< Tag, DictEntry > MapDictEntry
Definition gdcmDict.h:47
gdcm::PrivateDict::IsEmpty
bool IsEmpty() const
Definition gdcmDict.h:321
gdcm::PrivateDict::PrintXML
void PrintXML() const
Definition gdcmDict.h:295
gdcm::PrivateDict::operator<<
friend std::ostream & operator<<(std::ostream &os, const PrivateDict &val)
Definition gdcmDict.h:333
gdcm::PrivateDict::Dicts
friend class Dicts
Definition gdcmDict.h:323
gdcm::PrivateDict::GetDictEntry
const DictEntry & GetDictEntry(const PrivateTag &tag) const
Definition gdcmDict.h:278
gdcm::PrivateDict::FindDictEntry
bool FindDictEntry(const PrivateTag &tag) const
Definition gdcmDict.h:268
gdcm::PrivateDict::LoadDefault
void LoadDefault()
gdcm::PrivateDict::~PrivateDict
~PrivateDict()=default
gdcm::PrivateDict::AddDictEntry
void AddDictEntry(const PrivateTag &tag, const DictEntry &de)
Definition gdcmDict.h:225
gdcm::PrivateDict::PrivateDict
PrivateDict()=default
gdcm::PrivateDict::RemoveDictEntry
bool RemoveDictEntry(const PrivateTag &tag)
Definition gdcmDict.h:261
gdcm::PrivateTag
Class to represent a Private DICOM Data Element (Attribute) Tag (Group, Element, Owner)
Definition gdcmPrivateTag.h:39
gdcm::PrivateTag::GetOwner
const char * GetOwner() const
Definition gdcmPrivateTag.h:51
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::Tag::GetGroup
uint16_t GetGroup() const
Returns the 'Group number' of the given Tag.
Definition gdcmTag.h:55
gdcm::Tag::GetElement
uint16_t GetElement() const
Returns the 'Element number' of the given Tag.
Definition gdcmTag.h:57
gdcm::VR::UN
@ UN
Definition gdcmVR.h:89
gdcmDictEntry.h
gdcm_assert
#define gdcm_assert(cond)
Definition gdcmException.h:104
gdcmPrivateTag.h
gdcmSystem.h
gdcmTag.h
gdcmTypes.h
GDCM_EXPORT
#define GDCM_EXPORT
Definition gdcmWin32.h:34
gdcm
Definition gdcmASN1.h:21
gdcm::operator<<
std::ostream & operator<<(std::ostream &os, const Directory &d)
Definition gdcmDirectory.h:88