Mercurial > hg > orthanc-python
annotate Sources/Plugin.cpp @ 220:7ecdfdcb49d5
NEWS
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 04 Jul 2024 08:32:24 +0200 |
parents | 3678a028f1f6 |
children |
rev | line source |
---|---|
219
3678a028f1f6
making the project REUSE-compliant
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
217
diff
changeset
|
1 /** |
3678a028f1f6
making the project REUSE-compliant
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
217
diff
changeset
|
2 * SPDX-FileCopyrightText: 2020-2023 Osimis S.A., 2024-2024 Orthanc Team SRL, 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain |
3678a028f1f6
making the project REUSE-compliant
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
217
diff
changeset
|
3 * SPDX-License-Identifier: AGPL-3.0-or-later |
3678a028f1f6
making the project REUSE-compliant
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
217
diff
changeset
|
4 */ |
3678a028f1f6
making the project REUSE-compliant
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
217
diff
changeset
|
5 |
0 | 6 /** |
7 * Python plugin for Orthanc | |
166
6fada29b6759
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
157
diff
changeset
|
8 * Copyright (C) 2020-2023 Osimis S.A., Belgium |
6fada29b6759
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
157
diff
changeset
|
9 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
155
71d305c29cfa
updated year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
154
diff
changeset
|
10 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 11 * |
12 * This program is free software: you can redistribute it and/or | |
13 * modify it under the terms of the GNU Affero General Public License | |
14 * as published by the Free Software Foundation, either version 3 of | |
15 * the License, or (at your option) any later version. | |
16 * | |
17 * This program is distributed in the hope that it will be useful, but | |
18 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 * Affero General Public License for more details. | |
21 * | |
22 * You should have received a copy of the GNU Affero General Public License | |
23 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
24 **/ | |
25 | |
26 | |
27 // http://edcjones.tripod.com/refcount.html | |
28 // https://docs.python.org/3/extending/extending.html | |
29 | |
30 // https://www.codevate.com/blog/7-concurrency-with-embedded-python-in-a-multi-threaded-c-application | |
31 // https://fr.slideshare.net/YiLungTsai/embed-python | |
32 | |
33 | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
34 #include "DicomScpCallbacks.h" |
50
70abe3ebbbfc
New Python function: "orthanc.RegisterIncomingHttpRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
35 #include "IncomingHttpRequestFilter.h" |
70abe3ebbbfc
New Python function: "orthanc.RegisterIncomingHttpRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
36 #include "OnChangeCallback.h" |
0 | 37 #include "OnStoredInstanceCallback.h" |
96
627b8a19fb9f
orthanc.RegisterIncomingCStoreInstanceFilter()
Alain Mazy <am@osimis.io>
parents:
79
diff
changeset
|
38 #include "IncomingInstanceFilter.h" |
99
465bf098554b
new callback: orthanc.RegisterReceivedInstanceCallback()
Alain Mazy <am@osimis.io>
parents:
96
diff
changeset
|
39 #include "ReceivedInstanceCallback.h" |
79
068551520123
New Python function: "orthanc.RegisterStorageArea()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
77
diff
changeset
|
40 #include "StorageArea.h" |
119
cf6decdf9e15
wrapped new SDK callback: orthanc.RegisterStorageCommitmentScpCallback()
Alain Mazy <am@osimis.io>
parents:
114
diff
changeset
|
41 #include "StorageCommitmentScpCallback.h" |
50
70abe3ebbbfc
New Python function: "orthanc.RegisterIncomingHttpRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
42 |
216
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
43 #include "PythonModule.h" |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
44 #include "PythonThreadsAllower.h" |
0 | 45 #include "RestCallbacks.h" |
46 | |
47 #include "Autogenerated/sdk.h" | |
48 | |
36
fd58eb5749ed
CMake simplification using DownloadOrthancFramework.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
11
diff
changeset
|
49 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" |
0 | 50 |
51 #include <boost/algorithm/string/predicate.hpp> | |
52 #include <boost/filesystem.hpp> | |
124
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
53 #include <boost/thread.hpp> |
0 | 54 |
47
42de8b600c0c
Support of Apple OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
55 // The "dl_iterate_phdr()" function (to walk through shared libraries) |
42de8b600c0c
Support of Apple OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
56 // is not available on Microsoft Windows and Apple OS X |
42de8b600c0c
Support of Apple OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
57 #if defined(_WIN32) |
42de8b600c0c
Support of Apple OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
58 # define HAS_DL_ITERATE 0 |
42de8b600c0c
Support of Apple OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
59 #elif defined(__APPLE__) && defined(__MACH__) |
42de8b600c0c
Support of Apple OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
60 # define HAS_DL_ITERATE 0 |
42de8b600c0c
Support of Apple OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
61 #else |
42de8b600c0c
Support of Apple OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
62 # define HAS_DL_ITERATE 1 |
0 | 63 #endif |
64 | |
65 | |
66
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
66 #include "PythonString.h" |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
67 |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
68 PyObject* LookupDictionary(PyObject* module, PyObject* args) |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
69 { |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
70 const char* name = NULL; |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
71 |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
72 if (!PyArg_ParseTuple(args, "s", &name)) |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
73 { |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
74 PyErr_SetString(PyExc_TypeError, "Please provide a string containing the name of the DICOM tag of interest"); |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
75 return NULL; |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
76 } |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
77 else |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
78 { |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
79 OrthancPluginDictionaryEntry entry; |
216
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
80 OrthancPluginErrorCode code; |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
81 |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
82 { |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
83 PythonThreadsAllower allower; |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
84 code = OrthancPluginLookupDictionary(OrthancPlugins::GetGlobalContext(), &entry, name); |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
85 } |
66
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
86 |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
87 if (code == OrthancPluginErrorCode_Success) |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
88 { |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
89 /** |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
90 * "PyGILState_Ensure()" can be invoked several times from the |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
91 * same thread, so no problem in creating a PythonLock even if |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
92 * the GIL is already locked. |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
93 **/ |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
94 PythonLock lock; |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
95 |
205
234681297600
documented orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
203
diff
changeset
|
96 PythonObject dict(lock, PyDict_New()); |
127
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
97 |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
98 { |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
99 PythonObject tmp(lock, PyLong_FromUnsignedLong(entry.group)); |
205
234681297600
documented orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
203
diff
changeset
|
100 PyDict_SetItemString(dict.GetPyObject(), "Group", tmp.GetPyObject()); |
127
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
101 } |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
102 |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
103 { |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
104 PythonObject tmp(lock, PyLong_FromUnsignedLong(entry.element)); |
205
234681297600
documented orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
203
diff
changeset
|
105 PyDict_SetItemString(dict.GetPyObject(), "Element", tmp.GetPyObject()); |
127
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
106 } |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
107 |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
108 { |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
109 PythonObject tmp(lock, PyLong_FromUnsignedLong(entry.vr)); |
205
234681297600
documented orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
203
diff
changeset
|
110 PyDict_SetItemString(dict.GetPyObject(), "ValueRepresentation", tmp.GetPyObject()); |
127
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
111 } |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
112 |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
113 { |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
114 PythonObject tmp(lock, PyLong_FromUnsignedLong(entry.minMultiplicity)); |
205
234681297600
documented orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
203
diff
changeset
|
115 PyDict_SetItemString(dict.GetPyObject(), "MinMultiplicity", tmp.GetPyObject()); |
127
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
116 } |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
117 |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
118 { |
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
119 PythonObject tmp(lock, PyLong_FromUnsignedLong(entry.maxMultiplicity)); |
205
234681297600
documented orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
203
diff
changeset
|
120 PyDict_SetItemString(dict.GetPyObject(), "MaxMultiplicity", tmp.GetPyObject()); |
127
a862d554753f
fixing leaks associated with setting integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
124
diff
changeset
|
121 } |
66
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
122 |
205
234681297600
documented orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
203
diff
changeset
|
123 return dict.Release(); |
66
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
124 } |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
125 else |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
126 { |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
127 std::string message = "Unknown DICOM tag: " + std::string(name); |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
128 PyErr_SetString(PyExc_TypeError, message.c_str()); |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
129 return NULL; |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
130 } |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
131 } |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
132 } |
6fc445793796
new wrapped function: orthanc.LookupDictionary()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
65
diff
changeset
|
133 |
0 | 134 |
65
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
135 PyObject* GetInstanceData(sdk_OrthancPluginDicomInstance_Object* self, PyObject *args) |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
136 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
137 // The GIL is locked at this point (no need to create "PythonLock") |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
138 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
139 if (self->object_ == NULL) |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
140 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
141 PyErr_SetString(PyExc_ValueError, "Invalid object"); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
142 return NULL; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
143 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
144 else |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
145 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
146 OrthancPluginDicomInstance* instance = reinterpret_cast<sdk_OrthancPluginDicomInstance_Object*>(self)->object_; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
147 |
216
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
148 const void* data; |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
149 size_t size; |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
150 |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
151 { |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
152 PythonThreadsAllower allower; |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
153 data = OrthancPluginGetInstanceData(OrthancPlugins::GetGlobalContext(), instance); |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
154 size = OrthancPluginGetInstanceSize(OrthancPlugins::GetGlobalContext(), instance); |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
155 } |
65
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
156 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
157 if (data == NULL && |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
158 size != 0) |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
159 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
160 PyErr_SetString(PyExc_ValueError, "Accessing an invalid orthanc.DicomInstance object"); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
161 return NULL; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
162 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
163 else |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
164 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
165 return PyBytes_FromStringAndSize(reinterpret_cast<const char*>(data), size); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
166 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
167 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
168 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
169 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
170 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
171 /** |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
172 * Contrarily to "OrthancPluginGetImageBuffer()" that provides a |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
173 * read-write pointer, the method "orthanc.Image.GetImageBuffer()" |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
174 * returns a copy of the image buffer. Use "CreateImageFromBuffer()" |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
175 * to create an image from a Python buffer. |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
176 **/ |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
177 PyObject* GetImageBuffer(sdk_OrthancPluginImage_Object* self, PyObject *args) |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
178 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
179 // The GIL is locked at this point (no need to create "PythonLock") |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
180 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
181 if (self->object_ == NULL) |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
182 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
183 PyErr_SetString(PyExc_ValueError, "Invalid object"); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
184 return NULL; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
185 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
186 else |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
187 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
188 OrthancPluginImage* image = reinterpret_cast<sdk_OrthancPluginImage_Object*>(self)->object_; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
189 |
216
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
190 const void* buffer; |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
191 size_t size; |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
192 |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
193 { |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
194 PythonThreadsAllower allower; |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
195 buffer = OrthancPluginGetImageBuffer(OrthancPlugins::GetGlobalContext(), image); |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
196 size = (OrthancPluginGetImagePitch(OrthancPlugins::GetGlobalContext(), image) * |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
197 OrthancPluginGetImageHeight(OrthancPlugins::GetGlobalContext(), image)); |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
198 } |
65
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
199 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
200 if (buffer == NULL && |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
201 size != 0) |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
202 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
203 PyErr_SetString(PyExc_ValueError, "Accessing an invalid orthanc.Image object"); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
204 return NULL; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
205 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
206 else |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
207 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
208 return PyBytes_FromStringAndSize(reinterpret_cast<const char*>(buffer), size); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
209 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
210 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
211 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
212 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
213 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
214 /** |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
215 * This function is the Python alternative for function |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
216 * "OrthancPluginCreateImageAccessor()". Indeed, it is not possible to |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
217 * share a memory buffer between Orthanc and Python, so we have to |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
218 * create a copy of the image. |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
219 **/ |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
220 PyObject* CreateImageFromBuffer(PyObject* module, PyObject* args) |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
221 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
222 // The GIL is locked at this point (no need to create "PythonLock") |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
223 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
224 unsigned long format, width, height, sourcePitch; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
225 Py_buffer buffer; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
226 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
227 if (!PyArg_ParseTuple(args, "kkkks*", &format, &width, &height, &sourcePitch, &buffer)) |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
228 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
229 PyErr_SetString(PyExc_TypeError, "5 arguments are needed: image.PixelFormat, width, height, pitch and memory buffer"); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
230 return NULL; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
231 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
232 else if (static_cast<Py_ssize_t>(sourcePitch * height) != buffer.len) |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
233 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
234 PyBuffer_Release(&buffer); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
235 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
236 PyErr_SetString(PyExc_TypeError, "The size of the memory buffer must match the product of height by pitch"); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
237 return NULL; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
238 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
239 else |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
240 { |
216
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
241 OrthancPluginImage* image; |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
242 unsigned long targetPitch = 0; |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
243 |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
244 { |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
245 PythonThreadsAllower allower; |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
246 image = OrthancPluginCreateImage( |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
247 OrthancPlugins::GetGlobalContext(), static_cast<OrthancPluginPixelFormat>(format), width, height); |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
248 |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
249 if (image != NULL) |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
250 { |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
251 targetPitch = OrthancPluginGetImagePitch(OrthancPlugins::GetGlobalContext(), image); |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
252 } |
dff82fbd0a15
added PythonThreadsAllower in some custom functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
215
diff
changeset
|
253 } |
65
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
254 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
255 if (image == NULL) |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
256 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
257 PyBuffer_Release(&buffer); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
258 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
259 PyErr_SetString(PyExc_ValueError, "Cannot create the image"); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
260 return NULL; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
261 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
262 else |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
263 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
264 // Copy the image line by line |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
265 const uint8_t* sourcePixels = reinterpret_cast<const uint8_t*>(buffer.buf); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
266 uint8_t* targetPixels = reinterpret_cast<uint8_t*>(OrthancPluginGetImageBuffer(OrthancPlugins::GetGlobalContext(), image)); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
267 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
268 unsigned long rowSize = std::min(sourcePitch, targetPitch); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
269 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
270 for (unsigned int y = 0; y < height; y++) |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
271 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
272 memcpy(targetPixels, sourcePixels, rowSize); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
273 targetPixels += targetPitch; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
274 sourcePixels += sourcePitch; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
275 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
276 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
277 PyBuffer_Release(&buffer); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
278 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
279 { |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
280 PyObject *argList = Py_BuildValue("Lb", image, false /* not borrowed */); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
281 PyObject *python = PyObject_CallObject((PyObject *) GetOrthancPluginImageType(), argList); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
282 Py_DECREF(argList); |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
283 return python; |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
284 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
285 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
286 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
287 } |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
288 |
4da5ce3468b4
new wrapped functions: CreateImageFromBuffer(), DicomInstance.GetInstanceData() and Image.GetImageBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
64
diff
changeset
|
289 |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
290 |
0 | 291 static bool pythonEnabled_ = false; |
292 static std::string userScriptName_; | |
124
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
293 static boost::thread displayMemoryUsageThread_; |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
294 static bool displayMemoryUsageStopping_ = false; |
0 | 295 |
296 | |
47
42de8b600c0c
Support of Apple OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
297 #if HAS_DL_ITERATE == 1 |
0 | 298 |
47
42de8b600c0c
Support of Apple OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
299 #include <dlfcn.h> |
0 | 300 #include <link.h> // For dl_phdr_info |
301 | |
302 static int ForceImportCallback(struct dl_phdr_info *info, size_t size, void *data) | |
303 { | |
304 /** | |
305 * The following line solves the error: "ImportError: | |
306 * /usr/lib/python2.7/dist-packages/PIL/_imaging.x86_64-linux-gnu.so: | |
307 * undefined symbol: PyExc_SystemError" | |
308 * https://stackoverflow.com/a/48517485/881731 | |
309 * | |
310 * dlopen("/usr/lib/x86_64-linux-gnu/libpython2.7.so", RTLD_NOW | RTLD_LAZY | RTLD_GLOBAL); | |
311 * | |
312 * Another fix consists in using LD_PRELOAD as follows: | |
313 * LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libpython2.7.so ~/Subversion/orthanc/i/Orthanc tutu.json | |
314 **/ | |
315 | |
316 std::string module(info->dlpi_name); | |
317 | |
318 if (module.find("python") != std::string::npos) | |
319 { | |
171
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
320 ORTHANC_PLUGINS_LOG_WARNING("Force global loading of Python shared library: " + module); |
0 | 321 dlopen(module.c_str(), RTLD_NOW | RTLD_LAZY | RTLD_GLOBAL); |
322 } | |
323 | |
324 return 0; | |
325 } | |
326 | |
327 #endif | |
328 | |
329 | |
124
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
330 static void DisplayMemoryUsageThread() |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
331 { |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
332 { |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
333 PythonLock lock; |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
334 lock.ExecuteCommand("import tracemalloc"); |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
335 lock.ExecuteCommand("tracemalloc.start()"); |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
336 } |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
337 |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
338 while (!displayMemoryUsageStopping_) |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
339 { |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
340 { |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
341 PythonLock lock; |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
342 lock.ExecuteCommand("print('Python memory usage: %0.03fMB' % " |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
343 "(tracemalloc.get_traced_memory() [0] / (1024.0 * 1024.0)))"); |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
344 } |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
345 |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
346 boost::this_thread::sleep(boost::posix_time::seconds(1)); |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
347 } |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
348 } |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
349 |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
350 |
0 | 351 extern "C" |
352 { | |
353 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c) | |
354 { | |
203
28531e615104
documented orthanc.RegisterIncomingHttpRequestFilter() and orthanc.RegisterOnStoredInstanceCallback()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
195
diff
changeset
|
355 OrthancPlugins::SetGlobalContext(c, PLUGIN_NAME); |
171
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
356 ORTHANC_PLUGINS_LOG_WARNING("Python plugin is initializing"); |
0 | 357 |
358 | |
359 /* Check the version of the Orthanc core */ | |
360 if (OrthancPluginCheckVersion(c) == 0) | |
361 { | |
362 char info[1024]; | |
363 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin", | |
364 c->orthancVersion, | |
365 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, | |
366 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, | |
367 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); | |
368 OrthancPluginLogError(c, info); | |
369 return -1; | |
370 } | |
11 | 371 |
154
802d744d54d3
fix deprecated calls
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
144
diff
changeset
|
372 OrthancPlugins::SetDescription(PLUGIN_NAME, "Run Python scripts as Orthanc plugins"); |
11 | 373 |
0 | 374 try |
375 { | |
376 /** | |
377 * Detection of the user script | |
378 **/ | |
379 | |
124
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
380 OrthancPlugins::OrthancConfiguration globalConfig; |
0 | 381 |
124
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
382 OrthancPlugins::OrthancConfiguration pythonConfig; |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
383 globalConfig.GetSection(pythonConfig, "Python"); |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
384 |
0 | 385 std::string script; |
124
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
386 if (!globalConfig.LookupStringValue(script, "PythonScript") && |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
387 !pythonConfig.LookupStringValue(script, "Path")) |
0 | 388 { |
389 pythonEnabled_ = false; | |
390 | |
171
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
391 ORTHANC_PLUGINS_LOG_WARNING("Options \"PythonScript\" and \"Python.Path\" are not provided: " |
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
392 "Python scripting is disabled"); |
0 | 393 } |
394 else | |
395 { | |
396 pythonEnabled_ = true; | |
397 | |
398 /** | |
399 * Installation of the user script | |
400 **/ | |
401 | |
402 const boost::filesystem::path path(script); | |
403 if (!boost::iequals(path.extension().string(), ".py")) | |
404 { | |
171
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
405 ORTHANC_PLUGINS_LOG_ERROR("Python script must have the \".py\" file extension: " + |
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
406 path.string()); |
0 | 407 return -1; |
408 } | |
409 | |
410 if (!boost::filesystem::is_regular_file(path)) | |
411 { | |
171
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
412 ORTHANC_PLUGINS_LOG_ERROR("Inexistent directory for the Python script: " + |
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
413 path.string()); |
0 | 414 return -1; |
415 } | |
416 | |
417 boost::filesystem::path userScriptDirectory = boost::filesystem::absolute(path).parent_path(); | |
418 | |
419 { | |
420 boost::filesystem::path module = path.filename().replace_extension(""); | |
421 userScriptName_ = module.string(); | |
422 } | |
423 | |
171
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
424 ORTHANC_PLUGINS_LOG_WARNING("Using Python script \"" + userScriptName_ + |
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
425 ".py\" from directory: " + userScriptDirectory.string()); |
0 | 426 |
427 | |
428 /** | |
429 * Initialization of Python | |
430 **/ | |
431 | |
217
f764ba66e409
added option "AllowThreads"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
216
diff
changeset
|
432 PythonThreadsAllower::SetAllowThreads(pythonConfig.GetBooleanValue("AllowThreads", false)); |
f764ba66e409
added option "AllowThreads"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
216
diff
changeset
|
433 |
124
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
434 const bool isVerbose = (globalConfig.GetBooleanValue("PythonVerbose", false) || |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
435 pythonConfig.GetBooleanValue("Verbose", false)); |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
436 |
47
42de8b600c0c
Support of Apple OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
437 #if HAS_DL_ITERATE == 1 |
0 | 438 dl_iterate_phdr(ForceImportCallback, NULL); |
439 #endif | |
440 | |
215 | 441 PythonLock::GlobalInitialize("orthanc", "OrthancException", GetOrthancSdkFunctions, RegisterOrthancSdk, isVerbose); |
0 | 442 PythonLock::AddSysPath(userScriptDirectory.string()); |
443 | |
124
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
444 if (pythonConfig.GetBooleanValue("DisplayMemoryUsage", false)) |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
445 { |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
446 displayMemoryUsageThread_ = boost::thread(DisplayMemoryUsageThread); |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
447 } |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
448 |
0 | 449 |
450 /** | |
451 * Force loading the declarations in the user script | |
452 **/ | |
453 | |
454 PythonLock lock; | |
455 | |
456 { | |
457 PythonModule module(lock, userScriptName_); | |
458 } | |
459 | |
460 std::string traceback; | |
461 if (lock.HasErrorOccurred(traceback)) | |
462 { | |
171
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
463 ORTHANC_PLUGINS_LOG_ERROR("Error during the installation of the Python script, " |
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
464 "traceback:\n" + traceback); |
0 | 465 return -1; |
466 } | |
467 } | |
468 } | |
469 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
470 { | |
171
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
471 ORTHANC_PLUGINS_LOG_ERROR("Exception while starting the Python plugin: " + |
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
472 std::string(e.What(c))); |
0 | 473 return -1; |
474 } | |
475 | |
476 return 0; | |
477 } | |
478 | |
479 | |
480 ORTHANC_PLUGINS_API void OrthancPluginFinalize() | |
481 { | |
171
c8de83fe7faa
removed deprecation warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
166
diff
changeset
|
482 ORTHANC_PLUGINS_LOG_WARNING("Python plugin is finalizing"); |
0 | 483 |
484 if (pythonEnabled_) | |
485 { | |
486 FinalizeOnChangeCallback(); | |
487 FinalizeRestCallbacks(); | |
488 FinalizeOnStoredInstanceCallback(); | |
50
70abe3ebbbfc
New Python function: "orthanc.RegisterIncomingHttpRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
489 FinalizeIncomingHttpRequestFilter(); |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
490 FinalizeDicomScpCallbacks(); |
124
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
491 |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
492 displayMemoryUsageStopping_ = true; |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
493 |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
494 if (displayMemoryUsageThread_.joinable()) |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
495 { |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
496 displayMemoryUsageThread_.join(); |
35c0b2fc751c
added configuration section "Python" and option "DisplayMemoryUsage"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
497 } |
0 | 498 |
499 PythonLock::GlobalFinalize(); | |
500 } | |
144
93c6f12bf339
improved support for /reset but still not ok
Alain Mazy <am@osimis.io>
parents:
131
diff
changeset
|
501 |
93c6f12bf339
improved support for /reset but still not ok
Alain Mazy <am@osimis.io>
parents:
131
diff
changeset
|
502 OrthancPlugins::ResetGlobalContext(); |
0 | 503 } |
504 | |
505 | |
506 ORTHANC_PLUGINS_API const char* OrthancPluginGetName() | |
507 { | |
154
802d744d54d3
fix deprecated calls
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
144
diff
changeset
|
508 return PLUGIN_NAME; |
0 | 509 } |
510 | |
511 | |
512 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion() | |
513 { | |
514 return PLUGIN_VERSION; | |
515 } | |
516 } |