Debian Codegraph @

src/cdrizzleblot.c

raw symbols SCIP
1 #include <assert.h>
2 #define _USE_MATH_DEFINES /* needed for MS Windows to define M_PI */
3 #include <math.h>
4 #include <stdlib.h>
5
6 #ifndef NPY_NO_DEPRECATED_API
7 #define NPY_NO_DEPRECATED_API NPY_1_21_API_VERSION
8 #endif
9
10 #define NO_IMPORT_ARRAY
11 #define PY_ARRAY_UNIQUE_SYMBOL cdrizzle_blot_api
12
13 #pragma GCC diagnostic push
14 #pragma GCC diagnostic ignored "-Wpedantic"
15 #include <numpy/npy_math.h>
16 #include <numpy/arrayobject.h>
17 #pragma GCC diagnostic pop
18
19 #include "driz_portability.h"
20 #include "cdrizzlemap.h"
21 #include "cdrizzleblot.h"
22 #include "cdrizzleutil.h"
23
24 static const double lut_delta = 0.003; /* spacing of Lanczos LUT */
25
26 /** ---------------------------------------------------------------------------
27 * Signature for functions that perform blotting interpolation.
28 */
29
30 typedef int(interp_function)(
31 const void *, PyArrayObject *, const float, const float,
32 /* Output parameters */
33 float *, struct driz_error_t *);
34
35 /** ---------------------------------------------------------------------------
36 * A standard set of asserts for all of the interpolation functions
37 */
38
39 #define INTERPOLATION_ASSERTS \
40 assert(data); \
41 assert(isize[0] > 0); \
42 assert(isize[1] > 0); \
43 assert(x >= 0.0f && x < (float) isize[0]); \
44 assert(y >= 0.0f && y < (float) isize[1]); \
45 assert(value); \
46 assert(error);
47
48 /** ---------------------------------------------------------------------------
49 * A structure to hold parameters for sinc interpolation.
50 */
51
52 struct sinc_param_t {
53 /*The scaling factor for sinc interpolation */
54 float sinscl;
55 };
56
57 /** ---------------------------------------------------------------------------
58 * Procedure to evaluate the bicubic polynomial interpolant. The array coeff
59 * contains the coefficients of the 2D interpolant. The procedure assumes that
60 * 1 <= x <= isize[0] and 1 <= y <= isize[1] and that coeff[1+first_point] =
61 * datain[1,1]. The interpolant is evaluated using Everett's central difference
62 * formula. (Was: IIBIP3)
63 *
64 * coeff: Array of shape \a [len_coeff][len_coeff] contains the coefficients
65 * of the 2D interpolant. len_coeff: The dimension (each side of the square) of
66 * the coefficient array. firstt: Offset of the first data point. (In
67 * practice, this is always zero.) npts: The number of points to calculate.
68 * x: An array of length \a npts of x values.
69 * y: An array of length \a npts of y values.
70 * zfit: An array of length \a npts of interpolated values. (output)
71 */
72
73 static inline_macro int
74 ii_bipoly3(
75 const float *coeff /* [len_coeff][len_coeff] */, const integer_t len_coeff,
76 const integer_t firstt, const integer_t npts, const float *x /* [npts] */,
77 const float *y /* [npts] */,
78 /* Output parameters */
79 float *zfit /* [npts] */, struct driz_error_t *error)
80 {
81 float sx, tx, sx2m1, tx2m1, sy, ty;
82 float cd20[4], cd21[4], ztemp[4];
83 float cd20y, cd21y;
84 integer_t nxold, nyold;
85 integer_t nx, ny;
86 integer_t firstw, index;
87 integer_t i, j;
88
89 nxold = nyold = -1;
90
91 for (i = 0; i < npts; ++i) {
92 nx = (integer_t) x[i];
93 ny = (integer_t) y[i];
94 if (nx < 0 || ny < 0) {
95 driz_error_set(error, PyExc_ValueError, "Negative coordinates in ii_bipoly3.");
96 return 1;
97 }
98
99 sx = x[i] - (float) nx;
100 tx = 1.0f - sx;
101 sx2m1 = sx * sx - 1.0f;
102 tx2m1 = tx * tx - 1.0f;
103 sy = y[i] - (float) ny;
104 ty = 1.0f - sy;
105
106 /* Calculate pointer to data[nx, ny-1] */
107 firstw = firstt + (ny - 2) * len_coeff + nx - 1;
108
109 /* loop over the 4 surrounding rows of data calculate the central
110 differences at each value of y
111
112 If new data point calculate the central differences in x for
113 each y */
114 if (nx != nxold || ny != nyold) {
115 for (j = 0, index = firstw; j < 4; ++j, index += len_coeff) {
116 assert(index > 0 && index < (len_coeff * len_coeff) - 2);
117
118 cd20[j] = 1.0f / 6.0f * (coeff[index + 1] - 2.0f * coeff[index] + coeff[index - 1]);
119 cd21[j] = 1.0f / 6.0f * (coeff[index + 2] - 2.0f * coeff[index + 1] + coeff[index]);
120 }
121 }
122
123 /* Interpolate in x at each value of y */
124 for (j = 0, index = firstw; j < 4; ++j, index += len_coeff) {
125 assert(index >= 0 && index < (len_coeff * len_coeff) - 1);
126
127 ztemp[j] =
128 sx * (coeff[index + 1] + sx2m1 * cd21[j]) + tx * (coeff[index] + tx2m1 * cd20[j]);
129 }
130
131 /* Calculate y central differences */
132 cd20y = 1.0f / 6.0f * (ztemp[2] - 2.0f * ztemp[1] + ztemp[0]);
133 cd21y = 1.0f / 6.0f * (ztemp[3] - 2.0f * ztemp[2] + ztemp[1]);
134
135 /* Interpolate in y */
136 zfit[i] =
137 sy * (ztemp[2] + (sy * sy - 1.0f) * cd21y) + ty * (ztemp[1] + (ty * ty - 1.0f) * cd20y);
138
139 nxold = nx;
140 nyold = ny;
141 }
142 return 0;
143 }
144
145 /** ---------------------------------------------------------------------------
146 * Procedure to evaluate a biquintic polynomial. The array coeff contains the
147 * coefficents of the 2D interpolant. The routine assumes that 0 <= x <
148 * isize[0] and 0 <= y < isize[1]. The interpolant is evaluated using Everett's
149 * central difference formula. (Was: IIBIP5)
150 *
151 * coeff: Array of shape \a [len_coeff][len_coeff] contains the coefficients
152 * of the 2D interpolant. len_coeff: The dimension (each side of the square) of
153 * the coefficient array. firstt: Offset of the first data point. (In
154 * practice, this is always zero.) npts: The number of points to calculate.
155 * x: An array of length \a npts of x values.
156 * y: An array of length \a npts of y values.
157 * zfit: An array of length \a npts of interpolated values. (output)
158 */
159
160 static inline_macro int
161 ii_bipoly5(
162 const float *coeff /* [len_coeff][len_coeff] */, const integer_t len_coeff,
163 const integer_t firstt, const integer_t npts, const float *x /* [npts] */,
164 const float *y /* [npts] */,
165 /* Output parameters */
166 float *zfit /* [npts] */, struct driz_error_t *error)
167 {
168 integer_t nxold, nyold;
169 integer_t nx, ny;
170 float sx, sx2, tx, tx2, sy, sy2, ty, ty2;
171 float sx2m1, sx2m4, tx2m1, tx2m4;
172 float cd20[6], cd21[6], cd40[6], cd41[6];
173 float cd20y, cd21y, cd40y, cd41y;
174 float ztemp[6];
175 integer_t firstw, index;
176 integer_t i, j;
177
178 assert(coeff);
179 assert(len_coeff > 0);
180 assert(npts > 0);
181 assert(x);
182 assert(y);
183 assert(zfit);
184
185 nxold = nyold = -1;
186
187 for (i = 0; i < npts; ++i) {
188 nx = (integer_t) x[i];
189 ny = (integer_t) y[i];
190 if (nx < 0 || ny < 0) {
191 driz_error_set(error, PyExc_ValueError, "Negative coordinates in ii_bipoly3.");
192 return 1;
193 }
194
195 sx = x[i] - (float) nx;
196 sx2 = sx * sx;
197 sx2m1 = sx2 - 1.0f;
198 sx2m4 = sx2 - 4.0f;
199 tx = 1.0f - sx;
200 tx2 = tx * tx;
201 tx2m1 = tx2 - 1.0f;
202 tx2m4 = tx2 - 4.0f;
203
204 sy = y[i] - (float) ny;
205 sy2 = sy * sy;
206 ty = 1.0f - sy;
207 ty2 = ty * ty;
208
209 /* Calculate value of pointer to data[nx,ny-2] */
210 firstw = firstt + (ny - 3) * len_coeff + nx - 1;
211
212 /* Calculate the central differences in x at each value of y */
213 if (nx != nxold || ny != nyold) {
214 for (j = 0, index = firstw; j < 6; ++j, index += len_coeff) {
215 assert(index >= 1 && index < len_coeff * len_coeff - 3);
216
217 cd20[j] = 1.0f / 6.0f * (coeff[index + 1] - 2.0f * coeff[index] + coeff[index - 1]);
218 cd21[j] = 1.0f / 6.0f * (coeff[index + 2] - 2.0f * coeff[index + 1] + coeff[index]);
219 cd40[j] = 1.0f / 120.0f *
220 (coeff[index - 2] - 4.0f * coeff[index - 1] + 6.0f * coeff[index] -
221 4.0f * coeff[index + 1] + coeff[index + 2]);
222 cd41[j] = 1.0f / 120.0f *
223 (coeff[index - 1] - 4.0f * coeff[index] + 6.0f * coeff[index + 1] -
224 4.0f * coeff[index + 2] + coeff[index + 3]);
225 }
226 }
227
228 /* Interpolate in x at each value of y */
229 for (j = 0, index = firstw; j < 6; ++j, index += len_coeff) {
230 assert(index >= 0 && index < len_coeff * len_coeff - 1);
231
232 ztemp[j] = sx * (coeff[index + 1] + sx2m1 * (cd21[j] + sx2m4 * cd41[j])) +
233 tx * (coeff[index] + tx2m1 * (cd20[j] + tx2m4 * cd40[j]));
234 }
235
236 /* Central differences in y */
237 cd20y = 1.0f / 6.0f * (ztemp[3] - 2.0f * ztemp[2] + ztemp[1]);
238 cd21y = 1.0f / 6.0f * (ztemp[4] - 2.0f * ztemp[3] + ztemp[2]);
239 cd40y = 1.0f / 120.0f *
240 (ztemp[0] - 4.0f * ztemp[1] + 6.0f * ztemp[2] - 4.0f * ztemp[3] + ztemp[4]);
241 cd41y = 1.0f / 120.0f *
242 (ztemp[1] - 4.0f * ztemp[2] + 6.0f * ztemp[3] - 4.0f * ztemp[4] + ztemp[5]);
243
244 /* Interpolate in y */
245 zfit[i] = sy * (ztemp[3] + (sy2 - 1.0f) * (cd21y + (sy2 - 4.0f) * cd41y)) +
246 ty * (ztemp[2] + (ty2 - 1.0f) * (cd20y + (ty2 - 4.0f) * cd40y));
247
248 nxold = nx;
249 nyold = ny;
250 }
251 return 0;
252 }
253
254 /** ---------------------------------------------------------------------------
255 * Perform nearest neighbor interpolation.
256 *
257 * state: A pointer to any constant values specific to this interpolation type.
258 * (NULL). data: A 2D data array x: The fractional x coordinate y: The
259 * fractional y coordinate value: The resulting value at x, y after
260 * interpolating the data (output) error: The error structure (output)
261 */
262
263 static int
264 interpolate_nearest_neighbor(
265 const void *state, PyArrayObject *data, const float x, const float y,
266 /* Output parameters */
267 float *value, struct driz_error_t *error)
268 {
269 /* Unused parameters: */
270 (void) state;
271 (void) error;
272
273 integer_t isize[2];
274 get_dimensions(data, isize);
275
276 assert(state == NULL);
277 INTERPOLATION_ASSERTS;
278
279 *value = get_pixel(data, (integer_t) (x + 0.5), (integer_t) (y + 0.5));
280 return 0;
281 }
282
283 /** ---------------------------------------------------------------------------
284 * Perform basic bilinear interpolation.
285 *
286 * state: A pointer to any constant values specific to this interpolation type.
287 * (NULL). data: A 2D data array x: The fractional x coordinate y: The
288 * fractional y coordinate value: The resulting value at x, y after
289 * interpolating the data (output) error: The error structure (output)
290 */
291
292 static int
293 interpolate_bilinear(
294 const void *state, PyArrayObject *data, const float x, const float y,
295 /* Output parameters */
296 float *value, struct driz_error_t *error)
297 {
298 /* Unused parameters: */
299 (void) state;
300 (void) error;
301
302 integer_t nx, ny;
303 float sx, tx, sy, ty, f00;
304 integer_t isize[2];
305
306 get_dimensions(data, isize);
307
308 assert(state == NULL);
309 INTERPOLATION_ASSERTS;
310
311 nx = (integer_t) x;
312 ny = (integer_t) y;
313
314 if (nx < 0 || ny < 0 || nx >= isize[0] || ny >= isize[1]) {
315 driz_error_set_message(error, "Bilinear interpolation: point outside of the image.");
316 return 1;
317 }
318
319 f00 = get_pixel(data, nx, ny);
320
321 if (nx == (isize[0] - 1)) {
322 if (ny == (isize[1] - 1)) {
323 /* This is the last pixel (in x and y). Assign constant value of
324 * this pixel. */
325 *value = f00;
326 return 0;
327 }
328 /* Interpolate along Y-direction only */
329 sy = y - (float) ny;
330 *value = (1.0f - sy) * f00 + sy * get_pixel(data, nx, ny + 1);
331 } else if (ny == (isize[1] - 1)) {
332 /* Interpolate along X-direction only */
333 sx = x - (float) nx;
334 *value = (1.0f - sx) * f00 + sx * get_pixel(data, nx + 1, ny);
335 } else {
336 /* Bilinear - interpolation */
337 sx = x - (float) nx;
338 tx = 1.0f - sx;
339 sy = y - (float) ny;
340 ty = 1.0f - sy;
341
342 *value = tx * ty * f00 + sx * ty * get_pixel(data, nx + 1, ny) +
343 sy * tx * get_pixel(data, nx, ny + 1) + sx * sy * get_pixel(data, nx + 1, ny + 1);
344 }
345
346 return 0;
347 }
348
349 /** ---------------------------------------------------------------------------
350 * Perform cubic polynomial interpolation.
351 *
352 * state: A pointer to any constant values specific to this interpolation type.
353 * (NULL). data: A 2D data array x: The fractional x coordinate y: The
354 * fractional y coordinate value: The resulting value at x, y after
355 * interpolating the data (output) error: The error structure (output)
356 */
357
358 static int
359 interpolate_poly3(
360 const void *state, PyArrayObject *data, const float x, const float y,
361 /* Output parameters */
362 float *value, struct driz_error_t *error)
363 {
364 /* Unused parameters: */
365 (void) state;
366
367 integer_t nx, ny;
368 const integer_t rowleh = 4;
369 const integer_t nterms = 4;
370 float coeff[4][4];
371 integer_t i, j;
372 integer_t firstw, lastrw;
373 float xval, yval;
374 float *ci;
375 integer_t isize[2];
376 get_dimensions(data, isize);
377
378 assert(state == NULL);
379 INTERPOLATION_ASSERTS;
380 ;
381
382 nx = (integer_t) x;
383 ny = (integer_t) y;
384
385 ci = &coeff[0][0];
386 for (j = ny - 1; j <= ny + 2; ++j) {
387 if (j >= 0 && j < isize[1]) {
388 for (i = nx - 1; i <= nx + 2; ++i, ++ci) {
389 if (i < 0) {
390 *ci = 2.0f * get_pixel(data, 0, j) - get_pixel(data, -i, j);
391 } else if (i >= isize[0]) {
392 *ci = 2.0f * get_pixel(data, isize[0] - 1, j) -
393 get_pixel(data, 2 * isize[0] - 2 - i, j);
394 } else {
395 *ci = get_pixel(data, i, j);
396 }
397 }
398 } else if (j == ny + 2) {
399 for (i = nx - 1; i <= nx + 2; ++i, ++ci) {
400 if (i < 0) {
401 *ci =
402 2.0f * get_pixel(data, 0, isize[1] - 3) - get_pixel(data, -i, isize[1] - 3);
403 } else if (i >= isize[0]) {
404 *ci = 2.0f * get_pixel(data, isize[0] - 1, isize[1] - 3) -
405 get_pixel(data, 2 * isize[0] - 2 - i, isize[1] - 3);
406 } else {
407 *ci = get_pixel(data, i, isize[1] - 3);
408 }
409 }
410 } else {
411 ci += 4;
412 }
413 }
414
415 firstw = MAX(0, 1 - ny);
416 if (firstw > 0) {
417 assert(firstw < nterms);
418
419 for (j = 0; j < firstw; ++j) {
420 assert(2 * firstw - j >= 0 && 2 * firstw - j < nterms);
421
422 weighted_sum_vectors(
423 nterms, &coeff[firstw][0], 2.0, &coeff[2 * firstw - j][0], -1.0, &coeff[j][0]);
424 }
425 }
426
427 lastrw = MIN(nterms - 1, isize[1] - ny);
428 if (lastrw < nterms - 1) {
429 assert(lastrw >= 0 && lastrw < nterms);
430
431 for (j = lastrw + 1; j <= nterms - 1; ++j) {
432 assert(2 * lastrw - j >= 0 && 2 * lastrw - j < nterms);
433 assert(j >= 0 && j < 4);
434
435 weighted_sum_vectors(
436 nterms, &coeff[lastrw][0], 2.0, &coeff[2 * lastrw - j][0], -1.0, &coeff[j][0]);
437 }
438 } else if (lastrw == 1) {
439 assert(lastrw >= 0 && lastrw < nterms);
440
441 weighted_sum_vectors(nterms, &coeff[lastrw][0], 2.0, &coeff[3][0], -1.0, &coeff[3][0]);
442 } else {
443 assert(lastrw >= 0 && lastrw < nterms);
444 assert(2 * lastrw - 3 >= 0 && 2 * lastrw - 3 < nterms);
445
446 weighted_sum_vectors(
447 nterms, &coeff[lastrw][0], 2.0, &coeff[2 * lastrw - 3][0], -1.0, &coeff[3][0]);
448 }
449
450 xval = 2.0f + (x - (float) nx);
451 yval = 2.0f + (y - (float) ny);
452
453 return ii_bipoly3(&coeff[0][0], rowleh, 0, 1, &xval, &yval, value, error);
454 }
455
456 /** ---------------------------------------------------------------------------
457 * Perform quintic polynomial interpolation.
458 *
459 * state: A pointer to any constant values specific to this interpolation type.
460 * (NULL). data: A 2D data array x: The fractional x coordinate y: The
461 * fractional y coordinate value: The resulting value at x, y after
462 * interpolating the data (output) error: The error structure (output)
463 */
464
465 static int
466 interpolate_poly5(
467 const void *state, PyArrayObject *data, const float x, const float y,
468 /* Output parameters */
469 float *value, struct driz_error_t *error)
470 {
471 (void) state;
472
473 integer_t nx, ny;
474 const integer_t rowleh = 6;
475 const integer_t nterms = 6;
476 float coeff[6][6];
477 integer_t i, j;
478 integer_t firstw, lastrw;
479 float xval, yval;
480 float *ci;
481 integer_t isize[2];
482 get_dimensions(data, isize);
483
484 assert(state == NULL);
485 INTERPOLATION_ASSERTS;
486
487 nx = (integer_t) x;
488 ny = (integer_t) y;
489
490 ci = &coeff[0][0];
491 for (j = ny - 2; j <= ny + 3; ++j) {
492 if (j >= 0 && j < isize[1]) {
493 for (i = nx - 2; i <= nx + 3; ++i, ++ci) {
494 if (i < 0) {
495 *ci = 2.0f * get_pixel(data, 0, j) - get_pixel(data, -i, j);
496 } else if (i >= isize[0]) {
497 *ci = 2.0f * get_pixel(data, isize[0] - 1, j) -
498 get_pixel(data, 2 * isize[0] - 2 - i, j);
499 } else {
500 *ci = get_pixel(data, i, j);
501 }
502 }
503 } else if (j == (ny + 3)) {
504 for (i = nx - 2; i <= nx + 3; ++i, ++ci) {
505 if (i < 0) {
506 *ci =
507 2.0f * get_pixel(data, 0, isize[1] - 4) - get_pixel(data, -i, isize[1] - 4);
508 } else if (i >= isize[0]) {
509 *ci = 2.0f * get_pixel(data, isize[0] - 1, isize[1] - 4) -
510 get_pixel(data, 2 * isize[0] - 2 - i, isize[1] - 4);
511 } else {
512 *ci = get_pixel(data, i, isize[1] - 4);
513 }
514 }
515 } else {
516 ci += 6;
517 }
518 }
519
520 firstw = MAX(0, 2 - ny);
521 assert(firstw >= 0 && firstw < nterms);
522
523 if (firstw > 0) {
524 for (j = 0; j <= firstw; ++j) {
525 assert(2 * firstw - j >= 0 && 2 * firstw - j < nterms);
526
527 weighted_sum_vectors(
528 nterms, &coeff[firstw][0], 2.0, &coeff[2 * firstw - j][0], -1.0, &coeff[j][0]);
529 }
530 }
531
532 lastrw = MIN(nterms - 1, isize[1] - ny + 1);
533 assert(lastrw < nterms);
534
535 if (lastrw < nterms - 1) {
536 for (j = lastrw + 1; j <= nterms - 2; ++j) {
537 assert(2 * lastrw - j >= 0 && 2 * lastrw - j < nterms);
538
539 weighted_sum_vectors(
540 nterms, &coeff[lastrw][0], 2.0, &coeff[2 * lastrw - j][0], -1.0, &coeff[j][0]);
541 }
542 } else if (lastrw == 2) {
543 weighted_sum_vectors(nterms, &coeff[2][0], 2.0, &coeff[5][0], -1.0, &coeff[5][0]);
544 } else {
545 assert(2 * lastrw - 5 >= 0 && 2 * lastrw - 5 < nterms);
546
547 weighted_sum_vectors(
548 nterms, &coeff[lastrw][0], 2.0, &coeff[2 * lastrw - 5][0], -1.0, &coeff[5][0]);
549 }
550
551 xval = 3.0f + (x - (float) nx);
552 yval = 3.0f + (y - (float) ny);
553
554 return ii_bipoly5(&coeff[0][0], rowleh, 0, 1, &xval, &yval, value, error);
555 }
556
557 /** ---------------------------------------------------------------------------
558 * (Was: iinisc)
559 */
560
561 #define INTERPOLATE_SINC_NCONV 15
562
563 static inline_macro int
564 interpolate_sinc_(
565 PyArrayObject *data, const integer_t firstt, const integer_t npts, const float *x /*[npts]*/,
566 const float *y /*[npts]*/, const float mindx, const float mindy, const float sinscl,
567 /* Output parameters */
568 float *value, struct driz_error_t *error)
569 {
570 /* Unused parameters: */
571 (void) error;
572
573 const integer_t nconv = INTERPOLATE_SINC_NCONV;
574 const integer_t nsinc = (nconv - 1) / 2;
575 /* TODO: This is to match Fortan, but is probably technically less precise
576 */
577
578 const float sconst = powf((float) (M_PI_2 / nsinc), 2.0f);
579 const float a2 = -0.49670f;
580 const float a4 = 0.03705f;
581 float taper[INTERPOLATE_SINC_NCONV];
582 float ac[INTERPOLATE_SINC_NCONV], ar[INTERPOLATE_SINC_NCONV];
583 float sdx, dx, dy, dxn, dyn, dx2;
584 float ax, ay, px, py;
585 float sum, sumx, sumy;
586 float tmp;
587 integer_t minj, maxj, offj;
588 integer_t mink, maxk, offk;
589 integer_t nx, ny;
590 integer_t i, j, k, m, index;
591 integer_t indices[3][4];
592 integer_t isize[2];
593 get_dimensions(data, isize);
594
595 assert(x);
596 assert(y);
597 assert(value);
598 assert(error);
599
600 if ((nsinc % 2) == 0) {
601 sdx = 1.0;
602 for (j = -nsinc; j <= nsinc; ++j) {
603 assert(j + nsinc >= 0 && j + nsinc < INTERPOLATE_SINC_NCONV);
604
605 taper[j + nsinc] = 1.0;
606 }
607 } else {
608 sdx = -1.0;
609 errno = 0;
610 for (j = -nsinc; j <= nsinc; ++j) {
611 assert(j + nsinc >= 0 && j + nsinc < INTERPOLATE_SINC_NCONV);
612
613 dx2 = sconst * (float) j * (float) j;
614 tmp = powf(1.0f + a2 * dx2 + a4 * dx2 * dx2, 2.0);
615 if (errno != 0) {
616 driz_error_set_message(error, "pow failed");
617 return 1;
618 }
619 taper[j + nsinc] = sdx * tmp;
620
621 sdx = -sdx;
622 }
623 }
624
625 for (i = 0; i < npts; ++i) {
626 nx = nintf(x[i]);
627 ny = nintf(y[i]);
628 if (nx < 0 || nx >= isize[0] || ny < 0 || ny >= isize[1]) {
629 value[i] = 0.0;
630 continue;
631 }
632
633 dx = (x[i] - (float) nx) * sinscl;
634 dy = (y[i] - (float) ny) * sinscl;
635
636 if (fabsf(dx) < mindx && fabsf(dy) < mindy) {
637 index = firstt + (ny - 1) * isize[0] + nx - 1; /* TODO: Base check */
638 value[i] = get_pixel_at_pos(data, index);
639 continue;
640 }
641
642 dxn = 1.0f + (float) nsinc + dx;
643 dyn = 1.0f + (float) nsinc + dy;
644 sumx = 0.0f;
645 sumy = 0.0f;
646 for (j = 0; j < nconv; ++j) {
647 /* TODO: These out of range indices also seem to be in Fortran... */
648 ax = dxn - (float) j - 1; /* TODO: Base check */
649 ay = dyn - (float) j - 1; /* TODO: Base check */
650 assert(ax != 0.0);
651 assert(ay != 0.0);
652
653 if (ax == 0.0) {
654 px = 1.0;
655 } else if (dx == 0.0) {
656 px = 0.0;
657 } else {
658 px = taper[j - 1] / ax;
659 }
660
661 if (ay == 0.0) {
662 py = 1.0;
663 } else if (dy == 0.0) {
664 py = 0.0;
665 } else {
666 py = taper[j - 1] / ay;
667 }
668
669 /* TODO: These out of range indices also seem to be in Fortran... */
670 ac[j - 1] = px;
671 ar[j - 1] = py;
672 sumx += px;
673 sumy += py;
674 }
675
676 /* Compute the limits of the convolution */
677 minj = MAX(0, ny - nsinc - 1); /* TODO: Bases check */
678 maxj = MIN(isize[1], ny + nsinc); /* TODO: Bases check */
679 offj = nsinc - ny; /* TODO: Bases check */
680
681 mink = MAX(0, nx - nsinc - 1); /* TODO: Bases check */
682 maxk = MIN(isize[0], nx + nsinc); /* TODO: Bases check */
683 offk = nsinc - nx; /* TODO: Bases check */
684
685 value[i] = 0.0;
686
687 indices[0][0] = ny - nsinc;
688 indices[0][1] = minj - 1;
689 indices[0][2] = firstt;
690 indices[0][3] = 0;
691
692 indices[1][0] = minj;
693 indices[1][1] = maxj;
694 indices[1][2] = firstt;
695 indices[1][3] = isize[0];
696
697 indices[2][0] = maxj + 1;
698 indices[2][1] = ny + nsinc;
699 indices[2][2] = firstt + (isize[1] - 1) * isize[0];
700 indices[2][3] = 0;
701
702 for (m = 0; m < 3; ++m) {
703 for (j = indices[m][0]; j <= indices[m][1]; ++j) {
704 sum = 0.0;
705 index = indices[m][2] + j * indices[m][3];
706 assert(index >= 0 && index < isize[0] * isize[1] - 1);
707 assert(index + isize[0] >= 0 && index + isize[0] < isize[0] * isize[1]);
708
709 for (k = nx - nsinc; k < mink - 1; ++k) { /* TODO: Bases check */
710 assert(k + offk >= 0 && k + offk < INTERPOLATE_SINC_NCONV);
711
712 sum += ac[k + offk] * get_pixel_at_pos(data, index + 1);
713 }
714
715 for (k = mink; k <= maxk; ++k) { /* TODO: Bases check */
716 assert(k + offk >= 0 && k + offk < INTERPOLATE_SINC_NCONV);
717 assert(index + k >= 0 && index + k < isize[0] * isize[1]);
718
719 sum += ac[k + offk] * get_pixel_at_pos(data, index + k);
720 }
721
722 for (k = maxk + 1; k <= nx + nsinc; ++k) {
723 assert(k + offk >= 0 && k + offk < INTERPOLATE_SINC_NCONV);
724
725 sum += ac[k + offk] * get_pixel_at_pos(data, index + isize[0]);
726 }
727
728 assert(j + offj >= 0 && j + offj < INTERPOLATE_SINC_NCONV);
729
730 value[i] += ar[j + offj] * sum;
731 }
732 }
733
734 assert(sumx != 0.0);
735 assert(sumy != 0.0);
736
737 value[i] = value[i] / sumx / sumy;
738 }
739
740 return 0;
741 }
742
743 /** ---------------------------------------------------------------------------
744 * Perform sinc interpolation.
745 *
746 * state: A pointer to a \a sinc_param_t object
747 * data: A 2D data array
748 * x: The fractional x coordinate
749 * y: The fractional y coordinate
750 * value: The resulting value at x, y after interpolating the data (output)
751 * error: The error structure (output)
752 */
753
754 static int
755 interpolate_sinc(
756 const void *state, PyArrayObject *data, const float x, const float y,
757 /* Output parameters */
758 float *value, struct driz_error_t *error)
759 {
760 const struct sinc_param_t *param = (const struct sinc_param_t *) state;
761 integer_t isize[2];
762 get_dimensions(data, isize);
763
764 assert(state);
765 INTERPOLATION_ASSERTS;
766
767 return interpolate_sinc_(data, 0, 1, &x, &y, 0.001f, 0.001f, param->sinscl, value, error);
768 }
769
770 /** ---------------------------------------------------------------------------
771 * Perform Lanczos interpolation.
772 *
773 * state: A pointer to a \a lanczos_param_t object
774 * data: A 2D data array
775 * x: The fractional x coordinate
776 * y: The fractional y coordinate
777 * value: The resulting value at x, y after interpolating the data (output)
778 * error: The error structure (output)
779 */
780
781 static int
782 interpolate_lanczos(
783 const void *state, PyArrayObject *data, const float x, const float y,
784 /* Output parameters */
785 float *value, struct driz_error_t *error)
786 {
787 /* Unused parameters: */
788 (void) error;
789
790 integer_t ixs, iys, ixe, iye;
791 size_t xoff, yoff;
792 double luty, sum;
793 float nbox;
794 integer_t i, j;
795 const struct lanczos_param_t *lanczos = (const struct lanczos_param_t *) state;
796 integer_t isize[2];
797 get_dimensions(data, isize);
798
799 assert(state);
800 INTERPOLATION_ASSERTS;
801
802 nbox = (float) lanczos->nbox;
803
804 /* First check for being close to the edge and, if so, return the
805 missing value */
806 ixs = MAX((integer_t) floorf(x - nbox) + 1, 0);
807 ixe = MIN((integer_t) floorf(x + nbox), isize[0] - 1);
808 iys = MAX((integer_t) floorf(y - nbox) + 1, 0);
809 iye = MIN((integer_t) floorf(y + nbox), isize[1] - 1);
810
811 /* Don't divide-by-zero errors */
812 assert(lanczos->space != 0.0);
813
814 /* Loop over the box, which is assumed to be scaled appropriately */
815 sum = 0.0;
816 for (j = iys; j <= iye; ++j) {
817 yoff = (size_t) fabs(((double) y - (double) j) / lanczos->space);
818 if (yoff >= lanczos->nlut) {
819 continue;
820 }
821
822 luty = lanczos->lut[yoff];
823 for (i = ixs; i <= ixe; ++i) {
824 xoff = (size_t) (fabs(((double) x - (double) i) / lanczos->space));
825 if (xoff >= lanczos->nlut) {
826 continue;
827 }
828
829 sum += (double) get_pixel(data, i, j) * lanczos->lut[xoff] * luty;
830 }
831 }
832
833 *value = (float) sum;
834 return 0;
835 }
836
837 /** ---------------------------------------------------------------------------
838 * A mapping from e_interp_t enumeration values to function pointers that
839 * actually perform the interpolation. NULL elements will raise an
840 * "unimplemented" error.
841 */
842
843 interp_function *interp_function_map[interp_LAST] = {
844 &interpolate_nearest_neighbor,
845 &interpolate_bilinear,
846 &interpolate_poly3,
847 &interpolate_poly5,
848 NULL,
849 &interpolate_sinc,
850 &interpolate_sinc,
851 &interpolate_lanczos,
852 &interpolate_lanczos};
853
854 /** ---------------------------------------------------------------------------
855 * Interpolate grid of pixels onto new grid of different size.
856 *
857 * p: structure containing options, input, and output
858 */
859
860 int
861 doblot(struct driz_param_t *p)
862 {
863 int order;
864 size_t nlut;
865 integer_t isize[2], osize[2];
866 float xo, yo, v, s;
867 integer_t i, j;
868 interp_function *interpolate;
869 struct sinc_param_t sinc;
870 struct lanczos_param_t lanczos;
871 void *state = NULL;
872
873 driz_log_message("starting doblot");
874 get_dimensions(p->data, isize);
875 get_dimensions(p->output_data, osize);
876
877 s = p->ef * p->iscale;
878
879 /* Select interpolation function */
880 assert(p->interpolation >= 0 && p->interpolation < interp_LAST);
881 interpolate = interp_function_map[p->interpolation];
882 if (interpolate == NULL) {
883 driz_error_set_message(p->error, "Requested interpolation type not implemented.");
884 goto doblot_exit_;
885 }
886
887 lanczos.lut = NULL;
888
889 /* Some interpolation functions need some pre-calculated state */
890 if (p->interpolation == interp_lanczos3 || p->interpolation == interp_lanczos5) {
891 order = (p->interpolation == interp_lanczos3) ? 3 : 5;
892 nlut = (size_t) ceil((double) order / lut_delta) + 1;
893 if ((lanczos.lut = (double *) malloc(nlut * sizeof(double))) == NULL) {
894 driz_error_set_message(p->error, "Out of memory");
895 goto doblot_exit_;
896 }
897
898 create_lanczos_lut(order, nlut, lut_delta, lanczos.lut);
899
900 lanczos.nbox = (integer_t) order;
901 lanczos.nlut = nlut;
902 lanczos.space = lut_delta;
903
904 state = &lanczos;
905
906 } else if (p->interpolation == interp_sinc || p->interpolation == interp_lsinc) {
907 sinc.sinscl = p->sinscl;
908 state = &sinc;
909
910 } /* Otherwise state is NULL */
911
912 /* In the WCS case, we can't use the scale to calculate the Jacobian,
913 so we need to do it.
914
915 Note that we use the center of the image, rather than the reference pixel
916 as the reference here.
917
918 This is taken from dobox, except for the inversion of the image order.
919
920 This section applies in WBLOT mode and now contains the addition
921 correction to separate the distortion-induced scale change.
922 */
923
924 v = 1.0;
925
926 for (j = 0; j < osize[1]; ++j) {
927 /* Loop through the output positions and do the interpolation */
928 for (i = 0; i < osize[0]; ++i) {
929 if (oob_pixel(p->pixmap, i, j)) {
930 driz_error_format_message(p->error, "OOB in pixmap[%d,%d]", i, j);
931 return 1;
932 }
933
934 xo = (float) get_pixmap(p->pixmap, i, j)[0];
935 yo = (float) get_pixmap(p->pixmap, i, j)[1];
936
937 if (npy_isnan(xo) || npy_isnan(yo)) {
938 driz_error_format_message(p->error, "NaN in pixmap[%d,%d]", i, j);
939 return 1;
940 }
941
942 /* Check it is on the input image */
943 if (xo >= 0.0 && xo < (float) isize[0] && yo >= 0.0 && yo < (float) isize[1]) {
944 /* Check for look-up-table interpolation */
945 if (interpolate(state, p->data, xo, yo, &v, p->error)) {
946 goto doblot_exit_;
947 }
948
949 if (oob_pixel(p->output_data, i, j)) {
950 driz_error_format_message(p->error, "OOB in output_data[%d,%d]", i, j);
951 return 1;
952 } else {
953 set_pixel(p->output_data, i, j, v * s);
954 }
955
956 } else {
957 /* If there is nothing for us then set the output to missing C
958 value flag */
959 set_pixel(p->output_data, i, j, p->fill_value);
960 p->nmiss++;
961 }
962 }
963 }
964
965 doblot_exit_:
966 driz_log_message("ending doblot");
967 if (lanczos.lut) {
968 free(lanczos.lut);
969 }
970
971 return driz_error_is_set(p->error);
972 }