OTB  6.1.0
Orfeo Toolbox
otbAlphaBlendingFunctor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbAlphaBlendingFunctor_h
22 #define otbAlphaBlendingFunctor_h
23 
24 #include "itkRGBAPixel.h"
25 namespace otb
26 {
27 namespace Functor
28 {
44 
45 template<class TInputPixel1, class TInputPixel2, class TOutputPixel>
46 class ITK_EXPORT AlphaBlendingFunctorBase
47 {
48 public:
49  AlphaBlendingFunctorBase(): m_Alpha(1.0) {}
51 
52  typedef TInputPixel1 InputPixel1Type;
53  typedef TInputPixel2 InputPixel2Type;
54  typedef TOutputPixel OutputPixelType;
55 
56  virtual void SetAlpha(double a)
57  {
58  //Keep alpha between 0 and 1
59  m_Alpha = a<0.0?0.0:(a>1.0?1.0:a);
60  }
61 
62  virtual double GetAlpha() const
63  {
64  return m_Alpha;
65  }
66 
67  virtual inline OutputPixelType operator ()(InputPixel1Type input1, InputPixel2Type input2) const = 0;
68 
69 protected:
70  double m_Alpha;
71 };
72 
73 template<class TInputPixel1, class TInputPixel2, class TOutputPixel>
74 class ITK_EXPORT AlphaBlendingFunctor
75  :public AlphaBlendingFunctorBase< TInputPixel1, TInputPixel2, TOutputPixel >
76 {
77 public:
80 
81  typedef TInputPixel1 InputPixel1Type;
82  typedef TInputPixel2 InputPixel2Type;
83  typedef TOutputPixel OutputPixelType;
84 
85  inline OutputPixelType operator ()(InputPixel1Type input1, InputPixel2Type input2) const
86  {
87  OutputPixelType resp;
88  double alpha = this->GetAlpha();
89 
90  resp = static_cast<OutputPixelType>(vcl_floor(0.5 + (1.0 - alpha) * static_cast<double>(input1)
91  + alpha * static_cast<double>(input2)));
92  return resp;
93  }
94 
95 protected:
96  double m_Alpha;
97 };
98 
99 template<class TInputInternalPixel1, class TInputInternalPixel2, class TOutputInternalPixel>
100 class ITK_EXPORT AlphaBlendingFunctor< itk::RGBAPixel<TInputInternalPixel1>,
101  itk::RGBAPixel<TInputInternalPixel2>,
102  itk::RGBAPixel<TOutputInternalPixel> > :
103  public AlphaBlendingFunctorBase<itk::RGBAPixel<TInputInternalPixel1>,
104  itk::RGBAPixel<TInputInternalPixel2>,
105  itk::RGBAPixel<TOutputInternalPixel> >
106 {
107 public:
110 
111  typedef TInputInternalPixel1 InternalInputPixel1Type;
113  typedef TInputInternalPixel2 InternalInputPixel2Type;
115  typedef TOutputInternalPixel InternalOutputPixelType;
117 
118  inline OutputPixelType operator ()(InputPixel1Type input1, InputPixel2Type input2) const
119  {
120  OutputPixelType resp;
122  double alpha = static_cast<double>(input2.GetAlpha()) / 255.0 * this->GetAlpha();
123 
124  resp.SetRed(static_cast<InternalOutputPixelType>(vcl_floor(0.5 +
125  (1.0 - alpha) * static_cast<double>(input1.GetRed())
126  + alpha * static_cast<double>(input2.GetRed())
127  )));
128  resp.SetGreen(static_cast<InternalOutputPixelType>(vcl_floor(0.5 +
129  (1.0 - alpha) * static_cast<double>(input1.GetGreen())
130  + alpha * static_cast<double>(input2.GetGreen())
131  )));
132  resp.SetBlue(static_cast<InternalOutputPixelType>(vcl_floor(0.5 +
133  (1.0 - alpha) * static_cast<double>(input1.GetBlue())
134  + alpha * static_cast<double>(input2.GetBlue())
135  )));
136  return resp;
137  }
138 
139 protected:
140  double m_Alpha;
141 };
142 
143 
144 }
145 }
146 
147 #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