GDCM: gdcmVL.h Source File

GDCM 3.2.10

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
21namespace gdcm
22{
23
29 class GDCM_EXPORT VL
30{
31public:
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 differentiate 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 gdcm_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 if( IsOdd() )
103 {
104 ++copy;
105 }
106 TSwap::SwapArray(&copy,1);
107 return os.write((char*)(&copy), sizeof(uint32_t));
108 }
109
110 template <typename TSwap>
111 const std::ostream &Write16(std::ostream &os) const
112 {
113 gdcm_assert( ValueLength <= 65535 /*UINT16_MAX*/ );
114 uint16_t copy = (uint16_t)ValueLength;
115 if( IsOdd() )
116 {
117 ++copy;
118 }
119 TSwap::SwapArray(&copy,1);
120 return os.write((char*)(&copy), sizeof(uint16_t));
121 }
122
123private:
124 uint32_t ValueLength;
125};
126//----------------------------------------------------------------------------
127 inline std::ostream& operator<<(std::ostream& os, const VL& val)
128{
129 os << /*std::hex <<*/ val.ValueLength;
130 return os;
131}
132
133} // end namespace gdcm
134
135#endif //GDCMVL_H
gdcm::VL
Value Length.
Definition gdcmVL.h:30
gdcm::VL::IsOdd
bool IsOdd() const
Return whether or not the VL is odd or not.
Definition gdcmVL.h:47
gdcm::VL::Read
std::istream & Read(std::istream &is)
Definition gdcmVL.h:80
gdcm::VL::Write16
const std::ostream & Write16(std::ostream &os) const
Definition gdcmVL.h:111
gdcm::VL::Write
const std::ostream & Write(std::ostream &os) const
Definition gdcmVL.h:99
gdcm::VL::GetVL32Max
static uint32_t GetVL32Max()
Definition gdcmVL.h:36
gdcm::VL::VL
VL(uint32_t vl=0)
Definition gdcmVL.h:33
gdcm::VL::Type
uint32_t Type
Definition gdcmVL.h:32
gdcm::VL::GetLength
VL GetLength() const
Definition gdcmVL.h:68
gdcm::VL::GetVL16Max
static uint16_t GetVL16Max()
Definition gdcmVL.h:37
gdcm::VL::operator+=
VL & operator+=(VL const &vl)
+= operator
Definition gdcmVL.h:52
gdcm::VL::operator++
VL & operator++()
Definition gdcmVL.h:56
gdcm::VL::operator++
VL operator++(int)
Definition gdcmVL.h:60
gdcm::VL::IsUndefined
bool IsUndefined() const
Definition gdcmVL.h:39
gdcm::VL::Read16
std::istream & Read16(std::istream &is)
Definition gdcmVL.h:88
gdcm::VL::SetToUndefined
void SetToUndefined()
Definition gdcmVL.h:42
gdcm_assert
#define gdcm_assert(cond)
Definition gdcmException.h:104
gdcmTypes.h
GDCM_EXPORT
#define GDCM_EXPORT
Definition gdcmWin32.h:34
gdcm
Definition gdcmASN1.h:21
gdcm::operator<<
std::ostream & operator<<(std::ostream &os, const Directory &d)
Definition gdcmDirectory.h:88