Skip to content

Commit

Permalink
Merge pull request InsightSoftwareConsortium#4479 from N-Dekker/Alloc…
Browse files Browse the repository at this point in the history
…ateZeroInitializedPixelBuffer
  • Loading branch information
dzenanz authored Feb 27, 2024
2 parents 8f64fdb + 527843e commit 1b01d5c
Show file tree
Hide file tree
Showing 75 changed files with 111 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ FloodFilledFunctionConditionalConstIterator<TImage, TFunction>::InitializeIterat
typename TTempImage::RegionType tempRegion = this->m_Image->GetBufferedRegion();

m_TemporaryPointer->SetRegions(tempRegion);
m_TemporaryPointer->Allocate(true); // initialize buffer to zero
m_TemporaryPointer->AllocateInitialized();

// Initialize the queue by adding the start index assuming one of
// the m_Seeds is "inside" This might not be true, in which
Expand Down
8 changes: 8 additions & 0 deletions Modules/Core/Common/include/itkImageBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ class ITK_TEMPLATE_EXPORT ImageBase : public DataObject
virtual void
Allocate(bool initialize = false);

/** Allocates the pixel buffer of the image, zero-initializing its pixels. `AllocateInitialized()` is equivalent to
* `Allocate(true)`. It is just intended to make the code more readable. */
void
AllocateInitialized()
{
return this->Allocate(true);
}

/** Set the region object that defines the size and starting index
* for the largest possible region this image could represent. This
* is used in determining how much memory would be needed to load an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ ShapedFloodFilledFunctionConditionalConstIterator<TImage, TFunction>::Initialize
typename TTempImage::RegionType tempRegion = this->m_Image->GetBufferedRegion();

m_TempPtr->SetRegions(tempRegion);
m_TempPtr->Allocate(true); // initialize buffer to zero
m_TempPtr->AllocateInitialized();

// Initialize the queue by adding the start index assuming one of
// the m_Seeds is "inside" This might not be true, in which
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/test/itkAdaptorComparisonTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ itkAdaptorComparisonTest(int, char *[])
auto vector_image = VectorImageType::New();

scalar_image->SetRegions(region);
scalar_image->Allocate(true); // initialize buffer to zero
scalar_image->AllocateInitialized();

vector_image->SetRegions(region);
vector_image->Allocate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ itkBSplineInterpolationWeightFunctionTest(int, char *[])
region.SetSize(size);

image->SetRegions(region);
image->Allocate(true); // initialize buffer to zero
image->AllocateInitialized();

using IteratorType = itk::ImageRegionConstIteratorWithIndex<ImageType>;
IteratorType iter(image, image->GetBufferedRegion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ TEST(ConnectedImageNeighborhoodShape, SupportsConstShapedNeighborhoodIterator)
SizeType imageSize;
imageSize.Fill(1);
image->SetRegions(imageSize);
image->Allocate(true);
image->AllocateInitialized();

// Create a radius, (just) large enough for all offsets activated below here.
SizeType radius;
Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/Common/test/itkImageAlgorithmCopyTest2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ itkImageAlgorithmCopyTest2(int, char *[])

auto image2 = Short3DImageType::New();
image2->SetRegions(region);
image2->Allocate(true); // initialize buffer to zero
image2->AllocateInitialized();


auto image3 = Float3DImageType::New();
image3->SetRegions(region);
image3->Allocate(true); // initialize buffer to zero
image3->AllocateInitialized();

std::cout << "Copying two images of same type" << std::endl;
itk::ImageAlgorithm::Copy(image1.GetPointer(), image2.GetPointer(), region, region);
Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/Common/test/itkImageBufferRangeGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ CreateSmallImage()
imageSize.Fill(0);
image->SetRegions(imageSize);
SetVectorLengthIfImageIsVectorImage(*image, 1);
image->Allocate(true);
image->AllocateInitialized();
return image;
}

Expand Down Expand Up @@ -538,7 +538,7 @@ TEST(ImageBufferRange, SupportsVectorImage)
const typename ImageType::SizeType imageSize = { { sizeX, sizeY, sizeZ } };
image->SetRegions(imageSize);
image->SetVectorLength(vectorLength);
image->Allocate(true);
image->AllocateInitialized();
PixelType fillPixelValue(vectorLength);
fillPixelValue.Fill(42);
image->FillBuffer(fillPixelValue);
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/test/itkImageDuplicatorTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ itkImageDuplicatorTest(int, char *[])
std::cout << "Creating simulated image: ";
auto m_Image = ImageType::New();
m_Image->SetRegions(region);
m_Image->Allocate(true); // initialize buffer to zero
m_Image->AllocateInitialized();

itk::ImageRegionIterator<ImageType> it(m_Image, region);
it.GoToBegin();
Expand Down
8 changes: 3 additions & 5 deletions Modules/Core/Common/test/itkImageGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ Expect_allocated_initialized_image_equal_to_itself()

const auto image = TImage::New();
image->SetRegions(SizeType::Filled(2));

// Allocate and initialize the image:
image->Allocate(true);
image->AllocateInitialized();

Expect_equal_to_itself(*image);
}
Expand All @@ -102,11 +100,11 @@ Expect_unequal_when_sizes_differ()

const auto image1 = TImage::New();
image1->SetRegions(SizeType::Filled(2));
image1->Allocate(true);
image1->AllocateInitialized();

const auto image2 = TImage::New();
image2->SetRegions(SizeType::Filled(3));
image2->Allocate(true);
image2->AllocateInitialized();

Expect_unequal(*image1, *image2);
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/test/itkImageRandomIteratorTest2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ itkImageRandomIteratorTest2(int argc, char * argv[])
region.SetSize(size);

image->SetRegions(region);
image->Allocate(true); // initialize buffer to zero
image->AllocateInitialized();

using RandomIteratorType = itk::ImageRandomIteratorWithIndex<ImageType>;

Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/Common/test/itkImageRegionRangeGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ TEST(ImageRegionRange, SupportsVectorImage)
const typename ImageType::SizeType imageSize = { { sizeX, sizeY, sizeZ } };
image->SetRegions(imageSize);
image->SetVectorLength(vectorLength);
image->Allocate(true);
image->AllocateInitialized();
PixelType fillPixelValue(vectorLength);
fillPixelValue.Fill(42);
image->FillBuffer(fillPixelValue);
Expand Down Expand Up @@ -634,7 +634,7 @@ TEST(ImageRegionRange, ThrowsInReleaseWhenIterationRegionIsOutsideBufferedRegion
const SizeType imageSize{ { 3, 4 } };

image->SetRegions(RegionType{ imageIndex, imageSize });
image->Allocate(true);
image->AllocateInitialized();

Check_Range_constructor_throws_ExceptionObject_when_iteration_region_is_outside_of_buffered_region(
*image, RegionType{ imageIndex, imageSize + SizeType::Filled(1) });
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/test/itkLineIteratorTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ itkLineIteratorTest(int argc, char * argv[])

auto output = ImageType::New();
output->SetRegions(region);
output->Allocate(true); // initialize buffer to zero
output->AllocateInitialized();

// First test: empty line
IndexType startIndex;
Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/Common/test/itkObjectFactoryTest2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ ReallocateImage()
SizeType size = { { 5, 3 } };

testImage->SetRegions(size);
testImage->Allocate(true); // initialize buffer to zero
testImage->AllocateInitialized();

SizeType size2 = { { 100, 100 } };
testImage->SetRegions(size2);
testImage->Allocate(true); // initialize buffer to zero
testImage->AllocateInitialized();
}

int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ CreateSmallImage()
imageSize.Fill(0);
image->SetRegions(imageSize);
SetVectorLengthIfImageIsVectorImage(*image, 1);
image->Allocate(true);
image->AllocateInitialized();
return image;
}

Expand Down Expand Up @@ -623,7 +623,7 @@ TEST(ShapedImageNeighborhoodRange, SupportsVectorImage)
const typename ImageType::SizeType imageSize = { { sizeX, sizeY, sizeZ } };
image->SetRegions(imageSize);
image->SetVectorLength(vectorLength);
image->Allocate(true);
image->AllocateInitialized();
PixelType fillPixelValue(vectorLength);
fillPixelValue.Fill(42);
image->FillBuffer(fillPixelValue);
Expand Down Expand Up @@ -1006,7 +1006,7 @@ TEST(ShapedImageNeighborhoodRange, SupportsArbitraryBufferedRegionIndex)

const auto image = ImageType::New();
image->SetRegions(bufferedRegion);
image->Allocate(true);
image->AllocateInitialized();

// Set a 'magic value' at the begin of the buffered region.
const ImageType::PixelType magicPixelValue = 42;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ DoConvolution(typename ImageType::Pointer inputImage, unsigned long int directio

auto outputImage = ImageType::New();
outputImage->SetRegions(inputImage->GetRequestedRegion());
outputImage->Allocate(true);
outputImage->AllocateInitialized();

IteratorType out(outputImage, inputImage->GetRequestedRegion());
itk::NeighborhoodInnerProduct<ImageType> innerProduct;
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/GPUCommon/test/itkGPUImageTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ itkGPUImageTest(int argc, char * argv[])

dest = ItkImage1f::New();
dest->SetRegions(region);
dest->Allocate(true); // initialize buffer to zero
dest->AllocateInitialized();

// check pixel value
ItkImage1f::IndexType idx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ GaussianBlurImageFunction<TInputImage, TOutput>::RecomputeGaussianKernel()
typename InternalImageType::RegionType region;
region.SetSize(size);
m_InternalImage->SetRegions(region);
m_InternalImage->Allocate(true); // initialize buffer to zero
m_InternalImage->AllocateInitialized();
}

/** Evaluate the function at the specified point */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ itkBinaryThresholdImageFunctionTest(int, char *[])
region.SetSize(size);

image->SetRegions(region);
image->Allocate(true); // initialize buffer to zero
image->AllocateInitialized();

for (unsigned int i = 0; i < FloatImage::ImageDimension; ++i)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ itkGaussianBlurImageFunctionTest(int, char *[])
region.SetSize(size);

image->SetRegions(region);
image->Allocate(true); // initialize buffer to zero
image->AllocateInitialized();

// Fill the image with a straight line
for (unsigned int i = 0; i < 50; ++i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TestGaussianDerivativeImageFunction()
region.SetSize(size);

image->SetRegions(region);
image->Allocate(true); // initialize buffer to zero
image->AllocateInitialized();

// Fill the image with a straight line
for (unsigned int i = 0; i < 50; ++i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Expect_EvaluateAtIndex_returns_zero_when_all_pixels_are_zero(const typename TIma
{
const auto image = TImage::New();
image->SetRegions(imageSize);
image->Allocate(true);
image->AllocateInitialized();

const auto imageFunction = itk::SumOfSquaresImageFunction<TImage>::New();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Expect_AxisAlignedBoundingBoxRegion_is_empty_when_all_pixel_values_are_zero(
const auto image = itk::Image<TPixel, VImageDimension>::New();

image->SetRegions(imageRegion);
image->Allocate(true);
image->AllocateInitialized();

EXPECT_EQ(ComputeAxisAlignedBoundingBoxRegionInImageGridSpace(*image).GetSize(), itk::Size<VImageDimension>{});
}
Expand Down Expand Up @@ -87,9 +87,7 @@ Expect_AxisAlignedBoundingBoxRegion_equals_region_of_single_pixel_when_it_is_the
const auto image = itk::Image<TPixel, VImageDimension>::New();

image->SetRegions(imageRegion);

// Initialize all pixels to zero.
image->Allocate(true);
image->AllocateInitialized();

const itk::ImageRegionIndexRange<VImageDimension> indexRange{ imageRegion };

Expand Down Expand Up @@ -250,7 +248,7 @@ TEST(ImageMaskSpatialObject, IsInsideSingleNonZeroPixel)
// Create an image filled with zero valued pixels.
const auto image = ImageType::New();
image->SetRegions(SizeType::Filled(8));
image->Allocate(true);
image->AllocateInitialized();

constexpr itk::IndexValueType indexValue{ 4 };
image->SetPixel({ { indexValue, indexValue } }, 1);
Expand All @@ -277,7 +275,7 @@ TEST(ImageMaskSpatialObject, IsInsideIndependentOfDistantPixels)
// Create an image filled with zero valued pixels.
const auto image = ImageType::New();
image->SetRegions(SizeType::Filled(10));
image->Allocate(true);
image->AllocateInitialized();

// Set the value of a pixel to non-zero.
constexpr itk::IndexValueType indexValue{ 8 };
Expand Down Expand Up @@ -316,7 +314,7 @@ TEST(ImageMaskSpatialObject, CornerPointIsNotInsideMaskOfZeroValues)
// Create a mask image, and fill the image with zero vales.
const auto image = itk::Image<unsigned char>::New();
image->SetRegions(itk::Size<>{ { 2, 2 } });
image->Allocate(true);
image->AllocateInitialized();

const auto imageMaskSpatialObject = itk::ImageMaskSpatialObject<2>::New();
imageMaskSpatialObject->SetImage(image);
Expand All @@ -336,7 +334,7 @@ TEST(ImageMaskSpatialObject, IsInsideInWorldSpaceOverloads)
// Create a mask image.
const auto maskImage = MaskImageType::New();
maskImage->SetRegions(itk::Size<imageDimension>::Filled(2));
maskImage->Allocate(true);
maskImage->AllocateInitialized();
maskImage->SetPixel({}, MaskPixelType{ 1 });
maskImage->SetSpacing(itk::MakeFilled<MaskImageType::SpacingType>(0.5));

Expand Down Expand Up @@ -372,7 +370,7 @@ TEST(ImageMaskSpatialObject, StoresRegionsFromMaskImage)
// Create a mask image.
const auto maskImage = MaskImageType::New();
maskImage->SetRegions(RegionType{ IndexType::Filled(indexValue), SizeType::Filled(sizeValue) });
maskImage->Allocate(true);
maskImage->AllocateInitialized();

const auto imageMaskSpatialObject = ImageMaskSpatialObjectType::New();
imageMaskSpatialObject->SetImage(maskImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ itkImageMaskSpatialObjectTest(int, char *[])
region.SetIndex(index);

image->SetRegions(region);
image->Allocate(true); // initialize buffer to zero
image->AllocateInitialized();

ImageType::RegionType insideRegion;
ImageType::SizeType insideSize = { { 30, 30, 30 } };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ itkImageMaskSpatialObjectTest2(int, char *[])
region.SetSize(size);
region.SetIndex(index);
image->SetRegions(region);
image->Allocate(true); // initialize buffer to zero
image->AllocateInitialized();

ImageType::RegionType insideRegion;
constexpr unsigned int INSIDE_SIZE = 30;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ itkImageMaskSpatialObjectTest3(int, char *[])
region.SetSize(size);
region.SetIndex(index);
image->SetRegions(region);
image->Allocate(true); // initialize buffer to zero
image->AllocateInitialized();

auto imageMaskSpatialObject = ImageMaskSpatialObjectType::New();
imageMaskSpatialObject->SetImage(image);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ itkTestingExtractSliceImageFilterTest(int, char *[])
region.SetSize(size);

inputImage->SetRegions(region);
inputImage->Allocate(true); // initialize buffer to zero
inputImage->AllocateInitialized();

InputImageType::DirectionType direction;
direction[0][0] = 1.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ CLANG_SUPPRESS_Wfloat_equal
RealImagePointer logBiasField = RealImageType::New();
logBiasField->CopyInformation(inputImage);
logBiasField->SetRegions(inputImage->GetLargestPossibleRegion());
logBiasField->Allocate(true); // initialize buffer to zero
logBiasField->AllocateInitialized();

RealImagePointer logSharpenedImage = RealImageType::New();
logSharpenedImage->CopyInformation(inputImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ itkConvolutionImageFilterDeltaFunctionTest(int argc, char * argv[])
ImageType::RegionType region = reader->GetOutput()->GetLargestPossibleRegion();
auto deltaFunctionImage = ImageType::New();
deltaFunctionImage->SetRegions(region);
deltaFunctionImage->Allocate(true); // initialize
// buffer
// to zero
deltaFunctionImage->AllocateInitialized();

// Set the middle pixel (rounded up) to 1.
ImageType::IndexType middleIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ itkFFTConvolutionImageFilterDeltaFunctionTest(int argc, char * argv[])
ImageType::RegionType region = reader->GetOutput()->GetLargestPossibleRegion();
auto deltaFunctionImage = ImageType::New();
deltaFunctionImage->SetRegions(region);
deltaFunctionImage->Allocate(true); // initialize buffer to zero
deltaFunctionImage->AllocateInitialized();

// Set the middle pixel (rounded up) to 1
ImageType::IndexType middleIndex;
Expand Down
Loading

0 comments on commit 1b01d5c

Please sign in to comment.