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 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 2013 Daniel Sabo
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
extern "C"
{
#include "paint-types.h"
#include "gegl/gimp-babl.h"
#include "operations/layer-modes/gimp-layer-modes.h"
#include "core/gimptempbuf.h"
#include "operations/gimpoperationmaskcomponents.h"
#include "operations/layer-modes/gimpoperationlayermode.h"
#include "gimppaintcore-loops.h"
} /* extern "C" */
#define PIXELS_PER_THREAD \
(/* each thread costs as much as */ 64.0 * 64.0 /* pixels */)
/* In order to avoid iterating over the same region of the same buffers
* multiple times, when calling more than one of the paint-core loop functions
* (hereafter referred to as "algorithms") in succession, we provide a single
* function, gimp_paint_core_loops_process(), which can be used to perform
* multiple algorithms in a row. This function takes a pointer to a
* GimpPaintCoreLoopsParams structure, providing the parameters for the
* algorithms, and a GimpPaintCoreLoopsAlgorithm bitset, which specifies the
* set of algorithms to run; currently, the algorithms are always run in a
* fixed order. For convenience, we provide public functions for the
* individual algorithms, but they're merely wrappers around
* gimp_paint_core_loops_process().
*
* We use some C++ magic to statically generate specialized versions of
* gimp_paint_core_loops_process() for all possible combinations of algorithms,
* and, where relevant, formats and input parameters, and to dispatch to the
* correct version at runtime.
*
* To achieve this, each algorithm provides two components:
*
* - The algorithm class template, which implements the algorithm, following
* a common interface. See the AlgorithmBase class for a description of
* the interface. Each algorithm class takes its base class as a template
* parameter, which allows us to construct a class hierarchy corresponding
* to a specific set of algorithms. Some classes in the hierarchy are not
* algorithms themselves, but are rather helpers, which provide some
* functionality to the algorithms further down the hierarchy, such as
* access to specific buffers.
*
* - A dispatch function, which takes the input parameters, the requested set
* of algorithms, the (type of) the current algorithm hierarchy, and a
* visitor object. The function calls the visitor with a (potentially)
* modified hierarchy, depending on the input. Ihe dispatch function for
* an algorithm checks if the requested set of algorithms contains a
* certain algorithm, adds the said algorithm to the hierarchy accordingly,
* and calls the visitor with the new hierarchy. See the AlgorithmDispatch
* class, which provides a dispatch-function implementation which
* algorithms can use instead of providing their own dispatch function.
*
* Helper classes in the hierarchy may also provide dispatch functions,
* which likewise modify the hierarchy based on the input parameters. For
* example, the dispatch_paint_mask() function adds a certain PaintMask
* specialization to the hierarchy, depending on the format of the paint
* mask buffer; this can be used to specialize algorithms based on the mask
* format; an algorithm that depends on the paint mask may dispatch through
* this function, before modifying the hierarchy itself.
*
* The dispatch() function is used to construct an algorithm hierarchy by
* dispatching through a list of functions. gimp_paint_core_loops_process()
* calls dispatch() with the full list of algorithm dispatch functions,
* receiving in return the algorithm hierarchy matching the input. It then
* uses the algorithm interface to perform the actual processing.
*/
enum
{
ALGORITHM_PAINT_BUF = 1u << 31,
ALGORITHM_PAINT_MASK = 1u << 30,
ALGORITHM_STIPPLE = 1u << 29,
ALGORITHM_COMP_MASK = 1u << 28,
ALGORITHM_TEMP_COMP_MASK = 1u << 27,
ALGORITHM_COMP_BUFFER = 1u << 26,
ALGORITHM_TEMP_COMP_BUFFER = 1u << 25,
ALGORITHM_CANVAS_BUFFER_ITERATOR = 1u << 24,
ALGORITHM_MASK_BUFFER_ITERATOR = 1u << 23
};
template <class T>
struct identity
{
using type = T;
};
/* dispatch():
*
* Takes a list of dispatch function objects, and calls each of them, in order,
* with the same 'params' and 'algorithms' parameters, passing 'algorithm' as
* the input hierarchy to the first dispatch function, and passing the output
* hierarchy of the previous dispatch function as the input hierarchy for the
* next dispatch function. Calls 'visitor' with the output hierarchy of the
* last dispatch function.
*
* Each algorithm hierarchy should provide a 'filter' static data member, and
* each dispatch function object should provide a 'mask' static data member.
* If the bitwise-AND of the current hierarchy's 'filter' member and the
* current dispatch function's 'mask' member is equal to 'mask', the dispatch
* function is skipped. This can be used to make sure that a class appears
* only once in the hierarchy, even if its dispatch function is used multiple
* times, or to prevent an algorithm from being dispatched, if it cannot be
* used together with another algorithm.
*/
template <class Visitor,
class Algorithm>
static inline void
dispatch (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm)
{
visitor (algorithm);
}
template <class Algorithm,
class Dispatch,
gboolean = (Algorithm::filter & Dispatch::mask) == Dispatch::mask>
struct dispatch_impl
{
template <class Visitor,
class... DispatchRest>
static void
apply (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm,
Dispatch disp,
DispatchRest... disp_rest)
{
disp (
[&] (auto algorithm)
{
dispatch (visitor, params, algorithms, algorithm, disp_rest...);
},
params, algorithms, algorithm);
}
};
template <class Algorithm,
class Dispatch>
struct dispatch_impl<Algorithm, Dispatch, TRUE>
{
template <class Visitor,
class... DispatchRest>
static void
apply (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm,
Dispatch disp,
DispatchRest... disp_rest)
{
dispatch (visitor, params, algorithms, algorithm, disp_rest...);
}
};
template <class Visitor,
class Algorithm,
class Dispatch,
class... DispatchRest>
static inline void
dispatch (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm,
Dispatch disp,
DispatchRest... disp_rest)
{
dispatch_impl<Algorithm, Dispatch>::apply (
visitor, params, algorithms, algorithm, disp, disp_rest...);
}
/* value_to_float():
*
* Converts a component value to float.
*/
static inline gfloat
value_to_float (guint8 value)
{
return value / 255.0f;
}
static inline gfloat
value_to_float (gfloat value)
{
return value;
}
template <class T>
static inline gfloat
value_to_float (T value) = delete;
/* AlgorithmBase:
*
* The base class of the algorithm hierarchy.
*/
struct AlgorithmBase
{
/* Used to filter-out dispatch functions; see the description of dispatch().
* Algorithms that redefine 'filter' should bitwise-OR their filter with that
* of their base class.
*/
static constexpr guint filter = 0;
/* The current maximal number of iterators used by the hierarchy. Algorithms
* should redefine 'max_n_iterators' by adding the maximal number of
* iterators they use to this value.
*/
static constexpr gint max_n_iterators = 0;
/* Non-static data members should be initialized in the constructor, and
* should not be further modified.
*/
explicit
AlgorithmBase (const GimpPaintCoreLoopsParams *params)
{
}
/* Algorithms should store their dynamic state in the 'State' member class
* template. This template will be instantiated with the most-derived type
* of the hierarchy, which allows an algorithm to depend on the properties of
* its descendants. Algorithms that provide their own 'State' class should
* derive it from the 'State' class of their base class, passing 'Derived' as
* the template argument.
*
* Algorithms can be run in parallel on multiple threads. In this case, each
* thread uses its own 'State' object, while the algorithm object itself is
* either shared, or is a copy of a shared algorithm object. Either way, the
* algorithm object itself is immutable, while the state object is mutable.
*/
template <class Derived>
struct State
{
};
/* The 'init()' function is called once per state object before processing
* starts, and should initialize the state object, and, if necessary, the
* iterator.
*
* 'params' is the same parameter struct passed to the constructor. 'state'
* is the state object. 'iter' is the iterator; each distinct state object
* uses a distinct iterator; if the algorithm hierarchy doesn't use any
* iterator, 'iter' may be NULL. 'roi' is the full region to be processed.
* 'area' is the subregion to be processed by the current state object.
*
* An algorithm that overrides this function should call the 'init()'
* function of its base class first, using the same arguments.
*/
template <class Derived>
void
init (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area) const
{
}
/* The 'init_step()' function is called once after each
* 'gegl_buffer_iterator_next()' call, and should perform any necessary
* initialization required before processing the current chunk.
*
* The parameters are the same as for 'init()', with the addition of 'rect',
* which is the area of the current chunk.
*
* An algorithm that overrides this function should call the 'init_step()'
* function of its base class first, using the same arguments.
*/
template <class Derived>
void
init_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect) const
{
}
/* The 'process_row()' function is called for each row in the current chunk,
* and should perform the actual processing.
*
* The parameters are the same as for 'init_step()', with the addition of
* 'y', which is the current row.
*
* An algorithm that overrides this function should call the 'process_row()'
* function of its base class first, using the same arguments.
*/
template <class Derived>
void
process_row (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect,
gint y) const
{
}
/* The 'finalize_step()' function is called once per chunk after its
* processing is done, and should finalize any chunk-specific resources of
* the state object.
*
* 'params' is the same parameter struct passed to the constructor. 'state'
* is the state object.
*
* An algorithm that overrides this function should call the
* 'finalize_step()' function of its base class after performing its own
* finalization, using the same arguments.
*/
template <class Derived>
void
finalize_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state) const
{
}
/* The 'finalize()' function is called once per state object after processing
* is done, and should finalize the state object.
*
* 'params' is the same parameter struct passed to the constructor. 'state'
* is the state object.
*
* An algorithm that overrides this function should call the 'finalize()'
* function of its base class after performing its own finalization, using
* the same arguments.
*/
template <class Derived>
void
finalize (const GimpPaintCoreLoopsParams *params,
State<Derived> *state) const
{
}
};
/* BasicDispatch:
*
* A class template implementing a simple dispatch function object, which adds
* an algorithm to the hierarchy unconditionally. 'AlgorithmTemplate' is the
* alogithm class template (usually a helper class, rather than an actual
* algorithm), 'Mask' is the dispatch function mask, as described in
* 'dispatch()', and 'Dependencies' is a list of (types of) dispatch functions
* the algorithm depends on.
*
* Before adding the algorithm to the hierarchy, the hierarchy is augmented by
* dispatching through the list of dependencies, in order.
*/
template <template <class Base> class AlgorithmTemplate,
guint Mask,
class... Dependencies>
struct BasicDispatch
{
static constexpr guint mask = Mask;
template <class Visitor,
class Algorithm>
void
operator () (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm) const
{
dispatch (
[&] (auto algorithm)
{
using NewAlgorithm = typename decltype (algorithm)::type;
visitor (identity<AlgorithmTemplate<NewAlgorithm>> ());
},
params, algorithms, algorithm, Dependencies ()...);
}
};
template <template <class Base> class AlgorithmTemplate,
guint Mask>
struct BasicDispatch<AlgorithmTemplate, Mask>
{
static constexpr guint mask = Mask;
template <class Visitor,
class Algorithm>
void
operator () (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm) const
{
visitor (identity<AlgorithmTemplate<Algorithm>> ());
}
};
/* AlgorithmDispatch:
*
* A class template implementing a dispatch function suitable for dispatching
* algorithms. 'AlgorithmTemplate' is the algorithm class template, 'Mask' is
* the dispatch function mask, as described in 'dispatch()', and 'Dependencies'
* is a list of (types of) dispatch functions the algorithm depends on, used as
* explained below.
*
* 'AlgorithmDispatch' adds the algorithm to the hierarchy if it's included in
* the set of requested algorithms; specifically, if the bitwise-AND of the
* requested-algorithms bitset and of 'Mask' is equal to 'Mask'.
*
* Before adding the algorithm to the hierarchy, the hierarchy is augmented by
* dispatching through the list of dependencies, in order.
*/
template <template <class Base> class AlgorithmTemplate,
guint Mask,
class... Dependencies>
struct AlgorithmDispatch
{
static constexpr guint mask = Mask;
template <class Visitor,
class Algorithm>
void
operator () (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm) const
{
if ((algorithms & mask) == mask)
{
dispatch (
[&] (auto algorithm)
{
using NewAlgorithm = typename decltype (algorithm)::type;
visitor (identity<AlgorithmTemplate<NewAlgorithm>> ());
},
params, algorithms, algorithm, Dependencies ()...);
}
else
{
visitor (algorithm);
}
}
};
/* MandatoryAlgorithmDispatch:
*
* A class template implementing a dispatch function suitable for dispatching
* algorithms that must be included in all hierarchies. 'AlgorithmTemplate' is
* the algorithm class template, 'Mask' is the dispatch function mask, as
* described in 'dispatch()', and 'Dependencies' is a list of (types of)
* dispatch functions the algorithm depends on, used as explained below.
*
* 'MandatoryAlgorithmDispatch' verifies that the algorithm is included in the
* set of requested algorithms (specifically, that the bitwise-AND of the
* requested-algorithms bitset and of 'Mask' is equal to 'Mask'), and adds the
* it to the hierarchy unconditionally.
*
* Before adding the algorithm to the hierarchy, the hierarchy is augmented by
* dispatching through the list of dependencies, in order.
*/
template <template <class Base> class AlgorithmTemplate,
guint Mask,
class... Dependencies>
struct MandatoryAlgorithmDispatch
{
static constexpr guint mask = Mask;
template <class Visitor,
class Algorithm>
void
operator () (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm) const
{
g_return_if_fail ((algorithms & Mask) == Mask);
BasicDispatch<AlgorithmTemplate, Mask, Dependencies...> () (visitor,
params,
algorithms,
algorithm);
}
};
/* SuppressedAlgorithmDispatch:
*
* A class template implementing a placeholder dispatch function suitable for
* dispatching algorithms that are never included in any hierarchy.
* 'AlgorithmTemplate' is the algorithm class template, 'Mask' is the dispatch
* function mask, as described in 'dispatch()', and 'Dependencies' is a list of
* (types of) dispatch functions the algorithm depends on. Note that
* 'AlgorithmTemplate' and 'Dependencies' are not actually used, and are merely
* included for exposition.
*
* 'SuppressedAlgorithmDispatch' verifies that the algorithm is not included in
* the set of requested algorithms (specifically, that the bitwise-AND of the
* requested-algorithms bitset and of 'Mask' is not equal to 'Mask'), and
* doesn't modify the hierarchy.
*/
template <template <class Base> class AlgorithmTemplate,
guint Mask,
class... Dependencies>
struct SuppressedAlgorithmDispatch
{
static constexpr guint mask = Mask;
template <class Visitor,
class Algorithm>
void
operator () (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm) const
{
g_return_if_fail ((algorithms & Mask) != Mask);
visitor (algorithm);
}
};
/* PaintBuf, dispatch_paint_buf():
*
* An algorithm helper class, providing access to the paint buffer. Algorithms
* that use the paint buffer should specify 'dispatch_paint_buf()' as a
* dependency, and access 'PaintBuf' members through their base type/subobject.
*/
template <class Base>
struct PaintBuf : Base
{
/* Component type of the paint buffer. */
using paint_type = gfloat;
static constexpr guint filter = Base::filter | ALGORITHM_PAINT_BUF;
/* Paint buffer stride, in 'paint_type' elements. */
gint paint_stride;
/* Pointer to the start of the paint buffer data. */
paint_type *paint_data;
explicit
PaintBuf (const GimpPaintCoreLoopsParams *params) :
Base (params)
{
paint_stride = gimp_temp_buf_get_width (params->paint_buf) * 4;
paint_data = (paint_type *) gimp_temp_buf_get_data (params->paint_buf);
}
};
static BasicDispatch<PaintBuf, ALGORITHM_PAINT_BUF> dispatch_paint_buf;
/* PaintMask, dispatch_paint_mask():
*
* An algorithm helper class, providing access to the paint mask. Algorithms
* that use the paint mask should specify 'dispatch_paint_mask()' as a
* dependency, and access 'PaintMask' members through their base type/
* subobject.
*/
template <class Base,
class MaskType>
struct PaintMask : Base
{
/* Component type of the paint mask. */
using mask_type = MaskType;
static constexpr guint filter = Base::filter | ALGORITHM_PAINT_MASK;
/* Paint mask stride, in 'mask_type' elements. */
gint mask_stride;
/* Pointer to the start of the paint mask data, taking the mask offset into
* account.
*/
const mask_type *mask_data;
explicit
PaintMask (const GimpPaintCoreLoopsParams *params) :
Base (params)
{
mask_stride = gimp_temp_buf_get_width (params->paint_mask);
mask_data =
(const mask_type *) gimp_temp_buf_get_data (params->paint_mask) +
params->paint_mask_offset_y * mask_stride +
params->paint_mask_offset_x;
}
};
struct DispatchPaintMask
{
static constexpr guint mask = ALGORITHM_PAINT_MASK;
template <class Visitor,
class Algorithm>
void
operator () (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm) const
{
const Babl *mask_format = gimp_temp_buf_get_format (params->paint_mask);
if (mask_format == babl_format ("Y u8"))
visitor (identity<PaintMask<Algorithm, guint8>> ());
else if (mask_format == babl_format ("Y float"))
visitor (identity<PaintMask<Algorithm, gfloat>> ());
else
g_warning ("Mask format not supported: %s", babl_get_name (mask_format));
}
} static dispatch_paint_mask;
/* Stipple, dispatch_stipple():
*
* An algorithm helper class, providing access to the 'stipple' parameter.
* Algorithms that use the 'stipple' parameter should specify
* 'dispatch_stipple()' as a dependency, and access 'Stipple' members through
* their base type/subobject.
*/
template <class Base,
gboolean StippleFlag>
struct Stipple : Base
{
static constexpr guint filter = Base::filter | ALGORITHM_STIPPLE;
/* The value of the 'stipple' parameter, usable as a constant expression. */
static constexpr gboolean stipple = StippleFlag;
using Base::Base;
};
struct DispatchStipple
{
static constexpr guint mask = ALGORITHM_STIPPLE;
template <class Visitor,
class Algorithm>
void
operator () (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm) const
{
if (params->stipple)
visitor (identity<Stipple<Algorithm, TRUE>> ());
else
visitor (identity<Stipple<Algorithm, FALSE>> ());
}
} static dispatch_stipple;
/* CompMask, dispatch_comp_mask(), has_comp_mask(), comp_mask_data():
*
* An algorithm helper class, providing access to the mask used for
* compositing. When this class is part of the hierarchy, 'DoLayerBlend' uses
* this buffer as the mask, instead of the input parameters' 'mask_buffer'.
* Algorithms that use the compositing mask should specify
* 'dispatch_comp_mask()' as a dependency, and access 'CompMask' members
* through their base type/subobject.
*
* Note that 'CompMask' only provides *access* to the compositing mask, but
* doesn't provide its actual *storage*. This is the responsibility of the
* algorithms that use 'CompMask'. Algorithms that need temporary storage for
* the compositing mask can use 'TempCompMask'.
*
* The 'has_comp_mask()' constexpr function determines if a given algorithm
* hierarchy uses the compositing mask.
*
* The 'comp_mask_data()' function returns a pointer to the compositing mask
* data for the current row if the hierarchy uses the compositing mask, or NULL
* otherwise.
*/
template <class Base>
struct CompMask : Base
{
/* Component type of the compositing mask. */
using comp_mask_type = gfloat;
static constexpr guint filter = Base::filter | ALGORITHM_COMP_MASK;
using Base::Base;
template <class Derived>
struct State : Base::template State<Derived>
{
/* Pointer to the compositing mask data for the current row. */
comp_mask_type *comp_mask_data;
};
};
static BasicDispatch<CompMask, ALGORITHM_COMP_MASK> dispatch_comp_mask;
template <class Base>
static constexpr gboolean
has_comp_mask (const CompMask<Base> *algorithm)
{
return TRUE;
}
static constexpr gboolean
has_comp_mask (const AlgorithmBase *algorithm)
{
return FALSE;
}
template <class Base,
class State>
static gfloat *
comp_mask_data (const CompMask<Base> *algorithm,
State *state)
{
return state->comp_mask_data;
}
template <class State>
static gfloat *
comp_mask_data (const AlgorithmBase *algorithm,
State *state)
{
return NULL;
}
/* TempCompMask, dispatch_temp_comp_mask():
*
* An algorithm helper class, providing temporary storage for the compositing
* mask. Algorithms that need a temporary compositing mask should specify
* 'dispatch_temp_comp_mask()' as a dependency, which itself includes
* 'dispatch_comp_mask()' as a dependency.
*/
template <class Base>
struct TempCompMask : Base
{
static constexpr guint filter = Base::filter | ALGORITHM_TEMP_COMP_MASK;
using Base::Base;
template <class Derived>
using State = typename Base::template State<Derived>;
template <class Derived>
void
init_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect) const
{
Base::init_step (params, state, iter, roi, area, rect);
state->comp_mask_data = gegl_scratch_new (gfloat, rect->width);
}
template <class Derived>
void
finalize_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state) const
{
gegl_scratch_free (state->comp_mask_data);
Base::finalize_step (params, state);
}
};
static BasicDispatch<
TempCompMask,
ALGORITHM_TEMP_COMP_MASK,
decltype (dispatch_comp_mask)
> dispatch_temp_comp_mask;
/* CompBuffer, dispatch_comp_buffer(), has_comp_buffer(), comp_buffer_data():
*
* An algorithm helper class, providing access to the output buffer used for
* compositing. When this class is part of the hierarchy, 'DoLayerBlend' uses
* this buffer as the output buffer, instead of the input parameters'
* 'dest_buffer'. Algorithms that use the compositing buffer should specify
* 'dispatch_comp_buffer()' as a dependency, and access 'CompBuffer' members
* through their base type/subobject.
*
* Note that 'CompBuffer' only provides *access* to the compositing buffer, but
* doesn't provide its actual *storage*. This is the responsibility of the
* algorithms that use 'CompBuffer'. Algorithms that need temporary storage
* for the compositing buffer can use 'TempCompBuffer'.
*
* The 'has_comp_buffer()' constexpr function determines if a given algorithm
* hierarchy uses the compositing buffer.
*
* The 'comp_buffer_data()' function returns a pointer to the compositing
* buffer data for the current row if the hierarchy uses the compositing
* buffer, or NULL otherwise.
*/
template <class Base>
struct CompBuffer : Base
{
/* Component type of the compositing buffer. */
using comp_buffer_type = gfloat;
static constexpr guint filter = Base::filter | ALGORITHM_COMP_BUFFER;
using Base::Base;
template <class Derived>
struct State : Base::template State<Derived>
{
/* Pointer to the compositing buffer data for the current row. */
comp_buffer_type *comp_buffer_data;
};
};
static BasicDispatch<CompBuffer, ALGORITHM_COMP_BUFFER> dispatch_comp_buffer;
template <class Base>
static constexpr gboolean
has_comp_buffer (const CompBuffer<Base> *algorithm)
{
return TRUE;
}
static constexpr gboolean
has_comp_buffer (const AlgorithmBase *algorithm)
{
return FALSE;
}
template <class Base,
class State>
static gfloat *
comp_buffer_data (const CompBuffer<Base> *algorithm,
State *state)
{
return state->comp_buffer_data;
}
template <class State>
static gfloat *
comp_buffer_data (const AlgorithmBase *algorithm,
State *state)
{
return NULL;
}
/* TempCompBuffer, dispatch_temp_comp_buffer():
*
* An algorithm helper class, providing temporary storage for the compositing
* buffer. Algorithms that need a temporary compositing buffer should specify
* 'dispatch_temp_comp_buffer()' as a dependency, which itself includes
* 'dispatch_comp_buffer()' as a dependency.
*/
template <class Base>
struct TempCompBuffer : Base
{
static constexpr guint filter = Base::filter | ALGORITHM_TEMP_COMP_BUFFER;
using Base::Base;
template <class Derived>
using State = typename Base::template State<Derived>;
template <class Derived>
void
init_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect) const
{
Base::init_step (params, state, iter, roi, area, rect);
state->comp_buffer_data = gegl_scratch_new (gfloat, 4 * rect->width);
}
template <class Derived>
void
finalize_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state) const
{
gegl_scratch_free (state->comp_buffer_data);
Base::finalize_step (params, state);
}
};
static BasicDispatch<
TempCompBuffer,
ALGORITHM_TEMP_COMP_BUFFER,
decltype (dispatch_comp_buffer)
> dispatch_temp_comp_buffer;
/* CanvasBufferIterator, DispatchCanvasBufferIterator:
*
* An algorithm helper class, providing iterator-access to the canvas buffer.
* Algorithms that iterate over the canvas buffer should specify
* 'DispatchCanvasBufferIterator<Access>' as a dependency, where 'Access' is
* the desired access mode to the canvas buffer, and access its members through
* their base type/subobject.
*/
template <class Base,
guint Access,
gboolean First>
struct CanvasBufferIterator;
template <class Base,
guint Access>
static constexpr gboolean
canvas_buffer_iterator_is_first (CanvasBufferIterator<Base, Access, TRUE> *algorithm)
{
return FALSE;
}
static constexpr gboolean
canvas_buffer_iterator_is_first (AlgorithmBase *algorithm)
{
return TRUE;
}
template <class Base,
guint Access,
gboolean First = canvas_buffer_iterator_is_first ((Base *) NULL)>
struct CanvasBufferIterator : Base
{
/* The combined canvas-buffer access mode used by the hierarchy, up to, and
* including, the current class.
*/
static constexpr GeglAccessMode canvas_buffer_access =
(GeglAccessMode) (Base::canvas_buffer_access | Access);
using Base::Base;
};
template <class Base,
guint Access>
struct CanvasBufferIterator<Base, Access, TRUE> : Base
{
/* The combined canvas-buffer access mode used by the hierarchy, up to, and
* including, the current class.
*/
static constexpr GeglAccessMode canvas_buffer_access =
(GeglAccessMode) Access;
static constexpr gint max_n_iterators =
Base::max_n_iterators + 1;
using Base::Base;
template <class Derived>
struct State : Base::template State<Derived>
{
gint canvas_buffer_iterator;
};
template <class Derived>
void
init (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area) const
{
state->canvas_buffer_iterator = gegl_buffer_iterator_add (
iter, params->canvas_buffer, area, 0, babl_format ("Y float"),
Derived::canvas_buffer_access, GEGL_ABYSS_NONE);
/* initialize the base class *after* initializing the iterator, to make
* sure that canvas_buffer is the primary buffer of the iterator, if no
* subclass added an iterator first.
*/
Base::init (params, state, iter, roi, area);
}
};
template <guint Access>
struct DispatchCanvasBufferIteratorHelper
{
template <class Base>
using algorithm_template = CanvasBufferIterator<Base, Access>;
};
template <guint Access>
using DispatchCanvasBufferIterator = BasicDispatch<
DispatchCanvasBufferIteratorHelper<Access>::template algorithm_template,
ALGORITHM_CANVAS_BUFFER_ITERATOR
>;
/* MaskBufferIterator, mask_buffer_iterator_dispatch(),
* has_mask_buffer_iterator():
*
* An algorithm helper class, providing read-only iterator-access to the mask
* buffer. Algorithms that iterate over the mask buffer should specify
* 'dispatch_mask_buffer_iterator()' as a dependency, and access
* 'MaskBufferIterator' members through their base type/subobject.
*
* The 'has_mask_buffer_iterator()' constexpr function determines if a given
* algorithm hierarchy uses has a mask-buffer iterator.
*
* The 'mask_buffer_iterator()' function returns the index of the mask-buffer
* iterator if the hierarchy has one, or -1 otherwise.
*/
template <class Base>
struct MaskBufferIterator : Base
{
static constexpr guint filter = Base::filter | ALGORITHM_MASK_BUFFER_ITERATOR;
static constexpr gint max_n_iterators = Base::max_n_iterators + 1;
using Base::Base;
template <class Derived>
struct State : Base::template State<Derived>
{
gint mask_buffer_iterator;
};
template <class Derived>
void
init (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area) const
{
Base::init (params, state, iter, roi, area);
GeglRectangle mask_area = *area;
mask_area.x -= params->mask_offset_x;
mask_area.y -= params->mask_offset_y;
state->mask_buffer_iterator = gegl_buffer_iterator_add (
iter, params->mask_buffer, &mask_area, 0, babl_format ("Y float"),
GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
}
};
struct DispatchMaskBufferIterator
{
static constexpr guint mask = ALGORITHM_MASK_BUFFER_ITERATOR;
template <class Visitor,
class Algorithm>
void
operator () (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm) const
{
if (params->mask_buffer)
visitor (identity<MaskBufferIterator<Algorithm>> ());
else
visitor (algorithm);
}
} static dispatch_mask_buffer_iterator;
template <class Base>
static constexpr gboolean
has_mask_buffer_iterator (const MaskBufferIterator<Base> *algorithm)
{
return TRUE;
}
static constexpr gboolean
has_mask_buffer_iterator (const AlgorithmBase *algorithm)
{
return FALSE;
}
template <class Base,
class State>
static gint
mask_buffer_iterator (const MaskBufferIterator<Base> *algorithm,
State *state)
{
return state->mask_buffer_iterator;
}
template <class State>
static gint
mask_buffer_iterator (const AlgorithmBase *algorithm,
State *state)
{
return -1;
}
/* CombinePaintMaskToCanvasBufferToPaintBufAlpha,
* dispatch_combine_paint_mask_to_canvas_buffer_to_paint_buf_alpha():
*
* An algorithm class, providing an optimized version combining both the
* COMBINE_PAINT_MASK_TO_CANVAS_BUFFER and the CANVAS_BUFFER_TO_PAINT_BUF_ALPHA
* algorithms. Used instead of the individual implementations, when both
* algorithms are requested.
*/
template <class Base>
struct CombinePaintMaskToCanvasBufferToPaintBufAlpha : Base
{
using mask_type = typename Base::mask_type;
static constexpr guint filter =
Base::filter |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_COMBINE_PAINT_MASK_TO_CANVAS_BUFFER |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_PAINT_BUF_ALPHA |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_PAINT_BUF_ALPHA |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_COMP_MASK |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_COMP_MASK;
using Base::Base;
template <class Derived>
struct State : Base::template State<Derived>
{
gfloat *canvas_pixel;
};
template <class Derived>
void
init_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect) const
{
Base::init_step (params, state, iter, roi, area, rect);
state->canvas_pixel =
(gfloat *) iter->items[state->canvas_buffer_iterator].data;
}
template <class Derived>
void
process_row (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect,
gint y) const
{
Base::process_row (params, state, iter, roi, area, rect, y);
gint mask_offset = (y - roi->y) * this->mask_stride +
(rect->x - roi->x);
const mask_type *mask_pixel = &this->mask_data[mask_offset];
gint paint_offset = (y - roi->y) * this->paint_stride +
(rect->x - roi->x) * 4;
gfloat *paint_pixel = &this->paint_data[paint_offset];
gint x;
for (x = 0; x < rect->width; x++)
{
if (Base::stipple)
{
state->canvas_pixel[0] += (1.0 - state->canvas_pixel[0]) *
value_to_float (*mask_pixel) *
params->paint_opacity;
}
else
{
if (params->paint_opacity > state->canvas_pixel[0])
{
state->canvas_pixel[0] += (params->paint_opacity - state->canvas_pixel[0]) *
value_to_float (*mask_pixel) *
params->paint_opacity;
}
}
paint_pixel[3] *= state->canvas_pixel[0];
mask_pixel += 1;
state->canvas_pixel += 1;
paint_pixel += 4;
}
}
};
static SuppressedAlgorithmDispatch<
CombinePaintMaskToCanvasBufferToPaintBufAlpha,
GIMP_PAINT_CORE_LOOPS_ALGORITHM_COMBINE_PAINT_MASK_TO_CANVAS_BUFFER |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_PAINT_BUF_ALPHA,
decltype (dispatch_paint_buf),
decltype (dispatch_paint_mask),
decltype (dispatch_stipple),
DispatchCanvasBufferIterator<GEGL_BUFFER_READWRITE>
>
dispatch_combine_paint_mask_to_canvas_buffer_to_paint_buf_alpha;
/* CombinePaintMaskToCanvasBuffer,
* dispatch_combine_paint_mask_to_canvas_buffer():
*
* An algorithm class, implementing the COMBINE_PAINT_MASK_TO_CANVAS_BUFFER
* algorithm.
*/
template <class Base>
struct CombinePaintMaskToCanvasBuffer : Base
{
using mask_type = typename Base::mask_type;
static constexpr guint filter =
Base::filter |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_COMBINE_PAINT_MASK_TO_CANVAS_BUFFER |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_PAINT_BUF_ALPHA |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_PAINT_BUF_ALPHA |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_COMP_MASK;
using Base::Base;
template <class Derived>
struct State : Base::template State<Derived>
{
gfloat *canvas_pixel;
};
template <class Derived>
void
init_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect) const
{
Base::init_step (params, state, iter, roi, area, rect);
state->canvas_pixel =
(gfloat *) iter->items[state->canvas_buffer_iterator].data;
}
template <class Derived>
void
process_row (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect,
gint y) const
{
Base::process_row (params, state, iter, roi, area, rect, y);
gint mask_offset = (y - roi->y) * this->mask_stride +
(rect->x - roi->x);
const mask_type *mask_pixel = &this->mask_data[mask_offset];
gint x;
for (x = 0; x < rect->width; x++)
{
if (Base::stipple)
{
state->canvas_pixel[0] += (1.0 - state->canvas_pixel[0]) *
value_to_float (*mask_pixel) *
params->paint_opacity;
}
else
{
if (params->paint_opacity > state->canvas_pixel[0])
{
state->canvas_pixel[0] += (params->paint_opacity - state->canvas_pixel[0]) *
value_to_float (*mask_pixel) *
params->paint_opacity;
}
}
mask_pixel += 1;
state->canvas_pixel += 1;
}
}
};
static AlgorithmDispatch<
CombinePaintMaskToCanvasBuffer,
GIMP_PAINT_CORE_LOOPS_ALGORITHM_COMBINE_PAINT_MASK_TO_CANVAS_BUFFER,
decltype (dispatch_paint_mask),
decltype (dispatch_stipple),
DispatchCanvasBufferIterator<GEGL_BUFFER_READWRITE>
>
dispatch_combine_paint_mask_to_canvas_buffer;
/* CanvasBufferToPaintBufAlpha, dispatch_canvas_buffer_to_paint_buf_alpha():
*
* An algorithm class, implementing the CANVAS_BUFFER_TO_PAINT_BUF_ALPHA
* algorithm.
*/
template <class Base>
struct CanvasBufferToPaintBufAlpha : Base
{
static constexpr guint filter =
Base::filter |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_PAINT_BUF_ALPHA |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_PAINT_BUF_ALPHA |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_COMP_MASK |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_COMP_MASK;
using Base::Base;
template <class Derived>
struct State : Base::template State<Derived>
{
const gfloat *canvas_pixel;
};
template <class Derived>
void
init_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect) const
{
Base::init_step (params, state, iter, roi, area, rect);
state->canvas_pixel =
(const gfloat *) iter->items[state->canvas_buffer_iterator].data;
}
template <class Derived>
void
process_row (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect,
gint y) const
{
Base::process_row (params, state, iter, roi, area, rect, y);
/* Copy the canvas buffer in rect to the paint buffer's alpha channel */
gint paint_offset = (y - roi->y) * this->paint_stride +
(rect->x - roi->x) * 4;
gfloat *paint_pixel = &this->paint_data[paint_offset];
gint x;
for (x = 0; x < rect->width; x++)
{
paint_pixel[3] *= *state->canvas_pixel;
state->canvas_pixel += 1;
paint_pixel += 4;
}
}
};
static SuppressedAlgorithmDispatch<
CanvasBufferToPaintBufAlpha,
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_PAINT_BUF_ALPHA,
decltype (dispatch_paint_buf),
DispatchCanvasBufferIterator<GEGL_BUFFER_READ>
>
dispatch_canvas_buffer_to_paint_buf_alpha;
/* PaintMaskToPaintBufAlpha, dispatch_paint_mask_to_paint_buf_alpha():
*
* An algorithm class, implementing the PAINT_MASK_TO_PAINT_BUF_ALPHA
* algorithm.
*/
template <class Base>
struct PaintMaskToPaintBufAlpha : Base
{
using mask_type = typename Base::mask_type;
static constexpr guint filter =
Base::filter |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_PAINT_BUF_ALPHA |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_COMP_MASK |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_COMP_MASK;
explicit
PaintMaskToPaintBufAlpha (const GimpPaintCoreLoopsParams *params) :
Base (params)
{
/* Validate that the paint buffer is within the bounds of the paint mask */
g_return_if_fail (gimp_temp_buf_get_width (params->paint_buf) <=
gimp_temp_buf_get_width (params->paint_mask) -
params->paint_mask_offset_x);
g_return_if_fail (gimp_temp_buf_get_height (params->paint_buf) <=
gimp_temp_buf_get_height (params->paint_mask) -
params->paint_mask_offset_y);
}
template <class Derived>
using State = typename Base::template State<Derived>;
template <class Derived>
void
process_row (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect,
gint y) const
{
Base::process_row (params, state, iter, roi, area, rect, y);
gint paint_offset = (y - roi->y) * this->paint_stride +
(rect->x - roi->x) * 4;
gfloat *paint_pixel = &this->paint_data[paint_offset];
gint mask_offset = (y - roi->y) * this->mask_stride +
(rect->x - roi->x);
const mask_type *mask_pixel = &this->mask_data[mask_offset];
gint x;
for (x = 0; x < rect->width; x++)
{
paint_pixel[3] *= value_to_float (*mask_pixel) * params->paint_opacity;
mask_pixel += 1;
paint_pixel += 4;
}
}
};
static SuppressedAlgorithmDispatch<
PaintMaskToPaintBufAlpha,
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_PAINT_BUF_ALPHA,
decltype (dispatch_paint_buf),
decltype (dispatch_paint_mask)
>
dispatch_paint_mask_to_paint_buf_alpha;
/* CanvasBufferToCompMask, dispatch_canvas_buffer_to_comp_mask():
*
* An algorithm class, implementing the CANVAS_BUFFER_TO_COMP_MASK algorithm.
*/
template <class Base,
gboolean Direct>
struct CanvasBufferToCompMask : Base
{
using comp_mask_type = typename Base::comp_mask_type;
static constexpr guint filter =
Base::filter |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_COMP_MASK |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_COMP_MASK;
using Base::Base;
template <class Derived>
struct State : Base::template State<Derived>
{
const gfloat *canvas_pixel;
const gfloat *mask_pixel;
};
template <class Derived>
void
init_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect) const
{
Base::init_step (params, state, iter, roi, area, rect);
state->canvas_pixel =
(const gfloat *) iter->items[state->canvas_buffer_iterator].data;
state->mask_pixel =
(const gfloat *) iter->items[state->mask_buffer_iterator].data;
}
template <class Derived>
void
process_row (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect,
gint y) const
{
Base::process_row (params, state, iter, roi, area, rect, y);
comp_mask_type *comp_mask_pixel = state->comp_mask_data;
gint x;
for (x = 0; x < rect->width; x++)
{
comp_mask_pixel[0] = state->canvas_pixel[0] * state->mask_pixel[0];
comp_mask_pixel += 1;
state->canvas_pixel += 1;
state->mask_pixel += 1;
}
}
};
template <class Base>
struct CanvasBufferToCompMask<Base, TRUE> : Base
{
static constexpr guint filter =
Base::filter |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_COMP_MASK |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_COMP_MASK;
using Base::Base;
template <class Derived>
using State = typename Base::template State<Derived>;
template <class Derived>
void
init_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect) const
{
Base::init_step (params, state, iter, roi, area, rect);
state->comp_mask_data =
(gfloat *) iter->items[state->canvas_buffer_iterator].data - rect->width;
}
template <class Derived>
void
process_row (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect,
gint y) const
{
Base::process_row (params, state, iter, roi, area, rect, y);
state->comp_mask_data += rect->width;
}
};
struct DispatchCanvasBufferToCompMask
{
static constexpr guint mask =
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_COMP_MASK;
template <class Base>
using AlgorithmDirect = CanvasBufferToCompMask<Base, TRUE>;
template <class Base>
using AlgorithmIndirect = CanvasBufferToCompMask<Base, FALSE>;
using DispatchDirect = BasicDispatch<
AlgorithmDirect,
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_COMP_MASK,
decltype (dispatch_comp_mask)
>;
using DispatchIndirect = BasicDispatch<
AlgorithmIndirect,
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_COMP_MASK,
decltype (dispatch_temp_comp_mask)
>;
template <class Algorithm,
gboolean HasMaskBufferIterator = has_mask_buffer_iterator (
(Algorithm *) NULL)>
struct Dispatch : DispatchIndirect
{
};
template <class Algorithm>
struct Dispatch<Algorithm, FALSE> : DispatchDirect
{
};
template <class Visitor,
class Algorithm>
void
operator () (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm) const
{
if ((algorithms & mask) == mask)
{
dispatch (
[&] (auto algorithm)
{
using NewAlgorithm = typename decltype (algorithm)::type;
Dispatch<NewAlgorithm> () (visitor, params, algorithms, algorithm);
},
params, algorithms, algorithm,
DispatchCanvasBufferIterator<GEGL_BUFFER_READ> (),
dispatch_mask_buffer_iterator);
}
else
{
visitor (algorithm);
}
}
} static dispatch_canvas_buffer_to_comp_mask;
/* PaintMaskToCompMask, dispatch_paint_mask_to_comp_mask():
*
* An algorithm class, implementing the PAINT_MASK_TO_COMP_MASK algorithm.
*/
template <class Base,
gboolean Direct>
struct PaintMaskToCompMask : Base
{
using mask_type = typename Base::mask_type;
using comp_mask_type = typename Base::comp_mask_type;
static constexpr guint filter =
Base::filter |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_COMP_MASK;
using Base::Base;
template <class Derived>
struct State : Base::template State<Derived>
{
const gfloat *mask_pixel;
};
template <class Derived>
void
init_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect) const
{
Base::init_step (params, state, iter, roi, area, rect);
if (has_mask_buffer_iterator (this))
{
state->mask_pixel =
(const gfloat *) iter->items[mask_buffer_iterator (this, state)].data;
}
}
template <class Derived>
void
process_row (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect,
gint y) const
{
Base::process_row (params, state, iter, roi, area, rect, y);
gint mask_offset = (y - roi->y) * this->mask_stride +
(rect->x - roi->x);
const mask_type *mask_pixel = &this->mask_data[mask_offset];
comp_mask_type *comp_mask_pixel = state->comp_mask_data;
gint x;
if (has_mask_buffer_iterator (this))
{
for (x = 0; x < rect->width; x++)
{
comp_mask_pixel[0] = value_to_float (mask_pixel[0]) *
state->mask_pixel[0] *
params->paint_opacity;
comp_mask_pixel += 1;
mask_pixel += 1;
state->mask_pixel += 1;
}
}
else
{
for (x = 0; x < rect->width; x++)
{
comp_mask_pixel[0] = value_to_float (mask_pixel[0]) *
params->paint_opacity;
comp_mask_pixel += 1;
mask_pixel += 1;
}
}
}
};
template <class Base>
struct PaintMaskToCompMask<Base, TRUE> : Base
{
using mask_type = typename Base::mask_type;
static constexpr guint filter =
Base::filter |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_COMP_MASK |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_CANVAS_BUFFER_TO_COMP_MASK;
using Base::Base;
template <class Derived>
using State = typename Base::template State<Derived>;
template <class Derived>
void
init_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect) const
{
Base::init_step (params, state, iter, roi, area, rect);
gint mask_offset = (rect->y - roi->y) * this->mask_stride +
(rect->x - roi->x);
state->comp_mask_data = (mask_type *) &this->mask_data[mask_offset] -
this->mask_stride;
}
template <class Derived>
void
process_row (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect,
gint y) const
{
Base::process_row (params, state, iter, roi, area, rect, y);
state->comp_mask_data += this->mask_stride;
}
};
struct DispatchPaintMaskToCompMask
{
static constexpr guint mask =
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_COMP_MASK;
template <class Base>
using AlgorithmDirect = PaintMaskToCompMask<Base, TRUE>;
template <class Base>
using AlgorithmIndirect = PaintMaskToCompMask<Base, FALSE>;
using DispatchDirect = BasicDispatch<
AlgorithmDirect,
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_COMP_MASK,
decltype (dispatch_comp_mask)
>;
using DispatchIndirect = BasicDispatch<
AlgorithmIndirect,
GIMP_PAINT_CORE_LOOPS_ALGORITHM_PAINT_MASK_TO_COMP_MASK,
decltype (dispatch_temp_comp_mask)
>;
template <class Algorithm,
class MaskType = typename Algorithm::mask_type,
gboolean HasMaskBufferIterator = has_mask_buffer_iterator (
(Algorithm *) NULL)>
struct Dispatch : DispatchIndirect
{
};
template <class Algorithm>
struct Dispatch<Algorithm, gfloat, FALSE> : DispatchDirect
{
};
template <class Visitor,
class Algorithm>
void
operator () (Visitor visitor,
const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms,
identity<Algorithm> algorithm) const
{
if ((algorithms & mask) == mask)
{
dispatch (
[&] (auto algorithm)
{
using NewAlgorithm = typename decltype (algorithm)::type;
if (params->paint_opacity == GIMP_OPACITY_OPAQUE)
Dispatch<NewAlgorithm> () (visitor, params, algorithms, algorithm);
else
DispatchIndirect () (visitor, params, algorithms, algorithm);
},
params, algorithms, algorithm,
dispatch_paint_mask,
dispatch_mask_buffer_iterator);
}
else
{
visitor (algorithm);
}
}
} static dispatch_paint_mask_to_comp_mask;
/* DoLayerBlend, dispatch_do_layer_blend():
*
* An algorithm class, implementing the DO_LAYER_BLEND algorithm.
*/
template <class Base>
struct DoLayerBlend : Base
{
static constexpr guint filter =
Base::filter |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_DO_LAYER_BLEND;
static constexpr gint max_n_iterators = Base::max_n_iterators + 2;
const Babl *iterator_format;
GimpOperationLayerMode *layer_mode = NULL;
explicit
DoLayerBlend (const GimpPaintCoreLoopsParams *params) :
Base (params)
{
layer_mode = GIMP_OPERATION_LAYER_MODE (gimp_layer_mode_get_operation (params->paint_mode));
layer_mode->opacity = params->image_opacity;
iterator_format = gimp_layer_mode_get_format (params->paint_mode,
layer_mode->blend_space,
layer_mode->composite_space,
layer_mode->composite_mode,
gimp_temp_buf_get_format (params->paint_buf));
g_return_if_fail (gimp_temp_buf_get_format (params->paint_buf) == iterator_format);
}
template <class Derived>
struct State : Base::template State<Derived>
{
gint iterator_base;
GeglRectangle process_roi;
gfloat *out_pixel;
gfloat *in_pixel;
gfloat *mask_pixel;
gfloat *paint_pixel;
};
template <class Derived>
void
init (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area) const
{
state->iterator_base = gegl_buffer_iterator_add (iter, params->src_buffer,
area, 0, iterator_format,
GEGL_ACCESS_READ,
GEGL_ABYSS_NONE);
if (! has_comp_buffer ((const Derived *) this))
{
gegl_buffer_iterator_add (iter, params->dest_buffer, area, 0,
iterator_format,
GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);
}
/* initialize the base class *after* initializing the iterator, to make
* sure that src_buffer is the primary buffer of the iterator, if no
* subclass added an iterator first.
*/
Base::init (params, state, iter, roi, area);
}
template <class Derived>
void
init_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect) const
{
Base::init_step (params, state, iter, roi, area, rect);
state->in_pixel = (gfloat *) iter->items[state->iterator_base + 0].data;
state->paint_pixel = this->paint_data +
(rect->y - roi->y) * this->paint_stride +
(rect->x - roi->x) * 4;
if (! has_comp_mask (this) && has_mask_buffer_iterator (this))
{
state->mask_pixel =
(gfloat *) iter->items[mask_buffer_iterator (this, state)].data;
}
if (! has_comp_buffer ((const Derived *) this))
state->out_pixel = (gfloat *) iter->items[state->iterator_base + 1].data;
state->process_roi.x = rect->x;
state->process_roi.width = rect->width;
state->process_roi.height = 1;
}
template <class Derived>
void
process_row (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect,
gint y) const
{
Base::process_row (params, state, iter, roi, area, rect, y);
gfloat *mask_pixel;
gfloat *out_pixel;
if (has_comp_mask (this))
mask_pixel = comp_mask_data (this, state);
else if (has_mask_buffer_iterator (this))
mask_pixel = state->mask_pixel;
else
mask_pixel = NULL;
if (! has_comp_buffer ((const Derived *) this))
{
out_pixel = state->out_pixel;
}
else
{
out_pixel = comp_buffer_data (
(const Derived *) this,
(typename Derived::template State<Derived> *) state);
}
state->process_roi.y = y;
layer_mode->function ((GeglOperation*) layer_mode,
state->in_pixel,
state->paint_pixel,
mask_pixel,
out_pixel,
rect->width,
&state->process_roi,
0);
state->in_pixel += rect->width * 4;
state->paint_pixel += this->paint_stride;
if (! has_comp_mask (this) && has_mask_buffer_iterator (this))
state->mask_pixel += rect->width;
if (! has_comp_buffer ((const Derived *) this))
state->out_pixel += rect->width * 4;
}
};
static MandatoryAlgorithmDispatch<
DoLayerBlend,
GIMP_PAINT_CORE_LOOPS_ALGORITHM_DO_LAYER_BLEND,
decltype (dispatch_paint_buf),
decltype (dispatch_mask_buffer_iterator)
>
dispatch_do_layer_blend;
/* MaskComponents, dispatch_mask_components():
*
* An algorithm class, implementing the MASK_COMPONENTS algorithm.
*/
template <class Base>
struct MaskComponents : Base
{
static constexpr guint filter =
Base::filter |
GIMP_PAINT_CORE_LOOPS_ALGORITHM_MASK_COMPONENTS;
static constexpr gint max_n_iterators = Base::max_n_iterators + 1;
const Babl *format;
const Babl *comp_fish = NULL;
explicit
MaskComponents (const GimpPaintCoreLoopsParams *params) :
Base (params)
{
format = gimp_operation_mask_components_get_format (
gegl_buffer_get_format (params->dest_buffer));
if (format != this->iterator_format)
comp_fish = babl_fish (this->iterator_format, format);
}
template <class Derived>
struct State : Base::template State<Derived>
{
gint dest_buffer_iterator;
guint8 *dest_pixel;
guint8 *comp_pixel;
};
template <class Derived>
void
init (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area) const
{
state->dest_buffer_iterator = gegl_buffer_iterator_add (
iter, params->dest_buffer, area, 0, format,
GEGL_ACCESS_READWRITE, GEGL_ABYSS_NONE);
/* initialize the base class *after* initializing the iterator, to make
* sure that dest_buffer is the primary buffer of the iterator, if no
* subclass added an iterator first.
*/
Base::init (params, state, iter, roi, area);
}
template <class Derived>
void
init_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect) const
{
Base::init_step (params, state, iter, roi, area, rect);
state->dest_pixel =
(guint8 *) iter->items[state->dest_buffer_iterator].data;
if (comp_fish)
{
state->comp_pixel = (guint8 *) gegl_scratch_alloc (
rect->width * babl_format_get_bytes_per_pixel (format));
}
}
template <class Derived>
void
process_row (const GimpPaintCoreLoopsParams *params,
State<Derived> *state,
GeglBufferIterator *iter,
const GeglRectangle *roi,
const GeglRectangle *area,
const GeglRectangle *rect,
gint y) const
{
Base::process_row (params, state, iter, roi, area, rect, y);
gpointer comp_pixel;
if (comp_fish)
{
babl_process (comp_fish,
state->comp_buffer_data, state->comp_pixel,
rect->width);
comp_pixel = state->comp_pixel;
}
else
{
comp_pixel = state->comp_buffer_data;
}
gimp_operation_mask_components_process (format,
state->dest_pixel, comp_pixel,
state->dest_pixel,
rect->width, params->affect);
state->dest_pixel += rect->width * babl_format_get_bytes_per_pixel (format);
}
template <class Derived>
void
finalize_step (const GimpPaintCoreLoopsParams *params,
State<Derived> *state) const
{
if (comp_fish)
gegl_scratch_free (state->comp_pixel);
Base::finalize_step (params, state);
}
};
static AlgorithmDispatch<
MaskComponents,
GIMP_PAINT_CORE_LOOPS_ALGORITHM_MASK_COMPONENTS,
decltype (dispatch_temp_comp_buffer)
>
dispatch_mask_components;
/* gimp_paint_core_loops_process():
*
* Performs the set of algorithms requested in 'algorithms', specified as a
* bitwise-OR of 'GimpPaintCoreLoopsAlgorithm' values, given the set of
* parameters 'params'.
*
* Note that the order in which the algorithms are performed is currently
* fixed, and follows their order of appearance in the
* 'GimpPaintCoreLoopsAlgorithm' enum.
*/
void
gimp_paint_core_loops_process (const GimpPaintCoreLoopsParams *params,
GimpPaintCoreLoopsAlgorithm algorithms)
{
GeglRectangle roi;
if (params->paint_buf)
{
roi.x = params->paint_buf_offset_x;
roi.y = params->paint_buf_offset_y;
roi.width = gimp_temp_buf_get_width (params->paint_buf);
roi.height = gimp_temp_buf_get_height (params->paint_buf);
}
else
{
roi.x = params->paint_buf_offset_x;
roi.y = params->paint_buf_offset_y;
roi.width = gimp_temp_buf_get_width (params->paint_mask) -
params->paint_mask_offset_x;
roi.height = gimp_temp_buf_get_height (params->paint_mask) -
params->paint_mask_offset_y;
}
dispatch (
[&] (auto algorithm_type)
{
using Algorithm = typename decltype (algorithm_type)::type;
using State = typename Algorithm::template State<Algorithm>;
Algorithm algorithm (params);
gegl_parallel_distribute_area (
&roi, PIXELS_PER_THREAD,
[=] (const GeglRectangle *area)
{
State state;
gint y;
if (Algorithm::max_n_iterators > 0)
{
GeglBufferIterator *iter;
iter = gegl_buffer_iterator_empty_new (
Algorithm::max_n_iterators);
algorithm.init (params, &state, iter, &roi, area);
while (gegl_buffer_iterator_next (iter))
{
const GeglRectangle *rect = &iter->items[0].roi;
algorithm.init_step (params, &state, iter, &roi, area, rect);
for (y = 0; y < rect->height; y++)
{
algorithm.process_row (params, &state,
iter, &roi, area, rect,
rect->y + y);
}
algorithm.finalize_step (params, &state);
}
algorithm.finalize (params, &state);
}
else
{
algorithm.init (params, &state, NULL, &roi, area);
algorithm.init_step (params, &state, NULL, &roi, area, area);
for (y = 0; y < area->height; y++)
{
algorithm.process_row (params, &state,
NULL, &roi, area, area,
area->y + y);
}
algorithm.finalize_step (params, &state);
algorithm.finalize (params, &state);
}
});
},
params, algorithms, identity<AlgorithmBase> (),
dispatch_combine_paint_mask_to_canvas_buffer_to_paint_buf_alpha,
dispatch_combine_paint_mask_to_canvas_buffer,
dispatch_canvas_buffer_to_paint_buf_alpha,
dispatch_paint_mask_to_paint_buf_alpha,
dispatch_canvas_buffer_to_comp_mask,
dispatch_paint_mask_to_comp_mask,
dispatch_do_layer_blend,
dispatch_mask_components);
}
|