#include "gdcmBitmap.h"
#include "gdcmSequenceOfFragments.h"
#include "gdcmRAWCodec.h"
#include "gdcmJPEGCodec.h"
#include "gdcmPVRGCodec.h"
#include "gdcmKAKADUCodec.h"
#include "gdcmJPEGLSCodec.h"
#include "gdcmJPEG2000Codec.h"
#include "gdcmRLECodec.h"
#include <cstring>
namespace gdcm
{
Bitmap::Bitmap():
PlanarConfiguration(0),
NumberOfDimensions(2),
TS(),
PF(),
PI(),
Dimensions(),
PixelData(),
LUT(new LookupTable),
NeedByteSwap(false),
LossyFlag(false)
{}
Bitmap::~Bitmap() = default;
unsigned int Bitmap::GetNumberOfDimensions() const
{
gdcm_assert( NumberOfDimensions );
return NumberOfDimensions;
}
void Bitmap::SetNumberOfDimensions(unsigned int dim)
{
NumberOfDimensions = dim;
gdcm_assert( NumberOfDimensions );
Dimensions.resize( 3 ); gdcm_assert( NumberOfDimensions == 2 || NumberOfDimensions == 3 );
if( NumberOfDimensions == 2 )
{
Dimensions[2] = 1;
}
}
const unsigned int *Bitmap::GetDimensions() const
{
gdcm_assert( NumberOfDimensions );
return Dimensions.data();
}
unsigned int Bitmap::GetDimension(unsigned int idx) const
{
gdcm_assert( NumberOfDimensions );
return Dimensions[idx];
}
void Bitmap::SetDimensions(const unsigned int *dims)
{
gdcm_assert( NumberOfDimensions );
#if 0#else
gdcm_assert( Dimensions.size() == 3 );
Dimensions[0] = dims[0];
Dimensions[1] = dims[1];
if( NumberOfDimensions == 2 )
Dimensions[2] = 1;
else
Dimensions[2] = dims[2];
#endif
}
void Bitmap::SetDimension(unsigned int idx, unsigned int dim)
{
gdcm_assert( NumberOfDimensions );
gdcm_assert( idx < NumberOfDimensions );
Dimensions.resize( 3 );
Dimensions[idx] = dim;
if( NumberOfDimensions == 2 )
{
Dimensions[2] = 1;
}
}
unsigned int Bitmap::GetPlanarConfiguration() const
{
if( PlanarConfiguration && PF.GetSamplesPerPixel() != 3 )
{
gdcmWarningMacro(
"Can't set PlanarConfiguration if SamplesPerPixel is not 3" );
return 0;
}
return PlanarConfiguration;
}
void Bitmap::SetPlanarConfiguration(unsigned int pc)
{
gdcm_assert( pc == 0 || pc == 1 );
PlanarConfiguration = pc;
if( pc )
{
if( PF.GetSamplesPerPixel() != 3 ) {
gdcmWarningMacro( "Cannot have Planar Configuration in non RGB input. Discarding" );
PlanarConfiguration = 0;
}
const TransferSyntax &ts = GetTransferSyntax();
if( ts == TransferSyntax::JPEGBaselineProcess1
|| ts == TransferSyntax::JPEGExtendedProcess2_4
|| ts == TransferSyntax::JPEGExtendedProcess3_5
|| ts == TransferSyntax::JPEGSpectralSelectionProcess6_8
|| ts == TransferSyntax::JPEGFullProgressionProcess10_12
|| ts == TransferSyntax::JPEGLosslessProcess14
|| ts == TransferSyntax::JPEGLosslessProcess14_1
|| ts == TransferSyntax::JPEGLSLossless
|| ts == TransferSyntax::JPEGLSNearLossless
|| ts == TransferSyntax::JPEG2000Lossless
|| ts == TransferSyntax::JPEG2000
|| ts == TransferSyntax::JPIPReferenced
|| ts == TransferSyntax::RLELossless )
{
gdcmWarningMacro( "Cannot have Planar Configuration in JPEG/JPEG-LS/JPEG 2000. Discarding" );
PlanarConfiguration = 0;
}
}
gdcm_assert( PlanarConfiguration == 0 || PlanarConfiguration == 1 );
}
void Bitmap::Clear()
{
Dimensions.clear();
}
const PhotometricInterpretation &Bitmap::GetPhotometricInterpretation() const
{
return PI;
}
void Bitmap::SetPhotometricInterpretation(
PhotometricInterpretation const &pi)
{
PI = pi;
}
#if 0#endif
unsigned long Bitmap::GetBufferLength() const
{
if( PF == PixelFormat::UNKNOWN ) return 0;
gdcm_assert( NumberOfDimensions );
if( NumberOfDimensions != Dimensions.size() )
{
gdcm_assert( Dimensions[2] == 1 );
}
unsigned long len = 0;
unsigned int mul = 1;
std::vector<unsigned int>::const_iterator it = Dimensions.begin();
for(; it != Dimensions.end(); ++it)
{
if( *it == 0 ) { gdcmWarningMacro("Dimension has been found to be zero" ); }
mul *= *it;
}
if( PF == PixelFormat::UINT12 || PF == PixelFormat::INT12 )
{
#if 1
mul *= PF.GetPixelSize();
#else#endif
}
else if( PF == PixelFormat::SINGLEBIT )
{
gdcm_assert( PF.GetSamplesPerPixel() == 1 );
const size_t bytesPerRow = Dimensions[0] / 8 + (Dimensions[0] % 8 != 0 ? 1 : 0);
size_t save = bytesPerRow * Dimensions[1];
if( NumberOfDimensions > 2 )
save *= Dimensions[2];
if(Dimensions[0] % 8 == 0 )
gdcm_assert( save * 8 == mul );
mul = (unsigned int)save;
}
else if( PF.GetBitsAllocated() % 8 != 0 )
{
gdcm_assert( PF.GetSamplesPerPixel() == 1 );
const ByteValue *bv = PixelData.GetByteValue();
gdcm_assert( bv );
unsigned int ref = bv->GetLength() / mul;
if( !GetTransferSyntax().IsEncapsulated() )
gdcm_assert( bv->GetLength() % mul == 0 );
mul *= ref;
}
else
{
mul *= PF.GetPixelSize();
}
len = mul;
return len;
}
bool Bitmap::TryRAWCodec(char *buffer, bool &lossyflag) const
{
RAWCodec codec;
const TransferSyntax &ts = GetTransferSyntax();
if(!buffer)
{
if( codec.CanDecode( ts ) ) {
lossyflag = false;
if( GetPhotometricInterpretation() == PhotometricInterpretation::YBR_FULL_422 )
{
lossyflag = true;
}
return true;
}
return false;
}
const ByteValue *bv = PixelData.GetByteValue();
if( bv )
{
unsigned long len = GetBufferLength();
if( !codec.CanDecode( ts ) ) return false;
codec.SetPlanarConfiguration( GetPlanarConfiguration() );
codec.SetPhotometricInterpretation( GetPhotometricInterpretation() );
codec.SetLUT( GetLUT() );
codec.SetPixelFormat( GetPixelFormat() );
codec.SetNeedByteSwap( GetNeedByteSwap() );
codec.SetNeedOverlayCleanup( AreOverlaysInPixelData() || UnusedBitsPresentInPixelData() );
DataElement out;
bool r = codec.DecodeBytes(bv->GetPointer(), bv->GetLength(),
buffer, len);
if( GetNeedByteSwap() )
{
Bitmap *i = const_cast<Bitmap*>(this);
i->SetNeedByteSwap(false);
}
if( !r ) return false;
if( len != bv->GetLength() )
{
}
#if 0#else
const PixelFormat pf = GetPixelFormat(); const PixelFormat & cpf = codec.GetPixelFormat();
if( cpf.GetBitsAllocated() == 16 && pf.GetBitsAllocated() == 12 )
{
Bitmap *i = const_cast<Bitmap*>(this);
gdcmWarningMacro( "Mismatch Bits Allocated. correcting." );
i->GetPixelFormat().SetBitsAllocated( cpf.GetBitsAllocated() );
i->GetPixelFormat().SetBitsStored( pf.GetBitsStored() );
i->GetPixelFormat().SetHighBit( pf.GetHighBit() );
}
#endif
unsigned long check; check = len;
gdcm_assert( check == len || check == len + 1 );
(void)check; return r;
}
return false;
}
bool Bitmap::TryJPEGCodec(char *buffer, bool &lossyflag) const
{
JPEGCodec codec;
const TransferSyntax &ts = GetTransferSyntax();
if(!buffer)
{
if( codec.CanDecode( ts ) ) {
TransferSyntax ts2;
const SequenceOfFragments *sf = PixelData.GetSequenceOfFragments();
if( !sf ) return false;
const Fragment &frag = sf->GetFragment(0);
if( frag.IsEmpty() ) return false;
const ByteValue &bv2 = dynamic_cast<const ByteValue&>(frag.GetValue());
PixelFormat pf = GetPixelFormat(); codec.SetPixelFormat( pf );
std::stringstream ss;
ss.write( bv2.GetPointer(), bv2.GetLength() );
bool b = codec.GetHeaderInfo( ss, ts2 );
if(!b) return false;
gdcm_assert(b);
lossyflag = codec.IsLossy();
#if 0#else
const PixelFormat & cpf = codec.GetPixelFormat();
if( cpf.GetBitsAllocated() <= pf.GetBitsAllocated() )
{
if( cpf.GetPixelRepresentation() == pf.GetPixelRepresentation() )
{
if( cpf.GetSamplesPerPixel() == pf.GetSamplesPerPixel() )
{
if( cpf.GetBitsStored() < pf.GetBitsStored() )
{
Bitmap *i = const_cast<Bitmap*>(this);
gdcmWarningMacro( "Encapsulated stream has fewer bits actually stored on disk. correcting." );
i->GetPixelFormat().SetBitsAllocated( cpf.GetBitsAllocated() );
i->GetPixelFormat().SetBitsStored( cpf.GetBitsStored() );
}
}
}
}
#endif
if( GetDimensions()[0] != codec.GetDimensions()[0]
|| GetDimensions()[1] != codec.GetDimensions()[1] )
{
gdcmWarningMacro( "dimension mismatch for JPEG" );
(const_cast<Bitmap*>(this))->SetDimensions( codec.GetDimensions() ); }
return true;
}
return false;
}
if( codec.CanDecode( ts ) )
{
unsigned long len = GetBufferLength();
codec.SetNumberOfDimensions( GetNumberOfDimensions() );
codec.SetDimensions( GetDimensions() );
codec.SetPlanarConfiguration( GetPlanarConfiguration() );
codec.SetPhotometricInterpretation( GetPhotometricInterpretation() );
codec.SetPixelFormat( GetPixelFormat() );
codec.SetNeedOverlayCleanup( AreOverlaysInPixelData() || UnusedBitsPresentInPixelData() );
DataElement out;
bool r = codec.Decode(PixelData, out);
if( !r )
{
return false;
}
if ( GetPlanarConfiguration() != codec.GetPlanarConfiguration() )
{
Bitmap *i = const_cast<Bitmap*>(this); (void)i;
}
#if 1
const PixelFormat & cpf = codec.GetPixelFormat();
const PixelFormat & pf = GetPixelFormat();
if ( pf != cpf )
{
if( pf.GetPixelRepresentation() == cpf.GetPixelRepresentation() ) {
if( pf.GetBitsAllocated() == 12 ) {
Bitmap *i = const_cast<Bitmap*>(this);
i->GetPixelFormat().SetBitsAllocated( 16 );
i->GetPixelFormat().SetBitsStored( 12 );
}
}
}
#else#endif
const ByteValue *outbv = out.GetByteValue();
gdcm_assert( outbv );
unsigned long check = outbv->GetLength(); (void)check;
if( len > outbv->GetLength() )
{
gdcmErrorMacro( "Impossible length: " << len << " should be (max): " << outbv->GetLength() );
return false;
}
gdcm_assert( len <= outbv->GetLength() );
if(buffer) memcpy(buffer, outbv->GetPointer(), len );
lossyflag = codec.IsLossy();
return true;
}
return false;
}
bool Bitmap::TryJPEGCodec2(std::ostream &os) const
{
unsigned long len = GetBufferLength();
const TransferSyntax &ts = GetTransferSyntax();
JPEGCodec codec;
if( codec.CanCode( ts ) )
{
codec.SetDimensions( GetDimensions() );
codec.SetPlanarConfiguration( GetPlanarConfiguration() );
codec.SetPhotometricInterpretation( GetPhotometricInterpretation() );
codec.SetPixelFormat( GetPixelFormat() );
codec.SetNeedOverlayCleanup( AreOverlaysInPixelData() || UnusedBitsPresentInPixelData() );
DataElement out;
bool r = codec.Code(PixelData, out);
if( !r )
{
return false;
}
if ( GetPhotometricInterpretation() != codec.GetPhotometricInterpretation() )
{
}
const ByteValue *outbv = out.GetByteValue();
gdcm_assert( outbv );
unsigned long check = outbv->GetLength(); (void)check;
gdcm_assert( outbv->GetLength() < len ); (void)len;
os.write( outbv->GetPointer(), outbv->GetLength() );
return true;
}
return false;
}
bool Bitmap::TryPVRGCodec(char *buffer, bool &lossyflag) const
{
unsigned long len = GetBufferLength();
const TransferSyntax &ts = GetTransferSyntax();
PVRGCodec codec;
if( codec.CanDecode( ts ) )
{
codec.SetPixelFormat( GetPixelFormat() );
codec.SetPlanarConfiguration( GetPlanarConfiguration() );
codec.SetPhotometricInterpretation( GetPhotometricInterpretation() );
codec.SetNeedOverlayCleanup( AreOverlaysInPixelData() || UnusedBitsPresentInPixelData() );
codec.SetDimensions( GetDimensions() );
DataElement out;
bool r = codec.Decode(PixelData, out);
if(!r) return false;
codec.SetLossyFlag( ts.IsLossy() );
gdcm_assert( r );
if ( GetPlanarConfiguration() != codec.GetPlanarConfiguration() )
{
Bitmap *i = const_cast<Bitmap*>(this);
i->PlanarConfiguration = codec.GetPlanarConfiguration();
}
const ByteValue *outbv = out.GetByteValue();
gdcm_assert( outbv );
unsigned long check = outbv->GetLength(); (void)check;
gdcm_assert( len <= outbv->GetLength() );
if(buffer) memcpy(buffer, outbv->GetPointer(), len );
lossyflag = codec.IsLossy();
return r;
}
return false;
}
bool Bitmap::TryKAKADUCodec(char *buffer, bool &lossyflag) const
{
unsigned long len = GetBufferLength();
const TransferSyntax &ts = GetTransferSyntax();
KAKADUCodec codec;
if( codec.CanDecode( ts ) )
{
codec.SetPixelFormat( GetPixelFormat() );
codec.SetNumberOfDimensions( GetNumberOfDimensions() );
codec.SetPlanarConfiguration( GetPlanarConfiguration() );
codec.SetPhotometricInterpretation( GetPhotometricInterpretation() );
codec.SetNeedOverlayCleanup( AreOverlaysInPixelData() || UnusedBitsPresentInPixelData() );
codec.SetDimensions( GetDimensions() );
DataElement out;
bool r = codec.Decode(PixelData, out);
if( !r ) return false;
const ByteValue *outbv = out.GetByteValue();
gdcm_assert( outbv );
unsigned long check = outbv->GetLength(); (void)check;
gdcm_assert( len <= outbv->GetLength() );
gdcm_assert( len <= outbv->GetLength() );
if(buffer) memcpy(buffer, outbv->GetPointer(), len );
lossyflag = codec.IsLossy();
if( codec.IsLossy() != ts.IsLossy() )
{
gdcmErrorMacro( "EVIL file, it is declared as lossless but is in fact lossy." );
}
return r;
}
return false;
}
bool Bitmap::TryJPEGLSCodec(char *buffer, bool &lossyflag) const
{
JPEGLSCodec codec;
const TransferSyntax &ts = GetTransferSyntax();
if(!buffer)
{
if( codec.CanDecode( ts ) ) {
TransferSyntax ts2;
const SequenceOfFragments *sf = PixelData.GetSequenceOfFragments();
if( !sf ) return false;
const Fragment &frag = sf->GetFragment(0);
if( frag.IsEmpty() ) return false;
const ByteValue &bv2 = dynamic_cast<const ByteValue&>(frag.GetValue());
std::stringstream ss;
ss.write( bv2.GetPointer(), bv2.GetLength() );
bool b = codec.GetHeaderInfo( ss, ts2 );
if( !b ) return false;
lossyflag = codec.IsLossy();
#if 0#endif
return true;
}
return false;
}
if( codec.CanDecode( ts ) )
{
unsigned long len = GetBufferLength();
codec.SetPixelFormat( GetPixelFormat() );
codec.SetBufferLength( len );
codec.SetNumberOfDimensions( GetNumberOfDimensions() );
codec.SetPlanarConfiguration( GetPlanarConfiguration() );
codec.SetPhotometricInterpretation( GetPhotometricInterpretation() );
codec.SetNeedOverlayCleanup( AreOverlaysInPixelData() || UnusedBitsPresentInPixelData() );
codec.SetDimensions( GetDimensions() );
DataElement out;
bool r = codec.Decode(PixelData, out);
if( !r ) return false;
const ByteValue *outbv = out.GetByteValue();
gdcm_assert( outbv );
unsigned long check = outbv->GetLength(); (void)check;
gdcm_assert( len <= outbv->GetLength() );
gdcm_assert( len <= outbv->GetLength() );
memcpy(buffer, outbv->GetPointer(), len );
lossyflag = codec.IsLossy();
if( codec.IsLossy() != ts.IsLossy() )
{
gdcmErrorMacro( "EVIL file, it is declared as lossless but is in fact lossy." );
}
const PixelFormat & cpf = codec.GetPixelFormat();
const PixelFormat & pf = GetPixelFormat();
if( cpf.GetBitsAllocated() == pf.GetBitsAllocated() )
{
if( cpf.GetPixelRepresentation() == pf.GetPixelRepresentation() )
{
if( cpf.GetSamplesPerPixel() == pf.GetSamplesPerPixel() )
{
if( cpf.GetBitsStored() < pf.GetBitsStored() )
{
Bitmap *i = const_cast<Bitmap*>(this);
gdcmWarningMacro( "Encapsulated stream has fewer bits actually stored on disk. correcting." );
i->GetPixelFormat().SetBitsAllocated( cpf.GetBitsAllocated() );
i->GetPixelFormat().SetBitsStored( cpf.GetBitsStored() );
}
else if( cpf.GetBitsStored() > pf.GetBitsStored() )
{
Bitmap *i = const_cast<Bitmap*>(this);
gdcmWarningMacro( "Encapsulated stream has more bits actually stored on disk. correcting." );
i->GetPixelFormat().SetBitsAllocated( cpf.GetBitsAllocated() );
i->GetPixelFormat().SetBitsStored( cpf.GetBitsStored() );
}
}
}
}
else
{
gdcmWarningMacro( "Bits Allocated are different. This is pretty bad using info from codestream" );
Bitmap *i = const_cast<Bitmap*>(this);
i->SetPixelFormat( codec.GetPixelFormat() );
}
return r;
}
return false;
}
bool Bitmap::IsLossy() const
{
return LossyFlag;
}
bool Bitmap::ComputeLossyFlag()
{
bool lossyflag;
if( this->GetBufferInternal(nullptr, lossyflag) )
{
LossyFlag = lossyflag;
return true;
}
LossyFlag = false;
return false;
}
bool Bitmap::TryJPEG2000Codec(char *buffer, bool &lossyflag) const
{
JPEG2000Codec codec;
const TransferSyntax &ts = GetTransferSyntax();
if(!buffer)
{
if( codec.CanDecode( ts ) ) {
TransferSyntax ts2;
const SequenceOfFragments *sf = PixelData.GetSequenceOfFragments();
if( !sf ) return false;
const Fragment &frag = sf->GetFragment(0);
if( frag.IsEmpty() ) return false;
const ByteValue &bv2 = dynamic_cast<const ByteValue&>(frag.GetValue());
bool b = codec.GetHeaderInfo( bv2.GetPointer(), bv2.GetLength() , ts2 );
if( !b ) return false;
lossyflag = codec.IsLossy();
#if 0#else
const PixelFormat & cpf = codec.GetPixelFormat();
const PixelFormat & pf = GetPixelFormat();
if( cpf.GetBitsAllocated() == pf.GetBitsAllocated() )
{
if( cpf.GetPixelRepresentation() == pf.GetPixelRepresentation() )
{
if( cpf.GetSamplesPerPixel() == pf.GetSamplesPerPixel() )
{
if( cpf.GetBitsStored() < pf.GetBitsStored() )
{
Bitmap *i = const_cast<Bitmap*>(this);
gdcmWarningMacro( "Encapsulated stream has fewer bits actually stored on disk. correcting." );
i->GetPixelFormat().SetBitsAllocated( cpf.GetBitsAllocated() );
i->GetPixelFormat().SetBitsStored( cpf.GetBitsStored() );
}
else if( cpf.GetBitsStored() > pf.GetBitsStored() )
{
Bitmap *i = const_cast<Bitmap*>(this);
gdcmWarningMacro( "Encapsulated stream has more bits actually stored on disk. correcting." );
i->GetPixelFormat().SetBitsAllocated( cpf.GetBitsAllocated() );
i->GetPixelFormat().SetBitsStored( cpf.GetBitsStored() );
}
}
}
}
else
{
gdcmWarningMacro( "Bits Allocated are different. This is pretty bad using info from codestream" );
Bitmap *i = const_cast<Bitmap*>(this);
i->SetPixelFormat( codec.GetPixelFormat() );
}
#endif
return true;
}
return false;
}
if( codec.CanDecode( ts ) )
{
unsigned long len = GetBufferLength();
codec.SetPixelFormat( GetPixelFormat() );
codec.SetNumberOfDimensions( GetNumberOfDimensions() );
codec.SetPlanarConfiguration( GetPlanarConfiguration() );
codec.SetPhotometricInterpretation( GetPhotometricInterpretation() );
codec.SetNeedOverlayCleanup( AreOverlaysInPixelData() || UnusedBitsPresentInPixelData() );
codec.SetDimensions( GetDimensions() );
DataElement out;
bool r = codec.Decode(PixelData, out);
if(!r) return false;
gdcm_assert( r );
const ByteValue *outbv = out.GetByteValue();
gdcm_assert( outbv );
unsigned long check = outbv->GetLength(); (void)check;
gdcm_assert( len <= outbv->GetLength() );
memcpy(buffer, outbv->GetPointer(), len );
lossyflag = codec.IsLossy();
if( codec.IsLossy() && !ts.IsLossy() )
{
gdcm_assert( codec.IsLossy() );
gdcm_assert( !ts.IsLossy() );
gdcmErrorMacro( "EVIL file, it is declared as lossless but is in fact lossy." );
}
#if 0#else
const PixelFormat & cpf = codec.GetPixelFormat();
const PixelFormat & pf = GetPixelFormat();
if( cpf.GetBitsAllocated() == pf.GetBitsAllocated() )
{
if( cpf.GetPixelRepresentation() == pf.GetPixelRepresentation() )
{
if( cpf.GetSamplesPerPixel() == pf.GetSamplesPerPixel() )
{
if( cpf.GetBitsStored() < pf.GetBitsStored() )
{
Bitmap *i = const_cast<Bitmap*>(this);
gdcmWarningMacro( "Encapsulated stream has fewer bits actually stored on disk. correcting." );
i->GetPixelFormat().SetBitsStored( cpf.GetBitsStored() );
}
}
}
}
#endif
return r;
}
return false;
}
bool Bitmap::TryJPEG2000Codec2(std::ostream &os) const
{
unsigned long len = GetBufferLength();
(void)len;
const TransferSyntax &ts = GetTransferSyntax();
JPEG2000Codec codec;
if( codec.CanCode( ts ) )
{
codec.SetDimensions( GetDimensions() );
codec.SetPixelFormat( GetPixelFormat() );
codec.SetNumberOfDimensions( GetNumberOfDimensions() );
codec.SetPlanarConfiguration( GetPlanarConfiguration() );
codec.SetPhotometricInterpretation( GetPhotometricInterpretation() );
codec.SetNeedOverlayCleanup( AreOverlaysInPixelData() || UnusedBitsPresentInPixelData() );
DataElement out;
bool r = codec.Code(PixelData, out);
gdcm_assert( r );
const ByteValue *outbv = out.GetByteValue();
gdcm_assert( outbv );
unsigned long check = outbv->GetLength(); (void)check;
os.write(outbv->GetPointer(), outbv->GetLength() );
return r;
}
return false;
}
bool Bitmap::TryRLECodec(char *buffer, bool &lossyflag ) const
{
unsigned long len = GetBufferLength();
const TransferSyntax &ts = GetTransferSyntax();
RLECodec codec;
if( codec.CanDecode( ts ) )
{
codec.SetDimensions( GetDimensions() );
codec.SetNumberOfDimensions( GetNumberOfDimensions() );
codec.SetPlanarConfiguration( GetPlanarConfiguration() );
codec.SetPhotometricInterpretation( GetPhotometricInterpretation() );
codec.SetPixelFormat( GetPixelFormat() );
codec.SetLUT( GetLUT() );
codec.SetNeedOverlayCleanup( AreOverlaysInPixelData() || UnusedBitsPresentInPixelData() );
codec.SetBufferLength( len );
DataElement out;
bool r = codec.Decode(PixelData, out);
if( !r ) return false;
const ByteValue *outbv = out.GetByteValue();
gdcm_assert( len <= outbv->GetLength() );
if(buffer) memcpy(buffer, outbv->GetPointer(), len ); lossyflag = false;
return true;
}
return false;
}
bool Bitmap::GetBuffer(char *buffer) const
{
bool dummy;
return GetBufferInternal(buffer, dummy);
}
bool Bitmap::GetBufferInternal(char *buffer, bool &lossyflag) const
{
bool success = false;
if( !success ) success = TryRAWCodec(buffer, lossyflag);
if( !success ) success = TryJPEGCodec(buffer, lossyflag);
if( !success ) success = TryPVRGCodec(buffer, lossyflag); if( !success ) success = TryJPEG2000Codec(buffer, lossyflag);
if( !success ) success = TryJPEGLSCodec(buffer, lossyflag);
if( !success ) success = TryRLECodec(buffer, lossyflag);
if( !success )
{
}
return success;
}
bool Bitmap::GetBuffer2(std::ostream &os) const
{
bool success = false;
if( !success ) success = TryJPEGCodec2(os);
if( !success )
{
throw Exception( "No codec found for this image");
}
return success;
}
bool Bitmap::IsTransferSyntaxCompatible( TransferSyntax const & ts ) const
{
if( GetTransferSyntax() == ts ) return true;
if( GetTransferSyntax() == TransferSyntax::JPEGExtendedProcess2_4 )
{
if( GetPixelFormat().GetBitsAllocated() == 8 )
{
if( ts == TransferSyntax::JPEGBaselineProcess1 ) return true;
}
}
return false;
}
void Bitmap::Print(std::ostream &os) const
{
Object::Print(os);
if( !IsEmpty() )
{
os << "NumberOfDimensions: " << NumberOfDimensions << "\n";
gdcm_assert( !Dimensions.empty() );
os << "Dimensions: (";
std::vector<unsigned int>::const_iterator it = Dimensions.begin();
os << *it;
for(++it; it != Dimensions.end(); ++it)
{
os << "," << *it;
}
os << ")\n";
PF.Print(os);
os << "PhotometricInterpretation: " << PI << "\n";
os << "PlanarConfiguration: " << PlanarConfiguration << "\n";
os << "TransferSyntax: " << TS << "\n";
}
}
}