OTB  9.0.0
Orfeo Toolbox
otbAlphaBlendingFunctor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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
otb::Functor::AlphaBlendingFunctor::InputPixel1Type
TInputPixel1 InputPixel1Type
Definition: otbAlphaBlendingFunctor.h:88
otb::Functor::AlphaBlendingFunctor< itk::RGBAPixel< TInputInternalPixel1 >, itk::RGBAPixel< TInputInternalPixel2 >, itk::RGBAPixel< TOutputInternalPixel > >::InputPixel1Type
itk::RGBAPixel< InternalInputPixel1Type > InputPixel1Type
Definition: otbAlphaBlendingFunctor.h:118
otb::Functor::AlphaBlendingFunctorBase::SetAlpha
virtual void SetAlpha(double a)
Definition: otbAlphaBlendingFunctor.h:60
otb::Functor::AlphaBlendingFunctor::AlphaBlendingFunctor
AlphaBlendingFunctor()
Definition: otbAlphaBlendingFunctor.h:81
otb::Functor::AlphaBlendingFunctor< itk::RGBAPixel< TInputInternalPixel1 >, itk::RGBAPixel< TInputInternalPixel2 >, itk::RGBAPixel< TOutputInternalPixel > >::AlphaBlendingFunctor
AlphaBlendingFunctor()
Definition: otbAlphaBlendingFunctor.h:110
otb::Functor::AlphaBlendingFunctor< itk::RGBAPixel< TInputInternalPixel1 >, itk::RGBAPixel< TInputInternalPixel2 >, itk::RGBAPixel< TOutputInternalPixel > >::InternalInputPixel1Type
TInputInternalPixel1 InternalInputPixel1Type
Definition: otbAlphaBlendingFunctor.h:117
otb::Functor::AlphaBlendingFunctor::OutputPixelType
TOutputPixel OutputPixelType
Definition: otbAlphaBlendingFunctor.h:90
otb::Functor::AlphaBlendingFunctor< itk::RGBAPixel< TInputInternalPixel1 >, itk::RGBAPixel< TInputInternalPixel2 >, itk::RGBAPixel< TOutputInternalPixel > >::InternalOutputPixelType
TOutputInternalPixel InternalOutputPixelType
Definition: otbAlphaBlendingFunctor.h:121
otb::Functor::AlphaBlendingFunctor< itk::RGBAPixel< TInputInternalPixel1 >, itk::RGBAPixel< TInputInternalPixel2 >, itk::RGBAPixel< TOutputInternalPixel > >::InternalInputPixel2Type
TInputInternalPixel2 InternalInputPixel2Type
Definition: otbAlphaBlendingFunctor.h:119
otb::Functor::AlphaBlendingFunctor
Implements simple blending.
Definition: otbAlphaBlendingFunctor.h:78
otb::Functor::AlphaBlendingFunctorBase::InputPixel2Type
TInputPixel2 InputPixel2Type
Definition: otbAlphaBlendingFunctor.h:57
otb::Functor::AlphaBlendingFunctor< itk::RGBAPixel< TInputInternalPixel1 >, itk::RGBAPixel< TInputInternalPixel2 >, itk::RGBAPixel< TOutputInternalPixel > >::OutputPixelType
itk::RGBAPixel< InternalOutputPixelType > OutputPixelType
Definition: otbAlphaBlendingFunctor.h:122
otb::Functor::AlphaBlendingFunctorBase::InputPixel1Type
TInputPixel1 InputPixel1Type
Definition: otbAlphaBlendingFunctor.h:56
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::AlphaBlendingFunctor< itk::RGBAPixel< TInputInternalPixel1 >, itk::RGBAPixel< TInputInternalPixel2 >, itk::RGBAPixel< TOutputInternalPixel > >::operator()
OutputPixelType operator()(InputPixel1Type input1, InputPixel2Type input2) const
Definition: otbAlphaBlendingFunctor.h:124
otb::Functor::AlphaBlendingFunctor::operator()
OutputPixelType operator()(InputPixel1Type input1, InputPixel2Type input2) const
Definition: otbAlphaBlendingFunctor.h:92
otb::Functor::AlphaBlendingFunctorBase::m_Alpha
double m_Alpha
Definition: otbAlphaBlendingFunctor.h:74
otb::Functor::AlphaBlendingFunctor::InputPixel2Type
TInputPixel2 InputPixel2Type
Definition: otbAlphaBlendingFunctor.h:89
otb::Functor::AlphaBlendingFunctorBase::OutputPixelType
TOutputPixel OutputPixelType
Definition: otbAlphaBlendingFunctor.h:58
otb::Functor::AlphaBlendingFunctor::~AlphaBlendingFunctor
~AlphaBlendingFunctor()
Definition: otbAlphaBlendingFunctor.h:84
otb::Functor::AlphaBlendingFunctor< itk::RGBAPixel< TInputInternalPixel1 >, itk::RGBAPixel< TInputInternalPixel2 >, itk::RGBAPixel< TOutputInternalPixel > >::m_Alpha
double m_Alpha
Definition: otbAlphaBlendingFunctor.h:140
otb::Functor::AlphaBlendingFunctor< itk::RGBAPixel< TInputInternalPixel1 >, itk::RGBAPixel< TInputInternalPixel2 >, itk::RGBAPixel< TOutputInternalPixel > >::~AlphaBlendingFunctor
~AlphaBlendingFunctor()
Definition: otbAlphaBlendingFunctor.h:113
itk
Definition: otbNoDataHelper.h:31
otb::Functor::AlphaBlendingFunctorBase::~AlphaBlendingFunctorBase
~AlphaBlendingFunctorBase()
Definition: otbAlphaBlendingFunctor.h:52
otb::Functor::AlphaBlendingFunctor::m_Alpha
double m_Alpha
Definition: otbAlphaBlendingFunctor.h:102
otb::Functor::AlphaBlendingFunctor< itk::RGBAPixel< TInputInternalPixel1 >, itk::RGBAPixel< TInputInternalPixel2 >, itk::RGBAPixel< TOutputInternalPixel > >::InputPixel2Type
itk::RGBAPixel< InternalInputPixel2Type > InputPixel2Type
Definition: otbAlphaBlendingFunctor.h:120
otb::Functor::AlphaBlendingFunctorBase::AlphaBlendingFunctorBase
AlphaBlendingFunctorBase()
Definition: otbAlphaBlendingFunctor.h:49
otb::Functor::AlphaBlendingFunctorBase
Definition: otbAlphaBlendingFunctor.h:46
otb::Functor::AlphaBlendingFunctorBase::GetAlpha
virtual double GetAlpha() const
Definition: otbAlphaBlendingFunctor.h:66