changeset 4079:73c22208272f

ImageProcessing::ShiftScale2()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 22 Jun 2020 18:59:45 +0200
parents ae7ebd5b0443
children f18eaade6153
files Core/Images/ImageProcessing.cpp Core/Images/ImageProcessing.h UnitTestsSources/ImageProcessingTests.cpp
diffstat 3 files changed, 81 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Images/ImageProcessing.cpp	Mon Jun 22 15:09:40 2020 +0200
+++ b/Core/Images/ImageProcessing.cpp	Mon Jun 22 18:59:45 2020 +0200
@@ -1470,6 +1470,40 @@
   }
 
 
+  void ImageProcessing::ShiftScale2(ImageAccessor& image,
+                                    float offset,
+                                    float scaling,
+                                    bool useRound)
+  {
+    if (std::abs(scaling) < 0.001f)
+    {
+      Set(image, offset);
+    }
+    else
+    {
+      ShiftScale(image, offset / scaling, scaling, useRound);
+    }
+  }
+
+
+  void ImageProcessing::ShiftScale2(ImageAccessor& target,
+                                    const ImageAccessor& source,
+                                    float offset,
+                                    float scaling,
+                                    bool useRound)
+  {
+    if (std::abs(scaling) < 0.0001f)
+    {
+      Set(target, offset);
+    }
+    else
+    {
+      ShiftScale(target, source, offset / scaling, scaling, useRound);
+    }
+  }
+
+
+
   void ImageProcessing::Invert(ImageAccessor& image, int64_t maxValue)
   {
     const unsigned int width = image.GetWidth();
--- a/Core/Images/ImageProcessing.h	Mon Jun 22 15:09:40 2020 +0200
+++ b/Core/Images/ImageProcessing.h	Mon Jun 22 18:59:45 2020 +0200
@@ -143,6 +143,18 @@
                            float scaling,
                            bool useRound);
 
+    // Computes "x * scaling + offset" inplace. "useRound" is expensive.
+    static void ShiftScale2(ImageAccessor& image,
+                            float offset,
+                            float scaling,
+                            bool useRound);
+
+    static void ShiftScale2(ImageAccessor& target,
+                            const ImageAccessor& source,
+                            float offset,
+                            float scaling,
+                            bool useRound);
+
     static void Invert(ImageAccessor& image);
 
     static void Invert(ImageAccessor& image, int64_t maxValue);
--- a/UnitTestsSources/ImageProcessingTests.cpp	Mon Jun 22 15:09:40 2020 +0200
+++ b/UnitTestsSources/ImageProcessingTests.cpp	Mon Jun 22 18:59:45 2020 +0200
@@ -1016,3 +1016,38 @@
   ASSERT_TRUE(TestSignedGrayscale16Pixel(image, 3, 0, -82));
   ASSERT_TRUE(TestSignedGrayscale16Pixel(image, 4, 0, 2736));
 }
+
+
+TEST(ImageProcessing, ShiftScale2)
+{
+  std::vector<float> va;
+  va.push_back(0);
+  va.push_back(-10);
+  va.push_back(5);
+  
+  std::vector<float> vb;
+  vb.push_back(0);
+  vb.push_back(-42);
+  vb.push_back(42);
+
+  Image source(PixelFormat_Float32, 1, 1, false);
+  ImageTraits<PixelFormat_Float32>::SetFloatPixel(source, 10, 0, 0);
+  
+  for (std::vector<float>::const_iterator a = va.begin(); a != va.end(); ++a)
+  {
+    for (std::vector<float>::const_iterator b = vb.begin(); b != vb.end(); ++b)
+    {
+      Image target(PixelFormat_Float32, 1, 1, false);
+
+      ImageProcessing::Copy(target, source);
+      ImageProcessing::ShiftScale2(target, *b, *a, false);
+      ASSERT_FLOAT_EQ((*a) * 10.0f + (*b),
+                      ImageTraits<PixelFormat_Float32>::GetFloatPixel(target, 0, 0));
+
+      ImageProcessing::Copy(target, source);
+      ImageProcessing::ShiftScale(target, *b, *a, false);
+      ASSERT_FLOAT_EQ((*a) * (10.0f + (*b)),
+                      ImageTraits<PixelFormat_Float32>::GetFloatPixel(target, 0, 0));
+    }
+  }
+}