From d894c83fd6fc4e674dfeb1c08f9c9fe615dbf7cf Mon Sep 17 00:00:00 2001
From: Bryn Lloyd <bryn.lloyd@gmail.com>
Date: Sat, 28 Oct 2017 21:48:04 +0200
Subject: [PATCH] Added test case for SortIndexByLuminance

---
 Imaging/Color/Testing/Cxx/CMakeLists.txt      |  5 ++
 .../Testing/Cxx/ImageQuantizeToIndex.cxx      | 89 +++++++++++++++++++
 Imaging/Color/module.cmake                    |  4 +
 3 files changed, 98 insertions(+)
 create mode 100644 Imaging/Color/Testing/Cxx/CMakeLists.txt
 create mode 100644 Imaging/Color/Testing/Cxx/ImageQuantizeToIndex.cxx

diff --git a/Imaging/Color/Testing/Cxx/CMakeLists.txt b/Imaging/Color/Testing/Cxx/CMakeLists.txt
new file mode 100644
index 00000000000..bde5318dada
--- /dev/null
+++ b/Imaging/Color/Testing/Cxx/CMakeLists.txt
@@ -0,0 +1,5 @@
+vtk_add_test_cxx(${vtk-module}CxxTests tests
+  ImageQuantizeToIndex.cxx,NO_VALID
+  )
+
+vtk_test_cxx_executable(${vtk-module}CxxTests tests)
diff --git a/Imaging/Color/Testing/Cxx/ImageQuantizeToIndex.cxx b/Imaging/Color/Testing/Cxx/ImageQuantizeToIndex.cxx
new file mode 100644
index 00000000000..5e3f7168db2
--- /dev/null
+++ b/Imaging/Color/Testing/Cxx/ImageQuantizeToIndex.cxx
@@ -0,0 +1,89 @@
+#include <array>
+#include <set>
+
+#include "vtkTIFFReader.h"
+#include "vtkLookupTable.h"
+#include "vtkImageData.h"
+#include "vtkImageQuantizeRGBToIndex.h"
+#include "vtkSmartPointer.h"
+
+#include "vtkTestUtilities.h"
+
+#include <cmath>
+
+int ImageQuantizeToIndex(int argc, char *argv[])
+{
+  char* fname = vtkTestUtilities::ExpandDataFileName(
+    argc, argv, "Data/libtiff/gourds_tiled_200x300.tif");
+  // \note first I used earth.ppm, but the test failed because
+  // the lookup table contains duplicate colors (the non-sorted and sorted are identical).
+
+  auto reader = vtkSmartPointer<vtkTIFFReader>::New();
+  reader->SetFileName(fname);
+  delete[] fname;
+
+  reader->Update();
+
+  auto filter = vtkSmartPointer<vtkImageQuantizeRGBToIndex>::New();
+  filter->SetInputConnection(reader->GetOutputPort());
+  filter->SetNumberOfColors(16);
+  filter->SetSortIndexByLuminance(false);
+  filter->Update();
+  auto lut = filter->GetLookupTable();
+
+  auto filter2 = vtkSmartPointer<vtkImageQuantizeRGBToIndex>::New();
+  filter2->SetInputConnection(reader->GetOutputPort());
+  filter2->SetNumberOfColors(16);
+  filter2->SetSortIndexByLuminance(true);
+  filter2->Update();
+  auto lut2 = filter2->GetLookupTable();
+
+  bool retVal = lut->GetNumberOfColors() == lut2->GetNumberOfColors();
+  retVal = retVal && lut->GetNumberOfColors() == 16;
+  if (retVal)
+  {
+    // SortIndexByLuminance should produce the same colors, just at a different index
+    std::array<int,16> mapping;
+    double rgba[4];
+    double rgba2[4];
+
+    for (int i=0; i<16; i++)
+    {
+      lut->GetTableValue(i, rgba);
+      mapping[i] = 0;
+      double best = VTK_DOUBLE_MAX;
+
+      for (int j=0; j<16; j++)
+      {
+        lut2->GetTableValue(j, rgba2);
+        double dist = std::pow(rgba[0]-rgba2[0],2.)
+          + std::pow(rgba[1]-rgba2[1],2.)
+          + std::pow(rgba[2]-rgba2[2],2.);
+
+        if (dist < best)
+        {
+          best = dist;
+          mapping[i] = j;
+        }
+      }
+    }
+    // check mapping is correct, i.e. one-to-one correspondence
+    std::set<int> unique_mapped_values(mapping.begin(), mapping.end());
+    retVal = unique_mapped_values.size() == 16;
+    std::cerr << "mapping = " << unique_mapped_values.size() << std::endl;
+
+    if (retVal)
+    {
+      const auto data
+        = static_cast<unsigned short*>(filter->GetOutput()->GetScalarPointer());
+      const auto data2
+        = static_cast<unsigned short*>(filter2->GetOutput()->GetScalarPointer());
+      for (vtkIdType i=0; i<filter->GetOutput()->GetNumberOfPoints(); i++)
+      {
+        retVal = retVal && (mapping.at(data[i]) == data2[i]);
+      }
+    }
+  }
+
+  return !retVal;
+}
diff --git a/Imaging/Color/module.cmake b/Imaging/Color/module.cmake
index 17f633273ee..559e9595719 100644
--- a/Imaging/Color/module.cmake
+++ b/Imaging/Color/module.cmake
@@ -2,6 +2,10 @@ vtk_module(vtkImagingColor
   GROUPS
     Imaging
     StandAlone
+  TEST_DEPENDS
+    vtkImagingCore
+    vtkIOImage
+    vtkTestingCore
   KIT
     vtkImaging
   DEPENDS
-- 
GitLab