diff Core/Images/ImageProcessing.cpp @ 3545:551945086617

ShiftLeft + ShiftRight
author Alain Mazy <alain@mazy.be>
date Mon, 21 Oct 2019 10:13:53 +0200
parents 8c66c9c2257b
children dabe17e23e23
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,