GDCM  2.4.5
gdcmVR.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 GDCMVR_H
15 #define GDCMVR_H
16 
17 #include "gdcmTag.h"
18 #include "gdcmTrace.h"
19 #include "gdcmString.h"
20 
21 #include <iostream>
22 #include <fstream>
23 #include <assert.h>
24 
25 //these defines are here to ensure compilation on sunos gcc
26 #if defined (CS)
27 # undef CS
28 #endif
29 #if defined (DS)
30 # undef DS
31 #endif
32 #if defined (SS)
33 # undef SS
34 #endif
35 
36 
37 namespace gdcm
38 {
39 
55 {
56 public:
57  typedef enum {
58  // Warning: Do not write if ( vr & VR::INVALID ) but if ( vr == VR::INVALID )
59  INVALID = 0, // For Item/(Seq) Item Delimitation Item
60  AE = 1,
61  AS = 2,
62  AT = 4,
63  CS = 8,
64  DA = 16,
65  DS = 32,
66  DT = 64,
67  FD = 128,
68  FL = 256,
69  IS = 512,
70  LO = 1024,
71  LT = 2048,
72  OB = 4096,
73  OD = 134217728, // 2^27
74  OF = 8192,
75  OW = 16384,
76  PN = 32768,
77  SH = 65536,
78  SL = 131072,
79  SQ = 262144,
80  SS = 524288,
81  ST = 1048576,
82  TM = 2097152,
83  UI = 4194304,
84  UL = 8388608,
85  UN = 16777216,
86  US = 33554432,
87  UT = 67108864,
88  OB_OW = OB | OW,
89  US_SS = US | SS,
90  US_SS_OW = US | SS | OW,
91  // The following do not have a VRString equivalent (ie cannot be found in PS 3.6)
92  VL16 = AE | AS | AT | CS | DA | DS | DT | FD | FL | IS | LO | LT | PN | SH | SL | SS | ST | TM | UI | UL | US, // if( VR & VL16 ) => VR has its VL coded over 16bits
93  VL32 = OB | OW | OD | OF | SQ | UN | UT, // if( VR & VL32 ) => VR has its VL coded over 32bits
94  VRASCII = AE | AS | CS | DA | DS | DT | IS | LO | LT | PN | SH | ST | TM | UI | UT,
95  VRBINARY = AT | FL | FD | OB | OD | OF | OW | SL | SQ | SS | UL | UN | US, // FIXME: UN ?
96  // PS 3.5:
97  // Data Elements with a VR of SQ, OD, OF, OW, OB or UN shall always have a Value Multiplicity of one.
98  // GDCM is adding a couple more: AS, LT, ST, UT
99  VR_VM1 = AS | LT | ST | UT | SQ | OF | OD | OW | OB | UN, // All those VR have a VM1
100  VRALL = VRASCII | VRBINARY,
101  VR_END = UT+1 // Invalid VR, need to be max(VRType)+1
102  } VRType;
103 
104  static const char *GetVRString(VRType vr);
105 
106  // This function will only look at the very first two chars nothing else
107  static VRType GetVRTypeFromFile(const char *vr);
108 
109  // You need to make sure end of string is \0
110  static VRType GetVRType(const char *vr);
111  static const char *GetVRStringFromFile(VRType vr);
112 
113  static bool IsValid(const char *vr);
114  // Check if vr1 is valid against vr2,
115  // Typically vr1 is read from the file and vr2 is taken from the dict
116  static bool IsValid(const char *vr1, VRType vr2);
117  //static bool IsValid(const VRType &vr1, const VRType &vr2);
118  // Find out if the string read is byte swapped
119  static bool IsSwap(const char *vr);
120 
121  // Size read on disk
122  // FIXME: int ?
123  int GetLength() const {
124  return VR::GetLength(VRField);
125  }
126  unsigned int GetSizeof() const;
127  static uint32_t GetLength(VRType vr) {
128  //if( vr == VR::INVALID ) return 4;
129  if( vr & VL32 )
130  {
131  return 4;
132  }
133  else
134  return 2;
135  }
136 
137  // Some use of template metaprograming with ugly macro
138  static bool IsBinary(VRType vr);
139  static bool IsASCII(VRType vr);
140  // TODO: REMOVE ME
141  static bool CanDisplay(VRType vr);
142  // TODO: REMOVE ME
143  static bool IsBinary2(VRType vr);
144  // TODO: REMOVE ME
145  static bool IsASCII2(VRType vr);
146 
147  VR(VRType vr = INVALID):VRField(vr) { }
148  //VR(VR const &vr):VRField(vr.VRField) { }
149  std::istream &Read(std::istream &is)
150  {
151  char vr[2];
152  is.read(vr, 2);
153  VRField = GetVRTypeFromFile(vr);
154  assert( VRField != VR::VR_END );
155  //assert( VRField != VR::INVALID );
156  if( VRField == VR::INVALID ) throw Exception( "INVALID VR" );
157  if( VRField & VL32 )
158  {
159 #if 0
160  // For some reason this seems slower on my linux box...
161  is.seekg(2, std::ios::cur );
162 #else
163  char dum[2];
164  is.read(dum, 2);
165  if( !(dum[0] == 0 && dum[1] == 0 ))
166  {
167  // JDDICOM_Sample4.dcm
168  gdcmDebugMacro( "32bits VR contains non zero bytes. Skipped" );
169  }
170 #endif
171  }
172  return is;
173  }
174 
175  const std::ostream &Write(std::ostream &os) const
176  {
177  VRType vrfield = VRField;
178  gdcmAssertAlwaysMacro( !IsDual() );
179  if( vrfield == VR::INVALID )
180  {
181  //vrfield = VR::UN;
182  }
183  const char *vr = GetVRString(vrfield);
184  //assert( strlen( vr ) == 2 );
185  assert( vr[0] && vr[1] && vr[2] == 0 );
186  os.write(vr, 2);
187  // See PS 3.5, Data Element Structure With Explicit VR
188  if( vrfield & VL32 )
189  {
190  const char dum[2] = {0, 0};
191  os.write(dum,2);
192  }
193  return os;
194  }
195  friend std::ostream &operator<<(std::ostream &os, const VR &vr);
196 
197  operator VRType () const { return VRField; }
198 
199  unsigned int GetSize() const;
200 
201  bool Compatible(VR const &vr) const;
202 
203  bool IsVRFile() const;
204 
205  bool IsDual() const;
206 
207 private:
208  // Internal function that map a VRType to an index in the VRStrings table
209  static int GetIndex(VRType vr);
210  VRType VRField;
211 };
212 //-----------------------------------------------------------------------------
213 inline std::ostream &operator<<(std::ostream &_os, const VR &val)
214 {
215  //_os << VR::GetVRStringFromFile(val.VRField);
216  _os << VR::GetVRString(val.VRField);
217  return _os;
218 }
219 
220 // Apparently SWIG is not happy with something, somewhere below...
221 #ifndef SWIG
222 
223 // Tells whether VR Type is ASCII or Binary
224 template<int T> struct VRToEncoding;
225 // Convert from VR Type to real underlying type
226 template<int T> struct VRToType;
227 #define TYPETOENCODING(type,rep, rtype) \
228  template<> struct VRToEncoding<VR::type> \
229  { enum { Mode = VR::rep }; }; \
230  template<> struct VRToType<VR::type> \
231  { typedef rtype Type; };
232 
233 
234 // Do not use me
235 struct UI { char Internal[64+1];
236  friend std::ostream& operator<<(std::ostream &_os, const UI &_val);
237 };
238 inline std::ostream& operator<<(std::ostream &_os, const UI &_val)
239 {
240  _os << _val.Internal;
241  return _os;
242 }
243 
257 
258 
259 // TODO: Could be generated from XML file
260 TYPETOENCODING(AE,VRASCII ,AEComp)
261 TYPETOENCODING(AS,VRASCII ,ASComp)
263 TYPETOENCODING(CS,VRASCII ,CSComp)
264 TYPETOENCODING(DA,VRASCII ,DAComp)
265 TYPETOENCODING(DS,VRASCII ,double)
266 TYPETOENCODING(DT,VRASCII ,DTComp)
267 TYPETOENCODING(FL,VRBINARY,float)
268 TYPETOENCODING(FD,VRBINARY,double)
269 TYPETOENCODING(IS,VRASCII ,int32_t)
270 TYPETOENCODING(LO,VRASCII ,LOComp)
271 TYPETOENCODING(LT,VRASCII ,LTComp)
272 TYPETOENCODING(OB,VRBINARY,uint8_t)
273 TYPETOENCODING(OF,VRBINARY,float)
274 TYPETOENCODING(OW,VRBINARY,uint16_t)
275 TYPETOENCODING(PN,VRASCII ,PNComp)
276 TYPETOENCODING(SH,VRASCII ,SHComp)
277 TYPETOENCODING(SL,VRBINARY,int32_t)
278 TYPETOENCODING(SQ,VRBINARY,unsigned char) // FIXME
279 TYPETOENCODING(SS,VRBINARY,int16_t)
280 TYPETOENCODING(ST,VRASCII ,STComp)
281 TYPETOENCODING(TM,VRASCII ,TMComp)
282 TYPETOENCODING(UI,VRASCII ,UIComp)
283 TYPETOENCODING(UL,VRBINARY,uint32_t)
284 TYPETOENCODING(UN,VRBINARY,uint8_t) // FIXME ?
285 TYPETOENCODING(US,VRBINARY,uint16_t)
286 TYPETOENCODING(UT,VRASCII ,UTComp)
287 
288 #define VRTypeTemplateCase(type) \
289  case VR::type: \
290  return sizeof ( VRToType<VR::type>::Type );
291 
292 inline unsigned int VR::GetSize() const
293 {
294  switch(VRField)
295  {
323  case VR::US_SS:
324  return 2;
325  default:
326  assert( 0 && "should not" );
327  }
328  return 0;
329 }
330 #endif // SWIG
331 
332 
333 } // end namespace gdcm
334 
335 #endif //GDCMVR_H
String<'\\', 16 > AEComp
Definition: gdcmVR.h:244
Definition: gdcmVR.h:235
Definition: gdcmVR.h:59
Definition: gdcmVR.h:224
VRType
Definition: gdcmVR.h:57
VRBINARY
Definition: gdcmVR.h:284
Definition: gdcmVR.h:226
#define GDCM_EXPORT
Definition: gdcmWin32.h:34
int GetLength() const
Definition: gdcmVR.h:123
static const char * GetVRString(VRType vr)
String<'\\', 64 > ASComp
Definition: gdcmVR.h:245
String<'\\', 64 > STComp
Definition: gdcmVR.h:253
#define gdcmDebugMacro(msg)
Debug.
Definition: gdcmTrace.h:119
std::ostream & operator<<(std::ostream &os, const Directory &d)
Definition: gdcmDirectory.h:88
#define gdcmAssertAlwaysMacro(arg)
AssertAlways.
Definition: gdcmTrace.h:223
Definition: gdcmVR.h:101
String<'\\', 64 > LOComp
Definition: gdcmVR.h:249
Definition: gdcmVR.h:89
unsigned int GetSize() const
Definition: gdcmVR.h:292
#define VRTypeTemplateCase(type)
Definition: gdcmVR.h:288
String<'\\', 64 > DTComp
Definition: gdcmVR.h:248
String<'\\', 16 > CSComp
Definition: gdcmVR.h:246
static uint32_t GetLength(VRType vr)
Definition: gdcmVR.h:127
std::istream & Read(std::istream &is)
Definition: gdcmVR.h:149
TYPETOENCODING(SQ, VRBINARY, unsigned char) TYPETOENCODING(UN
LO.
Definition: gdcmLO.h:27
friend std::ostream & operator<<(std::ostream &_os, const UI &_val)
Definition: gdcmVR.h:238
VR(VRType vr=INVALID)
Definition: gdcmVR.h:147
Definition: gdcmVR.h:60
String<'\\', 16 > TMComp
Definition: gdcmVR.h:254
String.
Definition: gdcmString.h:31
String<'\\', 64 > SHComp
Definition: gdcmVR.h:252
String<'\\', 64 > LTComp
Definition: gdcmVR.h:250
String<'\\', 64, 0 > UIComp
Definition: gdcmVR.h:255
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element). Basically an uint32_t which...
Definition: gdcmTag.h:38
VR class This is adapted from DICOM standard The biggest difference is the INVALID VR and the composi...
Definition: gdcmVR.h:54
String<'\\', 64 > UTComp
Definition: gdcmVR.h:256
Definition: gdcmASN1.h:20
String<'\\', 64 > DAComp
Definition: gdcmVR.h:247
char Internal[64+1]
Definition: gdcmVR.h:235
Exception.
Definition: gdcmException.h:43
String<'\\', 64 > PNComp
Definition: gdcmVR.h:251
const std::ostream & Write(std::ostream &os) const
Definition: gdcmVR.h:175

Generated on Fri Sep 25 2015 17:58:24 for GDCM by doxygen 1.8.9.1
SourceForge.net Logo