changeset 3547:dabe17e23e23

Copy RGBA to BGRA & Set with alpha
author Alain Mazy <alain@mazy.be>
date Tue, 22 Oct 2019 17:30:43 +0200
parents 460ba650be6f
children e1ce68692069 fab5777f4dd4
files Core/Images/ImageProcessing.cpp Core/Images/ImageProcessing.h
diffstat 2 files changed, 86 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Images/ImageProcessing.cpp	Mon Oct 21 10:14:08 2019 +0200
+++ b/Core/Images/ImageProcessing.cpp	Tue Oct 22 17:30:43 2019 +0200
@@ -803,6 +803,29 @@
       return;
     }
 
+    if ((target.GetFormat() == PixelFormat_BGRA32 &&
+         source.GetFormat() == PixelFormat_RGBA32)
+        || (target.GetFormat() == PixelFormat_RGBA32 &&
+            source.GetFormat() == PixelFormat_BGRA32))
+    {
+      for (unsigned int y = 0; y < height; y++)
+      {
+        const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
+        uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
+        for (unsigned int x = 0; x < width; x++)
+        {
+          q[0] = p[2];
+          q[1] = p[1];
+          q[2] = p[0];
+          q[3] = p[3];
+          p += 4;
+          q += 4;
+        }
+      }
+
+      return;
+    }
+
     if (target.GetFormat() == PixelFormat_RGB24 &&
         source.GetFormat() == PixelFormat_RGB48)
     {
@@ -944,6 +967,63 @@
     }
   }
 
+  void ImageProcessing::Set(ImageAccessor& image,
+           uint8_t red,
+           uint8_t green,
+           uint8_t blue,
+           ImageAccessor& alpha)
+  {
+    uint8_t p[4];
+
+    if (alpha.GetWidth() != image.GetWidth() || alpha.GetHeight() != image.GetHeight())
+    {
+      throw OrthancException(ErrorCode_IncompatibleImageSize);
+    }
+
+    if (alpha.GetFormat() != PixelFormat_Grayscale8)
+    {
+      throw OrthancException(ErrorCode_NotImplemented);
+    }
+
+    switch (image.GetFormat())
+    {
+      case PixelFormat_RGBA32:
+        p[0] = red;
+        p[1] = green;
+        p[2] = blue;
+        break;
+
+      case PixelFormat_BGRA32:
+        p[0] = blue;
+        p[1] = green;
+        p[2] = red;
+        break;
+
+      default:
+        throw OrthancException(ErrorCode_NotImplemented);
+    }
+
+    const unsigned int width = image.GetWidth();
+    const unsigned int height = image.GetHeight();
+
+    for (unsigned int y = 0; y < height; y++)
+    {
+      uint8_t* q = reinterpret_cast<uint8_t*>(image.GetRow(y));
+      uint8_t* a = reinterpret_cast<uint8_t*>(alpha.GetRow(y));
+
+      for (unsigned int x = 0; x < width; x++)
+      {
+        for (unsigned int i = 0; i < 3; i++)
+        {
+          q[i] = p[i];
+        }
+        q[3] = *a;
+        q += 4;
+        ++a;
+      }
+    }
+  }
+
 
   void ImageProcessing::ShiftRight(ImageAccessor& image,
                                    unsigned int shift)
--- a/Core/Images/ImageProcessing.h	Mon Oct 21 10:14:08 2019 +0200
+++ b/Core/Images/ImageProcessing.h	Tue Oct 22 17:30:43 2019 +0200
@@ -89,6 +89,12 @@
              uint8_t blue,
              uint8_t alpha);
 
+    void Set(ImageAccessor& image,
+             uint8_t red,
+             uint8_t green,
+             uint8_t blue,
+             ImageAccessor& alpha);
+
     void ShiftRight(ImageAccessor& target,
                     unsigned int shift);