Skip to content

Commit

Permalink
STYLE: Replace Allocate(); FillBuffer(0) with AllocateInitialized()
Browse files Browse the repository at this point in the history
Replaced lines of code of the form

    image>Allocate();
    image>FillBuffer(zero);

With `image->AllocateInitialized();`.

Cases found by the regular expression `^(.+->)Allocate.+;\r\n\1FillBuffer`.

Follow-up to pull request #4479
commit 47fe345
"ENH: Add `AllocateInitialized()` to ImageBase"
  • Loading branch information
N-Dekker authored and hjmjohnson committed Mar 6, 2024
1 parent 634cbdc commit cd49925
Show file tree
Hide file tree
Showing 25 changed files with 33 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,12 @@ ConstantVelocityFieldTransform<TParametersValueType, VDimension>::SetFixedParame
}
}

PixelType zeroDisplacement;
zeroDisplacement.Fill(0.0);

auto velocityField = ConstantVelocityFieldType::New();
velocityField->SetSpacing(spacing);
velocityField->SetOrigin(origin);
velocityField->SetDirection(direction);
velocityField->SetRegions(size);
velocityField->Allocate();
velocityField->FillBuffer(zeroDisplacement);
velocityField->AllocateInitialized();

this->SetConstantVelocityField(velocityField);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,16 +516,12 @@ DisplacementFieldTransform<TParametersValueType, VDimension>::SetFixedParameters
}
}

PixelType zeroDisplacement;
zeroDisplacement.Fill(0.0);

auto displacementField = DisplacementFieldType::New();
displacementField->SetSpacing(spacing);
displacementField->SetOrigin(origin);
displacementField->SetDirection(direction);
displacementField->SetRegions(size);
displacementField->Allocate();
displacementField->FillBuffer(zeroDisplacement);
displacementField->AllocateInitialized();

this->SetDisplacementField(displacementField);

Expand All @@ -536,8 +532,7 @@ DisplacementFieldTransform<TParametersValueType, VDimension>::SetFixedParameters
inverseDisplacementField->SetOrigin(origin);
inverseDisplacementField->SetDirection(direction);
inverseDisplacementField->SetRegions(size);
inverseDisplacementField->Allocate();
inverseDisplacementField->FillBuffer(zeroDisplacement);
inverseDisplacementField->AllocateInitialized();

this->SetInverseDisplacementField(inverseDisplacementField);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,12 @@ VelocityFieldTransform<TParametersValueType, VDimension>::SetFixedParameters(
}
}

PixelType zeroDisplacement;
zeroDisplacement.Fill(0.0);

auto velocityField = VelocityFieldType::New();
velocityField->SetSpacing(spacing);
velocityField->SetOrigin(origin);
velocityField->SetDirection(direction);
velocityField->SetRegions(size);
velocityField->Allocate();
velocityField->FillBuffer(zeroDisplacement);
velocityField->AllocateInitialized();

this->SetVelocityField(velocityField);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,7 @@ FastMarchingImageFilterBase<TInput, TOutput>::InitializeOutput(OutputImageType *
m_ConnectedComponentImage->SetSpacing(m_OutputSpacing);
m_ConnectedComponentImage->SetRegions(m_BufferedRegion);
m_ConnectedComponentImage->SetDirection(m_OutputDirection);
m_ConnectedComponentImage->Allocate();
m_ConnectedComponentImage->FillBuffer(0);
m_ConnectedComponentImage->AllocateInitialized();
}

// Allocate memory for the PointTypeImage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ LaplacianRecursiveGaussianImageFilter<TInputImage, TOutputImage>::GenerateData()
CumulativeImagePointer cumulativeImage = CumulativeImageType::New();
cumulativeImage->SetRegions(outputImage->GetRequestedRegion());
cumulativeImage->CopyInformation(inputImage);
cumulativeImage->Allocate();
cumulativeImage->FillBuffer(InternalRealType{});
cumulativeImage->AllocateInitialized();

m_DerivativeFilter->SetInput(inputImage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ GradientMagnitudeRecursiveGaussianImageFilter<TInputImage, TOutputImage>::Genera

auto cumulativeImage = CumulativeImageType::New();
cumulativeImage->SetRegions(inputImage->GetBufferedRegion());
cumulativeImage->Allocate();
cumulativeImage->FillBuffer(InternalRealType{});
cumulativeImage->AllocateInitialized();
// The output's information must match the input's information
cumulativeImage->CopyInformation(this->GetInput());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,11 @@ BSplineScatteredDataPointSetToImageFilter<TInputPointSet, TOutputImage>::BeforeT
{
this->m_OmegaLatticePerThread[n] = RealImageType::New();
this->m_OmegaLatticePerThread[n]->SetRegions(size);
this->m_OmegaLatticePerThread[n]->Allocate();
this->m_OmegaLatticePerThread[n]->FillBuffer(0.0);
this->m_OmegaLatticePerThread[n]->AllocateInitialized();

this->m_DeltaLatticePerThread[n] = PointDataImageType::New();
this->m_DeltaLatticePerThread[n]->SetRegions(size);
this->m_DeltaLatticePerThread[n]->Allocate();
this->m_DeltaLatticePerThread[n]->FillBuffer(PointDataType{});
this->m_DeltaLatticePerThread[n]->AllocateInitialized();
}
}
}
Expand Down Expand Up @@ -426,8 +424,7 @@ BSplineScatteredDataPointSetToImageFilter<TInputPointSet, TOutputImage>::Threade
}
RealImagePointer neighborhoodWeightImage = RealImageType::New();
neighborhoodWeightImage->SetRegions(size);
neighborhoodWeightImage->Allocate();
neighborhoodWeightImage->FillBuffer(0.0);
neighborhoodWeightImage->AllocateInitialized();

ImageRegionIteratorWithIndex<RealImageType> ItW(neighborhoodWeightImage,
neighborhoodWeightImage->GetRequestedRegion());
Expand Down Expand Up @@ -704,8 +701,7 @@ BSplineScatteredDataPointSetToImageFilter<TInputPointSet, TOutputImage>::AfterTh
}
this->m_PhiLattice = PointDataImageType::New();
this->m_PhiLattice->SetRegions(size);
this->m_PhiLattice->Allocate();
this->m_PhiLattice->FillBuffer(PointDataType{});
this->m_PhiLattice->AllocateInitialized();

ImageRegionIterator<PointDataImageType> ItP(this->m_PhiLattice, this->m_PhiLattice->GetLargestPossibleRegion());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,7 @@ ShapeLabelMapFilter<TImage, TLabelImage>::ComputePerimeter(LabelObjectType * lab
// std::cout << boundingBox << " " << lRegion << " " << elRegion << std::endl;
// now initialize the image
lineImage->SetRegions(elRegion);
lineImage->Allocate();
lineImage->FillBuffer(VectorLineType());
lineImage->AllocateInitialized();

// std::cout << "lineContainer.size(): " << lineContainer.size() << std::endl;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ DiscreteGaussianDerivativeImageFunction<TInputImage, TOutput>::RecomputeGaussian
region.SetSize(size);

kernelImage->SetRegions(region);
kernelImage->Allocate();
kernelImage->FillBuffer(TOutput{});
kernelImage->AllocateInitialized();

// Initially the kernel image will be an impulse at the center
typename KernelImageType::IndexType centerIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ DiscreteGradientMagnitudeGaussianImageFunction<TInputImage, TOutput>::RecomputeG
region.SetSize(size);

kernelImage->SetRegions(region);
kernelImage->Allocate();
kernelImage->FillBuffer(TOutput{});
kernelImage->AllocateInitialized();

// Initially the kernel image will be an impulse at the center
typename KernelImageType::IndexType centerIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ DiscreteHessianGaussianImageFunction<TInputImage, TOutput>::RecomputeGaussianKer
region.SetSize(size);

kernelImage->SetRegions(region);
kernelImage->Allocate();
kernelImage->FillBuffer(TOutput{});
kernelImage->AllocateInitialized();

// Initially the kernel image will be an impulse at the center
typename KernelImageType::IndexType centerIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,13 @@ TimeVaryingBSplineVelocityFieldTransformParametersAdaptor<TTransform>::AdaptTran
using UpsampleFilterType = ResampleImageFilter<ComponentImageType, ComponentImageType, ParametersValueType>;
using DecompositionFilterType = BSplineDecompositionImageFilter<ComponentImageType, ComponentImageType>;

constexpr VectorType zeroVector{};

TimeVaryingVelocityFieldControlPointLatticePointer requiredLattice =
TimeVaryingVelocityFieldControlPointLatticeType::New();
requiredLattice->SetRegions(requiredLatticeSize);
requiredLattice->SetOrigin(requiredLatticeOrigin);
requiredLattice->SetSpacing(requiredLatticeSpacing);
requiredLattice->SetDirection(requiredLatticeDirection);
requiredLattice->Allocate();
requiredLattice->FillBuffer(zeroVector);
requiredLattice->AllocateInitialized();

// Loop over dimension: each direction is upsampled separately.
for (SizeValueType j = 0; j < TotalDimension - 1; ++j)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ JointHistogramMutualInformationComputeJointPDFThreaderBase<TDomainPartitioner,
this->m_JointHistogramMIPerThreadVariables[i].JointHistogram->CopyInformation(this->m_Associate->m_JointPDF);
this->m_JointHistogramMIPerThreadVariables[i].JointHistogram->SetRegions(
this->m_Associate->m_JointPDF->GetLargestPossibleRegion());
this->m_JointHistogramMIPerThreadVariables[i].JointHistogram->Allocate();
this->m_JointHistogramMIPerThreadVariables[i].JointHistogram->FillBuffer(SizeValueType{});
this->m_JointHistogramMIPerThreadVariables[i].JointHistogram->AllocateInitialized();
this->m_JointHistogramMIPerThreadVariables[i].JointHistogramCount = SizeValueType{};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,12 @@ typename BSplineSyNImageRegistrationMethod<TFixedImage, TMovingImage, TOutputTra

if (this->m_Metric->GetMetricCategory() == ObjectToObjectMetricBaseTemplateEnums::MetricCategory::POINT_SET_METRIC)
{
constexpr DisplacementVectorType zeroVector{};

VirtualImageBaseConstPointer virtualDomainImage = this->GetCurrentLevelVirtualDomainImage();

metricGradientField = DisplacementFieldType::New();
metricGradientField->CopyInformation(virtualDomainImage);
metricGradientField->SetRegions(virtualDomainImage->GetLargestPossibleRegion());
metricGradientField->Allocate();
metricGradientField->FillBuffer(zeroVector);
metricGradientField->AllocateInitialized();

this->m_Metric->SetFixedObject(fixedPointSets[0]);
this->m_Metric->SetMovingObject(movingPointSets[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,28 @@ SyNImageRegistrationMethod<TFixedImage, TMovingImage, TOutputTransform, TVirtual

VirtualImageBaseConstPointer virtualDomainImage = this->GetCurrentLevelVirtualDomainImage();

constexpr DisplacementVectorType zeroVector{};

auto fixedDisplacementField = DisplacementFieldType::New();
fixedDisplacementField->CopyInformation(virtualDomainImage);
fixedDisplacementField->SetRegions(virtualDomainImage->GetBufferedRegion());
fixedDisplacementField->Allocate();
fixedDisplacementField->FillBuffer(zeroVector);
fixedDisplacementField->AllocateInitialized();

auto fixedInverseDisplacementField = DisplacementFieldType::New();
fixedInverseDisplacementField->CopyInformation(virtualDomainImage);
fixedInverseDisplacementField->SetRegions(virtualDomainImage->GetBufferedRegion());
fixedInverseDisplacementField->Allocate();
fixedInverseDisplacementField->FillBuffer(zeroVector);
fixedInverseDisplacementField->AllocateInitialized();

this->m_FixedToMiddleTransform->SetDisplacementField(fixedDisplacementField);
this->m_FixedToMiddleTransform->SetInverseDisplacementField(fixedInverseDisplacementField);

auto movingDisplacementField = DisplacementFieldType::New();
movingDisplacementField->CopyInformation(virtualDomainImage);
movingDisplacementField->SetRegions(virtualDomainImage->GetBufferedRegion());
movingDisplacementField->Allocate();
movingDisplacementField->FillBuffer(zeroVector);
movingDisplacementField->AllocateInitialized();

auto movingInverseDisplacementField = DisplacementFieldType::New();
movingInverseDisplacementField->CopyInformation(virtualDomainImage);
movingInverseDisplacementField->SetRegions(virtualDomainImage->GetBufferedRegion());
movingInverseDisplacementField->Allocate();
movingInverseDisplacementField->FillBuffer(zeroVector);
movingInverseDisplacementField->AllocateInitialized();

this->m_MovingToMiddleTransform->SetDisplacementField(movingDisplacementField);
this->m_MovingToMiddleTransform->SetInverseDisplacementField(movingInverseDisplacementField);
Expand Down Expand Up @@ -572,13 +566,10 @@ typename SyNImageRegistrationMethod<TFixedImage, TMovingImage, TOutputTransform,
if (this->m_DownsampleImagesForMetricDerivatives &&
this->m_Metric->GetMetricCategory() != ObjectToObjectMetricBaseTemplateEnums::MetricCategory::POINT_SET_METRIC)
{
const DisplacementVectorType zeroVector{};

auto identityField = DisplacementFieldType::New();
identityField->CopyInformation(virtualDomainImage);
identityField->SetRegions(virtualDomainImage->GetLargestPossibleRegion());
identityField->Allocate();
identityField->FillBuffer(zeroVector);
identityField->AllocateInitialized();

DisplacementFieldTransformPointer identityDisplacementFieldTransform = DisplacementFieldTransformType::New();
identityDisplacementFieldTransform->SetDisplacementField(identityField);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,10 @@ TimeVaryingBSplineVelocityFieldImageRegistrationMethod<TFixedImage,
}
}

constexpr DisplacementVectorType zeroVector{};

auto identityField = DisplacementFieldType::New();
identityField->CopyInformation(virtualDomainImage);
identityField->SetRegions(virtualDomainImage->GetLargestPossibleRegion());
identityField->Allocate();
identityField->FillBuffer(zeroVector);
identityField->AllocateInitialized();

this->m_IdentityDisplacementFieldTransform = DisplacementFieldTransformType::New();
this->m_IdentityDisplacementFieldTransform->SetDisplacementField(identityField);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ CollidingFrontsImageFilter<TInputImage, TOutputImage>::GenerateData()

OutputImageRegionType region = outputImage->GetRequestedRegion();
outputImage->SetBufferedRegion(region);
outputImage->Allocate();
outputImage->FillBuffer(OutputPixelType{});
outputImage->AllocateInitialized();

using FunctionType = BinaryThresholdImageFunction<OutputImageType>;
using IteratorType = FloodFilledImageFunctionConditionalConstIterator<OutputImageType, FunctionType>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ class LevelSetContainer<TIdentifier, LevelSetDenseImage<TImage>>
image->SetBufferedRegion(otherImage->GetBufferedRegion());
image->SetRequestedRegion(otherImage->GetRequestedRegion());
image->SetLargestPossibleRegion(otherImage->GetLargestPossibleRegion());
image->Allocate();
image->FillBuffer(OutputPixelType{});
image->AllocateInitialized();

temp_ls->SetImage(image);
newContainer[it->first] = temp_ls;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ LevelSetDomainMapImageFilter<TInputImage, TOutputImage>::GenerateData()

this->m_OutputImage = this->GetOutput();
this->m_OutputImage->SetBufferedRegion(region);
this->m_OutputImage->Allocate();
this->m_OutputImage->FillBuffer(OutputImagePixelType{});
this->m_OutputImage->AllocateInitialized();

InputImageIndexType end;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ ConfidenceConnectedImageFilter<TInputImage, TOutputImage>::GenerateData()
// Zero the output
OutputImageRegionType region = outputImage->GetRequestedRegion();
outputImage->SetBufferedRegion(region);
outputImage->Allocate();
outputImage->FillBuffer(OutputImagePixelType{});
outputImage->AllocateInitialized();

// Compute the statistics of the seed point

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ ConnectedThresholdImageFilter<TInputImage, TOutputImage>::GenerateData()
// Zero the output
OutputImageRegionType region = outputImage->GetRequestedRegion();
outputImage->SetBufferedRegion(region);
outputImage->Allocate();
outputImage->FillBuffer(OutputImagePixelType{});
outputImage->AllocateInitialized();

using FunctionType = BinaryThresholdImageFunction<InputImageType, double>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ IsolatedConnectedImageFilter<TInputImage, TOutputImage>::GenerateData()
// Zero the output
OutputImageRegionType region = outputImage->GetRequestedRegion();
outputImage->SetBufferedRegion(region);
outputImage->Allocate();
outputImage->FillBuffer(OutputImagePixelType{});
outputImage->AllocateInitialized();

using FunctionType = BinaryThresholdImageFunction<InputImageType>;
using IteratorType = FloodFilledImageFunctionConditionalIterator<OutputImageType, FunctionType>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ VectorConfidenceConnectedImageFilter<TInputImage, TOutputImage>::GenerateData()
// Zero the output
OutputImageRegionType region = outputImage->GetRequestedRegion();
outputImage->SetBufferedRegion(region);
outputImage->Allocate();
outputImage->FillBuffer(OutputImagePixelType{});
outputImage->AllocateInitialized();

// Compute the statistics of the seed point
using VectorMeanImageFunctionType = VectorMeanImageFunction<InputImageType>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,8 +673,7 @@ SLICImageFilter<TInputImage, TOutputImage, TDistancePixel>::GenerateData()
m_MarkerImage = MarkerImageType::New();
m_MarkerImage->CopyInformation(inputImage);
m_MarkerImage->SetBufferedRegion(region);
m_MarkerImage->Allocate();
m_MarkerImage->FillBuffer(NumericTraits<typename MarkerImageType::PixelType>::Zero);
m_MarkerImage->AllocateInitialized();


this->GetMultiThreader()->ParallelizeArray(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ TobogganImageFilter<TInputImage, TOutputImage>::GenerateData()

// Zero the output
outputImage->SetBufferedRegion(outputImage->GetRequestedRegion());
outputImage->Allocate();
outputImage->FillBuffer(z);
outputImage->AllocateInitialized();

using InputIterator = ImageRegionConstIterator<InputImageType>;
using OutputIterator = ImageRegionConstIterator<OutputImageType>;
Expand Down

0 comments on commit cd49925

Please sign in to comment.