ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkLabelOverlayFunctor.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 __itkLabelOverlayFunctor_h
00019 #define __itkLabelOverlayFunctor_h
00020 
00021 #include "itkLabelToRGBFunctor.h"
00022 
00023 namespace itk
00024 {
00025 namespace Functor
00026 {
00046 template< class TInputPixel, class TLabel, class TRGBPixel >
00047 class LabelOverlayFunctor
00048 {
00049 public:
00050   LabelOverlayFunctor()
00051   {
00052     // provide some default value for external use (outside
00053     // LabelOverlayFunctorImageFilter) Inside LabelOverlayFunctorImageFilter,
00054     // the values are always initialized
00055     m_BackgroundValue = NumericTraits< TLabel >::Zero;
00056   }
00057 
00058   inline TRGBPixel operator()(const TInputPixel & p1, const TLabel & p2) const
00059   {
00060     TRGBPixel rgbPixel;
00061     NumericTraits<TRGBPixel>::SetLength(rgbPixel, 3);
00062 
00063     if ( p2 == m_BackgroundValue )
00064       {
00065       // value is background
00066       // return a gray pixel with the same intensity than the input pixel
00067       typename TRGBPixel::ValueType p =
00068         static_cast< typename TRGBPixel::ValueType >( p1 );
00069       rgbPixel[0] = p;
00070       rgbPixel[1] = p;
00071       rgbPixel[2] = p;
00072       return rgbPixel;
00073       }
00074 
00075     // taint the input pixel with the colored one returned by
00076     // the color functor.
00077     TRGBPixel opaque = m_RGBFunctor(p2);
00078     for ( unsigned int i = 0; i < 3; i++ )
00079       {
00080       rgbPixel[i] = static_cast< typename TRGBPixel::ValueType >(
00081         opaque[i] * m_Opacity + p1 * ( 1.0 - m_Opacity ) );
00082       }
00083     return rgbPixel;
00084   }
00085 
00086   bool operator!=(const LabelOverlayFunctor & l) const
00087   {
00088     bool value = l.m_Opacity != m_Opacity
00089                  || m_BackgroundValue != l.m_BackgroundValue;
00090 
00091     return value;
00092   }
00093 
00094   ~LabelOverlayFunctor() {}
00095 
00096   void SetOpacity(double opacity)
00097   {
00098     m_Opacity = opacity;
00099   }
00100 
00101   void SetBackgroundValue(TLabel v)
00102   {
00103     m_BackgroundValue = v;
00104     m_RGBFunctor.SetBackgroundValue(v);
00105   }
00106 
00107   void ResetColors()
00108   {
00109     m_RGBFunctor.ResetColors();
00110   }
00111 
00112   unsigned int GetNumberOfColors() const
00113   {
00114     return m_RGBFunctor.GetNumberOfColors();
00115   }
00116 
00118   typedef typename TRGBPixel::ComponentType ComponentType;
00119 
00120   void AddColor(ComponentType r, ComponentType g, ComponentType b)
00121   {
00122     m_RGBFunctor.AddColor(r, g, b);
00123   }
00124 
00125 protected:
00126 private:
00127   double m_Opacity;
00128   TLabel m_BackgroundValue;
00129 
00130   typename Functor::LabelToRGBFunctor< TLabel, TRGBPixel > m_RGBFunctor;
00131 };
00132 }  // end namespace functor
00133 }  // end namespace itk
00134 
00135 #endif
00136