Orfeo Toolbox  4.2
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 {
39 template<class TInputPixel1, class TInputPixel2, class TOutputPixel>
40 class ITK_EXPORT AlphaBlendingFunctorBase
41 {
42 public:
43  AlphaBlendingFunctorBase(): m_Alpha(1.0) {}
45 
46  typedef TInputPixel1 InputPixel1Type;
47  typedef TInputPixel2 InputPixel2Type;
48  typedef TOutputPixel OutputPixelType;
49 
50  virtual void SetAlpha(double a)
51  {
52  //Keep alpha between 0 and 1
53  m_Alpha = a<0.0?0.0:(a>1.0?1.0:a);
54  }
55 
56  virtual double GetAlpha() const
57  {
58  return m_Alpha;
59  }
60 
61  virtual inline OutputPixelType operator ()(InputPixel1Type input1, InputPixel2Type input2) const = 0;
62 
63 protected:
64  double m_Alpha;
65 };
66 
67 template<class TInputPixel1, class TInputPixel2, class TOutputPixel>
68 class ITK_EXPORT AlphaBlendingFunctor
69  :public AlphaBlendingFunctorBase< TInputPixel1, TInputPixel2, TOutputPixel >
70 {
71 public:
74 
75  typedef TInputPixel1 InputPixel1Type;
76  typedef TInputPixel2 InputPixel2Type;
77  typedef TOutputPixel OutputPixelType;
78 
79  inline OutputPixelType operator ()(InputPixel1Type input1, InputPixel2Type input2) const
80  {
81  OutputPixelType resp;
82  double alpha = this->GetAlpha();
83 
84  resp = static_cast<OutputPixelType>(vcl_floor(0.5 + (1.0 - alpha) * static_cast<double>(input1)
85  + alpha * static_cast<double>(input2)));
86  return resp;
87  }
88 
89 protected:
90  double m_Alpha;
91 };
92 
93 template<class TInputInternalPixel1, class TInputInternalPixel2, class TOutputInternalPixel>
94 class ITK_EXPORT AlphaBlendingFunctor< itk::RGBAPixel<TInputInternalPixel1>,
95  itk::RGBAPixel<TInputInternalPixel2>,
96  itk::RGBAPixel<TOutputInternalPixel> > :
97  public AlphaBlendingFunctorBase<itk::RGBAPixel<TInputInternalPixel1>,
98  itk::RGBAPixel<TInputInternalPixel2>,
99  itk::RGBAPixel<TOutputInternalPixel> >
100 {
101 public:
104 
105  typedef TInputInternalPixel1 InternalInputPixel1Type;
107  typedef TInputInternalPixel2 InternalInputPixel2Type;
109  typedef TOutputInternalPixel InternalOutputPixelType;
111 
112  inline OutputPixelType operator ()(InputPixel1Type input1, InputPixel2Type input2) const
113  {
114  OutputPixelType resp;
116  double alpha = static_cast<double>(input2.GetAlpha()) / 255.0 * this->GetAlpha();
117 
118  resp.SetRed(static_cast<InternalOutputPixelType>(vcl_floor(0.5 +
119  (1.0 - alpha) * static_cast<double>(input1.GetRed())
120  + alpha * static_cast<double>(input2.GetRed())
121  )));
122  resp.SetGreen(static_cast<InternalOutputPixelType>(vcl_floor(0.5 +
123  (1.0 - alpha) * static_cast<double>(input1.GetGreen())
124  + alpha * static_cast<double>(input2.GetGreen())
125  )));
126  resp.SetBlue(static_cast<InternalOutputPixelType>(vcl_floor(0.5 +
127  (1.0 - alpha) * static_cast<double>(input1.GetBlue())
128  + alpha * static_cast<double>(input2.GetBlue())
129  )));
130  return resp;
131  }
132 
133 protected:
134  double m_Alpha;
135 };
136 
137 
138 }
139 }
140 
141 #endif

Generated at Sat Aug 30 2014 15:55:10 for Orfeo Toolbox with doxygen 1.8.3.1