From 3b643f53f9dec01a5391edc4529f2b985e890310 Mon Sep 17 00:00:00 2001 From: Bradley Lowekamp Date: Wed, 19 Apr 2023 12:54:13 -0400 Subject: [PATCH 1/3] BUG: Remove dead code in MRC Header The computed values are unused and MRC extended headers are not fully supported. --- Modules/IO/MRC/src/itkMRCHeaderObject.cxx | 49 ----------------------- 1 file changed, 49 deletions(-) diff --git a/Modules/IO/MRC/src/itkMRCHeaderObject.cxx b/Modules/IO/MRC/src/itkMRCHeaderObject.cxx index d2b79a578ee..b9c05eef0a8 100644 --- a/Modules/IO/MRC/src/itkMRCHeaderObject.cxx +++ b/Modules/IO/MRC/src/itkMRCHeaderObject.cxx @@ -110,55 +110,6 @@ MRCHeaderObject::SetHeader(const Header * buffer) this->m_ExtendedHeader = nullptr; this->m_ExtendedFeiHeader = nullptr; - SizeValueType extendedHeaderBytes = 0; - if (this->m_Header.nreal & 1) - { - extendedHeaderBytes += 2; - } - if (this->m_Header.nreal & 2) - { - extendedHeaderBytes += 6; - } - if (this->m_Header.nreal & 4) - { - extendedHeaderBytes += 3; - } - if (this->m_Header.nreal & 8) - { - extendedHeaderBytes += 2; - } - if (this->m_Header.nreal & 16) - { - extendedHeaderBytes += 2; - } - if (this->m_Header.nreal & 32) - { - extendedHeaderBytes += 4; - } - if (this->m_Header.nreal & 64) - { - extendedHeaderBytes += 2; - } - if (this->m_Header.nreal & 128) - { - extendedHeaderBytes += 4; - } - if (this->m_Header.nreal & 256) - { - extendedHeaderBytes += 2; - } - if (this->m_Header.nreal & 512) - { - extendedHeaderBytes += 4; - } - if (this->m_Header.nreal & 1024) - { - extendedHeaderBytes += 2; - } - // TODO: all the above to set extendedHeaderBytes, yet extendedHeaderBytes is unused! - itkWarningMacro(<< "extendedHeaderBytes is: " << extendedHeaderBytes - << "If you see this log please contact https://github.com/InsightSoftwareConsortium/ITK/pull/2778"); - this->m_ExtendedHeaderSize = this->m_Header.next; // check to make sure the data makes sense From e7ffba648fd9d10274eef130b1f86ff56b90c801 Mon Sep 17 00:00:00 2001 From: Bradley Lowekamp Date: Wed, 19 Apr 2023 14:02:15 -0400 Subject: [PATCH 2/3] BUG: Fix MRC zero spacing for tilt series MRC image collected as a tilt series had spacing being computed a zero. This causes the image to be unreadable but ITK. Set the spacing to 1 in the case. --- Modules/IO/MRC/src/itkMRCImageIO.cxx | 18 ++++-------------- Modules/IO/MRC/test/CMakeLists.txt | 8 ++++++++ .../IO/MRC/test/Input/tilt_uint8.mrc.sha512 | 1 + 3 files changed, 13 insertions(+), 14 deletions(-) create mode 100644 Modules/IO/MRC/test/Input/tilt_uint8.mrc.sha512 diff --git a/Modules/IO/MRC/src/itkMRCImageIO.cxx b/Modules/IO/MRC/src/itkMRCImageIO.cxx index f879450d4ff..f866f37e9e5 100644 --- a/Modules/IO/MRC/src/itkMRCImageIO.cxx +++ b/Modules/IO/MRC/src/itkMRCImageIO.cxx @@ -200,20 +200,10 @@ MRCImageIO::ReadImageInformation() } } - if (header.xlen == 0.0f && header.ylen == 0.0f && header.zlen == 0.0f) - { - // if the spacing was not set in the header then this is the - // default - m_Spacing[0] = 1.0; - m_Spacing[1] = 1.0; - m_Spacing[2] = 1.0; - } - else - { - m_Spacing[0] = header.xlen / static_cast(header.mx); - m_Spacing[1] = header.ylen / static_cast(header.my); - m_Spacing[2] = header.zlen / static_cast(header.mz); - } + // if the spacing was not set in the header then 1.0 is the default + m_Spacing[0] = header.xlen != 0.0f ? header.xlen / static_cast(header.mx) : 1.0; + m_Spacing[1] = header.ylen != 0.0f ? header.ylen / static_cast(header.my) : 1.0; + m_Spacing[2] = header.zlen != 0.0f ? header.zlen / static_cast(header.mz) : 1.0; // copy the origin m_Origin[0] = header.xorg; diff --git a/Modules/IO/MRC/test/CMakeLists.txt b/Modules/IO/MRC/test/CMakeLists.txt index 8f6d85c74a4..cf7fef86b6e 100644 --- a/Modules/IO/MRC/test/CMakeLists.txt +++ b/Modules/IO/MRC/test/CMakeLists.txt @@ -34,3 +34,11 @@ itk_add_test(NAME itkMRCImageIOTest2c DATA{${ITK_DATA_ROOT}/Input/RGBTestImageLZW.tif} ${ITK_TEST_OUTPUT_DIR}/itkMRCImageIOTest2c.mrc 38a2dcfc08812e04b1528f15ca3d2ab7) +itk_add_test(NAME itkMRCImageIOTest2d + COMMAND ITKIOMRCTestDriver + --compare-MD5 ${ITK_TEST_OUTPUT_DIR}/itkMRCImageIOTest2d.mrc + 96093c8573ce2b4f197f0b0bfcc574bf + itkMRCImageIOTest2 + DATA{Input/tilt_uint8.mrc} + ${ITK_TEST_OUTPUT_DIR}/itkMRCImageIOTest2d.mrc + 96093c8573ce2b4f197f0b0bfcc574bf) diff --git a/Modules/IO/MRC/test/Input/tilt_uint8.mrc.sha512 b/Modules/IO/MRC/test/Input/tilt_uint8.mrc.sha512 new file mode 100644 index 00000000000..c165599ccd8 --- /dev/null +++ b/Modules/IO/MRC/test/Input/tilt_uint8.mrc.sha512 @@ -0,0 +1 @@ +332985f125b8584b9ba158faef6294cd2f2a9e8d9fb5b7f05a74b08089b2d4e13465c1c483e42b6c4263dac9af19542a8bd5ff33dc627507ba60c976a3cb5b0a From 8ca5d1f676d9632ae286b43d9b0c5d45592ff385 Mon Sep 17 00:00:00 2001 From: Bradley Lowekamp Date: Wed, 19 Apr 2023 14:23:23 -0400 Subject: [PATCH 3/3] BUG: MRC write full machine stamp The MRC2014 specification updated/clarifed the spec that the first two bytes should contain the specified value to determine endian. This appears to be enforced with Python mrcfile. This patch enables compatibility. --- Modules/IO/MRC/include/itkMRCHeaderObject.h | 4 ++-- Modules/IO/MRC/src/itkMRCImageIO.cxx | 2 ++ Modules/IO/MRC/test/itkMRCImageIOTest2.cxx | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Modules/IO/MRC/include/itkMRCHeaderObject.h b/Modules/IO/MRC/include/itkMRCHeaderObject.h index 6b95976ce7d..23cd3357486 100644 --- a/Modules/IO/MRC/include/itkMRCHeaderObject.h +++ b/Modules/IO/MRC/include/itkMRCHeaderObject.h @@ -164,8 +164,8 @@ class ITKIOMRC_EXPORT MRCHeaderObject : public LightObject float zorg; char cmap[4]; /**< Contains "MAP " */ - char stamp[4]; /**< First byte has 17 for big- or 68 for - little-endian */ + char stamp[4]; /**< First two bytes have 17 and 17 for big-endian or + 68 and 68 for little-endian */ float rms; // ALL HEADERS: diff --git a/Modules/IO/MRC/src/itkMRCImageIO.cxx b/Modules/IO/MRC/src/itkMRCImageIO.cxx index f866f37e9e5..5293963d67a 100644 --- a/Modules/IO/MRC/src/itkMRCImageIO.cxx +++ b/Modules/IO/MRC/src/itkMRCImageIO.cxx @@ -358,10 +358,12 @@ MRCImageIO::UpdateHeaderFromImageIO() if (ByteSwapper::SystemIsBigEndian()) { header.stamp[0] = 17; + header.stamp[1] = 17; } else { header.stamp[0] = 68; + header.stamp[1] = 68; } header.alpha = 90; diff --git a/Modules/IO/MRC/test/itkMRCImageIOTest2.cxx b/Modules/IO/MRC/test/itkMRCImageIOTest2.cxx index 2a16dbee158..444d8c4f546 100644 --- a/Modules/IO/MRC/test/itkMRCImageIOTest2.cxx +++ b/Modules/IO/MRC/test/itkMRCImageIOTest2.cxx @@ -24,6 +24,9 @@ #include "itkMetaDataObject.h" #include "itkTestingHashImageFilter.h" + +static_assert(sizeof(itk::MRCHeaderObject::Header) == 1024, " Ill defined MRC Header struct"); + namespace {