ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkRescaleIntensityImageFilter.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkRescaleIntensityImageFilter_h
00019 #define __itkRescaleIntensityImageFilter_h
00020 
00021 #include "itkUnaryFunctorImageFilter.h"
00022 
00023 namespace itk
00024 {
00025 // This functor class applies a linear transformation A.x + B
00026 // to input values.
00027 namespace Functor
00028 {
00029 template< typename TInput, typename  TOutput >
00030 class IntensityLinearTransform
00031 {
00032 public:
00033   typedef typename NumericTraits< TInput >::RealType RealType;
00034   IntensityLinearTransform()
00035   {
00036     m_Factor = 1.0;
00037     m_Offset = 0.0;
00038     m_Minimum = NumericTraits< TOutput >::NonpositiveMin();
00039     m_Maximum = NumericTraits< TOutput >::max();
00040   }
00041 
00042   ~IntensityLinearTransform() {}
00043   void SetFactor(RealType a) { m_Factor = a; }
00044   void SetOffset(RealType b) { m_Offset = b; }
00045   void SetMinimum(TOutput min) { m_Minimum = min; }
00046   void SetMaximum(TOutput max) { m_Maximum = max; }
00047   bool operator!=(const IntensityLinearTransform & other) const
00048   {
00049     if ( m_Factor != other.m_Factor
00050          || m_Offset != other.m_Offset
00051          || m_Maximum != other.m_Maximum
00052          || m_Minimum != other.m_Minimum )
00053       {
00054       return true;
00055       }
00056     return false;
00057   }
00058 
00059   bool operator==(const IntensityLinearTransform & other) const
00060   {
00061     return !( *this != other );
00062   }
00063 
00064   inline TOutput operator()(const TInput & x) const
00065   {
00066     RealType value  = static_cast< RealType >( x ) * m_Factor + m_Offset;
00067     TOutput  result = static_cast< TOutput >( value );
00068 
00069     result = ( result > m_Maximum ) ? m_Maximum : result;
00070     result = ( result < m_Minimum ) ? m_Minimum : result;
00071     return result;
00072   }
00073 
00074 private:
00075   RealType m_Factor;
00076   RealType m_Offset;
00077   TOutput  m_Maximum;
00078   TOutput  m_Minimum;
00079 };
00080 }  // end namespace functor
00081 
00120 template< typename  TInputImage, typename  TOutputImage = TInputImage >
00121 class ITK_EXPORT RescaleIntensityImageFilter:
00122   public
00123   UnaryFunctorImageFilter< TInputImage, TOutputImage,
00124                            Functor::IntensityLinearTransform<
00125                              typename TInputImage::PixelType,
00126                              typename TOutputImage::PixelType >   >
00127 {
00128 public:
00130   typedef RescaleIntensityImageFilter Self;
00131   typedef UnaryFunctorImageFilter<
00132     TInputImage, TOutputImage,
00133     Functor::IntensityLinearTransform<
00134       typename TInputImage::PixelType,
00135       typename TOutputImage::PixelType > >  Superclass;
00136 
00137   typedef SmartPointer< Self >       Pointer;
00138   typedef SmartPointer< const Self > ConstPointer;
00139 
00140   typedef typename TOutputImage::PixelType                   OutputPixelType;
00141   typedef typename TInputImage::PixelType                    InputPixelType;
00142   typedef typename NumericTraits< InputPixelType >::RealType RealType;
00143 
00145   itkNewMacro(Self);
00146 
00148   itkTypeMacro(RescaleIntensityImageFilter,
00149                UnaryFunctorImageFilter);
00150 
00151   itkSetMacro(OutputMinimum, OutputPixelType);
00152   itkSetMacro(OutputMaximum, OutputPixelType);
00153   itkGetConstReferenceMacro(OutputMinimum, OutputPixelType);
00154   itkGetConstReferenceMacro(OutputMaximum, OutputPixelType);
00155 
00159   itkGetConstReferenceMacro(Scale, RealType);
00160   itkGetConstReferenceMacro(Shift, RealType);
00162 
00165   itkGetConstReferenceMacro(InputMinimum, InputPixelType);
00166   itkGetConstReferenceMacro(InputMaximum, InputPixelType);
00168 
00170   void BeforeThreadedGenerateData(void);
00171 
00173   void PrintSelf(std::ostream & os, Indent indent) const;
00174 
00175 #ifdef ITK_USE_CONCEPT_CHECKING
00176 
00177   itkConceptMacro( InputHasNumericTraitsCheck,
00178                    ( Concept::HasNumericTraits< InputPixelType > ) );
00179   itkConceptMacro( OutputHasNumericTraitsCheck,
00180                    ( Concept::HasNumericTraits< OutputPixelType > ) );
00181   itkConceptMacro( RealTypeMultiplyOperatorCheck,
00182                    ( Concept::MultiplyOperator< RealType > ) );
00183   itkConceptMacro( RealTypeAdditiveOperatorsCheck,
00184                    ( Concept::AdditiveOperators< RealType > ) );
00185 
00187 #endif
00188 protected:
00189   RescaleIntensityImageFilter();
00190   virtual ~RescaleIntensityImageFilter() {}
00191 private:
00192   RescaleIntensityImageFilter(const Self &); //purposely not implemented
00193   void operator=(const Self &);              //purposely not implemented
00195 
00196   RealType m_Scale;
00197   RealType m_Shift;
00198 
00199   InputPixelType m_InputMinimum;
00200   InputPixelType m_InputMaximum;
00201 
00202   OutputPixelType m_OutputMinimum;
00203   OutputPixelType m_OutputMaximum;
00204 };
00205 } // end namespace itk
00206 
00207 #ifndef ITK_MANUAL_INSTANTIATION
00208 #include "itkRescaleIntensityImageFilter.hxx"
00209 #endif
00210 
00211 #endif
00212