diff --git a/docs/Coding_Knowledge_Base.md b/docs/Coding_Knowledge_Base.md index e58bc753e2..92b7f9dc07 100644 --- a/docs/Coding_Knowledge_Base.md +++ b/docs/Coding_Knowledge_Base.md @@ -1101,8 +1101,8 @@ chunks, of the same store can still race. **Example of the broken rule:** `ComputeAvgOrientations::computeVmfWatsonAverage()` ran workers that wrote the per-feature output stores without a gate (PR #1645 made it serial). `ComputeFeatureFaceMisorientation` was fixed in -PR #1618. `CombineStlFiles` (issue #1876), `ConvertOrientations`, `PointSampleEdgeGeometry` and `FeatureFaceCurvature` -still launch workers that write shared stores without a gate. +PR #1618, and `FeatureFaceCurvature` in the change that fixed issue #1908 items L73 to L79. `CombineStlFiles` (issue +#1876), `ConvertOrientations` and `PointSampleEdgeGeometry` still launch workers that write shared stores without a gate. **Why:** out-of-core stores do not support concurrent writes, and a race between two writers can lose values without an error. Distinct tuple ranges do not make the writes safe. diff --git a/src/Plugins/NXImageProcessing/docs/BlackTopHatImageFilter.md b/src/Plugins/NXImageProcessing/docs/BlackTopHatImageFilter.md index 5554546644..1495fc2ac6 100644 --- a/src/Plugins/NXImageProcessing/docs/BlackTopHatImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/BlackTopHatImageFilter.md @@ -25,6 +25,8 @@ The *Kernel Type* parameter selects the structuring element used for the morphol **Note on a zero radius component (e.g. `{2, 1, 0}`) with a Box kernel on a 3D image:** This filter computes the correct grayscale morphology (a radius of 0 on an axis simply means the kernel has extent 1 on that axis). The legacy ITK filter's decomposable-Box (van Herk–Gil–Werman anchor) path produces an incorrect result for a zero-radius axis on a 3D image, reaching values outside the intended neighborhood. Results therefore differ from the legacy filter *by design* for this case; a radius of 0 on the Z axis of a genuinely 2D (single-slice) image is unaffected. +**Note on a 2D image (Z = 1):** The filter ignores the kernel offsets along Z. If the kernel has only Z offsets (for example an Annulus with radius `{0, 0, 2}`), the output is zero everywhere. + ### Safe Border When *Safe Border* is enabled (the default), the internal closing pads the image by the kernel radius before its dilation/erosion passes and crops back afterward, matching the legacy ITK filter and avoiding boundary artifacts near the image edge. When disabled, the closing runs directly on the image with out-of-bounds neighbors skipped (equivalent to ITK's `SafeBorder = false`); results are identical in the image interior and differ only within one kernel radius of the border. diff --git a/src/Plugins/NXImageProcessing/docs/ClosingByReconstructionImageFilter.md b/src/Plugins/NXImageProcessing/docs/ClosingByReconstructionImageFilter.md index 92160098bf..cee888ba19 100644 --- a/src/Plugins/NXImageProcessing/docs/ClosingByReconstructionImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/ClosingByReconstructionImageFilter.md @@ -20,6 +20,8 @@ The input array must be single-component (scalar); the output element type match The structuring element shape (Annulus, Ball, Box, or Cross) and its per-axis radius, used by the dilation. +On a 2D image (Z = 1), the filter ignores the kernel offsets along Z. If the kernel has only Z offsets (for example an Annulus with radius `{0, 0, 2}`), the output is a copy of the input, with **Preserve Intensities** on or off. + ### Fully Connected Selects the connectivity of the reconstruction: **Off** (default) uses face connectivity only; **On** uses face + edge + vertex connectivity. diff --git a/src/Plugins/NXImageProcessing/docs/GrayscaleDilateImageFilter.md b/src/Plugins/NXImageProcessing/docs/GrayscaleDilateImageFilter.md index c58a6d1fb6..9c196b9d14 100644 --- a/src/Plugins/NXImageProcessing/docs/GrayscaleDilateImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/GrayscaleDilateImageFilter.md @@ -25,6 +25,8 @@ The *Kernel Type* parameter selects the structuring element used for the morphol **Note on a zero radius component (e.g. `{2, 1, 0}`) with a Box kernel on a 3D image:** This filter computes the correct grayscale dilation (a radius of 0 on an axis simply means the kernel has extent 1 on that axis). The legacy ITK filter's decomposable-Box (van Herk–Gil–Werman anchor) path produces an incorrect result for a zero-radius axis on a 3D image, reaching values outside the intended neighborhood. Results therefore differ from the legacy filter *by design* for this case; a radius of 0 on the Z axis of a genuinely 2D (single-slice) image is unaffected. +**Note on a 2D image (Z = 1):** The filter ignores the kernel offsets along Z. If the kernel has only Z offsets (for example an Annulus with radius `{0, 0, 2}`), the output is a copy of the input. + ## Algorithm ### In-Core Path diff --git a/src/Plugins/NXImageProcessing/docs/GrayscaleErodeImageFilter.md b/src/Plugins/NXImageProcessing/docs/GrayscaleErodeImageFilter.md index 0273c557df..9a931823ef 100644 --- a/src/Plugins/NXImageProcessing/docs/GrayscaleErodeImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/GrayscaleErodeImageFilter.md @@ -25,6 +25,8 @@ The *Kernel Type* parameter selects the structuring element used for the morphol **Note on a zero radius component (e.g. `{2, 1, 0}`) with a Box kernel on a 3D image:** This filter computes the correct grayscale erosion (a radius of 0 on an axis simply means the kernel has extent 1 on that axis). The legacy ITK filter's decomposable-Box (van Herk–Gil–Werman anchor) path produces an incorrect result for a zero-radius axis on a 3D image, reaching values outside the intended neighborhood. Results therefore differ from the legacy filter *by design* for this case; a radius of 0 on the Z axis of a genuinely 2D (single-slice) image is unaffected. +**Note on a 2D image (Z = 1):** The filter ignores the kernel offsets along Z. If the kernel has only Z offsets (for example an Annulus with radius `{0, 0, 2}`), the output is a copy of the input. + ## Algorithm ### In-Core Path diff --git a/src/Plugins/NXImageProcessing/docs/GrayscaleMorphologicalClosingImageFilter.md b/src/Plugins/NXImageProcessing/docs/GrayscaleMorphologicalClosingImageFilter.md index 3cb0060253..cf0e1af404 100644 --- a/src/Plugins/NXImageProcessing/docs/GrayscaleMorphologicalClosingImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/GrayscaleMorphologicalClosingImageFilter.md @@ -25,6 +25,8 @@ The *Kernel Type* parameter selects the structuring element used for the morphol **Note on a zero radius component (e.g. `{2, 1, 0}`) with a Box kernel on a 3D image:** This filter computes the correct grayscale morphology (a radius of 0 on an axis simply means the kernel has extent 1 on that axis). The legacy ITK filter's decomposable-Box (van Herk–Gil–Werman anchor) path produces an incorrect result for a zero-radius axis on a 3D image, reaching values outside the intended neighborhood. Results therefore differ from the legacy filter *by design* for this case; a radius of 0 on the Z axis of a genuinely 2D (single-slice) image is unaffected. +**Note on a 2D image (Z = 1):** The filter ignores the kernel offsets along Z. If the kernel has only Z offsets (for example an Annulus with radius `{0, 0, 2}`), the output is a copy of the input. + ### Safe Border When *Safe Border* is enabled (the default), the image is padded by the kernel radius before the dilation/erosion passes and cropped back afterward, matching the legacy ITK filter and avoiding boundary artifacts near the image edge. When disabled, the two passes run directly on the image with out-of-bounds neighbors skipped (equivalent to ITK's `SafeBorder = false`); results are identical in the image interior and differ only within one kernel radius of the border. diff --git a/src/Plugins/NXImageProcessing/docs/GrayscaleMorphologicalOpeningImageFilter.md b/src/Plugins/NXImageProcessing/docs/GrayscaleMorphologicalOpeningImageFilter.md index 5c648a1163..6955024026 100644 --- a/src/Plugins/NXImageProcessing/docs/GrayscaleMorphologicalOpeningImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/GrayscaleMorphologicalOpeningImageFilter.md @@ -25,6 +25,8 @@ The *Kernel Type* parameter selects the structuring element used for the morphol **Note on a zero radius component (e.g. `{2, 1, 0}`) with a Box kernel on a 3D image:** This filter computes the correct grayscale morphology (a radius of 0 on an axis simply means the kernel has extent 1 on that axis). The legacy ITK filter's decomposable-Box (van Herk–Gil–Werman anchor) path produces an incorrect result for a zero-radius axis on a 3D image, reaching values outside the intended neighborhood. Results therefore differ from the legacy filter *by design* for this case; a radius of 0 on the Z axis of a genuinely 2D (single-slice) image is unaffected. +**Note on a 2D image (Z = 1):** The filter ignores the kernel offsets along Z. If the kernel has only Z offsets (for example an Annulus with radius `{0, 0, 2}`), the output is a copy of the input. + ### Safe Border When *Safe Border* is enabled (the default), the image is padded by the kernel radius before the erosion/dilation passes and cropped back afterward, matching the legacy ITK filter and avoiding boundary artifacts near the image edge. When disabled, the two passes run directly on the image with out-of-bounds neighbors skipped (equivalent to ITK's `SafeBorder = false`); results are identical in the image interior and differ only within one kernel radius of the border. diff --git a/src/Plugins/NXImageProcessing/docs/MorphologicalGradientImageFilter.md b/src/Plugins/NXImageProcessing/docs/MorphologicalGradientImageFilter.md index b38ccd5567..b326eeeea8 100644 --- a/src/Plugins/NXImageProcessing/docs/MorphologicalGradientImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/MorphologicalGradientImageFilter.md @@ -25,6 +25,8 @@ The *Kernel Type* parameter selects the structuring element used for the morphol **Note on a zero radius component (e.g. `{2, 1, 0}`) with a Box kernel on a 3D image:** This filter computes the correct grayscale morphology (a radius of 0 on an axis simply means the kernel has extent 1 on that axis). The legacy ITK filter's decomposable-Box (van Herk–Gil–Werman anchor) path produces an incorrect result for a zero-radius axis on a 3D image, reaching values outside the intended neighborhood. Results therefore differ from the legacy filter *by design* for this case; a radius of 0 on the Z axis of a genuinely 2D (single-slice) image is unaffected. +**Note on a 2D image (Z = 1):** The filter ignores the kernel offsets along Z. If the kernel has only Z offsets (for example an Annulus with radius `{0, 0, 2}`), the output is zero everywhere. + ## Algorithm ### In-Core Path diff --git a/src/Plugins/NXImageProcessing/docs/OpeningByReconstructionImageFilter.md b/src/Plugins/NXImageProcessing/docs/OpeningByReconstructionImageFilter.md index dc8497ca90..5040dad76b 100644 --- a/src/Plugins/NXImageProcessing/docs/OpeningByReconstructionImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/OpeningByReconstructionImageFilter.md @@ -20,6 +20,8 @@ The input array must be single-component (scalar); the output element type match The structuring element shape (Annulus, Ball, Box, or Cross) and its per-axis radius, used by the erosion. +On a 2D image (Z = 1), the filter ignores the kernel offsets along Z. If the kernel has only Z offsets (for example an Annulus with radius `{0, 0, 2}`), the output is a copy of the input, with **Preserve Intensities** on or off. + ### Fully Connected Selects the connectivity of the reconstruction: **Off** (default) uses face connectivity only; **On** uses face + edge + vertex connectivity. diff --git a/src/Plugins/NXImageProcessing/docs/ReadImageFilter.md b/src/Plugins/NXImageProcessing/docs/ReadImageFilter.md index 7ec4d8d756..40aee48bac 100644 --- a/src/Plugins/NXImageProcessing/docs/ReadImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/ReadImageFilter.md @@ -8,6 +8,8 @@ IO (Input) Reads an image file into a newly created **Image Geometry**. This filter is dimension-aware: a single-page raster file is read as a 2D image (`Z = 1`), a multi-page TIFF is read as a 3D Z-stack (one page per Z slice), and a NRRD file is read as its native 2D or 3D volume. This filter does not depend on ITK. It uses stb_image for PNG/JPEG/BMP files, libtiff for TIFF files, and an ITK-free (zlib-only) reader for NRRD files. +In a multi-page TIFF, the first page (page 0) sets the component count and the stored data type. The output array has this data type, unless **Set Image Data Type** is on: then the filter converts the values to the **Output Data Type**. Each page that the filter reads must have the same component count, stored data type, width and height as page 0. Otherwise, the filter returns an error. The filter reads only the pages inside the Z crop. When it reads a single page (as Read Image Stack does), it reads only page 0. It does not check the pages that it does not read. + The following image types are supported: - PNG (via stb) @@ -16,9 +18,7 @@ The following image types are supported: - TIFF / TIF, including multi-page TIFF read as a 3D Z-stack (via libtiff) - NRRD (`.nrrd` attached, `.nhdr` detached) (ITK-free, zlib only) -All pages of a multi-page TIFF must have the same dimensions, component count, and scalar type. The filter compares the number of bytes per pixel and the dimensions of each page it reads with page 0. It does not compare component counts or scalar types. A change in component count or scalar type can go undetected when the number of bytes per pixel stays the same. This can cause incorrect pixel values. - -If the first page is 8-bit RGB and a later page is 8-bit grayscale, the filter fails with error `-2007` when it reads that later page. The number of bytes per pixel differs (3 and 1), so it fails before it copies any pixels of that page. This intentionally differs from DREAM3D 6.5.171, which could crash on mixed-page TIFF files (KI-053). +All pages of a multi-page TIFF that the filter reads must have the same dimensions, component count, and scalar type. Before it reads any pixels, the filter compares the component count and stored data type of each of these pages with page 0. A difference is error `-50579`, for example when page 0 is 8-bit RGB and a later page is 8-bit grayscale. While it reads, the filter compares the dimensions of each page with page 0. A difference is error `-2007`. This intentionally differs from DREAM3D 6.5.171, which could crash on mixed-page TIFF files (KI-053). ### NRRD Volumes diff --git a/src/Plugins/NXImageProcessing/docs/WhiteTopHatImageFilter.md b/src/Plugins/NXImageProcessing/docs/WhiteTopHatImageFilter.md index 491590338f..23e2197334 100644 --- a/src/Plugins/NXImageProcessing/docs/WhiteTopHatImageFilter.md +++ b/src/Plugins/NXImageProcessing/docs/WhiteTopHatImageFilter.md @@ -25,6 +25,8 @@ The *Kernel Type* parameter selects the structuring element used for the morphol **Note on a zero radius component (e.g. `{2, 1, 0}`) with a Box kernel on a 3D image:** This filter computes the correct grayscale morphology (a radius of 0 on an axis simply means the kernel has extent 1 on that axis). The legacy ITK filter's decomposable-Box (van Herk–Gil–Werman anchor) path produces an incorrect result for a zero-radius axis on a 3D image, reaching values outside the intended neighborhood. Results therefore differ from the legacy filter *by design* for this case; a radius of 0 on the Z axis of a genuinely 2D (single-slice) image is unaffected. +**Note on a 2D image (Z = 1):** The filter ignores the kernel offsets along Z. If the kernel has only Z offsets (for example an Annulus with radius `{0, 0, 2}`), the output is zero everywhere. + ### Safe Border When *Safe Border* is enabled (the default), the internal opening pads the image by the kernel radius before its erosion/dilation passes and crops back afterward, matching the legacy ITK filter and avoiding boundary artifacts near the image edge. When disabled, the opening runs directly on the image with out-of-bounds neighbors skipped (equivalent to ITK's `SafeBorder = false`); results are identical in the image interior and differ only within one kernel radius of the border. diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/ReadImage.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/ReadImage.cpp index 28232b156f..755e6f8c0a 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/ReadImage.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/ReadImage.cpp @@ -30,6 +30,22 @@ namespace fs = std::filesystem; namespace { +std::string DescribePageLayout(const IImageIO::PageMetadata& layout) +{ + switch(layout.sampleFormat) + { + case IImageIO::SampleFormat::Unsigned: + return fmt::format("{}-component uint{}", layout.numComponents, layout.bitsPerSample); + case IImageIO::SampleFormat::Signed: + return fmt::format("{}-component int{}", layout.numComponents, layout.bitsPerSample); + case IImageIO::SampleFormat::Float: + return fmt::format("{}-component float{}", layout.numComponents, layout.bitsPerSample); + case IImageIO::SampleFormat::Unknown: + break; + } + return fmt::format("{}-component {}-bit samples of an unsupported type", layout.numComponents, layout.bitsPerSample); +} + /** * @brief Geometry of one raster read (2D raster or multi-page TIFF Z-stack). * @@ -297,6 +313,12 @@ Result<> ReadRasterBackend(DataStructure& dataStructure, const ReadImageInputVal return MakeErrorResult(-2001, fmt::format("Crop window (start=[{},{},{}], size=[{},{},{}]) does not fit within the source image (size=[{},{},{}])", stream.xStart, stream.yStart, stream.zStart, stream.dstWidth, stream.dstHeight, stream.dstDepth, stream.srcWidth, stream.srcHeight, metadata.numPages)); } + // Only the decoded pages are validated. Page 0 sets the output layout, so each decoded page is compared with it. + auto layoutResult = ReadImage::ValidatePageLayouts(*imageIO, inputFilePath, stream.singlePageOnly ? usize{0} : stream.zStart, stream.dstDepth); + if(layoutResult.invalid()) + { + return layoutResult; + } const DataType destType = imageArray.getDataType(); const DataType srcType = metadata.dataType; @@ -468,3 +490,36 @@ Result<> ReadImage::operator()() return ReadRasterBackend(m_DataStructure, m_InputValues, m_MessageHandler, m_ShouldCancel); } + +// ----------------------------------------------------------------------------- +Result<> ReadImage::ValidatePageLayouts(const IImageIO& imageIO, const std::filesystem::path& filePath, usize firstPage, usize pageCount) +{ + // Page 0 sets the output layout. A read of page 0 alone has nothing to compare. + if(pageCount == 0 || (firstPage == 0 && pageCount == 1)) + { + return {}; + } + auto result = imageIO.readPageLayouts(filePath); + if(result.invalid()) + { + return ConvertResult(std::move(result)); + } + const auto& layouts = result.value(); + if(layouts.empty()) + { + return {}; + } + const auto& first = layouts.front(); + const usize endPage = std::min(layouts.size(), firstPage + pageCount); + for(usize page = std::max(firstPage, usize{1}); page < endPage; ++page) + { + const auto& current = layouts[page]; + if(current.numComponents != first.numComponents || current.bitsPerSample != first.bitsPerSample || current.sampleFormat != first.sampleFormat) + { + return MakeErrorResult( + -50579, fmt::format("TIFF '{}': page {} is {}, page 0 is {}. Each page that is read must have the same component count and data type as page 0. Use a TIFF with matching page layouts.", + filePath.string(), page, DescribePageLayout(current), DescribePageLayout(first))); + } + } + return {}; +} diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/ReadImage.hpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/ReadImage.hpp index 41b535000d..bd61b98e7f 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/ReadImage.hpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/Algorithms/ReadImage.hpp @@ -15,6 +15,7 @@ namespace nx::core { +class IImageIO; /** * @struct ReadImageInputValues @@ -77,6 +78,20 @@ class NXIMAGEPROCESSING_EXPORT ReadImage */ Result<> operator()(); + /** + * @brief Checks that each page the read decodes has the component count and sample type of page 0. + * + * Page 0 sets the output array layout, so it is the reference even when it is outside the read. + * Pages outside [firstPage, firstPage + pageCount) are not checked. A read of page 0 alone returns + * success. Page dimensions are not compared here; they are checked when each page is decoded. + * @param imageIO Image backend that reads the page layouts. + * @param filePath Image file to check. + * @param firstPage First decoded page (the Z crop start, or 0). + * @param pageCount Number of decoded pages (1 for a single-page read). + * @return Success, the backend's metadata error, or error -50579 for the first decoded page that differs. + */ + static Result<> ValidatePageLayouts(const IImageIO& imageIO, const std::filesystem::path& filePath, usize firstPage, usize pageCount); + private: DataStructure& m_DataStructure; const ReadImageInputValues& m_InputValues; diff --git a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ReadImageFilter.cpp b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ReadImageFilter.cpp index e60ba0b193..4bdc715543 100644 --- a/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ReadImageFilter.cpp +++ b/src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ReadImageFilter.cpp @@ -340,6 +340,8 @@ IFilter::PreflightResult ReadImageFilter::preflightImpl(const DataStructure& dat bool cropImage = croppingOptions.type != CropGeometryParameter::CropValues::TypeEnum::NoCropping; const bool zCropActive = cropImage && croppingOptions.cropZ && dims[2] > 1; bool cropAnyDim = cropImage && (croppingOptions.cropX || croppingOptions.cropY || zCropActive); + // First source page that the read decodes. A Z crop moves it; otherwise the read starts at page 0. + usize firstPage = 0; if(cropAnyDim) { DataStructure tmpDs; @@ -371,6 +373,17 @@ IFilter::PreflightResult ReadImageFilter::preflightImpl(const DataStructure& dat dims = croppedGeom.getDimensions().toContainer>(); spacing = croppedGeom.getSpacing().toContainer>(); origin = croppedGeom.getOrigin().toContainer>(); + if(zCropActive) + { + firstPage = static_cast(cropBounds.zMin); + } + } + + // Only the decoded pages are validated. Page 0 sets the output layout, so each decoded page is compared with it. + auto layoutResult = ReadImage::ValidatePageLayouts(*imageIO, fileName, firstPage, dims[2]); + if(layoutResult.invalid()) + { + return {ConvertResultTo(std::move(layoutResult), {})}; } if(originSpacingProcessing == OriginSpacingProcessing::Postprocessed) diff --git a/src/Plugins/NXImageProcessing/test/ClosingByReconstructionImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/ClosingByReconstructionImageFilterTest.cpp index b10d851a73..aaaa491597 100644 --- a/src/Plugins/NXImageProcessing/test/ClosingByReconstructionImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ClosingByReconstructionImageFilterTest.cpp @@ -163,3 +163,19 @@ TEST_CASE("NXImageProcessing::ClosingByReconstructionImageFilter: Ordinary Kerne { morph_test::RequireOrdinaryKernelRadius({5, 5, 5}); } + +// ----------------------------------------------------------------------------- +// 2D image (Z = 1) with an Annulus that has only Z offsets: the kernel is empty in the plane, so the filter copies +// the input with Preserve Intensities on or off (#1908 L73). +// ----------------------------------------------------------------------------- +TEST_CASE("NXImageProcessing::ClosingByReconstructionImageFilter: 2D out-of-plane Annulus passthrough", "[NXImageProcessing][ClosingByReconstructionImageFilter]") +{ + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope scope(scenario); + const uint32 radiusZ = GENERATE(1, 2); + const bool preserveIntensities = GENERATE(false, true); + const bool fullyConnected = GENERATE(false, true); + CAPTURE(radiusZ, preserveIntensities, fullyConnected); + rt::RequireZOnlyAnnulus2DPassthrough(scope, radiusZ, preserveIntensities, fullyConnected); +} diff --git a/src/Plugins/NXImageProcessing/test/MorphologyEngineTest.cpp b/src/Plugins/NXImageProcessing/test/MorphologyEngineTest.cpp index 3071037fea..33a66cecc7 100644 --- a/src/Plugins/NXImageProcessing/test/MorphologyEngineTest.cpp +++ b/src/Plugins/NXImageProcessing/test/MorphologyEngineTest.cpp @@ -2599,3 +2599,85 @@ TEST_CASE("NXImageProcessing::MorphologyEngine: Kernel Radius layout before allo UnitTest::CheckArraysInheritTupleDims(ds); } } + +TEMPLATE_TEST_CASE("NXImageProcessing::Grayscale morphology: 2D out-of-plane Annulus passthrough", "[NXImageProcessing][L73]", GrayscaleDilateImageFilter, GrayscaleErodeImageFilter, + GrayscaleMorphologicalOpeningImageFilter, GrayscaleMorphologicalClosingImageFilter, MorphologicalGradientImageFilter, WhiteTopHatImageFilter, BlackTopHatImageFilter) +{ + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope scope(scenario); + const uint32 radiusZ = GENERATE(0, 1, 2); + CAPTURE(radiusZ); + DataStructure ds; + const DataPath inputPath = morph_test::BuildMorphologyImage(ds, 7, 3, 1); + TestType filter; + Arguments args; + args.insertOrAssign(TestType::k_InputImageGeomPath_Key, std::make_any(inputPath.getParent().getParent())); + args.insertOrAssign(TestType::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(TestType::k_OutputImageArrayName_Key, std::make_any("Output")); + morph_test::DefaultKernelParamSetter()(args, static_cast(morph_test::KernelType::Annulus), {0, 0, radiusZ}); + if constexpr(std::is_same_v || std::is_same_v || + std::is_same_v || std::is_same_v) + { + args.insertOrAssign(TestType::k_SafeBorder_Key, std::make_any(GENERATE(false, true))); + } + auto preflight = filter.preflight(ds, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); + auto result = scope.executeFilter(filter, ds, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + args.insertOrAssign(TestType::k_OutputImageArrayName_Key, std::make_any("Empty")); + morph_test::DefaultKernelParamSetter()(args, static_cast(morph_test::KernelType::Annulus), {0, 0, 0}); + auto emptyResult = scope.executeFilter(filter, ds, args); + SIMPLNX_RESULT_REQUIRE_VALID(emptyResult.result); + const UInt8Array* empty = nullptr; + REQUIRE_NOTHROW(empty = &ds.getDataRefAs(inputPath.replaceName("Empty"))); + const UInt8Array* input = nullptr; + const UInt8Array* output = nullptr; + REQUIRE_NOTHROW(input = &ds.getDataRefAs(inputPath)); + REQUIRE_NOTHROW(output = &ds.getDataRefAs(inputPath.replaceName("Output"))); + for(usize i = 0; i < 21; ++i) + { + CAPTURE(i); + REQUIRE((*output)[i] == (*empty)[i]); + if constexpr(std::is_same_v || std::is_same_v || std::is_same_v) + { + REQUIRE((*output)[i] == 0); + } + else + { + REQUIRE((*output)[i] == (*input)[i]); + } + } + UnitTest::CheckArraysInheritTupleDims(ds); +} + +TEST_CASE("NXImageProcessing::MorphologyEngine: 2D out-of-plane Annulus composite pipeline", "[NXImageProcessing][MorphologyEngine][L73]") +{ + const bool safeBorder = GENERATE(false, true); + const auto firstOp = GENERATE(MorphOp::Erode, MorphOp::Dilate); + const auto outputMode = GENERATE(ImageProcessing::detail::MorphologyCompositeOutputMode::Morphology, ImageProcessing::detail::MorphologyCompositeOutputMode::OriginalMinusMorphology, + ImageProcessing::detail::MorphologyCompositeOutputMode::MorphologyMinusOriginal); + const uint32 radiusZ = GENERATE(0, 1, 2); + CAPTURE(safeBorder, firstOp, outputMode, radiusZ); + const SizeVec3 dims{7, 3, 1}; + std::vector values(21); + for(usize i = 0; i < values.size(); ++i) + { + values[i] = static_cast(3 + (i * 17) % 251); + } + auto input = MakeStore(values, 7, 3, 1); + auto output = MakeStore(std::vector(21, 99), 7, 3, 1); + const auto se = MakeStructuringElement(KernelType::Annulus, {0, 0, static_cast(radiusZ)}); + auto bytes = CalculateMorphologyCompositePipelineWorkingMemoryBytes(dims, se, safeBorder); + SIMPLNX_RESULT_REQUIRE_VALID(bytes); + const std::atomic_bool shouldCancel = false; + auto result = + ApplyMorphologyCompositePipeline(input, output, dims, se, firstOp, firstOp == MorphOp::Erode ? MorphOp::Dilate : MorphOp::Erode, safeBorder, outputMode, shouldCancel, bytes.value()); + SIMPLNX_RESULT_REQUIRE_VALID(result); + for(usize i = 0; i < values.size(); ++i) + { + CAPTURE(i); + const uint8 expected = outputMode == ImageProcessing::detail::MorphologyCompositeOutputMode::Morphology ? values[i] : 0; + REQUIRE(output.getValue(i) == expected); + } +} diff --git a/src/Plugins/NXImageProcessing/test/OpeningByReconstructionImageFilterTest.cpp b/src/Plugins/NXImageProcessing/test/OpeningByReconstructionImageFilterTest.cpp index 55bee3f3a4..215aeae595 100644 --- a/src/Plugins/NXImageProcessing/test/OpeningByReconstructionImageFilterTest.cpp +++ b/src/Plugins/NXImageProcessing/test/OpeningByReconstructionImageFilterTest.cpp @@ -165,3 +165,19 @@ TEST_CASE("NXImageProcessing::OpeningByReconstructionImageFilter: Ordinary Kerne { morph_test::RequireOrdinaryKernelRadius({0, 0, 0}); } + +// ----------------------------------------------------------------------------- +// 2D image (Z = 1) with an Annulus that has only Z offsets: the kernel is empty in the plane, so the filter copies +// the input with Preserve Intensities on or off (#1908 L73). +// ----------------------------------------------------------------------------- +TEST_CASE("NXImageProcessing::OpeningByReconstructionImageFilter: 2D out-of-plane Annulus passthrough", "[NXImageProcessing][OpeningByReconstructionImageFilter]") +{ + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope scope(scenario); + const uint32 radiusZ = GENERATE(1, 2); + const bool preserveIntensities = GENERATE(false, true); + const bool fullyConnected = GENERATE(false, true); + CAPTURE(radiusZ, preserveIntensities, fullyConnected); + rt::RequireZOnlyAnnulus2DPassthrough(scope, radiusZ, preserveIntensities, fullyConnected); +} diff --git a/src/Plugins/NXImageProcessing/test/ReadImageTest.cpp b/src/Plugins/NXImageProcessing/test/ReadImageTest.cpp index c52ea28ed9..301fcdf2c4 100644 --- a/src/Plugins/NXImageProcessing/test/ReadImageTest.cpp +++ b/src/Plugins/NXImageProcessing/test/ReadImageTest.cpp @@ -1,12 +1,15 @@ #include +#include "NXImageProcessing/Filters/Algorithms/ReadImage.hpp" #include "NXImageProcessing/Filters/ReadImageFilter.hpp" #include "NXImageProcessing/NXImageProcessing_test_dirs.hpp" #include "NXImageProcessing/utils/NrrdUtilities.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/Common/Uuid.hpp" #include "simplnx/Core/Application.hpp" #include "simplnx/Core/Preferences.hpp" +#include "simplnx/DataStructure/AttributeMatrix.hpp" #include "simplnx/DataStructure/DataArray.hpp" #include "simplnx/DataStructure/Geometry/ImageGeom.hpp" #include "simplnx/DataStructure/IDataArray.hpp" @@ -21,6 +24,7 @@ #include #include +#include #include #include #include @@ -780,34 +784,16 @@ TEST_CASE("NXImageProcessing::ReadImageFilter: MultiPage_TIFF_RGB_Then_Grayscale args.insertOrAssign(ReadImageFilter::k_ImageDataType_Key, ChoicesParameter::ValueType{1}); ReadImageFilter filter; DataStructure dataStructure; + // Page 1 has 1 component and page 0 has 3, so the page-layout check rejects the file + // before any output exists. Execute repeats the check, so no pixel is ever copied. auto preflight = filter.preflight(dataStructure, args); - SIMPLNX_RESULT_REQUIRE_VALID(preflight.outputActions); + SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); + REQUIRE(preflight.outputActions.errors()[0].code == -50579); IFilter::ExecuteResult execute; REQUIRE_NOTHROW(execute = filter.execute(dataStructure, args)); SIMPLNX_RESULT_REQUIRE_INVALID(execute.result); - REQUIRE(execute.result.errors()[0].code == -2007); // The page-layout check must reject page 1, not another error path. - REQUIRE(dataStructure.getDataRefAs(geomPath).getDimensions() == SizeVec3{width, height, 2}); - - // Oracle: page 0 has 3*2*3 = 18 white components (255, or 65535 after conversion). - // Page 1 supplies only 3 bytes per row, but the RGB destination needs 3*3 = 9. - // Reject that segment before copying any bytes; its 18 destination components must - // retain their initial zero values. This detects a late error even without a sanitizer. - const auto checkPixels = [](const auto& pixels, auto white) { - REQUIRE(pixels.getSize() == 36); - for(usize i = 0; i < 18; ++i) - { - REQUIRE(pixels[i] == white); - REQUIRE(pixels[18 + i] == 0); - } - }; - if(convertData) - { - checkPixels(dataStructure.getDataRefAs(arrayPath), uint16_t{65535}); - } - else - { - checkPixels(dataStructure.getDataRefAs(arrayPath), uint8_t{255}); - } + REQUIRE(execute.result.errors()[0].code == -50579); + CHECK(dataStructure.getData(arrayPath) == nullptr); } TEST_CASE("NXImageProcessing::ReadImageFilter: Nrrd_Scalar3D", "[NXImageProcessing][ReadImageFilter]") @@ -1609,3 +1595,296 @@ TEST_CASE("NXImageProcessing::ReadImageFilter: Raster_PhysicalCrop_MaxAtBound", } UnitTest::CheckArraysInheritTupleDims(dataStructure); } + +namespace +{ +template +void WriteLayoutPage(TIFF* tiff, nx::core::uint16 components, nx::core::uint16 format, const std::vector& pixels) +{ + REQUIRE(pixels.size() == usize{4} * components); + REQUIRE(TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, 2) == 1); + REQUIRE(TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, 2) == 1); + REQUIRE(TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, components) == 1); + REQUIRE(TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, static_cast(sizeof(T) * 8)) == 1); + if(format != 0) // Exercise TIFF's default unsigned sample format too. + { + REQUIRE(TIFFSetField(tiff, TIFFTAG_SAMPLEFORMAT, format) == 1); + } + REQUIRE(TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG) == 1); + REQUIRE(TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, components == 4 ? PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK) == 1); + if(components == 4) + { + nx::core::uint16 extra = EXTRASAMPLE_UNASSALPHA; + REQUIRE(TIFFSetField(tiff, TIFFTAG_EXTRASAMPLES, 1, &extra) == 1); + } + for(nx::core::uint32 row = 0; row < 2; ++row) + { + REQUIRE(TIFFWriteScanline(tiff, const_cast(pixels.data() + row * 2 * components), row, 0) == 1); + } + REQUIRE(TIFFWriteDirectory(tiff) == 1); +} + +// A TIFF path that is unique to one test run and is removed when the test ends, also when an assertion fails. +class ScopedLayoutTiffPath +{ +public: + explicit ScopedLayoutTiffPath(const std::string& stem) + : m_Path(ReadImageOutputDir() / fmt::format("{}_{}.tif", stem, Uuid::GenerateV4().str())) + { + } + + ~ScopedLayoutTiffPath() noexcept + { + std::error_code errorCode; + fs::remove(m_Path, errorCode); + } + + ScopedLayoutTiffPath(const ScopedLayoutTiffPath&) = delete; + ScopedLayoutTiffPath(ScopedLayoutTiffPath&&) = delete; + ScopedLayoutTiffPath& operator=(const ScopedLayoutTiffPath&) = delete; + ScopedLayoutTiffPath& operator=(ScopedLayoutTiffPath&&) = delete; + + const fs::path& path() const + { + return m_Path; + } + +private: + fs::path m_Path; +}; +} // namespace + +TEST_CASE("NXImageProcessing::ReadImageFilter: TIFF_PageLayout", "[NXImageProcessing][ReadImageFilter]") +{ + const ScopedLayoutTiffPath tiffPath("page_layout"); + const fs::path& path = tiffPath.path(); + bool mismatch = true; + { + std::unique_ptr tiff(TIFFOpen(path.string().c_str(), "w"), TIFFClose); + REQUIRE(tiff != nullptr); + SECTION("RGBA uint8 versus grayscale float32") + { + WriteLayoutPage(tiff.get(), 4, SAMPLEFORMAT_UINT, {1, 2, 3, 255, 4, 5, 6, 255, 7, 8, 9, 255, 10, 11, 12, 255}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_IEEEFP, {1, 2, 3, 4}); + } + SECTION("uint16 versus int16") + { + WriteLayoutPage(tiff.get(), 1, 0, {1, 2, 3, 4}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_INT, {-1, -2, -3, -4}); + } + SECTION("identical uint16 layouts") + { + mismatch = false; + WriteLayoutPage(tiff.get(), 1, 0, {1, 2, 3, 4}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_UINT, {11, 12, 13, 14}); + } + } + DataStructure ds; + ReadImageFilter filter; + const DataPath geomPath({"Layout"}); + const auto args = MakeReadImageNrrdArgs(path, geomPath, "Cell Data", "Pixels"); + if(mismatch) + { + const auto preflight = filter.preflight(ds, args); + SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); + const auto& error = preflight.outputActions.errors().front(); + REQUIRE(error.code == -50579); + REQUIRE(error.message.find(path.string()) != std::string::npos); + REQUIRE(error.message.find("page 1 is") != std::string::npos); + REQUIRE(error.message.find("page 0 is") != std::string::npos); + REQUIRE(ds.getDataAs(geomPath) == nullptr); // Preflight rejects before output creation. + } + else + { + const auto result = filter.execute(ds, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + const auto arrayPath = geomPath.createChildPath("Cell Data").createChildPath("Pixels"); + REQUIRE_NOTHROW(ds.getDataRefAs(arrayPath)); + const auto& array = ds.getDataRefAs(arrayPath); + const std::array expected = {1, 2, 3, 4, 11, 12, 13, 14}; + REQUIRE(array.getSize() == expected.size()); + for(usize i = 0; i < expected.size(); ++i) + { + REQUIRE(array[i] == expected[i]); + } + } + UnitTest::CheckArraysInheritTupleDims(ds); +} + +TEST_CASE("NXImageProcessing::ReadImageFilter: TIFF_PageLayout_SinglePageOnly", "[NXImageProcessing][ReadImageFilter]") +{ + // Read Image Stack reads only page 0 of each file. Pages it never reads must not be validated. + const ScopedLayoutTiffPath tiffPath("page_layout_single_page"); + const fs::path& path = tiffPath.path(); + { + std::unique_ptr tiff(TIFFOpen(path.string().c_str(), "w"), TIFFClose); + REQUIRE(tiff != nullptr); + WriteLayoutPage(tiff.get(), 4, SAMPLEFORMAT_UINT, {1, 2, 3, 255, 4, 5, 6, 255, 7, 8, 9, 255, 10, 11, 12, 255}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_IEEEFP, {1, 2, 3, 4}); + } + DataStructure ds; + ReadImageFilter filter; + filter.setReadSinglePageOnly(true); + const DataPath geomPath({"Layout"}); + const auto args = MakeReadImageNrrdArgs(path, geomPath, "Cell Data", "Pixels"); + const auto result = filter.execute(ds, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + const auto arrayPath = geomPath.createChildPath("Cell Data").createChildPath("Pixels"); + REQUIRE_NOTHROW(ds.getDataRefAs(arrayPath)); + const auto& array = ds.getDataRefAs(arrayPath); + REQUIRE(array.getNumberOfComponents() == 4); + const std::array expected = {1, 2, 3, 255, 4, 5, 6, 255, 7, 8, 9, 255, 10, 11, 12, 255}; + REQUIRE(array.getSize() == expected.size()); + for(usize i = 0; i < expected.size(); ++i) + { + CAPTURE(i); + REQUIRE(array[i] == expected[i]); + } + const auto& warnings = result.result.warnings(); + REQUIRE(std::any_of(warnings.begin(), warnings.end(), [](const Warning& warning) { return warning.message.find("reading only the first page") != std::string::npos; })); + UnitTest::CheckArraysInheritTupleDims(ds); +} + +TEST_CASE("NXImageProcessing::ReadImageFilter: TIFF_PageLayout_ZCrop", "[NXImageProcessing][ReadImageFilter]") +{ + // A Z crop decodes only the pages inside the crop. Page 0 sets the output type and component count, + // so each decoded page must match page 0, and pages outside the crop are not checked. + const ScopedLayoutTiffPath tiffPath("page_layout_zcrop"); + const fs::path& path = tiffPath.path(); + CropGeometryParameter::ValueType crop; + crop.type = CropGeometryParameter::CropValues::TypeEnum::VoxelSubvolume; + crop.cropX = false; + crop.cropY = false; + crop.cropZ = true; + bool expectError = false; + std::vector expected; + { + std::unique_ptr tiff(TIFFOpen(path.string().c_str(), "w"), TIFFClose); + REQUIRE(tiff != nullptr); + SECTION("Different page after the crop is not checked") + { + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_UINT, {1, 2, 3, 4}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_UINT, {11, 12, 13, 14}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_IEEEFP, {1, 2, 3, 4}); + crop.zBoundVoxels = {0, 1}; + expected = {1, 2, 3, 4, 11, 12, 13, 14}; + } + SECTION("Different page before a one-page crop is not checked") + { + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_UINT, {1, 2, 3, 4}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_IEEEFP, {1, 2, 3, 4}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_UINT, {21, 22, 23, 24}); + crop.zBoundVoxels = {2, 2}; + expected = {21, 22, 23, 24}; + } + SECTION("Physical crop skips a different page before the crop") + { + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_UINT, {1, 2, 3, 4}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_IEEEFP, {1, 2, 3, 4}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_UINT, {21, 22, 23, 24}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_UINT, {31, 32, 33, 34}); + // Default TIFF origin 0 and spacing 1: Z [2.5, 3.5] selects pages 2 and 3. + crop.type = CropGeometryParameter::CropValues::TypeEnum::PhysicalSubvolume; + crop.zBoundPhysical = {2.5f, 3.5f}; + expected = {21, 22, 23, 24, 31, 32, 33, 34}; + } + SECTION("Different page inside the crop is rejected") + { + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_UINT, {1, 2, 3, 4}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_IEEEFP, {1, 2, 3, 4}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_UINT, {21, 22, 23, 24}); + crop.zBoundVoxels = {0, 1}; + expectError = true; + } + SECTION("Cropped pages that differ from page 0 are rejected") + { + // The output type comes from page 0, so matching cropped pages are not enough. + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_UINT, {1, 2, 3, 4}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_INT, {-1, -2, -3, -4}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_INT, {-5, -6, -7, -8}); + crop.zBoundVoxels = {1, 2}; + expectError = true; + } + SECTION("One-page crop that differs from page 0 is rejected") + { + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_UINT, {1, 2, 3, 4}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_UINT, {11, 12, 13, 14}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_IEEEFP, {1, 2, 3, 4}); + crop.zBoundVoxels = {2, 2}; + expectError = true; + } + } + DataStructure ds; + ReadImageFilter filter; + const DataPath geomPath({"Layout"}); + const auto args = MakeReadImageNrrdArgs(path, geomPath, "Cell Data", "Pixels", crop); + if(expectError) + { + const auto preflight = filter.preflight(ds, args); + SIMPLNX_RESULT_REQUIRE_INVALID(preflight.outputActions); + const auto& error = preflight.outputActions.errors().front(); + REQUIRE(error.code == -50579); + REQUIRE(error.message.find("page 0 is") != std::string::npos); + REQUIRE(ds.getDataAs(geomPath) == nullptr); + } + else + { + const auto result = filter.execute(ds, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + const auto arrayPath = geomPath.createChildPath("Cell Data").createChildPath("Pixels"); + REQUIRE_NOTHROW(ds.getDataRefAs(arrayPath)); + const auto& array = ds.getDataRefAs(arrayPath); + REQUIRE(array.getSize() == expected.size()); + for(usize i = 0; i < expected.size(); ++i) + { + CAPTURE(i); + REQUIRE(array[i] == expected[i]); + } + } + UnitTest::CheckArraysInheritTupleDims(ds); +} + +TEST_CASE("NXImageProcessing::ReadImage: TIFF_PageLayout rejected in execute", "[NXImageProcessing][ReadImageFilter]") +{ + // A direct caller of the ReadImage algorithm (as Import Fiji Montage is) gets no ReadImageFilter preflight. + // The algorithm must reject a decoded page whose layout differs from page 0 before it writes any output value. + const ScopedLayoutTiffPath tiffPath("page_layout_execute"); + { + std::unique_ptr tiff(TIFFOpen(tiffPath.path().string().c_str(), "w"), TIFFClose); + REQUIRE(tiff != nullptr); + // Both pages use 4 bytes per pixel, so only the layout check can tell them apart. + WriteLayoutPage(tiff.get(), 4, SAMPLEFORMAT_UINT, {1, 2, 3, 255, 4, 5, 6, 255, 7, 8, 9, 255, 10, 11, 12, 255}); + WriteLayoutPage(tiff.get(), 1, SAMPLEFORMAT_IEEEFP, {1, 2, 3, 4}); + } + + // Destination for both pages, typed from page 0 (4-component uint8) and filled with a sentinel. + constexpr nx::core::uint8 k_Sentinel = 77; + DataStructure ds; + auto* geom = ImageGeom::Create(ds, "Layout"); + REQUIRE(geom != nullptr); + geom->setDimensions({2, 2, 2}); + auto* cellData = AttributeMatrix::Create(ds, "Cell Data", {2, 2, 2}, geom->getId()); + REQUIRE(cellData != nullptr); + geom->setCellData(*cellData); + auto* pixels = UInt8Array::CreateWithStore>(ds, "Pixels", {2, 2, 2}, {4}, cellData->getId()); + REQUIRE(pixels != nullptr); + pixels->fill(k_Sentinel); + + ReadImageInputValues inputValues; + inputValues.inputFilePath = tiffPath.path(); + inputValues.imageGeometryPath = DataPath({"Layout"}); + inputValues.imageDataArrayPath = DataPath({"Layout", "Cell Data", "Pixels"}); + inputValues.cellDataName = "Cell Data"; + inputValues.croppingOptions.type = CropGeometryParameter::CropValues::TypeEnum::NoCropping; + const std::atomic_bool shouldCancel = false; + const IFilter::MessageHandler messageHandler{[](const IFilter::Message&) {}}; + const auto result = ReadImage(ds, messageHandler, shouldCancel, inputValues)(); + SIMPLNX_RESULT_REQUIRE_INVALID(result); + REQUIRE(result.errors().front().code == -50579); + REQUIRE(result.errors().front().message.find("page 1 is 1-component float32, page 0 is 4-component uint8") != std::string::npos); + for(usize i = 0; i < pixels->getSize(); ++i) + { + CAPTURE(i); + REQUIRE((*pixels)[i] == k_Sentinel); + } +} diff --git a/src/Plugins/NXImageProcessing/test/ReconstructionFilterTestUtils.hpp b/src/Plugins/NXImageProcessing/test/ReconstructionFilterTestUtils.hpp index c8013f6231..ce2002eae8 100644 --- a/src/Plugins/NXImageProcessing/test/ReconstructionFilterTestUtils.hpp +++ b/src/Plugins/NXImageProcessing/test/ReconstructionFilterTestUtils.hpp @@ -16,6 +16,7 @@ #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/AlgorithmDispatch.hpp" #include "simplnx/Utilities/DataStoreUtilities.hpp" +#include "simplnx/Utilities/ImageProcessing/StructuringElement.hpp" #include @@ -510,4 +511,45 @@ inline void RunDistanceMapItkGoldenBaseline(const std::string& inputFile, const UnitTest::CheckArraysInheritTupleDims(ds); } + +/** + * @brief Runs an SE-based reconstruction filter (Opening/ClosingByReconstruction) on a 7x3x1 int32 image with an + * Annulus of radius {0, 0, radiusZ} and requires that the output equals the input. + * + * On a single-slice image, the Z offsets of the kernel are ignored, so this kernel is empty and the filter copies + * the input. The oracle is the input itself, and the same filter with the empty Annulus {0, 0, 0} must give the same + * values. Before #1908 L73, Preserve Intensities on returned INT32_MIN (opening) or INT32_MAX (closing) everywhere. + */ +template +inline void RequireZOnlyAnnulus2DPassthrough(UnitTest::AlgorithmTestScope& scope, uint32 radiusZ, bool preserveIntensities, bool fullyConnected) +{ + const std::vector pattern = {3, 20, 8, 100, 40, 12, 80, 70, 2, 90, 15, 55, 22, 101, 30, 10, 99, 6, 77, 32, 64}; + DataStructure ds; + const DataPath inputPath = BuildImageFromPattern(ds, 7, 3, 1, pattern); + scope.requireExpectedStore(ds.getDataRefAs(inputPath)); + const auto run = [&](const std::string& outputName, uint32 kernelRadiusZ) { + Arguments args; + args.insertOrAssign(FilterT::k_InputImageGeomPath_Key, std::make_any(inputPath.getParent().getParent())); + args.insertOrAssign(FilterT::k_InputImageDataPath_Key, std::make_any(inputPath)); + args.insertOrAssign(FilterT::k_OutputImageArrayName_Key, std::make_any(outputName)); + SEReconParamSetter(static_cast(ImageProcessing::KernelType::Annulus), {0, 0, kernelRadiusZ}, preserveIntensities)(args, fullyConnected, 0.0); + FilterT filter; + auto preflightResult = filter.preflight(ds, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + auto executeResult = scope.executeFilter(filter, ds, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + }; + run("Output", radiusZ); + run("Empty", 0); + const auto& output = ds.getDataRefAs>(inputPath.replaceName("Output")); + const auto& empty = ds.getDataRefAs>(inputPath.replaceName("Empty")); + REQUIRE(output.getSize() == pattern.size()); + for(usize i = 0; i < pattern.size(); ++i) + { + CAPTURE(i); + REQUIRE(output[i] == pattern[i]); + REQUIRE(empty[i] == pattern[i]); + } + UnitTest::CheckArraysInheritTupleDims(ds); +} } // namespace recon_test diff --git a/src/Plugins/SimplnxCore/CMakeLists.txt b/src/Plugins/SimplnxCore/CMakeLists.txt index aab4533d8b..199d83b602 100644 --- a/src/Plugins/SimplnxCore/CMakeLists.txt +++ b/src/Plugins/SimplnxCore/CMakeLists.txt @@ -587,8 +587,8 @@ if(EXISTS "${DREAM3D_DATA_DIR}" AND SIMPLNX_DOWNLOAD_TEST_FILES) # set_target_properties(Copy_${PLUGIN_NAME}_STL_Models PROPERTIES FOLDER Plugins/${PLUGIN_NAME}) download_test_data(DREAM3D_DATA_DIR ${DREAM3D_DATA_DIR} - ARCHIVE_NAME align_sections_input.tar.gz - SHA512 3c12c306fc8bba6fcf31a4c2c0c1771a5320267e02d12d7c74897852b73b7b6d74c5414aad9a90f64090e6b9baa6968925ea322ea0c3cb10fbb6022326c74e0b + ARCHIVE_NAME align_sections_input_v2.tar.gz + SHA512 061afbba0eb2ef6ee0b27f598bdf2425daf3d6b1c2b11b797104843c463a1e563e7b285678b7edf503545215eccb54d46d60580041fb2a3d5cdbb78ac78cfc54 INSTALL COPY_DATA ) diff --git a/src/Plugins/SimplnxCore/docs/AlignSectionsFeatureCentroidFilter.md b/src/Plugins/SimplnxCore/docs/AlignSectionsFeatureCentroidFilter.md index 06a45b5efc..ccd3d2c410 100644 --- a/src/Plugins/SimplnxCore/docs/AlignSectionsFeatureCentroidFilter.md +++ b/src/Plugins/SimplnxCore/docs/AlignSectionsFeatureCentroidFilter.md @@ -28,7 +28,7 @@ When **Use Reference Slice** is enabled, **Reference Slice** must be between 0 a The highest-Z section is also shifted when the reference slice is not 0. This corrects DREAM3D 6.5.171 behavior, which left that section unchanged. -An empty mask section has no finite centroid. If either centroid used for a shift is not finite, the relative shift is zero. In consecutive mode, the cumulative shift stays equal to the previous section's shift. In reference mode, the section stays unchanged. This defines behavior that was undefined in DREAM3D 6.5.171. +An empty mask section has no finite centroid. If either centroid used for a shift is not finite, that shift is zero. In consecutive mode, the relative shift is zero, so the cumulative shift stays equal to the previous section's shift. In reference mode, the cumulative shift is zero, so the section stays unchanged. This defines behavior that was undefined in DREAM3D 6.5.171. ## Algorithm @@ -62,8 +62,8 @@ In this new structure, what follows is what the created structures represent: - Alignment Shifts Data (Attribute Matrix) - The tuple size here is defined by the number of slices [ie the Z Dimension of the Image Geometry] - Slices (DataArray | 2 component) - The slice indices (stored as uint32s) -- Relative Shifts (DataArray | 2 component) - The slices shift relative to previous shift (stored as int64s) [*previously known as `newxshift` and `newyshift`*] -- Cumulative Shifts (DataArray | 2 component) - The slice's accumulated shift (stored as int64s) +- Relative Shifts (DataArray | 2 component) - The slice's shift relative to the slice above it (the slice with the next higher Z index), in both modes (stored as int64s). The first row has no slice above it, so its relative shift equals its cumulative shift. [*previously known as `newxshift` and `newyshift`*] +- Cumulative Shifts (DataArray | 2 component) - The slice's accumulated shift (stored as int64s). With **Use Reference Slice** on, this shift is relative to the reference slice. - Centroids (DataArray | 2 component) - The xy centroids of the corresponding slice In previous versions a file would have been produced instead. If you wish to recreate this, you can write the Attribute Matrix as a CSV/Text file. diff --git a/src/Plugins/SimplnxCore/docs/AlignSectionsListFilter.md b/src/Plugins/SimplnxCore/docs/AlignSectionsListFilter.md index 61abab9ac6..95fc00921b 100644 --- a/src/Plugins/SimplnxCore/docs/AlignSectionsListFilter.md +++ b/src/Plugins/SimplnxCore/docs/AlignSectionsListFilter.md @@ -29,7 +29,7 @@ If your shifts are stored in an external file, you need to import them into a Da **Requirements:** - The number of shift entries must equal the number of slices (Z dimension of the geometry) -- Slices must be ordered from bottom to top +- Shift rows must be ordered from the top section to the bottom section **Recommended file format** (space-delimited, two values per line): @@ -52,7 +52,21 @@ For more complex file formats (e.g., legacy DREAM3D alignment files with 6 colum ### Note on Legacy Files -Alignment shift files from DREAM3D/DREAM3DNX versions 7.0.3 and earlier used a 6-column format with both relative and cumulative shifts. If using these files, import via the [Read CSV File](ReadCSVFileFilter.md) filter and select the appropriate columns. Files produced by the legacy `AlignSectionsFeatureCentroid` contained zeros for relative shifts -- use cumulative shifts for those. +Legacy DREAM3D 6.5.171 shift files have six tab-separated columns: section, next section, relative X, relative Y, cumulative X, and cumulative Y. All shifts are in cells. These files have one row fewer than the number of sections. They have no row for the top section. + +For either input type, add a zero row at the start of the file, before importing it. Row 0 is the first data row, not a header. Set all four of its shifts to zero. For the 117-section example, the added row is: + +```text +116 117 0 0 0 0 +``` + +Import all 117 rows with [Read CSV File](ReadCSVFileFilter.md). Use custom column names and start at the first data row. For *Cumulative* input, combine the cumulative X and Y columns, in that order, into one two-component int64 array. For *Relative* input, combine the relative X and Y columns in the same way. + +For a volume with N sections, array row 0 represents the top section, N - 1, which stays fixed. The filter does not read row 0. Row i supplies the shift for section N - 1 - i: its cumulative shift for *Cumulative* input, or its shift relative to the section above it for *Relative* input. The last row supplies the shift for section 0. Without the added zero row, *Cumulative* input does not use the first legacy row and assigns the remaining shifts to the wrong sections. *Relative* input without the zero row returns an error, because the number of rows is not equal to the number of sections. + +See `Align_Sections_Backwards_Compatibility.d3dpipeline` in `Align_Sections_Examples` for the complete example. Its input file already includes the zero row. Do not add another row to that file. + +Legacy `AlignSectionsFeatureCentroid` files contain zeros in the relative shift columns. Use the cumulative shift columns for those files. ### Required Input Sources @@ -66,7 +80,7 @@ Alignment shift files from DREAM3D/DREAM3DNX versions 7.0.3 and earlier used a 6 - align_sections_no_file (meant for visualizing) - align_sections_backwards_compatibility -Note the backwards compatibility also demonstrates how it a more complex user input could be done as well (Using the Read CSV Data Filter). +The backwards compatibility example also shows how to import a more complex input file with Read CSV File. ## License & Copyright diff --git a/src/Plugins/SimplnxCore/docs/FeatureFaceCurvatureFilter.md b/src/Plugins/SimplnxCore/docs/FeatureFaceCurvatureFilter.md index d531d7394a..db36a3bcc3 100644 --- a/src/Plugins/SimplnxCore/docs/FeatureFaceCurvatureFilter.md +++ b/src/Plugins/SimplnxCore/docs/FeatureFaceCurvatureFilter.md @@ -18,6 +18,8 @@ The principal directions can have their signs flipped: they come from eigenvecto The fitting neighborhood contains the triangle itself and its neighbors within the **Neighborhood Ring Count**. Only neighbors with the same pair of **Face Labels** are included. If this neighborhood contains fewer than two triangles, all requested outputs for that triangle are NaN and the filter continues. This includes a Feature face with only one triangle. This differs from DREAM3D 6.5.171, which wrote invalid values for one-triangle faces. Triangles with a NaN component in their face normal or face centroid are excluded from the fit. If the evaluated triangle is excluded, all of its requested outputs are NaN. The outputs are also NaN if this exclusion leaves fewer than two triangles. The evaluated triangle counts toward this minimum. +The fit also needs at least seven other triangles with usable rows when **Use Face Normals for Curve Fitting** is on. It needs at least three when this option is off. The evaluated triangle does not count toward this minimum. A usable row has finite values and at least one nonzero coefficient in the fit. If too few usable rows remain, all requested outputs for the evaluated triangle are NaN. + ### Units Principal curvatures and mean curvature are reported in **1/length** (the reciprocal of the geometry length units; the radius of the best-fit circle is its reciprocal). Gaussian curvature, being a product of two curvatures, is reported in **1/length^2**. Principal directions are **dimensionless** unit 3-vectors. diff --git a/src/Plugins/SimplnxCore/pipelines/Align_Sections_Examples/Align_Sections_Backwards_Compatibility.d3dpipeline b/src/Plugins/SimplnxCore/pipelines/Align_Sections_Examples/Align_Sections_Backwards_Compatibility.d3dpipeline index f36916327b..086af807be 100644 --- a/src/Plugins/SimplnxCore/pipelines/Align_Sections_Examples/Align_Sections_Backwards_Compatibility.d3dpipeline +++ b/src/Plugins/SimplnxCore/pipelines/Align_Sections_Examples/Align_Sections_Backwards_Compatibility.d3dpipeline @@ -77,7 +77,7 @@ ], "Header Line": 1, "Header Mode": 1, - "Input File Path": "Data/align_sections_input/Alignment_By_Misorientation_Shifts.txt", + "Input File Path": "Data/align_sections_input_v2/Alignment_By_Misorientation_Shifts.txt", "Skipped Array Mask": [ false, false, @@ -88,7 +88,7 @@ ], "Start Import Row": 1, "Tuple Dimensions": [ - 116 + 117 ] }, "version": 1 diff --git a/src/Plugins/SimplnxCore/pipelines/Align_Sections_Examples/Align_Sections_User_Input.d3dpipeline b/src/Plugins/SimplnxCore/pipelines/Align_Sections_Examples/Align_Sections_User_Input.d3dpipeline index dfd15c0bca..42d0ba86a2 100644 --- a/src/Plugins/SimplnxCore/pipelines/Align_Sections_Examples/Align_Sections_User_Input.d3dpipeline +++ b/src/Plugins/SimplnxCore/pipelines/Align_Sections_Examples/Align_Sections_User_Input.d3dpipeline @@ -57,7 +57,7 @@ "version": 1 }, "input_file": { - "value": "Data/align_sections_input/example_relative_shifts.txt", + "value": "Data/align_sections_input_v2/example_relative_shifts.txt", "version": 1 }, "number_comp": { diff --git a/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/AlignSectionsFeatureCentroid.cpp b/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/AlignSectionsFeatureCentroid.cpp index d016d5ac7a..497e990ba6 100644 --- a/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/AlignSectionsFeatureCentroid.cpp +++ b/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/AlignSectionsFeatureCentroid.cpp @@ -146,11 +146,12 @@ Result<> AlignSectionsFeatureCentroid::findShifts(std::vector& xShifts, s slice = (dims[2] - 1) - iter; if(m_InputValues->UseReferenceSlice) { - // Reference-relative and cumulative shifts are identical in this mode. - relativexshift = ComputeCentroidShift(xCentroid[iter], xCentroid[static_cast(m_InputValues->ReferenceSlice)], spacing[0]); - relativeyshift = ComputeCentroidShift(yCentroid[iter], yCentroid[static_cast(m_InputValues->ReferenceSlice)], spacing[1]); - xShifts[iter] = relativexshift; - yShifts[iter] = relativeyshift; + // Difference the integer reference-relative shifts so relative input accumulates exactly. + // Row 0 (present for a nonzero reference) has no previous row, so its relative shift is its cumulative shift. + xShifts[iter] = ComputeCentroidShift(xCentroid[iter], xCentroid[static_cast(m_InputValues->ReferenceSlice)], spacing[0]); + yShifts[iter] = ComputeCentroidShift(yCentroid[iter], yCentroid[static_cast(m_InputValues->ReferenceSlice)], spacing[1]); + relativexshift = iter == 0 ? xShifts[iter] : xShifts[iter] - xShifts[iter - 1]; + relativeyshift = iter == 0 ? yShifts[iter] : yShifts[iter] - yShifts[iter - 1]; } else { @@ -400,8 +401,9 @@ Result<> AlignSectionsFeatureCentroid::findShiftsOoc(std::vector& xShifts int64 relativeyshift = 0; if(m_InputValues->UseReferenceSlice) { - relativexshift = xShifts[iter]; - relativeyshift = yShifts[iter]; + // Row 0 (present for a nonzero reference) has no previous row, so its relative shift is its cumulative shift. + relativexshift = iter == 0 ? xShifts[iter] : xShifts[iter] - xShifts[iter - 1]; + relativeyshift = iter == 0 ? yShifts[iter] : yShifts[iter] - yShifts[iter - 1]; } else { diff --git a/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/CalculateTriangleGroupCurvatures.cpp b/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/CalculateTriangleGroupCurvatures.cpp index 42e3392097..9257e2efb3 100644 --- a/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/CalculateTriangleGroupCurvatures.cpp +++ b/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/CalculateTriangleGroupCurvatures.cpp @@ -314,6 +314,7 @@ void CalculateTriangleGroupCurvatures::operator()() const size_t rows = patchCentroids->getNumberOfTuples(); Eigen::MatrixXd A(rows, cols); Eigen::VectorXd b(rows); + usize usableRows = 0; double x = 0.0, y = 0.0, z = 0.0; for(size_t m = 0; m < rows; ++m) { @@ -332,6 +333,17 @@ void CalculateTriangleGroupCurvatures::operator()() const A(m + rows * 6) = y * y * y; } b[m] = z; // The Z Values + if(m != 0 && A.row(m).allFinite() && !A.row(m).isZero(0.0) && std::isfinite(z)) + { + ++usableRows; + } + } + + // Translation makes the seed row zero. Each coefficient needs a usable neighbor row. + if(usableRows < cols) + { + markOutputsInvalid(triId); + continue; } Eigen::Matrix2d M; diff --git a/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/FeatureFaceCurvature.cpp b/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/FeatureFaceCurvature.cpp index d9f6232d90..5aa0c41e88 100644 --- a/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/FeatureFaceCurvature.cpp +++ b/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/FeatureFaceCurvature.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include @@ -139,16 +140,23 @@ Result<> FeatureFaceCurvature::operator()() const auto nonEmptyCount = static_cast(std::count_if(sharedFeatureFaces.begin(), sharedFeatureFaces.end(), [](const auto& featureFace) { return !featureFace.second.empty(); })); -/********************************* - * We are going to specifically invoke TBB directly instead of using ParallelTaskAlgorithm since we can just queue up all - * the tasks while the first tasks start up. TBB will then grab a new task from it's own queue to work on it up to the - * number of hardware limit of the machine. If we use ParallelTaskAlgorithm then at most we can only work on the number - * of tasks that equal the number of cores on the machine. We spend a lot of overhead spinning up new threads to do each - * task. This takes much longer (3x longer) in some cases. - */ + ParallelTaskAlgorithm taskAlgorithm; + taskAlgorithm.requireArraysInMemory({surfaceMeshPrincipalCurvature1sArrayPtr, surfaceMeshPrincipalCurvature2sArrayPtr, surfaceMeshPrincipalDirection1sArrayPtr, + surfaceMeshPrincipalDirection2sArrayPtr, surfaceMeshMeanCurvaturesArrayPtr, surfaceMeshGaussianCurvaturesArrayPtr, surfaceMeshWeingartenMatrixArrayPtr, + surfaceMeshFaceLabelsArrayPtr, surfaceMeshFaceNormalsArrayPtr, surfaceMeshTriangleCentroidsArrayPtr, surfaceMeshFeatureFaceIdsArrayPtr, + triangleGeomPtr->getVertices(), triangleGeomPtr->getFaces()}); + #ifdef SIMPLNX_ENABLE_MULTICORE - std::shared_ptr g(new tbb::task_group); - sendThreadSafeStatusMessage(fmt::format("Adding {} Feature Faces to the work queue....", nonEmptyCount)); + // Resident arrays use a raw TBB task_group instead of ParallelTaskAlgorithm. All feature faces are queued at once, + // and TBB starts the next task as soon as a thread is free. ParallelTaskAlgorithm runs at most one batch of tasks per + // core at a time, which was about 3x slower for this many small tasks. Shared stores that are not resident must be + // accessed by one worker at a time, so then the workers run inline and no additional task_group is created. + std::unique_ptr g; + if(taskAlgorithm.getParallelizationEnabled()) + { + g = std::make_unique(); + sendThreadSafeStatusMessage(fmt::format("Adding {} Feature Faces to the work queue....", nonEmptyCount)); + } #endif m_Throttle.reset(nonEmptyCount, "Calculating Curvatures"); @@ -167,19 +175,23 @@ Result<> FeatureFaceCurvature::operator()() surfaceMeshFaceNormalsArrayPtr, surfaceMeshTriangleCentroidsArrayPtr, m_MessageHandler, m_ShouldCancel); #ifdef SIMPLNX_ENABLE_MULTICORE + if(g != nullptr) { g->run(func); } -#else + else +#endif { func(); } -#endif } #ifdef SIMPLNX_ENABLE_MULTICORE - sendThreadSafeStatusMessage("Waiting on computations to complete."); - g->wait(); // Wait for all the threads to complete before moving on. + if(g != nullptr) + { + sendThreadSafeStatusMessage("Waiting on computations to complete."); + g->wait(); + } #endif // All workers have joined, so the throttle can send its final value before the cleanup step. diff --git a/src/Plugins/SimplnxCore/test/AlignSectionsFeatureCentroidTest.cpp b/src/Plugins/SimplnxCore/test/AlignSectionsFeatureCentroidTest.cpp index 0bdf7116fb..8c433bdf26 100644 --- a/src/Plugins/SimplnxCore/test/AlignSectionsFeatureCentroidTest.cpp +++ b/src/Plugins/SimplnxCore/test/AlignSectionsFeatureCentroidTest.cpp @@ -1,4 +1,5 @@ #include "SimplnxCore/Filters/AlignSectionsFeatureCentroidFilter.hpp" +#include "SimplnxCore/Filters/AlignSectionsListFilter.hpp" #include "SimplnxCore/SimplnxCore_test_dirs.hpp" #include "simplnx/DataStructure/AttributeMatrix.hpp" @@ -9,6 +10,7 @@ #include "simplnx/Parameters/ChoicesParameter.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/AlgorithmDispatch.hpp" +#include "simplnx/Utilities/DataStoreUtilities.hpp" #include @@ -120,9 +122,29 @@ TEST_CASE("SimplnxCore::AlignSectionsFeatureCentroidFilter: output test", "[Reco UnitTest::CompareDataArrays(exemplarDataStructure.getDataRefAs(slicesPath), dataStructure.getDataRefAs(slicesPath)); const DataPath relativeShiftsPath = alignmentAMPath.createChildPath(Constants::k_RelativeShiftsArrayName); - REQUIRE_NOTHROW(exemplarDataStructure.getDataRefAs(relativeShiftsPath)); - REQUIRE_NOTHROW(dataStructure.getDataRefAs(relativeShiftsPath)); - UnitTest::CompareDataArrays(exemplarDataStructure.getDataRefAs(relativeShiftsPath), dataStructure.getDataRefAs(relativeShiftsPath)); + { + // Stored Relative Shifts predate #1908 L77; derive expected values from the exemplar's Cumulative Shifts instead. + const DataPath exemplarCumulativePath = alignmentAMPath.createChildPath(Constants::k_CumulativeShiftsArrayName); + REQUIRE_NOTHROW(exemplarDataStructure.getDataRefAs(exemplarCumulativePath)); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(relativeShiftsPath)); + const auto& exemplarCumulative = exemplarDataStructure.getDataRefAs(exemplarCumulativePath); + const auto& relative = dataStructure.getDataRefAs(relativeShiftsPath); + REQUIRE(relative.getTupleShape() == exemplarCumulative.getTupleShape()); + REQUIRE(relative.getNumberOfComponents() == 2); + REQUIRE(exemplarCumulative.getNumberOfComponents() == 2); + REQUIRE(relative.getNumberOfTuples() > 0); + REQUIRE(relative[0] == 0); + REQUIRE(relative[1] == 0); + for(usize iter = 1; iter < relative.getNumberOfTuples(); iter++) + { + for(usize component = 0; component < 2; component++) + { + CAPTURE(iter, component); + const usize index = iter * 2 + component; + REQUIRE(relative[index] == exemplarCumulative[index] - exemplarCumulative[index - 2]); + } + } + } const DataPath cumulativeShiftsPath = alignmentAMPath.createChildPath(Constants::k_CumulativeShiftsArrayName); REQUIRE_NOTHROW(exemplarDataStructure.getDataRefAs(cumulativeShiftsPath)); @@ -241,12 +263,19 @@ TEST_CASE("SimplnxCore::AlignSectionsFeatureCentroidFilter: Nonzero reference to const DataPath shiftsPath({"Image", "Shifts", "Cumulative"}); REQUIRE_NOTHROW(dataStructure.getDataRefAs(shiftsPath)); const auto& shifts = dataStructure.getDataRefAs(shiftsPath); + const DataPath relativePath({"Image", "Shifts", "Relative Shifts"}); + REQUIRE_NOTHROW(dataStructure.getDataRefAs(relativePath)); + const auto& relative = dataStructure.getDataRefAs(relativePath); const std::array expectedShifts = {1, 0, -1, 0}; + // Row 0 has no previous row, so its relative shift equals its cumulative shift. + const std::array expectedRelative = {1, -1, -1, 1}; for(usize iter = 0; iter < 4; iter++) { CAPTURE(iter); CHECK(shifts[iter * 2] == expectedShifts[iter]); CHECK(shifts[(iter * 2) + 1] == expectedShifts[iter]); + CHECK(relative[iter * 2] == expectedRelative[iter]); + CHECK(relative[(iter * 2) + 1] == expectedRelative[iter]); } } UnitTest::CheckArraysInheritTupleDims(dataStructure); @@ -277,7 +306,8 @@ TEST_CASE("SimplnxCore::AlignSectionsFeatureCentroidFilter: Empty slices", "[Sim // Empty middle (index 2), consecutive: relative [0,-1,0,0], cumulative [0,-1,-1,-1]. // Both axes have the same values. A zero relative shift preserves the previous cumulative shift. std::array expectedShifts = emptyCase == 0 ? std::array{0, 0, -1, 0} : emptyCase == 1 ? std::array{0, -1, -1, -1} : std::array{0, 0, 0, 0}; - std::array expectedRelative = emptyCase == 1 ? std::array{0, -1, 0, 0} : expectedShifts; + // Relative shifts are slice-to-slice differences of the cumulative shifts: [0,0,-1,1] for an empty top. + std::array expectedRelative = emptyCase == 0 ? std::array{0, 0, -1, 1} : emptyCase == 1 ? std::array{0, -1, 0, 0} : std::array{0, 0, 0, 0}; const usize emptyZ = emptyCase == 0 ? 3 : emptyCase == 1 ? 1 : 2; const std::array markXY = {4, 0, 4, 8}; for(usize z = 0; z < 4; z++) @@ -386,3 +416,101 @@ TEST_CASE("SimplnxCore::AlignSectionsFeatureCentroidFilter: Reference slice boun } UnitTest::CheckArraysInheritTupleDims(dataStructure); } + +TEST_CASE("SimplnxCore::AlignSectionsFeatureCentroidFilter: Reference slice relative shifts round trip", "[SimplnxCore][AlignSectionsFeatureCentroidFilter]") +{ + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope scope(scenario); + const DataPath imagePath({"Image"}); + const DataPath maskPath({"Image", "Cells", "Mask"}); + const DataPath valuesPath({"Image", "Cells", "Values"}); + const DataPath relativePath({"Image", "Alignment", "Relative"}); + const DataPath cumulativePath({"Image", "Alignment", "Cumulative"}); + const auto buildInput = [&]() { + DataStructure data; + auto* image = ImageGeom::Create(data, "Image"); + REQUIRE(image != nullptr); + image->setDimensions({9, 9, 4}); + image->setSpacing({1.0F, 1.0F, 1.0F}); + image->setUnits(IGeometry::LengthUnit::Micrometer); + auto* cells = AttributeMatrix::Create(data, "Cells", {4, 9, 9}, image->getId()); + REQUIRE(cells != nullptr); + image->setCellData(*cells); + auto* mask = UInt8Array::Create(data, "Mask", DataStoreUtilities::CreateDataStore(data, maskPath, {4, 9, 9}, {1}), cells->getId()); + auto* values = Int32Array::Create(data, "Values", DataStoreUtilities::CreateDataStore(data, valuesPath, {4, 9, 9}, {1}), cells->getId()); + REQUIRE(mask != nullptr); + REQUIRE(values != nullptr); + mask->fill(0); + // Centroids in descending Z order: (3,3), (5,4), (4,6), (6,5). + const std::array x = {6, 4, 5, 3}; + const std::array y = {5, 6, 4, 3}; + for(usize z = 0; z < 4; z++) + { + (*mask)[z * 81 + y[z] * 9 + x[z]] = 1; + } + for(usize i = 0; i < values->getSize(); i++) + { + (*values)[i] = static_cast(i + 1); + } + return data; + }; + auto data = buildInput(); + using Filter = AlignSectionsFeatureCentroidFilter; + Arguments args; + args.insertOrAssign(Filter::k_SelectedImageGeometryPath_Key, std::make_any(imagePath)); + args.insertOrAssign(Filter::k_MaskArrayPath_Key, std::make_any(maskPath)); + args.insertOrAssign(Filter::k_UseReferenceSlice_Key, std::make_any(true)); + args.insertOrAssign(Filter::k_ReferenceSlice_Key, std::make_any(0)); + args.insertOrAssign(Filter::k_StoreAlignmentShifts_Key, std::make_any(true)); + args.insertOrAssign(Filter::k_AlignmentAMName_Key, std::make_any("Alignment")); + args.insertOrAssign(Filter::k_RelativeShiftsArrayName_Key, std::make_any("Relative")); + args.insertOrAssign(Filter::k_CumulativeShiftsArrayName_Key, std::make_any("Cumulative")); + auto result = scope.executeFilter(Filter{}, data, args); + SIMPLNX_RESULT_REQUIRE_VALID(result.result); + REQUIRE_NOTHROW(data.getDataRefAs(relativePath)); + REQUIRE_NOTHROW(data.getDataRefAs(cumulativePath)); + const auto& relative = data.getDataRefAs(relativePath); + const auto& cumulative = data.getDataRefAs(cumulativePath); + SECTION("Literal shift oracle") + { + const std::array expectedRelative = {0, 0, 2, 1, -1, 2, 2, -1}; + const std::array expectedCumulative = {0, 0, 2, 1, 1, 3, 3, 2}; + for(usize i = 0; i < expectedRelative.size(); i++) + { + CAPTURE(i); + CHECK(relative[i] == expectedRelative[i]); + CHECK(cumulative[i] == expectedCumulative[i]); + } + } + SECTION("List relative and cumulative inputs align identically") + { + auto relativeData = buildInput(); + auto cumulativeData = buildInput(); + const auto runList = [&](DataStructure& target, const Int64Array& shifts, ChoicesParameter::ValueType type) { + auto* array = Int64Array::Create(target, "Shifts", DataStoreUtilities::CreateDataStore(target, DataPath({"Shifts"}), {4}, {2})); + REQUIRE(array != nullptr); + for(usize i = 0; i < 8; i++) + { + (*array)[i] = shifts[i]; + } + Arguments listArgs; + listArgs.insertOrAssign(AlignSectionsListFilter::k_SelectedImageGeometryPath_Key, std::make_any(imagePath)); + listArgs.insertOrAssign(AlignSectionsListFilter::k_ShiftsArrayPath_Key, std::make_any(DataPath({"Shifts"}))); + listArgs.insertOrAssign(AlignSectionsListFilter::k_InputArrayType_Key, std::make_any(type)); + auto listResult = scope.executeFilter(AlignSectionsListFilter{}, target, listArgs); + SIMPLNX_RESULT_REQUIRE_VALID(listResult.result); + UnitTest::CheckArraysInheritTupleDims(target); + }; + runList(relativeData, relative, 0); + runList(cumulativeData, cumulative, 1); + for(const auto& path : {maskPath, valuesPath}) + { + REQUIRE_NOTHROW(relativeData.getDataRefAs(path)); + REQUIRE_NOTHROW(cumulativeData.getDataRefAs(path)); + } + UnitTest::CompareDataArrays(relativeData.getDataRefAs(maskPath), cumulativeData.getDataRefAs(maskPath)); + UnitTest::CompareDataArrays(relativeData.getDataRefAs(valuesPath), cumulativeData.getDataRefAs(valuesPath)); + } + UnitTest::CheckArraysInheritTupleDims(data); +} diff --git a/src/Plugins/SimplnxCore/test/AlignSectionsListTest.cpp b/src/Plugins/SimplnxCore/test/AlignSectionsListTest.cpp index 13effc8eb6..855d1db078 100644 --- a/src/Plugins/SimplnxCore/test/AlignSectionsListTest.cpp +++ b/src/Plugins/SimplnxCore/test/AlignSectionsListTest.cpp @@ -1,18 +1,25 @@ #include "SimplnxCore/Filters/AlignSectionsListFilter.hpp" +#include "SimplnxCore/Filters/CombineAttributeArraysFilter.hpp" +#include "SimplnxCore/Filters/ReadCSVFileFilter.hpp" #include "SimplnxCore/SimplnxCore_test_dirs.hpp" +#include "simplnx/Common/ScopeGuard.hpp" #include "simplnx/DataStructure/AttributeMatrix.hpp" #include "simplnx/DataStructure/Geometry/ImageGeom.hpp" #include "simplnx/Parameters/ChoicesParameter.hpp" +#include "simplnx/Parameters/ReadCSVFileParameter.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" #include "simplnx/Utilities/AlgorithmDispatch.hpp" #include "simplnx/Utilities/DataArrayUtilities.hpp" #include "simplnx/Utilities/DataGroupUtilities.hpp" +#include "simplnx/Utilities/DataStoreUtilities.hpp" #include +#include #include #include +#include namespace fs = std::filesystem; using namespace nx::core; @@ -185,3 +192,105 @@ TEST_CASE("SimplnxCore::AlignSectionsListFilter: Cumulative Shifts execution", " UnitTest::CheckArraysInheritTupleDims(dataStructure, SmallIn100::k_TupleCheckIgnoredPaths); } + +TEST_CASE("SimplnxCore::AlignSectionsListFilter: Legacy cumulative file requires zero row", "[SimplnxCore][AlignSectionsListFilter]") +{ + const auto scenario = GENERATE(from_range(UnitTest::SelectAlgorithmTestScenariosForInMemoryStores())); + CAPTURE(scenario); + UnitTest::AlgorithmTestScope scope(scenario); + const bool prependZero = GENERATE(false, true); + CAPTURE(prependZero); + + const fs::path directory = fs::temp_directory_path() / ("AlignSectionsListLegacyShifts_" + Uuid::GenerateV4().str()); + auto cleanup = MakeScopeGuard([&directory]() noexcept { + std::error_code error; + fs::remove_all(directory, error); + }); + REQUIRE(fs::create_directory(directory)); + const auto filePath = directory / "legacy.txt"; + std::ofstream file(filePath); + REQUIRE(file.is_open()); + if(prependZero) + { + file << "3\t4\t0\t0\t0\t0\n"; + } + // Legacy rows describe sections 2, 1, 0, in that order. Shifts are in cells. + file << "2\t3\t1\t0\t1\t0\n1\t2\t0\t1\t1\t1\n0\t1\t1\t0\t2\t1\n"; + file.close(); + REQUIRE_FALSE(file.fail()); + + DataStructure dataStructure; + auto* geometry = ImageGeom::Create(dataStructure, "Image"); + REQUIRE(geometry != nullptr); + geometry->setDimensions({5, 5, 4}); + geometry->setSpacing({1.0F, 1.0F, 1.0F}); + geometry->setUnits(IGeometry::LengthUnit::Micrometer); + auto* cells = AttributeMatrix::Create(dataStructure, "Cells", {4, 5, 5}, geometry->getId()); + REQUIRE(cells != nullptr); + geometry->setCellData(*cells); + auto* values = Int32Array::Create(dataStructure, "Position", DataStoreUtilities::CreateDataStore(dataStructure, DataPath({"Image", "Cells", "Position"}), {4, 5, 5}, {1}), cells->getId()); + REQUIRE(values != nullptr); + for(usize z = 0; z < 4; z++) + { + for(usize y = 0; y < 5; y++) + { + for(usize x = 0; x < 5; x++) + { + (*values)[z * 25 + y * 5 + x] = static_cast(1 + x + 10 * y + 100 * z); + } + } + } + + ReadCSVData csv; + csv.inputFilePath = filePath.string(); + csv.customHeaders = {"slice", "next", "relX", "relY", "cumX", "cumY"}; + csv.dataTypes = std::vector(6, CSVType::int64); + csv.skippedArrayMask = std::vector(6, false); + csv.delimiters = {'\t'}; + csv.headerMode = ReadCSVData::HeaderMode::CUSTOM; + csv.startImportRow = 1; + csv.tupleDims = {prependZero ? usize{4} : usize{3}}; + Arguments readArgs; + readArgs.insertOrAssign(ReadCSVFileFilter::k_ReadCSVData_Key, std::make_any(csv)); + readArgs.insertOrAssign(ReadCSVFileFilter::k_UseExistingGroup_Key, std::make_any(false)); + readArgs.insertOrAssign(ReadCSVFileFilter::k_CreatedDataGroup_Key, std::make_any(DataPath({"Shifts"}))); + auto readResult = ReadCSVFileFilter{}.execute(dataStructure, readArgs); + SIMPLNX_RESULT_REQUIRE_VALID(readResult.result); + + Arguments combineArgs; + combineArgs.insertOrAssign(CombineAttributeArraysFilter::k_SelectedDataArrayPaths_Key, + std::make_any>(std::vector{DataPath({"Shifts", "cumX"}), DataPath({"Shifts", "cumY"})})); + combineArgs.insertOrAssign(CombineAttributeArraysFilter::k_StackedDataArrayName_Key, std::make_any("Cumulative")); + combineArgs.insertOrAssign(CombineAttributeArraysFilter::k_NormalizeData_Key, std::make_any(false)); + combineArgs.insertOrAssign(CombineAttributeArraysFilter::k_MoveValues_Key, std::make_any(false)); + auto combineResult = CombineAttributeArraysFilter{}.execute(dataStructure, combineArgs); + SIMPLNX_RESULT_REQUIRE_VALID(combineResult.result); + + Arguments alignArgs; + alignArgs.insertOrAssign(AlignSectionsListFilter::k_InputArrayType_Key, std::make_any(1)); + alignArgs.insertOrAssign(AlignSectionsListFilter::k_SelectedImageGeometryPath_Key, std::make_any(DataPath({"Image"}))); + alignArgs.insertOrAssign(AlignSectionsListFilter::k_ShiftsArrayPath_Key, std::make_any(DataPath({"Shifts", "Cumulative"}))); + auto alignResult = scope.executeFilter(AlignSectionsListFilter{}, dataStructure, alignArgs); + SIMPLNX_RESULT_REQUIRE_VALID(alignResult.result); + + // Hand-computed shifts by section 0, 1, 2, 3. Without the zero row, the first + // legacy shift is unused, the middle sections use the row below, and section 0 is unchanged. + const std::array, 4> legacyMapping = {{{0, 0}, {2, 1}, {1, 1}, {0, 0}}}; + const std::array, 4> correctedMapping = {{{2, 1}, {1, 1}, {1, 0}, {0, 0}}}; + const auto& expectedShifts = prependZero ? correctedMapping : legacyMapping; + for(usize z = 0; z < 4; z++) + { + for(int64 y = 0; y < 5; y++) + { + for(int64 x = 0; x < 5; x++) + { + const int64 sourceX = x + expectedShifts[z][0]; + const int64 sourceY = y + expectedShifts[z][1]; + const int32 expected = sourceX < 5 && sourceY < 5 ? static_cast(1 + sourceX + 10 * sourceY + 100 * z) : 0; + CAPTURE(z, x, y); + REQUIRE((*values)[z * 25 + y * 5 + x] == expected); + } + } + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} diff --git a/src/Plugins/SimplnxCore/test/FeatureFaceCurvatureTest.cpp b/src/Plugins/SimplnxCore/test/FeatureFaceCurvatureTest.cpp index 1a765a1bcc..909172bf80 100644 --- a/src/Plugins/SimplnxCore/test/FeatureFaceCurvatureTest.cpp +++ b/src/Plugins/SimplnxCore/test/FeatureFaceCurvatureTest.cpp @@ -364,6 +364,129 @@ TEST_CASE("SimplnxCore::FeatureFaceCurvatureFilter: Single triangle face", "[Sim UnitTest::CheckArraysInheritTupleDims(dataStructure); } +TEST_CASE("SimplnxCore::FeatureFaceCurvatureFilter: Patch solvability", "[SimplnxCore][FeatureFaceCurvatureFilter][PatchSolvability]") +{ + const bool useNormals = GENERATE(false, true); + const bool optionalOutputs = GENERATE(false, true); + const usize nonSeedCount = GENERATE(1, 2, 3, 6, 7, 8); + const bool duplicateSeed = GENERATE(false, true); + const usize faceCount = nonSeedCount + 1 + (duplicateSeed ? 1 : 0); + CAPTURE(useNormals, optionalOutputs, nonSeedCount, duplicateSeed); + + DataStructure dataStructure; + auto* geom = TriangleGeom::Create(dataStructure, "Triangles"); + REQUIRE(geom != nullptr); + auto* faceData = AttributeMatrix::Create(dataStructure, "FaceData", {faceCount}, geom->getId()); + REQUIRE(faceData != nullptr); + geom->setFaceAttributeMatrix(*faceData); + auto* vertices = Float32Array::Create(dataStructure, "Vertices", std::make_unique>(ShapeType{2 * faceCount + 1}, ShapeType{3}, 0.0F), geom->getId()); + auto* faces = DataArray::Create(dataStructure, "Faces", std::make_unique>(ShapeType{faceCount}, ShapeType{3}, 0), geom->getId()); + REQUIRE(vertices != nullptr); + REQUIRE(faces != nullptr); + geom->setVertices(*vertices); + geom->setFaceList(*faces); + auto* labels = Int32Array::Create(dataStructure, "Labels", std::make_unique>(ShapeType{faceCount}, ShapeType{2}, 1), faceData->getId()); + auto* ids = Int32Array::Create(dataStructure, "Ids", std::make_unique>(ShapeType{faceCount}, ShapeType{1}, 1), faceData->getId()); + auto* normals = Float64Array::Create(dataStructure, "Normals", std::make_unique>(ShapeType{faceCount}, ShapeType{3}, 0.0), faceData->getId()); + auto* centroids = Float64Array::Create(dataStructure, "Centroids", std::make_unique>(ShapeType{faceCount}, ShapeType{3}, 0.0), faceData->getId()); + REQUIRE(labels != nullptr); + REQUIRE(ids != nullptr); + REQUIRE(normals != nullptr); + REQUIRE(centroids != nullptr); + + // All triangles share vertex 0, so the one-ring patch contains every face. + // Sample z=-(x*x+y*y)/(2R). At seed 0 the normal is +Z and both + // signed principal curvatures are +1/R. The quadratic is exact at this seed. + constexpr float64 radius = 10.0; + const std::array, 9> samples = {{{0, 0}, {1, 0}, {0, 1}, {1, 1}, {-1, 1}, {2, 1}, {1, 2}, {-1, 2}, {2, -1}}}; + vertices->setComponent(0, 2, 1.0F); + for(usize t = 0; t < faceCount; ++t) + { + const auto& sample = samples[t <= nonSeedCount ? t : 0]; + const float64 x = sample[0]; + const float64 y = sample[1]; + const float64 z = -(x * x + y * y) / (2.0 * radius); + // Distinct vertices give each triangle the supplied centroid, including seed 0. + vertices->setComponent(2 * t + 1, 0, static_cast(3.0 * x + 1.0)); + vertices->setComponent(2 * t + 1, 1, static_cast(3.0 * y + 1.0)); + vertices->setComponent(2 * t + 1, 2, static_cast(3.0 * z - 0.5)); + vertices->setComponent(2 * t + 2, 0, -1.0F); + vertices->setComponent(2 * t + 2, 1, -1.0F); + vertices->setComponent(2 * t + 2, 2, -0.5F); + faces->setComponent(t, 1, 2 * t + 1); + faces->setComponent(t, 2, 2 * t + 2); + labels->setComponent(t, 1, 2); + const float64 length = std::sqrt(1.0 + (x * x + y * y) / (radius * radius)); + normals->setComponent(t, 0, x / (radius * length)); + normals->setComponent(t, 1, y / (radius * length)); + normals->setComponent(t, 2, 1.0 / length); + centroids->setComponent(t, 0, x); + centroids->setComponent(t, 1, y); + centroids->setComponent(t, 2, z); + } + + const DataPath geomPath({"Triangles"}); + const DataPath facePath = geomPath.createChildPath("FaceData"); + Arguments args; + args.insertOrAssign(FeatureFaceCurvatureFilter::k_TriangleGeomPath_Key, std::make_any(geomPath)); + args.insertOrAssign(FeatureFaceCurvatureFilter::k_FaceAttribMatrixPath_Key, std::make_any(facePath)); + args.insertOrAssign(FeatureFaceCurvatureFilter::k_NeighborhoodRing_Key, std::make_any(1)); + args.insertOrAssign(FeatureFaceCurvatureFilter::k_UseFaceNormals_Key, std::make_any(useNormals)); + args.insertOrAssign(FeatureFaceCurvatureFilter::k_ComputePrincipalDirection_Key, std::make_any(optionalOutputs)); + args.insertOrAssign(FeatureFaceCurvatureFilter::k_ComputeGaussianCurvature_Key, std::make_any(optionalOutputs)); + args.insertOrAssign(FeatureFaceCurvatureFilter::k_ComputeMeanCurvaturePath_Key, std::make_any(optionalOutputs)); + args.insertOrAssign(FeatureFaceCurvatureFilter::k_ComputeWeingartenMatrix_Key, std::make_any(optionalOutputs)); + args.insertOrAssign(FeatureFaceCurvatureFilter::k_FaceLabelsPath_Key, std::make_any(facePath.createChildPath("Labels"))); + args.insertOrAssign(FeatureFaceCurvatureFilter::k_FeatureFaceIdsPath_Key, std::make_any(facePath.createChildPath("Ids"))); + args.insertOrAssign(FeatureFaceCurvatureFilter::k_FaceNormalsPath_Key, std::make_any(facePath.createChildPath("Normals"))); + args.insertOrAssign(FeatureFaceCurvatureFilter::k_FaceCentroidsPath_Key, std::make_any(facePath.createChildPath("Centroids"))); + const std::array outputKeys = {FeatureFaceCurvatureFilter::k_PrincipalCurvature1Path_Key, FeatureFaceCurvatureFilter::k_PrincipalCurvature2Path_Key, + FeatureFaceCurvatureFilter::k_GaussianCurvaturePath_Key, FeatureFaceCurvatureFilter::k_MeanCurvaturePath_Key, + FeatureFaceCurvatureFilter::k_WeingartenMatrixPath_Key, FeatureFaceCurvatureFilter::k_PrincipalDirection1Path_Key, + FeatureFaceCurvatureFilter::k_PrincipalDirection2Path_Key}; + for(usize i = 0; i < outputKeys.size(); ++i) + { + args.insertOrAssign(outputKeys[i], std::make_any(facePath.createChildPath("Output" + std::to_string(i)))); + } + FeatureFaceCurvatureFilter filter; + auto preflightResult = filter.preflight(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions); + auto executeResult = filter.execute(dataStructure, args); + SIMPLNX_RESULT_REQUIRE_VALID(executeResult.result); + + for(usize i = 0; i < (optionalOutputs ? outputKeys.size() : 2); ++i) + { + const auto* output = dataStructure.getDataAs(facePath.createChildPath("Output" + std::to_string(i))); + REQUIRE(output != nullptr); + // Only seed 0 has a horizontal tangent plane and the closed-form 1/R fit. + for(usize c = 0; c < output->getNumberOfComponents(); ++c) + { + CAPTURE(i, c); + const float64 value = output->getDataStoreRef().getComponentValue(0, c); + if(nonSeedCount < (useNormals ? 7 : 3)) + { + CHECK(std::isnan(value)); + } + else if(i < 4) + { + const float64 expected = i == 2 ? 1.0 / (radius * radius) : 1.0 / radius; + // Exact polynomial fit at the origin, with absolute roundoff tolerance. + CHECK(value == Approx(expected).margin(1.0e-10)); + } + else if(i == 4) + { + CHECK(value == Approx(c == 0 || c == 3 ? -1.0 / radius : 0.0).margin(1.0e-10)); + } + else + { + CHECK(std::isfinite(value)); + } + } + } + + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} + namespace { constexpr int32 k_InjectedError = -56991; @@ -893,3 +1016,188 @@ TEST_CASE("SimplnxCore::FeatureFaceCurvatureFilter: real store support ownership std::cout << fmt::format( "Curvature ownership/save/recovery complete incidence={} vertices=HDF5-OOC faces=HDF5-OOC labels=HDF5-OOC ids=HDF5-OOC normals=HDF5-OOC centroids=HDF5-OOC outputs=HDF5-OOC\n", incidence); } + +namespace +{ +void CheckMultiFeatureCurvature(bool diskBacked, bool optionalOutputs) +{ + const UnitTest::PreferencesSentinel preferences(diskBacked ? DataStorageMode::ForceOutOfCore : DataStorageMode::ForceInCore, 1); + constexpr usize featureCount = 8; + constexpr usize facesPerFeature = 9; + constexpr usize faceCount = featureCount * facesPerFeature; + const DataPath geomPath({"Triangles"}); + const DataPath facePath = geomPath.createChildPath("FaceData"); + DataStructure dataStructure; + auto* geom = TriangleGeom::Create(dataStructure, "Triangles"); + REQUIRE(geom != nullptr); + auto* faceData = AttributeMatrix::Create(dataStructure, "FaceData", {faceCount}, geom->getId()); + REQUIRE(faceData != nullptr); + geom->setFaceAttributeMatrix(*faceData); + auto* vertices = + Float32Array::Create(dataStructure, "Vertices", DataStoreUtilities::CreateDataStore(dataStructure, geomPath.createChildPath("Vertices"), {featureCount * 19}, {3}), geom->getId()); + auto* faces = DataArray::Create( + dataStructure, "Faces", DataStoreUtilities::CreateDataStore(dataStructure, geomPath.createChildPath("Faces"), {faceCount}, {3}), geom->getId()); + REQUIRE(vertices != nullptr); + REQUIRE(faces != nullptr); + geom->setVertices(*vertices); + geom->setFaceList(*faces); + auto* labels = Int32Array::Create(dataStructure, "Labels", DataStoreUtilities::CreateDataStore(dataStructure, facePath.createChildPath("Labels"), {faceCount}, {2}), faceData->getId()); + auto* ids = Int32Array::Create(dataStructure, "Ids", DataStoreUtilities::CreateDataStore(dataStructure, facePath.createChildPath("Ids"), {faceCount}, {1}), faceData->getId()); + auto* normals = Float64Array::Create(dataStructure, "Normals", DataStoreUtilities::CreateDataStore(dataStructure, facePath.createChildPath("Normals"), {faceCount}, {3}), faceData->getId()); + auto* centroids = + Float64Array::Create(dataStructure, "Centroids", DataStoreUtilities::CreateDataStore(dataStructure, facePath.createChildPath("Centroids"), {faceCount}, {3}), faceData->getId()); + REQUIRE(labels != nullptr); + REQUIRE(ids != nullptr); + REQUIRE(normals != nullptr); + REQUIRE(centroids != nullptr); + for(usize f = 0; f < featureCount; ++f) + { + const usize base = f * 19; + vertices->setComponent(base, 0, 0.0F); + vertices->setComponent(base, 1, 0.0F); + vertices->setComponent(base, 2, static_cast(f)); + for(usize t = 0; t < facesPerFeature; ++t) + { + const usize face = f * facesPerFeature + t; + const float64 x = 1.0 + static_cast(t % 3); + const float64 y = 1.0 + static_cast((t / 3) * (t / 3)); + // Each feature is a disjoint patch of the plane z=f. All nine triangles + // share a vertex, have centroid (x,y,f), and normal +Z. + for(usize side = 0; side < 2; ++side) + { + const float64 sign = side == 0 ? 1.0 : -1.0; + vertices->setComponent(base + 2 * t + 1 + side, 0, static_cast(1.5 * x + sign * y)); + vertices->setComponent(base + 2 * t + 1 + side, 1, static_cast(1.5 * y - sign * x)); + vertices->setComponent(base + 2 * t + 1 + side, 2, static_cast(f)); + } + faces->setComponent(face, 0, base); + faces->setComponent(face, 1, base + 2 * t + 1); + faces->setComponent(face, 2, base + 2 * t + 2); + labels->setComponent(face, 0, static_cast(2 * f + 1)); + labels->setComponent(face, 1, static_cast(2 * f + 2)); + ids->setComponent(face, 0, static_cast(f + 1)); + normals->setComponent(face, 0, 0.0); + normals->setComponent(face, 1, 0.0); + normals->setComponent(face, 2, 1.0); + centroids->setComponent(face, 0, x); + centroids->setComponent(face, 1, y); + centroids->setComponent(face, 2, static_cast(f)); + } + } + for(const IDataArray* array : std::array{vertices, faces, labels, ids, normals, centroids}) + { + REQUIRE((array->getIDataStoreRef().getStoreType() == IDataStore::StoreType::OutOfCore) == diskBacked); + if(diskBacked) + { + REQUIRE(array->getIDataStoreRef().getDataFormat() == "HDF5-OOC"); + } + } + // Run the algorithm on output arrays that already hold a sentinel. The expected curvatures of a plane are 0, the + // same value an unwritten store returns, so the sentinel makes every lost or skipped write fail the checks below. + constexpr float64 k_Sentinel = 999.0; + constexpr usize k_OutputCount = 7; + const std::array outputComponents = {1, 1, 1, 1, 4, 3, 3}; + std::array outputPaths; + for(usize i = 0; i < k_OutputCount; ++i) + { + outputPaths[i] = facePath.createChildPath("Output" + std::to_string(i)); + if(i >= 2 && !optionalOutputs) + { + continue; + } + auto* output = Float64Array::Create(dataStructure, outputPaths[i].getTargetName(), DataStoreUtilities::CreateDataStore(dataStructure, outputPaths[i], {faceCount}, {outputComponents[i]}), + faceData->getId()); + REQUIRE(output != nullptr); + output->fill(k_Sentinel); + } + FeatureFaceCurvatureInputValues inputValues; + inputValues.NRingCount = 1; + inputValues.useNormalsForCurveFitting = true; + inputValues.triangleGeomPath = geomPath; + inputValues.surfaceMeshFaceLabelsPath = facePath.createChildPath("Labels"); + inputValues.surfaceMeshFeatureFaceIdsPath = facePath.createChildPath("Ids"); + inputValues.surfaceMeshFaceNormalsPath = facePath.createChildPath("Normals"); + inputValues.surfaceMeshTriangleCentroidsPath = facePath.createChildPath("Centroids"); + inputValues.surfaceMeshPrincipalCurvature1Path = outputPaths[0]; + inputValues.surfaceMeshPrincipalCurvature1sPath = outputPaths[0]; + inputValues.surfaceMeshPrincipalCurvature2sPath = outputPaths[1]; + inputValues.surfaceMeshGaussianCurvaturesPath = outputPaths[2]; + inputValues.surfaceMeshMeanCurvaturesPath = outputPaths[3]; + inputValues.surfaceMeshWeingartenMatrixPath = outputPaths[4]; + inputValues.surfaceMeshPrincipalDirection1sPath = outputPaths[5]; + inputValues.surfaceMeshPrincipalDirection2sPath = outputPaths[6]; + // This existing status message is emitted only when tasks are queued to TBB. + // It witnesses the gate decision without relying on a race corrupting data. + std::atomic_bool queued = false; + const IFilter::MessageHandler handler{[&](const IFilter::Message& message) { + if(message.message.find("Feature Faces to the work queue") != std::string::npos) + { + queued = true; + } + }}; + const std::atomic_bool shouldCancel = false; + const auto result = FeatureFaceCurvature(dataStructure, handler, shouldCancel, &inputValues)(); + SIMPLNX_RESULT_REQUIRE_VALID(result); +#ifdef SIMPLNX_ENABLE_MULTICORE + CHECK(queued.load() == !diskBacked); +#else + CHECK_FALSE(queued.load()); +#endif + for(usize i = 0; i < (optionalOutputs ? k_OutputCount : 2); ++i) + { + const auto& output = dataStructure.getDataRefAs(outputPaths[i]); + REQUIRE((output.getDataStoreRef().getStoreType() == IDataStore::StoreType::OutOfCore) == diskBacked); + if(diskBacked) + { + REQUIRE(output.getDataStoreRef().getDataFormat() == "HDF5-OOC"); + } + for(usize t = 0; t < faceCount; ++t) + { + CAPTURE(i, t); + if(i < 5) + { + // Independent oracle: a plane has zero Hessian, hence zero principal, + // Gaussian and mean curvatures and a zero Weingarten matrix everywhere. + for(usize c = 0; c < output.getNumberOfComponents(); ++c) + { + CHECK(output.getDataStoreRef().getComponentValue(t, c) == Approx(0.0).margin(1.0e-12)); + } + } + else + { + // Repeated eigenvalues do not define a unique direction. Both returned + // directions must still be unit vectors in the plane (up to sign). + const float64 x = output.getDataStoreRef().getComponentValue(t, 0); + const float64 y = output.getDataStoreRef().getComponentValue(t, 1); + CHECK(x * x + y * y == Approx(1.0).margin(1.0e-12)); + CHECK(output.getDataStoreRef().getComponentValue(t, 2) == Approx(0.0).margin(1.0e-12)); + } + } + } + UnitTest::CheckArraysInheritTupleDims(dataStructure); +} +} // namespace + +TEST_CASE("SimplnxCore::FeatureFaceCurvatureFilter: multi-feature planes", "[SimplnxCore][FeatureFaceCurvatureFilter]") +{ + // Storage-aware array creation and the storage preferences need the application that LoadPlugins() creates. + UnitTest::LoadPlugins(); + const bool optionalOutputs = GENERATE(false, true); + CheckMultiFeatureCurvature(false, optionalOutputs); +} + +TEST_CASE("SimplnxCore::FeatureFaceCurvatureFilter: multi-feature planes with real OOC stores", "[SimplnxCore][FeatureFaceCurvatureFilter][OOC]") +{ + UnitTest::LoadPlugins(); + if(!DataStoreUtilities::GetIOCollection().hasDataStoreCreationFunction("HDF5-OOC")) + { +#if SIMPLNX_TEST_ALGORITHM_PATH == 1 + FAIL("The OOC-only build must register HDF5-OOC storage."); +#else + WARN("Real-store body requires an OOC-enabled build."); + return; +#endif + } + const bool optionalOutputs = GENERATE(false, true); + CheckMultiFeatureCurvature(true, optionalOutputs); +} diff --git a/src/simplnx/Utilities/ImageIO/IImageIO.hpp b/src/simplnx/Utilities/ImageIO/IImageIO.hpp index 08fc943de3..cfd750120d 100644 --- a/src/simplnx/Utilities/ImageIO/IImageIO.hpp +++ b/src/simplnx/Utilities/ImageIO/IImageIO.hpp @@ -4,12 +4,14 @@ #include "simplnx/Common/Result.hpp" #include "simplnx/Common/Types.hpp" +#include "simplnx/Common/TypesUtility.hpp" #include "simplnx/Utilities/ImageIO/ImageMetadata.hpp" #include #include #include #include +#include namespace nx::core { @@ -19,8 +21,8 @@ namespace nx::core * @brief Abstract interface for reading and writing 2D image files. * * Implementations use stb for PNG, JPEG, and BMP or libtiff for TIFF. This - * interface has no DataStore or geometry dependency. Pixel methods operate on - * the first image page. + * interface has no DataStore or geometry dependency. Pixel methods read one + * page, selected by its page index (single-image formats have only page 0). * * Buffers use packed, top-to-bottom row-major order. Each pixel contains * numComponents consecutive values. Each value uses the size of dataType. @@ -28,6 +30,29 @@ namespace nx::core class SIMPLNX_EXPORT IImageIO { public: + /** + * @brief Kind of number stored in each sample of a page, independent of the file format. + */ + enum class SampleFormat : uint8 + { + Unsigned, ///< Unsigned integer + Signed, ///< Signed integer + Float, ///< IEEE floating point + Unknown ///< Any other sample format that the backend reports + }; + + /** + * @brief Size and stored sample layout of one page: width, height, component count, bits per sample and sample format. + */ + struct PageMetadata + { + usize width = 0; + usize height = 0; + usize numComponents = 0; + usize bitsPerSample = 0; + SampleFormat sampleFormat = SampleFormat::Unsigned; + }; + /** * @typedef ReadRowCallback * @brief Receives one contiguous segment of a decoded image row. @@ -54,6 +79,41 @@ class SIMPLNX_EXPORT IImageIO */ virtual Result readMetadata(const std::filesystem::path& filePath) const = 0; + /** + * @brief Reads the layout of every logical page without decoding pixels. + * + * The default implementation is for single-page backends: it returns one layout built from readMetadata(). + * Multi-page backends override it. + * @param filePath Identifies the image file. + * @return Layouts in readPixelDataRows() page order, or a metadata error. + */ + virtual Result> readPageLayouts(const std::filesystem::path& filePath) const + { + auto result = readMetadata(filePath); + if(result.invalid()) + { + return ConvertResultTo>(ConvertResult(std::move(result)), {}); + } + const auto& metadata = result.value(); + SampleFormat format = SampleFormat::Unsigned; + switch(metadata.dataType) + { + case DataType::int8: + case DataType::int16: + case DataType::int32: + case DataType::int64: + format = SampleFormat::Signed; + break; + case DataType::float32: + case DataType::float64: + format = SampleFormat::Float; + break; + default: + break; + } + return {std::vector{{metadata.width, metadata.height, metadata.numComponents, GetDataTypeSize(metadata.dataType) * 8, format}}}; + } + /** * @brief Reads pixel data for a single page/directory into a caller-owned byte buffer. * diff --git a/src/simplnx/Utilities/ImageIO/TiffImageIO.cpp b/src/simplnx/Utilities/ImageIO/TiffImageIO.cpp index 377575043a..d7779e3b44 100644 --- a/src/simplnx/Utilities/ImageIO/TiffImageIO.cpp +++ b/src/simplnx/Utilities/ImageIO/TiffImageIO.cpp @@ -290,6 +290,24 @@ std::vector CollectPageDirectories(TIFF* tiff) } return pages; } + +/** + * @brief Maps a TIFF SampleFormat tag value to the format-neutral page sample format. + */ +IImageIO::SampleFormat ToPageSampleFormat(uint16_t tiffSampleFormat) +{ + switch(tiffSampleFormat) + { + case SAMPLEFORMAT_UINT: + return IImageIO::SampleFormat::Unsigned; + case SAMPLEFORMAT_INT: + return IImageIO::SampleFormat::Signed; + case SAMPLEFORMAT_IEEEFP: + return IImageIO::SampleFormat::Float; + default: + return IImageIO::SampleFormat::Unknown; + } +} } // namespace Result TiffImageIO::readMetadata(const std::filesystem::path& filePath) const @@ -374,6 +392,49 @@ Result TiffImageIO::readMetadata(const std::filesystem::path& fil return {std::move(metadata)}; } +// ----------------------------------------------------------------------------- +Result> TiffImageIO::readPageLayouts(const std::filesystem::path& filePath) const +{ + const std::string pathStr = filePath.string(); + TiffFile file(pathStr, "r"); + if(!file.valid()) + { + return MakeErrorResult>(k_ErrorOpenFailed, fmt::format("Failed to open TIFF '{}': {}", pathStr, file.errorMessage())); + } + TIFF* tiff = file.get(); + const auto directories = CollectPageDirectories(tiff); + std::vector layouts; + layouts.reserve(directories.size()); + usize directory = 0; + for(usize page = 0; page < directories.size(); ++page) + { + // CollectPageDirectories restores directory 0. Advance only forwards so reading + // layouts does not repeatedly traverse the IFD chain from the beginning. + while(directory < directories[page]) + { + if(TIFFReadDirectory(tiff) == 0) + { + return MakeErrorResult>(k_ErrorReadMetadataFailed, fmt::format("Cannot select page {} of TIFF '{}': {}", page, pathStr, file.errorMessage())); + } + ++directory; + } + nx::core::uint32 width = 0; + nx::core::uint32 height = 0; + nx::core::uint16 components = 1; + nx::core::uint16 bits = 1; + nx::core::uint16 format = SAMPLEFORMAT_UINT; + if(TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width) == 0 || TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height) == 0) + { + return MakeErrorResult>(k_ErrorReadMetadataFailed, fmt::format("Cannot read dimensions of page {} of TIFF '{}': {}", page, pathStr, file.errorMessage())); + } + TIFFGetFieldDefaulted(tiff, TIFFTAG_SAMPLESPERPIXEL, &components); + TIFFGetFieldDefaulted(tiff, TIFFTAG_BITSPERSAMPLE, &bits); + TIFFGetFieldDefaulted(tiff, TIFFTAG_SAMPLEFORMAT, &format); + layouts.push_back({width, height, components, bits, ToPageSampleFormat(format)}); + } + return {std::move(layouts)}; +} + // ----------------------------------------------------------------------------- Result<> TiffImageIO::readPixelData(const std::filesystem::path& filePath, std::span buffer, usize pageIndex) const { diff --git a/src/simplnx/Utilities/ImageIO/TiffImageIO.hpp b/src/simplnx/Utilities/ImageIO/TiffImageIO.hpp index acf65f6589..c849f44a2b 100644 --- a/src/simplnx/Utilities/ImageIO/TiffImageIO.hpp +++ b/src/simplnx/Utilities/ImageIO/TiffImageIO.hpp @@ -27,6 +27,7 @@ class SIMPLNX_EXPORT TiffImageIO : public IImageIO ~TiffImageIO() noexcept override = default; Result readMetadata(const std::filesystem::path& filePath) const override; + Result> readPageLayouts(const std::filesystem::path& filePath) const override; /** * @brief Reads the requested TIFF page into the destination buffer. */ diff --git a/src/simplnx/Utilities/ImageProcessing/MorphologyEngine.hpp b/src/simplnx/Utilities/ImageProcessing/MorphologyEngine.hpp index 8c74def14b..dea1422ee4 100644 --- a/src/simplnx/Utilities/ImageProcessing/MorphologyEngine.hpp +++ b/src/simplnx/Utilities/ImageProcessing/MorphologyEngine.hpp @@ -150,6 +150,19 @@ inline bool MorphologyFlatFoldIsRepresentable(const SizeVec3& dims, const Struct return true; } +// A single-slice image has no Z neighbors. Normalize before testing for an empty kernel +// so each operation retains its existing empty-kernel result. Leave 3D kernels unchanged. +inline std::optional MakeInPlaneMorphologyKernel(const SizeVec3& dims, const StructuringElement& se) +{ + if(dims[2] != 1 || std::none_of(se.offsets.begin(), se.offsets.end(), [](const SEOffset& offset) { return offset.dz != 0; })) + { + return std::nullopt; + } + StructuringElement inPlaneSE = se; + std::erase_if(inPlaneSE.offsets, [](const SEOffset& offset) { return offset.dz != 0; }); + return inPlaneSE; +} + inline bool ShouldUseMorphologyResidentFlatFold(const SizeVec3& dims, const StructuringElement& se) { // Larger kernels use the moving histogram, whose work scales with the entering and leaving boundary. @@ -3176,6 +3189,11 @@ class MorphScanline return layout; } + if(auto inPlaneSE = detail::MakeInPlaneMorphologyKernel(m_Dims, m_SE); inPlaneSE.has_value()) + { + return MorphScanline{m_In, m_Out, m_Dims, *inPlaneSE, m_Op, m_ShouldCancel, m_MessageHandler, m_TargetBufferBytes}(); + } + const usize dimX = m_Dims[0]; const usize dimY = m_Dims[1]; const usize dimZ = m_Dims[2]; @@ -3421,6 +3439,11 @@ class MorphDirect return layout; } + if(auto inPlaneSE = detail::MakeInPlaneMorphologyKernel(m_Dims, m_SE); inPlaneSE.has_value()) + { + return MorphDirect{m_In, m_Out, m_Dims, *inPlaneSE, m_Op, m_ShouldCancel, m_MessageHandler}(); + } + // Dispatch the runtime op to a compile-time bool so the histogram's comparator/extremum are fixed at // compile time (matching ITK's distinct Dilate/Erode filter classes). return (m_Op == MorphOp::Dilate) ? runImpl() : runImpl(); @@ -3843,6 +3866,10 @@ Result<> ApplyMorphologyCompositePipeline(const AbstractDataStore& input, Abs { return {}; } + if(auto inPlaneSE = detail::MakeInPlaneMorphologyKernel(dims, se); inPlaneSE.has_value()) + { + return ApplyMorphologyCompositePipeline(input, output, dims, *inPlaneSE, firstOp, secondOp, safeBorder, outputMode, shouldCancel, grantedWorkingMemoryBytes); + } auto requiredBytesResult = CalculateMorphologyCompositePipelineWorkingMemoryBytes(dims, se, safeBorder); if(requiredBytesResult.invalid()) { @@ -4272,6 +4299,11 @@ class MorphGradientScanline return layout; } + if(auto inPlaneSE = detail::MakeInPlaneMorphologyKernel(m_Dims, m_SE); inPlaneSE.has_value()) + { + return MorphGradientScanline{m_In, m_Out, m_Dims, *inPlaneSE, m_ShouldCancel, m_MessageHandler, m_TargetBufferBytes}(); + } + const usize dimX = m_Dims[0]; const usize dimY = m_Dims[1]; const usize dimZ = m_Dims[2]; @@ -4510,6 +4542,11 @@ class MorphGradientDirect return layout; } + if(auto inPlaneSE = detail::MakeInPlaneMorphologyKernel(m_Dims, m_SE); inPlaneSE.has_value()) + { + return MorphGradientDirect{m_In, m_Out, m_Dims, *inPlaneSE, m_ShouldCancel, m_MessageHandler}(); + } + const usize dimX = m_Dims[0]; const usize dimY = m_Dims[1]; const usize dimZ = m_Dims[2]; @@ -5681,11 +5718,9 @@ Result<> ApplyBinaryMorphologyCompositePipeline(const AbstractDataStore& inpu } // A single-slice image has no Z neighbors. Drop out-of-plane offsets like the other binary routes. // If none remain, the existing empty-kernel branches copy the input. - if(dims[2] == 1 && std::any_of(se.offsets.begin(), se.offsets.end(), [](const SEOffset& offset) { return offset.dz != 0; })) + if(auto inPlaneSE = detail::MakeInPlaneMorphologyKernel(dims, se); inPlaneSE.has_value()) { - StructuringElement inPlaneSE = se; - std::erase_if(inPlaneSE.offsets, [](const SEOffset& offset) { return offset.dz != 0; }); - return ApplyBinaryMorphologyCompositePipeline(input, output, dims, inPlaneSE, firstOp, secondOp, safeBorder, foreground, background, shouldCancel, grantedWorkingMemoryBytes); + return ApplyBinaryMorphologyCompositePipeline(input, output, dims, *inPlaneSE, firstOp, secondOp, safeBorder, foreground, background, shouldCancel, grantedWorkingMemoryBytes); } auto requiredBytesResult = CalculateBinaryMorphologyCompositePipelineWorkingMemoryBytes(dims, se, safeBorder); if(requiredBytesResult.invalid()) diff --git a/test/TiffImageIOTest.cpp b/test/TiffImageIOTest.cpp index bd16bc77c1..a55d76941b 100644 --- a/test/TiffImageIOTest.cpp +++ b/test/TiffImageIOTest.cpp @@ -1,5 +1,6 @@ #include "simplnx/Utilities/ImageIO/TiffImageIO.hpp" #include "simplnx/UnitTest/UnitTestCommon.hpp" +#include "simplnx/Utilities/ImageIO/StbImageIO.hpp" #include "simplnx/unit_test/simplnx_test_dirs.hpp" @@ -11,7 +12,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -432,3 +435,157 @@ TEST_CASE("TiffImageIO:: direct read rejects a short buffer without mutation", " REQUIRE(result.errors().front().code == -20105); REQUIRE(std::all_of(poison.cbegin(), poison.cend(), [](uint8_t value) { return value == 0xCD; })); } + +namespace +{ +// Appends one stripped page whose samples are firstValue, firstValue + 1, ... in row-major order. An empty +// sampleFormat leaves the tag out (TIFF then defaults to unsigned); an empty subfileType leaves that tag out. +template +void WriteLayoutDirectory(TIFF* tiff, uint32_t width, uint32_t height, uint16_t components, std::optional sampleFormat, std::optional subfileType, T firstValue) +{ + REQUIRE(TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, width) == 1); + REQUIRE(TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, height) == 1); + REQUIRE(TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, components) == 1); + REQUIRE(TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, static_cast(sizeof(T) * 8)) == 1); + if(sampleFormat.has_value()) + { + REQUIRE(TIFFSetField(tiff, TIFFTAG_SAMPLEFORMAT, *sampleFormat) == 1); + } + if(subfileType.has_value()) + { + REQUIRE(TIFFSetField(tiff, TIFFTAG_SUBFILETYPE, *subfileType) == 1); + } + REQUIRE(TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG) == 1); + REQUIRE(TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, components == 3 ? PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK) == 1); + REQUIRE(TIFFSetField(tiff, TIFFTAG_COMPRESSION, COMPRESSION_NONE) == 1); + std::vector row(static_cast(width) * components); + for(uint32_t y = 0; y < height; ++y) + { + for(usize i = 0; i < row.size(); ++i) + { + row[i] = static_cast(firstValue + static_cast(y * row.size() + i)); + } + REQUIRE(TIFFWriteScanline(tiff, row.data(), y, 0) == 1); + } + REQUIRE(TIFFWriteDirectory(tiff) == 1); +} + +void RequireLayout(const IImageIO::PageMetadata& layout, usize width, usize height, usize components, usize bits, IImageIO::SampleFormat format) +{ + CHECK(layout.width == width); + CHECK(layout.height == height); + CHECK(layout.numComponents == components); + CHECK(layout.bitsPerSample == bits); + CHECK(layout.sampleFormat == format); +} +} // namespace + +TEST_CASE("TiffImageIO:: readPageLayouts returns each page in order", "[simplnx][TiffImageIO]") +{ + TemporaryTiffFile file("TiffImageIO_page_layouts.tif"); + { + TiffHandle tiff(TIFFOpen(file.path().string().c_str(), "w"), TIFFClose); + REQUIRE(tiff != nullptr); + WriteLayoutDirectory(tiff.get(), 2, 3, 1, std::nullopt, std::nullopt, 1); // no SampleFormat tag + WriteLayoutDirectory(tiff.get(), 4, 1, 1, SAMPLEFORMAT_UINT, std::nullopt, 10); // explicit unsigned + WriteLayoutDirectory(tiff.get(), 1, 2, 1, SAMPLEFORMAT_INT, std::nullopt, -5); + WriteLayoutDirectory(tiff.get(), 3, 2, 1, SAMPLEFORMAT_IEEEFP, std::nullopt, 0.5F); + WriteLayoutDirectory(tiff.get(), 2, 2, 3, SAMPLEFORMAT_VOID, std::nullopt, 7); + } + + TiffImageIO imageIO; + const auto result = imageIO.readPageLayouts(file.path()); + SIMPLNX_RESULT_REQUIRE_VALID(result); + const auto& layouts = result.value(); + REQUIRE(layouts.size() == 5); + RequireLayout(layouts[0], 2, 3, 1, 8, IImageIO::SampleFormat::Unsigned); // TIFF default sample format + RequireLayout(layouts[1], 4, 1, 1, 16, IImageIO::SampleFormat::Unsigned); + RequireLayout(layouts[2], 1, 2, 1, 16, IImageIO::SampleFormat::Signed); + RequireLayout(layouts[3], 3, 2, 1, 32, IImageIO::SampleFormat::Float); + RequireLayout(layouts[4], 2, 2, 3, 8, IImageIO::SampleFormat::Unknown); +} + +TEST_CASE("TiffImageIO:: readPageLayouts skips the same subfiles as readPixelDataRows", "[simplnx][TiffImageIO]") +{ + // Directories: 0 page, 1 reduced-resolution image, 2 transparency mask, 3 page. Only directories 0 and 3 are pages. + TemporaryTiffFile file("TiffImageIO_page_layouts_subfiles.tif"); + { + TiffHandle tiff(TIFFOpen(file.path().string().c_str(), "w"), TIFFClose); + REQUIRE(tiff != nullptr); + WriteLayoutDirectory(tiff.get(), 2, 2, 1, SAMPLEFORMAT_UINT, 0, 100); + WriteLayoutDirectory(tiff.get(), 1, 1, 3, SAMPLEFORMAT_UINT, FILETYPE_REDUCEDIMAGE, 1); + WriteLayoutDirectory(tiff.get(), 2, 2, 1, SAMPLEFORMAT_UINT, FILETYPE_MASK, 2); + WriteLayoutDirectory(tiff.get(), 2, 2, 1, SAMPLEFORMAT_UINT, 0, 200); + } + + TiffImageIO imageIO; + const auto metadata = imageIO.readMetadata(file.path()); + SIMPLNX_RESULT_REQUIRE_VALID(metadata); + REQUIRE(metadata.value().numPages == 2); + const auto result = imageIO.readPageLayouts(file.path()); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.value().size() == metadata.value().numPages); + RequireLayout(result.value()[0], 2, 2, 1, 16, IImageIO::SampleFormat::Unsigned); + RequireLayout(result.value()[1], 2, 2, 1, 16, IImageIO::SampleFormat::Unsigned); + + // readPixelDataRows maps page 1 to directory 3: its first sample is 200, not a value of the skipped subfiles. + std::vector page1; + const auto readResult = imageIO.readPixelDataRows( + file.path(), + [&page1](usize, usize, usize, std::span segment) -> Result<> { + const usize start = page1.size(); + page1.resize(start + segment.size() / sizeof(uint16_t)); + std::memcpy(page1.data() + start, segment.data(), segment.size()); + return {}; + }, + 1); + SIMPLNX_RESULT_REQUIRE_VALID(readResult); + REQUIRE(page1 == std::vector{200, 201, 202, 203}); +} + +TEST_CASE("TiffImageIO:: readPageLayouts reports a file that cannot be read", "[simplnx][TiffImageIO]") +{ + TiffImageIO imageIO; + SECTION("Missing file") + { + const fs::path path = fs::path(unit_test::k_BinaryTestOutputDir.view()) / "TiffImageIO_page_layouts_missing.tif"; + std::error_code errorCode; + fs::remove(path, errorCode); + const auto result = imageIO.readPageLayouts(path); + REQUIRE(result.invalid()); + REQUIRE(result.errors().front().code == -20100); + REQUIRE(result.errors().front().message.find(path.string()) != std::string::npos); + } + SECTION("File that is not a TIFF") + { + TemporaryTiffFile file("TiffImageIO_page_layouts_not_a_tiff.tif"); + { + std::ofstream stream(file.path(), std::ios::binary); + stream << "This is not a TIFF file."; + } + const auto result = imageIO.readPageLayouts(file.path()); + REQUIRE(result.invalid()); + } +} + +TEST_CASE("TiffImageIO:: IImageIO default readPageLayouts returns the single page", "[simplnx][TiffImageIO]") +{ + // StbImageIO has no override, so it uses the IImageIO default built from readMetadata(). + const fs::path path = fs::path(unit_test::k_BinaryTestOutputDir.view()) / "IImageIO_default_page_layout.png"; + std::error_code errorCode; + fs::remove(path, errorCode); + StbImageIO imageIO; + ImageMetadata metadata; + metadata.width = 3; + metadata.height = 2; + metadata.numComponents = 3; + metadata.dataType = DataType::uint8; + const std::vector pixels(18, 42); + const auto writeResult = imageIO.writePixelData(path, pixels, metadata); + SIMPLNX_RESULT_REQUIRE_VALID(writeResult); + const auto result = imageIO.readPageLayouts(path); + fs::remove(path, errorCode); + SIMPLNX_RESULT_REQUIRE_VALID(result); + REQUIRE(result.value().size() == 1); + RequireLayout(result.value()[0], 3, 2, 3, 8, IImageIO::SampleFormat::Unsigned); +}