forked from pyosirix/pyosirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyDicomImage.m
325 lines (282 loc) · 9.55 KB
/
pyDicomImage.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//
// pyDicomImage.m
// pyOsiriX
//
/*
Copyright (c) 2016, The Institute of Cancer Research and The Royal Marsden.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "pyDicomImage.h"
#import "pyDicomSeries.h"
#import <OsiriXAPI/DicomSeries.h>
#include <Python/datetime.h>
# pragma mark -
# pragma mark pyDicomImageObject initialization/deallocation
static void pyDicomImage_dealloc(pyDicomImageObject *self)
{
[self->obj release];
self->ob_type->tp_free(self);
}
# pragma mark -
# pragma mark pyDicomImageObject str/repr
static PyObject *pyDicomImage_str(pyDicomImageObject *self)
{
NSString *str = [NSString stringWithFormat:@"DicomImage object\nDate: %@\nModality:%@\nInstance Number:%@\nNumber of frames:%@\nShape: (%@, %@)\n", [self->obj date], [self->obj modality], [self->obj instanceNumber], [self->obj numberOfFrames], [self->obj width], [self->obj height]];
PyObject *ostr = PyString_FromString([str UTF8String]);
if (ostr == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
return ostr;
}
# pragma mark -
# pragma mark pyDicomImageObject getters/setters
PyDoc_STRVAR(DicomImageDateAttr_doc,
"The date of image acquisition. This property cannot be set.\n"
);
static PyObject *pyDicomImage_getDate(pyDicomImageObject *self, void *closure)
{
NSDate *date = [self->obj date];
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSCalendarUnitNanosecond | NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
PyObject * oDate = PyDateTime_FromDateAndTime((int)[comps year], (int)[comps month], (int)[comps day], (int)[comps hour], (int)[comps minute], (int)[comps second], (int)([comps nanosecond]*1000));
return oDate;
}
PyDoc_STRVAR(DicomImageNumberOfFramesAttr_doc,
"The integer number of frames contained within the associated dicom SOP instance. This property cannot be set.\n"
);
static PyObject *pyDicomImage_getNumberOfFrames(pyDicomImageObject *self, void *closure)
{
NSNumber *frames = [self->obj numberOfFrames];
return PyInt_FromLong([frames longValue]);
}
PyDoc_STRVAR(DicomImageModalityAttr_doc,
"The modality of the image in string representation. This property cannot be set.\n"
);
static PyObject *pyDicomImage_getModality(pyDicomImageObject *self, void *closure)
{
NSString *modality = [self->obj modality];
return PyString_FromString([modality UTF8String]);
}
PyDoc_STRVAR(DicomImageSeriesAttr_doc,
"Returns the DicomSeries associated with the DicomImage. This property cannot be set.\n"
);
static PyObject *pyDicomImage_getSeries(pyDicomImageObject *self, void *closure)
{
DicomSeries *series = [self->obj series];
return [pyDicomSeries pythonObjectWithInstance:series];
}
PyDoc_STRVAR(DicomImageSliceLocationAttr_doc,
"The floating-point slice location in patient coordinates. This property cannot be set.\n"
);
static PyObject *pyDicomImage_getSliceLocation(pyDicomImageObject *self, void *closure)
{
NSNumber *loc = [self->obj sliceLocation];
return PyFloat_FromDouble([loc doubleValue]);
}
PyDoc_STRVAR(DicomImageInstanceNumberAttr_doc,
"The integer instance number. This property cannot be set.\n"
);
static PyObject *pyDicomImage_getInstanceNumber(pyDicomImageObject *self, void *closure)
{
NSNumber *num = [self->obj instanceNumber];
return PyInt_FromLong([num longValue]);
}
static PyGetSetDef pyDicomImage_getsetters[] =
{
{"date", (getter)pyDicomImage_getDate, NULL, DicomImageDateAttr_doc, NULL},
{"numberOfFrames", (getter)pyDicomImage_getNumberOfFrames, NULL, DicomImageNumberOfFramesAttr_doc, NULL},
{"modality", (getter)pyDicomImage_getModality, NULL, DicomImageModalityAttr_doc, NULL},
{"series", (getter)pyDicomImage_getSeries, NULL, DicomImageSeriesAttr_doc, NULL},
{"sliceLocation", (getter)pyDicomImage_getSliceLocation, NULL, DicomImageSliceLocationAttr_doc, NULL},
{"instanceNumber", (getter)pyDicomImage_getInstanceNumber, NULL, DicomImageInstanceNumberAttr_doc, NULL},
{NULL}
};
# pragma mark -
# pragma mark pyDicomImageObject methods
PyDoc_STRVAR(DicomImageWidth_doc,
"\n"
"The number of columns within the contained image.\n"
"\n"
"Args:\n"
" None.\n"
"\n"
"Returns:\n"
" int: The number of columns.\n"
"\n"
);
static PyObject *pyDicomImage_width(pyDicomImageObject *self)
{
NSNumber *width = [self->obj width];
PyObject *w = NULL;
if (width != nil) {
w = PyInt_FromLong([width longValue]);
}
return w;
}
PyDoc_STRVAR(DicomImageHeight_doc,
"\n"
"The number of rows within the contained image.\n"
"\n"
"Args:\n"
" None.\n"
"\n"
"Returns:\n"
" int: The number of rows.\n"
"\n"
);
static PyObject *pyDicomImage_height(pyDicomImageObject *self)
{
NSNumber *height = [self->obj height];
PyObject *h = NULL;
if (height != nil) {
h = PyInt_FromLong([height longValue]);
}
return h;
}
PyDoc_STRVAR(DicomImageSOPInstanceUID_doc,
"\n"
"The SOPInstance unique identifier of the image object represented as a string.\n"
"\n"
"Args:\n"
" None.\n"
"\n"
"Returns:\n"
" str: The UID value represented by dicom tag (0008, 0018) .\n"
"\n"
);
static PyObject *pyDicomImage_sopInstanceUID(pyDicomImageObject *self)
{
NSString *sop = [self->obj sopInstanceUID];
PyObject *sop_str = NULL;
if (sop != nil) {
sop_str = PyString_FromString([sop UTF8String]);
}
return sop_str;
}
PyDoc_STRVAR(DicomImageCompletePath_doc,
"\n"
"The path of the associated dicom file.\n"
"\n"
"Args:\n"
" None.\n"
"\n"
"Returns:\n"
" str: The path of dicom file conforming to RFC 1808.\n"
"\n"
);
static PyObject *pyDicomImage_completePath(pyDicomImageObject *self)
{
NSString *cPath = [self->obj completePath];
PyObject *cPath_str = NULL;
if (cPath != nil) {
cPath_str = PyString_FromString([cPath UTF8String]);
}
return cPath_str;
}
static PyMethodDef pyDicomImageMethods[] =
{
{"width", (PyCFunction)pyDicomImage_width, METH_NOARGS, DicomImageWidth_doc},
{"height", (PyCFunction)pyDicomImage_height, METH_NOARGS, DicomImageHeight_doc},
{"sopInstanceUID", (PyCFunction)pyDicomImage_sopInstanceUID, METH_NOARGS, DicomImageSOPInstanceUID_doc},
{"completePath", (PyCFunction)pyDicomImage_completePath, METH_NOARGS, DicomImageCompletePath_doc},
{NULL}
};
# pragma mark -
# pragma mark pyDicomImageType definition
PyDoc_STRVAR(DicomImage_doc,
"A python implementation of the OsiriX 'DicomImage' class.\n"
"DicomImage is a convienience class used by the main browser within OsiriX to organise dicom instances.\n"
"Instances of this class should not be created. Instead instances are accessed\n"
"via functions defined in the BrowserController class\n"
"\n"
"Example Usage:\n"
" >>> import osirix"
" >>> bc = osirix.currentBrowser()\n"
" >>> seriesOrStudies = bc.databaseSelection()\n"
" >>> images = seriesOrStudies.images\n"
" >>> print images[0].completePath\n"
);
PyTypeObject pyDicomImageType =
{
PyObject_HEAD_INIT(NULL)
0,
"osirix.DicomImage",
sizeof(pyDicomImageObject),
0,
(destructor)pyDicomImage_dealloc,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
(reprfunc)pyDicomImage_str,
0,
0,
0,
Py_TPFLAGS_DEFAULT,
DicomImage_doc,
0,
0,
0,
0,
0,
0,
pyDicomImageMethods,
0,
pyDicomImage_getsetters,
0,
0,
0,
0,
0,
0,
0,
0,
};
# pragma mark -
# pragma mark pyDicomImage implementation
@implementation pyDicomImage
+ (void)initTypeInModule:(PyObject *)module
{
if (PyType_Ready(&pyDicomImageType) < 0) {
return;
}
Py_INCREF(&pyDicomImageType);
PyDateTime_IMPORT;
PyModule_AddObject(module, "DicomImage", (PyObject*)&pyDicomImageType);
}
+ (PyObject *)pythonObjectWithInstance:(id)obj
{
if ([obj class] != [DicomImage class]) {
return NULL;
}
pyDicomImageObject *o = PyObject_New(pyDicomImageObject, &pyDicomImageType);
o->obj = [obj retain];
return (PyObject *)o;
}
@end