| 1 |
#ifndef CDRIZZLEUTIL_H |
| 2 |
#define CDRIZZLEUTIL_H |
| 3 |
#include "driz_portability.h" |
| 4 |
|
| 5 |
#include <assert.h> |
| 6 |
#include <errno.h> |
| 7 |
#if __STDC_VERSION__ >= 199901L |
| 8 |
#include <stdint.h> |
| 9 |
#endif |
| 10 |
#include <stdlib.h> |
| 11 |
|
| 12 |
#include <Python.h> |
| 13 |
#ifndef NPY_NO_DEPRECATED_API |
| 14 |
#define NPY_NO_DEPRECATED_API NPY_1_21_API_VERSION |
| 15 |
#endif |
| 16 |
#pragma GCC diagnostic push |
| 17 |
#pragma GCC diagnostic ignored "-Wpedantic" |
| 18 |
#include <numpy/arrayobject.h> |
| 19 |
#pragma GCC diagnostic pop |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#define MAX_DRIZ_ERROR_LEN 512 |
| 25 |
|
| 26 |
struct driz_error_t { |
| 27 |
PyObject *type; |
| 28 |
char last_message[MAX_DRIZ_ERROR_LEN]; |
| 29 |
}; |
| 30 |
|
| 31 |
void |
| 32 |
driz_error_init(struct driz_error_t *error); |
| 33 |
int |
| 34 |
driz_error_check(struct driz_error_t *error, const char *message, int test); |
| 35 |
void |
| 36 |
driz_error_set_message(struct driz_error_t *error, const char *message); |
| 37 |
void |
| 38 |
driz_error_format_message(struct driz_error_t *error, const char *format, ...); |
| 39 |
void |
| 40 |
driz_error_set(struct driz_error_t *error, PyObject *type, const char *format, ...); |
| 41 |
const char * |
| 42 |
driz_error_get_message(struct driz_error_t *error); |
| 43 |
int |
| 44 |
driz_error_is_set(struct driz_error_t *error); |
| 45 |
void |
| 46 |
driz_error_unset(struct driz_error_t *error); |
| 47 |
|
| 48 |
int |
| 49 |
py_warning(PyObject *warning_type, const char *format, ...); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#if !defined(MIN) |
| 55 |
#define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 56 |
#endif |
| 57 |
|
| 58 |
#if !defined(MAX) |
| 59 |
#define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
| 60 |
#endif |
| 61 |
|
| 62 |
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) |
| 63 |
#define CLAMP_ABOVE(x, low) (((x) < low) ? (low) : (x)) |
| 64 |
#define CLAMP_BELOW(x, high) (((x) > high) ? (high) : (x)) |
| 65 |
|
| 66 |
#define MAX_DOUBLE 1.7976931348623158e+308 |
| 67 |
#define MIN_DOUBLE 2.2250738585072014e-308 |
| 68 |
|
| 69 |
#define MAX_COEFFS 128 |
| 70 |
#define COEFF_OFFSET 100 |
| 71 |
|
| 72 |
#undef TRUE |
| 73 |
#define TRUE 1 |
| 74 |
|
| 75 |
#undef FALSE |
| 76 |
#define FALSE 0 |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
typedef int integer_t; |
| 82 |
#if __STDC_VERSION__ >= 199901L |
| 83 |
typedef int_fast8_t bool_t; |
| 84 |
#else |
| 85 |
typedef unsigned char bool_t; |
| 86 |
#endif |
| 87 |
|
| 88 |
enum e_kernel_t { |
| 89 |
kernel_square, |
| 90 |
kernel_gaussian, |
| 91 |
kernel_point, |
| 92 |
kernel_turbo, |
| 93 |
kernel_lanczos2, |
| 94 |
kernel_lanczos3, |
| 95 |
kernel_LAST |
| 96 |
}; |
| 97 |
|
| 98 |
enum e_unit_t { unit_counts, unit_cps }; |
| 99 |
|
| 100 |
enum e_interp_t { |
| 101 |
interp_nearest, |
| 102 |
interp_bilinear, |
| 103 |
interp_poly3, |
| 104 |
interp_poly5, |
| 105 |
interp_spline3, |
| 106 |
interp_sinc, |
| 107 |
interp_lsinc, |
| 108 |
interp_lanczos3, |
| 109 |
interp_lanczos5, |
| 110 |
interp_LAST |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
struct lanczos_param_t { |
| 115 |
size_t nlut; |
| 116 |
double *lut; |
| 117 |
double sdp; |
| 118 |
integer_t nbox; |
| 119 |
double space; |
| 120 |
}; |
| 121 |
|
| 122 |
struct driz_param_t { |
| 123 |
|
| 124 |
enum e_kernel_t kernel; |
| 125 |
double pixel_fraction; |
| 126 |
float exposure_time; |
| 127 |
float weight_scale; |
| 128 |
float fill_value; |
| 129 |
float fill_value2; |
| 130 |
bool_t do_fill; |
| 131 |
bool_t do_fill2; |
| 132 |
enum e_unit_t in_units; |
| 133 |
enum e_unit_t out_units; |
| 134 |
integer_t uuid; |
| 135 |
|
| 136 |
|
| 137 |
integer_t in_nx; |
| 138 |
integer_t in_ny; |
| 139 |
|
| 140 |
|
| 141 |
integer_t out_nx; |
| 142 |
integer_t out_ny; |
| 143 |
|
| 144 |
|
| 145 |
float iscale; |
| 146 |
float pscale_ratio; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
integer_t xmin; |
| 151 |
integer_t xmax; |
| 152 |
integer_t ymin; |
| 153 |
integer_t ymax; |
| 154 |
|
| 155 |
|
| 156 |
enum e_interp_t interpolation; |
| 157 |
float ef; |
| 158 |
float misval; |
| 159 |
float sinscl; |
| 160 |
|
| 161 |
|
| 162 |
PyArrayObject *data; |
| 163 |
PyArrayObject **data2; |
| 164 |
PyArrayObject *dq; |
| 165 |
PyArrayObject *weights; |
| 166 |
PyArrayObject *pixmap; |
| 167 |
|
| 168 |
|
| 169 |
PyArrayObject *output_data; |
| 170 |
PyArrayObject **output_data2; |
| 171 |
PyArrayObject *output_counts; |
| 172 |
PyArrayObject *output_context; |
| 173 |
PyArrayObject *output_dq; |
| 174 |
|
| 175 |
integer_t ndata2; |
| 176 |
|
| 177 |
|
| 178 |
integer_t nmiss; |
| 179 |
integer_t nskip; |
| 180 |
struct driz_error_t *error; |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
void |
| 190 |
driz_param_init(struct driz_param_t *p); |
| 191 |
|
| 192 |
void |
| 193 |
driz_param_dump(struct driz_param_t *p); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
#ifdef LOGGING |
| 199 |
extern FILE *driz_log_handle; |
| 200 |
|
| 201 |
static inline_macro FILE * |
| 202 |
driz_log_init(FILE *handle) |
| 203 |
{ |
| 204 |
const char *dirs[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"}; |
| 205 |
int i; |
| 206 |
char *p; |
| 207 |
char buf[1024]; |
| 208 |
#ifdef _WIN32 |
| 209 |
const char dirsep = '\\'; |
| 210 |
#else |
| 211 |
const char dirsep = '/'; |
| 212 |
#endif |
| 213 |
|
| 214 |
for (i = 0; i < 4; ++i) { |
| 215 |
p = getenv(dirs[i]); |
| 216 |
if (p) { |
| 217 |
sprintf(buf, "%s%cdrizzle.log", p, dirsep); |
| 218 |
break; |
| 219 |
} |
| 220 |
} |
| 221 |
if (!p) { |
| 222 |
sprintf(buf, "drizzle.log"); |
| 223 |
} |
| 224 |
|
| 225 |
handle = fopen(buf, "a"); |
| 226 |
if (handle) { |
| 227 |
setbuf(handle, 0); |
| 228 |
} |
| 229 |
return handle; |
| 230 |
} |
| 231 |
|
| 232 |
static inline_macro int |
| 233 |
driz_log_close(FILE *handle) |
| 234 |
{ |
| 235 |
if (handle) { |
| 236 |
return fclose(handle); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
static inline_macro int |
| 241 |
driz_log_message(const char *message) |
| 242 |
{ |
| 243 |
if (!driz_log_handle) { |
| 244 |
driz_log_handle = driz_log_init(driz_log_handle); |
| 245 |
if (!driz_log_handle) { |
| 246 |
return 1; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
fputs(message, driz_log_handle); |
| 251 |
fputs("\n", driz_log_handle); |
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
#else |
| 256 |
static inline_macro void * |
| 257 |
driz_log_idem(void *ptr) |
| 258 |
{ |
| 259 |
return ptr; |
| 260 |
} |
| 261 |
|
| 262 |
#define driz_log_init(handle) driz_log_idem(handle) |
| 263 |
#define driz_log_close(handle) driz_log_idem(handle) |
| 264 |
#define driz_log_message(message) driz_log_idem(message) |
| 265 |
|
| 266 |
#endif |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
static inline_macro void |
| 274 |
get_dimensions(PyArrayObject *image, integer_t size[2]) |
| 275 |
{ |
| 276 |
npy_intp *ndim = PyArray_DIMS(image); |
| 277 |
|
| 278 |
|
| 279 |
size[0] = ndim[1]; |
| 280 |
size[1] = ndim[0]; |
| 281 |
|
| 282 |
return; |
| 283 |
} |
| 284 |
|
| 285 |
static inline_macro double * |
| 286 |
get_pixmap(PyArrayObject *pixmap, integer_t xpix, integer_t ypix) |
| 287 |
{ |
| 288 |
return (double *) PyArray_GETPTR3(pixmap, ypix, xpix, 0); |
| 289 |
} |
| 290 |
|
| 291 |
#if defined(LOGGING) && defined(CHECK_OOB) |
| 292 |
|
| 293 |
static inline_macro int |
| 294 |
oob_pixel(PyArrayObject *image, integer_t xpix, integer_t ypix) |
| 295 |
{ |
| 296 |
char buffer[64]; |
| 297 |
npy_intp *ndim = PyArray_DIMS(image); |
| 298 |
if ((xpix < 0 || xpix >= ndim[1]) || (ypix < 0 || ypix >= ndim[0])) { |
| 299 |
sprintf( |
| 300 |
buffer, "Point [%d,%d] is outside of [%d, %d]", xpix, ypix, (int) ndim[1], |
| 301 |
(int) ndim[0]); |
| 302 |
driz_log_message(buffer); |
| 303 |
return 1; |
| 304 |
} |
| 305 |
|
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
static inline_macro int |
| 310 |
oob_output_pixel(driz_param_t *p, integer_t xpix, integer_t ypix) |
| 311 |
{ |
| 312 |
char buffer[64]; |
| 313 |
if ((xpix < 0 || xpix >= p->out_nx) || (ypix < 0 || ypix >= p->out_ny)) { |
| 314 |
sprintf( |
| 315 |
buffer, "Point [%d,%d] is outside of [%d, %d]", xpix, ypix, (int) p->out_nx, |
| 316 |
(int) p->out_ny); |
| 317 |
driz_log_message(buffer); |
| 318 |
return 1; |
| 319 |
} |
| 320 |
|
| 321 |
return 0; |
| 322 |
} |
| 323 |
|
| 324 |
#else |
| 325 |
|
| 326 |
#define oob_pixel(image, xpix, ypix) 0 |
| 327 |
|
| 328 |
#define oob_output_pixel(p, xpix, ypix) 0 |
| 329 |
|
| 330 |
#endif |
| 331 |
|
| 332 |
static inline_macro int |
| 333 |
get_uint_pixel(PyArrayObject *image, integer_t xpix, integer_t ypix) |
| 334 |
{ |
| 335 |
return *(unsigned int *) PyArray_GETPTR2(image, ypix, xpix); |
| 336 |
} |
| 337 |
|
| 338 |
static inline_macro float |
| 339 |
get_pixel(PyArrayObject *image, integer_t xpix, integer_t ypix) |
| 340 |
{ |
| 341 |
return *(float *) PyArray_GETPTR2(image, ypix, xpix); |
| 342 |
} |
| 343 |
|
| 344 |
static inline_macro float |
| 345 |
get_pixel_at_pos(PyArrayObject *image, integer_t pos) |
| 346 |
{ |
| 347 |
float *imptr; |
| 348 |
imptr = (float *) PyArray_DATA(image); |
| 349 |
return imptr[pos]; |
| 350 |
} |
| 351 |
|
| 352 |
static inline_macro void |
| 353 |
set_pixel(PyArrayObject *image, integer_t xpix, integer_t ypix, double value) |
| 354 |
{ |
| 355 |
*(float *) PyArray_GETPTR2(image, ypix, xpix) = value; |
| 356 |
return; |
| 357 |
} |
| 358 |
|
| 359 |
static inline_macro void |
| 360 |
set_uint_pixel(PyArrayObject *image, integer_t xpix, integer_t ypix, unsigned int value) |
| 361 |
{ |
| 362 |
*(unsigned int *) PyArray_GETPTR2(image, ypix, xpix) = value; |
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
static inline_macro int |
| 367 |
get_bit(PyArrayObject *image, integer_t xpix, integer_t ypix, integer_t bitval) |
| 368 |
{ |
| 369 |
integer_t value; |
| 370 |
value = *(integer_t *) PyArray_GETPTR2(image, ypix, xpix) & bitval; |
| 371 |
return value ? 1 : 0; |
| 372 |
} |
| 373 |
|
| 374 |
static inline_macro void |
| 375 |
set_bit(PyArrayObject *image, integer_t xpix, integer_t ypix, integer_t bitval) |
| 376 |
{ |
| 377 |
*(integer_t *) PyArray_GETPTR2(image, ypix, xpix) |= bitval; |
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
static inline_macro void |
| 382 |
unset_bit(PyArrayObject *image, integer_t xpix, integer_t ypix) |
| 383 |
{ |
| 384 |
*(integer_t *) PyArray_GETPTR2(image, ypix, xpix) = 0; |
| 385 |
return; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
int |
| 392 |
kernel_str2enum(const char *s, enum e_kernel_t *result, struct driz_error_t *error); |
| 393 |
|
| 394 |
int |
| 395 |
unit_str2enum(const char *s, enum e_unit_t *result, struct driz_error_t *error); |
| 396 |
|
| 397 |
int |
| 398 |
interp_str2enum(const char *s, enum e_interp_t *result, struct driz_error_t *error); |
| 399 |
|
| 400 |
const char * |
| 401 |
kernel_enum2str(enum e_kernel_t value); |
| 402 |
|
| 403 |
const char * |
| 404 |
unit_enum2str(enum e_unit_t value); |
| 405 |
|
| 406 |
const char * |
| 407 |
interp_enum2str(enum e_interp_t value); |
| 408 |
|
| 409 |
const char * |
| 410 |
bool2str(bool_t value); |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
void |
| 430 |
create_lanczos_lut( |
| 431 |
const int kernel_order, const size_t npix, const double del, double *lanczos_lut); |
| 432 |
|
| 433 |
void |
| 434 |
put_fill(struct driz_param_t *p, int fill, int fill2); |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
double |
| 441 |
mgf2(double lambda); |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
static inline_macro void |
| 449 |
weighted_sum_vectors( |
| 450 |
const integer_t npix, const float *a , const float w1, const float *b , |
| 451 |
const float w2, |
| 452 |
|
| 453 |
float *c ) |
| 454 |
{ |
| 455 |
float *c_end = c + npix; |
| 456 |
|
| 457 |
assert(a); |
| 458 |
assert(b); |
| 459 |
assert(c); |
| 460 |
|
| 461 |
while (c != c_end) { |
| 462 |
*(c++) = *(a++) * w1 + *(b++) * w2; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
static inline_macro integer_t |
| 470 |
nintd(const double x) |
| 471 |
{ |
| 472 |
return (integer_t) round(x); |
| 473 |
|
| 474 |
} |
| 475 |
static inline_macro integer_t |
| 476 |
nintf(const float x) |
| 477 |
{ |
| 478 |
return (integer_t) roundf(x); |
| 479 |
|
| 480 |
} |
| 481 |
|
| 482 |
static inline_macro double |
| 483 |
min_doubles(const double *a, const integer_t size) |
| 484 |
{ |
| 485 |
const double *end = a + size; |
| 486 |
double value = MAX_DOUBLE; |
| 487 |
for (; a != end; ++a) { |
| 488 |
if (*a < value) { |
| 489 |
value = *a; |
| 490 |
} |
| 491 |
} |
| 492 |
return value; |
| 493 |
} |
| 494 |
|
| 495 |
static inline_macro double |
| 496 |
max_doubles(const double *a, const integer_t size) |
| 497 |
{ |
| 498 |
const double *end = a + size; |
| 499 |
double value = MIN_DOUBLE; |
| 500 |
for (; a != end; ++a) { |
| 501 |
if (*a > value) { |
| 502 |
value = *a; |
| 503 |
} |
| 504 |
} |
| 505 |
return value; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
static inline_macro void |
| 524 |
rad3( |
| 525 |
const double x, const double y, const double *co, |
| 526 |
|
| 527 |
double *xo, double *yo) |
| 528 |
{ |
| 529 |
double r, f; |
| 530 |
|
| 531 |
assert(co); |
| 532 |
assert(xo); |
| 533 |
assert(yo); |
| 534 |
|
| 535 |
r = sqrt(x * x + y * y); |
| 536 |
|
| 537 |
f = 1.0 + co[0] + co[1] * r + co[2] * r * r; |
| 538 |
*xo = f * x; |
| 539 |
*yo = f * y; |
| 540 |
} |
| 541 |
|
| 542 |
#endif |