changeset 4849:4addabcab158 received-instance-callback

use ParsedDicomFile in Sanitizer sample plugin
author Alain Mazy <am@osimis.io>
date Mon, 13 Dec 2021 16:07:52 +0100
parents 02d77189d8ba
children 5ec96ada5a53
files OrthancServer/Plugins/Samples/Sanitizer/CMakeLists.txt OrthancServer/Plugins/Samples/Sanitizer/Plugin.cpp
diffstat 2 files changed, 28 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Plugins/Samples/Sanitizer/CMakeLists.txt	Thu Dec 09 17:22:40 2021 +0100
+++ b/OrthancServer/Plugins/Samples/Sanitizer/CMakeLists.txt	Mon Dec 13 16:07:52 2021 +0100
@@ -27,16 +27,28 @@
 
 SET(USE_SYSTEM_JSONCPP ON CACHE BOOL "Use the system version of JsonCpp")
 SET(USE_SYSTEM_BOOST ON CACHE BOOL "Use the system version of boost")
+SET(ORTHANC_FRAMEWORK_SOURCE path)
+SET(ORTHANC_FRAMEWORK_ROOT ${CMAKE_SOURCE_DIR}/../../../../OrthancFramework/Sources)
 
 include(${CMAKE_SOURCE_DIR}/../Common/OrthancPlugins.cmake)
 include(${CMAKE_SOURCE_DIR}/../../../../OrthancFramework/Resources/CMake/JsonCppConfiguration.cmake)
 include(${CMAKE_SOURCE_DIR}/../../../../OrthancFramework/Resources/CMake/BoostConfiguration.cmake)
 
+include(${CMAKE_SOURCE_DIR}/../../../../OrthancFramework/Resources/CMake/DownloadOrthancFramework.cmake)
+include(${ORTHANC_FRAMEWORK_ROOT}/../Resources/CMake/OrthancFrameworkParameters.cmake)
+set(ENABLE_LOCALE ON)
+set(ENABLE_DCMTK ON)
+include(${ORTHANC_FRAMEWORK_ROOT}/../Resources/CMake/OrthancFrameworkConfiguration.cmake)
+include_directories(${ORTHANC_FRAMEWORK_ROOT})
+
+
 add_library(Sanitizer SHARED 
     ${CMAKE_SOURCE_DIR}/../Common/OrthancPluginCppWrapper.cpp
     ${JSONCPP_SOURCES}
     ${BOOST_SOURCES}
     Plugin.cpp
+    ${ORTHANC_CORE_SOURCES}
+    ${ORTHANC_DICOM_SOURCES}
     )
 
 
--- a/OrthancServer/Plugins/Samples/Sanitizer/Plugin.cpp	Thu Dec 09 17:22:40 2021 +0100
+++ b/OrthancServer/Plugins/Samples/Sanitizer/Plugin.cpp	Mon Dec 13 16:07:52 2021 +0100
@@ -21,6 +21,8 @@
 
 
 #include "../../../../OrthancFramework/Sources/Compatibility.h"
+#include "../../../../OrthancFramework/Sources/DicomParsing/ParsedDicomFile.h"
+#include "../../../../OrthancFramework/Sources/OrthancFramework.h"
 #include "../Common/OrthancPluginCppWrapper.h"
 
 #include <boost/filesystem.hpp>
@@ -35,50 +37,20 @@
                                                                      uint64_t receivedDicomBufferSize,
                                                                      void** modifiedDicomBuffer,
                                                                      uint64_t* modifiedDicomBufferSize)
-                                                                    //  OrthancPluginMemoryBuffer* modifiedDicomBuffer)
 {
-    // note: this sample plugin won't work with multi-frame images or badly formed images 
-    // OrthancPluginCreateDicom and OrthancPluginDicomBufferToJson do not support multi-frame and are quite touchy with invalid tag values
-
-    Json::Value receivedDicomAsJson;
-    OrthancPlugins::OrthancString str;
-    str.Assign(OrthancPluginDicomBufferToJson
-               (OrthancPlugins::GetGlobalContext(), 
-                receivedDicomBuffer, 
-                receivedDicomBufferSize, 
-                OrthancPluginDicomToJsonFormat_Short, 
-                static_cast<OrthancPluginDicomToJsonFlags>(OrthancPluginDicomToJsonFlags_IncludeBinary | OrthancPluginDicomToJsonFlags_IncludePrivateTags | OrthancPluginDicomToJsonFlags_IncludeUnknownTags | OrthancPluginDicomToJsonFlags_SkipGroupLengths | OrthancPluginDicomToJsonFlags_IncludePixelData),
-                0));
-    
-    str.ToJson(receivedDicomAsJson);
-
-    if (receivedDicomAsJson["0008,0080"] != "My Institution")
-    {
-        receivedDicomAsJson["0008,0080"] = "My Institution";
+  Orthanc::ParsedDicomFile dicom(receivedDicomBuffer, receivedDicomBufferSize);
+  std::string institutionName = "My institution";
 
-        OrthancPluginMemoryBuffer modifiedDicom;
-        std::string serializedModifiedDicomAsJson;
-        OrthancPlugins::WriteFastJson(serializedModifiedDicomAsJson, receivedDicomAsJson);
-        OrthancPluginErrorCode createResult = OrthancPluginCreateDicom(OrthancPlugins::GetGlobalContext(), 
-                                                                       &modifiedDicom, 
-                                                                       serializedModifiedDicomAsJson.c_str(), 
-                                                                       NULL, 
-                                                                       OrthancPluginCreateDicomFlags_DecodeDataUriScheme);
+  dicom.Replace(Orthanc::DICOM_TAG_INSTITUTION_NAME, institutionName, false, Orthanc::DicomReplaceMode_InsertIfAbsent, "");
+  
+  std::string modifiedDicom;
+  dicom.SaveToMemoryBuffer(modifiedDicom);
 
-        if (createResult == OrthancPluginErrorCode_Success)
-        {    
-            *modifiedDicomBuffer = modifiedDicom.data;
-            *modifiedDicomBufferSize = modifiedDicom.size;
-        
-            return OrthancPluginReceivedInstanceCallbackResult_Modified;
-        }
-        else
-        {
-            return OrthancPluginReceivedInstanceCallbackResult_KeepAsIs;
-        }
-    }
-
-    return OrthancPluginReceivedInstanceCallbackResult_KeepAsIs;
+  *modifiedDicomBuffer = malloc(modifiedDicom.size());
+  *modifiedDicomBufferSize = modifiedDicom.size();
+  memcpy(*modifiedDicomBuffer, modifiedDicom.c_str(), modifiedDicom.size());
+  
+  return OrthancPluginReceivedInstanceCallbackResult_Modified;
 }
 
 
@@ -88,6 +60,8 @@
   {
     OrthancPlugins::SetGlobalContext(c);
 
+    Orthanc::InitializeFramework("", true);
+
     /* Check the version of the Orthanc core */
     // if (OrthancPluginCheckVersion(c) == 0)
     // {
@@ -109,6 +83,7 @@
   ORTHANC_PLUGINS_API void OrthancPluginFinalize()
   {
     OrthancPlugins::LogWarning("Sanitizer plugin is finalizing");
+    Orthanc::FinalizeFramework();
   }