Skip to content

Commit c54f1b1

Browse files
committed
BUG: GenerateImageSource sets Size from ReferenceImage
We want the same behavior as SetOutputParametersFromImage. Without this, a crash occurs on Update().
1 parent 087a1c4 commit c54f1b1

4 files changed

Lines changed: 85 additions & 0 deletions

File tree

Modules/Filtering/ImageSources/include/itkGenerateImageSource.hxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ GenerateImageSource<TOutputImage>::GenerateOutputInformation()
6363
outputPtr->SetSpacing(referenceImage->GetSpacing());
6464
outputPtr->SetOrigin(referenceImage->GetOrigin());
6565
outputPtr->SetDirection(referenceImage->GetDirection());
66+
this->m_Direction = referenceImage->GetDirection();
67+
this->m_StartIndex = referenceImage->GetLargestPossibleRegion().GetIndex();
68+
this->m_Size = referenceImage->GetLargestPossibleRegion().GetSize();
6669
}
6770
else
6871
{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bafkreidpb356aruiokudkoxyo47ol4nyesw3hywpfd4dbvigbixmcebmwe

Modules/Filtering/ImageSources/test/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(ITKImageSourcesTests
44
itkGaborImageSourceTest.cxx
55
itkGaussianImageSourceTest.cxx
66
itkGridImageSourceTest.cxx
7+
itkGridImageSourceTest2.cxx
78
itkPhysicalPointImageSourceTest.cxx)
89

910
createtestdriver(ITKImageSources "${ITKImageSources-Test_LIBRARIES}" "${ITKImageSourcesTests}")
@@ -138,6 +139,18 @@ itk_add_test(
138139
0
139140
0
140141
0)
142+
itk_add_test(
143+
NAME
144+
itkGridImageSourceTest5
145+
COMMAND
146+
ITKImageSourcesTestDriver
147+
--compare
148+
DATA{Baseline/itkGridImageSourceTest5.mha}
149+
${ITK_TEST_OUTPUT_DIR}/itkGridImageSourceTest5.mha
150+
itkGridImageSourceTest2
151+
DATA{${ITK_DATA_ROOT}/Input/cthead1.png}
152+
${ITK_TEST_OUTPUT_DIR}/itkGridImageSourceTest5.mha
153+
)
141154
itk_add_test(
142155
NAME
143156
itkPhysicalPointImageSourceTest0
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
#include "itkGridImageSource.h"
19+
#include "itkImageFileReader.h"
20+
#include "itkImageFileWriter.h"
21+
#include "itkSimpleFilterWatcher.h"
22+
#include "itkTestingMacros.h"
23+
24+
25+
int
26+
itkGridImageSourceTest2(int argc, char * argv[])
27+
{
28+
if (argc != 3)
29+
{
30+
std::cout << "Usage: " << itkNameOfTestExecutableMacro(argv)
31+
<< " inputImage"
32+
<< " outputImage" << std::endl;
33+
return EXIT_FAILURE;
34+
}
35+
const char * inputImageFile = argv[1];
36+
const char * outputImageFile = argv[2];
37+
38+
39+
constexpr unsigned int ImageDimension = 3;
40+
using PixelType = uint8_t;
41+
42+
using ImageType = itk::Image<PixelType, ImageDimension>;
43+
44+
// Instantiate the filter
45+
using GridSourceType = itk::GridImageSource<ImageType>;
46+
auto gridImage = GridSourceType::New();
47+
48+
ITK_EXERCISE_BASIC_OBJECT_METHODS(gridImage, GridImageSource, GenerateImageSource);
49+
50+
ImageType::Pointer inputImage = itk::ReadImage<ImageType>(inputImageFile);
51+
52+
gridImage->SetReferenceImage(inputImage);
53+
gridImage->UseReferenceImageOn();
54+
55+
itk::SimpleFilterWatcher watcher(gridImage, "GridImageSource");
56+
57+
ITK_TRY_EXPECT_NO_EXCEPTION(gridImage->Update());
58+
59+
using WriterType = itk::ImageFileWriter<ImageType>;
60+
auto writer = WriterType::New();
61+
writer->SetFileName(outputImageFile);
62+
writer->SetInput(gridImage->GetOutput());
63+
64+
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());
65+
66+
std::cout << "Test finished" << std::endl;
67+
return EXIT_SUCCESS;
68+
}

0 commit comments

Comments
 (0)