Skip to content

Commit

Permalink
GDCM 2024-03-25 (06091299)
Browse files Browse the repository at this point in the history
Code extracted from:

    https://github.com/malaterre/GDCM.git

at commit 06091299f2227f8470a4468821d231b760337ca0 (release).
  • Loading branch information
GDCM Upstream authored and thewtex committed Mar 25, 2024
1 parent e7657b4 commit 919cf42
Show file tree
Hide file tree
Showing 20 changed files with 255 additions and 60 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ endif()
#----------------------------------------------------------------------------

project(GDCM
VERSION 3.0.22
VERSION 3.0.23
LANGUAGES CXX C
)
## NOTE: the "DESCRIPTION" feature of project() was introduced in cmake 3.10.0
Expand Down
10 changes: 9 additions & 1 deletion Source/DataStructureAndEncodingDefinition/gdcmCSAHeader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,8 @@ bool check_mapping(uint32_t syngodt, const char *vr)
{
static const unsigned int max = sizeof(mapping) / sizeof(equ);
const equ *p = mapping;
assert( syngodt <= mapping[max-1].syngodt ); (void)max;
if( syngodt > mapping[max-1].syngodt ) return false;
assert( syngodt <= mapping[max-1].syngodt );
while(p->syngodt < syngodt )
{
//std::cout << "mapping:" << p->vr << std::endl;
Expand Down Expand Up @@ -1098,6 +1099,11 @@ bool CSAHeader::LoadFromDataElement(DataElement const &de)
char vr[4];
ss.read(vr, 4);
// In dataset without magic signature (OLD FORMAT) vr[3] is garbage...
if( vr[2] != 0 )
{
gdcmErrorMacro( "Garbage data. Stopping CSA parsing." );
return false;
}
assert( /*vr[3] == 0 &&*/ vr[2] == 0 );
csael.SetVR( VR::GetVRTypeFromFile(vr) );
//std::cout << "VR " << vr << ", ";
Expand Down Expand Up @@ -1131,8 +1137,10 @@ bool CSAHeader::LoadFromDataElement(DataElement const &de)
uint32_t item_xx[4];
ss.read((char*)&item_xx, 4*sizeof(uint32_t));
SwapperNoOp::SwapArray(item_xx,4);
if( item_xx[2] != 77 && item_xx[2] != 205 ) return false;
assert( item_xx[2] == 77 || item_xx[2] == 205 );
uint32_t len = item_xx[1]; // 2nd element
if( item_xx[0] != item_xx[1] || item_xx[1] != item_xx[3] ) return false;
assert( item_xx[0] == item_xx[1] && item_xx[1] == item_xx[3] );
if( len )
{
Expand Down
4 changes: 2 additions & 2 deletions Source/DataStructureAndEncodingDefinition/gdcmElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ template<> class EncodingImplementation<VR::VRBINARY> {
assert( _is ); // Is stream valid ?
_is.read( reinterpret_cast<char*>(data+0), type_size);
for(unsigned long i=1; i<length; ++i) {
assert( _is );
if( _is )
_is.read( reinterpret_cast<char*>(data+i), type_size );
}
//ByteSwap<T>::SwapRangeFromSwapCodeIntoSystem(data,
Expand All @@ -489,7 +489,7 @@ template<> class EncodingImplementation<VR::VRBINARY> {
assert( _is ); // Is stream valid ?
_is.read( reinterpret_cast<char*>(data+0), type_size);
for(unsigned long i=1; i<length; ++i) {
assert( _is );
if( _is )
_is.read( reinterpret_cast<char*>(data+i), type_size );
}
//ByteSwap<T>::SwapRangeFromSwapCodeIntoSystem(data,
Expand Down
33 changes: 28 additions & 5 deletions Source/MediaStorageAndFileFormat/gdcmCleaner.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,12 @@ struct Cleaner::impl {
bool AllMissingPrivateCreator;
bool AllGroupLength;
bool AllIllegal;
bool WhenScrubFails;
impl()
: AllMissingPrivateCreator(true),
AllGroupLength(true),
AllIllegal(true) {}
AllIllegal(true),
WhenScrubFails(false) {}

enum ACTION { NONE, EMPTY, REMOVE, SCRUB };
enum ACTION ComputeAction(File const &file, DataSet &ds,
Expand Down Expand Up @@ -668,6 +670,10 @@ struct Cleaner::impl {
bool RemoveMissingPrivateCreator(Tag const & /*t*/) { return false; }
void RemoveAllGroupLength(bool remove) { AllGroupLength = remove; }
void RemoveAllIllegal(bool remove) { AllIllegal = remove; }
void EmptyWhenScrubFails(bool empty) { WhenScrubFails = empty; }

bool CleanCSAImage(DataSet &ds, const DataElement &de);
bool CleanCSASeries(DataSet &ds, const DataElement &de);
};

static VR ComputeDictVR(File &file, DataSet &ds, DataElement const &de) {
Expand Down Expand Up @@ -840,7 +846,7 @@ static inline bool bs_is_signature(const ByteValue *bv, const char *str) {
return false;
}

static bool CleanCSAImage(DataSet &ds, const DataElement &de) {
bool Cleaner::impl::CleanCSAImage(DataSet &ds, const DataElement &de) {
const ByteValue *bv = de.GetByteValue();
// fast path:
if (!bv) return true;
Expand Down Expand Up @@ -888,6 +894,13 @@ static bool CleanCSAImage(DataSet &ds, const DataElement &de) {
gdcmDebugMacro("Zero-out CSA header");
return true;
}
// fallback logic:
if (WhenScrubFails && is_signature(bv, sv10)) {
// so SV10 header has been identified, but we failed to 'scrub', let's
// empty it:
ds.Replace(clean);
return true;
}
gdcmErrorMacro("Failure to call CleanCSAImage");
return false;
}
Expand All @@ -900,7 +913,7 @@ static bool CleanCSAImage(DataSet &ds, const DataElement &de) {
return true;
}

static bool CleanCSASeries(DataSet &ds, const DataElement &de) {
bool Cleaner::impl::CleanCSASeries(DataSet &ds, const DataElement &de) {
const ByteValue *bv = de.GetByteValue();
// fast path:
if (!bv) return true;
Expand Down Expand Up @@ -944,6 +957,13 @@ static bool CleanCSASeries(DataSet &ds, const DataElement &de) {
gdcmDebugMacro("Zero-out CSA header");
return true;
}
// fallback logic:
if (WhenScrubFails && is_signature(bv, sv10)) {
// so SV10 header has been identified, but we failed to 'scrub', let's
// empty it:
ds.Replace(clean);
return true;
}
gdcmErrorMacro("Failure to call CleanCSASeries");
return false;
}
Expand Down Expand Up @@ -1065,8 +1085,8 @@ static bool IsDPathInSet(std::set<DPath> const &aset, DPath const dpath) {
}

Cleaner::impl::ACTION Cleaner::impl::ComputeAction(
File const & /*file*/, DataSet &ds, const DataElement &de, VR const &ref_dict_vr,
const std::string &tag_path) {
File const & /*file*/, DataSet &ds, const DataElement &de,
VR const &ref_dict_vr, const std::string &tag_path) {
const Tag &tag = de.GetTag();
// Group Length & Illegal cannot be preserved so it is safe to do them now:
if (tag.IsGroupLength()) {
Expand Down Expand Up @@ -1302,6 +1322,9 @@ void Cleaner::RemoveAllGroupLength(bool remove) {
pimpl->RemoveAllGroupLength(remove);
}
void Cleaner::RemoveAllIllegal(bool remove) { pimpl->RemoveAllIllegal(remove); }
void Cleaner::EmptyWhenScrubFails(bool empty) {
pimpl->EmptyWhenScrubFails(empty);
}

bool Cleaner::Clean() {
DataSet &ds = F->GetDataSet();
Expand Down
3 changes: 3 additions & 0 deletions Source/MediaStorageAndFileFormat/gdcmCleaner.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class GDCM_EXPORT Cleaner : public Subject {
/// Should I remove all illegal attribute. Default: true
void RemoveAllIllegal(bool remove);

/// Should I empty instead of scrub upon failure
void EmptyWhenScrubFails(bool empty);

/// main loop
bool Clean();

Expand Down
13 changes: 9 additions & 4 deletions Source/MediaStorageAndFileFormat/gdcmDirectionCosines.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,20 @@ double DirectionCosines::Dot() const
}

// static function is within gdcm:: namespace, so should not pollute too much on UNIX
static inline double Norm(const double x[3])
static inline double NormImpl(const double x[3])
{
return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]);
}

double DirectionCosines::Norm(const double v[3])
{
return NormImpl(v);
}

void DirectionCosines::Normalize(double v[3])
{
double den;
if ( (den = Norm(v)) != 0.0 )
if ( (den = NormImpl(v)) != 0.0 )
{
for (int i=0; i < 3; i++)
{
Expand All @@ -119,15 +124,15 @@ void DirectionCosines::Normalize()
{
double *x = Values;
double den;
if ( (den = Norm(x)) != 0.0 )
if ( (den = NormImpl(x)) != 0.0 )
{
for (int i=0; i < 3; i++)
{
x[i] /= den;
}
}
x = Values+3;
if ( (den = Norm(x)) != 0.0 )
if ( (den = NormImpl(x)) != 0.0 )
{
for (int i=0; i < 3; i++)
{
Expand Down
3 changes: 3 additions & 0 deletions Source/MediaStorageAndFileFormat/gdcmDirectionCosines.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class GDCM_EXPORT DirectionCosines
/// Normalize in-place
static void Normalize(double v[3]);

/// Return norm of the vector
static double Norm(const double v[3]);

/// Make the class behave like a const double *
operator const double* () const { return Values; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,11 @@ EquipmentManufacturer::Type EquipmentManufacturer::Compute(DataSet const& ds) {
manu.SetFromDataSet(ds);
manufacturer = manu.GetValue().Trim();
// TODO: contributing equipement ?
} else {
}
if( manufacturer.empty() )
{
// MFSPLIT export seems to remove the attribute completely:
// or in some case make it empty
manufacturer = GetPrivateTagValueOrEmpty<VR::SH, VM::VM1>(
ds, PrivateTag(0x0021, 0x0022, "SIEMENS MR SDS 01"));
}
Expand Down
2 changes: 1 addition & 1 deletion Source/MediaStorageAndFileFormat/gdcmFileStreamer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include <io.h>
typedef int64_t off64_t;
#else
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__EMSCRIPTEN__)
# define off64_t off_t
#endif
#include <unistd.h> // ftruncate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ bool ImageChangeTransferSyntax::Change()
if( !b )
{
gdcmErrorMacro( "Error in getting buffer from input image." );
delete bv0;
return false;
}
pixeldata.SetValue( *bv0 );
Expand Down
17 changes: 11 additions & 6 deletions Source/MediaStorageAndFileFormat/gdcmImageCodec.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,8 @@ bool ImageCodec::CleanupUnusedBits(char * data8, size_t datalen)
smask = (uint16_t)(
smask << ( 16 - (PF.GetBitsAllocated() - PF.GetBitsStored() + 1) ));
// nmask : to propagate sign bit on negative values
int16_t nmask = (int16_t)(0x8000U >> ( PF.GetBitsAllocated() - PF.GetBitsStored() - 1 ));
int16_t nmask = (int16_t)0x8000;
nmask = (int16_t)(nmask >> ( PF.GetBitsAllocated() - PF.GetBitsStored() - 1 ));

uint16_t *start = (uint16_t*)data;
for( uint16_t *p = start ; p != start + datalen / 2; ++p )
Expand Down Expand Up @@ -515,7 +516,8 @@ bool ImageCodec::DoOverlayCleanup(std::istream &is, std::ostream &os)
smask = (uint16_t)(
smask << ( 16 - (PF.GetBitsAllocated() - PF.GetBitsStored() + 1) ));
// nmask : to propagate sign bit on negative values
int16_t nmask = (int16_t)(0x8000U >> ( PF.GetBitsAllocated() - PF.GetBitsStored() - 1 ));
int16_t nmask = (int16_t)0x8000;
nmask = (int16_t)(nmask >> ( PF.GetBitsAllocated() - PF.GetBitsStored() - 1 ));

uint16_t c;
while( is.read((char*)&c,2) )
Expand Down Expand Up @@ -673,6 +675,7 @@ bool ImageCodec::DecodeByStreams(std::istream &is, std::ostream &os)

// Do the overlay cleanup (cleanup the unused bits)
// must be the last operation (duh!)
bool copySuccess = false;
if ( PF.GetBitsAllocated() != PF.GetBitsStored()
&& PF.GetBitsAllocated() != 8 )
{
Expand All @@ -683,21 +686,23 @@ bool ImageCodec::DecodeByStreams(std::istream &is, std::ostream &os)
// Sigh, I finally found someone not declaring that unused bits where not zero:
// gdcmConformanceTests/dcm4chee_unusedbits_not_zero.dcm
if( NeedOverlayCleanup )
DoOverlayCleanup(*cur_is,os);
{
copySuccess = DoOverlayCleanup(*cur_is, os);
}
else
{
// Once the issue with IMAGES/JPLY/RG3_JPLY aka gdcmData/D_CLUNIE_RG3_JPLY.dcm is solved the previous
// code will be replace with a simple call to:
DoSimpleCopy(*cur_is,os);
copySuccess = DoSimpleCopy(*cur_is, os);
}
}
else
{
assert( PF.GetBitsAllocated() == PF.GetBitsStored() );
DoSimpleCopy(*cur_is,os);
copySuccess = DoSimpleCopy(*cur_is, os);
}

return true;
return copySuccess;
}

bool ImageCodec::IsValid(PhotometricInterpretation const &)
Expand Down
28 changes: 25 additions & 3 deletions Source/MediaStorageAndFileFormat/gdcmImageHelper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace gdcm
bool ImageHelper::ForceRescaleInterceptSlope = false;
bool ImageHelper::PMSRescaleInterceptSlope = true;
bool ImageHelper::ForcePixelSpacing = false;
bool ImageHelper::SecondaryCaptureImagePlaneModule = false;

static bool GetOriginValueFromSequence(const DataSet& ds, const Tag& tfgs, std::vector<double> &ori)
{
Expand Down Expand Up @@ -205,6 +206,7 @@ static bool ComputeZSpacingFromIPP(const DataSet &ds, double &zspacing)
double normal[3];
DirectionCosines dc( cosines.data() );
dc.Cross( normal );
DirectionCosines::Normalize(normal);

// For each item
SequenceOfItems::SizeType nitems = sqi->GetNumberOfItems();
Expand Down Expand Up @@ -577,7 +579,7 @@ std::vector<double> ImageHelper::GetOriginValue(File const & f)

// else
const Tag timagepositionpatient(0x0020, 0x0032);
if( ms != MediaStorage::SecondaryCaptureImageStorage && ds.FindDataElement( timagepositionpatient ) )
if( (ms != MediaStorage::SecondaryCaptureImageStorage || SecondaryCaptureImagePlaneModule) && ds.FindDataElement( timagepositionpatient ) )
{
const DataElement& de = ds.GetDataElement( timagepositionpatient );
Attribute<0x0020,0x0032> at = {{0,0,0}}; // default value if empty
Expand Down Expand Up @@ -729,7 +731,7 @@ std::vector<double> ImageHelper::GetDirectionCosinesValue(File const & f)
}

dircos.resize( 6 );
if( ms == MediaStorage::SecondaryCaptureImageStorage || !GetDirectionCosinesFromDataSet(ds, dircos) )
if( (ms == MediaStorage::SecondaryCaptureImageStorage && !SecondaryCaptureImagePlaneModule) || !GetDirectionCosinesFromDataSet(ds, dircos) )
{
dircos[0] = 1;
dircos[1] = 0;
Expand Down Expand Up @@ -773,6 +775,16 @@ bool ImageHelper::GetForcePixelSpacing()
return ForcePixelSpacing;
}

void ImageHelper::SetSecondaryCaptureImagePlaneModule(bool b)
{
SecondaryCaptureImagePlaneModule = b;
}

bool ImageHelper::GetSecondaryCaptureImagePlaneModule()
{
return SecondaryCaptureImagePlaneModule;
}

bool GetRescaleInterceptSlopeValueFromDataSet(const DataSet& ds, std::vector<double> & interceptslope)
{
Attribute<0x0028,0x1052> at1;
Expand Down Expand Up @@ -1187,7 +1199,7 @@ std::vector<double> ImageHelper::GetRescaleInterceptSlopeValue(File const & f)
// Case is: MAGNETOM Prisma / syngo MR XA30A with MFSPLIT
interceptslope[0] = dummy[0];
interceptslope[1] = dummy[1];
gdcmWarningMacro( "Forcing Modality LUT used for MR Image Storage: [" << dummy[0] << "," << dummy[1] << "]" );
gdcmDebugMacro( "Forcing Modality LUT used for MR Image Storage: [" << dummy[0] << "," << dummy[1] << "]" );
}
}
}
Expand Down Expand Up @@ -1263,6 +1275,14 @@ Tag ImageHelper::GetSpacingTagFromMediaStorage(MediaStorage const &ms)
t = Tag(0x3002,0x0011); // ImagePlanePixelSpacing
break;
case MediaStorage::SecondaryCaptureImageStorage:
if( ImageHelper::SecondaryCaptureImagePlaneModule ) {
// Make SecondaryCaptureImagePlaneModule act as ForcePixelSpacing
// This is different from Basic Pixel Spacing Calibration Macro Attributes
t = Tag(0x0028,0x0030);
} else {
t = Tag(0x0018,0x2010);
}
break;
case MediaStorage::MultiframeSingleBitSecondaryCaptureImageStorage:
case MediaStorage::MultiframeGrayscaleByteSecondaryCaptureImageStorage:
case MediaStorage::MultiframeGrayscaleWordSecondaryCaptureImageStorage:
Expand Down Expand Up @@ -2027,6 +2047,7 @@ void ImageHelper::SetOriginValue(DataSet & ds, const Image & image)

double normal[3];
dc.Cross( normal );
DirectionCosines::Normalize(normal);

for(unsigned int i = 0; i < dimz; ++i )
{
Expand Down Expand Up @@ -2896,6 +2917,7 @@ MediaStorage ImageHelper::ComputeMediaStorageFromModality(const char *modality,
|| pi == PhotometricInterpretation::YBR_RCT
|| pi == PhotometricInterpretation::YBR_ICT
|| pi == PhotometricInterpretation::YBR_PARTIAL_420
|| pi == PhotometricInterpretation::YBR_FULL /* PI when coming from other gdcm filter */
|| pi == PhotometricInterpretation::YBR_FULL_422 ) &&
pixeltype.GetBitsAllocated() == 8 &&
pixeltype.GetBitsStored() == 8 &&
Expand Down
Loading

0 comments on commit 919cf42

Please sign in to comment.