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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/*=========================================================================
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 "gdcmDataElement.h"
#include "gdcmByteValue.h"
#include "gdcmAttribute.h"
#include "gdcmSequenceOfFragments.h"
#include "gdcmSequenceOfItems.h"
#include "gdcmImplicitDataElement.h"
#include "gdcmTrace.h"
namespace gdcm
{
void DataElement::SetVLToUndefined() {
assert( VRField == VR::SQ || VRField == VR::INVALID
|| (VRField == VR::UN /*&& IsUndefinedLength()*/ ) );
SequenceOfItems *sqi = dynamic_cast<SequenceOfItems*>(ValueField.GetPointer());
if( sqi )
{
sqi->SetLengthToUndefined();
assert( GetValueAsSQ()->IsUndefinedLength() );
}
ValueLengthField.SetToUndefined();
}
const SequenceOfFragments* DataElement::GetSequenceOfFragments() const {
const SequenceOfFragments *sqf = dynamic_cast<SequenceOfFragments*>(ValueField.GetPointer());
return sqf;
}
SequenceOfFragments* DataElement::GetSequenceOfFragments() {
SequenceOfFragments *sqf = dynamic_cast<SequenceOfFragments*>(ValueField.GetPointer());
return sqf;
}
/*
* Two cases are handled:
* - Simple ones:
* Explicit file with VR::SQ (defined or undefined length)
* Implicit file with undefined length (clearly a SQ)
* - If not in the previous case then we are in the second case: complex one (hidden SQ)
* Implicit file with defined length SQ
* Explicit file with SQ declared as UN + undefined length
*/
SmartPointer<SequenceOfItems> DataElement::GetValueAsSQ() const
{
if( IsEmpty() /*|| GetByteValue()*/ || GetSequenceOfFragments() )
{
return 0;
}
SequenceOfItems *sq = dynamic_cast<SequenceOfItems*>(ValueField.GetPointer());
if( sq ) // all set !
{
//assert( GetVR() == VR::SQ );
SmartPointer<SequenceOfItems> sqi = sq;
return sqi;
}
const Tag itemStart(0xfffe, 0xe000);
{
{
if( GetVR() == VR::INVALID )
{
const ByteValue *bv = GetByteValue();
assert( bv );
SequenceOfItems *sqi = new SequenceOfItems;
sqi->SetLength( bv->GetLength() );
std::string s( bv->GetPointer(), bv->GetLength() );
try
{
std::stringstream ss;
ss.str( s );
sqi->Read<ImplicitDataElement,SwapperNoOp>( ss, true );
}
catch(Exception &ex)
{
const Tag itemPMSStart(0xfeff, 0x00e0);
const Tag itemPMSStart2(0x3f3f, 0x3f00);
// same player ...
//this to fix a broken dicom implementation
//for philips medical systems
std::stringstream ss;
ss.str(s);
Tag item;
item.Read<SwapperNoOp>(ss);
if( item == itemPMSStart )
{
ss.seekg(-4,std::ios::cur);
sqi->Read<ExplicitDataElement,SwapperDoOp>( ss, true );
gdcmWarningMacro(ex.what());
(void)ex; //to avoid unreferenced variable warning on release
}
else
{
delete sqi;
sqi = NULL;
}
}
return sqi;
}
else if ( GetVR() == VR::UN ) // cp 246, IVRLE SQ
{
assert( GetVR() == VR::UN ); // cp 246, IVRLE SQ
const ByteValue *bv = GetByteValue();
assert( bv );
SequenceOfItems *sqi = new SequenceOfItems;
sqi->SetLength( bv->GetLength() );
std::string s( bv->GetPointer(), bv->GetLength() );
try
{
std::stringstream ss;
ss.str( s );
sqi->Read<ImplicitDataElement,SwapperNoOp>( ss, true );
}
catch ( Exception &ex0 )
{
// Some people like to skew things up and write invalid SQ in VR:UN field
// if conversion fails, simply keep the binary VR:UN stuff as-is
// eg. AMIInvalidPrivateDefinedLengthSQasUN.dcm
//const char *s = e.what();
// s -> gdcm::ImplicitDataElement -> Impossible (more)
try
{
std::stringstream ss;
ss.str(s);
sqi->Read<ExplicitDataElement,SwapperDoOp>( ss, true );
gdcmWarningMacro(ex0.what()); (void)ex0;
}
catch ( Exception &ex1 )
{
// Let's try again this time but without the byte swapping option:
// eg. MEDILABInvalidCP246_EVRLESQasUN.dcm
try
{
std::stringstream ss;
ss.str(s);
sqi->Read<ExplicitDataElement,SwapperNoOp>( ss, true );
gdcmWarningMacro(ex1.what()); (void)ex1;
}
catch ( Exception &ex2 )
{
gdcmErrorMacro( "Could not read SQ. Giving up" );
gdcmErrorMacro(ex2.what()); (void)ex2;
delete sqi;
return NULL;
}
}
}
return sqi;
}
else
{
assert( GetVR().IsVRFile() );
assert( GetByteValue() );
return 0;
}
}
}
// catch( ParseException &pex )
// {
// gdcmDebugMacro( pex.what() );
// }
// catch( Exception &ex )
// {
// gdcmDebugMacro( ex.what() );
// }
// catch( ... )
// {
// gdcmWarningMacro( "Unknown exception" );
// }
}
void DataElement::SetValueFieldLength( VL vl, bool readvalues )
{
if( readvalues )
ValueField->SetLength(vl); // perform realloc
else
ValueField->SetLengthOnly(vl); // do not perform realloc
}
} // end namespace gdcm
|