/
itkNullImageToImageFilterDriver.hxx
195 lines (165 loc) · 5.49 KB
/
itkNullImageToImageFilterDriver.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*=========================================================================
*
* 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.
*
*=========================================================================*/
/**
* This file contains classes that can be used to drive an image-to-image
* type filter in or out of an itk pipeline for testing purposes.
*/
#ifndef itkNullImageToImageFilterDriver_hxx
#define itkNullImageToImageFilterDriver_hxx
#include "itkPixelTraits.h"
#include "itkImage.h"
#include "itkImageToImageFilter.h"
#include "itkIndex.h"
#include <iostream>
#include <ctime>
namespace itk
{
/**
* \class NullImageToImageFilterDriver
* \brief Drives an image-to-image type itk process object with null inputs and
* null outputs.
*
* Provides a non-pipeline framework for testing image-to-image
* filters. Allocates an input image and sets up an output image, then calls
* Update on the filter. Times the execution of the filter.
*/
template <typename TInputImage, typename TOutputImage>
class NullImageToImageFilterDriver
{
public:
NullImageToImageFilterDriver()
: m_Filter(nullptr){};
using ImageSizeType = typename TInputImage::SizeType;
using InputPixelType = typename TInputImage::PixelType;
enum
{
InputPixelDimension = PixelTraits<InputPixelType>::Dimension
};
/**
* Set the image-to-image filter to drive.
*/
void
SetFilter(ImageToImageFilter<TInputImage, TOutputImage> * filter)
{
m_Filter = filter;
}
/**
* Set the size of the input and output image.
*/
void
SetImageSize(const ImageSizeType size)
{
m_ImageSize = size;
}
/**
* Drive the filter without using the itk pipeline.
*/
void
Execute();
protected:
struct DispatchBase
{};
template <unsigned int VDimension>
struct Dispatch : public DispatchBase
{};
void
InitializePixel(InputPixelType & pixel);
void
InitializePixel(const DispatchBase &, InputPixelType & pixel);
void
InitializePixel(const Dispatch<1> &, InputPixelType & pixel);
private:
ImageToImageFilter<TInputImage, TOutputImage> * m_Filter;
ImageSizeType m_ImageSize;
};
template <typename TInputImage, typename TOutputImage>
void
NullImageToImageFilterDriver<TInputImage, TOutputImage>::InitializePixel(InputPixelType & pixel)
{
this->InitializePixel(Dispatch<InputPixelDimension>(), pixel);
}
template <typename TInputImage, typename TOutputImage>
void
NullImageToImageFilterDriver<TInputImage, TOutputImage>::InitializePixel(const DispatchBase &, InputPixelType & pixel)
{
for (unsigned int i = 0; i < InputPixelDimension; ++i)
{
pixel[i] = NumericTraits<typename PixelTraits<InputPixelType>::ValueType>::ZeroValue();
}
}
template <typename TInputImage, typename TOutputImage>
void
NullImageToImageFilterDriver<TInputImage, TOutputImage>::InitializePixel(const Dispatch<1> &, InputPixelType & pixel)
{
pixel = NumericTraits<InputPixelType>::ZeroValue();
}
/**
* Drive the filter without using the itk pipeline
*/
template <typename TInputImage, typename TOutputImage>
void
NullImageToImageFilterDriver<TInputImage, TOutputImage>::Execute()
{
enum
{
ImageDimension = TInputImage::ImageDimension
};
// Set up input images
auto ip = TInputImage::New();
typename TOutputImage::IndexType index;
for (unsigned int i = 0; i < ImageDimension; ++i)
{
index[i] = 0;
}
const typename TOutputImage::RegionType region(index, m_ImageSize);
// Allocate the input
ip->SetRegions(region);
ip->Allocate();
// Construct a pixel to fill the image
InputPixelType pixel;
this->InitializePixel(pixel);
ip->FillBuffer(pixel);
// Setup the filter
m_Filter->SetInput(ip);
// print out the output object so we can see it modified times and regions
// std::cout << "Output object before filter execution" << std::endl
// << m_Filter->GetOutput() << std::endl;
using ImageFilterType = ImageToImageFilter<TInputImage, TOutputImage>;
typename ImageFilterType::Pointer sourceBefore =
dynamic_cast<ImageFilterType *>(m_Filter->GetOutput()->GetSource().GetPointer());
// Execute the filter
clock_t start = ::clock();
m_Filter->UpdateLargestPossibleRegion();
clock_t stop = ::clock();
// print out the output object so we can see it modified times and regions
std::cout << "Output object after filter execution" << std::endl << m_Filter->GetOutput() << std::endl;
typename ImageFilterType::Pointer sourceAfter =
dynamic_cast<ImageFilterType *>(m_Filter->GetOutput()->GetSource().GetPointer());
std::cout << sourceBefore.GetPointer() << ", " << sourceAfter.GetPointer() << std::endl;
if (sourceBefore != sourceAfter.GetPointer())
{
std::cout << std::endl << "Pipeline corrupt, filter output source different after execution." << std::endl;
}
else
{
std::cout << std::endl << "Pipeline intact" << std::endl;
}
std::cout << "Execution time was approximately " << (stop - start) << " clock cycles." << std::endl;
}
} // end namespace itk
#endif