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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
#include "mplutils.h"
#include "_image_resample.h"
#include "_image.h"
#include "py_converters.h"
#ifndef NPY_1_7_API_VERSION
#define NPY_ARRAY_C_CONTIGUOUS NPY_C_CONTIGUOUS
#endif
/**********************************************************************
* Free functions
* */
const char* image_resample__doc__ =
"resample(input_array, output_array, matrix, interpolation=NEAREST, alpha=1.0, norm=0, radius=1)\n\n"
"Resample input_array, blending it in-place into output_array, using an\n"
"affine transformation.\n\n"
"Parameters\n"
"----------\n"
"input_array : 2-d or 3-d Numpy array of float, double or uint8\n"
" If 2-d, the image is grayscale. If 3-d, the image must be of size\n"
" 4 in the last dimension and represents RGBA data.\n\n"
"output_array : 2-d or 3-d Numpy array of float, double or uint8\n"
" The dtype and number of dimensions must match `input_array`.\n\n"
"transform : matplotlib.transforms.Transform instance\n"
" The transformation from the input array to the output\n"
" array.\n\n"
"interpolation : int, optional\n"
" The interpolation method. Must be one of the following constants\n"
" defined in this module:\n\n"
" NEAREST (default), BILINEAR, BICUBIC, SPLINE16, SPLINE36,\n"
" HANNING, HAMMING, HERMITE, KAISER, QUADRIC, CATROM, GAUSSIAN,\n"
" BESSEL, MITCHELL, SINC, LANCZOS, BLACKMAN\n\n"
"resample : bool, optional\n"
" When `True`, use a full resampling method. When `False`, only\n"
" resample when the output image is larger than the input image.\n\n"
"alpha : float, optional\n"
" The level of transparency to apply. 1.0 is completely opaque.\n"
" 0.0 is completely transparent.\n\n"
"norm : float, optional\n"
" The norm for the interpolation function. Default is 0.\n\n"
"radius: float, optional\n"
" The radius of the kernel, if method is SINC, LANCZOS or BLACKMAN.\n"
" Default is 1.\n";
static PyArrayObject *
_get_transform_mesh(PyObject *py_affine, npy_intp *dims)
{
/* TODO: Could we get away with float, rather than double, arrays here? */
/* Given a non-affine transform object, create a mesh that maps
every pixel in the output image to the input image. This is used
as a lookup table during the actual resampling. */
PyObject *py_inverse = NULL;
npy_intp out_dims[3];
out_dims[0] = dims[0] * dims[1];
out_dims[1] = 2;
py_inverse = PyObject_CallMethod(
py_affine, (char *)"inverted", (char *)"", NULL);
if (py_inverse == NULL) {
return NULL;
}
numpy::array_view<double, 2> input_mesh(out_dims);
double *p = (double *)input_mesh.data();
for (npy_intp y = 0; y < dims[0]; ++y) {
for (npy_intp x = 0; x < dims[1]; ++x) {
*p++ = (double)x;
*p++ = (double)y;
}
}
PyObject *output_mesh =
PyObject_CallMethod(
py_inverse, (char *)"transform", (char *)"O",
(char *)input_mesh.pyobj(), NULL);
Py_DECREF(py_inverse);
if (output_mesh == NULL) {
return NULL;
}
PyArrayObject *output_mesh_array =
(PyArrayObject *)PyArray_ContiguousFromAny(
output_mesh, NPY_DOUBLE, 2, 2);
Py_DECREF(output_mesh);
if (output_mesh_array == NULL) {
return NULL;
}
return output_mesh_array;
}
static PyObject *
image_resample(PyObject *self, PyObject* args, PyObject *kwargs)
{
PyObject *py_input_array = NULL;
PyObject *py_output_array = NULL;
PyObject *py_transform = NULL;
resample_params_t params;
int resample_;
PyArrayObject *input_array = NULL;
PyArrayObject *output_array = NULL;
PyArrayObject *transform_mesh_array = NULL;
params.transform_mesh = NULL;
const char *kwlist[] = {
"input_array", "output_array", "transform", "interpolation",
"resample", "alpha", "norm", "radius", NULL };
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "OOO|iiddd:resample", (char **)kwlist,
&py_input_array, &py_output_array, &py_transform,
¶ms.interpolation, &resample_, ¶ms.alpha, ¶ms.norm,
¶ms.radius)) {
return NULL;
}
if (params.interpolation < 0 || params.interpolation >= _n_interpolation) {
PyErr_Format(PyExc_ValueError, "invalid interpolation value %d",
params.interpolation);
goto error;
}
params.resample = (resample_ != 0);
input_array = (PyArrayObject *)PyArray_FromAny(
py_input_array, NULL, 2, 3, NPY_ARRAY_C_CONTIGUOUS, NULL);
if (input_array == NULL) {
goto error;
}
output_array = (PyArrayObject *)PyArray_FromAny(
py_output_array, NULL, 2, 3, NPY_ARRAY_C_CONTIGUOUS, NULL);
if (output_array == NULL) {
goto error;
}
if (py_transform == NULL || py_transform == Py_None) {
params.is_affine = true;
} else {
PyObject *py_is_affine;
int py_is_affine2;
py_is_affine = PyObject_GetAttrString(py_transform, "is_affine");
if (py_is_affine == NULL) {
goto error;
}
py_is_affine2 = PyObject_IsTrue(py_is_affine);
Py_DECREF(py_is_affine);
if (py_is_affine2 == -1) {
goto error;
} else if (py_is_affine2) {
if (!convert_trans_affine(py_transform, ¶ms.affine)) {
goto error;
}
params.is_affine = true;
} else {
transform_mesh_array = _get_transform_mesh(
py_transform, PyArray_DIMS(output_array));
if (transform_mesh_array == NULL) {
goto error;
}
params.transform_mesh = (double *)PyArray_DATA(transform_mesh_array);
params.is_affine = false;
}
}
if (PyArray_NDIM(input_array) != PyArray_NDIM(output_array)) {
PyErr_Format(
PyExc_ValueError,
"Mismatched number of dimensions. Got %d and %d.",
PyArray_NDIM(input_array), PyArray_NDIM(output_array));
goto error;
}
if (PyArray_TYPE(input_array) != PyArray_TYPE(output_array)) {
PyErr_SetString(PyExc_ValueError, "Mismatched types");
goto error;
}
if (PyArray_NDIM(input_array) == 3) {
if (PyArray_DIM(output_array, 2) != 4) {
PyErr_SetString(
PyExc_ValueError,
"Output array must be RGBA");
goto error;
}
if (PyArray_DIM(input_array, 2) == 4) {
switch(PyArray_TYPE(input_array)) {
case NPY_BYTE:
case NPY_UINT8:
Py_BEGIN_ALLOW_THREADS
resample(
(agg::rgba8 *)PyArray_DATA(input_array),
PyArray_DIM(input_array, 1),
PyArray_DIM(input_array, 0),
(agg::rgba8 *)PyArray_DATA(output_array),
PyArray_DIM(output_array, 1),
PyArray_DIM(output_array, 0),
params);
Py_END_ALLOW_THREADS
break;
case NPY_UINT16:
case NPY_INT16:
Py_BEGIN_ALLOW_THREADS
resample(
(agg::rgba16 *)PyArray_DATA(input_array),
PyArray_DIM(input_array, 1),
PyArray_DIM(input_array, 0),
(agg::rgba16 *)PyArray_DATA(output_array),
PyArray_DIM(output_array, 1),
PyArray_DIM(output_array, 0),
params);
Py_END_ALLOW_THREADS
break;
case NPY_FLOAT32:
Py_BEGIN_ALLOW_THREADS
resample(
(agg::rgba32 *)PyArray_DATA(input_array),
PyArray_DIM(input_array, 1),
PyArray_DIM(input_array, 0),
(agg::rgba32 *)PyArray_DATA(output_array),
PyArray_DIM(output_array, 1),
PyArray_DIM(output_array, 0),
params);
Py_END_ALLOW_THREADS
break;
case NPY_FLOAT64:
Py_BEGIN_ALLOW_THREADS
resample(
(agg::rgba64 *)PyArray_DATA(input_array),
PyArray_DIM(input_array, 1),
PyArray_DIM(input_array, 0),
(agg::rgba64 *)PyArray_DATA(output_array),
PyArray_DIM(output_array, 1),
PyArray_DIM(output_array, 0),
params);
Py_END_ALLOW_THREADS
break;
default:
PyErr_SetString(
PyExc_ValueError,
"3-dimensional arrays must be of dtype unsigned byte, "
"unsigned short, float32 or float64");
goto error;
}
} else {
PyErr_Format(
PyExc_ValueError,
"If 3-dimensional, array must be RGBA. Got %" NPY_INTP_FMT " planes.",
PyArray_DIM(input_array, 2));
goto error;
}
} else { // NDIM == 2
switch (PyArray_TYPE(input_array)) {
case NPY_DOUBLE:
Py_BEGIN_ALLOW_THREADS
resample(
(double *)PyArray_DATA(input_array),
PyArray_DIM(input_array, 1),
PyArray_DIM(input_array, 0),
(double *)PyArray_DATA(output_array),
PyArray_DIM(output_array, 1),
PyArray_DIM(output_array, 0),
params);
Py_END_ALLOW_THREADS
break;
case NPY_FLOAT:
Py_BEGIN_ALLOW_THREADS
resample(
(float *)PyArray_DATA(input_array),
PyArray_DIM(input_array, 1),
PyArray_DIM(input_array, 0),
(float *)PyArray_DATA(output_array),
PyArray_DIM(output_array, 1),
PyArray_DIM(output_array, 0),
params);
Py_END_ALLOW_THREADS
break;
case NPY_UINT8:
case NPY_BYTE:
Py_BEGIN_ALLOW_THREADS
resample(
(unsigned char *)PyArray_DATA(input_array),
PyArray_DIM(input_array, 1),
PyArray_DIM(input_array, 0),
(unsigned char *)PyArray_DATA(output_array),
PyArray_DIM(output_array, 1),
PyArray_DIM(output_array, 0),
params);
Py_END_ALLOW_THREADS
break;
case NPY_UINT16:
case NPY_INT16:
Py_BEGIN_ALLOW_THREADS
resample(
(unsigned short *)PyArray_DATA(input_array),
PyArray_DIM(input_array, 1),
PyArray_DIM(input_array, 0),
(unsigned short *)PyArray_DATA(output_array),
PyArray_DIM(output_array, 1),
PyArray_DIM(output_array, 0),
params);
Py_END_ALLOW_THREADS
break;
default:
PyErr_SetString(PyExc_ValueError, "Unsupported dtype");
goto error;
}
}
Py_DECREF(input_array);
Py_XDECREF(transform_mesh_array);
return (PyObject *)output_array;
error:
Py_XDECREF(input_array);
Py_XDECREF(output_array);
Py_XDECREF(transform_mesh_array);
return NULL;
}
const char *image_pcolor__doc__ =
"pcolor(x, y, data, rows, cols, bounds)\n"
"\n"
"Generate a pseudo-color image from data on a non-uniform grid using\n"
"nearest neighbour or linear interpolation.\n"
"bounds = (x_min, x_max, y_min, y_max)\n"
"interpolation = NEAREST or BILINEAR \n";
static PyObject *image_pcolor(PyObject *self, PyObject *args, PyObject *kwds)
{
numpy::array_view<const float, 1> x;
numpy::array_view<const float, 1> y;
numpy::array_view<const agg::int8u, 3> d;
npy_intp rows, cols;
float bounds[4];
int interpolation;
if (!PyArg_ParseTuple(args,
"O&O&O&nn(ffff)i:pcolor",
&x.converter,
&x,
&y.converter,
&y,
&d.converter_contiguous,
&d,
&rows,
&cols,
&bounds[0],
&bounds[1],
&bounds[2],
&bounds[3],
&interpolation)) {
return NULL;
}
npy_intp dim[3] = {rows, cols, 4};
numpy::array_view<const agg::int8u, 3> output(dim);
CALL_CPP("pcolor", (pcolor(x, y, d, rows, cols, bounds, interpolation, output)));
return output.pyobj();
}
const char *image_pcolor2__doc__ =
"pcolor2(x, y, data, rows, cols, bounds, bg)\n"
"\n"
"Generate a pseudo-color image from data on a non-uniform grid\n"
"specified by its cell boundaries.\n"
"bounds = (x_left, x_right, y_bot, y_top)\n"
"bg = ndarray of 4 uint8 representing background rgba\n";
static PyObject *image_pcolor2(PyObject *self, PyObject *args, PyObject *kwds)
{
numpy::array_view<const double, 1> x;
numpy::array_view<const double, 1> y;
numpy::array_view<const agg::int8u, 3> d;
npy_intp rows, cols;
float bounds[4];
numpy::array_view<const agg::int8u, 1> bg;
if (!PyArg_ParseTuple(args,
"O&O&O&nn(ffff)O&:pcolor2",
&x.converter_contiguous,
&x,
&y.converter_contiguous,
&y,
&d.converter_contiguous,
&d,
&rows,
&cols,
&bounds[0],
&bounds[1],
&bounds[2],
&bounds[3],
&bg.converter,
&bg)) {
return NULL;
}
npy_intp dim[3] = {rows, cols, 4};
numpy::array_view<const agg::int8u, 3> output(dim);
CALL_CPP("pcolor2", (pcolor2(x, y, d, rows, cols, bounds, bg, output)));
return output.pyobj();
}
static PyMethodDef module_functions[] = {
{"resample", (PyCFunction)image_resample, METH_VARARGS|METH_KEYWORDS, image_resample__doc__},
{"pcolor", (PyCFunction)image_pcolor, METH_VARARGS, image_pcolor__doc__},
{"pcolor2", (PyCFunction)image_pcolor2, METH_VARARGS, image_pcolor2__doc__},
{NULL}
};
extern "C" {
#if PY3K
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_image",
NULL,
0,
module_functions,
NULL,
NULL,
NULL,
NULL
};
#define INITERROR return NULL
PyMODINIT_FUNC PyInit__image(void)
#else
#define INITERROR return
PyMODINIT_FUNC init_image(void)
#endif
{
PyObject *m;
#if PY3K
m = PyModule_Create(&moduledef);
#else
m = Py_InitModule3("_image", module_functions, NULL);
#endif
if (m == NULL) {
INITERROR;
}
if (PyModule_AddIntConstant(m, "NEAREST", NEAREST) ||
PyModule_AddIntConstant(m, "BILINEAR", BILINEAR) ||
PyModule_AddIntConstant(m, "BICUBIC", BICUBIC) ||
PyModule_AddIntConstant(m, "SPLINE16", SPLINE16) ||
PyModule_AddIntConstant(m, "SPLINE36", SPLINE36) ||
PyModule_AddIntConstant(m, "HANNING", HANNING) ||
PyModule_AddIntConstant(m, "HAMMING", HAMMING) ||
PyModule_AddIntConstant(m, "HERMITE", HERMITE) ||
PyModule_AddIntConstant(m, "KAISER", KAISER) ||
PyModule_AddIntConstant(m, "QUADRIC", QUADRIC) ||
PyModule_AddIntConstant(m, "CATROM", CATROM) ||
PyModule_AddIntConstant(m, "GAUSSIAN", GAUSSIAN) ||
PyModule_AddIntConstant(m, "BESSEL", BESSEL) ||
PyModule_AddIntConstant(m, "MITCHELL", MITCHELL) ||
PyModule_AddIntConstant(m, "SINC", SINC) ||
PyModule_AddIntConstant(m, "LANCZOS", LANCZOS) ||
PyModule_AddIntConstant(m, "BLACKMAN", BLACKMAN) ||
PyModule_AddIntConstant(m, "_n_interpolation", _n_interpolation)) {
INITERROR;
}
import_array();
#if PY3K
return m;
#endif
}
} // extern "C"
|