GDCM 3.0.26

gdcmByteValue.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 GDCMBYTEVALUE_H
15#define GDCMBYTEVALUE_H
16
17#include "gdcmValue.h"
18#include "gdcmTrace.h"
19#include "gdcmVL.h"
20
21#include <vector>
22#include <iterator>
23#include <iomanip>
24#include <algorithm>
25
26 namespace gdcm_ns
27{
28#if !defined(SWIGPYTHON) && !defined(SWIGCSHARP) && !defined(SWIGJAVA) && !defined(SWIGPHP)
29 using namespace gdcm;
30#endif
34 class GDCM_EXPORT ByteValue : public Value
35{
36 public:
37 ByteValue(const char* array = nullptr, VL const &vl = 0):
38 Internal(array, array+vl),Length(vl) {
39 if( vl.IsOdd() )
40 {
41 gdcmDebugMacro( "Odd length" );
42 Internal.resize(vl+1);
43 ++Length;
44 }
45 }
46
48 ByteValue(std::vector<char> &v):Internal(v),Length((uint32_t)v.size()) {}
49 //ByteValue(std::ostringstream const &os) {
50 // (void)os;
51 // assert(0); // TODO
52 //}
53 ~ByteValue() override {
54 Internal.clear();
55 }
56
57 // When 'dumping' dicom file we still have some information from
58 // Either the VR: eg LO (private tag)
59 void PrintASCII(std::ostream &os, VL maxlength ) const;
60
61 void PrintHex(std::ostream &os, VL maxlength) const;
62
63 // Either from Element Number (== 0x0000)
64 void PrintGroupLength(std::ostream &os) {
65 assert( Length == 2 );
66 (void)os;
67 }
68
69 bool IsEmpty() const {
70#if 0
71 if( Internal.empty() ) assert( Length == 0 );
72 return Internal.empty();
73#else
74 return Length == 0;
75#endif
76 }
77 VL GetLength() const override { return Length; }
78
79 VL ComputeLength() const { return Length + Length % 2; }
80 // Does a reallocation
81 void SetLength(VL vl) override;
82
83 operator const std::vector<char>& () const { return Internal; }
84
85 ByteValue &operator=(const ByteValue &val) {
86 Internal = val.Internal;
87 Length = val.Length;
88 return *this;
89 }
90
91 bool operator==(const ByteValue &val) const {
92 if( Length != val.Length )
93 return false;
94 if( Internal == val.Internal )
95 return true;
96 return false;
97 }
98 bool operator==(const Value &val) const override
99 {
100 const ByteValue &bv = dynamic_cast<const ByteValue&>(val);
101 return Length == bv.Length && Internal == bv.Internal;
102 }
103
104 void Append(ByteValue const & bv);
105
106 void Clear() override {
107 Internal.clear();
108 }
109 // Use that only if you understand what you are doing
110 const char *GetPointer() const {
111 if(!Internal.empty()) return &Internal[0];
112 return nullptr;
113 }
114 // Use that only if you really understand what you are doing
115 const void *GetVoidPointer() const {
116 if(!Internal.empty()) return &Internal[0];
117 return nullptr;
118 }
119 void *GetVoidPointer() {
120 if(!Internal.empty()) return &Internal[0];
121 return nullptr;
122 }
123 void Fill(char c) {
124 //if( Internal.empty() ) return;
125 std::vector<char>::iterator it = Internal.begin();
126 for(; it != Internal.end(); ++it) *it = c;
127 }
128 bool GetBuffer(char *buffer, unsigned long length) const;
129 bool WriteBuffer(std::ostream &os) const {
130 if( Length ) {
131 //assert( Internal.size() <= Length );
132 assert( !(Internal.size() % 2) );
133 os.write(&Internal[0], Internal.size() );
134 }
135 return true;
136 }
137
138 template <typename TSwap, typename TType>
139 std::istream &Read(std::istream &is, bool readvalues = true) {
140 // If Length is odd we have detected that in SetLength
141 // and calling std::vector::resize make sure to allocate *AND*
142 // initialize values to 0 so we are sure to have a \0 at the end
143 // even in this case
144 if(Length)
145 {
146 if( readvalues )
147 {
148 is.read(&Internal[0], Length);
149 assert( Internal.size() == Length || Internal.size() == Length + 1 );
150 TSwap::SwapArray((TType*)GetVoidPointer(), Internal.size() / sizeof(TType) );
151 }
152 else
153 {
154 is.seekg(Length, std::ios::cur);
155 }
156 }
157 return is;
158 }
159
160 template <typename TSwap>
161 std::istream &Read(std::istream &is) {
162 return Read<TSwap,uint8_t>(is);
163 }
164
165
166 template <typename TSwap, typename TType>
167 std::ostream const &Write(std::ostream &os) const {
168 assert( !(Internal.size() % 2) );
169 if( !Internal.empty() ) {
170 //os.write(&Internal[0], Internal.size());
171 std::vector<char> copy = Internal;
172 TSwap::SwapArray((TType*)(void*)&copy[0], Internal.size() / sizeof(TType) );
173 os.write(&copy[0], copy.size());
174 }
175 return os;
176 }
177
178 template <typename TSwap>
179 std::ostream const &Write(std::ostream &os) const {
180 return Write<TSwap,uint8_t>(os);
181 }
182
189 bool IsPrintable(VL length) const {
190 assert( length <= Length );
191 for(unsigned int i=0; i<length; i++)
192 {
193 if ( i == (length-1) && Internal[i] == '\0') continue;
194 if ( !( isprint((unsigned char)Internal[i]) || isspace((unsigned char)Internal[i]) ) )
195 {
196 //gdcmWarningMacro( "Cannot print :" << i );
197 return false;
198 }
199 }
200 return true;
201 }
202
204 void PrintPNXML(std::ostream &os) const;
205 void PrintASCIIXML(std::ostream &os) const;
206 void PrintHexXML(std::ostream &os) const;
207 protected:
208 void Print(std::ostream &os) const override {
209 // This is perfectly valid to have a Length = 0 , so we cannot check
210 // the length for printing
211 if( !Internal.empty() )
212 {
213 if( IsPrintable(Length) )
214 {
215 // WARNING: Internal.end() != Internal.begin()+Length
216 std::vector<char>::size_type length = Length;
217 if( Internal.back() == 0 ) --length;
218 std::copy(Internal.begin(), Internal.begin()+length,
219 std::ostream_iterator<char>(os));
220 }
221 else
222 os << "Loaded:" << Internal.size();
223 }
224 else
225 {
226 //os << "Not Loaded";
227 os << "(no value available)";
228 }
229 }
230/*
231//Introduce check for invalid XML characters
232 friend std::ostream& operator<<(std::ostream &os,const char c);
233*/
234
235 void SetLengthOnly(VL vl) override {
236 Length = vl;
237 }
238
239 private:
240 std::vector<char> Internal;
241
242 // WARNING Length IS NOT Internal.size() some *featured* DICOM
243 // implementation define odd length, we always load them as even number
244 // of byte, so we need to keep the right Length
245 VL Length;
246};
247
248} // end namespace gdcm_ns
249
250#endif //GDCMBYTEVALUE_H
gdcm::ByteValue::operator==
bool operator==(const ByteValue &val) const
Definition gdcmByteValue.h:91
gdcm::ByteValue::IsPrintable
bool IsPrintable(VL length) const
Checks whether a 'ByteValue' is printable or not (in order to avoid corrupting the terminal of invoca...
Definition gdcmByteValue.h:189
gdcm::ByteValue::Append
void Append(ByteValue const &bv)
gdcm::ByteValue::operator=
ByteValue & operator=(const ByteValue &val)
Definition gdcmByteValue.h:85
gdcm::ByteValue::Read
std::istream & Read(std::istream &is, bool readvalues=true)
Definition gdcmByteValue.h:139
gdcm::ByteValue::SetLengthOnly
void SetLengthOnly(VL vl) override
Definition gdcmByteValue.h:235
gdcm::ByteValue::operator==
bool operator==(const Value &val) const override
Definition gdcmByteValue.h:98
gdcm::ByteValue::IsEmpty
bool IsEmpty() const
Definition gdcmByteValue.h:69
gdcm::ByteValue::GetVoidPointer
const void * GetVoidPointer() const
Definition gdcmByteValue.h:115
gdcm::ByteValue::Write
std::ostream const & Write(std::ostream &os) const
Definition gdcmByteValue.h:179
gdcm::ByteValue::SetLength
void SetLength(VL vl) override
gdcm::ByteValue::Read
std::istream & Read(std::istream &is)
Definition gdcmByteValue.h:161
gdcm::ByteValue::GetBuffer
bool GetBuffer(char *buffer, unsigned long length) const
gdcm::ByteValue::Write
std::ostream const & Write(std::ostream &os) const
Definition gdcmByteValue.h:167
gdcm::ByteValue::PrintGroupLength
void PrintGroupLength(std::ostream &os)
Definition gdcmByteValue.h:64
gdcm::ByteValue::PrintPNXML
void PrintPNXML(std::ostream &os) const
gdcm::ByteValue::PrintASCIIXML
void PrintASCIIXML(std::ostream &os) const
gdcm::ByteValue::Fill
void Fill(char c)
Definition gdcmByteValue.h:123
gdcm::ByteValue::WriteBuffer
bool WriteBuffer(std::ostream &os) const
Definition gdcmByteValue.h:129
gdcm::ByteValue::GetPointer
const char * GetPointer() const
Definition gdcmByteValue.h:110
gdcm::ByteValue::PrintHexXML
void PrintHexXML(std::ostream &os) const
gdcm::ByteValue::Clear
void Clear() override
Definition gdcmByteValue.h:106
gdcm::ByteValue::ByteValue
ByteValue(const char *array=nullptr, VL const &vl=0)
Definition gdcmByteValue.h:37
gdcm::ByteValue::ComputeLength
VL ComputeLength() const
Definition gdcmByteValue.h:79
gdcm::ByteValue::Print
void Print(std::ostream &os) const override
Definition gdcmByteValue.h:208
gdcm::ByteValue::GetVoidPointer
void * GetVoidPointer()
Definition gdcmByteValue.h:119
gdcm::ByteValue::GetLength
VL GetLength() const override
Definition gdcmByteValue.h:77
gdcm::ByteValue::PrintHex
void PrintHex(std::ostream &os, VL maxlength) const
gdcm::ByteValue::~ByteValue
~ByteValue() override
Definition gdcmByteValue.h:53
gdcm::ByteValue::PrintASCII
void PrintASCII(std::ostream &os, VL maxlength) const
gdcm::ByteValue::ByteValue
ByteValue(std::vector< char > &v)
Definition gdcmByteValue.h:48
gdcm::VL
Value Length.
Definition gdcmVL.h:30
gdcm::Value::Value
Value()=default
gdcmTrace.h
gdcmVL.h
gdcmValue.h
GDCM_EXPORT
#define GDCM_EXPORT
Definition gdcmWin32.h:34