OTB  5.0.0
Orfeo Toolbox
otbAlphaBlendingFunctor.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbAlphaBlendingFunctor_h
19 #define __otbAlphaBlendingFunctor_h
20 
21 #include "itkRGBAPixel.h"
22 namespace otb
23 {
24 namespace Functor
25 {
41 
42 template<class TInputPixel1, class TInputPixel2, class TOutputPixel>
43 class ITK_EXPORT AlphaBlendingFunctorBase
44 {
45 public:
46  AlphaBlendingFunctorBase(): m_Alpha(1.0) {}
48 
49  typedef TInputPixel1 InputPixel1Type;
50  typedef TInputPixel2 InputPixel2Type;
51  typedef TOutputPixel OutputPixelType;
52 
53  virtual void SetAlpha(double a)
54  {
55  //Keep alpha between 0 and 1
56  m_Alpha = a<0.0?0.0:(a>1.0?1.0:a);
57  }
58 
59  virtual double GetAlpha() const
60  {
61  return m_Alpha;
62  }
63 
64  virtual inline OutputPixelType operator ()(InputPixel1Type input1, InputPixel2Type input2) const = 0;
65 
66 protected:
67  double m_Alpha;
68 };
69 
70 template<class TInputPixel1, class TInputPixel2, class TOutputPixel>
71 class ITK_EXPORT AlphaBlendingFunctor
72  :public AlphaBlendingFunctorBase< TInputPixel1, TInputPixel2, TOutputPixel >
73 {
74 public:
77 
78  typedef TInputPixel1 InputPixel1Type;
79  typedef TInputPixel2 InputPixel2Type;
80  typedef TOutputPixel OutputPixelType;
81 
82  inline OutputPixelType operator ()(InputPixel1Type input1, InputPixel2Type input2) const
83  {
84  OutputPixelType resp;
85  double alpha = this->GetAlpha();
86 
87  resp = static_cast<OutputPixelType>(vcl_floor(0.5 + (1.0 - alpha) * static_cast<double>(input1)
88  + alpha * static_cast<double>(input2)));
89  return resp;
90  }
91 
92 protected:
93  double m_Alpha;
94 };
95 
96 template<class TInputInternalPixel1, class TInputInternalPixel2, class TOutputInternalPixel>
97 class ITK_EXPORT AlphaBlendingFunctor< itk::RGBAPixel<TInputInternalPixel1>,
98  itk::RGBAPixel<TInputInternalPixel2>,
99  itk::RGBAPixel<TOutputInternalPixel> > :
100  public AlphaBlendingFunctorBase<itk::RGBAPixel<TInputInternalPixel1>,
101  itk::RGBAPixel<TInputInternalPixel2>,
102  itk::RGBAPixel<TOutputInternalPixel> >
103 {
104 public:
107 
108  typedef TInputInternalPixel1 InternalInputPixel1Type;
110  typedef TInputInternalPixel2 InternalInputPixel2Type;
112  typedef TOutputInternalPixel InternalOutputPixelType;
114 
115  inline OutputPixelType operator ()(InputPixel1Type input1, InputPixel2Type input2) const
116  {
117  OutputPixelType resp;
119  double alpha = static_cast<double>(input2.GetAlpha()) / 255.0 * this->GetAlpha();
120 
121  resp.SetRed(static_cast<InternalOutputPixelType>(vcl_floor(0.5 +
122  (1.0 - alpha) * static_cast<double>(input1.GetRed())
123  + alpha * static_cast<double>(input2.GetRed())
124  )));
125  resp.SetGreen(static_cast<InternalOutputPixelType>(vcl_floor(0.5 +
126  (1.0 - alpha) * static_cast<double>(input1.GetGreen())
127  + alpha * static_cast<double>(input2.GetGreen())
128  )));
129  resp.SetBlue(static_cast<InternalOutputPixelType>(vcl_floor(0.5 +
130  (1.0 - alpha) * static_cast<double>(input1.GetBlue())
131  + alpha * static_cast<double>(input2.GetBlue())
132  )));
133  return resp;
134  }
135 
136 protected:
137  double m_Alpha;
138 };
139 
140 
141 }
142 }
143 
144 #endif
const ComponentType & GetBlue(void) const
void SetRed(ComponentType red)
void Fill(const ValueType &)
const ComponentType & GetRed(void) const
void SetGreen(ComponentType green)
void SetBlue(ComponentType blue)
const ComponentType & GetAlpha(void) const
const ComponentType & GetGreen(void) const