GDCM  2.2.6
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
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 dont want a user
34 // to shoot himself in the foot.
35 
36 class SequenceOfItems;
37 class SequenceOfFragments;
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(0) {}
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 { return *ValueField; }
95  Value &GetValue() { return *ValueField; }
97  void SetValue(Value const & vl) {
98  //assert( ValueField == 0 );
99  ValueField = vl;
100  ValueLengthField = vl.GetLength();
101  }
103  bool IsEmpty() const { return ValueField == 0 || (GetByteValue() && GetByteValue()->IsEmpty()); }
104 
106  void Empty() { ValueField = 0; ValueLengthField = 0; }
107 
109  void Clear()
110  {
111  TagField = 0;
112  VRField = VR::INVALID;
113  ValueField = 0;
114  ValueLengthField = 0;
115  }
116 
117  // Helper:
123  void SetByteValue(const char *array, VL length)
124  {
125  ByteValue *bv = new ByteValue(array,length);
126  SetValue( *bv );
127  }
130  const ByteValue* GetByteValue() const {
131  // Get the raw pointer from the gdcm::SmartPointer
132  const ByteValue *bv = dynamic_cast<const ByteValue*>(ValueField.GetPointer());
133  return bv; // Will return NULL if not ByteValue
134  }
135 
144  GDCM_LEGACY(const SequenceOfItems* GetSequenceOfItems() const)
145  GDCM_LEGACY(SequenceOfItems* GetSequenceOfItems())
146 
153  SmartPointer<SequenceOfItems> GetValueAsSQ() const;
154 
157  const SequenceOfFragments* GetSequenceOfFragments() const;
158  SequenceOfFragments* GetSequenceOfFragments();
159 
161  bool IsUndefinedLength() const {
162  return ValueLengthField.IsUndefined();
163  }
164 
166  {
167  if( this != &_val)
168  {
169  *this = _val;
170  }
171  }
172 
173  bool operator<(const DataElement &de) const
174  {
175  return GetTag() < de.GetTag();
176  }
178  {
179  TagField = de.TagField;
180  ValueLengthField = de.ValueLengthField;
181  VRField = de.VRField;
182  ValueField = de.ValueField; // Pointer copy
183  return *this;
184  }
185 
186  bool operator==(const DataElement &de) const
187  {
188  bool b = TagField == de.TagField
189  && ValueLengthField == de.ValueLengthField
190  && VRField == de.VRField;
191  if( !ValueField && !de.ValueField )
192  {
193  return b;
194  }
195  if( ValueField && de.ValueField )
196  {
197  return b && (*ValueField == *de.ValueField);
198  }
199  // ValueField != de.ValueField
200  return false;
201  }
202 
203  // The following fonctionalities are dependant on:
204  // # The Transfer Syntax: Explicit or Implicit
205  // # The Byte encoding: Little Endian / Big Endian
206 
207  /*
208  * The following was inspired by a C++ idiom: Curiously Recurring Template Pattern
209  * Ref: http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern
210  * The typename TDE is typically a derived class *without* any data
211  * while TSwap is a simple template parameter to achieve byteswapping (and allow factorization of
212  * highly identical code)
213  */
214  template <typename TDE>
215  VL GetLength() const {
216  return static_cast<const TDE*>(this)->GetLength();
217  }
218 
219  template <typename TDE, typename TSwap>
220  std::istream &Read(std::istream &is) {
221  return static_cast<TDE*>(this)->template Read<TSwap>(is);
222  }
223 
224  template <typename TDE, typename TSwap>
225  std::istream &ReadOrSkip(std::istream &is, std::set<Tag> const &skiptags) {
226  (void)skiptags;
227  return static_cast<TDE*>(this)->template Read<TSwap>(is);
228  }
229 
230  template <typename TDE, typename TSwap>
231  std::istream &ReadPreValue(std::istream &is, std::set<Tag> const &skiptags) {
232  (void)skiptags;
233  return static_cast<TDE*>(this)->template ReadPreValue<TSwap>(is);
234  }
235  template <typename TDE, typename TSwap>
236  std::istream &ReadValue(std::istream &is, std::set<Tag> const &skiptags) {
237  (void)skiptags;
238  return static_cast<TDE*>(this)->template ReadValue<TSwap>(is);
239  }
240  template <typename TDE, typename TSwap>
241  std::istream &ReadValueWithLength(std::istream &is, VL & length, std::set<Tag> const &skiptags) {
242  (void)skiptags;
243  return static_cast<TDE*>(this)->template ReadValueWithLength<TSwap>(is, length);
244  }
245 
246  template <typename TDE, typename TSwap>
247  std::istream &ReadWithLength(std::istream &is, VL &length) {
248  return static_cast<TDE*>(this)->template ReadWithLength<TSwap>(is,length);
249  }
250 
251  template <typename TDE, typename TSwap>
252  const std::ostream &Write(std::ostream &os) const {
253  return static_cast<const TDE*>(this)->template Write<TSwap>(os);
254  }
255 
256 protected:
258  // This is the value read from the file, might be different from the length of Value Field
259  VL ValueLengthField; // Can be 0xFFFFFFFF
260 
261  // Value Representation
265 
266  void SetValueFieldLength( VL vl, bool readvalues );
267 };
268 //-----------------------------------------------------------------------------
269 inline std::ostream& operator<<(std::ostream &os, const DataElement &val)
270 {
271  os << val.TagField;
272  os << "\t" << val.VRField;
273  os << "\t" << val.ValueLengthField;
274  if( val.ValueField )
275  {
276  val.ValueField->Print( os << "\t" );
277  }
278  return os;
279 }
280 
281 inline bool operator!=(const DataElement& lhs, const DataElement& rhs)
282 {
283  return ! ( lhs == rhs );
284 }
285 
286 } // end namespace gdcm
287 
288 #endif //GDCMDATAELEMENT_H
void SetVR(VR const &vr)
Definition: gdcmDataElement.h:88
void SetValue(Value const &vl)
Definition: gdcmDataElement.h:97
bool IsEmpty() const
Check if Data Element is empty.
Definition: gdcmDataElement.h:103
void SetTag(const Tag &t)
Definition: gdcmDataElement.h:71
std::istream & ReadWithLength(std::istream &is, VL &length)
Definition: gdcmDataElement.h:247
Class to represent a Sequence Of Items (value representation : SQ)
Definition: gdcmSequenceOfItems.h:39
void SetByteValue(const char *array, VL length)
Definition: gdcmDataElement.h:123
Definition: gdcmVR.h:59
std::istream & ReadValue(std::istream &is, std::set< Tag > const &skiptags)
Definition: gdcmDataElement.h:236
DataElement(const DataElement &_val)
Definition: gdcmDataElement.h:165
Class to represent the value of a Data Element.
Definition: gdcmValue.h:29
const VL & GetVL() const
Get VL.
Definition: gdcmDataElement.h:74
#define GDCM_EXPORT
Definition: gdcmWin32.h:34
bool operator!=(const CodeString &ref, const CodeString &cs)
Definition: gdcmCodeString.h:97
DataElement(const Tag &t=Tag(0), const VL &vl=0, const VR &vr=VR::INVALID)
Definition: gdcmDataElement.h:61
Value Length.
Definition: gdcmVL.h:29
Class to represent a Sequence Of Fragments.
Definition: gdcmSequenceOfFragments.h:31
std::ostream & operator<<(std::ostream &os, const Directory &d)
Definition: gdcmDirectory.h:92
VL ValueLengthField
Definition: gdcmDataElement.h:259
bool operator<(const DataElement &de) const
Definition: gdcmDataElement.h:173
const ByteValue * GetByteValue() const
Definition: gdcmDataElement.h:130
void SetVL(const VL &vl)
Definition: gdcmDataElement.h:79
VR VRField
Definition: gdcmDataElement.h:262
DataElement & operator=(const DataElement &de)
Definition: gdcmDataElement.h:177
const std::ostream & Write(std::ostream &os) const
Definition: gdcmDataElement.h:252
Class to represent a Data Element either Implicit or Explicit.
Definition: gdcmDataElement.h:58
Class to represent binary value (array of bytes)
Definition: gdcmByteValue.h:33
Value & GetValue()
Definition: gdcmDataElement.h:95
bool operator==(const DataElement &de) const
Definition: gdcmDataElement.h:186
std::istream & ReadPreValue(std::istream &is, std::set< Tag > const &skiptags)
Definition: gdcmDataElement.h:231
VR const & GetVR() const
Definition: gdcmDataElement.h:84
std::istream & Read(std::istream &is)
Definition: gdcmDataElement.h:220
Value const & GetValue() const
Set/Get Value (bytes array, SQ of items, SQ of fragments):
Definition: gdcmDataElement.h:94
void Clear()
Clear Data Element (make Value empty and invalidate Tag &amp; VR)
Definition: gdcmDataElement.h:109
Class for Smart Pointer.
Definition: gdcmObject.h:26
VL & GetVL()
Definition: gdcmDataElement.h:75
virtual VL GetLength() const =0
Tag & GetTag()
Definition: gdcmDataElement.h:68
#define GDCM_LEGACY(method)
Definition: gdcmLegacyMacro.h:44
const Tag & GetTag() const
Get Tag.
Definition: gdcmDataElement.h:67
ValuePtr ValueField
Definition: gdcmDataElement.h:264
std::istream & ReadOrSkip(std::istream &is, std::set< Tag > const &skiptags)
Definition: gdcmDataElement.h:225
virtual void Print(std::ostream &) const
Definition: gdcmObject.h:87
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element). Basically an uint32_t which...
Definition: gdcmTag.h:38
VL GetLength() const
Definition: gdcmDataElement.h:215
Tag TagField
Definition: gdcmDataElement.h:257
VR class This is adapted from DICOM standard The biggest difference is the INVALID VR and the composi...
Definition: gdcmVR.h:54
bool IsVRFile() const
void Empty()
Make Data Element empty (no Value)
Definition: gdcmDataElement.h:106
std::istream & ReadValueWithLength(std::istream &is, VL &length, std::set< Tag > const &skiptags)
Definition: gdcmDataElement.h:241
SmartPointer< Value > ValuePtr
Definition: gdcmDataElement.h:263

Generated on Sat Dec 21 2013 05:56:16 for GDCM by doxygen 1.8.5
SourceForge.net Logo