| 1 |
#define NO_IMPORT_ARRAY |
| 2 |
|
| 3 |
#include <assert.h> |
| 4 |
#define _USE_MATH_DEFINES |
| 5 |
#include <math.h> |
| 6 |
#include <stdlib.h> |
| 7 |
|
| 8 |
#ifndef NPY_NO_DEPRECATED_API |
| 9 |
#define NPY_NO_DEPRECATED_API NPY_1_21_API_VERSION |
| 10 |
#endif |
| 11 |
|
| 12 |
#define NO_IMPORT_ARRAY |
| 13 |
#define PY_ARRAY_UNIQUE_SYMBOL cdrizzle_box_api |
| 14 |
|
| 15 |
#include "driz_portability.h" |
| 16 |
#include "cdrizzlemap.h" |
| 17 |
#include "cdrizzlebox.h" |
| 18 |
#include "cdrizzleutil.h" |
| 19 |
|
| 20 |
static const double lut_delta = 0.003; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
inline_macro static int |
| 33 |
update_data( |
| 34 |
struct driz_param_t *p, const integer_t ii, const integer_t jj, const float d, const float dow) |
| 35 |
{ |
| 36 |
double vc_plus_dow; |
| 37 |
float vc; |
| 38 |
|
| 39 |
if (dow == 0.0f) { |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
vc = get_pixel(p->output_counts, ii, jj); |
| 45 |
|
| 46 |
vc_plus_dow = vc + dow; |
| 47 |
|
| 48 |
if (vc == 0.0f) { |
| 49 |
if (oob_pixel(p->output_data, ii, jj)) { |
| 50 |
driz_error_format_message(p->error, "OOB in output_data[%d,%d]", ii, jj); |
| 51 |
return 1; |
| 52 |
} else { |
| 53 |
set_pixel(p->output_data, ii, jj, d); |
| 54 |
} |
| 55 |
|
| 56 |
} else { |
| 57 |
if (oob_pixel(p->output_data, ii, jj)) { |
| 58 |
driz_error_format_message(p->error, "OOB in output_data[%d,%d]", ii, jj); |
| 59 |
return 1; |
| 60 |
} else { |
| 61 |
double value; |
| 62 |
value = (get_pixel(p->output_data, ii, jj) * vc + dow * d) / (vc_plus_dow); |
| 63 |
set_pixel(p->output_data, ii, jj, value); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
if (oob_pixel(p->output_counts, ii, jj)) { |
| 68 |
driz_error_format_message(p->error, "OOB in output_counts[%d,%d]", ii, jj); |
| 69 |
return 1; |
| 70 |
} else { |
| 71 |
set_pixel(p->output_counts, ii, jj, vc_plus_dow); |
| 72 |
} |
| 73 |
|
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
inline_macro static int |
| 91 |
update_data_var( |
| 92 |
struct driz_param_t *p, const integer_t ii, const integer_t jj, const float d, const float dow, |
| 93 |
float *d2, unsigned int dq) |
| 94 |
{ |
| 95 |
double vc_plus_dow, vc_plus_dow2; |
| 96 |
double v, vc2, dow2; |
| 97 |
float vc; |
| 98 |
int i; |
| 99 |
int output_dq_value; |
| 100 |
PyArrayObject **arr2; |
| 101 |
|
| 102 |
if (oob_output_pixel(p, ii, jj)) { |
| 103 |
driz_error_format_message(p->error, "OOB in accessing output data [%d,%d]", ii, jj); |
| 104 |
return 1; |
| 105 |
} |
| 106 |
|
| 107 |
if (dow == 0.0f) { |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
vc = get_pixel(p->output_counts, ii, jj); |
| 113 |
|
| 114 |
|
| 115 |
vc_plus_dow = vc + dow; |
| 116 |
|
| 117 |
if (vc == 0.0f) { |
| 118 |
set_pixel(p->output_data, ii, jj, d); |
| 119 |
if (d2 && (arr2 = p->output_data2)) { |
| 120 |
for (i = 0; i < p->ndata2; ++i) { |
| 121 |
set_pixel(arr2[i], ii, jj, d2[i]); |
| 122 |
} |
| 123 |
} |
| 124 |
if (p->dq) { |
| 125 |
set_uint_pixel(p->output_dq, ii, jj, dq); |
| 126 |
} |
| 127 |
} else { |
| 128 |
v = (get_pixel(p->output_data, ii, jj) * vc + dow * d) / vc_plus_dow; |
| 129 |
set_pixel(p->output_data, ii, jj, v); |
| 130 |
if (d2 && (arr2 = p->output_data2)) { |
| 131 |
dow2 = dow * dow; |
| 132 |
vc2 = vc * vc; |
| 133 |
vc_plus_dow2 = vc_plus_dow * vc_plus_dow; |
| 134 |
for (i = 0; i < p->ndata2; ++i) { |
| 135 |
v = (get_pixel(arr2[i], ii, jj) * vc2 + dow2 * d2[i]) / vc_plus_dow2; |
| 136 |
set_pixel(arr2[i], ii, jj, v); |
| 137 |
} |
| 138 |
} |
| 139 |
if (p->dq) { |
| 140 |
output_dq_value = get_uint_pixel(p->output_dq, ii, jj); |
| 141 |
set_uint_pixel(p->output_dq, ii, jj, output_dq_value | dq); |
| 142 |
} |
| 143 |
} |
| 144 |
set_pixel(p->output_counts, ii, jj, vc_plus_dow); |
| 145 |
|
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
integer_t |
| 156 |
compute_bit_value(integer_t uuid) |
| 157 |
{ |
| 158 |
integer_t bv; |
| 159 |
int np, bit_no; |
| 160 |
|
| 161 |
np = (uuid - 1) / 32 + 1; |
| 162 |
bit_no = (uuid - 1 - (32 * (np - 1))); |
| 163 |
bv = (integer_t) (1 << bit_no); |
| 164 |
|
| 165 |
return bv; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
static inline_macro double |
| 174 |
sgarea(const double x1, const double y1, const double x2, const double y2) |
| 175 |
{ |
| 176 |
double xlo, xhi, ylo, yhi, xtop; |
| 177 |
double dx, dy, det, sgn_dx; |
| 178 |
|
| 179 |
dx = x2 - x1; |
| 180 |
dy = y2 - y1; |
| 181 |
|
| 182 |
|
| 183 |
if (dx == 0) { |
| 184 |
return 0.0; |
| 185 |
} |
| 186 |
|
| 187 |
if (dx < 0) { |
| 188 |
sgn_dx = -1.0; |
| 189 |
xlo = x2; |
| 190 |
xhi = x1; |
| 191 |
} else { |
| 192 |
sgn_dx = 1.0; |
| 193 |
xlo = x1; |
| 194 |
xhi = x2; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
if (xlo >= 1.0 || xhi <= 0.0) { |
| 199 |
return 0.0; |
| 200 |
} |
| 201 |
|
| 202 |
xlo = MAX(xlo, 0.0); |
| 203 |
xhi = MIN(xhi, 1.0); |
| 204 |
|
| 205 |
|
| 206 |
double slope = dy / dx; |
| 207 |
ylo = y1 + slope * (xlo - x1); |
| 208 |
yhi = y1 + slope * (xhi - x1); |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
if (ylo <= 0.0 && yhi <= 0.0) { |
| 229 |
return 0.0; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
if (ylo >= 1.0 && yhi >= 1.0) { |
| 235 |
|
| 236 |
return sgn_dx * (xhi - xlo); |
| 237 |
} |
| 238 |
|
| 239 |
det = x1 * y2 - y1 * x2; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
if (ylo < 0.0) { |
| 244 |
ylo = 0.0; |
| 245 |
xlo = det / dy; |
| 246 |
} |
| 247 |
|
| 248 |
if (yhi < 0.0) { |
| 249 |
yhi = 0.0; |
| 250 |
xhi = det / dy; |
| 251 |
} |
| 252 |
|
| 253 |
if (ylo <= 1.0) { |
| 254 |
if (yhi <= 1.0) { |
| 255 |
|
| 256 |
|
| 257 |
return sgn_dx * 0.5 * (xhi - xlo) * (yhi + ylo); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
xtop = (dx + det) / dy; |
| 262 |
return sgn_dx * (0.5 * (xtop - xlo) * (1.0 + ylo) + xhi - xtop); |
| 263 |
} |
| 264 |
|
| 265 |
xtop = (dx + det) / dy; |
| 266 |
return sgn_dx * (0.5 * (xhi - xtop) * (1.0 + yhi) + xtop - xlo); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
double |
| 280 |
boxer(double is, double js, const double x[4], const double y[4]) |
| 281 |
{ |
| 282 |
integer_t i; |
| 283 |
double sum; |
| 284 |
double px[4], py[4]; |
| 285 |
|
| 286 |
assert(x); |
| 287 |
assert(y); |
| 288 |
|
| 289 |
is -= 0.5; |
| 290 |
js -= 0.5; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
for (i = 0; i < 4; ++i) { |
| 295 |
px[i] = x[i] - is; |
| 296 |
py[i] = y[i] - js; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
sum = 0.0; |
| 304 |
for (i = 0; i < 4; ++i) { |
| 305 |
sum += sgarea(px[i], py[i], px[(i + 1) & 0x3], py[(i + 1) & 0x3]); |
| 306 |
} |
| 307 |
|
| 308 |
return sum; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
double |
| 324 |
compute_area(double is, double js, const double x[4], const double y[4]) |
| 325 |
{ |
| 326 |
int ipoint, jpoint, idim, jdim, iside, outside, count; |
| 327 |
int positive[2]; |
| 328 |
double area, width; |
| 329 |
double midpoint[2], delta[2]; |
| 330 |
double border[2][2], segment[2][2]; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
area = 0.0; |
| 340 |
|
| 341 |
border[0][0] = is - 0.5; |
| 342 |
border[0][1] = js - 0.5; |
| 343 |
border[1][0] = is + 0.5; |
| 344 |
border[1][1] = js + 0.5; |
| 345 |
|
| 346 |
for (ipoint = 0; ipoint < 4; ++ipoint) { |
| 347 |
jpoint = (ipoint + 1) & 03; |
| 348 |
|
| 349 |
segment[0][0] = x[ipoint]; |
| 350 |
segment[0][1] = y[ipoint]; |
| 351 |
segment[1][0] = x[jpoint]; |
| 352 |
segment[1][1] = y[jpoint]; |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
for (idim = 0, count = 3; idim < 2; ++idim) { |
| 359 |
for (iside = 0; iside < 2; ++iside, --count) { |
| 360 |
delta[0] = segment[0][idim] - border[iside][idim]; |
| 361 |
delta[1] = segment[1][idim] - border[iside][idim]; |
| 362 |
|
| 363 |
positive[0] = delta[0] > 0.0; |
| 364 |
positive[1] = delta[1] > 0.0; |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
if (positive[0] == positive[1]) { |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
if (positive[0] == iside) { |
| 374 |
|
| 375 |
if (count == 0) { |
| 376 |
|
| 377 |
|
| 378 |
width = segment[1][0] - segment[0][0]; |
| 379 |
area += width; |
| 380 |
} else { |
| 381 |
goto _nextsegment; |
| 382 |
} |
| 383 |
|
| 384 |
} else { |
| 385 |
|
| 386 |
if (count == 0) { |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
width = segment[1][0] - segment[0][0]; |
| 393 |
area += 0.5 * width * (2.0 + delta[0] + delta[1]); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
} else { |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
outside = positive[iside]; |
| 402 |
jdim = (idim + 1) & 01; |
| 403 |
|
| 404 |
midpoint[idim] = border[iside][idim]; |
| 405 |
|
| 406 |
midpoint[jdim] = (delta[1] * segment[0][jdim] - delta[0] * segment[1][jdim]) / |
| 407 |
(delta[1] - delta[0]); |
| 408 |
|
| 409 |
if (count == 0) { |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
if (outside == 0) { |
| 415 |
width = midpoint[0] - segment[0][0]; |
| 416 |
area += width; |
| 417 |
width = segment[1][0] - midpoint[0]; |
| 418 |
|
| 419 |
|
| 420 |
area += 0.5 * width * (2.0 + delta[1]); |
| 421 |
} else { |
| 422 |
width = segment[1][0] - midpoint[0]; |
| 423 |
area += width; |
| 424 |
width = midpoint[0] - segment[0][0]; |
| 425 |
|
| 426 |
|
| 427 |
area += 0.5 * width * (2.0 + delta[0]); |
| 428 |
} |
| 429 |
|
| 430 |
} else { |
| 431 |
|
| 432 |
segment[outside][0] = midpoint[0]; |
| 433 |
segment[outside][1] = midpoint[1]; |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
_nextsegment: |
| 440 |
continue; |
| 441 |
} |
| 442 |
|
| 443 |
return fabs(area); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
static inline_macro double |
| 461 |
over( |
| 462 |
const integer_t i, const integer_t j, const double xmin, const double xmax, const double ymin, |
| 463 |
const double ymax) |
| 464 |
{ |
| 465 |
double dx, dy; |
| 466 |
|
| 467 |
assert(xmin <= xmax); |
| 468 |
assert(ymin <= ymax); |
| 469 |
|
| 470 |
dx = MIN(xmax, (double) (i) + 0.5) - MAX(xmin, (double) (i) -0.5); |
| 471 |
dy = MIN(ymax, (double) (j) + 0.5) - MAX(ymin, (double) (j) -0.5); |
| 472 |
|
| 473 |
if (dx > 0.0 && dy > 0.0) { |
| 474 |
return dx * dy; |
| 475 |
} |
| 476 |
|
| 477 |
return 0.0; |
| 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 |
int |
| 505 |
compute_pscale_ratio(struct driz_param_t *p, struct polygon *bounding_polygon, float *pscale_ratio) |
| 506 |
{ |
| 507 |
integer_t i, j, nx, ny, mapsize[2]; |
| 508 |
double cx, cy; |
| 509 |
double ox, oy, ox1, oy1, ox2, oy2; |
| 510 |
double cd11, cd12, cd21, cd22; |
| 511 |
|
| 512 |
if (polygon_centroid(bounding_polygon, &cx, &cy)) { |
| 513 |
goto _error; |
| 514 |
} |
| 515 |
|
| 516 |
i = (integer_t) cx; |
| 517 |
j = (integer_t) cy; |
| 518 |
|
| 519 |
get_dimensions(p->pixmap, mapsize); |
| 520 |
nx = mapsize[0]; |
| 521 |
ny = mapsize[1]; |
| 522 |
|
| 523 |
if (nx < 2 || ny < 2) { |
| 524 |
goto _error; |
| 525 |
} |
| 526 |
i = MAX(MIN(i, nx - 2), 0); |
| 527 |
j = MAX(MIN(j, ny - 2), 0); |
| 528 |
|
| 529 |
if (map_pixel(p->pixmap, i, j, &ox, &oy) || map_pixel(p->pixmap, i + 1, j, &ox1, &oy1) || |
| 530 |
map_pixel(p->pixmap, i, j + 1, &ox2, &oy2)) { |
| 531 |
goto _error; |
| 532 |
} |
| 533 |
|
| 534 |
cd11 = ox1 - ox; |
| 535 |
cd12 = oy1 - oy; |
| 536 |
cd21 = ox2 - ox; |
| 537 |
cd22 = oy2 - oy; |
| 538 |
*pscale_ratio = (float) sqrt(fabs(cd11 * cd22 - cd12 * cd21)); |
| 539 |
|
| 540 |
return 0; |
| 541 |
|
| 542 |
_error: |
| 543 |
driz_error_set_message(p->error, "Unable to estimate pscale_ratio"); |
| 544 |
return 1; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
static int |
| 554 |
do_kernel_point_var(struct driz_param_t *p) |
| 555 |
{ |
| 556 |
struct scanner s; |
| 557 |
integer_t i, j, ii, jj, k; |
| 558 |
integer_t osize[2]; |
| 559 |
float d, dow, *d2 = NULL, iscale2 = 1.0f; |
| 560 |
integer_t bv; |
| 561 |
int xmin, xmax, ymin, ymax, n, ndata2; |
| 562 |
unsigned int dqval = 0; |
| 563 |
|
| 564 |
ndata2 = p->ndata2; |
| 565 |
|
| 566 |
bv = compute_bit_value(p->uuid); |
| 567 |
|
| 568 |
if (init_image_scanner(p, &s, &ymin, &ymax)) { |
| 569 |
return 1; |
| 570 |
} |
| 571 |
|
| 572 |
p->nskip = (p->ymax - p->ymin) - (ymax - ymin); |
| 573 |
p->nmiss = p->nskip * (p->xmax - p->xmin); |
| 574 |
|
| 575 |
|
| 576 |
get_dimensions(p->output_data, osize); |
| 577 |
|
| 578 |
if (ndata2 > 0) { |
| 579 |
iscale2 = p->iscale * p->iscale; |
| 580 |
|
| 581 |
if (!(d2 = (float *) malloc(p->ndata2 * sizeof(float)))) { |
| 582 |
driz_error_set(p->error, PyExc_MemoryError, "Memory allocation failed."); |
| 583 |
return 1; |
| 584 |
} |
| 585 |
if (!p->output_data2) { |
| 586 |
driz_error_set( |
| 587 |
p->error, PyExc_RuntimeError, |
| 588 |
"'output_data2' must be a valid pointer when " |
| 589 |
"'data2' is valid."); |
| 590 |
free(d2); |
| 591 |
return 1; |
| 592 |
} |
| 593 |
for (i = 0; i < ndata2; ++i) { |
| 594 |
if (!p->output_data2[i]) { |
| 595 |
driz_error_set( |
| 596 |
p->error, PyExc_RuntimeError, |
| 597 |
"Some arrays in 'output_data2' have invalid pointers."); |
| 598 |
free(d2); |
| 599 |
return 1; |
| 600 |
} |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
for (j = ymin; j <= ymax; ++j) { |
| 605 |
|
| 606 |
n = get_scanline_limits(&s, j, &xmin, &xmax); |
| 607 |
if (n == 1) { |
| 608 |
|
| 609 |
p->nskip += (ymax + 1 - j); |
| 610 |
p->nmiss += (ymax + 1 - j) * (p->xmax - p->xmin); |
| 611 |
break; |
| 612 |
} else if (n == 2 || n == 3) { |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
p->nmiss += (p->xmax - p->xmin); |
| 617 |
++p->nskip; |
| 618 |
continue; |
| 619 |
} else { |
| 620 |
|
| 621 |
p->nmiss += (p->xmax - p->xmin) - (xmax + 1 - xmin); |
| 622 |
} |
| 623 |
|
| 624 |
for (i = xmin; i <= xmax; ++i) { |
| 625 |
double ox, oy; |
| 626 |
|
| 627 |
if (map_pixel(p->pixmap, i, j, &ox, &oy)) { |
| 628 |
++p->nmiss; |
| 629 |
|
| 630 |
} else { |
| 631 |
ii = nintd(ox); |
| 632 |
jj = nintd(oy); |
| 633 |
|
| 634 |
|
| 635 |
if (ii < 0 || ii >= osize[0] || jj < 0 || jj >= osize[1]) { |
| 636 |
++p->nmiss; |
| 637 |
|
| 638 |
} else { |
| 639 |
|
| 640 |
d = get_pixel(p->data, i, j) * p->iscale; |
| 641 |
for (k = 0; k < ndata2; ++k) { |
| 642 |
if (p->data2[k]) { |
| 643 |
d2[k] = get_pixel(p->data2[k], i, j) * iscale2; |
| 644 |
} else { |
| 645 |
d2[k] = 0.0f; |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
if (p->weights) { |
| 653 |
dow = get_pixel(p->weights, i, j) * p->weight_scale; |
| 654 |
} else { |
| 655 |
dow = 1.0f; |
| 656 |
} |
| 657 |
|
| 658 |
if (p->dq) { |
| 659 |
dqval = (int) get_uint_pixel(p->dq, i, j); |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
if (p->output_context && dow > 0.0f) { |
| 665 |
set_bit(p->output_context, ii, jj, bv); |
| 666 |
} |
| 667 |
|
| 668 |
if (update_data_var(p, ii, jj, d, dow, d2, dqval)) { |
| 669 |
free(d2); |
| 670 |
return 1; |
| 671 |
} |
| 672 |
} |
| 673 |
} |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
free(d2); |
| 678 |
return 0; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
static int |
| 689 |
do_kernel_gaussian_var(struct driz_param_t *p) |
| 690 |
{ |
| 691 |
struct scanner s; |
| 692 |
integer_t bv, i, j, ii, jj, k, nxi, nxa, nyi, nya, nhit; |
| 693 |
integer_t osize[2]; |
| 694 |
float d, dow, *d2 = NULL, iscale2 = 1.0f; |
| 695 |
double gaussian_efac, gaussian_es; |
| 696 |
double pfo, ac, w, ddx, ddy, r2, dover, kscale2; |
| 697 |
const double nsig = 2.5; |
| 698 |
int xmin, xmax, ymin, ymax, n, ndata2; |
| 699 |
unsigned int dqval = 0; |
| 700 |
|
| 701 |
ndata2 = p->ndata2; |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
if (init_image_scanner(p, &s, &ymin, &ymax)) { |
| 708 |
return 1; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
if (!isfinite(p->pscale_ratio) && |
| 715 |
compute_pscale_ratio(p, &s.bounding_polygon, &p->pscale_ratio)) { |
| 716 |
return 1; |
| 717 |
} |
| 718 |
|
| 719 |
kscale2 = (double) p->pscale_ratio * (double) p->pscale_ratio; |
| 720 |
|
| 721 |
pfo = nsig * p->pixel_fraction / 2.3548 / (double) p->pscale_ratio; |
| 722 |
pfo = CLAMP_ABOVE(pfo, 1.2 / (double) p->pscale_ratio); |
| 723 |
|
| 724 |
ac = 1.0 / (p->pixel_fraction * p->pixel_fraction); |
| 725 |
bv = compute_bit_value(p->uuid); |
| 726 |
|
| 727 |
gaussian_efac = (2.3548 * 2.3548) * kscale2 * ac / 2.0; |
| 728 |
gaussian_es = gaussian_efac / M_PI; |
| 729 |
|
| 730 |
p->nskip = (p->ymax - p->ymin) - (ymax - ymin); |
| 731 |
p->nmiss = p->nskip * (p->xmax - p->xmin); |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
get_dimensions(p->output_data, osize); |
| 736 |
|
| 737 |
if (ndata2 > 0) { |
| 738 |
iscale2 = p->iscale * p->iscale; |
| 739 |
if (!(d2 = (float *) malloc(p->ndata2 * sizeof(float)))) { |
| 740 |
driz_error_set(p->error, PyExc_MemoryError, "Memory allocation failed."); |
| 741 |
return 1; |
| 742 |
} |
| 743 |
if (!p->output_data2) { |
| 744 |
driz_error_set( |
| 745 |
p->error, PyExc_RuntimeError, |
| 746 |
"'output_data2' must be a valid pointer when " |
| 747 |
"'data2' is valid."); |
| 748 |
free(d2); |
| 749 |
return 1; |
| 750 |
} |
| 751 |
for (i = 0; i < ndata2; ++i) { |
| 752 |
if (!p->output_data2[i]) { |
| 753 |
driz_error_set( |
| 754 |
p->error, PyExc_RuntimeError, |
| 755 |
"Some arrays in 'output_data2' have invalid pointers."); |
| 756 |
free(d2); |
| 757 |
return 1; |
| 758 |
} |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
for (j = ymin; j <= ymax; ++j) { |
| 763 |
|
| 764 |
n = get_scanline_limits(&s, j, &xmin, &xmax); |
| 765 |
if (n == 1) { |
| 766 |
|
| 767 |
p->nskip += (ymax + 1 - j); |
| 768 |
p->nmiss += (ymax + 1 - j) * (p->xmax - p->xmin); |
| 769 |
break; |
| 770 |
} else if (n == 2 || n == 3) { |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
p->nmiss += (p->xmax - p->xmin); |
| 775 |
++p->nskip; |
| 776 |
continue; |
| 777 |
} else { |
| 778 |
|
| 779 |
p->nmiss += (p->xmax - p->xmin) - (xmax + 1 - xmin); |
| 780 |
} |
| 781 |
|
| 782 |
for (i = xmin; i <= xmax; ++i) { |
| 783 |
double ox, oy; |
| 784 |
|
| 785 |
if (map_pixel(p->pixmap, i, j, &ox, &oy)) { |
| 786 |
nhit = 0; |
| 787 |
|
| 788 |
} else { |
| 789 |
|
| 790 |
nxi = MAX(nintd(ox - pfo), 0); |
| 791 |
nxa = MIN(nintd(ox + pfo), osize[0] - 1); |
| 792 |
nyi = MAX(nintd(oy - pfo), 0); |
| 793 |
nya = MIN(nintd(oy + pfo), osize[1] - 1); |
| 794 |
|
| 795 |
nhit = 0; |
| 796 |
|
| 797 |
|
| 798 |
d = get_pixel(p->data, i, j) * p->iscale; |
| 799 |
for (k = 0; k < ndata2; ++k) { |
| 800 |
if (p->data2[k]) { |
| 801 |
d2[k] = get_pixel(p->data2[k], i, j) * iscale2; |
| 802 |
} else { |
| 803 |
d2[k] = 0.0f; |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
if (p->weights) { |
| 812 |
w = get_pixel(p->weights, i, j) * p->weight_scale; |
| 813 |
} else { |
| 814 |
w = 1.0; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
for (jj = nyi; jj <= nya; ++jj) { |
| 819 |
ddy = oy - (double) jj; |
| 820 |
for (ii = nxi; ii <= nxa; ++ii) { |
| 821 |
ddx = ox - (double) ii; |
| 822 |
|
| 823 |
r2 = ddx * ddx + ddy * ddy; |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
dover = gaussian_es * exp(-r2 * gaussian_efac); |
| 828 |
|
| 829 |
|
| 830 |
++nhit; |
| 831 |
|
| 832 |
dow = (float) dover * w; |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
if (p->output_context && dow > 0.0f) { |
| 837 |
set_bit(p->output_context, ii, jj, bv); |
| 838 |
} |
| 839 |
|
| 840 |
if (p->dq) { |
| 841 |
dqval = get_uint_pixel(p->dq, i, j); |
| 842 |
} |
| 843 |
|
| 844 |
if (update_data_var(p, ii, jj, d, dow, d2, dqval)) { |
| 845 |
free(d2); |
| 846 |
return 1; |
| 847 |
} |
| 848 |
} |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
if (nhit == 0) { |
| 854 |
++p->nmiss; |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
free(d2); |
| 860 |
return 0; |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
static int |
| 871 |
do_kernel_lanczos_var(struct driz_param_t *p) |
| 872 |
{ |
| 873 |
struct scanner s; |
| 874 |
integer_t bv, i, j, ii, jj, k, nxi, nxa, nyi, nya, nhit; |
| 875 |
integer_t osize[2]; |
| 876 |
float d, dow, *d2 = NULL, iscale2 = 1.0f; |
| 877 |
double pfo, xx, yy, w, dover; |
| 878 |
int kernel_order; |
| 879 |
size_t nlut; |
| 880 |
size_t ix, iy; |
| 881 |
double sdp; |
| 882 |
double *lut = NULL; |
| 883 |
int xmin, xmax, ymin, ymax, n, ndata2; |
| 884 |
unsigned int dqval = 0; |
| 885 |
|
| 886 |
if (fabs(p->pixel_fraction - 1.0) > 1.0e-5) { |
| 887 |
py_warning( |
| 888 |
NULL, "In lanczos kernel, pixel_fraction is ignored and " |
| 889 |
"assumed to be 1.0"); |
| 890 |
} |
| 891 |
|
| 892 |
ndata2 = p->ndata2; |
| 893 |
|
| 894 |
kernel_order = (p->kernel == kernel_lanczos2) ? 2 : 3; |
| 895 |
|
| 896 |
bv = compute_bit_value(p->uuid); |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
nlut = (size_t) ceil(kernel_order / lut_delta) + 1; |
| 901 |
if ((lut = malloc(nlut * sizeof(double))) == NULL) { |
| 902 |
driz_error_set_message(p->error, "Out of memory"); |
| 903 |
return driz_error_is_set(p->error); |
| 904 |
} |
| 905 |
create_lanczos_lut(kernel_order, nlut, lut_delta, lut); |
| 906 |
|
| 907 |
if (init_image_scanner(p, &s, &ymin, &ymax)) { |
| 908 |
return 1; |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
if (!isfinite(p->pscale_ratio) && |
| 915 |
compute_pscale_ratio(p, &s.bounding_polygon, &p->pscale_ratio)) { |
| 916 |
return 1; |
| 917 |
} |
| 918 |
|
| 919 |
pfo = (double) kernel_order / (double) p->pscale_ratio; |
| 920 |
sdp = p->pscale_ratio / lut_delta; |
| 921 |
|
| 922 |
p->nskip = (p->ymax - p->ymin) - (ymax - ymin); |
| 923 |
p->nmiss = p->nskip * (p->xmax - p->xmin); |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
get_dimensions(p->output_data, osize); |
| 928 |
|
| 929 |
if (ndata2 > 0) { |
| 930 |
iscale2 = p->iscale * p->iscale; |
| 931 |
|
| 932 |
if (!(d2 = (float *) malloc(p->ndata2 * sizeof(float)))) { |
| 933 |
driz_error_set(p->error, PyExc_MemoryError, "Memory allocation failed."); |
| 934 |
free(lut); |
| 935 |
return 1; |
| 936 |
} |
| 937 |
if (!p->output_data2) { |
| 938 |
driz_error_set( |
| 939 |
p->error, PyExc_RuntimeError, |
| 940 |
"'output_data2' must be a valid pointer when " |
| 941 |
"'data2' is valid."); |
| 942 |
free(lut); |
| 943 |
free(d2); |
| 944 |
return 1; |
| 945 |
} |
| 946 |
for (i = 0; i < ndata2; ++i) { |
| 947 |
if (!p->output_data2[i]) { |
| 948 |
driz_error_set( |
| 949 |
p->error, PyExc_RuntimeError, |
| 950 |
"Some arrays in 'output_data2' have invalid pointers."); |
| 951 |
free(lut); |
| 952 |
free(d2); |
| 953 |
return 1; |
| 954 |
} |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
for (j = ymin; j <= ymax; ++j) { |
| 959 |
|
| 960 |
n = get_scanline_limits(&s, j, &xmin, &xmax); |
| 961 |
if (n == 1) { |
| 962 |
|
| 963 |
p->nskip += (ymax + 1 - j); |
| 964 |
p->nmiss += (ymax + 1 - j) * (p->xmax - p->xmin); |
| 965 |
break; |
| 966 |
} else if (n == 2 || n == 3) { |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
p->nmiss += (p->xmax - p->xmin); |
| 971 |
++p->nskip; |
| 972 |
continue; |
| 973 |
} else { |
| 974 |
|
| 975 |
p->nmiss += (p->xmax - p->xmin) - (xmax + 1 - xmin); |
| 976 |
} |
| 977 |
|
| 978 |
for (i = xmin; i <= xmax; ++i) { |
| 979 |
if (map_pixel(p->pixmap, i, j, &xx, &yy)) { |
| 980 |
nhit = 0; |
| 981 |
|
| 982 |
} else { |
| 983 |
nxi = MAX((integer_t) floor(xx - pfo) + 1, 0); |
| 984 |
nxa = MIN((integer_t) floor(xx + pfo), osize[0] - 1); |
| 985 |
nyi = MAX((integer_t) floor(yy - pfo) + 1, 0); |
| 986 |
nya = MIN((integer_t) floor(yy + pfo), osize[1] - 1); |
| 987 |
|
| 988 |
nhit = 0; |
| 989 |
|
| 990 |
|
| 991 |
d = get_pixel(p->data, i, j) * p->iscale; |
| 992 |
for (k = 0; k < ndata2; ++k) { |
| 993 |
if (p->data2[k]) { |
| 994 |
d2[k] = get_pixel(p->data2[k], i, j) * iscale2; |
| 995 |
} else { |
| 996 |
d2[k] = 0.0f; |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
if (p->weights) { |
| 1005 |
w = get_pixel(p->weights, i, j) * p->weight_scale; |
| 1006 |
} else { |
| 1007 |
w = 1.0; |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
for (jj = nyi; jj <= nya; ++jj) { |
| 1012 |
for (ii = nxi; ii <= nxa; ++ii) { |
| 1013 |
|
| 1014 |
ix = nintd(fabs((xx - (double) ii) * sdp)); |
| 1015 |
iy = nintd(fabs((yy - (double) jj) * sdp)); |
| 1016 |
if (ix >= nlut || iy >= nlut) { |
| 1017 |
continue; |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
++nhit; |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
dover = lut[ix] * lut[iy]; |
| 1027 |
|
| 1028 |
dow = (float) (dover * w); |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
if (p->output_context && dow > 0.0f) { |
| 1033 |
set_bit(p->output_context, ii, jj, bv); |
| 1034 |
} |
| 1035 |
|
| 1036 |
if (p->dq) { |
| 1037 |
dqval = get_uint_pixel(p->dq, i, j); |
| 1038 |
} |
| 1039 |
|
| 1040 |
if (update_data_var(p, ii, jj, d, dow, d2, dqval)) { |
| 1041 |
free(d2); |
| 1042 |
free(lut); |
| 1043 |
return 1; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
|
| 1050 |
if (nhit == 0) { |
| 1051 |
++p->nmiss; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
free(d2); |
| 1057 |
free(lut); |
| 1058 |
|
| 1059 |
return 0; |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
static int |
| 1071 |
do_kernel_turbo_var(struct driz_param_t *p) |
| 1072 |
{ |
| 1073 |
struct scanner s; |
| 1074 |
integer_t bv, i, j, ii, jj, k, nhit, iis, iie, jjs, jje; |
| 1075 |
integer_t osize[2]; |
| 1076 |
float d, dow, *d2 = NULL, iscale2 = 1.0f; |
| 1077 |
double pfo, dover_scale, ac; |
| 1078 |
double xxi, xxa, yyi, yya, w, dover; |
| 1079 |
int xmin, xmax, ymin, ymax, n, ndata2; |
| 1080 |
unsigned int dqval = 0; |
| 1081 |
|
| 1082 |
ndata2 = p->ndata2; |
| 1083 |
|
| 1084 |
driz_log_message("starting do_kernel_turbo"); |
| 1085 |
bv = compute_bit_value(p->uuid); |
| 1086 |
ac = 1.0 / (p->pixel_fraction * p->pixel_fraction); |
| 1087 |
|
| 1088 |
if (init_image_scanner(p, &s, &ymin, &ymax)) { |
| 1089 |
return 1; |
| 1090 |
} |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
if (!isfinite(p->pscale_ratio) && |
| 1096 |
compute_pscale_ratio(p, &s.bounding_polygon, &p->pscale_ratio)) { |
| 1097 |
return 1; |
| 1098 |
} |
| 1099 |
|
| 1100 |
pfo = p->pixel_fraction / p->pscale_ratio / 2.0; |
| 1101 |
dover_scale = ac * (double) p->pscale_ratio * (double) p->pscale_ratio; |
| 1102 |
|
| 1103 |
p->nskip = (p->ymax - p->ymin) - (ymax - ymin); |
| 1104 |
p->nmiss = p->nskip * (p->xmax - p->xmin); |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
get_dimensions(p->output_data, osize); |
| 1109 |
|
| 1110 |
if (ndata2 > 0) { |
| 1111 |
iscale2 = p->iscale * p->iscale; |
| 1112 |
|
| 1113 |
if (!(d2 = (float *) malloc(ndata2 * sizeof(float)))) { |
| 1114 |
driz_error_set(p->error, PyExc_MemoryError, "Memory allocation failed."); |
| 1115 |
return 1; |
| 1116 |
} |
| 1117 |
if (!p->output_data2) { |
| 1118 |
driz_error_set( |
| 1119 |
p->error, PyExc_RuntimeError, |
| 1120 |
"'output_data2' must be a valid pointer when " |
| 1121 |
"'data2' is valid."); |
| 1122 |
return 1; |
| 1123 |
} |
| 1124 |
for (i = 0; i < ndata2; ++i) { |
| 1125 |
if (!p->output_data2[i]) { |
| 1126 |
driz_error_set( |
| 1127 |
p->error, PyExc_RuntimeError, |
| 1128 |
"Some arrays in 'output_data2' have invalid pointers."); |
| 1129 |
return 1; |
| 1130 |
} |
| 1131 |
} |
| 1132 |
} |
| 1133 |
|
| 1134 |
for (j = ymin; j <= ymax; ++j) { |
| 1135 |
|
| 1136 |
n = get_scanline_limits(&s, j, &xmin, &xmax); |
| 1137 |
|
| 1138 |
if (n == 1) { |
| 1139 |
|
| 1140 |
p->nskip += (ymax + 1 - j); |
| 1141 |
p->nmiss += (ymax + 1 - j) * (p->xmax - p->xmin); |
| 1142 |
break; |
| 1143 |
} else if (n == 2 || n == 3) { |
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
p->nmiss += (p->xmax - p->xmin); |
| 1148 |
++p->nskip; |
| 1149 |
continue; |
| 1150 |
} else { |
| 1151 |
|
| 1152 |
p->nmiss += (p->xmax - p->xmin) - (xmax + 1 - xmin); |
| 1153 |
} |
| 1154 |
|
| 1155 |
for (i = xmin; i <= xmax; ++i) { |
| 1156 |
double ox, oy; |
| 1157 |
|
| 1158 |
if (map_pixel(p->pixmap, i, j, &ox, &oy)) { |
| 1159 |
nhit = 0; |
| 1160 |
|
| 1161 |
} else { |
| 1162 |
|
| 1163 |
xxi = ox - pfo; |
| 1164 |
xxa = ox + pfo; |
| 1165 |
yyi = oy - pfo; |
| 1166 |
yya = oy + pfo; |
| 1167 |
|
| 1168 |
iis = MAX(nintd(xxi), 0); |
| 1169 |
iie = MIN(nintd(xxa), osize[0] - 1); |
| 1170 |
jjs = MAX(nintd(yyi), 0); |
| 1171 |
jje = MIN(nintd(yya), osize[1] - 1); |
| 1172 |
|
| 1173 |
nhit = 0; |
| 1174 |
|
| 1175 |
|
| 1176 |
d = get_pixel(p->data, i, j) * p->iscale; |
| 1177 |
for (k = 0; k < ndata2; ++k) { |
| 1178 |
if (p->data2[k]) { |
| 1179 |
d2[k] = get_pixel(p->data2[k], i, j) * iscale2; |
| 1180 |
} else { |
| 1181 |
d2[k] = 0.0f; |
| 1182 |
} |
| 1183 |
} |
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
if (p->weights) { |
| 1190 |
w = get_pixel(p->weights, i, j) * p->weight_scale; |
| 1191 |
} else { |
| 1192 |
w = 1.0; |
| 1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
for (jj = jjs; jj <= jje; ++jj) { |
| 1197 |
for (ii = iis; ii <= iie; ++ii) { |
| 1198 |
|
| 1199 |
|
| 1200 |
dover = over(ii, jj, xxi, xxa, yyi, yya); |
| 1201 |
|
| 1202 |
if (dover > 0.0) { |
| 1203 |
|
| 1204 |
dover *= dover_scale; |
| 1205 |
|
| 1206 |
|
| 1207 |
++nhit; |
| 1208 |
|
| 1209 |
dow = (float) (dover * w); |
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
if (p->output_context && dow > 0.0f) { |
| 1214 |
set_bit(p->output_context, ii, jj, bv); |
| 1215 |
} |
| 1216 |
|
| 1217 |
if (p->dq) { |
| 1218 |
dqval = get_uint_pixel(p->dq, i, j); |
| 1219 |
} |
| 1220 |
|
| 1221 |
if (update_data_var(p, ii, jj, d, dow, d2, dqval)) { |
| 1222 |
return 1; |
| 1223 |
} |
| 1224 |
} |
| 1225 |
} |
| 1226 |
} |
| 1227 |
} |
| 1228 |
|
| 1229 |
|
| 1230 |
if (nhit == 0) { |
| 1231 |
++p->nmiss; |
| 1232 |
} |
| 1233 |
} |
| 1234 |
} |
| 1235 |
|
| 1236 |
driz_log_message("ending do_kernel_turbo"); |
| 1237 |
return 0; |
| 1238 |
} |
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
int |
| 1251 |
do_kernel_square_var(struct driz_param_t *p) |
| 1252 |
{ |
| 1253 |
integer_t bv, i, j, ii, jj, k, min_ii, max_ii, min_jj, max_jj, nhit; |
| 1254 |
integer_t osize[2], mapsize[2]; |
| 1255 |
float d, dow, *d2 = NULL, iscale2 = 1.0f; |
| 1256 |
double dh, jaco, dover, w; |
| 1257 |
|
| 1258 |
double xin[4], yin[4], xout[4], yout[4]; |
| 1259 |
int ndata2; |
| 1260 |
unsigned int dqval = 0; |
| 1261 |
|
| 1262 |
ndata2 = p->ndata2; |
| 1263 |
|
| 1264 |
struct scanner s; |
| 1265 |
int xmin, xmax, ymin, ymax, n; |
| 1266 |
|
| 1267 |
driz_log_message("starting do_kernel_square"); |
| 1268 |
dh = 0.5 * p->pixel_fraction; |
| 1269 |
bv = compute_bit_value(p->uuid); |
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
if (init_image_scanner(p, &s, &ymin, &ymax)) { |
| 1275 |
return 1; |
| 1276 |
} |
| 1277 |
|
| 1278 |
p->nskip = (p->ymax - p->ymin) - (ymax - ymin); |
| 1279 |
p->nmiss = p->nskip * (p->xmax - p->xmin); |
| 1280 |
|
| 1281 |
|
| 1282 |
get_dimensions(p->output_data, osize); |
| 1283 |
get_dimensions(p->pixmap, mapsize); |
| 1284 |
|
| 1285 |
if (ndata2 > 0) { |
| 1286 |
iscale2 = p->iscale * p->iscale; |
| 1287 |
|
| 1288 |
if (!(d2 = (float *) malloc(p->ndata2 * sizeof(float)))) { |
| 1289 |
driz_error_set(p->error, PyExc_MemoryError, "Memory allocation failed."); |
| 1290 |
return 1; |
| 1291 |
} |
| 1292 |
if (!p->output_data2) { |
| 1293 |
driz_error_set( |
| 1294 |
p->error, PyExc_RuntimeError, |
| 1295 |
"'output_data2' must be a valid pointer when " |
| 1296 |
"'data2' is valid."); |
| 1297 |
free(d2); |
| 1298 |
return 1; |
| 1299 |
} |
| 1300 |
for (i = 0; i < ndata2; ++i) { |
| 1301 |
if (!p->output_data2[i]) { |
| 1302 |
driz_error_set( |
| 1303 |
p->error, PyExc_RuntimeError, |
| 1304 |
"Some arrays in 'output_data2' have invalid pointers."); |
| 1305 |
free(d2); |
| 1306 |
return 1; |
| 1307 |
} |
| 1308 |
} |
| 1309 |
} |
| 1310 |
|
| 1311 |
for (j = ymin; j <= ymax; ++j) { |
| 1312 |
|
| 1313 |
n = get_scanline_limits(&s, j, &xmin, &xmax); |
| 1314 |
if (n == 1) { |
| 1315 |
|
| 1316 |
p->nskip += (ymax + 1 - j); |
| 1317 |
p->nmiss += (ymax + 1 - j) * (p->xmax - p->xmin); |
| 1318 |
break; |
| 1319 |
} else if (n == 2 || n == 3) { |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
p->nmiss += (p->xmax - p->xmin); |
| 1324 |
++p->nskip; |
| 1325 |
continue; |
| 1326 |
} else { |
| 1327 |
|
| 1328 |
p->nmiss += (p->xmax - p->xmin) - (xmax + 1 - xmin); |
| 1329 |
} |
| 1330 |
|
| 1331 |
|
| 1332 |
yin[1] = yin[0] = (double) j + dh; |
| 1333 |
yin[3] = yin[2] = (double) j - dh; |
| 1334 |
|
| 1335 |
for (i = xmin; i <= xmax; ++i) { |
| 1336 |
nhit = 0; |
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
if (i > 0 && i < mapsize[0] - 2 && j > 0 && j < mapsize[1] - 2) { |
| 1353 |
if (interpolate_four_points( |
| 1354 |
p, i, j, dh, xout, xout + 1, xout + 2, xout + 3, yout, yout + 1, yout + 2, |
| 1355 |
yout + 3)) { |
| 1356 |
goto _miss; |
| 1357 |
} |
| 1358 |
} else { |
| 1359 |
xin[3] = xin[0] = (double) i - dh; |
| 1360 |
xin[2] = xin[1] = (double) i + dh; |
| 1361 |
for (ii = 0; ii < 4; ++ii) { |
| 1362 |
if (interpolate_point(p, xin[ii], yin[ii], xout + ii, yout + ii)) { |
| 1363 |
goto _miss; |
| 1364 |
} |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
jaco = 0.5f * ((xout[1] - xout[3]) * (yout[0] - yout[2]) - |
| 1377 |
(xout[0] - xout[2]) * (yout[1] - yout[3])); |
| 1378 |
|
| 1379 |
|
| 1380 |
d = get_pixel(p->data, i, j) * p->iscale; |
| 1381 |
for (k = 0; k < ndata2; ++k) { |
| 1382 |
if (p->data2[k]) { |
| 1383 |
d2[k] = get_pixel(p->data2[k], i, j) * iscale2; |
| 1384 |
} else { |
| 1385 |
d2[k] = 0.0f; |
| 1386 |
} |
| 1387 |
} |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
if (p->weights) { |
| 1393 |
w = get_pixel(p->weights, i, j) * p->weight_scale / jaco; |
| 1394 |
} else { |
| 1395 |
w = 1.0 / jaco; |
| 1396 |
} |
| 1397 |
|
| 1398 |
|
| 1399 |
min_jj = MAX(nintd(min_doubles(yout, 4)), 0); |
| 1400 |
max_jj = MIN(nintd(max_doubles(yout, 4)), osize[1] - 1); |
| 1401 |
min_ii = MAX(nintd(min_doubles(xout, 4)), 0); |
| 1402 |
max_ii = MIN(nintd(max_doubles(xout, 4)), osize[0] - 1); |
| 1403 |
|
| 1404 |
for (jj = min_jj; jj <= max_jj; ++jj) { |
| 1405 |
for (ii = min_ii; ii <= max_ii; ++ii) { |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
dover = boxer((double) ii, (double) jj, xout, yout); |
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
if (dover != 0.0) { |
| 1414 |
dow = (float) (dover * w); |
| 1415 |
|
| 1416 |
|
| 1417 |
++nhit; |
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
if (p->output_context && dow > 0.0f) { |
| 1422 |
set_bit(p->output_context, ii, jj, bv); |
| 1423 |
} |
| 1424 |
|
| 1425 |
if (p->dq) { |
| 1426 |
dqval = get_uint_pixel(p->dq, i, j); |
| 1427 |
} |
| 1428 |
|
| 1429 |
if (update_data_var(p, ii, jj, d, dow, d2, dqval)) { |
| 1430 |
free(d2); |
| 1431 |
return 1; |
| 1432 |
} |
| 1433 |
} |
| 1434 |
} |
| 1435 |
} |
| 1436 |
|
| 1437 |
|
| 1438 |
_miss: |
| 1439 |
if (nhit == 0) { |
| 1440 |
++p->nmiss; |
| 1441 |
} |
| 1442 |
} |
| 1443 |
} |
| 1444 |
|
| 1445 |
driz_log_message("ending do_kernel_square"); |
| 1446 |
free(d2); |
| 1447 |
return 0; |
| 1448 |
} |
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
static int |
| 1457 |
do_kernel_point(struct driz_param_t *p) |
| 1458 |
{ |
| 1459 |
struct scanner s; |
| 1460 |
integer_t i, j, ii, jj; |
| 1461 |
integer_t osize[2]; |
| 1462 |
float d, dow; |
| 1463 |
integer_t bv; |
| 1464 |
int xmin, xmax, ymin, ymax, n; |
| 1465 |
|
| 1466 |
bv = compute_bit_value(p->uuid); |
| 1467 |
|
| 1468 |
if (init_image_scanner(p, &s, &ymin, &ymax)) { |
| 1469 |
return 1; |
| 1470 |
} |
| 1471 |
|
| 1472 |
p->nskip = (p->ymax - p->ymin) - (ymax - ymin); |
| 1473 |
p->nmiss = p->nskip * (p->xmax - p->xmin); |
| 1474 |
|
| 1475 |
|
| 1476 |
get_dimensions(p->output_data, osize); |
| 1477 |
for (j = ymin; j <= ymax; ++j) { |
| 1478 |
|
| 1479 |
n = get_scanline_limits(&s, j, &xmin, &xmax); |
| 1480 |
if (n == 1) { |
| 1481 |
|
| 1482 |
p->nskip += (ymax + 1 - j); |
| 1483 |
p->nmiss += (ymax + 1 - j) * (p->xmax - p->xmin); |
| 1484 |
break; |
| 1485 |
} else if (n == 2 || n == 3) { |
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
p->nmiss += (p->xmax - p->xmin); |
| 1490 |
++p->nskip; |
| 1491 |
continue; |
| 1492 |
} else { |
| 1493 |
|
| 1494 |
p->nmiss += (p->xmax - p->xmin) - (xmax + 1 - xmin); |
| 1495 |
} |
| 1496 |
|
| 1497 |
for (i = xmin; i <= xmax; ++i) { |
| 1498 |
double ox, oy; |
| 1499 |
|
| 1500 |
if (map_pixel(p->pixmap, i, j, &ox, &oy)) { |
| 1501 |
++p->nmiss; |
| 1502 |
|
| 1503 |
} else { |
| 1504 |
ii = nintd(ox); |
| 1505 |
jj = nintd(oy); |
| 1506 |
|
| 1507 |
|
| 1508 |
if (ii < 0 || ii >= osize[0] || jj < 0 || jj >= osize[1]) { |
| 1509 |
++p->nmiss; |
| 1510 |
|
| 1511 |
} else { |
| 1512 |
|
| 1513 |
d = get_pixel(p->data, i, j) * p->iscale; |
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
if (p->weights) { |
| 1519 |
dow = get_pixel(p->weights, i, j) * p->weight_scale; |
| 1520 |
} else { |
| 1521 |
dow = 1.0f; |
| 1522 |
} |
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
if (p->output_context && dow > 0.0f) { |
| 1527 |
set_bit(p->output_context, ii, jj, bv); |
| 1528 |
} |
| 1529 |
|
| 1530 |
if (update_data(p, ii, jj, d, dow)) { |
| 1531 |
return 1; |
| 1532 |
} |
| 1533 |
} |
| 1534 |
} |
| 1535 |
} |
| 1536 |
} |
| 1537 |
|
| 1538 |
return 0; |
| 1539 |
} |
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
static int |
| 1549 |
do_kernel_gaussian(struct driz_param_t *p) |
| 1550 |
{ |
| 1551 |
struct scanner s; |
| 1552 |
integer_t bv, i, j, ii, jj, nxi, nxa, nyi, nya, nhit; |
| 1553 |
integer_t osize[2]; |
| 1554 |
float d, dow; |
| 1555 |
double gaussian_efac, gaussian_es; |
| 1556 |
double pfo, ac, kscale2, w, ddx, ddy, r2, dover; |
| 1557 |
const double nsig = 2.5; |
| 1558 |
int xmin, xmax, ymin, ymax, n; |
| 1559 |
|
| 1560 |
if (init_image_scanner(p, &s, &ymin, &ymax)) { |
| 1561 |
return 1; |
| 1562 |
} |
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
if (!isfinite(p->pscale_ratio) && |
| 1568 |
compute_pscale_ratio(p, &s.bounding_polygon, &p->pscale_ratio)) { |
| 1569 |
return 1; |
| 1570 |
} |
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
pfo = nsig * p->pixel_fraction / 2.3548 / (double) p->pscale_ratio; |
| 1577 |
pfo = CLAMP_ABOVE(pfo, 1.2 / (double) p->pscale_ratio); |
| 1578 |
|
| 1579 |
ac = 1.0 / (p->pixel_fraction * p->pixel_fraction); |
| 1580 |
kscale2 = (double) p->pscale_ratio * (double) p->pscale_ratio; |
| 1581 |
bv = compute_bit_value(p->uuid); |
| 1582 |
|
| 1583 |
gaussian_efac = (2.3548 * 2.3548) * kscale2 * ac / 2.0; |
| 1584 |
gaussian_es = gaussian_efac / M_PI; |
| 1585 |
|
| 1586 |
p->nskip = (p->ymax - p->ymin) - (ymax - ymin); |
| 1587 |
p->nmiss = p->nskip * (p->xmax - p->xmin); |
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
get_dimensions(p->output_data, osize); |
| 1592 |
for (j = ymin; j <= ymax; ++j) { |
| 1593 |
|
| 1594 |
n = get_scanline_limits(&s, j, &xmin, &xmax); |
| 1595 |
if (n == 1) { |
| 1596 |
|
| 1597 |
p->nskip += (ymax + 1 - j); |
| 1598 |
p->nmiss += (ymax + 1 - j) * (p->xmax - p->xmin); |
| 1599 |
break; |
| 1600 |
} else if (n == 2 || n == 3) { |
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
p->nmiss += (p->xmax - p->xmin); |
| 1605 |
++p->nskip; |
| 1606 |
continue; |
| 1607 |
} else { |
| 1608 |
|
| 1609 |
p->nmiss += (p->xmax - p->xmin) - (xmax + 1 - xmin); |
| 1610 |
} |
| 1611 |
|
| 1612 |
for (i = xmin; i <= xmax; ++i) { |
| 1613 |
double ox, oy; |
| 1614 |
|
| 1615 |
if (map_pixel(p->pixmap, i, j, &ox, &oy)) { |
| 1616 |
nhit = 0; |
| 1617 |
|
| 1618 |
} else { |
| 1619 |
|
| 1620 |
nxi = MAX(nintd(ox - pfo), 0); |
| 1621 |
nxa = MIN(nintd(ox + pfo), osize[0] - 1); |
| 1622 |
nyi = MAX(nintd(oy - pfo), 0); |
| 1623 |
nya = MIN(nintd(oy + pfo), osize[1] - 1); |
| 1624 |
|
| 1625 |
nhit = 0; |
| 1626 |
|
| 1627 |
|
| 1628 |
d = get_pixel(p->data, i, j) * p->iscale; |
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
if (p->weights) { |
| 1635 |
w = get_pixel(p->weights, i, j) * p->weight_scale; |
| 1636 |
} else { |
| 1637 |
w = 1.0; |
| 1638 |
} |
| 1639 |
|
| 1640 |
|
| 1641 |
for (jj = nyi; jj <= nya; ++jj) { |
| 1642 |
ddy = oy - (double) jj; |
| 1643 |
for (ii = nxi; ii <= nxa; ++ii) { |
| 1644 |
ddx = ox - (double) ii; |
| 1645 |
|
| 1646 |
r2 = ddx * ddx + ddy * ddy; |
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
dover = gaussian_es * exp(-r2 * gaussian_efac); |
| 1651 |
|
| 1652 |
|
| 1653 |
++nhit; |
| 1654 |
|
| 1655 |
dow = (float) dover * w; |
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
if (p->output_context && dow > 0.0f) { |
| 1660 |
set_bit(p->output_context, ii, jj, bv); |
| 1661 |
} |
| 1662 |
|
| 1663 |
if (update_data(p, ii, jj, d, dow)) { |
| 1664 |
return 1; |
| 1665 |
} |
| 1666 |
} |
| 1667 |
} |
| 1668 |
} |
| 1669 |
|
| 1670 |
|
| 1671 |
if (nhit == 0) { |
| 1672 |
++p->nmiss; |
| 1673 |
} |
| 1674 |
} |
| 1675 |
} |
| 1676 |
|
| 1677 |
return 0; |
| 1678 |
} |
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
static int |
| 1688 |
do_kernel_lanczos(struct driz_param_t *p) |
| 1689 |
{ |
| 1690 |
struct scanner s; |
| 1691 |
integer_t bv, i, j, ii, jj, nxi, nxa, nyi, nya, nhit; |
| 1692 |
integer_t osize[2]; |
| 1693 |
float d, dow; |
| 1694 |
double pfo, xx, yy, w, dover; |
| 1695 |
int kernel_order; |
| 1696 |
size_t nlut; |
| 1697 |
size_t ix, iy; |
| 1698 |
double sdp; |
| 1699 |
double *lut = NULL; |
| 1700 |
int xmin, xmax, ymin, ymax, n; |
| 1701 |
|
| 1702 |
if (fabs(p->pixel_fraction - 1.0) > 1.0e-5) { |
| 1703 |
py_warning( |
| 1704 |
NULL, "In lanczos kernel, pixel_fraction is ignored and " |
| 1705 |
"assumed to be 1.0"); |
| 1706 |
} |
| 1707 |
|
| 1708 |
kernel_order = (p->kernel == kernel_lanczos2) ? 2 : 3; |
| 1709 |
|
| 1710 |
bv = compute_bit_value(p->uuid); |
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
nlut = (size_t) ceil(kernel_order / lut_delta) + 1; |
| 1715 |
if ((lut = malloc(nlut * sizeof(double))) == NULL) { |
| 1716 |
driz_error_set_message(p->error, "Out of memory"); |
| 1717 |
return driz_error_is_set(p->error); |
| 1718 |
} |
| 1719 |
create_lanczos_lut(kernel_order, nlut, lut_delta, lut); |
| 1720 |
|
| 1721 |
if (init_image_scanner(p, &s, &ymin, &ymax)) { |
| 1722 |
return 1; |
| 1723 |
} |
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
if (!isfinite(p->pscale_ratio) && |
| 1729 |
compute_pscale_ratio(p, &s.bounding_polygon, &p->pscale_ratio)) { |
| 1730 |
return 1; |
| 1731 |
} |
| 1732 |
|
| 1733 |
pfo = (double) kernel_order / (double) p->pscale_ratio; |
| 1734 |
sdp = (double) p->pscale_ratio / lut_delta; |
| 1735 |
|
| 1736 |
p->nskip = (p->ymax - p->ymin) - (ymax - ymin); |
| 1737 |
p->nmiss = p->nskip * (p->xmax - p->xmin); |
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
get_dimensions(p->output_data, osize); |
| 1742 |
for (j = ymin; j <= ymax; ++j) { |
| 1743 |
|
| 1744 |
n = get_scanline_limits(&s, j, &xmin, &xmax); |
| 1745 |
if (n == 1) { |
| 1746 |
|
| 1747 |
p->nskip += (ymax + 1 - j); |
| 1748 |
p->nmiss += (ymax + 1 - j) * (p->xmax - p->xmin); |
| 1749 |
break; |
| 1750 |
} else if (n == 2 || n == 3) { |
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
p->nmiss += (p->xmax - p->xmin); |
| 1755 |
++p->nskip; |
| 1756 |
continue; |
| 1757 |
} else { |
| 1758 |
|
| 1759 |
p->nmiss += (p->xmax - p->xmin) - (xmax + 1 - xmin); |
| 1760 |
} |
| 1761 |
|
| 1762 |
for (i = xmin; i <= xmax; ++i) { |
| 1763 |
if (map_pixel(p->pixmap, i, j, &xx, &yy)) { |
| 1764 |
nhit = 0; |
| 1765 |
|
| 1766 |
} else { |
| 1767 |
nxi = MAX((integer_t) floor(xx - pfo) + 1, 0); |
| 1768 |
nxa = MIN((integer_t) floor(xx + pfo), osize[0] - 1); |
| 1769 |
nyi = MAX((integer_t) floor(yy - pfo) + 1, 0); |
| 1770 |
nya = MIN((integer_t) floor(yy + pfo), osize[1] - 1); |
| 1771 |
|
| 1772 |
nhit = 0; |
| 1773 |
|
| 1774 |
|
| 1775 |
d = get_pixel(p->data, i, j) * p->iscale; |
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
if (p->weights) { |
| 1782 |
w = get_pixel(p->weights, i, j) * p->weight_scale; |
| 1783 |
} else { |
| 1784 |
w = 1.0; |
| 1785 |
} |
| 1786 |
|
| 1787 |
|
| 1788 |
for (jj = nyi; jj <= nya; ++jj) { |
| 1789 |
for (ii = nxi; ii <= nxa; ++ii) { |
| 1790 |
|
| 1791 |
ix = nintd(fabs((xx - (double) ii) * sdp)); |
| 1792 |
iy = nintd(fabs((yy - (double) jj) * sdp)); |
| 1793 |
if (ix >= nlut || iy >= nlut) { |
| 1794 |
continue; |
| 1795 |
} |
| 1796 |
|
| 1797 |
|
| 1798 |
++nhit; |
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
dover = lut[ix] * lut[iy]; |
| 1804 |
|
| 1805 |
dow = (float) (dover * w); |
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
if (p->output_context && dow > 0.0f) { |
| 1810 |
set_bit(p->output_context, ii, jj, bv); |
| 1811 |
} |
| 1812 |
|
| 1813 |
if (update_data(p, ii, jj, d, dow)) { |
| 1814 |
free(lut); |
| 1815 |
return 1; |
| 1816 |
} |
| 1817 |
} |
| 1818 |
} |
| 1819 |
} |
| 1820 |
|
| 1821 |
|
| 1822 |
if (nhit == 0) { |
| 1823 |
++p->nmiss; |
| 1824 |
} |
| 1825 |
} |
| 1826 |
} |
| 1827 |
|
| 1828 |
free(lut); |
| 1829 |
return 0; |
| 1830 |
} |
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
static int |
| 1841 |
do_kernel_turbo(struct driz_param_t *p) |
| 1842 |
{ |
| 1843 |
struct scanner s; |
| 1844 |
integer_t bv, i, j, ii, jj, nhit, iis, iie, jjs, jje; |
| 1845 |
integer_t osize[2]; |
| 1846 |
float d, dow; |
| 1847 |
double pfo, ac; |
| 1848 |
double xxi, xxa, yyi, yya, w, dover, dover_scale; |
| 1849 |
int xmin, xmax, ymin, ymax, n; |
| 1850 |
|
| 1851 |
driz_log_message("starting do_kernel_turbo"); |
| 1852 |
bv = compute_bit_value(p->uuid); |
| 1853 |
ac = 1.0 / (p->pixel_fraction * p->pixel_fraction); |
| 1854 |
|
| 1855 |
if (init_image_scanner(p, &s, &ymin, &ymax)) { |
| 1856 |
return 1; |
| 1857 |
} |
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
if (!isfinite(p->pscale_ratio) && |
| 1863 |
compute_pscale_ratio(p, &s.bounding_polygon, &p->pscale_ratio)) { |
| 1864 |
return 1; |
| 1865 |
} |
| 1866 |
|
| 1867 |
pfo = p->pixel_fraction / p->pscale_ratio / 2.0; |
| 1868 |
dover_scale = ac * (double) p->pscale_ratio * (double) p->pscale_ratio; |
| 1869 |
|
| 1870 |
p->nskip = (p->ymax - p->ymin) - (ymax - ymin); |
| 1871 |
p->nmiss = p->nskip * (p->xmax - p->xmin); |
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
get_dimensions(p->output_data, osize); |
| 1876 |
for (j = ymin; j <= ymax; ++j) { |
| 1877 |
|
| 1878 |
n = get_scanline_limits(&s, j, &xmin, &xmax); |
| 1879 |
|
| 1880 |
if (n == 1) { |
| 1881 |
|
| 1882 |
p->nskip += (ymax + 1 - j); |
| 1883 |
p->nmiss += (ymax + 1 - j) * (p->xmax - p->xmin); |
| 1884 |
break; |
| 1885 |
} else if (n == 2 || n == 3) { |
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
p->nmiss += (p->xmax - p->xmin); |
| 1890 |
++p->nskip; |
| 1891 |
continue; |
| 1892 |
} else { |
| 1893 |
|
| 1894 |
p->nmiss += (p->xmax - p->xmin) - (xmax + 1 - xmin); |
| 1895 |
} |
| 1896 |
|
| 1897 |
for (i = xmin; i <= xmax; ++i) { |
| 1898 |
double ox, oy; |
| 1899 |
|
| 1900 |
if (map_pixel(p->pixmap, i, j, &ox, &oy)) { |
| 1901 |
nhit = 0; |
| 1902 |
|
| 1903 |
} else { |
| 1904 |
|
| 1905 |
xxi = ox - pfo; |
| 1906 |
xxa = ox + pfo; |
| 1907 |
yyi = oy - pfo; |
| 1908 |
yya = oy + pfo; |
| 1909 |
|
| 1910 |
iis = MAX(nintd(xxi), 0); |
| 1911 |
iie = MIN(nintd(xxa), osize[0] - 1); |
| 1912 |
jjs = MAX(nintd(yyi), 0); |
| 1913 |
jje = MIN(nintd(yya), osize[1] - 1); |
| 1914 |
|
| 1915 |
nhit = 0; |
| 1916 |
|
| 1917 |
|
| 1918 |
d = get_pixel(p->data, i, j) * p->iscale; |
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
if (p->weights) { |
| 1925 |
w = get_pixel(p->weights, i, j) * p->weight_scale; |
| 1926 |
} else { |
| 1927 |
w = 1.0; |
| 1928 |
} |
| 1929 |
|
| 1930 |
|
| 1931 |
for (jj = jjs; jj <= jje; ++jj) { |
| 1932 |
for (ii = iis; ii <= iie; ++ii) { |
| 1933 |
|
| 1934 |
|
| 1935 |
dover = over(ii, jj, xxi, xxa, yyi, yya); |
| 1936 |
|
| 1937 |
if (dover > 0.0) { |
| 1938 |
|
| 1939 |
dover *= dover_scale; |
| 1940 |
|
| 1941 |
|
| 1942 |
++nhit; |
| 1943 |
|
| 1944 |
dow = (float) (dover * w); |
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
if (p->output_context && dow > 0.0f) { |
| 1949 |
set_bit(p->output_context, ii, jj, bv); |
| 1950 |
} |
| 1951 |
|
| 1952 |
if (update_data(p, ii, jj, d, dow)) { |
| 1953 |
return 1; |
| 1954 |
} |
| 1955 |
} |
| 1956 |
} |
| 1957 |
} |
| 1958 |
} |
| 1959 |
|
| 1960 |
|
| 1961 |
if (nhit == 0) { |
| 1962 |
++p->nmiss; |
| 1963 |
} |
| 1964 |
} |
| 1965 |
} |
| 1966 |
|
| 1967 |
driz_log_message("ending do_kernel_turbo"); |
| 1968 |
return 0; |
| 1969 |
} |
| 1970 |
|
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
int |
| 1982 |
do_kernel_square(struct driz_param_t *p) |
| 1983 |
{ |
| 1984 |
integer_t bv, i, j, ii, jj, min_ii, max_ii, min_jj, max_jj, nhit; |
| 1985 |
integer_t osize[2], mapsize[2]; |
| 1986 |
float d, dow; |
| 1987 |
double dh, jaco, dover, w; |
| 1988 |
double xin[4], yin[4], xout[4], yout[4]; |
| 1989 |
|
| 1990 |
struct scanner s; |
| 1991 |
int xmin, xmax, ymin, ymax, n; |
| 1992 |
|
| 1993 |
driz_log_message("starting do_kernel_square"); |
| 1994 |
dh = 0.5 * p->pixel_fraction; |
| 1995 |
bv = compute_bit_value(p->uuid); |
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
if (init_image_scanner(p, &s, &ymin, &ymax)) { |
| 2001 |
return 1; |
| 2002 |
} |
| 2003 |
|
| 2004 |
p->nskip = (p->ymax - p->ymin) - (ymax - ymin); |
| 2005 |
p->nmiss = p->nskip * (p->xmax - p->xmin); |
| 2006 |
|
| 2007 |
|
| 2008 |
get_dimensions(p->output_data, osize); |
| 2009 |
get_dimensions(p->pixmap, mapsize); |
| 2010 |
|
| 2011 |
for (j = ymin; j <= ymax; ++j) { |
| 2012 |
|
| 2013 |
n = get_scanline_limits(&s, j, &xmin, &xmax); |
| 2014 |
if (n == 1) { |
| 2015 |
|
| 2016 |
p->nskip += (ymax + 1 - j); |
| 2017 |
p->nmiss += (ymax + 1 - j) * (p->xmax - p->xmin); |
| 2018 |
break; |
| 2019 |
} else if (n == 2 || n == 3) { |
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
p->nmiss += (p->xmax - p->xmin); |
| 2024 |
++p->nskip; |
| 2025 |
continue; |
| 2026 |
} else { |
| 2027 |
|
| 2028 |
p->nmiss += (p->xmax - p->xmin) - (xmax + 1 - xmin); |
| 2029 |
} |
| 2030 |
|
| 2031 |
|
| 2032 |
yin[1] = yin[0] = (double) j + dh; |
| 2033 |
yin[3] = yin[2] = (double) j - dh; |
| 2034 |
|
| 2035 |
for (i = xmin; i <= xmax; ++i) { |
| 2036 |
nhit = 0; |
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
|
| 2052 |
if (i > 0 && i < mapsize[0] - 2 && j > 0 && j < mapsize[1] - 2) { |
| 2053 |
if (interpolate_four_points( |
| 2054 |
p, i, j, dh, xout, xout + 1, xout + 2, xout + 3, yout, yout + 1, yout + 2, |
| 2055 |
yout + 3)) { |
| 2056 |
goto _miss; |
| 2057 |
} |
| 2058 |
} else { |
| 2059 |
xin[3] = xin[0] = (double) i - dh; |
| 2060 |
xin[2] = xin[1] = (double) i + dh; |
| 2061 |
for (ii = 0; ii < 4; ++ii) { |
| 2062 |
if (interpolate_point(p, xin[ii], yin[ii], xout + ii, yout + ii)) { |
| 2063 |
goto _miss; |
| 2064 |
} |
| 2065 |
} |
| 2066 |
} |
| 2067 |
|
| 2068 |
|
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
jaco = 0.5f * ((xout[1] - xout[3]) * (yout[0] - yout[2]) - |
| 2077 |
(xout[0] - xout[2]) * (yout[1] - yout[3])); |
| 2078 |
|
| 2079 |
|
| 2080 |
d = get_pixel(p->data, i, j) * p->iscale; |
| 2081 |
|
| 2082 |
|
| 2083 |
|
| 2084 |
|
| 2085 |
if (p->weights) { |
| 2086 |
w = get_pixel(p->weights, i, j) * p->weight_scale / jaco; |
| 2087 |
} else { |
| 2088 |
w = 1.0 / jaco; |
| 2089 |
} |
| 2090 |
|
| 2091 |
|
| 2092 |
min_jj = MAX(nintd(min_doubles(yout, 4)), 0); |
| 2093 |
max_jj = MIN(nintd(max_doubles(yout, 4)), osize[1] - 1); |
| 2094 |
min_ii = MAX(nintd(min_doubles(xout, 4)), 0); |
| 2095 |
max_ii = MIN(nintd(max_doubles(xout, 4)), osize[0] - 1); |
| 2096 |
|
| 2097 |
for (jj = min_jj; jj <= max_jj; ++jj) { |
| 2098 |
for (ii = min_ii; ii <= max_ii; ++ii) { |
| 2099 |
|
| 2100 |
|
| 2101 |
|
| 2102 |
dover = boxer((double) ii, (double) jj, xout, yout); |
| 2103 |
|
| 2104 |
|
| 2105 |
|
| 2106 |
if (dover != 0.0) { |
| 2107 |
dow = (float) (dover * w); |
| 2108 |
|
| 2109 |
|
| 2110 |
++nhit; |
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
if (p->output_context && dow > 0.0f) { |
| 2115 |
set_bit(p->output_context, ii, jj, bv); |
| 2116 |
} |
| 2117 |
|
| 2118 |
if (update_data(p, ii, jj, d, dow)) { |
| 2119 |
return 1; |
| 2120 |
} |
| 2121 |
} |
| 2122 |
} |
| 2123 |
} |
| 2124 |
|
| 2125 |
|
| 2126 |
_miss: |
| 2127 |
if (nhit == 0) { |
| 2128 |
++p->nmiss; |
| 2129 |
} |
| 2130 |
} |
| 2131 |
} |
| 2132 |
|
| 2133 |
driz_log_message("ending do_kernel_square"); |
| 2134 |
return 0; |
| 2135 |
} |
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
|
| 2141 |
|
| 2142 |
|
| 2143 |
|
| 2144 |
static kernel_handler_t kernel_handler_map[] = {do_kernel_square, do_kernel_gaussian, |
| 2145 |
do_kernel_point, do_kernel_turbo, |
| 2146 |
do_kernel_lanczos, do_kernel_lanczos}; |
| 2147 |
|
| 2148 |
static kernel_handler_t kernel_var_handler_map[] = {do_kernel_square_var, do_kernel_gaussian_var, |
| 2149 |
do_kernel_point_var, do_kernel_turbo_var, |
| 2150 |
do_kernel_lanczos_var, do_kernel_lanczos_var}; |
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
int |
| 2160 |
dobox(struct driz_param_t *p) |
| 2161 |
{ |
| 2162 |
kernel_handler_t kernel_handler = NULL; |
| 2163 |
driz_log_message("starting dobox"); |
| 2164 |
|
| 2165 |
|
| 2166 |
if (p->kernel < kernel_LAST) { |
| 2167 |
if (p->ndata2 > 0 || p->dq != NULL) { |
| 2168 |
kernel_handler = kernel_var_handler_map[p->kernel]; |
| 2169 |
} else { |
| 2170 |
kernel_handler = kernel_handler_map[p->kernel]; |
| 2171 |
} |
| 2172 |
|
| 2173 |
if (kernel_handler != NULL) { |
| 2174 |
kernel_handler(p); |
| 2175 |
} |
| 2176 |
} |
| 2177 |
|
| 2178 |
if (kernel_handler == NULL) { |
| 2179 |
driz_error_set_message(p->error, "Invalid kernel type"); |
| 2180 |
} |
| 2181 |
|
| 2182 |
driz_log_message("ending dobox"); |
| 2183 |
return driz_error_is_set(p->error); |
| 2184 |
} |