changeset 4780:ebb801d44e06

optimization in ImageProcessing::ShiftScale()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 30 Aug 2021 12:13:05 +0200
parents 4f368230f32b
children 235d3a9f8dec
files OrthancFramework/Sources/Images/ImageProcessing.cpp OrthancFramework/UnitTestsSources/ImageProcessingTests.cpp
diffstat 2 files changed, 57 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/Images/ImageProcessing.cpp	Mon Aug 30 11:51:56 2021 +0200
+++ b/OrthancFramework/Sources/Images/ImageProcessing.cpp	Mon Aug 30 12:13:05 2021 +0200
@@ -1405,6 +1405,14 @@
   }
 
 
+  static bool IsIdentityRescaling(float offset,
+                                  float scaling)
+  {
+    return (std::abs(offset) <= 10.0f * std::numeric_limits<float>::epsilon() &&
+            std::abs(scaling - 1.0f) <= 10.0f * std::numeric_limits<float>::epsilon());
+  }
+  
+
   void ImageProcessing::ShiftScale2(ImageAccessor& image,
                                     float offset,
                                     float scaling,
@@ -1413,6 +1421,11 @@
     // We compute "a * x + b"
     const float a = scaling;
     const float b = offset;
+
+    if (IsIdentityRescaling(offset, scaling))
+    {
+      return;
+    }
     
     switch (image.GetFormat())
     {
@@ -1477,6 +1490,13 @@
     const float a = scaling;
     const float b = offset;
     
+    if (target.GetFormat() == source.GetFormat() &&
+        IsIdentityRescaling(offset, scaling))
+    {
+      Copy(target, source);
+      return;
+    }
+    
     switch (target.GetFormat())
     {
       case PixelFormat_Grayscale8:
--- a/OrthancFramework/UnitTestsSources/ImageProcessingTests.cpp	Mon Aug 30 11:51:56 2021 +0200
+++ b/OrthancFramework/UnitTestsSources/ImageProcessingTests.cpp	Mon Aug 30 12:13:05 2021 +0200
@@ -975,6 +975,25 @@
 }
 
 
+TEST(ImageProcessing, Grayscale8_Identity)
+{
+  Image image(PixelFormat_Float32, 5, 1, false);
+  ImageTraits<PixelFormat_Float32>::SetPixel(image, 0, 0, 0);
+  ImageTraits<PixelFormat_Float32>::SetPixel(image, 2.5, 1, 0);
+  ImageTraits<PixelFormat_Float32>::SetPixel(image, 5.5, 2, 0);
+  ImageTraits<PixelFormat_Float32>::SetPixel(image, 10.5, 3, 0);
+  ImageTraits<PixelFormat_Float32>::SetPixel(image, 255.5, 4, 0);
+
+  Image image2(PixelFormat_Grayscale8, 5, 1, false);
+  ImageProcessing::ShiftScale(image2, image, 0, 1, false);
+  ASSERT_TRUE(TestGrayscale8Pixel(image2, 0, 0, 0));
+  ASSERT_TRUE(TestGrayscale8Pixel(image2, 1, 0, 2));
+  ASSERT_TRUE(TestGrayscale8Pixel(image2, 2, 0, 5));
+  ASSERT_TRUE(TestGrayscale8Pixel(image2, 3, 0, 10));
+  ASSERT_TRUE(TestGrayscale8Pixel(image2, 4, 0, 255));
+}
+
+
 TEST(ImageProcessing, ShiftScaleGrayscale16)
 {
   Image image(PixelFormat_Grayscale16, 5, 1, false);
@@ -1011,6 +1030,24 @@
 }
 
 
+TEST(ImageProcessing, ShiftScaleSignedGrayscale16_Identity)
+{
+  Image image(PixelFormat_SignedGrayscale16, 5, 1, false);
+  SetSignedGrayscale16Pixel(image, 0, 0, 0);
+  SetSignedGrayscale16Pixel(image, 1, 0, 2);
+  SetSignedGrayscale16Pixel(image, 2, 0, 5);
+  SetSignedGrayscale16Pixel(image, 3, 0, 10);
+  SetSignedGrayscale16Pixel(image, 4, 0, 255);
+
+  ImageProcessing::ShiftScale(image, 0, 1, true);
+  ASSERT_TRUE(TestSignedGrayscale16Pixel(image, 0, 0, 0));
+  ASSERT_TRUE(TestSignedGrayscale16Pixel(image, 1, 0, 2));
+  ASSERT_TRUE(TestSignedGrayscale16Pixel(image, 2, 0, 5));
+  ASSERT_TRUE(TestSignedGrayscale16Pixel(image, 3, 0, 10));
+  ASSERT_TRUE(TestSignedGrayscale16Pixel(image, 4, 0, 255));
+}
+
+
 TEST(ImageProcessing, ShiftScale2)
 {
   std::vector<float> va;