diff --git a/Modules/Filtering/ImageGrid/test/CMakeLists.txt b/Modules/Filtering/ImageGrid/test/CMakeLists.txt index 541f8cc28a34..b9b57eb36e76 100644 --- a/Modules/Filtering/ImageGrid/test/CMakeLists.txt +++ b/Modules/Filtering/ImageGrid/test/CMakeLists.txt @@ -665,6 +665,7 @@ itk_add_test( set(ITKImageGridGTests itkChangeInformationImageFilterGTest.cxx + itkComposeImageFilterGTest.cxx itkResampleImageFilterGTest.cxx itkSliceImageFilterTest.cxx itkTileImageFilterGTest.cxx diff --git a/Modules/Filtering/ImageGrid/test/itkComposeImageFilterGTest.cxx b/Modules/Filtering/ImageGrid/test/itkComposeImageFilterGTest.cxx new file mode 100644 index 000000000000..19223eb68c82 --- /dev/null +++ b/Modules/Filtering/ImageGrid/test/itkComposeImageFilterGTest.cxx @@ -0,0 +1,104 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ + +// The header file to be tested: +#include "itkComposeImageFilter.h" +#include "itkVectorIndexSelectionCastImageFilter.h" +#include "itkMinimumMaximumImageCalculator.h" +#include "itkExtractImageFilter.h" +#include "itkPasteImageFilter.h" + + +#include "itkImage.h" + +// Google Test header file: +#include + +// Standard C++ header files: +#include +#include + + +TEST(ComposeImageFilter, simpleitk_2220) +{ + // Test case for SimpleITK issue #2220 + // Reported data corruption with 4G compose on Windows system + + + const unsigned int size = 400; + const unsigned int nchannels = 100; + using ImageType = itk::Image; + using VectorImageType = itk::VectorImage; + std::vector images; + + for (unsigned int i = 0; i < nchannels; ++i) + { + auto image = ImageType::New(); + ImageType::RegionType region; + ImageType::SizeType imageSize = { { size, size, size } }; + region.SetSize(imageSize); + image->SetRegions(region); + image->Allocate(); + image->FillBuffer(i % 250); + + images.push_back(image); + } + + using ComposeFilterType = itk::ComposeImageFilter; + ComposeFilterType::Pointer composeFilter = ComposeFilterType::New(); + for (unsigned int i = 0; i < nchannels; ++i) + { + composeFilter->SetInput(i, images[i]); + } + + composeFilter->Update(); + VectorImageType::Pointer img = composeFilter->GetOutput(); + std::cout << "Compose filter executed." << std::endl; + + for (unsigned int i = 0; i < nchannels; ++i) + { + itk::IndexValueType z = i; + std::map values; + + for (itk::IndexValueType y = 0; y < size; ++y) + { + for (itk::IndexValueType x = 0; x < size; ++x) + { + itk::Index<3> idx = { { x, y, z } }; + + auto pixel = img->GetPixel(idx); + if (pixel[i] != i % 250) + { + ++values[pixel[i]]; + } + } + } + + EXPECT_TRUE(values.empty()) << "channel " << i << " has incorrect values;" << std::endl; + + if (!values.empty()) + { + std::cout << "values: "; + for (auto v : values) + { + std::cout << v.first << "(" << v.second << "), "; + } + std::cout << std::endl; + } + } +}