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
|
/** @file
* @brief image handling functions using gdk-pixbuf
* $Id: x_imagelib_gdk.c,v 1.89 2005/03/10 04:00:56 h_minami Exp $
*/
#include <math.h> /* pow */
#include <X11/Xatom.h> /* XInternAtom */
#include <X11/Xutil.h>
#include <string.h> /* memcpy */
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <kiklib/kik_types.h> /* u_int32_t/u_int16_t, HAVE_STDINT_H */
#include <kiklib/kik_unistd.h>
#include <kiklib/kik_str.h> /* strdup */
#include "x_imagelib.h"
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifndef SIZE_MAX
#ifdef SIZE_T_MAX
#define SIZE_MAX SIZE_T_MAX
#else
#define SIZE_MAX ((size_t)-1)
#endif
#endif
#define USE_FS 1
#define SUCCESS 0
/** Pixmap cache per display. */
typedef struct pixmap_cache_tag
{
Display * display; /**<Display */
Pixmap root; /**<Root pixmap !!! NOT owned by mlterm. DON'T FREE !!!*/
Pixmap cooked; /**<Background pixmap cache */
x_picture_modifier_t * pic_mod; /**<modification applied to "cooked" */
struct pixmap_cache_tag * next ;
} pixmap_cache_t ;
/* --- static variables --- */
static pixmap_cache_t * pixmap_cache = NULL ;
static int display_count = 0 ;
static unsigned char value_table[256] ;
/* --- static functions --- */
/* returned cmap shuold be freed by the caller */
static int
fetch_colormap(
Display * display,
int screen,
XColor ** color_list
)
{
int num_cells, i ;
Colormap cmap = DefaultColormap( display, screen) ;
num_cells = DisplayCells( display , screen) ;
*color_list = calloc( sizeof(XColor), num_cells) ;
if( !*color_list)
{
#ifdef DEBUG
kik_warn_printf(KIK_DEBUG_TAG "couldn't allocate color table\n") ;
#endif
return 0 ;
}
for( i = 0 ; i < num_cells ; i ++)
((*color_list)[i]).pixel = i ;
XQueryColors( display , cmap , *color_list, num_cells) ;
return num_cells ;
}
/* create GdkPixbuf from the specified file path.
*
* The returned pixbuf shouled be unrefed by the caller
* don't modify returned pixbuf since the pixbuf
* is stored in the cache and may be reused.
*/
static
GdkPixbuf *
load_file(
char * path,
unsigned int width,
unsigned int height,
GdkInterpType scale_type
)
{
static char * name = NULL ;
static GdkPixbuf * data = NULL ;
static GdkPixbuf * scaled = NULL ;
GdkPixbuf * pixbuf ;
pixbuf = NULL ;
if( !path)
{
/* free caches */
if( data)
{
#if GDK_PIXBUF_MAJOR >= 2
g_object_unref( data) ;
#else
gdk_pixbuf_unref( data) ;
#endif /*GDK_PIXBUF_MAJOR*/
data = NULL ;
}
if( scaled)
{
#if GDK_PIXBUF_MAJOR >= 2
g_object_unref( scaled) ;
#else
gdk_pixbuf_unref( scaled) ;
#endif /*GDK_PIXBUF_MAJOR*/
scaled = NULL ;
}
return NULL ;
}
/* cached one is still valid? */
if( name && (strcmp( name, path) == 0))
{
#ifdef DEBUG
kik_warn_printf(KIK_DEBUG_TAG "returning cached pixbuf\n") ;
#endif
pixbuf = data ;
}
else
{
/* create new pixbuf */
#ifdef DEBUG
kik_warn_printf(KIK_DEBUG_TAG "adding pixbuf to cache(%s)\n", path) ;
#endif
/* get rid of old cache data */
free( name) ;
name = strdup( path) ;
if( data)
{
#if GDK_PIXBUF_MAJOR >= 2
g_object_unref( data) ;
#else
gdk_pixbuf_unref( data) ;
#endif /*GDK_PIXBUF_MAJOR*/
}
if( scaled) /* scaled one is not vaild now */
{
#if GDK_PIXBUF_MAJOR >= 2
g_object_unref( scaled) ;
#else
gdk_pixbuf_unref( scaled) ;
#endif /*GDK_PIXBUF_MAJOR*/
}
scaled = NULL ;
#if GDK_PIXBUF_MAJOR >= 2
data = gdk_pixbuf_new_from_file( path, NULL) ;
#else
data = gdk_pixbuf_new_from_file( path) ;
#endif /*GDK_PIXBUF_MAJOR*/
pixbuf = data ;
}
/* loading from file/cache ends here */
if( !data)
return NULL ;
if( width == 0)
width = gdk_pixbuf_get_width( data) ;
if( height == 0)
height = gdk_pixbuf_get_height( data) ;
/* Old cached one became obsolete if width/height is changed */
if( ( width != gdk_pixbuf_get_width( data)) ||
( height != gdk_pixbuf_get_height( data)))
{
if( scaled && gdk_pixbuf_get_width( scaled) == width && gdk_pixbuf_get_height( scaled) == height)
{
#ifdef __DEBUG
kik_warn_printf(KIK_DEBUG_TAG "using the scaled pixbuf( %d x %d) from cache\n", width, height) ;
#endif
pixbuf = scaled ;
}
else
{
if( scaled)
{
#if GDK_PIXBUF_MAJOR >= 2
g_object_unref( scaled) ;
#else
gdk_pixbuf_unref( scaled) ;
#endif /*GDK_PIXBUF_MAJOR*/
}
#ifdef __DEBUG
kik_warn_printf(KIK_DEBUG_TAG "creating a scaled pixbuf(%d x %d) from %d %d \n", width, height) ;
#endif
scaled = gdk_pixbuf_scale_simple( data, width, height,
scale_type) ;
pixbuf = scaled ;
}
}
/* scaling ends here */
#if GDK_PIXBUF_MAJOR >= 2
g_object_ref( pixbuf) ;
#else
gdk_pixbuf_ref( pixbuf) ;
#endif /*GDK_PIXBUF_MAJOR*/
return pixbuf ;
}
/* create a pixbuf from an array of cardinals */
static GdkPixbuf *
create_pixbuf_from_cardinals(
u_int32_t * cardinal,
int req_width,
int req_height
)
{
GdkPixbuf * pixbuf ;
GdkPixbuf * scaled ;
int rowstride ;
unsigned char * line ;
unsigned char * pixel ;
int width,height ;
int i, j ;
width = cardinal[0] ;
height = cardinal[1] ;
pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB,
TRUE, 8, width, height) ;
if( !pixbuf)
return NULL ;
rowstride = gdk_pixbuf_get_rowstride( pixbuf) ;
line = gdk_pixbuf_get_pixels( pixbuf) ;
cardinal += 2 ;
for( i = 0 ; i < width ; i++)
{
pixel = line ;
for( j = 0 ; j < height ; j++)
{
/*ARGB -> RGBA conversion*/
pixel[2] = (*cardinal) & 0xFF ;
pixel[1] = ((*cardinal) & 0xFF00) >> 8 ;
pixel[0] = ((*cardinal) & 0xFF0000) >>16 ;
pixel[3] = (*cardinal) >> 24 ;
cardinal++ ;
pixel += 4;
}
line += rowstride ;
}
if( req_width == 0)
req_width = width ;
if( req_height == 0)
req_height = height ;
if( (req_width != width) || (req_height != height))
scaled = gdk_pixbuf_scale_simple(pixbuf, req_width, req_height,
GDK_INTERP_TILES) ;
else
scaled = NULL ;
if( scaled)
{
#if GDK_PIXBUF_MAJOR >= 2
g_object_unref( pixbuf) ;
#else
gdk_pixbuf_unref( pixbuf) ;
#endif /*GDK_PIXBUF_MAJOR*/
return scaled ;
}
else
{
return pixbuf ;
}
}
/* create an CARDINAL array for_NET_WM_ICON data */
static int
create_cardinals_from_bixbuf(
u_int32_t ** cardinal,
int width,
int height,
GdkPixbuf * pixbuf
)
{
int rowstride ;
unsigned char *line ;
unsigned char *pixel ;
int i, j ;
if( !width || !height)
return -1;
if( width > ((SIZE_MAX / 4) -2) / height)
return -1; /* integer overflow */
*cardinal = malloc( ((size_t)width * height + 2) *4) ;
if( !(*cardinal))
return -1 ;
/* create (maybe shriked) copy */
pixbuf = gdk_pixbuf_scale_simple(pixbuf, width, height, GDK_INTERP_TILES) ;
rowstride = gdk_pixbuf_get_rowstride( pixbuf) ;
line = gdk_pixbuf_get_pixels( pixbuf) ;
/* format of the array is {width, height, ARGB[][]} */
(*cardinal)[0] = width ;
(*cardinal)[1] = height ;
if( gdk_pixbuf_get_has_alpha( pixbuf)){
for( i = 0; i < height; i++) {
pixel = line ;
line += rowstride;
for( j = 0; j < width; j++) {
/* RGBA to ARGB */
(*cardinal)[(i*width+j)+2] = ((((((u_int32_t)(pixel[3]) << 8)
+ pixel[0]) << 8)
+ pixel[1]) << 8) + pixel[2] ;
pixel += 4 ;
}
}
}
else
{
for (i = 0; i < height; i++)
{
pixel = line ;
line += rowstride;
for (j = 0; j < width; j++) {
/* all pixels are completely opaque (0xFF) */
(*cardinal)[(i*width+j)+2] = ((((((u_int32_t)(0x0000FF) <<8)
+ pixel[0]) << 8)
+ pixel[1]) << 8) + pixel[2] ;
pixel += 3 ;
}
}
}
#if GDK_PIXBUF_MAJOR >= 2
g_object_unref( pixbuf) ;
#else
gdk_pixbuf_unref( pixbuf) ;
#endif /*GDK_PIXBUF_MAJOR*/
return SUCCESS ;
}
/* seek the closest color */
static int
closest_color_index(
Display * display ,
int screen ,
XColor * color_list,
int len,
int red,
int green,
int blue
)
{
int closest = 0 ;
int i ;
unsigned long min = 0xffffff ;
unsigned long diff ;
int diff_r, diff_g, diff_b ;
for( i = 0 ; i < len ; i++)
{
/* lazy color-space conversion*/
diff_r = red - (color_list[i].red >> 8) ;
diff_g = green - (color_list[i].green >> 8) ;
diff_b = blue - (color_list[i].blue >> 8) ;
diff = diff_r * diff_r *9 + diff_g * diff_g * 30 + diff_b * diff_b ;
if ( diff < min)
{
min = diff ;
closest = i ;
/* no one may notice the difference */
if ( diff < 31)
goto enough ;
}
}
enough:
return closest ;
}
static int
pixbuf_to_pixmap_pseudocolor(
Display * display ,
int screen,
GdkPixbuf * pixbuf,
Pixmap pixmap
)
{
unsigned int width, height, rowstride, bytes_per_pixel ;
int x, y ;
int num_cells;
#ifdef USE_FS
char * diff_next ;
char * diff_cur ;
char * temp;
#endif /* USE_SF */
unsigned char * line;
unsigned char * pixel;
XColor * color_list ;
int closest ;
GC gc;
XGCValues gcv ;
int diff_r, diff_g, diff_b ;
num_cells = fetch_colormap( display, screen, &color_list) ;
if( !num_cells)
return -8 ;
width = gdk_pixbuf_get_width (pixbuf) ;
height = gdk_pixbuf_get_height (pixbuf) ;
#ifdef USE_FS
diff_cur = calloc( width*3, 1) ;
if( !diff_cur)
{
return -1 ;
}
diff_next = calloc( width*3, 1) ;
if( !diff_next)
{
free( diff_cur) ;
return -2 ;
}
#endif /* USE_SF */
bytes_per_pixel = (gdk_pixbuf_get_has_alpha( pixbuf)) ? 4:3 ;
rowstride = gdk_pixbuf_get_rowstride (pixbuf) ;
line = gdk_pixbuf_get_pixels( pixbuf) ;
gc = XCreateGC (display, pixmap, 0, &gcv) ;
for ( y = 0 ; y < height ; y++)
{
pixel = line ;
#ifdef USE_FS
closest = closest_color_index( display, screen, color_list, num_cells,
pixel[0] - diff_cur[0],
pixel[1] - diff_cur[1],
pixel[2] - diff_cur[2]) ;
diff_r = (color_list[closest].red >>8) - pixel[0];
diff_g = (color_list[closest].green >>8) - pixel[1];
diff_b = (color_list[closest].blue >>8) - pixel[2];
diff_cur[3*1 + 0 ] += diff_r /2;
diff_cur[3*1 + 1 ] += diff_g /2;
diff_cur[3*1 + 2 ] += diff_b /2;
/* initialize next line */
diff_next[3*0 +0] = diff_r /4;
diff_next[3*0 +1] = diff_g /4;
diff_next[3*0 +2] = diff_b /4;
diff_next[3*1 +0] = diff_r /4;
diff_next[3*1 +1] = diff_g /4;
diff_next[3*1 +2] = diff_b /4;
#else
closest = closest_color_index( display, screen, color_list, num_cells,
pixel[0],
pixel[1],
pixel[2])
#endif /* USE_SF */
XSetForeground( display, gc, closest) ;
XDrawPoint( display, pixmap, gc, 0, y) ;
pixel += bytes_per_pixel ;
for ( x = 1 ; x < width -2 ; x++)
{
#ifdef USE_FS
closest = closest_color_index( display, screen, color_list, num_cells,
pixel[0] - diff_cur[3*x +0],
pixel[1] - diff_cur[3*x +1],
pixel[2] - diff_cur[3*x +2]) ;
diff_r = (color_list[closest].red >>8) - pixel[0];
diff_g = (color_list[closest].green >>8) - pixel[1];
diff_b = (color_list[closest].blue >>8) - pixel[2];
diff_cur[3*(x+1) + 0 ] += diff_r /2;
diff_cur[3*(x+1) + 1 ] += diff_g /2;
diff_cur[3*(x+1) + 2 ] += diff_b /2;
diff_next[3*(x-1) +0] += diff_r /8;
diff_next[3*(x-1) +1] += diff_g /8;
diff_next[3*(x-1) +2] += diff_b /8;
diff_next[3*(x+0) +0] += diff_r /8;
diff_next[3*(x+0) +1] += diff_g /8;
diff_next[3*(x+0) +2] += diff_b /8;
/* initialize next line */
diff_next[3*(x+1) +0] = diff_r /4;
diff_next[3*(x+1) +1] = diff_g /4;
diff_next[3*(x+1) +2] = diff_b /4;
#else
closest = closest_color_index( display, screen, color_list, num_cells,
pixel[0] ,
pixel[1] ,
pixel[2]) ;
#endif /* USE_SF */
XSetForeground( display, gc, closest) ;
XDrawPoint( display, pixmap, gc, x, y) ;
pixel += bytes_per_pixel ;
}
#ifdef USE_FS
closest = closest_color_index( display, screen, color_list, num_cells,
pixel[0] - diff_cur[3*x +0],
pixel[1] - diff_cur[3*x +1],
pixel[2] - diff_cur[3*x +2]) ;
diff_r = (color_list[closest].red >>8) - pixel[0];
diff_g = (color_list[closest].green >>8) - pixel[1];
diff_b = (color_list[closest].blue >>8) - pixel[2];
diff_next[3*(x-1) +0] += diff_r /4;
diff_next[3*(x-1) +1] += diff_g /4;
diff_next[3*(x-1) +2] += diff_b /4;
diff_next[3*(x+0) +0] += diff_r /4;
diff_next[3*(x+0) +1] += diff_g /4;
diff_next[3*(x+0) +2] += diff_b /4;
temp = diff_cur;
diff_cur = diff_next;
diff_next = temp;
#else
closest = closest_color_index( display, screen, color_list, num_cells,
pixel[0],
pixel[1],
pixel[2]) ;
#endif /* USE_SF */
XSetForeground( display, gc, closest) ;
XDrawPoint( display, pixmap, gc, x, y) ;
line += rowstride ;
}
free( color_list) ;
XFreeGC( display, gc) ;
#ifdef USE_FS
free( diff_cur) ;
free( diff_next) ;
#endif /* USE_SF */
return SUCCESS ;
}
/* Get an background pixmap from _XROOTMAP_ID */
static Pixmap
tile_pixmap(
Display * display,
int screen,
GC gc ,
Pixmap pixmap,
int force_copy
)
{
Window dummy ;
int ax,ay ;
unsigned int aw, ah;
unsigned int bw, depth;
Pixmap result;
/* border width is not used */
XGetGeometry( display, pixmap, &dummy, &ax, &ay,
&aw, &ah, &bw, &depth) ;
if ( force_copy ||
aw < DisplayWidth( display, screen) ||
ah < DisplayHeight( display, screen))
{
/* Some WM needs tiling... sigh.*/
result = XCreatePixmap( display, pixmap,
DisplayWidth( display, screen),
DisplayHeight( display, screen),
depth) ;
XSetTile( display, gc, pixmap) ;
XSetTSOrigin( display, gc, 0, 0) ;
XSetFillStyle( display, gc, FillTiled) ;
/* XXX not correct with virtual desktop? */
XFillRectangle( display, result, gc, 0, 0,
DisplayWidth( display, screen),
DisplayHeight( display, screen)) ;
return result ;
}
return pixmap ;
}
static Pixmap
root_pixmap(
Display * display
)
{
Atom id ;
int act_format ;
u_long nitems ;
u_long bytes_after ;
u_char * prop ;
id = XInternAtom( display, "_XROOTPMAP_ID", True) ;
if( !id)
{
#ifdef DEBUG
kik_warn_printf(KIK_DEBUG_TAG "_XROOTPMAP_ID is not available\n") ;
#endif
return None ;
}
if( XGetWindowProperty( display, DefaultRootWindow(display), id, 0, 1,
False, XA_PIXMAP, &id, &act_format,
&nitems, &bytes_after, &prop) == Success)
{
if( prop)
{
Pixmap root ;
root = *((Drawable *)prop) ;
XFree( prop) ;
return root ;
}
#ifdef DEBUG
else
{
kik_warn_printf(KIK_DEBUG_TAG "failed tp read prop\n") ;
}
#endif
}
return None ;
}
static pixmap_cache_t *
cache_seek(
Display * display
)
{
pixmap_cache_t * cache = pixmap_cache ;
while( cache)
{
if( cache->display == display)
break ;
cache = cache->next ;
}
return cache ;
}
/* add a new entry to the head of the cache */
static pixmap_cache_t *
cache_add(
Display * display
)
{
pixmap_cache_t * cache ;
cache = malloc( sizeof( pixmap_cache_t)) ;
if (!cache)
return NULL ;
cache->display = display ;
cache->root = None ;
cache->cooked = None ;
cache->pic_mod = NULL ;
cache->next = pixmap_cache ;
pixmap_cache = cache ;
return cache ;
}
/**Remove a cache for the display
*
*\param display display to be removed from the cache.
*
*/
static void
cache_delete(
Display * display
)
{
pixmap_cache_t * cache ;
pixmap_cache_t * o ;
o = NULL ;
cache = pixmap_cache ;
while( cache)
{
if ( cache->display == display)
{
if ((cache->cooked) && (cache->root != cache->cooked))
XFreePixmap( display, cache->cooked) ;
if ( cache->pic_mod)
free( cache->pic_mod) ;
if ( o == NULL)
{
pixmap_cache = cache->next ;
free( cache) ;
cache = pixmap_cache ;
}
else
{
o->next = cache->next ;
free( cache) ;
cache = o->next;
}
}
else
{
o = cache ;
cache = cache->next ;
}
}
}
/**judge whether pic_mods are equal or not
*\param a,b picture modifier
*\return 1 when they are same. 0 when not.
*/
static int
is_picmod_eq(
x_picture_modifier_t * a ,
x_picture_modifier_t * b
)
{
if( a == b)
return 1 ;
if ( a == NULL)
{
a = b ;
b = NULL ;
}
if (b == NULL)
{
if( (a->brightness == 100) &&
(a->contrast == 100) &&
(a->gamma == 100))
{
return 1 ;
}
}
else
{
if( (a->brightness == b->brightness) &&
(a->contrast == b->contrast) &&
(a->gamma == b->gamma))
{
return 1 ;
}
}
return 0 ;
}
/**Return position of the least significant bit
*
*\param val value to count
*
*/
static int
lsb(
unsigned int val
)
{
int nth = 0 ;
if( val == 0)
return 0 ;
while((val & 1) == 0)
{
val = val >>1 ;
nth ++ ;
}
return nth ;
}
/**Return position of the most significant bit
*
*\param val value to count
*
*/
static int
msb(
unsigned int val
)
{
int nth ;
if( val == 0)
return 0 ;
nth = lsb( val) +1 ;
while(val & (1 << nth))
nth++ ;
return nth ;
}
static XImage *
pixbuf_to_ximage_truecolor(
Display * display,
int screen,
GdkPixbuf * pixbuf,
int depth ,
XVisualInfo * vinfo
)
{
unsigned int i, j ;
unsigned int width, height, rowstride, bytes_per_pixel ;
unsigned char * line ;
unsigned char * pixel ;
long r_mask, g_mask, b_mask ;
int r_offset, g_offset, b_offset;
XImage * image = NULL;
width = gdk_pixbuf_get_width( pixbuf) ;
height = gdk_pixbuf_get_height( pixbuf) ;
if( !width || !height)
return NULL ;
r_mask = vinfo[0].red_mask ;
g_mask = vinfo[0].green_mask ;
b_mask = vinfo[0].blue_mask ;
r_offset = lsb( r_mask) ;
g_offset = lsb( g_mask) ;
b_offset = lsb( b_mask) ;
bytes_per_pixel = (gdk_pixbuf_get_has_alpha( pixbuf)) ? 4:3 ;
rowstride = gdk_pixbuf_get_rowstride (pixbuf) ;
line = gdk_pixbuf_get_pixels( pixbuf) ;
switch ( depth)
{
case 15:
case 16:
{
int r_limit, g_limit, b_limit ;
u_int16_t *data ;
if( width > (SIZE_MAX / 2) / height)
return NULL ;
data = (u_int16_t *)malloc( (size_t)width * height * 2) ;
if( !data)
return NULL ;
image = XCreateImage( display, DefaultVisual( display, screen),
DefaultDepth( display, screen), ZPixmap, 0,
(char *)data,
width, height,
16,
width * 2) ;
r_limit = 8 + r_offset - msb( r_mask) ;
g_limit = 8 + g_offset - msb( g_mask) ;
b_limit = 8 + b_offset - msb( b_mask) ;
for (i = 0; i < height; i++)
{
pixel = line ;
for (j = 0; j < width; j++)
{
XPutPixel( image, j, i,
( ( (pixel[0] >> r_limit) << r_offset) & r_mask) |
( ( (pixel[1] >> g_limit) << g_offset) & g_mask) |
( ( (pixel[2] >> b_limit) << b_offset) & b_mask)) ;
pixel += bytes_per_pixel ;
}
line += rowstride ;
}
break;
}
case 24:
case 32:
{
u_int32_t * data ;
if( width > (SIZE_MAX / 4) / height)
return NULL ;
data = (u_int32_t *)malloc( (size_t)width * height * 4) ;
if( !data)
return NULL;
image = XCreateImage( display, DefaultVisual( display, screen),
DefaultDepth( display, screen), ZPixmap, 0,
(char *)data,
width, height,
32,
width * 4) ;
for( i = 0; i < height; i++)
{
pixel = line ;
for( j = 0; j < width; j++)
{
XPutPixel( image, j, i, pixel[0] <<r_offset | pixel[1] <<g_offset | pixel[2]<<b_offset) ;
pixel +=bytes_per_pixel ;
}
line += rowstride ;
}
break ;
}
default:
break;
}
return image ;
}
static int
pixbuf_to_pixmap(
Display * display,
int screen,
GdkPixbuf * pixbuf,
Pixmap pixmap
)
{
XImage * image = NULL;
int matched ;
XVisualInfo * vinfolist ;
XVisualInfo vinfo ;
if( !pixbuf)
return -1 ;
vinfo.visualid = XVisualIDFromVisual( DefaultVisual( display, screen)) ;
if (!vinfo.visualid)
return -2 ;
vinfolist = XGetVisualInfo( display, VisualIDMask, &vinfo, &matched) ;
if( !vinfolist)
return -3 ;
if ( !matched)
{
XFree( vinfolist) ;
return -4 ;
}
switch( vinfolist[0].class)
{
case TrueColor:
{
image = pixbuf_to_ximage_truecolor( display,
screen,
pixbuf,
DefaultDepth( display, screen),
vinfolist) ;
XFree( vinfolist) ;
if( image)
{
XPutImage( display, pixmap, DefaultGC( display, screen), image, 0, 0, 0, 0,
gdk_pixbuf_get_width( pixbuf),gdk_pixbuf_get_height( pixbuf)) ;
XDestroyImage( image) ;
return SUCCESS ;
}
break ;
}
case PseudoColor:
{
XFree( vinfolist) ;
if( pixbuf_to_pixmap_pseudocolor( display, screen, pixbuf, pixmap) != SUCCESS)
return -1;
return SUCCESS ;
break ;
}
default:
{
XFree( vinfolist) ;
break ;
}
}
return -1 ;
}
static XImage *
compose_truecolor(
Display * display,
int screen,
GdkPixbuf * pixbuf,
Pixmap pixmap,
XVisualInfo * vinfo
)
{
XImage * image ;
unsigned int i, j ;
unsigned int width, height, rowstride ;
unsigned char *line ;
unsigned char *pixel ;
int r_offset, g_offset, b_offset;
long r_mask, g_mask, b_mask ;
long r, g, b ;
long data ;
width = gdk_pixbuf_get_width (pixbuf) ;
height = gdk_pixbuf_get_height (pixbuf) ;
image = XGetImage( display, pixmap, 0, 0, width, height, AllPlanes, ZPixmap) ;
r_mask = vinfo[0].red_mask ;
g_mask = vinfo[0].green_mask ;
b_mask = vinfo[0].blue_mask ;
r_offset = lsb( r_mask) ;
g_offset = lsb( g_mask) ;
b_offset = lsb( b_mask) ;
rowstride = gdk_pixbuf_get_rowstride( pixbuf) ;
line = gdk_pixbuf_get_pixels( pixbuf) ;
for( i = 0; i < height; i++)
{
pixel = line ;
for( j = 0; j < width; j++)
{
data = XGetPixel( image, j, i) ;
r = ((data & r_mask) >>r_offset) ;
g = ((data & g_mask) >>g_offset) ;
b = ((data & b_mask) >>b_offset) ;
r = (r*(256 - pixel[3]) + pixel[0] * pixel[3])>>8 ;
g = (g*(256 - pixel[3]) + pixel[1] * pixel[3])>>8 ;
b = (b*(256 - pixel[3]) + pixel[2] * pixel[3])>>8 ;
XPutPixel( image, j, i,
(r <<r_offset) |
(g <<g_offset) |
(b <<b_offset)) ;
pixel += 4 ;
}
line += rowstride ;
}
return image ;
}
static XImage *
compose_pseudocolor(
Display * display,
int screen,
GdkPixbuf * pixbuf,
Pixmap pixmap,
int depth,
XVisualInfo * vinfo
)
{
XImage * image ;
unsigned int i, j, num_cells, r, g, b ;
unsigned int width, height, rowstride ;
unsigned char *line ;
unsigned char *pixel ;
long data ;
XColor * color_list ;
num_cells = fetch_colormap( display, screen, &color_list) ;
if( !num_cells)
return NULL ;
width = gdk_pixbuf_get_width (pixbuf) ;
height = gdk_pixbuf_get_height (pixbuf) ;
image = XGetImage( display, pixmap, 0, 0, width, height, AllPlanes, ZPixmap) ;
rowstride = gdk_pixbuf_get_rowstride( pixbuf) ;
line = gdk_pixbuf_get_pixels( pixbuf) ;
for( i = 0; i < height; i++)
{
pixel = line ;
for( j = 0; j < width; j++)
{
data = XGetPixel( image, j ,i) ;
r = color_list[data].red >>8 ;
g = color_list[data].green >>8 ;
b = color_list[data].blue >>8 ;
r = (r*(256 - pixel[3]) + pixel[0] * pixel[3])>>8 ;
g = (g*(256 - pixel[3]) + pixel[1] * pixel[3])>>8 ;
b = (b*(256 - pixel[3]) + pixel[2] * pixel[3])>>8 ;
XPutPixel( image, j, i,
closest_color_index( display, screen,
color_list, num_cells,
r, g, b)) ;
pixel += 4 ;
}
line += rowstride ;
}
free( color_list) ;
return image ;
}
static int
compose_to_pixmap(
Display * display,
int screen,
GdkPixbuf * pixbuf,
Pixmap pixmap)
{
int matched ;
XVisualInfo * vinfolist ;
XVisualInfo vinfo ;
XImage * image = NULL ;
if(!pixbuf)
return -1 ;
vinfo.visualid = XVisualIDFromVisual( DefaultVisual( display, screen)) ;
if (!vinfo.visualid)
return -2 ;
vinfolist = XGetVisualInfo( display, VisualIDMask, &vinfo, &matched) ;
if( !vinfolist)
return -3 ;
if ( !matched)
{
XFree( vinfolist) ;
return -4 ;
}
switch( vinfolist[0].class)
{
case TrueColor:
image = compose_truecolor( display,
screen,
pixbuf,
pixmap,
vinfolist) ;
break;
case PseudoColor:
image = compose_pseudocolor( display,
screen,
pixbuf,
pixmap,
DefaultDepth( display, screen),
vinfolist) ;
break;
default:
break;
}
XFree( vinfolist) ;
if ( !image)
{
return -5 ;
}
XPutImage( display, pixmap, DefaultGC( display, screen), image, 0, 0, 0, 0,
gdk_pixbuf_get_width( pixbuf), gdk_pixbuf_get_height( pixbuf)) ;
XDestroyImage( image) ;
return SUCCESS ;
}
static int
pixbuf_to_pixmap_and_mask(
Display * display,
int screen,
GdkPixbuf * pixbuf,
Pixmap * pixmap,
Pixmap * mask
)
{
if( pixbuf_to_pixmap( display, screen, pixbuf, *pixmap) == -1)
return -1 ;
if( gdk_pixbuf_get_has_alpha( pixbuf))
{
int i, j ;
int width, height, rowstride ;
unsigned char * line ;
unsigned char * pixel ;
GC gc ;
XGCValues gcv ;
width = gdk_pixbuf_get_width (pixbuf) ;
height = gdk_pixbuf_get_height (pixbuf) ;
*mask = XCreatePixmap( display,
DefaultRootWindow( display),
width, height, 1) ;
gc = XCreateGC (display, *mask, 0, &gcv) ;
XSetForeground( display, gc, 0) ;
XFillRectangle( display, *mask, gc,
0, 0, width, height) ;
XSetForeground( display, gc, 1) ;
line = gdk_pixbuf_get_pixels( pixbuf) ;
rowstride = gdk_pixbuf_get_rowstride (pixbuf) ;
for (i = 0; i < height; i++)
{
pixel = line + 3;
for (j = 0; j < width; j++)
{
if( *pixel > 127)
XDrawPoint( display, *mask, gc, j, i) ;
pixel += 4 ;
}
line += rowstride ;
}
XFreeGC( display, gc) ;
}
else
{ /* no mask */
*mask = None ;
}
return SUCCESS ;
}
static void
value_table_refresh(
x_picture_modifier_t * mod
)
{
int i, tmp ;
double real_gamma , real_brightness, real_contrast;
real_gamma = (double)(mod->gamma) / 100 ;
real_contrast = (double)(mod->contrast) / 100 ;
real_brightness = (double)(mod->brightness) / 100 ;
for( i = 0 ; i < 256 ; i++)
{
tmp = real_contrast * (255 * pow(((double)i + 0.5)/ 255, real_gamma) -128)
+ 128 * real_brightness ;
if( tmp >= 255)
break;
if( tmp < 0)
value_table[i] = 0 ;
else
value_table[i] = tmp ;
}
for( ; i< 256 ; i++)
value_table[i] = 255 ;
}
static int
modify_image(
GdkPixbuf * pixbuf,
x_picture_modifier_t * pic_mod
)
{
int i, j ;
int width, height, rowstride, bytes_per_pixel ;
unsigned char *line ;
unsigned char *pixel ;
if ( !pixbuf)
return -1 ;
bytes_per_pixel = (gdk_pixbuf_get_has_alpha( pixbuf)) ? 4:3 ;
width = gdk_pixbuf_get_width (pixbuf) ;
height = gdk_pixbuf_get_height (pixbuf) ;
rowstride = gdk_pixbuf_get_rowstride (pixbuf) ;
if( !pic_mod)
return -2 ;
if( is_picmod_eq( pic_mod, NULL))
return SUCCESS ;
line = gdk_pixbuf_get_pixels( pixbuf) ;
value_table_refresh( pic_mod) ;
for (i = 0; i < height; i++)
{
pixel = line ;
line += rowstride ;
for (j = 0; j < width; j++)
{
/* XXX keeps neither hue nor saturation. MUST be replaced by another better color model(CIE Yxy? lab?)*/
pixel[0] = value_table[pixel[0]] ;
pixel[1] = value_table[pixel[1]] ;
pixel[2] = value_table[pixel[2]] ;
/* alpha plane is not changed */
pixel += bytes_per_pixel ;
}
}
return SUCCESS ;
}
static int
modify_pixmap(
Display * display,
int screen,
Pixmap pixmap,
x_picture_modifier_t * pic_mod
)
{
int i, j ;
int width, height ;
int border, depth, x, y ;
Window root ;
XImage * image ;
int r_offset, g_offset, b_offset ;
int r_mask, g_mask, b_mask ;
int matched ;
XVisualInfo * vinfolist ;
XVisualInfo vinfo ;
if ( !pixmap)
return -1 ;
if( is_picmod_eq( pic_mod, NULL))
return SUCCESS ;
vinfo.visualid = XVisualIDFromVisual( DefaultVisual( display, screen)) ;
vinfolist = XGetVisualInfo( display, VisualIDMask, &vinfo, &matched) ;
if( !vinfolist)
return -2;
if ( !matched)
{
XFree( vinfolist) ;
return -3;
}
XGetGeometry( display, pixmap, &root, &x, &y,
&width, &height, &border, &depth) ;
image = XGetImage( display, pixmap, 0, 0, width, height, AllPlanes, ZPixmap) ;
switch( vinfolist[0].class)
{
case TrueColor:
{
unsigned char r, g, b ;
int r_limit, g_limit, b_limit ;
long data ;
r_mask = vinfolist[0].red_mask ;
g_mask = vinfolist[0].green_mask ;
b_mask = vinfolist[0].blue_mask ;
r_offset = lsb( r_mask) ;
g_offset = lsb( g_mask) ;
b_offset = lsb( b_mask) ;
r_limit = 8 + r_offset - msb( r_mask) ;
g_limit = 8 + g_offset - msb( g_mask) ;
b_limit = 8 + b_offset - msb( b_mask) ;
value_table_refresh( pic_mod) ;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
data = XGetPixel( image, j, i) ;
r = ((data & r_mask) >> r_offset)<<r_limit ;
g = ((data & g_mask) >> g_offset)<<g_limit ;
b = ((data & b_mask) >> b_offset)<<b_limit ;
r = value_table[r] ;
g = value_table[g] ;
b = value_table[b] ;
XPutPixel( image, j, i,
(r >> r_limit) << r_offset |
(g >> g_limit) << g_offset |
(b >> b_limit) << b_offset) ;
}
}
XPutImage( display, pixmap, DefaultGC( display, screen),
image, 0, 0, 0, 0, width, height) ;
break ;
}
case PseudoColor:
switch( vinfolist[0].depth)
{
case 8:
{
XColor * color_list ;
int num_cells ;
long data ;
unsigned char r, g, b ;
num_cells = fetch_colormap( display, screen, &color_list) ;
if( !num_cells)
return -7 ;
value_table_refresh( pic_mod) ;
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
data = XGetPixel( image, j, i) ;
r = (color_list[data].red >>8) ;
g = (color_list[data].green >>8) ;
b = (color_list[data].blue >>8) ;
r = value_table[r] ;
g = value_table[g] ;
b = value_table[b] ;
XPutPixel( image, j, i,
closest_color_index( display, screen,
color_list, num_cells,
r, g, b)) ;
}
}
XPutImage( display, pixmap, DefaultGC( display, screen), image, 0, 0, 0, 0,
width, height) ;
free( color_list) ;
break ;
}
}
}
XFree(vinfolist) ;
XDestroyImage( image) ;
return SUCCESS ;
}
/* --- global functions --- */
int
x_imagelib_display_opened(
Display * display
)
{
if (display_count == 0)
{
#if GDK_PIXBUF_MAJOR >= 2
g_type_init() ;
#endif /*GDK_PIXBUF_MAJOR*/
}
display_count ++ ;
return 1 ;
}
int
x_imagelib_display_closed(
Display * display
)
{
display_count -- ;
cache_delete( display) ;
if( display_count == 0)
{
/* drop pixbuf cache */
load_file( NULL, 0, 0, 0) ;
}
return 1 ;
}
/** Load an image from the specified file.
*\param win mlterm window.
*\param path File full path.
*\param pic_mod picture modifier.
*
*\return Pixmap to be used as a window's background.
*/
Pixmap
x_imagelib_load_file_for_background(
x_window_t * win,
char * file_path,
x_picture_modifier_t * pic_mod
)
{
static x_picture_modifier_t * cached_mod = NULL ;
static GdkPixbuf * cached_pixbuf = NULL ;
static char * cached_pixbuf_file_path = NULL ;
GdkPixbuf * pixbuf ;
Pixmap pixmap ;
if(!file_path)
return None ;
if( cached_pixbuf
&& (is_picmod_eq( pic_mod, cached_mod))
&& ACTUAL_WIDTH(win) == gdk_pixbuf_get_width( cached_pixbuf)
&& ACTUAL_HEIGHT(win) == gdk_pixbuf_get_height( cached_pixbuf)
&& (strcmp( file_path, cached_pixbuf_file_path) == 0))
{
/* use pixbuf in the cache */
pixbuf = cached_pixbuf ;
}
else
{
/* re-generate cache */
if( !( pixbuf = load_file( file_path,
ACTUAL_WIDTH(win), ACTUAL_HEIGHT(win),
GDK_INTERP_NEAREST)))
{
return None ;
}
if( cached_pixbuf)
{
#if GDK_PIXBUF_MAJOR >= 2
g_object_unref( cached_pixbuf) ;
#else
gdk_pixbuf_unref( cached_pixbuf) ;
#endif /*GDK_PIXBUF_MAJOR*/
}
free(cached_mod) ;
free(cached_pixbuf_file_path) ;
cached_pixbuf_file_path = strdup( file_path) ;
if( is_picmod_eq( pic_mod, NULL))
{
cached_pixbuf = pixbuf ;
}
else
{
cached_pixbuf = gdk_pixbuf_copy(pixbuf) ;
#if GDK_PIXBUF_MAJOR >= 2
g_object_unref( pixbuf) ;
#else
gdk_pixbuf_unref( pixbuf) ;
#endif /*GDK_PIXBUF_MAJOR*/
modify_image( cached_pixbuf, pic_mod) ;
}
if( pic_mod)
{
cached_mod = malloc(sizeof(x_picture_modifier_t)) ;
memcpy( cached_mod, pic_mod, sizeof(x_picture_modifier_t)) ;
}
else
{
cached_mod = NULL ;
}
pixbuf = cached_pixbuf ;
}
if( gdk_pixbuf_get_has_alpha ( pixbuf) && (pixmap = x_imagelib_get_transparent_background( win, NULL)))
{
if( compose_to_pixmap( win->display, win->screen,
pixbuf, pixmap) != SUCCESS)
{
XFreePixmap( win->display, pixmap) ;
return None ;
}
}
else
{
pixmap = XCreatePixmap( win->display, win->my_window,
ACTUAL_WIDTH(win), ACTUAL_HEIGHT(win),
DefaultDepth( win->display, win->screen)) ;
if( pixbuf_to_pixmap( win->display, win->screen,
pixbuf, pixmap) != SUCCESS)
{
XFreePixmap( win->display, pixmap) ;
return None ;
}
}
return pixmap ;
}
/** Answer whether pseudo transparency is available
*\param display connection to X server.
*
*\return Success => 1, Failure => 0
*/
int
x_imagelib_root_pixmap_available(
Display * display
)
{
if( root_pixmap( display))
return 1 ;
return 0 ;
}
/** Create an pixmap from root window
*\param win window structure
*\param pic_mod picture modifier
*
*\return Newly allocated Pixmap (or None in the case of failure)
*/
Pixmap
x_imagelib_get_transparent_background(
x_window_t * win,
x_picture_modifier_t * pic_mod
)
{
int x ;
int y ;
int pix_x ;
int pix_y ;
u_int width ;
u_int height ;
pixmap_cache_t * cache ;
Pixmap pixmap ;
Pixmap current_root ;
GC gc ;
current_root = root_pixmap( win->display) ;
if(current_root == None)
return None;
cache = cache_seek( win->display) ;
if( cache)
{
/* discard old info */
if ( cache->root != current_root)
{
if((cache->cooked != None) && (cache->cooked != cache->root))
XFreePixmap( win->display, cache->cooked) ;
cache->root = current_root ;
cache->cooked = None ;
free(cache->pic_mod) ;
cache->pic_mod = NULL ;
}
}
else
{
cache = cache_add( win->display) ;
if( !cache)
return None ;
cache->root = current_root ;
}
if( ! x_window_get_visible_geometry( win, &x, &y, &pix_x, &pix_y, &width, &height))
return None ;
/* The pixmap to be returned */
pixmap = XCreatePixmap( win->display, win->my_window, ACTUAL_WIDTH(win), ACTUAL_HEIGHT(win),
DefaultDepth( win->display, win->screen)) ;
gc = XCreateGC( win->display, win->my_window, 0, 0) ;
if ( !is_picmod_eq( cache->pic_mod, pic_mod))
{
if((cache->cooked != None) && (cache->cooked != cache->root))
XFreePixmap( win->display, cache->cooked) ;
/* re-creation */
if(pic_mod)
{
/* we need a copy of pixmap to modify */
cache->cooked = tile_pixmap( win->display,
win->screen,
gc,
current_root
,1) ;
free(cache->pic_mod) ;
cache->pic_mod = malloc(sizeof(x_picture_modifier_t)) ;
memcpy( cache->pic_mod, pic_mod, sizeof(x_picture_modifier_t)) ;
modify_pixmap( win->display, win->screen, cache->cooked, pic_mod) ;
}
else
{
cache->cooked = tile_pixmap( win->display,
win->screen,
gc,
current_root,
0) ;
}
}
else
{
if( cache->cooked == None)
{
cache->cooked = tile_pixmap( win->display,
win->screen,
gc,
current_root,
0) ;
}
}
XCopyArea( win->display, cache->cooked, pixmap, gc, x, y, width, height, pix_x, pix_y) ;
XFreeGC( win->display, gc) ;
return pixmap ;
}
/** Load an image from the specified file with alpha plane. A pixmap and a mask are returned.
*\param display connection to the X server.
*\param path File full path.
*\param cardinal Returns pointer to a data structure for the extended WM hint spec.
*\param pixmap Returns an image pixmap for the old WM hint.
*\param mask Returns a mask bitmap for the old WM hint.
*\param width Pointer to the desired width. If *width is 0, the returned image would not be scaled and *width would be overwritten by its width. "width" can be NULL and the image would not be scaled and nothing would be returned in this case.
*\param height Pointer to the desired height. *height can be 0 and height can be NULL(see "width" 's description)
*
*\return Success => 1, Failure => 0
*/
int x_imagelib_load_file(
Display * display,
char * path,
u_int32_t ** cardinal,
Pixmap * pixmap,
Pixmap * mask,
unsigned int * width,
unsigned int * height
)
{
GdkPixbuf * pixbuf ;
unsigned int dst_height, dst_width ;
if( !width)
{
dst_width = 0 ;
}
else
{
dst_width = *width ;
}
if( !height)
{
dst_height = 0 ;
}
else
{
dst_height = *height ;
}
if( path)
{
/* create a pixbuf from the file and create a cardinal array */
if( !( pixbuf = load_file( path, dst_width, dst_height, GDK_INTERP_NEAREST)))
{
#ifdef DEBUG
kik_warn_printf(KIK_DEBUG_TAG "couldn't load pixbuf\n") ;
#endif
return 0 ;
}
if ( cardinal)
create_cardinals_from_bixbuf( cardinal, dst_width, dst_height, pixbuf) ;
}
else
{
if( !cardinal || !(*cardinal))
return 0 ;
/* create a pixbuf from the cardinal array */
pixbuf = create_pixbuf_from_cardinals( *cardinal, dst_width, dst_height) ;
if( !pixbuf)
return 0 ;
}
dst_width = gdk_pixbuf_get_width( pixbuf) ;
dst_height = gdk_pixbuf_get_height( pixbuf) ;
/* Create the Icon pixmap & mask to be used in WMHints.
* Note that none as a result is acceptable.
* Pixmaps can't be cached since the last pixmap may be freed by someone... */
if( pixmap)
{
*pixmap = XCreatePixmap( display, DefaultRootWindow( display),
dst_width, dst_height,
DefaultDepth( display, DefaultScreen( display))) ;
if( mask)
{
if( pixbuf_to_pixmap_and_mask( display,
DefaultScreen( display),
pixbuf, pixmap, mask) != SUCCESS)
{
#if GDK_PIXBUF_MAJOR >= 2
g_object_unref( pixbuf) ;
#else
gdk_pixbuf_unref( pixbuf) ;
#endif /*GDK_PIXBUF_MAJOR*/
XFreePixmap( display, *pixmap) ;
*pixmap = None ;
XFreePixmap( display, *mask) ;
*mask = None ;
return 0 ;
}
}
else
{
if( pixbuf_to_pixmap( display, DefaultScreen( display),
pixbuf, *pixmap) != SUCCESS)
{
#if GDK_PIXBUF_MAJOR >= 2
g_object_unref( pixbuf) ;
#else
gdk_pixbuf_unref( pixbuf) ;
#endif /*GDK_PIXBUF_MAJOR*/
XFreePixmap( display, *pixmap) ;
*pixmap = None ;
return 0 ;
}
}
}
if( width && *width == 0)
{
*width = dst_width ;
}
if( height && *height == 0)
{
*height = dst_height ;
}
#if GDK_PIXBUF_MAJOR >= 2
g_object_unref( pixbuf) ;
#else
gdk_pixbuf_unref( pixbuf) ;
#endif /*GDK_PIXBUF_MAJOR*/
return 1 ;
}
|