changeset 214:1c5a47dda299

SdlOrthancSurface
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 01 Jun 2018 18:33:34 +0200
parents f753a7e15a73
children 07792a582de9
files Applications/IBasicApplication.cpp Applications/Sdl/SdlEngine.cpp Applications/Sdl/SdlEngine.h Applications/Sdl/SdlOrthancSurface.cpp Applications/Sdl/SdlOrthancSurface.h Applications/Sdl/SdlWindow.cpp Applications/Sdl/SdlWindow.h Resources/CMake/OrthancStoneConfiguration.cmake
diffstat 8 files changed, 185 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/Applications/IBasicApplication.cpp	Fri Jun 01 18:09:09 2018 +0200
+++ b/Applications/IBasicApplication.cpp	Fri Jun 01 18:33:34 2018 +0200
@@ -87,7 +87,7 @@
     Orthanc::Logging::Initialize();
     Orthanc::HttpClient::InitializeOpenSsl();
     Orthanc::HttpClient::GlobalInitialize();
-    SdlEngine::GlobalInitialize();
+    SdlWindow::GlobalInitialize();
 
 
     /******************************************************************
@@ -280,7 +280,7 @@
      * Finalize all the subcomponents of Orthanc Stone
      ******************************************************************/
 
-    SdlEngine::GlobalFinalize();
+    SdlWindow::GlobalFinalize();
     Orthanc::HttpClient::GlobalFinalize();
     Orthanc::HttpClient::FinalizeOpenSsl();
 
--- a/Applications/Sdl/SdlEngine.cpp	Fri Jun 01 18:09:09 2018 +0200
+++ b/Applications/Sdl/SdlEngine.cpp	Fri Jun 01 18:33:34 2018 +0200
@@ -269,18 +269,6 @@
       SDL_Delay(1);
     }
   }
-
-
-  void SdlEngine::GlobalInitialize()
-  {
-    SDL_Init(SDL_INIT_VIDEO);
-  }
-
-
-  void SdlEngine::GlobalFinalize()
-  {
-    SDL_Quit();
-  }
 }
 
 #endif
--- a/Applications/Sdl/SdlEngine.h	Fri Jun 01 18:09:09 2018 +0200
+++ b/Applications/Sdl/SdlEngine.h	Fri Jun 01 18:33:34 2018 +0200
@@ -57,10 +57,6 @@
     }
 
     void Run();
-
-    static void GlobalInitialize();
-
-    static void GlobalFinalize();
   };
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Applications/Sdl/SdlOrthancSurface.cpp	Fri Jun 01 18:33:34 2018 +0200
@@ -0,0 +1,106 @@
+/**
+ * Stone of Orthanc
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2018 Osimis S.A., Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "SdlOrthancSurface.h"
+
+#if ORTHANC_ENABLE_SDL == 1
+
+#include <Core/Logging.h>
+#include <Core/OrthancException.h>
+#include <Core/Images/Image.h>
+
+namespace OrthancStone
+{
+  SdlOrthancSurface::SdlOrthancSurface(SdlWindow& window) :
+    window_(window),
+    sdlSurface_(NULL)
+  {
+  }
+
+
+  SdlOrthancSurface::~SdlOrthancSurface()
+  {
+    if (sdlSurface_)
+    {
+      SDL_FreeSurface(sdlSurface_);
+    }
+  }
+
+
+  void SdlOrthancSurface::SetSize(unsigned int width,
+                                  unsigned int height)
+  {
+    if (image_.get() == NULL ||
+        image_->GetWidth() != width ||
+        image_->GetHeight() != height)
+    {
+      image_.reset(new Orthanc::Image(Orthanc::PixelFormat_BGRA32, width, height, true));  // (*)
+
+      if (image_->GetPitch() != image_->GetWidth() * 4)
+      {
+        // This should have been ensured by setting "forceMinimalPitch" to "true" (*)
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+      }
+
+      // TODO Big endian?
+      static const uint32_t rmask = 0x00ff0000;
+      static const uint32_t gmask = 0x0000ff00;
+      static const uint32_t bmask = 0x000000ff;
+
+      if (sdlSurface_)
+      {
+        SDL_FreeSurface(sdlSurface_);
+      }
+
+      sdlSurface_ = SDL_CreateRGBSurfaceFrom(image_->GetBuffer(), width, height, 32,
+                                             image_->GetPitch(), rmask, gmask, bmask, 0);
+      if (!sdlSurface_)
+      {
+        LOG(ERROR) << "Cannot create a SDL surface from a Orthanc surface";
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+      }
+    }
+  }
+
+
+  Orthanc::ImageAccessor& SdlOrthancSurface::GetImage()
+  {
+    if (image_.get() == NULL)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+    }
+
+    return *image_;
+  }
+
+
+  void SdlOrthancSurface::Render()
+  {
+    if (image_.get() == NULL)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+    }
+
+    window_.Render(sdlSurface_);
+  }
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Applications/Sdl/SdlOrthancSurface.h	Fri Jun 01 18:33:34 2018 +0200
@@ -0,0 +1,54 @@
+/**
+ * Stone of Orthanc
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2018 Osimis S.A., Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#if ORTHANC_ENABLE_SDL == 1
+
+#include "SdlWindow.h"
+
+#include <Core/Images/ImageAccessor.h>
+#include <boost/thread/mutex.hpp>
+
+namespace OrthancStone
+{
+  class SdlOrthancSurface : public boost::noncopyable
+  {
+  private:
+    std::auto_ptr<Orthanc::ImageAccessor>  image_;
+    SdlWindow&                             window_;
+    SDL_Surface*                           sdlSurface_;
+
+  public:
+    SdlOrthancSurface(SdlWindow& window);
+
+    ~SdlOrthancSurface();
+
+    void SetSize(unsigned int width,
+                 unsigned int height);
+
+    Orthanc::ImageAccessor& GetImage();
+
+    void Render();
+  };
+}
+
+#endif
--- a/Applications/Sdl/SdlWindow.cpp	Fri Jun 01 18:09:09 2018 +0200
+++ b/Applications/Sdl/SdlWindow.cpp	Fri Jun 01 18:33:34 2018 +0200
@@ -26,6 +26,8 @@
 #include <Core/Logging.h>
 #include <Core/OrthancException.h>
 
+#include <SDL.h>
+
 namespace OrthancStone
 {
   SdlWindow::SdlWindow(const char* title,
@@ -145,6 +147,22 @@
       maximized_ = true;
     }
   }
+
+
+  void SdlWindow::GlobalInitialize()
+  {
+    if (SDL_Init(SDL_INIT_VIDEO) != 0)
+    {
+      LOG(ERROR) << "Cannot initialize SDL";
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+    }
+  }
+
+
+  void SdlWindow::GlobalFinalize()
+  {
+    SDL_Quit();
+  }
 }
 
 #endif
--- a/Applications/Sdl/SdlWindow.h	Fri Jun 01 18:09:09 2018 +0200
+++ b/Applications/Sdl/SdlWindow.h	Fri Jun 01 18:33:34 2018 +0200
@@ -51,6 +51,10 @@
     void Render(SDL_Surface* surface);
 
     void ToggleMaximize();
+
+    static void GlobalInitialize();
+
+    static void GlobalFinalize();
   };
 }
 
--- a/Resources/CMake/OrthancStoneConfiguration.cmake	Fri Jun 01 18:09:09 2018 +0200
+++ b/Resources/CMake/OrthancStoneConfiguration.cmake	Fri Jun 01 18:33:34 2018 +0200
@@ -147,6 +147,7 @@
     ${ORTHANC_STONE_DIR}/Applications/IBasicApplication.cpp
     ${ORTHANC_STONE_DIR}/Applications/Sdl/SdlEngine.cpp
     ${ORTHANC_STONE_DIR}/Applications/Sdl/SdlCairoSurface.cpp
+    ${ORTHANC_STONE_DIR}/Applications/Sdl/SdlOrthancSurface.cpp
     ${ORTHANC_STONE_DIR}/Applications/Sdl/SdlWindow.cpp
     )
 endif()