changeset 351:e722d8252522

support for grayscale 16 images
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 20 Dec 2024 12:13:01 +0100
parents 8d79f55453df
children 25630706f4ed
files ViewerPlugin/Plugin.cpp
diffstat 1 files changed, 55 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/ViewerPlugin/Plugin.cpp	Fri Dec 20 11:34:31 2024 +0100
+++ b/ViewerPlugin/Plugin.cpp	Fri Dec 20 12:13:01 2024 +0100
@@ -59,6 +59,31 @@
     std::unique_ptr<OrthancStone::IOrthancConnection>  orthanc_;
     bool                                               smooth_;
 
+    static void RenderGrayscale(Orthanc::ImageAccessor& target,
+                                const Orthanc::ImageAccessor& source)
+    {
+      Orthanc::Image converted(Orthanc::PixelFormat_Float32, source.GetWidth(), source.GetHeight(), false);
+      Orthanc::ImageProcessing::Convert(converted, source);
+
+      float minValue, maxValue;
+      Orthanc::ImageProcessing::GetMinMaxFloatValue(minValue, maxValue, converted);
+
+      assert(minValue <= maxValue);
+      if (std::abs(maxValue - minValue) < 0.0001)
+      {
+        Orthanc::ImageProcessing::Set(target, 0);
+      }
+      else
+      {
+        const float scaling = 255.0f / (maxValue - minValue);
+        const float offset = -minValue;
+
+        Orthanc::Image rescaled(Orthanc::PixelFormat_Grayscale8, source.GetWidth(), source.GetHeight(), false);
+        Orthanc::ImageProcessing::ShiftScale(rescaled, converted, static_cast<float>(offset), static_cast<float>(scaling), false);
+        Orthanc::ImageProcessing::Convert(target, rescaled);
+      }
+    }
+
   public:
     explicit OrthancPyramidFrameFetcher(OrthancStone::IOrthancConnection* orthanc,
                                         bool smooth) :
@@ -71,8 +96,8 @@
       }
     }
 
-    DecodedTiledPyramid * Fetch(const std::string &instanceId,
-                                unsigned frameNumber) ORTHANC_OVERRIDE
+    DecodedTiledPyramid* Fetch(const std::string &instanceId,
+                               unsigned frameNumber) ORTHANC_OVERRIDE
     {
       OrthancPlugins::MemoryBuffer buffer;
       buffer.GetDicomInstance(instanceId.c_str());
@@ -88,6 +113,14 @@
           format = Orthanc::PixelFormat_RGB24;
           break;
 
+        case OrthancPluginPixelFormat_Grayscale8:
+          format = Orthanc::PixelFormat_Grayscale8;
+          break;
+
+        case OrthancPluginPixelFormat_Grayscale16:
+          format = Orthanc::PixelFormat_Grayscale16;
+          break;
+
         default:
           throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
       }
@@ -95,10 +128,27 @@
       Orthanc::ImageAccessor source;
       source.AssignReadOnly(format, frame->GetWidth(), frame->GetHeight(), frame->GetPitch(), frame->GetBuffer());
 
-      std::unique_ptr<Orthanc::ImageAccessor> copy(new Orthanc::Image(Orthanc::PixelFormat_RGB24, source.GetWidth(), source.GetHeight(), false));
-      Orthanc::ImageProcessing::Convert(*copy, source);
+      std::unique_ptr<Orthanc::ImageAccessor> rendered(new Orthanc::Image(Orthanc::PixelFormat_RGB24, source.GetWidth(), source.GetHeight(), false));
+
+      switch (format)
+      {
+        case Orthanc::PixelFormat_RGB24:
+          Orthanc::ImageProcessing::Copy(*rendered, source);
+          break;
 
-      return new OnTheFlyPyramid(copy.release(), 512, 512, smooth_);
+        case Orthanc::PixelFormat_Grayscale8:
+          Orthanc::ImageProcessing::Convert(*rendered, source);
+          break;
+
+        case Orthanc::PixelFormat_Grayscale16:
+          RenderGrayscale(*rendered, source);
+          break;
+
+        default:
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
+      }
+
+      return new OnTheFlyPyramid(rendered.release(), 512, 512, smooth_);
     }
   };
 }