changeset 1816:87c069c94ac9

plugin sample: automated jpeg2k compression
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 24 Nov 2015 13:36:08 +0100
parents 2abfdca9b915
children 9a6de24209cf 2d8191b13567
files Plugins/Engine/PluginsEnumerations.cpp Plugins/Include/orthanc/OrthancCPlugin.h Plugins/Samples/AutomatedJpeg2kCompression/CMakeLists.txt Plugins/Samples/AutomatedJpeg2kCompression/Plugin.cpp
diffstat 4 files changed, 172 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Plugins/Engine/PluginsEnumerations.cpp	Tue Nov 24 11:38:14 2015 +0100
+++ b/Plugins/Engine/PluginsEnumerations.cpp	Tue Nov 24 13:36:08 2015 +0100
@@ -293,7 +293,7 @@
           return OrthancPluginInstanceOrigin_Lua;
 
         case RequestOrigin_Plugins:
-          return OrthancPluginInstanceOrigin_Plugins;
+          return OrthancPluginInstanceOrigin_Plugin;
 
         case RequestOrigin_Unknown:
           return OrthancPluginInstanceOrigin_Unknown;
--- a/Plugins/Include/orthanc/OrthancCPlugin.h	Tue Nov 24 11:38:14 2015 +0100
+++ b/Plugins/Include/orthanc/OrthancCPlugin.h	Tue Nov 24 13:36:08 2015 +0100
@@ -719,7 +719,7 @@
     OrthancPluginInstanceOrigin_Unknown = 1,        /*!< Unknown origin */
     OrthancPluginInstanceOrigin_DicomProtocol = 2,  /*!< Instance received through DICOM protocol */
     OrthancPluginInstanceOrigin_RestApi = 3,        /*!< Instance received through REST API of Orthanc */
-    OrthancPluginInstanceOrigin_Plugins = 4,        /*!< Instance added to Orthanc by a plugin */
+    OrthancPluginInstanceOrigin_Plugin = 4,         /*!< Instance added to Orthanc by a plugin */
     OrthancPluginInstanceOrigin_Lua = 5,            /*!< Instance added to Orthanc by a Lua script */
 
     _OrthancPluginInstanceOrigin_INTERNAL = 0x7fffffff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/Samples/AutomatedJpeg2kCompression/CMakeLists.txt	Tue Nov 24 13:36:08 2015 +0100
@@ -0,0 +1,8 @@
+cmake_minimum_required(VERSION 2.8)
+
+project(Basic)
+
+set(SAMPLES_ROOT ${CMAKE_SOURCE_DIR}/..)
+include(${SAMPLES_ROOT}/Common/OrthancPlugins.cmake)
+
+add_library(AutomatedJpeg2kCompression SHARED Plugin.cpp)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/Samples/AutomatedJpeg2kCompression/Plugin.cpp	Tue Nov 24 13:36:08 2015 +0100
@@ -0,0 +1,162 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include <orthanc/OrthancCPlugin.h>
+
+#include <string>
+
+static OrthancPluginContext* context_ = NULL;
+
+
+static bool ReadFile(std::string& result,
+                     const std::string& path)
+{
+  OrthancPluginMemoryBuffer tmp;
+  if (OrthancPluginReadFile(context_, &tmp, path.c_str()) == OrthancPluginErrorCode_Success)
+  {
+    result.assign(reinterpret_cast<const char*>(tmp.data), tmp.size);
+    OrthancPluginFreeMemoryBuffer(context_, &tmp);
+    return true;
+  }
+  else
+  {
+    return false;
+  }
+}
+
+
+OrthancPluginErrorCode OnStoredCallback(OrthancPluginDicomInstance* instance,
+                                        const char* instanceId)
+{
+  char buffer[1024];
+  sprintf(buffer, "Just received a DICOM instance of size %d and ID %s from origin %d (AET %s)", 
+          (int) OrthancPluginGetInstanceSize(context_, instance), instanceId, 
+          OrthancPluginGetInstanceOrigin(context_, instance),
+          OrthancPluginGetInstanceRemoteAet(context_, instance));
+  OrthancPluginLogInfo(context_, buffer);
+
+  if (OrthancPluginGetInstanceOrigin(context_, instance) == OrthancPluginInstanceOrigin_Plugin)
+  {
+    // Do not compress twice the same file
+    return OrthancPluginErrorCode_Success;
+  }
+
+  // Write the uncompressed DICOM content to some temporary file
+  std::string uncompressed = "uncompressed-" + std::string(instanceId) + ".dcm";
+  OrthancPluginErrorCode error = OrthancPluginWriteFile(context_, uncompressed.c_str(), 
+                                                        OrthancPluginGetInstanceData(context_, instance),
+                                                        OrthancPluginGetInstanceSize(context_, instance));
+  if (error)
+  {
+    return error;
+  }
+
+  // Remove the original DICOM instance
+  std::string uri = "/instances/" + std::string(instanceId);
+  error = OrthancPluginRestApiDelete(context_, uri.c_str());
+  if (error)
+  {
+    return error;
+  }
+
+  // Path to the temporary file that will contain the compressed DICOM content
+  std::string compressed = "compressed-" + std::string(instanceId) + ".dcm";
+
+  // Compress to JPEG2000 using gdcm
+  std::string command1 = "gdcmconv --j2k " + uncompressed + " " + compressed;
+
+  // Generate a new SOPInstanceUID for the JPEG2000 file, as gdcmconv
+  // does not do this by itself
+  std::string command2 = "dcmodify --no-backup -gin " + compressed;
+
+  // Make the required system calls
+  system(command1.c_str());
+  system(command2.c_str());
+
+  // Read the result of the JPEG2000 compression
+  std::string j2k;
+  bool ok = ReadFile(j2k, compressed);
+
+  // Remove the two temporary files
+  remove(compressed.c_str());
+  remove(uncompressed.c_str());
+
+  if (!ok)
+  {
+    return OrthancPluginErrorCode_Plugin;
+  }
+
+  // Upload the JPEG2000 file through the REST API
+  OrthancPluginMemoryBuffer tmp;
+  if (OrthancPluginRestApiPost(context_, &tmp, "/instances", j2k.c_str(), j2k.size()))
+  {
+    ok = false;
+  }
+
+  if (ok)
+  {
+    OrthancPluginFreeMemoryBuffer(context_, &tmp);
+  }
+
+  return ok ? OrthancPluginErrorCode_Success : OrthancPluginErrorCode_Plugin;
+}
+
+
+extern "C"
+{
+  ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
+  {
+    context_ = c;
+
+    /* Check the version of the Orthanc core */
+    if (OrthancPluginCheckVersion(c) == 0)
+    {
+      char info[1024];
+      sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
+              context_->orthancVersion,
+              ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
+              ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
+              ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
+      OrthancPluginLogError(context_, info);
+      return -1;
+    }
+
+    OrthancPluginRegisterOnStoredInstanceCallback(context_, OnStoredCallback);
+
+    return 0;
+  }
+
+
+  ORTHANC_PLUGINS_API void OrthancPluginFinalize()
+  {
+  }
+
+
+  ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
+  {
+    return "sample-jpeg2k";
+  }
+
+
+  ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
+  {
+    return "0.0";
+  }
+}