1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
|
/* $Id: cpl_image_resample.c,v 1.56 2012/03/13 15:46:03 llundin Exp $
*
* This file is part of the ESO Common Pipeline Library
* Copyright (C) 2001-2008 European Southern Observatory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* $Author: llundin $
* $Date: 2012/03/13 15:46:03 $
* $Revision: 1.56 $
* $Name: cpl-6_1_1 $
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*-----------------------------------------------------------------------------
Includes
-----------------------------------------------------------------------------*/
#include "cpl_tools.h"
#include "cpl_memory.h"
#include "cpl_image_resample.h"
#include "cpl_mask.h"
#include "cpl_error_impl.h"
#include "cpl_image_defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <float.h>
/*-----------------------------------------------------------------------------
Defines
-----------------------------------------------------------------------------*/
#define CPL_IMAGE_RESAMPLE_SUBSAMPLE 1
#define CPL_IMAGE_WARP 2
#define CPL_IMAGE_WARP_POLYNOMIAL 3
#define CPL_IMAGE_REBIN 4
#define CONCAT(a,b) a ## _ ## b
#define CONCAT2X(a,b) CONCAT(a,b)
/*-----------------------------------------------------------------------------
Private function prototypes
-----------------------------------------------------------------------------*/
inline static double cpl_image_get_interpolated_double(const double *,
const cpl_binary *,
cpl_size, cpl_size,
double, double,
double, double,
double *,
const double *, double,
const double *, double,
double, double,
double *);
inline static double cpl_image_get_interpolated_float(const float *,
const cpl_binary *,
cpl_size, cpl_size,
double, double,
double, double,
double *,
const double *, double,
const double *, double,
double, double,
double *);
inline static double cpl_image_get_interpolated_int(const int *,
const cpl_binary *,
cpl_size, cpl_size,
double, double,
double, double,
double *,
const double *, double,
const double *, double,
double, double,
double *);
/*-----------------------------------------------------------------------------
Function codes
-----------------------------------------------------------------------------*/
#define CPL_OPERATION CPL_IMAGE_WARP
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Warp an image
@param out Pre-allocated destination image to hold the result
@param in Source image to warp
@param deltax The x shift of each pixel, same image size as out
@param deltay The y shift of each pixel, same image size as out
@param xprofile Interpolation weight as a function of the distance in X
@param xradius Positive inclusion radius in the X-dimension
@param yprofile Interpolation weight as a function of the distance in Y
@param yradius Positive inclusion radius in the Y-dimension
@return CPL_ERROR_NONE or the relevant #_cpl_error_code_ on error
@see cpl_image_get_interpolated()
The pixel value at the (integer) position (u, v) in the destination image is
interpolated from the (typically non-integer) pixel position (x, y) in the
source image, where
@verbatim
x = u - deltax(u, v),
y = v - deltay(u, v).
@endverbatim
The identity transform is thus given by deltax and deltay filled with
zeros.
The first pixel is (1, 1), located in the lower left.
'out' and 'in' may have different sizes, while deltax and deltay must have
the same size as 'out'. deltax and deltay must have pixel type
CPL_TYPE_DOUBLE.
Beware that extreme transformations may lead to blank images.
'out' and 'in' may be of type CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
Examples of profiles and radius are:
@verbatim
xprofile = cpl_vector_new(CPL_KERNEL_DEF_SAMPLES);
cpl_vector_fill_kernel_profile(profile, CPL_KERNEL_DEFAULT,
CPL_KERNEL_DEF_WIDTH);
xradius = CPL_KERNEL_DEF_WIDTH;
@endverbatim
In case a correction for flux conservation were required, please create
a correction map using the function @c cpl_image_fill_jacobian().
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL
- CPL_ERROR_ILLEGAL_INPUT if the input images sizes are incompatible
or if the delta images are not of type CPL_TYPE_DOUBLE
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_warp(
cpl_image * out,
const cpl_image * in,
const cpl_image * deltax,
const cpl_image * deltay,
const cpl_vector * xprofile,
double xradius,
const cpl_vector * yprofile,
double yradius)
{
double x, y;
cpl_mask * bad;
cpl_binary * pbad;
double value, confidence;
int hasbad;
const double sqxradius = xradius * xradius;
const double sqyradius = yradius * yradius;
double sqyxratio;
double xtabsperpix;
double ytabsperpix;
const double * pxprof;
const double * pyprof;
double * yweight;
cpl_size ixprolen;
cpl_size iyprolen;
cpl_size pos;
const double * pdeltax;
const double * pdeltay;
cpl_size i, j;
/* Check entries */
cpl_ensure_code(out, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(in, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(deltax, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(deltay, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(cpl_image_get_size_x(deltax) == cpl_image_get_size_x(out),
CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(cpl_image_get_size_y(deltax) == cpl_image_get_size_y(out),
CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(cpl_image_get_size_x(deltay) == cpl_image_get_size_x(out),
CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(cpl_image_get_size_y(deltay) == cpl_image_get_size_y(out),
CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(cpl_image_get_type(deltax) == CPL_TYPE_DOUBLE,
CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(cpl_image_get_type(deltay) == CPL_TYPE_DOUBLE,
CPL_ERROR_ILLEGAL_INPUT);
ixprolen = cpl_vector_get_size(xprofile);
cpl_ensure_code(ixprolen > 0, CPL_ERROR_ILLEGAL_INPUT);
iyprolen = cpl_vector_get_size(yprofile);
cpl_ensure_code(iyprolen > 0, CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(xradius > 0, CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(yradius > 0, CPL_ERROR_ILLEGAL_INPUT);
/* Initialise */
bad = NULL;
hasbad = 0;
pxprof = cpl_vector_get_data_const(xprofile);
pyprof = cpl_vector_get_data_const(yprofile);
sqyxratio = sqyradius / sqxradius;
xtabsperpix = (double)(ixprolen-1)/xradius;
ytabsperpix = (double)(iyprolen-1)/yradius;
yweight = cpl_malloc((1 + (size_t)(2.0 * yradius)) * sizeof(double));
/* Access the offsets */
pdeltax = cpl_image_get_data_double_const(deltax);
pdeltay = cpl_image_get_data_double_const(deltay);
/* Create the bad pixels mask */
bad = cpl_mask_new(out->nx, out->ny);
pbad = cpl_mask_get_data(bad);
switch (in->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
default:
cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
cpl_free(yweight);
/* Handle bad pixels */
if (hasbad) cpl_image_reject_from_mask(out, bad);
cpl_mask_delete(bad);
return CPL_ERROR_NONE;
}
#undef CPL_OPERATION
#define CPL_OPERATION CPL_IMAGE_WARP_POLYNOMIAL
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Warp an image according to a 2D polynomial transformation.
@param out Pre-allocated image to hold the result
@param in Image to warp.
@param poly_x Defines source x-pos corresponding to destination (u,v).
@param poly_y Defines source y-pos corresponding to destination (u,v).
@param xprofile Interpolation weight as a function of the distance in X
@param xradius Positive inclusion radius in the X-dimension
@param yprofile Interpolation weight as a function of the distance in Y
@param yradius Positive inclusion radius in the Y-dimension
@return CPL_ERROR_NONE or the relevant #_cpl_error_code_ on error
@see cpl_image_get_interpolated()
'out' and 'in' may have different dimensions and types.
The pair of 2D polynomials are used internally like this
@verbatim
x = cpl_polynomial_eval(poly_x, (u, v));
y = cpl_polynomial_eval(poly_y, (u, v));
@endverbatim
where (u,v) are (integer) pixel positions in the destination image and (x,y)
are the corresponding pixel positions (typically non-integer) in the source
image.
The identity transform (poly_x(u,v) = u, poly_y(u,v) = v) would thus
overwrite the 'out' image with the 'in' image, starting from the lower left
if the two images are of different sizes.
Beware that extreme transformations may lead to blank images.
The input image type may be CPL_TYPE_INT, CPL_TYPE_FLOAT or CPL_TYPE_DOUBLE.
In case a correction for flux conservation were required, please create
a correction map using the function @c cpl_image_fill_jacobian_polynomial().
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL
- CPL_ERROR_ILLEGAL_INPUT if the polynomial dimensions are not 2
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_error_code cpl_image_warp_polynomial(
cpl_image * out,
const cpl_image * in,
const cpl_polynomial * poly_x,
const cpl_polynomial * poly_y,
const cpl_vector * xprofile,
double xradius,
const cpl_vector * yprofile,
double yradius)
{
double x, y;
cpl_vector * val = NULL;
double * pval;
cpl_mask * bad = NULL;
cpl_binary * pbad;
double value, confidence;
cpl_size i, j;
int hasbad = 0;
const double sqxradius = xradius * xradius;
const double sqyradius = yradius * yradius;
double sqyxratio;
double xtabsperpix;
double ytabsperpix;
const double * pxprof;
const double * pyprof;
double * yweight;
cpl_size ixprolen;
cpl_size iyprolen;
/* Check entries */
cpl_ensure_code(out, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(in, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(poly_x, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(poly_y, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(cpl_polynomial_get_dimension(poly_x) == 2,
CPL_ERROR_ILLEGAL_INPUT);
cpl_ensure_code(cpl_polynomial_get_dimension(poly_y) == 2,
CPL_ERROR_ILLEGAL_INPUT);
ixprolen = cpl_vector_get_size(xprofile);
cpl_ensure(ixprolen > 0, cpl_error_get_code(), -3);
iyprolen = cpl_vector_get_size(yprofile);
cpl_ensure(iyprolen > 0, cpl_error_get_code(), -4);
cpl_ensure(xradius > 0, CPL_ERROR_ILLEGAL_INPUT, -5);
cpl_ensure(yradius > 0, CPL_ERROR_ILLEGAL_INPUT, -6);
pxprof = cpl_vector_get_data_const(xprofile);
pyprof = cpl_vector_get_data_const(yprofile);
sqyxratio = sqyradius/sqxradius;
xtabsperpix = (double)(ixprolen-1) / xradius;
ytabsperpix = (double)(iyprolen-1) / yradius;
yweight = cpl_malloc((1 + (size_t)(2.0 * yradius)) * sizeof(double));
val = cpl_vector_new(2);
pval = cpl_vector_get_data(val);
bad = cpl_mask_new(out->nx, out->ny);
pbad = cpl_mask_get_data(bad);
switch (in->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
default:
cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
cpl_vector_delete(val);
cpl_free(yweight);
if (hasbad) cpl_image_reject_from_mask(out, bad);
cpl_mask_delete(bad);
return CPL_ERROR_NONE;
}
#undef CPL_OPERATION
#define CPL_OPERATION CPL_IMAGE_RESAMPLE_SUBSAMPLE
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Sub-sample an image
@param image The image to subsample
@param xstep Take every xstep pixel in x
@param ystep Take every ystep pixel in y
@return The newly allocated sub-sampled image or NULL in error case
@see cpl_image_extract
step represents the sampling step in x and y: both steps = 2 will create an
image with a quarter of the pixels of the input image.
image type can be CPL_TYPE_INT, CPL_TYPE_FLOAT and CPL_TYPE_DOUBLE.
If the image has bad pixels, they will be resampled in the same way.
The returned image must be deallocated using cpl_image_delete().
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if the input pointer is NULL
- CPL_ERROR_ILLEGAL_INPUT if xstep, ystep are not positive
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_image *cpl_image_extract_subsample(const cpl_image *image,
cpl_size xstep,
cpl_size ystep)
{
cpl_image *out_im;
cpl_size new_nx, new_ny;
cpl_size i, j;
cpl_ensure(image != NULL, CPL_ERROR_NULL_INPUT, NULL);
cpl_ensure(xstep > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
cpl_ensure(ystep > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
new_nx = (image->nx - 1)/xstep + 1;
new_ny = (image->ny - 1)/ystep + 1;
switch (image->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
default:
(void)cpl_error_set_(CPL_ERROR_INVALID_TYPE);
return NULL;
}
/* Sub sample the bad pixel map too */
if (image->bpm != NULL)
out_im->bpm = cpl_mask_extract_subsample(image->bpm, xstep, ystep);
return out_im;
}
#undef CPL_OPERATION
#define CPL_OPERATION CPL_IMAGE_REBIN
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Rebin an image
@param image The image to rebin
@param xstart start x position of binning (starting from 1...)
@param ystart start y position of binning (starting from 1...)
@param xstep Bin size in x.
@param ystep Bin size in y.
@return The newly allocated rebinned image or NULL in case of error
If both bin sizes in x and y are = 2, an image with (about) a quarter
of the pixels of the input image will be created. Each new pixel
will be the sum of the values of all contributing input pixels.
If a bin is incomplete (i.e., the input image size is not a multiple
of the bin sizes), it is not computed.
xstep and ystep must not be greater than the sizes of the rebinned
region.
The input image type can be CPL_TYPE_INT, CPL_TYPE_FLOAT and CPL_TYPE_DOUBLE.
If the image has bad pixels, they will be propagated to the rebinned
image "pessimistically", i.e., if at least one of the contributing
input pixels is bad, then the corresponding output pixel will also
be flagged "bad". If you need an image of "weights" for each rebinned
pixel, just cast the input image bpm into a CPL_TYPE_INT image, and
apply cpl_image_rebin() to it too.
The returned image must be deallocated using cpl_image_delete().
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if the input pointer is NULL
- CPL_ERROR_ILLEGAL_INPUT if xstep, ystep, xstart, ystart are not positive
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
cpl_image *cpl_image_rebin(const cpl_image *image,
cpl_size xstart,
cpl_size ystart,
cpl_size xstep,
cpl_size ystep)
{
cpl_image *out_im;
cpl_size nx, ny;
cpl_size new_nx, new_ny;
const cpl_size old_nx = cpl_image_get_size_x(image);
cpl_size i, j;
cpl_ensure(image != NULL, CPL_ERROR_NULL_INPUT, NULL);
cpl_ensure(xstart > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
cpl_ensure(ystart > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
cpl_ensure(xstep > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
cpl_ensure(ystep > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
new_nx = (image->nx - xstart + 1)/xstep;
new_ny = (image->ny - ystart + 1)/ystep;
cpl_ensure(new_nx > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
cpl_ensure(new_ny > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
nx = new_nx * xstep + xstart - 1;
ny = new_ny * ystep + ystart - 1;
switch (image->type) {
#define CPL_CLASS CPL_CLASS_DOUBLE
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
default:
(void)cpl_error_set_(CPL_ERROR_INVALID_TYPE);
return NULL;
}
/* Propagate the bad pixel map if present */
if (image->bpm != NULL) {
const cpl_binary *pin;
cpl_binary *pout;
cpl_size pos;
out_im->bpm = cpl_mask_new(new_nx, new_ny);
pin = cpl_mask_get_data_const(image->bpm);
pout = cpl_mask_get_data(out_im->bpm);
for (j = ystart - 1; j < ny; j++) {
for (i = xstart - 1; i < nx; i++) {
pos = (i-xstart+1)/xstep + ((j-ystart+1)/ystep)*new_nx;
if (pout[pos] == CPL_BINARY_0)
pout[pos] = pin[i + j*nx];
}
}
}
return out_im;
}
#undef CPL_OPERATION
/*----------------------------------------------------------------------------*/
/**
@ingroup cpl_image
@brief Interpolate a pixel
@param source Interpolation source
@param xpos Pixel x floating-point position (FITS convention)
@param ypos Pixel y floating-point position (FITS convention)
@param xprofile Interpolation weight as a function of the distance in X
@param xradius Positive inclusion radius in the X-dimension
@param yprofile Interpolation weight as a function of the distance in Y
@param yradius Positive inclusion radius in the Y-dimension
@param pconfid Confidence level of the interpolated value (range 0 to 1)
@return The interpolated pixel value, or undefined on error
@see cpl_image_get()
If the X- and Y-radii are identical the area of inclusion is a circle,
otherwise it is an ellipse, with the larger of the two radii as the
semimajor axis and the other as the semiminor axis.
The radii are only required to be positive. However, for small radii,
especially radii less than 1/sqrt(2), (xpos, ypos) may be located such that
no source pixels are included in the interpolation, causing the interpolated
pixel value to be undefined.
The X- and Y-profiles can be generated with
cpl_vector_fill_kernel_profile(profile, radius).
For profiles generated with cpl_vector_fill_kernel_profile() it is
important to use the same radius both there and in
cpl_image_get_interpolated().
A good profile length is CPL_KERNEL_DEF_SAMPLES,
using radius CPL_KERNEL_DEF_WIDTH.
On error *pconfid is negative (unless pconfid is NULL).
Otherwise, if *pconfid is zero, the interpolated pixel-value is undefined.
Otherwise, if *pconfid is less than 1, the area of inclusion is close to the
image border or contains rejected pixels.
The input image type can be CPL_TYPE_INT, CPL_TYPE_FLOAT and CPL_TYPE_DOUBLE.
Here is an example of a simple image unwarping (with error-checking omitted
for brevity):
const double xyradius = CPL_KERNEL_DEF_WIDTH;
cpl_vector * xyprofile = cpl_vector_new(CPL_KERNEL_DEF_SAMPLES);
cpl_image * unwarped = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
cpl_vector_fill_kernel_profile(xyprofile, CPL_KERNEL_DEFAULT, xyradius);
for (iv = 1; iv <= ny; iv++) {
for (iu = 1; iu <= nx; iu++) {
double confidence;
const double x = my_unwarped_x();
const double y = my_unwarped_y();
const double value = cpl_image_get_interpolated(warped, x, y,
xyprofile, xyradius,
xyprofile, xyradius,
&confidence);
if (confidence > 0)
cpl_image_set(unwarped, iu, iv, value);
else
cpl_image_reject(unwarped, iu, iv);
}
}
cpl_vector_delete(xyprofile);
Possible #_cpl_error_code_ set in this function:
- CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL
- CPL_ERROR_ILLEGAL_INPUT if xradius, xprofile, yprofile and yradius
are not as requested
- CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
/*----------------------------------------------------------------------------*/
double cpl_image_get_interpolated(const cpl_image * source,
double xpos,
double ypos,
const cpl_vector * xprofile,
double xradius,
const cpl_vector * yprofile,
double yradius,
double * pconfid)
{
const double * pxprof = cpl_vector_get_data_const(xprofile);
const double * pyprof = cpl_vector_get_data_const(yprofile);
const double sqxradius = xradius * xradius;
const double sqyradius = yradius * yradius;
double sqyxratio;
double xtabsperpix, ytabsperpix;
const cpl_size nx = cpl_image_get_size_x(source);
const cpl_size ny = cpl_image_get_size_y(source);
const cpl_size ixprolen = cpl_vector_get_size(xprofile);
const cpl_size iyprolen = cpl_vector_get_size(yprofile);
/* Use the stack except for ridiculously large radii that would use
too much. A negative Y radius is cast to a very large number */
const size_t ywsize = 1 + (size_t)(2.0 * yradius);
double syweight[ywsize < 200 ? ywsize : 1];
double * yweight;
double value;
cpl_ensure(pconfid !=NULL, CPL_ERROR_NULL_INPUT, -1);
*pconfid = -1;
cpl_ensure(nx > 0, cpl_error_get_code(), -2);
cpl_ensure(ixprolen > 0, cpl_error_get_code(), -3);
cpl_ensure(iyprolen > 0, cpl_error_get_code(), -4);
cpl_ensure(xradius > 0, CPL_ERROR_ILLEGAL_INPUT, -5);
cpl_ensure(yradius > 0, CPL_ERROR_ILLEGAL_INPUT, -6);
xtabsperpix = (double)(ixprolen-1) / xradius;
ytabsperpix = (double)(iyprolen-1) / yradius;
sqyxratio = sqyradius/sqxradius;
yweight = ywsize < 200 ? syweight : cpl_malloc(ywsize * sizeof(double));
switch (source->type) {
case CPL_TYPE_DOUBLE:
value = cpl_image_get_interpolated_double
((const double*)source->pixels,
source->bpm ? cpl_mask_get_data_const(source->bpm) : NULL,
nx, ny,
xtabsperpix, ytabsperpix,
xpos, ypos, yweight,
pxprof, xradius,
pyprof, yradius,
sqyradius, sqyxratio,
pconfid);
break;
case CPL_TYPE_FLOAT:
value = cpl_image_get_interpolated_float
((const float*)source->pixels,
source->bpm ? cpl_mask_get_data_const(source->bpm) : NULL,
nx, ny,
xtabsperpix, ytabsperpix,
xpos, ypos, yweight,
pxprof, xradius,
pyprof, yradius,
sqyradius, sqyxratio,
pconfid);
break;
case CPL_TYPE_INT:
value = cpl_image_get_interpolated_int
((const int*)source->pixels,
source->bpm ? cpl_mask_get_data_const(source->bpm) : NULL,
nx, ny,
xtabsperpix, ytabsperpix,
xpos, ypos, yweight,
pxprof, xradius,
pyprof, yradius,
sqyradius, sqyxratio,
pconfid);
break;
default:
value = 0;
cpl_error_set_(CPL_ERROR_INVALID_TYPE);
}
if (yweight != syweight)
cpl_free(yweight);
return value;
}
/**
@ingroup cpl_image
* @brief Compute area change ratio for a 2D polynomial transformation.
*
* @param out Pre-allocated image to hold the result
* @param poly_x Defines source x-pos corresponding to destination (u,v).
* @param poly_y Defines source y-pos corresponding to destination (u,v).
*
* @return CPL_ERROR_NONE or the relevant #_cpl_error_code_ on error
*
* @see cpl_image_warp_polynomial()
*
*
* Given an input image with pixel coordinates (x, y) which is
* mapped into an output image with pixel coordinates (u, v), and
* the polynomial inverse transformation (u, v) to (x, y) as in
* @ref cpl_image_warp_polynomial(), this function writes the density
* of the (u, v) coordinate system relative to the (x, y) coordinates
* for each (u, v) pixel of image @em out.
*
* This is trivially obtained by computing the absolute value of the
* determinant of the Jacobian of the transformation for each pixel
* of the (u, v) image @em out.
*
* Typically this function would be used to determine a flux-conservation
* factor map for the target image specified in function
* @c cpl_image_warp_polynomial(). For example,
*
* @verbatim
* cpl_image_warp_polynomial(out, in, poly_x, poly_y, xprof, xrad, yprof, yrad);
* correction_map = cpl_image_new(cpl_image_get_size_x(out),
* cpl_image_get_size_y(out),
* cpl_image_get_type(out));
* cpl_image_fill_jacobian_polynomial(correction_map, poly_x, poly_y);
* out_flux_corrected = cpl_image_multiply_create(out, correction_map);
* @endverbatim
*
* where @em out_flux_corrected is the resampled image @em out after
* correction for flux conservation.
*
* @note
* The map produced by this function is not applicable for
* flux conservation in case the transformation implies severe
* undersampling of the original signal.
*
* Possible #_cpl_error_code_ set in this function:
* - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL
* - CPL_ERROR_ILLEGAL_INPUT if the polynomial dimensions are not 2
* - CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
cpl_error_code cpl_image_fill_jacobian_polynomial(cpl_image *out,
const cpl_polynomial *poly_x,
const cpl_polynomial *poly_y)
{
cpl_polynomial *dxdu;
cpl_polynomial *dxdv;
cpl_polynomial *dydu;
cpl_polynomial *dydv;
cpl_vector *val;
double *pval;
double *ddata;
float *fdata;
cpl_size nx, ny;
cpl_size i, j;
cpl_error_code error = CPL_ERROR_NONE;
if (out == NULL || poly_x == NULL || poly_y == NULL)
return cpl_error_set_(CPL_ERROR_NULL_INPUT);
if (cpl_polynomial_get_dimension(poly_x) != 2 ||
cpl_polynomial_get_dimension(poly_y) != 2)
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
if (cpl_image_get_type(out) != CPL_TYPE_FLOAT &&
cpl_image_get_type(out) != CPL_TYPE_DOUBLE)
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
/*
* Compute the partial derivatives of the transformation:
*/
dxdu = cpl_polynomial_duplicate(poly_x);
dxdv = cpl_polynomial_duplicate(poly_x);
dydu = cpl_polynomial_duplicate(poly_y);
dydv = cpl_polynomial_duplicate(poly_y);
cpl_polynomial_derivative(dxdu, 0);
cpl_polynomial_derivative(dxdv, 1);
cpl_polynomial_derivative(dydu, 0);
cpl_polynomial_derivative(dydv, 1);
/*
* This section is for assigning the (local) scaling factor
* to each pixel of the reference image.
*/
nx = cpl_image_get_size_x(out);
ny = cpl_image_get_size_y(out);
val = cpl_vector_new(2);
pval = cpl_vector_get_data(val);
switch (cpl_image_get_type(out)) {
case CPL_TYPE_FLOAT:
fdata = cpl_image_get_data_float(out);
for (j=0; j < ny; j++) {
pval[1] = (double)(j + 1);
for (i=0; i < nx; i++) {
pval[0] = (double)(i + 1);
*fdata++ = (float)(cpl_polynomial_eval(dxdu, val)
* cpl_polynomial_eval(dydv, val)
- cpl_polynomial_eval(dxdv, val)
* cpl_polynomial_eval(dydu, val));
}
}
break;
case CPL_TYPE_DOUBLE:
ddata = cpl_image_get_data_double(out);
for (j=0; j < ny; j++) {
pval[1] = (double)(j + 1);
for (i=0; i < nx; i++) {
pval[0] = (double)(i + 1);
*ddata++ = cpl_polynomial_eval(dxdu, val)
* cpl_polynomial_eval(dydv, val)
- cpl_polynomial_eval(dxdv, val)
* cpl_polynomial_eval(dydu, val);
}
}
break;
default:
/* It is an error in CPL to reach this point */
error = CPL_ERROR_UNSPECIFIED;
break;
}
cpl_vector_delete(val);
cpl_polynomial_delete(dxdu);
cpl_polynomial_delete(dxdv);
cpl_polynomial_delete(dydu);
cpl_polynomial_delete(dydv);
/*
* Ensure the scale factor is positive...
*/
if (!error) error = cpl_image_abs(out);
/* Propagate error, if any */
return cpl_error_set_(error);
}
/**
@ingroup cpl_image
* @brief Compute area change ratio for a transformation map.
*
* @param out Pre-allocated image to hold the result
* @param deltax The x shifts for each pixel
* @param deltay The y shifts for each pixel
*
* @return CPL_ERROR_NONE or the relevant #_cpl_error_code_ on error
*
* @see cpl_image_warp()
*
* The shifts images @em deltax and @em deltay, describing the
* transformation, must be of type CPL_TYPE_DOUBLE and of the
* same size as @em out. For each pixel (u, v) of the @em out
* image, the deltax and deltay code the following transformation:
*
* @verbatim
* u - deltax(u,v) = x
* v - deltay(u,v) = y
* @endverbatim
*
* This function writes the density of the (u, v) coordinate
* system relative to the (x, y) coordinates for each (u, v)
* pixel of image @em out.
*
* This is trivially obtained by computing the absolute value of the
* determinant of the Jacobian of the transformation for each pixel
* of the (u, v) image @em out.
*
* The partial derivatives are estimated at the position (u, v)
* in the following way:
*
* @verbatim
* dx/du = 1 + 1/2 ( deltax(u-1, v) - deltax(u+1, v) )
* dx/dv = 1/2 ( deltax(u, v-1) - deltax(u, v+1) )
* dy/du = 1/2 ( deltay(u-1, v) - deltay(u+1, v) )
* dy/dv = 1 + 1/2 ( deltay(u, v-1) - deltay(u, v+1) )
* @endverbatim
*
* Typically this function would be used to determine a flux-conservation
* factor map for the target image specified in function
* @c cpl_image_warp(). For example,
*
* @verbatim
* cpl_image_warp(out, in, deltax, deltay, xprof, xrad, yprof, yrad);
* correction_map = cpl_image_new(cpl_image_get_size_x(out),
* cpl_image_get_size_y(out),
* cpl_image_get_type(out));
* cpl_image_fill_jacobian(correction_map, deltax, deltay);
* out_flux_corrected = cpl_image_multiply_create(out, correction_map);
* @endverbatim
*
* where @em out_flux_corrected is the resampled image @em out after
* correction for flux conservation.
*
* @note
* The map produced by this function is not applicable for
* flux conservation in case the transformation implies severe
* undersampling of the original signal.
*
* Possible #_cpl_error_code_ set in this function:
* - CPL_ERROR_NULL_INPUT if (one of) the input pointer(s) is NULL
* - CPL_ERROR_ILLEGAL_INPUT if the polynomial dimensions are not 2
* - CPL_ERROR_INVALID_TYPE if the passed image type is not supported
*/
cpl_error_code cpl_image_fill_jacobian(cpl_image *out,
const cpl_image *deltax,
const cpl_image *deltay)
{
cpl_image *dxdu;
cpl_image *dxdv;
cpl_image *dydu;
cpl_image *dydv;
double *d_dxdu;
double *d_dxdv;
double *d_dydu;
double *d_dydv;
const double *ddeltax;
const double *ddeltay;
double *ddata;
float *fdata;
cpl_size nx, ny, npix;
cpl_size i, j, pos;
cpl_error_code error = CPL_ERROR_NONE;
if (out == NULL || deltax == NULL || deltay == NULL)
return cpl_error_set_(CPL_ERROR_NULL_INPUT);
if (cpl_image_get_type(out) != CPL_TYPE_FLOAT &&
cpl_image_get_type(out) != CPL_TYPE_DOUBLE)
return cpl_error_set_(CPL_ERROR_INVALID_TYPE);
if (cpl_image_get_type(deltax) != CPL_TYPE_DOUBLE ||
cpl_image_get_type(deltay) != CPL_TYPE_DOUBLE)
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
nx = cpl_image_get_size_x(out);
ny = cpl_image_get_size_y(out);
if (nx != cpl_image_get_size_x(deltax) ||
nx != cpl_image_get_size_x(deltay) ||
ny != cpl_image_get_size_y(deltax) ||
ny != cpl_image_get_size_y(deltay)) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
/*
* Compute the partial derivatives of the transformation:
*/
dxdu = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
dxdv = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
dydu = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
dydv = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
d_dxdu = cpl_image_get_data_double(dxdu);
d_dxdv = cpl_image_get_data_double(dxdv);
d_dydu = cpl_image_get_data_double(dydu);
d_dydv = cpl_image_get_data_double(dydv);
ddeltax = cpl_image_get_data_double_const(deltax);
ddeltay = cpl_image_get_data_double_const(deltay);
/*
* Note: borders are excluded
*/
for (j = 1; j < ny - 1; j++) {
pos = j * nx;
for (i = 1; i < nx - 1; i++) {
pos++;
d_dxdu[pos] = (ddeltax[pos-1] - ddeltax[pos+1]) / 2 + 1;
d_dxdv[pos] = (ddeltax[pos-nx] - ddeltax[pos+nx]) / 2;
d_dydu[pos] = (ddeltay[pos-1] - ddeltay[pos+1]) / 2;
d_dydv[pos] = (ddeltay[pos-nx] - ddeltay[pos+nx]) / 2 + 1;
}
}
/*
* This section is for assigning the (local) scaling factor
* to each pixel of the reference image.
*/
npix = nx * ny;
switch (cpl_image_get_type(out)) {
case CPL_TYPE_FLOAT:
fdata = cpl_image_get_data_float(out);
for (i = 0; i < npix; i++) {
*fdata++ = (float)((*d_dxdu++) * (*d_dydv++)
- (*d_dxdv++) * (*d_dydu++));
}
break;
case CPL_TYPE_DOUBLE:
ddata = cpl_image_get_data_double(out);
for (i = 0; i < npix; i++) {
*ddata++ = (*d_dxdu++) * (*d_dydv++) - (*d_dxdv++) * (*d_dydu++);
}
break;
default:
/* It is an error in CPL to reach this point */
error = CPL_ERROR_UNSPECIFIED;
break;
}
cpl_image_delete(dxdu);
cpl_image_delete(dxdv);
cpl_image_delete(dydu);
cpl_image_delete(dydv);
/*
* Ensure the scale factor is positive...
*/
if (!error) error = cpl_image_abs(out);
/* Propagate error, if any */
return cpl_error_set_(error);
}
#define CPL_CLASS CPL_CLASS_DOUBLE
#undef CPL_OPERATION
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_FLOAT
#undef CPL_OPERATION
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
#define CPL_CLASS CPL_CLASS_INT
#undef CPL_OPERATION
#include "cpl_image_resample_body.h"
#undef CPL_CLASS
|