changeset 2032:d10bab7cc396 deep-learning

added OpenGLTexture::Download()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 21 Jan 2023 16:48:56 +0100
parents a56f7ed0cdf9
children 23b0a42eea85
files OrthancStone/Sources/OpenGL/OpenGLTexture.cpp OrthancStone/Sources/OpenGL/OpenGLTexture.h
diffstat 2 files changed, 49 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancStone/Sources/OpenGL/OpenGLTexture.cpp	Fri Dec 23 17:51:07 2022 +0100
+++ b/OrthancStone/Sources/OpenGL/OpenGLTexture.cpp	Sat Jan 21 16:48:56 2023 +0100
@@ -24,6 +24,7 @@
 #include "OpenGLTexture.h"
 #include "IOpenGLContext.h"
 
+#include <Images/Image.h>
 #include <Logging.h>
 #include <OrthancException.h>
 
@@ -31,10 +32,11 @@
 {
   namespace OpenGL
   {
-    OpenGLTexture::OpenGLTexture(OpenGL::IOpenGLContext& context)
-      : width_(0)
-      , height_(0)
-      , context_(context)
+    OpenGLTexture::OpenGLTexture(OpenGL::IOpenGLContext& context) :
+      texture_(0),
+      width_(0),
+      height_(0),
+      context_(context)
     {
       if (!context_.IsContextLost())
       {
@@ -43,7 +45,7 @@
         if (texture_ == 0)
         {
           throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
-            "Cannot create an OpenGL program");
+            "Cannot create an OpenGL texture");
         }
       }
     }
@@ -97,23 +99,26 @@
         glActiveTexture(GL_TEXTURE0);
         glBindTexture(GL_TEXTURE_2D, texture_);
 
-        GLenum sourceFormat, internalFormat;
+        GLenum sourceFormat, internalFormat, pixelType;
 
         switch (image.GetFormat())
         {
         case Orthanc::PixelFormat_Grayscale8:
           sourceFormat = GL_RED;
           internalFormat = GL_RED;
+          pixelType = GL_UNSIGNED_BYTE;
           break;
 
         case Orthanc::PixelFormat_RGB24:
           sourceFormat = GL_RGB;
           internalFormat = GL_RGB;
+          pixelType = GL_UNSIGNED_BYTE;
           break;
 
         case Orthanc::PixelFormat_RGBA32:
           sourceFormat = GL_RGBA;
           internalFormat = GL_RGBA;
+          pixelType = GL_UNSIGNED_BYTE;
           break;
 
         default:
@@ -129,7 +134,7 @@
 
         // Load the texture from the image buffer
         glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, image.GetWidth(), image.GetHeight(),
-                     0, sourceFormat, GL_UNSIGNED_BYTE, image.GetConstBuffer());
+                     0, sourceFormat, pixelType, image.GetConstBuffer());
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, interpolation);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, interpolation);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
@@ -144,5 +149,39 @@
       glBindTexture(GL_TEXTURE_2D, texture_);
       glUniform1i(location, 0 /* texture unit */);
     }
+
+
+    Orthanc::ImageAccessor* OpenGLTexture::Download(Orthanc::PixelFormat format)
+    {
+      if (context_.IsContextLost())
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
+                                        "OpenGL context is lost");
+      }
+
+      std::unique_ptr<Orthanc::ImageAccessor> target(new Orthanc::Image(format, width_, height_, true));
+
+      glBindTexture(GL_TEXTURE_2D, texture_);
+
+      switch (format)
+      {
+        case Orthanc::PixelFormat_Grayscale8:
+          glGetTexImage(GL_TEXTURE_2D, 0 /* base level */, GL_RED, GL_UNSIGNED_BYTE, target->GetBuffer());
+          break;
+
+        case Orthanc::PixelFormat_RGB24:
+          glGetTexImage(GL_TEXTURE_2D, 0 /* base level */, GL_RGB, GL_UNSIGNED_BYTE, target->GetBuffer());
+          break;
+
+        case Orthanc::PixelFormat_RGBA32:
+          glGetTexImage(GL_TEXTURE_2D, 0 /* base level */, GL_RGBA, GL_UNSIGNED_BYTE, target->GetBuffer());
+          break;
+
+        default:
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
+      }
+
+      return target.release();
+    }
   }
 }
--- a/OrthancStone/Sources/OpenGL/OpenGLTexture.h	Fri Dec 23 17:51:07 2022 +0100
+++ b/OrthancStone/Sources/OpenGL/OpenGLTexture.h	Sat Jan 21 16:48:56 2023 +0100
@@ -24,6 +24,7 @@
 #pragma once
 
 #include "OpenGLIncludes.h"
+#include "IOpenGLContext.h"
 
 #include <Images/ImageAccessor.h>
 
@@ -34,8 +35,6 @@
 {
   namespace OpenGL
   {
-    class IOpenGLContext;
-
     class OpenGLTexture : public boost::noncopyable
     {
     private:
@@ -63,6 +62,8 @@
                 bool isLinearInterpolation);
 
       void Bind(GLint location);
+
+      Orthanc::ImageAccessor* Download(Orthanc::PixelFormat format);
     };
   }
 }