ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkLabelOverlayFunctor.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkLabelOverlayFunctor_h
19 #define itkLabelOverlayFunctor_h
20 
21 #include "itkLabelToRGBFunctor.h"
22 #include "itkMath.h"
23 
24 namespace itk
25 {
26 namespace Functor
27 {
47 template< typename TInputPixel, typename TLabel, typename TRGBPixel >
49 {
50 public:
52  {
53  // provide some default value for external use (outside
54  // LabelOverlayFunctorImageFilter) Inside LabelOverlayFunctorImageFilter,
55  // the values are always initialized
56  m_Opacity = 1.0;
58  }
59 
60  inline TRGBPixel operator()(const TInputPixel & p1, const TLabel & p2) const
61  {
62  TRGBPixel rgbPixel;
64 
65  if ( p2 == m_BackgroundValue )
66  {
67  // value is background
68  // return a gray pixel with the same intensity than the input pixel
69  typename TRGBPixel::ValueType p =
70  static_cast< typename TRGBPixel::ValueType >( p1 );
71  rgbPixel[0] = p;
72  rgbPixel[1] = p;
73  rgbPixel[2] = p;
74  return rgbPixel;
75  }
76 
77  // taint the input pixel with the colored one returned by
78  // the color functor.
79  TRGBPixel opaque = m_RGBFunctor(p2);
80  // the following has been unrolled due to a bug with apple's
81  // llvm-gcc-4.2 build 5658
82  const double p1_blend= p1 * ( 1.0 - m_Opacity );
83  rgbPixel[0] = static_cast< typename TRGBPixel::ValueType >( opaque[0] * m_Opacity + p1_blend );
84  rgbPixel[1] = static_cast< typename TRGBPixel::ValueType >( opaque[1] * m_Opacity + p1_blend );
85  rgbPixel[2] = static_cast< typename TRGBPixel::ValueType >( opaque[2] * m_Opacity + p1_blend );
86 
87  return rgbPixel;
88  }
89 
90  bool operator!=(const LabelOverlayFunctor & l) const
91  {
92  bool areDifferent = Math::NotExactlyEquals(l.m_Opacity, m_Opacity)
94  || l.m_RGBFunctor != m_RGBFunctor;
95  return areDifferent;
96  }
97 
98  bool operator==(const LabelOverlayFunctor & l) const
99  {
100  return !(*this != l);
101  }
102 
104 
105  void SetOpacity(double opacity)
106  {
107  m_Opacity = opacity;
108  }
109 
110  void SetBackgroundValue(TLabel v)
111  {
112  m_BackgroundValue = v;
113  m_RGBFunctor.SetBackgroundValue(v);
114  }
115 
116  void ResetColors()
117  {
118  m_RGBFunctor.ResetColors();
119  }
120 
121  unsigned int GetNumberOfColors() const
122  {
123  return m_RGBFunctor.GetNumberOfColors();
124  }
125 
127  typedef typename TRGBPixel::ComponentType ComponentType;
128 
129  void AddColor(unsigned char r, unsigned char g, unsigned char b)
130  {
131  m_RGBFunctor.AddColor(r, g, b);
132  }
133 
134 protected:
135 
136 private:
137  double m_Opacity;
139 
141 };
142 } // end namespace functor
143 } // end namespace itk
144 
145 #endif
bool operator==(const LabelOverlayFunctor &l) const
Functor for applying a colormap to a label image and combine it with a grayscale image.
static void SetLength(T &m, const unsigned int s)
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:721
Functor for converting labels into RGB triplets.
bool operator!=(const LabelOverlayFunctor &l) const
void AddColor(unsigned char r, unsigned char g, unsigned char b)
TRGBPixel operator()(const TInputPixel &p1, const TLabel &p2) const
Functor::LabelToRGBFunctor< TLabel, TRGBPixel > m_RGBFunctor