OTB  9.0.0
Orfeo Toolbox
otbConvertTypeFunctor.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 otbConvertTypeFunctor_h
22 #define otbConvertTypeFunctor_h
23 
24 
26 #include "otbAlgoClamp.h"
27 #include "otbNumericTraits.h"
28 #include "itkNumericTraits.h"
29 #include <boost/type_traits/is_complex.hpp>
30 #include <limits>
31 #include <type_traits>
32 
33 namespace otb
34 {
35 
36 namespace Functor
37 {
38 
39 template <class TInputPixelType, class TOutputPixelType>
41 {
42 public:
43  using InputPixelType = TInputPixelType;
44  using OutputPixelType = TOutputPixelType;
46 
47  using InputInternalPixelType = typename itk::NumericTraits<InputPixelType>::ValueType;
48  using OutputInternalPixelType = typename itk::NumericTraits<OutputPixelType>::ValueType;
49 
50  using InputPixelValueType = typename itk::NumericTraits<InputInternalPixelType>::ValueType;
51  using OutputPixelValueType = typename itk::NumericTraits<OutputInternalPixelType>::ValueType;
52  using ThresholdPixelValueType = std::common_type_t<InputPixelValueType, OutputPixelValueType>;
53 
54  static constexpr bool m_cInPix = boost::is_complex<InputPixelType>::value;
55  static constexpr bool m_cOutPix = boost::is_complex<OutputPixelType>::value;
56  static constexpr bool m_cInInternalPix = boost::is_complex<InputInternalPixelType>::value;
57  static constexpr bool m_cOutInternalPix = boost::is_complex<OutputInternalPixelType>::value;
58 
59  ConvertTypeFunctor() = default;
60  ~ConvertTypeFunctor() = default;
61 
62  // template < class InternalPixelType >
63  void SetInputComponents(unsigned int sizeIn)
64  {
65  m_CompIn = sizeIn;
66  if (m_cInPix)
67  {
68  // needed as ITK thinks that one complex component is actually
69  // two components...
70  m_CompIn /= 2;
71  }
72 
74  m_Scal = 2 * m_CompIn;
75  else
76  m_Scal = m_CompIn;
77 
78  OutputPixelType out;
79  unsigned int size = itk::NumericTraits<OutputPixelType>::GetLength(out);
80  if (size == 0) // That means it is a variable size container
81  {
83  m_CompOut = (m_Scal + 1) / 2;
84  else
85  m_CompOut = m_Scal;
86  }
87  // It is a fixed size container, m_CompOut should be equal to its size
88  else if (m_cOutPix) // one complex is one component
89  m_CompOut = 1;
90  else // fized size container or scalar
91  m_CompOut = size;
92  }
93 
94  unsigned int GetOutputSize() const noexcept
95  {
96  return m_CompOut;
97  }
98 
99  void SetThresholds(OutputPixelValueType const& lowest, OutputPixelValueType const& highest) noexcept
100  {
101  assert( lowest <= highest );
102  m_LowestB = lowest;
103  m_HighestB = highest;
104  m_Zero = clamp<ThresholdPixelValueType>(ThresholdPixelValueType{}, lowest, highest);
105  }
106 
107  void operator()(OutputPixelType & out, InputPixelType const& in) const
108  {
109  // PERF: Because VLV pixel may be used, this operator is written
110  // with an output parameter instead of a returned value.
111 
112  auto r_out = PixelRange(out);
113  auto r_in = PixelRange(in);
114 
115  auto first = r_in.begin(); // Source iterators
116  auto const end = r_in.end();
117  auto dfirst = r_out.begin(); // D)estination iterators
118  auto const dend = r_out.end();
119 
120  static_assert(std::is_same<OutputPixelValueType, std::decay_t<decltype(*dfirst)>>::value, "They should match");
121  // PERF: Locally cache the member variable as the compiler cannot
122  auto const lo = m_LowestB;
123  auto const hi = m_HighestB;
124  // As std:transform stop condition is only on `end`, and not on both
125  // `end` and `dend`, we loop manually
126  for (; first!=end && dfirst!=dend ; ++first, ++dfirst)
127  {
128  // The conversion to OutputPixelValueType needs to be done after
129  // clamping!
130  // Indeed, let's suppose InputType=short, OutputType=unsigned char
131  // clamp<OutputType>(300) would yield 300-256=44 instead of 255.
132  // Hence the use of ThresholdPixelValueType which is the common
133  // type between Input and Output PixelValueTypes.
134  *dfirst = static_cast<OutputPixelValueType>(otb::clamp<ThresholdPixelValueType>(*first, lo, hi));
135  }
136  // complete with extra 0 (case where we have less input bands, and we store into complex)
137 #if 0
138  // It seems to be slightly slower with assertions on
139  std::fill(dfirst, dend, m_Zero);
140 #else
141  for (; dfirst != dend ; ++dfirst)
142  {
143  *dfirst = m_Zero;
144  }
145 #endif
146  }
147 
148 private:
149 
150  ConvertTypeFunctor(const Self&) = delete;
151  void operator=(const Self&) = delete;
152 
153  ThresholdPixelValueType m_LowestB = common_lowest<InputPixelValueType, OutputPixelValueType>();
154  ThresholdPixelValueType m_HighestB = common_highest<InputPixelValueType, OutputPixelValueType>();
155  OutputPixelValueType m_Zero {}; // initialized to zero!
156 
157  unsigned int m_CompIn, m_CompOut, m_Scal;
158 };
159 
160 } // end namespace Functor
161 
162 } // end namespace otb
163 
164 #endif
otb::Functor::ConvertTypeFunctor::GetOutputSize
unsigned int GetOutputSize() const noexcept
Definition: otbConvertTypeFunctor.h:94
otb::Functor::ConvertTypeFunctor::m_HighestB
ThresholdPixelValueType m_HighestB
Definition: otbConvertTypeFunctor.h:154
otb::Functor::ConvertTypeFunctor::m_cInInternalPix
static constexpr bool m_cInInternalPix
Definition: otbConvertTypeFunctor.h:56
otb::Functor::ConvertTypeFunctor::ConvertTypeFunctor
ConvertTypeFunctor()=default
otb::Functor::ConvertTypeFunctor< typename TInputImage::PixelType, typename TInputImage ::PixelType >::OutputInternalPixelType
typename itk::NumericTraits< OutputPixelType >::ValueType OutputInternalPixelType
Definition: otbConvertTypeFunctor.h:48
otb::Functor::ConvertTypeFunctor::m_cOutPix
static constexpr bool m_cOutPix
Definition: otbConvertTypeFunctor.h:55
otb::PixelRange
auto PixelRange(TPixel &&pixel)
Definition: otbPixelComponentIterator.h:715
otbPixelComponentIterator.h
otb::Functor::ConvertTypeFunctor::m_CompOut
unsigned int m_CompOut
Definition: otbConvertTypeFunctor.h:157
otb::Functor::ConvertTypeFunctor::m_Scal
unsigned int m_Scal
Definition: otbConvertTypeFunctor.h:157
otb::Functor::ConvertTypeFunctor::SetThresholds
void SetThresholds(OutputPixelValueType const &lowest, OutputPixelValueType const &highest) noexcept
Definition: otbConvertTypeFunctor.h:99
otb::Functor::ConvertTypeFunctor::m_CompIn
unsigned int m_CompIn
Definition: otbConvertTypeFunctor.h:157
otb::Functor::ConvertTypeFunctor< typename TInputImage::PixelType, typename TInputImage ::PixelType >::InputPixelType
typename TInputImage::PixelType InputPixelType
Definition: otbConvertTypeFunctor.h:43
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::ConvertTypeFunctor::m_cInPix
static constexpr bool m_cInPix
Definition: otbConvertTypeFunctor.h:54
otbNumericTraits.h
otb::Functor::ConvertTypeFunctor< typename TInputImage::PixelType, typename TInputImage ::PixelType >::OutputPixelValueType
typename itk::NumericTraits< OutputInternalPixelType >::ValueType OutputPixelValueType
Definition: otbConvertTypeFunctor.h:51
otb::Functor::ConvertTypeFunctor::~ConvertTypeFunctor
~ConvertTypeFunctor()=default
otb::Functor::ConvertTypeFunctor::SetInputComponents
void SetInputComponents(unsigned int sizeIn)
Definition: otbConvertTypeFunctor.h:63
otb::Functor::ConvertTypeFunctor< typename TInputImage::PixelType, typename TInputImage ::PixelType >::OutputPixelType
typename TInputImage ::PixelType OutputPixelType
Definition: otbConvertTypeFunctor.h:44
otb::Functor::ConvertTypeFunctor::m_cOutInternalPix
static constexpr bool m_cOutInternalPix
Definition: otbConvertTypeFunctor.h:57
otb::Functor::ConvertTypeFunctor::operator()
void operator()(OutputPixelType &out, InputPixelType const &in) const
Definition: otbConvertTypeFunctor.h:107
otb::Functor::ConvertTypeFunctor< typename TInputImage::PixelType, typename TInputImage ::PixelType >::InputInternalPixelType
typename itk::NumericTraits< InputPixelType >::ValueType InputInternalPixelType
Definition: otbConvertTypeFunctor.h:47
otb::Functor::ConvertTypeFunctor
Definition: otbConvertTypeFunctor.h:40
otbAlgoClamp.h
otb::Functor::ConvertTypeFunctor< typename TInputImage::PixelType, typename TInputImage ::PixelType >::ThresholdPixelValueType
std::common_type_t< InputPixelValueType, OutputPixelValueType > ThresholdPixelValueType
Definition: otbConvertTypeFunctor.h:52
otb::Functor::ConvertTypeFunctor::m_Zero
OutputPixelValueType m_Zero
Definition: otbConvertTypeFunctor.h:155
otb::Functor::ConvertTypeFunctor< typename TInputImage::PixelType, typename TInputImage ::PixelType >::InputPixelValueType
typename itk::NumericTraits< InputInternalPixelType >::ValueType InputPixelValueType
Definition: otbConvertTypeFunctor.h:50
otb::Functor::ConvertTypeFunctor::m_LowestB
ThresholdPixelValueType m_LowestB
Definition: otbConvertTypeFunctor.h:153
otb::Functor::ConvertTypeFunctor::Self
ConvertTypeFunctor Self
Definition: otbConvertTypeFunctor.h:45
otb::Functor::ConvertTypeFunctor::operator=
void operator=(const Self &)=delete