GDCM 2.2.1

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

gdcmVL.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 GDCMVL_H
15 #define GDCMVL_H
16
17 #include "gdcmTypes.h"
18
19 #include <iostream>
20
21 namespace gdcm
22 {
23
29 class GDCM_EXPORT VL
30 {
31 public:
32 typedef uint32_t Type;
33 VL(uint32_t vl = 0) : ValueLength(vl) { }
34
35 // FIXME: ugly
36 static uint32_t GetVL32Max() { return 0xFFFFFFFF; }
37 static uint16_t GetVL16Max() { return 0xFFFF; }
38
39 bool IsUndefined() const {
40 return ValueLength == 0xFFFFFFFF;
41 }
42 void SetToUndefined() {
43 ValueLength = 0xFFFFFFFF;
44 }
45
47 bool IsOdd() const {
48 return !IsUndefined() && ValueLength % 2;
49 }
50
52 VL& operator+=(VL const &vl) {
53 ValueLength += vl.ValueLength;
54 return *this;
55 }
56 VL& operator++() {
57 ++ValueLength;
58 return *this;
59 }
60 VL operator++(int) {
61 uint32_t tmp(ValueLength);
62 ++ValueLength;
63 return tmp;
64 }
65
66 operator uint32_t () const { return ValueLength; }
67
68 VL GetLength() const {
69 // VL cannot know it's length...well in implicit yes...
70 // TODO: need to check we cannot call this function from an Explicit element
71 return 4;
72 }
73
74 friend std::ostream& operator<<(std::ostream& os, const VL& vl);
75
76 // PURPOSELY not implemented (could not differenciate 16bits vs 32bits VL)
77 //friend std::istream& operator>>(std::istream& is, VL& n);
78
79 template <typename TSwap>
80 std::istream &Read(std::istream &is)
81 {
82 is.read((char*)(&ValueLength), sizeof(uint32_t));
83 TSwap::SwapArray(&ValueLength,1);
84 return is;
85 }
86
87 template <typename TSwap>
88 std::istream &Read16(std::istream &is)
89 {
90 uint16_t copy;
91 is.read((char*)(&copy), sizeof(uint16_t));
92 TSwap::SwapArray(&copy,1);
93 ValueLength = copy;
94 assert( ValueLength <= 65535 /*UINT16_MAX*/ ); // ?? doh !
95 return is;
96 }
97
98 template <typename TSwap>
99 const std::ostream &Write(std::ostream &os) const
100 {
101 uint32_t copy = ValueLength;
102 #ifndef GDCM_WRITE_ODD_LENGTH
103 if( IsOdd() )
104 {
105 ++copy;
106 }
107 #else
108 assert( !IsOdd() );
109 #endif
110 TSwap::SwapArray(&copy,1);
111 return os.write((char*)(&copy), sizeof(uint32_t));
112 }
113
114 template <typename TSwap>
115 const std::ostream &Write16(std::ostream &os) const
116 {
117 assert( ValueLength <= 65535 /*UINT16_MAX*/ );
118 uint16_t copy = (uint16_t)ValueLength;
119 #ifndef GDCM_WRITE_ODD_LENGTH
120 if( IsOdd() )
121 {
122 ++copy;
123 }
124 #else
125 assert( !IsOdd() );
126 #endif
127 TSwap::SwapArray(&copy,1);
128 return os.write((char*)(&copy), sizeof(uint16_t));
129 }
130
131 private:
132 uint32_t ValueLength;
133 };
134 //----------------------------------------------------------------------------
135 inline std::ostream& operator<<(std::ostream& os, const VL& val)
136 {
137 os << /*std::hex <<*/ val.ValueLength;
138 return os;
139 }
140
141 } // end namespace gdcm
142
143 #endif //GDCMVL_H