|
| 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