Skip to content

Commit 498fee2

Browse files
blowekampdzenanz
authored andcommitted
ENH: PasteImageFilter input ND and constant value
Add support to PasteImageFilter to accept a constant value as input, following the model of the binary function filters. Add support for the SourceImage input to be a lower dimension than the destination input. The collapse dimension is specified with the DestinationSkipAxes parameter.
1 parent c5b1eb1 commit 498fee2

File tree

4 files changed

+537
-65
lines changed

4 files changed

+537
-65
lines changed

Modules/Filtering/ImageGrid/include/itkPasteImageFilter.h

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,28 @@
2020

2121
#include "itkInPlaceImageFilter.h"
2222
#include "itkSmartPointer.h"
23+
#include "itkSimpleDataObjectDecorator.h"
24+
#include <numeric>
2325

2426
namespace itk
2527
{
28+
29+
2630
/** \class PasteImageFilter
27-
* \brief Paste an image into another image
31+
* \brief Paste an image (or a constant value) into another image.
32+
*
33+
* PasteImageFilter allows a region in a destination image to be filled with a source image or a constant pixel value.
34+
* The SetDestinationIndex() method prescribes where in the destination input to start pasting data from the source
35+
* input. The SetSourceRegion method prescribes the section of the second image to paste into the first. When a
36+
* constant pixel value is set, the SourceRegion describes the size of the region filled. If the output requested
37+
* region does not include the SourceRegion after it has been repositioned to DestinationIndex, then the output will
38+
* just be a copy of the input.
2839
*
29-
* PasteImageFilter allows you to take a section of one image and
30-
* paste into another image. The SetDestinationIndex() method
31-
* prescribes where in the first input to start pasting data from the
32-
* second input. The SetSourceRegion method prescribes the section of
33-
* the second image to paste into the first. If the output requested
34-
* region does not include the SourceRegion after it has been
35-
* repositioned to DestinationIndex, then the output will just be
36-
* a copy of the input.
40+
* This filter supports running "InPlace" to efficiently reuse the destination image buffer for the output, removing
41+
* the need to copy the destination pixels to the output.
42+
*
43+
* When the source image has a lower dimension than the destination image then the DestinationSkipAxes parameter
44+
* specifies which axes in the destination image are set to 1 when copying the region or filling with a constant.
3745
*
3846
* The two inputs and output image will have the same pixel type.
3947
*
@@ -90,6 +98,10 @@ class ITK_TEMPLATE_EXPORT PasteImageFilter : public InPlaceImageFilter<TInputIma
9098
using InputImageSizeType = typename InputImageType::SizeType;
9199
using SourceImageIndexType = typename SourceImageType::IndexType;
92100
using SourceImageSizeType = typename SourceImageType::SizeType;
101+
using DecoratedSourceImagePixelType = SimpleDataObjectDecorator<SourceImagePixelType>;
102+
103+
using InputSkipAxesArrayType = FixedArray<bool, InputImageType::ImageDimension>;
104+
93105

94106
/** ImageDimension enumeration */
95107
static constexpr unsigned int InputImageDimension = InputImageType::ImageDimension;
@@ -101,6 +113,18 @@ class ITK_TEMPLATE_EXPORT PasteImageFilter : public InPlaceImageFilter<TInputIma
101113
itkSetMacro(DestinationIndex, InputImageIndexType);
102114
itkGetConstMacro(DestinationIndex, InputImageIndexType);
103115

116+
/** Set/Get the array describing which axes in the destination image to skip
117+
*
118+
* The axes with true values are set to 1, to fill the difference between the dimension of the input and source image.
119+
* The number of true values in DestinationSkipAxes plus the DestinationImageDimension must equal the
120+
* InputImageDimension.
121+
*
122+
* By default this array contains SourceImageDimension false values followed by true values for the remainder.
123+
*/
124+
itkSetMacro(DestinationSkipAxes, InputSkipAxesArrayType);
125+
itkGetConstMacro(DestinationSkipAxes, InputSkipAxesArrayType);
126+
127+
104128
/** Set/Get the source region (what part of the second input will be
105129
* pasted. */
106130
itkSetMacro(SourceRegion, SourceImageRegionType);
@@ -116,6 +140,14 @@ class ITK_TEMPLATE_EXPORT PasteImageFilter : public InPlaceImageFilter<TInputIma
116140
itkSetInputMacro(SourceImage, SourceImageType);
117141
itkGetInputMacro(SourceImage, SourceImageType);
118142

143+
/** Set/Get a constant value to fill the destination region.
144+
*
145+
* This input is an alternative input to the SourceImage.
146+
*/
147+
itkSetDecoratedInputMacro(Constant, SourceImagePixelType);
148+
itkGetDecoratedInputMacro(Constant, SourceImagePixelType);
149+
150+
119151
/** PasteImageFilter needs to set the input requested regions for its
120152
* inputs. The first input's requested region will be set to match
121153
* the output requested region. The second input's requested region
@@ -138,13 +170,23 @@ class ITK_TEMPLATE_EXPORT PasteImageFilter : public InPlaceImageFilter<TInputIma
138170
VerifyInputInformation() ITKv5_CONST override
139171
{}
140172

173+
void
174+
VerifyPreconditions() ITKv5_CONST override;
175+
176+
bool
177+
CanRunInPlace() const override;
178+
141179
protected:
142180
PasteImageFilter();
143181
~PasteImageFilter() override = default;
144182
void
145183
PrintSelf(std::ostream & os, Indent indent) const override;
146184

147-
/** PasteImageFilter can be implemented as a multithreaded filter.
185+
InputImageSizeType
186+
GetPresumedDestinationSize() const;
187+
188+
189+
/** NPasteImageFilter can be implemented as a multithreaded filter.
148190
* Therefore, this implementation provides a DynamicThreadedGenerateData()
149191
* routine which is called for each processing thread. The output
150192
* image data is allocated automatically by the superclass prior to
@@ -159,7 +201,11 @@ class ITK_TEMPLATE_EXPORT PasteImageFilter : public InPlaceImageFilter<TInputIma
159201

160202
SourceImageRegionType m_SourceRegion;
161203

162-
InputImageIndexType m_DestinationIndex;
204+
InputImageIndexType m_DestinationIndex;
205+
InputSkipAxesArrayType m_DestinationSkipAxes;
206+
207+
static_assert(InputImageDimension >= SourceImageDimension,
208+
"The source image dimension is greater than the input image.");
163209
};
164210
} // end namespace itk
165211

0 commit comments

Comments
 (0)