/
itkImageSource.hxx
321 lines (281 loc) · 11.1 KB
/
itkImageSource.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* 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
*
* http://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.
*
*=========================================================================*/
/*=========================================================================
*
* Portions of this file are subject to the VTK Toolkit Version 3 copyright.
*
* Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
*
* For complete copyright, license and disclaimer of warranty information
* please refer to the NOTICE file at the top of the ITK source tree.
*
*=========================================================================*/
#ifndef itkImageSource_hxx
#define itkImageSource_hxx
#include "itkImageSource.h"
#include "itkOutputDataObjectIterator.h"
#include "itkImageRegionSplitterBase.h"
#include "itkMultiThreaderBase.h"
#include "itkMath.h"
namespace itk
{
template <typename TOutputImage>
ImageSource<TOutputImage>::ImageSource()
{
// Create the output. We use static_cast<> here because we know the default
// output must be of type TOutputImage
typename TOutputImage::Pointer output = static_cast<TOutputImage *>(this->MakeOutput(0).GetPointer());
this->ProcessObject::SetNumberOfRequiredOutputs(1);
this->ProcessObject::SetNthOutput(0, output.GetPointer());
#if defined(ITKV4_COMPATIBILITY)
m_DynamicMultiThreading = false;
#else
m_DynamicMultiThreading = true;
#endif
// Set the default behavior of an image source to NOT release its
// output bulk data prior to GenerateData() in case that bulk data
// can be reused (an thus avoid a costly deallocate/allocate cycle).
this->ReleaseDataBeforeUpdateFlagOff();
}
template <typename TOutputImage>
ProcessObject::DataObjectPointer ImageSource<TOutputImage>::MakeOutput(ProcessObject::DataObjectPointerArraySizeType)
{
return TOutputImage::New().GetPointer();
}
template <typename TOutputImage>
ProcessObject::DataObjectPointer
ImageSource<TOutputImage>::MakeOutput(const ProcessObject::DataObjectIdentifierType &)
{
return TOutputImage::New().GetPointer();
}
template <typename TOutputImage>
typename ImageSource<TOutputImage>::OutputImageType *
ImageSource<TOutputImage>::GetOutput()
{
// we assume that the first output is of the templated type
return itkDynamicCastInDebugMode<TOutputImage *>(this->GetPrimaryOutput());
}
template <typename TOutputImage>
const typename ImageSource<TOutputImage>::OutputImageType *
ImageSource<TOutputImage>::GetOutput() const
{
// we assume that the first output is of the templated type
return itkDynamicCastInDebugMode<const TOutputImage *>(this->GetPrimaryOutput());
}
template <typename TOutputImage>
typename ImageSource<TOutputImage>::OutputImageType *
ImageSource<TOutputImage>::GetOutput(unsigned int idx)
{
auto * out = dynamic_cast<TOutputImage *>(this->ProcessObject::GetOutput(idx));
if (out == nullptr && this->ProcessObject::GetOutput(idx) != nullptr)
{
itkWarningMacro(<< "Unable to convert output number " << idx << " to type " << typeid(OutputImageType).name());
}
return out;
}
template <typename TOutputImage>
void
ImageSource<TOutputImage>::GraftOutput(DataObject * graft)
{
this->GraftNthOutput(0, graft);
}
template <typename TOutputImage>
void
ImageSource<TOutputImage>::GraftOutput(const DataObjectIdentifierType & key, DataObject * graft)
{
if (!graft)
{
itkExceptionMacro(<< "Requested to graft output that is a nullptr pointer");
}
// we use the process object method since all out output may not be
// of the same type
DataObject * output = this->ProcessObject::GetOutput(key);
// Call GraftImage to copy meta-information, regions, and the pixel container
output->Graft(graft);
}
template <typename TOutputImage>
void
ImageSource<TOutputImage>::GraftNthOutput(unsigned int idx, DataObject * graft)
{
if (idx >= this->GetNumberOfIndexedOutputs())
{
itkExceptionMacro(<< "Requested to graft output " << idx << " but this filter only has "
<< this->GetNumberOfIndexedOutputs() << " indexed Outputs.");
}
this->GraftOutput(this->MakeNameFromOutputIndex(idx), graft);
}
//----------------------------------------------------------------------------
template <typename TOutputImage>
const ImageRegionSplitterBase *
ImageSource<TOutputImage>::GetImageRegionSplitter() const
{
return this->GetGlobalDefaultSplitter();
}
//----------------------------------------------------------------------------
template <typename TOutputImage>
unsigned int
ImageSource<TOutputImage>::SplitRequestedRegion(unsigned int i,
unsigned int pieces,
OutputImageRegionType & splitRegion)
{
const ImageRegionSplitterBase * splitter = this->GetImageRegionSplitter();
// Get the output pointer
OutputImageType * outputPtr = this->GetOutput();
splitRegion = outputPtr->GetRequestedRegion();
return splitter->GetSplit(i, pieces, splitRegion);
}
//----------------------------------------------------------------------------
template <typename TOutputImage>
void
ImageSource<TOutputImage>::AllocateOutputs()
{
using ImageBaseType = ImageBase<OutputImageDimension>;
typename ImageBaseType::Pointer outputPtr;
// Allocate the output memory
for (OutputDataObjectIterator it(this); !it.IsAtEnd(); ++it)
{
// Check whether the output is an image of the appropriate
// dimension (use ProcessObject's version of the GetInput()
// method since it returns the input as a pointer to a
// DataObject as opposed to the subclass version which
// static_casts the input to an TInputImage).
outputPtr = dynamic_cast<ImageBaseType *>(it.GetOutput());
if (outputPtr)
{
outputPtr->SetBufferedRegion(outputPtr->GetRequestedRegion());
outputPtr->Allocate();
}
}
}
//----------------------------------------------------------------------------
template <typename TOutputImage>
void
ImageSource<TOutputImage>::ClassicMultiThread(ThreadFunctionType callbackFunction)
{
ThreadStruct str;
str.Filter = this;
const OutputImageType * outputPtr = this->GetOutput();
const ImageRegionSplitterBase * splitter = this->GetImageRegionSplitter();
const unsigned int validThreads =
splitter->GetNumberOfSplits(outputPtr->GetRequestedRegion(), this->GetNumberOfWorkUnits());
this->GetMultiThreader()->SetNumberOfWorkUnits(validThreads);
this->GetMultiThreader()->SetSingleMethod(callbackFunction, &str);
this->GetMultiThreader()->SingleMethodExecute();
}
template <typename TOutputImage>
void
ImageSource<TOutputImage>::GenerateData()
{
// Call a method that can be overriden by a subclass to allocate
// memory for the filter's outputs
this->AllocateOutputs();
// Call a method that can be overridden by a subclass to perform
// some calculations prior to splitting the main computations into
// separate threads
this->BeforeThreadedGenerateData();
if (!m_DynamicMultiThreading)
{
this->ClassicMultiThread(this->ThreaderCallback);
}
else
{
this->GetMultiThreader()->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
this->GetMultiThreader()->template ParallelizeImageRegion<OutputImageDimension>(
this->GetOutput()->GetRequestedRegion(),
[this](const OutputImageRegionType & outputRegionForThread) {
this->DynamicThreadedGenerateData(outputRegionForThread);
},
this);
}
// Call a method that can be overridden by a subclass to perform
// some calculations after all the threads have completed
this->AfterThreadedGenerateData();
}
//----------------------------------------------------------------------------
// The execute method created by the subclass.
template <typename TOutputImage>
void
ImageSource<TOutputImage>::ThreadedGenerateData(const OutputImageRegionType &
#if !defined(ITK_LEGACY_REMOVE)
region
#endif
,
ThreadIdType)
{
#if !defined(ITK_LEGACY_REMOVE)
this->DynamicThreadedGenerateData(region);
#else
itkExceptionMacro("With DynamicMultiThreadingOff subclass should override this method. \
The signature of ThreadedGenerateData() has been changed in ITK v4 to use the new ThreadIdType.");
#endif
}
// The execute method created by the subclass.
template <typename TOutputImage>
void
ImageSource<TOutputImage>::DynamicThreadedGenerateData(const OutputImageRegionType &)
{
itkExceptionMacro("Subclass should override this method!!! \
If old behavior is desired invoke this->DynamicMultiThreadingOff(); \
before Update() is called. The best place is in class constructor.");
}
// Callback routine used by the classic threading library. This routine just calls
// the ThreadedGenerateData method after invoking (possibly) overloaded split region routine.
template <typename TOutputImage>
ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
ImageSource<TOutputImage>::ThreaderCallback(void * arg)
{
using ThreadInfo = MultiThreaderBase::WorkUnitInfo;
ThreadInfo * threadInfo = static_cast<ThreadInfo *>(arg);
ThreadIdType threadId = threadInfo->WorkUnitID;
ThreadIdType threadCount = threadInfo->NumberOfWorkUnits;
ThreadStruct * str = (ThreadStruct *)(threadInfo->UserData);
// execute the actual method with appropriate output region
// first find out how many pieces extent can be split into.
typename TOutputImage::RegionType splitRegion;
ThreadIdType total = str->Filter->SplitRequestedRegion(threadId, threadCount, splitRegion);
if (threadId < total)
{
str->Filter->ThreadedGenerateData(splitRegion, threadId);
#if defined(ITKV4_COMPATIBILITY)
if (str->Filter->GetAbortGenerateData())
{
std::string msg;
ProcessAborted e(__FILE__, __LINE__);
msg += "Object " + std::string(str->Filter->GetNameOfClass()) + ": AbortGenerateData was set!";
e.SetDescription(msg);
throw e;
}
else if (!str->Filter->GetDynamicMultiThreading() // progress reporting is not done in MultiThreaders
&& str->Filter->GetProgress() == 0.0f) // and progress was not set after at least the first chunk finished
{
str->Filter->UpdateProgress(float(threadId + 1) / total); // this will be the only progress update
}
#endif
}
// else don't use this thread. Threads were not split conveniently.
return ITK_THREAD_RETURN_DEFAULT_VALUE;
}
template <typename TOutputImage>
void
ImageSource<TOutputImage>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "DynamicMultiThreading: " << (m_DynamicMultiThreading ? "On" : "Off") << std::endl;
}
} // end namespace itk
#endif