changeset 2065:15f2e52835a1 deep-learning

removed OpenGLTextureArray::DownloadedArray()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 04 May 2023 16:59:28 +0200
parents 4e31d76c7ecd
children cf3d85eb291c
files OrthancStone/Sources/OpenGL/OpenGLTextureArray.cpp OrthancStone/Sources/OpenGL/OpenGLTextureArray.h
diffstat 2 files changed, 44 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancStone/Sources/OpenGL/OpenGLTextureArray.cpp	Thu May 04 11:06:19 2023 +0200
+++ b/OrthancStone/Sources/OpenGL/OpenGLTextureArray.cpp	Thu May 04 16:59:28 2023 +0200
@@ -223,55 +223,70 @@
     }
 
 
-    OpenGLTextureArray::DownloadedArray::DownloadedArray(const OpenGLTextureArray& texture) :
-      format_(texture.format_),
-      width_(texture.width_),
-      height_(texture.height_),
-      depth_(texture.depth_)
+    size_t OpenGLTextureArray::GetMemoryBufferSize() const
+    {
+      return static_cast<size_t>(Orthanc::GetBytesPerPixel(format_)) * width_ * height_ * depth_;
+    }
+
+
+    void OpenGLTextureArray::Download(void* targetBuffer,
+                                      size_t targetSize) const
     {
-      if (width_ != 0 &&
-          height_ != 0 &&
-          depth_ != 0)
+      if (targetSize != GetMemoryBufferSize())
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+      }
+      else if (targetSize == 0)
       {
-        buffer_.resize(Orthanc::GetBytesPerPixel(format_) * width_ * height_ * depth_);
-        assert(buffer_.size() > 0);
-
+        return;
+      }
+      else if (targetBuffer == NULL)
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
+      }
+      else
+      {
 #if 1 || defined(__EMSCRIPTEN__)
         /**
          * The "glGetTexImage()" function is unavailable in WebGL, it
          * is necessary to use a framebuffer:
          * https://stackoverflow.com/a/15064957
          **/
-        OpenGLFramebuffer framebuffer(texture.context_);
+        OpenGLFramebuffer framebuffer(context_);
+
+        const size_t sliceSize = targetSize / depth_;
 
-        Orthanc::Image tmp(texture.GetFormat(), texture.GetWidth(), texture.GetHeight(), true);
+        Orthanc::Image tmp(GetFormat(), GetWidth(), GetHeight(), true);
+        if (sliceSize != tmp.GetPitch() * height_)
+        {
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+        }
 
         for (unsigned int layer = 0; layer < depth_; layer++)
         {
-          framebuffer.ReadTexture(tmp, texture, layer);
-          memcpy(&buffer_[0] + layer * tmp.GetPitch() * height_,
-                 tmp.GetBuffer(), tmp.GetPitch() * height_);
+          framebuffer.ReadTexture(tmp, *this, layer);
+          memcpy(reinterpret_cast<uint8_t*>(targetBuffer) + layer * sliceSize, tmp.GetBuffer(), sliceSize);
         }
 
 #else
-        glBindTexture(GL_TEXTURE_2D_ARRAY, texture.texture_);
+        glBindTexture(GL_TEXTURE_3D, texture_);
 
         switch (format_)
         {
           case Orthanc::PixelFormat_Grayscale8:
-            glGetTexImage(GL_TEXTURE_2D_ARRAY, 0 /* base level */, GL_RED, GL_UNSIGNED_BYTE, &buffer_[0]);
+            glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RED, GL_UNSIGNED_BYTE, targetBuffer);
             break;
 
           case Orthanc::PixelFormat_RGB24:
-            glGetTexImage(GL_TEXTURE_2D_ARRAY, 0 /* base level */, GL_RGB, GL_UNSIGNED_BYTE, &buffer_[0]);
+            glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RGB, GL_UNSIGNED_BYTE, targetBuffer);
             break;
 
           case Orthanc::PixelFormat_RGBA32:
-            glGetTexImage(GL_TEXTURE_2D_ARRAY, 0 /* base level */, GL_RGBA, GL_UNSIGNED_BYTE, &buffer_[0]);
+            glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RGBA, GL_UNSIGNED_BYTE, targetBuffer);
             break;
 
           case Orthanc::PixelFormat_Float32:
-            glGetTexImage(GL_TEXTURE_2D_ARRAY, 0 /* base level */, GL_RED, GL_FLOAT, &buffer_[0]);
+            glGetTexImage(GL_TEXTURE_3D, 0 /* base level */, GL_RED, GL_FLOAT, targetBuffer);
             break;
 
           default:
@@ -282,27 +297,10 @@
     }
 
 
-    Orthanc::ImageAccessor* OpenGLTextureArray::DownloadedArray::GetLayer(unsigned int layer) const
+    void OpenGLTextureArray::Download(std::string& target) const
     {
-      if (layer >= depth_)
-      {
-        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
-      }
-      else if (width_ == 0 ||
-               height_ == 0 ||
-               depth_ == 0)
-      {
-        return new Orthanc::Image(format_, 0, 0, true);
-      }
-      else
-      {
-        const size_t rowSize = width_ * Orthanc::GetBytesPerPixel(format_);
-
-        std::unique_ptr<Orthanc::ImageAccessor> target(new Orthanc::ImageAccessor);
-        target->AssignReadOnly(format_, width_, height_, rowSize,
-                               reinterpret_cast<const uint8_t*>(buffer_.c_str()) + layer * height_ * rowSize);
-        return target.release();
-      }
+      target.resize(GetMemoryBufferSize());
+      Download(target);
     }
   }
 }
--- a/OrthancStone/Sources/OpenGL/OpenGLTextureArray.h	Thu May 04 11:06:19 2023 +0200
+++ b/OrthancStone/Sources/OpenGL/OpenGLTextureArray.h	Thu May 04 16:59:28 2023 +0200
@@ -105,40 +105,13 @@
       void Upload(const Orthanc::ImageAccessor& image,
                   unsigned int layer);
 
-      class DownloadedArray : public boost::noncopyable
-      {
-      private:
-        std::string           buffer_;
-        Orthanc::PixelFormat  format_;
-        unsigned int          width_;
-        unsigned int          height_;
-        unsigned int          depth_;
-
-      public:
-        DownloadedArray(const OpenGLTextureArray& texture);
-
-        Orthanc::PixelFormat GetFormat() const
-        {
-          return format_;
-        }
+      size_t GetMemoryBufferSize() const;
 
-        unsigned int GetWidth() const
-        {
-          return width_;
-        }
+      // "targetSize" must be equal to "GetMemoryBufferSize()"
+      void Download(void* targetBuffer,
+                    size_t targetSize) const;
 
-        unsigned int GetHeight() const
-        {
-          return height_;
-        }
-
-        unsigned int GetDepth() const
-        {
-          return depth_;
-        }
-
-        Orthanc::ImageAccessor* GetLayer(unsigned int layer) const;
-      };
+      void Download(std::string& target) const;
     };
   }
 }