| 1 | /* -*- coding: utf-8;mode:c++;c-file-style:"stroustrup" -*- */ |
| 2 | /* |
| 3 | Licensed under the terms of the BSD 3-Clause |
| 4 | (see plotpy/__init__.py for details) |
| 5 | */ |
| 6 | #include <Python.h> |
| 7 | #undef NO_IMPORT_ARRAY |
| 8 | #define PY_ARRAY_UNIQUE_SYMBOL PyScalerArray |
| 9 | #include <numpy/arrayobject.h> |
| 10 | #ifdef _MSC_VER |
| 11 | #include <float.h> |
| 12 | #define FE_TOWARDZERO _RC_CHOP |
| 13 | #define fegetround() (_controlfp(0, 0) & _MCW_RC) |
| 14 | int fesetround(int r) |
| 15 | { |
| 16 | if ((r & _MCW_RC) == r) |
| 17 | { /* check the supplied value is one of |
| 18 | those allowed */ |
| 19 | int result = _controlfp(r, _MCW_RC) & _MCW_RC; |
| 20 | return !(r == result); |
| 21 | } |
| 22 | return !0; |
| 23 | } |
| 24 | #else |
| 25 | #include <fenv.h> |
| 26 | #endif |
| 27 | #include <math.h> |
| 28 | #if defined(_MSC_VER) || defined(__MINGW32__) |
| 29 | #define isnan(x) _isnan(x) |
| 30 | #endif |
| 31 | #include <stdio.h> |
| 32 | #include <algorithm> |
| 33 | #include <iostream> |
| 34 | #include <vector> |
| 35 | #include "points.hpp" |
| 36 | #include "arrays.hpp" |
| 37 | #include "scaler.hpp" |
| 38 | |
| 39 | using std::max; |
| 40 | using std::min; |
| 41 | using std::swap; |
| 42 | using std::vector; |
| 43 | |
| 44 | enum |
| 45 | { |
| 46 | INTERP_NEAREST = 0, |
| 47 | INTERP_LINEAR = 1, |
| 48 | INTERP_AA = 2 |
| 49 | }; |
| 50 | |
| 51 | typedef union |
| 52 | { |
| 53 | npy_uint32 v; |
| 54 | npy_uint8 c[4]; |
| 55 | } rgba_t; |
| 56 | |
| 57 | typedef XYTransform<Array1D<double>> XYScale; |
| 58 | |
| 59 | template <class Transform> |
| 60 | struct params |
| 61 | { |
| 62 | typedef Transform transform_type; |
| 63 | params(PyArrayObject *_src, |
| 64 | PyArrayObject *_dst, PyObject *_dst_data, |
| 65 | PyObject *_lut, PyObject *_interp, |
| 66 | Transform &_trans) : p_dst(_dst), p_dst_data(_dst_data), |
| 67 | p_src(_src), |
| 68 | p_lut(_lut), p_interpolation(_interp), |
| 69 | trans(_trans) |
| 70 | { |
| 71 | } |
| 72 | // Source, dest, coordinate transformation |
| 73 | PyArrayObject *p_dst; |
| 74 | PyObject *p_dst_data; |
| 75 | PyArrayObject *p_src; |
| 76 | PyObject *p_lut; // Pixel value transformation tuple |
| 77 | PyObject *p_interpolation; |
| 78 | Transform &trans; |
| 79 | |
| 80 | int dx1, dx2, dy1, dy2; |
| 81 | }; |
| 82 | |
| 83 | template <class T, class TR> |
| 84 | struct NearestInterpolation |
| 85 | { |
| 86 | T operator()(const Array2D<T> &src, const TR &tr, const typename TR::point &p) |
| 87 | { |
| 88 | int nx = p.ix(); |
| 89 | int ny = p.iy(); |
| 90 | return src.value(nx, ny); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | template <class T, class TR> |
| 95 | struct LinearInterpolation |
| 96 | { |
| 97 | T operator()(const Array2D<T> &src, const TR &tr, const typename TR::point &p) |
| 98 | { |
| 99 | int nx = p.ix(); |
| 100 | int ny = p.iy(); |
| 101 | double v = src.value(nx, ny); |
| 102 | double a = 0; |
| 103 | |
| 104 | // The following couple of lines were commented out to avoid disabling |
| 105 | // the linear interpolation on image edges. Demonstrating the effect of |
| 106 | // this change is quite easy: just try to show a very small image |
| 107 | // (e.g. 10x10) with pyplot.imshow for example. |
| 108 | // if (nx==0||nx==src.nj-1) return (T)v; |
| 109 | // if (ny==0||ny==src.ni-1) return (T)v; |
| 110 | |
| 111 | if (nx < src.nj - 1) |
| 112 | { |
| 113 | a = p.x() - nx; |
| 114 | v = (1 - a) * v + a * src.value(nx + 1, ny); |
| 115 | } |
| 116 | if (ny >= src.ni - 1) |
| 117 | return (T)v; |
| 118 | double v2 = src.value(nx, ny + 1); |
| 119 | double b = p.y() - ny; |
| 120 | if (nx < src.nj - 1) |
| 121 | { |
| 122 | v2 = (1 - a) * v2 + a * src.value(nx + 1, ny + 1); |
| 123 | } |
| 124 | return (T)(v * (1 - b) + b * v2); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | template <class TR> |
| 129 | struct LinearInterpolation<npy_uint32, TR> |
| 130 | { |
| 131 | npy_uint32 operator()(const Array2D<npy_uint32> &src, const TR &tr, const typename TR::point &p) |
| 132 | { |
| 133 | int k; |
| 134 | int nx = p.ix(); |
| 135 | int ny = p.iy(); |
| 136 | rgba_t p1, p2, p3, p4, r; |
| 137 | p1.v = src.value(nx, ny); |
| 138 | float v[4], v2[4]; |
| 139 | double a = 0; |
| 140 | if (nx < src.nj - 1) |
| 141 | { |
| 142 | p2.v = src.value(nx + 1, ny); |
| 143 | a = p.x() - nx; |
| 144 | for (k = 0; k < 4; ++k) |
| 145 | { |
| 146 | v[k] = (1 - a) * p1.c[k] + a * p2.c[k]; |
| 147 | } |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | for (k = 0; k < 4; ++k) |
| 152 | { |
| 153 | v[k] = p1.c[k]; |
| 154 | } |
| 155 | } |
| 156 | if (ny >= src.ni - 1) |
| 157 | { |
| 158 | for (k = 0; k < 4; ++k) |
| 159 | { |
| 160 | r.c[k] = (npy_uint8)(v[k]); |
| 161 | } |
| 162 | return r.v; |
| 163 | } |
| 164 | p3.v = src.value(nx, ny + 1); |
| 165 | double b = p.y() - ny; |
| 166 | if (nx < src.nj - 1) |
| 167 | { |
| 168 | p4.v = src.value(nx + 1, ny + 1); |
| 169 | for (k = 0; k < 4; ++k) |
| 170 | { |
| 171 | v2[k] = (1 - a) * p3.c[k] + a * p4.c[k]; |
| 172 | } |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | for (k = 0; k < 4; ++k) |
| 177 | { |
| 178 | v2[k] = p3.c[k]; |
| 179 | } |
| 180 | } |
| 181 | for (k = 0; k < 4; ++k) |
| 182 | { |
| 183 | float px = v[k] * (1 - b) + b * v2[k]; |
| 184 | if (px < 0.0) |
| 185 | px = 0.0; |
| 186 | if (px > 255.0) |
| 187 | px = 255.0; |
| 188 | r.c[k] = (npy_uint8)px; |
| 189 | } |
| 190 | return r.v; |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | #if 0 |
| 195 | template<class T, class TR> |
| 196 | struct QuadInterpolation { |
| 197 | T operator()(const Array2D<T>& src, const TR& tr, const typename TR::point& p) { |
| 198 | int nx = p.ix(); |
| 199 | int ny = p.iy(); |
| 200 | double v = src.value(nx, ny); |
| 201 | double a=0; |
| 202 | if (nx==0||nx==src.nj-1) return src.value(nx,ny); |
| 203 | if (ny==0||ny==src.ni-1) return src.value(nx,ny); |
| 204 | |
| 205 | v0 = interp(src.value(nx-1,ny-1)); |
| 206 | if (nx<src.nj-1) { |
| 207 | a = p.x()-nx; |
| 208 | v = (1-a)*v+a*src.value(nx+1,ny); |
| 209 | } |
| 210 | if (ny>=src.ni-1) return v; |
| 211 | double v2 = src.value(nx,ny+1); |
| 212 | double b = p.y()-ny; |
| 213 | if (nx<src.nj-1) { |
| 214 | v2 = (1-a)*v2+a*src.value(nx+1,ny+1); |
| 215 | } |
| 216 | return v*(1-b)+b*v2; |
| 217 | } |
| 218 | }; |
| 219 | #endif |
| 220 | |
| 221 | template <class T> |
| 222 | struct LinearInterpolation<T, XYScale> |
| 223 | { |
| 224 | T operator()(const Array2D<T> &src, const XYScale &tr, const typename XYScale::point &p) |
| 225 | { |
| 226 | int nx = p.ix(); |
| 227 | int ny = p.iy(); |
| 228 | double v = src.value(nx, ny); |
| 229 | double a = 0; |
| 230 | |
| 231 | if (nx == 0 || nx == src.nj - 1) |
| 232 | return (T)v; |
| 233 | if (ny == 0 || ny == src.ni - 1) |
| 234 | return (T)v; |
| 235 | |
| 236 | if (nx < src.nj - 1) |
| 237 | { |
| 238 | double x0 = tr.ax.value(nx); |
| 239 | double x1 = tr.ax.value(nx + 1); |
| 240 | a = (p.x() - x0) / (x1 - x0); |
| 241 | v = (1 - a) * v + a * src.value(nx + 1, ny); |
| 242 | } |
| 243 | if (ny >= src.ni - 1) |
| 244 | return (T)v; |
| 245 | double v2 = src.value(nx, ny + 1); |
| 246 | double y0 = tr.ay.value(ny); |
| 247 | double y1 = tr.ay.value(ny + 1); |
| 248 | double b = (p.y() - y0) / (y1 - y0); |
| 249 | if (nx < src.nj - 1) |
| 250 | { |
| 251 | v2 = (1 - a) * v2 + a * src.value(nx + 1, ny + 1); |
| 252 | } |
| 253 | return (T)(v * (1 - b) + b * v2); |
| 254 | } |
| 255 | }; |
| 256 | |
| 257 | template <> |
| 258 | struct LinearInterpolation<npy_uint32, XYScale> |
| 259 | { |
| 260 | npy_uint32 operator()(const Array2D<npy_uint32> &src, const XYScale &tr, const XYScale::point &p) |
| 261 | { |
| 262 | return 0; |
| 263 | } |
| 264 | }; |
| 265 | |
| 266 | template <class T, class TR> |
| 267 | struct SubSampleInterpolation |
| 268 | { |
| 269 | SubSampleInterpolation(const Array2D<T> &_mask) : mask(_mask) |
| 270 | { |
| 271 | ki = 1. / (mask.ni - 1); |
| 272 | kj = 1. / (mask.nj - 1); |
| 273 | } |
| 274 | T operator()(const Array2D<T> &src, const TR &tr, const typename TR::point &p0) |
| 275 | { |
| 276 | int i, j; |
| 277 | typename TR::point p, p1; |
| 278 | typename num_trait<T>::large_type value = 0; |
| 279 | typename num_trait<T>::large_type count = 0, msk, val; |
| 280 | p1.copy(p0); |
| 281 | tr.incy(p1, -0.5); |
| 282 | tr.incx(p1, -0.5); |
| 283 | for (i = 0; i < mask.ni; ++i) |
| 284 | { |
| 285 | p.copy(p1); |
| 286 | for (j = 0; j < mask.nj; ++j) |
| 287 | { |
| 288 | if (p.inside()) |
| 289 | { |
| 290 | msk = mask.value(j, i); |
| 291 | val = src.value(p.ix(), p.iy()); |
| 292 | value += msk * val; |
| 293 | count += msk; |
| 294 | // printf("i,j=%d,%d : %f,%f / %d,%d = %lfx%lf\n", i, j, p.x(), p.y(), p.ix(), p.iy(), (double)val, (double)msk ); |
| 295 | } |
| 296 | tr.incx(p, kj); |
| 297 | } |
| 298 | tr.incy(p1, ki); |
| 299 | } |
| 300 | // printf("%d/%d\n", (int)value, (int)count); |
| 301 | if (count) |
| 302 | return value / count; |
| 303 | else |
| 304 | return value; |
| 305 | } |
| 306 | typename TR::real ki, kj; |
| 307 | const Array2D<T> &mask; |
| 308 | }; |
| 309 | |
| 310 | template <class DEST, class ST, class Scale, class Trans, class Interpolation> |
| 311 | void _scale_rgb(DEST &dest, |
| 312 | Array2D<ST> &src, const Scale &scale, const Trans &tr, |
| 313 | int dx1, int dy1, int dx2, int dy2, |
| 314 | Interpolation &interpolate) |
| 315 | { |
| 316 | int i, j; |
| 317 | ST val; |
| 318 | int round = fegetround(); |
| 319 | PixelIterator<DEST> it(dest); |
| 320 | typename Trans::point p, p0; |
| 321 | |
| 322 | fesetround(FE_TOWARDZERO); |
| 323 | /* |
| 324 | printf("SRC: ni=%d nj=%d\n", src.ni, src.nj); |
| 325 | printf("TR: dx=%lf dy=%lf\n", tr.dx, tr.dy); |
| 326 | printf("DST: ni=%d nj=%d si=%d sj=%d\n", dest.ni, dest.nj, dest.si, dest.sj); |
| 327 | */ |
| 328 | tr.set(p0, dx1, dy1); |
| 329 | for (i = dy1; i < dy2; ++i) |
| 330 | { |
| 331 | it.moveto(dx1, i); |
| 332 | p = p0; |
| 333 | for (j = dx1; j < dx2; ++j) |
| 334 | { |
| 335 | if (!p.inside()) |
| 336 | { |
| 337 | scale.set_bg(it()); |
| 338 | } |
| 339 | else |
| 340 | { |
| 341 | val = interpolate(src, tr, p); |
| 342 | if (isnan(val)) |
| 343 | { |
| 344 | scale.set_bg(it()); |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | it() = scale.eval(val); |
| 349 | } |
| 350 | } |
| 351 | tr.incx(p); |
| 352 | it.move(1, 0); |
| 353 | } |
| 354 | tr.incy(p0); |
| 355 | } |
| 356 | fesetround(round); |
| 357 | } |
| 358 | |
| 359 | static bool check_dispatch_type(const char *name, PyArrayObject *p_src) |
| 360 | { |
| 361 | if (PyArray_TYPE(p_src) != NPY_DOUBLE && |
| 362 | PyArray_TYPE(p_src) != NPY_FLOAT && |
| 363 | PyArray_TYPE(p_src) != NPY_UINT64 && |
| 364 | PyArray_TYPE(p_src) != NPY_INT64 && |
| 365 | PyArray_TYPE(p_src) != NPY_UINT32 && |
| 366 | PyArray_TYPE(p_src) != NPY_INT32 && |
| 367 | PyArray_TYPE(p_src) != NPY_UINT16 && |
| 368 | PyArray_TYPE(p_src) != NPY_INT16 && |
| 369 | PyArray_TYPE(p_src) != NPY_UINT8 && |
| 370 | PyArray_TYPE(p_src) != NPY_INT8 && |
| 371 | PyArray_TYPE(p_src) != NPY_BOOL) |
| 372 | { |
| 373 | PyErr_Format(PyExc_TypeError, "%s data type must be one of the following:" |
| 374 | " double, float, uint64, int64, uint32, int32, uint16, int16, uint8, int8, bool", |
| 375 | name); |
| 376 | return false; |
| 377 | } |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | static bool check_array_2d(const char *name, PyArrayObject *arr, int dtype) |
| 382 | { |
| 383 | if (!PyArray_Check(arr)) |
| 384 | { |
| 385 | PyErr_Format(PyExc_TypeError, "%s must be a ndarray", name); |
| 386 | return false; |
| 387 | } |
| 388 | if (PyArray_NDIM(arr) != 2) |
| 389 | { |
| 390 | PyErr_Format(PyExc_TypeError, "%s must be 2-D array", name); |
| 391 | return false; |
| 392 | } |
| 393 | if (dtype >= 0 && PyArray_TYPE(arr) != dtype) |
| 394 | { |
| 395 | PyErr_Format(PyExc_TypeError, "%s data type must be %d", name, dtype); |
| 396 | return false; |
| 397 | } |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | bool check_arrays(PyArrayObject *p_src, PyArrayObject *p_dest) |
| 402 | { |
| 403 | if (!PyArray_Check(p_src) || !PyArray_Check(p_dest)) |
| 404 | { |
| 405 | PyErr_SetString(PyExc_TypeError, "src and dst must be ndarrays"); |
| 406 | return false; |
| 407 | } |
| 408 | if (PyArray_TYPE(p_dest) != NPY_UINT32 && |
| 409 | PyArray_TYPE(p_dest) != NPY_FLOAT32 && |
| 410 | PyArray_TYPE(p_dest) != NPY_FLOAT64) |
| 411 | { |
| 412 | PyErr_SetString(PyExc_TypeError, "dst data type must be uint32 or float"); |
| 413 | return false; |
| 414 | } |
| 415 | if (PyArray_NDIM(p_src) != 2 || PyArray_NDIM(p_dest) != 2) |
| 416 | { |
| 417 | PyErr_SetString(PyExc_TypeError, "dst and src must be 2-D arrays"); |
| 418 | return false; |
| 419 | } |
| 420 | return check_dispatch_type("src", p_src); |
| 421 | } |
| 422 | |
| 423 | bool check_lut(PyArrayObject *p_lut) |
| 424 | { |
| 425 | if (!PyArray_Check(p_lut)) |
| 426 | { |
| 427 | PyErr_SetString(PyExc_TypeError, "lut must be an ndarray"); |
| 428 | return false; |
| 429 | } |
| 430 | if (PyArray_NDIM(p_lut) != 1) |
| 431 | { |
| 432 | PyErr_SetString(PyExc_TypeError, "lut must be a 1D array"); |
| 433 | return false; |
| 434 | } |
| 435 | if (PyArray_TYPE(p_lut) != NPY_UINT32) |
| 436 | { |
| 437 | PyErr_SetString(PyExc_TypeError, "lut data type must be uint32"); |
| 438 | return false; |
| 439 | } |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | static bool check_transform(PyArrayObject *p_tr) |
| 444 | { |
| 445 | if (!PyArray_Check(p_tr)) |
| 446 | { |
| 447 | PyErr_SetString(PyExc_TypeError, "transform must be an ndarray"); |
| 448 | return false; |
| 449 | } |
| 450 | if (PyArray_TYPE(p_tr) != NPY_DOUBLE) |
| 451 | { |
| 452 | PyErr_SetString(PyExc_TypeError, "transform data type must be float"); |
| 453 | return false; |
| 454 | } |
| 455 | int ni = PyArray_DIM(p_tr, 0); |
| 456 | int nj = PyArray_DIM(p_tr, 1); |
| 457 | |
| 458 | if (ni != 3 || nj != 3) |
| 459 | { |
| 460 | PyErr_SetString(PyExc_TypeError, "transform must be 3x3"); |
| 461 | return false; |
| 462 | } |
| 463 | return true; |
| 464 | } |
| 465 | |
| 466 | static void check_image_bounds(int ni, int nj, int &dx, int &dy) |
| 467 | { |
| 468 | if (dx < 0) |
| 469 | dx = 0; |
| 470 | if (dy < 0) |
| 471 | dy = 0; |
| 472 | if (dx > nj) |
| 473 | dx = nj; |
| 474 | if (dy > ni) |
| 475 | dy = ni; |
| 476 | } |
| 477 | |
| 478 | template <class Params, class PixelScale, class Interp> |
| 479 | static bool scale_src_dst_interp(Params &p, PixelScale &pixel_scale, Interp &interp) |
| 480 | { |
| 481 | typedef typename PixelScale::source_type ST; |
| 482 | typedef typename PixelScale::dest_type DT; |
| 483 | |
| 484 | Array2D<ST> src(p.p_src); |
| 485 | Array2D<DT> dst(p.p_dst); |
| 486 | |
| 487 | _scale_rgb(dst, src, pixel_scale, p.trans, |
| 488 | p.dx1, p.dy1, p.dx2, p.dy2, interp); |
| 489 | return true; |
| 490 | } |
| 491 | |
| 492 | template <class Params, class PixelScale> |
| 493 | static bool scale_src_dst(Params &p, PixelScale &pixel_scale) |
| 494 | { |
| 495 | typedef typename PixelScale::source_type ST; |
| 496 | typedef typename Params::transform_type TR; |
| 497 | typedef NearestInterpolation<ST, TR> Nearest; |
| 498 | typedef SubSampleInterpolation<ST, TR> SubAA; |
| 499 | typedef LinearInterpolation<ST, TR> Linear; |
| 500 | |
| 501 | int interpolation; |
| 502 | PyArrayObject *p_mask = 0; |
| 503 | |
| 504 | if (!PyArg_ParseTuple(p.p_interpolation, "i|O", &interpolation, &p_mask)) |
| 505 | { |
| 506 | PyErr_SetString(PyExc_ValueError, "Can't interpret interpolation"); |
| 507 | return false; |
| 508 | } |
| 509 | switch (interpolation) |
| 510 | { |
| 511 | case INTERP_NEAREST: |
| 512 | { |
| 513 | Nearest interp; |
| 514 | return scale_src_dst_interp<Params, PixelScale, Nearest>(p, pixel_scale, interp); |
| 515 | } |
| 516 | case INTERP_AA: |
| 517 | { |
| 518 | if (!check_array_2d("AA Mask", p_mask, PyArray_TYPE(p.p_src))) |
| 519 | return false; |
| 520 | Array2D<ST> mask(p_mask); |
| 521 | SubAA interp(mask); |
| 522 | return scale_src_dst_interp<Params, PixelScale, SubAA>(p, pixel_scale, interp); |
| 523 | } |
| 524 | case INTERP_LINEAR: |
| 525 | { |
| 526 | Linear interp; |
| 527 | return scale_src_dst_interp<Params, PixelScale, Linear>(p, pixel_scale, interp); |
| 528 | } |
| 529 | default: |
| 530 | PyErr_SetString(PyExc_ValueError, "Unknown interpolation type"); |
| 531 | return false; |
| 532 | }; |
| 533 | } |
| 534 | /* we know the transformation and source type, now we dispatch |
| 535 | on the destination type, which determines the LUT transformation |
| 536 | */ |
| 537 | template <class Params, class ST> |
| 538 | static bool scale_src_bw(Params &p) |
| 539 | { |
| 540 | typedef LutScale<ST, npy_uint32> color_scale; |
| 541 | typedef LinearScale<ST, npy_float32> bw32_scale; |
| 542 | typedef LinearScale<ST, npy_float64> bw64_scale; |
| 543 | double a, b; |
| 544 | PyObject *p_bg; |
| 545 | PyArrayObject *p_cmap = 0; |
| 546 | bool apply_bg = true; |
| 547 | |
| 548 | if (!PyArg_ParseTuple(p.p_lut, "ddO|O", &a, &b, &p_bg, &p_cmap)) |
| 549 | { |
| 550 | PyErr_SetString(PyExc_ValueError, "Can't interpret pixel transformation tuple"); |
| 551 | return false; |
| 552 | } |
| 553 | if (p_bg == Py_None) |
| 554 | apply_bg = false; |
| 555 | |
| 556 | switch (PyArray_TYPE(p.p_dst)) |
| 557 | { |
| 558 | case NPY_UINT32: |
| 559 | { |
| 560 | /* Destination is RGB */ |
| 561 | unsigned long bg = 0; |
| 562 | if (apply_bg) |
| 563 | { |
| 564 | #if PY_MAJOR_VERSION >= 3 |
| 565 | bg = PyLong_AsUnsignedLongMask(p_bg); |
| 566 | #else |
| 567 | bg = PyInt_AsUnsignedLongMask(p_bg); |
| 568 | #endif |
| 569 | if (PyErr_Occurred()) |
| 570 | return false; |
| 571 | } |
| 572 | if (!check_lut(p_cmap)) |
| 573 | { |
| 574 | return false; |
| 575 | } |
| 576 | Array1D<npy_uint32> cmap(p_cmap); |
| 577 | color_scale scale(a, b, cmap, bg, apply_bg); |
| 578 | return scale_src_dst<Params, color_scale>(p, scale); |
| 579 | } |
| 580 | case NPY_FLOAT32: |
| 581 | { |
| 582 | double bg = 0.0; |
| 583 | if (apply_bg) |
| 584 | { |
| 585 | bg = PyFloat_AsDouble(p_bg); |
| 586 | if (PyErr_Occurred()) |
| 587 | return false; |
| 588 | } |
| 589 | bw32_scale scale(a, b, bg, apply_bg); |
| 590 | return scale_src_dst<Params, bw32_scale>(p, scale); |
| 591 | } |
| 592 | case NPY_FLOAT64: |
| 593 | { |
| 594 | double bg = 0.0; |
| 595 | if (apply_bg) |
| 596 | { |
| 597 | bg = PyFloat_AsDouble(p_bg); |
| 598 | if (PyErr_Occurred()) |
| 599 | return false; |
| 600 | } |
| 601 | bw64_scale scale(a, b, bg, apply_bg); |
| 602 | return scale_src_dst<Params, bw64_scale>(p, scale); |
| 603 | } |
| 604 | default: |
| 605 | PyErr_SetString(PyExc_TypeError, "Destination array must be uint32 (rgb) or float (BW)"); |
| 606 | return false; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | template <class Params, class ST> |
| 611 | static bool scale_src_rgb(Params &p) |
| 612 | { |
| 613 | // Special case p_lut = None |
| 614 | NoScale<ST, npy_uint32> scale(0, false); |
| 615 | return scale_src_dst<Params, NoScale<ST, npy_uint32>>(p, scale); |
| 616 | } |
| 617 | |
| 618 | template <class Params> |
| 619 | static PyObject *dispatch_source(Params &p) |
| 620 | { |
| 621 | bool ok; |
| 622 | int dni = PyArray_DIM(p.p_dst, 0); |
| 623 | int dnj = PyArray_DIM(p.p_dst, 1); |
| 624 | |
| 625 | if (!PyArg_ParseTuple(p.p_dst_data, "iiii", |
| 626 | &p.dx1, &p.dy1, &p.dx2, &p.dy2)) |
| 627 | { |
| 628 | PyErr_SetString(PyExc_ValueError, "Invalid destination rectangle (expected tuple of 4 integers)"); |
| 629 | return NULL; |
| 630 | } |
| 631 | if (p.dx2 < p.dx1) |
| 632 | swap(p.dx1, p.dx2); |
| 633 | if (p.dy2 < p.dy1) |
| 634 | swap(p.dy1, p.dy2); |
| 635 | check_image_bounds(dni, dnj, p.dx1, p.dy1); |
| 636 | check_image_bounds(dni, dnj, p.dx2, p.dy2); |
| 637 | |
| 638 | switch (PyArray_TYPE(p.p_src)) |
| 639 | { |
| 640 | case NPY_FLOAT32: |
| 641 | ok = scale_src_bw<Params, npy_float32>(p); |
| 642 | break; |
| 643 | case NPY_FLOAT64: |
| 644 | ok = scale_src_bw<Params, npy_float64>(p); |
| 645 | break; |
| 646 | case NPY_UINT64: |
| 647 | ok = scale_src_bw<Params, npy_uint64>(p); |
| 648 | break; |
| 649 | case NPY_INT64: |
| 650 | ok = scale_src_bw<Params, npy_int64>(p); |
| 651 | break; |
| 652 | case NPY_INT32: |
| 653 | ok = scale_src_bw<Params, npy_int32>(p); |
| 654 | break; |
| 655 | case NPY_UINT32: // RGBA |
| 656 | ok = scale_src_rgb<Params, npy_uint32>(p); |
| 657 | break; |
| 658 | case NPY_UINT16: |
| 659 | ok = scale_src_bw<Params, npy_uint16>(p); |
| 660 | break; |
| 661 | case NPY_INT16: |
| 662 | ok = scale_src_bw<Params, npy_int16>(p); |
| 663 | break; |
| 664 | case NPY_UINT8: |
| 665 | ok = scale_src_bw<Params, npy_uint8>(p); |
| 666 | break; |
| 667 | case NPY_INT8: |
| 668 | ok = scale_src_bw<Params, npy_int8>(p); |
| 669 | break; |
| 670 | case NPY_BOOL: |
| 671 | ok = scale_src_bw<Params, npy_bool>(p); |
| 672 | break; |
| 673 | default: |
| 674 | PyErr_SetString(PyExc_TypeError, "Unknown source data type"); |
| 675 | return NULL; |
| 676 | } |
| 677 | if (!ok) |
| 678 | return NULL; |
| 679 | return Py_BuildValue("iiii", p.dx1, p.dy1, p.dx2, p.dy2); |
| 680 | } |
| 681 | |
| 682 | /* Input data : |
| 683 | |
| 684 | SRC, SRC_DATA, DST, DST_DATA, LUT_DATA |
| 685 | |
| 686 | SRC : PyArrayObject (i8,u8,i16,u16,float32,float64) |
| 687 | DST : PyArrayObject : u32 -> rgb, float32 : bw |
| 688 | SRC_DATA : varies : |
| 689 | Scale : source rect (x1,y1,x2,y2) |
| 690 | Transform : transformation matrix |
| 691 | XY : source rect, X array, Y array |
| 692 | DST_DATA : dest rect (dx1,dy1,dx2,dy2) |
| 693 | LUT_DATA : (a,b,bg) if DST is bw or (a,b,bg,cmap) if DST is rgb |
| 694 | */ |
| 695 | |
| 696 | static PyObject *py_scale_xy(PyObject *self, PyObject *args) |
| 697 | { |
| 698 | typedef params<XYScale> Params; |
| 699 | PyArrayObject *p_src = 0, *p_dst = 0, *p_ax = 0, *p_ay = 0; |
| 700 | PyObject *p_lut_data, *p_src_data, *p_dst_data, *p_interp_data; |
| 701 | double x1, y1, x2, y2; |
| 702 | |
| 703 | if (!PyArg_ParseTuple(args, "OOOOOO:_scale_xy", |
| 704 | &p_src, &p_src_data, |
| 705 | &p_dst, &p_dst_data, |
| 706 | &p_lut_data, &p_interp_data)) |
| 707 | { |
| 708 | return NULL; |
| 709 | } |
| 710 | if (!check_arrays(p_src, p_dst)) |
| 711 | { |
| 712 | return NULL; |
| 713 | } |
| 714 | if (!PyArg_ParseTuple(p_src_data, "OO(dddd):_scale_xy", |
| 715 | &p_ax, &p_ay, &x1, &y1, &x2, &y2)) |
| 716 | { |
| 717 | return NULL; |
| 718 | } |
| 719 | int ni = PyArray_DIM(p_src, 0); |
| 720 | int nj = PyArray_DIM(p_src, 1); |
| 721 | int dni = PyArray_DIM(p_dst, 0); |
| 722 | int dnj = PyArray_DIM(p_dst, 1); |
| 723 | double dx = (x2 - x1) / dnj; |
| 724 | double dy = (y2 - y1) / dni; |
| 725 | Array1D<double> ax(p_ax), ay(p_ay); |
| 726 | XYScale trans(nj, ni, ax, ay, x1, y1, dx, dy); |
| 727 | Params scale_params(p_src, p_dst, p_dst_data, |
| 728 | p_lut_data, p_interp_data, trans); |
| 729 | |
| 730 | // examine source type |
| 731 | return dispatch_source<Params>(scale_params); |
| 732 | } |
| 733 | |
| 734 | static PyObject *py_scale_tr(PyObject *self, PyObject *args) |
| 735 | { |
| 736 | typedef params<LinearTransform> Params; |
| 737 | PyArrayObject *p_src = 0, *p_dst = 0, *p_tr; |
| 738 | PyObject *p_lut_data, *p_dst_data, *p_interp_data; |
| 739 | |
| 740 | if (!PyArg_ParseTuple(args, "OOOOOO:_scale_tr", |
| 741 | &p_src, &p_tr, |
| 742 | &p_dst, &p_dst_data, |
| 743 | &p_lut_data, &p_interp_data)) |
| 744 | { |
| 745 | return NULL; |
| 746 | } |
| 747 | if (!check_arrays(p_src, p_dst)) |
| 748 | { |
| 749 | return NULL; |
| 750 | } |
| 751 | if (!check_transform(p_tr)) |
| 752 | { |
| 753 | return NULL; |
| 754 | } |
| 755 | |
| 756 | int ni = PyArray_DIM(p_src, 0); |
| 757 | int nj = PyArray_DIM(p_src, 1); |
| 758 | Array2D<double> tr(p_tr); |
| 759 | LinearTransform trans(nj, ni, |
| 760 | tr.value(2, 0), tr.value(2, 1), // x0, y0 |
| 761 | tr.value(0, 0), tr.value(1, 0), // xx, xy |
| 762 | tr.value(0, 1), tr.value(1, 1) // yx, yy |
| 763 | ); |
| 764 | Params scale_params(p_src, p_dst, p_dst_data, |
| 765 | p_lut_data, p_interp_data, trans); |
| 766 | |
| 767 | // examine source type |
| 768 | return dispatch_source<Params>(scale_params); |
| 769 | } |
| 770 | |
| 771 | static PyObject *py_scale_rect(PyObject *self, PyObject *args) |
| 772 | { |
| 773 | typedef params<ScaleTransform> Params; |
| 774 | PyArrayObject *p_src = 0, *p_dst = 0; |
| 775 | PyObject *p_lut_data, *p_dst_data, *p_interp_data, *p_src_data; |
| 776 | double x1, x2, y1, y2; |
| 777 | |
| 778 | if (!PyArg_ParseTuple(args, "OOOOOO:_scale_rect", |
| 779 | &p_src, &p_src_data, |
| 780 | &p_dst, &p_dst_data, |
| 781 | &p_lut_data, &p_interp_data)) |
| 782 | { |
| 783 | return NULL; |
| 784 | } |
| 785 | if (!check_arrays(p_src, p_dst)) |
| 786 | { |
| 787 | return NULL; |
| 788 | } |
| 789 | if (!PyArg_ParseTuple(p_src_data, "dddd:_scale_rect", |
| 790 | &x1, &y1, &x2, &y2)) |
| 791 | { |
| 792 | return NULL; |
| 793 | } |
| 794 | |
| 795 | int ni = PyArray_DIM(p_src, 0); |
| 796 | int nj = PyArray_DIM(p_src, 1); |
| 797 | int dni = PyArray_DIM(p_dst, 0); |
| 798 | int dnj = PyArray_DIM(p_dst, 1); |
| 799 | double dx = (x2 - x1) / dnj; |
| 800 | double dy = (y2 - y1) / dni; |
| 801 | ScaleTransform trans(nj, ni, x1, y1, dx, dy); |
| 802 | |
| 803 | Params scale_params(p_src, p_dst, p_dst_data, |
| 804 | p_lut_data, p_interp_data, trans); |
| 805 | |
| 806 | // examine source type |
| 807 | return dispatch_source<Params>(scale_params); |
| 808 | } |
| 809 | |
| 810 | class Histogram |
| 811 | { |
| 812 | public: |
| 813 | Histogram(PyArrayObject *_data, PyArrayObject *_bins, |
| 814 | PyArrayObject *_res, int ignore_bounds) : p_data(_data), |
| 815 | p_bins(_bins), |
| 816 | p_res(_res), |
| 817 | mode(ignore_bounds) |
| 818 | { |
| 819 | } |
| 820 | |
| 821 | template <class T> |
| 822 | void run() |
| 823 | { |
| 824 | Array1D<npy_uint32> res(p_res); |
| 825 | Array1D<T> data(p_data); |
| 826 | Array1D<T> bins(p_bins); |
| 827 | typename Array1D<T>::iterator it, end, bin, bin0, bin1; |
| 828 | end = data.end(); |
| 829 | bin0 = bins.begin(); |
| 830 | bin1 = bins.end(); |
| 831 | for (it = data.begin(); it < end; ++it) |
| 832 | { |
| 833 | bin = std::lower_bound(bin0, bin1, *it); |
| 834 | res.value(bin - bin0)++; |
| 835 | } |
| 836 | } |
| 837 | PyArrayObject *p_data, *p_bins, *p_res; |
| 838 | int mode; |
| 839 | }; |
| 840 | |
| 841 | static PyObject *py_histogram(PyObject *self, PyObject *args) |
| 842 | { |
| 843 | PyArrayObject *p_data = 0, *p_bins = 0, *p_res = 0; |
| 844 | int ignore_bounds = 0; |
| 845 | |
| 846 | if (!PyArg_ParseTuple(args, "OOO|i:_histogram", &p_data, &p_bins, &p_res, |
| 847 | &ignore_bounds)) |
| 848 | { |
| 849 | return NULL; |
| 850 | } |
| 851 | if (!PyArray_Check(p_data) || |
| 852 | !PyArray_Check(p_bins) || |
| 853 | !PyArray_Check(p_res)) |
| 854 | { |
| 855 | PyErr_SetString(PyExc_TypeError, "data, bins, dest must be ndarray"); |
| 856 | return NULL; |
| 857 | } |
| 858 | if (!check_dispatch_type("data", p_data)) |
| 859 | { |
| 860 | return NULL; |
| 861 | } |
| 862 | Histogram hist(p_data, p_bins, p_res, ignore_bounds); |
| 863 | dispatch_array(PyArray_TYPE(p_data), hist); |
| 864 | Py_INCREF(Py_None); |
| 865 | return Py_None; |
| 866 | } |
| 867 | |
| 868 | PyObject *py_vert_line(PyObject *self, PyObject *args); |
| 869 | PyObject *py_scale_quads(PyObject *self, PyObject *args); |
| 870 | |
| 871 | static PyMethodDef _meths[] = { |
| 872 | {"_scale_xy", py_scale_xy, METH_VARARGS, |
| 873 | "transform source to destination with arbitrary X and Y scale"}, |
| 874 | {"_scale_tr", py_scale_tr, METH_VARARGS, |
| 875 | "Linear transformation of source to destination"}, |
| 876 | {"_scale_rect", py_scale_rect, METH_VARARGS, |
| 877 | "Linear rescale of source to destination parallel to axes"}, |
| 878 | {"_scale_quads", py_scale_quads, METH_VARARGS, |
| 879 | "Linear rescale of a structured grid to destination parallel to axes"}, |
| 880 | {"_histogram", py_histogram, METH_VARARGS, |
| 881 | "Compute histogram of 1d data"}, |
| 882 | {"_line_test", py_vert_line, METH_VARARGS, |
| 883 | "Rasterize lines"}, |
| 884 | {NULL, NULL, 0, NULL} /* Sentinel */ |
| 885 | }; |
| 886 | |
| 887 | #if PY_MAJOR_VERSION >= 3 |
| 888 | static struct PyModuleDef moduledef = { |
| 889 | PyModuleDef_HEAD_INIT, |
| 890 | "_scaler", /* m_name */ |
| 891 | "Scaler module", /* m_doc */ |
| 892 | -1, /* m_size */ |
| 893 | _meths, /* m_methods */ |
| 894 | NULL, /* m_reload */ |
| 895 | NULL, /* m_traverse */ |
| 896 | NULL, /* m_clear */ |
| 897 | NULL, /* m_free */ |
| 898 | }; |
| 899 | #endif |
| 900 | |
| 901 | PyMODINIT_FUNC |
| 902 | #if PY_MAJOR_VERSION >= 3 |
| 903 | PyInit__scaler(void) |
| 904 | #else |
| 905 | init_scaler() |
| 906 | #endif |
| 907 | { |
| 908 | PyObject *m; |
| 909 | #if PY_MAJOR_VERSION >= 3 |
| 910 | m = PyModule_Create(&moduledef); |
| 911 | #else |
| 912 | m = Py_InitModule("_scaler", _meths); |
| 913 | #endif |
| 914 | import_array(); |
| 915 | PyModule_AddIntConstant(m, "INTERP_NEAREST", INTERP_NEAREST); |
| 916 | PyModule_AddIntConstant(m, "INTERP_LINEAR", INTERP_LINEAR); |
| 917 | PyModule_AddIntConstant(m, "INTERP_AA", INTERP_AA); |
| 918 | |
| 919 | #if PY_MAJOR_VERSION >= 3 |
| 920 | return m; |
| 921 | #endif |
| 922 | } |