Mercurial > hg > orthanc
diff OrthancServer/Plugins/Samples/Sanitizer/Plugin.cpp @ 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 | 43e613a7756b |
line wrap: on
line diff
--- 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(); }