Skip to content

Commit 857b3c8

Browse files
hjmjohnsonclaude
andcommitted
ENH: Convert itkImportImageTest to GTest
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ab4cf97 commit 857b3c8

2 files changed

Lines changed: 98 additions & 7 deletions

File tree

Modules/Core/Common/test/CMakeLists.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ set(
1414
itkImageIteratorsForwardBackwardTest.cxx
1515
itkImageLinearIteratorTest.cxx
1616
itkImageAdaptorPipeLineTest.cxx
17-
itkImportImageTest.cxx
1817
itkImageRandomIteratorTest.cxx
1918
itkImageRandomIteratorTest2.cxx
2019
itkImageSliceIteratorTest.cxx
@@ -222,12 +221,6 @@ itk_add_test(
222221
ITKCommon1TestDriver
223222
itkImageAdaptorPipeLineTest
224223
)
225-
itk_add_test(
226-
NAME itkImportImageTest
227-
COMMAND
228-
ITKCommon1TestDriver
229-
itkImportImageTest
230-
)
231224
itk_add_test(
232225
NAME itkExtractImage3Dto2DTest
233226
COMMAND
@@ -1612,6 +1605,7 @@ set(
16121605
itkThreadedImageRegionPartitionerGTest.cxx
16131606
itkThreadedIndexedContainerPartitionerGTest.cxx
16141607
itkImageTransformGTest.cxx
1608+
itkImportImageGTest.cxx
16151609
)
16161610
creategoogletestdriver(ITKCommon "${ITKCommon-Test_LIBRARIES}" "${ITKCommonGTests}")
16171611
# If `-static` was passed to CMAKE_EXE_LINKER_FLAGS, compilation fails. No need to
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*=========================================================================
2+
*
3+
* Copyright NumFOCUS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0.txt
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*=========================================================================*/
18+
19+
#include "itkImageRegionIterator.h"
20+
#include "itkShrinkImageFilter.h"
21+
#include "itkImportImageFilter.h"
22+
#include "itkGTest.h"
23+
#include "itkMath.h"
24+
25+
#include <iostream>
26+
27+
28+
TEST(ImportImageFilter, ImportAndShrink)
29+
{
30+
// Create a C-array to hold an image
31+
auto * rawImage = new short[8 * 12];
32+
for (unsigned int i = 0; i < 8 * 12; ++i)
33+
{
34+
rawImage[i] = static_cast<short>(i);
35+
}
36+
37+
constexpr unsigned int Dimension{ 2 };
38+
using PixelType = short;
39+
40+
using ImportImageFilter = itk::ImportImageFilter<PixelType, Dimension>;
41+
using ShortImage = itk::Image<PixelType, Dimension>;
42+
43+
// Test basic object methods
44+
auto basicImport = ImportImageFilter::New();
45+
ITK_GTEST_EXERCISE_BASIC_OBJECT_METHODS(basicImport, ImportImageFilter, ImageSource);
46+
47+
ShortImage::Pointer image;
48+
constexpr itk::ImageRegion<Dimension>::SizeType size = { { 8, 12 } };
49+
itk::ImageRegion<Dimension> region = { size };
50+
51+
{
52+
const ImportImageFilter::Pointer import = ImportImageFilter::New();
53+
54+
constexpr itk::SpacePrecisionType data[2]{ 1.0, 1.0 };
55+
import->SetSpacing(data);
56+
57+
constexpr float data2[2]{ 1.0, 1.0 };
58+
import->SetSpacing(data2);
59+
60+
const itk::SpacePrecisionType * spacingValue = import->GetSpacing().GetDataPointer();
61+
std::cout << "import->GetSpacing(): " << spacingValue << std::endl;
62+
63+
constexpr double data3[2]{ 1.0, 1.0 };
64+
import->SetOrigin(data3);
65+
66+
constexpr float data4[2]{ 1.0, 1.0 };
67+
import->SetOrigin(data4);
68+
69+
const itk::SpacePrecisionType * originValue = import->GetOrigin().GetDataPointer();
70+
std::cout << "import->GetOrigin(): " << originValue << std::endl;
71+
72+
import->SetRegion(region);
73+
import->SetImportPointer(rawImage, 8 * 12, true);
74+
import->Update();
75+
image = import->GetOutput();
76+
}
77+
78+
const itk::ShrinkImageFilter<ImportImageFilter::OutputImageType, ShortImage>::Pointer shrink =
79+
itk::ShrinkImageFilter<ImportImageFilter::OutputImageType, ShortImage>::New();
80+
81+
shrink->SetInput(image);
82+
shrink->SetShrinkFactors(2);
83+
EXPECT_NO_THROW(shrink->Update());
84+
85+
const ShortImage::RegionType requestedRegion = shrink->GetOutput()->GetRequestedRegion();
86+
itk::ImageRegionIterator<ShortImage> iterator2(shrink->GetOutput(), requestedRegion);
87+
88+
for (; !iterator2.IsAtEnd(); ++iterator2)
89+
{
90+
std::cout << "Pixel " << iterator2.ComputeIndex() << " = " << iterator2.Get() << std::endl;
91+
const short expectedValue = itk::Math::RoundHalfIntegerUp<short>(static_cast<float>(
92+
(shrink->GetShrinkFactors()[0] * iterator2.ComputeIndex()[0] + shrink->GetShrinkFactors()[0] / 2) +
93+
(region.GetSize()[0] *
94+
((shrink->GetShrinkFactors()[1] / 2) + (shrink->GetShrinkFactors()[0] * iterator2.ComputeIndex()[1])))));
95+
EXPECT_EQ(iterator2.Get(), expectedValue) << "Pixel mismatch at " << iterator2.ComputeIndex();
96+
}
97+
}

0 commit comments

Comments
 (0)