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