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