GDCM  2.2.6
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 
22 namespace gdcm
23 {
24 
39 {
40 public:
42  Tag(uint16_t group, uint16_t element) {
43  ElementTag.tags[0] = group; ElementTag.tags[1] = element;
44  }
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  assert(_id<2);
88  return ElementTag.tags[_id];
89  }
91  uint16_t &operator[](const unsigned int &_id)
92  {
93  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 garantee 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  }
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 
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  }
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  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 ReadFromPipeSeparatedString(const char *str);
249 
252  std::string PrintAsPipeSeparatedString() const;
253 
254 private:
255  union { uint32_t tag; uint16_t tags[2]; char bytes[4]; } ElementTag;
256 };
257 //-----------------------------------------------------------------------------
258 inline std::istream& operator>>(std::istream &_is, Tag &_val)
259 {
260  char c;
261  _is >> c;
262  uint16_t a, b;
263  _is >> std::hex >> a;
264  //_is >> std::hex >> _val[0];
265  //_is >> std::hex >> _val.ElementTag.tags[0];
266  _is >> c;
267  //_is >> _val[1];
268  //_is >> std::hex >> _val.ElementTag.tags[1];
269  _is >> std::hex >> b;
270  _is >> c;
271  _val.SetGroup( a );
272  _val.SetElement( b );
273  return _is;
274 }
275 
276 inline std::ostream& operator<<(std::ostream &_os, const Tag &_val)
277 {
278  _os.setf( std::ios::right);
279  _os << std::hex << '(' << std::setw( 4 ) << std::setfill( '0' )
280  << _val[0] << ',' << std::setw( 4 ) << std::setfill( '0' )
281  << _val[1] << ')' << std::setfill( ' ' ) << std::dec;
282  return _os;
283 }
284 
285 } // end namespace gdcm
286 
287 #endif //GDCMTAG_H
bool operator<=(const Tag &t2) const
Definition: gdcmTag.h:133
uint32_t tag
Definition: gdcmTag.h:255
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
uint32_t GetElementTag() const
Returns the full tag value of the given Tag.
Definition: gdcmTag.h:68
bool IsPrivate() const
Definition: gdcmTag.h:155
#define GDCM_EXPORT
Definition: gdcmWin32.h:34
std::istream & operator>>(std::istream &is, String< TDelimiter, TMaxLength, TPadChar > &ms)
Definition: gdcmString.h:117
uint16_t tags[2]
Definition: gdcmTag.h:255
uint16_t GetElement() const
Returns the &#39;Element number&#39; of the given Tag.
Definition: gdcmTag.h:57
std::ostream & operator<<(std::ostream &os, const Directory &d)
Definition: gdcmDirectory.h:92
const std::ostream & Write(std::ostream &os) const
Write a tag in binary rep.
Definition: gdcmTag.h:169
bool operator==(const Tag &_val) const
Definition: gdcmTag.h:103
Tag GetPrivateCreator() const
Return the Private Creator Data Element tag of a private data element.
Definition: gdcmTag.h:179
uint32_t GetLength() const
return the length of tag (read: size on disk)
Definition: gdcmTag.h:144
bool IsGroupXX(const Tag &t) const
e.g 6002,3000 belong to groupXX: 6000,3000
Definition: gdcmTag.h:228
bool operator!=(const Tag &_val) const
Definition: gdcmTag.h:107
void SetPrivateCreator(Tag const &t)
Set private creator:
Definition: gdcmTag.h:193
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
std::istream & Read(std::istream &is)
Read a tag from binary representation.
Definition: gdcmTag.h:160
bool IsPrivateCreator() const
Definition: gdcmTag.h:206
void SetElement(uint16_t element)
Sets the &#39;Element number&#39; of the given Tag.
Definition: gdcmTag.h:61
Tag & operator=(const Tag &_val)
Definition: gdcmTag.h:97
Tag(uint32_t tag=0)
Constructor with 1*uint32_t Prefer the cstor that takes two uint16_t.
Definition: gdcmTag.h:47
Tag(uint16_t group, uint16_t element)
Constructor with 2*uint16_t.
Definition: gdcmTag.h:42
bool IsPublic() const
Definition: gdcmTag.h:150
bool IsIllegal() const
return if the tag is considered to be an illegal tag
Definition: gdcmTag.h:212
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element). Basically an uint32_t which...
Definition: gdcmTag.h:38
uint16_t GetGroup() const
Returns the &#39;Group number&#39; of the given Tag.
Definition: gdcmTag.h:55
void SetElementTag(uint16_t group, uint16_t element)
Sets the &#39;Group number&#39; &amp; &#39;Element number&#39; of the given Tag.
Definition: gdcmTag.h:63
bool operator<(const Tag &_val) const
Definition: gdcmTag.h:119
Tag(const Tag &_val)
Definition: gdcmTag.h:139
void SetGroup(uint16_t group)
Sets the &#39;Group number&#39; of the given Tag.
Definition: gdcmTag.h:59
void SetElementTag(uint32_t tag)
Sets the full tag value of the given Tag.
Definition: gdcmTag.h:77
bool IsGroupLength() const
return whether the tag correspond to a group length tag:
Definition: gdcmTag.h:222

Generated on Sat Dec 21 2013 05:56:18 for GDCM by doxygen 1.8.5
SourceForge.net Logo