GDCM 3.2.1

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
54 class GDCM_EXPORT VR
55{
56 public:
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 gdcm_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 //gdcm_assert( strlen( vr ) == 2 );
203 gdcm_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
225 private:
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//-----------------------------------------------------------------------------
231 inline 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
242 template<long long T> struct VRToEncoding;
243// Convert from VR Type to real underlying type
244 template<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
253 struct UI { char Internal[64+1];
254 friend std::ostream& operator<<(std::ostream &_os, const UI &_val);
255};
256 inline std::ostream& operator<<(std::ostream &_os, const UI &_val)
257{
258 _os << _val.Internal;
259 return _os;
260}
261
262 typedef String<'\\',16> AEComp;
263 typedef String<'\\',64> ASComp;
264 typedef String<'\\',16> CSComp;
265 typedef String<'\\',64> DAComp;
266 typedef String<'\\',64> DTComp;
267 typedef String<'\\',64> LOComp;
268 typedef String<'\\',64> LTComp;
269 typedef String<'\\',64> PNComp;
270 typedef String<'\\',64> SHComp;
271 typedef String<'\\',64> STComp;
272 typedef String<'\\',4294967294> UCComp;
273 typedef String<'\\',4294967294> URComp;
274 typedef String<'\\',16> TMComp;
275 typedef String<'\\',64,0> UIComp;
276 typedef String<'\\',64> UTComp;
277
278
279// TODO: Could be generated from XML file
280 TYPETOENCODING(AE,VRASCII ,AEComp)
281 TYPETOENCODING(AS,VRASCII ,ASComp)
282 TYPETOENCODING(AT,VRBINARY,Tag)
283 TYPETOENCODING(CS,VRASCII ,CSComp)
284 TYPETOENCODING(DA,VRASCII ,DAComp)
285 TYPETOENCODING(DS,VRASCII ,double)
286 TYPETOENCODING(DT,VRASCII ,DTComp)
287 TYPETOENCODING(FL,VRBINARY,float)
288 TYPETOENCODING(FD,VRBINARY,double)
289 TYPETOENCODING(IS,VRASCII ,int32_t)
290 TYPETOENCODING(LO,VRASCII ,LOComp)
291 TYPETOENCODING(LT,VRASCII ,LTComp)
292 TYPETOENCODING(OB,VRBINARY,uint8_t)
293 TYPETOENCODING(OD,VRBINARY,double)
294 TYPETOENCODING(OF,VRBINARY,float)
295 TYPETOENCODING(OL,VRBINARY,uint32_t)
296 TYPETOENCODING(OV,VRBINARY,uint64_t)
297 TYPETOENCODING(OW,VRBINARY,uint16_t)
298 TYPETOENCODING(PN,VRASCII ,PNComp)
299 TYPETOENCODING(SH,VRASCII ,SHComp)
300 TYPETOENCODING(SL,VRBINARY,int32_t)
301 TYPETOENCODING(SQ,VRBINARY,unsigned char) // FIXME
302 TYPETOENCODING(SS,VRBINARY,int16_t)
303 TYPETOENCODING(ST,VRASCII ,STComp)
304 TYPETOENCODING(SV,VRBINARY,int64_t)
305 TYPETOENCODING(TM,VRASCII ,TMComp)
306 TYPETOENCODING(UC,VRASCII ,UCComp)
307 TYPETOENCODING(UI,VRASCII ,UIComp)
308 TYPETOENCODING(UL,VRBINARY,uint32_t)
309 TYPETOENCODING(UN,VRBINARY,uint8_t) // FIXME ?
310 TYPETOENCODING(UR,VRASCII,URComp)
311 TYPETOENCODING(US,VRBINARY,uint16_t)
312 TYPETOENCODING(UT,VRASCII ,UTComp)
313 TYPETOENCODING(UV,VRBINARY,uint64_t)
314
315#define VRTypeTemplateCase(type) \
316 case VR::type: \
317 return sizeof ( VRToType<VR::type>::Type );
318
319 inline unsigned int VR::GetSize() const
320{
321 switch(VRField)
322 {
323 VRTypeTemplateCase(AE)
324 VRTypeTemplateCase(AS)
325 VRTypeTemplateCase(AT)
326 VRTypeTemplateCase(CS)
327 VRTypeTemplateCase(DA)
328 VRTypeTemplateCase(DS)
329 VRTypeTemplateCase(DT)
330 VRTypeTemplateCase(FL)
331 VRTypeTemplateCase(FD)
332 VRTypeTemplateCase(IS)
333 VRTypeTemplateCase(LO)
334 VRTypeTemplateCase(LT)
335 VRTypeTemplateCase(OB)
336 VRTypeTemplateCase(OD)
337 VRTypeTemplateCase(OF)
338 VRTypeTemplateCase(OL)
339 VRTypeTemplateCase(OV)
340 VRTypeTemplateCase(OW)
341 VRTypeTemplateCase(PN)
342 VRTypeTemplateCase(SH)
343 VRTypeTemplateCase(SL)
344 VRTypeTemplateCase(SQ)
345 VRTypeTemplateCase(SS)
346 VRTypeTemplateCase(ST)
347 VRTypeTemplateCase(SV)
348 VRTypeTemplateCase(TM)
349 VRTypeTemplateCase(UC)
350 VRTypeTemplateCase(UI)
351 VRTypeTemplateCase(UL)
352 VRTypeTemplateCase(UN)
353 VRTypeTemplateCase(UR)
354 VRTypeTemplateCase(US)
355 VRTypeTemplateCase(UT)
356 VRTypeTemplateCase(UV)
357 case VR::US_SS:
358 return 2;
359
360 case VR::INVALID:
361 case VR::OB_OW:
362 case VR::US_SS_OW:
363 case VR::US_OW:
364 case VR::VL16:
365 case VR::VL32:
366 case VR::VRASCII:
367 case VR::VRBINARY:
368 case VR::VR_VM1:
369 case VR::VRALL:
370 case VR::VR_END:
371 default:
372 gdcm_assert( 0 && "should not" );
373 }
374 return 0;
375}
376#endif // SWIG
377
378
379} // end namespace gdcm
380
381#endif //GDCMVR_H
gdcm::Exception
Exception.
Definition gdcmException.h:46
gdcm::LO
LO.
Definition gdcmLO.h:28
gdcm::String
String.
Definition gdcmString.h:32
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::VR
VR class.
Definition gdcmVR.h:55
gdcm::VR::IsValid
static bool IsValid(const char *vr)
gdcm::VR::Read
std::istream & Read(std::istream &is)
Definition gdcmVR.h:156
gdcm::VR::IsASCII2
static bool IsASCII2(VRType vr)
gdcm::VR::IsDual
bool IsDual() const
gdcm::VR::IsBinary2
static bool IsBinary2(VRType vr)
gdcm::VR::GetLength
static uint32_t GetLength(VRType vr)
Definition gdcmVR.h:134
gdcm::VR::VR
VR(VRType vr=INVALID)
Definition gdcmVR.h:154
gdcm::VR::IsSwap
static bool IsSwap(const char *vr)
gdcm::VR::GetVRTypeFromFile
static VRType GetVRTypeFromFile(const char *vr)
gdcm::VR::Write
const std::ostream & Write(std::ostream &os) const
Definition gdcmVR.h:193
gdcm::VR::CanDisplay
static bool CanDisplay(VRType vr)
gdcm::VR::GetSizeof
unsigned int GetSizeof() const
gdcm::VR::Compatible
bool Compatible(VR const &vr) const
gdcm::VR::GetVRStringFromFile
static const char * GetVRStringFromFile(VRType vr)
gdcm::VR::IsValid
static bool IsValid(const char *vr1, VRType vr2)
gdcm::VR::GetSize
unsigned int GetSize() const
Definition gdcmVR.h:319
gdcm::VR::IsBinary
static bool IsBinary(VRType vr)
gdcm::VR::IsVRFile
bool IsVRFile() const
gdcm::VR::GetLength
int GetLength() const
Definition gdcmVR.h:130
gdcm::VR::GetVRType
static VRType GetVRType(const char *vr)
gdcm::VR::VRType
VRType
Definition gdcmVR.h:57
gdcm::VR::UC
@ UC
Definition gdcmVR.h:86
gdcm::VR::FL
@ FL
Definition gdcmVR.h:68
gdcm::VR::UL
@ UL
Definition gdcmVR.h:88
gdcm::VR::OL
@ OL
Definition gdcmVR.h:75
gdcm::VR::FD
@ FD
Definition gdcmVR.h:67
gdcm::VR::LT
@ LT
Definition gdcmVR.h:71
gdcm::VR::SH
@ SH
Definition gdcmVR.h:79
gdcm::VR::OB
@ OB
Definition gdcmVR.h:72
gdcm::VR::DT
@ DT
Definition gdcmVR.h:66
gdcm::VR::OW
@ OW
Definition gdcmVR.h:77
gdcm::VR::UT
@ UT
Definition gdcmVR.h:92
gdcm::VR::OF
@ OF
Definition gdcmVR.h:74
gdcm::VR::US_SS_OW
@ US_SS_OW
Definition gdcmVR.h:96
gdcm::VR::UV
@ UV
Definition gdcmVR.h:93
gdcm::VR::PN
@ PN
Definition gdcmVR.h:78
gdcm::VR::INVALID
@ INVALID
Definition gdcmVR.h:59
gdcm::VR::VL32
@ VL32
Definition gdcmVR.h:100
gdcm::VR::VL16
@ VL16
Definition gdcmVR.h:99
gdcm::VR::IS
@ IS
Definition gdcmVR.h:69
gdcm::VR::UI
@ UI
Definition gdcmVR.h:87
gdcm::VR::SL
@ SL
Definition gdcmVR.h:80
gdcm::VR::DS
@ DS
Definition gdcmVR.h:65
gdcm::VR::LO
@ LO
Definition gdcmVR.h:70
gdcm::VR::VR_VM1
@ VR_VM1
Definition gdcmVR.h:106
gdcm::VR::OV
@ OV
Definition gdcmVR.h:76
gdcm::VR::SS
@ SS
Definition gdcmVR.h:82
gdcm::VR::VRALL
@ VRALL
Definition gdcmVR.h:107
gdcm::VR::UR
@ UR
Definition gdcmVR.h:90
gdcm::VR::US_SS
@ US_SS
Definition gdcmVR.h:95
gdcm::VR::OB_OW
@ OB_OW
Definition gdcmVR.h:94
gdcm::VR::ST
@ ST
Definition gdcmVR.h:83
gdcm::VR::CS
@ CS
Definition gdcmVR.h:63
gdcm::VR::SV
@ SV
Definition gdcmVR.h:84
gdcm::VR::OD
@ OD
Definition gdcmVR.h:73
gdcm::VR::AT
@ AT
Definition gdcmVR.h:62
gdcm::VR::TM
@ TM
Definition gdcmVR.h:85
gdcm::VR::AS
@ AS
Definition gdcmVR.h:61
gdcm::VR::VRASCII
@ VRASCII
Definition gdcmVR.h:101
gdcm::VR::VRBINARY
@ VRBINARY
Definition gdcmVR.h:102
gdcm::VR::US
@ US
Definition gdcmVR.h:91
gdcm::VR::SQ
@ SQ
Definition gdcmVR.h:81
gdcm::VR::DA
@ DA
Definition gdcmVR.h:64
gdcm::VR::US_OW
@ US_OW
Definition gdcmVR.h:97
gdcm::VR::VR_END
@ VR_END
Definition gdcmVR.h:108
gdcm::VR::AE
@ AE
Definition gdcmVR.h:60
gdcm::VR::UN
@ UN
Definition gdcmVR.h:89
gdcm::VR::IsASCII
static bool IsASCII(VRType vr)
gdcm::VR::GetVRString
static const char * GetVRString(VRType vr)
gdcm_assert
#define gdcm_assert(cond)
Definition gdcmException.h:104
gdcmString.h
gdcmTag.h
gdcmTrace.h
gdcmAssertAlwaysMacro
#define gdcmAssertAlwaysMacro(arg)
AssertAlways.
Definition gdcmTrace.h:228
gdcmDebugMacro
#define gdcmDebugMacro(msg)
Debug.
Definition gdcmTrace.h:119
VRTypeTemplateCase
#define VRTypeTemplateCase(type)
Definition gdcmVR.h:315
TYPETOENCODING
#define TYPETOENCODING(type, rep, rtype)
Definition gdcmVR.h:245
GDCM_EXPORT
#define GDCM_EXPORT
Definition gdcmWin32.h:34
gdcm
Definition gdcmASN1.h:21
gdcm::PNComp
String<'\\', 64 > PNComp
Definition gdcmVR.h:269
gdcm::LOComp
String<'\\', 64 > LOComp
Definition gdcmVR.h:267
gdcm::UIComp
String<'\\', 64, 0 > UIComp
Definition gdcmVR.h:275
gdcm::AEComp
String<'\\', 16 > AEComp
Definition gdcmVR.h:262
gdcm::URComp
String<'\\', 4294967294 > URComp
Definition gdcmVR.h:273
gdcm::LTComp
String<'\\', 64 > LTComp
Definition gdcmVR.h:268
gdcm::DAComp
String<'\\', 64 > DAComp
Definition gdcmVR.h:265
gdcm::DTComp
String<'\\', 64 > DTComp
Definition gdcmVR.h:266
gdcm::operator<<
std::ostream & operator<<(std::ostream &os, const Directory &d)
Definition gdcmDirectory.h:88
gdcm::UCComp
String<'\\', 4294967294 > UCComp
Definition gdcmVR.h:272
gdcm::CSComp
String<'\\', 16 > CSComp
Definition gdcmVR.h:264
gdcm::TMComp
String<'\\', 16 > TMComp
Definition gdcmVR.h:274
gdcm::STComp
String<'\\', 64 > STComp
Definition gdcmVR.h:271
gdcm::ASComp
String<'\\', 64 > ASComp
Definition gdcmVR.h:263
gdcm::UTComp
String<'\\', 64 > UTComp
Definition gdcmVR.h:276
gdcm::SHComp
String<'\\', 64 > SHComp
Definition gdcmVR.h:270
gdcm::UI
Definition gdcmVR.h:253
gdcm::UI::Internal
char Internal[64+1]
Definition gdcmVR.h:253
gdcm::UI::operator<<
friend std::ostream & operator<<(std::ostream &_os, const UI &_val)
Definition gdcmVR.h:256
gdcm::VRToEncoding
Definition gdcmVR.h:242
gdcm::VRToType
Definition gdcmVR.h:244