annotate Sources/IncomingInstanceFilter.cpp @ 112:b3a18e333f93

integration 4.0->mainline
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 28 Mar 2023 17:28:16 +0200
parents 87fdb1a108db
children 65ec5597ec70
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
97
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
1 /**
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
2 * Python plugin for Orthanc
101
eb6ac5a801d1 update to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 100
diff changeset
3 * Copyright (C) 2020-2022 Osimis S.A., Belgium
eb6ac5a801d1 update to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 100
diff changeset
4 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
97
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
5 *
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
7 * modify it under the terms of the GNU Affero General Public License
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
8 * as published by the Free Software Foundation, either version 3 of
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
9 * the License, or (at your option) any later version.
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
10 *
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
11 * This program is distributed in the hope that it will be useful, but
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
14 * Affero General Public License for more details.
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
15 *
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
16 * You should have received a copy of the GNU Affero General Public License
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
18 **/
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
19
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
20
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
21 #include "IncomingInstanceFilter.h"
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
22
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
23 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
24 #include "Autogenerated/sdk.h"
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
25 #include "ICallbackRegistration.h"
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
26 #include "PythonString.h"
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
27
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
28
100
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
29 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 10, 0)
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
30
97
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
31 static PyObject* incomingCStoreInstanceFilter_ = NULL;
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
32
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
33
104
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
34 static int32_t IncomingCStoreInstanceFilter(uint16_t* dimseStatus,
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
35 const OrthancPluginDicomInstance *instance)
97
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
36 {
104
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
37 assert(dimseStatus != NULL &&
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
38 instance != NULL);
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
39
97
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
40 try
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
41 {
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
42 PythonLock lock;
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
43
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
44 /**
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
45 * Construct an instance object of the "orthanc.DicomInstance"
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
46 * class. This is done by calling the constructor function
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
47 * "sdk_OrthancPluginDicomInstance_Type".
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
48 **/
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
49 PythonObject args(lock, PyTuple_New(2));
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
50 PyTuple_SetItem(args.GetPyObject(), 0, PyLong_FromSsize_t((intptr_t) instance));
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
51 PyTuple_SetItem(args.GetPyObject(), 1, PyBool_FromLong(true /* borrowed, don't destruct */));
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
52 PyObject *pInst = PyObject_CallObject((PyObject*) GetOrthancPluginDicomInstanceType(), args.GetPyObject());
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
53
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
54 /**
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
55 * Construct the arguments tuple (instance)
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
56 **/
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
57 PythonObject args2(lock, PyTuple_New(1));
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
58 PyTuple_SetItem(args2.GetPyObject(), 0, pInst);
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
59
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
60 PythonObject result(lock, PyObject_CallObject(incomingCStoreInstanceFilter_, args2.GetPyObject()));
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
61
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
62 std::string traceback;
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
63 if (lock.HasErrorOccurred(traceback))
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
64 {
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
65 OrthancPlugins::LogError("Error in the Python incoming-cstore-instance callback, "
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
66 "traceback:\n" + traceback);
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
67 return -1;
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
68 }
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
69 else
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
70 {
104
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
71 if (PyNumber_Check(result.GetPyObject()))
97
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
72 {
104
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
73 int32_t code = PyLong_AsLong(result.GetPyObject());
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
74
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
75 if (code < 0)
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
76 {
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
77 OrthancPlugins::LogError("The Python incoming-cstore-instance filter has returned a negative value");
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
78 return -1;
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
79 }
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
80 else if (code == 0)
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
81 {
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
82 return 1; // The instance is to be stored
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
83 }
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
84 else if (code <= 0xffff)
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
85 {
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
86 // The instance is to be discarded with a custom DIMSE status
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
87 *dimseStatus = static_cast<uint16_t>(code);
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
88 return 0; // Discard
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
89 }
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
90 else
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
91 {
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
92 OrthancPlugins::LogError("The Python incoming-cstore-instance filter has returned an out-of-range DIMSE status: " +
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
93 boost::lexical_cast<std::string>(code));
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
94 return -1;
87fdb1a108db updated signature of IncomingCStoreInstanceFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 102
diff changeset
95 }
97
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
96 }
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
97 else
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
98 {
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
99 OrthancPlugins::LogError("The Python incoming-cstore-instance filter has not returned an integer");
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
100 return -1;
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
101 }
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
102 }
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
103 }
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
104 catch (OrthancPlugins::PluginException& e)
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
105 {
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
106 return e.GetErrorCode();
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
107 }
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
108 }
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
109
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
110
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
111 PyObject* RegisterIncomingCStoreInstanceFilter(PyObject* module, PyObject* args)
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
112 {
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
113 // The GIL is locked at this point (no need to create "PythonLock")
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
114
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
115 class Registration : public ICallbackRegistration
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
116 {
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
117 public:
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
118 virtual void Register() ORTHANC_OVERRIDE
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
119 {
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
120 OrthancPluginRegisterIncomingCStoreInstanceFilter(
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
121 OrthancPlugins::GetGlobalContext(), IncomingCStoreInstanceFilter);
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
122 }
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
123 };
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
124
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
125 Registration registration;
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
126 return ICallbackRegistration::Apply(
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
127 registration, args, incomingCStoreInstanceFilter_, "Python incoming CStore instance filter");
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
128 }
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
129
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
130
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
131 void FinalizeIncomingCStoreInstanceFilter()
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
132 {
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
133 ICallbackRegistration::Unregister(incomingCStoreInstanceFilter_);
e37ed36541be added missing files
Alain Mazy <am@osimis.io>
parents:
diff changeset
134 }
100
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
135
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
136 #else
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
137
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
138 #warning OrthancPluginRegisterIncomingCStoreInstanceFilter() is not supported
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
139
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
140 PyObject* RegisterIncomingCStoreInstanceFilter(PyObject* module, PyObject* args)
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
141 {
102
ed994991a20b improved error handling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 101
diff changeset
142 PyErr_SetString(PyExc_RuntimeError, "The version of your Orthanc SDK doesn't provide OrthancPluginRegisterIncomingCStoreInstanceFilter()");
ed994991a20b improved error handling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 101
diff changeset
143 return NULL;
100
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
144 }
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
145
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
146 void FinalizeIncomingCStoreInstanceFilter()
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
147 {
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
148 }
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
149
e2b2e1d4e1bb fixed compatibility with Orthanc SDK 1.8.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 97
diff changeset
150 #endif