#include "gdcmDataSet.h"
#include "gdcmPrivateTag.h"
namespace gdcm_ns
{
DataElement DataSet::DEEnd = DataElement( Tag(0xffff,0xffff) );
const DataElement& DataSet::GetDEEnd() const
{
return DEEnd;
}
std::string DataSet::GetPrivateCreator(const Tag &t) const
{
if( t.IsPrivate() && !t.IsGroupLength() && !t.IsPrivateCreator() && !t.IsIllegal() )
{
Tag pc = t.GetPrivateCreator();
gdcm_assert( pc.GetElement() );
{
const DataElement r(pc);
ConstIterator it = DES.find(r);
if( it == DES.end() )
{
return "";
}
const DataElement &de = *it;
if( de.IsEmpty() ) return "";
const ByteValue *bv = de.GetByteValue();
gdcm_assert( bv );
std::string owner = std::string(bv->GetPointer(),bv->GetLength()).c_str();
while( !owner.empty() && owner[owner.size()-1] == ' ' )
{
owner.erase(owner.size()-1,1);
}
gdcm_assert( owner.empty() || owner[owner.size()-1] != ' ' );
return owner;
}
}
return "";
}
PrivateTag DataSet::GetPrivateTag(const Tag &t) const
{
const std::string str = this->GetPrivateCreator(t);
PrivateTag pt(t);
pt.SetOwner(str.c_str());
return pt;
}
Tag DataSet::ComputeDataElement(const PrivateTag & t) const
{
const Tag start(t.GetGroup(), 0x0010 ); const DataElement r(start);
ConstIterator it = DES.lower_bound(r);
const char *refowner = t.GetOwner();
gdcm_assert( refowner );
bool found = false;
while( it != DES.end() && it->GetTag().GetGroup() == t.GetGroup() && it->GetTag().GetElement() < 0x100 )
{
const ByteValue * bv = it->GetByteValue();
if( bv )
{
std::string tmp(bv->GetPointer(),bv->GetLength());
tmp.erase(tmp.find_last_not_of(' ') + 1);
gdcm_assert( tmp.empty() || tmp[ tmp.size() - 1 ] != ' ' ); if( System::StrCaseCmp( tmp.c_str(), refowner ) == 0 )
{
found = true;
break;
}
}
++it;
}
if (!found) return GetDEEnd().GetTag();
Tag copy = t;
copy.SetPrivateCreator( it->GetTag() );
gdcmDebugMacro( "Compute found:" << copy );
return copy;
}
bool DataSet::FindDataElement(const PrivateTag &t) const
{
return FindDataElement( ComputeDataElement(t) );
}
const DataElement& DataSet::GetDataElement(const PrivateTag &t) const
{
return GetDataElement( ComputeDataElement(t) );
}
MediaStorage DataSet::GetMediaStorage() const
{
const Tag tsopclassuid(0x0008, 0x0016);
if( !FindDataElement( tsopclassuid) )
{
gdcmDebugMacro( "No SOP Class UID" );
return MediaStorage::MS_END;
}
const DataElement &de = GetDataElement(tsopclassuid);
if( de.IsEmpty() )
{
gdcmDebugMacro( "Empty SOP Class UID" );
return MediaStorage::MS_END;
}
std::string ts;
{
const ByteValue *bv = de.GetByteValue();
gdcm_assert( bv );
if( bv->GetPointer() && bv->GetLength() )
{
ts = std::string(bv->GetPointer(), bv->GetLength());
}
}
if( !ts.empty() )
{
char &last = ts[ts.size()-1];
if( last == ' ' )
{
gdcmWarningMacro( "Media Storage Class UID: " << ts << " contained a trailing space character" );
last = '\0';
}
}
gdcmDebugMacro( "TS: " << ts );
MediaStorage ms = MediaStorage::GetMSType(ts.c_str());
if( ms == MediaStorage::MS_END )
{
gdcmWarningMacro( "Media Storage Class UID: " << ts << " is unknown" );
}
return ms;
}
}