/usr/include/InsightToolkit/Common/itkImageSource.txx is in libinsighttoolkit3-dev 3.20.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkImageSource.txx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
Portions of this code are covered under the VTK copyright.
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkImageSource_txx
#define __itkImageSource_txx
#include "itkImageSource.h"
#include "vnl/vnl_math.h"
namespace itk
{
/**
*
*/
template<class 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());
// 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<class TOutputImage>
typename ImageSource<TOutputImage>::DataObjectPointer
ImageSource<TOutputImage>
::MakeOutput(unsigned int)
{
return static_cast<DataObject*>(TOutputImage::New().GetPointer());
}
/**
*
*/
template<class TOutputImage>
typename ImageSource<TOutputImage>::OutputImageType *
ImageSource<TOutputImage>
::GetOutput()
{
if (this->GetNumberOfOutputs() < 1)
{
return 0;
}
// we assume that the first output is of the templated type
return static_cast<TOutputImage*>
(this->ProcessObject::GetOutput(0));
}
/**
*
*/
template<class TOutputImage>
typename ImageSource<TOutputImage>::OutputImageType *
ImageSource<TOutputImage>
::GetOutput(unsigned int idx)
{
TOutputImage* out = dynamic_cast<TOutputImage*>
(this->ProcessObject::GetOutput(idx));
if ( out == NULL ) {
itkWarningMacro ( << "dynamic_cast to output type failed" );
}
return out;
}
/**
*
*/
template<class TOutputImage>
void
ImageSource<TOutputImage>
::GraftOutput(DataObject *graft)
{
this->GraftNthOutput(0, graft);
}
/**
*
*/
template<class TOutputImage>
void
ImageSource<TOutputImage>
::GraftNthOutput(unsigned int idx, DataObject *graft)
{
if ( idx >= this->GetNumberOfOutputs() )
{
itkExceptionMacro(<<"Requested to graft output " << idx <<
" but this filter only has " << this->GetNumberOfOutputs() << " Outputs.");
}
if ( !graft )
{
itkExceptionMacro(<<"Requested to graft output that is a NULL pointer" );
}
// we use the process object method since all out output may not be
// of the same type
DataObject * output = this->ProcessObject::GetOutput(idx);
// Call GraftImage to copy meta-information, regions, and the pixel container
output->Graft( graft );
}
//----------------------------------------------------------------------------
template <class TOutputImage>
int
ImageSource<TOutputImage>
::SplitRequestedRegion(int i, int num, OutputImageRegionType& splitRegion)
{
// Get the output pointer
OutputImageType * outputPtr = this->GetOutput();
const typename TOutputImage::SizeType& requestedRegionSize
= outputPtr->GetRequestedRegion().GetSize();
int splitAxis;
typename TOutputImage::IndexType splitIndex;
typename TOutputImage::SizeType splitSize;
// Initialize the splitRegion to the output requested region
splitRegion = outputPtr->GetRequestedRegion();
splitIndex = splitRegion.GetIndex();
splitSize = splitRegion.GetSize();
// split on the outermost dimension available
splitAxis = outputPtr->GetImageDimension() - 1;
while (requestedRegionSize[splitAxis] == 1)
{
--splitAxis;
if (splitAxis < 0)
{ // cannot split
itkDebugMacro(" Cannot Split");
return 1;
}
}
// determine the actual number of pieces that will be generated
typename TOutputImage::SizeType::SizeValueType range = requestedRegionSize[splitAxis];
int valuesPerThread = Math::Ceil<int>(range/(double)num);
int maxThreadIdUsed = Math::Ceil<int>(range/(double)valuesPerThread) - 1;
// Split the region
if (i < maxThreadIdUsed)
{
splitIndex[splitAxis] += i*valuesPerThread;
splitSize[splitAxis] = valuesPerThread;
}
if (i == maxThreadIdUsed)
{
splitIndex[splitAxis] += i*valuesPerThread;
// last thread needs to process the "rest" dimension being split
splitSize[splitAxis] = splitSize[splitAxis] - i*valuesPerThread;
}
// set the split region ivars
splitRegion.SetIndex( splitIndex );
splitRegion.SetSize( splitSize );
itkDebugMacro(" Split Piece: " << splitRegion );
return maxThreadIdUsed + 1;
}
//----------------------------------------------------------------------------
template <class TOutputImage>
void
ImageSource<TOutputImage>
::AllocateOutputs()
{
typedef ImageBase<OutputImageDimension> ImageBaseType;
typename ImageBaseType::Pointer outputPtr;
// Allocate the output memory
for (unsigned int i=0; i < this->GetNumberOfOutputs(); i++)
{
// 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 *>( this->ProcessObject::GetOutput(i) );
if ( outputPtr )
{
outputPtr->SetBufferedRegion( outputPtr->GetRequestedRegion() );
outputPtr->Allocate();
}
}
}
//----------------------------------------------------------------------------
template <class 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();
// Set up the multithreaded processing
ThreadStruct str;
str.Filter = this;
this->GetMultiThreader()->SetNumberOfThreads(this->GetNumberOfThreads());
this->GetMultiThreader()->SetSingleMethod(this->ThreaderCallback, &str);
// multithread the execution
this->GetMultiThreader()->SingleMethodExecute();
// 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 <class TOutputImage>
void
ImageSource<TOutputImage>
::ThreadedGenerateData(const OutputImageRegionType&,
int)
{
// The following code is equivalent to:
// itkExceptionMacro("subclass should override this method!!!");
// The ExceptionMacro is not used because gcc warns that a
// 'noreturn' function does return
OStringStream message;
message << "itk::ERROR: " << this->GetNameOfClass()
<< "(" << this << "): " << "Subclass should override this method!!!";
ExceptionObject e_(__FILE__, __LINE__, message.str().c_str(),ITK_LOCATION);
throw e_;
}
// Callback routine used by the threading library. This routine just calls
// the ThreadedGenerateData method after setting the correct region for this
// thread.
template <class TOutputImage>
ITK_THREAD_RETURN_TYPE
ImageSource<TOutputImage>
::ThreaderCallback( void *arg )
{
ThreadStruct *str;
int total, threadId, threadCount;
threadId = ((MultiThreader::ThreadInfoStruct *)(arg))->ThreadID;
threadCount = ((MultiThreader::ThreadInfoStruct *)(arg))->NumberOfThreads;
str = (ThreadStruct *)(((MultiThreader::ThreadInfoStruct *)(arg))->UserData);
// execute the actual method with appropriate output region
// first find out how many pieces extent can be split into.
typename TOutputImage::RegionType splitRegion;
total = str->Filter->SplitRequestedRegion(threadId, threadCount,
splitRegion);
if (threadId < total)
{
str->Filter->ThreadedGenerateData(splitRegion, threadId);
}
// else
// {
// otherwise don't use this thread. Sometimes the threads dont
// break up very well and it is just as efficient to leave a
// few threads idle.
// }
return ITK_THREAD_RETURN_VALUE;
}
} // end namespace itk
#endif
|