| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkInPlaceImageFilter.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:40 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
Portions of this code are covered under the VTK copyright. |
| 13 |
|
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details. |
| 14 |
|
|
| 15 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 16 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 17 |
IND |
*****PURPOSE. See the above copyright notices for more information. |
| 18 |
|
|
| 19 |
|
=========================================================================*/ |
| 20 |
|
#ifndef __itkInPlaceImageFilter_h |
| 21 |
|
#define __itkInPlaceImageFilter_h |
| 22 |
|
|
| 23 |
|
#include "itkImageToImageFilter.h" |
| 24 |
|
|
| 25 |
|
namespace itk |
| 26 |
|
{ |
| 27 |
|
|
| 28 |
|
/** \class InPlaceImageFilter |
| 29 |
LEN |
* \brief Base class for filters that take an image as input and overwrite that image as the output |
| 30 |
|
* |
| 31 |
|
* InPlaceImageFilter is the base class for all process objects whose |
| 32 |
|
* output image data is constructed by overwriting the input image |
| 33 |
|
* data. In other words, the output bulk data is the same block of |
| 34 |
|
* memory as the input bulk data. This filter provides the mechanisms |
| 35 |
|
* for in place image processing while maintaining general pipeline |
| 36 |
|
* mechanics. InPlaceImageFilters use less memory than standard |
| 37 |
|
* ImageToImageFilters because the input buffer is reused as the |
| 38 |
|
* output buffer. However, this benefit does not come without a cost. |
| 39 |
|
* Since the filter overwrites its input, the ownership of the bulk |
| 40 |
|
* data is transitioned from the input data object to the output data |
| 41 |
|
* object. When a data object has multiple consumers with one |
| 42 |
|
* of the consumers being an in place filter, the in place filter |
| 43 |
|
* effectively destroys the bulk data for the data object. Upstream |
| 44 |
|
* filters will then have to re-execute to regenerate the data object's |
| 45 |
|
* bulk data for the remaining consumers. |
| 46 |
|
* |
| 47 |
|
* Since an InPlaceImageFilter reuses the input bulk data memory for the |
| 48 |
|
* output bulk data memory, the input image type must match the output |
| 49 |
|
* image type. If the input and output image types are not identical, |
| 50 |
|
* the filter reverts to a traditional ImageToImageFilter behaviour |
| 51 |
|
* where an output image is allocated. In place operation can also be |
| 52 |
|
* controlled (when the input and output image type match) via the |
| 53 |
|
* methods InPlaceOn() and InPlaceOff(). |
| 54 |
|
* |
| 55 |
|
* Subclasses of InPlaceImageFilter must take extra care in how they |
| 56 |
|
* manage memory using (and perhaps overriding) the implementations of |
| 57 |
|
* ReleaseInputs() and AllocateOutputs() provided here. |
| 58 |
|
* |
| 59 |
|
* \ingroup ImageFilters |
| 60 |
|
*/ |
| 61 |
|
template <class TInputImage, class TOutputImage=TInputImage> |
| 62 |
LEN |
class ITK_EXPORT InPlaceImageFilter : public ImageToImageFilter<TInputImage, TOutputImage> |
| 63 |
|
{ |
| 64 |
|
public: |
| 65 |
|
/** Standard class typedefs. */ |
| 66 |
|
typedef InPlaceImageFilter Self; |
| 67 |
TDA |
typedef ImageToImageFilter<TInputImage, TOutputImage> Superclass; |
| 68 |
|
typedef SmartPointer<Self> Pointer; |
| 69 |
TDA |
typedef SmartPointer<const Self> ConstPointer; |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
/** Run-time type information (and related methods). */ |
| 73 |
|
itkTypeMacro(InPlaceImageFilter,ImageToImageFilter); |
| 74 |
|
|
| 75 |
|
/** Superclass typedefs. */ |
| 76 |
|
typedef typename Superclass::OutputImageType OutputImageType; |
| 77 |
TDA |
typedef typename Superclass::OutputImagePointer OutputImagePointer; |
| 78 |
TDA |
typedef typename Superclass::OutputImageRegionType OutputImageRegionType; |
| 79 |
TDA |
typedef typename Superclass::OutputImagePixelType OutputImagePixelType; |
| 80 |
|
|
| 81 |
|
/** Some convenient typedefs. */ |
| 82 |
|
typedef TInputImage InputImageType; |
| 83 |
TDA |
typedef typename InputImageType::Pointer InputImagePointer; |
| 84 |
TDA |
typedef typename InputImageType::ConstPointer InputImageConstPointer; |
| 85 |
TDA |
typedef typename InputImageType::RegionType InputImageRegionType; |
| 86 |
TDA |
typedef typename InputImageType::PixelType InputImagePixelType; |
| 87 |
|
|
| 88 |
|
/** ImageDimension constants */ |
| 89 |
|
itkStaticConstMacro(InputImageDimension, unsigned int, |
| 90 |
|
TInputImage::ImageDimension); |
| 91 |
|
itkStaticConstMacro(OutputImageDimension, unsigned int, |
| 92 |
|
TOutputImage::ImageDimension); |
| 93 |
|
|
| 94 |
|
/** In place operation can be turned on and off. This only has an |
| 95 |
|
* effect when the input and output image type match. */ |
| 96 |
|
itkSetMacro(InPlace, bool); |
| 97 |
|
itkGetMacro(InPlace, bool); |
| 98 |
|
itkBooleanMacro(InPlace); |
| 99 |
|
|
| 100 |
|
/** Can the filter run in place? To do so, the filter's first input |
| 101 |
|
* and output must have the same dimension and pixel type. This |
| 102 |
|
* method can be used in conjunction with the InPlace ivar to |
| 103 |
|
* determine whether a particular use of the filter is really |
| 104 |
|
* running in place. Some filters may be able to optimize their |
| 105 |
|
* operation if the InPlace is true and CanRunInPlace is true. */ |
| 106 |
IND |
***bool CanRunInPlace() const |
| 107 |
IND |
*****{ |
| 108 |
IND |
*******return (typeid(TInputImage) == typeid(TOutputImage)); |
| 109 |
IND |
*****}; |
| 110 |
|
|
| 111 |
IND |
*protected: |
| 112 |
|
InPlaceImageFilter(); |
| 113 |
|
~InPlaceImageFilter(); |
| 114 |
|
|
| 115 |
|
virtual void PrintSelf(std::ostream& os, Indent indent) const; |
| 116 |
|
|
| 117 |
|
/** The GenerateData method normally allocates the buffers for all |
| 118 |
|
* of the outputs of a filter. Since InPlaceImageFilter's can use an |
| 119 |
|
* overwritten version of the input for its output, the output |
| 120 |
|
* buffer should not be allocated. When possible, we graft the input |
| 121 |
|
* to the filter to the output. If an InPlaceFilter has multiple |
| 122 |
|
* outputs, then it would need to override this method to graft one |
| 123 |
|
* of its outputs and allocate the remaining. If a filter is |
| 124 |
|
* threaded (i.e. it provides an implementation of |
| 125 |
|
* ThreadedGenerateData()), this method is called automatically. If |
| 126 |
|
* an InPlaceFilter is not threaded (i.e. it provides an |
| 127 |
|
* implementation of GenerateData()), then this method (or |
| 128 |
|
* equivalent) must be called in GenerateData(). */ |
| 129 |
|
virtual void AllocateOutputs(); |
| 130 |
|
|
| 131 |
|
/** InPlaceImageFilter may transfer ownership of the input bulk data |
| 132 |
|
* to the output object. Once the output object owns the bulk data |
| 133 |
|
* (done in AllocateOutputs()), the input object must release its |
| 134 |
|
* hold on the bulk data. ProcessObject::ReleaseInputs() only |
| 135 |
|
* releases the input bulk data when the user has set the |
| 136 |
|
* ReleaseDataFlag. InPlaceImageFilter::ReleaseInputs() also |
| 137 |
|
* releases the input that it has overwritten. |
| 138 |
|
* |
| 139 |
|
* \sa ProcessObject::ReleaseInputs() */ |
| 140 |
|
virtual void ReleaseInputs(); |
| 141 |
|
|
| 142 |
|
private: |
| 143 |
|
InPlaceImageFilter(const Self&); //purposely not implemented |
| 144 |
|
void operator=(const Self&); //purposely not implemented |
| 145 |
|
|
| 146 |
|
bool m_InPlace; |
| 147 |
|
|
| 148 |
|
}; |
| 149 |
|
|
| 150 |
|
} // end namespace itk |
| 151 |
|
|
| 152 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 153 |
|
#include "itkInPlaceImageFilter.txx" |
| 154 |
|
#endif |
| 155 |
|
|
| 156 |
|
#endif |
| 157 |
|
|