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 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948
|
/*
** BMP.C For WILLUSLIB, set of routines to deal with 8-bit and 24-bit
** bitmap structures, including functions that read and
** write BMP files, PNG files, and JPEG files.
**
** Part of willus.com general purpose C code library.
**
** Copyright (C) 2018 http://willus.com
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
*/
#include "willus.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#ifdef HAVE_PNG_LIB
#include <zlib.h>
#include <png.h>
#endif
#ifdef HAVE_JPEG_LIB
#include <jpeglib.h>
#endif
#ifdef HAVE_JASPER_LIB
#include <jasper.h>
#endif
#define BOUND(x,xmin,xmax) if ((x)<(xmin)) (x)=(xmin); else { if ((x)>(xmax)) (x)=(xmax); }
#define RGBSET24(bmp,ptr,r,g,b) \
if (bmp->type==WILLUSBITMAP_TYPE_NATIVE) \
{ \
ptr[0]=r; \
ptr[1]=g; \
ptr[2]=b; \
} \
else \
{ \
ptr[2]=r; \
ptr[1]=g; \
ptr[0]=b; \
}
#define RGBGET(bmp,ptr,r,g,b) \
if (bmp->bpp==8) \
{ \
r=bmp->red[ptr[0]]; \
g=bmp->green[ptr[0]]; \
b=bmp->blue[ptr[0]]; \
} \
else if (bmp->type==WILLUSBITMAP_TYPE_NATIVE) \
{ \
r=ptr[0]; \
g=ptr[1]; \
b=ptr[2]; \
} \
else \
{ \
r=ptr[2]; \
g=ptr[1]; \
b=ptr[0]; \
}
#define RGBGETINCPTR(bmp,ptr,r,g,b) \
if (bmp->bpp==8) \
{ \
r=bmp->red[ptr[0]]; \
g=bmp->green[ptr[0]]; \
b=bmp->blue[ptr[0]]; \
ptr++; \
} \
else if (bmp->type==WILLUSBITMAP_TYPE_NATIVE) \
{ \
r=ptr[0]; \
g=ptr[1]; \
b=ptr[2]; \
ptr+=3; \
} \
else \
{ \
r=ptr[2]; \
g=ptr[1]; \
b=ptr[0]; \
ptr+=3; \
}
static double willusbmp_dpi=150.;
static int willusbmp_pageno=-1;
static char *cnames[]={"red","green","blue","magenta","cyan","yellow",
"grey","black","white",""};
static char *acodes[]={ANSI_RED,ANSI_GREEN,ANSI_BLUE,ANSI_MAGENTA,
ANSI_CYAN,ANSI_YELLOW,ANSI_NORMAL,ANSI_NORMAL,
ANSI_WHITE};
static double bmp_dpi=-1.;
#ifdef HAVE_JPEG_LIB
static int bmp_std_huffman_tables=0;
static void my_error_exit(j_common_ptr cinfo);
#endif
static int bmp8_write(WILLUSBITMAP *bmap,char *filename,FILE *out);
static int bmp24_write(WILLUSBITMAP *bmap,char *filename,FILE *out);
static void insert_int32lsbmsb(char *a,int x);
static int retrieve_int32lsbmsb(char *a);
static void get_file_ext(char *fileext,char *filename);
static void bmp_resample_1(double *tempbmp,WILLUSBITMAP *src,double x1,double y1,
double x2,double y2,int newwidth,int newheight,
double *temprow,int color);
static void resample_1d(double *dst,double *src,double x1,double x2,int n);
static double resample_single(double *y,double x1,double x2);
static void bmp_resample_1_fixed_point(int *tempbmp,WILLUSBITMAP *src,int x1_fp,int y1_fp,
int x2_fp,int y2_fp,int newwidth,int newheight,
int *temprow,int color);
static void resample_1d_fixed_point(int *dst,int *src,int x1_fp,int x2_fp,int n);
static double resample_single_fixed_point(int *y,int x1_fp,int x2_fp);
#ifdef HAVE_PNG_LIB
static void bmp_read_png_from_memory(png_structp png_ptr,void *buf,int nbytes);
#endif
static void new_rgb(int *dpc,int *spc,int *dbgc,int *dfgc,int *sbgc,int *sfgc);
static int jpeg_write_comments(FILE *out,char *buf);
static int jpeg_read2(FILE *f,int *x);
static int jpeg_write2(FILE *f,int x);
static void find_most_common_color(double ***hist,int n,int *r,int *g,int *b,
double *percent);
static int bmp_uniform_row(WILLUSBITMAP *bmp,int row);
static int bmp_uniform_col(WILLUSBITMAP *bmp,int col);
static void bmp_color_xform8(WILLUSBITMAP *dest,WILLUSBITMAP *src,unsigned char *newval);
static void bmp_one_component_erode(WILLUSBITMAP *src,WILLUSBITMAP *dst,int offsetplane,int bytesperpixel);
static void bmp_apply_filter_gray(WILLUSBITMAP *dest,WILLUSBITMAP *src,
double **filter,int ncols,int nrows);
static double bmp_row_by_row_stdev(WILLUSBITMAP *bmp,int ccount,int whitethresh,
double theta_radians);
static int pixval_dither(int pv,int n,int maxsrc,int maxdst,int x0,int y0);
static int dither_rec(int bits,int x0,int y0);
static int pcl_get_resolution(char *pclbuf,int n,int *w,int *h);
static int pcl_next_raster_row(char *pclbuf,int n,int *index);
double bmp_get_dpi(void)
{
return(bmp_dpi);
}
double bmp_last_read_dpi(void)
{
return(bmp_dpi);
}
void bmp_set_dpi(double dpi)
{
bmp_dpi=dpi;
}
void bmp_set_pdf_dpi(double dpi)
{
willusbmp_dpi=dpi;
}
double bmp_get_pdf_dpi(void)
{
return(willusbmp_dpi);
}
void bmp_set_pdf_pageno(int pageno)
{
willusbmp_pageno=pageno;
}
int bmp_get_pdf_pageno(void)
{
return(willusbmp_pageno);
}
/*
** Quality is ignored if not JPEG.
*/
int bmp_write(WILLUSBITMAP *bmap,char *filename,FILE *out,int quality)
{
char fileext[16];
get_file_ext(fileext,filename);
if (!stricmp(fileext,"ico"))
return(bmp_write_ico(bmap,filename,out));
#ifdef HAVE_PNG_LIB
if (!stricmp(fileext,"png"))
return(bmp_write_png(bmap,filename,out));
#endif
if (!stricmp(fileext,"pdf"))
{
PDFFILE _pdf,*pdf;
pdf=&_pdf;
if (pdffile_init(pdf,filename,1)!=NULL)
{
pdffile_add_bitmap(pdf,bmap,willusbmp_dpi,quality,0);
pdffile_finish(pdf,NULL,NULL,NULL,NULL);
pdffile_close(pdf);
return(0);
}
else
{
fprintf(out,"pdffile_init(%s) failed.\n",filename);
return(-10);
}
}
#ifdef HAVE_JPEG_LIB
if (!stricmp(fileext,"jpg") || !stricmp(fileext,"jpeg"))
{
if (bmap->bpp!=24)
{
if (out!=NULL)
fprintf(out,"Can only write JPEG output for 24-bit bitmaps.\n");
return(-10);
}
return(bmp_write_jpeg(bmap,filename,quality,out));
}
#endif
if (stricmp(fileext,"bmp") && out!=NULL)
fprintf(out,"Warning: file %s has no extension. Treating as BMP file.\n",
filename);
if (bmap->bpp==24)
return(bmp24_write(bmap,filename,out));
return(bmp8_write(bmap,filename,out));
}
int bmp_write_ico(WILLUSBITMAP *bmp,char *filename,FILE *out)
{
FILE *f;
char a[4];
int i,j,nb;
nb=(bmp->width+31)/8;
while (nb&3)
nb--;
nb*=bmp->height;
f=wfile_fopen_utf8(filename,"wb");
a[0]=a[1]=a[2]=a[3]=0;
fwrite(a,1,2,f);
a[0]=1;
fwrite(a,1,2,f);
fwrite(a,1,2,f);
a[0]=bmp->width;
a[1]=bmp->height;
a[2]=0; // True color
a[3]=0;
fwrite(a,4,1,f);
a[0]=1; // N planes
a[1]=0;
a[2]=24; // Bits-per-pixel
a[3]=0;
fwrite(a,4,1,f);
i=40+bmp->width*bmp->height*3+nb;
fwrite(&i,4,1,f);
i=22;
fwrite(&i,4,1,f);
i=40;
fwrite(&i,4,1,f);
i=bmp->width;
fwrite(&i,4,1,f);
i=bmp->height*2;
fwrite(&i,4,1,f);
a[0]=1;
a[1]=0;
a[2]=24;
a[3]=0;
fwrite(a,4,1,f);
i=0;
fwrite(&i,4,1,f);
fwrite(&i,4,1,f);
i=2834; // Pixels per meter (72 dpi)
fwrite(&i,4,1,f);
fwrite(&i,4,1,f);
i=0;
fwrite(&i,4,1,f);
fwrite(&i,4,1,f);
for (i=bmp->height-1;i>=0;i--)
{
unsigned char *p;
p=bmp_rowptr_from_top(bmp,i);
for (j=0;j<bmp->width;j++,p+=bmp->bpp/8)
{
int r,g,b;
RGBGET(bmp,p,r,g,b);
a[0]=b;
a[1]=g;
a[2]=r;
fwrite(a,3,1,f);
}
}
a[0]=0;
for (i=0;i<nb;i++)
fwrite(a,1,1,f);
fclose(f);
return(0);
}
/*
** If 8-bit, the bitmap is filled with <r>.
** If 24-bit, it gets <r>, <g>, <b> values.
*/
void bmp_fill(WILLUSBITMAP *bmp,int r,int g,int b)
{
int y,n;
if (bmp->bpp==8 || (r==g && r==b))
{
memset(bmp->data,r,bmp->size_allocated);
return;
}
if (bmp->type==WILLUSBITMAP_TYPE_WIN32 && bmp->bpp==24)
{
y=r;
r=b;
b=y;
}
for (y=bmp->height-1;y>=0;y--)
{
unsigned char *p;
p=bmp_rowptr_from_top(bmp,y);
for (n=bmp->width-1;n>=0;n--)
{
(*p)=r;
p++;
(*p)=g;
p++;
(*p)=b;
p++;
}
}
}
/*
** Should be called right after bmp_init() to set the bitmap type.
** Type defaults to WILLUSBITMAP_TYPE_NATIVE if this is not called.
*/
void bmp_set_type(WILLUSBITMAP *bmap,int type)
{
bmap->type = type;
}
int bmp_get_type(WILLUSBITMAP *bmp)
{
return(bmp->type);
}
/*
** Write out to an 8-bit BMP file, OUT.BMP
*/
static int bmp8_write(WILLUSBITMAP *bmap,char *filename,FILE *out)
{
FILE *f;
char a[54];
long bytewidth,y,i,extra;
long height;
height=bmap->height;
bytewidth=bmp_bytewidth_win32(bmap);
for (i=0;i<54;i++)
a[i]=0;
/* Fill in header */
a[0]='B';
a[1]='M';
insert_int32lsbmsb(&a[2],bytewidth*bmap->height+54+256*4);
insert_int32lsbmsb(&a[10],0x436); /* Where the bitmap data starts */
insert_int32lsbmsb(&a[14],0x28); /* Size of BITMAPINFOHEADER */
insert_int32lsbmsb(&a[18],bmap->width);
insert_int32lsbmsb(&a[22],bmap->height);
a[26]=1;
a[28]=8;
insert_int32lsbmsb(&a[34],bytewidth*bmap->height);
insert_int32lsbmsb(&a[38],0x1274); /* pixels per meter */
insert_int32lsbmsb(&a[42],0x1274);
f=wfile_fopen_utf8(filename,"wb");
if (f==NULL)
{
if (out!=NULL)
fprintf(out,"Cannot open file OUT.BMP for writing.\n");
return(-2);
}
fwrite(a,sizeof(char),54,f);
for (i=0;i<256;i++)
{
fputc(bmap->blue[i],f);
fputc(bmap->green[i],f);
fputc(bmap->red[i],f);
fputc(0,f);
}
if (bmap->type==WILLUSBITMAP_TYPE_WIN32)
fwrite(bmap->data,sizeof(char),bytewidth*bmap->height,f);
else
{
extra=bytewidth-bmap->width;
a[0]=a[1]=a[2]=a[3]=0;
for (y=0;y<height;y++)
{
fwrite(&bmap->data[(bmap->height-1-y)*bmap->width],sizeof(char),bmap->width,f);
if (extra)
fwrite(a,sizeof(char),extra,f);
}
}
fclose(f);
return(0);
}
/*
** Write out to a 24-bit BMP file.
*/
static int bmp24_write(WILLUSBITMAP *bmap,char *filename,FILE *out)
{
FILE *f;
char a[54];
int n;
long bytewidth,y,i;
n=0;
bytewidth = bmp_bytewidth_win32(bmap);
for (i=0;i<54;i++)
a[i]=0;
/* Fill in header */
a[0]='B';
a[1]='M';
insert_int32lsbmsb(&a[2],bytewidth*bmap->height+54);
insert_int32lsbmsb(&a[10],0x36); /* Where the bitmap data starts */
insert_int32lsbmsb(&a[14],0x28); /* size of BITMAPINFOHEADER */
insert_int32lsbmsb(&a[18],bmap->width);
insert_int32lsbmsb(&a[22],bmap->height);
a[26]=1;
a[28]=24;
insert_int32lsbmsb(&a[34],bytewidth*bmap->height);
insert_int32lsbmsb(&a[38],0x1274); /* pixels per meter */
insert_int32lsbmsb(&a[42],0x1274);
f=wfile_fopen_utf8(filename,"wb");
if (f==NULL)
{
if (out!=NULL)
fprintf(out,"Cannot open file %s for writing.\n",filename);
return(-2);
}
fwrite(a,sizeof(char),54,f);
if (bmap->type == WILLUSBITMAP_TYPE_WIN32)
fwrite(bmap->data,sizeof(char),bytewidth*bmap->height,f);
else
{
a[0]=a[1]=a[2]=a[3]=0;
n=bytewidth - (bmap->width*3);
bmp24_flip_rgb(bmap);
for (y=bmap->height-1;y>=0;y--)
{
fwrite(&bmap->data[y*bmap->width*3],sizeof(char),bmap->width*3,f);
if (n)
fwrite(a,sizeof(char),n,f);
}
bmp24_flip_rgb(bmap);
}
fclose(f);
return(0);
}
#ifdef HAVE_PNG_LIB
int bmp_png_info(char *filename,int *width,int *height,int *bpp,FILE *out)
{
FILE *f;
char header[8];
png_structp png_ptr;
png_infop info_ptr,end_info;
int color_type,bppel;
png_uint_32 wid,hght;
f=wfile_fopen_utf8(filename,"rb");
if (f==NULL)
{
if (out!=NULL)
fprintf(out,"Cannot open file %s for PNG input.\n",filename);
return(-1);
}
if (fread(header,1,8,f)<8 || png_sig_cmp((unsigned char *)header,0,8))
{
fclose(f);
if (out!=NULL)
fprintf(out,"File %s doesn't appear to be PNG.\n",filename);
return(-2);
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if (png_ptr==NULL)
{
fclose(f);
if (out!=NULL)
fprintf(out,"Cannot create PNG structure.\n");
return(-3);
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr==NULL)
{
fclose(f);
png_destroy_read_struct(&png_ptr,NULL,NULL);
if (out!=NULL)
fprintf(out,"Cannot create PNG info structure.\n");
return(-4);
}
end_info = png_create_info_struct(png_ptr);
if (end_info==NULL)
{
fclose(f);
png_destroy_read_struct(&png_ptr,&info_ptr,NULL);
if (out!=NULL)
fprintf(out,"Cannot create PNG end info structure.\n");
return(-5);
}
/* Error handler */
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
fclose(f);
if (out!=NULL)
fprintf(out,"PNG Read Error!\n");
return(-6);
}
png_init_io(png_ptr,f);
png_set_sig_bytes(png_ptr,8);
png_read_info(png_ptr,info_ptr);
png_get_IHDR(png_ptr,info_ptr,&wid,&hght,&bppel,
&color_type,NULL,NULL,NULL);
if (width!=NULL)
(*width)=(int)wid;
if (height!=NULL)
(*height)=(int)hght;
if (color_type==PNG_COLOR_TYPE_GRAY)
bppel=8;
else if (color_type&PNG_COLOR_MASK_PALETTE)
bppel=8;
else
bppel=24;
if (bpp!=NULL)
(*bpp)=bppel;
png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
fclose(f);
return(0);
}
int bmp_read_png(WILLUSBITMAP *bmp,char *filename,FILE *out)
{
FILE *f;
int status;
f=wfile_fopen_utf8(filename,"rb");
if (f==NULL)
{
nprintf(out,"Cannot open file %s for PNG input.\n",filename);
return(-1);
}
status=bmp_read_png_stream(bmp,(void *)f,0,out);
fclose(f);
return(status);
}
static unsigned char *pngdata;
static int pngindex;
static void bmp_read_png_from_memory(png_structp png_ptr,void *buf,int nbytes)
{
memcpy(buf,&pngdata[pngindex],nbytes);
pngindex+=nbytes;
}
int bmp_read_png_stream(WILLUSBITMAP *bmp,void *io,int size,FILE *out)
{
unsigned char header[8];
png_structp png_ptr;
png_infop info_ptr,end_info;
int color_type,gotpal,rowbytes;
static png_colorp pngpal;
unsigned char **rowptrs;
double *dptr;
int i,num_palette;
static char *funcname="bmp_read_png_stream";
FILE *f;
static char *notpng="File doesn't appear to be PNG.\n";
f=NULL;
rowptrs=NULL;
if (size==0)
{
f=(FILE *)io;
if (fread(header,1,8,f)<8 || png_sig_cmp(header,0,8))
{
nprintf(out,"%s",notpng);
return(-2);
}
}
else
{
pngindex=8;
pngdata=(unsigned char *)io;
if (png_sig_cmp(pngdata,0,8))
{
nprintf(out,"%s",notpng);
return(-2);
}
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if (png_ptr==NULL)
{
nprintf(out,"Cannot create PNG structure.\n");
return(-3);
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr==NULL)
{
png_destroy_read_struct(&png_ptr,NULL,NULL);
nprintf(out,"Cannot create PNG info structure.\n");
return(-4);
}
end_info = png_create_info_struct(png_ptr);
if (end_info==NULL)
{
png_destroy_read_struct(&png_ptr,&info_ptr,NULL);
nprintf(out,"Cannot create PNG end info structure.\n");
return(-5);
}
/* Error handler */
if (setjmp(png_jmpbuf(png_ptr)))
{
dptr=(double *)rowptrs;
willus_mem_free(&dptr,funcname);
rowptrs=(unsigned char **)dptr;
bmp_free(bmp);
png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
nprintf(out,"PNG Read Error!\n");
return(-6);
}
if (size>0)
png_set_read_fn(png_ptr,NULL,(png_rw_ptr)bmp_read_png_from_memory);
else
png_init_io(png_ptr,f);
png_set_sig_bytes(png_ptr,8);
png_read_info(png_ptr,info_ptr);
{
png_uint_32 ww,hh;
png_get_IHDR(png_ptr,info_ptr,&ww,&hh,&bmp->bpp,
&color_type,NULL,NULL,NULL);
bmp_dpi = (double)png_get_x_pixels_per_meter(png_ptr,info_ptr)*.0254;
bmp->width=(int)ww;
bmp->height=(int)hh;
}
/* Only allow up to 8-bits per channel */
if (bmp->bpp==16)
png_set_strip_16(png_ptr);
gotpal=0;
if (color_type==PNG_COLOR_TYPE_GRAY)
{
png_set_expand_gray_1_2_4_to_8(png_ptr);
bmp->bpp = 8;
for (i=0;i<256;i++)
{
bmp->red[i]=i;
bmp->blue[i]=i;
bmp->green[i]=i;
}
}
else if (color_type&PNG_COLOR_MASK_PALETTE)
{
if (bmp->bpp<8)
png_set_packing(png_ptr);
bmp->bpp=8;
if (png_get_valid(png_ptr,info_ptr,PNG_INFO_PLTE))
{
int i;
png_get_PLTE(png_ptr,info_ptr,&pngpal,&num_palette);
for (i=0;i<num_palette;i++)
{
bmp->red[i] = pngpal[i].red;
bmp->green[i] = pngpal[i].green;
bmp->blue[i] = pngpal[i].blue;
}
gotpal=1;
}
}
else
{
bmp->bpp=24;
if (bmp->type == WILLUSBITMAP_TYPE_WIN32)
png_set_bgr(png_ptr);
}
/* Strip alpha channel if there. */
if (color_type&PNG_COLOR_MASK_ALPHA)
png_set_strip_alpha(png_ptr);
/* Allocate bitmap. */
if (!bmp_alloc(bmp))
{
png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
nprintf(out,"Cannot allocate memory for %d x %d x %d bitmap.\n",
bmp->width,bmp->height,bmp->bpp);
return(-7);
}
if (!willus_mem_alloc(&dptr,bmp->height*sizeof(unsigned char *),funcname))
{
bmp_free(bmp);
png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
nprintf(out,"Cannot allocate memory (%d pointers) for bitmap.\n",
bmp->height);
return(-8);
}
rowptrs=(unsigned char **)dptr;
rowbytes = bmp_bytewidth(bmp);
if (bmp->type==WILLUSBITMAP_TYPE_WIN32)
for (i=0;i<bmp->height;i++)
rowptrs[i] = &bmp->data[(bmp->height-1-i)*rowbytes];
else
for (i=0;i<bmp->height;i++)
rowptrs[i] = &bmp->data[i*rowbytes];
/* Okay, should be ready to read data. */
png_read_image(png_ptr,(void *)rowptrs);
dptr=(double *)rowptrs;
willus_mem_free(&dptr,funcname);
rowptrs=(unsigned char **)dptr;
if ((color_type&PNG_COLOR_MASK_PALETTE) && !gotpal)
{
png_read_end(png_ptr,end_info);
if (png_get_valid(png_ptr,end_info,PNG_INFO_PLTE))
{
int i;
png_get_PLTE(png_ptr,info_ptr,&pngpal,&num_palette);
for (i=0;i<num_palette;i++)
{
bmp->red[i] = pngpal[i].red;
bmp->green[i] = pngpal[i].green;
bmp->blue[i] = pngpal[i].blue;
}
gotpal=1;
}
}
png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
return(0);
}
int bmp_write_png(WILLUSBITMAP *bmp,char *filename,FILE *out)
{
FILE *f;
int status;
f=wfile_fopen_utf8(filename,"wb");
if (f==NULL)
{
if (out!=NULL)
fprintf(out,"Cannot open file %s for PNG output.\n",filename);
return(-1);
}
status = bmp_write_png_stream(bmp,f,out);
fclose(f);
return(status);
}
int bmp_write_png_stream(WILLUSBITMAP *bmp,FILE *f,FILE *out)
{
png_structp png_ptr;
png_infop info_ptr;
unsigned char **rowptrs;
double *dptr;
int i,rowbytes;
static char *funcname="bmp_write_png_stream";
rowptrs=NULL;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if (png_ptr==NULL)
{
if (out!=NULL)
fprintf(out,"Could not allocate PNG structure.\n");
return(-2);
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr==NULL)
{
if (out!=NULL)
fprintf(out,"Could not allocate PNG info structure.\n");
png_destroy_write_struct(&png_ptr,NULL);
return(-3);
}
/* Error handler */
if (setjmp(png_jmpbuf(png_ptr)))
{
dptr=(double *)rowptrs;
willus_mem_free(&dptr,funcname);
rowptrs=(unsigned char **)dptr;
png_destroy_write_struct(&png_ptr,&info_ptr);
if (out!=NULL)
fprintf(out,"PNG Write Error!\n");
return(-4);
}
png_init_io(png_ptr,f);
png_set_compression_level(png_ptr,Z_BEST_COMPRESSION);
png_set_IHDR(png_ptr,info_ptr,bmp->width,bmp->height,8,
bmp->bpp==24 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_PALETTE,
PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
if (bmp->bpp==8)
{
static png_color pngpal[256];
int i;
for (i=0;i<256;i++)
{
pngpal[i].red = bmp->red[i];
pngpal[i].green = bmp->green[i];
pngpal[i].blue = bmp->blue[i];
}
png_set_PLTE(png_ptr,info_ptr,pngpal,256);
}
png_set_pHYs(png_ptr,info_ptr,(int)(bmp_dpi/.0254+.5),(int)(bmp_dpi/.0254+.5),
PNG_RESOLUTION_METER);
png_write_info(png_ptr,info_ptr);
if (bmp->type==WILLUSBITMAP_TYPE_WIN32)
png_set_bgr(png_ptr);
if (!willus_mem_alloc(&dptr,bmp->height*sizeof(unsigned char *),funcname))
{
png_destroy_write_struct(&png_ptr,&info_ptr);
if (out!=NULL)
fprintf(out,"Cannot allocate memory (%d pointers) for bitmap.\n",
bmp->height);
return(-5);
}
rowptrs=(unsigned char **)dptr;
rowbytes = bmp_bytewidth(bmp);
if (bmp->type==WILLUSBITMAP_TYPE_WIN32)
for (i=0;i<bmp->height;i++)
rowptrs[i] = &bmp->data[(bmp->height-1-i)*rowbytes];
else
for (i=0;i<bmp->height;i++)
rowptrs[i] = &bmp->data[i*rowbytes];
png_write_image(png_ptr,(void *)rowptrs);
dptr=(double *)rowptrs;
willus_mem_free(&dptr,funcname);
rowptrs=NULL;
png_write_end(png_ptr,info_ptr);
png_destroy_write_struct(&png_ptr,&info_ptr);
return(0);
}
#endif /* HAVE_PNG_LIB */
#ifdef HAVE_JPEG_LIB
struct my_error_mgr
{
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
};
typedef struct my_error_mgr *my_error_ptr;
static void my_error_exit (j_common_ptr cinfo)
{
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
my_error_ptr myerr = (my_error_ptr) cinfo->err;
/* Always display the message. */
/* We could postpone this until after returning, if we chose. */
(*cinfo->err->output_message) (cinfo);
/* Return control to the setjmp point */
longjmp(myerr->setjmp_buffer, 1);
}
int bmp_write_jpeg(WILLUSBITMAP *bmp,char *filename,int quality,FILE *out)
{
FILE *f;
int status;
f=wfile_fopen_utf8(filename,"wb");
if (f==NULL)
{
if (out!=NULL)
fprintf(out,"Cannot open file %s for JPEG output.\n",filename);
return(-1);
}
status=bmp_write_jpeg_stream(bmp,f,quality,out);
fclose(f);
return(status);
}
/*
** If status==0, the bmp_write_jpeg_stream will write a JPEG file with
** optimized encoding (optimized huffman tables). If status!=0, the
** JPEG file will be written with standard Huffman tables (JPEG standard
** section K.3)--see jcparam.c file in the jpeg library.
*/
void bmp_jpeg_set_std_huffman(int status)
{
bmp_std_huffman_tables=status;
}
int bmp_write_jpeg_stream(WILLUSBITMAP *bmp,FILE *outfile,int quality,FILE *out)
{
struct jpeg_compress_struct cinfo;
struct my_error_mgr jerr;
JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
int row_stride; /* physical row width in image buffer */
/* Error handler */
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
if (setjmp(jerr.setjmp_buffer))
{
jpeg_destroy_compress(&cinfo);
return(-2);
}
/* Create the JPEG compression object. */
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo,outfile);
cinfo.image_width = bmp->width;
cinfo.image_height = bmp->height;
cinfo.input_components = bmp->bpp==8 ? 1 : 3;
cinfo.in_color_space = bmp->bpp==8 ? JCS_GRAYSCALE : JCS_RGB;
jpeg_set_defaults(&cinfo);
if (bmp_dpi > 0)
{
cinfo.density_unit = 1;
cinfo.X_density = bmp_dpi;
cinfo.Y_density = bmp_dpi;
}
/* See bmp_jpeg_set_std_huffman() */
cinfo.optimize_coding = bmp_std_huffman_tables ? 0 : 1;
jpeg_set_quality(&cinfo,quality,TRUE);
/* Do it! */
jpeg_start_compress(&cinfo, TRUE);
row_stride = bmp_bytewidth(bmp);
if (bmp->type==WILLUSBITMAP_TYPE_WIN32)
{
if (bmp->bpp==24)
bmp24_flip_rgb(bmp);
while (cinfo.next_scanline < cinfo.image_height)
{
row_pointer[0] = &bmp->data[(cinfo.image_height-1-cinfo.next_scanline)*row_stride];
jpeg_write_scanlines(&cinfo,row_pointer,1);
}
if (bmp->bpp==24)
bmp24_flip_rgb(bmp);
}
else
while (cinfo.next_scanline < cinfo.image_height)
{
row_pointer[0] = &bmp->data[cinfo.next_scanline*row_stride];
jpeg_write_scanlines(&cinfo,row_pointer,1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return(0);
}
int bmp_jpeg_info(char *filename,int *width,int *height,int *bpp)
{
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
FILE *infile;
infile=wfile_fopen_utf8(filename,"rb");
if (infile==NULL)
{
fprintf(stderr,"Cannot open JPEG file %s for input.\n",filename);
return(-1);
}
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
if (setjmp(jerr.setjmp_buffer))
{
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return(-2);
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo,infile);
cinfo.out_color_space = JCS_RGB;
jpeg_read_header(&cinfo,TRUE);
jpeg_start_decompress(&cinfo);
if (width!=NULL)
(*width)=cinfo.output_width;
if (height!=NULL)
(*height)=cinfo.output_height;
if (bpp!=NULL)
(*bpp) = (cinfo.out_color_space==JCS_GRAYSCALE) ? 8 : 24;
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return(0);
}
int bmp_read_jpeg(WILLUSBITMAP *bmp,char *filename,FILE *out)
{
FILE *infile;
int status;
infile=wfile_fopen_utf8(filename,"rb");
if (infile==NULL)
{
if (out!=NULL)
fprintf(out,"Cannot open JPEG file %s for input.\n",filename);
return(-1);
}
status=bmp_read_jpeg_stream(bmp,infile,0,out);
fclose(infile);
return(status);
}
/*
** Reads JPEG from EITHER file stream or from memory buffer.
** If file stream (determined by if size==0),
** infile must be FILE * (already open).
** If memory buffer, infile must be (unsigned char *) and size is
** size of buffer.
*/
int bmp_read_jpeg_stream(WILLUSBITMAP *bmp,void *infile,int size,FILE *out)
{
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
void *p[1];
int row_stride,i;
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
if (setjmp(jerr.setjmp_buffer))
{
jpeg_destroy_decompress(&cinfo);
return(-2);
}
jpeg_create_decompress(&cinfo);
if (size>0)
jpeg_mem_src(&cinfo,(unsigned char *)infile,size);
else
jpeg_stdio_src(&cinfo,(FILE *)infile);
cinfo.out_color_space = JCS_RGB;
jpeg_read_header(&cinfo,TRUE);
bmp_dpi = cinfo.density_unit==2 ? cinfo.X_density*2.54 : cinfo.X_density;
jpeg_start_decompress(&cinfo);
bmp->width=cinfo.output_width;
bmp->height=cinfo.output_height;
bmp->bpp = (cinfo.out_color_space==JCS_GRAYSCALE) ? 8 : 24;
if (bmp->bpp==8)
for (i=0;i<256;i++)
{
bmp->red[i]=i;
bmp->green[i]=i;
bmp->blue[i]=i;
}
if (!bmp_alloc(bmp))
{
jpeg_destroy_decompress(&cinfo);
return(-3);
}
row_stride = bmp_bytewidth(bmp);
/*
row_stride = cinfo.output_width*cinfo.output_components;
*/
if (bmp->type==WILLUSBITMAP_TYPE_WIN32)
{
while (cinfo.output_scanline < cinfo.output_height)
{
p[0] = (void *)&bmp->data[(cinfo.output_height-1-cinfo.output_scanline)*row_stride];
jpeg_read_scanlines(&cinfo,(JSAMPARRAY)p,1);
}
if (bmp->bpp==24)
bmp24_flip_rgb(bmp);
}
else
while (cinfo.output_scanline < cinfo.output_height)
{
p[0] = (void *)&bmp->data[cinfo.output_scanline*row_stride];
jpeg_read_scanlines(&cinfo,(JSAMPARRAY)p,1);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return(0);
}
#endif /* HAVE_JPEG_LIB */
static void insert_int32lsbmsb(char *a,int x)
{
a[3] = (x >> 24) & 0xff;
a[2] = (x >> 16) & 0xff;
a[1] = (x >> 8) & 0xff;
a[0] = x & 0xff;
}
static int retrieve_int32lsbmsb(char *a)
{
int a3,a2,a1,a0,x;
a3=a[3];
a2=a[2];
a1=a[1];
a0=a[0];
x = ((a3&0xff)<<24) | ((a2&0xff)<<16) | ((a1&0xff)<<8) | (a0&0xff);
return(x);
}
void bmp24_mixbmps(WILLUSBITMAP *dest,WILLUSBITMAP *src1,WILLUSBITMAP *src2,int level)
{
int i,n;
if (dest->bpp!=24 || src1->bpp!=24 || src2->bpp!=24)
return;
n=bmp_bytewidth(dest) * dest->height;
for (i=0;i<n;i++)
{
int d,s1,s2;
s1=src1->data[i];
s2=src2->data[i];
d=(s2*level+(256-level)*s1)>>8;
dest->data[i]=d;
}
}
void bmp8_palette_info(WILLUSBITMAP *bmap,FILE *out)
{
long i;
int counts[256];
long pixcount;
pixcount=bmap->width*bmap->height;
for (i=0;i<256;i++)
counts[i]=0;
for (i=0;i<pixcount;i++)
counts[bmap->data[i]]++;
for (i=0;i<256;i++)
fprintf(out,"Index %3ld (%3d,%3d,%3d): %6d\n",
i,bmap->red[i],bmap->blue[i],bmap->green[i],counts[i]);
}
/*
** Put palette values directly into data values for 8-bit grey-level bitmap.
*/
void bmp8_to_grey(WILLUSBITMAP *bmap)
{
long pixcount;
long i;
pixcount=bmp_bytewidth(bmap)*bmap->height;
for (i=pixcount-1;i>=0;i--)
{
int di,r,g,b;
di=bmap->data[i];
r=bmap->red[di];
g=bmap->green[di];
b=bmap->blue[di];
bmap->data[i] = bmp8_greylevel_convert(r,g,b);
}
}
void bmp8_to_bw(WILLUSBITMAP *bmap,int thresh)
{
long pixcount;
int r,g,b;
long i;
pixcount=bmp_bytewidth(bmap)*bmap->height;
for (i=pixcount-1;i>=0;i--)
{
int di;
di=bmap->data[i];
r=bmap->red[di];
g=bmap->green[di];
b=bmap->blue[di];
bmap->data[i] = (bmp8_greylevel_convert(r,g,b) >= thresh ? 0 : 1);
}
}
int bmp8_greylevel_convert(int r,int g,int b)
{
return((int)((r*0.3+g*0.59+b*0.11)*1.002));
}
/*
** Should call bmp_set_type() right after this to set the bitmap type.
*/
void bmp_init(WILLUSBITMAP *bmap)
{
bmap->data=NULL;
bmap->size_allocated=0;
bmap->type=WILLUSBITMAP_TYPE_NATIVE;
}
/*
** The width, height, and bpp parameters of the WILLUSBITMAP structure
** should be set before calling this function.
*/
int bmp_alloc(WILLUSBITMAP *bmap)
{
int size;
static char *funcname="bmp_alloc";
if (bmap->bpp!=8 && bmap->bpp!=24)
{
printf("Internal error: call to bmp_alloc has bpp!=8 and bpp!=24!\n");
exit(10);
}
/* Choose the max size even if not WIN32 to avoid memory faults */
/* and to allow the possibility of changing the "type" of the */
/* bitmap without reallocating memory. */
size = bmp_bytewidth_win32(bmap)*bmap->height;
if (bmap->data!=NULL && bmap->size_allocated>=size)
return(1);
if (bmap->data!=NULL)
willus_mem_realloc_robust_warn((void **)&bmap->data,size,bmap->size_allocated,funcname,10);
else
willus_mem_alloc_warn((void **)&bmap->data,size,funcname,10);
bmap->size_allocated=size;
return(1);
}
int bmp_bytewidth(WILLUSBITMAP *bmp)
{
if (bmp->type == WILLUSBITMAP_TYPE_WIN32)
return(bmp_bytewidth_win32(bmp));
return(bmp->bpp==24 ? bmp->width*3 : bmp->width);
}
/*
** row==0 ==> top row of bitmap
** row==bmp->height-1 ==> bottom row of bitmap
** (regardless of bitmap type)
*/
unsigned char *bmp_rowptr_from_top(WILLUSBITMAP *bmp,int row)
{
if (bmp->type==WILLUSBITMAP_TYPE_WIN32)
return(&bmp->data[bmp_bytewidth(bmp)*(bmp->height-1-row)]);
else
return(&bmp->data[bmp_bytewidth(bmp)*row]);
}
/*
** y0 is row FROM TOP! 0=top row, bmp->height-1=bottom row.
*/
void bmp_crop(WILLUSBITMAP *bmp,int x0,int y0_from_top,int width,int height)
{
int x1,y1,y0,i,dbw,sbw;
unsigned char *psrc,*pdest;
y0 = y0_from_top;
y1 = y0+height-1;
x1 = x0+width-1;
if (x1 > bmp->width-1)
x1 = bmp->width-1;
if (y1 > bmp->height-1)
y1 = bmp->height-1;
if (x0<0)
x0 = 0;
if (y0<0)
y0 = 0;
if (x0==0 && y0==0 && x1==bmp->width-1 && y1==bmp->height-1)
return;
sbw = bmp_bytewidth(bmp);
psrc = bmp_rowptr_from_top(bmp,bmp->type==WILLUSBITMAP_TYPE_WIN32?y1:y0)
+ ((bmp->bpp+7)>>3)*x0;
bmp->width=width;
bmp->height=height;
dbw = bmp_bytewidth(bmp);
pdest = bmp->data;
for (i=height;i>0;i--,psrc+=sbw,pdest+=dbw)
memmove(pdest,psrc,dbw);
}
void bmp_crop_ex(WILLUSBITMAP *dst,WILLUSBITMAP *src,int x0,int y0_from_top,int width,int height)
{
int i,bpp,bpr;
bpp=src->bpp==24 ? 3 : 1;
dst->width=width;
dst->height=height;
dst->type=src->type;
dst->bpp=src->bpp;
bmp_alloc(dst);
memcpy(dst->red,src->red,256);
memcpy(dst->green,src->green,256);
memcpy(dst->blue,src->blue,256);
bpr=bmp_bytewidth(dst);
for (i=0;i<height;i++)
{
unsigned char *psrc,*pdst;
psrc=bmp_rowptr_from_top(src,y0_from_top+i)+x0*bpp;
pdst=bmp_rowptr_from_top(dst,i);
memcpy(pdst,psrc,bpr);
}
}
void bmp_rotate_fast(WILLUSBITMAP *bmp,double degrees,int expand)
{
WILLUSBITMAP _dst,*dst;
double th,sth,cth;
int i,r,g,b,w,h,row,col;
dst=&_dst;
th=degrees*PI/180.;
sth=sin(th);
cth=cos(th);
if (expand)
{
w=(int)(fabs(bmp->width*cth)+fabs(bmp->height*sth)+.5);
h=(int)(fabs(bmp->height*cth)+fabs(bmp->width*sth)+.5);
}
else
{
w=bmp->width;
h=bmp->height;
}
dst=&_dst;
bmp_init(dst);
dst->width=w;
dst->height=h;
dst->bpp=bmp->bpp;
if (dst->bpp==8)
for (i=0;i<=255;i++)
dst->red[i] = dst->green[i] = dst->blue[i]=i;
bmp_alloc(dst);
bmp_pix_vali(bmp,0,0,&r,&g,&b);
bmp_fill(dst,r,g,b);
if (dst->bpp==8)
for (row=0;row<dst->height;row++)
{
unsigned char *p;
double x1,y1,x2,y2;
y2=dst->height/2.-row;
p=bmp_rowptr_from_top(dst,row);
for (x2=-dst->width/2.,col=0;col<dst->width;col++,p++,x2+=1.0)
{
double g;
x1 = -.5 + bmp->width/2. + x2*cth + y2*sth;
y1 = -.5 + bmp->height/2. + y2*cth - x2*sth;
if (x1<0. || x1>=bmp->width || y1<0. || y1>=bmp->height)
continue;
g=bmp_grey_pix_vald(bmp,x1,y1);
if (g>=0.)
p[0]=g;
}
}
else
for (row=0;row<dst->height;row++)
{
unsigned char *p;
double x1,y1,x2,y2;
y2=dst->height/2.-row;
p=bmp_rowptr_from_top(dst,row);
for (x2=-dst->width/2.,col=0;col<dst->width;col++,p+=3,x2+=1.0)
{
double rr,gg,bb;
x1 = -.5 + bmp->width/2. + x2*cth + y2*sth;
y1 = -.5 + bmp->height/2. + y2*cth - x2*sth;
if (x1<0. || x1>=bmp->width || y1<0. || y1>=bmp->height)
continue;
bmp_pix_vald(bmp,x1,y1,&rr,&gg,&bb);
if (rr<0.)
continue;
p[0]=rr;
p[1]=gg;
p[2]=bb;
}
}
bmp_copy(bmp,dst);
bmp_free(dst);
}
/*
** 1 = okay, 0 = fail
*/
int bmp_rotate_right_angle(WILLUSBITMAP *bmp,int degrees)
{
int d;
d=degrees%360;
if (d<0)
d+=360;
d=(d+45)/90;
if (d==1)
return(bmp_rotate_90(bmp));
if (d==2)
{
bmp_flip_horizontal(bmp);
bmp_flip_vertical(bmp);
return(1);
}
if (d==3)
return(bmp_rotate_270(bmp));
return(1);
}
int bmp_rotate_90(WILLUSBITMAP *bmp)
{
WILLUSBITMAP *sbmp,_sbmp;
int bpp,dbw,sr;
sbmp=&_sbmp;
bmp_init(sbmp);
if (!bmp_copy(sbmp,bmp))
return(0);
bmp->width = sbmp->height;
bmp->height = sbmp->width;
bpp = bmp->bpp/8;
if (!bmp_alloc(bmp))
{
bmp_free(sbmp);
return(0);
}
dbw = (int)(bmp_rowptr_from_top(bmp,1) - bmp_rowptr_from_top(bmp,0));
for (sr=0;sr<sbmp->height;sr++)
{
unsigned char *sp,*dp;
int j,sc;
sp = bmp_rowptr_from_top(sbmp,sr);
dp = bmp_rowptr_from_top(bmp,bmp->height-1) + bpp*sr;
for (sc=sbmp->width;sc>0;sc--,dp-=dbw)
for (j=0;j<bpp;j++,sp++)
dp[j] = sp[0];
}
bmp_free(sbmp);
return(1);
}
int bmp_rotate_270(WILLUSBITMAP *bmp)
{
WILLUSBITMAP *sbmp,_sbmp;
int bpp,dbw,sr;
sbmp=&_sbmp;
bmp_init(sbmp);
if (!bmp_copy(sbmp,bmp))
return(0);
bmp->width = sbmp->height;
bmp->height = sbmp->width;
bpp = bmp->bpp/8;
if (!bmp_alloc(bmp))
{
bmp_free(sbmp);
return(0);
}
dbw = (int)(bmp_rowptr_from_top(bmp,1) - bmp_rowptr_from_top(bmp,0));
for (sr=0;sr<sbmp->height;sr++)
{
unsigned char *sp,*dp;
int j,sc;
sp = bmp_rowptr_from_top(sbmp,sr);
dp = bmp_rowptr_from_top(bmp,0) + bpp*(sbmp->height-1-sr);
for (sc=sbmp->width;sc>0;sc--,dp+=dbw)
for (j=0;j<bpp;j++,sp++)
dp[j] = sp[0];
}
bmp_free(sbmp);
return(1);
}
int bmp_copy(WILLUSBITMAP *dest,WILLUSBITMAP *src)
{
dest->width = src->width;
dest->height = src->height;
dest->bpp = src->bpp;
dest->type = src->type;
if (!bmp_alloc(dest))
return(0);
memcpy(dest->data,src->data,src->height*bmp_bytewidth(src));
memcpy(dest->red,src->red,sizeof(int)*256);
memcpy(dest->green,src->green,sizeof(int)*256);
memcpy(dest->blue,src->blue,sizeof(int)*256);
return(1);
}
/*
** I figured this out empirically on a sample of four bitmaps.
** It reports approximately the number of bytes per pixel that a JPEG
** file will be based on the quality.
*/
double bmp_jpeg_bytes_per_pixel(int quality)
{
double x,x0,sum;
static double coeffs[9]={0.003269,0.4074,-3.465,23.66,-68.83,82.94,
-16.82,-40.34,22.78};
int i;
x0=quality/100.;
for (x=1,i=0,sum=0.;i<9;sum+=x*coeffs[i],i++,x*=x0);
return(sum/.75);
}
void bmp_flip_horizontal(WILLUSBITMAP *bmp)
{
int i,j,bpp;
bpp = bmp->bpp/8;
for (i=0;i<bmp->height;i++)
{
unsigned char *p,*p2;
p=bmp_rowptr_from_top(bmp,i);
p2=&p[(bmp->width-1)*bpp];
for (;p<p2;p+=bpp,p2-=bpp)
for (j=0;j<bpp;j++)
{
unsigned char t;
t=p[j];
p[j]=p2[j];
p2[j]=t;
}
}
}
void bmp_flip_vertical(WILLUSBITMAP *bmp)
{
int i,bw,n;
bw = bmp_bytewidth(bmp);
n=bmp->height/2;
for (i=0;i<n;i++)
{
unsigned char *p,*p2;
int j;
p=bmp_rowptr_from_top(bmp,i);
p2=bmp_rowptr_from_top(bmp,bmp->height-i-1);
for (j=bw;j>0;j--,p++,p2++)
{
unsigned char t;
t=p[0];
p[0]=p2[0];
p2[0]=t;
}
}
}
int bmp_bytewidth_win32(WILLUSBITMAP *bmp)
{
return(((bmp->bpp==24 ? bmp->width*3 : bmp->width)+3)&(~0x3));
}
void bmp_free(WILLUSBITMAP *bmap)
{
if (bmap->data!=NULL)
{
willus_mem_free((double **)&bmap->data,"bmp_free");
bmap->data=NULL;
bmap->size_allocated=0;
}
}
int bmp_read(WILLUSBITMAP *bmap,char *filename,FILE *out)
{
FILE *f;
int bpp;
char fileext[16];
get_file_ext(fileext,filename);
#ifdef HAVE_GHOSTSCRIPT
if (!stricmp(fileext,"ps") || !stricmp(fileext,"eps") || !stricmp(fileext,"pdf"))
return(willusgs_read_pdf_or_ps_bmp(bmap,filename,willusbmp_pageno,willusbmp_dpi,out));
#endif
#ifdef HAVE_PNG_LIB
if (!stricmp(fileext,"png"))
return(bmp_read_png(bmap,filename,out));
#endif
#ifdef HAVE_JPEG_LIB
if (!stricmp(fileext,"jpg") || !stricmp(fileext,"jpeg"))
return(bmp_read_jpeg(bmap,filename,out));
#endif
if (stricmp(fileext,"bmp"))
#ifdef HAVE_JASPER_LIB
{
int fmt;
fmt=bmp_jasper_read(NULL,filename,NULL);
if (fmt<0)
#endif
fprintf(out,"Warning: file %s has no extension. Treating as BMP file.\n",filename);
#ifdef HAVE_JASPER_LIB
else
return(bmp_jasper_read(bmap,filename,out));
}
#endif
f=wfile_fopen_utf8(filename,"rb");
if (f==NULL)
{
if (out!=NULL)
fprintf(out,"Cannot open BMP file %s for input.\n",filename);
return(-1);
}
fseek(f,28L,0);
bpp=fgetc(f);
if (bpp!=8 && bpp!=24 && bpp!=32)
{
if (out!=NULL)
fprintf(out,"BMP file %s is not 8-bit or 24-bit.\n",filename);
fclose(f);
return(-9);
}
fclose(f);
return(bpp==8 ? bmp_read_bmp8(bmap,filename,out)
: bmp_read_bmp24(bmap,filename,out));
}
int bmp_info(char *filename,int *width,int *height,int *bpp,FILE *out)
{
char fileext[16];
get_file_ext(fileext,filename);
#ifdef HAVE_PNG_LIB
if (!stricmp(fileext,"png"))
return(bmp_png_info(filename,width,height,bpp,out));
#endif
#ifdef HAVE_JPEG_LIB
if (!stricmp(fileext,"jpg") || !stricmp(fileext,"jpeg"))
return(bmp_jpeg_info(filename,width,height,bpp));
#endif
if (stricmp(fileext,"bmp") && out!=NULL)
fprintf(out,"Warning: file %s has no extension. Treating as BMP file.\n",
filename);
return(bmp_bmp_info(filename,width,height,bpp,out));
}
static void get_file_ext(char *dest,char *src)
{
int i;
for (i=strlen(src)-1;i>=0 && src[i]!='.';i--);
strncpy(dest,&src[i+1],15);
}
int bmp_bmp_info(char *filename,int *width,int *height,int *bpp,FILE *out)
{
FILE *f;
char a[20];
long filelen;
f=wfile_fopen_utf8(filename,"rb");
if (f==NULL)
{
if (out!=NULL)
fprintf(out,"Can't open file %s for input.\n",filename);
return(-1);
}
fseek(f,0L,2);
filelen=ftell(f);
if (filelen<54)
{
fclose(f);
if (out!=NULL)
fprintf(out,"Input file %s is too small to be a BMP file.\n",filename);
return(-2);
}
fseek(f,18L,0);
if (fread(a,sizeof(char),20,f)<20)
{
fclose(f);
if (out!=NULL)
fprintf(out,"Error reading BMP file %s.\n",filename);
return(-3);
}
fclose(f);
if (width!=NULL)
(*width)=retrieve_int32lsbmsb(a);
if (height!=NULL)
(*height)=retrieve_int32lsbmsb(&a[4]);
if (bpp!=NULL)
(*bpp) = a[10];
return(0);
}
int bmp_read_bmp8(WILLUSBITMAP *bmap,char *filename,FILE *out)
{
FILE *f;
char a[20];
int i,k,pixwidth,pixheight,bytewidth;
long filelen,bytesize;
static char palette[1024];
f=wfile_fopen_utf8(filename,"rb");
if (f==NULL)
{
if (out!=NULL)
fprintf(out,"Can't open file %s for input.\n",filename);
return(-1);
}
fseek(f,0L,2);
filelen=ftell(f);
if (filelen<1078)
{
fclose(f);
if (out!=NULL)
fprintf(out,"Input file %s is too small to be an 8-bit BMP file.\n",filename);
return(-2);
}
fseek(f,18L,0);
if (fread(a,sizeof(char),20,f)<20)
{
fclose(f);
if (out!=NULL)
fprintf(out,"Error reading BMP file %s.\n",filename);
return(-3);
}
bmap->width=pixwidth=retrieve_int32lsbmsb(a);
bmap->height=pixheight=retrieve_int32lsbmsb(&a[4]);
if (a[10]!=8)
{
fclose(f);
if (out!=NULL)
fprintf(out,"BMP file %s is not 8-bit.\n",filename);
return(-9);
}
bmap->bpp=8;
if (out!=NULL)
fprintf(out,"Image %s is %d x %d pixels.\n",filename,pixwidth,pixheight);
bytewidth=bmp_bytewidth_win32(bmap);
bytesize=bytewidth*pixheight;
if (filelen < bytesize+1078)
{
if (out!=NULL)
fprintf(out,"File %s is too short. Perhaps it is not a BMP file.\n",filename);
fclose(f);
return(-4);
}
if (!bmp_alloc(bmap))
{
fclose(f);
if (out!=NULL)
fprintf(out,"Cannot allocate memory for bitmap.\n");
return(-5);
}
/* Get DPI */
{
int dpm1,dpm2,dpm;
fseek(f,38L,0);
dpm1=fgetc(f);
dpm2=fgetc(f);
dpm=dpm1|(dpm2<<8);
bmp_dpi = dpm*.0254;
}
/* Read palette */
fseek(f,54L,0);
if (fread(palette,sizeof(char),1024,f)<1024)
{
if (out!=NULL)
fprintf(out,"Cannot read color palette from file %s.\n",filename);
fclose(f);
return(-6);
}
for (i=0;i<256;i++)
{
bmap->blue[i]=(unsigned char)palette[(i<<2)+0];
bmap->green[i]=(unsigned char)palette[(i<<2)+1];
bmap->red[i]=(unsigned char)palette[(i<<2)+2];
}
/* Read bitmap data */
fseek(f,1078L,0);
if (bmap->type == WILLUSBITMAP_TYPE_WIN32)
{
int n;
n=fread(bmap->data,sizeof(char),bytewidth*bmap->height,f);
if (n<bytewidth*bmap->height)
{
if (out!=NULL)
fprintf(out,"Premature EOF reading BMP8 file %s.\n",filename);
bmp_free(bmap);
fclose(f);
return(-7);
}
}
else
for (k=0;k<pixheight;k++)
{
int n;
fseek(f,1078L+bytewidth*k,0);
n=fread(&bmap->data[bmap->width*(bmap->height-1-k)],sizeof(char),bmap->width,f);
if (n<bmap->width)
{
if (out!=NULL)
fprintf(out,"Premature EOF reading BMP8 file %s.\n",filename);
bmp_free(bmap);
fclose(f);
return(-7);
}
}
fclose(f);
return(0);
}
int bmp_read_bmp24(WILLUSBITMAP *bmap,char *filename,FILE *out)
{
FILE *f;
char a[20];
int k,pixwidth,pixheight,extra,bpp;
long filelen,bytewidth,bw32,totalbytes;
static char *funcname="bmp_read_bmp24";
f=wfile_fopen_utf8(filename,"rb");
if (f==NULL)
{
if (out!=NULL)
fprintf(out,"Can't open file %s for input.\n",filename);
return(-1);
}
fseek(f,0L,2);
filelen=ftell(f);
if (filelen<54)
{
fclose(f);
if (out!=NULL)
fprintf(out,"Input file %s is too small to be an 24/32-bit BMP file.\n",filename);
return(-2);
}
fseek(f,18L,0);
if (fread(a,sizeof(char),20,f)<20)
{
fclose(f);
if (out!=NULL)
fprintf(out,"Error reading BMP file %s.\n",filename);
return(-3);
}
bpp=a[10];
if (bpp!=24 && bpp!=32)
{
fclose(f);
if (out!=NULL)
fprintf(out,"BMP file %s is not 24/32-bit.\n",filename);
return(-9);
}
bmap->bpp=24;
bmap->width=pixwidth=retrieve_int32lsbmsb(a);
bmap->height=pixheight=retrieve_int32lsbmsb(&a[4]);
if (out!=NULL)
fprintf(out,"Image %s is %d x %d pixels.\n",filename,pixwidth,pixheight);
bw32 = bmp_bytewidth_win32(bmap);
totalbytes = bw32*pixheight;
if (filelen < totalbytes+54)
{
if (out!=NULL)
fprintf(out,"File %s is too short. Perhaps it is not a BMP file.\n",filename);
fclose(f);
return(-4);
}
if (!bmp_alloc(bmap))
{
fclose(f);
if (out!=NULL)
fprintf(out,"Cannot allocate memory for bitmap.\n");
return(-5);
}
{
int dpm1,dpm2,dpm;
fseek(f,38L,0);
dpm1=fgetc(f);
dpm2=fgetc(f);
dpm=dpm1|(dpm2<<8);
bmp_dpi = dpm*.0254;
}
/* Read bitmap data */
fseek(f,54L,0);
if (bmap->type == WILLUSBITMAP_TYPE_WIN32)
{
int n;
n=fread(bmap->data,sizeof(char),totalbytes,f);
if (n<totalbytes)
{
if (out!=NULL)
fprintf(out,"Premature EOF reading BMP file %s.\n",filename);
bmp_free(bmap);
fclose(f);
return(-7);
}
}
else if (bpp==24)
{
bytewidth=pixwidth*3;
extra = bw32 - bytewidth;
for (k=0;k<bmap->height;k++)
{
int n;
char *p;
p=(char *)&bmap->data[bytewidth*(bmap->height-1-k)];
n=fread(p,sizeof(char),bytewidth,f);
if (n<bytewidth)
{
if (out!=NULL)
fprintf(out,"Premature EOF reading BMP file %s.\n",filename);
bmp_free(bmap);
fclose(f);
return(-7);
}
if (extra)
fseek(f,extra,1);
}
bmp24_flip_rgb(bmap);
}
else /* bpp==32 */
{
unsigned char *fdata;
int bw4;
bytewidth=pixwidth*3;
bw4=bmap->width*4;
willus_mem_alloc_warn((void **)&fdata,bw4,funcname,10);
for (k=0;k<bmap->height;k++)
{
int m;
char *p;
unsigned char *fd0;
p=(char *)&bmap->data[bytewidth*(bmap->height-1-k)];
if (fread(fdata,sizeof(char),bw4,f)<bw4)
{
if (out!=NULL)
fprintf(out,"Premature EOF reading BMP file %s.\n",filename);
willus_mem_free((double **)&fdata,funcname);
bmp_free(bmap);
fclose(f);
return(-7);
}
for (fd0=fdata,m=0;m<bmap->width;m++,fd0++)
{
int kk;
for (kk=0;kk<3;kk++,fd0++,p++)
(*p)=(*fd0);
}
}
willus_mem_free((double **)&fdata,funcname);
bmp24_flip_rgb(bmap);
}
fclose(f);
return(0);
}
void bmp24_reduce_size(WILLUSBITMAP *bmp,int mx,int my)
{
int i,j,nw,nh,bw,nbw,c;
if (bmp->bpp!=24)
return;
c = mx*my;
if (c<=0)
return;
nw = bmp->width/mx;
nh = bmp->height/my;
bw=bmp_bytewidth(bmp);
nbw = bmp->type==WILLUSBITMAP_TYPE_WIN32 ? (nw*3+3)&(~0x3) : nw*3;
for (j=0;j<nh;j++)
{
int iy;
iy = j*my;
for (i=0;i<nw;i++)
{
int sum0,sum1,sum2,dx,dy,ix;
unsigned char *p;
ix = i*mx;
sum0=sum1=sum2=0;
for (dx=0;dx<mx;dx++)
for (dy=0;dy<my;dy++)
{
p = &bmp->data[(iy+dy)*bw + (ix+dx)*3];
sum0 += p[0];
sum1 += p[1];
sum2 += p[2];
}
p = &bmp->data[j*nbw + i*3];
p[0] = (sum0+c/2)/c;
p[1] = (sum1+c/2)/c;
p[2] = (sum2+c/2)/c;
}
}
bmp->width = nw;
bmp->height = nh;
}
void bmp24_flip_rgb(WILLUSBITMAP *bmp)
{
int n,bw,i;
unsigned char *p;
unsigned char t;
if (bmp->bpp!=24)
return;
bw=bmp_bytewidth(bmp);
for (i=0;i<bmp->height;i++)
for (p=&bmp->data[i*bw],n=bmp->width;n>0;n--,p+=3)
{
t=p[0];
p[0]=p[2];
p[2]=t;
}
}
/*
** Returns -1 if enough bytes allocated to promote 8-bit to 24-bit
** (and does the promotion)
**
** Returns 0 if not enough bytes or if bitmap is not 8-bits.
**
*/
int bmp_promote_to_24(WILLUSBITMAP *bmp)
{
int oldbpr,newbpr,rownum,colnum;
/* static char *funcname="bmp_promote_to_24"; */
if (bmp->bpp!=8)
return(0);
oldbpr = bmp_bytewidth(bmp);
bmp->bpp=24;
newbpr = bmp_bytewidth(bmp);
if (!bmp_alloc(bmp))
{
bmp->bpp=8;
return(0);
}
for (rownum = bmp->height-1;rownum>=0;rownum--)
{
unsigned char *oldp,*newp;
oldp = &bmp->data[oldbpr*rownum];
newp = &bmp->data[newbpr*rownum + bmp->width*3-1];
for (colnum = bmp->width-1;colnum>=0;colnum--)
{
(*newp) = bmp->red[oldp[colnum]];
newp--;
(*newp) = bmp->green[oldp[colnum]];
newp--;
(*newp) = bmp->blue[oldp[colnum]];
newp--;
}
}
if (bmp->type != WILLUSBITMAP_TYPE_WIN32)
bmp24_flip_rgb(bmp);
return(-1);
}
void bmp_convert_to_greyscale(WILLUSBITMAP *bmp)
{
bmp_convert_to_greyscale_ex(bmp,bmp);
}
/*
** Convert bitmap to grey-scale in-situ
*/
void bmp_convert_to_greyscale_ex(WILLUSBITMAP *dst,WILLUSBITMAP *src)
{
int oldbpr,newbpr,bpp,dp,rownum,colnum,i;
oldbpr=bmp_bytewidth(src);
dp = src->bpp==8 ? 1 : 3;
bpp=src->bpp;
dst->bpp=8;
for (i=0;i<256;i++)
dst->red[i]=dst->green[i]=dst->blue[i]=i;
if (dst!=src)
{
dst->width=src->width;
dst->height=src->height;
bmp_alloc(dst);
}
newbpr=bmp_bytewidth(dst);
/* Possibly restore src->bpp to 24 so RGBGET works right (src & dst may be the same) */
src->bpp=bpp;
for (rownum=0;rownum<src->height;rownum++)
{
unsigned char *oldp,*newp;
oldp = &src->data[oldbpr*rownum];
newp = &dst->data[newbpr*rownum];
for (colnum=0;colnum<src->width;colnum++,oldp+=dp,newp++)
{
int r,g,b;
RGBGET(src,oldp,r,g,b);
(*newp)=bmp8_greylevel_convert(r,g,b);
}
}
dst->bpp=8; /* Possibly restore dst->bpp to 8 since src & dst may be the same. */
}
/*
** Return pix value (0.0 - 255.0) in double precision given
** a double precision position. Bitmap is assumed to be 8-bit greyscale.
**
** x0,y0 are from bottom corner.
** x0=0.5, y0=0.5 would give exactly the value of the pixel
** in the lower left corner of the bitmap.
*/
double bmp_grey_pix_vald(WILLUSBITMAP *bmp,double x0,double y0)
{
int ix0,iy0,ix1,iy1;
double fx0,fx1,fy0,fy1;
ix0 = (int)(x0-.5);
ix1 = ix0+1;
iy0 = (int)(y0-.5);
iy1 = iy0+1;
BOUND(ix0,0,bmp->width-1);
BOUND(ix1,0,bmp->width-1);
BOUND(iy0,0,bmp->height-1);
BOUND(iy1,0,bmp->height-1);
fx0 = 1.-fabs(ix0+0.5-x0);
if (fx0<0.)
fx0=0.;
fx1 = 1.-fabs(ix1+0.5-x0);
if (fx1<0.)
fx1=0.;
fy0 = 1.-fabs(iy0+0.5-y0);
if (fy0<0.)
fy0=0.;
fy1 = 1.-fabs(iy1+0.5-y0);
if (fy1<0.)
fy1=0.;
if ((fx0==0. && fx1==0.) || (fy0==0. && fy1==0.))
return(-1.);
return( ( fy0 * ( fx0*bmp_grey_pix_vali(bmp,ix0,iy0)
+ fx1*bmp_grey_pix_vali(bmp,ix1,iy0) )
+ fy1 * ( fx0*bmp_grey_pix_vali(bmp,ix0,iy1)
+ fx1*bmp_grey_pix_vali(bmp,ix1,iy1) ) )
/ ( (fx0+fx1) * (fy0+fy1) ) );
}
/*
** Return pix values (0.0 - 255.0) in double precision given
** a double precision position.
**
** x0,y0 are from BOTTOM CORNER.
** x0=0.5, y0=0.5 would give exactly the value of the pixel
** in the lower left corner of the bitmap.
*/
void bmp_pix_vald(WILLUSBITMAP *bmp,double x0,double y0,
double *r,double *g,double *b)
{
int ix0,iy0,ix1,iy1;
double fx0,fx1,fy0,fy1;
int r00,r10,r01,r11;
int g00,g10,g01,g11;
int b00,b10,b01,b11;
ix0 = (int)(x0-.5);
ix1 = ix0+1;
iy0 = (int)(y0-.5);
iy1 = iy0+1;
BOUND(ix0,0,bmp->width-1);
BOUND(ix1,0,bmp->width-1);
BOUND(iy0,0,bmp->height-1);
BOUND(iy1,0,bmp->height-1);
fx0 = 1.-fabs(ix0+0.5-x0);
if (fx0<0.)
fx0=0.;
fx1 = 1.-fabs(ix1+0.5-x0);
if (fx1<0.)
fx1=0.;
fy0 = 1.-fabs(iy0+0.5-y0);
if (fy0<0.)
fy0=0.;
fy1 = 1.-fabs(iy1+0.5-y0);
if (fy1<0.)
fy1=0.;
if ((fx0==0. && fx1==0.) || (fy0==0. && fy1==0.))
{
(*r) = (*g) = (*b) = -1.;
return;
}
bmp_pix_vali(bmp,ix0,iy0,&r00,&g00,&b00);
bmp_pix_vali(bmp,ix1,iy0,&r10,&g10,&b10);
bmp_pix_vali(bmp,ix0,iy1,&r01,&g01,&b01);
bmp_pix_vali(bmp,ix1,iy1,&r11,&g11,&b11);
(*r)=((fy0*(fx0*r00+fx1*r10)+fy1*(fx0*r01+fx1*r11))/((fx0+fx1)*(fy0+fy1)));
(*g)=((fy0*(fx0*g00+fx1*g10)+fy1*(fx0*g01+fx1*g11))/((fx0+fx1)*(fy0+fy1)));
(*b)=((fy0*(fx0*b00+fx1*b10)+fy1*(fx0*b01+fx1*b11))/((fx0+fx1)*(fy0+fy1)));
}
/*
** y0 = 0 ==> bottom row!
*/
int bmp_grey_pix_vali(WILLUSBITMAP *bmp,int x0,int y0)
{
unsigned char *p;
int r,g,b;
p = bmp_rowptr_from_top(bmp,bmp->height-1-y0);
p = &p[x0*(bmp->bpp>>3)];
RGBGET(bmp,p,r,g,b);
return(bmp8_greylevel_convert(r,g,b));
}
/*
** y0 = 0 ==> bottom row!
*/
void bmp_pix_vali(WILLUSBITMAP *bmp,int x0,int y0,int *r,int *g,int *b)
{
unsigned char *p;
int rr,gg,bb;
p = bmp_rowptr_from_top(bmp,bmp->height-1-y0);
p = &p[x0*(bmp->bpp>>3)];
RGBGET(bmp,p,rr,gg,bb);
(*r)=rr;
(*g)=gg;
(*b)=bb;
}
void bmp_grey_pixel_setd(WILLUSBITMAP *bmp,double x0,double y0,int grey)
{
bmp_rgb_pixel_setd(bmp,x0,y0,grey,grey,grey);
}
void bmp_rgb_pixel_setd(WILLUSBITMAP *bmp,double x0,double y0,int r,int g,int b)
{
int ix0,iy0,ix1,iy1;
double fx0,fx1,fy0,fy1;
ix0 = (int)(x0-.5);
ix1 = ix0+1;
iy0 = (int)(y0-.5);
iy1 = iy0+1;
BOUND(ix0,0,bmp->width-1);
BOUND(ix1,0,bmp->width-1);
BOUND(iy0,0,bmp->height-1);
BOUND(iy1,0,bmp->height-1);
fx0 = 1.-fabs(ix0+0.5-x0);
if (fx0<0.)
fx0=0.;
fx1 = 1.-fabs(ix1+0.5-x0);
if (fx1<0.)
fx1=0.;
fy0 = 1.-fabs(iy0+0.5-y0);
if (fy0<0.)
fy0=0.;
fy1 = 1.-fabs(iy1+0.5-y0);
if (fy1<0.)
fy1=0.;
if ((fx0==0. && fx1==0.) || (fy0==0. && fy1==0.))
return;
bmp_rgb_pixel_setf(bmp,ix0,iy0,r,g,b,fx0*fy0);
bmp_rgb_pixel_setf(bmp,ix1,iy0,r,g,b,fx1*fy0);
bmp_rgb_pixel_setf(bmp,ix0,iy1,r,g,b,fx0*fy1);
bmp_rgb_pixel_setf(bmp,ix1,iy1,r,g,b,fx1*fy1);
}
void bmp_grey_pixel_setf(WILLUSBITMAP *bmp,int x,int y,int grey,double f)
{
bmp_rgb_pixel_setf(bmp,x,y,grey,grey,grey,f);
}
void bmp_rgb_pixel_setf(WILLUSBITMAP *bmp,int x,int y,int r,int g,int b,double f)
{
unsigned char *p;
int pv;
p = bmp_rowptr_from_top(bmp,bmp->height-1-y);
p = &p[x*(bmp->bpp>>3)];
if (bmp->bpp==24)
{
int ir,ib;
ir = bmp->type==WILLUSBITMAP_TYPE_WIN32 ? 2 : 0;
ib = 2-ir;
pv = f*r + (1.-f)*p[ir];
BOUND(pv,0,255)
p[ir] = pv;
pv = f*g + (1.-f)*p[1];
BOUND(pv,0,255)
p[1] = pv;
pv = f*b + (1.-f)*p[ib];
BOUND(pv,0,255)
p[ib] = pv;
}
else
{
pv = f*((r+g+b)/3) + (1.-f)*(*p);
BOUND(pv,0,255)
(*p)=pv;
}
}
void bmp_resize(WILLUSBITMAP *bmp,double scalefactor)
{
WILLUSBITMAP *copy,_copy;
copy=&_copy;
bmp_init(copy);
bmp_copy(copy,bmp);
bmp->width = bmp->width*scalefactor+0.5;
bmp->height = bmp->height*scalefactor+0.5;
bmp_resample(bmp,copy,0.,0.,(double)copy->width,(double)copy->height,
bmp->width,bmp->height);
bmp_free(copy);
}
/*
** Fast all-integer resample.
**
** Resample bitmap to be an integer size smaller, e.g.
** every n x n pixels in the source bitmap map to one pixel in the
** destination bitmap.
**
** This can be done with bmp_resample(), but this function is faster.
**
** The destination bitmap will be 8-bit grayscale if the source bitmap
** passes the bmp_is_grayscale() function. Otherwise it will be 24-bit.
**
*/
void bmp_integer_resample(WILLUSBITMAP *dest,WILLUSBITMAP *src,int n)
{
int gray,np,n2,colorplanes,sbw;
int color,row,col;
dest->width = (src->width+(n-1))/n;
dest->height = (src->height+(n-1))/n;
if ((gray=bmp_is_grayscale(src))!=0)
{
int i;
dest->bpp=8;
for (i=0;i<256;i++)
dest->red[i]=dest->blue[i]=dest->green[i]=i;
}
else
dest->bpp=24;
dest->type=WILLUSBITMAP_TYPE_NATIVE;
bmp_alloc(dest);
np=n*n;
n2=np/2;
colorplanes = gray ? 1 : 3;
sbw=bmp_bytewidth(src);
for (color=0;color<colorplanes;color++)
{
int drow;
for (drow=0;drow<dest->height;drow++)
{
unsigned char *dp;
unsigned char *sp1;
int r1,r2,dr,dcol;
r1=drow*n;
r2=r1+n;
if (r2>src->height)
r2=src->height;
sp1=bmp_rowptr_from_top(src,r1)+color;
dp=bmp_rowptr_from_top(dest,drow)+color;
dr=r2-r1;
for (dcol=0;dcol<dest->width;dcol++,dp+=colorplanes)
{
int pixsum,c1,c2,c1x,c2x,dc;
unsigned char *sp;
c1=dcol*n;
c2=c1+n;
if (c2>src->width)
c2=src->width;
dc=c2-c1;
np=dc*dr;
n2=np/2;
c1x=c1*colorplanes;
c2x=c2*colorplanes;
for (pixsum=n2,row=r1,sp=sp1;row<r2;row++,sp+=sbw)
for (col=c1x;col<c2x;col+=colorplanes)
pixsum += sp[col];
pixsum /= np;
(*dp)=pixsum;
}
}
}
}
void bmp_draw_filled_rect(WILLUSBITMAP *bmp,int col1,int row1,int col2,int row2,
int r,int g,int b)
{
if (row1<0)
row1=0;
if (row1>bmp->height-1)
row1=bmp->height-1;
if (row2<0)
row2=0;
if (row2>bmp->height-1)
row2=bmp->height-1;
if (col1<0)
col1=0;
if (col1>bmp->width-1)
col1=bmp->width-1;
if (col2<0)
col2=0;
if (col2>bmp->width-1)
col2=bmp->width-1;
if (row2<row1)
{
int t;
t=row2;
row2=row1;
row1=t;
}
if (col2<col1)
{
int t;
t=col2;
col2=col1;
col1=t;
}
if (bmp->bpp==24)
{
int row;
for (row=row1;row<=row2;row++)
{
int col;
unsigned char *p;
p=bmp_rowptr_from_top(bmp,row)+col1*3;
for (col=col1;col<=col2;col++,p+=3)
{
RGBSET24(bmp,p,r,g,b);
}
}
}
else
{
int row;
for (row=row1;row<=row2;row++)
{
int col;
unsigned char *p;
p=bmp_rowptr_from_top(bmp,row)+col1;
for (col=col1;col<=col2;col++,p++)
(*p)=r;
}
}
}
/*
** Resample (re-size) bitmap. The pixel positions left to right go from
** 0.0 to src->width (x-coord), and top to bottom go from
** 0.0 to src->height (y-coord).
** The cropped rectangle (x1,y1) to (x2,y2) is placed into
** the destination bitmap, which need not be allocated yet.
**
** The destination bitmap will be 8-bit grayscale if the source bitmap
** passes the bmp_is_grayscale() function. Otherwise it will be 24-bit.
**
** Returns 0 for okay.
** -1 for not enough memory.
** -2 for bad cropping area or destination bitmap size
*/
int bmp_resample(WILLUSBITMAP *dest,WILLUSBITMAP *src,double x1,double y1,
double x2,double y2,int newwidth,int newheight)
{
int gray,maxlen,colorplanes;
double t;
double *tempbmp;
double *temprow;
int color,hmax,row,col,dy;
static char *funcname="bmp_resample";
if (newwidth==0 || newheight==0)
return(-1);
/* Quick check if we should use simple bmp_copy() call */
if (x1==0. && y1==0. && x2==newwidth && y2==newheight)
{
bmp_copy(dest,src);
return(0);
}
/* Clip and sort x1,y1 and x2,y2 */
if (x1>src->width)
x1=src->width;
else if (x1<0.)
x1=0.;
if (x2>src->width)
x2=src->width;
else if (x2<0.)
x2=0.;
if (y1>src->height)
y1=src->height;
else if (y1<0.)
y1=0.;
if (y2>src->height)
y2=src->height;
else if (y2<0.)
y2=0.;
if (x2<x1)
{
t=x2;
x2=x1;
x1=t;
}
if (y2<y1)
{
t=y2;
y2=y1;
y1=t;
}
dy=y2-y1;
dy += 2;
if (x2-x1==0. || y2-y1==0.)
return(-2);
/* Allocate temp storage */
maxlen = x2-x1 > dy+newheight ? (int)(x2-x1) : dy+newheight;
maxlen += 16;
hmax = newheight > dy ? newheight : dy;
if (!willus_mem_alloc(&temprow,maxlen*sizeof(double),funcname))
return(-1);
if (!willus_mem_alloc(&tempbmp,hmax*newwidth*sizeof(double),funcname))
{
willus_mem_free(&temprow,funcname);
return(-1);
}
if ((gray=bmp_is_grayscale(src))!=0)
{
int i;
dest->bpp=8;
for (i=0;i<256;i++)
dest->red[i]=dest->blue[i]=dest->green[i]=i;
}
else
dest->bpp=24;
dest->width=newwidth;
dest->height=newheight;
dest->type=WILLUSBITMAP_TYPE_NATIVE;
if (!bmp_alloc(dest))
{
willus_mem_free(&tempbmp,funcname);
willus_mem_free(&temprow,funcname);
return(-1);
}
colorplanes = gray ? 1 : 3;
for (color=0;color<colorplanes;color++)
{
bmp_resample_1(tempbmp,src,x1,y1,x2,y2,newwidth,newheight,
temprow,gray ? -1 : color);
for (row=0;row<newheight;row++)
{
unsigned char *p;
double *s;
p=bmp_rowptr_from_top(dest,row)+color;
s=&tempbmp[row*newwidth];
if (colorplanes==1)
for (col=0;col<newwidth;p[0]=(int)(s[0]+.5),col++,s++,p++);
else
for (col=0;col<newwidth;p[0]=(int)(s[0]+.5),col++,s++,p+=colorplanes);
}
}
willus_mem_free(&tempbmp,funcname);
willus_mem_free(&temprow,funcname);
return(0);
}
static void bmp_resample_1(double *tempbmp,WILLUSBITMAP *src,double x1,double y1,
double x2,double y2,int newwidth,int newheight,
double *temprow,int color)
{
int row,col,x0,dx,y0,dy;
x0=floor(x1);
dx=ceil(x2)-x0;
x1-=x0;
x2-=x0;
y0=floor(y1);
dy=ceil(y2)-y0;
y1-=y0;
y2-=y0;
if (src->type==WILLUSBITMAP_TYPE_WIN32 && color>=0)
color=2-color;
for (row=0;row<dy;row++)
{
unsigned char *p;
p=bmp_rowptr_from_top(src,row+y0);
if (src->bpp==8)
{
switch (color)
{
case -1:
for (col=0,p+=x0;col<dx;col++,p++)
temprow[col]=p[0];
break;
case 0:
for (col=0,p+=x0;col<dx;col++,p++)
temprow[col]=src->red[p[0]];
break;
case 1:
for (col=0,p+=x0;col<dx;col++,p++)
temprow[col]=src->green[p[0]];
break;
case 2:
for (col=0,p+=x0;col<dx;col++,p++)
temprow[col]=src->blue[p[0]];
break;
}
}
else
{
p+=color;
for (col=0,p+=3*x0;col<dx;temprow[col]=p[0],col++,p+=3);
}
resample_1d(&tempbmp[row*newwidth],temprow,x1,x2,newwidth);
}
for (col=0;col<newwidth;col++)
{
double *p,*s;
p=&tempbmp[col];
s=&temprow[dy];
for (row=0;row<dy;row++,p+=newwidth)
temprow[row]=p[0];
resample_1d(s,temprow,y1,y2,newheight);
p=&tempbmp[col];
for (row=0;row<newheight;row++,p+=newwidth,s++)
p[0]=s[0];
}
}
/*
** Resample src[] into dst[].
** Examples: resample_1d(dst,src,0.,5.,5) would simply copy the
** first five elements of src[] to dst[].
**
** resample_1d(dst,src,0.,5.,10) would work as follows:
** dst[0] and dst[1] would get src[0].
** dst[2] and dst[3] would get src[1].
** and so on.
**
*/
static void resample_1d(double *dst,double *src,double x1,double x2,int n)
{
int i;
double new,last;
last=x1;
for (i=0;i<n;i++)
{
new=x1+(x2-x1)*(i+1)/n;
dst[i] = resample_single(src,last,new);
last=new;
}
}
static double resample_single(double *y,double x1,double x2)
{
int i,i1,i2;
double dx,dx1,dx2,sum;
i1=floor(x1);
i2=floor(x2);
if (i1==i2)
return(y[i1]);
dx=x2-x1;
if (dx>1.)
dx=1.;
dx1= 1.-(x1-i1);
dx2= x2-i2;
sum=0.;
if (dx1 > 1e-8*dx)
sum += dx1*y[i1];
if (dx2 > 1e-8*dx)
sum += dx2*y[i2];
for (i=i1+1;i<=i2-1;sum+=y[i],i++);
return(sum/(x2-x1));
}
/*
** Determine fixed-point accuracy of resample function
*/
#define FPBITS 10
#define FPPIXBITS 8
/*
** Derived quantities
*/
#define FPMULT (1<<FPBITS)
#define FPMAX ((1<<(31-FPBITS))-1)
#define FPPIXMULT (1<<FPPIXBITS)
#define FPARMAX (1<<(22-FPPIXBITS))
#define FPDIMMAX (1<<(30-FPBITS))
/*
** Resample (re-size) bitmap, but use fixed-point and all integer math.
** Only slightly less accurate than floating point, but faster.
*/
int bmp_resample_fixed_point(WILLUSBITMAP *dest,WILLUSBITMAP *src,double fx1,double fy1,
double fx2,double fy2,int newwidth,int newheight)
{
int gray,maxlen,colorplanes;
/* Assumes integers are 32-bit */
int *tempbmp;
int *temprow;
int x1_fp,y1_fp,x2_fp,y2_fp,swidth_fp,sheight_fp;
int color,hmax,row,col,dy,dx;
static char *funcname="bmp_resample";
if (newwidth==0 || newheight==0)
return(-1);
/* Quick check if we should use simple bmp_copy() call */
if (fx1==0. && fy1==0. && fx2==newwidth && fy2==newheight)
{
bmp_copy(dest,src);
return(0);
}
/*
** Make sure we won't have fixed-precision overruns.
** If so, just use the float routine
*/
if (fabs(fx2-fx1)*fabs(fy2-fy1)/(newheight*newwidth) > FPARMAX
|| fx1>FPDIMMAX || fx2>FPDIMMAX || fy1>FPDIMMAX || fy2>FPDIMMAX
|| newwidth>FPDIMMAX || newheight>FPDIMMAX
|| src->width>FPDIMMAX || src->height>FPDIMMAX)
return(bmp_resample(dest,src,fx1,fy1,fx2,fy2,newwidth,newheight));
/* Convert to fixed-point integers */
x1_fp=fx1*FPMULT+0.5;
y1_fp=fy1*FPMULT+0.5;
x2_fp=fx2*FPMULT+0.5;
y2_fp=fy2*FPMULT+0.5;
swidth_fp = src->width<<FPBITS;
sheight_fp = src->height<<FPBITS;
/* Clip and sort x1,y1 and x2,y2 */
if (x1_fp>swidth_fp)
x1_fp=swidth_fp;
else if (x1_fp<0)
x1_fp=0;
if (x2_fp>swidth_fp)
x2_fp=swidth_fp;
else if (x2_fp<0)
x2_fp=0;
if (y1_fp>sheight_fp)
y1_fp=sheight_fp;
else if (y1_fp<0)
y1_fp=0;
if (y2_fp>sheight_fp)
y2_fp=sheight_fp;
else if (y2_fp<0)
y2_fp=0;
if (x2_fp<x1_fp)
{
int t;
t=x2_fp;
x2_fp=x1_fp;
x1_fp=t;
}
if (y2_fp<y1_fp)
{
int t;
t=y2_fp;
y2_fp=y1_fp;
y1_fp=t;
}
dy=(y2_fp-y1_fp)>>FPBITS;
dy += 2;
if (x2_fp==x1_fp || y2_fp==y1_fp)
return(-2);
/* Allocate temp storage */
dx = (x2_fp-x1_fp)>>FPBITS;
maxlen = dx > dy+newheight ? dx : dy+newheight;
maxlen += 16;
hmax = newheight > dy ? newheight : dy;
if (!willus_mem_alloc((double **)&temprow,maxlen*sizeof(int),funcname))
return(-1);
if (!willus_mem_alloc((double **)&tempbmp,hmax*newwidth*sizeof(int),funcname))
{
willus_mem_free((double **)&temprow,funcname);
return(-1);
}
if ((gray=bmp_is_grayscale(src))!=0)
{
int i;
dest->bpp=8;
for (i=0;i<256;i++)
dest->red[i]=dest->blue[i]=dest->green[i]=i;
}
else
dest->bpp=24;
dest->width=newwidth;
dest->height=newheight;
dest->type=WILLUSBITMAP_TYPE_NATIVE;
if (!bmp_alloc(dest))
{
willus_mem_free((double **)&tempbmp,funcname);
willus_mem_free((double **)&temprow,funcname);
return(-1);
}
colorplanes = gray ? 1 : 3;
for (color=0;color<colorplanes;color++)
{
bmp_resample_1_fixed_point(tempbmp,src,x1_fp,y1_fp,x2_fp,y2_fp,newwidth,newheight,
temprow,gray ? -1 : color);
for (row=0;row<newheight;row++)
{
unsigned char *p;
int *s;
p=bmp_rowptr_from_top(dest,row)+color;
s=&tempbmp[row*newwidth];
if (colorplanes==1)
for (col=0;col<newwidth;p[0]=(s[0]+(FPPIXMULT/2-1))>>FPPIXBITS,col++,s++,p++);
else
for (col=0;col<newwidth;p[0]=(s[0]+(FPPIXMULT/2-1))>>FPPIXBITS,col++,s++,p+=colorplanes);
}
}
willus_mem_free((double **)&tempbmp,funcname);
willus_mem_free((double **)&temprow,funcname);
return(0);
}
static void bmp_resample_1_fixed_point(int *tempbmp,WILLUSBITMAP *src,int x1_fp,int y1_fp,
int x2_fp,int y2_fp,int newwidth,int newheight,
int *temprow,int color)
{
int row,col,x0,dx,y0,dy;
x0=x1_fp>>FPBITS;
dx=((x2_fp+(FPMULT-1))>>FPBITS)-x0;
x1_fp-=(x0<<FPBITS);
x2_fp-=(x0<<FPBITS);
y0=y1_fp>>FPBITS;
dy=((y2_fp+(FPMULT-1))>>FPBITS)-y0;
y1_fp-=(y0<<FPBITS);
y2_fp-=(y0<<FPBITS);
if (src->type==WILLUSBITMAP_TYPE_WIN32 && color>=0)
color=2-color;
for (row=0;row<dy;row++)
{
unsigned char *p;
p=bmp_rowptr_from_top(src,row+y0);
if (src->bpp==8)
{
switch (color)
{
case -1:
for (col=0,p+=x0;col<dx;col++,p++)
temprow[col]=(((unsigned int)p[0])<<FPPIXBITS);
break;
case 0:
for (col=0,p+=x0;col<dx;col++,p++)
temprow[col]=src->red[p[0]]<<FPPIXBITS;
break;
case 1:
for (col=0,p+=x0;col<dx;col++,p++)
temprow[col]=src->green[p[0]]<<FPPIXBITS;
break;
case 2:
for (col=0,p+=x0;col<dx;col++,p++)
temprow[col]=src->blue[p[0]]<<FPPIXBITS;
break;
}
}
else
{
p+=color;
for (col=0,p+=3*x0;col<dx;temprow[col]=(((unsigned int)p[0])<<FPPIXBITS),col++,p+=3);
}
resample_1d_fixed_point(&tempbmp[row*newwidth],temprow,x1_fp,x2_fp,newwidth);
}
for (col=0;col<newwidth;col++)
{
int *p,*s;
p=&tempbmp[col];
s=&temprow[dy];
for (row=0;row<dy;row++,p+=newwidth)
temprow[row]=p[0];
resample_1d_fixed_point(s,temprow,y1_fp,y2_fp,newheight);
p=&tempbmp[col];
for (row=0;row<newheight;row++,p+=newwidth,s++)
p[0]=s[0];
}
}
/*
** Fixed point version of resample_1d().
*/
static void resample_1d_fixed_point(int *dst,int *src,int x1_fp,int x2_fp,int n)
{
int i,new_fp,last_fp;
last_fp=x1_fp;
for (i=0;i<n;i++)
{
/* Needs to go to double to prevent integer overflow */
new_fp=(int)(x1_fp+(double)(x2_fp-x1_fp)*(i+1)/n+.5);
dst[i] = resample_single_fixed_point(src,last_fp,new_fp);
last_fp=new_fp;
}
}
static double resample_single_fixed_point(int *y,int x1_fp,int x2_fp)
{
int i,i1,i2;
int dx_fp,dx1_fp,dx2_fp,sum;
i1=x1_fp>>FPBITS;
i2=x2_fp>>FPBITS;
if (i1==i2)
return(y[i1]);
dx_fp = x2_fp-x1_fp;
dx1_fp = FPMULT-(x1_fp-(i1<<FPBITS));
dx2_fp = x2_fp-(i2<<FPBITS);
sum= ((dx1_fp*y[i1]+(FPMULT/2))>>FPBITS) + ((dx2_fp*y[i2]+(FPMULT/2))>>FPBITS);
for (i=i1+1;i<=i2-1;sum+=y[i],i++);
return((sum<=FPMAX) ? ((sum<<FPBITS)+(FPMULT/2))/dx_fp : ((double)sum*FPMULT+(FPMULT/2))/dx_fp);
}
/*
** Crop all edge pixels of bitmap that match the color of the upper
** left corner pixel
*/
/*
void bmp_autocrop(WILLUSBITMAP *bmp)
{
unsigned char *p0,*p;
int i,j,pbw,imin,imax,jmin,jmax;
p0=bmp_rowptr_from_top(bmp,0);
pbw=bmp->bpp>>3;
imin=bmp->height+1;
imax=-1;
jmax=-1;
jmin=bmp->width+1;
for (i=0;i<bmp->height;i++)
{
p=bmp_rowptr_from_top(bmp,i);
for (j=0;j<bmp->width;j++,p+=pbw)
if (memcmp(p,p0,pbw))
{
if (i<imin)
imin=i;
if (i>imax)
imax=i;
if (j<jmin)
jmin=j;
if (j>jmax)
jmax=j;
}
}
if (imax>=0)
bmp_crop(bmp,imin,jmin,(jmax-jmin+1),(imax-imin+1));
}
*/
/*
** dest bitmap MUST BE 24-bit
*/
void bmp_overlay(WILLUSBITMAP *dest,WILLUSBITMAP *src,int x0,int y0_from_top,
int *dbgc,int *dfgc,int *sbgc,int *sfgc)
{
unsigned char *dp,*sp;
int i,j,sbw;
// printf("@bmp_overlay, x0=%d, y0_from_top=%d\n",x0,y0_from_top);
sbw=src->bpp>>3;
if (dest->bpp!=24)
{
printf("bmp_overlay: destination bitmap must be 24-bit!\n");
return;
}
for (i=0;i<src->height;i++)
{
sp=bmp_rowptr_from_top(src,i);
if (i+y0_from_top < 0 || i+y0_from_top >= dest->height)
continue;
dp=bmp_rowptr_from_top(dest,i+y0_from_top)+3*x0;
for (j=0;j<src->width;j++,dp+=3,sp+=sbw)
{
int spc[3],dpc[3];
if (j+x0<0 || j+x0>=dest->width)
continue;
RGBGET(src,sp,spc[0],spc[1],spc[2]);
RGBGET(dest,dp,dpc[0],dpc[1],dpc[2]);
new_rgb(dpc,spc,dbgc,dfgc,sbgc,sfgc);
RGBSET24(dest,dp,dpc[0],dpc[1],dpc[2]);
}
}
}
void bmp_invert(WILLUSBITMAP *bmp)
{
int i;
if (bmp->bpp==24 || bmp_is_grayscale(bmp))
{
unsigned char *p;
int nb;
p=bmp_rowptr_from_top(bmp,0);
nb=bmp_bytewidth(bmp)*bmp->height;
for (i=0;i<nb;i++,p++)
(*p) = 255-(*p);
}
else
for (i=0;i<256;i++)
{
bmp->red[i]=255-bmp->red[i];
bmp->green[i]=255-bmp->green[i];
bmp->blue[i]=255-bmp->blue[i];
}
}
/*
** Figure out what color to make a destination pixel based on these
** inputs:
** dpc[0..2]=r,g,b original destination pixel color.
** spc[0..2]=r,g,b source pixel color.
** dbgc[0..2]=r,g,b destination bitmap "background" color.
** dfgc[0..2]=r,g,b destination bitmap "foreground" color.
** sbgc[0..2]=r,g,b source background color.
** sfgc[0..2]=r,g,b source foreground color.
**
** Examples:
** new_rgb(dpc,spc,NULL,NULL,NULL,NULL)
** This will simply copy the source pixel color the destination.
**
** new_rgb(dpc,spc,dbgc,dfgc,sbgc,sfgc)
** This will determine how far the source pixel color is from
** its background color (relative to the foreground) and make
** the destination pixel an equivalent distance between its
** background and foreground color.
**
** new_rgb(dpc,spc,NULL,dfgc,sbgc,NULL)
** This is like an OR. Every source pixel that does not exactly
** match the source background color overwrites the destination
** with dfgc (or if dfgc==NULL, spc).
*/
static void new_rgb(int *dpc,int *spc,int *dbgc,int *dfgc,int *sbgc,int *sfgc)
{
double f;
int bgc,i;
if (sbgc==NULL && sfgc==NULL)
{
dpc[0]=spc[0];
dpc[1]=spc[1];
dpc[2]=spc[2];
return;
}
if (sbgc!=NULL && sfgc!=NULL)
{
f=((double)(spc[0]-sbgc[0])/(sfgc[0]-sbgc[0])
+(double)(spc[1]-sbgc[1])/(sfgc[1]-sbgc[1])
+(double)(spc[2]-sbgc[2])/(sfgc[2]-sbgc[2]))/3.;
if (dbgc!=NULL && dfgc!=NULL)
{
for (i=0;i<3;i++)
{
dpc[i]=dbgc[i]+f*(dfgc[i]-dbgc[i]);
BOUND(dpc[i],0,255);
}
return;
}
if (dfgc==NULL)
{
for (i=0;i<3;i++)
{
dpc[i]*=f;
BOUND(dpc[i],0,255);
}
return;
}
for (i=0;i<3;i++)
{
dpc[i]=dfgc[i]*f;
BOUND(dpc[i],0,255);
}
return;
}
if (sbgc!=NULL && sfgc==NULL)
bgc=(spc[0]==sbgc[0] && spc[1]==sbgc[1] && spc[2]==sbgc[2]);
else
bgc=(spc[0]!=sfgc[0] || spc[1]!=sfgc[1] || spc[2]!=sfgc[2]);
if (bgc)
{
if (dbgc!=NULL)
for (i=0;i<3;i++)
dpc[i]=dbgc[i];
return;
}
if (dfgc==NULL)
for (i=0;i<3;i++)
dpc[i]=spc[i];
else
for (i=0;i<3;i++)
dpc[i]=dfgc[i];
}
/*
** One of dest or src can be NULL, which is the
** same as setting them equal to each other, but
** in this case, the bitmap must be 24-bit!
** Note: contrast > 1 will increase the contrast.
** contrast < 1 will decrease the contrast.
** contrast of 0 will make all pixels the same value.
** contrast of 1 will not change the image.
*/
void bmp_contrast_adjust(WILLUSBITMAP *dest,WILLUSBITMAP *src,double contrast)
{
int i;
static unsigned char newval[256];
for (i=0;i<256;i++)
{
double x,y;
int sgn,v;
x=(i-127.5)/127.5;
sgn = x<0 ? -1 : 1;
if (contrast<0)
sgn = -sgn;
x=fabs(x);
if (fabs(contrast)>1.5)
y=x<.99999 ? 1-exp(fabs(contrast)*x/(x-1)) : 1.;
else
{
y=fabs(contrast)*x;
if (y>1.)
y=1.;
}
y = 127.5+y*sgn*127.5;
v = (int)(y+.5);
if (v<0)
v=0;
if (v>255)
v=255;
newval[i] = v;
}
bmp_color_xform(dest,src,newval);
}
/*
** One of dest or src can be NULL, which is the
** same as setting them equal to each other, but
** in this case, the bitmap must be 24-bit!
**
** Note: Used ...pow(i/256.,gc)... before 9-7-11.
*/
void bmp_gamma_correct(WILLUSBITMAP *dest,WILLUSBITMAP *src,double gamma)
{
double gc;
int i;
static unsigned char newval[256];
if (gamma<0.001)
gamma=0.001;
gc=1./gamma;
for (i=0;i<256;i++)
newval[i] = 255.*pow(i/255.,gc)+.5;
bmp_color_xform(dest,src,newval);
}
/*
** One of dest or src can be NULL, which is the
** same as setting them equal to each other, but
** in this case, the bitmap must be 24-bit!
*/
void bmp_color_xform(WILLUSBITMAP *dest,WILLUSBITMAP *src,unsigned char *newval)
{
int ir,ic;
if (src==NULL)
src=dest;
if (dest==NULL)
dest=src;
if (bmp_is_grayscale(src))
{
bmp_color_xform8(dest,src,newval);
return;
}
if (dest!=src)
{
dest->width = src->width;
dest->height = src->height;
dest->bpp = 24;
bmp_alloc(dest);
}
for (ir=0;ir<src->height;ir++)
{
unsigned char *sp,*dp;
sp=bmp_rowptr_from_top(src,ir);
dp=bmp_rowptr_from_top(dest,ir);
for (ic=0;ic<src->width;ic++,dp+=3)
{
int r,g,b;
RGBGETINCPTR(src,sp,r,g,b);
r=newval[r];
g=newval[g];
b=newval[b];
RGBSET24(dest,dp,r,g,b);
}
}
}
/*
** One of dest or src can be NULL, which is the
** same as setting them equal to each other, but
** in this case, the bitmap must be 24-bit!
*/
int bmp_is_grayscale(WILLUSBITMAP *bmp)
{
int i;
if (bmp->bpp!=8)
return(0);
for (i=0;i<256;i++)
if (bmp->red[i]!=i || bmp->green[i]!=i || bmp->blue[i]!=i)
return(0);
return(1);
}
static void bmp_color_xform8(WILLUSBITMAP *dest,WILLUSBITMAP *src,unsigned char *newval)
{
int i,ir;
if (src==NULL)
src=dest;
if (dest==NULL)
dest=src;
if (dest!=src)
{
dest->width = src->width;
dest->height = src->height;
dest->bpp = 8;
for (i=0;i<256;i++)
dest->red[i]=dest->green[i]=dest->blue[i]=i;
bmp_alloc(dest);
}
for (ir=0;ir<src->height;ir++)
{
unsigned char *sp,*dp;
sp=bmp_rowptr_from_top(src,ir);
dp=bmp_rowptr_from_top(dest,ir);
for (i=0;i<src->width;i++)
dp[i]=newval[sp[i]];
}
}
void bmp_erode(WILLUSBITMAP *dst0,WILLUSBITMAP *src)
{
WILLUSBITMAP *dst,_dst;
int plane;
if (dst0==src || dst0==NULL)
{
dst=&_dst;
bmp_init(dst);
}
else
dst=dst0;
bmp_copy(dst,src);
if (bmp_is_grayscale(src))
bmp_one_component_erode(src,dst,0,1);
else
{
if (src->bpp==8)
bmp_promote_to_24(src);
for (plane=0;plane<3;plane++)
bmp_one_component_erode(src,dst,plane,3);
}
if (dst0==src || dst0==NULL)
{
bmp_copy(src,dst);
bmp_free(dst);
}
}
static void bmp_one_component_erode(WILLUSBITMAP *src,WILLUSBITMAP *dst,int offsetplane,int bytesperpixel)
{
int r;
for (r=0;r<src->height;r++)
{
unsigned char *p[3];
unsigned char *d;
int a[9];
int c,min;
p[0]= (r==0) ? NULL : bmp_rowptr_from_top(src,r-1)+offsetplane;
p[1] = bmp_rowptr_from_top(src,r)+offsetplane;
p[2]= (r>=src->height-1) ? NULL : bmp_rowptr_from_top(src,r+1)+offsetplane;
d=bmp_rowptr_from_top(dst,r)+offsetplane;
for (c=0;c<src->width;c++,d+=bytesperpixel)
{
int i,j,k;
for (i=k=0;i<3;i++)
if (p[i]!=NULL)
{
for (j=-1;j<=1;j++)
{
a[k]=c+j>=0 && c+j<src->width ? p[i][(c+j)*bytesperpixel] : 255;
k++;
}
}
else
{
a[k]=a[k+1]=a[k+2]=255;
k+=3;
}
for (min=a[0],i=1;i<9;i++)
if (min>a[i])
min=a[i];
(*d)=min;
}
}
}
/*
** Sharpen a bitmap.
*/
void bmp_sharpen(WILLUSBITMAP *dest,WILLUSBITMAP *src)
{
void *ptr;
double **filter;
int i,j;
vector_2d_alloc(&ptr,sizeof(double),3,3);
filter=(double **)ptr;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
filter[i][j]=-0.1;
filter[1][1]=1.8;
bmp_apply_filter(dest,src,filter,3,3);
ptr=(void *)filter;
vector_2d_free(&ptr,3,3);
}
/*
** Apply filter[0..ncols-1][0..nrows-1] to the src bitmap to create the
** dest bitmap.
**
** The src bitmap can be 8-bit or 24-bit.
** The dest bitmap need not be allocated yet. It will be allocated
** as a 24-bit bitmap unless src is grayscale.
*/
void bmp_apply_filter(WILLUSBITMAP *dest,WILLUSBITMAP *src,double **filter,
int ncols,int nrows)
{
int ir,ic,rc,cc;
int rf,rf1,rf2;
int cf,cf1,cf2;
int delspr,delspc;
if (bmp_is_grayscale(src))
{
bmp_apply_filter_gray(dest,src,filter,ncols,nrows);
return;
}
dest->width = src->width;
dest->height = src->height;
dest->bpp = 24;
bmp_alloc(dest);
rc=nrows/2;
cc=ncols/2;
delspr = bmp_rowptr_from_top(src,1)-bmp_rowptr_from_top(src,0);
delspc = src->bpp==24 ? 3 : 1;
for (ir=0;ir<src->height;ir++)
{
unsigned char *sp,*dp;
sp=bmp_rowptr_from_top(src,ir);
dp=bmp_rowptr_from_top(dest,ir);
rf1=ir-rc<0 ? -ir : -rc;
rf2=(ir+(nrows-rc-1)>src->height-1) ? src->height-1-ir : nrows-rc-1;
for (ic=0;ic<src->width;ic++,dp+=3,sp+=delspc)
{
double weight,sr,sg,sb;
int mr,mg,mb;
cf1=ic-cc<0 ? -ic : -cc;
cf2=(ic+(ncols-cc-1)>src->width-1) ? src->width-1-ic : ncols-cc-1;
weight=sr=sg=sb=0.;
for (rf=rf1;rf<=rf2;rf++)
{
unsigned char *sp1;
sp1=sp+rf*delspr+cf1*delspc;
for (cf=cf1;cf<=cf2;cf++)
{
int r,g,b;
double fw;
RGBGETINCPTR(src,sp1,r,g,b);
fw=filter[cf+cc][rf+rc];
weight+=fw;
sr += r*fw;
sg += g*fw;
sb += b*fw;
}
}
if (weight==0.)
continue;
mr = (sr/weight+.5);
mg = (sg/weight+.5);
mb = (sb/weight+.5);
BOUND(mr,0,255);
BOUND(mg,0,255);
BOUND(mb,0,255);
if (dest->type==WILLUSBITMAP_TYPE_NATIVE)
{
dp[0]=mr;
dp[1]=mg;
dp[2]=mb;
}
else
{
dp[2]=mr;
dp[1]=mg;
dp[0]=mb;
}
}
}
}
/*
** src must be 8-bit grayscale
*/
static void bmp_apply_filter_gray(WILLUSBITMAP *dest,WILLUSBITMAP *src,
double **filter,int ncols,int nrows)
{
int i,ir,ic,rc,cc;
int rf,rf1,rf2;
int cf,cf1,cf2;
int delspr;
dest->width = src->width;
dest->height = src->height;
dest->bpp = 8;
for (i=0;i<256;i++)
dest->red[i]=dest->blue[i]=dest->green[i]=i;
bmp_alloc(dest);
rc=nrows/2;
cc=ncols/2;
delspr = bmp_rowptr_from_top(src,1)-bmp_rowptr_from_top(src,0);
for (ir=0;ir<src->height;ir++)
{
unsigned char *sp,*dp;
sp=bmp_rowptr_from_top(src,ir);
dp=bmp_rowptr_from_top(dest,ir);
rf1=ir-rc<0 ? -ir : -rc;
rf2=(ir+(nrows-rc-1)>src->height-1) ? src->height-1-ir : nrows-rc-1;
for (ic=0;ic<src->width;ic++,dp++,sp++)
{
double weight,sr;
int mr;
cf1=ic-cc<0 ? -ic : -cc;
cf2=(ic+(ncols-cc-1)>src->width-1) ? src->width-1-ic : ncols-cc-1;
weight=sr=0.;
for (rf=rf1;rf<=rf2;rf++)
{
unsigned char *sp1;
sp1=sp+rf*delspr+cf1;
for (cf=cf1;cf<=cf2;cf++,sp1++)
{
double fw;
fw=filter[cf+cc][rf+rc];
weight+=fw;
sr += sp1[0]*fw;
}
}
if (weight==0.)
continue;
mr = (sr/weight+.5);
BOUND(mr,0,255);
dp[0]=mr;
}
}
}
int bmp_jpeg_get_comments(char *filename,char **memptr,FILE *out)
{
FILE *f;
int i,len;
char *buf;
static char *funcname="bmp_jpeg_get_comments";
f=wfile_fopen_utf8(filename,"rb");
if (f==NULL)
{
nprintf(out,"Cannot open jpeg file %s for getting comments.\n",
filename);
return(-1);
}
if (!jpeg_read2(f,&i))
{
nprintf(out,"File %s is < 2 bytes.\n",filename);
fclose(f);
return(-2);
}
if (i!=0xffd8)
{
nprintf(out,"First two bytes of file %s aren't JPEG-like, = %04X\n",
filename,i);
fclose(f);
return(-3);
}
while (1)
{
int key;
if (!jpeg_read2(f,&key) || !jpeg_read2(f,&len) || key==0xffda)
{
nprintf(out,"No comments in JPEG file %s.\n",filename);
fclose(f);
return(-4);
}
if (key==0xfffe)
break;
fseek(f,len-2,1);
}
willus_mem_alloc_warn((void **)memptr,len,funcname,10);
buf=(*memptr);
i=fread(buf,1,len-2,f);
buf[len-2]='\0';
fclose(f);
if (i<len-2)
{
nprintf(out,"Comments from JPEG file %s may not be complete!\n",
filename);
return(-5);
}
return(0);
}
int bmp_jpeg_set_comments(char *filename,char *buf,FILE *out)
{
FILE *t,*f;
int i,c,status;
char tempfile[MAXFILENAMELEN];
static char *werr="Error writing to temporary JPEG file %s!\n"
"File NOT deleted.\n";
static char *peof="Premature EOF in JPEG file %s!\n";
wfile_abstmpnam(tempfile);
f=wfile_fopen_utf8(filename,"rb+");
if (f==NULL)
{
nprintf(out,"Cannot open jpeg file %s for putting comments.\n",
filename);
return(-1);
}
t=wfile_fopen_utf8(tempfile,"wb");
if (t==NULL)
{
fclose(f);
nprintf(out,"Cannot open temporary jpeg file %s for writing.\n",
tempfile);
return(-2);
}
if (!jpeg_read2(f,&i))
{
nprintf(out,"File %s is < 2 bytes.\n",filename);
fclose(t);
wfile_remove_utf8(tempfile);
fclose(f);
return(-3);
}
if (i!=0xffd8)
{
nprintf(out,"First two bytes of file %s aren't JPEG-like, = %04X\n",
filename,i);
fclose(t);
wfile_remove_utf8(tempfile);
fclose(f);
return(-4);
}
if (!jpeg_write2(t,i))
{
nprintf(out,werr,tempfile);
fclose(t);
fclose(f);
return(-5);
}
while (1)
{
int key,len;
if (!jpeg_read2(f,&key) || !jpeg_read2(f,&len))
{
nprintf(out,"Ending key not found in JPEG file %s.\n",filename);
fclose(t);
wfile_remove_utf8(tempfile);
fclose(f);
return(-6);
}
if (key==0xfffe || key==0xffda)
{
if (!jpeg_write_comments(t,buf))
{
nprintf(out,werr,tempfile);
fclose(t);
fclose(f);
return(-7);
}
if (key==0xfffe)
status=fseek(f,len-2,1);
else
status=fseek(f,-4,1);
if (status)
{
nprintf(out,peof,filename);
fclose(t);
wfile_remove_utf8(tempfile);
fclose(f);
return(-8);
}
break;
}
else
{
/* Copy this section to file */
if (!jpeg_write2(t,key) || !jpeg_write2(t,len))
{
nprintf(out,werr,tempfile);
fclose(t);
fclose(f);
return(-9);
}
for (i=0;i<len-2;i++)
{
if ((c=fgetc(f))==EOF)
{
nprintf(out,peof,filename);
fclose(t);
wfile_remove_utf8(tempfile);
fclose(f);
return(-10);
}
if (fputc(c,t)<0)
{
nprintf(out,werr,tempfile);
fclose(t);
fclose(f);
return(-11);
}
}
}
}
/* Copy remaining part of file */
while ((c=fgetc(f))!=EOF)
if (fputc(c,t)<0)
{
nprintf(out,werr,tempfile);
fclose(t);
fclose(f);
return(-12);
}
if (fclose(f))
{
nprintf(out,peof,filename);
fclose(t);
wfile_remove_utf8(tempfile);
return(-13);
}
if (fclose(t))
{
nprintf(out,werr,tempfile);
wfile_remove_utf8(tempfile);
return(-14);
}
if (wfile_remove_utf8(filename))
{
nprintf(out,"Error removing file %s, which is to be replaced by file %s.\n"
"File %s not removed!\n",filename,tempfile);
return(-15);
}
if (wfile_rename_utf8(tempfile,filename))
{
nprintf(out,"Error renaming file %s to %s!\n"
"Temporary file %s not deleted!\n",
tempfile,filename,tempfile);
return(-16);
}
return(0);
}
static int jpeg_write_comments(FILE *out,char *buf)
{
int len;
len=strlen(buf)+3;
if (!jpeg_write2(out,0xfffe) || !jpeg_write2(out,len)
|| fwrite(buf,1,len-2,out)<len-2)
return(0);
return(1);
}
static int jpeg_read2(FILE *f,int *x)
{
int b1,b2;
b1=fgetc(f);
if (b1==EOF)
return(0);
b2=fgetc(f);
if (b2==EOF)
return(0);
(*x)=(b1<<8)|b2;
return(1);
}
static int jpeg_write2(FILE *f,int x)
{
int b1,b2;
b1=(x>>8)&0xff;
if (fputc(b1,f)<0)
return(0);
b2=x&0xff;
if (fputc(b2,f)<0)
return(0);
return(1);
}
/*
** Tries to statistically find the most common colors used in an image.
**
** Puts them in order (most used first) in the rr[], gg[], and bb[] arrays
** (0-255) with percent[] indicating the percent of the bitmap that
** uses a color close to that color. nmax is the max number to find.
** res tells how to break up the colorspace. E.g. res=8 breaks it
** into an 8 x 8 x 8 cube.
**
*/
void bmp_find_most_used_colors(WILLUSBITMAP *bmp,int *rr,int *gg,int *bb,
double *percent,int nmax,int res)
{
double ***hist;
double npix;
int n,i,j,k;
void *ptr;
n=res;
vector_3d_alloc(&ptr,sizeof(double),n,n,n);
hist = (double ***)ptr;
for (i=0;i<n;i++)
for (j=0;j<n;j++)
for (k=0;k<n;k++)
hist[i][j][k]=0.;
for (i=0;i<bmp->height;i++)
{
unsigned char *p;
p=bmp_rowptr_from_top(bmp,i);
for (j=0;j<bmp->width;j++,p+=3)
{
int r,g,b;
r=p[0];
g=p[1];
b=p[2];
r=r*n/256;
g=g*n/256;
b=b*n/256;
hist[r][g][b] += 1.0;
}
}
npix = 100./(bmp->width*bmp->height);
for (i=0;i<n;i++)
for (j=0;j<n;j++)
for (k=0;k<n;k++)
hist[i][j][k] *= npix;
for (i=0;i<nmax;i++)
{
find_most_common_color(hist,n,&rr[i],&gg[i],&bb[i],&percent[i]);
/*
if (i>1 && close_to_grey(rr,gg,bb))
continue;
printf("%2d. (r=%3d, g=%3d, b=%3d): %7.4f %%\n",i,rr,gg,bb,percent);
*/
}
ptr=(void *)hist;
vector_3d_free(&ptr,n,n,n);
hist = (double ***)ptr;
}
int bmp_close_to_grey(int r,int g,int b,double threshold)
{
double a,r0,g0,b0,max,min;
a=(r+g+b)/3.;
r0=r/a;
g0=g/a;
b0=b/a;
max=min=r0;
if (max<g0)
max=g0;
if (max<b0)
max=b0;
if (min>g0)
min=g0;
if (min>b0)
min=b0;
// printf(" %3d %3d %3d --------> %5.3f\n",r,g,b,max/min);
if (max/min < threshold)
return(1);
return(0);
}
static void find_most_common_color(double ***hist,int n,int *r,int *g,int *b,
double *percent)
{
int rmax,gmax,bmax;
int ir,ig,ib,dr,dg,db;
double rwsum,gwsum,bwsum,count,wmax;
rmax=gmax=bmax=wmax=0;
for (ir=0;ir<n;ir++)
for (ig=0;ig<n;ig++)
for (ib=0;ib<n;ib++)
{
double w;
w=0.;
for (dr=-1;dr<=1;dr++)
{
if (ir+dr < 0 || ir+dr >= n)
continue;
for (dg=-1;dg<=1;dg++)
{
if (ig+dg < 0 || ig+dg >= n)
continue;
for (db=-1;db<=1;db++)
{
if (ib+db < 0 || ib+db >= n)
continue;
w += hist[ir+dr][ig+dg][ib+db];
}
}
}
if (w > wmax)
{
wmax=w;
rmax=ir;
gmax=ig;
bmax=ib;
}
}
rwsum=gwsum=bwsum=count=0.;
for (dr=-1;dr<=1;dr++)
{
if (rmax+dr < 0 || rmax+dr >= n)
continue;
for (dg=-1;dg<=1;dg++)
{
if (gmax+dg < 0 || gmax+dg >= n)
continue;
for (db=-1;db<=1;db++)
{
double weight;
if (bmax+db < 0 || bmax+db >= n)
continue;
weight = hist[rmax+dr][gmax+dg][bmax+db];
rwsum += (rmax+dr)*weight;
gwsum += (gmax+dg)*weight;
bwsum += (bmax+db)*weight;
count += weight;
hist[rmax+dr][gmax+dg][bmax+db] = 0.;
}
}
}
if (count==0.)
{
(*r) = 0;
(*g) = 0;
(*b) = 0;
}
else
{
(*r) = (int)(255.9*((rwsum/count)+0.5)/n);
(*g) = (int)(255.9*((gwsum/count)+0.5)/n);
(*b) = (int)(255.9*((bwsum/count)+0.5)/n);
}
(*percent) = count;
}
char *bmp_ansi_code(int r,int g,int b)
{
int i;
static char ansicode[32];
strcpy(ansicode,bmp_color_name(r,g,b));
for (i=0;cnames[i][0]!='\0';i++)
if (!stricmp(cnames[i],ansicode))
{
strcpy(ansicode,acodes[i]);
return(ansicode);
}
strcpy(ansicode,ANSI_NORMAL);
return(ansicode);
}
/*
** Primitive color naming function
*/
char *bmp_color_name(int r,int g,int b)
{
if (r > g*1.2 && r > b*1.2)
return(cnames[0]);
if (g > b*1.2 && g > r*1.2)
return(cnames[1]);
if (b > g*1.2 && b > r*1.2)
return(cnames[2]);
if (r > g*1.2 && b > g*1.2)
return(cnames[3]);
if (b > r*1.2 && g > r*1.2)
return(cnames[4]);
if (r > b*1.2 && g > b*1.2)
return(cnames[5]);
if ((r+g+b) > 225*3)
return(cnames[8]);
if ((r+g+b) < 30*3)
return(cnames[7]);
return(cnames[6]);
}
void bmp_autocrop(WILLUSBITMAP *bmp,int pad)
{
int r1,r2,c1,c2;
WILLUSBITMAP *new,_new;
new=&_new;
bmp_init(new);
for (r1=0;r1<bmp->height;r1++)
if (!bmp_uniform_row(bmp,r1))
break;
if (r1>=bmp->height)
return;
r1-=pad;
if (r1<0)
r1=0;
for (r2=bmp->height-1;r2>=0;r2--)
if (!bmp_uniform_row(bmp,r2))
break;
r2+=pad;
if (r2>bmp->height-1)
r2=bmp->height-1;
for (c1=0;c1<bmp->width;c1++)
if (!bmp_uniform_col(bmp,c1))
break;
if (c1>=bmp->width)
return;
c1-=pad;
if (c1<0)
c1=0;
for (c2=bmp->width-1;c2>=0;c2--)
if (!bmp_uniform_col(bmp,c2))
break;
c2+=pad;
if (c2>bmp->width-1)
c2=bmp->width-1;
bmp_crop(bmp,c1,r1,(c2-c1)+1,(r2-r1)+1);
}
static int bmp_uniform_row(WILLUSBITMAP *bmp,int row)
{
unsigned char *p;
int i,j;
p=bmp_rowptr_from_top(bmp,row);
if (bmp->bpp==8)
{
for (i=1;i<bmp->width;i++)
if (p[i]!=p[0])
break;
}
else
{
for (j=3,i=1;i<bmp->width;i++,j+=3)
if (p[j]!=p[0] || p[j+1]!=p[1] || p[j+2]!=p[2])
break;
}
return(i>=bmp->width);
}
static int bmp_uniform_col(WILLUSBITMAP *bmp,int col)
{
unsigned char *p,*p0;
int i;
if (bmp->bpp==8)
{
p0=bmp_rowptr_from_top(bmp,0)+col;
for (i=1;i<bmp->height;i++)
{
p=bmp_rowptr_from_top(bmp,i)+col;
if (p[0]!=p0[0])
break;
}
}
else
{
p0=bmp_rowptr_from_top(bmp,0)+col*3;
for (i=1;i<bmp->height;i++)
{
p=bmp_rowptr_from_top(bmp,i)+col*3;
if (p[0]!=p0[0] || p[1]!=p0[1] || p[2]!=p0[2])
break;
}
}
return(i>=bmp->height);
}
#ifdef HAVE_JASPER_LIB
/*
** Read image using Jasper library
** 9-3-2010
*/
int bmp_jasper_read(WILLUSBITMAP *bmp,char *filename,FILE *out)
{
jas_image_t *image;
jas_stream_t *in;
jas_matrix_t *jasdata;
// jas_tmr_t dectmr;
// double dectime; /* seconds? */
int imagefmt,nc,ii,status;
if (jas_init())
{
nprintf(out,"Jasper library init failure.\n");
return(-1);
}
jas_setdbglevel(0);
in=jas_stream_fopen(filename,"rb");
if (in==NULL)
{
jas_image_clearfmts();
nprintf(out,"Could not open file %s for input.\n",filename);
return(-2);
}
/* Get image format */
if ((imagefmt=jas_image_getfmt(in)) < 0)
{
jas_image_clearfmts();
nprintf(out,"Format of file %s unknown by Jasper library.\n",filename);
return(-3);
}
if (bmp==NULL)
{
jas_image_clearfmts();
return(imagefmt);
}
/* Read image */
// jas_tmr_start(&dectmr); /* If you want to time the image decode */
image=jas_image_decode(in,imagefmt,0);
// jas_tmr_stop(&dectmr); /* If you want to time the image decode */
// dectime = jas_tmr_get(&dectmr);
if (image==NULL)
{
jas_image_clearfmts();
nprintf(out,"Could not read file %s with Jasper library.\n",filename);
return(-4);
}
nc=jas_image_numcmpts(image);
if (nc<=0 || nc==2)
{
jas_image_destroy(image);
jas_image_clearfmts();
nprintf(out,"File %s has %d image components--can't process.\n",filename,nc);
return(-5);
}
if (nc>3)
nc=3;
for (ii=0;ii<nc;ii++)
{
int bpp,bs,bw,row;
if (ii==0)
{
bmp->width = jas_image_cmptwidth(image,ii);
bmp->height = jas_image_cmptheight(image,ii);
if (nc==1)
{
int j;
bmp->bpp=8;
for (j=0;j<256;j++)
bmp->red[j]=bmp->green[j]=bmp->blue[j]=j;
}
else
bmp->bpp=24;
if (!bmp_alloc(bmp))
{
jas_image_destroy(image);
jas_image_clearfmts();
nprintf(out,"Not enough mem for %d x %d x %d Jasper image from file %s.\n",bmp->width,bmp->height,bmp->bpp,filename);
return(-6);
}
}
else
{
if (bmp->width != jas_image_cmptwidth(image,ii)
|| bmp->height != jas_image_cmptheight(image,ii))
{
jas_image_destroy(image);
jas_image_clearfmts();
nprintf(out,"File %s has different-sized image components.\n",filename);
return(-7);
}
}
bpp = jas_image_cmptprec(image,ii);
bs = bpp>8 ? bpp-8 : 8-bpp;
jasdata = jas_matrix_create(bmp->height,bmp->width);
if (jasdata==NULL)
{
jas_image_destroy(image);
jas_image_clearfmts();
nprintf(out,"Not enough mem for %d x %d x %d Jasper image from file %s.\n",bmp->width,bmp->height,bpp,filename);
return(-8);
}
status=jas_image_readcmpt(image,ii,0,0,bmp->width,bmp->height,jasdata);
if (status)
{
jas_matrix_destroy(jasdata);
jas_image_destroy(image);
jas_image_clearfmts();
nprintf(out,"Error %d reading %d x %d x %d Jasper image from file %s.\n",status,bmp->width,bmp->height,bpp,filename);
return(-9);
}
/* Copy component to bitmap structure */
bw=bmp->bpp>>3;
for (row=0;row<bmp->height;row++)
{
unsigned char *p;
int col;
p=bmp_rowptr_from_top(bmp,row)+ii;
if (bpp>8)
for (col=0;col<bmp->width;col++,p+=bw)
p[0]=(jas_matrix_get(jasdata,row,col) >> bs);
else if (bpp<8)
for (col=0;col<bmp->width;col++,p+=bw)
p[0]=(jas_matrix_get(jasdata,row,col) << bs);
else
for (col=0;col<bmp->width;col++,p+=bw)
p[0]=jas_matrix_get(jasdata,row,col);
}
jas_matrix_destroy(jasdata);
}
jas_image_destroy(image);
jas_image_clearfmts();
return(0);
}
#endif // HAVE_JASPER_LIB
/*
** Allocate more bitmap rows.
** ratio typically something like 1.5 or 2.0
** If ratio not enough to increment height, 128 rows get added.
*/
void bmp_more_rows(WILLUSBITMAP *bmp,double ratio,int pixval)
{
int new_height,new_bytes,bw;
static char *funcname="bmp_more_rows";
new_height=(int)(bmp->height*ratio+.5);
if (new_height <= bmp->height)
new_height = bmp->height + 128;
bw=bmp_bytewidth(bmp);
new_bytes=bw*new_height;
if (new_bytes > bmp->size_allocated)
{
willus_mem_realloc_robust_warn((void **)&bmp->data,
new_bytes,bmp->size_allocated,funcname,10);
bmp->size_allocated=new_bytes;
}
/* Fill in */
memset(bmp_rowptr_from_top(bmp,bmp->height),pixval,(new_height-bmp->height)*bw);
bmp->height=new_height;
}
/*
** Bitmap is assumed to be grayscale
*/
static double bmp_row_by_row_stdev(WILLUSBITMAP *bmp,int ccount,int whitethresh,
double theta_radians)
{
static char *funcname="bmp_row_by_row_stdev";
int dc1,dc2,c1,c2;
int r,n,nn,nw,dw,countthresh;
double tanth,csum,csumsq,stdev;
int *row,*rowoff;
c1=bmp->width/15.;
c2=bmp->width-c1;
dw=(int)((c2-c1)/ccount+.5);
if (dw<1)
dw=1;
tanth=-tan(theta_radians);
dc1=(int)(tanth*bmp->width);
if (dc1<0)
{
dc1=1-dc1;
dc2=0;
}
else
{
dc2=-dc1-1;
dc1=0;
}
dc1 += bmp->height/15.;
dc2 -= bmp->height/15.;
csum=csumsq=0.;
n=0;
{
int c;
for (nw=0,c=c1;c<c2;c+=dw,nw++);
countthresh=nw*2/3;
willus_mem_alloc_warn((void **)&row,sizeof(int)*nw*2,funcname,10);
rowoff=&row[nw];
for (nn=0,c=c1;c<c2;c+=dw,nn++)
{
row[nn]=tanth*c;
rowoff[nn]=(int)(bmp_rowptr_from_top(bmp,row[nn]) - bmp_rowptr_from_top(bmp,0));
}
}
for (r=dc1+1;r<bmp->height+dc2-1;r++)
{
int c,cin,count;
double dcount;
unsigned char *p;
p=bmp_rowptr_from_top(bmp,r);
for (cin=nn=count=0,c=c1;c<c2;c+=dw,nn++)
{
int r0;
r0=r+row[nn];
if (r0<0 || r0>=bmp->height)
{
if (cin>0)
break;
continue;
}
cin++;
if (p[c+rowoff[nn]]<whitethresh)
count++;
}
if (cin < countthresh)
continue;
dcount=100.*count/cin;
csum+=dcount;
csumsq+=dcount*dcount;
n++;
}
willus_mem_free((double **)&row,funcname);
if (n<=0)
stdev=0.;
else
stdev=sqrt(fabs((csum/n)*(csum/n)-csumsq/n));
return(stdev);
}
double bmp_autostraighten(WILLUSBITMAP *src,WILLUSBITMAP *srcgrey,int white,double maxdegrees,
double mindegrees,int debug,FILE *out)
{
int i,na,n,imax,maxpt;
double stepsize,sdmin,sdmax,rotdeg;
double *sdev;
FILE *f;
static int rpc=0;
static char *funcname="bmp_autostraighten";
rpc++;
f=NULL;
if (debug)
{
f=wfile_fopen_utf8("straighten_metrics.ep",rpc==1?"w":"a");
nprintf(f,"/sa l \"src page %d\" 2\n",rpc);
}
stepsize=.05;
na = (int)(maxdegrees/stepsize+.5);
if (na<1)
na=1;
n = 1+na*2;
sdmin=999.;
sdmax=-999.;
imax=0;
willus_mem_alloc_warn((void **)&sdev,n*sizeof(double),funcname,10);
for (i=0;i<n;i++)
{
double theta,sdev0;
theta = (i-na)*stepsize*PI/180.;
sdev0=bmp_row_by_row_stdev(srcgrey,400,white,theta);
if (sdmin > sdev0)
sdmin = sdev0;
if (sdmax < sdev0)
{
imax = i;
sdmax = sdev0;
}
sdev[i]=sdev0;
}
if (sdmax<=0.)
{
willus_mem_free((double **)&sdev,funcname);
if (debug)
fclose(f);
return(0.);
}
for (i=0;i<n;i++)
sdev[i] /= sdmax;
if (debug)
{
for (i=0;i<n;i++)
nprintf(f,"%.3f %g\n",(i-na)*stepsize,sdev[i]);
}
sdmin /= sdmax;
rotdeg = -(imax-na)*stepsize;
/*
** If (1) not a clear optimum point, or
** (2) rot angle too close to zero, or
** (3) rot angle too close to limits,
** then: don't rotate
*/
if (sdmin > 0.95 || fabs(rotdeg) <= mindegrees
|| fabs(fabs(rotdeg)-fabs(maxdegrees)) < 0.25)
{
willus_mem_free((double **)&sdev,funcname);
if (debug)
{
nprintf(f,"//nc\n");
nprintf(f,"/sa l \"srcpage %d, rot=%.2f deg (no rot)\" 2\n/sa m 2 3\n%g 0\n%g 1\n//nc\n",
rpc,-rotdeg,-rotdeg,-rotdeg);
fclose(f);
}
return(0.);
}
/*
** Count peaks in the rotation figure of merit
*/
for (maxpt=0,i=1;i<n-1;i++)
{
if (sdev[i]>.95 && ((sdev[i]>sdev[i-1] && sdev[i]>sdev[i+1])
|| (i<n-2 && sdev[i]>sdev[i-1] && sdev[i]==sdev[i+1] && sdev[i+1]>sdev[i+2])))
{
maxpt++;
}
}
/* If one peak (maximum)--do fine grain search near that point and return the highest value */
if (maxpt==1)
{
double sdmax2;
double thbest;
int ifine,nfine;
sdmax2=1.0;
thbest=(imax-na)*stepsize*PI/180.;
nfine=5;
for (ifine=-nfine+1;ifine<nfine;ifine++)
{
double theta,sdev0;
if (ifine==0)
continue;
theta = (imax+(double)ifine/nfine-na)*stepsize*PI/180.;
sdev0=bmp_row_by_row_stdev(srcgrey,400,white,theta)/sdmax;
if (debug)
nprintf(f,"%.3f %g\n",theta*180./PI,sdev0);
if (sdev0>sdmax2)
{
sdmax2=sdev0;
thbest=theta;
}
}
rotdeg = -thbest*180./PI;
if (debug)
nprintf(f,"//nc\n");
}
/* If not just one max, do weighted average near highest point */
else
{
if (debug)
nprintf(f,"//nc\n");
if (imax>=3 && imax<=n-4)
{
double sd1min,sd2min,sdthresh;
for (sd1min=sdev[imax-1],i=imax-2;i>=0;i--)
if (sd1min > sdev[i])
sd1min = sdev[i];
for (sd2min=sdev[imax+1],i=imax+2;i<n;i++)
if (sd2min > sdev[i])
sd2min = sdev[i];
sdthresh = sd1min > sd2min ? sd1min*1.01 : sd2min*1.01;
if (sdthresh < 0.9)
sdthresh = 0.9;
if (sdthresh < 0.95)
{
double deg1,deg2;
int i1,i2;
for (i=imax-1;i>=0;i--)
if (sdev[i]<sdthresh)
break;
i1=i+1;
deg1=stepsize*((i-na)+(sdthresh-sdev[i])/(sdev[i+1]-sdev[i]));
for (i=imax+1;i<n-1;i++)
if (sdev[i]<sdthresh)
break;
i2=i-1;
deg2=stepsize*((i-na)-(sdthresh-sdev[i])/(sdev[i-1]-sdev[i]));
if (deg2 - deg1 < 2.5)
{
double wsum,sum;
for (sum=wsum=0.,i=i1;i<=i2;i++)
{
wsum += (sdev[i]-sdthresh)*stepsize*(i-na);
sum += (sdev[i]-sdthresh);
}
if (sum==0.)
rotdeg = -(deg1+deg2)/2.;
else
rotdeg = -wsum/sum;
if (debug)
nprintf(f,"/sa l \"srcpage %d, %.1f%% line\" 2\n/sa m 2 2\n"
"%g 0\n%g 1\n//nc\n"
"/sa l \"srcpage %d, %.1f%% line\" 2\n/sa m 2 2\n"
"%g 0\n%g 1\n//nc\n",rpc,sdthresh*100.,deg1,deg1,
rpc,sdthresh*100.,deg2,deg2);
}
}
}
}
if (debug)
{
nprintf(f,"/sa l \"srcpage %d, rot=%.2f\" 2\n/sa m 2 3\n%g 0\n%g 1\n//nc\n",
rpc,-rotdeg,-rotdeg,-rotdeg);
fclose(f);
}
nprintf(out,"\n(Straightening page: rotating cc by %.2f deg.)\n",rotdeg);
/* BMP rotation fills with pixel value at (0,0) */
if (debug)
{
char filename[256];
sprintf(filename,"unrotated%05d.png",rpc);
bmp_write(srcgrey,filename,stdout,100);
}
srcgrey->data[0]=255;
bmp_rotate_fast(srcgrey,rotdeg,0);
if (debug)
{
char filename[256];
sprintf(filename,"rotated%05d_%03ddeg.png",rpc,(int)(rotdeg*100.));
bmp_write(srcgrey,filename,stdout,100);
}
if (src!=NULL)
{
src->data[0]=src->data[1]=src->data[2]=255;
bmp_rotate_fast(src,rotdeg,0);
}
willus_mem_free((double **)&sdev,funcname);
return(rotdeg);
}
/*
** Everything >= whitethresh gets 255.
*/
void bmp_apply_whitethresh(WILLUSBITMAP *bmp,int whitethresh)
{
int i,j;
if (!bmp_is_grayscale(bmp))
{
for (i=0;i<bmp->height;i++)
{
unsigned char *p,*pcolor;
pcolor=bmp_rowptr_from_top(bmp,i);
p=bmp_rowptr_from_top(bmp,i);
for (j=0;j<bmp->width;j++,p++,pcolor+=3)
if ((*p)>=whitethresh)
(*p)=pcolor[0]=pcolor[1]=pcolor[2]=255;
}
}
else
{
for (i=0;i<bmp->height;i++)
{
unsigned char *p;
p=bmp_rowptr_from_top(bmp,i);
for (j=0;j<bmp->width;j++,p++)
if ((*p)>=whitethresh)
(*p)=255;
}
}
}
/*
** Non-Floyd-Steinberg
** bpc = bits per color plane.
*/
void bmp_dither_to_bpc(WILLUSBITMAP *bmp,int newbpc)
{
int r,c,k,dbits,newmax,bshift,kmax;
kmax=bmp->bpp==24 ? 3 : 1;
newmax=(1<<newbpc)-1;
bshift=8-newbpc;
if (newbpc<1 || newbpc>7)
return;
if (newbpc<2)
dbits=4;
else if (newbpc<4)
dbits=3;
else if (newbpc<6)
dbits=2;
else
dbits=1;
for (r=0;r<bmp->height;r++)
{
unsigned char *p;
p=bmp_rowptr_from_top(bmp,r);
for (c=0;c<bmp->width;c++)
for (k=0;k<kmax;k++,p++)
p[0]=(pixval_dither(p[0],dbits,255,newmax,c,r)<<bshift);
}
}
/*
** pv = src pix value
** Dither using a 2^n x 2^n pattern (n from 1 to 4)
*/
static int pixval_dither(int pv,int n,int maxsrc,int maxdst,int x0,int y0)
{
int pv1,newval,newvalr,dl;
if (pv==maxsrc)
return(maxdst);
if (pv==0)
return(pv);
pv1=pv*maxdst;
newval=pv1/maxsrc;
newvalr=pv1%maxsrc;
dl=(newvalr<<(n*2))/maxsrc;
return(newval+( (dl > dither_rec(n,x0,y0)) ? 1 : 0));
}
static int dither_rec(int bits,int x0,int y0)
{
static int da[4]={0,3,2,1};
int xr,yr;
if (bits==0)
return(0);
xr=x0&1;
yr=y0&1;
return((da[(yr<<1)|xr]<<((bits-1)<<1))+dither_rec(bits-1,x0>>1,y0>>1));
}
/*
** Extract a cropped part of src into dst.
** y0 is row FROM TOP! 0=top row, bmp->height-1=bottom row.
*/
void bmp_extract(WILLUSBITMAP *dst,WILLUSBITMAP *src,int x0,int y0_from_top,int width,int height)
{
int x1,y1,y0,i,dbw,sbw;
unsigned char *psrc,*pdest;
dst->width=width;
dst->height=height;
dst->bpp=src->bpp;
dst->type=src->type;
for (i=0;i<256;i++)
{
dst->red[i]=src->red[i];
dst->green[i]=src->green[i];
dst->blue[i]=src->blue[i];
}
bmp_alloc(dst);
y0 = y0_from_top;
y1 = y0+height-1;
x1 = x0+width-1;
if (x1 > src->width-1)
x1 = src->width-1;
if (y1 > src->height-1)
y1 = src->height-1;
if (x0<0)
x0 = 0;
if (y0<0)
y0 = 0;
if (x0==0 && y0==0 && x1==src->width-1 && y1==src->height-1)
{
bmp_copy(dst,src);
return;
}
sbw = bmp_bytewidth(src);
psrc = bmp_rowptr_from_top(src,src->type==WILLUSBITMAP_TYPE_WIN32?y1:y0)
+ ((src->bpp+7)>>3)*x0;
dbw = bmp_bytewidth(dst);
pdest = dst->data;
for (i=height;i>0;i--,psrc+=sbw,pdest+=dbw)
memcpy(pdest,psrc,dbw);
}
/*
** Read BMP from simple PCL-formatted data
** Return 0 for okay.
** Negative for error.
**
** Presently works only for the simplest black/white rasters--no encoding.
** This is enough to correctly convert HP 8722D VNA raster downloads.
**
*/
int bmp_read_pcl(WILLUSBITMAP *bmp,char *pclbuf,int n)
{
int i,r,status;
/* Set up bitmap params */
status=pcl_get_resolution(pclbuf,n,&bmp->width,&bmp->height);
if (status<0)
return(status);
bmp->bpp=8;
bmp->type=WILLUSBITMAP_TYPE_WIN32;
for (i=0;i<256;i++)
bmp->red[i]=bmp->blue[i]=bmp->green[i]=i;
/* Allocate */
if (!bmp_alloc(bmp))
return(-4);
/* Fill */
i=0;
for (r=0;r<bmp->height;r++)
{
unsigned char *p;
int status,j,k;
p=bmp_rowptr_from_top(bmp,r);
status=pcl_next_raster_row(pclbuf,n,&i);
if (status<=0)
break;
if (status*8!=bmp->width)
return(-1);
for (i++,k=0;k<status;i++,k++)
for (j=128;j;j>>=1,p++)
if (pclbuf[i]&j)
(*p)=0;
else
(*p)=255;
i--;
}
return(0);
}
static int pcl_get_resolution(char *pclbuf,int n,int *w,int *h)
{
int i,nr,nc;
i=nc=nr=0;
while (1)
{
int status;
status=pcl_next_raster_row(pclbuf,n,&i);
if (status<=0)
break;
if (nc==0)
nc=status;
else if (nc!=status)
return(-2);
nr++;
}
if (nr<=0 || nc<=0)
return(-3);
(*w)=nc*8;
(*h)=nr;
return(0);
}
static int pcl_next_raster_row(char *pclbuf,int n,int *index)
{
int i;
static char *mstr="\x1b\x2a\x62";
char numbuf[32];
while (1)
{
if (memcmp(mstr,&pclbuf[(*index)],3))
{
(*index)=(*index)+1;
if ((*index)>n-3)
break;
continue;
}
(*index)=(*index)+3;
for (i=0;(*index)<n && pclbuf[(*index)]>='0' && pclbuf[(*index)]<='9' && i<30;)
{
numbuf[i++]=pclbuf[(*index)];
(*index)=(*index)+1;
}
numbuf[i]='\0';
if ((*index)>=n || i<=0)
break;
if (is_an_integer(numbuf))
return(atoi(numbuf));
}
return(-1);
}
|