1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062
|
/* _____ ___ _ _ _____
** __ _| ___/ _ \| \ | |_ _|
** / _` | |_ | | | | \| | | |
** | (_| | _|| |_| | |\ | | |
** \__, |_| \___/|_| \_| |_|
** |___/
**
** gFONT -- Create GIF image rendered with TeX-available Font
**
** The gFONT program creates a GIF image for a given ASCII string
** by the use of an arbitrary TeX-available font (Postscript or
** METAFONT). The used font is converted from TeX's PK format to
** gFONT's own GdF format (Gd Font) and rendered into the
** resulting GIF image by the use of its own enhanced Gd library.
** The result is intended to be included into HTML pages with an
** IMG tag.
**
** ======================================================================
**
** Copyright (c) 1997 Ralf S. Engelschall, All rights reserved.
**
** This program is free software; it may be redistributed and/or modified
** only under the terms of either the Artistic License or the GNU General
** Public License, which may be found in the ePerl source distribution.
** Look at the files ARTISTIC and COPYING or run ``eperl -l'' to receive
** a built-in copy of both license files.
**
** 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 either the
** Artistic License or the GNU General Public License for more details.
**
** ======================================================================
**
** gfont_gd.c -- Enhanced Gd library functions
*/
/*
** This is my patched gd library, version 1.2+.
** I've enhanced it by the following features:
** o Loading of GDF (Gd Font) format files
** o Support for proportional fonts
** o gdImageStringWidth
** o gdImageCharUp|Down
** o gdImageStringUp|Down
** o gdImageFilledEllipse
** -- 970215 rse
*/
/*
** Written by Tom Boutell, 5/94.
** Copyright 1994, Cold Spring Harbor Labs.
** Permission granted to use this code in any fashion provided
** that this notice is retained and any alterations are
** labeled as such. It is requested, but not required, that
** you share extensions to this module with us so that we
** can incorporate them into new versions.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#else /* HAVE_STRINGS_H */
#include <string.h>
#endif /* HAVE_STRINGS_H */
#include <sys/stat.h>
#include <math.h>
#include "gfont_gd.h"
#include "gfont_gdfontl.h"
#include "lib/xalloc.h"
#define costScale 1024
int cost[] = {
1024, 1023, 1023, 1022, 1021, 1020, 1018, 1016, 1014, 1011, 1008, 1005,
1001, 997, 993, 989, 984, 979, 973, 968, 962, 955, 949, 942, 935, 928, 920,
912, 904, 895, 886, 877, 868, 858, 848, 838, 828, 817, 806, 795, 784, 772,
760, 748, 736, 724, 711, 698, 685, 671, 658, 644, 630, 616, 601, 587, 572,
557, 542, 527, 512, 496, 480, 464, 448, 432, 416, 400, 383, 366, 350, 333,
316, 299, 282, 265, 247, 230, 212, 195, 177, 160, 142, 124, 107, 89, 71, 53,
35, 17, 0, -17, -35, -53, -71, -89, -107, -124, -142, -160, -177, -195,
-212, -230, -247, -265, -282, -299, -316, -333, -350, -366, -383, -400,
-416, -432, -448, -464, -480, -496, -512, -527, -542, -557, -572, -587,
-601, -616, -630, -644, -658, -671, -685, -698, -711, -724, -736, -748,
-760, -772, -784, -795, -806, -817, -828, -838, -848, -858, -868, -877,
-886, -895, -904, -912, -920, -928, -935, -942, -949, -955, -962, -968,
-973, -979, -984, -989, -993, -997, -1001, -1005, -1008, -1011, -1014,
-1016, -1018, -1020, -1021, -1022, -1023, -1023, -1024, -1023, -1023, -1022,
-1021, -1020, -1018, -1016, -1014, -1011, -1008, -1005, -1001, -997, -993,
-989, -984, -979, -973, -968, -962, -955, -949, -942, -935, -928, -920,
-912, -904, -895, -886, -877, -868, -858, -848, -838, -828, -817, -806,
-795, -784, -772, -760, -748, -736, -724, -711, -698, -685, -671, -658,
-644, -630, -616, -601, -587, -572, -557, -542, -527, -512, -496, -480,
-464, -448, -432, -416, -400, -383, -366, -350, -333, -316, -299, -282,
-265, -247, -230, -212, -195, -177, -160, -142, -124, -107, -89, -71, -53,
-35, -17, 0, 17, 35, 53, 71, 89, 107, 124, 142, 160, 177, 195, 212, 230,
247, 265, 282, 299, 316, 333, 350, 366, 383, 400, 416, 432, 448, 464, 480,
496, 512, 527, 542, 557, 572, 587, 601, 616, 630, 644, 658, 671, 685, 698,
711, 724, 736, 748, 760, 772, 784, 795, 806, 817, 828, 838, 848, 858, 868,
877, 886, 895, 904, 912, 920, 928, 935, 942, 949, 955, 962, 968, 973, 979,
984, 989, 993, 997, 1001, 1005, 1008, 1011, 1014, 1016, 1018, 1020, 1021,
1022, 1023, 1023
};
#define sintScale 1024
int sint[] = {
0, 17, 35, 53, 71, 89, 107, 124, 142, 160, 177, 195, 212, 230, 247, 265,
282, 299, 316, 333, 350, 366, 383, 400, 416, 432, 448, 464, 480, 496, 512,
527, 542, 557, 572, 587, 601, 616, 630, 644, 658, 671, 685, 698, 711, 724,
736, 748, 760, 772, 784, 795, 806, 817, 828, 838, 848, 858, 868, 877, 886,
895, 904, 912, 920, 928, 935, 942, 949, 955, 962, 968, 973, 979, 984, 989,
993, 997, 1001, 1005, 1008, 1011, 1014, 1016, 1018, 1020, 1021, 1022, 1023,
1023, 1024, 1023, 1023, 1022, 1021, 1020, 1018, 1016, 1014, 1011, 1008,
1005, 1001, 997, 993, 989, 984, 979, 973, 968, 962, 955, 949, 942, 935, 928,
920, 912, 904, 895, 886, 877, 868, 858, 848, 838, 828, 817, 806, 795, 784,
772, 760, 748, 736, 724, 711, 698, 685, 671, 658, 644, 630, 616, 601, 587,
572, 557, 542, 527, 512, 496, 480, 464, 448, 432, 416, 400, 383, 366, 350,
333, 316, 299, 282, 265, 247, 230, 212, 195, 177, 160, 142, 124, 107, 89,
71, 53, 35, 17, 0, -17, -35, -53, -71, -89, -107, -124, -142, -160, -177,
-195, -212, -230, -247, -265, -282, -299, -316, -333, -350, -366, -383,
-400, -416, -432, -448, -464, -480, -496, -512, -527, -542, -557, -572,
-587, -601, -616, -630, -644, -658, -671, -685, -698, -711, -724, -736,
-748, -760, -772, -784, -795, -806, -817, -828, -838, -848, -858, -868,
-877, -886, -895, -904, -912, -920, -928, -935, -942, -949, -955, -962,
-968, -973, -979, -984, -989, -993, -997, -1001, -1005, -1008, -1011, -1014,
-1016, -1018, -1020, -1021, -1022, -1023, -1023, -1024, -1023, -1023, -1022,
-1021, -1020, -1018, -1016, -1014, -1011, -1008, -1005, -1001, -997, -993,
-989, -984, -979, -973, -968, -962, -955, -949, -942, -935, -928, -920,
-912, -904, -895, -886, -877, -868, -858, -848, -838, -828, -817, -806,
-795, -784, -772, -760, -748, -736, -724, -711, -698, -685, -671, -658,
-644, -630, -616, -601, -587, -572, -557, -542, -527, -512, -496, -480,
-464, -448, -432, -416, -400, -383, -366, -350, -333, -316, -299, -282,
-265, -247, -230, -212, -195, -177, -160, -142, -124, -107, -89, -71, -53,
-35, -17
};
static void gdImageBrushApply(gdImagePtr im, int x, int y);
static void gdImageTileApply(gdImagePtr im, int x, int y);
static Font_Table *FontTable = 0; /* Tabelle fuer bereits geladene Fonts (CACHE) */
gdImagePtr gdImageCreate(int sx, int sy)
{
int i;
gdImagePtr im;
im = (gdImage *) xmalloc(sizeof(gdImage));
im->pixels = (unsigned char **) xmalloc(sizeof(unsigned char *) * sx);
im->polyInts = 0;
im->polyAllocated = 0;
im->brush = 0;
im->tile = 0;
im->style = 0;
for (i=0; (i<sx); i++) {
im->pixels[i] = (unsigned char *) xcalloc(
sy, sizeof(unsigned char));
}
im->sx = sx;
im->sy = sy;
im->colorsTotal = 0;
im->transparent = (-1);
im->interlace = 0;
return im;
}
void gdImageDestroy(gdImagePtr im)
{
int i;
for (i=0; (i<im->sx); i++) {
free(im->pixels[i]);
}
free(im->pixels);
if (im->polyInts) {
free(im->polyInts);
}
if (im->style) {
free(im->style);
}
free(im);
}
int gdImageColorClosest(gdImagePtr im, int r, int g, int b)
{
int i;
long rd, gd, bd;
int ct = (-1);
long mindist = 0;
for (i=0; (i<(im->colorsTotal)); i++) {
long dist;
if (im->open[i]) {
continue;
}
rd = (im->red[i] - r);
gd = (im->green[i] - g);
bd = (im->blue[i] - b);
dist = rd * rd + gd * gd + bd * bd;
if ((i == 0) || (dist < mindist)) {
mindist = dist;
ct = i;
}
}
return ct;
}
int gdImageColorExact(gdImagePtr im, int r, int g, int b)
{
int i;
for (i=0; (i<(im->colorsTotal)); i++) {
if (im->open[i]) {
continue;
}
if ((im->red[i] == r) &&
(im->green[i] == g) &&
(im->blue[i] == b)) {
return i;
}
}
return -1;
}
int gdImageColorAllocate(gdImagePtr im, int r, int g, int b)
{
int i;
int ct = (-1);
for (i=0; (i<(im->colorsTotal)); i++) {
if (im->open[i]) {
ct = i;
break;
}
}
if (ct == (-1)) {
ct = im->colorsTotal;
if (ct == gdMaxColors) {
return -1;
}
im->colorsTotal++;
}
im->red[ct] = r;
im->green[ct] = g;
im->blue[ct] = b;
im->open[ct] = 0;
return ct;
}
void gdImageColorDeallocate(gdImagePtr im, int color)
{
/* Mark it open. */
im->open[color] = 1;
}
void gdImageColorTransparent(gdImagePtr im, int color)
{
im->transparent = color;
}
void gdImageSetPixel(gdImagePtr im, int x, int y, int color)
{
int p;
switch(color) {
case gdStyled:
if (!im->style) {
/* Refuse to draw if no style is set. */
return;
} else {
p = im->style[im->stylePos++];
}
if (p != (gdTransparent)) {
gdImageSetPixel(im, x, y, p);
}
im->stylePos = im->stylePos % im->styleLength;
break;
case gdStyledBrushed:
if (!im->style) {
/* Refuse to draw if no style is set. */
return;
}
p = im->style[im->stylePos++];
if ((p != gdTransparent) && (p != 0)) {
gdImageSetPixel(im, x, y, gdBrushed);
}
im->stylePos = im->stylePos % im->styleLength;
break;
case gdBrushed:
gdImageBrushApply(im, x, y);
break;
case gdTiled:
gdImageTileApply(im, x, y);
break;
default:
if (gdImageBoundsSafe(im, x, y)) {
im->pixels[x][y] = color;
}
break;
}
}
static void gdImageBrushApply(gdImagePtr im, int x, int y)
{
int lx, ly;
int hy;
int hx;
int x1, y1, x2, y2;
int srcx, srcy;
if (!im->brush) {
return;
}
hy = gdImageSY(im->brush)/2;
y1 = y - hy;
y2 = y1 + gdImageSY(im->brush);
hx = gdImageSX(im->brush)/2;
x1 = x - hx;
x2 = x1 + gdImageSX(im->brush);
srcy = 0;
for (ly = y1; (ly < y2); ly++) {
srcx = 0;
for (lx = x1; (lx < x2); lx++) {
int p;
p = gdImageGetPixel(im->brush, srcx, srcy);
/* Allow for non-square brushes! */
if (p != gdImageGetTransparent(im->brush)) {
gdImageSetPixel(im, lx, ly,
im->brushColorMap[p]);
}
srcx++;
}
srcy++;
}
}
static void gdImageTileApply(gdImagePtr im, int x, int y)
{
int srcx, srcy;
int p;
if (!im->tile) {
return;
}
srcx = x % gdImageSX(im->tile);
srcy = y % gdImageSY(im->tile);
p = gdImageGetPixel(im->tile, srcx, srcy);
/* Allow for transparency */
if (p != gdImageGetTransparent(im->tile)) {
gdImageSetPixel(im, x, y,
im->tileColorMap[p]);
}
}
int gdImageGetPixel(gdImagePtr im, int x, int y)
{
if (gdImageBoundsSafe(im, x, y)) {
return im->pixels[x][y];
} else {
return 0;
}
}
/* Bresenham as presented in Foley & Van Dam */
void gdImageLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color)
{
int dx, dy, incr1, incr2, d, x, y, xend, yend, xdirflag, ydirflag;
dx = abs(x2-x1);
dy = abs(y2-y1);
if (dy <= dx) {
d = 2*dy - dx;
incr1 = 2*dy;
incr2 = 2 * (dy - dx);
if (x1 > x2) {
x = x2;
y = y2;
ydirflag = (-1);
xend = x1;
} else {
x = x1;
y = y1;
ydirflag = 1;
xend = x2;
}
gdImageSetPixel(im, x, y, color);
if (((y2 - y1) * ydirflag) > 0) {
while (x < xend) {
x++;
if (d <0) {
d+=incr1;
} else {
y++;
d+=incr2;
}
gdImageSetPixel(im, x, y, color);
}
} else {
while (x < xend) {
x++;
if (d <0) {
d+=incr1;
} else {
y--;
d+=incr2;
}
gdImageSetPixel(im, x, y, color);
}
}
} else {
d = 2*dx - dy;
incr1 = 2*dx;
incr2 = 2 * (dx - dy);
if (y1 > y2) {
y = y2;
x = x2;
yend = y1;
xdirflag = (-1);
} else {
y = y1;
x = x1;
yend = y2;
xdirflag = 1;
}
gdImageSetPixel(im, x, y, color);
if (((x2 - x1) * xdirflag) > 0) {
while (y < yend) {
y++;
if (d <0) {
d+=incr1;
} else {
x++;
d+=incr2;
}
gdImageSetPixel(im, x, y, color);
}
} else {
while (y < yend) {
y++;
if (d <0) {
d+=incr1;
} else {
x--;
d+=incr2;
}
gdImageSetPixel(im, x, y, color);
}
}
}
}
/* As above, plus dashing */
#define dashedSet \
{ \
dashStep++; \
if (dashStep == gdDashSize) { \
dashStep = 0; \
on = !on; \
} \
if (on) { \
gdImageSetPixel(im, x, y, color); \
} \
}
void gdImageDashedLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color)
{
int dx, dy, incr1, incr2, d, x, y, xend, yend, xdirflag, ydirflag;
int dashStep = 0;
int on = 1;
dx = abs(x2-x1);
dy = abs(y2-y1);
if (dy <= dx) {
d = 2*dy - dx;
incr1 = 2*dy;
incr2 = 2 * (dy - dx);
if (x1 > x2) {
x = x2;
y = y2;
ydirflag = (-1);
xend = x1;
} else {
x = x1;
y = y1;
ydirflag = 1;
xend = x2;
}
dashedSet;
if (((y2 - y1) * ydirflag) > 0) {
while (x < xend) {
x++;
if (d <0) {
d+=incr1;
} else {
y++;
d+=incr2;
}
dashedSet;
}
} else {
while (x < xend) {
x++;
if (d <0) {
d+=incr1;
} else {
y--;
d+=incr2;
}
dashedSet;
}
}
} else {
d = 2*dx - dy;
incr1 = 2*dx;
incr2 = 2 * (dx - dy);
if (y1 > y2) {
y = y2;
x = x2;
yend = y1;
xdirflag = (-1);
} else {
y = y1;
x = x1;
yend = y2;
xdirflag = 1;
}
dashedSet;
if (((x2 - x1) * xdirflag) > 0) {
while (y < yend) {
y++;
if (d <0) {
d+=incr1;
} else {
x++;
d+=incr2;
}
dashedSet;
}
} else {
while (y < yend) {
y++;
if (d <0) {
d+=incr1;
} else {
x--;
d+=incr2;
}
dashedSet;
}
}
}
}
int gdImageBoundsSafe(gdImagePtr im, int x, int y)
{
return (!(((y < 0) || (y >= im->sy)) ||
((x < 0) || (x >= im->sx))));
}
void gdImageChar(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char c, int color)
{
int cx, cy;
int px, py;
int fline;
cx = 0;
cy = 0;
if ((c < f->offset) || (c >= (f->offset + f->nchars))) {
return;
}
fline = (c - f->offset) * f->h * f->w;
for (py = y; (py < (y + f->h)); py++) {
for (px = x; (px < (x + f->w)); px++) {
if (f->data[fline + cy * f->w + cx]) {
gdImageSetPixel(im, px, py, color);
}
cx++;
}
cx = 0;
cy++;
}
}
void gdImageCharUp(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char c, int color)
{
int cx, cy;
int px, py;
int fline;
cx = 0;
cy = 0;
if ((c < f->offset) || (c >= (f->offset + f->nchars))) {
return;
}
fline = (c - f->offset) * f->h * f->w;
for (py = y; (py > (y - f->w)); py--) {
for (px = x; (px < (x + f->h)); px++) {
if (f->data[fline + cy * f->w + cx]) {
gdImageSetPixel(im, px, py, color);
}
cy++;
}
cy = 0;
cx++;
}
}
#if 0
void gdImageString(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char *s, int color)
{
int i;
int l;
l = strlen(s);
for (i=0; (i<l); i++) {
gdImageChar(im, f, x, y, s[i], color);
x += f->w;
}
}
#endif
#if 0
void gdImageStringUp(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char *s, int color)
{
int i;
int l;
l = strlen(s);
for (i=0; (i<l); i++) {
gdImageCharUp(im, f, x, y, s[i], color);
y -= f->w;
}
}
#endif
/* s and e are integers modulo 360 (degrees), with 0 degrees
being the rightmost extreme and degrees changing clockwise.
cx and cy are the center in pixels; w and h are the horizontal
and vertical diameter in pixels. Nice interface, but slow, since
I don't yet use Bresenham (I'm using an inefficient but
simple solution with too much work going on in it; generalizing
Bresenham to ellipses and partial arcs of ellipses is non-trivial,
at least for me) and there are other inefficiencies (small circles
do far too much work). */
void gdImageArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color)
{
int i;
int lx = 0, ly = 0;
int w2, h2;
w2 = w/2;
h2 = h/2;
while (e < s) {
e += 360;
}
for (i=s; (i <= e); i++) {
int x, y;
x = ((long)cost[i % 360] * (long)w2 / costScale) + cx;
y = ((long)sint[i % 360] * (long)h2 / sintScale) + cy;
if (i != s) {
gdImageLine(im, lx, ly, x, y, color);
}
lx = x;
ly = y;
}
}
#if 0
/* Bresenham octant code, which I should use eventually */
int x, y, d;
x = 0;
y = w;
d = 3-2*w;
while (x < y) {
gdImageSetPixel(im, cx+x, cy+y, color);
if (d < 0) {
d += 4 * x + 6;
} else {
d += 4 * (x - y) + 10;
y--;
}
x++;
}
if (x == y) {
gdImageSetPixel(im, cx+x, cy+y, color);
}
#endif
void gdImageFillToBorder(gdImagePtr im, int x, int y, int border, int color)
{
int lastBorder;
/* Seek left */
int leftLimit, rightLimit;
int i;
leftLimit = (-1);
if (border < 0) {
/* Refuse to fill to a non-solid border */
return;
}
for (i = x; (i >= 0); i--) {
if (gdImageGetPixel(im, i, y) == border) {
break;
}
gdImageSetPixel(im, i, y, color);
leftLimit = i;
}
if (leftLimit == (-1)) {
return;
}
/* Seek right */
rightLimit = x;
for (i = (x+1); (i < im->sx); i++) {
if (gdImageGetPixel(im, i, y) == border) {
break;
}
gdImageSetPixel(im, i, y, color);
rightLimit = i;
}
/* Look at lines above and below and start paints */
/* Above */
if (y > 0) {
lastBorder = 1;
for (i = leftLimit; (i <= rightLimit); i++) {
int c;
c = gdImageGetPixel(im, i, y-1);
if (lastBorder) {
if ((c != border) && (c != color)) {
gdImageFillToBorder(im, i, y-1,
border, color);
lastBorder = 0;
}
} else if ((c == border) || (c == color)) {
lastBorder = 1;
}
}
}
/* Below */
if (y < ((im->sy) - 1)) {
lastBorder = 1;
for (i = leftLimit; (i <= rightLimit); i++) {
int c;
c = gdImageGetPixel(im, i, y+1);
if (lastBorder) {
if ((c != border) && (c != color)) {
gdImageFillToBorder(im, i, y+1,
border, color);
lastBorder = 0;
}
} else if ((c == border) || (c == color)) {
lastBorder = 1;
}
}
}
}
void gdImageFill(gdImagePtr im, int x, int y, int color)
{
int lastBorder;
int old;
int leftLimit, rightLimit;
int i;
old = gdImageGetPixel(im, x, y);
if (color == gdTiled) {
/* Tile fill -- got to watch out! */
int p, tileColor;
int srcx, srcy;
if (!im->tile) {
return;
}
/* Refuse to flood-fill with a transparent pattern --
I can't do it without allocating another image */
if (gdImageGetTransparent(im->tile) != (-1)) {
return;
}
srcx = x % gdImageSX(im->tile);
srcy = y % gdImageSY(im->tile);
p = gdImageGetPixel(im->tile, srcx, srcy);
tileColor = im->tileColorMap[p];
if (old == tileColor) {
/* Nothing to be done */
return;
}
} else {
if (old == color) {
/* Nothing to be done */
return;
}
}
/* Seek left */
leftLimit = (-1);
for (i = x; (i >= 0); i--) {
if (gdImageGetPixel(im, i, y) != old) {
break;
}
gdImageSetPixel(im, i, y, color);
leftLimit = i;
}
if (leftLimit == (-1)) {
return;
}
/* Seek right */
rightLimit = x;
for (i = (x+1); (i < im->sx); i++) {
if (gdImageGetPixel(im, i, y) != old) {
break;
}
gdImageSetPixel(im, i, y, color);
rightLimit = i;
}
/* Look at lines above and below and start paints */
/* Above */
if (y > 0) {
lastBorder = 1;
for (i = leftLimit; (i <= rightLimit); i++) {
int c;
c = gdImageGetPixel(im, i, y-1);
if (lastBorder) {
if (c == old) {
gdImageFill(im, i, y-1, color);
lastBorder = 0;
}
} else if (c != old) {
lastBorder = 1;
}
}
}
/* Below */
if (y < ((im->sy) - 1)) {
lastBorder = 1;
for (i = leftLimit; (i <= rightLimit); i++) {
int c;
c = gdImageGetPixel(im, i, y+1);
if (lastBorder) {
if (c == old) {
gdImageFill(im, i, y+1, color);
lastBorder = 0;
}
} else if (c != old) {
lastBorder = 1;
}
}
}
}
#ifdef TEST_CODE
void gdImageDump(gdImagePtr im)
{
int i, j;
for (i=0; (i < im->sy); i++) {
for (j=0; (j < im->sx); j++) {
printf("%d", im->pixels[j][i]);
}
printf("\n");
}
}
#endif
/* Code drawn from ppmtogif.c, from the pbmplus package
**
** Based on GIFENCOD by David Rowley <mgardi@watdscu.waterloo.edu>. A
** Lempel-Zim compression based on "compress".
**
** Modified by Marcel Wijkstra <wijkstra@fwi.uva.nl>
**
** Copyright (C) 1989 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided "as is" without express or
** implied warranty.
**
** The Graphics Interchange Format(c) is the Copyright property of
** CompuServe Incorporated. GIF(sm) is a Service Mark property of
** CompuServe Incorporated.
*/
/*
* a code_int must be able to hold 2**GIFBITS values of type int, and also -1
*/
typedef int code_int;
#ifdef SIGNED_COMPARE_SLOW
typedef unsigned long int count_int;
typedef unsigned short int count_short;
#else /*SIGNED_COMPARE_SLOW*/
typedef long int count_int;
#endif /*SIGNED_COMPARE_SLOW*/
static int colorstobpp(int colors);
static void BumpPixel (void);
static int GIFNextPixel (gdImagePtr im);
static void GIFEncode (FILE *fp, int GWidth, int GHeight, int GInterlace, int Background, int Transparent, int BitsPerPixel, int *Red, int *Green, int *Blue, gdImagePtr im);
static void Putword (int w, FILE *fp);
static void compress (int init_bits, FILE *outfile, gdImagePtr im);
static void output (code_int code);
static void cl_block (void);
static void cl_hash (register count_int hsize);
static void char_init (void);
static void char_out (int c);
static void flush_char (void);
/* Allows for reuse */
static void init_statics(void);
void gdImageGif(gdImagePtr im, FILE *out)
{
int interlace, transparent, BitsPerPixel;
interlace = im->interlace;
transparent = im->transparent;
BitsPerPixel = colorstobpp(im->colorsTotal);
/* Clear any old values in statics strewn through the GIF code */
init_statics();
/* All set, let's do it. */
GIFEncode(
out, im->sx, im->sy, interlace, 0, transparent, BitsPerPixel,
im->red, im->green, im->blue, im);
}
static int
colorstobpp(int colors)
{
int bpp = 0;
if ( colors <= 2 )
bpp = 1;
else if ( colors <= 4 )
bpp = 2;
else if ( colors <= 8 )
bpp = 3;
else if ( colors <= 16 )
bpp = 4;
else if ( colors <= 32 )
bpp = 5;
else if ( colors <= 64 )
bpp = 6;
else if ( colors <= 128 )
bpp = 7;
else if ( colors <= 256 )
bpp = 8;
return bpp;
}
/*****************************************************************************
*
* GIFENCODE.C - GIF Image compression interface
*
* GIFEncode( FName, GHeight, GWidth, GInterlace, Background, Transparent,
* BitsPerPixel, Red, Green, Blue, gdImagePtr )
*
*****************************************************************************/
#define TRUE 1
#define FALSE 0
static int Width, Height;
static int curx, cury;
static long CountDown;
static int Pass = 0;
static int Interlace;
/*
* Bump the 'curx' and 'cury' to point to the next pixel
*/
static void
BumpPixel(void)
{
/*
* Bump the current X position
*/
++curx;
/*
* If we are at the end of a scan line, set curx back to the beginning
* If we are interlaced, bump the cury to the appropriate spot,
* otherwise, just increment it.
*/
if( curx == Width ) {
curx = 0;
if( !Interlace )
++cury;
else {
switch( Pass ) {
case 0:
cury += 8;
if( cury >= Height ) {
++Pass;
cury = 4;
}
break;
case 1:
cury += 8;
if( cury >= Height ) {
++Pass;
cury = 2;
}
break;
case 2:
cury += 4;
if( cury >= Height ) {
++Pass;
cury = 1;
}
break;
case 3:
cury += 2;
break;
}
}
}
}
/*
* Return the next pixel from the image
*/
static int
GIFNextPixel(gdImagePtr im)
{
int r;
if( CountDown == 0 )
return EOF;
--CountDown;
r = gdImageGetPixel(im, curx, cury);
BumpPixel();
return r;
}
/* public */
static void
GIFEncode(FILE *fp, int GWidth, int GHeight, int GInterlace, int Background, int Transparent, int BitsPerPixel, int *Red, int *Green, int *Blue, gdImagePtr im)
{
int B;
int RWidth, RHeight;
int LeftOfs, TopOfs;
int Resolution;
int ColorMapSize;
int InitCodeSize;
int i;
Interlace = GInterlace;
ColorMapSize = 1 << BitsPerPixel;
RWidth = Width = GWidth;
RHeight = Height = GHeight;
LeftOfs = TopOfs = 0;
Resolution = BitsPerPixel;
/*
* Calculate number of bits we are expecting
*/
CountDown = (long)Width * (long)Height;
/*
* Indicate which pass we are on (if interlace)
*/
Pass = 0;
/*
* The initial code size
*/
if( BitsPerPixel <= 1 )
InitCodeSize = 2;
else
InitCodeSize = BitsPerPixel;
/*
* Set up the current x and y position
*/
curx = cury = 0;
/*
* Write the Magic header
*/
fwrite( Transparent < 0 ? "GIF87a" : "GIF89a", 1, 6, fp );
/*
* Write out the screen width and height
*/
Putword( RWidth, fp );
Putword( RHeight, fp );
/*
* Indicate that there is a global colour map
*/
B = 0x80; /* Yes, there is a color map */
/*
* OR in the resolution
*/
B |= (Resolution - 1) << 5;
/*
* OR in the Bits per Pixel
*/
B |= (BitsPerPixel - 1);
/*
* Write it out
*/
fputc( B, fp );
/*
* Write out the Background colour
*/
fputc( Background, fp );
/*
* Byte of 0's (future expansion)
*/
fputc( 0, fp );
/*
* Write out the Global Colour Map
*/
for( i=0; i<ColorMapSize; ++i ) {
fputc( Red[i], fp );
fputc( Green[i], fp );
fputc( Blue[i], fp );
}
/*
* Write out extension for transparent colour index, if necessary.
*/
if ( Transparent >= 0 ) {
fputc( '!', fp );
fputc( 0xf9, fp );
fputc( 4, fp );
fputc( 1, fp );
fputc( 0, fp );
fputc( 0, fp );
fputc( (unsigned char) Transparent, fp );
fputc( 0, fp );
}
/*
* Write an Image separator
*/
fputc( ',', fp );
/*
* Write the Image header
*/
Putword( LeftOfs, fp );
Putword( TopOfs, fp );
Putword( Width, fp );
Putword( Height, fp );
/*
* Write out whether or not the image is interlaced
*/
if( Interlace )
fputc( 0x40, fp );
else
fputc( 0x00, fp );
/*
* Write out the initial code size
*/
fputc( InitCodeSize, fp );
/*
* Go and actually compress the data
*/
compress( InitCodeSize+1, fp, im );
/*
* Write out a Zero-length packet (to end the series)
*/
fputc( 0, fp );
/*
* Write the GIF file terminator
*/
fputc( ';', fp );
}
/*
* Write out a word to the GIF file
*/
static void
Putword(int w, FILE *fp)
{
fputc( w & 0xff, fp );
fputc( (w / 256) & 0xff, fp );
}
/***************************************************************************
*
* GIFCOMPR.C - GIF Image compression routines
*
* Lempel-Ziv compression based on 'compress'. GIF modifications by
* David Rowley (mgardi@watdcsu.waterloo.edu)
*
***************************************************************************/
/*
* General DEFINEs
*/
#define GIFBITS 12
#define HSIZE 5003 /* 80% occupancy */
#ifdef NO_UCHAR
typedef char char_type;
#else /*NO_UCHAR*/
typedef unsigned char char_type;
#endif /*NO_UCHAR*/
/*
*
* GIF Image compression - modified 'compress'
*
* Based on: compress.c - File compression ala IEEE Computer, June 1984.
*
* By Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
* Jim McKie (decvax!mcvax!jim)
* Steve Davies (decvax!vax135!petsd!peora!srd)
* Ken Turkowski (decvax!decwrl!turtlevax!ken)
* James A. Woods (decvax!ihnp4!ames!jaw)
* Joe Orost (decvax!vax135!petsd!joe)
*
*/
#include <ctype.h>
#define ARGVAL() (*++(*argv) || (--argc && *++argv))
static int n_bits; /* number of bits/code */
static int maxbits = GIFBITS; /* user settable max # bits/code */
static code_int maxcode; /* maximum code, given n_bits */
static code_int maxmaxcode = (code_int)1 << GIFBITS; /* should NEVER generate this code */
#ifdef COMPATIBLE /* But wrong! */
# define MAXCODE(n_bits) ((code_int) 1 << (n_bits) - 1)
#else /*COMPATIBLE*/
# define MAXCODE(n_bits) (((code_int) 1 << (n_bits)) - 1)
#endif /*COMPATIBLE*/
static count_int htab [HSIZE];
static unsigned short codetab [HSIZE];
#define HashTabOf(i) htab[i]
#define CodeTabOf(i) codetab[i]
static code_int hsize = HSIZE; /* for dynamic table sizing */
/*
* To save much memory, we overlay the table used by compress() with those
* used by decompress(). The tab_prefix table is the same size and type
* as the codetab. The tab_suffix table needs 2**GIFBITS characters. We
* get this from the beginning of htab. The output stack uses the rest
* of htab, and contains characters. There is plenty of room for any
* possible stack (stack used to be 8000 characters).
*/
#define tab_prefixof(i) CodeTabOf(i)
#define tab_suffixof(i) ((char_type*)(htab))[i]
#define de_stack ((char_type*)&tab_suffixof((code_int)1<<GIFBITS))
static code_int free_ent = 0; /* first unused entry */
/*
* block compression parameters -- after all codes are used up,
* and compression rate changes, start over.
*/
static int clear_flg = 0;
static int offset;
static long int in_count = 1; /* length of input */
static long int out_count = 0; /* # of codes output (for debugging) */
/*
* compress stdin to stdout
*
* Algorithm: use open addressing double hashing (no chaining) on the
* prefix code / next character combination. We do a variant of Knuth's
* algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
* secondary probe. Here, the modular division first probe is gives way
* to a faster exclusive-or manipulation. Also do block compression with
* an adaptive reset, whereby the code table is cleared when the compression
* ratio decreases, but after the table fills. The variable-length output
* codes are re-sized at this point, and a special CLEAR code is generated
* for the decompressor. Late addition: construct the table according to
* file size for noticeable speed improvement on small files. Please direct
* questions about this implementation to ames!jaw.
*/
static int g_init_bits;
static FILE* g_outfile;
static int ClearCode;
static int EOFCode;
static void
compress(int init_bits, FILE *outfile, gdImagePtr im)
{
register long fcode;
register code_int i /* = 0 */;
register int c;
register code_int ent;
register code_int disp;
register code_int hsize_reg;
register int hshift;
/*
* Set up the globals: g_init_bits - initial number of bits
* g_outfile - pointer to output file
*/
g_init_bits = init_bits;
g_outfile = outfile;
/*
* Set up the necessary values
*/
offset = 0;
out_count = 0;
clear_flg = 0;
in_count = 1;
maxcode = MAXCODE(n_bits = g_init_bits);
ClearCode = (1 << (init_bits - 1));
EOFCode = ClearCode + 1;
free_ent = ClearCode + 2;
char_init();
ent = GIFNextPixel( im );
hshift = 0;
for ( fcode = (long) hsize; fcode < 65536L; fcode *= 2L )
++hshift;
hshift = 8 - hshift; /* set hash code range bound */
hsize_reg = hsize;
cl_hash( (count_int) hsize_reg); /* clear hash table */
output( (code_int)ClearCode );
#ifdef SIGNED_COMPARE_SLOW
while ( (c = GIFNextPixel( im )) != (unsigned) EOF ) {
#else /*SIGNED_COMPARE_SLOW*/
while ( (c = GIFNextPixel( im )) != EOF ) { /* } */
#endif /*SIGNED_COMPARE_SLOW*/
++in_count;
fcode = (long) (((long) c << maxbits) + ent);
i = (((code_int)c << hshift) ^ ent); /* xor hashing */
if ( HashTabOf (i) == fcode ) {
ent = CodeTabOf (i);
continue;
} else if ( (long)HashTabOf (i) < 0 ) /* empty slot */
goto nomatch;
disp = hsize_reg - i; /* secondary hash (after G. Knott) */
if ( i == 0 )
disp = 1;
probe:
if ( (i -= disp) < 0 )
i += hsize_reg;
if ( HashTabOf (i) == fcode ) {
ent = CodeTabOf (i);
continue;
}
if ( (long)HashTabOf (i) > 0 )
goto probe;
nomatch:
output ( (code_int) ent );
++out_count;
ent = c;
#ifdef SIGNED_COMPARE_SLOW
if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
#else /*SIGNED_COMPARE_SLOW*/
if ( free_ent < maxmaxcode ) { /* } */
#endif /*SIGNED_COMPARE_SLOW*/
CodeTabOf (i) = free_ent++; /* code -> hashtable */
HashTabOf (i) = fcode;
} else
cl_block();
}
/*
* Put out the final code.
*/
output( (code_int)ent );
++out_count;
output( (code_int) EOFCode );
}
/*****************************************************************
* TAG( output )
*
* Output the given code.
* Inputs:
* code: A n_bits-bit integer. If == -1, then EOF. This assumes
* that n_bits =< (long)wordsize - 1.
* Outputs:
* Outputs code to the file.
* Assumptions:
* Chars are 8 bits long.
* Algorithm:
* Maintain a GIFBITS character long buffer (so that 8 codes will
* fit in it exactly). Use the VAX insv instruction to insert each
* code in turn. When the buffer fills up empty it and start over.
*/
static unsigned long cur_accum = 0;
static int cur_bits = 0;
static unsigned long masks[] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F,
0x001F, 0x003F, 0x007F, 0x00FF,
0x01FF, 0x03FF, 0x07FF, 0x0FFF,
0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
static void
output(code_int code)
{
cur_accum &= masks[ cur_bits ];
if( cur_bits > 0 )
cur_accum |= ((long)code << cur_bits);
else
cur_accum = code;
cur_bits += n_bits;
while( cur_bits >= 8 ) {
char_out( (unsigned int)(cur_accum & 0xff) );
cur_accum >>= 8;
cur_bits -= 8;
}
/*
* If the next entry is going to be too big for the code size,
* then increase it, if possible.
*/
if ( free_ent > maxcode || clear_flg ) {
if( clear_flg ) {
maxcode = MAXCODE (n_bits = g_init_bits);
clear_flg = 0;
} else {
++n_bits;
if ( n_bits == maxbits )
maxcode = maxmaxcode;
else
maxcode = MAXCODE(n_bits);
}
}
if( code == EOFCode ) {
/*
* At EOF, write the rest of the buffer.
*/
while( cur_bits > 0 ) {
char_out( (unsigned int)(cur_accum & 0xff) );
cur_accum >>= 8;
cur_bits -= 8;
}
flush_char();
fflush( g_outfile );
if( ferror( g_outfile ) )
return;
}
}
/*
* Clear out the hash table
*/
static void
cl_block (void) /* table clear for block compress */
{
cl_hash ( (count_int) hsize );
free_ent = ClearCode + 2;
clear_flg = 1;
output( (code_int)ClearCode );
}
static void
cl_hash(register count_int hsize) /* reset code table */
{
register count_int *htab_p = htab+hsize;
register long i;
register long m1 = -1;
i = hsize - 16;
do { /* might use Sys V memset(3) here */
*(htab_p-16) = m1;
*(htab_p-15) = m1;
*(htab_p-14) = m1;
*(htab_p-13) = m1;
*(htab_p-12) = m1;
*(htab_p-11) = m1;
*(htab_p-10) = m1;
*(htab_p-9) = m1;
*(htab_p-8) = m1;
*(htab_p-7) = m1;
*(htab_p-6) = m1;
*(htab_p-5) = m1;
*(htab_p-4) = m1;
*(htab_p-3) = m1;
*(htab_p-2) = m1;
*(htab_p-1) = m1;
htab_p -= 16;
} while ((i -= 16) >= 0);
for ( i += 16; i > 0; --i )
*--htab_p = m1;
}
/******************************************************************************
*
* GIF Specific routines
*
******************************************************************************/
/*
* Number of characters so far in this 'packet'
*/
static int a_count;
/*
* Set up the 'byte output' routine
*/
static void
char_init(void)
{
a_count = 0;
}
/*
* Define the storage for the packet accumulator
*/
static char accum[ 256 ];
/*
* Add a character to the end of the current packet, and if it is 254
* characters, flush the packet to disk.
*/
static void
char_out(int c)
{
accum[ a_count++ ] = c;
if( a_count >= 254 )
flush_char();
}
/*
* Flush the packet to disk, and reset the accumulator
*/
static void
flush_char(void)
{
if( a_count > 0 ) {
fputc( a_count, g_outfile );
fwrite( accum, 1, a_count, g_outfile );
a_count = 0;
}
}
static void init_statics(void) {
/* Some of these are properly initialized later. What I'm doing
here is making sure code that depends on C's initialization
of statics doesn't break when the code gets called more
than once. */
Width = 0;
Height = 0;
curx = 0;
cury = 0;
CountDown = 0;
Pass = 0;
Interlace = 0;
a_count = 0;
cur_accum = 0;
cur_bits = 0;
g_init_bits = 0;
g_outfile = 0;
ClearCode = 0;
EOFCode = 0;
free_ent = 0;
clear_flg = 0;
offset = 0;
in_count = 1;
out_count = 0;
hsize = HSIZE;
n_bits = 0;
maxbits = GIFBITS;
maxcode = 0;
maxmaxcode = (code_int)1 << GIFBITS;
}
/* +-------------------------------------------------------------------+ */
/* | Copyright 1990, 1991, 1993, David Koblas. (koblas@netcom.com) | */
/* | Permission to use, copy, modify, and distribute this software | */
/* | and its documentation for any purpose and without fee is hereby | */
/* | granted, provided that the above copyright notice appear in all | */
/* | copies and that both that copyright notice and this permission | */
/* | notice appear in supporting documentation. This software is | */
/* | provided "as is" without express or implied warranty. | */
/* +-------------------------------------------------------------------+ */
#define MAXCOLORMAPSIZE 256
#define TRUE 1
#define FALSE 0
#define CM_RED 0
#define CM_GREEN 1
#define CM_BLUE 2
#define MAX_LWZ_BITS 12
#define INTERLACE 0x40
#define LOCALCOLORMAP 0x80
#define BitSet(byte, bit) (((byte) & (bit)) == (bit))
#define ReadOK(file,buffer,len) (fread(buffer, len, 1, file) != 0)
#define LM_to_uint(a,b) (((b)<<8)|(a))
/* We may eventually want to use this information, but def it out for now */
#if 0
static struct {
unsigned int Width;
unsigned int Height;
unsigned char ColorMap[3][MAXCOLORMAPSIZE];
unsigned int BitPixel;
unsigned int ColorResolution;
unsigned int Background;
unsigned int AspectRatio;
} GifScreen;
#endif
static struct {
int transparent;
int delayTime;
int inputFlag;
int disposal;
} Gif89 = { -1, -1, -1, 0 };
static int ReadColorMap (FILE *fd, int number, unsigned char (*buffer)[256]);
static int DoExtension (FILE *fd, int label, int *Transparent);
static int GetDataBlock (FILE *fd, unsigned char *buf);
static int GetCode (FILE *fd, int code_size, int flag);
static int LWZReadByte (FILE *fd, int flag, int input_code_size);
static void ReadImage (gdImagePtr im, FILE *fd, int len, int height, unsigned char (*cmap)[256], int interlace, int ignore);
int ZeroDataBlock;
gdImagePtr
gdImageCreateFromGif(FILE *fd)
{
int imageNumber;
int BitPixel;
int ColorResolution;
int Background;
int AspectRatio;
int Transparent = (-1);
unsigned char buf[16];
unsigned char c;
unsigned char ColorMap[3][MAXCOLORMAPSIZE];
unsigned char localColorMap[3][MAXCOLORMAPSIZE];
int imw, imh;
int useGlobalColormap;
int bitPixel;
int imageCount = 0;
char version[4];
gdImagePtr im = 0;
ZeroDataBlock = FALSE;
imageNumber = 1;
if (! ReadOK(fd,buf,6)) {
return 0;
}
if (strncmp((char *)buf,"GIF",3) != 0) {
return 0;
}
strncpy(version, (char *)buf + 3, 3);
version[3] = '\0';
if ((strcmp(version, "87a") != 0) && (strcmp(version, "89a") != 0)) {
return 0;
}
if (! ReadOK(fd,buf,7)) {
return 0;
}
BitPixel = 2<<(buf[4]&0x07);
ColorResolution = (int) (((buf[4]&0x70)>>3)+1);
Background = buf[5];
AspectRatio = buf[6];
if (BitSet(buf[4], LOCALCOLORMAP)) { /* Global Colormap */
if (ReadColorMap(fd, BitPixel, ColorMap)) {
return 0;
}
}
for (;;) {
if (! ReadOK(fd,&c,1)) {
return 0;
}
if (c == ';') { /* GIF terminator */
int i;
if (imageCount < imageNumber) {
return 0;
}
/* Terminator before any image was declared! */
if (!im) {
return 0;
}
/* Check for open colors at the end, so
we can reduce colorsTotal and ultimately
BitsPerPixel */
for (i=((im->colorsTotal-1)); (i>=0); i--) {
if (im->open[i]) {
im->colorsTotal--;
} else {
break;
}
}
return im;
}
if (c == '!') { /* Extension */
if (! ReadOK(fd,&c,1)) {
return 0;
}
DoExtension(fd, c, &Transparent);
continue;
}
if (c != ',') { /* Not a valid start character */
continue;
}
++imageCount;
if (! ReadOK(fd,buf,9)) {
return 0;
}
useGlobalColormap = ! BitSet(buf[8], LOCALCOLORMAP);
bitPixel = 1<<((buf[8]&0x07)+1);
imw = LM_to_uint(buf[4],buf[5]);
imh = LM_to_uint(buf[6],buf[7]);
if (!(im = gdImageCreate(imw, imh))) {
return 0;
}
im->interlace = BitSet(buf[8], INTERLACE);
if (! useGlobalColormap) {
if (ReadColorMap(fd, bitPixel, localColorMap)) {
return 0;
}
ReadImage(im, fd, imw, imh, localColorMap,
BitSet(buf[8], INTERLACE),
imageCount != imageNumber);
} else {
ReadImage(im, fd, imw, imh,
ColorMap,
BitSet(buf[8], INTERLACE),
imageCount != imageNumber);
}
if (Transparent != (-1)) {
gdImageColorTransparent(im, Transparent);
}
}
}
static int
ReadColorMap(FILE *fd, int number, unsigned char (*buffer)[256])
{
int i;
unsigned char rgb[3];
for (i = 0; i < number; ++i) {
if (! ReadOK(fd, rgb, sizeof(rgb))) {
return TRUE;
}
buffer[CM_RED][i] = rgb[0] ;
buffer[CM_GREEN][i] = rgb[1] ;
buffer[CM_BLUE][i] = rgb[2] ;
}
return FALSE;
}
static int
DoExtension(FILE *fd, int label, int *Transparent)
{
static unsigned char buf[256];
switch (label) {
case 0xf9: /* Graphic Control Extension */
(void) GetDataBlock(fd, (unsigned char*) buf);
Gif89.disposal = (buf[0] >> 2) & 0x7;
Gif89.inputFlag = (buf[0] >> 1) & 0x1;
Gif89.delayTime = LM_to_uint(buf[1],buf[2]);
if ((buf[0] & 0x1) != 0)
*Transparent = buf[3];
while (GetDataBlock(fd, (unsigned char*) buf) != 0)
;
return FALSE;
default:
break;
}
while (GetDataBlock(fd, (unsigned char*) buf) != 0)
;
return FALSE;
}
static int
GetDataBlock(FILE *fd, unsigned char *buf)
{
unsigned char count;
if (! ReadOK(fd,&count,1)) {
return -1;
}
ZeroDataBlock = count == 0;
if ((count != 0) && (! ReadOK(fd, buf, count))) {
return -1;
}
return count;
}
static int
GetCode(FILE *fd, int code_size, int flag)
{
static unsigned char buf[280];
static int curbit, lastbit, done, last_byte;
int i, j, ret;
unsigned char count;
if (flag) {
curbit = 0;
lastbit = 0;
done = FALSE;
return 0;
}
if ( (curbit+code_size) >= lastbit) {
if (done) {
if (curbit >= lastbit) {
/* Oh well */
}
return -1;
}
buf[0] = buf[last_byte-2];
buf[1] = buf[last_byte-1];
if ((count = GetDataBlock(fd, &buf[2])) == 0)
done = TRUE;
last_byte = 2 + count;
curbit = (curbit - lastbit) + 16;
lastbit = (2+count)*8 ;
}
ret = 0;
for (i = curbit, j = 0; j < code_size; ++i, ++j)
ret |= ((buf[ i / 8 ] & (1 << (i % 8))) != 0) << j;
curbit += code_size;
return ret;
}
static int
LWZReadByte(FILE *fd, int flag, int input_code_size)
{
static int fresh = FALSE;
int code, incode;
static int code_size, set_code_size;
static int max_code, max_code_size;
static int firstcode, oldcode;
static int clear_code, end_code;
static int table[2][(1<< MAX_LWZ_BITS)];
static int stack[(1<<(MAX_LWZ_BITS))*2], *sp;
register int i;
if (flag) {
set_code_size = input_code_size;
code_size = set_code_size+1;
clear_code = 1 << set_code_size ;
end_code = clear_code + 1;
max_code_size = 2*clear_code;
max_code = clear_code+2;
GetCode(fd, 0, TRUE);
fresh = TRUE;
for (i = 0; i < clear_code; ++i) {
table[0][i] = 0;
table[1][i] = i;
}
for (; i < (1<<MAX_LWZ_BITS); ++i)
table[0][i] = table[1][0] = 0;
sp = stack;
return 0;
} else if (fresh) {
fresh = FALSE;
do {
firstcode = oldcode =
GetCode(fd, code_size, FALSE);
} while (firstcode == clear_code);
return firstcode;
}
if (sp > stack)
return *--sp;
while ((code = GetCode(fd, code_size, FALSE)) >= 0) {
if (code == clear_code) {
for (i = 0; i < clear_code; ++i) {
table[0][i] = 0;
table[1][i] = i;
}
for (; i < (1<<MAX_LWZ_BITS); ++i)
table[0][i] = table[1][i] = 0;
code_size = set_code_size+1;
max_code_size = 2*clear_code;
max_code = clear_code+2;
sp = stack;
firstcode = oldcode =
GetCode(fd, code_size, FALSE);
return firstcode;
} else if (code == end_code) {
int count;
unsigned char buf[260];
if (ZeroDataBlock)
return -2;
while ((count = GetDataBlock(fd, buf)) > 0)
;
if (count != 0)
return -2;
}
incode = code;
if (code >= max_code) {
*sp++ = firstcode;
code = oldcode;
}
while (code >= clear_code) {
*sp++ = table[1][code];
if (code == table[0][code]) {
/* Oh well */
}
code = table[0][code];
}
*sp++ = firstcode = table[1][code];
if ((code = max_code) <(1<<MAX_LWZ_BITS)) {
table[0][code] = oldcode;
table[1][code] = firstcode;
++max_code;
if ((max_code >= max_code_size) &&
(max_code_size < (1<<MAX_LWZ_BITS))) {
max_code_size *= 2;
++code_size;
}
}
oldcode = incode;
if (sp > stack)
return *--sp;
}
return code;
}
static void
ReadImage(gdImagePtr im, FILE *fd, int len, int height, unsigned char (*cmap)[256], int interlace, int ignore)
{
unsigned char c;
int v;
int xpos = 0, ypos = 0, pass = 0;
int i;
/* Stash the color map into the image */
for (i=0; (i<gdMaxColors); i++) {
im->red[i] = cmap[CM_RED][i];
im->green[i] = cmap[CM_GREEN][i];
im->blue[i] = cmap[CM_BLUE][i];
im->open[i] = 1;
}
/* Many (perhaps most) of these colors will remain marked open. */
im->colorsTotal = gdMaxColors;
/*
** Initialize the Compression routines
*/
if (! ReadOK(fd,&c,1)) {
return;
}
if (LWZReadByte(fd, TRUE, c) < 0) {
return;
}
/*
** If this is an "uninteresting picture" ignore it.
*/
if (ignore) {
while (LWZReadByte(fd, FALSE, c) >= 0)
;
return;
}
while ((v = LWZReadByte(fd,FALSE,c)) >= 0 ) {
/* This how we recognize which colors are actually used. */
if (im->open[v]) {
im->open[v] = 0;
}
gdImageSetPixel(im, xpos, ypos, v);
++xpos;
if (xpos == len) {
xpos = 0;
if (interlace) {
switch (pass) {
case 0:
case 1:
ypos += 8; break;
case 2:
ypos += 4; break;
case 3:
ypos += 2; break;
}
if (ypos >= height) {
++pass;
switch (pass) {
case 1:
ypos = 4; break;
case 2:
ypos = 2; break;
case 3:
ypos = 1; break;
default:
goto fini;
}
}
} else {
++ypos;
}
}
if (ypos >= height)
break;
}
fini:
if (LWZReadByte(fd,FALSE,c)>=0) {
/* Ignore extra */
}
}
void gdImageRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color)
{
gdImageLine(im, x1, y1, x2, y1, color);
gdImageLine(im, x1, y2, x2, y2, color);
gdImageLine(im, x1, y1, x1, y2, color);
gdImageLine(im, x2, y1, x2, y2, color);
}
void gdImageFilledRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color)
{
int x, y;
for (y=y1; (y<=y2); y++) {
for (x=x1; (x<=x2); x++) {
gdImageSetPixel(im, x, y, color);
}
}
}
void gdImageCopy(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h)
{
int c;
int x, y;
int tox, toy;
int i;
int colorMap[gdMaxColors];
for (i=0; (i<gdMaxColors); i++) {
colorMap[i] = (-1);
}
toy = dstY;
for (y=srcY; (y < (srcY + h)); y++) {
tox = dstX;
for (x=srcX; (x < (srcX + w)); x++) {
int nc;
c = gdImageGetPixel(src, x, y);
/* Added 7/24/95: support transparent copies */
if (gdImageGetTransparent(src) == c) {
tox++;
continue;
}
/* Have we established a mapping for this color? */
if (colorMap[c] == (-1)) {
/* If it's the same image, mapping is trivial */
if (dst == src) {
nc = c;
} else {
/* First look for an exact match */
nc = gdImageColorExact(dst,
src->red[c], src->green[c],
src->blue[c]);
}
if (nc == (-1)) {
/* No, so try to allocate it */
nc = gdImageColorAllocate(dst,
src->red[c], src->green[c],
src->blue[c]);
/* If we're out of colors, go for the
closest color */
if (nc == (-1)) {
nc = gdImageColorClosest(dst,
src->red[c], src->green[c],
src->blue[c]);
}
}
colorMap[c] = nc;
}
gdImageSetPixel(dst, tox, toy, colorMap[c]);
tox++;
}
toy++;
}
}
void gdImageCopyResized(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)
{
int c;
int x, y;
int tox, toy;
int ydest;
int i;
int colorMap[gdMaxColors];
/* Stretch vectors */
int *stx;
int *sty;
/* We only need to use floating point to determine the correct
stretch vector for one line's worth. */
double accum;
stx = (int *) xmalloc(sizeof(int) * srcW);
sty = (int *) xmalloc(sizeof(int) * srcH);
accum = 0.;
for (i=0; (i < srcW); i++) {
int got;
accum += (double)dstW/(double)srcW;
got = floor(accum);
stx[i] = got;
accum -= got;
}
accum = 0;
for (i=0; (i < srcH); i++) {
int got;
accum += (double)dstH/(double)srcH;
got = floor(accum);
sty[i] = got;
accum -= got;
}
for (i=0; (i<gdMaxColors); i++) {
colorMap[i] = (-1);
}
toy = dstY;
for (y=srcY; (y < (srcY + srcH)); y++) {
for (ydest=0; (ydest < sty[y-srcY]); ydest++) {
tox = dstX;
for (x=srcX; (x < (srcX + srcW)); x++) {
int nc;
if (!stx[x - srcX]) {
continue;
}
c = gdImageGetPixel(src, x, y);
/* Added 7/24/95: support transparent copies */
if (gdImageGetTransparent(src) == c) {
tox += stx[x-srcX];
continue;
}
/* Have we established a mapping for this color? */
if (colorMap[c] == (-1)) {
/* If it's the same image, mapping is trivial */
if (dst == src) {
nc = c;
} else {
/* First look for an exact match */
nc = gdImageColorExact(dst,
src->red[c], src->green[c],
src->blue[c]);
}
if (nc == (-1)) {
/* No, so try to allocate it */
nc = gdImageColorAllocate(dst,
src->red[c], src->green[c],
src->blue[c]);
/* If we're out of colors, go for the
closest color */
if (nc == (-1)) {
nc = gdImageColorClosest(dst,
src->red[c], src->green[c],
src->blue[c]);
}
}
colorMap[c] = nc;
}
for (i=0; (i < stx[x - srcX]); i++) {
gdImageSetPixel(dst, tox, toy, colorMap[c]);
tox++;
}
}
toy++;
}
}
free(stx);
free(sty);
}
int gdGetWord(int *result, FILE *in)
{
int r;
r = getc(in);
if (r == EOF) {
return 0;
}
*result = r << 8;
r = getc(in);
if (r == EOF) {
return 0;
}
*result += r;
return 1;
}
void gdPutWord(int w, FILE *out)
{
putc((unsigned char)(w >> 8), out);
putc((unsigned char)(w & 0xFF), out);
}
int gdGetByte(int *result, FILE *in)
{
int r;
r = getc(in);
if (r == EOF) {
return 0;
}
*result = r;
return 1;
}
gdImagePtr gdImageCreateFromGd(FILE *in)
{
int sx, sy;
int x, y;
int i;
gdImagePtr im;
if (!gdGetWord(&sx, in)) {
goto fail1;
}
if (!gdGetWord(&sy, in)) {
goto fail1;
}
im = gdImageCreate(sx, sy);
if (!gdGetByte(&im->colorsTotal, in)) {
goto fail2;
}
if (!gdGetWord(&im->transparent, in)) {
goto fail2;
}
if (im->transparent == 257) {
im->transparent = (-1);
}
for (i=0; (i<gdMaxColors); i++) {
if (!gdGetByte(&im->red[i], in)) {
goto fail2;
}
if (!gdGetByte(&im->green[i], in)) {
goto fail2;
}
if (!gdGetByte(&im->blue[i], in)) {
goto fail2;
}
}
for (y=0; (y<sy); y++) {
for (x=0; (x<sx); x++) {
int ch;
ch = getc(in);
if (ch == EOF) {
gdImageDestroy(im);
return 0;
}
im->pixels[x][y] = ch;
}
}
return im;
fail2:
gdImageDestroy(im);
fail1:
return 0;
}
void gdImageGd(gdImagePtr im, FILE *out)
{
int x, y;
int i;
int trans;
gdPutWord(im->sx, out);
gdPutWord(im->sy, out);
putc((unsigned char)im->colorsTotal, out);
trans = im->transparent;
if (trans == (-1)) {
trans = 257;
}
gdPutWord(trans, out);
for (i=0; (i<gdMaxColors); i++) {
putc((unsigned char)im->red[i], out);
putc((unsigned char)im->green[i], out);
putc((unsigned char)im->blue[i], out);
}
for (y=0; (y < im->sy); y++) {
for (x=0; (x < im->sx); x++) {
putc((unsigned char)im->pixels[x][y], out);
}
}
}
gdImagePtr
gdImageCreateFromXbm(FILE *fd)
{
gdImagePtr im;
int bit;
int w, h;
int bytes;
int ch;
int i, x, y;
char *sp;
char s[161];
if (!fgets(s, 160, fd)) {
return 0;
}
sp = &s[0];
/* Skip #define */
sp = strchr(sp, ' ');
if (!sp) {
return 0;
}
/* Skip width label */
sp++;
sp = strchr(sp, ' ');
if (!sp) {
return 0;
}
/* Get width */
w = atoi(sp + 1);
if (!w) {
return 0;
}
if (!fgets(s, 160, fd)) {
return 0;
}
sp = s;
/* Skip #define */
sp = strchr(sp, ' ');
if (!sp) {
return 0;
}
/* Skip height label */
sp++;
sp = strchr(sp, ' ');
if (!sp) {
return 0;
}
/* Get height */
h = atoi(sp + 1);
if (!h) {
return 0;
}
/* Skip declaration line */
if (!fgets(s, 160, fd)) {
return 0;
}
bytes = (w * h / 8) + 1;
im = gdImageCreate(w, h);
gdImageColorAllocate(im, 255, 255, 255);
gdImageColorAllocate(im, 0, 0, 0);
x = 0;
y = 0;
for (i=0; (i < bytes); i++) {
char h[3];
int b;
/* Skip spaces, commas, CRs, 0x */
while(1) {
ch = getc(fd);
if (ch == EOF) {
goto fail;
}
if (ch == 'x') {
break;
}
}
/* Get hex value */
ch = getc(fd);
if (ch == EOF) {
goto fail;
}
h[0] = ch;
ch = getc(fd);
if (ch == EOF) {
goto fail;
}
h[1] = ch;
h[2] = '\0';
sscanf(h, "%x", &b);
for (bit = 1; (bit <= 128); (bit = bit << 1)) {
gdImageSetPixel(im, x++, y, (b & bit) ? 1 : 0);
if (x == im->sx) {
x = 0;
y++;
if (y == im->sy) {
return im;
}
/* Fix 8/8/95 */
break;
}
}
}
/* Shouldn't happen */
fprintf(stderr, "Error: bug in gdImageCreateFromXbm!\n");
return 0;
fail:
gdImageDestroy(im);
return 0;
}
void gdImagePolygon(gdImagePtr im, gdPointPtr p, int n, int c)
{
int i;
int lx, ly;
if (!n) {
return;
}
lx = p->x;
ly = p->y;
gdImageLine(im, lx, ly, p[n-1].x, p[n-1].y, c);
for (i=1; (i < n); i++) {
p++;
gdImageLine(im, lx, ly, p->x, p->y, c);
lx = p->x;
ly = p->y;
}
}
int gdCompareInt(const void *a, const void *b);
void gdImageFilledPolygon(gdImagePtr im, gdPointPtr p, int n, int c)
{
int i;
int y;
int y1, y2;
int ints;
if (!n) {
return;
}
if (!im->polyAllocated) {
im->polyInts = (int *) xmalloc(sizeof(int) * n);
im->polyAllocated = n;
}
if (im->polyAllocated < n) {
while (im->polyAllocated < n) {
im->polyAllocated *= 2;
}
im->polyInts = (int *) xrealloc(im->polyInts,
sizeof(int) * im->polyAllocated);
}
y1 = p[0].y;
y2 = p[0].y;
for (i=1; (i < n); i++) {
if (p[i].y < y1) {
y1 = p[i].y;
}
if (p[i].y > y2) {
y2 = p[i].y;
}
}
for (y=y1; (y <= y2); y++) {
int interLast = 0;
int dirLast = 0;
int interFirst = 1;
ints = 0;
for (i=0; (i <= n); i++) {
int x1, x2;
int y1, y2;
int dir;
int ind1, ind2;
int lastInd1 = 0;
if ((i == n) || (!i)) {
ind1 = n-1;
ind2 = 0;
} else {
ind1 = i-1;
ind2 = i;
}
y1 = p[ind1].y;
y2 = p[ind2].y;
if (y1 < y2) {
y1 = p[ind1].y;
y2 = p[ind2].y;
x1 = p[ind1].x;
x2 = p[ind2].x;
dir = -1;
} else if (y1 > y2) {
y2 = p[ind1].y;
y1 = p[ind2].y;
x2 = p[ind1].x;
x1 = p[ind2].x;
dir = 1;
} else {
/* Horizontal; just draw it */
gdImageLine(im,
p[ind1].x, y1,
p[ind2].x, y1,
c);
continue;
}
if ((y >= y1) && (y <= y2)) {
int inter =
(y-y1) * (x2-x1) / (y2-y1) + x1;
/* Only count intersections once
except at maxima and minima. Also,
if two consecutive intersections are
endpoints of the same horizontal line
that is not at a maxima or minima,
discard the leftmost of the two. */
if (!interFirst) {
if ((p[ind1].y == p[lastInd1].y) &&
(p[ind1].x != p[lastInd1].x)) {
if (dir == dirLast) {
if (inter > interLast) {
/* Replace the old one */
im->polyInts[ints] = inter;
} else {
/* Discard this one */
}
continue;
}
}
if (inter == interLast) {
if (dir == dirLast) {
continue;
}
}
}
if (i > 0) {
im->polyInts[ints++] = inter;
}
lastInd1 = i;
dirLast = dir;
interLast = inter;
interFirst = 0;
}
}
qsort(im->polyInts, ints, sizeof(int), gdCompareInt);
for (i=0; (i < (ints-1)); i+=2) {
gdImageLine(im, im->polyInts[i], y,
im->polyInts[i+1], y, c);
}
}
}
int gdCompareInt(const void *a, const void *b)
{
return (*(const int *)a) - (*(const int *)b);
}
void gdImageSetStyle(gdImagePtr im, int *style, int noOfPixels)
{
if (im->style) {
free(im->style);
}
im->style = (int *)
xmalloc(sizeof(int) * noOfPixels);
memcpy(im->style, style, sizeof(int) * noOfPixels);
im->styleLength = noOfPixels;
im->stylePos = 0;
}
void gdImageSetBrush(gdImagePtr im, gdImagePtr brush)
{
int i;
im->brush = brush;
for (i=0; (i < gdImageColorsTotal(brush)); i++) {
int index;
index = gdImageColorExact(im,
gdImageRed(brush, i),
gdImageGreen(brush, i),
gdImageBlue(brush, i));
if (index == (-1)) {
index = gdImageColorAllocate(im,
gdImageRed(brush, i),
gdImageGreen(brush, i),
gdImageBlue(brush, i));
if (index == (-1)) {
index = gdImageColorClosest(im,
gdImageRed(brush, i),
gdImageGreen(brush, i),
gdImageBlue(brush, i));
}
}
im->brushColorMap[i] = index;
}
}
void gdImageSetTile(gdImagePtr im, gdImagePtr tile)
{
int i;
im->tile = tile;
for (i=0; (i < gdImageColorsTotal(tile)); i++) {
int index;
index = gdImageColorExact(im,
gdImageRed(tile, i),
gdImageGreen(tile, i),
gdImageBlue(tile, i));
if (index == (-1)) {
index = gdImageColorAllocate(im,
gdImageRed(tile, i),
gdImageGreen(tile, i),
gdImageBlue(tile, i));
if (index == (-1)) {
index = gdImageColorClosest(im,
gdImageRed(tile, i),
gdImageGreen(tile, i),
gdImageBlue(tile, i));
}
}
im->tileColorMap[i] = index;
}
}
void gdImageInterlace(gdImagePtr im, int interlaceArg)
{
im->interlace = interlaceArg;
}
/*****************/
/*****************/
/*****************/
/*
* Byte Conversion
*/
unsigned long gdIntern_convert_long(unsigned long z)
{
#ifndef WORDS_BIGENDIAN /* { */
return(z);
#else /* }{ */
return(((z & 0xff000000L) >> 24) |
((z & 0x00ff0000L) >> 8) |
((z & 0x0000ff00L) << 8) |
((z & 0x000000ffL) << 24));
#endif /* } */
}
gdFontPtr gdFontCreateFromGdf(FILE *in)
{
gdFontPtr pFont;
unsigned int int32[10];
unsigned long i;
if ((pFont = xmalloc(sizeof(gdFont))) == NULL)
return(NULL);
pFont->name = NULL;
if (fread(int32, sizeof(unsigned int), 5, in) == 0)
return(NULL);
pFont->nchars = gdIntern_convert_long(int32[1]);
pFont->offset = gdIntern_convert_long(int32[2]);
pFont->w = gdIntern_convert_long(int32[3]);
pFont->h = gdIntern_convert_long(int32[4]);
if ((pFont->width = xmalloc(sizeof(unsigned int)*pFont->nchars)) == NULL)
return(NULL);
if (fread(pFont->width, sizeof(unsigned int), pFont->nchars, in) == 0)
return(NULL);
for(i = 0;i<pFont->nchars;i++) pFont->width[i] = gdIntern_convert_long(pFont->width[i]);
if ((pFont->data = xmalloc(sizeof(char)*pFont->w*pFont->h*pFont->nchars)) == NULL)
return(NULL);
if (fread(pFont->data, sizeof(char), pFont->w*pFont->h*pFont->nchars, in) == 0)
return(NULL);
return(pFont);
}
gdFontPtr gdFontCreateFromGdfFile(char *filename)
{
FILE *fp;
char *cp;
int gzipped = 0;
char cmd[1024];
gdFontPtr pFont;
if ((cp = strrchr(filename, '.')) != NULL) {
if (strcmp(cp+1, "gz") == 0) {
gzipped = 1;
}
}
if (gzipped) {
sprintf(cmd, "gunzip -c %s", filename);
if ((fp = popen(cmd, "r")) == NULL)
return (NULL);
pFont = gdFontCreateFromGdf(fp);
pclose(fp);
}
else {
if ((fp = fopen(filename, "r")) == NULL)
return (NULL);
pFont = gdFontCreateFromGdf(fp);
fclose(fp);
}
return(pFont);
}
static char gdFontPath[2048]; /* preserve space */
static int gdFontPathIsInitialized = 0; /* is initialised */
void gdFontPathSet(char *path)
{
if (path != NULL) {
if (strcmp(path, "") != 0) {
strcpy(gdFontPath, path);
gdFontPathIsInitialized = 1;
}
}
return;
}
gdFontPtr gdFontLoadSmart(char *name)
{
int akt;
if(FontTable == NULL) { return(gdFontLoad(name)); }
for(akt = 0;FontTable[akt] != NULL;akt++)
{
if(strcmp(FontTable[akt]->FontName,name)==0) return(FontTable[akt]->FontPtr);
}
return(gdFontLoad(name));
}
unsigned char gd_make_entry_fonttable(char *name, gdFontPtr FontPtr)
{
int akt;
int anz_eintraege;
if (FontTable == NULL)
anz_eintraege = 0;
else {
for(akt = 0;FontTable[akt] != NULL;akt++) {
if(strcmp(FontTable[akt]->FontName,name)==0) return(1);
}
anz_eintraege = akt;
}
FontTable = (FontEntry **) xrealloc(FontTable,(anz_eintraege+2) * sizeof(FontEntry *));
FontTable[anz_eintraege] = (FontEntry *) xmalloc(sizeof(FontEntry));
FontTable[anz_eintraege]->FontName = strdup(name);
FontTable[anz_eintraege]->FontPtr = FontPtr;
FontTable[anz_eintraege+1] = NULL;
return(0);
}
gdFontPtr gdFontLoad(char *name)
{
struct stat statbuf;
char path[2024];
char filename[2024];
char *cpS;
char *cpE;
gdFontPtr pFont;
path[0] = '\0';
if (gdFontPathIsInitialized)
strcpy(path, gdFontPath);
sprintf(path+strlen(path), ":%s", GDFPATH);
cpE = path;
while (1) {
cpS = cpE;
for (; *cpS == ':'; )
cpS++;
for (cpE = cpS; *cpE != ':' && *cpE != '\0'; )
cpE++;
*cpE = '\0';
if (cpS == cpE)
break;
sprintf(filename, "%s/%s.gdf", cpS, name);
if (stat(filename, &statbuf) == 0) {
pFont = gdFontCreateFromGdfFile(filename);
if (pFont) {
pFont->name = strdup(name);
gd_make_entry_fonttable(name,pFont);
}
return (pFont);
}
sprintf(filename, "%s/%s.gdf.gz", cpS, name);
if (stat(filename, &statbuf) == 0) {
pFont = gdFontCreateFromGdfFile(filename);
if (pFont) {
pFont->name = strdup(name);
gd_make_entry_fonttable(name,pFont);
}
return (pFont);
}
*cpE = ':';
}
return (NULL);
}
gdFontPtr *gdFontLoadList(char *fontname[])
{
int i, j;
gdFontPtr *list;
gdFontPtr fp;
for (i = 0; fontname[i] != NULL; i++)
;
list = (gdFontPtr *)xmalloc(sizeof(gdFontPtr)*(i+2));
for (i = 0, j = 0; fontname[i] != NULL; i++) {
fp = gdFontLoad(fontname[i]);
if (fp != NULL) {
list[j++] = fp;
}
}
list[j++] = NULL;
return (list);
}
gdFontPtr gdFontFind(gdFontPtr list[], char *fontname)
{
int i;
for (i = 0; list[i] != NULL; i++) {
if (strcmp(list[i]->name, fontname) == 0)
return(list[i]);
}
return (gdFontLarge);
}
void gdImageCharDown(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char c, int color)
{
int cx, cy;
int px, py;
int fline;
cx = 0;
cy = f->h;
if ((c < f->offset) || (c >= (f->offset + f->nchars))) {
return;
}
fline = (c - f->offset) * f->h * f->w;
for (py = y - f->w; py < y; py++) {
for (px = x; px < x + f->h; px++) {
if (f->data[fline + cy * f->w + cx]) {
gdImageSetPixel(im, px, py, color);
}
cy--;
}
cy = f->h;
cx++;
}
}
void gdImageString(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char *s, int color)
{
int i;
int l;
l = strlen(s);
if (f->width == NULL) {
/* non proportional font */
for (i=0; (i<l); i++)
{
gdImageChar(im, f, x, y, s[i], color);
x += f->w;
}
}
else {
/* proportional font */
for (i=0; (i<l); i++) {
if (s[i] == ' ')
{
x += (f->width)['-'];
}
else
{
gdImageChar(im, f, x, y, s[i], color);
x += (f->width)[s[i]];
}
}
}
return;
}
int gdImageStringWidth(gdFontPtr f, unsigned char *s)
{
int i, l;
int w;
w = 0;
l = strlen(s);
if (f->width == NULL) {
/* non proportional font */
for (i=0; (i<l); i++)
w += f->w;
}
else {
/* proportional font */
for (i=0; (i<l); i++) {
if (s[i] == ' ')
w += (f->width)['-'];
else
w += (f->width)[s[i]];
}
}
return (w);
}
void gdImageStringUp(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char *s, int color)
{
int i;
int l;
char zeichen[2];
l = strlen(s);
for (i=0; (i<l); i++) {
if(s[i] == ' ')
zeichen[0] = 0x20;
else
{
gdImageCharUp(im, f, x, y, s[i], color);
zeichen[0] = s[i];
}
zeichen[1] = 0;
y -= gdImageStringWidth(f, zeichen);
}
}
void gdImageStringDown(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char *s, int color)
{
int i;
int l;
char zeichen[2];
l = strlen(s);
for (i=0; i<l; i++) {
if(s[i] == ' ')
zeichen[0] = 0x20;
else
{
gdImageCharDown(im, f, x, y, s[i], color);
zeichen[0] = s[i];
}
zeichen[1] = 0;
y += gdImageStringWidth(f, zeichen);
}
}
void gdImageFilledEllipse(gdImagePtr im, int cx, int cy, int w, int h,
int color, int fillcolor)
{
int x = 0;
int y = h / 2;
long a = w / 2;
long b = h / 2;
long Asquared = a * a;
long TwoAsquared = 2 * Asquared;
long Bsquared = b * b;
long TwoBsquared = 2 * Bsquared;
long d;
long dx, dy;
d = Bsquared - Asquared * b + Asquared / 4L;
dx = 0;
dy = TwoAsquared * b;
while(dx < dy)
{
gdImageLine(im, cx-x, cy-y, cx+x, cy-y, fillcolor);
gdImageLine(im, cx-x, cy+y, cx+x, cy+y, fillcolor);
gdImageSetPixel(im, cx-x, cy-y, color);
gdImageSetPixel(im, cx-x, cy+y, color);
gdImageSetPixel(im, cx+x, cy-y, color);
gdImageSetPixel(im, cx+x, cy+y, color);
if(d > 0L)
{
y--;
dy -= TwoAsquared;
d -= dy;
}
x++;
dx += TwoBsquared;
d += Bsquared + dx;
}
d += (3L * (Asquared - Bsquared) / 2L - (dx+dy)) / 2L;
while(y >= 0)
{
gdImageLine(im, cx-x, cy-y, cx+x, cy-y, fillcolor);
gdImageLine(im, cx-x, cy+y, cx+x, cy+y, fillcolor);
gdImageSetPixel(im, cx-x, cy-y, color);
gdImageSetPixel(im, cx-x, cy+y, color);
gdImageSetPixel(im, cx+x, cy-y, color);
gdImageSetPixel(im, cx+x, cy+y, color);
if(d < 0L)
{
x++;
dx += TwoBsquared;
d += dx;
}
y--;
dy -= TwoAsquared;
d += Asquared - dy;
}
}
/*EOF*/
|