GDCM
2.0.18
|
00001 /*========================================================================= 00002 00003 Program: GDCM (Grassroots DICOM). A DICOM library 00004 00005 Copyright (c) 2006-2011 Mathieu Malaterre 00006 All rights reserved. 00007 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details. 00008 00009 This software is distributed WITHOUT ANY WARRANTY; without even 00010 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00011 PURPOSE. See the above copyright notice for more information. 00012 00013 =========================================================================*/ 00014 #ifndef GDCMVL_H 00015 #define GDCMVL_H 00016 00017 #include "gdcmTypes.h" 00018 00019 #include <iostream> 00020 00021 namespace gdcm 00022 { 00023 00029 class GDCM_EXPORT VL 00030 { 00031 public: 00032 typedef uint32_t Type; 00033 VL(uint32_t vl = 0) : ValueLength(vl) { } 00034 00035 // FIXME: ugly 00036 static uint32_t GetVL32Max() { return 0xFFFFFFFF; } 00037 static uint16_t GetVL16Max() { return 0xFFFF; } 00038 00039 bool IsUndefined() const { 00040 return ValueLength == 0xFFFFFFFF; 00041 } 00042 void SetToUndefined() { 00043 ValueLength = 0xFFFFFFFF; 00044 } 00045 00047 bool IsOdd() const { 00048 return !IsUndefined() && ValueLength % 2; 00049 } 00050 00052 VL& operator+=(VL const &vl) { 00053 ValueLength += vl.ValueLength; 00054 return *this; 00055 } 00056 VL& operator++() { 00057 ++ValueLength; 00058 return *this; 00059 } 00060 VL operator++(int) { 00061 uint32_t tmp(ValueLength); 00062 ++ValueLength; 00063 return tmp; 00064 } 00065 00066 operator uint32_t () const { return ValueLength; } 00067 00068 VL GetLength() const { 00069 // VL cannot know it's length...well in implicit yes... 00070 // TODO: need to check we cannot call this function from an Explicit element 00071 return 4; 00072 } 00073 00074 friend std::ostream& operator<<(std::ostream& os, const VL& vl); 00075 00076 // PURPOSELY not implemented (could not differenciate 16bits vs 32bits VL) 00077 //friend std::istream& operator>>(std::istream& is, VL& n); 00078 00079 template <typename TSwap> 00080 std::istream &Read(std::istream &is) 00081 { 00082 is.read((char*)(&ValueLength), sizeof(uint32_t)); 00083 TSwap::SwapArray(&ValueLength,1); 00084 return is; 00085 } 00086 00087 template <typename TSwap> 00088 std::istream &Read16(std::istream &is) 00089 { 00090 uint16_t copy; 00091 is.read((char*)(©), sizeof(uint16_t)); 00092 TSwap::SwapArray(©,1); 00093 ValueLength = copy; 00094 assert( ValueLength <= 65535 /*UINT16_MAX*/ ); // ?? doh ! 00095 return is; 00096 } 00097 00098 template <typename TSwap> 00099 const std::ostream &Write(std::ostream &os) const 00100 { 00101 uint32_t copy = ValueLength; 00102 #ifndef GDCM_WRITE_ODD_LENGTH 00103 if( IsOdd() ) 00104 { 00105 ++copy; 00106 } 00107 #endif 00108 TSwap::SwapArray(©,1); 00109 return os.write((char*)(©), sizeof(uint32_t)); 00110 } 00111 00112 template <typename TSwap> 00113 const std::ostream &Write16(std::ostream &os) const 00114 { 00115 assert( ValueLength <= 65535 /*UINT16_MAX*/ ); 00116 uint16_t copy = ValueLength; 00117 #ifndef GDCM_WRITE_ODD_LENGTH 00118 if( IsOdd() ) 00119 { 00120 ++copy; 00121 } 00122 #endif 00123 TSwap::SwapArray(©,1); 00124 return os.write((char*)(©), sizeof(uint16_t)); 00125 } 00126 00127 private: 00128 uint32_t ValueLength; 00129 }; 00130 //---------------------------------------------------------------------------- 00131 inline std::ostream& operator<<(std::ostream& os, const VL& val) 00132 { 00133 os << /*std::hex <<*/ val.ValueLength; 00134 return os; 00135 } 00136 00137 } // end namespace gdcm 00138 00139 #endif //GDCMVL_H