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 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
|
/*
* This file is part of the HDRL
* Copyright (C) 2013 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
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*-----------------------------------------------------------------------------
Includes
-----------------------------------------------------------------------------*/
#include "hdrl_collapse.h"
#include "hdrl_parameter.h"
#include "hdrl_parameter_defs.h"
#include "hdrl_sigclip.h"
#include "hdrl_utils.h"
#include "hdrl_types.h"
#include <cpl.h>
#include <string.h>
#include <math.h>
/*-----------------------------------------------------------------------------
Types
-----------------------------------------------------------------------------*/
/* function performing imglist -> image reduction */
typedef cpl_error_code (hdrl_collapse_imagelist_to_image_f)(
const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_image ** out,
cpl_image ** err,
cpl_image ** contrib,
void * parameters,
void * extra_out);
struct hdrl_collapse_imagelist_to_image_s {
/* function performing imglist -> image reduction */
hdrl_collapse_imagelist_to_image_f * func;
/* create extra out storage */
void * (*create_eout)(const cpl_image *);
/* move extra out storage */
cpl_error_code (*move_eout)(void *, void *, const cpl_size);
/* unwrap extra out storage */
hdrl_free * unwrap_eout;
/* delete extra out storage */
void (*delete_eout)(void *);
/* parameters the reduction function requires */
hdrl_parameter * parameters;
};
/* function performing imglist -> image reduction */
typedef cpl_error_code (hdrl_collapse_imagelist_to_vector_f)(
const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_vector ** out,
cpl_vector ** err,
cpl_array ** contrib,
void * parameters,
void * extra_out);
struct hdrl_collapse_imagelist_to_vector_s {
/* function performing imglist -> vector reduction */
hdrl_collapse_imagelist_to_vector_f * func;
/* create extra out storage */
void * (*create_eout)(cpl_size);
/* move extra out storage */
cpl_error_code (*move_eout)(void *, void *, const cpl_size);
/* unwrap extra out storage */
hdrl_free * unwrap_eout;
/* delete extra out storage */
void (*delete_eout)(void *);
/* parameters the reduction function requires */
hdrl_parameter * parameters;
};
static cpl_error_code
hdrl_collapse_mean(const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_image ** out, cpl_image ** err,
cpl_image ** contrib, void *, void *);
static cpl_error_code
hdrl_collapse_median(const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_image ** out, cpl_image ** err,
cpl_image ** contrib, void *, void *);
static cpl_error_code
hdrl_collapse_sigclip(const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_image ** out, cpl_image ** err,
cpl_image ** contrib,
void * parameters, void * extra_out);
static cpl_error_code
hdrl_collapse_minmax(const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_image ** out, cpl_image ** err,
cpl_image ** contrib,
void * parameters, void * extra_out);
static cpl_error_code
hdrl_collapse_weighted_mean(const cpl_imagelist * data_,
const cpl_imagelist * errors_,
cpl_image ** out, cpl_image ** err,
cpl_image ** contrib, void *, void *);
/*-----------------------------------------------------------------------------
Static
-----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/**
@defgroup hdrl_collapse Collapse Parameters
This module provides collapse parameters to be used by hdrl_image and
hdrl_imagelist objects.
*/
/*----------------------------------------------------------------------------*/
/**@{*/
/** @cond PRIVATE */
/*-----------------------------------------------------------------------------
Collapse Parameters
-----------------------------------------------------------------------------*/
static void hdrl_nop_free(void * HDRL_UNUSED(x))
{
return;
}
static void * hdrl_mean_alloc(size_t n);
static void * hdrl_median_alloc(size_t n);
static void * hdrl_weighted_mean_alloc(size_t n);
/* Mean COLLAPSE */
typedef hdrl_parameter_empty hdrl_collapse_mean_parameter;
static hdrl_parameter_typeobj hdrl_collapse_mean_parameter_type = {
HDRL_PARAMETER_COLLAPSE_MEAN, /* type */
(hdrl_alloc *)&hdrl_mean_alloc, /* fp_alloc */
(hdrl_free *)&hdrl_nop_free, /* fp_free */
NULL, /* fp_destroy */
sizeof(hdrl_collapse_mean_parameter), /* obj_size */
};
HDRL_PARAMETER_SINGLETON(HDRL_COLLAPSE_MEAN,
hdrl_collapse_mean_parameter_type,
hdrl_mean_alloc);
/* Median COLLAPSE */
typedef hdrl_parameter_empty hdrl_collapse_median_parameter;
static hdrl_parameter_typeobj hdrl_collapse_median_parameter_type = {
HDRL_PARAMETER_COLLAPSE_MEDIAN, /* type */
(hdrl_alloc *)&hdrl_median_alloc, /* fp_alloc */
(hdrl_free *)&hdrl_nop_free, /* fp_free */
NULL, /* fp_destroy */
sizeof(hdrl_collapse_median_parameter), /* obj_size */
};
HDRL_PARAMETER_SINGLETON(HDRL_COLLAPSE_MEDIAN,
hdrl_collapse_median_parameter_type,
hdrl_median_alloc);
/* Weighted Mean COLLAPSE */
typedef hdrl_parameter_empty hdrl_collapse_weighted_mean_parameter;
static hdrl_parameter_typeobj hdrl_collapse_weighted_mean_parameter_type = {
HDRL_PARAMETER_COLLAPSE_WEIGHTED_MEAN, /* type */
(hdrl_alloc *)&hdrl_weighted_mean_alloc, /* fp_alloc */
(hdrl_free *)&hdrl_nop_free, /* fp_free */
NULL, /* fp_destroy */
sizeof(hdrl_collapse_weighted_mean_parameter), /* obj_size */
};
HDRL_PARAMETER_SINGLETON(HDRL_COLLAPSE_WEIGHTED_MEAN,
hdrl_collapse_weighted_mean_parameter_type,
hdrl_weighted_mean_alloc);
/* Sigma-Clipping COLLAPSE */
typedef struct {
HDRL_PARAMETER_HEAD;
double kappa_low;
double kappa_high;
int niter;
} hdrl_collapse_sigclip_parameter;
static hdrl_parameter_typeobj hdrl_collapse_sigclip_parameter_type = {
HDRL_PARAMETER_COLLAPSE_SIGCLIP, /* type */
(hdrl_alloc *)&cpl_malloc, /* fp_alloc */
(hdrl_free *)&cpl_free, /* fp_free */
NULL, /* fp_destroy */
sizeof(hdrl_collapse_sigclip_parameter), /* obj_size */
};
/* Minmax-Clipping COLLAPSE */
typedef struct {
HDRL_PARAMETER_HEAD;
double nlow;
double nhigh;
} hdrl_collapse_minmax_parameter;
static hdrl_parameter_typeobj hdrl_collapse_minmax_parameter_type = {
HDRL_PARAMETER_COLLAPSE_MINMAX, /* type */
(hdrl_alloc *)&cpl_malloc, /* fp_alloc */
(hdrl_free *)&cpl_free, /* fp_free */
NULL, /* fp_destroy */
sizeof(hdrl_collapse_minmax_parameter), /* obj_size */
};
/** @endcond */
/* ---------------------------------------------------------------------------*/
/**
* @brief create a parameter object for mean
* @return hdrl_parameter
*/
/* ---------------------------------------------------------------------------*/
hdrl_parameter * hdrl_collapse_mean_parameter_create(void)
{
hdrl_parameter_empty * p = (hdrl_parameter_empty *)
hdrl_parameter_new(&hdrl_collapse_mean_parameter_type);
return (hdrl_parameter *)p;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief check if parameter is a mean parameter
* @return boolean
*/
/* ---------------------------------------------------------------------------*/
cpl_boolean hdrl_collapse_parameter_is_mean(const hdrl_parameter * self)
{
return hdrl_parameter_check_type(self, &hdrl_collapse_mean_parameter_type);
}
/* ---------------------------------------------------------------------------*/
/**
* @brief create a parameter object for median
* @return hdrl_parameter
*/
/* ---------------------------------------------------------------------------*/
hdrl_parameter * hdrl_collapse_median_parameter_create(void)
{
hdrl_parameter_empty * p = (hdrl_parameter_empty *)
hdrl_parameter_new(&hdrl_collapse_median_parameter_type);
return (hdrl_parameter *)p;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief check if parameter is a median parameter
* @return boolean
*/
/* ---------------------------------------------------------------------------*/
cpl_boolean hdrl_collapse_parameter_is_median(const hdrl_parameter * self)
{
return hdrl_parameter_check_type(self, &hdrl_collapse_median_parameter_type);
}
/* ---------------------------------------------------------------------------*/
/**
* @brief create a parameter object for weighted mean
* @return hdrl_parameter
*/
/* ---------------------------------------------------------------------------*/
hdrl_parameter * hdrl_collapse_weighted_mean_parameter_create(void)
{
hdrl_parameter_empty * p = (hdrl_parameter_empty *)
hdrl_parameter_new(&hdrl_collapse_weighted_mean_parameter_type);
return (hdrl_parameter *)p;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief check if parameter is a weighted mean parameter
* @return boolean
*/
/* ---------------------------------------------------------------------------*/
cpl_boolean hdrl_collapse_parameter_is_weighted_mean(
const hdrl_parameter * self)
{
return hdrl_parameter_check_type(self,
&hdrl_collapse_weighted_mean_parameter_type);
}
/* ---------------------------------------------------------------------------*/
/**
* @brief create a parameter object for sigclipped mean
*
* @param kappa_low low kappa multiplier
* @param kappa_high high kappa multiplier
* @param niter maximum number of clipping iterations
*/
/* ---------------------------------------------------------------------------*/
hdrl_parameter *
hdrl_collapse_sigclip_parameter_create(double kappa_low, double kappa_high, int niter)
{
hdrl_collapse_sigclip_parameter * p = (hdrl_collapse_sigclip_parameter *)
hdrl_parameter_new(&hdrl_collapse_sigclip_parameter_type);
p->kappa_low = kappa_low;
p->kappa_high = kappa_high;
p->niter = niter;
if (hdrl_collapse_sigclip_parameter_verify((hdrl_parameter*)p) !=
CPL_ERROR_NONE) {
hdrl_parameter_delete((hdrl_parameter*)p);
return NULL;
}
return (hdrl_parameter *)p;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief check if parameter is a sigclip mean parameter
* @return boolean
*/
/* ---------------------------------------------------------------------------*/
cpl_boolean hdrl_collapse_parameter_is_sigclip(const hdrl_parameter * self)
{
return hdrl_parameter_check_type(self,
&hdrl_collapse_sigclip_parameter_type);
}
/** @cond PRIVATE */
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Verify basic correctness of the parameters
@param param Collapse siglip parameters
@return CPL_ERROR_NONE if everything is ok, an error code otherwise
*/
/*----------------------------------------------------------------------------*/
cpl_error_code hdrl_collapse_sigclip_parameter_verify(
const hdrl_parameter * param)
{
cpl_error_ensure(param != NULL, CPL_ERROR_NULL_INPUT,
return CPL_ERROR_NULL_INPUT,
"NULL Collapse Sigclip Parameters");
cpl_error_ensure(hdrl_collapse_parameter_is_sigclip(param),
CPL_ERROR_INCOMPATIBLE_INPUT, return
CPL_ERROR_INCOMPATIBLE_INPUT,
"Not a Sigclip parameter");
const hdrl_collapse_sigclip_parameter * param_loc =
(const hdrl_collapse_sigclip_parameter *)param ;
cpl_error_ensure(param_loc->niter > 0, CPL_ERROR_ILLEGAL_INPUT,
return CPL_ERROR_ILLEGAL_INPUT,
"sigma-clipping iter (%d) value must be > 0",
param_loc->niter);
return CPL_ERROR_NONE ;
}
/** @endcond */
/* ---------------------------------------------------------------------------*/
/**
* @brief get low kappa
* @param p parameter
* @return kappa_low if p is of sigclip type
*/
/* ---------------------------------------------------------------------------*/
double hdrl_collapse_sigclip_parameter_get_kappa_high(const hdrl_parameter * p)
{
cpl_ensure(p, CPL_ERROR_NULL_INPUT, -1);
cpl_ensure(hdrl_collapse_parameter_is_sigclip(p),
CPL_ERROR_INCOMPATIBLE_INPUT, -1);
return ((const hdrl_collapse_sigclip_parameter *)p)->kappa_high;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief get high kappa
* @param p parameter
* @return kappa_high if p is of sigclip type
*/
/* ---------------------------------------------------------------------------*/
double hdrl_collapse_sigclip_parameter_get_kappa_low(const hdrl_parameter * p)
{
cpl_ensure(p, CPL_ERROR_NULL_INPUT, -1);
cpl_ensure(hdrl_collapse_parameter_is_sigclip(p),
CPL_ERROR_INCOMPATIBLE_INPUT, -1);
return ((const hdrl_collapse_sigclip_parameter *)p)->kappa_low;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief get maximum number of clipping iterations
* @param p parameter
* @return n if p is of sigclip type
*/
/* ---------------------------------------------------------------------------*/
int hdrl_collapse_sigclip_parameter_get_niter(const hdrl_parameter * p)
{
cpl_ensure(p, CPL_ERROR_NULL_INPUT, -1);
cpl_ensure(hdrl_collapse_parameter_is_sigclip(p),
CPL_ERROR_INCOMPATIBLE_INPUT, -1);
return ((const hdrl_collapse_sigclip_parameter *)p)->niter;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief create a parameter object for min-max rejected mean
*
* @param nlow number of low pixels to be rejected
* @param nhigh number of high pixels to be rejected
* @return minmax collapse parameter or NULL on error
*/
/* ---------------------------------------------------------------------------*/
hdrl_parameter *
hdrl_collapse_minmax_parameter_create(double nlow, double nhigh)
{
hdrl_collapse_minmax_parameter * p = (hdrl_collapse_minmax_parameter *)
hdrl_parameter_new(&hdrl_collapse_minmax_parameter_type);
p->nlow = nlow;
p->nhigh = nhigh;
if (hdrl_collapse_minmax_parameter_verify((hdrl_parameter*)p) !=
CPL_ERROR_NONE) {
hdrl_parameter_delete((hdrl_parameter*)p);
return NULL;
}
return (hdrl_parameter *)p;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief check if parameter is a minmax mean parameter
* @return boolean
*/
/* ---------------------------------------------------------------------------*/
cpl_boolean hdrl_collapse_parameter_is_minmax(const hdrl_parameter * self)
{
return hdrl_parameter_check_type(self,
&hdrl_collapse_minmax_parameter_type);
}
/** @cond PRIVATE */
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Verify basic correctness of the parameters
@param param Collapse minmax parameters
@return CPL_ERROR_NONE if everything is ok, an error code otherwise
*/
/*----------------------------------------------------------------------------*/
cpl_error_code hdrl_collapse_minmax_parameter_verify(
const hdrl_parameter * param)
{
cpl_error_ensure(param != NULL, CPL_ERROR_NULL_INPUT,
return CPL_ERROR_NULL_INPUT,
"NULL Collapse Minmax Parameters");
cpl_error_ensure(hdrl_collapse_parameter_is_minmax(param),
CPL_ERROR_INCOMPATIBLE_INPUT, return
CPL_ERROR_INCOMPATIBLE_INPUT,
"Not a minmax parameter");
const hdrl_collapse_minmax_parameter * param_loc =
(const hdrl_collapse_minmax_parameter *)param ;
cpl_error_ensure(param_loc->nlow >= 0, CPL_ERROR_ILLEGAL_INPUT,
return CPL_ERROR_ILLEGAL_INPUT,
"nlow value (%g) must be >= 0",
param_loc->nlow);
cpl_error_ensure(param_loc->nhigh >= 0, CPL_ERROR_ILLEGAL_INPUT,
return CPL_ERROR_ILLEGAL_INPUT,
"nhigh value (%g) must be >= 0",
param_loc->nlow);
return CPL_ERROR_NONE ;
}
/** @endcond */
/* ---------------------------------------------------------------------------*/
/**
* @brief get low value
* @param p parameter
* @return nlow if p is of minmax type
*/
/* ---------------------------------------------------------------------------*/
double hdrl_collapse_minmax_parameter_get_nhigh(const hdrl_parameter * p)
{
cpl_ensure(p, CPL_ERROR_NULL_INPUT, -1);
cpl_ensure(hdrl_collapse_parameter_is_minmax(p),
CPL_ERROR_INCOMPATIBLE_INPUT, -1);
return ((const hdrl_collapse_minmax_parameter *)p)->nhigh;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief get high value
* @param p parameter
* @return nhigh if p is of minmax type
*/
/* ---------------------------------------------------------------------------*/
double hdrl_collapse_minmax_parameter_get_nlow(const hdrl_parameter * p)
{
cpl_ensure(p, CPL_ERROR_NULL_INPUT, -1);
cpl_ensure(hdrl_collapse_parameter_is_minmax(p),
CPL_ERROR_INCOMPATIBLE_INPUT, -1);
return ((const hdrl_collapse_minmax_parameter *)p)->nlow;
}
/* ---------------------------------------------------------------------------*/
/**
@brief Create parameters for the collapse
@param base_context base context of parameter (e.g. recipe name)
@param prefix prefix of parameter, may be empty string
@param method_def default collapse method
@param sigclip_def default sigclip parameters
@param minmax_def default minmax parameters
@return The created parameter list
Creates a parameterlist containing
base_context.prefix.method
base_context.prefix.sigclip.*
The CLI aliases omit the base_context.
*/
/* ---------------------------------------------------------------------------*/
cpl_parameterlist * hdrl_collapse_parameter_create_parlist(
const char *base_context,
const char *prefix,
const char *method_def,
hdrl_parameter *sigclip_def,
hdrl_parameter *minmax_def)
{
cpl_ensure(base_context && prefix, CPL_ERROR_NULL_INPUT, NULL);
cpl_ensure( hdrl_collapse_parameter_is_sigclip(sigclip_def)
&& hdrl_collapse_parameter_is_minmax( minmax_def ),
CPL_ERROR_INCOMPATIBLE_INPUT, NULL);
char *name;
cpl_parameterlist *parlist = cpl_parameterlist_new();
cpl_parameter *p;
char *context = hdrl_join_string(".", 2, base_context, prefix);
/* --prefix.method */
name = hdrl_join_string(".", 2, context, "method");
p = cpl_parameter_new_enum(name, CPL_TYPE_STRING,
"Method used for collapsing the data", context,
method_def, 5, "MEAN", "WEIGHTED_MEAN", "MEDIAN", "SIGCLIP",
"MINMAX");
cpl_free(name) ;
name = hdrl_join_string(".", 2, prefix, "method");
cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, name);
cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
cpl_free(name) ;
cpl_parameterlist_append(parlist, p);
/* --prefix.sigclip.xxx */
name = hdrl_join_string(".", 2, prefix, "sigclip");
cpl_parameterlist * psigclip = hdrl_sigclip_parameter_create_parlist(
base_context, name, sigclip_def);
cpl_free(name) ;
for (cpl_parameter * par = cpl_parameterlist_get_first(psigclip);
par != NULL;
par = cpl_parameterlist_get_next(psigclip)) {
cpl_parameterlist_append(parlist, cpl_parameter_duplicate(par));
}
cpl_parameterlist_delete(psigclip);
/* --prefix.minmax.xxx */
name = hdrl_join_string(".", 2, prefix, "minmax");
cpl_parameterlist * pminmax = hdrl_minmax_parameter_create_parlist(
base_context, name, minmax_def);
cpl_free(name) ;
for (cpl_parameter * par = cpl_parameterlist_get_first(pminmax);
par != NULL;
par = cpl_parameterlist_get_next(pminmax)) {
cpl_parameterlist_append(parlist, cpl_parameter_duplicate(par));
}
cpl_parameterlist_delete(pminmax);
cpl_free(context);
if (cpl_error_get_code()) {
cpl_parameterlist_delete(parlist);
return NULL;
}
return parlist;
}
/* ---------------------------------------------------------------------------*/
/**
@brief parse parameterlist for imagelist reduction method
@param parlist parameter list to parse
@param prefix prefix of parameter name
@return hdrl_parameter
Reads a Parameterlist in order to create collapse parameters.
Expects a parameterlist containing
prefix.method
prefix.sigclip.*
*/
/* ---------------------------------------------------------------------------*/
hdrl_parameter * hdrl_collapse_parameter_parse_parlist(
const cpl_parameterlist * parlist,
const char * prefix)
{
cpl_ensure(prefix && parlist, CPL_ERROR_NULL_INPUT, NULL);
hdrl_parameter * p = NULL;
/* Get the Method parameter */
char * name = hdrl_join_string(".", 2, prefix, "method");
const cpl_parameter * par = cpl_parameterlist_find_const(parlist, name);
const char * value = cpl_parameter_get_string(par);
if (value == NULL) {
cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
"Parameter %s not found", name);
cpl_free(name);
return NULL;
}
/* Switch on the methods */
if(!strcmp(value, "MEDIAN")) {
p = hdrl_collapse_median_parameter_create();
} else if (!strcmp(value, "WEIGHTED_MEAN")) {
p = hdrl_collapse_weighted_mean_parameter_create();
} else if (!strcmp(value, "MEAN")) {
p = hdrl_collapse_mean_parameter_create();
} else if (!strcmp(value, "SIGCLIP")) {
double kappa_low, kappa_high;
int niter;
hdrl_sigclip_parameter_parse_parlist(parlist, prefix, &kappa_low,
&kappa_high, &niter);
p = hdrl_collapse_sigclip_parameter_create(kappa_low,kappa_high,niter);
} else if (!strcmp(value, "MINMAX")) {
double nlow, nhigh;
hdrl_minmax_parameter_parse_parlist(parlist, prefix, &nlow,
&nhigh);
p = hdrl_collapse_minmax_parameter_create(nlow,nhigh);
} else {
cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
"%s not a valid method for %s", value, name);
cpl_free(name);
return NULL;
}
cpl_free(name);
return p;
}
/** @cond PRIVATE */
/* ---------------------------------------------------------------------------*/
/**
* @brief create a new imagelist wrapping errors with same bpm as data
*
* @param data data list
* @param errors error list
* @returns cpl_imagelist which needs deleted with unwrap_synced_errlist
*
* Creates a new imagelist containing the wrapped data from the error list but
* the bad pixel map from the data list.
* This avoids issues with desynchronized bad pixel maps and avoids modifiying
* the inputs for collapse operations.
*/
/* ---------------------------------------------------------------------------*/
static cpl_imagelist *
wrap_synced_errlist(const cpl_imagelist * data, const cpl_imagelist * errors)
{
cpl_imagelist * nerrors = cpl_imagelist_new();
for (size_t i = 0; i < (size_t)cpl_imagelist_get_size(errors); i++) {
const cpl_image * img = cpl_imagelist_get_const(data, i);
const cpl_image * err = cpl_imagelist_get_const(errors, i);
CPL_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
cpl_image * nerr = cpl_image_wrap(cpl_image_get_size_x(err),
cpl_image_get_size_y(err),
cpl_image_get_type(err),
(void*)cpl_image_get_data_const(err));
cpl_mask_delete(hcpl_image_set_bpm(nerr,
(cpl_mask*)cpl_image_get_bpm_const(img)));
CPL_DIAG_PRAGMA_POP;
cpl_imagelist_set(nerrors, nerr, i);
}
return nerrors;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief unwrap imagelist created with wrap_synced_errlist
*
* @param nerrors imagelist from wrap_synced_errlist
*/
/* ---------------------------------------------------------------------------*/
static void
unwrap_synced_errlist(cpl_imagelist * nerrors)
{
for (size_t i = 0; i < (size_t)cpl_imagelist_get_size(nerrors); i++) {
CPL_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
cpl_image * err = (cpl_image*)cpl_imagelist_get_const(nerrors, i);
CPL_DIAG_PRAGMA_POP;
cpl_image_unset_bpm(err);
cpl_image_unwrap(err);
}
cpl_imagelist_unwrap(nerrors);
}
/* ---------------------------------------------------------------------------*/
/**
@internal
@brief calculate sum of squares of an imagelist
@param data imagelist
@param contrib if not NULL contribution map of reduction is stored
@return image containing the sum of squares along the imagelist axis
equivalent to:
cpl_imagelist_power(data, 2.)
sqlist = cpl_imagelist_collapse_create(data)
cpl_image_multiply(sqlist, contrib);
*/
/* ---------------------------------------------------------------------------*/
static cpl_image * imagelist_sqsum(
const cpl_imagelist * data,
cpl_image ** pcontrib)
{
cpl_image * contrib = cpl_image_new_from_accepted(data);
cpl_image * res = NULL;
for (cpl_size i = 0; i < cpl_imagelist_get_size(data); i++) {
const cpl_image * img = cpl_imagelist_get_const(data, i);
cpl_image * sqerr = cpl_image_multiply_create(img, img);
if (cpl_image_get_bpm_const(sqerr)) {
cpl_image_fill_rejected(sqerr, 0.0);
cpl_image_accept_all(sqerr);
}
if (i == 0) {
res = sqerr;
} else {
cpl_image_add(res, sqerr);
cpl_image_delete(sqerr);
}
}
cpl_mask * allbad = cpl_mask_threshold_image_create(contrib, 0, 0);
cpl_image_reject_from_mask(res, allbad);
cpl_mask_delete(allbad);
if (pcontrib)
*pcontrib = contrib;
else
cpl_image_delete(contrib);
return res;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief implements mean combination on input image list
*
* @param data input data images
* @param errors input errors images
* @param out output combined images
* @param err output combined errors
* @param contrib output contribution mask
* @param parameters parameters to control, not used
* @param extra_out optional extra output, not used
*
*
* @return cpl_error_code
*
* @doc
* Mean and associated error are computed with standard formulae
*
* \f$
* x_{mean}=\frac{(\sum_{i}^{n} x_{i})} { n }
* \f$
*
* \f$
* \sigma_{x}=\sqrt{ \frac{ \sum_{i}^{n} x_{i}^{2} }{ n } }
* \f$
*
*/
/* ---------------------------------------------------------------------------*/
static cpl_error_code
hdrl_collapse_mean(const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_image ** out, cpl_image ** err,
cpl_image ** contrib,
void * HDRL_UNUSED(parameters),
void * HDRL_UNUSED(extra_out))
{
/* (\Sum_i^n x_i) / n */
/* \sqrt(\Sum_i^n x_i^2) / n */
cpl_errorstate prestate = cpl_errorstate_get();
*out = cpl_imagelist_collapse_create(data);
/* ignore division by 0 on all pixels zero error */
if (*out == NULL) {
cpl_errorstate_set(prestate);
*out = cpl_image_duplicate(cpl_imagelist_get_const(data, 0));
cpl_image_accept_all(*out);
cpl_mask_not(cpl_image_get_bpm(*out));
*err = cpl_image_duplicate(cpl_imagelist_get_const(errors, 0));
cpl_image_accept_all(*err);
cpl_mask_not(cpl_image_get_bpm(*err));
*contrib = cpl_image_new(cpl_image_get_size_x(*err),
cpl_image_get_size_y(*err), CPL_TYPE_INT);
cpl_image_fill_rejected(*out, NAN);
cpl_image_fill_rejected(*err, NAN);
}
else {
*err = imagelist_sqsum(errors, contrib);
cpl_image_power(*err, 0.5);
cpl_image_divide(*err, *contrib);
cpl_image_fill_rejected(*out, NAN);
cpl_image_fill_rejected(*err, NAN);
}
return cpl_error_get_code();
}
/* ---------------------------------------------------------------------------*/
/**
* @brief implements weighted mean combination on input image list
*
* @param data input data images
* @param errors_ input errors images
* @param out output combined images
* @param err output combined errors
* @param contrib output contribution mask
* @param parameters parameters to control, not used
* @param extra_out optional extra output, not used
*
* @return cpl_error_code
*
* @doc
* weighted mean and associated error are computed with standard formulae
*
* \f$
* x_{mean}=\frac{(\sum_{i}^{n} w_{i} \cdot x_{i})} { \sum_{i}^{n} w_{i} }
* \f$
*
* \f$
* \sigma_{x}=\frac{ 1 } { \sqrt{ \sum_{i}^{n} w_{i}^{2} } }
* \f$
*
*/
/* ---------------------------------------------------------------------------*/
static cpl_error_code
hdrl_collapse_weighted_mean(const cpl_imagelist * data_,
const cpl_imagelist * errors_,
cpl_image ** out, cpl_image ** err,
cpl_image ** contrib,
void * HDRL_UNUSED(parameters),
void * HDRL_UNUSED(extra_out))
{
/* (\Sum_i^n w_i * x_i) / (\Sum_i^n w_i) */
/* 1 / \sqrt(\Sum_i^n w_i^2) */
cpl_errorstate prestate = cpl_errorstate_get();
cpl_imagelist * data = cpl_imagelist_duplicate(data_);
cpl_imagelist * errors = cpl_imagelist_new();
cpl_image * tmperr;
cpl_imagelist_cast(errors, errors_,
cpl_image_get_type(cpl_imagelist_get(data, 0)));
cpl_imagelist_power(errors, -2);
cpl_imagelist_multiply(data, errors);
*contrib = cpl_image_new_from_accepted(data);
*out = cpl_imagelist_collapse_create(data);
if (*out == NULL) {
cpl_errorstate_set(prestate);
*out = cpl_image_duplicate(cpl_imagelist_get_const(data, 0));
cpl_image_accept_all(*out);
cpl_mask_not(cpl_image_get_bpm(*out));
*err = cpl_image_duplicate(cpl_imagelist_get_const(errors, 0));
cpl_image_accept_all(*err);
cpl_mask_not(cpl_image_get_bpm(*err));
cpl_image_fill_rejected(*out, NAN);
cpl_image_fill_rejected(*err, NAN);
cpl_imagelist_delete(errors);
cpl_imagelist_delete(data);
return cpl_error_get_code();
}
cpl_imagelist_delete(data);
tmperr = cpl_imagelist_collapse_create(errors);
cpl_imagelist_delete(errors);
cpl_image_multiply(*out, *contrib);
cpl_image_multiply(tmperr, *contrib);
cpl_image_divide(*out, tmperr);
cpl_image_power(tmperr, -0.5);
if (cpl_image_get_type(cpl_imagelist_get_const(errors_, 0)) ==
cpl_image_get_type(cpl_imagelist_get_const(data_, 0))) {
*err = tmperr;
}
else {
*err = cpl_image_cast(tmperr,
cpl_image_get_type(cpl_imagelist_get_const(errors_, 0)));
cpl_image_delete(tmperr);
}
cpl_image_fill_rejected(*out, NAN);
cpl_image_fill_rejected(*err, NAN);
return cpl_error_get_code();
}
/* ---------------------------------------------------------------------------*/
/**
* @brief implements median combination on input image list
*
* @param data input data images
* @param errors input errors images
* @param out output combined images
* @param err output combined errors
* @param contrib output contribution mask
* @param parameters parameters to control, not used
* @param extra_out optional extra output, not used
*
* @return cpl_error_code
*
* @doc
* Median and associated error are computed similarly as for mean but
* scaling by \f$ \sqrt{ \frac{ \pi } { 2 } } \f$
*/
/* ---------------------------------------------------------------------------*/
static cpl_error_code
hdrl_collapse_median(const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_image ** out, cpl_image ** err,
cpl_image ** contrib,
void * HDRL_UNUSED(parameters),
void * HDRL_UNUSED(extra_out))
{
cpl_errorstate prestate = cpl_errorstate_get();
/* same as mean with scaling by \sqrt(\pi / 2) */
*out = cpl_imagelist_collapse_median_create(data);
*err = imagelist_sqsum(errors, contrib);
cpl_image_power(*err, 0.5);
cpl_image_divide(*err, *contrib);
if (cpl_error_get_code() == CPL_ERROR_DIVISION_BY_ZERO) {
cpl_errorstate_set(prestate);
cpl_image_accept_all(*out);
cpl_mask_not(cpl_image_get_bpm(*out));
cpl_image_accept_all(*err);
cpl_mask_not(cpl_image_get_bpm(*err));
cpl_image_fill_rejected(*out, NAN);
cpl_image_fill_rejected(*err, NAN);
return cpl_error_get_code();
}
/* scale error so it estimates stdev of normal distribution */
cpl_image_multiply_scalar(*err, sqrt(CPL_MATH_PI_2));
/* revert scaling for contrib <= 2 as median == mean in this case */
cpl_image * tmp = cpl_image_cast(*contrib, CPL_TYPE_DOUBLE);
cpl_image_threshold(tmp, 2.1, 2.1, 1. / sqrt(CPL_MATH_PI_2), 1.);
cpl_image_multiply(*err, tmp);
cpl_image_delete(tmp);
cpl_image_fill_rejected(*out, NAN);
cpl_image_fill_rejected(*err, NAN);
return cpl_error_get_code();
}
/* ---------------------------------------------------------------------------*/
/**
* @brief implements sigma-clipped combination on input image list
*
* @param data input data images
* @param errors input errors images
* @param out output combined images
* @param err output combined errors
* @param contrib output contribution mask
* @param parameters parameters to control, not used
* @param extra_out optional extra output, not used
*
* @return cpl_error_code
*
* @doc
* sigma-clipped mean and associated error, computed similarly as for mean but
* without taking the clipped values into account.
*/
/* ---------------------------------------------------------------------------*/
static cpl_error_code
hdrl_collapse_sigclip(const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_image ** out, cpl_image ** err,
cpl_image ** contrib,
void * parameters, void * extra_out)
{
/* same as mean but working on the not-clipped values only */
hdrl_collapse_sigclip_parameter * par = parameters;
hdrl_sigclip_image_output * eout =
(hdrl_sigclip_image_output *)extra_out;
cpl_ensure_code(par, CPL_ERROR_NULL_INPUT);
const cpl_image * img = cpl_imagelist_get_const(data, 0);
size_t nx = cpl_image_get_size_x(img);
size_t ny = cpl_image_get_size_y(img);
*out = cpl_image_new(nx, ny, HDRL_TYPE_DATA);
*err = cpl_image_new(nx, ny, HDRL_TYPE_ERROR);
*contrib = cpl_image_new(nx, ny, CPL_TYPE_INT);
hdrl_vector_cache * cache =
hdrl_vector_cache_new(cpl_imagelist_get_size(data), nx * 2);
/* sigmaclip along imagelist axis */
for (size_t y = 1; y < ny + 1; y++) {
cpl_vector * vdv[nx];
cpl_vector * vev[nx];
hdrl_imagelist_to_vector_row(data, y, vdv, cache);
hdrl_imagelist_to_vector_row(errors, y, vev, cache);
for (size_t x = 1; x < nx + 1; x++) {
cpl_vector * vd = vdv[x - 1];
cpl_vector * ve = vev[x - 1];
if (vd && ve) {
double m, e, rej_low, rej_high;
cpl_size naccepted;
hdrl_kappa_sigma_clip(vd, ve,
par->kappa_low, par->kappa_high,
par->niter, CPL_TRUE,
&m, &e, &naccepted,
&rej_low, &rej_high);
cpl_image_set(*out, x, y, m);
cpl_image_set(*err, x, y, e);
cpl_image_set(*contrib, x, y, naccepted);
if (eout) {
cpl_image_set(eout->reject_low, x, y, rej_low);
cpl_image_set(eout->reject_high, x, y, rej_high);
}
}
else {
cpl_image_set(*out, x, y, NAN);
cpl_image_set(*err, x, y, NAN);
cpl_image_reject(*out, x, y);
cpl_image_reject(*err, x, y);
cpl_image_set(*contrib, x, y, 0);
if (eout) {
cpl_image_set(eout->reject_low, x, y, 0.);
cpl_image_set(eout->reject_high, x, y, 0.);
}
}
hdrl_cplvector_delete_to_cache(cache, vd);
hdrl_cplvector_delete_to_cache(cache, ve);
}
}
hdrl_vector_cache_delete(cache);
return cpl_error_get_code();
}
/* ---------------------------------------------------------------------------*/
/**
* @brief implements minmax-clipped combination on input image list
*
* @param data input data images
* @param errors input errors images
* @param out output combined images
* @param err output combined errors
* @param contrib output contribution mask
* @param parameters parameters to control, not used
* @param extra_out optional extra output, not used
*
* @return cpl_error_code
*
* @doc minmax-clipped mean and associated error, computed similarly
* as for mean but without taking the clipped values into account
*
*/
/* ---------------------------------------------------------------------------*/
static cpl_error_code
hdrl_collapse_minmax(const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_image ** out, cpl_image ** err,
cpl_image ** contrib,
void * parameters, void * extra_out)
{
/* same as mean but working on the not-clipped values only */
hdrl_collapse_minmax_parameter * par = parameters;
cpl_ensure_code(par, CPL_ERROR_NULL_INPUT);
hdrl_minmax_image_output * eout =
(hdrl_minmax_image_output *)extra_out;
const cpl_image * img = cpl_imagelist_get_const(data, 0);
size_t nx = cpl_image_get_size_x(img);
size_t ny = cpl_image_get_size_y(img);
*out = cpl_image_new(nx, ny, HDRL_TYPE_DATA);
*err = cpl_image_new(nx, ny, HDRL_TYPE_ERROR);
*contrib = cpl_image_new(nx, ny, CPL_TYPE_INT);
hdrl_vector_cache * cache =
hdrl_vector_cache_new(cpl_imagelist_get_size(data), nx * 2);
/* minmaxclip along imagelist axis */
for (size_t y = 1; y < ny + 1; y++) {
cpl_vector * vdv[nx];
cpl_vector * vev[nx];
hdrl_imagelist_to_vector_row(data, y, vdv, cache);
hdrl_imagelist_to_vector_row(errors, y, vev, cache);
for (size_t x = 1; x < nx + 1; x++) {
cpl_vector * vd = vdv[x - 1];
cpl_vector * ve = vev[x - 1];
if (vd && ve) {
double m, e, rej_low, rej_high;
cpl_size naccepted;
hdrl_minmax_clip(vd, ve,
par->nlow, par->nhigh, CPL_TRUE,
&m, &e, &naccepted,
&rej_low, &rej_high);
cpl_image_set(*out, x, y, m);
cpl_image_set(*err, x, y, e);
cpl_image_set(*contrib, x, y, naccepted);
if (eout) {
cpl_image_set(eout->reject_low, x, y, rej_low);
cpl_image_set(eout->reject_high, x, y, rej_high);
}
}
else {
cpl_image_set(*out, x, y, NAN);
cpl_image_set(*err, x, y, NAN);
cpl_image_reject(*out, x, y);
cpl_image_reject(*err, x, y);
cpl_image_set(*contrib, x, y, 0);
if (eout) {
cpl_image_set(eout->reject_low, x, y, 0.);
cpl_image_set(eout->reject_high, x, y, 0.);
}
}
hdrl_cplvector_delete_to_cache(cache, vd);
hdrl_cplvector_delete_to_cache(cache, ve);
}
}
hdrl_vector_cache_delete(cache);
return cpl_error_get_code();
}
static void *
hdrl_nop_create_eout_vec(cpl_size HDRL_UNUSED(size))
{
return NULL;
}
static void *
hdrl_nop_create_eout_img(const cpl_image * HDRL_UNUSED(img))
{
return NULL;
}
static cpl_error_code
hdrl_nop_move_eout(void * HDRL_UNUSED(dst_),
void * HDRL_UNUSED(src_), const cpl_size HDRL_UNUSED(y))
{
return CPL_ERROR_NONE;
}
static void
hdrl_nop_unwrap_eout(void * HDRL_UNUSED(dst))
{
return;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief reduction object to reduce imagelist via mean
* @return mean reduction object
*/
/* ---------------------------------------------------------------------------*/
hdrl_collapse_imagelist_to_image_t *
hdrl_collapse_imagelist_to_image_mean(void)
{
hdrl_collapse_imagelist_to_image_t * s = cpl_calloc(1, sizeof(*s));
s->func = &hdrl_collapse_mean;
s->create_eout = &hdrl_nop_create_eout_img;
s->move_eout = &hdrl_nop_move_eout;
s->unwrap_eout = &hdrl_nop_unwrap_eout;
s->delete_eout = &hdrl_nop_unwrap_eout;
return s;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief reduction object to reduce imagelist via weighted mean
* @return weighted mean reduction object
*/
/* ---------------------------------------------------------------------------*/
hdrl_collapse_imagelist_to_image_t *
hdrl_collapse_imagelist_to_image_weighted_mean(void)
{
hdrl_collapse_imagelist_to_image_t * s = cpl_calloc(1, sizeof(*s));
s->func = &hdrl_collapse_weighted_mean;
s->create_eout = &hdrl_nop_create_eout_img;
s->move_eout = &hdrl_nop_move_eout;
s->unwrap_eout = &hdrl_nop_unwrap_eout;
s->delete_eout = &hdrl_nop_unwrap_eout;
return s;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief reduction object to reduce imagelist via median
* @return median reduction object
*/
/* ---------------------------------------------------------------------------*/
hdrl_collapse_imagelist_to_image_t *
hdrl_collapse_imagelist_to_image_median(void)
{
hdrl_collapse_imagelist_to_image_t * s = cpl_calloc(1, sizeof(*s));
s->func = &hdrl_collapse_median;
s->create_eout = &hdrl_nop_create_eout_img;
s->move_eout = &hdrl_nop_move_eout;
s->unwrap_eout = &hdrl_nop_unwrap_eout;
s->delete_eout = &hdrl_nop_unwrap_eout;
return s;
}
static void *
hdrl_sigclip_create_eout_img(const cpl_image * img)
{
cpl_ensure(img, CPL_ERROR_NULL_INPUT, NULL);
hdrl_sigclip_image_output * eout = cpl_calloc(sizeof(*eout), 1);
eout->reject_low = cpl_image_new(cpl_image_get_size_x(img),
cpl_image_get_size_y(img),
cpl_image_get_type(img));
eout->reject_high = cpl_image_new(cpl_image_get_size_x(img),
cpl_image_get_size_y(img),
cpl_image_get_type(img));
/* add masks for thread safety on move */
cpl_image_get_bpm(eout->reject_low);
cpl_image_get_bpm(eout->reject_high);
return eout;
}
static void
hdrl_sigclip_delete_eout_img(void * eout_)
{
if (eout_ == NULL) {
return;
}
hdrl_sigclip_image_output * eout = (hdrl_sigclip_image_output*)eout_;
cpl_image_delete(eout->reject_low);
cpl_image_delete(eout->reject_high);
cpl_free(eout);
}
static cpl_error_code
hdrl_sigclip_move_eout_img(void * dst_, void * src_, const cpl_size y)
{
hdrl_sigclip_image_output * dst = dst_;
hdrl_sigclip_image_output * src = src_;
cpl_ensure_code(dst, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(src, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(y > 0, CPL_ERROR_ACCESS_OUT_OF_RANGE);
cpl_ensure_code(y <= cpl_image_get_size_y(dst->reject_low),
CPL_ERROR_ACCESS_OUT_OF_RANGE);
cpl_image_copy(dst->reject_low, src->reject_low, 1, y);
cpl_image_copy(dst->reject_high, src->reject_high, 1, y);
cpl_image_delete(src->reject_low);
cpl_image_delete(src->reject_high);
cpl_free(src);
return cpl_error_get_code();
}
/* ---------------------------------------------------------------------------*/
/**
* @brief reduction object to reduce imagelist via kappa sigma clipped mean
*
* @param kappa_low low sigma bound
* @param kappa_high high sigma bound
* @param niter number of clipping iterators
*
* @return sigma clip reduction object
* @see hdrl_kappa_sigma_clip()
*
* the high and low reject values are stored in extra_out if applicable
*/
/* ---------------------------------------------------------------------------*/
hdrl_collapse_imagelist_to_image_t *
hdrl_collapse_imagelist_to_image_sigclip(double kappa_low,
double kappa_high,
int niter)
{
hdrl_collapse_imagelist_to_image_t * s = cpl_calloc(1, sizeof(*s));
hdrl_parameter * sp =
hdrl_collapse_sigclip_parameter_create(kappa_low, kappa_high, niter);
s->func = &hdrl_collapse_sigclip;
s->create_eout = &hdrl_sigclip_create_eout_img;
s->move_eout = &hdrl_sigclip_move_eout_img;
s->unwrap_eout = &cpl_free;
s->delete_eout = &hdrl_sigclip_delete_eout_img;
s->parameters = sp;
return s;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief reduction object to reduce imagelist via minmax clipped mean
*
* @param nlow number of low pixel to reject
* @param nhigh number of high pixel to reject
*
* @return minmax cliped reduction object
* @see hdrl_minmax_clip()
*
*
*/
/* ---------------------------------------------------------------------------*/
hdrl_collapse_imagelist_to_image_t *
hdrl_collapse_imagelist_to_image_minmax(double nlow,
double nhigh)
{
hdrl_collapse_imagelist_to_image_t * s = cpl_calloc(1, sizeof(*s));
hdrl_parameter * sp =
hdrl_collapse_minmax_parameter_create(nlow, nhigh);
s->func = &hdrl_collapse_minmax;
s->create_eout = &hdrl_sigclip_create_eout_img;
s->move_eout = &hdrl_sigclip_move_eout_img;
s->unwrap_eout = &cpl_free;
s->delete_eout = &hdrl_sigclip_delete_eout_img;
s->parameters = sp;
return s;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief Call the associated reduction function
*
* @param f reduction function object
* @param data data to apply function on
* @param errors errors to use for propagation
* @param out pointer which will contain reduced data image, type double
* @param err pointer which will contain reduced error image, type double
* @param contrib pointer which will contain contribution map, type integer
* @param eout storage for extra output, may be NULL
*
* @return cpl_error_code
*/
/* ---------------------------------------------------------------------------*/
cpl_error_code
hdrl_collapse_imagelist_to_image_call(hdrl_collapse_imagelist_to_image_t * f,
const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_image ** out,
cpl_image ** err,
cpl_image ** contrib,
void ** eout)
{
cpl_ensure_code(f, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(data, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(errors, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(out, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(err, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(contrib, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(cpl_imagelist_get_size(data) ==
cpl_imagelist_get_size(errors),
CPL_ERROR_INCOMPATIBLE_INPUT);
if (eout) {
*eout = f->create_eout(cpl_imagelist_get_const(data, 0));
}
cpl_imagelist * nerrors = wrap_synced_errlist(data, errors);
if (nerrors == NULL) {
return cpl_error_get_code();
}
cpl_error_code errcode = f->func(data, nerrors, out, err, contrib,
f->parameters, eout ? *eout : NULL);
unwrap_synced_errlist(nerrors);
return errcode;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief call extra output creation function
*
* @param f reduction function object
* @param data an image of same size and type as the intended reductee
*
* @return reduction objects extra output function, its entries must be deleted
* by the caller and the structure unwraped with the unwrap_eout
* function
*/
/* ---------------------------------------------------------------------------*/
void *
hdrl_collapse_imagelist_to_image_create_eout(
hdrl_collapse_imagelist_to_image_t * f,
const cpl_image * data)
{
cpl_ensure(f, CPL_ERROR_NULL_INPUT, NULL);
cpl_ensure(data, CPL_ERROR_NULL_INPUT, NULL);
return f->create_eout(data);
}
/* ---------------------------------------------------------------------------*/
/**
* @brief call extra output unwrap function
*
* @param f reduction function object
*
* @note does not delete the members
*
*/
/* ---------------------------------------------------------------------------*/
void
hdrl_collapse_imagelist_to_image_unwrap_eout(
hdrl_collapse_imagelist_to_image_t * f,
void * eout)
{
if (f != NULL) {
f->unwrap_eout(eout);
}
}
/* ---------------------------------------------------------------------------*/
/**
* @brief call extra output delete function
*
* @param f reduction function object
*
* @note does delete the members
*
*/
/* ---------------------------------------------------------------------------*/
void
hdrl_collapse_imagelist_to_image_delete_eout(
hdrl_collapse_imagelist_to_image_t * f,
void * eout)
{
if (f != NULL) {
f->delete_eout(eout);
}
}
/* ---------------------------------------------------------------------------*/
/**
* @brief call function to move extra output do destination with offset
*
* @param f reduction function object
* @param dst destination output
* @param src source output
* @param y offset in y, see cpl_image_copy
*
* @return cpl_error_code
*
* @note deletes the source after the content is copied
*/
/* ---------------------------------------------------------------------------*/
cpl_error_code
hdrl_collapse_imagelist_to_image_move_eout(
hdrl_collapse_imagelist_to_image_t * f,
void * dst,
void * src,
cpl_size y)
{
cpl_ensure_code(f, CPL_ERROR_NULL_INPUT);
return f->move_eout(dst, src, y);
}
/* ---------------------------------------------------------------------------*/
/**
* @brief delete imagelist reduction object
*
* @param p imagelist reduction object or NULL
*/
/* ---------------------------------------------------------------------------*/
void hdrl_collapse_imagelist_to_image_delete(hdrl_collapse_imagelist_to_image_t * p)
{
if (p) {
hdrl_parameter_delete(p->parameters);
}
cpl_free(p);
}
/* ---------------------------------------------------------------------------*/
/**
* @brief call extra output creation function
*
* @param f reduction function object
* @param size size of the imagelist which should be reduced
*
* @return reduction objects extra output function, its entries must be deleted
* by the caller and the structure unwraped with the unwrap_eout
* function
*/
/* ---------------------------------------------------------------------------*/
void * hdrl_collapse_imagelist_to_vector_create_eout(
hdrl_collapse_imagelist_to_vector_t * f,
const cpl_size size)
{
cpl_ensure(f, CPL_ERROR_NULL_INPUT, NULL);
cpl_ensure(size > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
return f->create_eout(size);
}
/* ---------------------------------------------------------------------------*/
/**
* @brief call extra output unwrap function
*
* @param f reduction function object
*
* @note does not delete the members
*
*/
/* ---------------------------------------------------------------------------*/
void
hdrl_collapse_imagelist_to_vector_unwrap_eout(
hdrl_collapse_imagelist_to_vector_t * f,
void * eout)
{
if (f != NULL) {
f->unwrap_eout(eout);
}
}
/* ---------------------------------------------------------------------------*/
/**
* @brief call extra output delete function
*
* @param f reduction function object
*
* @note does delete the members
*
*/
/* ---------------------------------------------------------------------------*/
void hdrl_collapse_imagelist_to_vector_delete_eout(
hdrl_collapse_imagelist_to_vector_t * f,
void * eout)
{
if (f != NULL) {
f->delete_eout(eout);
}
}
/* ---------------------------------------------------------------------------*/
/**
* @brief call function to move extra output do destination with offset
*
* @param f reduction function object
* @param dst destination output
* @param src source output
* @param y offset in y, see cpl_image_copy
*
* @return cpl_error_code
*
* @note deletes the source after the content is copied
*/
/* ---------------------------------------------------------------------------*/
cpl_error_code hdrl_collapse_imagelist_to_vector_move_eout(
hdrl_collapse_imagelist_to_vector_t * f,
void * dst,
void * src,
cpl_size y)
{
cpl_ensure_code(f, CPL_ERROR_NULL_INPUT);
return f->move_eout(dst, src, y);
}
/* ---------------------------------------------------------------------------*/
/**
* @brief implements mean reduction on each image of an imagelist
*
* @param data input data images
* @param errors input errors images
* @param out vector of median of each image
* @param err vector of errors of median of each image
* @param contrib array of contributions
* @param parameters parameters to control, not used
* @param extra_out optional extra output, not used
*
* @return cpl_error_code
*
* @doc
* The mean value on all good pixels of each image of an imagelist, the
* associated error and the number of good pixels are stored as elements
* of the corresponding output vectors.
* If all pixels of an image in the list are bad the contribution is 0 and the
* out and err are set to NAN.
*
*/
/* ---------------------------------------------------------------------------*/
static cpl_error_code
reduce_imagelist_to_vector_mean(const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_vector ** out, cpl_vector ** err,
cpl_array ** contrib,
void * HDRL_UNUSED(parameters),
void * HDRL_UNUSED(extra_out))
{
size_t nz = cpl_imagelist_get_size(data);
*out = cpl_vector_new(nz);
*err = cpl_vector_new(nz);
*contrib = cpl_array_new(nz, CPL_TYPE_INT);
for (size_t i = 0; i < nz; i++) {
const cpl_image * img = cpl_imagelist_get_const(data, i);
const cpl_image * ierr = cpl_imagelist_get_const(errors, i);
size_t naccepted = hdrl_get_image_good_npix(img);
if (naccepted != 0) {
double error = sqrt(cpl_image_get_sqflux(ierr)) / naccepted;
cpl_vector_set(*out, i, cpl_image_get_mean(img));
cpl_vector_set(*err, i, error);
}
else {
cpl_vector_set(*out, i, NAN);
cpl_vector_set(*err, i, NAN);
}
cpl_array_set_int(*contrib, i, naccepted);
}
return cpl_error_get_code();
}
/* ---------------------------------------------------------------------------*/
/**
* @brief reduction object to reduce imagelist via mean
* @return mean reduction object
*/
/* ---------------------------------------------------------------------------*/
hdrl_collapse_imagelist_to_vector_t *
hdrl_collapse_imagelist_to_vector_mean(void)
{
hdrl_collapse_imagelist_to_vector_t * s = cpl_calloc(1, sizeof(*s));
s->create_eout = &hdrl_nop_create_eout_vec;
s->move_eout = &hdrl_nop_move_eout;
s->unwrap_eout = &hdrl_nop_unwrap_eout;
s->delete_eout = &hdrl_nop_unwrap_eout;
s->func = &reduce_imagelist_to_vector_mean;
return s;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief implements weighted mean reduction on each image of an imagelist
*
* @param data input data images
* @param errors input errors images
* @param out vector of median of each image
* @param err vector of errors of median of each image
* @param contrib array of contributions
* @param parameters parameters to control, not used
* @param extra_out optional extra output, not used
*
* @return cpl_error_code
*
* @doc
* weighted mean and associated error are computed with standard formulae
*
* \f$
* x_{mean}=\frac{(\sum_{i}^{n} w_{i} \cdot x_{i})} { \sum_{i}^{n} w_{i} }
* \f$
*
* \f$
* \sigma_{x}=\frac{ 1 } { \sqrt{ \sum_{i}^{n} w_{i}^{2} } }
* \f$
*
*/
/* ---------------------------------------------------------------------------*/
static cpl_error_code
reduce_imagelist_to_vector_weighted_mean(const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_vector ** out, cpl_vector ** err,
cpl_array ** contrib,
void * HDRL_UNUSED(parameters),
void * HDRL_UNUSED(extra_out))
{
size_t nz = cpl_imagelist_get_size(data);
*out = cpl_vector_new(nz);
*err = cpl_vector_new(nz);
*contrib = cpl_array_new(nz, CPL_TYPE_INT);
for (size_t i = 0; i < nz; i++) {
cpl_image * img =
cpl_image_duplicate(cpl_imagelist_get_const(data, i));
cpl_image * ierr =
cpl_image_duplicate(cpl_imagelist_get_const(errors, i));
size_t naccepted = hdrl_get_image_good_npix(img);
if (naccepted != 0) {
/* (\Sum_i^n w_i * x_i) / (\Sum_i^n w_i) */
/* 1 / \sqrt(\Sum_i^n w_i^2) */
cpl_image_power(ierr, -2);
/* ierr = weights now */
cpl_image_multiply(img, ierr);
double sum_v = cpl_image_get_mean(img) * naccepted;
double sum_w = cpl_image_get_mean(ierr) * naccepted;
double wmean = sum_v / sum_w;
double error = 1. / sqrt(sum_w);
cpl_vector_set(*out, i, wmean);
cpl_vector_set(*err, i, error);
}
else {
cpl_vector_set(*out, i, NAN);
cpl_vector_set(*err, i, NAN);
}
cpl_array_set_int(*contrib, i, naccepted);
cpl_image_delete(img);
cpl_image_delete(ierr);
}
return cpl_error_get_code();
}
/* ---------------------------------------------------------------------------*/
/**
* @brief reduction object to reduce imagelist via weighted mean
* @return weighted mean reduction object
*/
/* ---------------------------------------------------------------------------*/
hdrl_collapse_imagelist_to_vector_t *
hdrl_collapse_imagelist_to_vector_weighted_mean(void)
{
hdrl_collapse_imagelist_to_vector_t * s = cpl_calloc(1, sizeof(*s));
s->create_eout = &hdrl_nop_create_eout_vec;
s->move_eout = &hdrl_nop_move_eout;
s->unwrap_eout = &hdrl_nop_unwrap_eout;
s->delete_eout = &hdrl_nop_unwrap_eout;
s->func = &reduce_imagelist_to_vector_weighted_mean;
return s;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief implements median reduction on each image of an imagelist
*
* @param data input data images
* @param errors input errors images
* @param out vector of median of each image
* @param err vector of errors of median of each image
* @param contrib array of contributions
* @param parameters parameters to control, not used
* @param extra_out optional extra output, not used
* @return cpl_error_code
*
* @doc
* The median value on all good pixels of each image of an imagelist, the
* associated error and the number of good pixels are stored as elements
* of the corresponding output vectors.
* If all pixels of an image in the list are bad the contribution is 0 and the
* out and err are set to NAN.
* The errors are scaled by the sqrt of the statistical efficiency of the
* median on normal distributed data which is \f$ \frac{ \pi }{ 2 } \f$
*
*/
/* ---------------------------------------------------------------------------*/
static cpl_error_code
reduce_imagelist_to_vector_median(const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_vector ** out, cpl_vector ** err,
cpl_array ** contrib,
void * HDRL_UNUSED(parameters),
void * HDRL_UNUSED(extra_out))
{
size_t nz = cpl_imagelist_get_size(data);
*out = cpl_vector_new(nz);
*err = cpl_vector_new(nz);
*contrib = cpl_array_new(nz, CPL_TYPE_INT);
for (size_t i = 0; i < nz; i++) {
const cpl_image * img = cpl_imagelist_get_const(data, i);
const cpl_image * ierr = cpl_imagelist_get_const(errors, i);
size_t naccepted = hdrl_get_image_good_npix(img);
if (naccepted != 0) {
double error = sqrt(cpl_image_get_sqflux(ierr)) / naccepted;
/* sqrt(statistical efficiency on normal data)*/
if (naccepted > 2) {
error *= sqrt(CPL_MATH_PI_2);
}
cpl_vector_set(*out, i, cpl_image_get_median(img));
cpl_vector_set(*err, i, error);
}
else {
cpl_vector_set(*out, i, NAN);
cpl_vector_set(*err, i, NAN);
}
cpl_array_set_int(*contrib, i, naccepted);
}
return cpl_error_get_code();
}
/* ---------------------------------------------------------------------------*/
/**
* @brief reduction object to reduce imagelist via median
* @return median reduction object
*/
/* ---------------------------------------------------------------------------*/
hdrl_collapse_imagelist_to_vector_t *
hdrl_collapse_imagelist_to_vector_median(void)
{
hdrl_collapse_imagelist_to_vector_t * s = cpl_calloc(1, sizeof(*s));
s->create_eout = &hdrl_nop_create_eout_vec;
s->move_eout = &hdrl_nop_move_eout;
s->unwrap_eout = &hdrl_nop_unwrap_eout;
s->delete_eout = &hdrl_nop_unwrap_eout;
s->func = &reduce_imagelist_to_vector_median;
return s;
}
static void *
hdrl_sigclip_create_eout_vec(cpl_size size)
{
hdrl_sigclip_vector_output * eout = cpl_calloc(sizeof(*eout), 1);
eout->reject_low = cpl_vector_new(size);
eout->reject_high = cpl_vector_new(size);
return eout;
}
static void
hdrl_sigclip_delete_eout_vec(void * eout_)
{
if (eout_ == NULL) {
return;
}
hdrl_sigclip_vector_output * eout = (hdrl_sigclip_vector_output*)eout_;
cpl_vector_delete(eout->reject_low);
cpl_vector_delete(eout->reject_high);
cpl_free(eout);
}
static cpl_error_code
hdrl_sigclip_move_eout_vec(void * dst_, void * src_, const cpl_size y)
{
hdrl_sigclip_vector_output * dst = dst_;
hdrl_sigclip_vector_output * src = src_;
cpl_ensure_code(dst, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(src, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(y >= 0, CPL_ERROR_ACCESS_OUT_OF_RANGE);
cpl_ensure_code(y < cpl_vector_get_size(dst->reject_low),
CPL_ERROR_ACCESS_OUT_OF_RANGE);
double * ddst = cpl_vector_get_data(dst->reject_low);
double * dsrc = cpl_vector_get_data(src->reject_low);
memcpy(ddst + y, dsrc, cpl_vector_get_size(src->reject_low));
ddst = cpl_vector_get_data(dst->reject_high);
dsrc = cpl_vector_get_data(src->reject_high);
memcpy(ddst + y, dsrc, cpl_vector_get_size(src->reject_high));
cpl_vector_delete(src->reject_low);
cpl_vector_delete(src->reject_high);
cpl_free(src);
return cpl_error_get_code();
}
/* ---------------------------------------------------------------------------*/
/**
* @internal
* @brief implements sigma-clipped combination on input image list into a vector
*
* @param data input data imagelist
* @param errors input errors imagelist
* @param out output vector
* @param err output vector errors
* @param contrib output contribution vector
*
* @return cpl_error_code
*
* If all pixels of an image in the list are rejected the contribution is 0 and
* the out and err are set to NAN.
*
*/
/* ---------------------------------------------------------------------------*/
static cpl_error_code
reduce_imagelist_to_vector_sigclip(const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_vector ** out, cpl_vector ** err,
cpl_array ** contrib, void * parameters,
void * extra_out)
{
hdrl_collapse_sigclip_parameter * par = parameters;
hdrl_minmax_vector_output * eout =
(hdrl_minmax_vector_output *)extra_out;
cpl_size nz = cpl_imagelist_get_size(data);
*out = cpl_vector_new(nz);
*err = cpl_vector_new(nz);
*contrib = cpl_array_new(nz, CPL_TYPE_INT);
/* sigmaclip on each image of the imagelist */
for (cpl_size z = 0; z < nz ; z++) {
double corr, error, low, high;
cpl_size contribution;
if (hdrl_kappa_sigma_clip_image(cpl_imagelist_get_const(data, z),
cpl_imagelist_get_const(errors, z),
par->kappa_low,
par->kappa_high,
par->niter,
&corr,
&error,
&contribution,
&low,
&high) != CPL_ERROR_NONE) {
break;
}
cpl_vector_set(*out, z, corr);
cpl_vector_set(*err, z, error);
cpl_array_set_int(*contrib, z, contribution);
if (eout) {
cpl_vector_set(eout->reject_low, z, low);
cpl_vector_set(eout->reject_high, z, high);
}
}
return cpl_error_get_code();
}
/* ---------------------------------------------------------------------------*/
/**
* @brief reduction object to reduce imagelist to a vector via kappa sigma
* clipped mean
*
* @param kappa_low low sigma bound
* @param kappa_high high sigma bound
* @param niter maximum number of clipping iterations
*
* @return sigma clip reduction object
* @see hdrl_kappa_sigma_clip()
*
* the high and low reject values are stored in extra_out if applicable
*/
/* ---------------------------------------------------------------------------*/
hdrl_collapse_imagelist_to_vector_t *
hdrl_collapse_imagelist_to_vector_sigclip(double kappa_low, double kappa_high,
int niter)
{
hdrl_collapse_imagelist_to_vector_t * s = cpl_calloc(1, sizeof(*s));
hdrl_parameter * sp =
hdrl_collapse_sigclip_parameter_create(kappa_low, kappa_high, niter);
s->func = &reduce_imagelist_to_vector_sigclip;
s->create_eout = &hdrl_sigclip_create_eout_vec;
s->move_eout = &hdrl_sigclip_move_eout_vec;
s->unwrap_eout = &cpl_free;
s->delete_eout = &hdrl_sigclip_delete_eout_vec;
s->parameters = sp;
return s;
}
/* ---------------------------------------------------------------------------*/
/**
* @internal
* @brief implements minmax-clipped combination on input image list into a vector
*
* @param data input data imagelist
* @param errors input errors imagelist
* @param out output vector
* @param err output vector errors
*
* @return cpl_error_code
*
* If all pixels of an image in the list are rejected the contribution is 0 and
* the out and err are set to NAN.
*
*/
/* ---------------------------------------------------------------------------*/
static cpl_error_code
reduce_imagelist_to_vector_minmax(const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_vector ** out, cpl_vector ** err,
cpl_array ** contrib, void * parameters,
void * extra_out)
{
hdrl_collapse_minmax_parameter * par = parameters;
hdrl_sigclip_vector_output * eout =
(hdrl_sigclip_vector_output *)extra_out;
cpl_size nz = cpl_imagelist_get_size(data);
*out = cpl_vector_new(nz);
*err = cpl_vector_new(nz);
*contrib = cpl_array_new(nz, CPL_TYPE_INT);
/* minmax on each image of the imagelist */
for (cpl_size z = 0; z < nz ; z++) {
double corr, error, low, high;
cpl_size contribution;
if (hdrl_minmax_clip_image(cpl_imagelist_get_const(data, z),
cpl_imagelist_get_const(errors, z),
par->nlow,
par->nhigh,
&corr,
&error,
&contribution,
&low, &high) != CPL_ERROR_NONE) {
break;
}
cpl_vector_set(*out, z, corr);
cpl_vector_set(*err, z, error);
cpl_array_set_int(*contrib, z, contribution);
if (eout) {
cpl_vector_set(eout->reject_low, z, low);
cpl_vector_set(eout->reject_high, z, high);
}
}
return cpl_error_get_code();
}
/* ---------------------------------------------------------------------------*/
/**
* @brief reduction object to reduce imagelist to a vector via min-max rejection
*
* @param nlow low bound
* @param nhigh high bound
*
* @return minmax rejected reduction object
* @see hdrl_minmax()
*
*/
/* ---------------------------------------------------------------------------*/
hdrl_collapse_imagelist_to_vector_t *
hdrl_collapse_imagelist_to_vector_minmax(double nlow, double nhigh)
{
hdrl_collapse_imagelist_to_vector_t * s = cpl_calloc(1, sizeof(*s));
hdrl_parameter * sp =
hdrl_collapse_minmax_parameter_create(nlow, nhigh);
s->func = &reduce_imagelist_to_vector_minmax;
s->create_eout = &hdrl_sigclip_create_eout_vec;
s->move_eout = &hdrl_sigclip_move_eout_vec;
s->unwrap_eout = &cpl_free;
s->delete_eout = &hdrl_sigclip_delete_eout_vec;
s->parameters = sp;
return s;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief Call the associated reduction function
*
* @param f reduction function object
* @param data data to apply function on
* @param errors errors to use for propagation
* @param out pointer which will contain reduced data image, type double
* @param err pointer which will contain reduced error image, type double
* @param contrib pointer which will contain contribution map, type integer
* @param eout storage for extra output, may be NULL
*
* @return cpl_error_code
*/
/* ---------------------------------------------------------------------------*/
cpl_error_code
hdrl_collapse_imagelist_to_vector_call(hdrl_collapse_imagelist_to_vector_t * f,
const cpl_imagelist * data,
const cpl_imagelist * errors,
cpl_vector ** out,
cpl_vector ** err,
cpl_array ** contrib,
void ** eout)
{
cpl_ensure_code(f, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(data, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(errors, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(out, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(err, CPL_ERROR_NULL_INPUT);
cpl_ensure_code(contrib, CPL_ERROR_NULL_INPUT);
if (eout) {
*eout = f->create_eout(cpl_imagelist_get_size(data));
}
cpl_imagelist * nerrors = wrap_synced_errlist(data, errors);
if (nerrors == NULL) {
return cpl_error_get_code();
}
cpl_error_code errcode = f->func(data, errors, out, err, contrib,
f->parameters, eout ? *eout : NULL);
unwrap_synced_errlist(nerrors);
return errcode;
}
/* ---------------------------------------------------------------------------*/
/**
* @brief delete imagelist reduction object
*
* @param p imagelist reduction object or NULL
*/
/* ---------------------------------------------------------------------------*/
void
hdrl_collapse_imagelist_to_vector_delete(hdrl_collapse_imagelist_to_vector_t * p)
{
if (p) {
cpl_free(p->parameters);
}
cpl_free(p);
}
/** @endcond */
/**@}*/
|