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
|
import os
import unittest
from pygame.tests import test_utils
from pygame.tests.test_utils import (
example_path,
SurfaceSubclass,
)
try:
from pygame.tests.test_utils.arrinter import *
except (ImportError, NameError):
pass
import pygame
from pygame.locals import *
from pygame.bufferproxy import BufferProxy
import platform
import gc
import weakref
import ctypes
IS_PYPY = "PyPy" == platform.python_implementation()
class SurfaceTypeTest(unittest.TestCase):
def test_surface__pixel_format_as_surface_subclass(self):
"""Ensure a subclassed surface can be used for pixel format
when creating a new surface."""
expected_depth = 16
expected_flags = SRCALPHA
expected_size = (13, 37)
depth_surface = SurfaceSubclass((11, 21), expected_flags, expected_depth)
surface = pygame.Surface(expected_size, expected_flags, depth_surface)
self.assertIsNot(surface, depth_surface)
self.assertIsInstance(surface, pygame.Surface)
self.assertNotIsInstance(surface, SurfaceSubclass)
self.assertEqual(surface.get_size(), expected_size)
self.assertEqual(surface.get_flags(), expected_flags)
self.assertEqual(surface.get_bitsize(), expected_depth)
def test_surface_created_opaque_black(self):
surf = pygame.Surface((20, 20))
self.assertEqual(surf.get_at((0, 0)), (0, 0, 0, 255))
# See https://github.com/pygame/pygame/issues/1395
pygame.display.set_mode((500, 500))
surf = pygame.Surface((20, 20))
self.assertEqual(surf.get_at((0, 0)), (0, 0, 0, 255))
def test_set_clip(self):
"""see if surface.set_clip(None) works correctly."""
s = pygame.Surface((800, 600))
r = pygame.Rect(10, 10, 10, 10)
s.set_clip(r)
r.move_ip(10, 0)
s.set_clip(None)
res = s.get_clip()
# this was garbled before.
self.assertEqual(res[0], 0)
self.assertEqual(res[2], 800)
def test_print(self):
surf = pygame.Surface((70, 70), 0, 32)
self.assertEqual(repr(surf), "<Surface(70x70x32 SW)>")
def test_keyword_arguments(self):
surf = pygame.Surface((70, 70), flags=SRCALPHA, depth=32)
self.assertEqual(surf.get_flags() & SRCALPHA, SRCALPHA)
self.assertEqual(surf.get_bitsize(), 32)
# sanity check to make sure the check below is valid
surf_16 = pygame.Surface((70, 70), 0, 16)
self.assertEqual(surf_16.get_bytesize(), 2)
# try again with an argument list
surf_16 = pygame.Surface((70, 70), depth=16)
self.assertEqual(surf_16.get_bytesize(), 2)
def test_set_at(self):
# 24bit surfaces
s = pygame.Surface((100, 100), 0, 24)
s.fill((0, 0, 0))
# set it with a tuple.
s.set_at((0, 0), (10, 10, 10, 255))
r = s.get_at((0, 0))
self.assertIsInstance(r, pygame.Color)
self.assertEqual(r, (10, 10, 10, 255))
# try setting a color with a single integer.
s.fill((0, 0, 0, 255))
s.set_at((10, 1), 0x0000FF)
r = s.get_at((10, 1))
self.assertEqual(r, (0, 0, 255, 255))
def test_set_at__big_endian(self):
"""png files are loaded in big endian format (BGR rather than RGB)"""
pygame.display.init()
try:
image = pygame.image.load(example_path(os.path.join("data", "BGR.png")))
# Check they start red, green and blue
self.assertEqual(image.get_at((10, 10)), pygame.Color(255, 0, 0))
self.assertEqual(image.get_at((10, 20)), pygame.Color(0, 255, 0))
self.assertEqual(image.get_at((10, 40)), pygame.Color(0, 0, 255))
# Set three pixels that are already red, green, blue
# to red, green and, blue with set_at:
image.set_at((10, 10), pygame.Color(255, 0, 0))
image.set_at((10, 20), pygame.Color(0, 255, 0))
image.set_at((10, 40), pygame.Color(0, 0, 255))
# Check they still are
self.assertEqual(image.get_at((10, 10)), pygame.Color(255, 0, 0))
self.assertEqual(image.get_at((10, 20)), pygame.Color(0, 255, 0))
self.assertEqual(image.get_at((10, 40)), pygame.Color(0, 0, 255))
finally:
pygame.display.quit()
def test_SRCALPHA(self):
# has the flag been passed in ok?
surf = pygame.Surface((70, 70), SRCALPHA, 32)
self.assertEqual(surf.get_flags() & SRCALPHA, SRCALPHA)
# 24bit surfaces can not have SRCALPHA.
self.assertRaises(ValueError, pygame.Surface, (100, 100), pygame.SRCALPHA, 24)
# if we have a 32 bit surface, the SRCALPHA should have worked too.
surf2 = pygame.Surface((70, 70), SRCALPHA)
if surf2.get_bitsize() == 32:
self.assertEqual(surf2.get_flags() & SRCALPHA, SRCALPHA)
def test_flags_default0_nodisplay(self):
"""is set to zero, and SRCALPHA is not set by default with no display initialized."""
pygame.display.quit()
surf = pygame.Surface((70, 70))
self.assertEqual(surf.get_flags() & SRCALPHA, 0)
def test_flags_default0_display(self):
"""is set to zero, and SRCALPH is not set by default even when the display is initialized."""
pygame.display.set_mode((320, 200))
try:
surf = pygame.Surface((70, 70))
self.assertEqual(surf.get_flags() & SRCALPHA, 0)
finally:
pygame.display.quit()
def test_masks(self):
def make_surf(bpp, flags, masks):
pygame.Surface((10, 10), flags, bpp, masks)
# With some masks SDL_CreateRGBSurface does not work properly.
masks = (0xFF000000, 0xFF0000, 0xFF00, 0)
self.assertEqual(make_surf(32, 0, masks), None)
# For 24 and 32 bit surfaces Pygame assumes no losses.
masks = (0x7F0000, 0xFF00, 0xFF, 0)
self.assertRaises(ValueError, make_surf, 24, 0, masks)
self.assertRaises(ValueError, make_surf, 32, 0, masks)
# What contiguous bits in a mask.
masks = (0x6F0000, 0xFF00, 0xFF, 0)
self.assertRaises(ValueError, make_surf, 32, 0, masks)
def test_get_bounding_rect(self):
surf = pygame.Surface((70, 70), SRCALPHA, 32)
surf.fill((0, 0, 0, 0))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.width, 0)
self.assertEqual(bound_rect.height, 0)
surf.set_at((30, 30), (255, 255, 255, 1))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.left, 30)
self.assertEqual(bound_rect.top, 30)
self.assertEqual(bound_rect.width, 1)
self.assertEqual(bound_rect.height, 1)
surf.set_at((29, 29), (255, 255, 255, 1))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.left, 29)
self.assertEqual(bound_rect.top, 29)
self.assertEqual(bound_rect.width, 2)
self.assertEqual(bound_rect.height, 2)
surf = pygame.Surface((70, 70), 0, 24)
surf.fill((0, 0, 0))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.width, surf.get_width())
self.assertEqual(bound_rect.height, surf.get_height())
surf.set_colorkey((0, 0, 0))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.width, 0)
self.assertEqual(bound_rect.height, 0)
surf.set_at((30, 30), (255, 255, 255))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.left, 30)
self.assertEqual(bound_rect.top, 30)
self.assertEqual(bound_rect.width, 1)
self.assertEqual(bound_rect.height, 1)
surf.set_at((60, 60), (255, 255, 255))
bound_rect = surf.get_bounding_rect()
self.assertEqual(bound_rect.left, 30)
self.assertEqual(bound_rect.top, 30)
self.assertEqual(bound_rect.width, 31)
self.assertEqual(bound_rect.height, 31)
# Issue #180
pygame.display.init()
try:
surf = pygame.Surface((4, 1), 0, 8)
surf.fill((255, 255, 255))
surf.get_bounding_rect() # Segfault.
finally:
pygame.display.quit()
def test_copy(self):
"""Ensure a surface can be copied."""
color = (25, 25, 25, 25)
s1 = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
s1.fill(color)
s2 = s1.copy()
s1rect = s1.get_rect()
s2rect = s2.get_rect()
self.assertEqual(s1rect.size, s2rect.size)
self.assertEqual(s2.get_at((10, 10)), color)
def test_fill(self):
"""Ensure a surface can be filled."""
color = (25, 25, 25, 25)
fill_rect = pygame.Rect(0, 0, 16, 16)
s1 = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
s1.fill(color, fill_rect)
for pt in test_utils.rect_area_pts(fill_rect):
self.assertEqual(s1.get_at(pt), color)
for pt in test_utils.rect_outer_bounds(fill_rect):
self.assertNotEqual(s1.get_at(pt), color)
def test_fill_rle(self):
"""Test RLEACCEL flag with fill()"""
color = (250, 25, 25, 255)
surf = pygame.Surface((32, 32))
blit_surf = pygame.Surface((32, 32))
blit_surf.set_colorkey((255, 0, 255), pygame.RLEACCEL)
self.assertTrue(blit_surf.get_flags() & pygame.RLEACCELOK)
surf.blit(blit_surf, (0, 0))
blit_surf.fill(color)
self.assertEqual(
blit_surf.mustlock(), (blit_surf.get_flags() & pygame.RLEACCEL) != 0
)
self.assertTrue(blit_surf.get_flags() & pygame.RLEACCEL)
def test_mustlock_rle(self):
"""Test RLEACCEL flag with mustlock()"""
surf = pygame.Surface((100, 100))
blit_surf = pygame.Surface((100, 100))
blit_surf.set_colorkey((0, 0, 255), pygame.RLEACCEL)
self.assertTrue(blit_surf.get_flags() & pygame.RLEACCELOK)
surf.blit(blit_surf, (0, 0))
self.assertTrue(blit_surf.get_flags() & pygame.RLEACCEL)
self.assertTrue(blit_surf.mustlock())
def test_mustlock_surf_alpha_rle(self):
"""Test RLEACCEL flag with mustlock() on a surface
with per pixel alpha - new feature in SDL2"""
surf = pygame.Surface((100, 100))
blit_surf = pygame.Surface((100, 100), depth=32, flags=pygame.SRCALPHA)
blit_surf.set_colorkey((192, 191, 192, 255), pygame.RLEACCEL)
self.assertTrue(blit_surf.get_flags() & pygame.RLEACCELOK)
surf.blit(blit_surf, (0, 0))
self.assertTrue(blit_surf.get_flags() & pygame.RLEACCEL)
self.assertTrue(blit_surf.get_flags() & pygame.SRCALPHA)
self.assertTrue(blit_surf.mustlock())
def test_copy_rle(self):
"""Test copying a surface set to use run length encoding"""
s1 = pygame.Surface((32, 32), 24)
s1.set_colorkey((255, 0, 255), pygame.RLEACCEL)
self.assertTrue(s1.get_flags() & pygame.RLEACCELOK)
newsurf = s1.copy()
self.assertTrue(s1.get_flags() & pygame.RLEACCELOK)
self.assertTrue(newsurf.get_flags() & pygame.RLEACCELOK)
def test_subsurface_rle(self):
"""Ensure an RLE sub-surface works independently of its parent."""
color = (250, 25, 25, 255)
color2 = (200, 200, 250, 255)
sub_rect = pygame.Rect(16, 16, 16, 16)
s0 = pygame.Surface((32, 32), 24)
s1 = pygame.Surface((32, 32), 24)
s1.set_colorkey((255, 0, 255), pygame.RLEACCEL)
s1.fill(color)
s2 = s1.subsurface(sub_rect)
s2.fill(color2)
s0.blit(s1, (0, 0))
self.assertTrue(s1.get_flags() & pygame.RLEACCEL)
self.assertTrue(not s2.get_flags() & pygame.RLEACCEL)
def test_subsurface_rle2(self):
"""Ensure an RLE sub-surface works independently of its parent."""
color = (250, 25, 25, 255)
color2 = (200, 200, 250, 255)
sub_rect = pygame.Rect(16, 16, 16, 16)
s0 = pygame.Surface((32, 32), 24)
s1 = pygame.Surface((32, 32), 24)
s1.set_colorkey((255, 0, 255), pygame.RLEACCEL)
s1.fill(color)
s2 = s1.subsurface(sub_rect)
s2.fill(color2)
s0.blit(s2, (0, 0))
self.assertTrue(s1.get_flags() & pygame.RLEACCELOK)
self.assertTrue(not s2.get_flags() & pygame.RLEACCELOK)
def test_solarwolf_rle_usage(self):
"""Test for error/crash when calling set_colorkey() followed
by convert twice in succession. Code originally taken
from solarwolf."""
def optimize(img):
clear = img.get_colorkey()
img.set_colorkey(clear, RLEACCEL)
self.assertEqual(img.get_colorkey(), clear)
return img.convert()
pygame.display.init()
try:
pygame.display.set_mode((640, 480))
image = pygame.image.load(example_path(os.path.join("data", "alien1.png")))
image = image.convert()
orig_colorkey = image.get_colorkey()
image = optimize(image)
image = optimize(image)
self.assertTrue(image.get_flags() & pygame.RLEACCELOK)
self.assertTrue(not image.get_flags() & pygame.RLEACCEL)
self.assertEqual(image.get_colorkey(), orig_colorkey)
self.assertTrue(isinstance(image, pygame.Surface))
finally:
pygame.display.quit()
def test_solarwolf_rle_usage_2(self):
"""Test for RLE status after setting alpha"""
pygame.display.init()
try:
pygame.display.set_mode((640, 480), depth=32)
blit_to_surf = pygame.Surface((100, 100))
image = pygame.image.load(example_path(os.path.join("data", "alien1.png")))
image = image.convert()
orig_colorkey = image.get_colorkey()
# set the colorkey with RLEACCEL, should add the RLEACCELOK flag
image.set_colorkey(orig_colorkey, RLEACCEL)
self.assertTrue(image.get_flags() & pygame.RLEACCELOK)
self.assertTrue(not image.get_flags() & pygame.RLEACCEL)
# now blit the surface - should add the RLEACCEL flag
blit_to_surf.blit(image, (0, 0))
self.assertTrue(image.get_flags() & pygame.RLEACCELOK)
self.assertTrue(image.get_flags() & pygame.RLEACCEL)
# Now set the alpha, without RLE acceleration - should strip all
# RLE flags
image.set_alpha(90)
self.assertTrue(not image.get_flags() & pygame.RLEACCELOK)
self.assertTrue(not image.get_flags() & pygame.RLEACCEL)
finally:
pygame.display.quit()
def test_set_alpha__set_colorkey_rle(self):
pygame.display.init()
try:
pygame.display.set_mode((640, 480))
blit_to_surf = pygame.Surface((80, 71))
blit_to_surf.fill((255, 255, 255))
image = pygame.image.load(example_path(os.path.join("data", "alien1.png")))
image = image.convert()
orig_colorkey = image.get_colorkey()
# Add the RLE flag while setting alpha for the whole surface
image.set_alpha(90, RLEACCEL)
blit_to_surf.blit(image, (0, 0))
sample_pixel_rle = blit_to_surf.get_at((50, 50))
# Now reset the colorkey to the original value with RLE
self.assertEqual(image.get_colorkey(), orig_colorkey)
image.set_colorkey(orig_colorkey, RLEACCEL)
blit_to_surf.fill((255, 255, 255))
blit_to_surf.blit(image, (0, 0))
sample_pixel_no_rle = blit_to_surf.get_at((50, 50))
self.assertAlmostEqual(sample_pixel_rle.r, sample_pixel_no_rle.r, delta=2)
self.assertAlmostEqual(sample_pixel_rle.g, sample_pixel_no_rle.g, delta=2)
self.assertAlmostEqual(sample_pixel_rle.b, sample_pixel_no_rle.b, delta=2)
finally:
pygame.display.quit()
def test_fill_negative_coordinates(self):
# negative coordinates should be clipped by fill, and not draw outside the surface.
color = (25, 25, 25, 25)
color2 = (20, 20, 20, 25)
fill_rect = pygame.Rect(-10, -10, 16, 16)
s1 = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
r1 = s1.fill(color, fill_rect)
c = s1.get_at((0, 0))
self.assertEqual(c, color)
# make subsurface in the middle to test it doesn't over write.
s2 = s1.subsurface((5, 5, 5, 5))
r2 = s2.fill(color2, (-3, -3, 5, 5))
c2 = s1.get_at((4, 4))
self.assertEqual(c, color)
# rect returns the area we actually fill.
r3 = s2.fill(color2, (-30, -30, 5, 5))
# since we are using negative coords, it should be an zero sized rect.
self.assertEqual(tuple(r3), (0, 0, 0, 0))
def test_fill_keyword_args(self):
"""Ensure fill() accepts keyword arguments."""
color = (1, 2, 3, 255)
area = (1, 1, 2, 2)
s1 = pygame.Surface((4, 4), 0, 32)
s1.fill(special_flags=pygame.BLEND_ADD, color=color, rect=area)
self.assertEqual(s1.get_at((0, 0)), (0, 0, 0, 255))
self.assertEqual(s1.get_at((1, 1)), color)
########################################################################
def test_get_alpha(self):
"""Ensure a surface's alpha value can be retrieved."""
s1 = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
self.assertEqual(s1.get_alpha(), 255)
for alpha in (0, 32, 127, 255):
s1.set_alpha(alpha)
for t in range(4):
s1.set_alpha(s1.get_alpha())
self.assertEqual(s1.get_alpha(), alpha)
########################################################################
def test_get_bytesize(self):
"""Ensure a surface's bit and byte sizes can be retrieved."""
pygame.display.init()
try:
depth = 32
depth_bytes = 4
s1 = pygame.Surface((32, 32), pygame.SRCALPHA, depth)
self.assertEqual(s1.get_bytesize(), depth_bytes)
self.assertEqual(s1.get_bitsize(), depth)
depth = 15
depth_bytes = 2
s1 = pygame.Surface((32, 32), 0, depth)
self.assertEqual(s1.get_bytesize(), depth_bytes)
self.assertEqual(s1.get_bitsize(), depth)
depth = 12
depth_bytes = 2
s1 = pygame.Surface((32, 32), 0, depth)
self.assertEqual(s1.get_bytesize(), depth_bytes)
self.assertEqual(s1.get_bitsize(), depth)
with self.assertRaises(pygame.error):
surface = pygame.display.set_mode()
pygame.display.quit()
surface.get_bytesize()
finally:
pygame.display.quit()
########################################################################
def test_get_flags(self):
"""Ensure a surface's flags can be retrieved."""
s1 = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
self.assertEqual(s1.get_flags(), pygame.SRCALPHA)
@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER") == "dummy",
'requires a non-"dummy" SDL_VIDEODRIVER',
)
def test_get_flags__display_surf(self):
pygame.display.init()
try:
# FULLSCREEN
screen_surf = pygame.display.set_mode((600, 400), flags=0)
self.assertFalse(screen_surf.get_flags() & pygame.FULLSCREEN)
screen_surf = pygame.display.set_mode((600, 400), flags=pygame.FULLSCREEN)
self.assertTrue(screen_surf.get_flags() & pygame.FULLSCREEN)
# NOFRAME
screen_surf = pygame.display.set_mode((600, 400), flags=0)
self.assertFalse(screen_surf.get_flags() & pygame.NOFRAME)
screen_surf = pygame.display.set_mode((600, 400), flags=pygame.NOFRAME)
self.assertTrue(screen_surf.get_flags() & pygame.NOFRAME)
# RESIZABLE
screen_surf = pygame.display.set_mode((600, 400), flags=0)
self.assertFalse(screen_surf.get_flags() & pygame.RESIZABLE)
screen_surf = pygame.display.set_mode((600, 400), flags=pygame.RESIZABLE)
self.assertTrue(screen_surf.get_flags() & pygame.RESIZABLE)
# OPENGL
screen_surf = pygame.display.set_mode((600, 400), flags=0)
# it can have an OPENGL flag by default on Macos?
if not (screen_surf.get_flags() & pygame.OPENGL):
self.assertFalse(screen_surf.get_flags() & pygame.OPENGL)
try:
pygame.display.set_mode((200, 200), pygame.OPENGL, 32)
except pygame.error:
pass # If we can't create OPENGL surface don't try this test
else:
self.assertTrue(screen_surf.get_flags() & pygame.OPENGL)
finally:
pygame.display.quit()
########################################################################
def test_get_parent(self):
"""Ensure a surface's parent can be retrieved."""
pygame.display.init()
try:
parent = pygame.Surface((16, 16))
child = parent.subsurface((0, 0, 5, 5))
self.assertIs(child.get_parent(), parent)
with self.assertRaises(pygame.error):
surface = pygame.display.set_mode()
pygame.display.quit()
surface.get_parent()
finally:
pygame.display.quit()
########################################################################
def test_get_rect(self):
"""Ensure a surface's rect can be retrieved."""
size = (16, 16)
surf = pygame.Surface(size)
rect = surf.get_rect()
self.assertEqual(rect.size, size)
########################################################################
def test_get_width__size_and_height(self):
"""Ensure a surface's size, width and height can be retrieved."""
for w in range(0, 255, 32):
for h in range(0, 127, 15):
s = pygame.Surface((w, h))
self.assertEqual(s.get_width(), w)
self.assertEqual(s.get_height(), h)
self.assertEqual(s.get_size(), (w, h))
def test_get_view(self):
"""Ensure a buffer view of the surface's pixels can be retrieved."""
# Check that BufferProxys are returned when array depth is supported,
# ValueErrors returned otherwise.
Error = ValueError
s = pygame.Surface((5, 7), 0, 8)
v2 = s.get_view("2")
self.assertRaises(Error, s.get_view, "0")
self.assertRaises(Error, s.get_view, "1")
self.assertIsInstance(v2, BufferProxy)
self.assertRaises(Error, s.get_view, "3")
s = pygame.Surface((8, 7), 0, 8)
length = s.get_bytesize() * s.get_width() * s.get_height()
v0 = s.get_view("0")
v1 = s.get_view("1")
self.assertIsInstance(v0, BufferProxy)
self.assertEqual(v0.length, length)
self.assertIsInstance(v1, BufferProxy)
self.assertEqual(v1.length, length)
s = pygame.Surface((5, 7), 0, 16)
v2 = s.get_view("2")
self.assertRaises(Error, s.get_view, "0")
self.assertRaises(Error, s.get_view, "1")
self.assertIsInstance(v2, BufferProxy)
self.assertRaises(Error, s.get_view, "3")
s = pygame.Surface((8, 7), 0, 16)
length = s.get_bytesize() * s.get_width() * s.get_height()
v0 = s.get_view("0")
v1 = s.get_view("1")
self.assertIsInstance(v0, BufferProxy)
self.assertEqual(v0.length, length)
self.assertIsInstance(v1, BufferProxy)
self.assertEqual(v1.length, length)
s = pygame.Surface((5, 7), pygame.SRCALPHA, 16)
v2 = s.get_view("2")
self.assertIsInstance(v2, BufferProxy)
self.assertRaises(Error, s.get_view, "3")
s = pygame.Surface((5, 7), 0, 24)
v2 = s.get_view("2")
v3 = s.get_view("3")
self.assertRaises(Error, s.get_view, "0")
self.assertRaises(Error, s.get_view, "1")
self.assertIsInstance(v2, BufferProxy)
self.assertIsInstance(v3, BufferProxy)
s = pygame.Surface((8, 7), 0, 24)
length = s.get_bytesize() * s.get_width() * s.get_height()
v0 = s.get_view("0")
v1 = s.get_view("1")
self.assertIsInstance(v0, BufferProxy)
self.assertEqual(v0.length, length)
self.assertIsInstance(v1, BufferProxy)
self.assertEqual(v1.length, length)
s = pygame.Surface((5, 7), 0, 32)
length = s.get_bytesize() * s.get_width() * s.get_height()
v0 = s.get_view("0")
v1 = s.get_view("1")
v2 = s.get_view("2")
v3 = s.get_view("3")
self.assertIsInstance(v0, BufferProxy)
self.assertEqual(v0.length, length)
self.assertIsInstance(v1, BufferProxy)
self.assertEqual(v1.length, length)
self.assertIsInstance(v2, BufferProxy)
self.assertIsInstance(v3, BufferProxy)
s2 = s.subsurface((0, 0, 4, 7))
self.assertRaises(Error, s2.get_view, "0")
self.assertRaises(Error, s2.get_view, "1")
s2 = None
s = pygame.Surface((5, 7), pygame.SRCALPHA, 32)
for kind in ("2", "3", "a", "A", "r", "R", "g", "G", "b", "B"):
self.assertIsInstance(s.get_view(kind), BufferProxy)
# Check default argument value: '2'
s = pygame.Surface((2, 4), 0, 32)
v = s.get_view()
if not IS_PYPY:
ai = ArrayInterface(v)
self.assertEqual(ai.nd, 2)
# Check locking.
s = pygame.Surface((2, 4), 0, 32)
self.assertFalse(s.get_locked())
v = s.get_view("2")
self.assertFalse(s.get_locked())
c = v.__array_interface__
self.assertTrue(s.get_locked())
c = None
gc.collect()
self.assertTrue(s.get_locked())
v = None
gc.collect()
self.assertFalse(s.get_locked())
# Check invalid view kind values.
s = pygame.Surface((2, 4), pygame.SRCALPHA, 32)
self.assertRaises(TypeError, s.get_view, "")
self.assertRaises(TypeError, s.get_view, "9")
self.assertRaises(TypeError, s.get_view, "RGBA")
self.assertRaises(TypeError, s.get_view, 2)
# Both unicode and bytes strings are allowed for kind.
s = pygame.Surface((2, 4), 0, 32)
s.get_view("2")
s.get_view(b"2")
# Garbage collection
s = pygame.Surface((2, 4), 0, 32)
weak_s = weakref.ref(s)
v = s.get_view("3")
weak_v = weakref.ref(v)
gc.collect()
self.assertTrue(weak_s() is s)
self.assertTrue(weak_v() is v)
del v
gc.collect()
self.assertTrue(weak_s() is s)
self.assertTrue(weak_v() is None)
del s
gc.collect()
self.assertTrue(weak_s() is None)
def test_get_buffer(self):
# Check that get_buffer works for all pixel sizes and for a subsurface.
# Check for all pixel sizes
for bitsize in [8, 16, 24, 32]:
s = pygame.Surface((5, 7), 0, bitsize)
length = s.get_pitch() * s.get_height()
v = s.get_buffer()
self.assertIsInstance(v, BufferProxy)
self.assertEqual(v.length, length)
self.assertEqual(repr(v), f"<BufferProxy({length})>")
# Check for a subsurface (not contiguous)
s = pygame.Surface((7, 10), 0, 32)
s2 = s.subsurface((1, 2, 5, 7))
length = s2.get_pitch() * s2.get_height()
v = s2.get_buffer()
self.assertIsInstance(v, BufferProxy)
self.assertEqual(v.length, length)
# Check locking.
s = pygame.Surface((2, 4), 0, 32)
v = s.get_buffer()
self.assertTrue(s.get_locked())
v = None
gc.collect()
self.assertFalse(s.get_locked())
OLDBUF = hasattr(pygame.bufferproxy, "get_segcount")
@unittest.skipIf(not OLDBUF, "old buffer not available")
def test_get_buffer_oldbuf(self):
from pygame.bufferproxy import get_segcount, get_write_buffer
s = pygame.Surface((2, 4), pygame.SRCALPHA, 32)
v = s.get_buffer()
segcount, buflen = get_segcount(v)
self.assertEqual(segcount, 1)
self.assertEqual(buflen, s.get_pitch() * s.get_height())
seglen, segaddr = get_write_buffer(v, 0)
self.assertEqual(segaddr, s._pixels_address)
self.assertEqual(seglen, buflen)
@unittest.skipIf(not OLDBUF, "old buffer not available")
def test_get_view_oldbuf(self):
from pygame.bufferproxy import get_segcount, get_write_buffer
s = pygame.Surface((2, 4), pygame.SRCALPHA, 32)
v = s.get_view("1")
segcount, buflen = get_segcount(v)
self.assertEqual(segcount, 8)
self.assertEqual(buflen, s.get_pitch() * s.get_height())
seglen, segaddr = get_write_buffer(v, 7)
self.assertEqual(segaddr, s._pixels_address + s.get_bytesize() * 7)
self.assertEqual(seglen, s.get_bytesize())
def test_set_colorkey(self):
# __doc__ (as of 2008-06-25) for pygame.surface.Surface.set_colorkey:
# Surface.set_colorkey(Color, flags=0): return None
# Surface.set_colorkey(None): return None
# Set the transparent colorkey
s = pygame.Surface((16, 16), pygame.SRCALPHA, 32)
colorkeys = ((20, 189, 20, 255), (128, 50, 50, 255), (23, 21, 255, 255))
for colorkey in colorkeys:
s.set_colorkey(colorkey)
for t in range(4):
s.set_colorkey(s.get_colorkey())
self.assertEqual(s.get_colorkey(), colorkey)
def test_set_masks(self):
s = pygame.Surface((32, 32))
r, g, b, a = s.get_masks()
self.assertRaises(TypeError, s.set_masks, (b, g, r, a))
def test_set_shifts(self):
s = pygame.Surface((32, 32))
r, g, b, a = s.get_shifts()
self.assertRaises(TypeError, s.set_shifts, (b, g, r, a))
def test_blit_keyword_args(self):
color = (1, 2, 3, 255)
s1 = pygame.Surface((4, 4), 0, 32)
s2 = pygame.Surface((2, 2), 0, 32)
s2.fill((1, 2, 3))
s1.blit(special_flags=BLEND_ADD, source=s2, dest=(1, 1), area=s2.get_rect())
self.assertEqual(s1.get_at((0, 0)), (0, 0, 0, 255))
self.assertEqual(s1.get_at((1, 1)), color)
def test_blit_big_rects(self):
"""SDL2 can have more than 16 bits for x, y, width, height."""
big_surf = pygame.Surface((100, 68000), 0, 32)
big_surf_color = (255, 0, 0)
big_surf.fill(big_surf_color)
background = pygame.Surface((500, 500), 0, 32)
background_color = (0, 255, 0)
background.fill(background_color)
# copy parts of the big_surf using more than 16bit parts.
background.blit(big_surf, (100, 100), area=(0, 16000, 100, 100))
background.blit(big_surf, (200, 200), area=(0, 32000, 100, 100))
background.blit(big_surf, (300, 300), area=(0, 66000, 100, 100))
# check that all three areas are drawn.
self.assertEqual(background.get_at((101, 101)), big_surf_color)
self.assertEqual(background.get_at((201, 201)), big_surf_color)
self.assertEqual(background.get_at((301, 301)), big_surf_color)
# areas outside the 3 blitted areas not covered by those blits.
self.assertEqual(background.get_at((400, 301)), background_color)
self.assertEqual(background.get_at((400, 201)), background_color)
self.assertEqual(background.get_at((100, 201)), background_color)
self.assertEqual(background.get_at((99, 99)), background_color)
self.assertEqual(background.get_at((450, 450)), background_color)
class TestSurfaceBlit(unittest.TestCase):
"""Tests basic blitting functionality and options."""
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.blit:
# Surface.blit(source, dest, area=None, special_flags = 0): return Rect
# draw one image onto another
#
# Draws a source Surface onto this Surface. The draw can be positioned
# with the dest argument. Dest can either be pair of coordinates
# representing the upper left corner of the source. A Rect can also be
# passed as the destination and the topleft corner of the rectangle
# will be used as the position for the blit. The size of the
# destination rectangle does not effect the blit.
#
# An optional area rectangle can be passed as well. This represents a
# smaller portion of the source Surface to draw.
#
# An optional special flags is for passing in new in 1.8.0: BLEND_ADD,
# BLEND_SUB, BLEND_MULT, BLEND_MIN, BLEND_MAX new in 1.8.1:
# BLEND_RGBA_ADD, BLEND_RGBA_SUB, BLEND_RGBA_MULT, BLEND_RGBA_MIN,
# BLEND_RGBA_MAX BLEND_RGB_ADD, BLEND_RGB_SUB, BLEND_RGB_MULT,
# BLEND_RGB_MIN, BLEND_RGB_MAX With other special blitting flags
# perhaps added in the future.
#
# The return rectangle is the area of the affected pixels, excluding
# any pixels outside the destination Surface, or outside the clipping
# area.
#
# Pixel alphas will be ignored when blitting to an 8 bit Surface.
# special_flags new in pygame 1.8.
def setUp(self):
"""Resets starting surfaces."""
self.src_surface = pygame.Surface((256, 256), 32)
self.src_surface.fill(pygame.Color(255, 255, 255))
self.dst_surface = pygame.Surface((64, 64), 32)
self.dst_surface.fill(pygame.Color(0, 0, 0))
def test_blit_overflow_coord(self):
"""Full coverage w/ overflow, specified with Coordinate"""
result = self.dst_surface.blit(self.src_surface, (0, 0))
self.assertIsInstance(result, pygame.Rect)
self.assertEqual(result.size, (64, 64))
for k in [(x, x) for x in range(64)]:
self.assertEqual(self.dst_surface.get_at(k), (255, 255, 255))
def test_blit_overflow_rect(self):
"""Full coverage w/ overflow, specified with a Rect"""
result = self.dst_surface.blit(self.src_surface, pygame.Rect(-1, -1, 300, 300))
self.assertIsInstance(result, pygame.Rect)
self.assertEqual(result.size, (64, 64))
for k in [(x, x) for x in range(64)]:
self.assertEqual(self.dst_surface.get_at(k), (255, 255, 255))
def test_blit_overflow_nonorigin(self):
"""Test Rectangle Dest, with overflow but with starting rect with top-left at (1,1)"""
result = self.dst_surface.blit(self.src_surface, dest=pygame.Rect((1, 1, 1, 1)))
self.assertIsInstance(result, pygame.Rect)
self.assertEqual(result.size, (63, 63))
self.assertEqual(self.dst_surface.get_at((0, 0)), (0, 0, 0))
self.assertEqual(self.dst_surface.get_at((63, 0)), (0, 0, 0))
self.assertEqual(self.dst_surface.get_at((0, 63)), (0, 0, 0))
self.assertEqual(self.dst_surface.get_at((1, 1)), (255, 255, 255))
self.assertEqual(self.dst_surface.get_at((63, 63)), (255, 255, 255))
def test_blit_area_contraint(self):
"""Testing area constraint"""
result = self.dst_surface.blit(
self.src_surface,
dest=pygame.Rect((1, 1, 1, 1)),
area=pygame.Rect((2, 2, 2, 2)),
)
self.assertIsInstance(result, pygame.Rect)
self.assertEqual(result.size, (2, 2))
self.assertEqual(self.dst_surface.get_at((0, 0)), (0, 0, 0)) # Corners
self.assertEqual(self.dst_surface.get_at((63, 0)), (0, 0, 0))
self.assertEqual(self.dst_surface.get_at((0, 63)), (0, 0, 0))
self.assertEqual(self.dst_surface.get_at((63, 63)), (0, 0, 0))
self.assertEqual(
self.dst_surface.get_at((1, 1)), (255, 255, 255)
) # Blitted Area
self.assertEqual(self.dst_surface.get_at((2, 2)), (255, 255, 255))
self.assertEqual(self.dst_surface.get_at((3, 3)), (0, 0, 0))
# Should stop short of filling in (3,3)
def test_blit_zero_overlap(self):
"""Testing zero-overlap condition."""
result = self.dst_surface.blit(
self.src_surface,
dest=pygame.Rect((-256, -256, 1, 1)),
area=pygame.Rect((2, 2, 256, 256)),
)
self.assertIsInstance(result, pygame.Rect)
self.assertEqual(result.size, (0, 0)) # No blitting expected
for k in [(x, x) for x in range(64)]:
self.assertEqual(self.dst_surface.get_at(k), (0, 0, 0)) # Diagonal
self.assertEqual(
self.dst_surface.get_at((63, 0)), (0, 0, 0)
) # Remaining corners
self.assertEqual(self.dst_surface.get_at((0, 63)), (0, 0, 0))
def test_blit__SRCALPHA_opaque_source(self):
src = pygame.Surface((256, 256), SRCALPHA, 32)
dst = src.copy()
for i, j in test_utils.rect_area_pts(src.get_rect()):
dst.set_at((i, j), (i, 0, 0, j))
src.set_at((i, j), (0, i, 0, 255))
dst.blit(src, (0, 0))
for pt in test_utils.rect_area_pts(src.get_rect()):
self.assertEqual(dst.get_at(pt)[1], src.get_at(pt)[1])
def test_blit__blit_to_self(self):
"""Test that blit operation works on self, alpha value is
correct, and that no RGB distortion occurs."""
test_surface = pygame.Surface((128, 128), SRCALPHA, 32)
area = test_surface.get_rect()
for pt, test_color in test_utils.gradient(area.width, area.height):
test_surface.set_at(pt, test_color)
reference_surface = test_surface.copy()
test_surface.blit(test_surface, (0, 0))
for x in range(area.width):
for y in range(area.height):
(r, g, b, a) = reference_color = reference_surface.get_at((x, y))
expected_color = (r, g, b, (a + (a * ((256 - a) // 256))))
self.assertEqual(reference_color, expected_color)
self.assertEqual(reference_surface.get_rect(), test_surface.get_rect())
def test_blit__SRCALPHA_to_SRCALPHA_non_zero(self):
"""Tests blitting a nonzero alpha surface to another nonzero alpha surface
both straight alpha compositing method. Test is fuzzy (+/- 1/256) to account for
different implementations in SDL1 and SDL2.
"""
size = (32, 32)
def check_color_diff(color1, color2):
"""Returns True if two colors are within (1, 1, 1, 1) of each other."""
for val in color1 - color2:
if abs(val) > 1:
return False
return True
def high_a_onto_low(high, low):
"""Tests straight alpha case. Source is low alpha, destination is high alpha"""
high_alpha_surface = pygame.Surface(size, pygame.SRCALPHA, 32)
low_alpha_surface = high_alpha_surface.copy()
high_alpha_color = Color(
(high, high, low, high)
) # Injecting some RGB variance.
low_alpha_color = Color((high, low, low, low))
high_alpha_surface.fill(high_alpha_color)
low_alpha_surface.fill(low_alpha_color)
high_alpha_surface.blit(low_alpha_surface, (0, 0))
expected_color = low_alpha_color + Color(
tuple(
((x * (255 - low_alpha_color.a)) // 255) for x in high_alpha_color
)
)
self.assertTrue(
check_color_diff(high_alpha_surface.get_at((0, 0)), expected_color)
)
def low_a_onto_high(high, low):
"""Tests straight alpha case. Source is high alpha, destination is low alpha"""
high_alpha_surface = pygame.Surface(size, pygame.SRCALPHA, 32)
low_alpha_surface = high_alpha_surface.copy()
high_alpha_color = Color(
(high, high, low, high)
) # Injecting some RGB variance.
low_alpha_color = Color((high, low, low, low))
high_alpha_surface.fill(high_alpha_color)
low_alpha_surface.fill(low_alpha_color)
low_alpha_surface.blit(high_alpha_surface, (0, 0))
expected_color = high_alpha_color + Color(
tuple(
((x * (255 - high_alpha_color.a)) // 255) for x in low_alpha_color
)
)
self.assertTrue(
check_color_diff(low_alpha_surface.get_at((0, 0)), expected_color)
)
for low_a in range(0, 128):
for high_a in range(128, 256):
high_a_onto_low(high_a, low_a)
low_a_onto_high(high_a, low_a)
def test_blit__SRCALPHA32_to_8(self):
# Bug: fatal
# SDL_DisplayConvert segfaults when video is uninitialized.
target = pygame.Surface((11, 8), 0, 8)
test_color = target.get_palette_at(2)
source = pygame.Surface((1, 1), pygame.SRCALPHA, 32)
source.set_at((0, 0), test_color)
target.blit(source, (0, 0))
class GeneralSurfaceTests(unittest.TestCase):
@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER") == "dummy",
'requires a non-"dummy" SDL_VIDEODRIVER',
)
def test_image_convert_bug_131(self):
# bug #131: Unable to Surface.convert(32) some 1-bit images.
# https://github.com/pygame/pygame/issues/131
pygame.display.init()
try:
pygame.display.set_mode((640, 480))
im = pygame.image.load(example_path(os.path.join("data", "city.png")))
im2 = pygame.image.load(example_path(os.path.join("data", "brick.png")))
self.assertEqual(im.get_palette(), ((0, 0, 0, 255), (255, 255, 255, 255)))
self.assertEqual(im2.get_palette(), ((0, 0, 0, 255), (0, 0, 0, 255)))
self.assertEqual(repr(im.convert(32)), "<Surface(24x24x32 SW)>")
self.assertEqual(repr(im2.convert(32)), "<Surface(469x137x32 SW)>")
# Ensure a palette format to palette format works.
im3 = im.convert(8)
self.assertEqual(repr(im3), "<Surface(24x24x8 SW)>")
self.assertEqual(im3.get_palette(), im.get_palette())
finally:
pygame.display.quit()
@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER") == "dummy",
'requires a non-"dummy" SDL_VIDEODRIVER',
)
def test_convert_init(self):
"""Ensure initialization exceptions are raised
for surf.convert()."""
pygame.display.quit()
surf = pygame.Surface((1, 1))
self.assertRaisesRegex(pygame.error, "display initialized", surf.convert)
pygame.display.init()
try:
try:
surf.convert(32)
surf.convert(pygame.Surface((1, 1)))
except pygame.error:
self.fail("convert() should not raise an exception here.")
self.assertRaisesRegex(pygame.error, "No video mode", surf.convert)
pygame.display.set_mode((640, 480))
try:
surf.convert()
except pygame.error:
self.fail("convert() should not raise an exception here.")
finally:
pygame.display.quit()
@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER") == "dummy",
'requires a non-"dummy" SDL_VIDEODRIVER',
)
def test_convert_alpha_init(self):
"""Ensure initialization exceptions are raised
for surf.convert_alpha()."""
pygame.display.quit()
surf = pygame.Surface((1, 1))
self.assertRaisesRegex(pygame.error, "display initialized", surf.convert_alpha)
pygame.display.init()
try:
self.assertRaisesRegex(pygame.error, "No video mode", surf.convert_alpha)
pygame.display.set_mode((640, 480))
try:
surf.convert_alpha()
except pygame.error:
self.fail("convert_alpha() should not raise an exception here.")
finally:
pygame.display.quit()
def test_convert_alpha_SRCALPHA(self):
"""Ensure that the surface returned by surf.convert_alpha()
has alpha blending enabled"""
pygame.display.init()
try:
pygame.display.set_mode((640, 480))
s1 = pygame.Surface((100, 100), 0, 32)
# s2=pygame.Surface((100,100), pygame.SRCALPHA, 32)
s1_alpha = s1.convert_alpha()
self.assertEqual(s1_alpha.get_flags() & SRCALPHA, SRCALPHA)
self.assertEqual(s1_alpha.get_alpha(), 255)
finally:
pygame.display.quit()
def test_src_alpha_issue_1289(self):
"""blit should be white."""
surf1 = pygame.Surface((1, 1), pygame.SRCALPHA, 32)
surf1.fill((255, 255, 255, 100))
surf2 = pygame.Surface((1, 1), pygame.SRCALPHA, 32)
self.assertEqual(surf2.get_at((0, 0)), (0, 0, 0, 0))
surf2.blit(surf1, (0, 0))
self.assertEqual(surf1.get_at((0, 0)), (255, 255, 255, 100))
self.assertEqual(surf2.get_at((0, 0)), (255, 255, 255, 100))
def test_src_alpha_compatible(self):
""" "What pygame 1.9.x did". Is the alpha blitter as before?"""
# The table below was generated with the SDL1 blit.
# def print_table():
# nums = [0, 1, 65, 126, 127, 199, 254, 255]
# results = {}
# for dest_r, dest_b, dest_a in zip(nums, reversed(nums), reversed(nums)):
# for src_r, src_b, src_a in zip(nums, reversed(nums), nums):
# src_surf = pygame.Surface((66, 66), pygame.SRCALPHA, 32)
# src_surf.fill((src_r, 255, src_b, src_a))
# dest_surf = pygame.Surface((66, 66), pygame.SRCALPHA, 32)
# dest_surf.fill((dest_r, 255, dest_b, dest_a))
# dest_surf.blit(src_surf, (0, 0))
# key = ((dest_r, dest_b, dest_a), (src_r, src_b, src_a))
# results[key] = dest_surf.get_at((65, 33))
# print("(dest_r, dest_b, dest_a), (src_r, src_b, src_a): color")
# pprint(results)
results_expected = {
((0, 255, 255), (0, 255, 0)): (0, 255, 255, 255),
((0, 255, 255), (1, 254, 1)): (0, 255, 255, 255),
((0, 255, 255), (65, 199, 65)): (16, 255, 241, 255),
((0, 255, 255), (126, 127, 126)): (62, 255, 192, 255),
((0, 255, 255), (127, 126, 127)): (63, 255, 191, 255),
((0, 255, 255), (199, 65, 199)): (155, 255, 107, 255),
((0, 255, 255), (254, 1, 254)): (253, 255, 2, 255),
((0, 255, 255), (255, 0, 255)): (255, 255, 0, 255),
((1, 254, 254), (0, 255, 0)): (1, 255, 254, 254),
((1, 254, 254), (1, 254, 1)): (1, 255, 254, 255),
((1, 254, 254), (65, 199, 65)): (17, 255, 240, 255),
((1, 254, 254), (126, 127, 126)): (63, 255, 191, 255),
((1, 254, 254), (127, 126, 127)): (64, 255, 190, 255),
((1, 254, 254), (199, 65, 199)): (155, 255, 107, 255),
((1, 254, 254), (254, 1, 254)): (253, 255, 2, 255),
((1, 254, 254), (255, 0, 255)): (255, 255, 0, 255),
((65, 199, 199), (0, 255, 0)): (65, 255, 199, 199),
((65, 199, 199), (1, 254, 1)): (64, 255, 200, 200),
((65, 199, 199), (65, 199, 65)): (65, 255, 199, 214),
((65, 199, 199), (126, 127, 126)): (95, 255, 164, 227),
((65, 199, 199), (127, 126, 127)): (96, 255, 163, 227),
((65, 199, 199), (199, 65, 199)): (169, 255, 95, 243),
((65, 199, 199), (254, 1, 254)): (253, 255, 2, 255),
((65, 199, 199), (255, 0, 255)): (255, 255, 0, 255),
((126, 127, 127), (0, 255, 0)): (126, 255, 127, 127),
((126, 127, 127), (1, 254, 1)): (125, 255, 128, 128),
((126, 127, 127), (65, 199, 65)): (110, 255, 146, 160),
((126, 127, 127), (126, 127, 126)): (126, 255, 127, 191),
((126, 127, 127), (127, 126, 127)): (126, 255, 126, 191),
((126, 127, 127), (199, 65, 199)): (183, 255, 79, 227),
((126, 127, 127), (254, 1, 254)): (253, 255, 1, 255),
((126, 127, 127), (255, 0, 255)): (255, 255, 0, 255),
((127, 126, 126), (0, 255, 0)): (127, 255, 126, 126),
((127, 126, 126), (1, 254, 1)): (126, 255, 127, 127),
((127, 126, 126), (65, 199, 65)): (111, 255, 145, 159),
((127, 126, 126), (126, 127, 126)): (127, 255, 126, 190),
((127, 126, 126), (127, 126, 127)): (127, 255, 126, 191),
((127, 126, 126), (199, 65, 199)): (183, 255, 78, 227),
((127, 126, 126), (254, 1, 254)): (254, 255, 1, 255),
((127, 126, 126), (255, 0, 255)): (255, 255, 0, 255),
((199, 65, 65), (0, 255, 0)): (199, 255, 65, 65),
((199, 65, 65), (1, 254, 1)): (198, 255, 66, 66),
((199, 65, 65), (65, 199, 65)): (165, 255, 99, 114),
((199, 65, 65), (126, 127, 126)): (163, 255, 96, 159),
((199, 65, 65), (127, 126, 127)): (163, 255, 95, 160),
((199, 65, 65), (199, 65, 199)): (199, 255, 65, 214),
((199, 65, 65), (254, 1, 254)): (254, 255, 1, 255),
((199, 65, 65), (255, 0, 255)): (255, 255, 0, 255),
((254, 1, 1), (0, 255, 0)): (254, 255, 1, 1),
((254, 1, 1), (1, 254, 1)): (253, 255, 2, 2),
((254, 1, 1), (65, 199, 65)): (206, 255, 52, 66),
((254, 1, 1), (126, 127, 126)): (191, 255, 63, 127),
((254, 1, 1), (127, 126, 127)): (191, 255, 63, 128),
((254, 1, 1), (199, 65, 199)): (212, 255, 51, 200),
((254, 1, 1), (254, 1, 254)): (254, 255, 1, 255),
((254, 1, 1), (255, 0, 255)): (255, 255, 0, 255),
((255, 0, 0), (0, 255, 0)): (0, 255, 255, 0),
((255, 0, 0), (1, 254, 1)): (1, 255, 254, 1),
((255, 0, 0), (65, 199, 65)): (65, 255, 199, 65),
((255, 0, 0), (126, 127, 126)): (126, 255, 127, 126),
((255, 0, 0), (127, 126, 127)): (127, 255, 126, 127),
((255, 0, 0), (199, 65, 199)): (199, 255, 65, 199),
((255, 0, 0), (254, 1, 254)): (254, 255, 1, 254),
((255, 0, 0), (255, 0, 255)): (255, 255, 0, 255),
}
# chosen because they contain edge cases.
nums = [0, 1, 65, 126, 127, 199, 254, 255]
results = {}
for dst_r, dst_b, dst_a in zip(nums, reversed(nums), reversed(nums)):
for src_r, src_b, src_a in zip(nums, reversed(nums), nums):
with self.subTest(
src_r=src_r,
src_b=src_b,
src_a=src_a,
dest_r=dst_r,
dest_b=dst_b,
dest_a=dst_a,
):
src_surf = pygame.Surface((66, 66), pygame.SRCALPHA, 32)
src_surf.fill((src_r, 255, src_b, src_a))
dest_surf = pygame.Surface((66, 66), pygame.SRCALPHA, 32)
dest_surf.fill((dst_r, 255, dst_b, dst_a))
dest_surf.blit(src_surf, (0, 0))
key = ((dst_r, dst_b, dst_a), (src_r, src_b, src_a))
results[key] = dest_surf.get_at((65, 33))
self.assertEqual(results[key], results_expected[key])
self.assertEqual(results, results_expected)
def test_src_alpha_compatible_16bit(self):
""" "What pygame 1.9.x did". Is the alpha blitter as before?"""
# The table below was generated with the SDL1 blit.
# def print_table():
# nums = [0, 1, 65, 126, 127, 199, 254, 255]
# results = {}
# for dest_r, dest_b, dest_a in zip(nums, reversed(nums), reversed(nums)):
# for src_r, src_b, src_a in zip(nums, reversed(nums), nums):
# src_surf = pygame.Surface((66, 66), pygame.SRCALPHA, 16)
# src_surf.fill((src_r, 255, src_b, src_a))
# dest_surf = pygame.Surface((66, 66), pygame.SRCALPHA, 16)
# dest_surf.fill((dest_r, 255, dest_b, dest_a))
# dest_surf.blit(src_surf, (0, 0))
# key = ((dest_r, dest_b, dest_a), (src_r, src_b, src_a))
# results[key] = dest_surf.get_at((65, 33))
# print("(dest_r, dest_b, dest_a), (src_r, src_b, src_a): color")
# pprint(results)
results_expected = {
((0, 255, 255), (0, 255, 0)): (0, 255, 255, 255),
((0, 255, 255), (1, 254, 1)): (0, 255, 255, 255),
((0, 255, 255), (65, 199, 65)): (17, 255, 255, 255),
((0, 255, 255), (126, 127, 126)): (51, 255, 204, 255),
((0, 255, 255), (127, 126, 127)): (51, 255, 204, 255),
((0, 255, 255), (199, 65, 199)): (170, 255, 102, 255),
((0, 255, 255), (254, 1, 254)): (255, 255, 0, 255),
((0, 255, 255), (255, 0, 255)): (255, 255, 0, 255),
((1, 254, 254), (0, 255, 0)): (0, 255, 255, 255),
((1, 254, 254), (1, 254, 1)): (0, 255, 255, 255),
((1, 254, 254), (65, 199, 65)): (17, 255, 255, 255),
((1, 254, 254), (126, 127, 126)): (51, 255, 204, 255),
((1, 254, 254), (127, 126, 127)): (51, 255, 204, 255),
((1, 254, 254), (199, 65, 199)): (170, 255, 102, 255),
((1, 254, 254), (254, 1, 254)): (255, 255, 0, 255),
((1, 254, 254), (255, 0, 255)): (255, 255, 0, 255),
((65, 199, 199), (0, 255, 0)): (68, 255, 204, 204),
((65, 199, 199), (1, 254, 1)): (68, 255, 204, 204),
((65, 199, 199), (65, 199, 65)): (68, 255, 204, 221),
((65, 199, 199), (126, 127, 126)): (85, 255, 170, 238),
((65, 199, 199), (127, 126, 127)): (85, 255, 170, 238),
((65, 199, 199), (199, 65, 199)): (187, 255, 85, 255),
((65, 199, 199), (254, 1, 254)): (255, 255, 0, 255),
((65, 199, 199), (255, 0, 255)): (255, 255, 0, 255),
((126, 127, 127), (0, 255, 0)): (119, 255, 119, 119),
((126, 127, 127), (1, 254, 1)): (119, 255, 119, 119),
((126, 127, 127), (65, 199, 65)): (102, 255, 136, 153),
((126, 127, 127), (126, 127, 126)): (119, 255, 119, 187),
((126, 127, 127), (127, 126, 127)): (119, 255, 119, 187),
((126, 127, 127), (199, 65, 199)): (187, 255, 68, 238),
((126, 127, 127), (254, 1, 254)): (255, 255, 0, 255),
((126, 127, 127), (255, 0, 255)): (255, 255, 0, 255),
((127, 126, 126), (0, 255, 0)): (119, 255, 119, 119),
((127, 126, 126), (1, 254, 1)): (119, 255, 119, 119),
((127, 126, 126), (65, 199, 65)): (102, 255, 136, 153),
((127, 126, 126), (126, 127, 126)): (119, 255, 119, 187),
((127, 126, 126), (127, 126, 127)): (119, 255, 119, 187),
((127, 126, 126), (199, 65, 199)): (187, 255, 68, 238),
((127, 126, 126), (254, 1, 254)): (255, 255, 0, 255),
((127, 126, 126), (255, 0, 255)): (255, 255, 0, 255),
((199, 65, 65), (0, 255, 0)): (204, 255, 68, 68),
((199, 65, 65), (1, 254, 1)): (204, 255, 68, 68),
((199, 65, 65), (65, 199, 65)): (170, 255, 102, 119),
((199, 65, 65), (126, 127, 126)): (170, 255, 85, 153),
((199, 65, 65), (127, 126, 127)): (170, 255, 85, 153),
((199, 65, 65), (199, 65, 199)): (204, 255, 68, 221),
((199, 65, 65), (254, 1, 254)): (255, 255, 0, 255),
((199, 65, 65), (255, 0, 255)): (255, 255, 0, 255),
((254, 1, 1), (0, 255, 0)): (0, 255, 255, 0),
((254, 1, 1), (1, 254, 1)): (0, 255, 255, 0),
((254, 1, 1), (65, 199, 65)): (68, 255, 204, 68),
((254, 1, 1), (126, 127, 126)): (119, 255, 119, 119),
((254, 1, 1), (127, 126, 127)): (119, 255, 119, 119),
((254, 1, 1), (199, 65, 199)): (204, 255, 68, 204),
((254, 1, 1), (254, 1, 254)): (255, 255, 0, 255),
((254, 1, 1), (255, 0, 255)): (255, 255, 0, 255),
((255, 0, 0), (0, 255, 0)): (0, 255, 255, 0),
((255, 0, 0), (1, 254, 1)): (0, 255, 255, 0),
((255, 0, 0), (65, 199, 65)): (68, 255, 204, 68),
((255, 0, 0), (126, 127, 126)): (119, 255, 119, 119),
((255, 0, 0), (127, 126, 127)): (119, 255, 119, 119),
((255, 0, 0), (199, 65, 199)): (204, 255, 68, 204),
((255, 0, 0), (254, 1, 254)): (255, 255, 0, 255),
((255, 0, 0), (255, 0, 255)): (255, 255, 0, 255),
}
# chosen because they contain edge cases.
nums = [0, 1, 65, 126, 127, 199, 254, 255]
results = {}
for dst_r, dst_b, dst_a in zip(nums, reversed(nums), reversed(nums)):
for src_r, src_b, src_a in zip(nums, reversed(nums), nums):
with self.subTest(
src_r=src_r,
src_b=src_b,
src_a=src_a,
dest_r=dst_r,
dest_b=dst_b,
dest_a=dst_a,
):
src_surf = pygame.Surface((66, 66), pygame.SRCALPHA, 16)
src_surf.fill((src_r, 255, src_b, src_a))
dest_surf = pygame.Surface((66, 66), pygame.SRCALPHA, 16)
dest_surf.fill((dst_r, 255, dst_b, dst_a))
dest_surf.blit(src_surf, (0, 0))
key = ((dst_r, dst_b, dst_a), (src_r, src_b, src_a))
results[key] = dest_surf.get_at((65, 33))
self.assertEqual(results[key], results_expected[key])
self.assertEqual(results, results_expected)
def test_sdl1_mimic_blitter_with_set_alpha(self):
"""does the SDL 1 style blitter in pygame 2 work with set_alpha(),
this feature only exists in pygame 2/SDL2 SDL1 did not support
combining surface and pixel alpha"""
results_expected = {
((0, 255, 255), (0, 255, 0)): (0, 255, 255, 255),
((0, 255, 255), (1, 254, 1)): (0, 255, 255, 255),
((0, 255, 255), (65, 199, 65)): (16, 255, 241, 255),
((0, 255, 255), (126, 127, 126)): (62, 255, 192, 255),
((0, 255, 255), (127, 126, 127)): (63, 255, 191, 255),
((0, 255, 255), (199, 65, 199)): (155, 255, 107, 255),
((0, 255, 255), (254, 1, 254)): (253, 255, 2, 255),
((0, 255, 255), (255, 0, 255)): (255, 255, 0, 255),
((1, 254, 254), (0, 255, 0)): (1, 255, 254, 254),
((1, 254, 254), (1, 254, 1)): (1, 255, 254, 255),
((1, 254, 254), (65, 199, 65)): (17, 255, 240, 255),
((1, 254, 254), (126, 127, 126)): (63, 255, 191, 255),
((1, 254, 254), (127, 126, 127)): (64, 255, 190, 255),
((1, 254, 254), (199, 65, 199)): (155, 255, 107, 255),
((1, 254, 254), (254, 1, 254)): (253, 255, 2, 255),
((1, 254, 254), (255, 0, 255)): (255, 255, 0, 255),
((65, 199, 199), (0, 255, 0)): (65, 255, 199, 199),
((65, 199, 199), (1, 254, 1)): (64, 255, 200, 200),
((65, 199, 199), (65, 199, 65)): (65, 255, 199, 214),
((65, 199, 199), (126, 127, 126)): (95, 255, 164, 227),
((65, 199, 199), (127, 126, 127)): (96, 255, 163, 227),
((65, 199, 199), (199, 65, 199)): (169, 255, 95, 243),
((65, 199, 199), (254, 1, 254)): (253, 255, 2, 255),
((65, 199, 199), (255, 0, 255)): (255, 255, 0, 255),
((126, 127, 127), (0, 255, 0)): (126, 255, 127, 127),
((126, 127, 127), (1, 254, 1)): (125, 255, 128, 128),
((126, 127, 127), (65, 199, 65)): (110, 255, 146, 160),
((126, 127, 127), (126, 127, 126)): (126, 255, 127, 191),
((126, 127, 127), (127, 126, 127)): (126, 255, 126, 191),
((126, 127, 127), (199, 65, 199)): (183, 255, 79, 227),
((126, 127, 127), (254, 1, 254)): (253, 255, 1, 255),
((126, 127, 127), (255, 0, 255)): (255, 255, 0, 255),
((127, 126, 126), (0, 255, 0)): (127, 255, 126, 126),
((127, 126, 126), (1, 254, 1)): (126, 255, 127, 127),
((127, 126, 126), (65, 199, 65)): (111, 255, 145, 159),
((127, 126, 126), (126, 127, 126)): (127, 255, 126, 190),
((127, 126, 126), (127, 126, 127)): (127, 255, 126, 191),
((127, 126, 126), (199, 65, 199)): (183, 255, 78, 227),
((127, 126, 126), (254, 1, 254)): (254, 255, 1, 255),
((127, 126, 126), (255, 0, 255)): (255, 255, 0, 255),
((199, 65, 65), (0, 255, 0)): (199, 255, 65, 65),
((199, 65, 65), (1, 254, 1)): (198, 255, 66, 66),
((199, 65, 65), (65, 199, 65)): (165, 255, 99, 114),
((199, 65, 65), (126, 127, 126)): (163, 255, 96, 159),
((199, 65, 65), (127, 126, 127)): (163, 255, 95, 160),
((199, 65, 65), (199, 65, 199)): (199, 255, 65, 214),
((199, 65, 65), (254, 1, 254)): (254, 255, 1, 255),
((199, 65, 65), (255, 0, 255)): (255, 255, 0, 255),
((254, 1, 1), (0, 255, 0)): (254, 255, 1, 1),
((254, 1, 1), (1, 254, 1)): (253, 255, 2, 2),
((254, 1, 1), (65, 199, 65)): (206, 255, 52, 66),
((254, 1, 1), (126, 127, 126)): (191, 255, 63, 127),
((254, 1, 1), (127, 126, 127)): (191, 255, 63, 128),
((254, 1, 1), (199, 65, 199)): (212, 255, 51, 200),
((254, 1, 1), (254, 1, 254)): (254, 255, 1, 255),
((254, 1, 1), (255, 0, 255)): (255, 255, 0, 255),
((255, 0, 0), (0, 255, 0)): (0, 255, 255, 0),
((255, 0, 0), (1, 254, 1)): (1, 255, 254, 1),
((255, 0, 0), (65, 199, 65)): (65, 255, 199, 65),
((255, 0, 0), (126, 127, 126)): (126, 255, 127, 126),
((255, 0, 0), (127, 126, 127)): (127, 255, 126, 127),
((255, 0, 0), (199, 65, 199)): (199, 255, 65, 199),
((255, 0, 0), (254, 1, 254)): (254, 255, 1, 254),
((255, 0, 0), (255, 0, 255)): (255, 255, 0, 255),
}
# chosen because they contain edge cases.
nums = [0, 1, 65, 126, 127, 199, 254, 255]
results = {}
for dst_r, dst_b, dst_a in zip(nums, reversed(nums), reversed(nums)):
for src_r, src_b, src_a in zip(nums, reversed(nums), nums):
with self.subTest(
src_r=src_r,
src_b=src_b,
src_a=src_a,
dest_r=dst_r,
dest_b=dst_b,
dest_a=dst_a,
):
src_surf = pygame.Surface((66, 66), pygame.SRCALPHA, 32)
src_surf.fill((src_r, 255, src_b, 255))
src_surf.set_alpha(src_a)
dest_surf = pygame.Surface((66, 66), pygame.SRCALPHA, 32)
dest_surf.fill((dst_r, 255, dst_b, dst_a))
dest_surf.blit(src_surf, (0, 0))
key = ((dst_r, dst_b, dst_a), (src_r, src_b, src_a))
results[key] = dest_surf.get_at((65, 33))
self.assertEqual(results[key], results_expected[key])
self.assertEqual(results, results_expected)
@unittest.skipIf(
"arm" in platform.machine() or "aarch64" in platform.machine(),
"sdl2 blitter produces different results on arm",
)
def test_src_alpha_sdl2_blitter(self):
"""Checking that the BLEND_ALPHA_SDL2 flag works - this feature
only exists when using SDL2"""
results_expected = {
((0, 255, 255), (0, 255, 0)): (0, 255, 255, 255),
((0, 255, 255), (1, 254, 1)): (0, 253, 253, 253),
((0, 255, 255), (65, 199, 65)): (16, 253, 239, 253),
((0, 255, 255), (126, 127, 126)): (62, 253, 190, 253),
((0, 255, 255), (127, 126, 127)): (63, 253, 189, 253),
((0, 255, 255), (199, 65, 199)): (154, 253, 105, 253),
((0, 255, 255), (254, 1, 254)): (252, 253, 0, 253),
((0, 255, 255), (255, 0, 255)): (255, 255, 0, 255),
((1, 254, 254), (0, 255, 0)): (1, 255, 254, 254),
((1, 254, 254), (1, 254, 1)): (0, 253, 252, 252),
((1, 254, 254), (65, 199, 65)): (16, 253, 238, 252),
((1, 254, 254), (126, 127, 126)): (62, 253, 189, 252),
((1, 254, 254), (127, 126, 127)): (63, 253, 189, 253),
((1, 254, 254), (199, 65, 199)): (154, 253, 105, 253),
((1, 254, 254), (254, 1, 254)): (252, 253, 0, 253),
((1, 254, 254), (255, 0, 255)): (255, 255, 0, 255),
((65, 199, 199), (0, 255, 0)): (65, 255, 199, 199),
((65, 199, 199), (1, 254, 1)): (64, 253, 197, 197),
((65, 199, 199), (65, 199, 65)): (64, 253, 197, 211),
((65, 199, 199), (126, 127, 126)): (94, 253, 162, 225),
((65, 199, 199), (127, 126, 127)): (95, 253, 161, 225),
((65, 199, 199), (199, 65, 199)): (168, 253, 93, 241),
((65, 199, 199), (254, 1, 254)): (252, 253, 0, 253),
((65, 199, 199), (255, 0, 255)): (255, 255, 0, 255),
((126, 127, 127), (0, 255, 0)): (126, 255, 127, 127),
((126, 127, 127), (1, 254, 1)): (125, 253, 126, 126),
((126, 127, 127), (65, 199, 65)): (109, 253, 144, 158),
((126, 127, 127), (126, 127, 126)): (125, 253, 125, 188),
((126, 127, 127), (127, 126, 127)): (126, 253, 125, 189),
((126, 127, 127), (199, 65, 199)): (181, 253, 77, 225),
((126, 127, 127), (254, 1, 254)): (252, 253, 0, 253),
((126, 127, 127), (255, 0, 255)): (255, 255, 0, 255),
((127, 126, 126), (0, 255, 0)): (127, 255, 126, 126),
((127, 126, 126), (1, 254, 1)): (126, 253, 125, 125),
((127, 126, 126), (65, 199, 65)): (110, 253, 143, 157),
((127, 126, 126), (126, 127, 126)): (125, 253, 125, 188),
((127, 126, 126), (127, 126, 127)): (126, 253, 125, 189),
((127, 126, 126), (199, 65, 199)): (181, 253, 77, 225),
((127, 126, 126), (254, 1, 254)): (252, 253, 0, 253),
((127, 126, 126), (255, 0, 255)): (255, 255, 0, 255),
((199, 65, 65), (0, 255, 0)): (199, 255, 65, 65),
((199, 65, 65), (1, 254, 1)): (197, 253, 64, 64),
((199, 65, 65), (65, 199, 65)): (163, 253, 98, 112),
((199, 65, 65), (126, 127, 126)): (162, 253, 94, 157),
((199, 65, 65), (127, 126, 127)): (162, 253, 94, 158),
((199, 65, 65), (199, 65, 199)): (197, 253, 64, 212),
((199, 65, 65), (254, 1, 254)): (252, 253, 0, 253),
((199, 65, 65), (255, 0, 255)): (255, 255, 0, 255),
((254, 1, 1), (0, 255, 0)): (254, 255, 1, 1),
((254, 1, 1), (1, 254, 1)): (252, 253, 0, 0),
((254, 1, 1), (65, 199, 65)): (204, 253, 50, 64),
((254, 1, 1), (126, 127, 126)): (189, 253, 62, 125),
((254, 1, 1), (127, 126, 127)): (190, 253, 62, 126),
((254, 1, 1), (199, 65, 199)): (209, 253, 50, 198),
((254, 1, 1), (254, 1, 254)): (252, 253, 0, 253),
((254, 1, 1), (255, 0, 255)): (255, 255, 0, 255),
((255, 0, 0), (0, 255, 0)): (255, 255, 0, 0),
((255, 0, 0), (1, 254, 1)): (253, 253, 0, 0),
((255, 0, 0), (65, 199, 65)): (205, 253, 50, 64),
((255, 0, 0), (126, 127, 126)): (190, 253, 62, 125),
((255, 0, 0), (127, 126, 127)): (190, 253, 62, 126),
((255, 0, 0), (199, 65, 199)): (209, 253, 50, 198),
((255, 0, 0), (254, 1, 254)): (252, 253, 0, 253),
((255, 0, 0), (255, 0, 255)): (255, 255, 0, 255),
}
# chosen because they contain edge cases.
nums = [0, 1, 65, 126, 127, 199, 254, 255]
results = {}
for dst_r, dst_b, dst_a in zip(nums, reversed(nums), reversed(nums)):
for src_r, src_b, src_a in zip(nums, reversed(nums), nums):
with self.subTest(
src_r=src_r,
src_b=src_b,
src_a=src_a,
dest_r=dst_r,
dest_b=dst_b,
dest_a=dst_a,
):
src_surf = pygame.Surface((66, 66), pygame.SRCALPHA, 32)
src_surf.fill((src_r, 255, src_b, src_a))
dest_surf = pygame.Surface((66, 66), pygame.SRCALPHA, 32)
dest_surf.fill((dst_r, 255, dst_b, dst_a))
dest_surf.blit(
src_surf, (0, 0), special_flags=pygame.BLEND_ALPHA_SDL2
)
key = ((dst_r, dst_b, dst_a), (src_r, src_b, src_a))
results[key] = tuple(dest_surf.get_at((65, 33)))
for i in range(4):
self.assertAlmostEqual(
results[key][i], results_expected[key][i], delta=4
)
# print("(dest_r, dest_b, dest_a), (src_r, src_b, src_a): color")
# pprint(results)
def test_opaque_destination_blit_with_set_alpha(self):
# no set_alpha()
src_surf = pygame.Surface((32, 32), pygame.SRCALPHA, 32)
src_surf.fill((255, 255, 255, 200))
dest_surf = pygame.Surface((32, 32))
dest_surf.fill((100, 100, 100))
dest_surf.blit(src_surf, (0, 0))
no_surf_alpha_col = dest_surf.get_at((0, 0))
dest_surf.fill((100, 100, 100))
dest_surf.set_alpha(200)
dest_surf.blit(src_surf, (0, 0))
surf_alpha_col = dest_surf.get_at((0, 0))
self.assertEqual(no_surf_alpha_col, surf_alpha_col)
def todo_test_convert(self):
self.fail()
# Below should not use a display Surface, but create one and check it is converted
# to the depth of the display surface.
# def test_convert(self):
# """Ensure to creates a new copy of the Surface with the pixel format changed"""
# width = 23
# height = 17
# size = (width, height)
# flags = 0
# depth = 32
# pygame.display.init()
# try:
# convert_surface = pygame.display.set_mode(size)
# surface = pygame.surface.Surface.convert(convert_surface)
# self.assertIsNot(surface, convert_surface)
# self.assertNotEqual(surface.get_size(), size)
# depth_surface = pygame.display.set_mode(size, flags, depth)
# surface2 = pygame.surface.Surface.convert(depth_surface)
# self.assertIsNot(surface2, depth_surface)
# self.assertEqual(surface2.get_size(), size)
# finally:
# pygame.display.quit()
def test_convert__pixel_format_as_surface_subclass(self):
"""Ensure convert accepts a Surface subclass argument."""
expected_size = (23, 17)
convert_surface = SurfaceSubclass(expected_size, 0, 32)
depth_surface = SurfaceSubclass((31, 61), 0, 32)
pygame.display.init()
try:
surface = convert_surface.convert(depth_surface)
self.assertIsNot(surface, depth_surface)
self.assertIsNot(surface, convert_surface)
self.assertIsInstance(surface, pygame.Surface)
self.assertIsInstance(surface, SurfaceSubclass)
self.assertEqual(surface.get_size(), expected_size)
finally:
pygame.display.quit()
def test_convert_alpha(self):
"""Ensure the surface returned by surf.convert_alpha
has alpha values added"""
pygame.display.init()
try:
pygame.display.set_mode((640, 480))
s1 = pygame.Surface((100, 100), 0, 32)
s1_alpha = pygame.Surface.convert_alpha(s1)
s2 = pygame.Surface((100, 100), 0, 32)
s2_alpha = s2.convert_alpha()
s3 = pygame.Surface((100, 100), 0, 8)
s3_alpha = s3.convert_alpha()
s4 = pygame.Surface((100, 100), 0, 12)
s4_alpha = s4.convert_alpha()
s5 = pygame.Surface((100, 100), 0, 15)
s5_alpha = s5.convert_alpha()
s6 = pygame.Surface((100, 100), 0, 16)
s6_alpha = s6.convert_alpha()
s7 = pygame.Surface((100, 100), 0, 24)
s7_alpha = s7.convert_alpha()
self.assertEqual(s1_alpha.get_alpha(), 255)
self.assertEqual(s2_alpha.get_alpha(), 255)
self.assertEqual(s3_alpha.get_alpha(), 255)
self.assertEqual(s4_alpha.get_alpha(), 255)
self.assertEqual(s5_alpha.get_alpha(), 255)
self.assertEqual(s6_alpha.get_alpha(), 255)
self.assertEqual(s7_alpha.get_alpha(), 255)
self.assertEqual(s1_alpha.get_bitsize(), 32)
self.assertEqual(s2_alpha.get_bitsize(), 32)
self.assertEqual(s3_alpha.get_bitsize(), 32)
self.assertEqual(s4_alpha.get_bitsize(), 32)
self.assertEqual(s5_alpha.get_bitsize(), 32)
self.assertEqual(s6_alpha.get_bitsize(), 32)
self.assertEqual(s6_alpha.get_bitsize(), 32)
with self.assertRaises(pygame.error):
surface = pygame.display.set_mode()
pygame.display.quit()
surface.convert_alpha()
finally:
pygame.display.quit()
def test_convert_alpha__pixel_format_as_surface_subclass(self):
"""Ensure convert_alpha accepts a Surface subclass argument."""
expected_size = (23, 17)
convert_surface = SurfaceSubclass(expected_size, SRCALPHA, 32)
depth_surface = SurfaceSubclass((31, 57), SRCALPHA, 32)
pygame.display.init()
try:
pygame.display.set_mode((60, 60))
# This is accepted as an argument, but its values are ignored.
# See issue #599.
surface = convert_surface.convert_alpha(depth_surface)
self.assertIsNot(surface, depth_surface)
self.assertIsNot(surface, convert_surface)
self.assertIsInstance(surface, pygame.Surface)
self.assertIsInstance(surface, SurfaceSubclass)
self.assertEqual(surface.get_size(), expected_size)
finally:
pygame.display.quit()
def test_get_abs_offset(self):
pygame.display.init()
try:
parent = pygame.Surface((64, 64), SRCALPHA, 32)
# Stack bunch of subsurfaces
sub_level_1 = parent.subsurface((2, 2), (34, 37))
sub_level_2 = sub_level_1.subsurface((0, 0), (30, 29))
sub_level_3 = sub_level_2.subsurface((3, 7), (20, 21))
sub_level_4 = sub_level_3.subsurface((6, 1), (14, 14))
sub_level_5 = sub_level_4.subsurface((5, 6), (3, 4))
# Parent is always (0, 0)
self.assertEqual(parent.get_abs_offset(), (0, 0))
# Total offset: (0+2, 0+2) = (2, 2)
self.assertEqual(sub_level_1.get_abs_offset(), (2, 2))
# Total offset: (0+2+0, 0+2+0) = (2, 2)
self.assertEqual(sub_level_2.get_abs_offset(), (2, 2))
# Total offset: (0+2+0+3, 0+2+0+7) = (5, 9)
self.assertEqual(sub_level_3.get_abs_offset(), (5, 9))
# Total offset: (0+2+0+3+6, 0+2+0+7+1) = (11, 10)
self.assertEqual(sub_level_4.get_abs_offset(), (11, 10))
# Total offset: (0+2+0+3+6+5, 0+2+0+7+1+6) = (16, 16)
self.assertEqual(sub_level_5.get_abs_offset(), (16, 16))
with self.assertRaises(pygame.error):
surface = pygame.display.set_mode()
pygame.display.quit()
surface.get_abs_offset()
finally:
pygame.display.quit()
def test_get_abs_parent(self):
pygame.display.init()
try:
parent = pygame.Surface((32, 32), SRCALPHA, 32)
# Stack bunch of subsurfaces
sub_level_1 = parent.subsurface((1, 1), (15, 15))
sub_level_2 = sub_level_1.subsurface((1, 1), (12, 12))
sub_level_3 = sub_level_2.subsurface((1, 1), (9, 9))
sub_level_4 = sub_level_3.subsurface((1, 1), (8, 8))
sub_level_5 = sub_level_4.subsurface((2, 2), (3, 4))
sub_level_6 = sub_level_5.subsurface((0, 0), (2, 1))
# Can't have subsurfaces bigger than parents
self.assertRaises(ValueError, parent.subsurface, (5, 5), (100, 100))
self.assertRaises(ValueError, sub_level_3.subsurface, (0, 0), (11, 5))
self.assertRaises(ValueError, sub_level_6.subsurface, (0, 0), (5, 5))
# Calling get_abs_parent on parent should return itself
self.assertEqual(parent.get_abs_parent(), parent)
# On subclass "depth" of 1, get_abs_parent and get_parent should return the same
self.assertEqual(sub_level_1.get_abs_parent(), sub_level_1.get_parent())
self.assertEqual(sub_level_2.get_abs_parent(), parent)
self.assertEqual(sub_level_3.get_abs_parent(), parent)
self.assertEqual(sub_level_4.get_abs_parent(), parent)
self.assertEqual(sub_level_5.get_abs_parent(), parent)
self.assertEqual(
sub_level_6.get_abs_parent(), sub_level_6.get_parent().get_abs_parent()
)
with self.assertRaises(pygame.error):
surface = pygame.display.set_mode()
pygame.display.quit()
surface.get_abs_parent()
finally:
pygame.display.quit()
def test_get_at(self):
surf = pygame.Surface((2, 2), 0, 24)
c00 = pygame.Color(1, 2, 3)
c01 = pygame.Color(5, 10, 15)
c10 = pygame.Color(100, 50, 0)
c11 = pygame.Color(4, 5, 6)
surf.set_at((0, 0), c00)
surf.set_at((0, 1), c01)
surf.set_at((1, 0), c10)
surf.set_at((1, 1), c11)
c = surf.get_at((0, 0))
self.assertIsInstance(c, pygame.Color)
self.assertEqual(c, c00)
self.assertEqual(surf.get_at((0, 1)), c01)
self.assertEqual(surf.get_at((1, 0)), c10)
self.assertEqual(surf.get_at((1, 1)), c11)
for p in [(-1, 0), (0, -1), (2, 0), (0, 2)]:
self.assertRaises(IndexError, surf.get_at, p)
def test_get_at_mapped(self):
color = pygame.Color(10, 20, 30)
for bitsize in [8, 16, 24, 32]:
surf = pygame.Surface((2, 2), 0, bitsize)
surf.fill(color)
pixel = surf.get_at_mapped((0, 0))
self.assertEqual(
pixel,
surf.map_rgb(color),
"%i != %i, bitsize: %i" % (pixel, surf.map_rgb(color), bitsize),
)
def test_get_bitsize(self):
pygame.display.init()
try:
expected_size = (11, 21)
# Check that get_bitsize returns passed depth
expected_depth = 32
surface = pygame.Surface(expected_size, pygame.SRCALPHA, expected_depth)
self.assertEqual(surface.get_size(), expected_size)
self.assertEqual(surface.get_bitsize(), expected_depth)
expected_depth = 16
surface = pygame.Surface(expected_size, pygame.SRCALPHA, expected_depth)
self.assertEqual(surface.get_size(), expected_size)
self.assertEqual(surface.get_bitsize(), expected_depth)
expected_depth = 15
surface = pygame.Surface(expected_size, 0, expected_depth)
self.assertEqual(surface.get_size(), expected_size)
self.assertEqual(surface.get_bitsize(), expected_depth)
# Check for invalid depths
expected_depth = -1
self.assertRaises(
ValueError, pygame.Surface, expected_size, 0, expected_depth
)
expected_depth = 11
self.assertRaises(
ValueError, pygame.Surface, expected_size, 0, expected_depth
)
expected_depth = 1024
self.assertRaises(
ValueError, pygame.Surface, expected_size, 0, expected_depth
)
with self.assertRaises(pygame.error):
surface = pygame.display.set_mode()
pygame.display.quit()
surface.get_bitsize()
finally:
pygame.display.quit()
def test_get_clip(self):
s = pygame.Surface((800, 600))
rectangle = s.get_clip()
self.assertEqual(rectangle, (0, 0, 800, 600))
def test_get_colorkey(self):
pygame.display.init()
try:
# if set_colorkey is not used
s = pygame.Surface((800, 600), 0, 32)
self.assertIsNone(s.get_colorkey())
# if set_colorkey is used
s.set_colorkey(None)
self.assertIsNone(s.get_colorkey())
# setting up remainder of tests...
r, g, b, a = 20, 40, 60, 12
colorkey = pygame.Color(r, g, b)
s.set_colorkey(colorkey)
# test for ideal case
self.assertEqual(s.get_colorkey(), (r, g, b, 255))
# test for if the color_key is set using pygame.RLEACCEL
s.set_colorkey(colorkey, pygame.RLEACCEL)
self.assertEqual(s.get_colorkey(), (r, g, b, 255))
# test for if the color key is not what's expected
s.set_colorkey(pygame.Color(r + 1, g + 1, b + 1))
self.assertNotEqual(s.get_colorkey(), (r, g, b, 255))
s.set_colorkey(pygame.Color(r, g, b, a))
# regardless of whether alpha is not 255
# colorkey returned from surface is always 255
self.assertEqual(s.get_colorkey(), (r, g, b, 255))
finally:
# test for using method after display.quit() is called...
s = pygame.display.set_mode((200, 200), 0, 32)
pygame.display.quit()
with self.assertRaises(pygame.error):
s.get_colorkey()
def test_get_height(self):
sizes = ((1, 1), (119, 10), (10, 119), (1, 1000), (1000, 1), (1000, 1000))
for width, height in sizes:
surf = pygame.Surface((width, height))
found_height = surf.get_height()
self.assertEqual(height, found_height)
def test_get_locked(self):
def blit_locked_test(surface):
newSurf = pygame.Surface((10, 10))
try:
newSurf.blit(surface, (0, 0))
except pygame.error:
return True
else:
return False
surf = pygame.Surface((100, 100))
self.assertIs(surf.get_locked(), blit_locked_test(surf)) # Unlocked
# Surface should lock
surf.lock()
self.assertIs(surf.get_locked(), blit_locked_test(surf)) # Locked
# Surface should unlock
surf.unlock()
self.assertIs(surf.get_locked(), blit_locked_test(surf)) # Unlocked
# Check multiple locks
surf = pygame.Surface((100, 100))
surf.lock()
surf.lock()
self.assertIs(surf.get_locked(), blit_locked_test(surf)) # Locked
surf.unlock()
self.assertIs(surf.get_locked(), blit_locked_test(surf)) # Locked
surf.unlock()
self.assertIs(surf.get_locked(), blit_locked_test(surf)) # Unlocked
# Check many locks
surf = pygame.Surface((100, 100))
for i in range(1000):
surf.lock()
self.assertIs(surf.get_locked(), blit_locked_test(surf)) # Locked
for i in range(1000):
surf.unlock()
self.assertFalse(surf.get_locked()) # Unlocked
# Unlocking an unlocked surface
surf = pygame.Surface((100, 100))
surf.unlock()
self.assertIs(surf.get_locked(), blit_locked_test(surf)) # Unlocked
surf.unlock()
self.assertIs(surf.get_locked(), blit_locked_test(surf)) # Unlocked
def test_get_locks(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.get_locks:
# Surface.get_locks(): return tuple
# Gets the locks for the Surface
#
# Returns the currently existing locks for the Surface.
# test on a surface that is not initially locked
surface = pygame.Surface((100, 100))
self.assertEqual(surface.get_locks(), ())
# test on the same surface after it has been locked
surface.lock()
self.assertEqual(surface.get_locks(), (surface,))
# test on the same surface after it has been unlocked
surface.unlock()
self.assertEqual(surface.get_locks(), ())
# test with PixelArray initialization: locks surface
pxarray = pygame.PixelArray(surface)
self.assertNotEqual(surface.get_locks(), ())
# closing the PixelArray releases the surface lock
pxarray.close()
self.assertEqual(surface.get_locks(), ())
# AttributeError raised when called on invalid object type (i.e. not a pygame.Surface object)
with self.assertRaises(AttributeError):
"DUMMY".get_locks()
# test multiple locks and unlocks on the same surface
surface.lock()
surface.lock()
surface.lock()
self.assertEqual(surface.get_locks(), (surface, surface, surface))
surface.unlock()
surface.unlock()
self.assertEqual(surface.get_locks(), (surface,))
surface.unlock()
self.assertEqual(surface.get_locks(), ())
def test_get_losses(self):
"""Ensure a surface's losses can be retrieved"""
pygame.display.init()
try:
# Masks for different color component configurations
mask8 = (224, 28, 3, 0)
mask15 = (31744, 992, 31, 0)
mask16 = (63488, 2016, 31, 0)
mask24 = (16711680, 65280, 255, 0)
mask32 = (4278190080, 16711680, 65280, 255)
# Surfaces with standard depths and masks
display_surf = pygame.display.set_mode((100, 100))
surf = pygame.Surface((100, 100))
surf_8bit = pygame.Surface((100, 100), depth=8, masks=mask8)
surf_15bit = pygame.Surface((100, 100), depth=15, masks=mask15)
surf_16bit = pygame.Surface((100, 100), depth=16, masks=mask16)
surf_24bit = pygame.Surface((100, 100), depth=24, masks=mask24)
surf_32bit = pygame.Surface((100, 100), depth=32, masks=mask32)
# Test output is correct type, length, and value range
losses = surf.get_losses()
self.assertIsInstance(losses, tuple)
self.assertEqual(len(losses), 4)
for loss in losses:
self.assertIsInstance(loss, int)
self.assertGreaterEqual(loss, 0)
self.assertLessEqual(loss, 8)
# Test each surface for correct losses
# Display surface losses gives idea of default surface losses
if display_surf.get_losses() == (0, 0, 0, 8):
self.assertEqual(losses, (0, 0, 0, 8))
elif display_surf.get_losses() == (8, 8, 8, 8):
self.assertEqual(losses, (8, 8, 8, 8))
self.assertEqual(surf_8bit.get_losses(), (5, 5, 6, 8))
self.assertEqual(surf_15bit.get_losses(), (3, 3, 3, 8))
self.assertEqual(surf_16bit.get_losses(), (3, 2, 3, 8))
self.assertEqual(surf_24bit.get_losses(), (0, 0, 0, 8))
self.assertEqual(surf_32bit.get_losses(), (0, 0, 0, 0))
# Method should fail when display is not initialized
with self.assertRaises(pygame.error):
surface = pygame.display.set_mode((100, 100))
pygame.display.quit()
surface.get_losses()
finally:
pygame.display.quit()
def test_get_masks__rgba(self):
"""
Ensure that get_mask can return RGBA mask.
"""
masks = [
(0x0F00, 0x00F0, 0x000F, 0xF000),
(0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000),
]
depths = [16, 32]
for expected, depth in list(zip(masks, depths)):
surface = pygame.Surface((10, 10), pygame.SRCALPHA, depth)
self.assertEqual(expected, surface.get_masks())
def test_get_masks__rgb(self):
"""
Ensure that get_mask can return RGB mask.
"""
masks = [
(0x60, 0x1C, 0x03, 0x00),
(0xF00, 0x0F0, 0x00F, 0x000),
(0x7C00, 0x03E0, 0x001F, 0x0000),
(0xF800, 0x07E0, 0x001F, 0x0000),
(0xFF0000, 0x00FF00, 0x0000FF, 0x000000),
(0xFF0000, 0x00FF00, 0x0000FF, 0x000000),
]
depths = [8, 12, 15, 16, 24, 32]
for expected, depth in list(zip(masks, depths)):
surface = pygame.Surface((10, 10), 0, depth)
if depth == 8:
expected = (0x00, 0x00, 0x00, 0x00)
self.assertEqual(expected, surface.get_masks())
def test_get_masks__no_surface(self):
"""
Ensure that after display.quit, calling get_masks raises pygame.error.
"""
with self.assertRaises(pygame.error):
surface = pygame.display.set_mode((10, 10))
pygame.display.quit()
surface.get_masks()
def test_get_offset(self):
"""get_offset returns the (0,0) if surface is not a child
returns the position of child subsurface inside of parent
"""
pygame.display.init()
try:
surf = pygame.Surface((100, 100))
self.assertEqual(surf.get_offset(), (0, 0))
# subsurface offset test
subsurf = surf.subsurface(1, 1, 10, 10)
self.assertEqual(subsurf.get_offset(), (1, 1))
with self.assertRaises(pygame.error):
surface = pygame.display.set_mode()
pygame.display.quit()
surface.get_offset()
finally:
pygame.display.quit()
def test_get_palette(self):
palette = [Color(i, i, i) for i in range(256)]
surf = pygame.Surface((2, 2), 0, 8)
surf.set_palette(palette)
palette2 = surf.get_palette()
self.assertEqual(len(palette2), len(palette))
for c2, c in zip(palette2, palette):
self.assertEqual(c2, c)
for c in palette2:
self.assertIsInstance(c, pygame.Color)
def test_get_palette_at(self):
# See also test_get_palette
surf = pygame.Surface((2, 2), 0, 8)
color = pygame.Color(1, 2, 3, 255)
surf.set_palette_at(0, color)
color2 = surf.get_palette_at(0)
self.assertIsInstance(color2, pygame.Color)
self.assertEqual(color2, color)
self.assertRaises(IndexError, surf.get_palette_at, -1)
self.assertRaises(IndexError, surf.get_palette_at, 256)
def test_get_pitch(self):
# Test get_pitch() on several surfaces of varying size/depth
sizes = ((2, 2), (7, 33), (33, 7), (2, 734), (734, 2), (734, 734))
depths = [8, 24, 32]
for width, height in sizes:
for depth in depths:
# Test get_pitch() on parent surface
surf = pygame.Surface((width, height), depth=depth)
buff = surf.get_buffer()
pitch = buff.length / surf.get_height()
test_pitch = surf.get_pitch()
self.assertEqual(pitch, test_pitch)
# Test get_pitch() on subsurface with same rect as parent
rect1 = surf.get_rect()
subsurf1 = surf.subsurface(rect1)
sub_buff1 = subsurf1.get_buffer()
sub_pitch1 = sub_buff1.length / subsurf1.get_height()
test_sub_pitch1 = subsurf1.get_pitch()
self.assertEqual(sub_pitch1, test_sub_pitch1)
# Test get_pitch on subsurface with modified rect
rect2 = rect1.inflate(-width / 2, -height / 2)
subsurf2 = surf.subsurface(rect2)
sub_buff2 = subsurf2.get_buffer()
sub_pitch2 = sub_buff2.length / float(subsurf2.get_height())
test_sub_pitch2 = subsurf2.get_pitch()
self.assertEqual(sub_pitch2, test_sub_pitch2)
def test_get_shifts(self):
"""
Tests whether Surface.get_shifts returns proper
RGBA shifts under various conditions.
"""
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.get_shifts:
# Surface.get_shifts(): return (R, G, B, A)
# the bit shifts needed to convert between color and mapped integer.
# Returns the pixel shifts need to convert between each color and a
# mapped integer.
# This value is not needed for normal Pygame usage.
# Test for SDL2 on surfaces with various depths and alpha on/off
depths = [8, 24, 32]
alpha = 128
off = None
for bit_depth in depths:
surface = pygame.Surface((32, 32), depth=bit_depth)
surface.set_alpha(alpha)
r1, g1, b1, a1 = surface.get_shifts()
surface.set_alpha(off)
r2, g2, b2, a2 = surface.get_shifts()
self.assertEqual((r1, g1, b1, a1), (r2, g2, b2, a2))
def test_get_size(self):
sizes = ((1, 1), (119, 10), (1000, 1000), (1, 5000), (1221, 1), (99, 999))
for width, height in sizes:
surf = pygame.Surface((width, height))
found_size = surf.get_size()
self.assertEqual((width, height), found_size)
def test_lock(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.lock:
# Surface.lock(): return None
# lock the Surface memory for pixel access
#
# Lock the pixel data of a Surface for access. On accelerated
# Surfaces, the pixel data may be stored in volatile video memory or
# nonlinear compressed forms. When a Surface is locked the pixel
# memory becomes available to access by regular software. Code that
# reads or writes pixel values will need the Surface to be locked.
#
# Surfaces should not remain locked for more than necessary. A locked
# Surface can often not be displayed or managed by Pygame.
#
# Not all Surfaces require locking. The Surface.mustlock() method can
# determine if it is actually required. There is no performance
# penalty for locking and unlocking a Surface that does not need it.
#
# All pygame functions will automatically lock and unlock the Surface
# data as needed. If a section of code is going to make calls that
# will repeatedly lock and unlock the Surface many times, it can be
# helpful to wrap the block inside a lock and unlock pair.
#
# It is safe to nest locking and unlocking calls. The surface will
# only be unlocked after the final lock is released.
#
# Basic
surf = pygame.Surface((100, 100))
surf.lock()
self.assertTrue(surf.get_locked())
# Nested
surf = pygame.Surface((100, 100))
surf.lock()
surf.lock()
surf.unlock()
self.assertTrue(surf.get_locked())
surf.unlock()
surf.lock()
surf.lock()
self.assertTrue(surf.get_locked())
surf.unlock()
self.assertTrue(surf.get_locked())
surf.unlock()
self.assertFalse(surf.get_locked())
# Already Locked
surf = pygame.Surface((100, 100))
surf.lock()
surf.lock()
self.assertTrue(surf.get_locked())
surf.unlock()
self.assertTrue(surf.get_locked())
surf.unlock()
self.assertFalse(surf.get_locked())
def test_map_rgb(self):
color = Color(0, 128, 255, 64)
surf = pygame.Surface((5, 5), SRCALPHA, 32)
c = surf.map_rgb(color)
self.assertEqual(surf.unmap_rgb(c), color)
self.assertEqual(surf.get_at((0, 0)), (0, 0, 0, 0))
surf.fill(c)
self.assertEqual(surf.get_at((0, 0)), color)
surf.fill((0, 0, 0, 0))
self.assertEqual(surf.get_at((0, 0)), (0, 0, 0, 0))
surf.set_at((0, 0), c)
self.assertEqual(surf.get_at((0, 0)), color)
def test_mustlock(self):
# Test that subsurfaces mustlock
surf = pygame.Surface((1024, 1024))
subsurf = surf.subsurface((0, 0, 1024, 1024))
self.assertTrue(subsurf.mustlock())
self.assertFalse(surf.mustlock())
# Tests nested subsurfaces
rects = ((0, 0, 512, 512), (0, 0, 256, 256), (0, 0, 128, 128))
surf_stack = []
surf_stack.append(surf)
surf_stack.append(subsurf)
for rect in rects:
surf_stack.append(surf_stack[-1].subsurface(rect))
self.assertTrue(surf_stack[-1].mustlock())
self.assertTrue(surf_stack[-2].mustlock())
def test_set_alpha_none(self):
"""surf.set_alpha(None) disables blending"""
s = pygame.Surface((1, 1), SRCALPHA, 32)
s.fill((0, 255, 0, 128))
s.set_alpha(None)
self.assertEqual(None, s.get_alpha())
s2 = pygame.Surface((1, 1), SRCALPHA, 32)
s2.fill((255, 0, 0, 255))
s2.blit(s, (0, 0))
self.assertEqual(s2.get_at((0, 0))[0], 0, "the red component should be 0")
def test_set_alpha_value(self):
"""surf.set_alpha(x), where x != None, enables blending"""
s = pygame.Surface((1, 1), SRCALPHA, 32)
s.fill((0, 255, 0, 128))
s.set_alpha(255)
s2 = pygame.Surface((1, 1), SRCALPHA, 32)
s2.fill((255, 0, 0, 255))
s2.blit(s, (0, 0))
self.assertGreater(
s2.get_at((0, 0))[0], 0, "the red component should be above 0"
)
def test_palette_colorkey(self):
"""test bug discovered by robertpfeiffer
https://github.com/pygame/pygame/issues/721
"""
surf = pygame.image.load(example_path(os.path.join("data", "alien2.png")))
key = surf.get_colorkey()
self.assertEqual(surf.get_palette()[surf.map_rgb(key)], key)
def test_palette_colorkey_set_px(self):
surf = pygame.image.load(example_path(os.path.join("data", "alien2.png")))
key = surf.get_colorkey()
surf.set_at((0, 0), key)
self.assertEqual(surf.get_at((0, 0)), key)
def test_palette_colorkey_fill(self):
surf = pygame.image.load(example_path(os.path.join("data", "alien2.png")))
key = surf.get_colorkey()
surf.fill(key)
self.assertEqual(surf.get_at((0, 0)), key)
def test_set_palette(self):
palette = [pygame.Color(i, i, i) for i in range(256)]
palette[10] = tuple(palette[10]) # 4 element tuple
palette[11] = tuple(palette[11])[0:3] # 3 element tuple
surf = pygame.Surface((2, 2), 0, 8)
surf.set_palette(palette)
for i in range(256):
self.assertEqual(surf.map_rgb(palette[i]), i, "palette color %i" % (i,))
c = palette[i]
surf.fill(c)
self.assertEqual(surf.get_at((0, 0)), c, "palette color %i" % (i,))
for i in range(10):
palette[i] = pygame.Color(255 - i, 0, 0)
surf.set_palette(palette[0:10])
for i in range(256):
self.assertEqual(surf.map_rgb(palette[i]), i, "palette color %i" % (i,))
c = palette[i]
surf.fill(c)
self.assertEqual(surf.get_at((0, 0)), c, "palette color %i" % (i,))
self.assertRaises(ValueError, surf.set_palette, [Color(1, 2, 3, 254)])
self.assertRaises(ValueError, surf.set_palette, (1, 2, 3, 254))
def test_set_palette__fail(self):
palette = 256 * [(10, 20, 30)]
surf = pygame.Surface((2, 2), 0, 32)
self.assertRaises(pygame.error, surf.set_palette, palette)
def test_set_palette__set_at(self):
surf = pygame.Surface((2, 2), depth=8)
palette = 256 * [(10, 20, 30)]
palette[1] = (50, 40, 30)
surf.set_palette(palette)
# calling set_at on a palettized surface should set the pixel to
# the closest color in the palette.
surf.set_at((0, 0), (60, 50, 40))
self.assertEqual(surf.get_at((0, 0)), (50, 40, 30, 255))
self.assertEqual(surf.get_at((1, 0)), (10, 20, 30, 255))
def test_set_palette_at(self):
surf = pygame.Surface((2, 2), 0, 8)
original = surf.get_palette_at(10)
replacement = Color(1, 1, 1, 255)
if replacement == original:
replacement = Color(2, 2, 2, 255)
surf.set_palette_at(10, replacement)
self.assertEqual(surf.get_palette_at(10), replacement)
next = tuple(original)
surf.set_palette_at(10, next)
self.assertEqual(surf.get_palette_at(10), next)
next = tuple(original)[0:3]
surf.set_palette_at(10, next)
self.assertEqual(surf.get_palette_at(10), next)
self.assertRaises(IndexError, surf.set_palette_at, 256, replacement)
self.assertRaises(IndexError, surf.set_palette_at, -1, replacement)
def test_subsurface(self):
# __doc__ (as of 2008-08-02) for pygame.surface.Surface.subsurface:
# Surface.subsurface(Rect): return Surface
# create a new surface that references its parent
#
# Returns a new Surface that shares its pixels with its new parent.
# The new Surface is considered a child of the original. Modifications
# to either Surface pixels will effect each other. Surface information
# like clipping area and color keys are unique to each Surface.
#
# The new Surface will inherit the palette, color key, and alpha
# settings from its parent.
#
# It is possible to have any number of subsurfaces and subsubsurfaces
# on the parent. It is also possible to subsurface the display Surface
# if the display mode is not hardware accelerated.
#
# See the Surface.get_offset(), Surface.get_parent() to learn more
# about the state of a subsurface.
#
surf = pygame.Surface((16, 16))
s = surf.subsurface(0, 0, 1, 1)
s = surf.subsurface((0, 0, 1, 1))
# s = surf.subsurface((0,0,1,1), 1)
# This form is not acceptable.
# s = surf.subsurface(0,0,10,10, 1)
self.assertRaises(ValueError, surf.subsurface, (0, 0, 1, 1, 666))
self.assertEqual(s.get_shifts(), surf.get_shifts())
self.assertEqual(s.get_masks(), surf.get_masks())
self.assertEqual(s.get_losses(), surf.get_losses())
# Issue https://github.com/pygame/pygame/issues/2
surf = pygame.Surface.__new__(pygame.Surface)
self.assertRaises(pygame.error, surf.subsurface, (0, 0, 0, 0))
def test_unlock(self):
# Basic
surf = pygame.Surface((100, 100))
surf.lock()
surf.unlock()
self.assertFalse(surf.get_locked())
# Nested
surf = pygame.Surface((100, 100))
surf.lock()
surf.lock()
surf.unlock()
self.assertTrue(surf.get_locked())
surf.unlock()
self.assertFalse(surf.get_locked())
# Already Unlocked
surf = pygame.Surface((100, 100))
surf.unlock()
self.assertFalse(surf.get_locked())
surf.unlock()
self.assertFalse(surf.get_locked())
# Surface can be relocked
surf = pygame.Surface((100, 100))
surf.lock()
surf.unlock()
self.assertFalse(surf.get_locked())
surf.lock()
surf.unlock()
self.assertFalse(surf.get_locked())
def test_unmap_rgb(self):
# Special case, 8 bit-per-pixel surface (has a palette).
surf = pygame.Surface((2, 2), 0, 8)
c = (1, 1, 1) # Unlikely to be in a default palette.
i = 67
surf.set_palette_at(i, c)
unmapped_c = surf.unmap_rgb(i)
self.assertEqual(unmapped_c, c)
# Confirm it is a Color instance
self.assertIsInstance(unmapped_c, pygame.Color)
# Remaining, non-pallete, cases.
c = (128, 64, 12, 255)
formats = [(0, 16), (0, 24), (0, 32), (SRCALPHA, 16), (SRCALPHA, 32)]
for flags, bitsize in formats:
surf = pygame.Surface((2, 2), flags, bitsize)
unmapped_c = surf.unmap_rgb(surf.map_rgb(c))
surf.fill(c)
comparison_c = surf.get_at((0, 0))
self.assertEqual(
unmapped_c,
comparison_c,
"%s != %s, flags: %i, bitsize: %i"
% (unmapped_c, comparison_c, flags, bitsize),
)
# Confirm it is a Color instance
self.assertIsInstance(unmapped_c, pygame.Color)
def test_scroll(self):
scrolls = [
(8, 2, 3),
(16, 2, 3),
(24, 2, 3),
(32, 2, 3),
(32, -1, -3),
(32, 0, 0),
(32, 11, 0),
(32, 0, 11),
(32, -11, 0),
(32, 0, -11),
(32, -11, 2),
(32, 2, -11),
]
for bitsize, dx, dy in scrolls:
surf = pygame.Surface((10, 10), 0, bitsize)
surf.fill((255, 0, 0))
surf.fill((0, 255, 0), (2, 2, 2, 2))
comp = surf.copy()
comp.blit(surf, (dx, dy))
surf.scroll(dx, dy)
w, h = surf.get_size()
for x in range(w):
for y in range(h):
with self.subTest(x=x, y=y):
self.assertEqual(
surf.get_at((x, y)),
comp.get_at((x, y)),
"%s != %s, bpp:, %i, x: %i, y: %i"
% (
surf.get_at((x, y)),
comp.get_at((x, y)),
bitsize,
dx,
dy,
),
)
# Confirm clip rect containment
surf = pygame.Surface((20, 13), 0, 32)
surf.fill((255, 0, 0))
surf.fill((0, 255, 0), (7, 1, 6, 6))
comp = surf.copy()
clip = Rect(3, 1, 8, 14)
surf.set_clip(clip)
comp.set_clip(clip)
comp.blit(surf, (clip.x + 2, clip.y + 3), surf.get_clip())
surf.scroll(2, 3)
w, h = surf.get_size()
for x in range(w):
for y in range(h):
self.assertEqual(surf.get_at((x, y)), comp.get_at((x, y)))
# Confirm keyword arguments and per-pixel alpha
spot_color = (0, 255, 0, 128)
surf = pygame.Surface((4, 4), pygame.SRCALPHA, 32)
surf.fill((255, 0, 0, 255))
surf.set_at((1, 1), spot_color)
surf.scroll(dx=1)
self.assertEqual(surf.get_at((2, 1)), spot_color)
surf.scroll(dy=1)
self.assertEqual(surf.get_at((2, 2)), spot_color)
surf.scroll(dy=1, dx=1)
self.assertEqual(surf.get_at((3, 3)), spot_color)
surf.scroll(dx=-3, dy=-3)
self.assertEqual(surf.get_at((0, 0)), spot_color)
class SurfaceSubtypeTest(unittest.TestCase):
"""Issue #280: Methods that return a new Surface preserve subclasses"""
def setUp(self):
pygame.display.init()
def tearDown(self):
pygame.display.quit()
def test_copy(self):
"""Ensure method copy() preserves the surface's class
When Surface is subclassed, the inherited copy() method will return
instances of the subclass. Non Surface fields are uncopied, however.
This includes instance attributes.
"""
expected_size = (32, 32)
ms1 = SurfaceSubclass(expected_size, SRCALPHA, 32)
ms2 = ms1.copy()
self.assertIsNot(ms1, ms2)
self.assertIsInstance(ms1, pygame.Surface)
self.assertIsInstance(ms2, pygame.Surface)
self.assertIsInstance(ms1, SurfaceSubclass)
self.assertIsInstance(ms2, SurfaceSubclass)
self.assertTrue(ms1.test_attribute)
self.assertRaises(AttributeError, getattr, ms2, "test_attribute")
self.assertEqual(ms2.get_size(), expected_size)
def test_convert(self):
"""Ensure method convert() preserves the surface's class
When Surface is subclassed, the inherited convert() method will return
instances of the subclass. Non Surface fields are omitted, however.
This includes instance attributes.
"""
expected_size = (32, 32)
ms1 = SurfaceSubclass(expected_size, 0, 24)
ms2 = ms1.convert(24)
self.assertIsNot(ms1, ms2)
self.assertIsInstance(ms1, pygame.Surface)
self.assertIsInstance(ms2, pygame.Surface)
self.assertIsInstance(ms1, SurfaceSubclass)
self.assertIsInstance(ms2, SurfaceSubclass)
self.assertTrue(ms1.test_attribute)
self.assertRaises(AttributeError, getattr, ms2, "test_attribute")
self.assertEqual(ms2.get_size(), expected_size)
def test_convert_alpha(self):
"""Ensure method convert_alpha() preserves the surface's class
When Surface is subclassed, the inherited convert_alpha() method will
return instances of the subclass. Non Surface fields are omitted,
however. This includes instance attributes.
"""
pygame.display.set_mode((40, 40))
expected_size = (32, 32)
s = pygame.Surface(expected_size, SRCALPHA, 16)
ms1 = SurfaceSubclass(expected_size, SRCALPHA, 32)
ms2 = ms1.convert_alpha(s)
self.assertIsNot(ms1, ms2)
self.assertIsInstance(ms1, pygame.Surface)
self.assertIsInstance(ms2, pygame.Surface)
self.assertIsInstance(ms1, SurfaceSubclass)
self.assertIsInstance(ms2, SurfaceSubclass)
self.assertTrue(ms1.test_attribute)
self.assertRaises(AttributeError, getattr, ms2, "test_attribute")
self.assertEqual(ms2.get_size(), expected_size)
def test_subsurface(self):
"""Ensure method subsurface() preserves the surface's class
When Surface is subclassed, the inherited subsurface() method will
return instances of the subclass. Non Surface fields are uncopied,
however. This includes instance attributes.
"""
expected_size = (10, 12)
ms1 = SurfaceSubclass((32, 32), SRCALPHA, 32)
ms2 = ms1.subsurface((4, 5), expected_size)
self.assertIsNot(ms1, ms2)
self.assertIsInstance(ms1, pygame.Surface)
self.assertIsInstance(ms2, pygame.Surface)
self.assertIsInstance(ms1, SurfaceSubclass)
self.assertIsInstance(ms2, SurfaceSubclass)
self.assertTrue(ms1.test_attribute)
self.assertRaises(AttributeError, getattr, ms2, "test_attribute")
self.assertEqual(ms2.get_size(), expected_size)
class SurfaceGetBufferTest(unittest.TestCase):
# These tests requires ctypes. They are disabled if ctypes
# is not installed.
try:
ArrayInterface
except NameError:
__tags__ = ("ignore", "subprocess_ignore")
lilendian = pygame.get_sdl_byteorder() == pygame.LIL_ENDIAN
def _check_interface_2D(self, s):
s_w, s_h = s.get_size()
s_bytesize = s.get_bytesize()
s_pitch = s.get_pitch()
s_pixels = s._pixels_address
# check the array interface structure fields.
v = s.get_view("2")
if not IS_PYPY:
flags = PAI_ALIGNED | PAI_NOTSWAPPED | PAI_WRITEABLE
if s.get_pitch() == s_w * s_bytesize:
flags |= PAI_FORTRAN
inter = ArrayInterface(v)
self.assertEqual(inter.two, 2)
self.assertEqual(inter.nd, 2)
self.assertEqual(inter.typekind, "u")
self.assertEqual(inter.itemsize, s_bytesize)
self.assertEqual(inter.shape[0], s_w)
self.assertEqual(inter.shape[1], s_h)
self.assertEqual(inter.strides[0], s_bytesize)
self.assertEqual(inter.strides[1], s_pitch)
self.assertEqual(inter.flags, flags)
self.assertEqual(inter.data, s_pixels)
def _check_interface_3D(self, s):
s_w, s_h = s.get_size()
s_bytesize = s.get_bytesize()
s_pitch = s.get_pitch()
s_pixels = s._pixels_address
s_shifts = list(s.get_shifts())
# Check for RGB or BGR surface.
if s_shifts[0:3] == [0, 8, 16]:
if self.lilendian:
# RGB
offset = 0
step = 1
else:
# BGR
offset = s_bytesize - 1
step = -1
elif s_shifts[0:3] == [8, 16, 24]:
if self.lilendian:
# xRGB
offset = 1
step = 1
else:
# BGRx
offset = s_bytesize - 2
step = -1
elif s_shifts[0:3] == [16, 8, 0]:
if self.lilendian:
# BGR
offset = 2
step = -1
else:
# RGB
offset = s_bytesize - 3
step = 1
elif s_shifts[0:3] == [24, 16, 8]:
if self.lilendian:
# BGRx
offset = 2
step = -1
else:
# RGBx
offset = s_bytesize - 4
step = -1
else:
return
# check the array interface structure fields.
v = s.get_view("3")
if not IS_PYPY:
inter = ArrayInterface(v)
flags = PAI_ALIGNED | PAI_NOTSWAPPED | PAI_WRITEABLE
self.assertEqual(inter.two, 2)
self.assertEqual(inter.nd, 3)
self.assertEqual(inter.typekind, "u")
self.assertEqual(inter.itemsize, 1)
self.assertEqual(inter.shape[0], s_w)
self.assertEqual(inter.shape[1], s_h)
self.assertEqual(inter.shape[2], 3)
self.assertEqual(inter.strides[0], s_bytesize)
self.assertEqual(inter.strides[1], s_pitch)
self.assertEqual(inter.strides[2], step)
self.assertEqual(inter.flags, flags)
self.assertEqual(inter.data, s_pixels + offset)
def _check_interface_rgba(self, s, plane):
s_w, s_h = s.get_size()
s_bytesize = s.get_bytesize()
s_pitch = s.get_pitch()
s_pixels = s._pixels_address
s_shifts = s.get_shifts()
s_masks = s.get_masks()
# Find the color plane position within the pixel.
if not s_masks[plane]:
return
alpha_shift = s_shifts[plane]
offset = alpha_shift // 8
if not self.lilendian:
offset = s_bytesize - offset - 1
# check the array interface structure fields.
v = s.get_view("rgba"[plane])
if not IS_PYPY:
inter = ArrayInterface(v)
flags = PAI_ALIGNED | PAI_NOTSWAPPED | PAI_WRITEABLE
self.assertEqual(inter.two, 2)
self.assertEqual(inter.nd, 2)
self.assertEqual(inter.typekind, "u")
self.assertEqual(inter.itemsize, 1)
self.assertEqual(inter.shape[0], s_w)
self.assertEqual(inter.shape[1], s_h)
self.assertEqual(inter.strides[0], s_bytesize)
self.assertEqual(inter.strides[1], s_pitch)
self.assertEqual(inter.flags, flags)
self.assertEqual(inter.data, s_pixels + offset)
def test_array_interface(self):
self._check_interface_2D(pygame.Surface((5, 7), 0, 8))
self._check_interface_2D(pygame.Surface((5, 7), 0, 16))
self._check_interface_2D(pygame.Surface((5, 7), pygame.SRCALPHA, 16))
self._check_interface_3D(pygame.Surface((5, 7), 0, 24))
self._check_interface_3D(pygame.Surface((8, 4), 0, 24)) # No gaps
self._check_interface_2D(pygame.Surface((5, 7), 0, 32))
self._check_interface_3D(pygame.Surface((5, 7), 0, 32))
self._check_interface_2D(pygame.Surface((5, 7), pygame.SRCALPHA, 32))
self._check_interface_3D(pygame.Surface((5, 7), pygame.SRCALPHA, 32))
def test_array_interface_masks(self):
"""Test non-default color byte orders on 3D views"""
sz = (5, 7)
# Reversed RGB byte order
s = pygame.Surface(sz, 0, 32)
s_masks = list(s.get_masks())
masks = [0xFF, 0xFF00, 0xFF0000]
if s_masks[0:3] == masks or s_masks[0:3] == masks[::-1]:
masks = s_masks[2::-1] + s_masks[3:4]
self._check_interface_3D(pygame.Surface(sz, 0, 32, masks))
s = pygame.Surface(sz, 0, 24)
s_masks = list(s.get_masks())
masks = [0xFF, 0xFF00, 0xFF0000]
if s_masks[0:3] == masks or s_masks[0:3] == masks[::-1]:
masks = s_masks[2::-1] + s_masks[3:4]
self._check_interface_3D(pygame.Surface(sz, 0, 24, masks))
masks = [0xFF00, 0xFF0000, 0xFF000000, 0]
self._check_interface_3D(pygame.Surface(sz, 0, 32, masks))
def test_array_interface_alpha(self):
for shifts in [[0, 8, 16, 24], [8, 16, 24, 0], [24, 16, 8, 0], [16, 8, 0, 24]]:
masks = [0xFF << s for s in shifts]
s = pygame.Surface((4, 2), pygame.SRCALPHA, 32, masks)
self._check_interface_rgba(s, 3)
def test_array_interface_rgb(self):
for shifts in [[0, 8, 16, 24], [8, 16, 24, 0], [24, 16, 8, 0], [16, 8, 0, 24]]:
masks = [0xFF << s for s in shifts]
masks[3] = 0
for plane in range(3):
s = pygame.Surface((4, 2), 0, 24)
self._check_interface_rgba(s, plane)
s = pygame.Surface((4, 2), 0, 32)
self._check_interface_rgba(s, plane)
@unittest.skipIf(not pygame.HAVE_NEWBUF, "newbuf not implemented")
def test_newbuf_PyBUF_flags_bytes(self):
from pygame.tests.test_utils import buftools
Importer = buftools.Importer
s = pygame.Surface((10, 6), 0, 32)
a = s.get_buffer()
b = Importer(a, buftools.PyBUF_SIMPLE)
self.assertEqual(b.ndim, 0)
self.assertTrue(b.format is None)
self.assertEqual(b.len, a.length)
self.assertEqual(b.itemsize, 1)
self.assertTrue(b.shape is None)
self.assertTrue(b.strides is None)
self.assertTrue(b.suboffsets is None)
self.assertFalse(b.readonly)
self.assertEqual(b.buf, s._pixels_address)
b = Importer(a, buftools.PyBUF_WRITABLE)
self.assertEqual(b.ndim, 0)
self.assertTrue(b.format is None)
self.assertFalse(b.readonly)
b = Importer(a, buftools.PyBUF_FORMAT)
self.assertEqual(b.ndim, 0)
self.assertEqual(b.format, "B")
b = Importer(a, buftools.PyBUF_ND)
self.assertEqual(b.ndim, 1)
self.assertTrue(b.format is None)
self.assertEqual(b.len, a.length)
self.assertEqual(b.itemsize, 1)
self.assertEqual(b.shape, (a.length,))
self.assertTrue(b.strides is None)
self.assertTrue(b.suboffsets is None)
self.assertFalse(b.readonly)
self.assertEqual(b.buf, s._pixels_address)
b = Importer(a, buftools.PyBUF_STRIDES)
self.assertEqual(b.ndim, 1)
self.assertTrue(b.format is None)
self.assertEqual(b.strides, (1,))
s2 = s.subsurface((1, 1, 7, 4)) # Not contiguous
a = s2.get_buffer()
b = Importer(a, buftools.PyBUF_SIMPLE)
self.assertEqual(b.ndim, 0)
self.assertTrue(b.format is None)
self.assertEqual(b.len, a.length)
self.assertEqual(b.itemsize, 1)
self.assertTrue(b.shape is None)
self.assertTrue(b.strides is None)
self.assertTrue(b.suboffsets is None)
self.assertFalse(b.readonly)
self.assertEqual(b.buf, s2._pixels_address)
b = Importer(a, buftools.PyBUF_C_CONTIGUOUS)
self.assertEqual(b.ndim, 1)
self.assertEqual(b.strides, (1,))
b = Importer(a, buftools.PyBUF_F_CONTIGUOUS)
self.assertEqual(b.ndim, 1)
self.assertEqual(b.strides, (1,))
b = Importer(a, buftools.PyBUF_ANY_CONTIGUOUS)
self.assertEqual(b.ndim, 1)
self.assertEqual(b.strides, (1,))
@unittest.skipIf(not pygame.HAVE_NEWBUF, "newbuf not implemented")
def test_newbuf_PyBUF_flags_0D(self):
# This is the same handler as used by get_buffer(), so just
# confirm that it succeeds for one case.
from pygame.tests.test_utils import buftools
Importer = buftools.Importer
s = pygame.Surface((10, 6), 0, 32)
a = s.get_view("0")
b = Importer(a, buftools.PyBUF_SIMPLE)
self.assertEqual(b.ndim, 0)
self.assertTrue(b.format is None)
self.assertEqual(b.len, a.length)
self.assertEqual(b.itemsize, 1)
self.assertTrue(b.shape is None)
self.assertTrue(b.strides is None)
self.assertTrue(b.suboffsets is None)
self.assertFalse(b.readonly)
self.assertEqual(b.buf, s._pixels_address)
@unittest.skipIf(not pygame.HAVE_NEWBUF, "newbuf not implemented")
def test_newbuf_PyBUF_flags_1D(self):
from pygame.tests.test_utils import buftools
Importer = buftools.Importer
s = pygame.Surface((10, 6), 0, 32)
a = s.get_view("1")
b = Importer(a, buftools.PyBUF_SIMPLE)
self.assertEqual(b.ndim, 0)
self.assertTrue(b.format is None)
self.assertEqual(b.len, a.length)
self.assertEqual(b.itemsize, s.get_bytesize())
self.assertTrue(b.shape is None)
self.assertTrue(b.strides is None)
self.assertTrue(b.suboffsets is None)
self.assertFalse(b.readonly)
self.assertEqual(b.buf, s._pixels_address)
b = Importer(a, buftools.PyBUF_WRITABLE)
self.assertEqual(b.ndim, 0)
self.assertTrue(b.format is None)
self.assertFalse(b.readonly)
b = Importer(a, buftools.PyBUF_FORMAT)
self.assertEqual(b.ndim, 0)
self.assertEqual(b.format, "=I")
b = Importer(a, buftools.PyBUF_ND)
self.assertEqual(b.ndim, 1)
self.assertTrue(b.format is None)
self.assertEqual(b.len, a.length)
self.assertEqual(b.itemsize, s.get_bytesize())
self.assertEqual(b.shape, (s.get_width() * s.get_height(),))
self.assertTrue(b.strides is None)
self.assertTrue(b.suboffsets is None)
self.assertFalse(b.readonly)
self.assertEqual(b.buf, s._pixels_address)
b = Importer(a, buftools.PyBUF_STRIDES)
self.assertEqual(b.ndim, 1)
self.assertTrue(b.format is None)
self.assertEqual(b.strides, (s.get_bytesize(),))
@unittest.skipIf(not pygame.HAVE_NEWBUF, "newbuf not implemented")
def test_newbuf_PyBUF_flags_2D(self):
from pygame.tests.test_utils import buftools
Importer = buftools.Importer
s = pygame.Surface((10, 6), 0, 32)
a = s.get_view("2")
# Non dimensional requests, no PyDEF_ND, are handled by the
# 1D surface buffer code, so only need to confirm a success.
b = Importer(a, buftools.PyBUF_SIMPLE)
self.assertEqual(b.ndim, 0)
self.assertTrue(b.format is None)
self.assertEqual(b.len, a.length)
self.assertEqual(b.itemsize, s.get_bytesize())
self.assertTrue(b.shape is None)
self.assertTrue(b.strides is None)
self.assertTrue(b.suboffsets is None)
self.assertFalse(b.readonly)
self.assertEqual(b.buf, s._pixels_address)
# Uniquely 2D
b = Importer(a, buftools.PyBUF_STRIDES)
self.assertEqual(b.ndim, 2)
self.assertTrue(b.format is None)
self.assertEqual(b.len, a.length)
self.assertEqual(b.itemsize, s.get_bytesize())
self.assertEqual(b.shape, s.get_size())
self.assertEqual(b.strides, (s.get_bytesize(), s.get_pitch()))
self.assertTrue(b.suboffsets is None)
self.assertFalse(b.readonly)
self.assertEqual(b.buf, s._pixels_address)
b = Importer(a, buftools.PyBUF_RECORDS_RO)
self.assertEqual(b.ndim, 2)
self.assertEqual(b.format, "=I")
self.assertEqual(b.strides, (s.get_bytesize(), s.get_pitch()))
b = Importer(a, buftools.PyBUF_RECORDS)
self.assertEqual(b.ndim, 2)
self.assertEqual(b.format, "=I")
self.assertEqual(b.strides, (s.get_bytesize(), s.get_pitch()))
b = Importer(a, buftools.PyBUF_F_CONTIGUOUS)
self.assertEqual(b.ndim, 2)
self.assertEqual(b.format, None)
self.assertEqual(b.strides, (s.get_bytesize(), s.get_pitch()))
b = Importer(a, buftools.PyBUF_ANY_CONTIGUOUS)
self.assertEqual(b.ndim, 2)
self.assertEqual(b.format, None)
self.assertEqual(b.strides, (s.get_bytesize(), s.get_pitch()))
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_ND)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_C_CONTIGUOUS)
s2 = s.subsurface((1, 1, 7, 4)) # Not contiguous
a = s2.get_view("2")
b = Importer(a, buftools.PyBUF_STRIDES)
self.assertEqual(b.ndim, 2)
self.assertTrue(b.format is None)
self.assertEqual(b.len, a.length)
self.assertEqual(b.itemsize, s2.get_bytesize())
self.assertEqual(b.shape, s2.get_size())
self.assertEqual(b.strides, (s2.get_bytesize(), s.get_pitch()))
self.assertTrue(b.suboffsets is None)
self.assertFalse(b.readonly)
self.assertEqual(b.buf, s2._pixels_address)
b = Importer(a, buftools.PyBUF_RECORDS)
self.assertEqual(b.ndim, 2)
self.assertEqual(b.format, "=I")
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_SIMPLE)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_FORMAT)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_WRITABLE)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_ND)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_C_CONTIGUOUS)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_F_CONTIGUOUS)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_ANY_CONTIGUOUS)
@unittest.skipIf(not pygame.HAVE_NEWBUF, "newbuf not implemented")
def test_newbuf_PyBUF_flags_3D(self):
from pygame.tests.test_utils import buftools
Importer = buftools.Importer
s = pygame.Surface((12, 6), 0, 24)
rmask, gmask, bmask, amask = s.get_masks()
if self.lilendian:
if rmask == 0x0000FF:
color_step = 1
addr_offset = 0
else:
color_step = -1
addr_offset = 2
else:
if rmask == 0xFF0000:
color_step = 1
addr_offset = 0
else:
color_step = -1
addr_offset = 2
a = s.get_view("3")
b = Importer(a, buftools.PyBUF_STRIDES)
w, h = s.get_size()
shape = w, h, 3
strides = 3, s.get_pitch(), color_step
self.assertEqual(b.ndim, 3)
self.assertTrue(b.format is None)
self.assertEqual(b.len, a.length)
self.assertEqual(b.itemsize, 1)
self.assertEqual(b.shape, shape)
self.assertEqual(b.strides, strides)
self.assertTrue(b.suboffsets is None)
self.assertFalse(b.readonly)
self.assertEqual(b.buf, s._pixels_address + addr_offset)
b = Importer(a, buftools.PyBUF_RECORDS_RO)
self.assertEqual(b.ndim, 3)
self.assertEqual(b.format, "B")
self.assertEqual(b.strides, strides)
b = Importer(a, buftools.PyBUF_RECORDS)
self.assertEqual(b.ndim, 3)
self.assertEqual(b.format, "B")
self.assertEqual(b.strides, strides)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_SIMPLE)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_FORMAT)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_WRITABLE)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_ND)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_C_CONTIGUOUS)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_F_CONTIGUOUS)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_ANY_CONTIGUOUS)
@unittest.skipIf(not pygame.HAVE_NEWBUF, "newbuf not implemented")
def test_newbuf_PyBUF_flags_rgba(self):
# All color plane views are handled by the same routine,
# so only one plane need be checked.
from pygame.tests.test_utils import buftools
Importer = buftools.Importer
s = pygame.Surface((12, 6), 0, 24)
rmask, gmask, bmask, amask = s.get_masks()
if self.lilendian:
if rmask == 0x0000FF:
addr_offset = 0
else:
addr_offset = 2
else:
if rmask == 0xFF0000:
addr_offset = 0
else:
addr_offset = 2
a = s.get_view("R")
b = Importer(a, buftools.PyBUF_STRIDES)
w, h = s.get_size()
shape = w, h
strides = s.get_bytesize(), s.get_pitch()
self.assertEqual(b.ndim, 2)
self.assertTrue(b.format is None)
self.assertEqual(b.len, a.length)
self.assertEqual(b.itemsize, 1)
self.assertEqual(b.shape, shape)
self.assertEqual(b.strides, strides)
self.assertTrue(b.suboffsets is None)
self.assertFalse(b.readonly)
self.assertEqual(b.buf, s._pixels_address + addr_offset)
b = Importer(a, buftools.PyBUF_RECORDS_RO)
self.assertEqual(b.ndim, 2)
self.assertEqual(b.format, "B")
self.assertEqual(b.strides, strides)
b = Importer(a, buftools.PyBUF_RECORDS)
self.assertEqual(b.ndim, 2)
self.assertEqual(b.format, "B")
self.assertEqual(b.strides, strides)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_SIMPLE)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_FORMAT)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_WRITABLE)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_ND)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_C_CONTIGUOUS)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_F_CONTIGUOUS)
self.assertRaises(BufferError, Importer, a, buftools.PyBUF_ANY_CONTIGUOUS)
class SurfaceBlendTest(unittest.TestCase):
def setUp(self):
# Needed for 8 bits-per-pixel color palette surface tests.
pygame.display.init()
def tearDown(self):
pygame.display.quit()
_test_palette = [
(0, 0, 0, 255),
(10, 30, 60, 0),
(25, 75, 100, 128),
(200, 150, 100, 200),
(0, 100, 200, 255),
]
surf_size = (10, 12)
_test_points = [
((0, 0), 1),
((4, 5), 1),
((9, 0), 2),
((5, 5), 2),
((0, 11), 3),
((4, 6), 3),
((9, 11), 4),
((5, 6), 4),
]
def _make_surface(self, bitsize, srcalpha=False, palette=None):
if palette is None:
palette = self._test_palette
flags = 0
if srcalpha:
flags |= SRCALPHA
surf = pygame.Surface(self.surf_size, flags, bitsize)
if bitsize == 8:
surf.set_palette([c[:3] for c in palette])
return surf
def _fill_surface(self, surf, palette=None):
if palette is None:
palette = self._test_palette
surf.fill(palette[1], (0, 0, 5, 6))
surf.fill(palette[2], (5, 0, 5, 6))
surf.fill(palette[3], (0, 6, 5, 6))
surf.fill(palette[4], (5, 6, 5, 6))
def _make_src_surface(self, bitsize, srcalpha=False, palette=None):
surf = self._make_surface(bitsize, srcalpha, palette)
self._fill_surface(surf, palette)
return surf
def _assert_surface(self, surf, palette=None, msg=""):
if palette is None:
palette = self._test_palette
if surf.get_bitsize() == 16:
palette = [surf.unmap_rgb(surf.map_rgb(c)) for c in palette]
for posn, i in self._test_points:
self.assertEqual(
surf.get_at(posn),
palette[i],
"%s != %s: flags: %i, bpp: %i, posn: %s%s"
% (
surf.get_at(posn),
palette[i],
surf.get_flags(),
surf.get_bitsize(),
posn,
msg,
),
)
def test_blit_blend(self):
sources = [
self._make_src_surface(8),
self._make_src_surface(16),
self._make_src_surface(16, srcalpha=True),
self._make_src_surface(24),
self._make_src_surface(32),
self._make_src_surface(32, srcalpha=True),
]
destinations = [
self._make_surface(8),
self._make_surface(16),
self._make_surface(16, srcalpha=True),
self._make_surface(24),
self._make_surface(32),
self._make_surface(32, srcalpha=True),
]
blend = [
("BLEND_ADD", (0, 25, 100, 255), lambda a, b: min(a + b, 255)),
("BLEND_SUB", (100, 25, 0, 100), lambda a, b: max(a - b, 0)),
("BLEND_MULT", (100, 200, 0, 0), lambda a, b: ((a * b) + 255) >> 8),
("BLEND_MIN", (255, 0, 0, 255), min),
("BLEND_MAX", (0, 255, 0, 255), max),
]
for src in sources:
src_palette = [src.unmap_rgb(src.map_rgb(c)) for c in self._test_palette]
for dst in destinations:
for blend_name, dst_color, op in blend:
dc = dst.unmap_rgb(dst.map_rgb(dst_color))
p = []
for sc in src_palette:
c = [op(dc[i], sc[i]) for i in range(3)]
if dst.get_masks()[3]:
c.append(dc[3])
else:
c.append(255)
c = dst.unmap_rgb(dst.map_rgb(c))
p.append(c)
dst.fill(dst_color)
dst.blit(src, (0, 0), special_flags=getattr(pygame, blend_name))
self._assert_surface(
dst,
p,
(
", op: %s, src bpp: %i"
", src flags: %i"
% (blend_name, src.get_bitsize(), src.get_flags())
),
)
src = self._make_src_surface(32)
masks = src.get_masks()
dst = pygame.Surface(
src.get_size(), 0, 32, [masks[2], masks[1], masks[0], masks[3]]
)
for blend_name, dst_color, op in blend:
p = []
for src_color in self._test_palette:
c = [op(dst_color[i], src_color[i]) for i in range(3)]
c.append(255)
p.append(tuple(c))
dst.fill(dst_color)
dst.blit(src, (0, 0), special_flags=getattr(pygame, blend_name))
self._assert_surface(dst, p, f", {blend_name}")
# Blend blits are special cased for 32 to 32 bit surfaces.
#
# Confirm that it works when the rgb bytes are not the
# least significant bytes.
pat = self._make_src_surface(32)
masks = pat.get_masks()
if min(masks) == 0xFF000000:
masks = [m >> 8 for m in masks]
else:
masks = [m << 8 for m in masks]
src = pygame.Surface(pat.get_size(), 0, 32, masks)
self._fill_surface(src)
dst = pygame.Surface(src.get_size(), 0, 32, masks)
for blend_name, dst_color, op in blend:
p = []
for src_color in self._test_palette:
c = [op(dst_color[i], src_color[i]) for i in range(3)]
c.append(255)
p.append(tuple(c))
dst.fill(dst_color)
dst.blit(src, (0, 0), special_flags=getattr(pygame, blend_name))
self._assert_surface(dst, p, f", {blend_name}")
def test_blit_blend_rgba(self):
sources = [
self._make_src_surface(8),
self._make_src_surface(16),
self._make_src_surface(16, srcalpha=True),
self._make_src_surface(24),
self._make_src_surface(32),
self._make_src_surface(32, srcalpha=True),
]
destinations = [
self._make_surface(8),
self._make_surface(16),
self._make_surface(16, srcalpha=True),
self._make_surface(24),
self._make_surface(32),
self._make_surface(32, srcalpha=True),
]
blend = [
("BLEND_RGBA_ADD", (0, 25, 100, 255), lambda a, b: min(a + b, 255)),
("BLEND_RGBA_SUB", (0, 25, 100, 255), lambda a, b: max(a - b, 0)),
("BLEND_RGBA_MULT", (0, 7, 100, 255), lambda a, b: ((a * b) + 255) >> 8),
("BLEND_RGBA_MIN", (0, 255, 0, 255), min),
("BLEND_RGBA_MAX", (0, 255, 0, 255), max),
]
for src in sources:
src_palette = [src.unmap_rgb(src.map_rgb(c)) for c in self._test_palette]
for dst in destinations:
for blend_name, dst_color, op in blend:
dc = dst.unmap_rgb(dst.map_rgb(dst_color))
p = []
for sc in src_palette:
c = [op(dc[i], sc[i]) for i in range(4)]
if not dst.get_masks()[3]:
c[3] = 255
c = dst.unmap_rgb(dst.map_rgb(c))
p.append(c)
dst.fill(dst_color)
dst.blit(src, (0, 0), special_flags=getattr(pygame, blend_name))
self._assert_surface(
dst,
p,
(
", op: %s, src bpp: %i"
", src flags: %i"
% (blend_name, src.get_bitsize(), src.get_flags())
),
)
# Blend blits are special cased for 32 to 32 bit surfaces
# with per-pixel alpha.
#
# Confirm the general case is used instead when the formats differ.
src = self._make_src_surface(32, srcalpha=True)
masks = src.get_masks()
dst = pygame.Surface(
src.get_size(), SRCALPHA, 32, (masks[2], masks[1], masks[0], masks[3])
)
for blend_name, dst_color, op in blend:
p = [
tuple(op(dst_color[i], src_color[i]) for i in range(4))
for src_color in self._test_palette
]
dst.fill(dst_color)
dst.blit(src, (0, 0), special_flags=getattr(pygame, blend_name))
self._assert_surface(dst, p, f", {blend_name}")
# Confirm this special case handles subsurfaces.
src = pygame.Surface((8, 10), SRCALPHA, 32)
dst = pygame.Surface((8, 10), SRCALPHA, 32)
tst = pygame.Surface((8, 10), SRCALPHA, 32)
src.fill((1, 2, 3, 4))
dst.fill((40, 30, 20, 10))
subsrc = src.subsurface((2, 3, 4, 4))
subdst = dst.subsurface((2, 3, 4, 4))
subdst.blit(subsrc, (0, 0), special_flags=BLEND_RGBA_ADD)
tst.fill((40, 30, 20, 10))
tst.fill((41, 32, 23, 14), (2, 3, 4, 4))
for x in range(8):
for y in range(10):
self.assertEqual(
dst.get_at((x, y)),
tst.get_at((x, y)),
"%s != %s at (%i, %i)"
% (dst.get_at((x, y)), tst.get_at((x, y)), x, y),
)
def test_blit_blend_premultiplied(self):
def test_premul_surf(
src_col,
dst_col,
src_size=(16, 16),
dst_size=(16, 16),
src_bit_depth=32,
dst_bit_depth=32,
src_has_alpha=True,
dst_has_alpha=True,
):
if src_bit_depth == 8:
src = pygame.Surface(src_size, 0, src_bit_depth)
palette = [src_col, dst_col]
src.set_palette(palette)
src.fill(palette[0])
elif src_has_alpha:
src = pygame.Surface(src_size, SRCALPHA, src_bit_depth)
src.fill(src_col)
else:
src = pygame.Surface(src_size, 0, src_bit_depth)
src.fill(src_col)
if dst_bit_depth == 8:
dst = pygame.Surface(dst_size, 0, dst_bit_depth)
palette = [src_col, dst_col]
dst.set_palette(palette)
dst.fill(palette[1])
elif dst_has_alpha:
dst = pygame.Surface(dst_size, SRCALPHA, dst_bit_depth)
dst.fill(dst_col)
else:
dst = pygame.Surface(dst_size, 0, dst_bit_depth)
dst.fill(dst_col)
dst.blit(src, (0, 0), special_flags=BLEND_PREMULTIPLIED)
actual_col = dst.get_at(
(int(float(src_size[0] / 2.0)), int(float(src_size[0] / 2.0)))
)
# This is the blend pre-multiplied formula
if src_col.a == 0:
expected_col = dst_col
elif src_col.a == 255:
expected_col = src_col
else:
# sC + dC - (((dC + 1) * sA >> 8)
expected_col = pygame.Color(
(src_col.r + dst_col.r - ((dst_col.r + 1) * src_col.a >> 8)),
(src_col.g + dst_col.g - ((dst_col.g + 1) * src_col.a >> 8)),
(src_col.b + dst_col.b - ((dst_col.b + 1) * src_col.a >> 8)),
(src_col.a + dst_col.a - ((dst_col.a + 1) * src_col.a >> 8)),
)
if not dst_has_alpha:
expected_col.a = 255
return (expected_col, actual_col)
# # Colour Tests
self.assertEqual(
*test_premul_surf(pygame.Color(40, 20, 0, 51), pygame.Color(40, 20, 0, 51))
)
self.assertEqual(
*test_premul_surf(pygame.Color(0, 0, 0, 0), pygame.Color(40, 20, 0, 51))
)
self.assertEqual(
*test_premul_surf(pygame.Color(40, 20, 0, 51), pygame.Color(0, 0, 0, 0))
)
self.assertEqual(
*test_premul_surf(pygame.Color(0, 0, 0, 0), pygame.Color(0, 0, 0, 0))
)
self.assertEqual(
*test_premul_surf(pygame.Color(2, 2, 2, 2), pygame.Color(40, 20, 0, 51))
)
self.assertEqual(
*test_premul_surf(pygame.Color(40, 20, 0, 51), pygame.Color(2, 2, 2, 2))
)
self.assertEqual(
*test_premul_surf(pygame.Color(2, 2, 2, 2), pygame.Color(2, 2, 2, 2))
)
self.assertEqual(
*test_premul_surf(pygame.Color(9, 9, 9, 9), pygame.Color(40, 20, 0, 51))
)
self.assertEqual(
*test_premul_surf(pygame.Color(40, 20, 0, 51), pygame.Color(9, 9, 9, 9))
)
self.assertEqual(
*test_premul_surf(pygame.Color(9, 9, 9, 9), pygame.Color(9, 9, 9, 9))
)
self.assertEqual(
*test_premul_surf(
pygame.Color(127, 127, 127, 127), pygame.Color(40, 20, 0, 51)
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(40, 20, 0, 51), pygame.Color(127, 127, 127, 127)
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(127, 127, 127, 127), pygame.Color(127, 127, 127, 127)
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(200, 200, 200, 200), pygame.Color(40, 20, 0, 51)
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(40, 20, 0, 51), pygame.Color(200, 200, 200, 200)
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(200, 200, 200, 200), pygame.Color(200, 200, 200, 200)
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(255, 255, 255, 255), pygame.Color(40, 20, 0, 51)
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(40, 20, 0, 51), pygame.Color(255, 255, 255, 255)
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(255, 255, 255, 255), pygame.Color(255, 255, 255, 255)
)
)
# Surface format tests
self.assertRaises(
IndexError,
test_premul_surf,
pygame.Color(255, 255, 255, 255),
pygame.Color(255, 255, 255, 255),
src_size=(0, 0),
dst_size=(0, 0),
)
self.assertEqual(
*test_premul_surf(
pygame.Color(40, 20, 0, 51),
pygame.Color(30, 20, 0, 51),
src_size=(4, 4),
dst_size=(9, 9),
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(30, 20, 0, 51),
pygame.Color(40, 20, 0, 51),
src_size=(17, 67),
dst_size=(69, 69),
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(30, 20, 0, 255),
pygame.Color(40, 20, 0, 51),
src_size=(17, 67),
dst_size=(69, 69),
src_has_alpha=True,
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(30, 20, 0, 51),
pygame.Color(40, 20, 0, 255),
src_size=(17, 67),
dst_size=(69, 69),
dst_has_alpha=False,
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(30, 20, 0, 255),
pygame.Color(40, 20, 0, 255),
src_size=(17, 67),
dst_size=(69, 69),
src_has_alpha=False,
dst_has_alpha=False,
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(30, 20, 0, 255),
pygame.Color(40, 20, 0, 255),
src_size=(17, 67),
dst_size=(69, 69),
dst_bit_depth=24,
src_has_alpha=True,
dst_has_alpha=False,
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(30, 20, 0, 255),
pygame.Color(40, 20, 0, 255),
src_size=(17, 67),
dst_size=(69, 69),
src_bit_depth=24,
src_has_alpha=False,
dst_has_alpha=True,
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(30, 20, 0, 255),
pygame.Color(40, 20, 0, 255),
src_size=(17, 67),
dst_size=(69, 69),
src_bit_depth=24,
dst_bit_depth=24,
src_has_alpha=False,
dst_has_alpha=False,
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(30, 20, 0, 255),
pygame.Color(40, 20, 0, 255),
src_size=(17, 67),
dst_size=(69, 69),
src_bit_depth=8,
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(30, 20, 0, 255),
pygame.Color(40, 20, 0, 255),
src_size=(17, 67),
dst_size=(69, 69),
dst_bit_depth=8,
)
)
self.assertEqual(
*test_premul_surf(
pygame.Color(30, 20, 0, 255),
pygame.Color(40, 20, 0, 255),
src_size=(17, 67),
dst_size=(69, 69),
src_bit_depth=8,
dst_bit_depth=8,
)
)
def test_blit_blend_big_rect(self):
"""test that an oversized rect works ok."""
color = (1, 2, 3, 255)
area = (1, 1, 30, 30)
s1 = pygame.Surface((4, 4), 0, 32)
r = s1.fill(special_flags=pygame.BLEND_ADD, color=color, rect=area)
self.assertEqual(pygame.Rect((1, 1, 3, 3)), r)
self.assertEqual(s1.get_at((0, 0)), (0, 0, 0, 255))
self.assertEqual(s1.get_at((1, 1)), color)
black = pygame.Color("black")
red = pygame.Color("red")
self.assertNotEqual(black, red)
surf = pygame.Surface((10, 10), 0, 32)
surf.fill(black)
subsurf = surf.subsurface(pygame.Rect(0, 1, 10, 8))
self.assertEqual(surf.get_at((0, 0)), black)
self.assertEqual(surf.get_at((0, 9)), black)
subsurf.fill(red, (0, -1, 10, 1), pygame.BLEND_RGB_ADD)
self.assertEqual(surf.get_at((0, 0)), black)
self.assertEqual(surf.get_at((0, 9)), black)
subsurf.fill(red, (0, 8, 10, 1), pygame.BLEND_RGB_ADD)
self.assertEqual(surf.get_at((0, 0)), black)
self.assertEqual(surf.get_at((0, 9)), black)
def test_GET_PIXELVALS(self):
# surface.h GET_PIXELVALS bug regarding whether of not
# a surface has per-pixel alpha. Looking at the Amask
# is not enough. The surface's SRCALPHA flag must also
# be considered. Fix rev. 1923.
src = self._make_surface(32, srcalpha=True)
src.fill((0, 0, 0, 128))
src.set_alpha(None) # Clear SRCALPHA flag.
dst = self._make_surface(32, srcalpha=True)
dst.blit(src, (0, 0), special_flags=BLEND_RGBA_ADD)
self.assertEqual(dst.get_at((0, 0)), (0, 0, 0, 255))
def test_fill_blend(self):
destinations = [
self._make_surface(8),
self._make_surface(16),
self._make_surface(16, srcalpha=True),
self._make_surface(24),
self._make_surface(32),
self._make_surface(32, srcalpha=True),
]
blend = [
("BLEND_ADD", (0, 25, 100, 255), lambda a, b: min(a + b, 255)),
("BLEND_SUB", (0, 25, 100, 255), lambda a, b: max(a - b, 0)),
("BLEND_MULT", (0, 7, 100, 255), lambda a, b: ((a * b) + 255) >> 8),
("BLEND_MIN", (0, 255, 0, 255), min),
("BLEND_MAX", (0, 255, 0, 255), max),
]
for dst in destinations:
dst_palette = [dst.unmap_rgb(dst.map_rgb(c)) for c in self._test_palette]
for blend_name, fill_color, op in blend:
fc = dst.unmap_rgb(dst.map_rgb(fill_color))
self._fill_surface(dst)
p = []
for dc in dst_palette:
c = [op(dc[i], fc[i]) for i in range(3)]
if dst.get_masks()[3]:
c.append(dc[3])
else:
c.append(255)
c = dst.unmap_rgb(dst.map_rgb(c))
p.append(c)
dst.fill(fill_color, special_flags=getattr(pygame, blend_name))
self._assert_surface(dst, p, f", {blend_name}")
def test_fill_blend_rgba(self):
destinations = [
self._make_surface(8),
self._make_surface(16),
self._make_surface(16, srcalpha=True),
self._make_surface(24),
self._make_surface(32),
self._make_surface(32, srcalpha=True),
]
blend = [
("BLEND_RGBA_ADD", (0, 25, 100, 255), lambda a, b: min(a + b, 255)),
("BLEND_RGBA_SUB", (0, 25, 100, 255), lambda a, b: max(a - b, 0)),
("BLEND_RGBA_MULT", (0, 7, 100, 255), lambda a, b: ((a * b) + 255) >> 8),
("BLEND_RGBA_MIN", (0, 255, 0, 255), min),
("BLEND_RGBA_MAX", (0, 255, 0, 255), max),
]
for dst in destinations:
dst_palette = [dst.unmap_rgb(dst.map_rgb(c)) for c in self._test_palette]
for blend_name, fill_color, op in blend:
fc = dst.unmap_rgb(dst.map_rgb(fill_color))
self._fill_surface(dst)
p = []
for dc in dst_palette:
c = [op(dc[i], fc[i]) for i in range(4)]
if not dst.get_masks()[3]:
c[3] = 255
c = dst.unmap_rgb(dst.map_rgb(c))
p.append(c)
dst.fill(fill_color, special_flags=getattr(pygame, blend_name))
self._assert_surface(dst, p, f", {blend_name}")
def test_surface_premul_alpha(self):
"""Ensure that .premul_alpha() works correctly"""
# basic functionality at valid bit depths - 32, 16 & 8
s1 = pygame.Surface((100, 100), pygame.SRCALPHA, 32)
s1.fill(pygame.Color(255, 255, 255, 100))
s1_alpha = s1.premul_alpha()
self.assertEqual(s1_alpha.get_at((50, 50)), pygame.Color(100, 100, 100, 100))
# 16 bit colour has less precision
s2 = pygame.Surface((100, 100), pygame.SRCALPHA, 16)
s2.fill(pygame.Color(255, 255, 255, 170))
s2_alpha = s2.premul_alpha()
self.assertEqual(s2_alpha.get_at((50, 50)), pygame.Color(170, 170, 170, 170))
# invalid surface - we need alpha to pre-multiply
invalid_surf = pygame.Surface((100, 100), 0, 32)
invalid_surf.fill(pygame.Color(255, 255, 255, 100))
with self.assertRaises(ValueError):
invalid_surf.premul_alpha()
# churn a bunch of values
test_colors = [
(200, 30, 74),
(76, 83, 24),
(184, 21, 6),
(74, 4, 74),
(76, 83, 24),
(184, 21, 234),
(160, 30, 74),
(96, 147, 204),
(198, 201, 60),
(132, 89, 74),
(245, 9, 224),
(184, 112, 6),
]
for r, g, b in test_colors:
for a in range(255):
with self.subTest(r=r, g=g, b=b, a=a):
surf = pygame.Surface((10, 10), pygame.SRCALPHA, 32)
surf.fill(pygame.Color(r, g, b, a))
surf = surf.premul_alpha()
self.assertEqual(
surf.get_at((5, 5)),
Color(
((r + 1) * a) >> 8,
((g + 1) * a) >> 8,
((b + 1) * a) >> 8,
a,
),
)
class SurfaceSelfBlitTest(unittest.TestCase):
"""Blit to self tests.
This test case is in response to https://github.com/pygame/pygame/issues/19
"""
def setUp(self):
# Needed for 8 bits-per-pixel color palette surface tests.
pygame.display.init()
def tearDown(self):
pygame.display.quit()
_test_palette = [(0, 0, 0, 255), (255, 0, 0, 0), (0, 255, 0, 255)]
surf_size = (9, 6)
def _fill_surface(self, surf, palette=None):
if palette is None:
palette = self._test_palette
surf.fill(palette[1])
surf.fill(palette[2], (1, 2, 1, 2))
def _make_surface(self, bitsize, srcalpha=False, palette=None):
if palette is None:
palette = self._test_palette
flags = 0
if srcalpha:
flags |= SRCALPHA
surf = pygame.Surface(self.surf_size, flags, bitsize)
if bitsize == 8:
surf.set_palette([c[:3] for c in palette])
self._fill_surface(surf, palette)
return surf
def _assert_same(self, a, b):
w, h = a.get_size()
for x in range(w):
for y in range(h):
self.assertEqual(
a.get_at((x, y)),
b.get_at((x, y)),
(
"%s != %s, bpp: %i"
% (a.get_at((x, y)), b.get_at((x, y)), a.get_bitsize())
),
)
def test_overlap_check(self):
# Ensure overlapping blits are properly detected. There are two
# places where this is done, within SoftBlitPyGame() in alphablit.c
# and PySurface_Blit() in surface.c. SoftBlitPyGame should catch the
# per-pixel alpha surface, PySurface_Blit the colorkey and blanket
# alpha surface. per-pixel alpha and blanket alpha self blits are
# not properly handled by SDL 1.2.13, so Pygame does them.
bgc = (0, 0, 0, 255)
rectc_left = (128, 64, 32, 255)
rectc_right = (255, 255, 255, 255)
colors = [(255, 255, 255, 255), (128, 64, 32, 255)]
overlaps = [
(0, 0, 1, 0, (50, 0)),
(0, 0, 49, 1, (98, 2)),
(0, 0, 49, 49, (98, 98)),
(49, 0, 0, 1, (0, 2)),
(49, 0, 0, 49, (0, 98)),
]
surfs = [pygame.Surface((100, 100), SRCALPHA, 32)]
surf = pygame.Surface((100, 100), 0, 32)
surf.set_alpha(255)
surfs.append(surf)
surf = pygame.Surface((100, 100), 0, 32)
surf.set_colorkey((0, 1, 0))
surfs.append(surf)
for surf in surfs:
for s_x, s_y, d_x, d_y, test_posn in overlaps:
surf.fill(bgc)
surf.fill(rectc_right, (25, 0, 25, 50))
surf.fill(rectc_left, (0, 0, 25, 50))
surf.blit(surf, (d_x, d_y), (s_x, s_y, 50, 50))
self.assertEqual(surf.get_at(test_posn), rectc_right)
# https://github.com/pygame/pygame/issues/370#issuecomment-364625291
@unittest.skipIf("ppc64le" in platform.uname(), "known ppc64le issue")
def test_colorkey(self):
# Check a workaround for an SDL 1.2.13 surface self-blit problem
# https://github.com/pygame/pygame/issues/19
pygame.display.set_mode((100, 50)) # Needed for 8bit surface
bitsizes = [8, 16, 24, 32]
for bitsize in bitsizes:
surf = self._make_surface(bitsize)
surf.set_colorkey(self._test_palette[1])
surf.blit(surf, (3, 0))
p = []
for c in self._test_palette:
c = surf.unmap_rgb(surf.map_rgb(c))
p.append(c)
p[1] = (p[1][0], p[1][1], p[1][2], 0)
tmp = self._make_surface(32, srcalpha=True, palette=p)
tmp.blit(tmp, (3, 0))
tmp.set_alpha(None)
comp = self._make_surface(bitsize)
comp.blit(tmp, (0, 0))
self._assert_same(surf, comp)
# https://github.com/pygame/pygame/issues/370#issuecomment-364625291
@unittest.skipIf("ppc64le" in platform.uname(), "known ppc64le issue")
def test_blanket_alpha(self):
# Check a workaround for an SDL 1.2.13 surface self-blit problem
# https://github.com/pygame/pygame/issues/19
pygame.display.set_mode((100, 50)) # Needed for 8bit surface
bitsizes = [8, 16, 24, 32]
for bitsize in bitsizes:
surf = self._make_surface(bitsize)
surf.set_alpha(128)
surf.blit(surf, (3, 0))
p = []
for c in self._test_palette:
c = surf.unmap_rgb(surf.map_rgb(c))
p.append((c[0], c[1], c[2], 128))
tmp = self._make_surface(32, srcalpha=True, palette=p)
tmp.blit(tmp, (3, 0))
tmp.set_alpha(None)
comp = self._make_surface(bitsize)
comp.blit(tmp, (0, 0))
self._assert_same(surf, comp)
def test_pixel_alpha(self):
bitsizes = [16, 32]
for bitsize in bitsizes:
surf = self._make_surface(bitsize, srcalpha=True)
comp = self._make_surface(bitsize, srcalpha=True)
comp.blit(surf, (3, 0))
surf.blit(surf, (3, 0))
self._assert_same(surf, comp)
def test_blend(self):
bitsizes = [8, 16, 24, 32]
blends = ["BLEND_ADD", "BLEND_SUB", "BLEND_MULT", "BLEND_MIN", "BLEND_MAX"]
for bitsize in bitsizes:
surf = self._make_surface(bitsize)
comp = self._make_surface(bitsize)
for blend in blends:
self._fill_surface(surf)
self._fill_surface(comp)
comp.blit(surf, (3, 0), special_flags=getattr(pygame, blend))
surf.blit(surf, (3, 0), special_flags=getattr(pygame, blend))
self._assert_same(surf, comp)
def test_blend_rgba(self):
bitsizes = [16, 32]
blends = [
"BLEND_RGBA_ADD",
"BLEND_RGBA_SUB",
"BLEND_RGBA_MULT",
"BLEND_RGBA_MIN",
"BLEND_RGBA_MAX",
]
for bitsize in bitsizes:
surf = self._make_surface(bitsize, srcalpha=True)
comp = self._make_surface(bitsize, srcalpha=True)
for blend in blends:
self._fill_surface(surf)
self._fill_surface(comp)
comp.blit(surf, (3, 0), special_flags=getattr(pygame, blend))
surf.blit(surf, (3, 0), special_flags=getattr(pygame, blend))
self._assert_same(surf, comp)
def test_subsurface(self):
# Blitting a surface to its subsurface is allowed.
surf = self._make_surface(32, srcalpha=True)
comp = surf.copy()
comp.blit(surf, (3, 0))
sub = surf.subsurface((3, 0, 6, 6))
sub.blit(surf, (0, 0))
del sub
self._assert_same(surf, comp)
# Blitting a subsurface to its owner is forbidden because of
# lock conflicts. This limitation allows the overlap check
# in PySurface_Blit of alphablit.c to be simplified.
def do_blit(d, s):
d.blit(s, (0, 0))
sub = surf.subsurface((1, 1, 2, 2))
self.assertRaises(pygame.error, do_blit, surf, sub)
def test_copy_alpha(self):
"""issue 581: alpha of surface copy with SRCALPHA is set to 0."""
surf = pygame.Surface((16, 16), pygame.SRCALPHA, 32)
self.assertEqual(surf.get_alpha(), 255)
surf2 = surf.copy()
self.assertEqual(surf2.get_alpha(), 255)
class SurfaceFillTest(unittest.TestCase):
def setUp(self):
pygame.display.init()
def tearDown(self):
pygame.display.quit()
def test_fill(self):
screen = pygame.display.set_mode((640, 480))
# Green and blue test pattern
screen.fill((0, 255, 0), (0, 0, 320, 240))
screen.fill((0, 255, 0), (320, 240, 320, 240))
screen.fill((0, 0, 255), (320, 0, 320, 240))
screen.fill((0, 0, 255), (0, 240, 320, 240))
# Now apply a clip rect, such that only the left side of the
# screen should be effected by blit operations.
screen.set_clip((0, 0, 320, 480))
# Test fills with each special flag, and additionally without any.
screen.fill((255, 0, 0, 127), (160, 0, 320, 30), 0)
screen.fill((255, 0, 0, 127), (160, 30, 320, 30), pygame.BLEND_ADD)
screen.fill((0, 127, 127, 127), (160, 60, 320, 30), pygame.BLEND_SUB)
screen.fill((0, 63, 63, 127), (160, 90, 320, 30), pygame.BLEND_MULT)
screen.fill((0, 127, 127, 127), (160, 120, 320, 30), pygame.BLEND_MIN)
screen.fill((127, 0, 0, 127), (160, 150, 320, 30), pygame.BLEND_MAX)
screen.fill((255, 0, 0, 127), (160, 180, 320, 30), pygame.BLEND_RGBA_ADD)
screen.fill((0, 127, 127, 127), (160, 210, 320, 30), pygame.BLEND_RGBA_SUB)
screen.fill((0, 63, 63, 127), (160, 240, 320, 30), pygame.BLEND_RGBA_MULT)
screen.fill((0, 127, 127, 127), (160, 270, 320, 30), pygame.BLEND_RGBA_MIN)
screen.fill((127, 0, 0, 127), (160, 300, 320, 30), pygame.BLEND_RGBA_MAX)
screen.fill((255, 0, 0, 127), (160, 330, 320, 30), pygame.BLEND_RGB_ADD)
screen.fill((0, 127, 127, 127), (160, 360, 320, 30), pygame.BLEND_RGB_SUB)
screen.fill((0, 63, 63, 127), (160, 390, 320, 30), pygame.BLEND_RGB_MULT)
screen.fill((0, 127, 127, 127), (160, 420, 320, 30), pygame.BLEND_RGB_MIN)
screen.fill((255, 0, 0, 127), (160, 450, 320, 30), pygame.BLEND_RGB_MAX)
# Update the display so we can see the results
pygame.display.flip()
# Compare colors on both sides of window
for y in range(5, 480, 10):
self.assertEqual(screen.get_at((10, y)), screen.get_at((330, 480 - y)))
if __name__ == "__main__":
unittest.main()
|