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
|
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "appenv.h"
#include "asupsample.h"
#include "blend.h"
#include "brush_select.h"
#include "buildmenu.h"
#include "draw_core.h"
#include "drawable.h"
#include "errors.h"
#include "fuzzy_select.h"
#include "gdisplay.h"
#include "gimage_mask.h"
#include "gradient.h"
#include "interface.h"
#include "paint_funcs.h"
#include "palette.h"
#include "selection.h"
#include "tools.h"
#include "undo.h"
/* target size */
#define TARGET_HEIGHT 15
#define TARGET_WIDTH 15
#define SQR(x) ((x) * (x))
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif /* M_PI */
/* the Blend structures */
typedef enum
{
Linear,
BiLinear,
Radial,
Square,
ConicalSymmetric,
ConicalAsymmetric,
ShapeburstAngular,
ShapeburstSpherical,
ShapeburstDimpled
} GradientType;
typedef enum
{
FG_BG_RGB_MODE,
FG_BG_HSV_MODE,
FG_TRANS_MODE,
CUSTOM_MODE
} BlendMode;
typedef enum
{
REPEAT_NONE,
REPEAT_SAWTOOTH,
REPEAT_TRIANGULAR
} RepeatMode;
typedef double (*RepeatFunc)(double);
typedef struct _BlendTool BlendTool;
struct _BlendTool
{
DrawCore * core; /* Core select object */
int startx; /* starting x coord */
int starty; /* starting y coord */
int endx; /* ending x coord */
int endy; /* ending y coord */
};
typedef struct _BlendOptions BlendOptions;
struct _BlendOptions
{
double opacity;
double offset;
BlendMode blend_mode;
int paint_mode;
GradientType gradient_type;
RepeatMode repeat;
GtkWidget *repeat_mode_menu;
int supersample;
GtkWidget *frame;
int max_depth;
double threshold;
};
typedef struct {
double offset;
double sx, sy;
BlendMode blend_mode;
GradientType gradient_type;
color_t fg, bg;
double dist;
double vec[2];
RepeatFunc repeat_func;
} RenderBlendData;
typedef struct {
PixelRegion *PR;
unsigned char *row_data;
int bytes;
int width;
} PutPixelData;
/* local function prototypes */
static void blend_scale_update (GtkAdjustment *, double *);
static void gradient_type_callback (GtkWidget *, gpointer);
static void blend_mode_callback (GtkWidget *, gpointer);
static void paint_mode_callback (GtkWidget *, gpointer);
static void repeat_type_callback (GtkWidget *, gpointer);
static void supersample_toggle_update (GtkWidget *, gpointer);
static void max_depth_scale_update (GtkAdjustment *, int *);
static void threshold_scale_update (GtkAdjustment *, double *);
static void blend_button_press (Tool *, GdkEventButton *, gpointer);
static void blend_button_release (Tool *, GdkEventButton *, gpointer);
static void blend_motion (Tool *, GdkEventMotion *, gpointer);
static void blend_cursor_update (Tool *, GdkEventMotion *, gpointer);
static void blend_control (Tool *, int, gpointer);
static void blend (GImage *gimage, GimpDrawable *drawable,
BlendMode blend_mode, int paint_mode,
GradientType gradient_type,
double opacity, double offset,
RepeatMode repeat,
int supersample, int max_depth, double threshold,
double startx, double starty,
double endx, double endy);
static double gradient_calc_conical_sym_factor (double dist, double *axis, double offset,
double x, double y);
static double gradient_calc_conical_asym_factor (double dist, double *axis, double offset,
double x, double y);
static double gradient_calc_square_factor (double dist, double offset,
double x, double y);
static double gradient_calc_radial_factor (double dist, double offset,
double x, double y);
static double gradient_calc_linear_factor (double dist, double *vec,
double x, double y);
static double gradient_calc_bilinear_factor (double dist, double *vec, double offset,
double x, double y);
static double gradient_calc_shapeburst_angular_factor (double x, double y);
static double gradient_calc_shapeburst_spherical_factor (double x, double y);
static double gradient_calc_shapeburst_dimpled_factor (double x, double y);
static double gradient_repeat_none(double val);
static double gradient_repeat_sawtooth(double val);
static double gradient_repeat_triangular(double val);
static void gradient_precalc_shapeburst (GImage *gimage, GimpDrawable *drawable, PixelRegion *PR, double dist);
static void gradient_render_pixel(double x, double y, color_t *color, void *render_data);
static void gradient_put_pixel(int x, int y, color_t color, void *put_pixel_data);
static void gradient_fill_region (GImage *gimage, GimpDrawable *drawable, PixelRegion *PR,
int width, int height,
BlendMode blend_mode, GradientType gradient_type,
double offset, RepeatMode repeat,
int supersample, int max_depth, double threshold,
double sx, double sy, double ex, double ey);
static void calc_rgb_to_hsv(double *r, double *g, double *b);
static void calc_hsv_to_rgb(double *h, double *s, double *v);
static BlendOptions *create_blend_options (void);
static Argument *blend_invoker (Argument *);
/* variables for the shapeburst algs */
static PixelRegion distR =
{
NULL, /* data */
NULL, /* tiles */
0, /* rowstride */
0, 0, /* w, h */
0, 0, /* x, y */
4, /* bytes */
0 /* process count */
};
/* the blend option menu items -- the blend modes */
static MenuItem blend_option_items[] =
{
{ "FG to BG (RGB)", 0, 0, blend_mode_callback, (gpointer) FG_BG_RGB_MODE, NULL, NULL },
{ "FG to BG (HSV)", 0, 0, blend_mode_callback, (gpointer) FG_BG_HSV_MODE, NULL, NULL },
{ "FG to Transparent", 0, 0, blend_mode_callback, (gpointer) FG_TRANS_MODE, NULL, NULL },
{ "Custom (from editor)", 0, 0, blend_mode_callback, (gpointer) CUSTOM_MODE, NULL, NULL },
{ NULL, 0, 0, NULL, NULL, NULL, NULL }
};
/* the gradient option menu items -- the gradient modes */
static MenuItem gradient_option_items[] =
{
{ "Linear", 0, 0, gradient_type_callback, (gpointer) Linear, NULL, NULL },
{ "Bi-Linear", 0, 0, gradient_type_callback, (gpointer) BiLinear, NULL, NULL },
{ "Radial", 0, 0, gradient_type_callback, (gpointer) Radial, NULL, NULL },
{ "Square", 0, 0, gradient_type_callback, (gpointer) Square, NULL, NULL },
{ "Conical (symmetric)", 0, 0, gradient_type_callback, (gpointer) ConicalSymmetric, NULL, NULL },
{ "Conical (asymmetric)", 0, 0, gradient_type_callback, (gpointer) ConicalAsymmetric, NULL, NULL },
{ "Shapeburst (angular)", 0, 0, gradient_type_callback, (gpointer) ShapeburstAngular, NULL, NULL },
{ "Shapeburst (spherical)", 0, 0, gradient_type_callback, (gpointer) ShapeburstSpherical, NULL, NULL },
{ "Shapeburst (dimpled)", 0, 0, gradient_type_callback, (gpointer) ShapeburstDimpled, NULL, NULL },
{ NULL, 0, 0, NULL, NULL, NULL, NULL }
};
/* blend options */
static BlendOptions *blend_options = NULL;
/* repeat menu items */
static MenuItem repeat_option_items[] =
{
{ "None", 0, 0, repeat_type_callback, (gpointer) REPEAT_NONE, NULL, NULL },
{ "Sawtooth wave", 0, 0, repeat_type_callback, (gpointer) REPEAT_SAWTOOTH, NULL, NULL },
{ "Triangular wave", 0, 0, repeat_type_callback, (gpointer) REPEAT_TRIANGULAR, NULL, NULL },
{ NULL, 0, 0, NULL, NULL, NULL, NULL }
};
static void
blend_scale_update (GtkAdjustment *adjustment,
double *scale_val)
{
*scale_val = adjustment->value;
}
static void
gradient_type_callback (GtkWidget *w,
gpointer client_data)
{
blend_options->gradient_type = (GradientType) client_data;
gtk_widget_set_sensitive (blend_options->repeat_mode_menu,
(blend_options->gradient_type < 6));
}
static void
blend_mode_callback (GtkWidget *w,
gpointer client_data)
{
blend_options->blend_mode = (BlendMode) client_data;
}
static void
paint_mode_callback (GtkWidget *w,
gpointer client_data)
{
blend_options->paint_mode = (long) client_data;
}
static void
repeat_type_callback(GtkWidget *widget,
gpointer client_data)
{
blend_options->repeat = (RepeatMode) client_data;
}
static void
supersample_toggle_update(GtkWidget *widget,
gpointer client_data)
{
if (GTK_TOGGLE_BUTTON(widget)->active)
{
blend_options->supersample = TRUE;
gtk_widget_set_sensitive(blend_options->frame, TRUE);
}
else
{
blend_options->supersample = FALSE;
gtk_widget_set_sensitive(blend_options->frame, FALSE);
}
}
static void
max_depth_scale_update(GtkAdjustment *adjustment,
int *scale_val)
{
*scale_val = (int) adjustment->value;
}
static void
threshold_scale_update(GtkAdjustment *adjustment,
double *scale_val)
{
*scale_val = adjustment->value;
}
static BlendOptions *
create_blend_options ()
{
BlendOptions *options;
GtkWidget *vbox;
GtkWidget *frame;
GtkWidget *label;
GtkWidget *bm_option_menu;
GtkWidget *bm_menu;
GtkWidget *pm_option_menu;
GtkWidget *pm_menu;
GtkWidget *gt_option_menu;
GtkWidget *gt_menu;
GtkWidget *rt_option_menu;
GtkWidget *rt_menu;
GtkWidget *opacity_scale;
GtkWidget *table;
GtkWidget *offset_scale;
GtkObject *opacity_scale_data;
GtkObject *offset_scale_data;
GtkWidget *button;
GtkObject *depth_scale_data;
GtkWidget *depth_scale;
GtkObject *threshold_scale_data;
GtkWidget *threshold_scale;
/* the new options structure */
options = (BlendOptions *) g_malloc (sizeof (BlendOptions));
options->opacity = 100.0;
options->offset = 0.0;
options->blend_mode = FG_BG_RGB_MODE;
options->paint_mode = NORMAL;
options->gradient_type = Linear;
options->repeat = REPEAT_NONE;
options->supersample = FALSE;
options->max_depth = 3;
options->threshold = 0.2;
/* the main vbox */
vbox = gtk_vbox_new (FALSE, 2);
/* the main label */
label = gtk_label_new ("Blend Options");
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
gtk_widget_show (label);
/* the table */
table = gtk_table_new (6, 2, FALSE);
gtk_container_border_width (GTK_CONTAINER (table), 2);
gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
/* the opacity scale */
label = gtk_label_new ("Opacity:");
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 1.0);
gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 2);
gtk_widget_show (label);
opacity_scale_data = gtk_adjustment_new (100.0, 0.0, 100.0, 1.0, 1.0, 0.0);
opacity_scale = gtk_hscale_new (GTK_ADJUSTMENT (opacity_scale_data));
gtk_table_attach (GTK_TABLE (table), opacity_scale, 1, 2, 0, 1,
GTK_EXPAND | GTK_SHRINK | GTK_FILL, GTK_SHRINK, 4, 2);
gtk_scale_set_value_pos (GTK_SCALE (opacity_scale), GTK_POS_TOP);
gtk_range_set_update_policy (GTK_RANGE (opacity_scale), GTK_UPDATE_DELAYED);
gtk_signal_connect (GTK_OBJECT (opacity_scale_data), "value_changed",
(GtkSignalFunc) blend_scale_update,
&options->opacity);
gtk_widget_show (opacity_scale);
/* the offset scale */
label = gtk_label_new ("Offset:");
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 1.0);
gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 2);
gtk_widget_show (label);
offset_scale_data = gtk_adjustment_new (0.0, 0.0, 100.0, 1.0, 1.0, 0.0);
offset_scale = gtk_hscale_new (GTK_ADJUSTMENT (offset_scale_data));
gtk_table_attach (GTK_TABLE (table), offset_scale, 1, 2, 1, 2,
GTK_EXPAND | GTK_SHRINK | GTK_FILL, GTK_SHRINK, 4, 2);
gtk_scale_set_value_pos (GTK_SCALE (offset_scale), GTK_POS_TOP);
gtk_range_set_update_policy (GTK_RANGE (offset_scale), GTK_UPDATE_DELAYED);
gtk_signal_connect (GTK_OBJECT (offset_scale_data), "value_changed",
(GtkSignalFunc) blend_scale_update,
&options->offset);
gtk_widget_show (offset_scale);
/* the paint mode menu */
label = gtk_label_new ("Mode:");
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
gtk_table_attach (GTK_TABLE (table), label, 0, 1, 2, 3,
GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 2);
pm_menu = create_paint_mode_menu (paint_mode_callback);
pm_option_menu = gtk_option_menu_new ();
gtk_table_attach (GTK_TABLE (table), pm_option_menu, 1, 2, 2, 3,
GTK_EXPAND | GTK_SHRINK | GTK_FILL, GTK_SHRINK, 4, 2);
gtk_widget_show (label);
gtk_widget_show (pm_option_menu);
/* the blend mode menu */
label = gtk_label_new ("Blend:");
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
gtk_table_attach (GTK_TABLE (table), label, 0, 1, 3, 4,
GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 2);
bm_menu = build_menu (blend_option_items, NULL);
bm_option_menu = gtk_option_menu_new ();
gtk_table_attach (GTK_TABLE (table), bm_option_menu, 1, 2, 3, 4,
GTK_EXPAND | GTK_SHRINK | GTK_FILL, GTK_SHRINK, 4, 2);
gtk_widget_show (label);
gtk_widget_show (bm_option_menu);
/* the gradient type menu */
label = gtk_label_new ("Gradient:");
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
gtk_table_attach (GTK_TABLE (table), label, 0, 1, 4, 5,
GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 2);
gt_menu = build_menu (gradient_option_items, NULL);
gt_option_menu = gtk_option_menu_new ();
gtk_table_attach (GTK_TABLE (table), gt_option_menu, 1, 2, 4, 5,
GTK_EXPAND | GTK_SHRINK | GTK_FILL, GTK_SHRINK, 4, 2);
gtk_widget_show (label);
gtk_widget_show (gt_option_menu);
/* the repeat option */
label = gtk_label_new("Repeat:");
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
gtk_table_attach(GTK_TABLE(table), label, 0, 1, 5, 6,
GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 2);
rt_menu = build_menu(repeat_option_items, NULL);
rt_option_menu = gtk_option_menu_new();
gtk_table_attach(GTK_TABLE(table), rt_option_menu, 1, 2, 5, 6,
GTK_EXPAND | GTK_SHRINK | GTK_FILL, GTK_SHRINK, 4, 2);
gtk_widget_show(label);
gtk_widget_show(rt_option_menu);
options->repeat_mode_menu = rt_option_menu;
/* show the whole table */
gtk_widget_show (table);
/* supersampling toggle */
button = gtk_check_button_new_with_label("Adaptive supersampling");
gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
gtk_signal_connect(GTK_OBJECT(button), "toggled",
(GtkSignalFunc) supersample_toggle_update,
NULL);
gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), FALSE);
gtk_widget_show(button);
/* frame for supersampling options */
frame = gtk_frame_new(NULL);
gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_ETCHED_IN);
gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);
/* table for supersamplign options */
table = gtk_table_new(2, 2, FALSE);
gtk_container_border_width(GTK_CONTAINER(table), 2);
gtk_container_add(GTK_CONTAINER(frame), table);
/* max depth scale */
label = gtk_label_new("Max depth:");
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 1.0);
gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 2);
gtk_widget_show(label);
depth_scale_data = gtk_adjustment_new(3.0, 1.0, 10.0, 1.0, 1.0, 1.0);
depth_scale = gtk_hscale_new(GTK_ADJUSTMENT(depth_scale_data));
gtk_scale_set_digits(GTK_SCALE(depth_scale), 0);
gtk_table_attach(GTK_TABLE(table), depth_scale, 1, 2, 0, 1,
GTK_EXPAND | GTK_SHRINK | GTK_FILL, GTK_SHRINK, 4, 2);
gtk_scale_set_value_pos(GTK_SCALE(depth_scale), GTK_POS_TOP);
gtk_signal_connect(GTK_OBJECT(depth_scale_data), "value_changed",
(GtkSignalFunc) max_depth_scale_update,
&options->max_depth);
gtk_widget_show(depth_scale);
/* threshold scale */
label = gtk_label_new("Threshold:");
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 1.0);
gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
GTK_SHRINK | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 2);
gtk_widget_show(label);
threshold_scale_data = gtk_adjustment_new(0.2, 0.0, 4.0, 0.01, 0.01, 0.0);
threshold_scale = gtk_hscale_new(GTK_ADJUSTMENT(threshold_scale_data));
gtk_scale_set_digits(GTK_SCALE(threshold_scale), 2);
gtk_table_attach(GTK_TABLE(table), threshold_scale, 1, 2, 1, 2,
GTK_EXPAND | GTK_SHRINK | GTK_FILL, GTK_SHRINK, 4, 2);
gtk_scale_set_value_pos(GTK_SCALE(threshold_scale), GTK_POS_TOP);
gtk_signal_connect(GTK_OBJECT(threshold_scale_data), "value_changed",
(GtkSignalFunc) threshold_scale_update,
&options->threshold);
gtk_widget_show(threshold_scale);
/* show table */
gtk_widget_show(table);
/* show frame */
gtk_widget_show(frame);
gtk_widget_set_sensitive(frame, FALSE);
options->frame = frame;
/* Register this selection options widget with the main tools options dialog */
tools_register_options (BLEND, vbox);
/* Post initialization */
gtk_option_menu_set_menu (GTK_OPTION_MENU (bm_option_menu), bm_menu);
gtk_option_menu_set_menu (GTK_OPTION_MENU (gt_option_menu), gt_menu);
gtk_option_menu_set_menu (GTK_OPTION_MENU (pm_option_menu), pm_menu);
gtk_option_menu_set_menu (GTK_OPTION_MENU (rt_option_menu), rt_menu);
return options;
}
static void
blend_button_press (Tool *tool,
GdkEventButton *bevent,
gpointer gdisp_ptr)
{
GDisplay * gdisp;
BlendTool * blend_tool;
gdisp = (GDisplay *) gdisp_ptr;
blend_tool = (BlendTool *) tool->private;
switch (drawable_type (gimage_active_drawable (gdisp->gimage)))
{
case INDEXED_GIMAGE: case INDEXEDA_GIMAGE:
g_message ("Blend: Invalid for indexed images.");
return;
break;
default:
break;
}
/* Keep the coordinates of the target */
gdisplay_untransform_coords (gdisp, bevent->x, bevent->y,
&blend_tool->startx, &blend_tool->starty, FALSE, 1);
blend_tool->endx = blend_tool->startx;
blend_tool->endy = blend_tool->starty;
/* Make the tool active and set the gdisplay which owns it */
gdk_pointer_grab (gdisp->canvas->window, FALSE,
GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
NULL, NULL, bevent->time);
tool->gdisp_ptr = gdisp_ptr;
tool->state = ACTIVE;
/* Start drawing the blend tool */
draw_core_start (blend_tool->core, gdisp->canvas->window, tool);
}
static void
blend_button_release (Tool *tool,
GdkEventButton *bevent,
gpointer gdisp_ptr)
{
GImage * gimage;
BlendTool * blend_tool;
Argument *return_vals;
int nreturn_vals;
gimage = ((GDisplay *) gdisp_ptr)->gimage;
blend_tool = (BlendTool *) tool->private;
gdk_pointer_ungrab (bevent->time);
gdk_flush ();
draw_core_stop (blend_tool->core, tool);
tool->state = INACTIVE;
/* if the 3rd button isn't pressed, fill the selected region */
if (! (bevent->state & GDK_BUTTON3_MASK) &&
((blend_tool->startx != blend_tool->endx) ||
(blend_tool->starty != blend_tool->endy)))
{
return_vals = procedural_db_run_proc ("gimp_blend",
&nreturn_vals,
PDB_IMAGE, gimage->ID,
PDB_DRAWABLE, drawable_ID (gimage_active_drawable (gimage)),
PDB_INT32, (gint32) blend_options->blend_mode,
PDB_INT32, (gint32) blend_options->paint_mode,
PDB_INT32, (gint32) blend_options->gradient_type,
PDB_FLOAT, (gdouble) blend_options->opacity,
PDB_FLOAT, (gdouble) blend_options->offset,
PDB_INT32, (gint32) blend_options->repeat,
PDB_INT32, (gint32) blend_options->supersample,
PDB_INT32, (gint32) blend_options->max_depth,
PDB_FLOAT, (gdouble) blend_options->threshold,
PDB_FLOAT, (gdouble) blend_tool->startx,
PDB_FLOAT, (gdouble) blend_tool->starty,
PDB_FLOAT, (gdouble) blend_tool->endx,
PDB_FLOAT, (gdouble) blend_tool->endy,
PDB_END);
if (return_vals[0].value.pdb_int == PDB_SUCCESS)
gdisplays_flush ();
else
g_message ("Blend operation failed.");
procedural_db_destroy_args (return_vals, nreturn_vals);
}
}
static void
blend_motion (Tool *tool,
GdkEventMotion *mevent,
gpointer gdisp_ptr)
{
GDisplay * gdisp;
BlendTool * blend_tool;
gdisp = (GDisplay *) gdisp_ptr;
blend_tool = (BlendTool *) tool->private;
/* undraw the current tool */
draw_core_pause (blend_tool->core, tool);
/* Get the current coordinates */
gdisplay_untransform_coords (gdisp, mevent->x, mevent->y,
&blend_tool->endx, &blend_tool->endy, FALSE, 1);
/* redraw the current tool */
draw_core_resume (blend_tool->core, tool);
}
static void
blend_cursor_update (Tool *tool,
GdkEventMotion *mevent,
gpointer gdisp_ptr)
{
GDisplay * gdisp;
gdisp = (GDisplay *) gdisp_ptr;
switch (drawable_type (gimage_active_drawable (gdisp->gimage)))
{
case INDEXED_GIMAGE: case INDEXEDA_GIMAGE:
gdisplay_install_tool_cursor (gdisp, GDK_TOP_LEFT_ARROW);
break;
default:
gdisplay_install_tool_cursor (gdisp, GDK_TCROSS);
break;
}
}
static void
blend_draw (Tool *tool)
{
GDisplay * gdisp;
BlendTool * blend_tool;
int tx1, ty1, tx2, ty2;
gdisp = (GDisplay *) tool->gdisp_ptr;
blend_tool = (BlendTool *) tool->private;
gdisplay_transform_coords (gdisp, blend_tool->startx, blend_tool->starty,
&tx1, &ty1, 1);
gdisplay_transform_coords (gdisp, blend_tool->endx, blend_tool->endy,
&tx2, &ty2, 1);
/* Draw start target */
gdk_draw_line (blend_tool->core->win, blend_tool->core->gc,
tx1 - (TARGET_WIDTH >> 1), ty1,
tx1 + (TARGET_WIDTH >> 1), ty1);
gdk_draw_line (blend_tool->core->win, blend_tool->core->gc,
tx1, ty1 - (TARGET_HEIGHT >> 1),
tx1, ty1 + (TARGET_HEIGHT >> 1));
/* Draw end target */
gdk_draw_line (blend_tool->core->win, blend_tool->core->gc,
tx2 - (TARGET_WIDTH >> 1), ty2,
tx2 + (TARGET_WIDTH >> 1), ty2);
gdk_draw_line (blend_tool->core->win, blend_tool->core->gc,
tx2, ty2 - (TARGET_HEIGHT >> 1),
tx2, ty2 + (TARGET_HEIGHT >> 1));
/* Draw the line between the start and end coords */
gdk_draw_line (blend_tool->core->win, blend_tool->core->gc,
tx1, ty1, tx2, ty2);
}
static void
blend_control (Tool *tool,
int action,
gpointer gdisp_ptr)
{
BlendTool * blend_tool;
blend_tool = (BlendTool *) tool->private;
switch (action)
{
case PAUSE :
draw_core_pause (blend_tool->core, tool);
break;
case RESUME :
draw_core_resume (blend_tool->core, tool);
break;
case HALT :
draw_core_stop (blend_tool->core, tool);
break;
}
}
/*****/
/* The actual blending procedure */
static void
blend (GImage *gimage,
GimpDrawable *drawable,
BlendMode blend_mode,
int paint_mode,
GradientType gradient_type,
double opacity,
double offset,
RepeatMode repeat,
int supersample,
int max_depth,
double threshold,
double startx,
double starty,
double endx,
double endy)
{
TileManager *buf_tiles;
PixelRegion bufPR;
int has_alpha;
int has_selection;
int bytes;
int x1, y1, x2, y2;
has_selection = drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
has_alpha = drawable_has_alpha (drawable);
bytes = drawable_bytes (drawable);
/* Always create an alpha temp buf (for generality) */
if (! has_alpha)
{
has_alpha = TRUE;
bytes += 1;
}
buf_tiles = tile_manager_new ((x2 - x1), (y2 - y1), bytes);
pixel_region_init (&bufPR, buf_tiles, 0, 0, (x2 - x1), (y2 - y1), TRUE);
gradient_fill_region (gimage, drawable,
&bufPR, (x2 - x1), (y2 - y1),
blend_mode, gradient_type, offset, repeat,
supersample, max_depth, threshold,
(startx - x1), (starty - y1),
(endx - x1), (endy - y1));
pixel_region_init (&bufPR, buf_tiles, 0, 0, (x2 - x1), (y2 - y1), FALSE);
gimage_apply_image (gimage, drawable, &bufPR, TRUE,
(opacity * 255) / 100, paint_mode, NULL, x1, y1);
/* update the image */
drawable_update (drawable, x1, y1, (x2 - x1), (y2 - y1));
/* free the temporary buffer */
tile_manager_destroy (buf_tiles);
}
static double
gradient_calc_conical_sym_factor (double dist,
double *axis,
double offset,
double x,
double y)
{
double vec[2];
double r;
double rat;
if (dist == 0.0)
rat = 0.0;
else
if ((x != 0) || (y != 0))
{
/* Calculate offset from the start in pixels */
r = sqrt(x * x + y * y);
vec[0] = x / r;
vec[1] = y / r;
rat = axis[0] * vec[0] + axis[1] * vec[1]; /* Dot product */
if (rat > 1.0)
rat = 1.0;
else if (rat < -1.0)
rat = -1.0;
/* This cool idea is courtesy Josh MacDonald,
* Ali Rahimi --- two more XCF losers. */
rat = acos(rat) / M_PI;
rat = pow(rat, (offset / 10) + 1);
rat = BOUNDS(rat, 0.0, 1.0);
}
else
rat = 0.5;
return rat;
} /* gradient_calc_conical_sym_factor */
/*****/
static double
gradient_calc_conical_asym_factor (double dist,
double *axis,
double offset,
double x,
double y)
{
double ang0, ang1;
double ang;
double rat;
if (dist == 0.0)
rat = 0.0;
else
{
if ((x != 0) || (y != 0))
{
ang0 = atan2(axis[0], axis[1]) + M_PI;
ang1 = atan2(x, y) + M_PI;
ang = ang1 - ang0;
if (ang < 0.0)
ang += (2.0 * M_PI);
rat = ang / (2.0 * M_PI);
rat = pow(rat, (offset / 10) + 1);
rat = BOUNDS(rat, 0.0, 1.0);
}
else
rat = 0.5; /* We are on middle point */
} /* else */
return rat;
} /* gradient_calc_conical_asym_factor */
/*****/
static double
gradient_calc_square_factor (double dist,
double offset,
double x,
double y)
{
double r;
double rat;
if (dist == 0.0)
rat = 0.0;
else
{
/* Calculate offset from start as a value in [0, 1] */
offset = offset / 100.0;
r = MAXIMUM(abs(x), abs(y));
rat = r / dist;
if (rat < offset)
rat = 0.0;
else if (offset == 1)
rat = (rat>=1) ? 1 : 0;
else
rat = (rat - offset) / (1.0 - offset);
} /* else */
return rat;
} /* gradient_calc_square_factor */
/*****/
static double
gradient_calc_radial_factor (double dist,
double offset,
double x,
double y)
{
double r;
double rat;
if (dist == 0.0)
rat = 0.0;
else
{
/* Calculate radial offset from start as a value in [0, 1] */
offset = offset / 100.0;
r = sqrt(SQR(x) + SQR(y));
rat = r / dist;
if (rat < offset)
rat = 0.0;
else if (offset == 1)
rat = (rat>=1) ? 1 : 0;
else
rat = (rat - offset) / (1.0 - offset);
} /* else */
return rat;
} /* gradient_calc_radial_factor */
/*****/
static double
gradient_calc_linear_factor (double dist,
double *vec,
double x,
double y)
{
double r;
double rat;
if (dist == 0.0)
rat = 0.0;
else
{
r = vec[0] * x + vec[1] * y;
rat = r / dist;
} /* else */
return rat;
} /* gradient_calc_linear_factor */
/*****/
static double
gradient_calc_bilinear_factor (double dist,
double *vec,
double offset,
double x,
double y)
{
double r;
double rat;
if (dist == 0.0)
rat = 0.0;
else
{
/* Calculate linear offset from the start line outward */
offset = offset / 100.0;
r = vec[0] * x + vec[1] * y;
rat = r / dist;
if (fabs(rat) < offset)
rat = 0.0;
else if (offset == 1)
rat = (rat>=1) ? 1 : 0;
else
rat = (fabs(rat) - offset) / (1.0 - offset);
} /* else */
return rat;
} /* gradient_calc_bilinear_factor */
static double
gradient_calc_shapeburst_angular_factor (double x,
double y)
{
int ix, iy;
Tile *tile;
float value;
ix = (int) BOUNDS (x, 0, distR.w);
iy = (int) BOUNDS (y, 0, distR.h);
tile = tile_manager_get_tile (distR.tiles, ix, iy, 0);
tile_ref (tile);
value = 1.0 - *(((float *) tile->data) + ((iy % TILE_HEIGHT) * tile->ewidth + (ix % TILE_WIDTH)));
tile_unref (tile, FALSE);
return value;
}
static double
gradient_calc_shapeburst_spherical_factor (double x,
double y)
{
int ix, iy;
Tile *tile;
float value;
ix = (int) BOUNDS (x, 0, distR.w);
iy = (int) BOUNDS (y, 0, distR.h);
tile = tile_manager_get_tile (distR.tiles, ix, iy, 0);
tile_ref (tile);
value = *(((float *) tile->data) + ((iy % TILE_HEIGHT) * tile->ewidth + (ix % TILE_WIDTH)));
value = 1.0 - sin (0.5 * M_PI * value);
tile_unref (tile, FALSE);
return value;
}
static double
gradient_calc_shapeburst_dimpled_factor (double x,
double y)
{
int ix, iy;
Tile *tile;
float value;
ix = (int) BOUNDS (x, 0, distR.w);
iy = (int) BOUNDS (y, 0, distR.h);
tile = tile_manager_get_tile (distR.tiles, ix, iy, 0);
tile_ref (tile);
value = *(((float *) tile->data) + ((iy % TILE_HEIGHT) * tile->ewidth + (ix % TILE_WIDTH)));
value = cos (0.5 * M_PI * value);
tile_unref (tile, FALSE);
return value;
}
static double
gradient_repeat_none(double val)
{
return BOUNDS(val, 0.0, 1.0);
}
static double
gradient_repeat_sawtooth(double val)
{
if (val >= 0.0)
return fmod(val, 1.0);
else
return 1.0 - fmod(-val, 1.0);
}
static double
gradient_repeat_triangular(double val)
{
int ival;
if (val < 0.0)
val = -val;
ival = (int) val;
if (ival & 1)
return 1.0 - fmod(val, 1.0);
else
return fmod(val, 1.0);
}
/*****/
static void
gradient_precalc_shapeburst (GImage *gimage,
GimpDrawable *drawable,
PixelRegion *PR,
double dist)
{
Channel *mask;
PixelRegion tempR;
float max_iteration;
float *distp;
int size;
void * pr;
unsigned char white[1] = { OPAQUE_OPACITY };
/* allocate the distance map */
if (distR.tiles)
tile_manager_destroy (distR.tiles);
distR.tiles = tile_manager_new (PR->w, PR->h, sizeof (float));
/* allocate the selection mask copy */
tempR.tiles = tile_manager_new (PR->w, PR->h, 1);
pixel_region_init (&tempR, tempR.tiles, 0, 0, PR->w, PR->h, TRUE);
/* If the gimage mask is not empty, use it as the shape burst source */
if (! gimage_mask_is_empty (gimage))
{
PixelRegion maskR;
int x1, y1, x2, y2;
int offx, offy;
drawable_mask_bounds (drawable, &x1, &y1, &x2, &y2);
drawable_offsets (drawable, &offx, &offy);
/* the selection mask */
mask = gimage_get_mask (gimage);
pixel_region_init (&maskR, drawable_data (GIMP_DRAWABLE(mask)),
x1 + offx, y1 + offy, (x2 - x1), (y2 - y1), FALSE);
/* copy the mask to the temp mask */
copy_region (&maskR, &tempR);
}
/* otherwise... */
else
{
/* If the intended drawable has an alpha channel, use that */
if (drawable_has_alpha (drawable))
{
PixelRegion drawableR;
pixel_region_init (&drawableR, drawable_data (drawable), PR->x, PR->y, PR->w, PR->h, FALSE);
extract_alpha_region (&drawableR, NULL, &tempR);
}
/* Otherwise, just fill the shapeburst to white */
else
color_region (&tempR, white);
}
pixel_region_init (&tempR, tempR.tiles, 0, 0, PR->w, PR->h, TRUE);
pixel_region_init (&distR, distR.tiles, 0, 0, PR->w, PR->h, TRUE);
max_iteration = shapeburst_region (&tempR, &distR);
/* normalize the shapeburst with the max iteration */
if (max_iteration > 0)
{
pixel_region_init (&distR, distR.tiles, 0, 0, PR->w, PR->h, TRUE);
for (pr = pixel_regions_register (1, &distR); pr != NULL; pr = pixel_regions_process (pr))
{
distp = (float *) distR.data;
size = distR.w * distR.h;
while (size--)
*distp++ /= max_iteration;
}
pixel_region_init (&distR, distR.tiles, 0, 0, PR->w, PR->h, FALSE);
}
tile_manager_destroy (tempR.tiles);
}
static void
gradient_render_pixel(double x, double y, color_t *color, void *render_data)
{
RenderBlendData *rbd;
double factor;
rbd = render_data;
/* Calculate blending factor */
switch (rbd->gradient_type)
{
case Radial:
factor = gradient_calc_radial_factor(rbd->dist, rbd->offset,
x - rbd->sx, y - rbd->sy);
break;
case ConicalSymmetric:
factor = gradient_calc_conical_sym_factor(rbd->dist, rbd->vec, rbd->offset,
x - rbd->sx, y - rbd->sy);
break;
case ConicalAsymmetric:
factor = gradient_calc_conical_asym_factor(rbd->dist, rbd->vec, rbd->offset,
x - rbd->sx, y - rbd->sy);
break;
case Square:
factor = gradient_calc_square_factor(rbd->dist, rbd->offset,
x - rbd->sx, y - rbd->sy);
break;
case Linear:
factor = gradient_calc_linear_factor(rbd->dist, rbd->vec,
x - rbd->sx, y - rbd->sy);
break;
case BiLinear:
factor = gradient_calc_bilinear_factor(rbd->dist, rbd->vec, rbd->offset,
x - rbd->sx, y - rbd->sy);
break;
case ShapeburstAngular:
factor = gradient_calc_shapeburst_angular_factor(x, y);
break;
case ShapeburstSpherical:
factor = gradient_calc_shapeburst_spherical_factor(x, y);
break;
case ShapeburstDimpled:
factor = gradient_calc_shapeburst_dimpled_factor(x, y);
break;
default:
fatal_error("gradient_render_pixel(): unknown gradient type %d",
(int) rbd->gradient_type);
return;
}
/* Adjust for repeat */
factor = (*rbd->repeat_func)(factor);
/* Blend the colors */
if (rbd->blend_mode == CUSTOM_MODE)
grad_get_color_at(factor, &color->r, &color->g, &color->b, &color->a);
else
{
/* Blend values */
color->r = rbd->fg.r + (rbd->bg.r - rbd->fg.r) * factor;
color->g = rbd->fg.g + (rbd->bg.g - rbd->fg.g) * factor;
color->b = rbd->fg.b + (rbd->bg.b - rbd->fg.b) * factor;
color->a = rbd->fg.a + (rbd->bg.a - rbd->fg.a) * factor;
if (rbd->blend_mode == FG_BG_HSV_MODE)
calc_hsv_to_rgb(&color->r, &color->g, &color->b);
}
}
static void
gradient_put_pixel(int x, int y, color_t color, void *put_pixel_data)
{
PutPixelData *ppd;
unsigned char *data;
ppd = put_pixel_data;
/* Paint */
data = ppd->row_data + ppd->bytes * x;
if (ppd->bytes >= 3)
{
*data++ = color.r * 255.0;
*data++ = color.g * 255.0;
*data++ = color.b * 255.0;
*data++ = color.a * 255.0;
}
else
{
/* Convert to grayscale */
*data++ = 255.0 * (0.30 * color.r +
0.59 * color.g +
0.11 * color.b);
*data++ = color.a * 255.0;
}
/* Paint whole row if we are on the rightmost pixel */
if (x == (ppd->width - 1))
pixel_region_set_row(ppd->PR, 0, y, ppd->width, ppd->row_data);
}
static void
gradient_fill_region (GImage *gimage,
GimpDrawable *drawable,
PixelRegion *PR,
int width,
int height,
BlendMode blend_mode,
GradientType gradient_type,
double offset,
RepeatMode repeat,
int supersample,
int max_depth,
double threshold,
double sx,
double sy,
double ex,
double ey)
{
RenderBlendData rbd;
PutPixelData ppd;
unsigned char r, g, b;
int x, y;
int endx, endy;
void *pr;
unsigned char *data;
color_t color;
/* Get foreground and background colors, normalized */
palette_get_foreground(&r, &g, &b);
rbd.fg.r = r / 255.0;
rbd.fg.g = g / 255.0;
rbd.fg.b = b / 255.0;
rbd.fg.a = 1.0; /* Foreground is always opaque */
palette_get_background(&r, &g, &b);
rbd.bg.r = r / 255.0;
rbd.bg.g = g / 255.0;
rbd.bg.b = b / 255.0;
rbd.bg.a = 1.0; /* opaque, for now */
switch (blend_mode)
{
case FG_BG_RGB_MODE:
break;
case FG_BG_HSV_MODE:
/* Convert to HSV */
calc_rgb_to_hsv(&rbd.fg.r, &rbd.fg.g, &rbd.fg.b);
calc_rgb_to_hsv(&rbd.bg.r, &rbd.bg.g, &rbd.bg.b);
break;
case FG_TRANS_MODE:
/* Color does not change, just the opacity */
rbd.bg = rbd.fg;
rbd.bg.a = 0.0; /* transparent */
break;
case CUSTOM_MODE:
break;
default:
fatal_error("gradient_fill_region(): unknown blend mode %d",
(int) blend_mode);
break;
}
/* Calculate type-specific parameters */
switch (gradient_type)
{
case Radial:
rbd.dist = sqrt(SQR(ex - sx) + SQR(ey - sy));
break;
case Square:
rbd.dist = MAXIMUM(fabs(ex - sx), fabs(ey - sy));
break;
case ConicalSymmetric:
case ConicalAsymmetric:
case Linear:
case BiLinear:
rbd.dist = sqrt(SQR(ex - sx) + SQR(ey - sy));
if (rbd.dist > 0.0)
{
rbd.vec[0] = (ex - sx) / rbd.dist;
rbd.vec[1] = (ey - sy) / rbd.dist;
}
break;
case ShapeburstAngular:
case ShapeburstSpherical:
case ShapeburstDimpled:
rbd.dist = sqrt(SQR(ex - sx) + SQR(ey - sy));
gradient_precalc_shapeburst(gimage, drawable, PR, rbd.dist);
break;
default:
fatal_error("gradient_fill_region(): unknown gradient type %d",
(int) gradient_type);
break;
}
/* Set repeat function */
switch (repeat)
{
case REPEAT_NONE:
rbd.repeat_func = gradient_repeat_none;
break;
case REPEAT_SAWTOOTH:
rbd.repeat_func = gradient_repeat_sawtooth;
break;
case REPEAT_TRIANGULAR:
rbd.repeat_func = gradient_repeat_triangular;
break;
default:
fatal_error("gradient_fill_region(): unknown repeat mode %d",
(int) repeat);
break;
}
/* Initialize render data */
rbd.offset = offset;
rbd.sx = sx;
rbd.sy = sy;
rbd.blend_mode = blend_mode;
rbd.gradient_type = gradient_type;
/* Render the gradient! */
if (supersample)
{
/* Initialize put pixel data */
ppd.PR = PR;
ppd.row_data = g_malloc(width * PR->bytes);
ppd.bytes = PR->bytes;
ppd.width = width;
/* Render! */
adaptive_supersample_area(0, 0, (width - 1), (height - 1),
max_depth, threshold,
gradient_render_pixel, &rbd,
gradient_put_pixel, &ppd,
NULL, NULL);
/* Clean up */
g_free(ppd.row_data);
}
else
{
for (pr = pixel_regions_register(1, PR); pr != NULL; pr = pixel_regions_process(pr))
{
data = PR->data;
endx = PR->x + PR->w;
endy = PR->y + PR->h;
for (y = PR->y; y < endy; y++)
for (x = PR->x; x < endx; x++)
{
gradient_render_pixel(x, y, &color, &rbd);
if (PR->bytes >= 3)
{
*data++ = color.r * 255.0;
*data++ = color.g * 255.0;
*data++ = color.b * 255.0;
*data++ = color.a * 255.0;
}
else
{
/* Convert to grayscale */
*data++ = 255.0 * (0.30 * color.r +
0.59 * color.g +
0.11 * color.b);
*data++ = color.a * 255.0;
}
}
}
}
}
static void
calc_rgb_to_hsv(double *r, double *g, double *b)
{
double red, green, blue;
double h, s, v;
double min, max;
double delta;
red = *r;
green = *g;
blue = *b;
h = 0.0; /* Shut up -Wall */
if (red > green)
{
if (red > blue)
max = red;
else
max = blue;
if (green < blue)
min = green;
else
min = blue;
}
else
{
if (green > blue)
max = green;
else
max = blue;
if (red < blue)
min = red;
else
min = blue;
}
v = max;
if (max != 0.0)
s = (max - min) / max;
else
s = 0.0;
if (s == 0.0)
h = 0.0;
else
{
delta = max - min;
if (red == max)
h = (green - blue) / delta;
else if (green == max)
h = 2 + (blue - red) / delta;
else if (blue == max)
h = 4 + (red - green) / delta;
h /= 6.0;
if (h < 0.0)
h += 1.0;
else if (h > 1.0)
h -= 1.0;
}
*r = h;
*g = s;
*b = v;
}
static void
calc_hsv_to_rgb(double *h, double *s, double *v)
{
double hue, saturation, value;
double f, p, q, t;
if (*s == 0.0)
{
*h = *v;
*s = *v;
*v = *v; /* heh */
}
else
{
hue = *h * 6.0;
saturation = *s;
value = *v;
if (hue == 6.0)
hue = 0.0;
f = hue - (int) hue;
p = value * (1.0 - saturation);
q = value * (1.0 - saturation * f);
t = value * (1.0 - saturation * (1.0 - f));
switch ((int) hue)
{
case 0:
*h = value;
*s = t;
*v = p;
break;
case 1:
*h = q;
*s = value;
*v = p;
break;
case 2:
*h = p;
*s = value;
*v = t;
break;
case 3:
*h = p;
*s = q;
*v = value;
break;
case 4:
*h = t;
*s = p;
*v = value;
break;
case 5:
*h = value;
*s = p;
*v = q;
break;
}
}
}
/****************************/
/* Global blend functions */
/****************************/
Tool *
tools_new_blend ()
{
Tool * tool;
BlendTool * private;
if (! blend_options)
blend_options = create_blend_options ();
tool = (Tool *) g_malloc (sizeof (Tool));
private = (BlendTool *) g_malloc (sizeof (BlendTool));
private->core = draw_core_new (blend_draw);
tool->type = BLEND;
tool->state = INACTIVE;
tool->scroll_lock = 1; /* Disallow scrolling */
tool->auto_snap_to = TRUE;
tool->private = private;
tool->button_press_func = blend_button_press;
tool->button_release_func = blend_button_release;
tool->motion_func = blend_motion;
tool->arrow_keys_func = standard_arrow_keys_func;
tool->cursor_update_func = blend_cursor_update;
tool->control_func = blend_control;
tool->preserve = TRUE;
return tool;
}
void
tools_free_blend (Tool *tool)
{
BlendTool * blend_tool;
blend_tool = (BlendTool *) tool->private;
if (tool->state == ACTIVE)
draw_core_stop (blend_tool->core, tool);
draw_core_free (blend_tool->core);
/* free the distance map data if it exists */
if (distR.tiles)
tile_manager_destroy (distR.tiles);
distR.tiles = NULL;
g_free (blend_tool);
}
/* The blend procedure definition */
ProcArg blend_args[] =
{
{ PDB_IMAGE,
"image",
"The image"
},
{ PDB_DRAWABLE,
"drawable",
"The affected drawable"
},
{ PDB_INT32,
"blend_mode",
"The type of blend: { FG-BG-RGB (0), FG-BG-HSV (1), FG-TRANS (2), CUSTOM (3) }"
},
{ PDB_INT32,
"paint_mode",
"the paint application mode: { NORMAL (0), DISSOLVE (1), BEHIND (2), MULTIPLY (3), SCREEN (4), OVERLAY (5) DIFFERENCE (6), ADDITION (7), SUBTRACT (8), DARKEN-ONLY (9), LIGHTEN-ONLY (10), HUE (11), SATURATION (12), COLOR (13), VALUE (14) }"
},
{ PDB_INT32,
"gradient_type",
"The type of gradient: { LINEAR (0), BILINEAR (1), RADIAL (2), SQUARE (3), CONICAL-SYMMETRIC (4), CONICAL-ASYMMETRIC (5), SHAPEBURST-ANGULAR (6), SHAPEBURST-SPHERICAL (7), SHAPEBURST-DIMPLED (8) }"
},
{ PDB_FLOAT,
"opacity",
"The opacity of the final blend (0 <= opacity <= 100)"
},
{ PDB_FLOAT,
"offset",
"Offset relates to the starting and ending coordinates specified for the blend. This parameter is mode depndent (0 <= offset)"
},
{ PDB_INT32,
"repeat",
"Repeat mode: { REPEAT-NONE (0), REPEAT-SAWTOOTH (1), REPEAT-TRIANGULAR (2) }"
},
{ PDB_INT32,
"supersample",
"Do adaptive supersampling (true / false)"
},
{ PDB_INT32,
"max_depth",
"Maximum recursion levels for supersampling"
},
{ PDB_FLOAT,
"threshold",
"Supersampling threshold"
},
{ PDB_FLOAT,
"x1",
"The x coordinate of this blend's starting point"
},
{ PDB_FLOAT,
"y1",
"The y coordinate of this blend's starting point"
},
{ PDB_FLOAT,
"x2",
"The x coordinate of this blend's ending point"
},
{ PDB_FLOAT,
"y2",
"The y coordinate of this blend's ending point"
}
};
ProcRecord blend_proc =
{
"gimp_blend",
"Blend between the starting and ending coordinates with the specified blend mode and gradient type.",
"This tool requires information on the paint application mode, the blend mode, and the gradient type. It creates the specified variety of blend using the starting and ending coordinates as defined for each gradient type.",
"Spencer Kimball & Peter Mattis & Federico Mena Quintero",
"Spencer Kimball & Peter Mattis & Federico Mena Quintero",
"1995-1996",
PDB_INTERNAL,
/* Input arguments */
15,
blend_args,
/* Output arguments */
0,
NULL,
/* Exec method */
{ { blend_invoker } },
};
static Argument *
blend_invoker (Argument *args)
{
int success = TRUE;
GImage *gimage;
GimpDrawable *drawable;
BlendMode blend_mode;
int paint_mode;
GradientType gradient_type;
double opacity;
double offset;
RepeatMode repeat;
int supersample;
int max_depth;
double threshold;
double x1, y1;
double x2, y2;
int int_value;
double fp_value;
drawable = NULL;
blend_mode = FG_BG_RGB_MODE;
paint_mode = NORMAL_MODE;
gradient_type = Linear;
opacity = 100.0;
offset = 0.0;
repeat = REPEAT_NONE;
supersample = FALSE;
max_depth = 0;
threshold = 0.0;
/* the gimage */
if (success)
{
int_value = args[0].value.pdb_int;
if (! (gimage = gimage_get_ID (int_value)))
success = FALSE;
}
/* the drawable */
if (success)
{
int_value = args[1].value.pdb_int;
drawable = drawable_get_ID (int_value);
if (drawable == NULL || gimage != drawable_gimage (drawable))
success = FALSE;
}
/* blend mode */
if (success)
{
int_value = args[2].value.pdb_int;
switch (int_value)
{
case 0: blend_mode = FG_BG_RGB_MODE; break;
case 1: blend_mode = FG_BG_HSV_MODE; break;
case 2: blend_mode = FG_TRANS_MODE; break;
case 3: blend_mode = CUSTOM_MODE; break;
default: success = FALSE;
}
}
/* paint mode */
if (success)
{
int_value = args[3].value.pdb_int;
if (int_value >= NORMAL_MODE && int_value <= VALUE_MODE)
paint_mode = int_value;
else
success = FALSE;
}
/* gradient type */
if (success)
{
int_value = args[4].value.pdb_int;
switch (int_value)
{
case 0: gradient_type = Linear; break;
case 1: gradient_type = BiLinear; break;
case 2: gradient_type = Radial; break;
case 3: gradient_type = Square; break;
case 4: gradient_type = ConicalSymmetric; break;
case 5: gradient_type = ConicalAsymmetric; break;
case 6: gradient_type = ShapeburstAngular; break;
case 7: gradient_type = ShapeburstSpherical; break;
case 8: gradient_type = ShapeburstDimpled; break;
default: success = FALSE;
}
}
/* opacity */
if (success)
{
fp_value = args[5].value.pdb_float;
if (fp_value >= 0.0 && fp_value <= 100.0)
opacity = fp_value;
else
success = FALSE;
}
/* offset */
if (success)
{
fp_value = args[6].value.pdb_float;
if (fp_value >= 0.0)
offset = fp_value;
else
success = FALSE;
}
/* repeat */
if (success)
{
int_value = args[7].value.pdb_int;
switch (int_value)
{
case 0: repeat = REPEAT_NONE; break;
case 1: repeat = REPEAT_SAWTOOTH; break;
case 2: repeat = REPEAT_TRIANGULAR; break;
default: success = FALSE;
}
}
/* supersampling */
if (success)
{
int_value = args[8].value.pdb_int;
supersample = (int_value ? TRUE : FALSE);
}
/* max_depth */
if (success)
{
int_value = args[9].value.pdb_int;
if (((int_value >= 1) && (int_value <= 9)) || !supersample)
max_depth = int_value;
else
success = FALSE;
}
/* threshold */
if (success)
{
fp_value = args[10].value.pdb_float;
if (((fp_value >= 0.0) && (fp_value <= 4.0)) || !supersample)
threshold = fp_value;
else
success = FALSE;
}
/* x1, y1, x2, y2 */
if (success)
{
x1 = args[11].value.pdb_float;
y1 = args[12].value.pdb_float;
x2 = args[13].value.pdb_float;
y2 = args[14].value.pdb_float;
}
/* call the blend procedure */
if (success)
{
blend (gimage, drawable, blend_mode, paint_mode, gradient_type,
opacity, offset, repeat, supersample, max_depth, threshold, x1, y1, x2, y2);
}
return procedural_db_return_args (&blend_proc, success);
}
|