From 85b278f8aee6a1141fd0b251de26929edcdece8c Mon Sep 17 00:00:00 2001
From: Julien Michel <julien.michel@orfeo-toolbox.org>
Date: Fri, 11 Sep 2015 17:01:55 +0200
Subject: [PATCH] TEST: Adding test for ImageToNoDataMaskFilter. Test is
 standalone and does not require ulages or baselines from OTB-Data

---
 .../ImageManipulation/test/CMakeLists.txt     |  6 +-
 .../test/otbImageManipulationTestDriver.cxx   |  1 +
 .../test/otbImageToNoDataMaskFilter.cxx       | 88 +++++++++++++++++++
 3 files changed, 94 insertions(+), 1 deletion(-)
 create mode 100644 Modules/Filtering/ImageManipulation/test/otbImageToNoDataMaskFilter.cxx

diff --git a/Modules/Filtering/ImageManipulation/test/CMakeLists.txt b/Modules/Filtering/ImageManipulation/test/CMakeLists.txt
index 48a44bee06..6b26881e63 100644
--- a/Modules/Filtering/ImageManipulation/test/CMakeLists.txt
+++ b/Modules/Filtering/ImageManipulation/test/CMakeLists.txt
@@ -71,6 +71,7 @@ otbFunctionWithNeighborhoodToImageFilterNew.cxx
 otbEuclideanDistanceMetricWithMissingValue.cxx
 otbEuclideanDistanceMetricWithMissingValueNew.cxx
 otbChangeNoDataValueFilter.cxx
+otbImageToNoDataMaskFilter.cxx
 )
 
 add_executable(otbImageManipulationTestDriver ${OTBImageManipulationTests})
@@ -672,4 +673,7 @@ otb_add_test(NAME bfTuEuclideanDistanceMetricWithMissingValueNew COMMAND otbImag
   otbEuclideanDistanceMetricWithMissingValueNew)
 
 otb_add_test(NAME filteringImageManipulationChangeNoDataValueFilter COMMAND otbImageManipulationTestDriver
-             otbChangeNoDataValueFilter)
+  otbChangeNoDataValueFilter)
+
+otb_add_test(NAME filteringImageManipulationImageToNoDataMaskFilter COMMAND otbImageManipulationTestDriver
+  otbImageToNoDataMaskFilter)
diff --git a/Modules/Filtering/ImageManipulation/test/otbImageManipulationTestDriver.cxx b/Modules/Filtering/ImageManipulation/test/otbImageManipulationTestDriver.cxx
index fa7931fd88..5fd89db0d9 100644
--- a/Modules/Filtering/ImageManipulation/test/otbImageManipulationTestDriver.cxx
+++ b/Modules/Filtering/ImageManipulation/test/otbImageManipulationTestDriver.cxx
@@ -77,4 +77,5 @@ void RegisterTests()
   REGISTER_TEST(otbEuclideanDistanceMetricWithMissingValue);
   REGISTER_TEST(otbEuclideanDistanceMetricWithMissingValueNew);
   REGISTER_TEST(otbChangeNoDataValueFilter);
+  REGISTER_TEST(otbImageToNoDataMaskFilter);
 }
diff --git a/Modules/Filtering/ImageManipulation/test/otbImageToNoDataMaskFilter.cxx b/Modules/Filtering/ImageManipulation/test/otbImageToNoDataMaskFilter.cxx
new file mode 100644
index 0000000000..109a2ec7d4
--- /dev/null
+++ b/Modules/Filtering/ImageManipulation/test/otbImageToNoDataMaskFilter.cxx
@@ -0,0 +1,88 @@
+/*=========================================================================
+
+  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 "otbImage.h"
+#include "otbImageToNoDataMaskFilter.h"
+#include "itkImageRegionIterator.h"
+
+int otbImageToNoDataMaskFilter(int itkNotUsed(argc),char * itkNotUsed(argv) [])
+{
+
+  // Build an image
+  typedef otb::Image<double> ImageType;
+  ImageType::Pointer img = ImageType::New();
+
+  ImageType::SizeType size;
+  size.Fill(20);
+
+  ImageType::RegionType region;
+  region.SetSize(size);
+
+  // Fill it with a default value
+  img->SetRegions(region);
+  img->Allocate();
+  img->FillBuffer(10);
+
+  // Write no-data flags to it
+  std::vector<bool> flags(1,true);
+  std::vector<double> values(1,-10.);
+  otb::WriteNoDataFlags(flags,values,img->GetMetaDataDictionary());
+
+  // Fill half of the pixels with no-data values
+  itk::ImageRegionIterator<ImageType> it(img,region);
+  unsigned int count = 0;
+  for(it.GoToBegin();!it.IsAtEnd();++it,++count)
+    {
+    if (count%2 == 0)
+      it.Set(-10.);
+    }
+
+  // Instanciate filter
+  typedef otb::ImageToNoDataMaskFilter<ImageType,ImageType> FilterType;
+  FilterType::Pointer filter = FilterType::New();
+
+  filter->SetInput(img);
+  filter->SetInsideValue(255);
+  filter->SetOutsideValue(0);
+  filter->Update();
+
+  // Check output
+  it = itk::ImageRegionIterator<ImageType>(filter->GetOutput(),region);
+  count = 0;
+
+  bool failed = false;
+  
+  for(it.GoToBegin();!it.IsAtEnd();++it,++count)
+    {
+    if (count%2 == 0 and it.Get()!=0)
+      {
+      std::cerr<<"Pixel should be masked"<<std::endl;
+      failed = true;
+      }
+    else if(count%2 == 1 and it.Get()!=255)
+      {
+      std::cerr<<"Pixel should not be masked"<<std::endl;
+      failed = true;
+      }
+    }
+  
+  if(failed)
+    return EXIT_FAILURE;
+
+  return EXIT_SUCCESS;
+}
-- 
GitLab