GDCM: gdcmTag.h Source File

GDCM 3.2.5

gdcmTag.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 GDCMTAG_H
15#define GDCMTAG_H
16
17#include "gdcmTypes.h"
18
19#include <iostream>
20#include <iomanip>
21
22namespace gdcm
23{
24
38 class GDCM_EXPORT Tag
39{
40public:
42 Tag(uint16_t group, uint16_t element) {
43 ElementTag.tags[0] = group; ElementTag.tags[1] = element;
44 }
45
47 Tag(uint32_t tag = 0) {
48 SetElementTag(tag);
49 }
50
51 friend std::ostream& operator<<(std::ostream &_os, const Tag &_val);
52 friend std::istream& operator>>(std::istream &_is, Tag &_val);
53
55 uint16_t GetGroup() const { return ElementTag.tags[0]; }
57 uint16_t GetElement() const { return ElementTag.tags[1]; }
59 void SetGroup(uint16_t group) { ElementTag.tags[0] = group; }
61 void SetElement(uint16_t element) { ElementTag.tags[1] = element; }
63 void SetElementTag(uint16_t group, uint16_t element) {
64 ElementTag.tags[0] = group; ElementTag.tags[1] = element;
65 }
66
68 uint32_t GetElementTag() const {
69#ifndef GDCM_WORDS_BIGENDIAN
70 return (ElementTag.tag<<16) | (ElementTag.tag>>16);
71#else
72 return ElementTag.tag;
73#endif
74 }
75
77 void SetElementTag(uint32_t tag) {
78#ifndef GDCM_WORDS_BIGENDIAN
79 tag = ( (tag<<16) | (tag>>16) );
80#endif
81 ElementTag.tag = tag;
82 }
83
85 const uint16_t &operator[](const unsigned int &_id) const
86 {
87 gdcm_assert(_id<2);
88 return ElementTag.tags[_id];
89 }
90
91 uint16_t &operator[](const unsigned int &_id)
92 {
93 gdcm_assert(_id<2);
94 return ElementTag.tags[_id];
95 }
96
97 Tag &operator=(const Tag &_val)
98 {
99 ElementTag.tag = _val.ElementTag.tag;
100 return *this;
101 }
102
103 bool operator==(const Tag &_val) const
104 {
105 return ElementTag.tag == _val.ElementTag.tag;
106 }
107 bool operator!=(const Tag &_val) const
108 {
109 return ElementTag.tag != _val.ElementTag.tag;
110 }
111
114 // FIXME FIXME FIXME TODO
115 // the following is pretty dumb. Since we have control over who is group
116 // and who is element, we should reverse them in little endian and big endian case
117 // since what we really want is fast comparison and not guarantee that group is in #0
118 // ...
119 bool operator<(const Tag &_val) const
120 {
121#ifndef GDCM_WORDS_BIGENDIAN
122 if( ElementTag.tags[0] < _val.ElementTag.tags[0] )
123 return true;
124 if( ElementTag.tags[0] == _val.ElementTag.tags[0]
125 && ElementTag.tags[1] < _val.ElementTag.tags[1] )
126 return true;
127 return false;
128#else
129 // Plain comparison is enough!
130 return ( ElementTag.tag < _val.ElementTag.tag );
131#endif
132 }
133 bool operator<=(const Tag &t2) const
134 {
135 const Tag &t1 = *this;
136 return t1 == t2 || t1 < t2;
137 }
138
139 Tag(const Tag &_val)
140 {
141 ElementTag.tag = _val.ElementTag.tag;
142 }
143
144 uint32_t GetLength() const { return 4; }
145
150 bool IsPublic() const { return !(ElementTag.tags[0] % 2); }
151
155 bool IsPrivate() const { return !IsPublic(); }
156
157 //-----------------------------------------------------------------------------
159 template <typename TSwap>
160 std::istream &Read(std::istream &is)
161 {
162 if( is.read(ElementTag.bytes, 4) )
163 TSwap::SwapArray(ElementTag.tags, 2);
164 return is;
165 }
166
168 template <typename TSwap>
169 const std::ostream &Write(std::ostream &os) const
170 {
171 uint16_t copy[2];
172 copy[0]= ElementTag.tags[0];
173 copy[1]= ElementTag.tags[1];
174 TSwap::SwapArray(copy, 2);
175 return os.write((char*)(&copy), 4);
176 }
177
179 Tag GetPrivateCreator() const
180 {
181 // See PS 3.5 - 7.8.1 PRIVATE DATA ELEMENT TAGS
182 // eg: 0x0123,0x1425 -> 0x0123,0x0014
183 if( IsPrivate() && !IsPrivateCreator() )
184 {
185 Tag r = *this;
186 r.SetElement( (uint16_t)(GetElement() >> 8) );
187 return r;
188 }
189 if( IsPrivateCreator() ) return *this;
190 return Tag(0x0,0x0);
191 }
192
193 void SetPrivateCreator(Tag const &t)
194 {
195 // See PS 3.5 - 7.8.1 PRIVATE DATA ELEMENT TAGS
196 // eg: 0x0123,0x0045 -> 0x0123,0x4567
197 gdcm_assert( t.IsPrivate() /*&& t.IsPrivateCreator()*/ );
198 const uint16_t element = (uint16_t)(t.GetElement() << 8);
199 const uint16_t base = (uint16_t)(GetElement() << 8);
200 SetElement( (uint16_t)((base >> 8) + element) );
201 SetGroup( t.GetGroup() );
202 }
203
206 bool IsPrivateCreator() const
207 {
208 return IsPrivate() && (GetElement() <= 0xFF && GetElement() >= 0x10);
209 }
210
212 bool IsIllegal() const
213 {
214 // DICOM reserved those groups:
215 return GetGroup() == 0x0001 || GetGroup() == 0x0003 || GetGroup() == 0x0005 || GetGroup() == 0x0007
216 // This is a very special case, in private group, one cannot use element [0x01,0x09] ...
217// || (IsPrivate() && !IsPrivateCreator() && !IsGroupLength());
218 || (IsPrivate() && GetElement() > 0x0 && GetElement() < 0x10 );
219 }
220
222 bool IsGroupLength() const
223 {
224 return GetElement() == 0x0;
225 }
226
228 bool IsGroupXX(const Tag &t) const
229 {
230 if( t.GetElement() == GetElement() )
231 {
232 if( t.IsPrivate() ) return false;
233 uint16_t group = (uint16_t)((GetGroup() >> 8 ) << 8);
234 return group == t.GetGroup();
235 }
236 return false;
237 }
238
244 bool ReadFromCommaSeparatedString(const char *str);
245
248 bool ReadFromContinuousString(const char *str);
249
252 std::string PrintAsContinuousString() const;
253
255 std::string PrintAsContinuousUpperCaseString() const;
256
259 bool ReadFromPipeSeparatedString(const char *str);
260
263 std::string PrintAsPipeSeparatedString() const;
264
265private:
266 union { uint32_t tag; uint16_t tags[2]; char bytes[4]; } ElementTag;
267};
268//-----------------------------------------------------------------------------
269 inline std::istream& operator>>(std::istream &_is, Tag &_val)
270{
271 char c;
272 _is >> c;
273 uint16_t a, b;
274 _is >> std::hex >> a;
275 //_is >> std::hex >> _val[0];
276 //_is >> std::hex >> _val.ElementTag.tags[0];
277 _is >> c;
278 //_is >> _val[1];
279 //_is >> std::hex >> _val.ElementTag.tags[1];
280 _is >> std::hex >> b;
281 _is >> c;
282 _val.SetGroup( a );
283 _val.SetElement( b );
284 return _is;
285}
286
287 inline std::ostream& operator<<(std::ostream &_os, const Tag &_val)
288{
289 _os.setf( std::ios::right);
290 _os << std::hex << '(' << std::setw( 4 ) << std::setfill( '0' )
291 << _val[0] << ',' << std::setw( 4 ) << std::setfill( '0' )
292 << _val[1] << ')' << std::setfill( ' ' ) << std::dec;
293 return _os;
294}
295
296} // end namespace gdcm
297
298#endif //GDCMTAG_H
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::Tag::SetGroup
void SetGroup(uint16_t group)
Sets the 'Group number' of the given Tag.
Definition gdcmTag.h:59
gdcm::Tag::ReadFromCommaSeparatedString
bool ReadFromCommaSeparatedString(const char *str)
gdcm::Tag::operator[]
uint16_t & operator[](const unsigned int &_id)
Returns the Group or Element of the given Tag, depending on id (0/1).
Definition gdcmTag.h:91
gdcm::Tag::IsPrivateCreator
bool IsPrivateCreator() const
Definition gdcmTag.h:206
gdcm::Tag::operator<
bool operator<(const Tag &_val) const
Definition gdcmTag.h:119
gdcm::Tag::operator[]
const uint16_t & operator[](const unsigned int &_id) const
Returns the Group or Element of the given Tag, depending on id (0/1).
Definition gdcmTag.h:85
gdcm::Tag::PrintAsContinuousString
std::string PrintAsContinuousString() const
gdcm::Tag::SetElement
void SetElement(uint16_t element)
Sets the 'Element number' of the given Tag.
Definition gdcmTag.h:61
gdcm::Tag::SetPrivateCreator
void SetPrivateCreator(Tag const &t)
Set private creator:
Definition gdcmTag.h:193
gdcm::Tag::SetElementTag
void SetElementTag(uint16_t group, uint16_t element)
Sets the 'Group number' & 'Element number' of the given Tag.
Definition gdcmTag.h:63
gdcm::Tag::GetGroup
uint16_t GetGroup() const
Returns the 'Group number' of the given Tag.
Definition gdcmTag.h:55
gdcm::Tag::operator<=
bool operator<=(const Tag &t2) const
Definition gdcmTag.h:133
gdcm::Tag::GetLength
uint32_t GetLength() const
return the length of tag (read: size on disk)
Definition gdcmTag.h:144
gdcm::Tag::IsPrivate
bool IsPrivate() const
Definition gdcmTag.h:155
gdcm::Tag::Tag
Tag(const Tag &_val)
Definition gdcmTag.h:139
gdcm::Tag::SetElementTag
void SetElementTag(uint32_t tag)
Sets the full tag value of the given Tag.
Definition gdcmTag.h:77
gdcm::Tag::IsIllegal
bool IsIllegal() const
return if the tag is considered to be an illegal tag
Definition gdcmTag.h:212
gdcm::Tag::IsGroupXX
bool IsGroupXX(const Tag &t) const
e.g 6002,3000 belong to groupXX: 6000,3000
Definition gdcmTag.h:228
gdcm::Tag::Write
const std::ostream & Write(std::ostream &os) const
Write a tag in binary rep.
Definition gdcmTag.h:169
gdcm::Tag::GetPrivateCreator
Tag GetPrivateCreator() const
Return the Private Creator Data Element tag of a private data element.
Definition gdcmTag.h:179
gdcm::Tag::ReadFromPipeSeparatedString
bool ReadFromPipeSeparatedString(const char *str)
gdcm::Tag::PrintAsContinuousUpperCaseString
std::string PrintAsContinuousUpperCaseString() const
Same as PrintAsContinuousString, but hexadecimal [a-f] are printed using upper case.
gdcm::Tag::bytes
char bytes[4]
Definition gdcmTag.h:266
gdcm::Tag::IsGroupLength
bool IsGroupLength() const
return whether the tag correspond to a group length tag:
Definition gdcmTag.h:222
gdcm::Tag::operator==
bool operator==(const Tag &_val) const
Definition gdcmTag.h:103
gdcm::Tag::tags
uint16_t tags[2]
Definition gdcmTag.h:266
gdcm::Tag::IsPublic
bool IsPublic() const
Definition gdcmTag.h:150
gdcm::Tag::Tag
Tag(uint16_t group, uint16_t element)
Constructor with 2*uint16_t.
Definition gdcmTag.h:42
gdcm::Tag::GetElement
uint16_t GetElement() const
Returns the 'Element number' of the given Tag.
Definition gdcmTag.h:57
gdcm::Tag::Read
std::istream & Read(std::istream &is)
Read a tag from binary representation.
Definition gdcmTag.h:160
gdcm::Tag::ReadFromContinuousString
bool ReadFromContinuousString(const char *str)
gdcm::Tag::Tag
Tag(uint32_t tag=0)
Constructor with 1*uint32_t Prefer the cstor that takes two uint16_t.
Definition gdcmTag.h:47
gdcm::Tag::tag
uint32_t tag
Definition gdcmTag.h:266
gdcm::Tag::PrintAsPipeSeparatedString
std::string PrintAsPipeSeparatedString() const
gdcm::Tag::operator=
Tag & operator=(const Tag &_val)
Definition gdcmTag.h:97
gdcm::Tag::GetElementTag
uint32_t GetElementTag() const
Returns the full tag value of the given Tag.
Definition gdcmTag.h:68
gdcm::Tag::operator!=
bool operator!=(const Tag &_val) const
Definition gdcmTag.h:107
gdcm_assert
#define gdcm_assert(cond)
Definition gdcmException.h:104
gdcmTypes.h
GDCM_EXPORT
#define GDCM_EXPORT
Definition gdcmWin32.h:34
gdcm
Definition gdcmASN1.h:21
gdcm::operator>>
std::istream & operator>>(std::istream &is, String< TDelimiter, TMaxLength, TPadChar > &ms)
Definition gdcmString.h:116
gdcm::operator<<
std::ostream & operator<<(std::ostream &os, const Directory &d)
Definition gdcmDirectory.h:88