OTB  10.0.0
Orfeo Toolbox
otbAlphaBlendingFunctor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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)
50  {
51  }
53  {
54  }
55 
56  typedef TInputPixel1 InputPixel1Type;
57  typedef TInputPixel2 InputPixel2Type;
58  typedef TOutputPixel OutputPixelType;
59 
60  virtual void SetAlpha(double a)
61  {
62  // Keep alpha between 0 and 1
63  m_Alpha = a < 0.0 ? 0.0 : (a > 1.0 ? 1.0 : a);
64  }
65 
66  virtual double GetAlpha() const
67  {
68  return m_Alpha;
69  }
70 
71  virtual inline OutputPixelType operator()(InputPixel1Type input1, InputPixel2Type input2) const = 0;
72 
73 protected:
74  double m_Alpha;
75 };
76 
77 template <class TInputPixel1, class TInputPixel2, class TOutputPixel>
78 class ITK_EXPORT AlphaBlendingFunctor : public AlphaBlendingFunctorBase<TInputPixel1, TInputPixel2, TOutputPixel>
79 {
80 public:
82  {
83  }
85  {
86  }
87 
88  typedef TInputPixel1 InputPixel1Type;
89  typedef TInputPixel2 InputPixel2Type;
90  typedef TOutputPixel OutputPixelType;
91 
93  {
94  OutputPixelType resp;
95  double alpha = this->GetAlpha();
96 
97  resp = static_cast<OutputPixelType>(std::floor(0.5 + (1.0 - alpha) * static_cast<double>(input1) + alpha * static_cast<double>(input2)));
98  return resp;
99  }
100 
101 protected:
102  double m_Alpha;
103 };
104 
105 template <class TInputInternalPixel1, class TInputInternalPixel2, class TOutputInternalPixel>
106 class ITK_EXPORT AlphaBlendingFunctor<itk::RGBAPixel<TInputInternalPixel1>, itk::RGBAPixel<TInputInternalPixel2>, itk::RGBAPixel<TOutputInternalPixel>>
107  : public AlphaBlendingFunctorBase<itk::RGBAPixel<TInputInternalPixel1>, itk::RGBAPixel<TInputInternalPixel2>, itk::RGBAPixel<TOutputInternalPixel>>
108 {
109 public:
111  {
112  }
114  {
115  }
116 
117  typedef TInputInternalPixel1 InternalInputPixel1Type;
118  typedef itk::RGBAPixel<InternalInputPixel1Type> InputPixel1Type;
119  typedef TInputInternalPixel2 InternalInputPixel2Type;
120  typedef itk::RGBAPixel<InternalInputPixel2Type> InputPixel2Type;
121  typedef TOutputInternalPixel InternalOutputPixelType;
122  typedef itk::RGBAPixel<InternalOutputPixelType> OutputPixelType;
123 
125  {
126  OutputPixelType resp;
127  resp.Fill(itk::NumericTraits<InternalOutputPixelType>::max());
128  double alpha = static_cast<double>(input2.GetAlpha()) / 255.0 * this->GetAlpha();
129 
130  resp.SetRed(static_cast<InternalOutputPixelType>(
131  std::floor(0.5 + (1.0 - alpha) * static_cast<double>(input1.GetRed()) + alpha * static_cast<double>(input2.GetRed()))));
132  resp.SetGreen(static_cast<InternalOutputPixelType>(
133  std::floor(0.5 + (1.0 - alpha) * static_cast<double>(input1.GetGreen()) + alpha * static_cast<double>(input2.GetGreen()))));
134  resp.SetBlue(static_cast<InternalOutputPixelType>(
135  std::floor(0.5 + (1.0 - alpha) * static_cast<double>(input1.GetBlue()) + alpha * static_cast<double>(input2.GetBlue()))));
136  return resp;
137  }
138 
139 protected:
140  double m_Alpha;
141 };
142 }
143 }
144 
145 #endif
virtual OutputPixelType operator()(InputPixel1Type input1, InputPixel2Type input2) const =0
OutputPixelType operator()(InputPixel1Type input1, InputPixel2Type input2) const
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.