gdcmTag.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 GDCMTAG_H
#define GDCMTAG_H
#include "gdcmTypes.h"
#include <iostream>
#include <iomanip>
namespace gdcm
{
class GDCM_EXPORT Tag
{
public:
Tag(uint16_t group, uint16_t element) {
ElementTag.tags[0] = group; ElementTag.tags[1] = element;
}
Tag(uint32_t tag = 0) {
SetElementTag(tag);
}
friend std::ostream& operator<<(std::ostream &_os, const Tag &_val);
friend std::istream& operator>>(std::istream &_is, Tag &_val);
uint16_t GetGroup() const { return ElementTag.tags[0]; }
uint16_t GetElement() const { return ElementTag.tags[1]; }
void SetGroup(uint16_t group) { ElementTag.tags[0] = group; }
void SetElement(uint16_t element) { ElementTag.tags[1] = element; }
void SetElementTag(uint16_t group, uint16_t element) {
ElementTag.tags[0] = group; ElementTag.tags[1] = element;
}
uint32_t GetElementTag() const {
#ifndef GDCM_WORDS_BIGENDIAN
return (ElementTag.tag<<16) | (ElementTag.tag>>16);
#else
return ElementTag.tag;
#endif
}
void SetElementTag(uint32_t tag) {
#ifndef GDCM_WORDS_BIGENDIAN
tag = ( (tag<<16) | (tag>>16) );
#endif
ElementTag.tag = tag;
}
const uint16_t &operator[](const unsigned int &_id) const
{
assert(_id<2);
return ElementTag.tags[_id];
}
uint16_t &operator[](const unsigned int &_id)
{
assert(_id<2);
return ElementTag.tags[_id];
}
Tag &operator=(const Tag &_val)
{
ElementTag.tag = _val.ElementTag.tag;
return *this;
}
bool operator==(const Tag &_val) const
{
return ElementTag.tag == _val.ElementTag.tag;
}
bool operator!=(const Tag &_val) const
{
return ElementTag.tag != _val.ElementTag.tag;
}
// FIXME FIXME FIXME TODO
// the following is pretty dumb. Since we have control over who is group
// and who is element, we should reverse them in little endian and big endian case
// since what we really want is fast comparison and not guarantee that group is in #0
// ...
bool operator<(const Tag &_val) const
{
#ifndef GDCM_WORDS_BIGENDIAN
if( ElementTag.tags[0] < _val.ElementTag.tags[0] )
return true;
if( ElementTag.tags[0] == _val.ElementTag.tags[0]
&& ElementTag.tags[1] < _val.ElementTag.tags[1] )
return true;
return false;
#else
// Plain comparison is enough!
return ( ElementTag.tag < _val.ElementTag.tag );
#endif
}
bool operator<=(const Tag &t2) const
{
const Tag &t1 = *this;
return t1 == t2 || t1 < t2;
}
Tag(const Tag &_val)
{
ElementTag.tag = _val.ElementTag.tag;
}
uint32_t GetLength() const { return 4; }
bool IsPublic() const { return !(ElementTag.tags[0] % 2); }
bool IsPrivate() const { return !IsPublic(); }
//-----------------------------------------------------------------------------
template <typename TSwap>
std::istream &Read(std::istream &is)
{
if( is.read(ElementTag.bytes, 4) )
TSwap::SwapArray(ElementTag.tags, 2);
return is;
}
template <typename TSwap>
const std::ostream &Write(std::ostream &os) const
{
uint16_t copy[2];
copy[0]= ElementTag.tags[0];
copy[1]= ElementTag.tags[1];
TSwap::SwapArray(copy, 2);
return os.write((char*)(&copy), 4);
}
Tag GetPrivateCreator() const
{
// See PS 3.5 - 7.8.1 PRIVATE DATA ELEMENT TAGS
// eg: 0x0123,0x1425 -> 0x0123,0x0014
if( IsPrivate() && !IsPrivateCreator() )
{
Tag r = *this;
r.SetElement( (uint16_t)(GetElement() >> 8) );
return r;
}
if( IsPrivateCreator() ) return *this;
return Tag(0x0,0x0);
}
void SetPrivateCreator(Tag const &t)
{
// See PS 3.5 - 7.8.1 PRIVATE DATA ELEMENT TAGS
// eg: 0x0123,0x0045 -> 0x0123,0x4567
assert( t.IsPrivate() /*&& t.IsPrivateCreator()*/ );
const uint16_t element = (uint16_t)(t.GetElement() << 8);
const uint16_t base = (uint16_t)(GetElement() << 8);
SetElement( (uint16_t)((base >> 8) + element) );
SetGroup( t.GetGroup() );
}
bool IsPrivateCreator() const
{
return IsPrivate() && (GetElement() <= 0xFF && GetElement() >= 0x10);
}
bool IsIllegal() const
{
// DICOM reserved those groups:
return GetGroup() == 0x0001 || GetGroup() == 0x0003 || GetGroup() == 0x0005 || GetGroup() == 0x0007
// This is a very special case, in private group, one cannot use element [0x01,0x09] ...
// || (IsPrivate() && !IsPrivateCreator() && !IsGroupLength());
|| (IsPrivate() && GetElement() > 0x0 && GetElement() < 0x10 );
}
bool IsGroupLength() const
{
return GetElement() == 0x0;
}
bool IsGroupXX(const Tag &t) const
{
if( t.GetElement() == GetElement() )
{
if( t.IsPrivate() ) return false;
uint16_t group = (uint16_t)((GetGroup() >> 8 ) << 8);
return group == t.GetGroup();
}
return false;
}
bool ReadFromCommaSeparatedString(const char *str);
bool ReadFromContinuousString(const char *str);
std::string PrintAsContinuousString() const;
std::string PrintAsContinuousUpperCaseString() const;
bool ReadFromPipeSeparatedString(const char *str);
std::string PrintAsPipeSeparatedString() const;
private:
union { uint32_t tag; uint16_t tags[2]; char bytes[4]; } ElementTag;
};
//-----------------------------------------------------------------------------
inline std::istream& operator>>(std::istream &_is, Tag &_val)
{
char c;
_is >> c;
uint16_t a, b;
_is >> std::hex >> a;
//_is >> std::hex >> _val[0];
//_is >> std::hex >> _val.ElementTag.tags[0];
_is >> c;
//_is >> _val[1];
//_is >> std::hex >> _val.ElementTag.tags[1];
_is >> std::hex >> b;
_is >> c;
_val.SetGroup( a );
_val.SetElement( b );
return _is;
}
inline std::ostream& operator<<(std::ostream &_os, const Tag &_val)
{
_os.setf( std::ios::right);
_os << std::hex << '(' << std::setw( 4 ) << std::setfill( '0' )
<< _val[0] << ',' << std::setw( 4 ) << std::setfill( '0' )
<< _val[1] << ')' << std::setfill( ' ' ) << std::dec;
return _os;
}
} // end namespace gdcm
#endif //GDCMTAG_H
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::Tag::SetGroup
void SetGroup(uint16_t group)
Sets the 'Group number' of the given Tag.
Definition gdcmTag.h:59
gdcm::Tag::ReadFromCommaSeparatedString
bool ReadFromCommaSeparatedString(const char *str)
gdcm::Tag::operator[]
uint16_t & operator[](const unsigned int &_id)
Returns the Group or Element of the given Tag, depending on id (0/1).
Definition gdcmTag.h:91
gdcm::Tag::IsPrivateCreator
bool IsPrivateCreator() const
Definition gdcmTag.h:206
gdcm::Tag::operator<
bool operator<(const Tag &_val) const
Definition gdcmTag.h:119
gdcm::Tag::operator[]
const uint16_t & operator[](const unsigned int &_id) const
Returns the Group or Element of the given Tag, depending on id (0/1).
Definition gdcmTag.h:85
gdcm::Tag::PrintAsContinuousString
std::string PrintAsContinuousString() const
gdcm::Tag::SetElement
void SetElement(uint16_t element)
Sets the 'Element number' of the given Tag.
Definition gdcmTag.h:61
gdcm::Tag::SetPrivateCreator
void SetPrivateCreator(Tag const &t)
Set private creator:
Definition gdcmTag.h:193
gdcm::Tag::SetElementTag
void SetElementTag(uint16_t group, uint16_t element)
Sets the 'Group number' & 'Element number' of the given Tag.
Definition gdcmTag.h:63
gdcm::Tag::GetGroup
uint16_t GetGroup() const
Returns the 'Group number' of the given Tag.
Definition gdcmTag.h:55
gdcm::Tag::operator<=
bool operator<=(const Tag &t2) const
Definition gdcmTag.h:133
gdcm::Tag::GetLength
uint32_t GetLength() const
return the length of tag (read: size on disk)
Definition gdcmTag.h:144
gdcm::Tag::IsPrivate
bool IsPrivate() const
Definition gdcmTag.h:155
gdcm::Tag::Tag
Tag(const Tag &_val)
Definition gdcmTag.h:139
gdcm::Tag::SetElementTag
void SetElementTag(uint32_t tag)
Sets the full tag value of the given Tag.
Definition gdcmTag.h:77
gdcm::Tag::IsIllegal
bool IsIllegal() const
return if the tag is considered to be an illegal tag
Definition gdcmTag.h:212
gdcm::Tag::IsGroupXX
bool IsGroupXX(const Tag &t) const
e.g 6002,3000 belong to groupXX: 6000,3000
Definition gdcmTag.h:228
gdcm::Tag::Write
const std::ostream & Write(std::ostream &os) const
Write a tag in binary rep.
Definition gdcmTag.h:169
gdcm::Tag::GetPrivateCreator
Tag GetPrivateCreator() const
Return the Private Creator Data Element tag of a private data element.
Definition gdcmTag.h:179
gdcm::Tag::ReadFromPipeSeparatedString
bool ReadFromPipeSeparatedString(const char *str)
gdcm::Tag::PrintAsContinuousUpperCaseString
std::string PrintAsContinuousUpperCaseString() const
Same as PrintAsContinuousString, but hexadecimal [a-f] are printed using upper case.
gdcm::Tag::bytes
char bytes[4]
Definition gdcmTag.h:266
gdcm::Tag::IsGroupLength
bool IsGroupLength() const
return whether the tag correspond to a group length tag:
Definition gdcmTag.h:222
gdcm::Tag::operator==
bool operator==(const Tag &_val) const
Definition gdcmTag.h:103
gdcm::Tag::tags
uint16_t tags[2]
Definition gdcmTag.h:266
gdcm::Tag::IsPublic
bool IsPublic() const
Definition gdcmTag.h:150
gdcm::Tag::Tag
Tag(uint16_t group, uint16_t element)
Constructor with 2*uint16_t.
Definition gdcmTag.h:42
gdcm::Tag::GetElement
uint16_t GetElement() const
Returns the 'Element number' of the given Tag.
Definition gdcmTag.h:57
gdcm::Tag::Read
std::istream & Read(std::istream &is)
Read a tag from binary representation.
Definition gdcmTag.h:160
gdcm::Tag::ReadFromContinuousString
bool ReadFromContinuousString(const char *str)
gdcm::Tag::Tag
Tag(uint32_t tag=0)
Constructor with 1*uint32_t Prefer the cstor that takes two uint16_t.
Definition gdcmTag.h:47
gdcm::Tag::tag
uint32_t tag
Definition gdcmTag.h:266
gdcm::Tag::PrintAsPipeSeparatedString
std::string PrintAsPipeSeparatedString() const
gdcm::Tag::operator=
Tag & operator=(const Tag &_val)
Definition gdcmTag.h:97
gdcm::Tag::GetElementTag
uint32_t GetElementTag() const
Returns the full tag value of the given Tag.
Definition gdcmTag.h:68
gdcm::Tag::operator!=
bool operator!=(const Tag &_val) const
Definition gdcmTag.h:107
gdcmTypes.h
GDCM_EXPORT
#define GDCM_EXPORT
Definition gdcmWin32.h:34
gdcm
Definition gdcmASN1.h:21
gdcm::operator>>
std::istream & operator>>(std::istream &is, String< TDelimiter, TMaxLength, TPadChar > &ms)
Definition gdcmString.h:116
gdcm::operator<<
std::ostream & operator<<(std::ostream &os, const Directory &d)
Definition gdcmDirectory.h:88