Skip to content

Commit

Permalink
Merge branch 'upstream-GDCM' into gdcm-sc-image-metadata
Browse files Browse the repository at this point in the history
# By GDCM Upstream
* upstream-GDCM:
  GDCM 2024-03-15 (a9916012)
  • Loading branch information
thewtex committed Mar 15, 2024
2 parents 770b749 + 3637e49 commit 1b97204
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 983 deletions.
3 changes: 0 additions & 3 deletions Modules/ThirdParty/GDCM/src/gdcm/.codespellrc

This file was deleted.

2 changes: 1 addition & 1 deletion Modules/ThirdParty/GDCM/src/gdcm/CMakeLists.txt
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
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
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
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
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
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
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
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 @@ -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 @@ -1388,6 +1400,7 @@ std::vector<double> ImageHelper::GetSpacingValue(File const & f)
MediaStorage ms;
ms.SetFromFile(f);
const DataSet& ds = f.GetDataSet();
Tag spacingtag(0xffff,0xffff);

if( ms == MediaStorage::EnhancedCTImageStorage
|| ms == MediaStorage::EnhancedMRImageStorage
Expand Down Expand Up @@ -1450,8 +1463,24 @@ std::vector<double> ImageHelper::GetSpacingValue(File const & f)
return sp;
}
}
else if( ms == MediaStorage::SecondaryCaptureImageStorage )
{
if( ds.FindDataElement( Tag(0x0028,0x0030) ) )
{
// Type 1C in 'SC Image' (for calibrated images)
spacingtag = Tag(0x0028,0x0030);
}
else if( ds.FindDataElement( Tag(0x0018,0x2010) ) )
{
// Type 3 in 'SC Image'
spacingtag = Tag(0x0018,0x2010);
}
}
else
{
spacingtag = GetSpacingTagFromMediaStorage(ms);
}

Tag spacingtag = GetSpacingTagFromMediaStorage(ms);
if( spacingtag != Tag(0xffff,0xffff) && ds.FindDataElement( spacingtag ) && !ds.GetDataElement( spacingtag ).IsEmpty() )
{
const DataElement& de = ds.GetDataElement( spacingtag );
Expand Down Expand Up @@ -2027,6 +2056,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 +2926,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
Expand Up @@ -85,6 +85,12 @@ class GDCM_EXPORT ImageHelper
static void SetForcePixelSpacing(bool);
static bool GetForcePixelSpacing();

/// Opt into Image Plane Module for Secondary Capture Image Storage
/// Enable reading Image Position Patient (IPP) and Image Orientation Patient (IOP)
/// for Secondary Capture Image Storage.
static void SetSecondaryCaptureImagePlaneModule(bool);
static bool GetSecondaryCaptureImagePlaneModule();

/// This function checks tags (0x0028, 0x0010) and (0x0028, 0x0011) for the
/// rows and columns of the image in pixels (as opposed to actual distances).
/// The output is {col , row}
Expand Down Expand Up @@ -156,6 +162,7 @@ class GDCM_EXPORT ImageHelper
static bool ForceRescaleInterceptSlope;
static bool PMSRescaleInterceptSlope;
static bool ForcePixelSpacing;
static bool SecondaryCaptureImagePlaneModule;
};

} // end namespace gdcm
Expand Down

0 comments on commit 1b97204

Please sign in to comment.