comparison 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
comparison
equal deleted inserted replaced
4845:02d77189d8ba 4849:4addabcab158
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 **/ 20 **/
21 21
22 22
23 #include "../../../../OrthancFramework/Sources/Compatibility.h" 23 #include "../../../../OrthancFramework/Sources/Compatibility.h"
24 #include "../../../../OrthancFramework/Sources/DicomParsing/ParsedDicomFile.h"
25 #include "../../../../OrthancFramework/Sources/OrthancFramework.h"
24 #include "../Common/OrthancPluginCppWrapper.h" 26 #include "../Common/OrthancPluginCppWrapper.h"
25 27
26 #include <boost/filesystem.hpp> 28 #include <boost/filesystem.hpp>
27 #include <json/value.h> 29 #include <json/value.h>
28 #include <string.h> 30 #include <string.h>
33 35
34 OrthancPluginReceivedInstanceCallbackResult ReceivedInstanceCallback(const void* receivedDicomBuffer, 36 OrthancPluginReceivedInstanceCallbackResult ReceivedInstanceCallback(const void* receivedDicomBuffer,
35 uint64_t receivedDicomBufferSize, 37 uint64_t receivedDicomBufferSize,
36 void** modifiedDicomBuffer, 38 void** modifiedDicomBuffer,
37 uint64_t* modifiedDicomBufferSize) 39 uint64_t* modifiedDicomBufferSize)
38 // OrthancPluginMemoryBuffer* modifiedDicomBuffer)
39 { 40 {
40 // note: this sample plugin won't work with multi-frame images or badly formed images 41 Orthanc::ParsedDicomFile dicom(receivedDicomBuffer, receivedDicomBufferSize);
41 // OrthancPluginCreateDicom and OrthancPluginDicomBufferToJson do not support multi-frame and are quite touchy with invalid tag values 42 std::string institutionName = "My institution";
42 43
43 Json::Value receivedDicomAsJson; 44 dicom.Replace(Orthanc::DICOM_TAG_INSTITUTION_NAME, institutionName, false, Orthanc::DicomReplaceMode_InsertIfAbsent, "");
44 OrthancPlugins::OrthancString str; 45
45 str.Assign(OrthancPluginDicomBufferToJson 46 std::string modifiedDicom;
46 (OrthancPlugins::GetGlobalContext(), 47 dicom.SaveToMemoryBuffer(modifiedDicom);
47 receivedDicomBuffer,
48 receivedDicomBufferSize,
49 OrthancPluginDicomToJsonFormat_Short,
50 static_cast<OrthancPluginDicomToJsonFlags>(OrthancPluginDicomToJsonFlags_IncludeBinary | OrthancPluginDicomToJsonFlags_IncludePrivateTags | OrthancPluginDicomToJsonFlags_IncludeUnknownTags | OrthancPluginDicomToJsonFlags_SkipGroupLengths | OrthancPluginDicomToJsonFlags_IncludePixelData),
51 0));
52
53 str.ToJson(receivedDicomAsJson);
54 48
55 if (receivedDicomAsJson["0008,0080"] != "My Institution") 49 *modifiedDicomBuffer = malloc(modifiedDicom.size());
56 { 50 *modifiedDicomBufferSize = modifiedDicom.size();
57 receivedDicomAsJson["0008,0080"] = "My Institution"; 51 memcpy(*modifiedDicomBuffer, modifiedDicom.c_str(), modifiedDicom.size());
58 52
59 OrthancPluginMemoryBuffer modifiedDicom; 53 return OrthancPluginReceivedInstanceCallbackResult_Modified;
60 std::string serializedModifiedDicomAsJson;
61 OrthancPlugins::WriteFastJson(serializedModifiedDicomAsJson, receivedDicomAsJson);
62 OrthancPluginErrorCode createResult = OrthancPluginCreateDicom(OrthancPlugins::GetGlobalContext(),
63 &modifiedDicom,
64 serializedModifiedDicomAsJson.c_str(),
65 NULL,
66 OrthancPluginCreateDicomFlags_DecodeDataUriScheme);
67
68 if (createResult == OrthancPluginErrorCode_Success)
69 {
70 *modifiedDicomBuffer = modifiedDicom.data;
71 *modifiedDicomBufferSize = modifiedDicom.size;
72
73 return OrthancPluginReceivedInstanceCallbackResult_Modified;
74 }
75 else
76 {
77 return OrthancPluginReceivedInstanceCallbackResult_KeepAsIs;
78 }
79 }
80
81 return OrthancPluginReceivedInstanceCallbackResult_KeepAsIs;
82 } 54 }
83 55
84 56
85 extern "C" 57 extern "C"
86 { 58 {
87 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c) 59 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
88 { 60 {
89 OrthancPlugins::SetGlobalContext(c); 61 OrthancPlugins::SetGlobalContext(c);
62
63 Orthanc::InitializeFramework("", true);
90 64
91 /* Check the version of the Orthanc core */ 65 /* Check the version of the Orthanc core */
92 // if (OrthancPluginCheckVersion(c) == 0) 66 // if (OrthancPluginCheckVersion(c) == 0)
93 // { 67 // {
94 // OrthancPlugins::ReportMinimalOrthancVersion(ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, 68 // OrthancPlugins::ReportMinimalOrthancVersion(ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
107 81
108 82
109 ORTHANC_PLUGINS_API void OrthancPluginFinalize() 83 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
110 { 84 {
111 OrthancPlugins::LogWarning("Sanitizer plugin is finalizing"); 85 OrthancPlugins::LogWarning("Sanitizer plugin is finalizing");
86 Orthanc::FinalizeFramework();
112 } 87 }
113 88
114 89
115 ORTHANC_PLUGINS_API const char* OrthancPluginGetName() 90 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
116 { 91 {