gdcmVR.h
Go to the documentation of this file.
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#ifndef GDCMVR_H
#define GDCMVR_H
#include "gdcmTag.h"
#include "gdcmTrace.h"
#include "gdcmString.h"
#include <iostream>
#include <fstream>
#include <assert.h>
//these defines are here to ensure compilation on sunos gcc
#if defined (CS)
# undef CS
#endif
#if defined (DS)
# undef DS
#endif
#if defined (SS)
# undef SS
#endif
namespace gdcm
{
class GDCM_EXPORT VR
{
public:
enum VRType : long long {
// Warning: Do not write if ( vr & VR::INVALID ) but if ( vr == VR::INVALID )
INVALID = 0, // For Item/(Seq) Item Delimitation Item
AE = 1,
AS = 2,
AT = 4,
CS = 8,
DA = 16,
DS = 32,
DT = 64,
FD = 128,
FL = 256,
IS = 512,
LO = 1024,
LT = 2048,
OB = 4096,
OD = 134217728, // 2^27
OF = 8192,
OL = 268435456, // 2^28
OV = 2147483648, // 2^31
OW = 16384,
PN = 32768,
SH = 65536,
SL = 131072,
SQ = 262144,
SS = 524288,
ST = 1048576,
SV = 4294967296, // 2^32
TM = 2097152,
UC = 536870912, // 2^29
UI = 4194304,
UL = 8388608,
UN = 16777216,
UR = 1073741824, // 2^30
US = 33554432,
UT = 67108864,
UV = 8589934592, // 2^33
OB_OW = OB | OW,
US_SS = US | SS,
US_SS_OW = US | SS | OW,
US_OW = US | OW,
// The following do not have a VRString equivalent (ie cannot be found in PS 3.6)
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
VL32 = OB | OW | OD | OF | OL | OV | SQ | SV | UC | UN | UR | UT | UV, // if( VR & VL32 ) => VR has its VL coded over 32bits
VRASCII = AE | AS | CS | DA | DS | DT | IS | LO | LT | PN | SH | ST | TM | UC | UI | UR | UT,
VRBINARY = AT | FL | FD | OB | OD | OF | OL | OV | OW | SL | SQ | SS | SV | UL | UN | US | UV, // FIXME: UN ?
// PS 3.5:
// Data Elements with a VR of SQ, OD, OF, OL, OW, OB or UN shall always have a Value Multiplicity of one.
// GDCM is adding a couple more: AS, LT, ST, UT
VR_VM1 = AS | LT | ST | UT | SQ | OF | OL | OV | OD | OW | OB | UN, // All those VR have a VM1
VRALL = VRASCII | VRBINARY,
VR_END = UV+1 // Invalid VR, need to be max(VRType)+1
};
static const char *GetVRString(VRType vr);
// This function will only look at the very first two chars nothing else
static VRType GetVRTypeFromFile(const char *vr);
// You need to make sure end of string is \0
static VRType GetVRType(const char *vr);
static const char *GetVRStringFromFile(VRType vr);
static bool IsValid(const char *vr);
// Check if vr1 is valid against vr2,
// Typically vr1 is read from the file and vr2 is taken from the dict
static bool IsValid(const char *vr1, VRType vr2);
//static bool IsValid(const VRType &vr1, const VRType &vr2);
// Find out if the string read is byte swapped
static bool IsSwap(const char *vr);
// Size read on disk
// FIXME: int ?
int GetLength() const {
return VR::GetLength(VRField);
}
unsigned int GetSizeof() const;
static uint32_t GetLength(VRType vr) {
//if( vr == VR::INVALID ) return 4;
if( vr & VL32 )
{
return 4;
}
else
return 2;
}
// Some use of template metaprograming with ugly macro
static bool IsBinary(VRType vr);
static bool IsASCII(VRType vr);
// TODO: REMOVE ME
static bool CanDisplay(VRType vr);
// TODO: REMOVE ME
static bool IsBinary2(VRType vr);
// TODO: REMOVE ME
static bool IsASCII2(VRType vr);
VR(VRType vr = INVALID):VRField(vr) { }
//VR(VR const &vr):VRField(vr.VRField) { }
std::istream &Read(std::istream &is)
{
char vr[2];
is.read(vr, 2);
VRField = GetVRTypeFromFile(vr);
assert( VRField != VR::VR_END );
if( VRField == VR::INVALID )
{
// \0\2 Data/TheralysGDCM120Bug.dcm
// \0\0 Data/MR_Philips_Intera_PrivateSequenceExplicitVR_in_SQ_2001_e05f_item_wrong_lgt_use_NOSHADOWSEQ.dcm
// \0\4 Data/BugGDCM2_UndefItemWrongVL.dcm
// \44\0 Data/gdcm-MR-PHILIPS-16-Multi-Seq.dcm
// \0\20 Data/ExplicitVRforPublicElementsImplicitVRforShadowElements.dcm
// \0\3 Data/DMCPACS_ExplicitImplicit_BogusIOP.dcm
// \0\4 Data/THERALYS-12-MONO2-Uncompressed-Even_Length_Tag.dcm
// \0\4 Data/PrivateGEImplicitVRBigEndianTransferSyntax16Bits.dcm
// \0\4 Data/GE_DLX-8-MONO2-PrivateSyntax.dcm
throw Exception( "INVALID VR" );
}
if( VRField & VL32 )
{
#if 0
// For some reason this seems slower on my linux box...
is.seekg(2, std::ios::cur );
#else
char dumb[2];
is.read(dumb, 2);
if( !(dumb[0] == 0 && dumb[1] == 0 ))
{
// JDDICOM_Sample4.dcm
gdcmDebugMacro( "32bits VR contains non zero bytes. Skipped" );
}
#endif
}
return is;
}
const std::ostream &Write(std::ostream &os) const
{
VRType vrfield = VRField;
gdcmAssertAlwaysMacro( !IsDual() );
if( vrfield == VR::INVALID )
{
//vrfield = VR::UN;
}
const char *vr = GetVRString(vrfield);
//assert( strlen( vr ) == 2 );
assert( vr[0] && vr[1] && vr[2] == 0 );
os.write(vr, 2);
// See PS 3.5, Data Element Structure With Explicit VR
if( vrfield & VL32 )
{
const char dumb[2] = {0, 0};
os.write(dumb,2);
}
return os;
}
friend std::ostream &operator<<(std::ostream &os, const VR &vr);
operator VRType () const { return VRField; }
unsigned int GetSize() const;
bool Compatible(VR const &vr) const;
bool IsVRFile() const;
bool IsDual() const;
private:
// Internal function that map a VRType to an index in the VRStrings table
static unsigned int GetIndex(VRType vr);
VRType VRField;
};
//-----------------------------------------------------------------------------
inline std::ostream &operator<<(std::ostream &_os, const VR &val)
{
//_os << VR::GetVRStringFromFile(val.VRField);
_os << VR::GetVRString(val.VRField);
return _os;
}
// Apparently SWIG is not happy with something, somewhere below...
#ifndef SWIG
// Tells whether VR Type is ASCII or Binary
template<long long T> struct VRToEncoding;
// Convert from VR Type to real underlying type
template<long long T> struct VRToType;
#define TYPETOENCODING(type,rep, rtype) \
template<> struct VRToEncoding<VR::type> \
{ enum:long long { Mode = VR::rep }; }; \
template<> struct VRToType<VR::type> \
{ typedef rtype Type; };
// Do not use me
struct UI { char Internal[64+1];
friend std::ostream& operator<<(std::ostream &_os, const UI &_val);
};
inline std::ostream& operator<<(std::ostream &_os, const UI &_val)
{
_os << _val.Internal;
return _os;
}
typedef String<'\\',16> AEComp;
typedef String<'\\',64> ASComp;
typedef String<'\\',16> CSComp;
typedef String<'\\',64> DAComp;
typedef String<'\\',64> DTComp;
typedef String<'\\',64> LOComp;
typedef String<'\\',64> LTComp;
typedef String<'\\',64> PNComp;
typedef String<'\\',64> SHComp;
typedef String<'\\',64> STComp;
typedef String<'\\',4294967294> UCComp;
typedef String<'\\',4294967294> URComp;
typedef String<'\\',16> TMComp;
typedef String<'\\',64,0> UIComp;
typedef String<'\\',64> UTComp;
// TODO: Could be generated from XML file
TYPETOENCODING(AE,VRASCII ,AEComp)
TYPETOENCODING(AS,VRASCII ,ASComp)
TYPETOENCODING(AT,VRBINARY,Tag)
TYPETOENCODING(CS,VRASCII ,CSComp)
TYPETOENCODING(DA,VRASCII ,DAComp)
TYPETOENCODING(DS,VRASCII ,double)
TYPETOENCODING(DT,VRASCII ,DTComp)
TYPETOENCODING(FL,VRBINARY,float)
TYPETOENCODING(FD,VRBINARY,double)
TYPETOENCODING(IS,VRASCII ,int32_t)
TYPETOENCODING(LO,VRASCII ,LOComp)
TYPETOENCODING(LT,VRASCII ,LTComp)
TYPETOENCODING(OB,VRBINARY,uint8_t)
TYPETOENCODING(OD,VRBINARY,double)
TYPETOENCODING(OF,VRBINARY,float)
TYPETOENCODING(OL,VRBINARY,uint32_t)
TYPETOENCODING(OV,VRBINARY,uint64_t)
TYPETOENCODING(OW,VRBINARY,uint16_t)
TYPETOENCODING(PN,VRASCII ,PNComp)
TYPETOENCODING(SH,VRASCII ,SHComp)
TYPETOENCODING(SL,VRBINARY,int32_t)
TYPETOENCODING(SQ,VRBINARY,unsigned char) // FIXME
TYPETOENCODING(SS,VRBINARY,int16_t)
TYPETOENCODING(ST,VRASCII ,STComp)
TYPETOENCODING(SV,VRBINARY,int64_t)
TYPETOENCODING(TM,VRASCII ,TMComp)
TYPETOENCODING(UC,VRASCII ,UCComp)
TYPETOENCODING(UI,VRASCII ,UIComp)
TYPETOENCODING(UL,VRBINARY,uint32_t)
TYPETOENCODING(UN,VRBINARY,uint8_t) // FIXME ?
TYPETOENCODING(UR,VRASCII,URComp)
TYPETOENCODING(US,VRBINARY,uint16_t)
TYPETOENCODING(UT,VRASCII ,UTComp)
TYPETOENCODING(UV,VRBINARY,uint64_t)
#define VRTypeTemplateCase(type) \
case VR::type: \
return sizeof ( VRToType<VR::type>::Type );
inline unsigned int VR::GetSize() const
{
switch(VRField)
{
VRTypeTemplateCase(AE)
VRTypeTemplateCase(AS)
VRTypeTemplateCase(AT)
VRTypeTemplateCase(CS)
VRTypeTemplateCase(DA)
VRTypeTemplateCase(DS)
VRTypeTemplateCase(DT)
VRTypeTemplateCase(FL)
VRTypeTemplateCase(FD)
VRTypeTemplateCase(IS)
VRTypeTemplateCase(LO)
VRTypeTemplateCase(LT)
VRTypeTemplateCase(OB)
VRTypeTemplateCase(OD)
VRTypeTemplateCase(OF)
VRTypeTemplateCase(OL)
VRTypeTemplateCase(OV)
VRTypeTemplateCase(OW)
VRTypeTemplateCase(PN)
VRTypeTemplateCase(SH)
VRTypeTemplateCase(SL)
VRTypeTemplateCase(SQ)
VRTypeTemplateCase(SS)
VRTypeTemplateCase(ST)
VRTypeTemplateCase(SV)
VRTypeTemplateCase(TM)
VRTypeTemplateCase(UC)
VRTypeTemplateCase(UI)
VRTypeTemplateCase(UL)
VRTypeTemplateCase(UN)
VRTypeTemplateCase(UR)
VRTypeTemplateCase(US)
VRTypeTemplateCase(UT)
VRTypeTemplateCase(UV)
case VR::US_SS:
return 2;
case VR::INVALID:
case VR::OB_OW:
case VR::US_SS_OW:
case VR::US_OW:
case VR::VL16:
case VR::VL32:
case VR::VRASCII:
case VR::VRBINARY:
case VR::VR_VM1:
case VR::VRALL:
case VR::VR_END:
default:
assert( 0 && "should not" );
}
return 0;
}
#endif // SWIG
} // end namespace gdcm
#endif //GDCMVR_H
gdcm::Exception
Exception.
Definition gdcmException.h:44
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)
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