OTB  9.0.0
Orfeo Toolbox
otbNoDataHelper.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 otbNoDataHelper_h
22 #define otbNoDataHelper_h
23 #include <vector>
24 #include <cassert>
25 #include "vnl/vnl_math.h"
26 #include <itkVariableLengthVector.h>
27 #include "OTBMetadataExport.h"
28 
29 #include "otbImageMetadata.h"
30 
31 namespace itk
32 {
33 class MetaDataDictionary;
34 }
35 
36 namespace otb
37 {
42 bool OTBMetadata_EXPORT ReadNoDataFlags(const ImageMetadata & imd, std::vector<bool>& flags, std::vector<double>& values);
43 
48 void OTBMetadata_EXPORT WriteNoDataFlags(const std::vector<bool>& flags, const std::vector<double>& values, ImageMetadata & imd);
49 
50 
51 
62 template <typename T>
63 bool IsNoData(const T& pixel, const std::vector<bool>& flags, const std::vector<double>& values, bool nanIsNoData = false)
64 {
65  assert(flags.size() > 0);
66  assert(values.size() > 0);
68 
69  return ((nanIsNoData && vnl_math_isnan(pixel)) || (flags[0] && (pixel == values[0])));
70 }
71 
88 template <typename T>
89 T ChangeNoData(const T& pixel, const std::vector<bool>& flags, const std::vector<double>& values, const std::vector<double>& newValues,
90  bool nanIsNoData = false)
91 {
92  assert(flags.size() > 0);
93  assert(values.size() > 0);
94  assert(newValues.size() > 0);
96 
97  if (nanIsNoData && vnl_math_isnan(pixel))
98  {
99  return static_cast<T>(newValues[0]);
100  }
101 
102  if (flags[0] && pixel == values[0])
103  {
104  return static_cast<T>(newValues[0]);
105  }
106  return pixel;
107 }
108 
109 
113 template <typename T>
114 bool IsNoData(const itk::VariableLengthVector<T>& pixel, const std::vector<bool>& flags, const std::vector<double>& values, bool nanIsNoData = false)
115 {
116  assert(flags.size() >= pixel.Size());
117  assert(values.size() >= pixel.Size());
119 
120  for (unsigned int i = 0; i < pixel.Size(); ++i)
121  {
122  if ((nanIsNoData && vnl_math_isnan(pixel[i])) || (flags[i] && (pixel[i] == values[i])))
123  {
124  return true;
125  }
126  }
127  return false;
128 }
129 
130 
134 template <typename T>
135 itk::VariableLengthVector<T> ChangeNoData(const itk::VariableLengthVector<T>& pixel, const std::vector<bool>& flags, const std::vector<double>& values,
136  const std::vector<double>& newValues, bool nanIsNoData = false)
137 {
138  assert(flags.size() >= pixel.Size());
139  assert(values.size() >= pixel.Size());
140  assert(newValues.size() >= pixel.Size());
142 
143  itk::VariableLengthVector<T> outPixel(pixel.Size());
144 
145  for (unsigned int i = 0; i < pixel.Size(); ++i)
146  {
147  if ((nanIsNoData && vnl_math_isnan(pixel[i])) || (flags[i] && (pixel[i] == values[i])))
148  {
149  outPixel[i] = newValues[i];
150  }
151  else
152  {
153  outPixel[i] = pixel[i];
154  }
155  }
156 
157  return outPixel;
158 }
159 
160 } // End namespace otb
161 
162 #endif
otb::WriteNoDataFlags
void OTBMetadata_EXPORT WriteNoDataFlags(const std::vector< bool > &flags, const std::vector< double > &values, ImageMetadata &imd)
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ChangeNoData
T ChangeNoData(const T &pixel, const std::vector< bool > &flags, const std::vector< double > &values, const std::vector< double > &newValues, bool nanIsNoData=false)
Definition: otbNoDataHelper.h:89
otb::ReadNoDataFlags
bool OTBMetadata_EXPORT ReadNoDataFlags(const ImageMetadata &imd, std::vector< bool > &flags, std::vector< double > &values)
otb::IsNoData
bool IsNoData(const T &pixel, const std::vector< bool > &flags, const std::vector< double > &values, bool nanIsNoData=false)
Definition: otbNoDataHelper.h:63
itk
Definition: otbNoDataHelper.h:31
otbImageMetadata.h