Skip to content

Commit

Permalink
Add conversion of SQ elements
Browse files Browse the repository at this point in the history
  • Loading branch information
lamyj committed May 12, 2015
1 parent 7d1154c commit 0b4ad55
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 10 deletions.
14 changes: 14 additions & 0 deletions src/dcmtkpp/DataSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ ::size(Tag const & tag) const
return it->second.size();
}

bool
DataSet
::operator==(DataSet const & other) const
{
return (this->_elements == other._elements);
}

bool
DataSet
::operator!=(DataSet const & other) const
{
return !(*this == other);
}

bool
DataSet
::_is_int_vr(VR vr)
Expand Down
6 changes: 6 additions & 0 deletions src/dcmtkpp/DataSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ class DataSet

// FIXME: AT, binary

/// @brief Equality test
bool operator==(DataSet const & other) const;

/// @brief Difference test
bool operator!=(DataSet const & other) const;

private:
typedef std::map<Tag, Element> ElementMap;

Expand Down
14 changes: 14 additions & 0 deletions src/dcmtkpp/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,18 @@ Element::as_data_set()
return this->_value.as_data_sets();
}

bool
Element
::operator==(Element const & other) const
{
return (this->vr == other.vr) && (this->_value == other._value);
}

bool
Element
::operator!=(Element const & other) const
{
return !(*this == other);
}

}
6 changes: 6 additions & 0 deletions src/dcmtkpp/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ class Element
*/
Value::DataSets & as_data_set();

/// @brief Equality test
bool operator==(Element const & other) const;

/// @brief Difference test
bool operator!=(Element const & other) const;

private:
struct Empty
{
Expand Down
46 changes: 46 additions & 0 deletions src/dcmtkpp/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,50 @@ DECLARE_NON_CONST_ACCESSOR(Strings, strings)
DECLARE_CONST_ACCESSOR(DataSets, data_sets)
DECLARE_NON_CONST_ACCESSOR(DataSets, data_sets)

#undef DECLARE_NON_CONST_ACCESSOR
#undef DECLARE_CONST_ACCESSOR

bool
Value
::operator==(Value const & other) const
{
if(this->_type != other._type)
{
return false;
}
else if(this->_type == Value::Type::Empty)
{
return true;
}
else if(this->_type == Value::Type::Integers)
{
return this->_integers == other._integers;
}
else if(this->_type == Value::Type::Reals)
{
return this->_reals == other._reals;
}
else if(this->_type == Value::Type::Strings)
{
return this->_strings == other._strings;
}
else if(this->_type == Value::Type::DataSets)
{
return this->_data_sets == other._data_sets;
}
else
{
throw Exception("Unknown type");
}
}

bool
Value
::operator!=(Value const & other) const
{
return !(*this == other);
}



}
6 changes: 6 additions & 0 deletions src/dcmtkpp/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ class Value
*/
DataSets & as_data_sets();

/// @brief Equality test.
bool operator==(Value const & other) const;

/// @brief Difference test.
bool operator!=(Value const & other) const;

private:
Integers _integers;
Reals _reals;
Expand Down
42 changes: 37 additions & 5 deletions src/dcmtkpp/conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace dcmtkpp
{

DcmVR convert(VR vr)
DcmEVR convert(VR vr)
{
if(vr == VR::AE) { return EVR_AE; }
else if(vr == VR::AS) { return EVR_AS; }
Expand Down Expand Up @@ -54,9 +54,8 @@ DcmVR convert(VR vr)
}
}

VR convert(DcmVR vr)
VR convert(DcmEVR evr)
{
DcmEVR const evr = vr.getValidEVR();
if(evr == EVR_AE) { return VR::AE; }
else if(evr == EVR_AS) { return VR::AS; }
else if(evr == EVR_AT) { return VR::AT; }
Expand Down Expand Up @@ -179,7 +178,16 @@ DcmElement * convert(const Tag & tag, Element const & source)
destination = new DcmSignedLong(destination_tag);
convert<Value::Integers, Sint32>(source, destination, &Element::as_int);
}
// SQ
else if(source.vr == VR::SQ)
{
DcmSequenceOfItems * sequence = new DcmSequenceOfItems(destination_tag);
for(auto const & source_item: source.as_data_set())
{
DcmDataset destination_item = convert(source_item);
sequence->append(new DcmDataset(destination_item));
}
destination = sequence;
}
else if (source.vr == VR::SS)
{
destination = new DcmSignedShort(destination_tag);
Expand Down Expand Up @@ -304,7 +312,31 @@ Element convert(DcmElement * source)
destination = Element(Value::Integers(), VR::SL);
convert<Sint32, Value::Integers>(source, destination, &Element::as_int);
}
// SQ
else if(source_vr == EVR_SQ)
{
destination = Element(Value::DataSets(), VR::SQ);
DcmSequenceOfItems * sequence = dynamic_cast<DcmSequenceOfItems*>(source);
if(sequence == NULL)
{
throw Exception("Element is not a DcmSequenceOfItems");
}

Value::DataSets & destination_value = destination.as_data_set();

destination_value.reserve(sequence->card());
for(unsigned int i=0; i<sequence->card(); ++i)
{
DcmItem * item = sequence->getItem(i);
DcmDataset * source_item = dynamic_cast<DcmDataset *>(item);
if(source_item == NULL)
{
throw Exception("Item is not a DcmDataset");
}

DataSet const destination_item = convert(*source_item);
destination_value.push_back(destination_item);
}
}
else if(source_vr == EVR_SS)
{
destination = Element(Value::Integers(), VR::SS);
Expand Down
4 changes: 2 additions & 2 deletions src/dcmtkpp/conversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ namespace dcmtkpp
{

/// @brief Convert a dcmtkpp::VR to a DcmVR.
DcmVR convert(VR vr);
DcmEVR convert(VR vr);

/// @brief Convert a DcmVR to a dcmtkpp::VR.
VR convert(DcmVR vr);
VR convert(DcmEVR evr);

/// @brief Convert a dcmtkpp::Tag to a DcmTagKey.
DcmTagKey convert(Tag const & tag);
Expand Down
56 changes: 53 additions & 3 deletions tests/code/conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ ElementTest(
SL, dcmtkpp::Value::Integers, DcmSignedLong,
dcmtkpp::Value::Integers({34567, -56789}), &dcmtkpp::Element::as_int);

// SQ

ElementTest(
SS, dcmtkpp::Value::Integers, DcmSignedShort,
dcmtkpp::Value::Integers({1234, -5678}), &dcmtkpp::Element::as_int);
Expand Down Expand Up @@ -260,6 +258,59 @@ ElementTest(
dcmtkpp::Value::Strings({"foo\nbar\\something else"}),
&dcmtkpp::Element::as_string);

BOOST_AUTO_TEST_CASE(SQFromDcmtkpp)
{
dcmtkpp::DataSet item;
item.add("PatientID");
item.as_string("PatientID").push_back("DJ1234");

dcmtkpp::Element const source(
dcmtkpp::Value::DataSets({item}), dcmtkpp::VR::SQ);

dcmtkpp::Tag const source_tag(0xdead, 0xbeef);

DcmElement * destination = dcmtkpp::convert(source_tag, source);

BOOST_CHECK_NE(destination, (DcmElement const *)(NULL));

BOOST_CHECK_EQUAL(destination->getVR(), dcmtkpp::convert(source.vr));
BOOST_CHECK_NE(
dynamic_cast<DcmSequenceOfItems *>(destination),
(DcmSequenceOfItems *)(NULL));

BOOST_CHECK_EQUAL(destination->getVM(), source.size());
for(std::size_t i=0; i<source.size(); ++i)
{
dcmtkpp::DataSet const & source_item = source.as_data_set()[i];
DcmItem * item = dynamic_cast<DcmSequenceOfItems *>(destination)->getItem(i);
DcmDataset * destination_item = dynamic_cast<DcmDataset *>(item);
BOOST_CHECK(source_item == dcmtkpp::convert(*destination_item));
}
}

BOOST_AUTO_TEST_CASE(SQToDcmtkpp)
{
DcmDataset * item = new DcmDataset;
item->putAndInsertOFStringArray(DCM_PatientID, "DJ1234");

DcmSequenceOfItems source(DcmTag(0xdead, 0xbeef, EVR_SQ));
source.append(item);

dcmtkpp::Element const destination = dcmtkpp::convert(&source);

BOOST_CHECK(destination.vr == dcmtkpp::convert(source.getVR()));
BOOST_CHECK_EQUAL(source.getVM(), destination.size());
for(std::size_t i=0; i<destination.size(); ++i)
{
DcmItem * item = source.getItem(i);
DcmDataset * source_item = dynamic_cast<DcmDataset *>(item);
BOOST_REQUIRE(source_item != NULL);

dcmtkpp::DataSet const & destination_item = destination.as_data_set()[i];
BOOST_CHECK(dcmtkpp::convert(*source_item) == destination_item);
}
}

BOOST_AUTO_TEST_CASE(EmptyDataSetFromDcmtkpp)
{
dcmtkpp::DataSet const empty;
Expand All @@ -274,7 +325,6 @@ BOOST_AUTO_TEST_CASE(EmptyDataSetFromDcmtk)
BOOST_CHECK(result.empty());
}


BOOST_AUTO_TEST_CASE(DataSetFromDcmtkpp)
{
dcmtkpp::Element const patient_id_source(
Expand Down

0 comments on commit 0b4ad55

Please sign in to comment.