Skip to content

ENH: Add itk::MakeGraft and use it for mini-pipeline grafts - #7246

Merged
hjmjohnson merged 2 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:enh-make-graft
Nov 27, 2026
Merged

hjmjohnson merged 2 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:enh-make-graft

Conversation

@hjmjohnson

Copy link
Copy Markdown
Member

Add itk::MakeGraft(data), a namespace-scope helper that grafts a data object for use as a mini-pipeline input, and use it at the hand-written sites (about 40) and in place of two class-local copies (the protected ImageRegistrationMethodv4::MakeGraft from #7095 and the private N4BiasFieldCorrectionImageFilter::MakeInputView from #7093). Closes #7243; a follow-up to #7093.
Merge danger: two-way door; additive API, behavior unchanged. The removed protected member is in no release.

Shape of the change
 // itkDataObject.h
+template <typename TDataObject>
+typename TDataObject::Pointer MakeGraft(const TDataObject * data);               // null gives null
+template <typename TDataObject>
+typename TDataObject::Pointer MakeGraft(const SmartPointer<TDataObject> & data);

 // each site, for example itkDiscreteGaussianImageFilter.hxx
-  auto localInput = TInputImage::New();
-  localInput->Graft(this->GetInput());
+  auto localInput = MakeGraft(this->GetInput());

 // itkMaskedFFTNormalizedCorrelationImageFilter.hxx: null-guarded masks collapse, since null gives null
-  MaskImagePointer fixedMask = nullptr;
-  if (this->GetFixedImageMask()) { fixedMask = ...; }
+  MaskImagePointer fixedMask = MakeGraft(this->GetFixedImageMask());

 // itkImageRegistrationMethodv4.h: protected static MakeGraft removed; callers drop `this->`
 // itkN4BiasFieldCorrectionImageFilter.h/.hxx: private MakeInputView removed
  • Commit 1 (ENH) adds the helper and itkMakeGraftGTest.cxx. Commit 2 (STYLE) converts the sites, including the output grafts in the Canny edge detection and Hough circles filters,, which also drops the const_casts the old form used, and updates the mini-pipeline example in the ImageSource.h documentation.
  • Left as they are: itkGPUDiscreteGaussianImageFilter.hxx grafts into a different image type, and itkInvertDisplacementFieldImageFilter.hxx re-grafts the same two objects every iteration (PERF: Reuse one composer across InvertDisplacementField iterations #7212), which MakeGraft cannot express.
  • The semantics follow the existing ImageRegistrationMethodv4 member, so its callers see no change.
Evidence

Local build, /Users/johnsonhj/src/ITK-build-test-tree/build-makegraft (Release, Homebrew clang; default modules plus IsotropicWavelets, ParabolicMorphology, TextureFeatures, Thickness3D, StructuralSimilarity, and ITKSuperPixel):

  • Full build: 0 errors. The only warning is pre-existing (itkRieszRotationMatrix.h:199, -Wreturn-stack-address).
  • ctest -R MakeGraft: 6/6 pass, including a check that a graft of a filter output has no source.
  • Test labels of the 20 touched modules: 664 pass, 0 fail. 113 Not Run are drivers of other modules that were not built; KWStyle tests excluded.
  • After the final review fixes, the tests of every file they touched (MaskedFFTNormalizedCorrelation, TimeVaryingVelocityField, HoughTransform2DCircles, CannySegmentationLevelSet, ImageRegistrationMethodv4, N4BiasField, MakeGraft): 43/43 pass.
  • pre-commit run --all-files passes.

Not built: GPU modules (ITK_USE_GPU=OFF).

AI assistance
  • Tool: Claude Code (claude-opus-5-5)
  • Role: found the sites and the two private copies, wrote the helper, the GTest, and the conversion script, and ran the local build and tests
  • Reviewed by the author before committing

@github-actions github-actions Bot added type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Enhancement Improvement of existing methods or implementation type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:Registration Issues affecting the Registration module area:Segmentation Issues affecting the Segmentation module labels Nov 26, 2026

@dzenanz dzenanz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly looks good on a glance. I have not looked at the tests.

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 InsightSoftwareConsortium#7243.
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 InsightSoftwareConsortium#7095, in no
release) and the private N4BiasFieldCorrectionImageFilter::MakeInputView
(from InsightSoftwareConsortium#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.
@hjmjohnson

Copy link
Copy Markdown
Member Author

Two force-pushes: the first (6a65cd0) inlines single-use grafts into SetInput(MakeGraft(...)) per review; the second (aa727d4) is a plain rebase on main, with both commits unchanged.

@hjmjohnson
hjmjohnson marked this pull request as ready for review November 26, 2026 19:14
@greptile-apps

greptile-apps Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

security Security Review

This PR appears safe to merge from a security standpoint.

What was checked
  • Every converted site replaces an existing New() plus Graft() pair with the same types, so the mini-pipelines see the same shared buffers as before.
  • The new MakeGraft helper in itkDataObject.h checks for null before grafting, and null optional inputs (the masks in MaskedFFTNormalizedCorrelation) still produce null, matching the old guarded code.
  • The dropped dynamic_cast in itkMedialThicknessImageFilter3D.hxx was an identity cast on an already-typed pointer, and ImageBase::Graft is null-safe, so the null-input error path stays safe.
  • The graft shares the pixel container through a refcounted smart pointer, so releasing the original image cannot leave the graft with a dangling buffer.
  • The PR adds no untrusted input path, allocation, dependency, or endpoint; the ImageSource.h change is a doc comment and the GTest is test-only code.

@greptile-apps

greptile-apps Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; no actionable issues were found.RetriggerView in GreptileConfidence Score: 5/5 Tier: plus

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Adds itk::MakeGraft and replaces repeated allocation-and-graft code across filters and registration methods.

  • Mini-pipelines can make a protective graft with one shared helper.
  • Filters use protective grafts when they pass data into mini-pipelines.
  • Registration pipelines use the shared helper for their grafted inputs.

Acknowledgments: hjmjohnson explicitly identifies removal of the unreleased protected helper as intentional. hjmjohnson also leaves the GPU cross-type graft and repeated iteration grafts unchanged because MakeGraft does not express those operations.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  Original["Original data object"] --> Helper["MakeGraft"]
  Helper --> Graft["New data object without a source"]
  Original --> Buffer["Shared bulk data"]
  Graft --> Buffer
  Graft --> Filter["Mini-pipeline input"]
Loading

Reviews (1) · Last reviewed commit: "STYLE: Use itk::MakeGraft for mini-pipel..." · Reviewed by Greptile

@hjmjohnson
hjmjohnson merged commit aeda6d6 into InsightSoftwareConsortium:main Nov 27, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:Registration Issues affecting the Registration module area:Segmentation Issues affecting the Segmentation module type:Enhancement Improvement of existing methods or implementation type:Infrastructure Infrastructure/ecosystem related changes, such as CMake or buildbots type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: Add itk::MakeGraft for mini-pipeline inputs

2 participants