changeset 3545:551945086617

ShiftLeft + ShiftRight
author Alain Mazy <alain@mazy.be>
date Mon, 21 Oct 2019 10:13:53 +0200
parents f25e84cc5f87
children 460ba650be6f
files Core/Images/ImageProcessing.cpp Core/Images/ImageProcessing.h
diffstat 2 files changed, 82 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Images/ImageProcessing.cpp	Tue Oct 15 12:57:16 2019 +0200
+++ b/Core/Images/ImageProcessing.cpp	Mon Oct 21 10:13:53 2019 +0200
@@ -413,6 +413,41 @@
     }
   }
 
+  template <typename PixelType>
+  static void ShiftRightInternal(ImageAccessor& image,
+                                 unsigned int shift)
+  {
+    const unsigned int height = image.GetHeight();
+    const unsigned int width = image.GetWidth();
+
+    for (unsigned int y = 0; y < height; y++)
+    {
+      PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
+
+      for (unsigned int x = 0; x < width; x++, p++)
+      {
+        *p = *p >> shift;
+      }
+    }
+  }
+
+  template <typename PixelType>
+  static void ShiftLeftInternal(ImageAccessor& image,
+                                unsigned int shift)
+  {
+    const unsigned int height = image.GetHeight();
+    const unsigned int width = image.GetWidth();
+
+    for (unsigned int y = 0; y < height; y++)
+    {
+      PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
+
+      for (unsigned int x = 0; x < width; x++, p++)
+      {
+        *p = *p << shift;
+      }
+    }
+  }
 
   void ImageProcessing::Copy(ImageAccessor& target,
                              const ImageAccessor& source)
@@ -921,9 +956,52 @@
       return;
     }
 
-    throw OrthancException(ErrorCode_NotImplemented);
+    switch (image.GetFormat())
+    {
+      case PixelFormat_Grayscale8:
+      {
+        ShiftRightInternal<uint8_t>(image, shift);
+        break;
+      }
+
+      case PixelFormat_Grayscale16:
+      {
+        ShiftRightInternal<uint16_t>(image, shift);
+        break;
+      }
+    default:
+        throw OrthancException(ErrorCode_NotImplemented);
+    }
   }
 
+  void ImageProcessing::ShiftLeft(ImageAccessor& image,
+                                  unsigned int shift)
+  {
+    if (image.GetWidth() == 0 ||
+        image.GetHeight() == 0 ||
+        shift == 0)
+    {
+      // Nothing to do
+      return;
+    }
+
+    switch (image.GetFormat())
+    {
+      case PixelFormat_Grayscale8:
+      {
+        ShiftLeftInternal<uint8_t>(image, shift);
+        break;
+      }
+
+      case PixelFormat_Grayscale16:
+      {
+        ShiftLeftInternal<uint16_t>(image, shift);
+        break;
+      }
+    default:
+        throw OrthancException(ErrorCode_NotImplemented);
+    }
+  }
 
   void ImageProcessing::GetMinMaxIntegerValue(int64_t& minValue,
                                               int64_t& maxValue,
--- a/Core/Images/ImageProcessing.h	Tue Oct 15 12:57:16 2019 +0200
+++ b/Core/Images/ImageProcessing.h	Mon Oct 21 10:13:53 2019 +0200
@@ -92,6 +92,9 @@
     void ShiftRight(ImageAccessor& target,
                     unsigned int shift);
 
+    void ShiftLeft(ImageAccessor& target,
+                   unsigned int shift);
+
     void GetMinMaxIntegerValue(int64_t& minValue,
                                int64_t& maxValue,
                                const ImageAccessor& image);