OTB  9.0.0
Orfeo Toolbox
otbScalarToRainbowRGBPixelFunctor.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 
22 #ifndef otbScalarToRainbowRGBPixelFunctor_h
23 #define otbScalarToRainbowRGBPixelFunctor_h
24 
25 #include "itkColormapFunction.h"
26 
27 namespace otb
28 {
29 
30 namespace Functor
31 {
32 
41 template <class TRGBPixel>
42 class ITK_EXPORT HSVToRGBFunctor
43 {
44 public:
45  HSVToRGBFunctor() = default;
46  ~HSVToRGBFunctor() = default;
48 
49  TRGBPixel operator()(double h, double s, double v) const
50  {
51  const double onethird = 1.0 / 3.0;
52  const double onesixth = 1.0 / 6.0;
53  const double twothird = 2.0 / 3.0;
54  const double fivesixth = 5.0 / 6.0;
55  double r, g, b;
56 
57  // compute RGB from HSV
58  if (h > onesixth && h <= onethird) // green/red
59  {
60  g = 1.0;
61  r = (onethird - h) / onesixth;
62  b = 0.0;
63  }
64  else if (h > onethird && h <= 0.5) // green/blue
65  {
66  g = 1.0;
67  b = (h - onethird) / onesixth;
68  r = 0.0;
69  }
70  else if (h > 0.5 && h <= twothird) // blue/green
71  {
72  b = 1.0;
73  g = (twothird - h) / onesixth;
74  r = 0.0;
75  }
76  else if (h > twothird && h <= fivesixth) // blue/red
77  {
78  b = 1.0;
79  r = (h - twothird) / onesixth;
80  g = 0.0;
81  }
82  else if (h > fivesixth && h <= 1.0) // red/blue
83  {
84  r = 1.0;
85  b = (1.0 - h) / onesixth;
86  g = 0.0;
87  }
88  else // red/green
89  {
90  r = 1.0;
91  g = h / onesixth;
92  b = 0.0;
93  }
94 
95  // add Saturation to the equation.
96  r = (s * r + (1.0 - s));
97  g = (s * g + (1.0 - s));
98  b = (s * b + (1.0 - s));
99 
100  r *= v;
101  g *= v;
102  b *= v;
103 
104  // std::cout << h << ", " << s << ", " << v << " -> " << r << ", " << g << ", " << b << std::endl;
105 
106  using RGBComponentType = typename TRGBPixel::ComponentType;
107  TRGBPixel ans;
108  ans[0] = static_cast<RGBComponentType>(r);
109  ans[1] = static_cast<RGBComponentType>(g);
110  ans[2] = static_cast<RGBComponentType>(b);
111  return ans;
112  }
113 };
114 }
115 
116 namespace Functor
117 {
118 
133 template <class TScalar, class TRGBPixel = itk::RGBPixel<unsigned char>>
134 class ITK_EXPORT ScalarToRainbowRGBPixelFunctor : public itk::Function::ColormapFunction<TScalar, TRGBPixel>
135 // public itk::Functor::ScalarToRGBPixelFunctor<TScalar>
136 {
137 public:
139  ~ScalarToRainbowRGBPixelFunctor() = default;
141 
143  using Superclass = itk::Function::ColormapFunction<TScalar, TRGBPixel>;
144  using Pointer = itk::SmartPointer<Self>;
145  using ConstPointer = itk::SmartPointer<const Self>;
146 
148  itkNewMacro(Self);
149 
150  using RGBPixelType = TRGBPixel;
151  using RGBComponentType = typename RGBPixelType::ComponentType;
152  using ScalarType = TScalar;
154 
155  RGBPixelType operator()(const TScalar&) const override;
156 
157 protected:
158  RGBPixelType HSVToRGB(double h, double s, double v) const;
159 
160 private:
161  ScalarToRainbowRGBPixelFunctor(const Self&) = delete;
162  void operator=(const Self&) = delete;
163 
165 };
166 
167 } // end namespace functor
168 
169 } // end namespace itk
170 
171 #ifndef OTB_MANUAL_INSTANTIATION
173 #endif
174 
175 #endif
otb::Functor::ScalarToRainbowRGBPixelFunctor::RGBPixelType
TRGBPixel RGBPixelType
Definition: otbScalarToRainbowRGBPixelFunctor.h:150
otb::Functor::ScalarToRainbowRGBPixelFunctor::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbScalarToRainbowRGBPixelFunctor.h:144
otb::Functor::ScalarToRainbowRGBPixelFunctor::RGBComponentType
typename RGBPixelType::ComponentType RGBComponentType
Definition: otbScalarToRainbowRGBPixelFunctor.h:151
otbScalarToRainbowRGBPixelFunctor.hxx
otb::Functor::ScalarToRainbowRGBPixelFunctor
Function object which maps a scalar value into a rainbow RGB pixel value.
Definition: otbScalarToRainbowRGBPixelFunctor.h:134
otb::Functor::ScalarToRainbowRGBPixelFunctor::Superclass
itk::Function::ColormapFunction< TScalar, TRGBPixel > Superclass
Definition: otbScalarToRainbowRGBPixelFunctor.h:143
otb::Functor::ScalarToRainbowRGBPixelFunctor::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbScalarToRainbowRGBPixelFunctor.h:145
otb::Functor::ScalarToRainbowRGBPixelFunctor::ScalarType
TScalar ScalarType
Definition: otbScalarToRainbowRGBPixelFunctor.h:152
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::HSVToRGBFunctor
Function object to convert HSV value to RGB.
Definition: otbScalarToRainbowRGBPixelFunctor.h:42
otb::Functor::ScalarToRainbowRGBPixelFunctor::m_HSVToRGBFunctor
HSVToRGBFunctorType m_HSVToRGBFunctor
Definition: otbScalarToRainbowRGBPixelFunctor.h:164
otb::Functor::HSVToRGBFunctor::operator()
TRGBPixel operator()(double h, double s, double v) const
Definition: otbScalarToRainbowRGBPixelFunctor.h:49