Debian Codegraph @

src/cdrizzlebox.c

raw symbols SCIP
1 #define NO_IMPORT_ARRAY
2
3 #include <assert.h>
4 #define _USE_MATH_DEFINES /* needed for MS Windows to define M_PI */
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; /* spacing of Lanczos LUT */
21
22 /** ---------------------------------------------------------------------------
23 * Update the flux and counts in the output image using a weighted average
24 *
25 * p: structure containing options, input, and output
26 * ii: x coordinate in output images
27 * jj: y coordinate in output images
28 * d: new contribution to weighted flux
29 * dow: new contribution to weighted counts
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 // get previous output image weight:
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 * Update the flux, variance, and counts in the output image using a weighted
79 * average.
80 *
81 * p: structure containing options, input, and output
82 * ii: x coordinate in output images
83 * jj: y coordinate in output images
84 * d: new contribution to weighted flux
85 * vc: previous value of counts
86 * dow: new contribution to weighted counts
87 * d2: array of data2 values that need to be co-added using squared weights
88 * (i.e., variance arrays)
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 // get previous output image weight:
112 vc = get_pixel(p->output_counts, ii, jj);
113
114 // new output image weight:
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 * The bit value, trimmed to the appropriate range
151 *
152 * uuid: the id of the input image
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 To calculate area under a line segment within unit square at origin.
170 This is used by BOXER.
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 /* Trap vertical line */
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 /* And determine the bounds ignoring y for now */
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 /* Now look at y */
206 double slope = dy / dx;
207 ylo = y1 + slope * (xlo - x1);
208 yhi = y1 + slope * (xhi - x1);
209
210 /* Alternative code that may be more stable under certain circumstances */
211 /*
212 if (xlo < 0.0) {
213 xlo = 0.0;
214 ylo = y1 + (dy / dx) * (xlo - x1);
215 } else {
216 ylo = (sgn_dx > 0.0) ? y1 : y2;
217 }
218
219 if (xhi > 1.0) {
220 xhi = 1.0;
221 yhi = y1 + (dy / dx) * (xhi - x1);
222 } else {
223 yhi = (sgn_dx > 0.0) ? y2 : y1;
224 }
225 */
226
227 /* Trap segment entirely below axis */
228 if (ylo <= 0.0 && yhi <= 0.0) {
229 return 0.0;
230 }
231
232 /* There are four possibilities: both y below 1, both y above 1 and
233 one of each. */
234 if (ylo >= 1.0 && yhi >= 1.0) {
235 /* Line segment is entirely above square */
236 return sgn_dx * (xhi - xlo);
237 }
238
239 det = x1 * y2 - y1 * x2;
240
241 /* Adjust bounds if segment crosses axis (to exclude anything below
242 axis) */
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 /* Segment is entirely within the square.
256 The case of zero slope will end up here. */
257 return sgn_dx * 0.5 * (xhi - xlo) * (yhi + ylo);
258 }
259
260 /* Otherwise, it must cross the top of the square */
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 compute area of box overlap
271
272 Calculate the area common to input clockwise polygon x(n), y(n) with
273 square (is, js) to (is+1, js+1).
274 This version is for a quadrilateral.
275
276 Used by do_square_kernel.
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 /* Set up coords relative to unit square at origin Note that the
292 +0.5s were added when this code was included in DRIZZLE */
293
294 for (i = 0; i < 4; ++i) {
295 px[i] = x[i] - is;
296 py[i] = y[i] - js;
297 }
298
299 /* For each line in the polygon (or at this stage, input
300 quadrilateral) calculate the area common to the unit square
301 (allow negative area for subsequent `vector' addition of
302 subareas). */
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 * Compute area of box overlap. Calculate the area common to input clockwise
313 * polygon x(n), y(n) with square (is, js) to (is+1, js+1). This version is for
314 * a quadrilateral. Used by do_square_kernel.
315 *
316 * is: x coordinate of a pixel on the output image
317 * js: y coordinate of a pixel on the output image
318 * x: x coordinates of endpoints of quadrilateral containing flux of input
319 * pixel y: y coordinates of endpoints of quadrilateral containing flux of
320 * input pixel
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 /* The area for a qadrilateral clipped to a square of unit length whose
333 * sides are aligned with the axes. The area is computed by computing the
334 * area under each line segment clipped to the boundary of three sides of
335 * the sqaure. Since the computed width is positive for two of the sides and
336 * negative for the other two, we subtract the area outside the
337 * quadrilateral without any extra code.
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; /* Next point in cyclical order */
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 /* Compute the endpoints of the line segment that
355 * lie inside the border (possibly the whole segment)
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 /* If both deltas have the same signe there is no baundary
367 * crossing
368 */
369 if (positive[0] == positive[1]) {
370 /* A diagram will convince that you decide a point is
371 * inside or outside the boundary by the following test
372 */
373 if (positive[0] == iside) {
374 /* Segment is entirely outside the boundary */
375 if (count == 0) {
376 /* Implicitly multiplied by 1.0, the square height
377 */
378 width = segment[1][0] - segment[0][0];
379 area += width;
380 } else {
381 goto _nextsegment;
382 }
383
384 } else {
385 /* Segment entirely within the boundary */
386 if (count == 0) {
387 /* Use the trapezoid formula to compute the area
388 * under the segment. Delta is the distance to the
389 * top of the square and is negative or zero for the
390 * segment inside the square
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 /* If ends of the line segment are on opposite sides of the
399 * boundary, calculate midpoint, the point of intersection
400 */
401 outside = positive[iside];
402 jdim = (idim + 1) & 01; /* the other dimension */
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 /* If a segment cross the boundary the formula for its
411 * area is a combination of the formulas for segments
412 * entirely inside and outside.
413 */
414 if (outside == 0) {
415 width = midpoint[0] - segment[0][0];
416 area += width;
417 width = segment[1][0] - midpoint[0];
418 /* Delta[0] is at the crossing point and thus zero
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 /* Delta[1] is at the crossing point and thus zero
426 */
427 area += 0.5 * width * (2.0 + delta[0]);
428 }
429
430 } else {
431 /* Clip segment against each boundary except the last */
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 * Calculate overlap between an arbitrary rectangle, aligned with the axes, and
448 * a pixel. This is a simplified version of the compute_area, only valid if axes
449 * are nearly aligned. Used by do_kernel_turbo.
450 *
451 * i: the x coordinate of a pixel on the output image
452 * j: the y coordinate of a pixel on the output image
453 * xmin: the x coordinate of the lower edge of rectangle containing flux of
454 * input pixel xmax: the x coordinate of the upper edge of rectangle containing
455 * flux of input pixel ymin: the y coordinate of the lower edge of rectangle
456 * containing flux of input pixel ymax: the y coordinate of the upper edge of
457 * rectangle containing flux of input pixel
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 * @brief Computes the pixel scale ratio (pscale_ratio) at the centroid of a
482 * bounding polygon.
483 *
484 * This function estimates the pixel scale ratio (pscale_ratio) for a given
485 * region defined by a bounding polygon within a pixmap. The scale is computed
486 * at the centroid of the polygon by mapping the centroid to pixel coordinates,
487 * then calculating the local transformation matrix determinant using
488 * neighboring pixels.
489 *
490 * TODO: in the future, we could estimate pscale_ratio for each pixel - a
491 * varying pscale_ratio across the input image that is more accurate for
492 * distorted input images although this will have significant cost penalty.
493 *
494 * @param p Pointer to the driz_param_t structure containing pixmap and error
495 * information.
496 * @param bounding_polygon Pointer to the polygon structure representing the
497 * region of interest.
498 * @param pscale_ratio Pointer to a float where the computed pixel scale ratio
499 * will be stored.
500 *
501 * @return 0 on success, 1 on failure. On failure, an error message is set in
502 * p->error.
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 // estimate pixel scale ratio:
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 * The kernel assumes all the flux in an input pixel is at the center
549 *
550 * p: structure containing options, input, and output
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 /* This is the outer loop over all the lines in the input image */
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 /* Check the overlap with the output */
606 n = get_scanline_limits(&s, j, &xmin, &xmax);
607 if (n == 1) {
608 // scan ended (y reached the top vertex/edge)
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 // pixel centered on y is outside of scanner's limits or image
614 // [0, height - 1] OR: limits (x1, x2) are equal (line width is
615 // 0)
616 p->nmiss += (p->xmax - p->xmin);
617 ++p->nskip;
618 continue;
619 } else {
620 // limits (x1, x2) are equal (line width is 0)
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 /* Check it is on the output image */
635 if (ii < 0 || ii >= osize[0] || jj < 0 || jj >= osize[1]) {
636 ++p->nmiss;
637
638 } else {
639 /* Allow for stretching because of scale change */
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 /* Scale the weighting mask by the scale factor. Note
650 that we DON'T scale by the Jacobian as it hasn't been
651 calculated */
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 /* If we are creating or modifying the context image,
663 we do so here. */
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 * This kernel assumes the flux is distributed acrass a gaussian around the
683 * center of an input pixel
684 *
685 * p: structure containing options, input, and output
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 /* Added in V2.9 - make sure pfo doesn't get less than 1.2
704 divided by the scale so that there are never holes in the
705 output */
706
707 if (init_image_scanner(p, &s, &ymin, &ymax)) {
708 return 1;
709 }
710
711 /* if pscale_ratio is not defined, estimate it near the center of the
712 intersection polygon.
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 /* This is the outer loop over all the lines in the input image */
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 /* Check the overlap with the output */
764 n = get_scanline_limits(&s, j, &xmin, &xmax);
765 if (n == 1) {
766 // scan ended (y reached the top vertex/edge)
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 // pixel centered on y is outside of scanner's limits or image
772 // [0, height - 1] OR: limits (x1, x2) are equal (line width is
773 // 0)
774 p->nmiss += (p->xmax - p->xmin);
775 ++p->nskip;
776 continue;
777 } else {
778 // limits (x1, x2) are equal (line width is 0)
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 /* Offset within the subset */
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 /* Allow for stretching because of scale change */
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 /* Scale the weighting mask by the scale factor and
808 inversely by the Jacobian to ensure conservation of
809 weight in the output
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 /* Loop over output pixels which could be affected */
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 /* Radial distance */
823 r2 = ddx * ddx + ddy * ddy;
824
825 /* Weight is a scaled Gaussian function of radial
826 distance */
827 dover = gaussian_es * exp(-r2 * gaussian_efac);
828
829 /* Count the hits */
830 ++nhit;
831
832 dow = (float) dover * w;
833
834 /* If we are creating or modifying the context
835 image, we do so here. */
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 /* Count cases where the pixel is off the output image */
853 if (nhit == 0) {
854 ++p->nmiss;
855 }
856 }
857 }
858
859 free(d2);
860 return 0;
861 }
862
863 /** ---------------------------------------------------------------------------
864 * This kernel assumes flux of input pixel is distributed according to
865 * lanczos function
866 *
867 * p: structure containing options, input, and output
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 /* Set up a look-up-table for Lanczos-style interpolation
899 kernels */
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 /* if pscale_ratio is not defined, estimate it near the center of the
912 intersection polygon.
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 /* This is the outer loop over all the lines in the input image */
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 /* Check the overlap with the output */
960 n = get_scanline_limits(&s, j, &xmin, &xmax);
961 if (n == 1) {
962 // scan ended (y reached the top vertex/edge)
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 // pixel centered on y is outside of scanner's limits or image
968 // [0, height - 1] OR: limits (x1, x2) are equal (line width is
969 // 0)
970 p->nmiss += (p->xmax - p->xmin);
971 ++p->nskip;
972 continue;
973 } else {
974 // limits (x1, x2) are equal (line width is 0)
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 /* Allow for stretching because of scale change */
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 /* Scale the weighting mask by the scale factor and
1001 inversely by the Jacobian to ensure conservation of
1002 weight in the output
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 /* Loop over output pixels which could be affected */
1011 for (jj = nyi; jj <= nya; ++jj) {
1012 for (ii = nxi; ii <= nxa; ++ii) {
1013 /* X and Y offsets */
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 /* Count the hits */
1021 ++nhit;
1022
1023 /* Weight is product of Lanczos function values in X
1024 * and
1025 * Y */
1026 dover = lut[ix] * lut[iy];
1027
1028 dow = (float) (dover * w);
1029
1030 /* If we are creating or modifying the context
1031 image, we do so here. */
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 /* Count cases where the pixel is off the output image */
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 * This kernel assumes the input flux is evenly distributed over a rectangle
1064 * whose sides are aligned with the ouput pixel. Called turbo because it is
1065 * fast, but approximate.
1066 *
1067 * p: structure containing options, input, and output
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 /* if pscale_ratio is not defined, estimate it near the center of the
1093 intersection polygon.
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 /* This is the outer loop over all the lines in the input image */
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 /* Check the overlap with the output */
1136 n = get_scanline_limits(&s, j, &xmin, &xmax);
1137
1138 if (n == 1) {
1139 // scan ended (y reached the top vertex/edge)
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 // pixel centered on y is outside of scanner's limits or image
1145 // [0, height - 1] OR: limits (x1, x2) are equal (line width is
1146 // 0)
1147 p->nmiss += (p->xmax - p->xmin);
1148 ++p->nskip;
1149 continue;
1150 } else {
1151 // limits (x1, x2) are equal (line width is 0)
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 /* Offset within the subset */
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 /* Allow for stretching because of scale change */
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 /* Scale the weighting mask by the scale factor and
1186 inversely by the Jacobian to ensure conservation of
1187 weight in the output.
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 /* Loop over the output pixels which could be affected */
1196 for (jj = jjs; jj <= jje; ++jj) {
1197 for (ii = iis; ii <= iie; ++ii) {
1198 /* Calculate the overlap using the simpler "aligned"
1199 box routine */
1200 dover = over(ii, jj, xxi, xxa, yyi, yya);
1201
1202 if (dover > 0.0) {
1203 /* Correct for the pixfrac area factor */
1204 dover *= dover_scale;
1205
1206 /* Count the hits */
1207 ++nhit;
1208
1209 dow = (float) (dover * w);
1210
1211 /* If we are creating or modifying the context
1212 image, we do so here. */
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 /* Count cases where the pixel is off the output image */
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 * This module does the actual mapping of input flux to output images. It
1242 * works by calculating the positions of the four corners of a quadrilateral
1243 * on the output grid corresponding to the corners of the input pixel and
1244 * then working out exactly how much of each pixel in the output is covered,
1245 * or not.
1246 *
1247 * p: structure containing options, input, and output
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 /* Next the "classic" drizzle square kernel... this is different
1272 because we have to transform all four corners of the shrunken
1273 pixel */
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 /* This is the outer loop over all the lines in the input image */
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 /* Check the overlap with the output */
1313 n = get_scanline_limits(&s, j, &xmin, &xmax);
1314 if (n == 1) {
1315 // scan ended (y reached the top vertex/edge)
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 // pixel centered on y is outside of scanner's limits or image
1321 // [0, height - 1] OR: limits (x1, x2) are equal (line width is
1322 // 0)
1323 p->nmiss += (p->xmax - p->xmin);
1324 ++p->nskip;
1325 continue;
1326 } else {
1327 // limits (x1, x2) are equal (line width is 0)
1328 p->nmiss += (p->xmax - p->xmin) - (xmax + 1 - xmin);
1329 }
1330
1331 /* Set the input corner positions */
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 // xin[3] = xin[0] = (double)i - dh;
1339 // xin[2] = xin[1] = (double)i + dh;
1340
1341 // for (ii = 0; ii < 4; ++ii) {
1342 // if (interpolate_point(p, xin[ii], yin[ii], xout + ii,
1343 // yout + ii)) {
1344 // goto _miss;
1345 // }
1346 // }
1347
1348 /* Assuming we don't need to extrapolate, call a more
1349 * efficient interpolator that takes advantage of the fact
1350 * that pixfrac<1 and that we are using a square grid.
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 /* Work out the area of the quadrilateral on the output
1369 * grid. If the points are in clockwise order we get a
1370 * postive area. If they are in anticlockwise order, jaco
1371 * will be negative, but so will the areas computed by
1372 * boxer, so it doesn't actually matter once we divide it
1373 * out.
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 /* Allow for stretching because of scale change */
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 /* Scale the weighting mask by the scale factor and inversely by
1390 the Jacobian to ensure conservation of weight in the output
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 /* Loop over output pixels which could be affected */
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 /* Call boxer to calculate overlap */
1407 // dover = compute_area((double)ii, (double)jj, xout,
1408 // yout);
1409 dover = boxer((double) ii, (double) jj, xout, yout);
1410
1411 /* Could be positive or negative, depending on the sign
1412 * of jaco */
1413 if (dover != 0.0) {
1414 dow = (float) (dover * w);
1415
1416 /* Count the hits */
1417 ++nhit;
1418
1419 /* If we are creating or modifying the context image
1420 we do so here */
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 /* Count cases where the pixel is off the output image */
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 * The kernel assumes all the flux in an input pixel is at the center
1452 *
1453 * p: structure containing options, input, and output
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 /* This is the outer loop over all the lines in the input image */
1476 get_dimensions(p->output_data, osize);
1477 for (j = ymin; j <= ymax; ++j) {
1478 /* Check the overlap with the output */
1479 n = get_scanline_limits(&s, j, &xmin, &xmax);
1480 if (n == 1) {
1481 // scan ended (y reached the top vertex/edge)
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 // pixel centered on y is outside of scanner's limits or image
1487 // [0, height - 1] OR: limits (x1, x2) are equal (line width is
1488 // 0)
1489 p->nmiss += (p->xmax - p->xmin);
1490 ++p->nskip;
1491 continue;
1492 } else {
1493 // limits (x1, x2) are equal (line width is 0)
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 /* Check it is on the output image */
1508 if (ii < 0 || ii >= osize[0] || jj < 0 || jj >= osize[1]) {
1509 ++p->nmiss;
1510
1511 } else {
1512 /* Allow for stretching because of scale change */
1513 d = get_pixel(p->data, i, j) * p->iscale;
1514
1515 /* Scale the weighting mask by the scale factor. Note
1516 that we DON'T scale by the Jacobian as it hasn't been
1517 calculated */
1518 if (p->weights) {
1519 dow = get_pixel(p->weights, i, j) * p->weight_scale;
1520 } else {
1521 dow = 1.0f;
1522 }
1523
1524 /* If we are creating or modifying the context image,
1525 we do so here. */
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 * This kernel assumes the flux is distributed acrass a gaussian around the
1543 * center of an input pixel
1544 *
1545 * p: structure containing options, input, and output
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 /* if pscale_ratio is not defined, estimate it near the center of the
1565 intersection polygon.
1566 */
1567 if (!isfinite(p->pscale_ratio) &&
1568 compute_pscale_ratio(p, &s.bounding_polygon, &p->pscale_ratio)) {
1569 return 1;
1570 }
1571
1572 /* Added in V2.9 - make sure pfo doesn't get less than 1.2
1573 divided by the scale so that there are never holes in the
1574 output */
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 /* This is the outer loop over all the lines in the input image */
1590
1591 get_dimensions(p->output_data, osize);
1592 for (j = ymin; j <= ymax; ++j) {
1593 /* Check the overlap with the output */
1594 n = get_scanline_limits(&s, j, &xmin, &xmax);
1595 if (n == 1) {
1596 // scan ended (y reached the top vertex/edge)
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 // pixel centered on y is outside of scanner's limits or image
1602 // [0, height - 1] OR: limits (x1, x2) are equal (line width is
1603 // 0)
1604 p->nmiss += (p->xmax - p->xmin);
1605 ++p->nskip;
1606 continue;
1607 } else {
1608 // limits (x1, x2) are equal (line width is 0)
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 /* Offset within the subset */
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 /* Allow for stretching because of scale change */
1628 d = get_pixel(p->data, i, j) * p->iscale;
1629
1630 /* Scale the weighting mask by the scale factor and
1631 inversely by the Jacobian to ensure conservation of
1632 weight in the output
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 /* Loop over output pixels which could be affected */
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 /* Radial distance */
1646 r2 = ddx * ddx + ddy * ddy;
1647
1648 /* Weight is a scaled Gaussian function of radial
1649 distance */
1650 dover = gaussian_es * exp(-r2 * gaussian_efac);
1651
1652 /* Count the hits */
1653 ++nhit;
1654
1655 dow = (float) dover * w;
1656
1657 /* If we are creating or modifying the context
1658 image, we do so here. */
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 /* Count cases where the pixel is off the output image */
1671 if (nhit == 0) {
1672 ++p->nmiss;
1673 }
1674 }
1675 }
1676
1677 return 0;
1678 }
1679
1680 /** ---------------------------------------------------------------------------
1681 * This kernel assumes flux of input pixel is distributed according to
1682 * lanczos function
1683 *
1684 * p: structure containing options, input, and output
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 /* Set up a look-up-table for Lanczos-style interpolation
1713 kernels */
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 /* if pscale_ratio is not defined, estimate it near the center of the
1726 intersection polygon.
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 /* This is the outer loop over all the lines in the input image */
1740
1741 get_dimensions(p->output_data, osize);
1742 for (j = ymin; j <= ymax; ++j) {
1743 /* Check the overlap with the output */
1744 n = get_scanline_limits(&s, j, &xmin, &xmax);
1745 if (n == 1) {
1746 // scan ended (y reached the top vertex/edge)
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 // pixel centered on y is outside of scanner's limits or image
1752 // [0, height - 1] OR: limits (x1, x2) are equal (line width is
1753 // 0)
1754 p->nmiss += (p->xmax - p->xmin);
1755 ++p->nskip;
1756 continue;
1757 } else {
1758 // limits (x1, x2) are equal (line width is 0)
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 /* Allow for stretching because of scale change */
1775 d = get_pixel(p->data, i, j) * p->iscale;
1776
1777 /* Scale the weighting mask by the scale factor and
1778 inversely by the Jacobian to ensure conservation of
1779 weight in the output
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 /* Loop over output pixels which could be affected */
1788 for (jj = nyi; jj <= nya; ++jj) {
1789 for (ii = nxi; ii <= nxa; ++ii) {
1790 /* X and Y offsets */
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 /* Count the hits */
1798 ++nhit;
1799
1800 /* Weight is product of Lanczos function values in X
1801 * and
1802 * Y */
1803 dover = lut[ix] * lut[iy];
1804
1805 dow = (float) (dover * w);
1806
1807 /* If we are creating or modifying the context
1808 image, we do so here. */
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 /* Count cases where the pixel is off the output image */
1822 if (nhit == 0) {
1823 ++p->nmiss;
1824 }
1825 }
1826 }
1827
1828 free(lut);
1829 return 0;
1830 }
1831
1832 /** ---------------------------------------------------------------------------
1833 * This kernel assumes the input flux is evenly distributed over a rectangle
1834 * whose sides are aligned with the ouput pixel. Called turbo because it is
1835 * fast, but approximate.
1836 *
1837 * p: structure containing options, input, and output
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 /* if pscale_ratio is not defined, estimate it near the center of the
1860 intersection polygon.
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 /* This is the outer loop over all the lines in the input image */
1874
1875 get_dimensions(p->output_data, osize);
1876 for (j = ymin; j <= ymax; ++j) {
1877 /* Check the overlap with the output */
1878 n = get_scanline_limits(&s, j, &xmin, &xmax);
1879
1880 if (n == 1) {
1881 // scan ended (y reached the top vertex/edge)
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 // pixel centered on y is outside of scanner's limits or image
1887 // [0, height - 1] OR: limits (x1, x2) are equal (line width is
1888 // 0)
1889 p->nmiss += (p->xmax - p->xmin);
1890 ++p->nskip;
1891 continue;
1892 } else {
1893 // limits (x1, x2) are equal (line width is 0)
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 /* Offset within the subset */
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 /* Allow for stretching because of scale change */
1918 d = get_pixel(p->data, i, j) * p->iscale;
1919
1920 /* Scale the weighting mask by the scale factor and
1921 inversely by the Jacobian to ensure conservation of
1922 weight in the output.
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 /* Loop over the output pixels which could be affected */
1931 for (jj = jjs; jj <= jje; ++jj) {
1932 for (ii = iis; ii <= iie; ++ii) {
1933 /* Calculate the overlap using the simpler "aligned"
1934 box routine */
1935 dover = over(ii, jj, xxi, xxa, yyi, yya);
1936
1937 if (dover > 0.0) {
1938 /* Correct for the pixfrac area factor */
1939 dover *= dover_scale;
1940
1941 /* Count the hits */
1942 ++nhit;
1943
1944 dow = (float) (dover * w);
1945
1946 /* If we are creating or modifying the context
1947 image, we do so here. */
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 /* Count cases where the pixel is off the output image */
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 * This module does the actual mapping of input flux to output images. It
1973 * works by calculating the positions of the four corners of a quadrilateral
1974 * on the output grid corresponding to the corners of the input pixel and
1975 * then working out exactly how much of each pixel in the output is covered,
1976 * or not.
1977 *
1978 * p: structure containing options, input, and output
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 /* Next the "classic" drizzle square kernel... this is different
1998 because we have to transform all four corners of the shrunken
1999 pixel */
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 /* This is the outer loop over all the lines in the input image */
2008 get_dimensions(p->output_data, osize);
2009 get_dimensions(p->pixmap, mapsize);
2010
2011 for (j = ymin; j <= ymax; ++j) {
2012 /* Check the overlap with the output */
2013 n = get_scanline_limits(&s, j, &xmin, &xmax);
2014 if (n == 1) {
2015 // scan ended (y reached the top vertex/edge)
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 // pixel centered on y is outside of scanner's limits or image
2021 // [0, height - 1] OR: limits (x1, x2) are equal (line width is
2022 // 0)
2023 p->nmiss += (p->xmax - p->xmin);
2024 ++p->nskip;
2025 continue;
2026 } else {
2027 // limits (x1, x2) are equal (line width is 0)
2028 p->nmiss += (p->xmax - p->xmin) - (xmax + 1 - xmin);
2029 }
2030
2031 /* Set the input corner positions */
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 // xin[3] = xin[0] = (double)i - dh;
2039 // xin[2] = xin[1] = (double)i + dh;
2040
2041 // for (ii = 0; ii < 4; ++ii) {
2042 // if (interpolate_point(p, xin[ii], yin[ii], xout + ii,
2043 // yout + ii)) {
2044 // goto _miss;
2045 // }
2046 // }
2047
2048 /* Assuming we don't need to extrapolate, call a more
2049 * efficient interpolator that takes advantage of the fact
2050 * that pixfrac<1 and that we are using a square grid.
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 /* Work out the area of the quadrilateral on the output
2069 * grid. If the points are in clockwise order we get a
2070 * postive area. If they are in anticlockwise order, jaco
2071 * will be negative, but so will the areas computed by
2072 * boxer, so it doesn't actually matter once we divide it
2073 * out.
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 /* Allow for stretching because of scale change */
2080 d = get_pixel(p->data, i, j) * p->iscale;
2081
2082 /* Scale the weighting mask by the scale factor and inversely by
2083 the Jacobian to ensure conservation of weight in the output
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 /* Loop over output pixels which could be affected */
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 /* Call boxer to calculate overlap */
2100 // dover = compute_area((double)ii, (double)jj, xout,
2101 // yout);
2102 dover = boxer((double) ii, (double) jj, xout, yout);
2103
2104 /* Could be positive or negative, depending on the sign
2105 * of jaco */
2106 if (dover != 0.0) {
2107 dow = (float) (dover * w);
2108
2109 /* Count the hits */
2110 ++nhit;
2111
2112 /* If we are creating or modifying the context image
2113 we do so here */
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 /* Count cases where the pixel is off the output image */
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 * The user selects a kernel to use for drizzling from a function in the
2139 * following tables The kernels differ in how the flux inside a single pixel
2140 * is allocated: evenly spread across the pixel, concentrated at the central
2141 * point, or by some other function.
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 * The executive function which calls the kernel which does the actual
2154 * drizzling
2155 *
2156 * p: structure containing options, input, and output
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 /* Set up a function pointer to handle the appropriate kernel */
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 }