OTB  9.0.0
Orfeo Toolbox
otbConvertPixelBuffer.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbConvertPixelBuffer_hxx
23 #define otbConvertPixelBuffer_hxx
24 #include "otbConvertPixelBuffer.h"
25 
26 #include "itkConvertPixelBuffer.h"
27 
28 namespace otb
29 {
30 template <typename InputPixelType, typename OutputPixelType, class OutputConvertTraits>
31 void ConvertPixelBuffer<InputPixelType, OutputPixelType, OutputConvertTraits>::Convert(InputPixelType* inputData, int inputNumberOfComponents,
32  OutputPixelType* outputData, size_t size)
33 {
34  if ((OutputConvertTraits::GetNumberOfComponents() == 2) && inputNumberOfComponents == 1)
35  {
36  // OTB patch : monoband to complex
37  ConvertGrayToComplex(inputData, outputData, size);
38  }
39  else
40  {
41  // use ITK pixel buffer converter
42  itk::ConvertPixelBuffer<InputPixelType, OutputPixelType, OutputConvertTraits>::Convert(inputData, inputNumberOfComponents, outputData, size);
43  }
44 }
45 
46 // Convert Grayscale to Complex
47 template <typename InputPixelType, typename OutputPixelType, class OutputConvertTraits>
49  size_t size)
50 {
51  InputPixelType* endInput = inputData + size;
52  while (inputData != endInput)
53  {
54  OutputConvertTraits::SetNthComponent(0, *outputData, static_cast<OutputComponentType>(*inputData));
55  OutputConvertTraits::SetNthComponent(1, *outputData, static_cast<OutputComponentType>(0.));
56  inputData++;
57  outputData++;
58  }
59 }
60 
61 /* To be able to convert transparently*/
62 
63 template <typename InputType, typename OutputType>
64 OutputType ComplexCast(const std::complex<InputType>& in, const OutputType& itkNotUsed(dummy))
65 {
66  typedef typename itk::NumericTraits<std::complex<InputType>>::RealType RealType;
67  typedef typename itk::NumericTraits<std::complex<InputType>>::ScalarRealType ScalarRealType;
68 
69  RealType inReal(static_cast<ScalarRealType>(in.real()), static_cast<ScalarRealType>(in.imag()));
70 
71  return static_cast<OutputType>(std::abs(inReal));
72 }
73 
74 template <typename InputType, typename OutputType>
75 std::complex<OutputType> ComplexCast(const std::complex<InputType>& in, const std::complex<OutputType>& itkNotUsed(dummy))
76 {
77  typedef typename itk::NumericTraits<std::complex<InputType>>::RealType RealType;
78  typedef typename itk::NumericTraits<std::complex<InputType>>::ScalarRealType ScalarRealType;
79 
80  RealType inReal(static_cast<ScalarRealType>(in.real()), static_cast<ScalarRealType>(in.imag()));
81 
82  return static_cast<std::complex<OutputType>>(inReal);
83 }
84 
85 template <typename InputPixelType, typename OutputPixelType, class OutputConvertTraits>
87  int itkNotUsed(inputNumberOfComponents),
88  OutputPixelType* outputData, size_t size)
89 {
90  OutputPixelType dummy;
91  std::complex<InputPixelType>* endInput = inputData + size;
92  while (inputData != endInput)
93  {
94  OutputConvertTraits::SetNthComponent(0, *outputData, static_cast<OutputPixelType>(ComplexCast(*inputData, dummy)));
95  inputData++;
96  outputData++;
97  }
98 }
99 
100 template <typename InputPixelType, typename OutputPixelType, class OutputConvertTraits>
102  OutputPixelType* outputData, size_t size)
103 {
104  itk::ConvertPixelBuffer<InputPixelType, OutputPixelType, OutputConvertTraits>::ConvertVectorImage(inputData, inputNumberOfComponents, outputData, size);
105 }
106 
107 template <typename InputPixelType, typename OutputPixelType, class OutputConvertTraits>
109  int inputNumberOfComponents,
110  OutputPixelType* outputData, size_t size)
111 {
112  size_t length = size * (size_t)inputNumberOfComponents;
113  for (size_t i = 0; i < length / 2; i++)
114  {
115  OutputConvertTraits::SetNthComponent(0, *outputData, (*inputData).real());
116  ++outputData;
117  OutputConvertTraits::SetNthComponent(0, *outputData, (*inputData).imag());
118  ++outputData;
119  ++inputData;
120  }
121 }
122 
123 template <typename InputPixelType, typename OutputPixelType, class OutputConvertTraits>
125  std::complex<InputPixelType>* inputData, int inputNumberOfComponents, OutputPixelType* outputData, size_t size)
126 {
127  size_t length = size * (size_t)inputNumberOfComponents;
128  OutputPixelType dummy;
129  for (size_t i = 0; i < length; i++)
130  {
131  OutputConvertTraits::SetNthComponent(0, *outputData, ComplexCast(*inputData, dummy));
132  ++outputData;
133  ++inputData;
134  }
135 }
136 
137 } // end namespace otb
138 
139 #endif
otbConvertPixelBuffer.h
otb::ConvertPixelBuffer::Convert
static void Convert(InputPixelType *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
Definition: otbConvertPixelBuffer.hxx:31
otb::ConvertPixelBuffer::ConvertVectorImage
static void ConvertVectorImage(InputPixelType *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
Definition: otbConvertPixelBuffer.hxx:101
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ComplexCast
OutputType ComplexCast(const std::complex< InputType > &in, const OutputType &)
Definition: otbConvertPixelBuffer.hxx:64
otb::ConvertPixelBuffer::ConvertGrayToComplex
static void ConvertGrayToComplex(InputPixelType *inputData, OutputPixelType *OutputData, vcl_size_t size)
Definition: otbConvertPixelBuffer.hxx:48
otb::mpl::GetNumberOfComponents
unsigned int GetNumberOfComponents(PixelType const &pix)
Definition: otbArrayTraits.h:215
otb::ConvertPixelBuffer::ConvertComplexToGray
static void ConvertComplexToGray(std::complex< InputPixelType > *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
Definition: otbConvertPixelBuffer.hxx:86
otb::ConvertPixelBuffer::OutputComponentType
OutputConvertTraits::ComponentType OutputComponentType
Definition: otbConvertPixelBuffer.h:51
otb::ConvertPixelBuffer::ConvertComplexVectorImageToVectorImage
static void ConvertComplexVectorImageToVectorImage(std::complex< InputPixelType > *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
Definition: otbConvertPixelBuffer.hxx:108
otb::ConvertPixelBuffer::ConvertComplexVectorImageToVectorImageComplex
static void ConvertComplexVectorImageToVectorImageComplex(std::complex< InputPixelType > *inputData, int inputNumberOfComponents, OutputPixelType *outputData, vcl_size_t size)
Definition: otbConvertPixelBuffer.hxx:124