diff --git a/Modules/Core/ImageBase/include/otbNoDataHelper.h b/Modules/Core/ImageBase/include/otbNoDataHelper.h new file mode 100644 index 0000000000000000000000000000000000000000..e9ce0fa00c0b7dae586ae6f1e26ce5c2b6748ec5 --- /dev/null +++ b/Modules/Core/ImageBase/include/otbNoDataHelper.h @@ -0,0 +1,75 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef __otbNoDataHelper_h +#define __otbNoDataHelper_h +#include <vector> +#include <cassert> +#include "otbNoDataHelper.h" +#include <itkVariableLengthVector.h> + +namespace otb +{ +/** +* Test if the pixel corresponds to a no data pixel according to a +* vector of no data flags, and a vector of no data values. +* \param pixel The pixel to test +* \param flags A vector of size > 1 containing a flag per band to +* indicate if a no data value is available for this band +* \param values A vector of size > 1 corresponding to the no data +* value for each band. If flag is 0, the value will be ignored. +*/ +template<typename T> bool IsNoData(const T & pixel, const +std::vector<bool> & flags, const std::vector<double> & values) { + assert(flags.size()>0); + assert(values.size()>0); + + if(std::isnan(flags[0])) + return true; + + + if(flags[0]) + { + return (pixel == values[0]); + } + else + { + return false; + } +} +/** + * Specialization of IsNoData function to handle itk::VariableLengthVector + */ +template <typename T> bool IsNoData(const itk::VariableLengthVector<T> & pixel, const std::vector<bool> & flags, const std::vector<double> & values) +{ + assert(flags.size()>=pixel.Size()); + assert(values.size()>=pixel.Size()); + + for(unsigned int i = 0; i < pixel.Size();++i) + { + if(std::isnan(pixel[i]) || (flags[i] && (pixel[i] == values[i]))) + { + return true; + } + } + return false; +} + + +} // End namespace otb + +#endif diff --git a/Modules/Core/ImageBase/test/CMakeLists.txt b/Modules/Core/ImageBase/test/CMakeLists.txt index 52ecd423d23e8580c1c1e20a016b31fffeecbcbe..dd4ecc9d09b0a5477f4d0e66786ffeda21dab34c 100644 --- a/Modules/Core/ImageBase/test/CMakeLists.txt +++ b/Modules/Core/ImageBase/test/CMakeLists.txt @@ -28,6 +28,7 @@ set(OTBImageBaseTests otbImageFunctionAdaptor.cxx otbMultiChannelExtractROINew.cxx otbMetaImageFunction.cxx + otbNoDataHelperTest.cxx ) add_executable(otbImageBaseTestDriver ${OTBImageBaseTests}) @@ -824,3 +825,6 @@ otb_add_test(NAME ioTvOtbVectorImageTestRadarsat COMMAND otbImageBaseTestDriver otbVectorImageLegacyTest LARGEINPUT{/RADARSAT1/GOMA/SCENE01/} ${TEMP}/ioOtbVectorImageTestRadarsat.txt) + +otb_add_test(NAME coreImageBaseNoDataHelperTest COMMAND otbImageBaseTestDriver + otbNoDataHelperTest) diff --git a/Modules/Core/ImageBase/test/otbImageBaseTestDriver.cxx b/Modules/Core/ImageBase/test/otbImageBaseTestDriver.cxx index fc275609cb19c75305902543f926fc37e3cb3f83..dcf63d236ebad2c066389989da824836833ab7d9 100644 --- a/Modules/Core/ImageBase/test/otbImageBaseTestDriver.cxx +++ b/Modules/Core/ImageBase/test/otbImageBaseTestDriver.cxx @@ -31,4 +31,5 @@ void RegisterTests() REGISTER_TEST(otbMultiChannelExtractROINew); REGISTER_TEST(otbMetaImageFunction); REGISTER_TEST(otbMetaImageFunctionNew); + REGISTER_TEST(otbNoDataHelperTest); } diff --git a/Modules/Core/ImageBase/test/otbNoDataHelperTest.cxx b/Modules/Core/ImageBase/test/otbNoDataHelperTest.cxx new file mode 100644 index 0000000000000000000000000000000000000000..3ea2f96c1c8ec4d044afdafa95722a2b6610fc3e --- /dev/null +++ b/Modules/Core/ImageBase/test/otbNoDataHelperTest.cxx @@ -0,0 +1,47 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ + +#include "otbNoDataHelper.h" +#include "otbMacro.h" + +int otbNoDataHelperTest(int itkNotUsed(argc),char ** itkNotUsed(argv)) +{ + std::vector<bool> b1(1,true); + std::vector<double> v1(1,0); + + otbControlConditionTestMacro(otb::IsNoData(10,b1,v1)," wrong output of IsNoData function"); + otbControlConditionTestMacro(!otb::IsNoData(0,b1,v1)," wrong output of IsNoData function"); + b1[0]=false; + otbControlConditionTestMacro(otb::IsNoData(10,b1,v1)," wrong output of IsNoData function"); + otbControlConditionTestMacro(otb::IsNoData(0,b1,v1)," wrong output of IsNoData function"); + + + std::vector<bool> b2(4,true); + std::vector<double> v2(4,1); + + itk::VariableLengthVector<unsigned short int> v(4); + v.Fill(10); + + otbControlConditionTestMacro(otb::IsNoData(v,b2,v2)," wrong output of IsNoData function"); + v[1]=1; + otbControlConditionTestMacro(!otb::IsNoData(v,b2,v2)," wrong output of IsNoData function"); + b2[1]=false; + otbControlConditionTestMacro(otb::IsNoData(v,b2,v2)," wrong output of IsNoData function"); + + return EXIT_SUCCESS; +}