Skip to content

Commit

Permalink
BUG: Fix ImageRegionSplitters with zero sized image
Browse files Browse the repository at this point in the history
When the image had a zero size, undefined results of division by zero
occoured. For both the ImageRegionSplitterSlowDimension and the
ImageRegionSplitterDirection classes when a region's size is zero, the
region has one "split" region of a zero sized region.
  • Loading branch information
blowekamp committed Sep 21, 2021
1 parent 0539a2c commit 63f4ab7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Modules/Core/Common/src/itkImageRegionSplitterDirection.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ ImageRegionSplitterDirection::GetNumberOfSplitsInternal(unsigned int dim
const SizeValueType regionSize[],
unsigned int requestedNumber) const
{
requestedNumber = std::max(1u, requestedNumber);
// split on the outermost dimension available
int splitAxis = dim - 1;
while (regionSize[splitAxis] == 1 || splitAxis == (int)m_Direction)
while (regionSize[splitAxis] <= 1 || splitAxis == (int)m_Direction)
{
--splitAxis;
if (splitAxis < 0)
Expand Down Expand Up @@ -71,7 +72,7 @@ ImageRegionSplitterDirection::GetSplitInternal(unsigned int dim,
// split on the outermost dimension available
// and avoid the current dimension
int splitAxis = dim - 1;
while (regionSize[splitAxis] == 1 || splitAxis == (int)m_Direction)
while (regionSize[splitAxis] <= 1 || splitAxis == (int)m_Direction)
{
--splitAxis;
if (splitAxis < 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ ImageRegionSplitterSlowDimension::GetNumberOfSplitsInternal(unsigned int
const SizeValueType regionSize[],
unsigned int requestedNumber) const
{
requestedNumber = std::max(1u, requestedNumber);
// split on the outermost dimension available
int splitAxis = dim - 1;
while (regionSize[splitAxis] == 1)
while (regionSize[splitAxis] <= 1)
{
--splitAxis;
if (splitAxis < 0)
Expand All @@ -57,10 +58,9 @@ ImageRegionSplitterSlowDimension::GetSplitInternal(unsigned int dim,
IndexValueType regionIndex[],
SizeValueType regionSize[]) const
{

// split on the outermost dimension available
unsigned int splitAxis = dim - 1;
while (regionSize[splitAxis] == 1)
while (regionSize[splitAxis] <= 1)
{
if (splitAxis == 0)
{ // cannot split
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ itkImageRegionSplitterDirectionTest(int, char *[])

const itk::ImageRegion<2> lpRegion = region;

ITK_TEST_EXPECT_EQUAL(splitter->GetNumberOfSplits(lpRegion, 0), 1);
ITK_TEST_EXPECT_EQUAL(splitter->GetNumberOfSplits(lpRegion, 1), 1);
ITK_TEST_EXPECT_EQUAL(splitter->GetNumberOfSplits(lpRegion, 2), 2);
ITK_TEST_EXPECT_EQUAL(splitter->GetNumberOfSplits(lpRegion, 3), 3);
Expand Down Expand Up @@ -66,6 +67,13 @@ itkImageRegionSplitterDirectionTest(int, char *[])
ITK_TEST_EXPECT_EQUAL(region.GetSize(0), 5);
ITK_TEST_EXPECT_EQUAL(region.GetSize(1), 11);

const itk::ImageRegion<2> lpRegion2{}; // default zero sized
region = lpRegion2;
ITK_TEST_EXPECT_EQUAL(1, splitter->GetNumberOfSplits(lpRegion2, 1));
ITK_TEST_EXPECT_EQUAL(1, splitter->GetSplit(1, 1, region));
ITK_TEST_EXPECT_EQUAL(region.GetSize(0), 0);
ITK_TEST_EXPECT_EQUAL(region.GetSize(1), 0);


return EXIT_SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ itkImageRegionSplitterSlowDimensionTest(int, char *[])
region.SetIndex(1, 10);

const itk::ImageRegion<2> lpRegion = region;

ITK_TEST_EXPECT_EQUAL(splitter->GetNumberOfSplits(lpRegion, 0), 1);
ITK_TEST_EXPECT_EQUAL(splitter->GetNumberOfSplits(lpRegion, 1), 1);
ITK_TEST_EXPECT_EQUAL(splitter->GetNumberOfSplits(lpRegion, 2), 2);
ITK_TEST_EXPECT_EQUAL(splitter->GetNumberOfSplits(lpRegion, 3), 3);
Expand All @@ -58,6 +58,12 @@ itkImageRegionSplitterSlowDimensionTest(int, char *[])
ITK_TEST_EXPECT_EQUAL(region.GetSize(0), 10);
ITK_TEST_EXPECT_EQUAL(region.GetSize(1), 5);

const itk::ImageRegion<2> lpRegion2{}; // default zero sized
region = lpRegion2;
ITK_TEST_EXPECT_EQUAL(1, splitter->GetNumberOfSplits(lpRegion2, 1));
ITK_TEST_EXPECT_EQUAL(1, splitter->GetSplit(1, 1, region));
ITK_TEST_EXPECT_EQUAL(region.GetSize(0), 0);
ITK_TEST_EXPECT_EQUAL(region.GetSize(1), 0);

return EXIT_SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@
#include "itkLabelImageToLabelMapFilter.h"
#include "itkTestingMacros.h"

void
zeroSizeCase()
{
// test filter with zero sized image
auto p_filter = itk::LabelImageToLabelMapFilter<itk::Image<unsigned char, 3>>::New();
auto p_image = itk::Image<unsigned char, 3>::New();
p_image->SetRegions(itk::Size<3>{ 0, 0, 0 });
p_image->Allocate(true);

p_filter->SetInput(p_image);
p_filter->Update();
}

int
itkLabelImageToLabelMapFilterTest(int argc, char * argv[])
{
Expand Down Expand Up @@ -131,5 +144,8 @@ itkLabelImageToLabelMapFilterTest(int argc, char * argv[])

conversion->Print(std::cout);

zeroSizeCase();


return EXIT_SUCCESS;
}

0 comments on commit 63f4ab7

Please sign in to comment.