1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
/*=========================================================================
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.
=========================================================================*/
#include "gdcmByteSwapFilter.h"
#include "gdcmElement.h"
#include "gdcmByteValue.h"
#include "gdcmSequenceOfFragments.h"
#include "gdcmSequenceOfItems.h"
#include "gdcmSwapper.h"
namespace gdcm
{
//-----------------------------------------------------------------------------
//ByteSwapFilter::ByteSwapFilter()
//{
//}
//-----------------------------------------------------------------------------
ByteSwapFilter::~ByteSwapFilter()
{
}
bool ByteSwapFilter::ByteSwap()
{
for(
DataSet::ConstIterator it = DS.Begin();
it != DS.End(); ++it)
{
const DataElement &de = *it;
VR const & vr = de.GetVR();
//assert( vr & VR::VRASCII || vr & VR::VRBINARY );
const ByteValue *bv = de.GetByteValue();
gdcm::SmartPointer<gdcm::SequenceOfItems> si = de.GetValueAsSQ();
if( de.IsEmpty() )
{
}
else if( bv && !si )
{
assert( !si );
// ASCII do not need byte swap
if( vr & VR::VRBINARY /*&& de.GetTag().IsPrivate()*/ )
{
//assert( de.GetTag().IsPrivate() );
switch(vr)
{
case VR::AT:
assert( 0 && "Should not happen" );
break;
case VR::FL:
// FIXME: Technically FL should not be byte-swapped...
//std::cerr << "ByteSwap FL:" << de.GetTag() << std::endl;
SwapperDoOp::SwapArray((uint32_t*)bv->GetPointer(), bv->GetLength() / sizeof(uint32_t) );
break;
case VR::FD:
assert( 0 && "Should not happen" );
break;
case VR::OB:
// I think we are fine, unless this is one of those OB_OW thingy
break;
case VR::OF:
assert( 0 && "Should not happen" );
break;
case VR::OW:
assert( 0 && "Should not happen" );
break;
case VR::SL:
SwapperDoOp::SwapArray((uint32_t*)bv->GetPointer(), bv->GetLength() / sizeof(uint32_t) );
break;
case VR::SQ:
assert( 0 && "Should not happen" );
break;
case VR::SS:
SwapperDoOp::SwapArray((uint16_t*)bv->GetPointer(), bv->GetLength() / sizeof(uint16_t) );
break;
case VR::UL:
SwapperDoOp::SwapArray((uint32_t*)bv->GetPointer(), bv->GetLength() / sizeof(uint32_t) );
break;
case VR::UN:
assert( 0 && "Should not happen" );
break;
case VR::US:
SwapperDoOp::SwapArray((uint16_t*)bv->GetPointer(), bv->GetLength() / sizeof(uint16_t) );
break;
case VR::UT:
assert( 0 && "Should not happen" );
break;
default:
assert( 0 && "Should not happen" );
}
}
}
//else if( const SequenceOfItems *si = de.GetSequenceOfItems() )
else if( si )
{
//if( de.GetTag().IsPrivate() )
{
//std::cerr << "ByteSwap SQ:" << de.GetTag() << std::endl;
SequenceOfItems::ConstIterator it2 = si->Begin();
for( ; it2 != si->End(); ++it2)
{
const Item &item = *it2;
DataSet &ds = const_cast<DataSet&>(item.GetNestedDataSet()); // FIXME
ByteSwapFilter bsf(ds);
bsf.ByteSwap();
}
}
}
else if( const SequenceOfFragments *sf = de.GetSequenceOfFragments() )
{
(void)sf;
assert( 0 && "Should not happen" );
}
else
{
assert( 0 && "error" );
}
}
if( ByteSwapTag )
{
DataSet copy;
DataSet::ConstIterator it = DS.Begin();
for( ; it != DS.End(); ++it)
{
DataElement de = *it;
const Tag& tag = de.GetTag();
de.SetTag(
Tag( SwapperDoOp::Swap( tag.GetGroup() ), SwapperDoOp::Swap( tag.GetElement() ) ) );
copy.Insert( de );
DS.Remove( de.GetTag() );
}
DS = copy;
}
return true;
}
}
|