GDCM 3.2.1

gdcmDataElement.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 GDCMDATAELEMENT_H
15#define GDCMDATAELEMENT_H
16
17#include "gdcmTag.h"
18#include "gdcmVL.h"
19#include "gdcmVR.h"
20#include "gdcmByteValue.h"
21#include "gdcmSmartPointer.h"
22
23#include <set>
24
25 namespace gdcm_ns
26{
27// Data Element
28// Contains multiple fields:
29// -> Tag
30// -> Optional VR (Explicit Transfer Syntax)
31// -> ValueLength
32// -> Value
33// TODO: This class SHOULD be pure virtual. I don't want a user
34// to shoot himself in the foot.
35
36 class SequenceOfItems;
37 class SequenceOfFragments;
58 class GDCM_EXPORT DataElement
59{
60 public:
61 DataElement(const Tag& t = Tag(0), const VL& vl = 0, const VR &vr = VR::INVALID):TagField(t),ValueLengthField(vl),VRField(vr),ValueField(nullptr) {}
62 //DataElement( Attribute const &att );
63
64 friend std::ostream& operator<<(std::ostream &_os, const DataElement &_val);
65
67 const Tag& GetTag() const { return TagField; }
68 Tag& GetTag() { return TagField; }
71 void SetTag(const Tag &t) { TagField = t; }
72
74 const VL& GetVL() const { return ValueLengthField; }
75 VL& GetVL() { return ValueLengthField; }
79 void SetVL(const VL &vl) { ValueLengthField = vl; }
80 void SetVLToUndefined();
81
84 VR const &GetVR() const { return VRField; }
88 void SetVR(VR const &vr) {
89 if( vr.IsVRFile() )
90 VRField = vr;
91 }
92
94 Value const &GetValue() const { gdcmAssertAlwaysMacro(ValueField); return *ValueField; }
95 Value &GetValue() {
96 gdcmAssertAlwaysMacro(ValueField);
97 return *ValueField;
98 }
99
100 void SetValue(Value const & vl) {
101 //gdcm_assert( ValueField == 0 );
102 ValueField = vl;
103 ValueLengthField = vl.GetLength();
104 }
105
106 bool IsEmpty() const { return ValueField == nullptr || (GetByteValue() && GetByteValue()->IsEmpty()); }
107
109 void Empty() { ValueField = nullptr; ValueLengthField = 0; }
110
112 void Clear()
113 {
114 TagField = 0;
115 VRField = VR::INVALID;
116 ValueField = nullptr;
117 ValueLengthField = 0;
118 }
119
120 // Helper:
126 void SetByteValue(const char *array, VL length)
127 {
128 ByteValue *bv = new ByteValue(array,length);
129 SetValue( *bv );
130 }
131
133 const ByteValue* GetByteValue() const {
134 // Get the raw pointer from the gdcm::SmartPointer
135 const ByteValue *bv = dynamic_cast<const ByteValue*>(ValueField.GetPointer());
136 return bv; // Will return NULL if not ByteValue
137 }
138
145 SmartPointer<SequenceOfItems> GetValueAsSQ() const;
146
149 const SequenceOfFragments* GetSequenceOfFragments() const;
150 SequenceOfFragments* GetSequenceOfFragments();
151
153 bool IsUndefinedLength() const {
154 return ValueLengthField.IsUndefined();
155 }
156
157 DataElement(const DataElement &_val)
158 {
159 if( this != &_val)
160 {
161 *this = _val;
162 }
163 }
164
165 bool operator<(const DataElement &de) const
166 {
167 return GetTag() < de.GetTag();
168 }
169 DataElement &operator=(const DataElement &)
170 = default;
171
172 bool operator==(const DataElement &de) const
173 {
174 bool b = TagField == de.TagField
175 && ValueLengthField == de.ValueLengthField
176 && VRField == de.VRField;
177 if( !ValueField && !de.ValueField )
178 {
179 return b;
180 }
181 if( ValueField && de.ValueField )
182 {
183 return b && (*ValueField == *de.ValueField);
184 }
185 // ValueField != de.ValueField
186 return false;
187 }
188
189 // The following functionalities are dependent on:
190 // # The Transfer Syntax: Explicit or Implicit
191 // # The Byte encoding: Little Endian / Big Endian
192
193 /*
194 * The following was inspired by a C++ idiom: Curiously Recurring Template Pattern
195 * Ref: http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern
196 * The typename TDE is typically a derived class *without* any data
197 * while TSwap is a simple template parameter to achieve byteswapping (and allow factorization of
198 * highly identical code)
199 */
200 template <typename TDE>
201 VL GetLength() const {
202 return static_cast<const TDE*>(this)->GetLength();
203 }
204
205 template <typename TDE, typename TSwap>
206 std::istream &Read(std::istream &is) {
207 return static_cast<TDE*>(this)->template Read<TSwap>(is);
208 }
209
210 template <typename TDE, typename TSwap>
211 std::istream &ReadOrSkip(std::istream &is, std::set<Tag> const &skiptags) {
212 (void)skiptags;
213 return static_cast<TDE*>(this)->template Read<TSwap>(is);
214 }
215
216 template <typename TDE, typename TSwap>
217 std::istream &ReadPreValue(std::istream &is, std::set<Tag> const &skiptags) {
218 (void)skiptags;
219 return static_cast<TDE*>(this)->template ReadPreValue<TSwap>(is);
220 }
221 template <typename TDE, typename TSwap>
222 std::istream &ReadValue(std::istream &is, std::set<Tag> const &skiptags) {
223 (void)skiptags;
224 return static_cast<TDE*>(this)->template ReadValue<TSwap>(is);
225 }
226 template <typename TDE, typename TSwap>
227 std::istream &ReadValueWithLength(std::istream &is, VL & length, std::set<Tag> const &skiptags) {
228 (void)skiptags;
229 return static_cast<TDE*>(this)->template ReadValueWithLength<TSwap>(is, length);
230 }
231
232 template <typename TDE, typename TSwap>
233 std::istream &ReadWithLength(std::istream &is, VL &length) {
234 return static_cast<TDE*>(this)->template ReadWithLength<TSwap>(is,length);
235 }
236
237 template <typename TDE, typename TSwap>
238 const std::ostream &Write(std::ostream &os) const {
239 return static_cast<const TDE*>(this)->template Write<TSwap>(os);
240 }
241
242 protected:
243 Tag TagField;
244 // This is the value read from the file, might be different from the length of Value Field
245 VL ValueLengthField; // Can be 0xFFFFFFFF
246
247 // Value Representation
248 VR VRField;
249 typedef SmartPointer<Value> ValuePtr;
250 ValuePtr ValueField;
251
252 void SetValueFieldLength( VL vl, bool readvalues );
253};
254//-----------------------------------------------------------------------------
255 inline std::ostream& operator<<(std::ostream &os, const DataElement &val)
256{
257 os << val.TagField;
258 os << "\t" << val.VRField;
259 os << "\t" << val.ValueLengthField;
260 if( val.ValueField )
261 {
262 val.ValueField->Print( os << "\t" );
263 }
264 return os;
265}
266
267 inline bool operator!=(const DataElement& lhs, const DataElement& rhs)
268{
269 return ! ( lhs == rhs );
270}
271
272} // end namespace gdcm_ns
273
274#endif //GDCMDATAELEMENT_H
gdcm::ByteValue
Class to represent binary value (array of bytes)
Definition gdcmByteValue.h:36
gdcm::DataElement
Class to represent a Data Element either Implicit or Explicit.
Definition gdcmDataElement.h:59
gdcm::DataElement::GetVL
const VL & GetVL() const
Get VL.
Definition gdcmDataElement.h:74
gdcm::DataElement::GetLength
VL GetLength() const
Definition gdcmDataElement.h:201
gdcm::DataElement::ValuePtr
SmartPointer< Value > ValuePtr
Definition gdcmDataElement.h:249
gdcm::DataElement::GetTag
const Tag & GetTag() const
Get Tag.
Definition gdcmDataElement.h:67
gdcm::DataElement::Write
const std::ostream & Write(std::ostream &os) const
Definition gdcmDataElement.h:238
gdcm::DataElement::Clear
void Clear()
Clear Data Element (make Value empty and invalidate Tag & VR)
Definition gdcmDataElement.h:112
gdcm::DataElement::IsUndefinedLength
bool IsUndefinedLength() const
return if Value Length if of undefined length
Definition gdcmDataElement.h:153
gdcm::DataElement::SetVL
void SetVL(const VL &vl)
Definition gdcmDataElement.h:79
gdcm::DataElement::Empty
void Empty()
Make Data Element empty (no Value)
Definition gdcmDataElement.h:109
gdcm::DataElement::GetVR
VR const & GetVR() const
Definition gdcmDataElement.h:84
gdcm::DataElement::ReadOrSkip
std::istream & ReadOrSkip(std::istream &is, std::set< Tag > const &skiptags)
Definition gdcmDataElement.h:211
gdcm::DataElement::ValueField
ValuePtr ValueField
Definition gdcmDataElement.h:250
gdcm::DataElement::operator<
bool operator<(const DataElement &de) const
Definition gdcmDataElement.h:165
gdcm::DataElement::ReadValueWithLength
std::istream & ReadValueWithLength(std::istream &is, VL &length, std::set< Tag > const &skiptags)
Definition gdcmDataElement.h:227
gdcm::DataElement::GetByteValue
const ByteValue * GetByteValue() const
Definition gdcmDataElement.h:133
gdcm::DataElement::SetTag
void SetTag(const Tag &t)
Definition gdcmDataElement.h:71
gdcm::DataElement::TagField
Tag TagField
Definition gdcmDataElement.h:243
gdcm::DataElement::IsEmpty
bool IsEmpty() const
Check if Data Element is empty.
Definition gdcmDataElement.h:106
gdcm::DataElement::GetVL
VL & GetVL()
Definition gdcmDataElement.h:75
gdcm::DataElement::operator==
bool operator==(const DataElement &de) const
Definition gdcmDataElement.h:172
gdcm::DataElement::SetVLToUndefined
void SetVLToUndefined()
gdcm::DataElement::ReadWithLength
std::istream & ReadWithLength(std::istream &is, VL &length)
Definition gdcmDataElement.h:233
gdcm::DataElement::ValueLengthField
VL ValueLengthField
Definition gdcmDataElement.h:245
gdcm::DataElement::DataElement
DataElement(const DataElement &_val)
Definition gdcmDataElement.h:157
gdcm::DataElement::GetSequenceOfFragments
const SequenceOfFragments * GetSequenceOfFragments() const
gdcm::DataElement::operator=
DataElement & operator=(const DataElement &)=default
gdcm::DataElement::DataElement
DataElement(const Tag &t=Tag(0), const VL &vl=0, const VR &vr=VR::INVALID)
Definition gdcmDataElement.h:61
gdcm::DataElement::GetValue
Value const & GetValue() const
Set/Get Value (bytes array, SQ of items, SQ of fragments):
Definition gdcmDataElement.h:94
gdcm::DataElement::SetValue
void SetValue(Value const &vl)
Definition gdcmDataElement.h:100
gdcm::DataElement::GetSequenceOfFragments
SequenceOfFragments * GetSequenceOfFragments()
gdcm::DataElement::GetValueAsSQ
SmartPointer< SequenceOfItems > GetValueAsSQ() const
gdcm::DataElement::SetByteValue
void SetByteValue(const char *array, VL length)
Definition gdcmDataElement.h:126
gdcm::DataElement::VRField
VR VRField
Definition gdcmDataElement.h:248
gdcm::DataElement::operator<<
friend std::ostream & operator<<(std::ostream &_os, const DataElement &_val)
Definition gdcmDataElement.h:255
gdcm::DataElement::GetTag
Tag & GetTag()
Definition gdcmDataElement.h:68
gdcm::DataElement::GetValue
Value & GetValue()
Definition gdcmDataElement.h:95
gdcm::DataElement::ReadPreValue
std::istream & ReadPreValue(std::istream &is, std::set< Tag > const &skiptags)
Definition gdcmDataElement.h:217
gdcm::DataElement::ReadValue
std::istream & ReadValue(std::istream &is, std::set< Tag > const &skiptags)
Definition gdcmDataElement.h:222
gdcm::DataElement::Read
std::istream & Read(std::istream &is)
Definition gdcmDataElement.h:206
gdcm::DataElement::SetVR
void SetVR(VR const &vr)
Definition gdcmDataElement.h:88
gdcm::DataElement::SetValueFieldLength
void SetValueFieldLength(VL vl, bool readvalues)
gdcm::Object::Print
virtual void Print(std::ostream &) const
Definition gdcmObject.h:87
gdcm::SequenceOfFragments
Class to represent a Sequence Of Fragments.
Definition gdcmSequenceOfFragments.h:32
gdcm::SmartPointer
Class for Smart Pointer.
Definition gdcmSmartPointer.h:40
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::VL
Value Length.
Definition gdcmVL.h:30
gdcm::VR
VR class.
Definition gdcmVR.h:55
gdcm::VR::IsVRFile
bool IsVRFile() const
gdcm::VR::INVALID
@ INVALID
Definition gdcmVR.h:59
gdcm::Value
Class to represent the value of a Data Element.
Definition gdcmValue.h:32
gdcm::Value::GetLength
virtual VL GetLength() const =0
gdcmByteValue.h
gdcmSmartPointer.h
gdcmTag.h
gdcmAssertAlwaysMacro
#define gdcmAssertAlwaysMacro(arg)
AssertAlways.
Definition gdcmTrace.h:228
gdcmVL.h
gdcmVR.h
GDCM_EXPORT
#define GDCM_EXPORT
Definition gdcmWin32.h:34
gdcm::operator<<
std::ostream & operator<<(std::ostream &os, const Directory &d)
Definition gdcmDirectory.h:88
gdcm::operator!=
bool operator!=(const CodeString &ref, const CodeString &cs)
Definition gdcmCodeString.h:97