From ae4389d9f44d0bfb6857ed2765dff914fb630962 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Thu, 26 Nov 2026 06:15:49 -0600 Subject: [PATCH 1/2] ENH: Add itk::MakeGraft for mini-pipeline inputs Filters that run a mini-pipeline give it a graft of their input, so that the internal filters cannot release or rewire the real one. MakeGraft names that idiom at namespace scope. It takes a const pointer or a SmartPointer, so callers need no const_cast, returns null for null, and documents that the graft has no source. Closes #7243. --- Modules/Core/Common/include/itkDataObject.h | 24 ++++ Modules/Core/Common/test/CMakeLists.txt | 1 + .../Core/Common/test/itkMakeGraftGTest.cxx | 126 ++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 Modules/Core/Common/test/itkMakeGraftGTest.cxx diff --git a/Modules/Core/Common/include/itkDataObject.h b/Modules/Core/Common/include/itkDataObject.h index b08d3a2cd42..5b0733021c5 100644 --- a/Modules/Core/Common/include/itkDataObject.h +++ b/Modules/Core/Common/include/itkDataObject.h @@ -593,6 +593,30 @@ class ITK_FORCE_EXPORT_MACRO(ITKCommon) DataObject : public Object friend class ProcessObject; friend class DataObjectError; }; + +/** Makes a new data object that shares the bulk data of `data`, for use in a mini-pipeline, so that the + * mini-pipeline cannot release or rewire `data`. Returns null for a null `data`. The graft has no source: call + * `data->UpdateSource()` first when `data` is a pipeline output that may be out of date. */ +template +typename TDataObject::Pointer +MakeGraft(const TDataObject * data) +{ + if (data == nullptr) + { + return nullptr; + } + auto graft = TDataObject::New(); + graft->Graft(data); + return graft; +} + +/** \overload */ +template +typename TDataObject::Pointer +MakeGraft(const SmartPointer & data) +{ + return MakeGraft(data.GetPointer()); +} } // end namespace itk #endif diff --git a/Modules/Core/Common/test/CMakeLists.txt b/Modules/Core/Common/test/CMakeLists.txt index 7241611b7e3..af58f252d88 100644 --- a/Modules/Core/Common/test/CMakeLists.txt +++ b/Modules/Core/Common/test/CMakeLists.txt @@ -1602,6 +1602,7 @@ set( itkIndexRangeGTest.cxx itkIntTypesGTest.cxx itkLightObjectGTest.cxx + itkMakeGraftGTest.cxx itkMakeUniqueForOverwriteGTest.cxx itkMathCastWithRangeCheckGTest.cxx itkMathGTest.cxx diff --git a/Modules/Core/Common/test/itkMakeGraftGTest.cxx b/Modules/Core/Common/test/itkMakeGraftGTest.cxx new file mode 100644 index 00000000000..5e762b6646b --- /dev/null +++ b/Modules/Core/Common/test/itkMakeGraftGTest.cxx @@ -0,0 +1,126 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ + +// First include the header file to be tested: +#include "itkDataObject.h" +#include "itkImage.h" +#include "itkImportImageFilter.h" +#include "itkPointSet.h" +#include +#include +#include + +namespace +{ +using ImageType = itk::Image; +using PointSetType = itk::PointSet; + +ImageType::Pointer +MakeImage() +{ + auto image = ImageType::New(); + image->SetRegions(ImageType::SizeType::Filled(4)); + image->SetSpacing(itk::MakeFilled(0.5)); + image->SetOrigin(itk::MakeFilled(-1.5)); + ImageType::DirectionType direction{}; + direction(0, 1) = 1.0; + direction(1, 0) = -1.0; + image->SetDirection(direction); + image->AllocateInitialized(); + return image; +} +} // namespace + + +TEST(MakeGraft, ImageSharesBufferAndGeometry) +{ + const ImageType::ConstPointer image = MakeImage(); + const auto graft = itk::MakeGraft(image.GetPointer()); + + static_assert(std::is_same_v); + ASSERT_NE(graft, nullptr); + EXPECT_NE(graft.GetPointer(), image.GetPointer()); + EXPECT_EQ(graft->GetBufferPointer(), image->GetBufferPointer()); + EXPECT_EQ(graft->GetBufferedRegion(), image->GetBufferedRegion()); + EXPECT_EQ(graft->GetLargestPossibleRegion(), image->GetLargestPossibleRegion()); + EXPECT_EQ(graft->GetSpacing(), image->GetSpacing()); + EXPECT_EQ(graft->GetOrigin(), image->GetOrigin()); + EXPECT_EQ(graft->GetDirection(), image->GetDirection()); +} + + +TEST(MakeGraft, ReleasingTheGraftKeepsTheOriginalBuffered) +{ + const auto image = MakeImage(); + const auto graft = itk::MakeGraft(image.GetPointer()); + + graft->ReleaseData(); + + EXPECT_EQ(graft->GetBufferedRegion().GetNumberOfPixels(), 0u); + EXPECT_EQ(image->GetBufferedRegion(), image->GetLargestPossibleRegion()); + EXPECT_NE(image->GetBufferPointer(), nullptr); +} + + +TEST(MakeGraft, GraftOfAFilterOutputHasNoSource) +{ + std::vector buffer(16, 1.0f); + const auto importer = itk::ImportImageFilter::New(); + importer->SetRegion(ImageType::RegionType(ImageType::SizeType::Filled(4))); + importer->SetImportPointer(buffer.data(), buffer.size(), false); + importer->Update(); + const ImageType::Pointer image = importer->GetOutput(); + + const auto graft = itk::MakeGraft(image); + + ASSERT_NE(image->GetSource(), nullptr); + EXPECT_EQ(graft->GetSource(), nullptr); + EXPECT_EQ(graft->GetBufferPointer(), buffer.data()); +} + + +TEST(MakeGraft, NullGivesNull) +{ + EXPECT_EQ(itk::MakeGraft(static_cast(nullptr)), nullptr); + EXPECT_EQ(itk::MakeGraft(ImageType::ConstPointer{}), nullptr); + EXPECT_EQ(itk::MakeGraft(ImageType::Pointer{}), nullptr); +} + + +TEST(MakeGraft, AcceptsSmartPointers) +{ + const ImageType::Pointer image = MakeImage(); + const ImageType::ConstPointer constImage = image; + + static_assert(std::is_same_v); + static_assert(std::is_same_v); + EXPECT_EQ(itk::MakeGraft(image)->GetBufferPointer(), image->GetBufferPointer()); + EXPECT_EQ(itk::MakeGraft(constImage)->GetBufferPointer(), image->GetBufferPointer()); +} + + +TEST(MakeGraft, PointSetSharesPoints) +{ + auto pointSet = PointSetType::New(); + pointSet->SetPoint(0, itk::MakeFilled(1.5)); + + const auto graft = itk::MakeGraft(pointSet.GetPointer()); + + ASSERT_NE(graft, nullptr); + EXPECT_EQ(graft->GetPoints(), pointSet->GetPoints()); +} From aa727d499301bc3589b5ef9f7270b172fee30727 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Thu, 26 Nov 2026 06:19:34 -0600 Subject: [PATCH 2/2] STYLE: Use itk::MakeGraft for mini-pipeline grafts Replace each hand-written New() and Graft() pair, about 40 sites, with MakeGraft, which also drops the const_casts the old form used. Remove the protected ImageRegistrationMethodv4::MakeGraft (from #7095, in no release) and the private N4BiasFieldCorrectionImageFilter::MakeInputView (from #7093), and update the mini-pipeline example in the ImageSource documentation. The GPU discrete Gaussian filter grafts into a different image type and InvertDisplacementFieldImageFilter re-grafts the same objects each iteration, so both keep their explicit Graft() calls. --- Modules/Core/Common/include/itkImageSource.h | 4 +-- .../itkTransformGeometryImageFilter.hxx | 3 +-- .../itkN4BiasFieldCorrectionImageFilter.h | 5 ---- .../itkN4BiasFieldCorrectionImageFilter.hxx | 17 +++--------- .../include/itkConvolutionImageFilter.hxx | 5 +--- .../include/itkFFTConvolutionImageFilter.hxx | 3 +-- ...kedFFTNormalizedCorrelationImageFilter.hxx | 20 +++----------- .../itkInverseDeconvolutionImageFilter.hxx | 3 +-- .../itkTikhonovDeconvolutionImageFilter.hxx | 3 +-- .../itkWienerDeconvolutionImageFilter.hxx | 3 +-- ...meVaryingBSplineVelocityFieldTransform.hxx | 14 +++------- .../itkTimeVaryingVelocityFieldTransform.hxx | 11 ++------ ...tkDirectedHausdorffDistanceImageFilter.hxx | 5 +--- .../include/itkBilateralImageFilter.hxx | 5 +--- .../itkCannyEdgeDetectionImageFilter.hxx | 6 ++--- ...kDiscreteGaussianDerivativeImageFilter.hxx | 3 +-- .../itkHoughTransform2DCirclesImageFilter.hxx | 8 ++---- .../itkLaplacianSharpeningImageFilter.hxx | 3 +-- .../include/itkUnsharpMaskImageFilter.hxx | 4 +-- ...tkGradientRecursiveGaussianImageFilter.hxx | 4 +-- .../include/itkNormalizeImageFilter.hxx | 3 +-- ...aptiveHistogramEqualizationImageFilter.hxx | 3 +-- ...kWaveletCoeffsPhaseAnalyzisImageFilter.hxx | 3 +-- ...kWaveletCoeffsSpatialDomainImageFilter.hxx | 3 +-- ...arabolicOpenCloseSafeBorderImageFilter.hxx | 6 ++--- .../itkDiscreteGaussianImageFilter.hxx | 3 +-- .../itkFFTDiscreteGaussianImageFilter.hxx | 3 +-- .../itkStructuralSimilarityImageFilter.hxx | 4 +-- ...kCoocurrenceTextureFeaturesImageFilter.hxx | 6 ++--- ...itkRunLengthTextureFeaturesImageFilter.hxx | 6 ++--- .../itkMedialThicknessImageFilter3D.hxx | 3 +-- ...elocityFieldTransformParametersAdaptor.hxx | 6 ++--- .../include/itkImageToImageMetricv4.hxx | 8 ++---- .../itkBSplineSyNImageRegistrationMethod.hxx | 10 +++---- .../include/itkImageRegistrationMethodv4.h | 20 -------------- .../include/itkImageRegistrationMethodv4.hxx | 10 +++---- .../include/itkSyNImageRegistrationMethod.hxx | 27 +++++++++---------- ...neVelocityFieldImageRegistrationMethod.hxx | 10 +++---- ...VelocityFieldImageRegistrationMethodv4.hxx | 4 +-- .../itkCannySegmentationLevelSetFunction.hxx | 4 +-- .../SuperPixel/include/itkSLICImageFilter.hxx | 3 +-- 41 files changed, 80 insertions(+), 194 deletions(-) diff --git a/Modules/Core/Common/include/itkImageSource.h b/Modules/Core/Common/include/itkImageSource.h index a760406e98a..57429eb4f82 100644 --- a/Modules/Core/Common/include/itkImageSource.h +++ b/Modules/Core/Common/include/itkImageSource.h @@ -165,9 +165,7 @@ class ITK_TEMPLATE_EXPORT ImageSource \code // Setup the mini-pipeline to process the input to this filter // The input is not connected to the pipeline. - auto input = InputImageType::New(); - input->Graft( const_cast< InputImageType * >( this->GetInput() ); - firstFilterInMiniPipeline->SetInput( input ); + firstFilterInMiniPipeline->SetInput( MakeGraft( this->GetInput() ) ); // setup the mini-pipeline to calculate the correct regions // and write to the appropriate bulk data block diff --git a/Modules/Core/Transform/include/itkTransformGeometryImageFilter.hxx b/Modules/Core/Transform/include/itkTransformGeometryImageFilter.hxx index 3aacfd82a74..5be23a59f0b 100644 --- a/Modules/Core/Transform/include/itkTransformGeometryImageFilter.hxx +++ b/Modules/Core/Transform/include/itkTransformGeometryImageFilter.hxx @@ -74,8 +74,7 @@ TransformGeometryImageFilter::GenerateData() } OutputImageType * output = this->GetOutput(); - auto input = InputImageType::New(); - input->Graft(const_cast(this->GetInput())); + auto input = MakeGraft(this->GetInput()); // SEE HEADER FILE FOR MATH DESCRIPTION diff --git a/Modules/Filtering/BiasCorrection/include/itkN4BiasFieldCorrectionImageFilter.h b/Modules/Filtering/BiasCorrection/include/itkN4BiasFieldCorrectionImageFilter.h index 0907da51b08..5a951f4d34b 100644 --- a/Modules/Filtering/BiasCorrection/include/itkN4BiasFieldCorrectionImageFilter.h +++ b/Modules/Filtering/BiasCorrection/include/itkN4BiasFieldCorrectionImageFilter.h @@ -391,11 +391,6 @@ class ITK_TEMPLATE_EXPORT N4BiasFieldCorrectionImageFilter : public ImageToImage GenerateData() override; private: - /** An image that shares the buffer of the given one, as a mini-pipeline input whose release frees only the view. */ - template - static typename TImage::Pointer - MakeInputView(const TImage * image); - // N4 algorithm functions: The basic algorithm iterates between sharpening // the intensity histogram of the corrected input image and spatially // smoothing those results with a B-spline scalar field estimate of the diff --git a/Modules/Filtering/BiasCorrection/include/itkN4BiasFieldCorrectionImageFilter.hxx b/Modules/Filtering/BiasCorrection/include/itkN4BiasFieldCorrectionImageFilter.hxx index bf7807c2446..b9c5563c67d 100644 --- a/Modules/Filtering/BiasCorrection/include/itkN4BiasFieldCorrectionImageFilter.hxx +++ b/Modules/Filtering/BiasCorrection/include/itkN4BiasFieldCorrectionImageFilter.hxx @@ -41,17 +41,6 @@ ITK_GCC_PRAGMA_POP namespace itk { -template -template -auto -N4BiasFieldCorrectionImageFilter::MakeInputView(const TImage * image) -> - typename TImage::Pointer -{ - auto view = TImage::New(); - view->Graft(image); - return view; -} - template N4BiasFieldCorrectionImageFilter::N4BiasFieldCorrectionImageFilter() : m_MaskLabel(NumericTraits::OneValue()) @@ -615,7 +604,7 @@ typename N4BiasFieldCorrectionImageFilter N4BiasFieldCorrectionImageFilter::ReconstructBiasField( const BiasFieldControlPointLatticeType * controlPointLattice) { - // The view made below has no source, so bring a pipeline output up to date first. + // The graft made below has no source, so bring a pipeline output up to date first. controlPointLattice->UpdateSource(); const auto reconstructer = this->MakeBiasFieldReconstructer(controlPointLattice); @@ -643,8 +632,8 @@ N4BiasFieldCorrectionImageFilter::MakeBia const InputImageType * inputImage = this->GetInput(); auto reconstructer = BiasFieldReconstructerType::New(); - // A view, so that releasing the reconstructer's input leaves the lattice buffered. - reconstructer->SetInput(MakeInputView(controlPointLattice)); + // A graft, so that releasing the reconstructer's input leaves the lattice buffered. + reconstructer->SetInput(MakeGraft(controlPointLattice)); reconstructer->SetOrigin(inputImage->GetOrigin()); reconstructer->SetSpacing(inputImage->GetSpacing()); reconstructer->SetDirection(inputImage->GetDirection()); diff --git a/Modules/Filtering/Convolution/include/itkConvolutionImageFilter.hxx b/Modules/Filtering/Convolution/include/itkConvolutionImageFilter.hxx index 90a38862c4e..5c5e11eddc9 100644 --- a/Modules/Filtering/Convolution/include/itkConvolutionImageFilter.hxx +++ b/Modules/Filtering/Convolution/include/itkConvolutionImageFilter.hxx @@ -124,15 +124,12 @@ ConvolutionImageFilter::ComputeConvolut const KernelSizeType radius = this->GetKernelRadius(kernelImage); kernelOperator.CreateToRadius(radius); - auto localInput = InputImageType::New(); - localInput->Graft(this->GetInput()); - // The NeighborhoodOperatorImageFilter does all the work. using ConvolutionFilterType = NeighborhoodOperatorImageFilter; auto convolutionFilter = ConvolutionFilterType::New(); convolutionFilter->SetOperator(kernelOperator); convolutionFilter->OverrideBoundaryCondition(this->GetBoundaryCondition()); - convolutionFilter->SetInput(localInput); + convolutionFilter->SetInput(MakeGraft(this->GetInput())); convolutionFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits()); convolutionFilter->ReleaseDataFlagOn(); progress->RegisterInternalFilter(convolutionFilter, 1.0f - optionalFilterWeights); diff --git a/Modules/Filtering/Convolution/include/itkFFTConvolutionImageFilter.hxx b/Modules/Filtering/Convolution/include/itkFFTConvolutionImageFilter.hxx index 31f2d7c2462..0d81416f99d 100644 --- a/Modules/Filtering/Convolution/include/itkFFTConvolutionImageFilter.hxx +++ b/Modules/Filtering/Convolution/include/itkFFTConvolutionImageFilter.hxx @@ -85,8 +85,7 @@ FFTConvolutionImageFilterSetMiniPipelineFilter(this); - auto localInput = InputImageType::New(); - localInput->Graft(this->GetInput()); + auto localInput = MakeGraft(this->GetInput()); const KernelImageType * kernelImage = this->GetKernelImage(); diff --git a/Modules/Filtering/Convolution/include/itkMaskedFFTNormalizedCorrelationImageFilter.hxx b/Modules/Filtering/Convolution/include/itkMaskedFFTNormalizedCorrelationImageFilter.hxx index 906a73c1da1..b3595a4e3c1 100644 --- a/Modules/Filtering/Convolution/include/itkMaskedFFTNormalizedCorrelationImageFilter.hxx +++ b/Modules/Filtering/Convolution/include/itkMaskedFFTNormalizedCorrelationImageFilter.hxx @@ -112,25 +112,13 @@ void MaskedFFTNormalizedCorrelationImageFilter::GenerateData() { // Store the input images. - InputImagePointer fixedImage = InputImageType::New(); - fixedImage->Graft(this->GetFixedImage()); + InputImagePointer fixedImage = MakeGraft(this->GetFixedImage()); - InputImagePointer movingImage = InputImageType::New(); - movingImage->Graft(this->GetMovingImage()); + InputImagePointer movingImage = MakeGraft(this->GetMovingImage()); - MaskImagePointer fixedMask = nullptr; - if (this->GetFixedImageMask()) - { - fixedMask = MaskImageType::New(); - fixedMask->Graft(this->GetFixedImageMask()); - } + MaskImagePointer fixedMask = MakeGraft(this->GetFixedImageMask()); - MaskImagePointer movingMask = nullptr; - if (this->GetMovingImageMask()) - { - movingMask = MaskImageType::New(); - movingMask->Graft(this->GetMovingImageMask()); - } + MaskImagePointer movingMask = MakeGraft(this->GetMovingImageMask()); this->UpdateProgress(m_AccumulatedProgress); const OutputImagePointer outputImage = this->GetOutput(); diff --git a/Modules/Filtering/Deconvolution/include/itkInverseDeconvolutionImageFilter.hxx b/Modules/Filtering/Deconvolution/include/itkInverseDeconvolutionImageFilter.hxx index db02df0485b..016d63226e3 100644 --- a/Modules/Filtering/Deconvolution/include/itkInverseDeconvolutionImageFilter.hxx +++ b/Modules/Filtering/Deconvolution/include/itkInverseDeconvolutionImageFilter.hxx @@ -39,8 +39,7 @@ InverseDeconvolutionImageFilterSetMiniPipelineFilter(this); - auto localInput = InputImageType::New(); - localInput->Graft(this->GetInput()); + auto localInput = MakeGraft(this->GetInput()); const KernelImageType * kernelImage = this->GetKernelImage(); diff --git a/Modules/Filtering/Deconvolution/include/itkTikhonovDeconvolutionImageFilter.hxx b/Modules/Filtering/Deconvolution/include/itkTikhonovDeconvolutionImageFilter.hxx index 07f853fdacc..b7b22dfd93b 100644 --- a/Modules/Filtering/Deconvolution/include/itkTikhonovDeconvolutionImageFilter.hxx +++ b/Modules/Filtering/Deconvolution/include/itkTikhonovDeconvolutionImageFilter.hxx @@ -33,8 +33,7 @@ TikhonovDeconvolutionImageFilterSetMiniPipelineFilter(this); - auto localInput = InputImageType::New(); - localInput->Graft(this->GetInput()); + auto localInput = MakeGraft(this->GetInput()); const KernelImageType * kernelImage = this->GetKernelImage(); diff --git a/Modules/Filtering/Deconvolution/include/itkWienerDeconvolutionImageFilter.hxx b/Modules/Filtering/Deconvolution/include/itkWienerDeconvolutionImageFilter.hxx index c7924cc822c..410725c718a 100644 --- a/Modules/Filtering/Deconvolution/include/itkWienerDeconvolutionImageFilter.hxx +++ b/Modules/Filtering/Deconvolution/include/itkWienerDeconvolutionImageFilter.hxx @@ -33,8 +33,7 @@ WienerDeconvolutionImageFilterSetMiniPipelineFilter(this); - auto localInput = InputImageType::New(); - localInput->Graft(this->GetInput()); + auto localInput = MakeGraft(this->GetInput()); const KernelImageType * kernelImage = this->GetKernelImage(); diff --git a/Modules/Filtering/DisplacementField/include/itkTimeVaryingBSplineVelocityFieldTransform.hxx b/Modules/Filtering/DisplacementField/include/itkTimeVaryingBSplineVelocityFieldTransform.hxx index 6e372a36ae2..5d15d30d275 100644 --- a/Modules/Filtering/DisplacementField/include/itkTimeVaryingBSplineVelocityFieldTransform.hxx +++ b/Modules/Filtering/DisplacementField/include/itkTimeVaryingBSplineVelocityFieldTransform.hxx @@ -56,12 +56,9 @@ TimeVaryingBSplineVelocityFieldTransform::Inte closeDimensions[VDimension] = 1; } - // A graft, so that releasing the B-spline filter's input leaves the control point lattice buffered. - auto latticeGraft = TimeVaryingVelocityFieldControlPointLatticeType::New(); - latticeGraft->Graft(this->GetTimeVaryingVelocityFieldControlPointLattice()); - auto bspliner = BSplineFilterType::New(); - bspliner->SetInput(latticeGraft); + // A graft, so that releasing the B-spline filter's input leaves the control point lattice buffered. + bspliner->SetInput(MakeGraft(this->GetTimeVaryingVelocityFieldControlPointLattice())); bspliner->SetSplineOrder(this->m_SplineOrder); bspliner->SetSpacing(this->m_VelocityFieldSpacing); bspliner->SetSize(this->m_VelocityFieldSize); @@ -75,12 +72,9 @@ TimeVaryingBSplineVelocityFieldTransform::Inte using IntegratorType = TimeVaryingVelocityFieldIntegrationImageFilter; - // A graft, so that releasing this integrator's input leaves the sampled field buffered for the inverse integrator. - auto bsplinerOutputGraft = VelocityFieldType::New(); - bsplinerOutputGraft->Graft(bsplinerOutput); - auto integrator = IntegratorType::New(); - integrator->SetInput(bsplinerOutputGraft); + // A graft, so that releasing this integrator's input leaves the sampled field buffered for the inverse integrator. + integrator->SetInput(MakeGraft(bsplinerOutput)); integrator->SetLowerTimeBound(this->GetLowerTimeBound()); integrator->SetUpperTimeBound(this->GetUpperTimeBound()); diff --git a/Modules/Filtering/DisplacementField/include/itkTimeVaryingVelocityFieldTransform.hxx b/Modules/Filtering/DisplacementField/include/itkTimeVaryingVelocityFieldTransform.hxx index 680b6864bbe..59dfc542e5e 100644 --- a/Modules/Filtering/DisplacementField/include/itkTimeVaryingVelocityFieldTransform.hxx +++ b/Modules/Filtering/DisplacementField/include/itkTimeVaryingVelocityFieldTransform.hxx @@ -32,13 +32,6 @@ TimeVaryingVelocityFieldTransform::IntegrateVe { using IntegratorType = TimeVaryingVelocityFieldIntegrationImageFilter; - // Grafts, so that releasing the integrators' inputs leaves the velocity field buffered. - const auto makeVelocityFieldGraft = [this]() { - auto graft = VelocityFieldType::New(); - graft->Graft(this->GetVelocityField()); - return graft; - }; - // The integrators point the shared velocity field interpolator at their input; point it back afterward. const auto restoreInterpolatorInput = [this]() { if (this->GetVelocityFieldInterpolator()) @@ -49,7 +42,7 @@ TimeVaryingVelocityFieldTransform::IntegrateVe try { auto integrator = IntegratorType::New(); - integrator->SetInput(makeVelocityFieldGraft()); + integrator->SetInput(MakeGraft(this->GetVelocityField())); integrator->SetLowerTimeBound(this->GetLowerTimeBound()); integrator->SetUpperTimeBound(this->GetUpperTimeBound()); @@ -68,7 +61,7 @@ TimeVaryingVelocityFieldTransform::IntegrateVe this->GetModifiableInterpolator()->SetInputImage(displacementField); auto inverseIntegrator = IntegratorType::New(); - inverseIntegrator->SetInput(makeVelocityFieldGraft()); + inverseIntegrator->SetInput(MakeGraft(this->GetVelocityField())); inverseIntegrator->SetLowerTimeBound(this->GetUpperTimeBound()); inverseIntegrator->SetUpperTimeBound(this->GetLowerTimeBound()); if (!this->GetVelocityFieldInterpolator()) diff --git a/Modules/Filtering/DistanceMap/include/itkDirectedHausdorffDistanceImageFilter.hxx b/Modules/Filtering/DistanceMap/include/itkDirectedHausdorffDistanceImageFilter.hxx index 1b95486d7f0..a9fee4dd954 100644 --- a/Modules/Filtering/DistanceMap/include/itkDirectedHausdorffDistanceImageFilter.hxx +++ b/Modules/Filtering/DistanceMap/include/itkDirectedHausdorffDistanceImageFilter.hxx @@ -119,10 +119,7 @@ DirectedHausdorffDistanceImageFilter::BeforeThreaded using FilterType = itk::SignedMaurerDistanceMapImageFilter; auto filter = FilterType::New(); - auto input2 = InputImage2Type::New(); - input2->Graft(const_cast(this->GetInput2())); - - filter->SetInput(input2); + filter->SetInput(MakeGraft(this->GetInput2())); filter->SetSquaredDistance(false); filter->SetUseImageSpacing(m_UseImageSpacing); filter->Update(); diff --git a/Modules/Filtering/ImageFeature/include/itkBilateralImageFilter.hxx b/Modules/Filtering/ImageFeature/include/itkBilateralImageFilter.hxx index 37886ddf5a0..5bca379ed5b 100644 --- a/Modules/Filtering/ImageFeature/include/itkBilateralImageFilter.hxx +++ b/Modules/Filtering/ImageFeature/include/itkBilateralImageFilter.hxx @@ -190,13 +190,10 @@ BilateralImageFilter::BeforeThreadedGenerateData() // Build a lookup table for the range gaussian - auto localInput = TInputImage::New(); - localInput->Graft(this->GetInput()); - // First, determine the min and max intensity range auto statistics = StatisticsImageFilter::New(); - statistics->SetInput(localInput); + statistics->SetInput(MakeGraft(this->GetInput())); statistics->Update(); // Now create the lookup table whose domain runs from 0.0 to diff --git a/Modules/Filtering/ImageFeature/include/itkCannyEdgeDetectionImageFilter.hxx b/Modules/Filtering/ImageFeature/include/itkCannyEdgeDetectionImageFilter.hxx index 61a4a18ce9e..c8e37030712 100644 --- a/Modules/Filtering/ImageFeature/include/itkCannyEdgeDetectionImageFilter.hxx +++ b/Modules/Filtering/ImageFeature/include/itkCannyEdgeDetectionImageFilter.hxx @@ -191,13 +191,11 @@ CannyEdgeDetectionImageFilter::GenerateData() // Use grafting of the input and output of this filter to isolate // the mini-pipeline and other modifications from the pipeline. - auto input = InputImageType::New(); - input->Graft(const_cast(this->GetInput())); + auto input = MakeGraft(this->GetInput()); // Allocate the output, and graft Superclass::AllocateOutputs(); - auto output = OutputImageType::New(); - output->Graft(this->GetOutput()); + auto output = MakeGraft(this->GetOutput()); this->m_OutputImage = output; auto zeroCrossFilter = ZeroCrossingImageFilter::New(); diff --git a/Modules/Filtering/ImageFeature/include/itkDiscreteGaussianDerivativeImageFilter.hxx b/Modules/Filtering/ImageFeature/include/itkDiscreteGaussianDerivativeImageFilter.hxx index e3854335f1f..3b9a02f63ad 100644 --- a/Modules/Filtering/ImageFeature/include/itkDiscreteGaussianDerivativeImageFilter.hxx +++ b/Modules/Filtering/ImageFeature/include/itkDiscreteGaussianDerivativeImageFilter.hxx @@ -106,8 +106,7 @@ DiscreteGaussianDerivativeImageFilter::GenerateData() // Create an internal image to protect the input image's metadata // (e.g. RequestedRegion). The StreamingImageFilter changes the // requested region as part of its normal processing. - auto localInput = TInputImage::New(); - localInput->Graft(this->GetInput()); + auto localInput = MakeGraft(this->GetInput()); // Type of the pixel to use for intermediate results using RealOutputPixelType = typename NumericTraits::RealType; diff --git a/Modules/Filtering/ImageFeature/include/itkHoughTransform2DCirclesImageFilter.hxx b/Modules/Filtering/ImageFeature/include/itkHoughTransform2DCirclesImageFilter.hxx index 15572741b71..d704dfd3adc 100644 --- a/Modules/Filtering/ImageFeature/include/itkHoughTransform2DCirclesImageFilter.hxx +++ b/Modules/Filtering/ImageFeature/include/itkHoughTransform2DCirclesImageFilter.hxx @@ -200,14 +200,10 @@ HoughTransform2DCirclesImageFilter; - // The variable "outputImage" is only used as input to gaussianFilter. - // It should not be modified, because GetOutput(0) should not be changed. - const auto outputImage = OutputImageType::New(); - outputImage->Graft(this->GetOutput(0)); - const auto gaussianFilter = DiscreteGaussianImageFilter::New(); - gaussianFilter->SetInput(outputImage); // The output is the accumulator image + // A graft, so that gaussianFilter cannot change GetOutput(0). + gaussianFilter->SetInput(MakeGraft(this->GetOutput(0))); // The output is the accumulator image gaussianFilter->SetVariance(m_Variance); gaussianFilter->SetUseImageSpacing(m_UseImageSpacing); diff --git a/Modules/Filtering/ImageFeature/include/itkLaplacianSharpeningImageFilter.hxx b/Modules/Filtering/ImageFeature/include/itkLaplacianSharpeningImageFilter.hxx index 2aee02fd236..510a162c63a 100644 --- a/Modules/Filtering/ImageFeature/include/itkLaplacianSharpeningImageFilter.hxx +++ b/Modules/Filtering/ImageFeature/include/itkLaplacianSharpeningImageFilter.hxx @@ -46,8 +46,7 @@ void LaplacianSharpeningImageFilter::GenerateData() { - auto localInput = TInputImage::New(); - localInput->Graft(this->GetInput()); + auto localInput = MakeGraft(this->GetInput()); // Calculate the needed input image statistics diff --git a/Modules/Filtering/ImageFeature/include/itkUnsharpMaskImageFilter.hxx b/Modules/Filtering/ImageFeature/include/itkUnsharpMaskImageFilter.hxx index 7276f293dc2..8df7a263091 100644 --- a/Modules/Filtering/ImageFeature/include/itkUnsharpMaskImageFilter.hxx +++ b/Modules/Filtering/ImageFeature/include/itkUnsharpMaskImageFilter.hxx @@ -70,10 +70,8 @@ template ::GenerateData() { - auto input = TInputImage::New(); - input->Graft(const_cast(this->GetInput())); auto gaussianF = GaussianType::New(); - gaussianF->SetInput(input); + gaussianF->SetInput(MakeGraft(this->GetInput())); gaussianF->SetSigmaArray(m_Sigmas); gaussianF->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits()); diff --git a/Modules/Filtering/ImageGradient/include/itkGradientRecursiveGaussianImageFilter.hxx b/Modules/Filtering/ImageGradient/include/itkGradientRecursiveGaussianImageFilter.hxx index 0c3d9c2282d..b0d55c223c4 100644 --- a/Modules/Filtering/ImageGradient/include/itkGradientRecursiveGaussianImageFilter.hxx +++ b/Modules/Filtering/ImageGradient/include/itkGradientRecursiveGaussianImageFilter.hxx @@ -212,9 +212,7 @@ GradientRecursiveGaussianImageFilter::GenerateData() { if (graftInput) { - auto inputGraft = TInputImage::New(); - inputGraft->Graft(inputImage); - m_DerivativeFilter->SetInput(inputGraft); + m_DerivativeFilter->SetInput(MakeGraft(inputImage)); } unsigned int i = 0; diff --git a/Modules/Filtering/ImageIntensity/include/itkNormalizeImageFilter.hxx b/Modules/Filtering/ImageIntensity/include/itkNormalizeImageFilter.hxx index da2cc0ab6cf..11009fa4aa6 100644 --- a/Modules/Filtering/ImageIntensity/include/itkNormalizeImageFilter.hxx +++ b/Modules/Filtering/ImageIntensity/include/itkNormalizeImageFilter.hxx @@ -63,8 +63,7 @@ NormalizeImageFilter::GenerateData() // Gather statistics - auto localInput = TInputImage::New(); - localInput->Graft(this->GetInput()); + auto localInput = MakeGraft(this->GetInput()); m_StatisticsFilter->SetInput(localInput); m_StatisticsFilter->Update(); diff --git a/Modules/Filtering/ImageStatistics/include/itkAdaptiveHistogramEqualizationImageFilter.hxx b/Modules/Filtering/ImageStatistics/include/itkAdaptiveHistogramEqualizationImageFilter.hxx index 9810b6a91f1..c51be032a15 100644 --- a/Modules/Filtering/ImageStatistics/include/itkAdaptiveHistogramEqualizationImageFilter.hxx +++ b/Modules/Filtering/ImageStatistics/include/itkAdaptiveHistogramEqualizationImageFilter.hxx @@ -36,8 +36,7 @@ template void AdaptiveHistogramEqualizationImageFilter::BeforeThreadedGenerateData() { - auto input = ImageType::New(); - input->Graft(const_cast(this->GetInput())); + auto input = MakeGraft(this->GetInput()); // Calculate min and max gray level of an input image // NOTE: This computation of min/max means that this filter should diff --git a/Modules/Filtering/IsotropicWavelets/include/itkWaveletCoeffsPhaseAnalyzisImageFilter.hxx b/Modules/Filtering/IsotropicWavelets/include/itkWaveletCoeffsPhaseAnalyzisImageFilter.hxx index c88e5ce1a33..e621033a828 100644 --- a/Modules/Filtering/IsotropicWavelets/include/itkWaveletCoeffsPhaseAnalyzisImageFilter.hxx +++ b/Modules/Filtering/IsotropicWavelets/include/itkWaveletCoeffsPhaseAnalyzisImageFilter.hxx @@ -73,8 +73,7 @@ WaveletCoeffsPhaseAnalyzisImageFilter::GenerateDat // ==================================================================== // ==================== Graft Input Declaration ======================= // ==================================================================== - typename ImageType::Pointer input = ImageType::New(); - input->Graft(const_cast(this->GetInput())); + typename ImageType::Pointer input = MakeGraft(this->GetInput()); m_FFTPadFilter->SetInput(input); // ==================== Filter Inter Connection ======================= diff --git a/Modules/Filtering/IsotropicWavelets/include/itkWaveletCoeffsSpatialDomainImageFilter.hxx b/Modules/Filtering/IsotropicWavelets/include/itkWaveletCoeffsSpatialDomainImageFilter.hxx index 8211d4c16a0..cf02d71eee9 100644 --- a/Modules/Filtering/IsotropicWavelets/include/itkWaveletCoeffsSpatialDomainImageFilter.hxx +++ b/Modules/Filtering/IsotropicWavelets/include/itkWaveletCoeffsSpatialDomainImageFilter.hxx @@ -61,8 +61,7 @@ void WaveletCoeffsSpatialDomainImageFilter::GenerateData() { - typename ImageType::Pointer input = ImageType::New(); - input->Graft(const_cast(this->GetInput())); + typename ImageType::Pointer input = MakeGraft(this->GetInput()); m_FFTPadFilter->SetInput(input); m_ZeroDCFilter->SetInput(m_FFTPadFilter->GetOutput()); diff --git a/Modules/Filtering/ParabolicMorphology/include/itkParabolicOpenCloseSafeBorderImageFilter.hxx b/Modules/Filtering/ParabolicMorphology/include/itkParabolicOpenCloseSafeBorderImageFilter.hxx index 93d4bc40c31..e38280169b0 100644 --- a/Modules/Filtering/ParabolicMorphology/include/itkParabolicOpenCloseSafeBorderImageFilter.hxx +++ b/Modules/Filtering/ParabolicMorphology/include/itkParabolicOpenCloseSafeBorderImageFilter.hxx @@ -37,8 +37,7 @@ ParabolicOpenCloseSafeBorderImageFilter::Gene typename PadFilterType::SizeType BoundsSize; if (this->m_SafeBorder) { - auto localInput = TInputImage::New(); - localInput->Graft(this->GetInput()); + auto localInput = MakeGraft(this->GetInput()); // need to compute some image statistics and determine the padding // extent. This will almost certainly be an over estimate @@ -81,8 +80,7 @@ ParabolicOpenCloseSafeBorderImageFilter::Gene } else { - auto localInput = TInputImage::New(); - localInput->Graft(this->GetInput()); + auto localInput = MakeGraft(this->GetInput()); inputImage = localInput; } diff --git a/Modules/Filtering/Smoothing/include/itkDiscreteGaussianImageFilter.hxx b/Modules/Filtering/Smoothing/include/itkDiscreteGaussianImageFilter.hxx index 160035414b5..0044854c6ab 100644 --- a/Modules/Filtering/Smoothing/include/itkDiscreteGaussianImageFilter.hxx +++ b/Modules/Filtering/Smoothing/include/itkDiscreteGaussianImageFilter.hxx @@ -159,8 +159,7 @@ DiscreteGaussianImageFilter::GenerateData() // Create an internal image to protect the input image's metadata // (e.g. RequestedRegion). The StreamingImageFilter changes the // requested region as part of its normal processing. - auto localInput = TInputImage::New(); - localInput->Graft(this->GetInput()); + auto localInput = MakeGraft(this->GetInput()); // Determine the dimensionality to filter unsigned int filterDimensionality = m_FilterDimensionality; diff --git a/Modules/Filtering/Smoothing/include/itkFFTDiscreteGaussianImageFilter.hxx b/Modules/Filtering/Smoothing/include/itkFFTDiscreteGaussianImageFilter.hxx index a159b5255b1..85377908bef 100644 --- a/Modules/Filtering/Smoothing/include/itkFFTDiscreteGaussianImageFilter.hxx +++ b/Modules/Filtering/Smoothing/include/itkFFTDiscreteGaussianImageFilter.hxx @@ -167,8 +167,7 @@ FFTDiscreteGaussianImageFilter::GenerateData() // Create an internal image to protect the input image's metadata // (e.g. RequestedRegion). The StreamingImageFilter changes the // requested region as part of its normal processing. - auto localInput = TInputImage::New(); - localInput->Graft(this->GetInput()); + auto localInput = MakeGraft(this->GetInput()); // Determine the dimensionality to filter unsigned int filterDimensionality = this->GetFilterDimensionality(); diff --git a/Modules/Filtering/StructuralSimilarity/include/itkStructuralSimilarityImageFilter.hxx b/Modules/Filtering/StructuralSimilarity/include/itkStructuralSimilarityImageFilter.hxx index 0370827969c..3c6cbb69d64 100644 --- a/Modules/Filtering/StructuralSimilarity/include/itkStructuralSimilarityImageFilter.hxx +++ b/Modules/Filtering/StructuralSimilarity/include/itkStructuralSimilarityImageFilter.hxx @@ -188,10 +188,8 @@ StructuralSimilarityImageFilter::GenerateData() }; // Grafting the inputs keeps the internal Update() calls from propagating upstream. const auto toRealImage = [](const InputImageType * input) -> RealImagePointer { - auto grafted = InputImageType::New(); - grafted->Graft(input); auto cast = CastFilterType::New(); - cast->SetInput(grafted); + cast->SetInput(MakeGraft(input)); cast->Update(); return cast->GetOutput(); }; diff --git a/Modules/Filtering/TextureFeatures/include/itkCoocurrenceTextureFeaturesImageFilter.hxx b/Modules/Filtering/TextureFeatures/include/itkCoocurrenceTextureFeaturesImageFilter.hxx index efcf6ce9b31..e43df0b4131 100644 --- a/Modules/Filtering/TextureFeatures/include/itkCoocurrenceTextureFeaturesImageFilter.hxx +++ b/Modules/Filtering/TextureFeatures/include/itkCoocurrenceTextureFeaturesImageFilter.hxx @@ -85,8 +85,7 @@ void CoocurrenceTextureFeaturesImageFilter::BeforeThreadedGenerateData() { - typename TInputImage::Pointer input = InputImageType::New(); - input->Graft(const_cast(this->GetInput())); + typename TInputImage::Pointer input = MakeGraft(this->GetInput()); using DigitizerFunctorType = Digitizer; @@ -96,8 +95,7 @@ CoocurrenceTextureFeaturesImageFilter::Be typename FilterType::Pointer filter = FilterType::New(); if (this->GetMaskImage() != nullptr) { - typename TMaskImage::Pointer mask = MaskImageType::New(); - mask->Graft(const_cast(this->GetMaskImage())); + typename TMaskImage::Pointer mask = MakeGraft(this->GetMaskImage()); filter->SetInput1(mask); } else diff --git a/Modules/Filtering/TextureFeatures/include/itkRunLengthTextureFeaturesImageFilter.hxx b/Modules/Filtering/TextureFeatures/include/itkRunLengthTextureFeaturesImageFilter.hxx index a21f94f9878..ce919787a0c 100644 --- a/Modules/Filtering/TextureFeatures/include/itkRunLengthTextureFeaturesImageFilter.hxx +++ b/Modules/Filtering/TextureFeatures/include/itkRunLengthTextureFeaturesImageFilter.hxx @@ -84,8 +84,7 @@ void RunLengthTextureFeaturesImageFilter::BeforeThreadedGenerateData() { - typename TInputImage::Pointer input = InputImageType::New(); - input->Graft(const_cast(this->GetInput())); + typename TInputImage::Pointer input = MakeGraft(this->GetInput()); using DigitizerFunctorType = Digitizer; @@ -96,8 +95,7 @@ RunLengthTextureFeaturesImageFilter::Befo typename FilterType::Pointer filter = FilterType::New(); if (this->GetMaskImage() != nullptr) { - typename TMaskImage::Pointer mask = MaskImageType::New(); - mask->Graft(const_cast(this->GetMaskImage())); + typename TMaskImage::Pointer mask = MakeGraft(this->GetMaskImage()); filter->SetInput1(mask); } else diff --git a/Modules/Filtering/Thickness3D/include/itkMedialThicknessImageFilter3D.hxx b/Modules/Filtering/Thickness3D/include/itkMedialThicknessImageFilter3D.hxx index 50ce52132c1..b0646436385 100644 --- a/Modules/Filtering/Thickness3D/include/itkMedialThicknessImageFilter3D.hxx +++ b/Modules/Filtering/Thickness3D/include/itkMedialThicknessImageFilter3D.hxx @@ -41,8 +41,7 @@ template void MedialThicknessImageFilter3D::GenerateData() { - typename InputImageType::Pointer input = InputImageType::New(); - input->Graft(dynamic_cast(this->GetInput())); + typename InputImageType::Pointer input = MakeGraft(this->GetInput()); m_DistanceFilter->SetInput(input); m_ThinningFilter->SetInput(input); m_MultiplyFilter->Update(); diff --git a/Modules/Registration/Common/include/itkTimeVaryingBSplineVelocityFieldTransformParametersAdaptor.hxx b/Modules/Registration/Common/include/itkTimeVaryingBSplineVelocityFieldTransformParametersAdaptor.hxx index d8f869f47d8..f4377edf037 100644 --- a/Modules/Registration/Common/include/itkTimeVaryingBSplineVelocityFieldTransformParametersAdaptor.hxx +++ b/Modules/Registration/Common/include/itkTimeVaryingBSplineVelocityFieldTransformParametersAdaptor.hxx @@ -277,11 +277,9 @@ TimeVaryingBSplineVelocityFieldTransformParametersAdaptor::AdaptTran { using SelectionFilterType = VectorIndexSelectionCastImageFilter; - // A graft, so that releasing the selector's input leaves the lattice buffered for the next component. - auto latticeGraft = TimeVaryingVelocityFieldControlPointLatticeType::New(); - latticeGraft->Graft(this->m_Transform->GetTimeVaryingVelocityFieldControlPointLattice()); auto selector = SelectionFilterType::New(); - selector->SetInput(latticeGraft); + // A graft, so that releasing the selector's input leaves the lattice buffered for the next component. + selector->SetInput(MakeGraft(this->m_Transform->GetTimeVaryingVelocityFieldControlPointLattice())); selector->SetIndex(j); /* Set the coefficient image as the input of the upsampler filter. diff --git a/Modules/Registration/Metricsv4/include/itkImageToImageMetricv4.hxx b/Modules/Registration/Metricsv4/include/itkImageToImageMetricv4.hxx index a6eeddb3521..33478da37e8 100644 --- a/Modules/Registration/Metricsv4/include/itkImageToImageMetricv4.hxx +++ b/Modules/Registration/Metricsv4/include/itkImageToImageMetricv4.hxx @@ -436,9 +436,7 @@ ImageToImageMetricv4m_FixedImage->ShouldIReleaseData()) { - auto graft = FixedImageType::New(); - graft->Graft(this->m_FixedImage); - this->m_FixedImageGradientFilter->SetInput(graft); + this->m_FixedImageGradientFilter->SetInput(MakeGraft(this->m_FixedImage)); } else { @@ -461,9 +459,7 @@ ImageToImageMetricv4m_MovingImage->ShouldIReleaseData()) { - auto graft = MovingImageType::New(); - graft->Graft(this->m_MovingImage); - this->m_MovingImageGradientFilter->SetInput(graft); + this->m_MovingImageGradientFilter->SetInput(MakeGraft(this->m_MovingImage)); } else { diff --git a/Modules/Registration/RegistrationMethodsv4/include/itkBSplineSyNImageRegistrationMethod.hxx b/Modules/Registration/RegistrationMethodsv4/include/itkBSplineSyNImageRegistrationMethod.hxx index 570a2a90765..595fc5dcaa5 100644 --- a/Modules/Registration/RegistrationMethodsv4/include/itkBSplineSyNImageRegistrationMethod.hxx +++ b/Modules/Registration/RegistrationMethodsv4/include/itkBSplineSyNImageRegistrationMethod.hxx @@ -161,8 +161,8 @@ BSplineSyNImageRegistrationMethod; auto fixedComposer = ComposerType::New(); - fixedComposer->SetDisplacementField(this->MakeGraft(fixedToMiddleSmoothUpdateField)); - fixedComposer->SetWarpingField(this->MakeGraft(this->m_FixedToMiddleTransform->GetDisplacementField())); + fixedComposer->SetDisplacementField(MakeGraft(fixedToMiddleSmoothUpdateField)); + fixedComposer->SetWarpingField(MakeGraft(this->m_FixedToMiddleTransform->GetDisplacementField())); fixedComposer->Update(); const DisplacementFieldPointer fixedToMiddleSmoothTotalFieldTmp = @@ -172,8 +172,8 @@ BSplineSyNImageRegistrationMethodSetDisplacementField(this->MakeGraft(movingToMiddleSmoothUpdateField)); - movingComposer->SetWarpingField(this->MakeGraft(this->m_MovingToMiddleTransform->GetDisplacementField())); + movingComposer->SetDisplacementField(MakeGraft(movingToMiddleSmoothUpdateField)); + movingComposer->SetWarpingField(MakeGraft(this->m_MovingToMiddleTransform->GetDisplacementField())); movingComposer->Update(); const DisplacementFieldPointer movingToMiddleSmoothTotalFieldTmp = @@ -336,7 +336,7 @@ typename BSplineSyNImageRegistrationMethod; auto fixedMaskResampler = MaskResamplerType::New(); fixedMaskResampler->SetTransform(fixedTransform); - fixedMaskResampler->SetInput(this->MakeGraft( + fixedMaskResampler->SetInput(MakeGraft( dynamic_cast(const_cast(fixedImageMasks[0].GetPointer())) ->GetImage())); fixedMaskResampler->SetOutputParametersFromImage(virtualDomainImage); diff --git a/Modules/Registration/RegistrationMethodsv4/include/itkImageRegistrationMethodv4.h b/Modules/Registration/RegistrationMethodsv4/include/itkImageRegistrationMethodv4.h index d61025e7d14..d5b651538b6 100644 --- a/Modules/Registration/RegistrationMethodsv4/include/itkImageRegistrationMethodv4.h +++ b/Modules/Registration/RegistrationMethodsv4/include/itkImageRegistrationMethodv4.h @@ -540,26 +540,6 @@ class ITK_TEMPLATE_EXPORT ImageRegistrationMethodv4 : public ProcessObject InitializeCenterOfLinearOutputTransform(); protected: - /** A graft of an image, so that a mini-pipeline that releases its inputs leaves the image buffered. */ - template - static typename TImage::Pointer - MakeGraft(const TImage * image) - { - if (image == nullptr) - { - return nullptr; - } - auto graft = TImage::New(); - graft->Graft(image); - return graft; - } - template - static typename TImage::Pointer - MakeGraft(const SmartPointer & image) - { - return MakeGraft(image.GetPointer()); - } - ImageRegistrationMethodv4(); ~ImageRegistrationMethodv4() override = default; void diff --git a/Modules/Registration/RegistrationMethodsv4/include/itkImageRegistrationMethodv4.hxx b/Modules/Registration/RegistrationMethodsv4/include/itkImageRegistrationMethodv4.hxx index efd0420ddb7..6118709f23e 100644 --- a/Modules/Registration/RegistrationMethodsv4/include/itkImageRegistrationMethodv4.hxx +++ b/Modules/Registration/RegistrationMethodsv4/include/itkImageRegistrationMethodv4.hxx @@ -449,7 +449,7 @@ ImageRegistrationMethodv4SetShrinkFactors(this->m_ShrinkFactorsPerLevel[level]); - shrinkFilter->SetInput(this->MakeGraft(this->m_VirtualDomainImage)); + shrinkFilter->SetInput(MakeGraft(this->m_VirtualDomainImage)); currentLevelVirtualDomainImage = shrinkFilter->GetOutput(); currentLevelVirtualDomainImage->Update(); @@ -502,7 +502,7 @@ ImageRegistrationMethodv4; auto caster = CasterType::New(); - caster->SetInput(this->MakeGraft(currentLevelVirtualDomainImage)); + caster->SetInput(MakeGraft(currentLevelVirtualDomainImage)); caster->Update(); dynamic_cast(multiMetric->GetMetricQueue()[n].GetPointer()) @@ -553,7 +553,7 @@ ImageRegistrationMethodv4; auto caster = CasterType::New(); - caster->SetInput(this->MakeGraft(currentLevelVirtualDomainImage)); + caster->SetInput(MakeGraft(currentLevelVirtualDomainImage)); caster->Update(); pointSetMetric->SetVirtualDomainFromImage(caster->GetOutput()); @@ -607,7 +607,7 @@ ImageRegistrationMethodv4SetSigmaArray(fixedImageSigmaArray); - fixedImageSmoothingFilter->SetInput(this->MakeGraft(this->GetFixedImage(n))); + fixedImageSmoothingFilter->SetInput(MakeGraft(this->GetFixedImage(n))); this->m_FixedSmoothImages[n] = fixedImageSmoothingFilter->GetOutput(); fixedImageSmoothingFilter->Update(); @@ -627,7 +627,7 @@ ImageRegistrationMethodv4SetSigmaArray(movingImageSigmaArray); - movingImageSmoothingFilter->SetInput(this->MakeGraft(this->GetMovingImage(n))); + movingImageSmoothingFilter->SetInput(MakeGraft(this->GetMovingImage(n))); this->m_MovingSmoothImages[n] = movingImageSmoothingFilter->GetOutput(); movingImageSmoothingFilter->Update(); diff --git a/Modules/Registration/RegistrationMethodsv4/include/itkSyNImageRegistrationMethod.hxx b/Modules/Registration/RegistrationMethodsv4/include/itkSyNImageRegistrationMethod.hxx index 6cb565bfccb..5e1e0e8344c 100644 --- a/Modules/Registration/RegistrationMethodsv4/include/itkSyNImageRegistrationMethod.hxx +++ b/Modules/Registration/RegistrationMethodsv4/include/itkSyNImageRegistrationMethod.hxx @@ -219,8 +219,8 @@ SyNImageRegistrationMethodSetDisplacementField(this->MakeGraft(fixedToMiddleSmoothUpdateField)); - fixedComposer->SetWarpingField(this->MakeGraft(this->m_FixedToMiddleTransform->GetDisplacementField())); + fixedComposer->SetDisplacementField(MakeGraft(fixedToMiddleSmoothUpdateField)); + fixedComposer->SetWarpingField(MakeGraft(this->m_FixedToMiddleTransform->GetDisplacementField())); fixedComposer->Update(); fixedToMiddleSmoothTotalFieldTmp = fixedComposer->GetOutput(); @@ -236,8 +236,8 @@ SyNImageRegistrationMethodSetDisplacementField(this->MakeGraft(movingToMiddleSmoothUpdateField)); - movingComposer->SetWarpingField(this->MakeGraft(this->m_MovingToMiddleTransform->GetDisplacementField())); + movingComposer->SetDisplacementField(MakeGraft(movingToMiddleSmoothUpdateField)); + movingComposer->SetWarpingField(MakeGraft(this->m_MovingToMiddleTransform->GetDisplacementField())); movingComposer->Update(); movingToMiddleSmoothTotalFieldTmp = movingComposer->GetOutput(); @@ -635,7 +635,7 @@ SyNImageRegistrationMethod; auto resampler = ResamplerType::New(); - resampler->SetInput(this->MakeGraft(image)); + resampler->SetInput(MakeGraft(image)); resampler->SetTransform(transform); if (interpolation == ResampleInterpolation::NearestNeighbor) { @@ -707,7 +707,7 @@ typename SyNImageRegistrationMethod; auto multiplier = MultiplierType::New(); - multiplier->SetInput(this->MakeGraft(updateField)); + multiplier->SetInput(MakeGraft(updateField)); multiplier->SetConstant(scale); typename DisplacementFieldType::Pointer scaledUpdateField = multiplier->GetOutput(); @@ -730,8 +730,8 @@ typename SyNImageRegistrationMethod; auto inverter = InverterType::New(); - inverter->SetInput(this->MakeGraft(field)); - inverter->SetInverseFieldInitialEstimate(this->MakeGraft(inverseFieldEstimate)); + inverter->SetInput(MakeGraft(field)); + inverter->SetInverseFieldInitialEstimate(MakeGraft(inverseFieldEstimate)); inverter->SetMaximumNumberOfIterations(20); inverter->SetMeanErrorToleranceThreshold(0.001); inverter->SetMaxErrorToleranceThreshold(0.1); @@ -762,8 +762,7 @@ typename SyNImageRegistrationMethodGraft(field); + const auto input = MakeGraft(field); DisplacementFieldPointer smoothField; @@ -908,13 +907,13 @@ SyNImageRegistrationMethod; auto composer = ComposerType::New(); - composer->SetDisplacementField(this->MakeGraft(this->m_MovingToMiddleTransform->GetInverseDisplacementField())); - composer->SetWarpingField(this->MakeGraft(this->m_FixedToMiddleTransform->GetDisplacementField())); + composer->SetDisplacementField(MakeGraft(this->m_MovingToMiddleTransform->GetInverseDisplacementField())); + composer->SetWarpingField(MakeGraft(this->m_FixedToMiddleTransform->GetDisplacementField())); composer->Update(); auto inverseComposer = ComposerType::New(); - inverseComposer->SetDisplacementField(this->MakeGraft(this->m_FixedToMiddleTransform->GetInverseDisplacementField())); - inverseComposer->SetWarpingField(this->MakeGraft(this->m_MovingToMiddleTransform->GetDisplacementField())); + inverseComposer->SetDisplacementField(MakeGraft(this->m_FixedToMiddleTransform->GetInverseDisplacementField())); + inverseComposer->SetWarpingField(MakeGraft(this->m_MovingToMiddleTransform->GetDisplacementField())); inverseComposer->Update(); this->m_OutputTransform->SetDisplacementField(composer->GetOutput()); diff --git a/Modules/Registration/RegistrationMethodsv4/include/itkTimeVaryingBSplineVelocityFieldImageRegistrationMethod.hxx b/Modules/Registration/RegistrationMethodsv4/include/itkTimeVaryingBSplineVelocityFieldImageRegistrationMethod.hxx index 1316d3777a8..ac4f1c06356 100644 --- a/Modules/Registration/RegistrationMethodsv4/include/itkTimeVaryingBSplineVelocityFieldImageRegistrationMethod.hxx +++ b/Modules/Registration/RegistrationMethodsv4/include/itkTimeVaryingBSplineVelocityFieldImageRegistrationMethod.hxx @@ -645,7 +645,7 @@ TimeVaryingBSplineVelocityFieldImageRegistrationMethod< { using FixedResamplerType = ResampleImageFilter; auto fixedResampler = FixedResamplerType::New(); - fixedResampler->SetInput(this->MakeGraft(fixedImages[n])); + fixedResampler->SetInput(MakeGraft(fixedImages[n])); fixedResampler->SetTransform(fixedTransform); fixedResampler->SetOutputParametersFromImage(virtualDomainImage); fixedResampler->SetDefaultPixelValue(0); @@ -653,7 +653,7 @@ TimeVaryingBSplineVelocityFieldImageRegistrationMethod< using MovingResamplerType = ResampleImageFilter; auto movingResampler = MovingResamplerType::New(); - movingResampler->SetInput(this->MakeGraft(movingImages[n])); + movingResampler->SetInput(MakeGraft(movingImages[n])); movingResampler->SetTransform(movingTransform); movingResampler->SetOutputParametersFromImage(virtualDomainImage); movingResampler->SetDefaultPixelValue(0); @@ -675,7 +675,7 @@ TimeVaryingBSplineVelocityFieldImageRegistrationMethod< { using FixedResamplerType = ResampleImageFilter; auto fixedResampler = FixedResamplerType::New(); - fixedResampler->SetInput(this->MakeGraft(fixedImages[0])); + fixedResampler->SetInput(MakeGraft(fixedImages[0])); fixedResampler->SetTransform(fixedTransform); fixedResampler->SetOutputParametersFromImage(virtualDomainImage); fixedResampler->SetDefaultPixelValue(0); @@ -683,7 +683,7 @@ TimeVaryingBSplineVelocityFieldImageRegistrationMethod< using MovingResamplerType = ResampleImageFilter; auto movingResampler = MovingResamplerType::New(); - movingResampler->SetInput(this->MakeGraft(movingImages[0])); + movingResampler->SetInput(MakeGraft(movingImages[0])); movingResampler->SetTransform(movingTransform); movingResampler->SetOutputParametersFromImage(virtualDomainImage); movingResampler->SetDefaultPixelValue(0); @@ -735,7 +735,7 @@ TimeVaryingBSplineVelocityFieldImageRegistrationMethod< using FixedMaskResamplerType = ResampleImageFilter; auto fixedMaskResampler = FixedMaskResamplerType::New(); fixedMaskResampler->SetTransform(fixedTransform); - fixedMaskResampler->SetInput(this->MakeGraft( + fixedMaskResampler->SetInput(MakeGraft( dynamic_cast(const_cast(fixedImageMasks[0].GetPointer())) ->GetImage())); fixedMaskResampler->SetSize(virtualDomainImage->GetRequestedRegion().GetSize()); diff --git a/Modules/Registration/RegistrationMethodsv4/include/itkTimeVaryingVelocityFieldImageRegistrationMethodv4.hxx b/Modules/Registration/RegistrationMethodsv4/include/itkTimeVaryingVelocityFieldImageRegistrationMethodv4.hxx index 3ac8e906561..12e745ee756 100644 --- a/Modules/Registration/RegistrationMethodsv4/include/itkTimeVaryingVelocityFieldImageRegistrationMethodv4.hxx +++ b/Modules/Registration/RegistrationMethodsv4/include/itkTimeVaryingVelocityFieldImageRegistrationMethodv4.hxx @@ -207,7 +207,7 @@ TimeVaryingVelocityFieldImageRegistrationMethodv4; auto movingResampler = MovingResamplerType::New(); movingResampler->SetTransform(this->m_CompositeTransform); - movingResampler->SetInput(this->MakeGraft(this->m_MovingSmoothImages[n])); + movingResampler->SetInput(MakeGraft(this->m_MovingSmoothImages[n])); movingResampler->SetSize(virtualDomainImage->GetRequestedRegion().GetSize()); movingResampler->SetOutputOrigin(virtualDomainImage->GetOrigin()); movingResampler->SetOutputSpacing(virtualDomainImage->GetSpacing()); @@ -218,7 +218,7 @@ TimeVaryingVelocityFieldImageRegistrationMethodv4; auto fixedResampler = FixedResamplerType::New(); fixedResampler->SetTransform(fixedDisplacementFieldTransform); - fixedResampler->SetInput(this->MakeGraft(this->m_FixedSmoothImages[n])); + fixedResampler->SetInput(MakeGraft(this->m_FixedSmoothImages[n])); fixedResampler->SetSize(virtualDomainImage->GetRequestedRegion().GetSize()); fixedResampler->SetOutputOrigin(virtualDomainImage->GetOrigin()); fixedResampler->SetOutputSpacing(virtualDomainImage->GetSpacing()); diff --git a/Modules/Segmentation/LevelSets/include/itkCannySegmentationLevelSetFunction.hxx b/Modules/Segmentation/LevelSets/include/itkCannySegmentationLevelSetFunction.hxx index 885fe063ed0..a5672d9ef9a 100644 --- a/Modules/Segmentation/LevelSets/include/itkCannySegmentationLevelSetFunction.hxx +++ b/Modules/Segmentation/LevelSets/include/itkCannySegmentationLevelSetFunction.hxx @@ -71,14 +71,12 @@ template void CannySegmentationLevelSetFunction::CalculateDistanceImage() { - auto tempFeature = TFeatureImageType::New(); - // The minipipeline might muck with its feature image requested // region. The rest of the class relies on the feature image requested // region as specified by the original level set // filter. We make a temporary shallow copy of feature image to // build the distance image. - tempFeature->Graft(this->GetFeatureImage()); + auto tempFeature = MakeGraft(this->GetFeatureImage()); // AssignCannyInput either sets up a pipeline through the // CastImageFilter if TImageType != TFeatureImageType diff --git a/Modules/Segmentation/SuperPixel/include/itkSLICImageFilter.hxx b/Modules/Segmentation/SuperPixel/include/itkSLICImageFilter.hxx index 76bff9688da..ce3bbdc9221 100644 --- a/Modules/Segmentation/SuperPixel/include/itkSLICImageFilter.hxx +++ b/Modules/Segmentation/SuperPixel/include/itkSLICImageFilter.hxx @@ -101,8 +101,7 @@ SLICImageFilter::BeforeThreadedGenera { itkDebugMacro("Starting BeforeThreadedGenerateData"); - auto inputImage = InputImageType::New(); - inputImage->Graft(const_cast(this->GetInput())); + auto inputImage = MakeGraft(this->GetInput()); m_AverageResidual = NumericTraits::max();