Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add static member, itk::Image::CreateInitialized(const RegionType &) #4599

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Modules/Core/Common/include/itkImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ class ITK_TEMPLATE_EXPORT Image : public ImageBase<VImageDimension>
void
Allocate(bool initializePixels = false) override;

/** Creates an image with the specified region. Allocates its pixel buffer, zero-initializing its pixels. */
static Pointer
CreateInitialized(const RegionType & region)
{
const auto image = Image::New();
image->SetRegions(region);
image->AllocateInitialized();
return image;
}

/** Restore the data object to its initial state. This means releasing
* memory. */
void
Expand Down
6 changes: 2 additions & 4 deletions Modules/Core/Common/test/itkAdaptorComparisonTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,9 @@ itkAdaptorComparisonTest(int, char *[])
region.SetSize(size);
region.SetIndex(index);

auto scalar_image = ScalarImageType::New();
auto vector_image = VectorImageType::New();
auto scalar_image = ScalarImageType::CreateInitialized(region);

scalar_image->SetRegions(region);
scalar_image->AllocateInitialized();
auto vector_image = VectorImageType::New();

vector_image->SetRegions(region);
vector_image->Allocate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,11 @@ itkBSplineInterpolationWeightFunctionTest(int, char *[])
auto kernel = KernelType::New();

using ImageType = itk::Image<char, SpaceDimension>;
auto image = ImageType::New();
ImageType::RegionType region;
region.SetIndex(startIndex);
region.SetSize(size);

image->SetRegions(region);
image->AllocateInitialized();
auto image = ImageType::CreateInitialized(region);

using IteratorType = itk::ImageRegionConstIteratorWithIndex<ImageType>;
IteratorType iter(image, image->GetBufferedRegion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,9 @@ TEST(ConnectedImageNeighborhoodShape, SupportsConstShapedNeighborhoodIterator)
using OffsetType = itk::Offset<ImageDimension>;

// Create a "dummy" image.
const auto image = ImageType::New();
SizeType imageSize;
SizeType imageSize;
imageSize.Fill(1);
image->SetRegions(imageSize);
image->AllocateInitialized();
const auto image = ImageType::CreateInitialized(imageSize);

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

auto image2 = Short3DImageType::New();
image2->SetRegions(region);
image2->AllocateInitialized();


auto image3 = Float3DImageType::New();
image3->SetRegions(region);
image3->AllocateInitialized();
auto image2 = Short3DImageType::CreateInitialized(region);
auto image3 = Float3DImageType::CreateInitialized(region);

std::cout << "Copying two images of same type" << std::endl;
itk::ImageAlgorithm::Copy(image1.GetPointer(), image2.GetPointer(), region, region);
Expand Down
4 changes: 1 addition & 3 deletions Modules/Core/Common/test/itkImageDuplicatorTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ itkImageDuplicatorTest(int, char *[])
{
/** Create an image image */
std::cout << "Creating simulated image: ";
auto m_Image = ImageType::New();
m_Image->SetRegions(region);
m_Image->AllocateInitialized();
auto m_Image = ImageType::CreateInitialized(region);

itk::ImageRegionIterator<ImageType> it(m_Image, region);
it.GoToBegin();
Expand Down
15 changes: 3 additions & 12 deletions Modules/Core/Common/test/itkImageGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ void
Expect_allocated_initialized_image_equal_to_itself()
{
using SizeType = typename TImage::SizeType;

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

const auto image = TImage::CreateInitialized(SizeType::Filled(2));
Expect_equal_to_itself(*image);
}

Expand All @@ -98,13 +94,8 @@ Expect_unequal_when_sizes_differ()
{
using SizeType = typename TImage::SizeType;

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

const auto image2 = TImage::New();
image2->SetRegions(SizeType::Filled(3));
image2->AllocateInitialized();
const auto image1 = TImage::CreateInitialized(SizeType::Filled(2));
const auto image2 = TImage::CreateInitialized(SizeType::Filled(3));

Expect_unequal(*image1, *image2);
}
Expand Down
5 changes: 1 addition & 4 deletions Modules/Core/Common/test/itkImageRandomIteratorTest2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ itkImageRandomIteratorTest2(int argc, char * argv[])

using ImageType = itk::Image<PixelType, ImageDimension>;

auto image = ImageType::New();

ImageType::SizeType size;

size[0] = 1000;
Expand All @@ -59,8 +57,7 @@ itkImageRandomIteratorTest2(int argc, char * argv[])
region.SetIndex(start);
region.SetSize(size);

image->SetRegions(region);
image->AllocateInitialized();
auto image = ImageType::CreateInitialized(region);

using RandomIteratorType = itk::ImageRandomIteratorWithIndex<ImageType>;

Expand Down
5 changes: 1 addition & 4 deletions Modules/Core/Common/test/itkImageRegionRangeGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -628,13 +628,10 @@ TEST(ImageRegionRange, ThrowsInReleaseWhenIterationRegionIsOutsideBufferedRegion
using SizeType = ImageType::SizeType;
using RegionType = ImageType::RegionType;

const auto image = ImageType::New();

const IndexType imageIndex{ { -1, -2 } };
const SizeType imageSize{ { 3, 4 } };

image->SetRegions(RegionType{ imageIndex, imageSize });
image->AllocateInitialized();
const auto image = ImageType::CreateInitialized({ imageIndex, imageSize });

Check_Range_constructor_throws_ExceptionObject_when_iteration_region_is_outside_of_buffered_region(
*image, RegionType{ imageIndex, imageSize + SizeType::Filled(1) });
Expand Down
4 changes: 1 addition & 3 deletions Modules/Core/Common/test/itkLineIteratorTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ itkLineIteratorTest(int argc, char * argv[])
region.SetIndex(index);
region.SetSize(size);

auto output = ImageType::New();
output->SetRegions(region);
output->AllocateInitialized();
auto output = ImageType::CreateInitialized(region);

// First test: empty line
IndexType startIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1004,9 +1004,7 @@ TEST(ShapedImageNeighborhoodRange, SupportsArbitraryBufferedRegionIndex)

const ImageType::RegionType bufferedRegion{ arbitraryIndex, imageSize };

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

// 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 @@ -64,9 +64,7 @@ DoConvolution(typename ImageType::Pointer inputImage, unsigned long int directio

NeighborhoodIteratorType it(radius, inputImage, inputImage->GetRequestedRegion());

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

IteratorType out(outputImage, inputImage->GetRequestedRegion());
itk::NeighborhoodInnerProduct<ImageType> innerProduct;
Expand Down
4 changes: 1 addition & 3 deletions Modules/Core/GPUCommon/test/itkGPUImageTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ itkGPUImageTest(int argc, char * argv[])
srcB->Allocate();
srcB->FillBuffer(3.0f);

dest = ItkImage1f::New();
dest->SetRegions(region);
dest->AllocateInitialized();
dest = ItkImage1f::CreateInitialized(region);

// check pixel value
ItkImage1f::IndexType idx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ itkBinaryThresholdImageFunctionTest(int, char *[])

using FloatImage = itk::Image<float, 3>;

auto image = FloatImage::New();
FloatImage::RegionType region;
FloatImage::SizeType size;
size.Fill(64);
Expand All @@ -39,8 +38,7 @@ itkBinaryThresholdImageFunctionTest(int, char *[])
region.SetIndex(index);
region.SetSize(size);

image->SetRegions(region);
image->AllocateInitialized();
auto image = FloatImage::CreateInitialized(region);

for (unsigned int i = 0; i < FloatImage::ImageDimension; ++i)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ itkGaussianBlurImageFunctionTest(int, char *[])
using GFunctionType = itk::GaussianBlurImageFunction<ImageType>;

// Create and allocate the image
auto image = ImageType::New();
ImageType::SizeType size;
ImageType::IndexType start;
ImageType::RegionType region;
Expand All @@ -41,8 +40,7 @@ itkGaussianBlurImageFunctionTest(int, char *[])
region.SetIndex(start);
region.SetSize(size);

image->SetRegions(region);
image->AllocateInitialized();
auto image = ImageType::CreateInitialized(region);

// 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 @@ -29,7 +29,6 @@ TestGaussianDerivativeImageFunction()
using ImageType = itk::Image<PixelType, Dimension>;

// Create and allocate the image
auto image = ImageType::New();
typename ImageType::SizeType size;
typename ImageType::IndexType start;
typename ImageType::RegionType region;
Expand All @@ -41,8 +40,7 @@ TestGaussianDerivativeImageFunction()
region.SetIndex(start);
region.SetSize(size);

image->SetRegions(region);
image->AllocateInitialized();
auto image = ImageType::CreateInitialized(region);

// 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 @@ -56,9 +56,7 @@ template <typename TImage>
void
Expect_EvaluateAtIndex_returns_zero_when_all_pixels_are_zero(const typename TImage::SizeType & imageSize)
{
const auto image = TImage::New();
image->SetRegions(imageSize);
image->AllocateInitialized();
const auto image = TImage::CreateInitialized(imageSize);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ void
Expect_AxisAlignedBoundingBoxRegion_is_empty_when_all_pixel_values_are_zero(
const itk::ImageRegion<VImageDimension> & imageRegion)
{
const auto image = itk::Image<TPixel, VImageDimension>::New();

image->SetRegions(imageRegion);
image->AllocateInitialized();
const auto image = itk::Image<TPixel, VImageDimension>::CreateInitialized(imageRegion);

EXPECT_EQ(ComputeAxisAlignedBoundingBoxRegionInImageGridSpace(*image).GetSize(), itk::Size<VImageDimension>{});
}
Expand Down Expand Up @@ -84,10 +81,7 @@ void
Expect_AxisAlignedBoundingBoxRegion_equals_region_of_single_pixel_when_it_is_the_only_non_zero_pixel(
const itk::ImageRegion<VImageDimension> & imageRegion)
{
const auto image = itk::Image<TPixel, VImageDimension>::New();

image->SetRegions(imageRegion);
image->AllocateInitialized();
const auto image = itk::Image<TPixel, VImageDimension>::CreateInitialized(imageRegion);

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

Expand Down Expand Up @@ -246,9 +240,7 @@ TEST(ImageMaskSpatialObject, IsInsideSingleNonZeroPixel)
using PointType = ImageType::PointType;

// Create an image filled with zero valued pixels.
const auto image = ImageType::New();
image->SetRegions(SizeType::Filled(8));
image->AllocateInitialized();
const auto image = ImageType::CreateInitialized(SizeType::Filled(8));

constexpr itk::IndexValueType indexValue{ 4 };
image->SetPixel({ { indexValue, indexValue } }, 1);
Expand All @@ -273,9 +265,7 @@ TEST(ImageMaskSpatialObject, IsInsideIndependentOfDistantPixels)
using PointType = ImageType::PointType;

// Create an image filled with zero valued pixels.
const auto image = ImageType::New();
image->SetRegions(SizeType::Filled(10));
image->AllocateInitialized();
const auto image = ImageType::CreateInitialized(SizeType::Filled(10));

// Set the value of a pixel to non-zero.
constexpr itk::IndexValueType indexValue{ 8 };
Expand Down Expand Up @@ -312,9 +302,7 @@ TEST(ImageMaskSpatialObject, IsInsideIndependentOfDistantPixels)
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->AllocateInitialized();
const auto image = itk::Image<unsigned char>::CreateInitialized(itk::Size<>{ { 2, 2 } });

const auto imageMaskSpatialObject = itk::ImageMaskSpatialObject<2>::New();
imageMaskSpatialObject->SetImage(image);
Expand All @@ -332,9 +320,7 @@ TEST(ImageMaskSpatialObject, IsInsideInWorldSpaceOverloads)
using PointType = MaskImageType::PointType;

// Create a mask image.
const auto maskImage = MaskImageType::New();
maskImage->SetRegions(itk::Size<imageDimension>::Filled(2));
maskImage->AllocateInitialized();
const auto maskImage = MaskImageType::CreateInitialized(itk::Size<imageDimension>::Filled(2));
maskImage->SetPixel({}, MaskPixelType{ 1 });
maskImage->SetSpacing(itk::MakeFilled<MaskImageType::SpacingType>(0.5));

Expand Down Expand Up @@ -368,9 +354,8 @@ TEST(ImageMaskSpatialObject, StoresRegionsFromMaskImage)
using SizeType = MaskImageType::SizeType;

// Create a mask image.
const auto maskImage = MaskImageType::New();
maskImage->SetRegions(RegionType{ IndexType::Filled(indexValue), SizeType::Filled(sizeValue) });
maskImage->AllocateInitialized();
const auto maskImage =
MaskImageType::CreateInitialized({ IndexType::Filled(indexValue), SizeType::Filled(sizeValue) });

const auto imageMaskSpatialObject = ImageMaskSpatialObjectType::New();
imageMaskSpatialObject->SetImage(maskImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,14 @@ itkImageMaskSpatialObjectTest(int, char *[])
using ImageType = ImageMaskSpatialObject::ImageType;
using Iterator = itk::ImageRegionIterator<ImageType>;

auto image = ImageType::New();
ImageType::SizeType size = { { 50, 50, 50 } };
ImageType::IndexType index = { { 0, 0, 0 } };
ImageType::RegionType region;

region.SetSize(size);
region.SetIndex(index);

image->SetRegions(region);
image->AllocateInitialized();
auto image = ImageType::CreateInitialized(region);

ImageType::RegionType insideRegion;
ImageType::SizeType insideSize = { { 30, 30, 30 } };
Expand Down
21 changes: 10 additions & 11 deletions Modules/Core/SpatialObjects/test/itkImageMaskSpatialObjectTest2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ itkImageMaskSpatialObjectTest2(int, char *[])
// Also explicitly uses nonzero origin, non identity scales
// to fully test the commonly encountered cases from the real world

auto image = ImageType::New();
const ImageType::SizeType size = { { 50, 50, 50 } };

constexpr unsigned int index_offset = 6543;
const ImageType::IndexType index = { { index_offset, index_offset, index_offset } };

ImageType::RegionType region;
region.SetSize(size);
region.SetIndex(index);
auto image = ImageType::CreateInitialized(region);

// Set the direction for a non-oriented image
// to better test the frequently encountered case
Expand All @@ -61,8 +69,7 @@ itkImageMaskSpatialObjectTest2(int, char *[])
const ImageType::DirectionType direction = tfm->GetMatrix();
image->SetDirection(direction);

const ImageType::SizeType size = { { 50, 50, 50 } };
ImageType::PointType origin;
ImageType::PointType origin;
origin[0] = 1.51;
origin[1] = 2.10;
origin[2] = -300;
Expand All @@ -73,14 +80,6 @@ itkImageMaskSpatialObjectTest2(int, char *[])
spacing[1] = 0.7;
spacing[2] = 1.1;
image->SetSpacing(spacing);
constexpr unsigned int index_offset = 6543;
const ImageType::IndexType index = { { index_offset, index_offset, index_offset } };

ImageType::RegionType region;
region.SetSize(size);
region.SetIndex(index);
image->SetRegions(region);
image->AllocateInitialized();

ImageType::RegionType insideRegion;
constexpr unsigned int INSIDE_SIZE = 30;
Expand Down
Loading