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 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
|
#include "Python.h"
#include <stdio.h>
#include <math.h>
#include <signal.h>
#include <ctype.h>
#include "libnumarray.h"
typedef enum
{
PIX_NEAREST,
PIX_REFLECT,
PIX_WRAP,
PIX_CONSTANT
} PixMode;
typedef struct
{
PixMode mode;
long rows, cols;
Float64 constval;
Float64 *data;
} PixData;
static long
SlowCoord(long x, long maxx, PixMode m)
{
switch(m) {
case PIX_NEAREST:
if (x < 0) x = 0;
if (x >= maxx) x = maxx-1;
return x;
case PIX_REFLECT:
if (x < 0) x = -x-1;
if (x >= maxx) x = maxx - (x - maxx) - 1;
return x;
case PIX_WRAP:
if (x < 0) x += maxx;
if (x >= maxx) x -= maxx;
return x;
case PIX_CONSTANT: /* handled in SlowPix, suppress warnings */
break;
}
return x;
}
static Float64
SlowPix(long r, long c, PixData *p)
{
long fr, fc;
if (p->mode == PIX_CONSTANT) {
if ((r < 0) || (r >= p->rows) || (c < 0) || (c >= p->cols))
return p->constval;
else {
fr = r;
fc = c;
}
} else {
fr = SlowCoord(r, p->rows, p->mode);
fc = SlowCoord(c, p->cols, p->mode);
}
return p->data[fr*p->cols + fc];
}
static int
_reject_complex(PyObject *a)
{
NumarrayType t;
if ((a == Py_None) || (a == NULL))
return 0;
t = NA_NumarrayType(a);
if (t < 0) {
PyErr_Clear();
return 0;
}
if (t == tComplex32 || t == tComplex64) {
PyErr_Format(PyExc_TypeError,
"function doesn't support complex arrays.");
return 1;
}
return 0;
}
static void
Correlate1d(long ksizex, Float64 *kernel,
long dsizex, Float64 *data,
Float64 *correlated)
{
long xc;
long halfk = ksizex/2;
for(xc=0; xc<halfk; xc++)
correlated[xc] = data[xc];
for(xc=halfk; xc<dsizex-halfk; xc++) {
long xk;
double temp = 0;
for (xk=0; xk<ksizex; xk++)
temp += kernel[xk]*data[xc-halfk+xk];
correlated[xc] = temp;
}
for(xc=dsizex-halfk; xc<dsizex; xc++)
correlated[xc] = data[xc];
}
static PyObject *
Py_Correlate1d(PyObject *obj, PyObject *args)
{
PyObject *okernel, *odata, *ocorrelated=NULL;
PyArrayObject *kernel, *data, *correlated;
if (!PyArg_ParseTuple(args, "OO|O:Correlate1d",
&okernel, &odata, &ocorrelated))
return NULL;
/* Align, Byteswap, Contiguous, Typeconvert */
kernel = NA_InputArray(okernel, tFloat64, C_ARRAY);
data = NA_InputArray(odata, tFloat64, C_ARRAY);
correlated = NA_OptionalOutputArray(ocorrelated, tFloat64, C_ARRAY,
data);
if (!kernel || !data || !correlated)
goto _fail;
if (_reject_complex(okernel) || _reject_complex(odata) ||
_reject_complex(ocorrelated))
goto _fail;
if ((kernel->nd != 1) || (data->nd != 1)) {
PyErr_Format(PyExc_ValueError,
"Correlate1d: numarray must have exactly 1 dimension.");
goto _fail;
}
if (!NA_ShapeEqual(data, correlated)) {
PyErr_Format(PyExc_ValueError,
"Correlate1d: data and output must have identical length.");
goto _fail;
}
Correlate1d(kernel->dimensions[0], NA_OFFSETDATA(kernel),
data->dimensions[0], NA_OFFSETDATA(data),
NA_OFFSETDATA(correlated));
Py_DECREF(kernel);
Py_DECREF(data);
/* Align, Byteswap, Contiguous, Typeconvert */
return NA_ReturnOutput(ocorrelated, correlated);
_fail:
Py_XDECREF(kernel);
Py_XDECREF(data);
Py_XDECREF(correlated);
return NULL;
}
/* SlowCorrelate computes 2D correlation near the boundaries of an array.
The output array shares the same dimensions as the input array, the latter
fully described by PixData.
The region defined by rmin,rmax,cmin,cmax is assumed to contain only valid
coordinates. However, access to the input array is performed using SlowPix
because pixels reachable via "kernel offsets" may be at invalid coordinates.
*/
static void
SlowCorrelate2d(long rmin, long rmax, long cmin, long cmax,
long krows, long kcols, Float64 *kernel,
PixData *pix, Float64 *output)
{
long kr, kc, r, c;
long halfkrows = krows/2;
long halfkcols = kcols/2;
for(r=rmin; r<rmax; r++) {
for(c=cmin; c<cmax; c++) {
Float64 temp = 0;
for(kr=0; kr<krows; kr++) {
long pr = r + kr - halfkrows;
for(kc=0; kc<kcols; kc++) {
long pc = c + kc - halfkcols;
temp += SlowPix(pr, pc, pix) *
kernel[kr*kcols+kc];
}
}
output[r*pix->cols+c] = temp;
}
}
}
static void
Correlate2d(long krows, long kcols, Float64 *kernel,
long drows, long dcols, Float64 *data, Float64 *correlated,
PixMode mode, Float64 cval)
{
long ki, kj, di, dj;
long halfkrows = krows/2;
long halfkcols = kcols/2;
PixData pix;
pix.mode = mode;
pix.data = data;
pix.constval = cval;
pix.rows = drows;
pix.cols = dcols;
/* Compute the boundaries using SlowPix */
SlowCorrelate2d(0, halfkrows, 0, dcols,
krows, kcols, kernel, &pix, correlated); /* top */
SlowCorrelate2d(drows-halfkrows, drows, 0, dcols,
krows, kcols, kernel, &pix, correlated); /* bottom */
SlowCorrelate2d(halfkrows, drows-halfkrows, 0, halfkcols,
krows, kcols, kernel, &pix, correlated); /* left */
SlowCorrelate2d(halfkrows, drows-halfkrows, dcols-halfkcols, dcols,
krows, kcols, kernel, &pix, correlated); /* right */
/* Correlate the center data using unchecked array access */
for(di=halfkrows; di<drows-halfkrows; di++) {
for(dj=halfkcols; dj<dcols-halfkcols; dj++) {
Float64 temp = 0;
for(ki=0; ki<krows; ki++) {
long pi = di + ki - halfkrows;
for(kj=0; kj<kcols; kj++) {
long pj = dj + kj - halfkcols;
temp += data[pi*dcols+pj] *
kernel[ki*kcols+kj];
}
}
correlated[di*dcols+dj] = temp;
}
}
}
static PyObject *
Py_Correlate2d(PyObject *obj, PyObject *args, PyObject *kw)
{
PyObject *okernel, *odata, *ocorrelated=NULL;
PyArrayObject *kernel, *data, *correlated;
Float64 cval = 0;
int mode = PIX_NEAREST;
char *keywds[] = { "kernel", "data", "output", "mode", "cval", NULL };
if (!PyArg_ParseTupleAndKeywords(
args, kw, "OO|Oid:Correlate2d", keywds,
&okernel, &odata, &ocorrelated, &mode, &cval))
return NULL;
if ((mode < PIX_NEAREST) || (mode > PIX_CONSTANT))
return PyErr_Format(PyExc_ValueError,
"Correlate2d: mode value not in range(%d,%d)",
PIX_NEAREST, PIX_CONSTANT);
/* Align, Byteswap, Contiguous, Typeconvert */
kernel = NA_InputArray(okernel, tFloat64, C_ARRAY);
data = NA_InputArray(odata, tFloat64, C_ARRAY);
correlated = NA_OptionalOutputArray(ocorrelated, tFloat64, C_ARRAY,
data);
if (!kernel || !data || !correlated)
goto _fail;
if ((kernel->nd != 2) || (data->nd != 2) || (correlated->nd != 2)) {
PyErr_Format(PyExc_ValueError, "Correlate2d: inputs must have 2 dimensions.");
goto _fail;
}
if (!NA_ShapeEqual(data, correlated)) {
PyErr_Format(PyExc_ValueError,
"Correlate2d: data and output numarray need identical shapes.");
goto _fail;
}
if (_reject_complex(okernel) || _reject_complex(odata) ||
_reject_complex(ocorrelated))
goto _fail;
Correlate2d(kernel->dimensions[0], kernel->dimensions[1],
NA_OFFSETDATA(kernel),
data->dimensions[0], data->dimensions[1],
NA_OFFSETDATA(data),
NA_OFFSETDATA(correlated),
mode, cval);
Py_DECREF(kernel);
Py_DECREF(data);
/* Align, Byteswap, Contiguous, Typeconvert */
return NA_ReturnOutput(ocorrelated, correlated);
_fail:
Py_XDECREF(kernel);
Py_XDECREF(data);
Py_XDECREF(correlated);
return NULL;
}
void Shift2d( long rows, long cols, Float64 *data, long dx, long dy, Float64 *output, int mode, Float64 cval)
{
long r, c;
PixData pix;
pix.mode = mode;
pix.constval = cval;
pix.rows = rows;
pix.cols = cols;
pix.data = data;
for(r=0; r<rows; r++)
for(c=0; c<cols; c++)
output[ r*cols + c] = SlowPix(r+dy, c+dx, &pix);
}
static PyObject *
Py_Shift2d(PyObject *obj, PyObject *args, PyObject *kw)
{
PyObject *odata, *ooutput=NULL;
PyArrayObject *data, *output;
int dx, dy;
Float64 cval = 0;
int mode = PIX_NEAREST;
char *keywds[] = { "data", "dx", "dy",
"output", "mode", "cval", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kw, "Oii|Oid:Shift2d", keywds,
&odata, &dx, &dy, &ooutput, &mode, &cval))
return NULL;
if ((mode < PIX_NEAREST) || (mode > PIX_CONSTANT))
return PyErr_Format(PyExc_ValueError,
"Shift2d: mode value not in range(%d,%d)",
PIX_NEAREST, PIX_CONSTANT);
/* Align, Byteswap, Contiguous, Typeconvert */
data = NA_InputArray(odata, tFloat64, C_ARRAY);
output = NA_OptionalOutputArray(ooutput, tFloat64, C_ARRAY,
data);
if (!data || !output)
goto _fail;
if (_reject_complex(odata) || _reject_complex(ooutput))
goto _fail;
if ((data->nd != 2)) {
PyErr_Format(PyExc_ValueError,
"Shift2d: numarray must have 2 dimensions.");
goto _fail;
}
if (!NA_ShapeEqual(data, output)) {
PyErr_Format(PyExc_ValueError,
"Shift2d: data and output numarray need identical shapes.");
goto _fail;
}
/* Invert sign of deltas to match sense of 2x2 correlation. */
Shift2d( data->dimensions[0], data->dimensions[1], NA_OFFSETDATA(data),
-dx, -dy, NA_OFFSETDATA(output), mode, cval);
Py_XDECREF(data);
/* Align, Byteswap, Contiguous, Typeconvert */
return NA_ReturnOutput(ooutput, output);
_fail:
Py_XDECREF(data);
Py_XDECREF(output);
return NULL;
}
typedef struct s_BoxData BoxData;
typedef Float64 (*SumColFunc)(long,long,BoxData*);
typedef Float64 (*SumBoxFunc)(long,long,BoxData*);
struct s_BoxData {
PixData pix;
long krows, kcols;
SumColFunc sumcol;
SumBoxFunc sumbox;
};
static Float64
SlowSumCol(long r, long c, BoxData *D)
{
Float64 sum = 0;
long i, krows = D->krows;
for(i=0; i<krows; i++) {
sum += SlowPix(r+i, c, &D->pix);
}
return sum;
}
static Float64
SlowSumBox(long r, long c, BoxData *D)
{
long i, j;
Float64 sum = 0;
for(i=0; i<D->krows; i++)
for(j=0; j<D->kcols; j++)
sum += SlowPix(r+i, c+j, &D->pix);
return sum;
}
static Float64
FastSumCol(long r, long c, BoxData *D)
{
Float64 sum = 0;
long krows = D->krows;
long cols = D->pix.cols;
Float64 *data = D->pix.data;
data += r*cols + c;
for(; krows--; data += cols) {
sum += *data;
}
return sum;
}
static Float64
FastSumBox(long r, long c, BoxData *D)
{
long i, j;
Float64 sum = 0;
long cols = D->pix.cols;
Float64 *data = D->pix.data;
data += r*cols + c;
for(i=0; i<D->krows; i++, data += cols-D->kcols)
for(j=0; j<D->kcols; j++, data++)
sum += *data;
return sum;
}
static long bound(long x, long max)
{
if (x < 0) return 0;
else if (x > max) return max;
else return x;
}
static void
BoxFunc(long rmin, long rmax, long cmin, long cmax, Float64 *output, BoxData *D)
{
long r, c;
long krows2 = D->krows/2;
long kcols2 = D->kcols/2;
long kcolseven = !(D->kcols & 1);
long rows = D->pix.rows;
long cols = D->pix.cols;
rmin = bound(rmin, rows);
rmax = bound(rmax, rows);
cmin = bound(cmin, cols);
cmax = bound(cmax, cols);
for(r=rmin; r<rmax; r++) {
Float64 sum = D->sumbox(r - krows2, cmin - kcols2, D);
for(c=cmin; c<cmax; c++) {
output[r*cols + c] = sum;
sum -= D->sumcol(r - krows2, c - kcols2, D);
sum += D->sumcol(r - krows2, c + kcols2 - kcolseven + 1, D);
}
}
}
/* BoxFuncI computes a boxcar incrementally, using a formula independent of
the size of the boxcar. Each incremental step is based on dropping a
whole column of the "back" of the boxcar, and adding in a new column in
the "front". The sums of these columns are further optimized by realizing
they can be computed from their counterparts one element above by adding in
bottom corners and subtracting out top corners.
incremental pixel layout: B C where S is the unknown, and A, B, C are known neighbors
A S each of these refers to the output array
S = A + a1 - a0 where a0 and a1 are column vectors with *bottom* elements { a, d }
C - B = b1 - b0 where b0 and b1 are column vectors with *top* elements { b, g }
column vectors and corner elements refer to the input array
offset matrix layout: b g where b is actually in b0
[b0] [b1] g " b1
[a0] S [a1] a " a0
a d d is actually in a1
a0 = b0 - b + a column vector a0 is b0 dropping top element b and adding bottom a
a1 = b1 - g + d column vector a1 is b1 dropping top element g and adding bottom d
S = A + (b1 - g + f) - (b0 - b + a) by substitution
S = A + (b1 - b0) - g + d + b - a rearranging additions
S = A + C - B - g + d + b - a by substitution
*/
static void
BoxFuncI(long rmin, long rmax, long cmin, long cmax, Float64 *output, BoxData *D)
{
long r, c;
long krows2 = D->krows/2;
long kcols2 = D->kcols/2;
long krowseven = !(D->krows & 1);
long kcolseven = !(D->kcols & 1);
long rows = D->pix.rows;
long cols = D->pix.cols;
Float64 *input = D->pix.data;
rmin = bound(rmin, rows);
rmax = bound(rmax, rows);
cmin = bound(cmin, cols);
cmax = bound(cmax, cols);
for(r=rmin; r<rmax; r++) {
long top = r - krows2 - 1;
long bottom = r + krows2 - krowseven;
for(c=cmin; c<cmax; c++) {
long left = c - kcols2 - 1;
long right = c + kcols2 - kcolseven;
Float64 A = output [ r * cols + (c-1) ];
Float64 B = output [ (r-1) * cols + (c-1) ];
Float64 C = output [ (r-1) * cols + c ];
Float64 a = input [ bottom * cols + left ];
Float64 b = input [ top * cols + left ];
Float64 g = input [ top * cols + right ];
Float64 d = input [ bottom * cols + right ];
output[r*cols + c] = A + C - B - g + d + b - a;
}
}
}
static void
Boxcar2d(long krows, long kcols, long rows, long cols, Float64 *data,
Float64 *output, PixMode mode, Float64 constval)
{
long krows2 = krows/2;
long krowseven = !(krows&1);
long kcols2 = kcols/2;
long kcolseven = !(kcols&1);
long r, c;
Float64 karea;
BoxData D;
D.pix.mode = mode;
D.pix.constval = constval;
D.pix.rows = rows;
D.pix.cols = cols;
D.pix.data = data;
D.krows = krows;
D.kcols = kcols;
D.sumcol = SlowSumCol;
D.sumbox = SlowSumBox;
/* The next 4 calls compute boxcars on the boundary pixels with
different modes detemining what data values are fetched when "out
of bounds". Presumably, this is a small minority of the data and
therefore the implementation inefficiency is not *too* damaging.
It should at least beat making 2 outsized copies of the data array. */
/* top whole plus one */
BoxFunc(0, krows2+2, 0, cols, output, &D);
/* bottom whole */
BoxFunc(rows-krows2+krowseven, rows, 0, cols, output, &D);
/* left whole plus one */
BoxFunc(0, rows, 0, kcols2+2, output, &D);
/* right whole */
BoxFunc(0, rows, cols-kcols2+kcolseven, cols, output, &D);
/* Do the boxcar on the "center" data, the data where the boxcar is
always "in bounds". Presumably, this is the bulk of the data so
this should be fast.
*/
D.sumcol = FastSumCol;
D.sumbox = FastSumBox;
BoxFuncI( krows2+2, rows-krows2+krowseven,
kcols2+2, cols-kcols2+kcolseven,
output, &D);
karea = kcols * krows;
for(r=0; r<rows; r++)
for(c=0; c<cols; c++)
output[r*cols + c] /= karea;
}
static PyObject *
Py_Boxcar2d(PyObject *obj, PyObject *args, PyObject *kw)
{
PyObject *odata, *ooutput=NULL;
PyArrayObject *data, *output;
int krows, kcols, mode=PIX_NEAREST;
Float64 cval = 0;
char *keywds[] = { "data", "krows", "kcols",
"output", "mode", "cval", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kw, "Oii|Oid:Boxcar2d", keywds,
&odata, &krows, &kcols, &ooutput, &mode, &cval))
return NULL;
/* Align, Byteswap, Contiguous, Typeconvert */
data = NA_InputArray(odata, tFloat64, C_ARRAY);
output = NA_OptionalOutputArray(ooutput, tFloat64, C_ARRAY, data);
if (!data || !output)
goto _fail;
if (_reject_complex(odata) || _reject_complex(ooutput))
goto _fail;
if ((krows < 0) || (kcols < 0)) {
PyErr_Format(PyExc_ValueError, "krows and kcols must be > 0.");
goto _fail;
}
if ((mode < PIX_NEAREST) || (mode > PIX_CONSTANT)) {
PyErr_Format(PyExc_ValueError,
"Boxcar2d: mode value not in range(%d,%d)",
PIX_NEAREST, PIX_CONSTANT);
goto _fail;
}
if ((data->nd != 2)|| (output->nd != 2)) {
PyErr_Format(PyExc_ValueError,
"Boxcar2d: numarray must have 2 dimensions.");
goto _fail;
}
if (!NA_ShapeEqual(data, output)) {
PyErr_Format(PyExc_ValueError,
"Boxcar2d: data and output numarray need identical shapes.");
goto _fail;
}
if ((kcols <=0) || (krows <= 0)) {
PyErr_Format(PyExc_ValueError,
"Boxcar2d: invalid data shape.");
goto _fail;
}
if ((kcols > data->dimensions[1]) || (krows > data->dimensions[0])) {
PyErr_Format(PyExc_ValueError, "Boxcar2d: boxcar shape incompatible with"
" data shape.");
goto _fail;
}
Boxcar2d(krows, kcols, data->dimensions[0], data->dimensions[1],
NA_OFFSETDATA(data), NA_OFFSETDATA(output), mode, cval);
Py_XDECREF(data);
/* Align, Byteswap, Contiguous, Typeconvert */
return NA_ReturnOutput(ooutput, output);
_fail:
Py_XDECREF(data);
Py_XDECREF(output);
return NULL;
}
static PyMethodDef _correlateMethods[] = {
{"Correlate1d", Py_Correlate1d, METH_VARARGS},
{"Correlate2d", (PyCFunction) Py_Correlate2d, METH_VARARGS | METH_KEYWORDS},
{"Shift2d", (PyCFunction) Py_Shift2d, METH_VARARGS | METH_KEYWORDS,
"Shift2d shifts and image by an integer number of pixels, and uses IRAF compatible modes for the boundary pixels."},
{"Boxcar2d", (PyCFunction) Py_Boxcar2d, METH_VARARGS | METH_KEYWORDS,
"Boxcar2d computes a sliding 2D boxcar average on a 2D array"},
{NULL, NULL} /* Sentinel */
};
/* platform independent*/
#ifdef MS_WIN32
__declspec(dllexport)
#endif
void init_correlate(void)
{
PyObject *m, *d;
m = Py_InitModule("_correlate", _correlateMethods);
d = PyModule_GetDict(m);
import_libnumarray();
}
/*
* Local Variables:
* mode: C
* c-file-style: "python"
* End:
*/
|