gdcmDataElement.h
Go to the documentation of this file.
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#ifndef GDCMDATAELEMENT_H
#define GDCMDATAELEMENT_H
#include "gdcmTag.h"
#include "gdcmVL.h"
#include "gdcmVR.h"
#include "gdcmByteValue.h"
#include "gdcmSmartPointer.h"
#include <set>
namespace gdcm_ns
{
// Data Element
// Contains multiple fields:
// -> Tag
// -> Optional VR (Explicit Transfer Syntax)
// -> ValueLength
// -> Value
// TODO: This class SHOULD be pure virtual. I don't want a user
// to shoot himself in the foot.
class SequenceOfItems;
class SequenceOfFragments;
class GDCM_EXPORT DataElement
{
public:
DataElement(const Tag& t = Tag(0), const VL& vl = 0, const VR &vr = VR::INVALID):TagField(t),ValueLengthField(vl),VRField(vr),ValueField(nullptr) {}
//DataElement( Attribute const &att );
friend std::ostream& operator<<(std::ostream &_os, const DataElement &_val);
const Tag& GetTag() const { return TagField; }
Tag& GetTag() { return TagField; }
void SetTag(const Tag &t) { TagField = t; }
const VL& GetVL() const { return ValueLengthField; }
VL& GetVL() { return ValueLengthField; }
void SetVL(const VL &vl) { ValueLengthField = vl; }
void SetVLToUndefined();
VR const &GetVR() const { return VRField; }
void SetVR(VR const &vr) {
if( vr.IsVRFile() )
VRField = vr;
}
Value const &GetValue() const { gdcmAssertAlwaysMacro(ValueField); return *ValueField; }
Value &GetValue() {
gdcmAssertAlwaysMacro(ValueField);
return *ValueField;
}
void SetValue(Value const & vl) {
//assert( ValueField == 0 );
ValueField = vl;
ValueLengthField = vl.GetLength();
}
bool IsEmpty() const { return ValueField == nullptr || (GetByteValue() && GetByteValue()->IsEmpty()); }
void Empty() { ValueField = nullptr; ValueLengthField = 0; }
void Clear()
{
TagField = 0;
VRField = VR::INVALID;
ValueField = nullptr;
ValueLengthField = 0;
}
// Helper:
void SetByteValue(const char *array, VL length)
{
ByteValue *bv = new ByteValue(array,length);
SetValue( *bv );
}
const ByteValue* GetByteValue() const {
// Get the raw pointer from the gdcm::SmartPointer
const ByteValue *bv = dynamic_cast<const ByteValue*>(ValueField.GetPointer());
return bv; // Will return NULL if not ByteValue
}
SmartPointer<SequenceOfItems> GetValueAsSQ() const;
const SequenceOfFragments* GetSequenceOfFragments() const;
SequenceOfFragments* GetSequenceOfFragments();
bool IsUndefinedLength() const {
return ValueLengthField.IsUndefined();
}
DataElement(const DataElement &_val)
{
if( this != &_val)
{
*this = _val;
}
}
bool operator<(const DataElement &de) const
{
return GetTag() < de.GetTag();
}
DataElement &operator=(const DataElement &)
= default;
bool operator==(const DataElement &de) const
{
bool b = TagField == de.TagField
&& ValueLengthField == de.ValueLengthField
&& VRField == de.VRField;
if( !ValueField && !de.ValueField )
{
return b;
}
if( ValueField && de.ValueField )
{
return b && (*ValueField == *de.ValueField);
}
// ValueField != de.ValueField
return false;
}
// The following functionalities are dependent on:
// # The Transfer Syntax: Explicit or Implicit
// # The Byte encoding: Little Endian / Big Endian
/*
* The following was inspired by a C++ idiom: Curiously Recurring Template Pattern
* Ref: http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern
* The typename TDE is typically a derived class *without* any data
* while TSwap is a simple template parameter to achieve byteswapping (and allow factorization of
* highly identical code)
*/
template <typename TDE>
VL GetLength() const {
return static_cast<const TDE*>(this)->GetLength();
}
template <typename TDE, typename TSwap>
std::istream &Read(std::istream &is) {
return static_cast<TDE*>(this)->template Read<TSwap>(is);
}
template <typename TDE, typename TSwap>
std::istream &ReadOrSkip(std::istream &is, std::set<Tag> const &skiptags) {
(void)skiptags;
return static_cast<TDE*>(this)->template Read<TSwap>(is);
}
template <typename TDE, typename TSwap>
std::istream &ReadPreValue(std::istream &is, std::set<Tag> const &skiptags) {
(void)skiptags;
return static_cast<TDE*>(this)->template ReadPreValue<TSwap>(is);
}
template <typename TDE, typename TSwap>
std::istream &ReadValue(std::istream &is, std::set<Tag> const &skiptags) {
(void)skiptags;
return static_cast<TDE*>(this)->template ReadValue<TSwap>(is);
}
template <typename TDE, typename TSwap>
std::istream &ReadValueWithLength(std::istream &is, VL & length, std::set<Tag> const &skiptags) {
(void)skiptags;
return static_cast<TDE*>(this)->template ReadValueWithLength<TSwap>(is, length);
}
template <typename TDE, typename TSwap>
std::istream &ReadWithLength(std::istream &is, VL &length) {
return static_cast<TDE*>(this)->template ReadWithLength<TSwap>(is,length);
}
template <typename TDE, typename TSwap>
const std::ostream &Write(std::ostream &os) const {
return static_cast<const TDE*>(this)->template Write<TSwap>(os);
}
protected:
Tag TagField;
// This is the value read from the file, might be different from the length of Value Field
VL ValueLengthField; // Can be 0xFFFFFFFF
// Value Representation
VR VRField;
typedef SmartPointer<Value> ValuePtr;
ValuePtr ValueField;
void SetValueFieldLength( VL vl, bool readvalues );
};
//-----------------------------------------------------------------------------
inline std::ostream& operator<<(std::ostream &os, const DataElement &val)
{
os << val.TagField;
os << "\t" << val.VRField;
os << "\t" << val.ValueLengthField;
if( val.ValueField )
{
val.ValueField->Print( os << "\t" );
}
return os;
}
inline bool operator!=(const DataElement& lhs, const DataElement& rhs)
{
return ! ( lhs == rhs );
}
} // end namespace gdcm_ns
#endif //GDCMDATAELEMENT_H
gdcm::ByteValue
Class to represent binary value (array of bytes).
Definition gdcmByteValue.h:35
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