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
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <qimage.h>
#include <qimagereader.h>
#include <qlist.h>
#include <qtransform.h>
#include <qrandom.h>
#include <stdio.h>
#include <qpainter.h>
#include <private/qimage_p.h>
#include <private/qdrawhelper_p.h>
#ifdef Q_OS_DARWIN
#include <CoreGraphics/CoreGraphics.h>
#endif
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
# include <qt_windows.h>
#endif
Q_DECLARE_METATYPE(QImage::Format)
Q_DECLARE_METATYPE(Qt::GlobalColor)
class tst_QImage : public QObject
{
Q_OBJECT
public:
tst_QImage();
private slots:
void initTestCase();
void swap();
void create();
void createInvalidXPM();
void createFromUChar();
void formatHandlersInput_data();
void formatHandlersInput();
void setAlphaChannel_data();
void setAlphaChannel();
void alphaChannel();
void convertToFormat_data();
void convertToFormat();
void convertToFormatWithColorTable();
void convertToFormatRgb888ToRGB32();
void createAlphaMask_data();
void createAlphaMask();
#ifndef QT_NO_IMAGE_HEURISTIC_MASK
void createHeuristicMask();
#endif
void dotsPerMeterZero();
void dotsPerMeterAndDpi();
void convertToFormatPreserveDotsPrMeter();
void convertToFormatPreserveText();
void rotate_data();
void rotate();
void copy();
void load();
void loadFromData();
#if !defined(QT_NO_DATASTREAM)
void loadFromDataStream();
#endif
void setPixel_data();
void setPixel();
void setPixelWithAlpha_data();
void setPixelWithAlpha();
void setPixelColorWithAlpha_data();
void setPixelColorWithAlpha();
void defaultColorTable_data();
void defaultColorTable();
void setColorCount();
void setColor();
void rasterClipping();
void pointOverloads();
void destructor();
void cacheKey();
void smoothScale();
void smoothScale2_data();
void smoothScale2();
void smoothScale3_data();
void smoothScale3();
void smoothScale4_data();
void smoothScale4();
void smoothScaleBig();
void smoothScaleAlpha();
void transformed_data();
void transformed();
void transformed2();
void scaled();
void paintEngine();
void setAlphaChannelWhilePainting();
void smoothScaledSubImage();
void nullSize_data();
void nullSize();
void premultipliedAlphaConsistency();
void compareIndexed();
void fillColor_data();
void fillColor();
void fillColorWithAlpha_data();
void fillColorWithAlpha();
void fillRGB888();
void fillPixel_data();
void fillPixel();
void rgbSwapped_data();
void rgbSwapped();
void mirrored_data();
void mirrored();
void inplaceRgbSwapped_data();
void inplaceRgbSwapped();
void inplaceMirrored_data();
void inplaceMirrored();
void inplaceMirroredOdd_data();
void inplaceMirroredOdd();
void inplaceRgbMirrored();
void genericRgbConversion_data();
void genericRgbConversion();
void inplaceRgbConversion_data();
void inplaceRgbConversion();
void largeGenericRgbConversion_data();
void largeGenericRgbConversion();
void largeInplaceRgbConversion_data();
void largeInplaceRgbConversion();
void deepCopyWhenPaintingActive();
void scaled_QTBUG19157();
void convertOverUnPreMul();
void scaled_QTBUG35972();
void convertToPixelFormat();
void convertToImageFormat_data();
void convertToImageFormat();
void invertPixelsRGB_data();
void invertPixelsRGB();
void invertPixelsIndexed();
void exifOrientation_data();
void exifOrientation();
void exif_QTBUG45865();
void exifInvalidData_data();
void exifInvalidData();
void exifReadComments();
void cleanupFunctions();
void devicePixelRatio();
void rgb30Unpremul();
void rgb30Repremul_data();
void rgb30Repremul();
void metadataPassthrough();
void pixelColor();
void pixel();
void ditherGradient_data();
void ditherGradient();
void reinterpretAsFormat_data();
void reinterpretAsFormat();
void reinterpretAsFormat2();
void complexTransform8bit();
#ifdef Q_OS_DARWIN
void toCGImage_data();
void toCGImage();
#endif
void hugeQImage();
void convertColorTable();
void wideImage();
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
void toWinHBITMAP_data();
void toWinHBITMAP();
void fromMonoHBITMAP();
#endif // Q_OS_WIN && !Q_OS_WINRT
private:
const QString m_prefix;
};
static QLatin1String formatToString(QImage::Format format)
{
switch (format) {
case QImage::Format_Invalid:
return QLatin1String("Invalid");
case QImage::Format_Mono:
return QLatin1String("Mono");
case QImage::Format_MonoLSB:
return QLatin1String("MonoLSB");
case QImage::Format_Indexed8:
return QLatin1String("Indexed8");
case QImage::Format_RGB32:
return QLatin1String("RGB32");
case QImage::Format_ARGB32:
return QLatin1String("ARGB32");
case QImage::Format_ARGB32_Premultiplied:
return QLatin1String("ARGB32pm");
case QImage::Format_RGB16:
return QLatin1String("RGB16");
case QImage::Format_ARGB8565_Premultiplied:
return QLatin1String("ARGB8565pm");
case QImage::Format_RGB666:
return QLatin1String("RGB666");
case QImage::Format_ARGB6666_Premultiplied:
return QLatin1String("ARGB6666pm");
case QImage::Format_RGB555:
return QLatin1String("RGB555");
case QImage::Format_ARGB8555_Premultiplied:
return QLatin1String("ARGB8555pm");
case QImage::Format_RGB888:
return QLatin1String("RGB888");
case QImage::Format_RGB444:
return QLatin1String("RGB444");
case QImage::Format_ARGB4444_Premultiplied:
return QLatin1String("ARGB4444pm");
case QImage::Format_RGBX8888:
return QLatin1String("RGBx88888");
case QImage::Format_RGBA8888:
return QLatin1String("RGBA88888");
case QImage::Format_RGBA8888_Premultiplied:
return QLatin1String("RGBA88888pm");
case QImage::Format_BGR30:
return QLatin1String("BGR30");
case QImage::Format_A2BGR30_Premultiplied:
return QLatin1String("A2BGR30pm");
case QImage::Format_RGB30:
return QLatin1String("RGB30");
case QImage::Format_A2RGB30_Premultiplied:
return QLatin1String("A2RGB30pm");
case QImage::Format_Alpha8:
return QLatin1String("Alpha8");
case QImage::Format_Grayscale8:
return QLatin1String("Grayscale8");
case QImage::Format_RGBX64:
return QLatin1String("RGBx64");
case QImage::Format_RGBA64:
return QLatin1String("RGBA64");
case QImage::Format_RGBA64_Premultiplied:
return QLatin1String("RGBA64pm");
case QImage::Format_Grayscale16:
return QLatin1String("Grayscale16");
case QImage::Format_BGR888:
return QLatin1String("BGR888");
default:
break;
};
Q_UNREACHABLE();
qWarning("Unhandled image format");
return QLatin1String("unknown");
}
tst_QImage::tst_QImage()
: m_prefix(QFINDTESTDATA("images/"))
{
}
void tst_QImage::initTestCase()
{
QVERIFY(!m_prefix.isEmpty());
}
void tst_QImage::swap()
{
QImage i1( 16, 16, QImage::Format_RGB32 ), i2( 32, 32, QImage::Format_RGB32 );
i1.fill( Qt::white );
i2.fill( Qt::black );
const qint64 i1k = i1.cacheKey();
const qint64 i2k = i2.cacheKey();
i1.swap(i2);
QCOMPARE(i1.cacheKey(), i2k);
QCOMPARE(i1.size(), QSize(32,32));
QCOMPARE(i2.cacheKey(), i1k);
QCOMPARE(i2.size(), QSize(16,16));
}
// Test if QImage (or any functions called from QImage) throws an
// exception when creating an extremely large image.
// QImage::create() should return "false" in this case.
void tst_QImage::create()
{
bool cr = true;
QT_TRY {
QImage image(700000000, 70000000, QImage::Format_Indexed8);
image.setColorCount(256);
cr = !image.isNull();
} QT_CATCH (...) {
}
QVERIFY( !cr );
}
void tst_QImage::createInvalidXPM()
{
QTest::ignoreMessage(QtWarningMsg, "QImage::QImage(), XPM is not supported");
const char *xpm[] = {""};
QImage invalidXPM(xpm);
QVERIFY(invalidXPM.isNull());
}
void tst_QImage::createFromUChar()
{
uint data[] = { 0xff010101U,
0xff020202U,
0xff030303U,
0xff040404U };
// When the data is const, nothing you do to the image will change the source data.
QImage i1((const uchar*)data, 2, 2, 8, QImage::Format_RGB32);
QCOMPARE(i1.pixel(0,0), 0xFF010101U);
QCOMPARE(i1.pixel(1,0), 0xFF020202U);
QCOMPARE(i1.pixel(0,1), 0xFF030303U);
QCOMPARE(i1.pixel(1,1), 0xFF040404U);
{
QImage i(i1);
i.setPixel(0,0,5);
}
QCOMPARE(i1.pixel(0,0), 0xFF010101U);
QCOMPARE(*(QRgb*)data, 0xFF010101U);
*((QRgb*)i1.bits()) = 0xFF070707U;
QCOMPARE(i1.pixel(0,0), 0xFF070707U);
QCOMPARE(*(QRgb*)data, 0xFF010101U);
// Changing copies should not change the original image or data.
{
QImage i(i1);
i.setPixel(0,0,5);
QCOMPARE(*(QRgb*)data, 0xFF010101U);
i1.setPixel(0,0,9);
QCOMPARE(i1.pixel(0,0), 0xFF000009U);
QCOMPARE(i.pixel(0,0), 0xFF000005U);
}
QCOMPARE(i1.pixel(0,0), 0xFF000009U);
// When the data is non-const, nothing you do to copies of the image will change the source data,
// but changing the image (here via bits()) will change the source data.
QImage i2((uchar*)data, 2, 2, 8, QImage::Format_RGB32);
QCOMPARE(i2.pixel(0,0), 0xFF010101U);
QCOMPARE(i2.pixel(1,0), 0xFF020202U);
QCOMPARE(i2.pixel(0,1), 0xFF030303U);
QCOMPARE(i2.pixel(1,1), 0xFF040404U);
{
QImage i(i2);
i.setPixel(0,0,5);
}
QCOMPARE(i2.pixel(0,0), 0xFF010101U);
QCOMPARE(*(QRgb*)data, 0xFF010101U);
*((QRgb*)i2.bits()) = 0xFF070707U;
QCOMPARE(i2.pixel(0,0), 0xFF070707U);
QCOMPARE(*(QRgb*)data, 0xFF070707U);
// Changing the data will change the image in either case.
QImage i3((uchar*)data, 2, 2, 8, QImage::Format_RGB32);
QImage i4((const uchar*)data, 2, 2, 8, QImage::Format_RGB32);
*(QRgb*)data = 0xFF060606U;
QCOMPARE(i3.pixel(0,0), 0xFF060606U);
QCOMPARE(i4.pixel(0,0), 0xFF060606U);
}
void tst_QImage::formatHandlersInput_data()
{
QTest::addColumn<QString>("testFormat");
QTest::addColumn<QString>("testFile");
// add a new line here when a file is added
QTest::newRow("ICO") << "ICO" << m_prefix + "image.ico";
QTest::newRow("PNG") << "PNG" << m_prefix + "image.png";
QTest::newRow("GIF") << "GIF" << m_prefix + "image.gif";
QTest::newRow("BMP") << "BMP" << m_prefix + "image.bmp";
QTest::newRow("JPEG") << "JPEG" << m_prefix + "image.jpg";
QTest::newRow("PBM") << "PBM" << m_prefix + "image.pbm";
QTest::newRow("PGM") << "PGM" << m_prefix + "image.pgm";
QTest::newRow("PPM") << "PPM" << m_prefix + "image.ppm";
QTest::newRow("XBM") << "XBM" << m_prefix + "image.xbm";
QTest::newRow("XPM") << "XPM" << m_prefix + "image.xpm";
}
void tst_QImage::formatHandlersInput()
{
QFETCH(QString, testFormat);
QFETCH(QString, testFile);
QList<QByteArray> formats = QImageReader::supportedImageFormats();
// qDebug("Image input formats : %s", formats.join(" | ").latin1());
bool formatSupported = false;
for (QList<QByteArray>::Iterator it = formats.begin(); it != formats.end(); ++it) {
if (*it == testFormat.toLower().toUtf8()) {
formatSupported = true;
break;
}
}
if (formatSupported) {
// qDebug(QImage::imageFormat(testFile));
QCOMPARE(testFormat.toLatin1().toLower(), QImageReader::imageFormat(testFile));
} else {
QString msg = "Format not supported : ";
QSKIP(QString(msg + testFormat).toLatin1());
}
}
void tst_QImage::setAlphaChannel_data()
{
QTest::addColumn<int>("red");
QTest::addColumn<int>("green");
QTest::addColumn<int>("blue");
QTest::addColumn<int>("alpha");
QTest::addColumn<bool>("gray");
QTest::newRow("red at 0%, gray") << 255 << 0 << 0 << 0 << true;
QTest::newRow("red at 25%, gray") << 255 << 0 << 0 << 63 << true;
QTest::newRow("red at 50%, gray") << 255 << 0 << 0 << 127 << true;
QTest::newRow("red at 100%, gray") << 255 << 0 << 0 << 191 << true;
QTest::newRow("red at 0%, 32bit") << 255 << 0 << 0 << 0 << false;
QTest::newRow("red at 25%, 32bit") << 255 << 0 << 0 << 63 << false;
QTest::newRow("red at 50%, 32bit") << 255 << 0 << 0 << 127 << false;
QTest::newRow("red at 100%, 32bit") << 255 << 0 << 0 << 191 << false;
QTest::newRow("green at 0%, gray") << 0 << 255 << 0 << 0 << true;
QTest::newRow("green at 25%, gray") << 0 << 255 << 0 << 63 << true;
QTest::newRow("green at 50%, gray") << 0 << 255 << 0 << 127 << true;
QTest::newRow("green at 100%, gray") << 0 << 255 << 0 << 191 << true;
QTest::newRow("green at 0%, 32bit") << 0 << 255 << 0 << 0 << false;
QTest::newRow("green at 25%, 32bit") << 0 << 255 << 0 << 63 << false;
QTest::newRow("green at 50%, 32bit") << 0 << 255 << 0 << 127 << false;
QTest::newRow("green at 100%, 32bit") << 0 << 255 << 0 << 191 << false;
QTest::newRow("blue at 0%, gray") << 0 << 0 << 255 << 0 << true;
QTest::newRow("blue at 25%, gray") << 0 << 0 << 255 << 63 << true;
QTest::newRow("blue at 50%, gray") << 0 << 0 << 255 << 127 << true;
QTest::newRow("blue at 100%, gray") << 0 << 0 << 255 << 191 << true;
QTest::newRow("blue at 0%, 32bit") << 0 << 0 << 255 << 0 << false;
QTest::newRow("blue at 25%, 32bit") << 0 << 0 << 255 << 63 << false;
QTest::newRow("blue at 50%, 32bit") << 0 << 0 << 255 << 127 << false;
QTest::newRow("blue at 100%, 32bit") << 0 << 0 << 255 << 191 << false;
}
void tst_QImage::setAlphaChannel()
{
QFETCH(int, red);
QFETCH(int, green);
QFETCH(int, blue);
QFETCH(int, alpha);
QFETCH(bool, gray);
int width = 100;
int height = 100;
QImage image(width, height, QImage::Format_RGB32);
image.fill(qRgb(red, green, blue));
// Create the alpha channel
QImage alphaChannel;
if (gray) {
alphaChannel = QImage(width, height, QImage::Format_Indexed8);
alphaChannel.setColorCount(256);
for (int i=0; i<256; ++i)
alphaChannel.setColor(i, qRgb(i, i, i));
alphaChannel.fill(alpha);
} else {
alphaChannel = QImage(width, height, QImage::Format_ARGB32);
alphaChannel.fill(qRgb(alpha, alpha, alpha));
}
image.setAlphaChannel(alphaChannel);
image = image.convertToFormat(QImage::Format_ARGB32);
QCOMPARE(image.format(), QImage::Format_ARGB32);
// alpha of 0 becomes black at a=0 due to premultiplication
QRgb pixel = alpha == 0 ? 0 : qRgba(red, green, blue, alpha);
bool allPixelsOK = true;
for (int y=0; y<height; ++y) {
for (int x=0; x<width; ++x) {
allPixelsOK &= image.pixel(x, y) == pixel;
}
}
QVERIFY(allPixelsOK);
QImage outAlpha = image.alphaChannel();
QCOMPARE(outAlpha.size(), image.size());
bool allAlphaOk = true;
for (int y=0; y<height; ++y) {
for (int x=0; x<width; ++x) {
allAlphaOk &= outAlpha.pixelIndex(x, y) == alpha;
}
}
QVERIFY(allAlphaOk);
}
void tst_QImage::alphaChannel()
{
QImage img(10, 10, QImage::Format_Mono);
img.setColor(0, Qt::transparent);
img.setColor(1, Qt::black);
img.fill(0);
QPainter p(&img);
p.fillRect(2, 2, 6, 6, Qt::black);
p.end();
QCOMPARE(img.alphaChannel(), img.convertToFormat(QImage::Format_ARGB32).alphaChannel());
}
void tst_QImage::convertToFormat_data()
{
QTest::addColumn<int>("inFormat");
QTest::addColumn<uint>("inPixel");
QTest::addColumn<int>("resFormat");
QTest::addColumn<uint>("resPixel");
QTest::newRow("red rgb32 -> argb32") << int(QImage::Format_RGB32) << 0xffff0000
<< int(QImage::Format_ARGB32) << 0xffff0000;
QTest::newRow("green rgb32 -> argb32") << int(QImage::Format_RGB32) << 0xff00ff00
<< int(QImage::Format_ARGB32) << 0xff00ff00;
QTest::newRow("blue rgb32 -> argb32") << int(QImage::Format_RGB32) << 0xff0000ff
<< int(QImage::Format_ARGB32) << 0xff0000ff;
QTest::newRow("red rgb32 -> rgb16") << int(QImage::Format_RGB32) << 0xffff0000
<< int(QImage::Format_RGB16) << 0xffff0000;
QTest::newRow("green rgb32 -> rgb16") << int(QImage::Format_RGB32) << 0xff00ff00
<< int(QImage::Format_RGB16) << 0xff00ff00;
QTest::newRow("blue rgb32 -> rgb16") << int(QImage::Format_RGB32) << 0xff0000ff
<< int(QImage::Format_RGB16) << 0xff0000ff;
QTest::newRow("funky rgb32 -> rgb16") << int(QImage::Format_RGB32) << 0xfff0c080
<< int(QImage::Format_RGB16) << 0xfff7c384;
QTest::newRow("red rgb32 -> argb32_pm") << int(QImage::Format_RGB32) << 0xffff0000
<< int(QImage::Format_ARGB32_Premultiplied) << 0xffff0000;
QTest::newRow("green rgb32 -> argb32_pm") << int(QImage::Format_RGB32) << 0xff00ff00
<< int(QImage::Format_ARGB32_Premultiplied) <<0xff00ff00;
QTest::newRow("blue rgb32 -> argb32_pm") << int(QImage::Format_RGB32) << 0xff0000ff
<< int(QImage::Format_ARGB32_Premultiplied) << 0xff0000ff;
QTest::newRow("semired argb32 -> pm") << int(QImage::Format_ARGB32) << 0x7fff0000u
<< int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f0000u;
QTest::newRow("semigreen argb32 -> pm") << int(QImage::Format_ARGB32) << 0x7f00ff00u
<< int(QImage::Format_ARGB32_Premultiplied) << 0x7f007f00u;
QTest::newRow("semiblue argb32 -> pm") << int(QImage::Format_ARGB32) << 0x7f0000ffu
<< int(QImage::Format_ARGB32_Premultiplied) << 0x7f00007fu;
QTest::newRow("semiwhite argb32 -> pm") << int(QImage::Format_ARGB32) << 0x7fffffffu
<< int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f7f7fu;
QTest::newRow("semiblack argb32 -> pm") << int(QImage::Format_ARGB32) << 0x7f000000u
<< int(QImage::Format_ARGB32_Premultiplied) << 0x7f000000u;
QTest::newRow("semired pm -> argb32") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f0000u
<< int(QImage::Format_ARGB32) << 0x7fff0000u;
QTest::newRow("semigreen pm -> argb32") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f007f00u
<< int(QImage::Format_ARGB32) << 0x7f00ff00u;
QTest::newRow("semiblue pm -> argb32") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f00007fu
<< int(QImage::Format_ARGB32) << 0x7f0000ffu;
QTest::newRow("semiwhite pm -> argb32") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f7f7fu
<< int(QImage::Format_ARGB32) << 0x7fffffffu;
QTest::newRow("semiblack pm -> argb32") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f000000u
<< int(QImage::Format_ARGB32) << 0x7f000000u;
QTest::newRow("semired pm -> rgb32") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f0000u
<< int(QImage::Format_RGB32) << 0xffff0000u;
QTest::newRow("semigreen pm -> rgb32") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f007f00u
<< int(QImage::Format_RGB32) << 0xff00ff00u;
QTest::newRow("semiblue pm -> rgb32") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f00007fu
<< int(QImage::Format_RGB32) << 0xff0000ffu;
QTest::newRow("semiwhite pm -> rgb32") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f7f7fu
<< int(QImage::Format_RGB32) << 0xffffffffu;
QTest::newRow("semiblack pm -> rgb32") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f000000u
<< int(QImage::Format_RGB32) << 0xff000000u;
QTest::newRow("semired argb32 -> rgb32") << int(QImage::Format_ARGB32) << 0x7fff0000u
<< int(QImage::Format_RGB32) << 0xffff0000u;
QTest::newRow("semigreen argb32 -> rgb32") << int(QImage::Format_ARGB32) << 0x7f00ff00u
<< int(QImage::Format_RGB32) << 0xff00ff00u;
QTest::newRow("semiblue argb32 -> rgb32") << int(QImage::Format_ARGB32) << 0x7f0000ffu
<< int(QImage::Format_RGB32) << 0xff0000ffu;
QTest::newRow("semiwhite argb -> rgb32") << int(QImage::Format_ARGB32) << 0x7fffffffu
<< int(QImage::Format_RGB32) << 0xffffffffu;
QTest::newRow("semiblack argb -> rgb32") << int(QImage::Format_ARGB32) << 0x7f000000u
<< int(QImage::Format_RGB32) << 0xff000000u;
QTest::newRow("black mono -> rgb32") << int(QImage::Format_Mono) << 0x00000000u
<< int(QImage::Format_RGB32) << 0xff000000u;
QTest::newRow("white mono -> rgb32") << int(QImage::Format_Mono) << 0x00000001u
<< int(QImage::Format_RGB32) << 0xffffffffu;
QTest::newRow("red rgb16 -> argb32") << int(QImage::Format_RGB16) << 0xffff0000
<< int(QImage::Format_ARGB32) << 0xffff0000;
QTest::newRow("green rgb16 -> argb32") << int(QImage::Format_RGB16) << 0xff00ff00
<< int(QImage::Format_ARGB32) << 0xff00ff00;
QTest::newRow("blue rgb16 -> argb32") << int(QImage::Format_RGB16) << 0xff0000ff
<< int(QImage::Format_ARGB32) << 0xff0000ff;
QTest::newRow("red rgb16 -> rgb16") << int(QImage::Format_RGB32) << 0xffff0000
<< int(QImage::Format_RGB16) << 0xffff0000;
QTest::newRow("green rgb16 -> rgb16") << int(QImage::Format_RGB32) << 0xff00ff00
<< int(QImage::Format_RGB16) << 0xff00ff00;
QTest::newRow("blue rgb16 -> rgb16") << int(QImage::Format_RGB32) << 0xff0000ff
<< int(QImage::Format_RGB16) << 0xff0000ff;
QTest::newRow("semired argb32 -> rgb16") << int(QImage::Format_ARGB32) << 0x7fff0000u
<< int(QImage::Format_RGB16) << 0xffff0000;
QTest::newRow("semigreen argb32 -> rgb16") << int(QImage::Format_ARGB32) << 0x7f00ff00u
<< int(QImage::Format_RGB16) << 0xff00ff00;
QTest::newRow("semiblue argb32 -> rgb16") << int(QImage::Format_ARGB32) << 0x7f0000ffu
<< int(QImage::Format_RGB16) << 0xff0000ff;
QTest::newRow("semired pm -> rgb16") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f0000u
<< int(QImage::Format_RGB16) << 0xffff0000u;
QTest::newRow("semigreen pm -> rgb16") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f007f00u
<< int(QImage::Format_RGB16) << 0xff00ff00u;
QTest::newRow("semiblue pm -> rgb16") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f00007fu
<< int(QImage::Format_RGB16) << 0xff0000ffu;
QTest::newRow("semiwhite pm -> rgb16") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f7f7fu
<< int(QImage::Format_RGB16) << 0xffffffffu;
QTest::newRow("semiblack pm -> rgb16") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f000000u
<< int(QImage::Format_RGB16) << 0xff000000u;
QTest::newRow("mono -> mono lsb") << int(QImage::Format_Mono) << 1u
<< int(QImage::Format_MonoLSB) << 0xffffffffu;
QTest::newRow("mono lsb -> mono") << int(QImage::Format_MonoLSB) << 1u
<< int(QImage::Format_Mono) << 0xffffffffu;
QTest::newRow("red rgb32 -> rgb666") << int(QImage::Format_RGB32) << 0xffff0000
<< int(QImage::Format_RGB666) << 0xffff0000;
QTest::newRow("green rgb32 -> rgb666") << int(QImage::Format_RGB32) << 0xff00ff00
<< int(QImage::Format_RGB666) << 0xff00ff00;
QTest::newRow("blue rgb32 -> rgb666") << int(QImage::Format_RGB32) << 0xff0000ff
<< int(QImage::Format_RGB666) << 0xff0000ff;
QTest::newRow("red rgb16 -> rgb666") << int(QImage::Format_RGB16) << 0xffff0000
<< int(QImage::Format_RGB666) << 0xffff0000;
QTest::newRow("green rgb16 -> rgb666") << int(QImage::Format_RGB16) << 0xff00ff00
<< int(QImage::Format_RGB666) << 0xff00ff00;
QTest::newRow("blue rgb16 -> rgb666") << int(QImage::Format_RGB16) << 0xff0000ff
<< int(QImage::Format_RGB666) << 0xff0000ff;
QTest::newRow("red rgb32 -> rgb15") << int(QImage::Format_RGB32) << 0xffff0000
<< int(QImage::Format_RGB555) << 0xffff0000;
QTest::newRow("green rgb32 -> rgb15") << int(QImage::Format_RGB32) << 0xff00ff00
<< int(QImage::Format_RGB555) << 0xff00ff00;
QTest::newRow("blue rgb32 -> rgb15") << int(QImage::Format_RGB32) << 0xff0000ff
<< int(QImage::Format_RGB555) << 0xff0000ff;
QTest::newRow("funky rgb32 -> rgb15") << int(QImage::Format_RGB32) << 0xfff0c080
<< int(QImage::Format_RGB555) << 0xfff7c684;
QTest::newRow("red rgb16 -> rgb15") << int(QImage::Format_RGB16) << 0xffff0000
<< int(QImage::Format_RGB555) << 0xffff0000;
QTest::newRow("green rgb16 -> rgb15") << int(QImage::Format_RGB16) << 0xff00ff00
<< int(QImage::Format_RGB555) << 0xff00ff00;
QTest::newRow("blue rgb16 -> rgb15") << int(QImage::Format_RGB16) << 0xff0000ff
<< int(QImage::Format_RGB555) << 0xff0000ff;
QTest::newRow("funky rgb16 -> rgb15") << int(QImage::Format_RGB16) << 0xfff0c080
<< int(QImage::Format_RGB555) << 0xfff7c684;
QTest::newRow("red rgb32 -> argb8565") << int(QImage::Format_RGB32) << 0xffff0000
<< int(QImage::Format_ARGB8565_Premultiplied) << 0xffff0000;
QTest::newRow("green rgb32 -> argb8565") << int(QImage::Format_RGB32) << 0xff00ff00
<< int(QImage::Format_ARGB8565_Premultiplied) << 0xff00ff00;
QTest::newRow("blue rgb32 -> argb8565") << int(QImage::Format_RGB32) << 0xff0000ff
<< int(QImage::Format_ARGB8565_Premultiplied) << 0xff0000ff;
QTest::newRow("red rgb16 -> argb8565") << int(QImage::Format_RGB16) << 0xffff0000
<< int(QImage::Format_ARGB8565_Premultiplied) << 0xffff0000;
QTest::newRow("green rgb16 -> argb8565") << int(QImage::Format_RGB16) << 0xff00ff00
<< int(QImage::Format_ARGB8565_Premultiplied) << 0xff00ff00;
QTest::newRow("blue rgb16 -> argb8565") << int(QImage::Format_RGB16) << 0xff0000ff
<< int(QImage::Format_ARGB8565_Premultiplied) << 0xff0000ff;
QTest::newRow("red argb8565 -> argb32") << int(QImage::Format_ARGB8565_Premultiplied) << 0xffff0000
<< int(QImage::Format_ARGB32) << 0xffff0000;
QTest::newRow("green argb8565 -> argb32") << int(QImage::Format_ARGB8565_Premultiplied) << 0xff00ff00
<< int(QImage::Format_ARGB32) << 0xff00ff00;
QTest::newRow("blue argb8565 -> argb32") << int(QImage::Format_ARGB8565_Premultiplied) << 0xff0000ff
<< int(QImage::Format_ARGB32) << 0xff0000ff;
QTest::newRow("semired argb32 -> argb8565") << int(QImage::Format_ARGB32) << 0x7fff0000u
<< int(QImage::Format_ARGB8565_Premultiplied) << 0x7f7b0000u;
QTest::newRow("semigreen argb32 -> argb8565") << int(QImage::Format_ARGB32) << 0x7f00ff00u
<< int(QImage::Format_ARGB8565_Premultiplied) << 0x7f007d00u;
QTest::newRow("semiblue argb32 -> argb8565") << int(QImage::Format_ARGB32) << 0x7f0000ffu
<< int(QImage::Format_ARGB8565_Premultiplied) << 0x7f00007bu;
QTest::newRow("semired pm -> argb8565") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f0000u
<< int(QImage::Format_ARGB8565_Premultiplied) << 0x7f7b0000u;
QTest::newRow("semigreen pm -> argb8565") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f007f00u
<< int(QImage::Format_ARGB8565_Premultiplied) << 0x7f007d00u;
QTest::newRow("semiblue pm -> argb8565") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f00007fu
<< int(QImage::Format_ARGB8565_Premultiplied) << 0x7f00007bu;
QTest::newRow("semiwhite pm -> argb8565") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f7f7fu
<< int(QImage::Format_ARGB8565_Premultiplied) << 0x7f7b7d7bu;
QTest::newRow("semiblack pm -> argb8565") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f000000u
<< int(QImage::Format_ARGB8565_Premultiplied) << 0x7f000000u;
QTest::newRow("red rgb666 -> argb32") << int(QImage::Format_RGB666) << 0xffff0000
<< int(QImage::Format_ARGB32) << 0xffff0000;
QTest::newRow("green rgb666 -> argb32") << int(QImage::Format_RGB666) << 0xff00ff00
<< int(QImage::Format_ARGB32) << 0xff00ff00;
QTest::newRow("blue rgb666 -> argb32") << int(QImage::Format_RGB666) << 0xff0000ff
<< int(QImage::Format_ARGB32) << 0xff0000ff;
QTest::newRow("semired argb32 -> rgb666") << int(QImage::Format_ARGB32) << 0x7fff0000u
<< int(QImage::Format_RGB666) << 0xffff0000;
QTest::newRow("semigreen argb32 -> rgb666") << int(QImage::Format_ARGB32) << 0x7f00ff00u
<< int(QImage::Format_RGB666) << 0xff00ff00;
QTest::newRow("semiblue argb32 -> rgb666") << int(QImage::Format_ARGB32) << 0x7f0000ffu
<< int(QImage::Format_RGB666) << 0xff0000ff;
QTest::newRow("semired pm -> rgb666") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f0000u
<< int(QImage::Format_RGB666) << 0xffff0000u;
QTest::newRow("semigreen pm -> rgb666") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f007f00u
<< int(QImage::Format_RGB666) << 0xff00ff00u;
QTest::newRow("semiblue pm -> rgb666") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f00007fu
<< int(QImage::Format_RGB666) << 0xff0000ffu;
QTest::newRow("semiwhite pm -> rgb666") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f7f7fu
<< int(QImage::Format_RGB666) << 0xffffffffu;
QTest::newRow("semiblack pm -> rgb666") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f000000u
<< int(QImage::Format_RGB666) << 0xff000000u;
QTest::newRow("red rgb15 -> argb32") << int(QImage::Format_RGB555) << 0xffff0000
<< int(QImage::Format_ARGB32) << 0xffff0000;
QTest::newRow("green rgb15 -> argb32") << int(QImage::Format_RGB555) << 0xff00ff00
<< int(QImage::Format_ARGB32) << 0xff00ff00;
QTest::newRow("blue rgb15 -> argb32") << int(QImage::Format_RGB555) << 0xff0000ff
<< int(QImage::Format_ARGB32) << 0xff0000ff;
QTest::newRow("semired argb32 -> rgb15") << int(QImage::Format_ARGB32) << 0x7fff0000u
<< int(QImage::Format_RGB555) << 0xffff0000;
QTest::newRow("semigreen argb32 -> rgb15") << int(QImage::Format_ARGB32) << 0x7f00ff00u
<< int(QImage::Format_RGB555) << 0xff00ff00;
QTest::newRow("semiblue argb32 -> rgb15") << int(QImage::Format_ARGB32) << 0x7f0000ffu
<< int(QImage::Format_RGB555) << 0xff0000ff;
QTest::newRow("semired pm -> rgb15") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f0000u
<< int(QImage::Format_RGB555) << 0xffff0000u;
QTest::newRow("semigreen pm -> rgb15") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f007f00u
<< int(QImage::Format_RGB555) << 0xff00ff00u;
QTest::newRow("semiblue pm -> rgb15") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f00007fu
<< int(QImage::Format_RGB555) << 0xff0000ffu;
QTest::newRow("semiwhite pm -> rgb15") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f7f7fu
<< int(QImage::Format_RGB555) << 0xffffffffu;
QTest::newRow("semiblack pm -> rgb15") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f000000u
<< int(QImage::Format_RGB555) << 0xff000000u;
QTest::newRow("red rgb32 -> argb8555") << int(QImage::Format_RGB32) << 0xffff0000
<< int(QImage::Format_ARGB8555_Premultiplied) << 0xffff0000;
QTest::newRow("green rgb32 -> argb8555") << int(QImage::Format_RGB32) << 0xff00ff00
<< int(QImage::Format_ARGB8555_Premultiplied) << 0xff00ff00;
QTest::newRow("blue rgb32 -> argb8555") << int(QImage::Format_RGB32) << 0xff0000ff
<< int(QImage::Format_ARGB8555_Premultiplied) << 0xff0000ff;
QTest::newRow("red rgb16 -> argb8555") << int(QImage::Format_RGB16) << 0xffff0000
<< int(QImage::Format_ARGB8555_Premultiplied) << 0xffff0000;
QTest::newRow("green rgb16 -> argb8555") << int(QImage::Format_RGB16) << 0xff00ff00
<< int(QImage::Format_ARGB8555_Premultiplied) << 0xff00ff00;
QTest::newRow("blue rgb16 -> argb8555") << int(QImage::Format_RGB16) << 0xff0000ff
<< int(QImage::Format_ARGB8555_Premultiplied) << 0xff0000ff;
QTest::newRow("red argb8555 -> argb32") << int(QImage::Format_ARGB8555_Premultiplied) << 0xffff0000
<< int(QImage::Format_ARGB32) << 0xffff0000;
QTest::newRow("green argb8555 -> argb32") << int(QImage::Format_ARGB8555_Premultiplied) << 0xff00ff00
<< int(QImage::Format_ARGB32) << 0xff00ff00;
QTest::newRow("blue argb8555 -> argb32") << int(QImage::Format_ARGB8555_Premultiplied) << 0xff0000ff
<< int(QImage::Format_ARGB32) << 0xff0000ff;
QTest::newRow("semired argb32 -> argb8555") << int(QImage::Format_ARGB32) << 0x7fff0000u
<< int(QImage::Format_ARGB8555_Premultiplied) << 0x7f7b0000u;
QTest::newRow("semigreen argb32 -> argb8555") << int(QImage::Format_ARGB32) << 0x7f00ff00u
<< int(QImage::Format_ARGB8555_Premultiplied) << 0x7f007b00u;
QTest::newRow("semiblue argb32 -> argb8555") << int(QImage::Format_ARGB32) << 0x7f0000ffu
<< int(QImage::Format_ARGB8555_Premultiplied) << 0x7f00007bu;
QTest::newRow("semired pm -> argb8555") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f0000u
<< int(QImage::Format_ARGB8555_Premultiplied) << 0x7f7b0000u;
QTest::newRow("semigreen pm -> argb8555") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f007f00u
<< int(QImage::Format_ARGB8555_Premultiplied) << 0x7f007b00u;
QTest::newRow("semiblue pm -> argb8555") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f00007fu
<< int(QImage::Format_ARGB8555_Premultiplied) << 0x7f00007bu;
QTest::newRow("semiwhite pm -> argb8555") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f7f7fu
<< int(QImage::Format_ARGB8555_Premultiplied) << 0x7f7b7b7bu;
QTest::newRow("semiblack pm -> argb8555") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f000000u
<< int(QImage::Format_ARGB8555_Premultiplied) << 0x7f000000u;
QTest::newRow("red rgb32 -> rgb888") << int(QImage::Format_RGB32) << 0xffff0000
<< int(QImage::Format_RGB888) << 0xffff0000;
QTest::newRow("green rgb32 -> rgb888") << int(QImage::Format_RGB32) << 0xff00ff00
<< int(QImage::Format_RGB888) << 0xff00ff00;
QTest::newRow("blue rgb32 -> rgb888") << int(QImage::Format_RGB32) << 0xff0000ff
<< int(QImage::Format_RGB888) << 0xff0000ff;
QTest::newRow("red rgb32 -> bgr888") << int(QImage::Format_RGB32) << 0xffff0000
<< int(QImage::Format_BGR888) << 0xffff0000;
QTest::newRow("green rgb32 -> bgr888") << int(QImage::Format_RGB32) << 0xff00ff00
<< int(QImage::Format_BGR888) << 0xff00ff00;
QTest::newRow("blue rgb32 -> bgr888") << int(QImage::Format_RGB32) << 0xff0000ff
<< int(QImage::Format_BGR888) << 0xff0000ff;
QTest::newRow("red rgb16 -> rgb888") << int(QImage::Format_RGB16) << 0xffff0000
<< int(QImage::Format_RGB888) << 0xffff0000;
QTest::newRow("green rgb16 -> rgb888") << int(QImage::Format_RGB16) << 0xff00ff00
<< int(QImage::Format_RGB888) << 0xff00ff00;
QTest::newRow("blue rgb16 -> rgb888") << int(QImage::Format_RGB16) << 0xff0000ff
<< int(QImage::Format_RGB888) << 0xff0000ff;
QTest::newRow("red rgb888 -> argb32") << int(QImage::Format_RGB888) << 0xffff0000
<< int(QImage::Format_ARGB32) << 0xffff0000;
QTest::newRow("green rgb888 -> argb32") << int(QImage::Format_RGB888) << 0xff00ff00
<< int(QImage::Format_ARGB32) << 0xff00ff00;
QTest::newRow("blue rgb888 -> argb32") << int(QImage::Format_RGB888) << 0xff0000ff
<< int(QImage::Format_ARGB32) << 0xff0000ff;
QTest::newRow("red bgr888 -> argb32") << int(QImage::Format_RGB888) << 0xffff0000
<< int(QImage::Format_ARGB32) << 0xffff0000;
QTest::newRow("green bgr888 -> argb32") << int(QImage::Format_RGB888) << 0xff00ff00
<< int(QImage::Format_ARGB32) << 0xff00ff00;
QTest::newRow("blue bgr888 -> argb32") << int(QImage::Format_RGB888) << 0xff0000ff
<< int(QImage::Format_ARGB32) << 0xff0000ff;
QTest::newRow("red rgb888 -> rgbx8888") << int(QImage::Format_RGB888) << 0xffff0000
<< int(QImage::Format_RGBX8888) << 0xffff0000;
QTest::newRow("green rgb888 -> rgbx8888") << int(QImage::Format_RGB888) << 0xff00ff00
<< int(QImage::Format_RGBX8888) << 0xff00ff00;
QTest::newRow("blue rgb888 -> rgbx8888") << int(QImage::Format_RGB888) << 0xff0000ff
<< int(QImage::Format_RGBX8888) << 0xff0000ff;
QTest::newRow("semired argb32 -> rgb888") << int(QImage::Format_ARGB32) << 0x7fff0000u
<< int(QImage::Format_RGB888) << 0xffff0000;
QTest::newRow("semigreen argb32 -> rgb888") << int(QImage::Format_ARGB32) << 0x7f00ff00u
<< int(QImage::Format_RGB888) << 0xff00ff00;
QTest::newRow("semiblue argb32 -> rgb888") << int(QImage::Format_ARGB32) << 0x7f0000ffu
<< int(QImage::Format_RGB888) << 0xff0000ff;
QTest::newRow("semired pm -> rgb888") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f0000u
<< int(QImage::Format_RGB888) << 0xffff0000u;
QTest::newRow("semigreen pm -> rgb888") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f007f00u
<< int(QImage::Format_RGB888) << 0xff00ff00u;
QTest::newRow("semiblue pm -> rgb888") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f00007fu
<< int(QImage::Format_RGB888) << 0xff0000ffu;
QTest::newRow("semiwhite pm -> rgb888") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f7f7fu
<< int(QImage::Format_RGB888) << 0xffffffffu;
QTest::newRow("semiblack pm -> rgb888") << int(QImage::Format_ARGB32_Premultiplied) << 0x7f000000u
<< int(QImage::Format_RGB888) << 0xff000000u;
QTest::newRow("red rgba8888 -> argb32") << int(QImage::Format_RGBA8888) << 0xffff0000
<< int(QImage::Format_ARGB32) << 0xffff0000;
QTest::newRow("green rgba8888 -> argb32") << int(QImage::Format_RGBA8888) << 0xff00ff00
<< int(QImage::Format_ARGB32) << 0xff00ff00;
QTest::newRow("blue rgba8888 -> argb32") << int(QImage::Format_RGBA8888) << 0xff0000ff
<< int(QImage::Format_ARGB32) << 0xff0000ff;
QTest::newRow("semired rgba8888 -> argb pm") << int(QImage::Format_RGBA8888) << 0x7fff0000u
<< int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f0000u;
QTest::newRow("semigreen rgba8888 -> argb pm") << int(QImage::Format_RGBA8888) << 0x7f00ff00u
<< int(QImage::Format_ARGB32_Premultiplied) << 0x7f007f00u;
QTest::newRow("semiblue rgba8888 -> argb pm") << int(QImage::Format_RGBA8888) << 0x7f0000ffu
<< int(QImage::Format_ARGB32_Premultiplied) << 0x7f00007fu;
QTest::newRow("semiwhite rgba8888 -> argb pm") << int(QImage::Format_RGBA8888) << 0x7fffffffu
<< int(QImage::Format_ARGB32_Premultiplied) << 0x7f7f7f7fu;
QTest::newRow("semiblack rgba8888 -> argb pm") << int(QImage::Format_RGBA8888) << 0x7f000000u
<< int(QImage::Format_ARGB32_Premultiplied) << 0x7f000000u;
QTest::newRow("red rgb30 -> argb32") << int(QImage::Format_RGB30) << 0xffff0000
<< int(QImage::Format_ARGB32) << 0xffff0000;
QTest::newRow("green rgb30 -> argb32") << int(QImage::Format_RGB30) << 0xff00ff00
<< int(QImage::Format_ARGB32) << 0xff00ff00;
QTest::newRow("blue rgb30 -> argb32") << int(QImage::Format_RGB30) << 0xff0000ff
<< int(QImage::Format_ARGB32) << 0xff0000ff;
QTest::newRow("semigray argb32 -> a2rgb30 pm") << int(QImage::Format_ARGB32) << 0x4c646565u
<< int(QImage::Format_A2RGB30_Premultiplied) << 0x55212222u;
QTest::newRow("white gray8 -> argb pm") << int(QImage::Format_Grayscale8) << 0xfffffeffu
<< int(QImage::Format_ARGB32_Premultiplied) << 0xfffefefeu;
QTest::newRow("gray gray8 -> argb pm") << int(QImage::Format_Grayscale8) << 0xff565557u
<< int(QImage::Format_ARGB32_Premultiplied) << 0xff555555u;
QTest::newRow("black gray8 -> argb pm") << int(QImage::Format_Grayscale8) << 0xff000100u
<< int(QImage::Format_ARGB32_Premultiplied) << 0xff000000u;
}
void tst_QImage::convertToFormat()
{
QFETCH(int, inFormat);
QFETCH(uint, inPixel);
QFETCH(int, resFormat);
QFETCH(uint, resPixel);
QImage src(32, 32, QImage::Format(inFormat));
if (inFormat == QImage::Format_Mono) {
src.setColor(0, qRgba(0,0,0,0xff));
src.setColor(1, qRgba(255,255,255,0xff));
}
for (int y=0; y<src.height(); ++y)
for (int x=0; x<src.width(); ++x)
src.setPixel(x, y, inPixel);
QImage result = src.convertToFormat(QImage::Format(resFormat));
QCOMPARE(src.width(), result.width());
QCOMPARE(src.height(), result.height());
bool same = true;
for (int y=0; y<result.height(); ++y) {
for (int x=0; x<result.width(); ++x) {
QRgb pixel = result.pixel(x, y);
same &= (pixel == resPixel);
if (!same) {
printf("expect=%08x, result=%08x\n", resPixel, pixel);
y = 100000;
break;
}
}
}
QVERIFY(same);
// repeat tests converting from an image with nonstandard stride
int dp = (src.depth() < 8 || result.depth() < 8) ? 8 : 1;
QImage src2(src.bits() + (dp*src.depth())/8,
src.width() - dp*2,
src.height() - 1, src.bytesPerLine(),
src.format());
if (src.depth() < 8)
src2.setColorTable(src.colorTable());
const QImage result2 = src2.convertToFormat(QImage::Format(resFormat));
QCOMPARE(src2.width(), result2.width());
QCOMPARE(src2.height(), result2.height());
QImage expected2(result.bits() + (dp*result.depth())/8,
result.width() - dp*2,
result.height() - 1, result.bytesPerLine(),
result.format());
if (result.depth() < 8)
expected2.setColorTable(result.colorTable());
result2.save("result2.xpm", "XPM");
expected2.save("expected2.xpm", "XPM");
QCOMPARE(result2, expected2);
QFile::remove(QLatin1String("result2.xpm"));
QFile::remove(QLatin1String("expected2.xpm"));
}
void tst_QImage::convertToFormatWithColorTable()
{
QVector<QRgb> colors(2);
colors[0] = 0xFF000000;
colors[1] = 0xFFFFFFFF;
for (int format = QImage::Format_RGB32; format < QImage::Format_Alpha8; ++format) {
QImage fromImage(10, 10, (QImage::Format)format);
QImage bitmap = fromImage.convertToFormat(QImage::Format_Mono, colors);
QVERIFY(!bitmap.isNull());
}
}
void tst_QImage::convertToFormatRgb888ToRGB32()
{
// 545 so width % 4 != 0. This ensure there is padding at the end of the scanlines
const int height = 545;
const int width = 545;
QImage source(width, height, QImage::Format_RGB888);
for (int y = 0; y < height; ++y) {
uchar *srcPixels = source.scanLine(y);
for (int x = 0; x < width * 3; ++x)
srcPixels[x] = x;
}
QImage rgb32Image = source.convertToFormat(QImage::Format_RGB888);
QCOMPARE(rgb32Image.format(), QImage::Format_RGB888);
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y)
QCOMPARE(rgb32Image.pixel(x, y), source.pixel(x, y));
}
}
void tst_QImage::createAlphaMask_data()
{
QTest::addColumn<int>("x");
QTest::addColumn<int>("y");
QTest::addColumn<int>("alpha1");
QTest::addColumn<int>("alpha2");
int alphas[] = { 0, 127, 255 };
for (unsigned a1 = 0; a1 < sizeof(alphas) / sizeof(int); ++a1) {
const QByteArray a1B = QByteArray::number(alphas[a1]);
for (unsigned a2 = 0; a2 < sizeof(alphas) / sizeof(int); ++a2) {
if (a1 == a2)
continue;
const QByteArray a2B = QByteArray::number(alphas[a2]);
for (int x=10; x<18; x+=3) {
const QByteArray xB = QByteArray::number(x);
for (int y=100; y<108; y+=3) {
const QByteArray testName = "x=" + xB + ", y=" + QByteArray::number(y)
+ ", a1=" + a1B + ", a2=" + a2B;
QTest::newRow(testName.constData()) << x << y << alphas[a1] << alphas[a2];
}
}
}
}
}
void tst_QImage::createAlphaMask()
{
QFETCH(int, x);
QFETCH(int, y);
QFETCH(int, alpha1);
QFETCH(int, alpha2);
QSize size(255, 255);
int pixelsInLines = size.width() + size.height() - 1;
int pixelsOutofLines = size.width() * size.height() - pixelsInLines;
// Generate an white image with two lines, horizontal at y and vertical at x.
// Lines have alpha of alpha2, rest has alpha of alpha1
QImage image(size, QImage::Format_ARGB32);
for (int cy=0; cy<image.height(); ++cy) {
for (int cx=0; cx<image.width(); ++cx) {
int alpha = (y == cy || x == cx) ? alpha2 : alpha1;
image.setPixel(cx, cy, qRgba(255, 255, 255, alpha));
}
}
QImage mask = image.createAlphaMask(Qt::OrderedAlphaDither);
// Sanity check...
QCOMPARE(mask.width(), image.width());
QCOMPARE(mask.height(), image.height());
// Sum up the number of pixels set for both lines and other area
int sumAlpha1 = 0;
int sumAlpha2 = 0;
for (int cy=0; cy<image.height(); ++cy) {
for (int cx=0; cx<image.width(); ++cx) {
int *alpha = (y == cy || x == cx) ? &sumAlpha2 : &sumAlpha1;
*alpha += mask.pixelIndex(cx, cy);
}
}
// Compare the set bits to whats expected for that alpha.
const int threshold = 5;
QVERIFY(qAbs(sumAlpha1 * 255 / pixelsOutofLines - alpha1) < threshold);
QVERIFY(qAbs(sumAlpha2 * 255 / pixelsInLines - alpha2) < threshold);
}
void tst_QImage::dotsPerMeterZero()
{
QImage img(100, 100, QImage::Format_RGB32);
QVERIFY(!img.isNull());
int defaultDpmX = img.dotsPerMeterX();
int defaultDpmY = img.dotsPerMeterY();
QVERIFY(defaultDpmX != 0);
QVERIFY(defaultDpmY != 0);
img.setDotsPerMeterX(0);
img.setDotsPerMeterY(0);
QCOMPARE(img.dotsPerMeterX(), defaultDpmX);
QCOMPARE(img.dotsPerMeterY(), defaultDpmY);
}
// verify that setting dotsPerMeter has an effect on the dpi.
void tst_QImage::dotsPerMeterAndDpi()
{
QImage img(100, 100, QImage::Format_RGB32);
QVERIFY(!img.isNull());
QPoint defaultLogicalDpi(img.logicalDpiX(), img.logicalDpiY());
QPoint defaultPhysicalDpi(img.physicalDpiX(), img.physicalDpiY());
img.setDotsPerMeterX(100); // set x
QCOMPARE(img.logicalDpiY(), defaultLogicalDpi.y()); // no effect on y
QCOMPARE(img.physicalDpiY(), defaultPhysicalDpi.y());
QVERIFY(img.logicalDpiX() != defaultLogicalDpi.x()); // x changed
QVERIFY(img.physicalDpiX() != defaultPhysicalDpi.x());
img.setDotsPerMeterY(200); // set y
QVERIFY(img.logicalDpiY() != defaultLogicalDpi.y()); // y changed
QVERIFY(img.physicalDpiY() != defaultPhysicalDpi.y());
}
void tst_QImage::rotate_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<int>("degrees");
QVector<int> degrees;
degrees << 0 << 90 << 180 << 270;
foreach (int d, degrees) {
const QString dB = QString::number(d);
for (int i = QImage::Format_Indexed8; i < QImage::NImageFormats; i++) {
QImage::Format format = static_cast<QImage::Format>(i);
QTest::newRow(qPrintable(dB + " " + formatToString(format))) << format << d;
}
}
}
void tst_QImage::rotate()
{
QFETCH(QImage::Format, format);
QFETCH(int, degrees);
// test if rotate90 is lossless
int w = 54;
int h = 13;
QImage original(w, h, format);
original.fill(qRgb(255,255,255));
if (format == QImage::Format_Indexed8) {
original.setColorCount(256);
for (int i = 0; i < 255; ++i)
original.setColor(i, qRgb(0, i, i));
}
if (original.colorTable().isEmpty()) {
for (int x = 0; x < w; ++x) {
original.setPixel(x,0, qRgb(x,0,128));
original.setPixel(x,h - 1, qRgb(0,255 - x,128));
}
for (int y = 0; y < h; ++y) {
original.setPixel(0, y, qRgb(y,0,255));
original.setPixel(w - 1, y, qRgb(0,255 - y,255));
}
} else {
const int n = original.colorTable().size();
for (int x = 0; x < w; ++x) {
original.setPixel(x, 0, x % n);
original.setPixel(x, h - 1, n - (x % n) - 1);
}
for (int y = 0; y < h; ++y) {
original.setPixel(0, y, y % n);
original.setPixel(w - 1, y, n - (y % n) - 1);
}
}
// original.save("rotated90_original.png", "png");
// Initialize the matrix manually (do not use rotate) to avoid rounding errors
QTransform matRotate90;
matRotate90.rotate(degrees);
QImage dest = original;
// And rotate it 4 times, then the image should be identical to the original
for (int i = 0; i < 4 ; ++i) {
dest = dest.transformed(matRotate90);
}
// Make sure they are similar in format before we compare them.
dest = dest.convertToFormat(format);
// dest.save("rotated90_result.png","png");
QCOMPARE(original, dest);
// Test with QTransform::rotate 90 also, since we trust that now
matRotate90.rotate(degrees);
dest = original;
// And rotate it 4 times, then the image should be identical to the original
for (int i = 0; i < 4 ; ++i) {
dest = dest.transformed(matRotate90);
}
// Make sure they are similar in format before we compare them.
dest = dest.convertToFormat(format);
QCOMPARE(original, dest);
}
void tst_QImage::copy()
{
// Task 99250
{
QImage img(16,16,QImage::Format_ARGB32);
img.copy(QRect(1000,1,1,1));
}
}
void tst_QImage::load()
{
const QString filePath = m_prefix + QLatin1String("image.jpg");
QImage dest(filePath);
QVERIFY(!dest.isNull());
QVERIFY(!dest.load("image_that_does_not_exist.png"));
QVERIFY(dest.isNull());
QVERIFY(dest.load(filePath));
QVERIFY(!dest.isNull());
}
void tst_QImage::loadFromData()
{
const QString filePath = m_prefix + QLatin1String("image.jpg");
QImage original(filePath);
QVERIFY(!original.isNull());
QByteArray ba;
{
QBuffer buf(&ba);
QVERIFY(buf.open(QIODevice::WriteOnly));
QVERIFY(original.save(&buf, "BMP"));
}
QVERIFY(!ba.isEmpty());
QImage dest;
QVERIFY(dest.loadFromData(ba, "BMP"));
QVERIFY(!dest.isNull());
QCOMPARE(original, dest);
QVERIFY(!dest.loadFromData(QByteArray()));
QVERIFY(dest.isNull());
}
#if !defined(QT_NO_DATASTREAM)
void tst_QImage::loadFromDataStream()
{
const QString filePath = m_prefix + QLatin1String("image.jpg");
QImage original(filePath);
QVERIFY(!original.isNull());
QByteArray ba;
{
QDataStream s(&ba, QIODevice::WriteOnly);
s << original;
}
QVERIFY(!ba.isEmpty());
QImage dest;
{
QDataStream s(&ba, QIODevice::ReadOnly);
s >> dest;
}
QVERIFY(!dest.isNull());
QCOMPARE(original, dest);
{
ba.clear();
QDataStream s(&ba, QIODevice::ReadOnly);
s >> dest;
}
QVERIFY(dest.isNull());
}
#endif // QT_NO_DATASTREAM
void tst_QImage::setPixel_data()
{
QTest::addColumn<int>("format");
QTest::addColumn<uint>("value");
QTest::addColumn<uint>("expected");
QTest::newRow("ARGB32 red") << int(QImage::Format_ARGB32)
<< 0xffff0000 << 0xffff0000;
QTest::newRow("ARGB32 green") << int(QImage::Format_ARGB32)
<< 0xff00ff00 << 0xff00ff00;
QTest::newRow("ARGB32 blue") << int(QImage::Format_ARGB32)
<< 0xff0000ff << 0xff0000ff;
QTest::newRow("RGB16 red") << int(QImage::Format_RGB16)
<< 0xffff0000 << 0xf800u;
QTest::newRow("RGB16 green") << int(QImage::Format_RGB16)
<< 0xff00ff00 << 0x07e0u;
QTest::newRow("RGB16 blue") << int(QImage::Format_RGB16)
<< 0xff0000ff << 0x001fu;
QTest::newRow("ARGB8565_Premultiplied red") << int(QImage::Format_ARGB8565_Premultiplied)
<< 0xffff0000 << 0xf800ffu;
QTest::newRow("ARGB8565_Premultiplied green") << int(QImage::Format_ARGB8565_Premultiplied)
<< 0xff00ff00 << 0x07e0ffu;
QTest::newRow("ARGB8565_Premultiplied blue") << int(QImage::Format_ARGB8565_Premultiplied)
<< 0xff0000ff << 0x001fffu;
QTest::newRow("RGB666 red") << int(QImage::Format_RGB666)
<< 0xffff0000 << 0x03f000u;
QTest::newRow("RGB666 green") << int(QImage::Format_RGB666)
<< 0xff00ff00 << 0x000fc0u;
QTest::newRow("RGB666 blue") << int(QImage::Format_RGB666)
<< 0xff0000ff << 0x00003fu;
QTest::newRow("RGB555 red") << int(QImage::Format_RGB555)
<< 0xffff0000 << 0x7c00u;
QTest::newRow("RGB555 green") << int(QImage::Format_RGB555)
<< 0xff00ff00 << 0x03e0u;
QTest::newRow("RGB555 blue") << int(QImage::Format_RGB555)
<< 0xff0000ff << 0x001fu;
QTest::newRow("ARGB8555_Premultiplied red") << int(QImage::Format_ARGB8555_Premultiplied)
<< 0xffff0000 << 0x7c00ffu;
QTest::newRow("ARGB8555_Premultiplied green") << int(QImage::Format_ARGB8555_Premultiplied)
<< 0xff00ff00 << 0x03e0ffu;
QTest::newRow("ARGB8555_Premultiplied blue") << int(QImage::Format_ARGB8555_Premultiplied)
<< 0xff0000ff << 0x001fffu;
QTest::newRow("RGB888 red") << int(QImage::Format_RGB888)
<< 0xffff0000 << 0xff0000u;
QTest::newRow("RGB888 green") << int(QImage::Format_RGB888)
<< 0xff00ff00 << 0x00ff00u;
QTest::newRow("RGB888 blue") << int(QImage::Format_RGB888)
<< 0xff0000ff << 0x0000ffu;
QTest::newRow("BGR888 red") << int(QImage::Format_BGR888)
<< 0xffff0000 << 0x0000ffu;
QTest::newRow("BGR888 green") << int(QImage::Format_BGR888)
<< 0xff00ff00 << 0x00ff00u;
QTest::newRow("BGR888 blue") << int(QImage::Format_BGR888)
<< 0xff0000ff << 0xff0000u;
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
QTest::newRow("RGBA8888 red") << int(QImage::Format_RGBA8888)
<< 0xffff0000u << 0xff0000ffu;
QTest::newRow("RGBA8888 green") << int(QImage::Format_RGBA8888)
<< 0xff00ff00u << 0x00ff00ffu;
QTest::newRow("RGBA8888 blue") << int(QImage::Format_RGBA8888)
<< 0xff0000ffu << 0x0000ffffu;
#else
QTest::newRow("RGBA8888 red") << int(QImage::Format_RGBA8888)
<< 0xffff0000u << 0xff0000ffu;
QTest::newRow("RGBA8888 green") << int(QImage::Format_RGBA8888)
<< 0xff00ff00u << 0xff00ff00u;
QTest::newRow("RGBA8888 blue") << int(QImage::Format_RGBA8888)
<< 0xff0000ffu << 0xffff0000u;
#endif
QTest::newRow("A2BGR30_Premultiplied red") << int(QImage::Format_A2BGR30_Premultiplied)
<< 0xffff0000u << 0xc00003ffu;
QTest::newRow("A2BGR30_Premultiplied green") << int(QImage::Format_A2BGR30_Premultiplied)
<< 0xff00ff00u << 0xc00ffc00u;
QTest::newRow("A2BGR30_Premultiplied blue") << int(QImage::Format_A2BGR30_Premultiplied)
<< 0xff0000ffu << 0xfff00000u;
QTest::newRow("RGB30 red") << int(QImage::Format_RGB30)
<< 0xffff0000u << 0xfff00000u;
QTest::newRow("RGB30 green") << int(QImage::Format_RGB30)
<< 0xff00ff00u << 0xc00ffc00u;
QTest::newRow("RGB30 blue") << int(QImage::Format_RGB30)
<< 0xff0000ffu << 0xc00003ffu;
}
void tst_QImage::setPixel()
{
QFETCH(int, format);
QFETCH(uint, value);
QFETCH(uint, expected);
const int w = 13;
const int h = 15;
QImage img(w, h, QImage::Format(format));
// fill image
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
img.setPixel(x, y, value);
// check pixel values
switch (format) {
case int(QImage::Format_RGB32):
case int(QImage::Format_ARGB32):
case int(QImage::Format_ARGB32_Premultiplied):
case int(QImage::Format_RGBX8888):
case int(QImage::Format_RGBA8888):
case int(QImage::Format_RGBA8888_Premultiplied):
case int(QImage::Format_A2BGR30_Premultiplied):
case int(QImage::Format_RGB30):
{
for (int y = 0; y < h; ++y) {
const quint32 *row = (const quint32*)(img.scanLine(y));
for (int x = 0; x < w; ++x) {
quint32 result = row[x];
if (result != expected)
printf("[x,y]: %d,%d, expected=%08x, result=%08x\n",
x, y, expected, result);
QCOMPARE(uint(result), expected);
}
}
break;
}
case int(QImage::Format_RGB555):
case int(QImage::Format_RGB16):
{
for (int y = 0; y < h; ++y) {
const quint16 *row = (const quint16*)(img.scanLine(y));
for (int x = 0; x < w; ++x) {
quint16 result = row[x];
if (result != expected)
printf("[x,y]: %d,%d, expected=%04x, result=%04x\n",
x, y, expected, result);
QCOMPARE(uint(result), expected);
}
}
break;
}
case int(QImage::Format_RGB666):
case int(QImage::Format_ARGB8565_Premultiplied):
case int(QImage::Format_ARGB8555_Premultiplied):
case int(QImage::Format_RGB888):
case int(QImage::Format_BGR888):
{
for (int y = 0; y < h; ++y) {
const quint24 *row = (const quint24*)(img.scanLine(y));
for (int x = 0; x < w; ++x) {
quint32 result = row[x];
if (result != expected)
printf("[x,y]: %d,%d, expected=%04x, result=%04x\n",
x, y, expected, result);
QCOMPARE(result, expected);
}
}
break;
}
default:
qFatal("Test not implemented for format %d", format);
}
}
void tst_QImage::setPixelWithAlpha_data()
{
QTest::addColumn<QImage::Format>("format");
for (int c = QImage::Format_RGB32; c < QImage::NImageFormats; ++c) {
if (c == QImage::Format_Grayscale8)
continue;
if (c == QImage::Format_Grayscale16)
continue;
if (c == QImage::Format_Alpha8)
continue;
QTest::newRow(qPrintable(formatToString(QImage::Format(c)))) << QImage::Format(c);
}
}
void tst_QImage::setPixelWithAlpha()
{
QFETCH(QImage::Format, format);
QImage image(1, 1, format);
QRgb referenceColor = qRgba(0, 170, 85, 170);
image.setPixel(0, 0, referenceColor);
if (!image.hasAlphaChannel())
referenceColor = 0xff000000 | referenceColor;
QRgb color = image.pixel(0, 0);
QCOMPARE(qRed(color) & 0xf0, qRed(referenceColor) & 0xf0);
QCOMPARE(qGreen(color) & 0xf0, qGreen(referenceColor) & 0xf0);
QCOMPARE(qBlue(color) & 0xf0, qBlue(referenceColor) & 0xf0);
QCOMPARE(qAlpha(color) & 0xf0, qAlpha(referenceColor) & 0xf0);
}
void tst_QImage::setPixelColorWithAlpha_data()
{
setPixelWithAlpha_data();
}
void tst_QImage::setPixelColorWithAlpha()
{
QFETCH(QImage::Format, format);
QImage image(1, 1, format);
image.setPixelColor(0, 0, QColor(170, 85, 255, 170));
QRgb referenceColor = qRgba(170, 85, 255, 170);
if (!image.hasAlphaChannel())
referenceColor = 0xff000000 | referenceColor;
else if (image.pixelFormat().premultiplied() == QPixelFormat::Premultiplied)
referenceColor = qPremultiply(referenceColor);
QRgb color = image.pixel(0, 0);
QCOMPARE(qRed(color) & 0xf0, qRed(referenceColor) & 0xf0);
QCOMPARE(qGreen(color) & 0xf0, qGreen(referenceColor) & 0xf0);
QCOMPARE(qBlue(color) & 0xf0, qBlue(referenceColor) & 0xf0);
QCOMPARE(qAlpha(color) & 0xf0, qAlpha(referenceColor) & 0xf0);
}
void tst_QImage::convertToFormatPreserveDotsPrMeter()
{
QImage img(100, 100, QImage::Format_ARGB32_Premultiplied);
int dpmx = 123;
int dpmy = 234;
img.setDotsPerMeterX(dpmx);
img.setDotsPerMeterY(dpmy);
img.fill(0x12345678);
img = img.convertToFormat(QImage::Format_RGB32);
QCOMPARE(img.dotsPerMeterX(), dpmx);
QCOMPARE(img.dotsPerMeterY(), dpmy);
}
void tst_QImage::convertToFormatPreserveText()
{
QImage img(100, 100, QImage::Format_ARGB32_Premultiplied);
img.setText("foo", "bar");
img.setText("foo2", "bar2");
img.fill(0x12345678);
QStringList listResult;
listResult << "foo" << "foo2";
QString result = "foo: bar\n\nfoo2: bar2";
QImage imgResult1 = img.convertToFormat(QImage::Format_RGB32);
QCOMPARE(imgResult1.text(), result);
QCOMPARE(imgResult1.textKeys(), listResult);
QVector<QRgb> colorTable(4);
for (int i = 0; i < 4; ++i)
colorTable[i] = QRgb(42);
QImage imgResult2 = img.convertToFormat(QImage::Format_MonoLSB,
colorTable);
QCOMPARE(imgResult2.text(), result);
QCOMPARE(imgResult2.textKeys(), listResult);
}
void tst_QImage::defaultColorTable_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<int>("createdDataCount");
QTest::addColumn<int>("externalDataCount");
// For historical reasons, internally created mono images get a default colormap.
// Externally created and Indexed8 images do not.
QTest::newRow("Mono") << QImage::Format_Mono << 2 << 0;
QTest::newRow("MonoLSB") << QImage::Format_MonoLSB << 2 << 0;
QTest::newRow("Indexed8") << QImage::Format_Indexed8 << 0 << 0;
QTest::newRow("ARGB32_PM") << QImage::Format_A2BGR30_Premultiplied << 0 << 0;
}
void tst_QImage::defaultColorTable()
{
QFETCH(QImage::Format, format);
QFETCH(int, createdDataCount);
QFETCH(int, externalDataCount);
QImage img1(1, 1, format);
QCOMPARE(img1.colorCount(), createdDataCount);
QCOMPARE(img1.colorTable().size(), createdDataCount);
quint32 buf;
QImage img2(reinterpret_cast<uchar *>(&buf), 1, 1, format);
QCOMPARE(img2.colorCount(), externalDataCount);
QImage nullImg(0, 0, format);
QCOMPARE(nullImg.colorCount(), 0);
}
void tst_QImage::setColorCount()
{
QImage img(0, 0, QImage::Format_Indexed8);
QTest::ignoreMessage(QtWarningMsg, "QImage::setColorCount: null image");
img.setColorCount(256);
QCOMPARE(img.colorCount(), 0);
}
void tst_QImage::setColor()
{
QImage img(0, 0, QImage::Format_Indexed8);
img.setColor(0, qRgba(18, 219, 108, 128));
QCOMPARE(img.colorCount(), 0);
QImage img2(1, 1, QImage::Format_Indexed8);
img2.setColor(0, qRgba(18, 219, 108, 128));
QCOMPARE(img2.colorCount(), 1);
}
/* Just some sanity checking that we don't draw outside the buffer of
* the image. Hopefully this will create crashes or at least some
* random test fails when broken.
*/
void tst_QImage::rasterClipping()
{
QImage image(10, 10, QImage::Format_RGB32);
image.fill(0xffffffff);
QPainter p(&image);
p.drawLine(-1000, 5, 5, 5);
p.drawLine(-1000, 5, 1000, 5);
p.drawLine(5, 5, 1000, 5);
p.drawLine(5, -1000, 5, 5);
p.drawLine(5, -1000, 5, 1000);
p.drawLine(5, 5, 5, 1000);
p.setBrush(Qt::red);
p.drawEllipse(3, 3, 4, 4);
p.drawEllipse(-100, -100, 210, 210);
p.drawEllipse(-1000, 0, 2010, 2010);
p.drawEllipse(0, -1000, 2010, 2010);
p.drawEllipse(-2010, -1000, 2010, 2010);
p.drawEllipse(-1000, -2010, 2010, 2010);
QVERIFY(true);
}
// Tests the new QPoint overloads in QImage in Qt 4.2
void tst_QImage::pointOverloads()
{
QImage image(100, 100, QImage::Format_RGB32);
image.fill(0xff00ff00);
// IsValid
QVERIFY(image.valid(QPoint(0, 0)));
QVERIFY(image.valid(QPoint(99, 0)));
QVERIFY(image.valid(QPoint(0, 99)));
QVERIFY(image.valid(QPoint(99, 99)));
QVERIFY(!image.valid(QPoint(50, -1))); // outside on the top
QVERIFY(!image.valid(QPoint(50, 100))); // outside on the bottom
QVERIFY(!image.valid(QPoint(-1, 50))); // outside on the left
QVERIFY(!image.valid(QPoint(100, 50))); // outside on the right
// Test the pixel setter
image.setPixel(QPoint(10, 10), 0xff0000ff);
QCOMPARE(image.pixel(10, 10), 0xff0000ff);
// pixel getter
QCOMPARE(image.pixel(QPoint(10, 10)), 0xff0000ff);
// pixelIndex()
QImage indexed = image.convertToFormat(QImage::Format_Indexed8);
QCOMPARE(indexed.pixelIndex(10, 10), indexed.pixelIndex(QPoint(10, 10)));
}
void tst_QImage::destructor()
{
QPolygon poly(6);
poly.setPoint(0,-1455, 1435);
QImage image(100, 100, QImage::Format_RGB32);
QPainter ptPix(&image);
ptPix.setPen(Qt::black);
ptPix.setBrush(Qt::black);
ptPix.drawPolygon(poly, Qt::WindingFill);
ptPix.end();
}
/* XPM */
static const char *monoPixmap[] = {
/* width height ncolors chars_per_pixel */
"4 4 2 1",
"x c #000000",
". c #ffffff",
/* pixels */
"xxxx",
"x..x",
"x..x",
"xxxx"
};
#ifndef QT_NO_IMAGE_HEURISTIC_MASK
void tst_QImage::createHeuristicMask()
{
QImage img(monoPixmap);
img = img.convertToFormat(QImage::Format_MonoLSB);
QImage mask = img.createHeuristicMask();
QImage newMask = mask.convertToFormat(QImage::Format_ARGB32_Premultiplied);
// line 2
QVERIFY(newMask.pixel(0,1) != newMask.pixel(1,1));
QCOMPARE(newMask.pixel(1,1), newMask.pixel(2,1));
QVERIFY(newMask.pixel(2,1) != newMask.pixel(3,1));
// line 3
QVERIFY(newMask.pixel(0,2) != newMask.pixel(1,2));
QCOMPARE(newMask.pixel(1,2), newMask.pixel(2,2));
QVERIFY(newMask.pixel(2,2) != newMask.pixel(3,2));
}
#endif
void tst_QImage::cacheKey()
{
QImage image1(2, 2, QImage::Format_RGB32);
qint64 image1_key = image1.cacheKey();
QImage image2 = image1;
QCOMPARE(image2.cacheKey(), image1.cacheKey());
image2.detach();
QVERIFY(image2.cacheKey() != image1.cacheKey());
QCOMPARE(image1.cacheKey(), image1_key);
}
void tst_QImage::smoothScale()
{
unsigned int data[2] = { qRgba(0, 0, 0, 0), qRgba(128, 128, 128, 128) };
QImage imgX((unsigned char *)data, 2, 1, QImage::Format_ARGB32_Premultiplied);
QImage imgY((unsigned char *)data, 1, 2, QImage::Format_ARGB32_Premultiplied);
QImage scaledX = imgX.scaled(QSize(4, 1), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QImage scaledY = imgY.scaled(QSize(1, 4), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
uint *scaled[2] = {
(unsigned int *)scaledX.bits(),
(unsigned int *)scaledY.bits()
};
int expected[4] = { 0, 32, 96, 128 };
for (int image = 0; image < 2; ++image) {
for (int index = 0; index < 4; ++index) {
for (int component = 0; component < 4; ++component) {
int pixel = scaled[image][index];
int val = (pixel >> (component * 8)) & 0xff;
QCOMPARE(val, expected[index]);
}
}
}
}
// test area sampling
void tst_QImage::smoothScale2_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<int>("size");
int sizes[] = { 2, 3, 4, 6, 7, 8, 10, 16, 20, 32, 40, 64, 100, 101, 128, 0 };
QImage::Format formats[] = { QImage::Format_RGB32, QImage::Format_ARGB32_Premultiplied, QImage::Format_RGBX64, QImage::Format_RGBA64_Premultiplied, QImage::Format_Invalid };
for (int j = 0; formats[j] != QImage::Format_Invalid; ++j) {
QString formatstr = formatToString(formats[j]);
for (int i = 0; sizes[i] != 0; ++i) {
const QByteArray sizeB = QByteArray::number(sizes[i]);
QTest::newRow(QString("%1 %2x%2").arg(formatstr).arg(sizes[i]).toUtf8()) << formats[j] << sizes[i];
}
}
}
void tst_QImage::smoothScale2()
{
QFETCH(QImage::Format, format);
QFETCH(int, size);
bool opaque = (format == QImage::Format_RGB32 || format == QImage::Format_RGBX64);
QRgb expected = opaque ? qRgb(63, 127, 255) : qRgba(31, 63, 127, 127);
QImage img(size, size, format);
img.fill(expected);
// scale x down, y down
QImage scaled = img.scaled(QSize(1, 1), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QRgb pixel = scaled.pixel(0, 0);
QCOMPARE(qAlpha(pixel), qAlpha(expected));
QCOMPARE(qRed(pixel), qRed(expected));
QCOMPARE(qGreen(pixel), qGreen(expected));
QCOMPARE(qBlue(pixel), qBlue(expected));
// scale x down, y up
scaled = img.scaled(QSize(1, size * 2), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
for (int y = 0; y < scaled.height(); ++y) {
pixel = scaled.pixel(0, y);
QCOMPARE(qAlpha(pixel), qAlpha(expected));
QCOMPARE(qRed(pixel), qRed(expected));
QCOMPARE(qGreen(pixel), qGreen(expected));
QCOMPARE(qBlue(pixel), qBlue(expected));
}
// scale x up, y down
scaled = img.scaled(QSize(size * 2, 1), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
for (int x = 0; x < scaled.width(); ++x) {
pixel = scaled.pixel(x, 0);
QCOMPARE(qAlpha(pixel), qAlpha(expected));
QCOMPARE(qRed(pixel), qRed(expected));
QCOMPARE(qGreen(pixel), qGreen(expected));
QCOMPARE(qBlue(pixel), qBlue(expected));
}
// scale x up
scaled = img.scaled(QSize(size, size * 2), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
for (int y = 0; y < scaled.height(); ++y) {
for (int x = 0; x < scaled.width(); ++x) {
pixel = scaled.pixel(x, y);
QCOMPARE(qAlpha(pixel), qAlpha(expected));
QCOMPARE(qRed(pixel), qRed(expected));
QCOMPARE(qGreen(pixel), qGreen(expected));
QCOMPARE(qBlue(pixel), qBlue(expected));
}
}
// scale y up
scaled = img.scaled(QSize(size * 2, size), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
for (int y = 0; y < scaled.height(); ++y) {
for (int x = 0; x < scaled.width(); ++x) {
pixel = scaled.pixel(x, y);
QCOMPARE(qAlpha(pixel), qAlpha(expected));
QCOMPARE(qRed(pixel), qRed(expected));
QCOMPARE(qGreen(pixel), qGreen(expected));
QCOMPARE(qBlue(pixel), qBlue(expected));
}
}
// scale x up, y up
scaled = img.scaled(QSize(size * 2, size * 2), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
for (int y = 0; y < scaled.height(); ++y) {
for (int x = 0; x < scaled.width(); ++x) {
pixel = scaled.pixel(x, y);
QCOMPARE(qAlpha(pixel), qAlpha(expected));
QCOMPARE(qRed(pixel), qRed(expected));
QCOMPARE(qGreen(pixel), qGreen(expected));
QCOMPARE(qBlue(pixel), qBlue(expected));
}
}
}
static inline int rand8()
{
return QRandomGenerator::global()->bounded(256);
}
void tst_QImage::smoothScale3_data()
{
QTest::addColumn<QImage>("img");
QTest::addColumn<qreal>("scale_x");
QTest::addColumn<qreal>("scale_y");
QImage img(128, 128, QImage::Format_RGB32);
for (int y = 0; y < img.height(); ++y) {
for (int x = 0; x < img.width(); ++x) {
const int red = rand8();
const int green = rand8();
const int blue = rand8();
const int alpha = 255;
img.setPixel(x, y, qRgba(red, green, blue, alpha));
}
}
QTest::newRow("(0.5, 0.5)") << img << qreal(0.5) << qreal(0.5);
QTest::newRow("(0.5, 1.0)") << img << qreal(0.5) << qreal(1.0);
QTest::newRow("(1.0, 0.5)") << img << qreal(1.0) << qreal(0.5);
QTest::newRow("(0.5, 2.0)") << img << qreal(0.5) << qreal(2.0);
QTest::newRow("(1.0, 2.0)") << img << qreal(1.0) << qreal(2.0);
QTest::newRow("(2.0, 0.5)") << img << qreal(2.0) << qreal(0.5);
QTest::newRow("(2.0, 1.0)") << img << qreal(2.0) << qreal(1.0);
QTest::newRow("(2.0, 2.0)") << img << qreal(2) << qreal(2);
}
// compares img.scale against the bilinear filtering used by QPainter
void tst_QImage::smoothScale3()
{
QFETCH(QImage, img);
QFETCH(qreal, scale_x);
QFETCH(qreal, scale_y);
QImage a = img.scaled(img.width() * scale_x, img.height() * scale_y, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QImage b(a.size(), a.format());
b.fill(0x0);
QPainter p(&b);
p.setRenderHint(QPainter::SmoothPixmapTransform);
p.scale(scale_x, scale_y);
p.drawImage(0, 0, img);
p.end();
int err = 0;
for (int y = 0; y < a.height(); ++y) {
for (int x = 0; x < a.width(); ++x) {
QRgb ca = a.pixel(x, y);
QRgb cb = b.pixel(x, y);
// tolerate a little bit of rounding errors
int tolerance = 3;
bool r = true;
r &= qAbs(qRed(ca) - qRed(cb)) <= tolerance;
r &= qAbs(qGreen(ca) - qGreen(cb)) <= tolerance;
r &= qAbs(qBlue(ca) - qBlue(cb)) <= tolerance;
if (!r)
err++;
}
}
QCOMPARE(err, 0);
}
// Tests smooth upscale is smooth
void tst_QImage::smoothScale4_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::newRow("RGB32") << QImage::Format_RGB32;
#if QT_CONFIG(raster_64bit)
QTest::newRow("RGBx64") << QImage::Format_RGBX64;
#endif
}
void tst_QImage::smoothScale4()
{
QFETCH(QImage::Format, format);
QImage img(4, 4, format);
for (int y = 0; y < 4; ++y) {
for (int x = 0; x < 4; ++x) {
img.setPixel(x, y, qRgb(x * 255 / 3, y * 255 / 3, 0));
}
}
QImage scaled = img.scaled(37, 23, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QCOMPARE(scaled.format(), format);
for (int y = 0; y < scaled.height(); ++y) {
for (int x = 0; x < scaled.width(); ++x) {
if (x > 0)
QVERIFY(scaled.pixelColor(x, y).redF() >= scaled.pixelColor(x - 1, y).redF());
if (y > 0)
QVERIFY(scaled.pixelColor(x, y).greenF() >= scaled.pixelColor(x, y - 1).greenF());
}
}
}
void tst_QImage::smoothScaleBig()
{
int bigValue = 200000;
QImage tall(4, bigValue, QImage::Format_ARGB32);
tall.fill(0x0);
QImage wide(bigValue, 4, QImage::Format_ARGB32);
wide.fill(0x0);
QImage tallScaled = tall.scaled(4, tall.height() / 4, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QImage wideScaled = wide.scaled(wide.width() / 4, 4, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QCOMPARE(tallScaled.pixel(0, 0), QRgb(0x0));
QCOMPARE(wideScaled.pixel(0, 0), QRgb(0x0));
}
void tst_QImage::smoothScaleAlpha()
{
QImage src(128, 128, QImage::Format_ARGB32_Premultiplied);
src.fill(0x0);
QPainter srcPainter(&src);
srcPainter.setPen(Qt::NoPen);
srcPainter.setBrush(Qt::white);
srcPainter.drawEllipse(QRect(QPoint(), src.size()));
srcPainter.end();
QImage dst(32, 32, QImage::Format_ARGB32_Premultiplied);
dst.fill(0xffffffff);
QImage expected = dst;
QPainter dstPainter(&dst);
dstPainter.drawImage(0, 0, src.scaled(dst.size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
dstPainter.end();
QCOMPARE(dst, expected);
}
static int count(const QImage &img, int x, int y, int dx, int dy, QRgb pixel)
{
int i = 0;
while (x >= 0 && x < img.width() && y >= 0 && y < img.height()) {
i += (img.pixel(x, y) == pixel);
x += dx;
y += dy;
}
return i;
}
const int transformed_image_width = 128;
const int transformed_image_height = 128;
void tst_QImage::transformed_data()
{
QTest::addColumn<QTransform>("transform");
{
QTransform transform;
transform.translate(10.4, 10.4);
QTest::newRow("Translate") << transform;
}
{
QTransform transform;
transform.scale(1.5, 1.5);
QTest::newRow("Scale") << transform;
}
{
QTransform transform;
transform.rotate(30);
QTest::newRow("Rotate 30") << transform;
}
{
QTransform transform;
transform.rotate(90);
QTest::newRow("Rotate 90") << transform;
}
{
QTransform transform;
transform.rotate(180);
QTest::newRow("Rotate 180") << transform;
}
{
QTransform transform;
transform.rotate(270);
QTest::newRow("Rotate 270") << transform;
}
{
QTransform transform;
transform.translate(transformed_image_width/2, transformed_image_height/2);
transform.rotate(155, Qt::XAxis);
transform.translate(-transformed_image_width/2, -transformed_image_height/2);
QTest::newRow("Perspective 1") << transform;
}
{
QTransform transform;
transform.rotate(155, Qt::XAxis);
transform.translate(-transformed_image_width/2, -transformed_image_height/2);
QTest::newRow("Perspective 2") << transform;
}
}
void tst_QImage::transformed()
{
QFETCH(QTransform, transform);
QImage img(transformed_image_width, transformed_image_height, QImage::Format_ARGB32_Premultiplied);
QPainter p(&img);
p.fillRect(0, 0, img.width(), img.height(), Qt::red);
p.drawRect(0, 0, img.width()-1, img.height()-1);
p.end();
QImage transformed = img.transformed(transform, Qt::SmoothTransformation);
// all borders should have touched pixels
QVERIFY(count(transformed, 0, 0, 1, 0, 0x0) < transformed.width());
QVERIFY(count(transformed, 0, 0, 0, 1, 0x0) < transformed.height());
QVERIFY(count(transformed, 0, img.height() - 1, 1, 0, 0x0) < transformed.width());
QVERIFY(count(transformed, img.width() - 1, 0, 0, 1, 0x0) < transformed.height());
QImage transformedPadded(transformed.width() + 2, transformed.height() + 2, img.format());
transformedPadded.fill(0x0);
p.begin(&transformedPadded);
p.setRenderHint(QPainter::SmoothPixmapTransform);
p.setRenderHint(QPainter::Antialiasing);
p.setTransform(transformed.trueMatrix(transform, img.width(), img.height()) * QTransform().translate(1, 1));
p.drawImage(0, 0, img);
p.end();
// no borders should have touched pixels since we have a one-pixel padding
QCOMPARE(count(transformedPadded, 0, 0, 1, 0, 0x0), transformedPadded.width());
QCOMPARE(count(transformedPadded, 0, 0, 0, 1, 0x0), transformedPadded.height());
QCOMPARE(count(transformedPadded, 0, transformedPadded.height() - 1, 1, 0, 0x0), transformedPadded.width());
QCOMPARE(count(transformedPadded, transformedPadded.width() - 1, 0, 0, 1, 0x0), transformedPadded.height());
}
void tst_QImage::transformed2()
{
QImage img(3, 3, QImage::Format_Mono);
QPainter p(&img);
p.fillRect(0, 0, 3, 3, Qt::white);
p.fillRect(0, 0, 3, 3, Qt::Dense4Pattern);
p.end();
QTransform transform;
transform.scale(3, 3);
QImage expected(9, 9, QImage::Format_Mono);
p.begin(&expected);
p.fillRect(0, 0, 9, 9, Qt::white);
p.setBrush(Qt::black);
p.setPen(Qt::NoPen);
p.drawRect(3, 0, 3, 3);
p.drawRect(0, 3, 3, 3);
p.drawRect(6, 3, 3, 3);
p.drawRect(3, 6, 3, 3);
p.end();
{
QImage actual = img.transformed(transform);
QCOMPARE(actual.format(), expected.format());
QCOMPARE(actual.size(), expected.size());
QCOMPARE(actual, expected);
}
{
transform.rotate(-90);
QImage actual = img.transformed(transform);
QCOMPARE(actual.convertToFormat(QImage::Format_ARGB32_Premultiplied),
expected.convertToFormat(QImage::Format_ARGB32_Premultiplied));
}
}
void tst_QImage::scaled()
{
QImage img(102, 3, QImage::Format_Mono);
QPainter p(&img);
p.fillRect(0, 0, img.width(), img.height(), Qt::white);
p.end();
QImage scaled = img.scaled(1994, 10);
QImage expected(1994, 10, QImage::Format_Mono);
p.begin(&expected);
p.fillRect(0, 0, expected.width(), expected.height(), Qt::white);
p.end();
QCOMPARE(scaled, expected);
}
void tst_QImage::paintEngine()
{
QImage img;
QPaintEngine *engine;
{
QImage temp(100, 100, QImage::Format_RGB32);
temp.fill(0xff000000);
QPainter p(&temp);
p.fillRect(80,80,10,10,Qt::blue);
p.end();
img = temp;
engine = temp.paintEngine();
}
{
QPainter p(&img);
p.fillRect(80,10,10,10,Qt::yellow);
p.end();
}
QImage expected(100, 100, QImage::Format_RGB32);
expected.fill(0xff000000);
QPainter p(&expected);
p.fillRect(80,80,10,10,Qt::blue);
p.fillRect(80,10,10,10,Qt::yellow);
p.end();
QCOMPARE(engine, img.paintEngine());
QCOMPARE(img, expected);
{
QImage img1(16, 16, QImage::Format_ARGB32);
QImage img2 = img1;
QVERIFY(img2.paintEngine());
}
}
void tst_QImage::setAlphaChannelWhilePainting()
{
QImage image(100, 100, QImage::Format_ARGB32);
image.fill(Qt::black);
QPainter p(&image);
image.setAlphaChannel(image.createMaskFromColor(QColor(Qt::black).rgb(), Qt::MaskInColor));
}
// See task 240047 for details
void tst_QImage::smoothScaledSubImage()
{
QImage original(128, 128, QImage::Format_RGB32);
QPainter p(&original);
p.fillRect(0, 0, 64, 128, Qt::black);
p.fillRect(64, 0, 64, 128, Qt::white);
p.end();
QImage subimage(((const QImage &) original).bits(), 32, 32, original.bytesPerLine(), QImage::Format_RGB32); // only in the black part of the source...
QImage scaled = subimage.scaled(8, 8, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
for (int y=0; y<scaled.height(); ++y)
for (int x=0; x<scaled.width(); ++x)
QCOMPARE(scaled.pixel(x, y), 0xff000000);
}
void tst_QImage::nullSize_data()
{
QTest::addColumn<QImage>("image");
QTest::newRow("null image") << QImage();
QTest::newRow("zero-size image") << QImage(0, 0, QImage::Format_RGB32);
}
void tst_QImage::nullSize()
{
QFETCH(QImage, image);
QCOMPARE(image.isNull(), true);
QCOMPARE(image.width(), image.size().width());
QCOMPARE(image.height(), image.size().height());
}
void tst_QImage::premultipliedAlphaConsistency()
{
QImage img(256, 1, QImage::Format_ARGB32);
for (int x = 0; x < 256; ++x)
img.setPixel(x, 0, (x << 24) | 0xffffff);
QImage converted = img.convertToFormat(QImage::Format_ARGB8565_Premultiplied);
QImage pm32 = converted.convertToFormat(QImage::Format_ARGB32_Premultiplied);
for (int i = 0; i < pm32.width(); ++i) {
QRgb pixel = pm32.pixel(i, 0);
QVERIFY(qRed(pixel) <= qAlpha(pixel));
QVERIFY(qGreen(pixel) <= qAlpha(pixel));
QVERIFY(qBlue(pixel) <= qAlpha(pixel));
}
}
void tst_QImage::compareIndexed()
{
QImage img(256, 1, QImage::Format_Indexed8);
QVector<QRgb> colorTable(256);
for (int i = 0; i < 256; ++i)
colorTable[i] = qRgb(i, i, i);
img.setColorTable(colorTable);
for (int i = 0; i < 256; ++i) {
img.setPixel(i, 0, i);
}
QImage imgInverted(256, 1, QImage::Format_Indexed8);
QVector<QRgb> invertedColorTable(256);
for (int i = 0; i < 256; ++i)
invertedColorTable[255-i] = qRgb(i, i, i);
imgInverted.setColorTable(invertedColorTable);
for (int i = 0; i < 256; ++i) {
imgInverted.setPixel(i, 0, (255-i));
}
QCOMPARE(img, imgInverted);
}
void tst_QImage::fillColor_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<Qt::GlobalColor>("color");
QTest::addColumn<uint>("pixelValue");
QTest::newRow("Mono, color0") << QImage::Format_Mono << Qt::color0 << 0u;
QTest::newRow("Mono, color1") << QImage::Format_Mono << Qt::color1 << 1u;
QTest::newRow("MonoLSB, color0") << QImage::Format_MonoLSB << Qt::color0 << 0u;
QTest::newRow("MonoLSB, color1") << QImage::Format_MonoLSB << Qt::color1 << 1u;
const char *names[] = {
"Indexed8",
"RGB32",
"ARGB32",
"ARGB32pm",
"RGB16",
"ARGB8565pm",
"RGB666",
"ARGB6666pm",
"RGB555",
"ARGB8555pm",
"RGB888",
"RGB444",
"ARGB4444pm",
"RGBx8888",
"RGBA8888pm",
"BGR30",
"A2RGB30pm",
0
};
QImage::Format formats[] = {
QImage::Format_Indexed8,
QImage::Format_RGB32,
QImage::Format_ARGB32,
QImage::Format_ARGB32_Premultiplied,
QImage::Format_RGB16,
QImage::Format_ARGB8565_Premultiplied,
QImage::Format_RGB666,
QImage::Format_ARGB6666_Premultiplied,
QImage::Format_RGB555,
QImage::Format_ARGB8555_Premultiplied,
QImage::Format_RGB888,
QImage::Format_RGB444,
QImage::Format_ARGB4444_Premultiplied,
QImage::Format_RGBX8888,
QImage::Format_RGBA8888_Premultiplied,
QImage::Format_BGR30,
QImage::Format_A2RGB30_Premultiplied,
};
for (int i=0; names[i] != 0; ++i) {
QByteArray name;
name.append(names[i]).append(", ");
QTest::newRow(QByteArray(name).append("black").constData()) << formats[i] << Qt::black << 0xff000000;
QTest::newRow(QByteArray(name).append("white").constData()) << formats[i] << Qt::white << 0xffffffff;
QTest::newRow(QByteArray(name).append("red").constData()) << formats[i] << Qt::red << 0xffff0000;
QTest::newRow(QByteArray(name).append("green").constData()) << formats[i] << Qt::green << 0xff00ff00;
QTest::newRow(QByteArray(name).append("blue").constData()) << formats[i] << Qt::blue << 0xff0000ff;
}
QTest::newRow("RGB16, transparent") << QImage::Format_RGB16 << Qt::transparent << 0xff000000;
QTest::newRow("RGB32, transparent") << QImage::Format_RGB32 << Qt::transparent << 0xff000000;
QTest::newRow("ARGB32, transparent") << QImage::Format_ARGB32 << Qt::transparent << 0x00000000u;
QTest::newRow("ARGB32pm, transparent") << QImage::Format_ARGB32_Premultiplied << Qt::transparent << 0x00000000u;
QTest::newRow("RGBA8888pm, transparent") << QImage::Format_RGBA8888_Premultiplied << Qt::transparent << 0x00000000u;
QTest::newRow("A2RGB30pm, transparent") << QImage::Format_A2RGB30_Premultiplied << Qt::transparent << 0x00000000u;
}
void tst_QImage::fillColor()
{
QFETCH(QImage::Format, format);
QFETCH(Qt::GlobalColor, color);
QFETCH(uint, pixelValue);
QImage image(1, 1, format);
if (image.depth() == 8) {
QVector<QRgb> table;
table << 0xff000000;
table << 0xffffffff;
table << 0xffff0000;
table << 0xff00ff00;
table << 0xff0000ff;
image.setColorTable(table);
}
image.fill(color);
if (image.depth() == 1) {
QCOMPARE(image.pixelIndex(0, 0), (int) pixelValue);
} else {
QCOMPARE(image.pixel(0, 0), pixelValue);
}
image.fill(QColor(color));
if (image.depth() == 1) {
QCOMPARE(image.pixelIndex(0, 0), (int) pixelValue);
} else {
QCOMPARE(image.pixel(0, 0), pixelValue);
}
}
void tst_QImage::fillColorWithAlpha_data()
{
setPixelWithAlpha_data();
}
void tst_QImage::fillColorWithAlpha()
{
QFETCH(QImage::Format, format);
QImage image(1, 1, format);
image.fill(QColor(255, 170, 85, 170));
QRgb referenceColor = qRgba(255, 170, 85, 170);
if (!image.hasAlphaChannel())
referenceColor = 0xff000000 | referenceColor;
else if (image.pixelFormat().premultiplied() == QPixelFormat::Premultiplied)
referenceColor = qPremultiply(referenceColor);
QRgb color = image.pixel(0, 0);
QCOMPARE(qRed(color) & 0xf0, qRed(referenceColor) & 0xf0);
QCOMPARE(qGreen(color) & 0xf0, qGreen(referenceColor) & 0xf0);
QCOMPARE(qBlue(color) & 0xf0, qBlue(referenceColor) & 0xf0);
QCOMPARE(qAlpha(color) & 0xf0, qAlpha(referenceColor) & 0xf0);
}
void tst_QImage::fillRGB888()
{
QImage expected(1, 1, QImage::Format_RGB888);
QImage actual(1, 1, QImage::Format_RGB888);
for (int c = Qt::black; c < Qt::transparent; ++c) {
QColor color = QColor(Qt::GlobalColor(c));
expected.fill(color);
actual.fill(color.rgba());
QCOMPARE(actual.pixel(0, 0), expected.pixel(0, 0));
}
}
void tst_QImage::fillPixel_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<uint>("color");
QTest::addColumn<uint>("pixelValue");
QTest::newRow("RGB16, transparent") << QImage::Format_RGB16 << 0x0u << 0xff000000u;
QTest::newRow("RGB32, transparent") << QImage::Format_RGB32 << 0x0u << 0xff000000u;
QTest::newRow("RGB444, transparent") << QImage::Format_RGB444 << 0x0u << 0xff000000u;
QTest::newRow("RGB666, transparent") << QImage::Format_RGB666 << 0x0u << 0xff000000u;
QTest::newRow("RGBx8888, transparent") << QImage::Format_RGBX8888 << 0x0u << 0xff000000u;
QTest::newRow("ARGB32, transparent") << QImage::Format_ARGB32 << 0x0u << 0x00000000u;
QTest::newRow("ARGB32pm, transparent") << QImage::Format_ARGB32_Premultiplied << 0x0u << 0x00000000u;
QTest::newRow("RGBA8888pm, transparent") << QImage::Format_RGBA8888_Premultiplied << 0x0u << 0x00000000u;
QTest::newRow("Grayscale8, transparent") << QImage::Format_Grayscale8 << 0x0u << 0xff000000u;
QTest::newRow("Alpha8, transparent") << QImage::Format_Alpha8 << 0x0u << 0x00000000u;
QTest::newRow("RGB16, red") << QImage::Format_RGB16 << (uint)qConvertRgb32To16(0xffff0000) << 0xffff0000u;
QTest::newRow("RGB32, red") << QImage::Format_RGB32 << 0xffff0000u << 0xffff0000u;
QTest::newRow("ARGB32, red") << QImage::Format_ARGB32 << 0xffff0000u << 0xffff0000u;
QTest::newRow("RGBA8888, red") << QImage::Format_RGBA8888 << 0xff0000ffu << 0xffff0000u;
QTest::newRow("Grayscale8, grey") << QImage::Format_Grayscale8 << 0x80u << 0xff808080u;
QTest::newRow("RGB32, semi-red") << QImage::Format_RGB32 << 0x80ff0000u << 0xffff0000u;
QTest::newRow("ARGB32, semi-red") << QImage::Format_ARGB32 << 0x80ff0000u << 0x80ff0000u;
QTest::newRow("ARGB32pm, semi-red") << QImage::Format_ARGB32 << 0x80800000u << 0x80800000u;
QTest::newRow("RGBA8888pm, semi-red") << QImage::Format_RGBA8888_Premultiplied << 0x80000080u << 0x80800000u;
QTest::newRow("Alpha8, semi-transparent") << QImage::Format_Alpha8 << 0x80u << 0x80000000u;
}
void tst_QImage::fillPixel()
{
QFETCH(QImage::Format, format);
QFETCH(uint, color);
QFETCH(uint, pixelValue);
QImage image(1, 1, format);
image.fill(color);
QCOMPARE(image.pixel(0, 0), pixelValue);
if (image.depth() == 8)
QCOMPARE(*(const uchar *)image.constBits(), color);
}
void tst_QImage::rgbSwapped_data()
{
QTest::addColumn<QImage::Format>("format");
for (int i = QImage::Format_Indexed8; i < QImage::NImageFormats; ++i) {
if (i == QImage::Format_Alpha8
|| i == QImage::Format_Grayscale8
|| i == QImage::Format_Grayscale16) {
continue;
}
QTest::addRow("%s", formatToString(QImage::Format(i)).data()) << QImage::Format(i);
}
}
void tst_QImage::rgbSwapped()
{
QFETCH(QImage::Format, format);
QImage image(100, 1, format);
image.fill(0);
QVector<QColor> testColor(image.width());
for (int i = 0; i < image.width(); ++i)
testColor[i] = QColor(i, 10 + i, 20 + i * 2, 30 + i);
if (format != QImage::Format_Indexed8) {
QPainter p(&image);
p.setCompositionMode(QPainter::CompositionMode_Source);
for (int i = 0; i < image.width(); ++i)
p.fillRect(QRect(i, 0, 1, 1), testColor[i].rgb());
} else {
image.setColorCount(image.width());
for (int i = 0; i < image.width(); ++i) {
image.setColor(0, testColor[i].rgba());
image.setPixel(i, 0, i);
}
}
QImage imageSwapped = image.rgbSwapped();
for (int i = 0; i < image.width(); ++i) {
QColor referenceColor = QColor(image.pixel(i, 0));
QColor swappedColor = QColor(imageSwapped.pixel(i, 0));
QCOMPARE(swappedColor.alpha(), referenceColor.alpha());
QCOMPARE(swappedColor.red(), referenceColor.blue());
QCOMPARE(swappedColor.green(), referenceColor.green());
QCOMPARE(swappedColor.blue(), referenceColor.red());
}
QImage imageSwappedTwice = imageSwapped.rgbSwapped();
QCOMPARE(image, imageSwappedTwice);
QCOMPARE(memcmp(image.constBits(), imageSwappedTwice.constBits(), image.sizeInBytes()), 0);
}
void tst_QImage::mirrored_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<bool>("swap_vertical");
QTest::addColumn<bool>("swap_horizontal");
QTest::addColumn<int>("width");
QTest::addColumn<int>("height");
QTest::newRow("Format_RGB32, vertical") << QImage::Format_RGB32 << true << false << 16 << 16;
QTest::newRow("Format_ARGB32, vertical") << QImage::Format_ARGB32 << true << false << 16 << 16;
QTest::newRow("Format_ARGB32_Premultiplied, vertical") << QImage::Format_ARGB32_Premultiplied << true << false << 16 << 16;
QTest::newRow("Format_RGB16, vertical") << QImage::Format_RGB16 << true << false << 16 << 16;
QTest::newRow("Format_ARGB8565_Premultiplied, vertical") << QImage::Format_ARGB8565_Premultiplied << true << false << 16 << 16;
QTest::newRow("Format_ARGB6666_Premultiplied, vertical") << QImage::Format_ARGB6666_Premultiplied << true << false << 16 << 16;
QTest::newRow("Format_ARGB4444_Premultiplied, vertical") << QImage::Format_ARGB4444_Premultiplied << true << false << 16 << 16;
QTest::newRow("Format_RGB666, vertical") << QImage::Format_RGB666 << true << false << 16 << 16;
QTest::newRow("Format_RGB555, vertical") << QImage::Format_RGB555 << true << false << 16 << 16;
QTest::newRow("Format_ARGB8555_Premultiplied, vertical") << QImage::Format_ARGB8555_Premultiplied << true << false << 16 << 16;
QTest::newRow("Format_RGB888, vertical") << QImage::Format_RGB888 << true << false << 16 << 16;
QTest::newRow("Format_BGR888, vertical") << QImage::Format_BGR888 << true << false << 16 << 16;
QTest::newRow("Format_RGB444, vertical") << QImage::Format_RGB444 << true << false << 16 << 16;
QTest::newRow("Format_RGBX8888, vertical") << QImage::Format_RGBX8888 << true << false << 16 << 16;
QTest::newRow("Format_RGBA8888_Premultiplied, vertical") << QImage::Format_RGBA8888_Premultiplied << true << false << 16 << 16;
QTest::newRow("Format_A2BGR30_Premultiplied, vertical") << QImage::Format_A2BGR30_Premultiplied << true << false << 16 << 16;
QTest::newRow("Format_RGB30, vertical") << QImage::Format_RGB30 << true << false << 16 << 16;
QTest::newRow("Format_Indexed8, vertical") << QImage::Format_Indexed8 << true << false << 16 << 16;
QTest::newRow("Format_Mono, vertical") << QImage::Format_Mono << true << false << 16 << 16;
QTest::newRow("Format_MonoLSB, vertical") << QImage::Format_MonoLSB << true << false << 16 << 16;
QTest::newRow("Format_ARGB32_Premultiplied, horizontal") << QImage::Format_ARGB32_Premultiplied << false << true << 16 << 16;
QTest::newRow("Format_RGB888, horizontal") << QImage::Format_RGB888 << false << true << 16 << 16;
QTest::newRow("Format_RGB16, horizontal") << QImage::Format_RGB16 << false << true << 16 << 16;
QTest::newRow("Format_Indexed8, horizontal") << QImage::Format_Indexed8 << false << true << 16 << 16;
QTest::newRow("Format_Mono, horizontal") << QImage::Format_Mono << false << true << 16 << 16;
QTest::newRow("Format_MonoLSB, horizontal") << QImage::Format_MonoLSB << false << true << 16 << 16;
QTest::newRow("Format_ARGB32_Premultiplied, horizontal+vertical") << QImage::Format_ARGB32_Premultiplied << true << true << 16 << 16;
QTest::newRow("Format_RGB888, horizontal+vertical") << QImage::Format_RGB888 << true << true << 16 << 16;
QTest::newRow("Format_RGB16, horizontal+vertical") << QImage::Format_RGB16 << true << true << 16 << 16;
QTest::newRow("Format_Indexed8, horizontal+vertical") << QImage::Format_Indexed8 << true << true << 16 << 16;
QTest::newRow("Format_Mono, horizontal+vertical") << QImage::Format_Mono << true << true << 16 << 16;
QTest::newRow("Format_MonoLSB, horizontal+vertical") << QImage::Format_MonoLSB << true << true << 16 << 16;
QTest::newRow("Format_RGB32, vertical") << QImage::Format_RGB32 << true << false << 8 << 16;
QTest::newRow("Format_ARGB32, vertical") << QImage::Format_ARGB32 << true << false << 16 << 8;
QTest::newRow("Format_Mono, vertical, non-aligned") << QImage::Format_Mono << true << false << 19 << 25;
QTest::newRow("Format_MonoLSB, vertical, non-aligned") << QImage::Format_MonoLSB << true << false << 19 << 25;
// Non-aligned horizontal 1-bit needs special handling so test this.
QTest::newRow("Format_Mono, horizontal, non-aligned") << QImage::Format_Mono << false << true << 13 << 17;
QTest::newRow("Format_Mono, horizontal, non-aligned") << QImage::Format_Mono << false << true << 19 << 25;
QTest::newRow("Format_Mono, horizontal+vertical, non-aligned") << QImage::Format_Mono << true << true << 25 << 47;
QTest::newRow("Format_Mono, horizontal+vertical, non-aligned") << QImage::Format_Mono << true << true << 21 << 16;
QTest::newRow("Format_MonoLSB, horizontal, non-aligned") << QImage::Format_MonoLSB << false << true << 13 << 17;
QTest::newRow("Format_MonoLSB, horizontal, non-aligned") << QImage::Format_MonoLSB << false << true << 19 << 25;
QTest::newRow("Format_MonoLSB, horizontal+vertical, non-aligned") << QImage::Format_MonoLSB << true << true << 25 << 47;
QTest::newRow("Format_MonoLSB, horizontal+vertical, non-aligned") << QImage::Format_MonoLSB << true << true << 21 << 16;
}
void tst_QImage::mirrored()
{
QFETCH(QImage::Format, format);
QFETCH(bool, swap_vertical);
QFETCH(bool, swap_horizontal);
QFETCH(int, width);
QFETCH(int, height);
QImage image(width, height, format);
switch (format) {
case QImage::Format_Mono:
case QImage::Format_MonoLSB:
for (int i = 0; i < image.height(); ++i) {
ushort* scanLine = (ushort*)image.scanLine(i);
*scanLine = (i % 2) ? 0x5555U : 0xCCCCU;
}
break;
case QImage::Format_Indexed8:
for (int i = 0; i < image.height(); ++i) {
for (int j = 0; j < image.width(); ++j) {
image.setColor(i*16+j, qRgb(j*16, i*16, 0));
image.setPixel(j, i, i*16+j);
}
}
break;
default:
for (int i = 0; i < image.height(); ++i)
for (int j = 0; j < image.width(); ++j)
image.setPixel(j, i, qRgb(j*16, i*16, 0));
break;
}
QImage imageMirrored = image.mirrored(swap_horizontal, swap_vertical);
for (int i = 0; i < image.height(); ++i) {
int mirroredI = swap_vertical ? (image.height() - i - 1) : i;
for (int j = 0; j < image.width(); ++j) {
QRgb referenceColor = image.pixel(j, i);
int mirroredJ = swap_horizontal ? (image.width() - j - 1) : j;
QRgb mirroredColor = imageMirrored.pixel(mirroredJ, mirroredI);
QCOMPARE(mirroredColor, referenceColor);
}
}
QImage imageMirroredTwice = imageMirrored.mirrored(swap_horizontal, swap_vertical);
QCOMPARE(image, imageMirroredTwice);
if (format != QImage::Format_Mono && format != QImage::Format_MonoLSB)
QCOMPARE(memcmp(image.constBits(), imageMirroredTwice.constBits(), image.sizeInBytes()), 0);
else {
for (int i = 0; i < image.height(); ++i)
for (int j = 0; j < image.width(); ++j)
QCOMPARE(image.pixel(j,i), imageMirroredTwice.pixel(j,i));
}
}
void tst_QImage::inplaceRgbSwapped_data()
{
rgbSwapped_data();
}
void tst_QImage::inplaceRgbSwapped()
{
#if defined(Q_COMPILER_REF_QUALIFIERS)
QFETCH(QImage::Format, format);
QImage image(64, 1, format);
image.fill(0);
QVector<QRgb> testColor(image.width());
for (int i = 0; i < image.width(); ++i)
testColor[i] = qRgb(i * 2, i * 3, 255 - i * 4);
if (format == QImage::Format_Indexed8) {
for (int i = 0; i < image.width(); ++i) {
image.setColor(i, testColor[i]);
image.setPixel(i, 0, i);
}
} else {
for (int i = 0; i < image.width(); ++i)
image.setPixel(i, 0, testColor[i]);
}
const uchar* orginalPtr = image.constScanLine(0);
QImage imageSwapped = std::move(image).rgbSwapped();
for (int i = 0; i < imageSwapped.width(); ++i) {
QRgb referenceColor = testColor[i];
QRgb swappedColor = imageSwapped.pixel(i, 0);
QCOMPARE(qRed(swappedColor) & 0xf0, qBlue(referenceColor) & 0xf0);
QCOMPARE(qGreen(swappedColor) & 0xf0, qGreen(referenceColor) & 0xf0);
QCOMPARE(qBlue(swappedColor) & 0xf0, qRed(referenceColor) & 0xf0);
}
QCOMPARE(imageSwapped.constScanLine(0), orginalPtr);
for (int rw = 0; rw <= 1; rw++) {
// Test attempted inplace conversion of images created on existing buffer
uchar *volatileData = 0;
QImage orig = imageSwapped;
QImage dataSwapped;
{
QVERIFY(!orig.isNull());
volatileData = new uchar[orig.sizeInBytes()];
memcpy(volatileData, orig.constBits(), orig.sizeInBytes());
QImage dataImage;
if (rw)
dataImage = QImage(volatileData, orig.width(), orig.height(), orig.format());
else
dataImage = QImage((const uchar *)volatileData, orig.width(), orig.height(), orig.format());
if (orig.colorCount())
dataImage.setColorTable(orig.colorTable());
dataSwapped = std::move(dataImage).rgbSwapped();
QVERIFY(!dataSwapped.isNull());
delete[] volatileData;
}
QVERIFY2(dataSwapped.constBits() != volatileData, rw ? "non-const" : "const");
QCOMPARE(dataSwapped, orig.rgbSwapped());
}
#endif
}
void tst_QImage::inplaceMirrored_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<bool>("swap_vertical");
QTest::addColumn<bool>("swap_horizontal");
for (int i = QImage::Format_Mono; i < QImage::NImageFormats; ++i) {
if (i == QImage::Format_Alpha8
|| i == QImage::Format_Grayscale8
|| i == QImage::Format_Grayscale16) {
continue;
}
if (i == QImage::Format_RGB444 || i == QImage::Format_ARGB4444_Premultiplied)
continue;
const auto fmt = formatToString(QImage::Format(i));
QTest::addRow("%s, vertical", fmt.data())
<< QImage::Format(i) << true << false;
QTest::addRow("%s, horizontal", fmt.data())
<< QImage::Format(i) << false << true;
QTest::addRow("%s, horizontal+vertical", fmt.data())
<< QImage::Format(i) << true << true;
}
}
void tst_QImage::inplaceMirrored()
{
#if defined(Q_COMPILER_REF_QUALIFIERS)
QFETCH(QImage::Format, format);
QFETCH(bool, swap_vertical);
QFETCH(bool, swap_horizontal);
QImage image(16, 16, format);
switch (format) {
case QImage::Format_Mono:
case QImage::Format_MonoLSB:
for (int i = 0; i < image.height(); ++i) {
ushort* scanLine = (ushort*)image.scanLine(i);
*scanLine = (i % 2) ? 0x0fffU : 0xf000U;
}
break;
case QImage::Format_Indexed8:
for (int i = 0; i < image.height(); ++i) {
for (int j = 0; j < image.width(); ++j) {
image.setColor(i*16+j, qRgb(j*16, i*16, 0));
image.setPixel(j, i, i*16+j);
}
}
break;
default:
for (int i = 0; i < image.height(); ++i)
for (int j = 0; j < image.width(); ++j)
image.setPixel(j, i, qRgb(j*16, i*16, 0));
}
const uchar* originalPtr = image.constScanLine(0);
QImage imageMirrored = std::move(image).mirrored(swap_horizontal, swap_vertical);
if (format != QImage::Format_Mono && format != QImage::Format_MonoLSB) {
for (int i = 0; i < imageMirrored.height(); ++i) {
int mirroredI = swap_vertical ? (imageMirrored.height() - i - 1) : i;
for (int j = 0; j < imageMirrored.width(); ++j) {
int mirroredJ = swap_horizontal ? (imageMirrored.width() - j - 1) : j;
QRgb mirroredColor = imageMirrored.pixel(mirroredJ, mirroredI);
QCOMPARE(qRed(mirroredColor) & 0xF8, j * 16);
QCOMPARE(qGreen(mirroredColor) & 0xF8, i * 16);
}
}
} else {
for (int i = 0; i < imageMirrored.height(); ++i) {
ushort* scanLine = (ushort*)imageMirrored.scanLine(i);
ushort expect;
if (swap_vertical && swap_horizontal)
expect = (i % 2) ? 0x000fU : 0xfff0U;
else if (swap_vertical)
expect = (i % 2) ? 0xf000U : 0x0fffU;
else
expect = (i % 2) ? 0xfff0U : 0x000fU;
QCOMPARE(*scanLine, expect);
}
}
QCOMPARE(imageMirrored.constScanLine(0), originalPtr);
for (int rw = 0; rw <= 1; rw++) {
// Test attempted inplace conversion of images created on existing buffer
uchar *volatileData = 0;
QImage orig = imageMirrored;
QImage dataSwapped;
{
QVERIFY(!orig.isNull());
volatileData = new uchar[orig.sizeInBytes()];
memcpy(volatileData, orig.constBits(), orig.sizeInBytes());
QImage dataImage;
if (rw)
dataImage = QImage(volatileData, orig.width(), orig.height(), orig.format());
else
dataImage = QImage((const uchar *)volatileData, orig.width(), orig.height(), orig.format());
if (orig.colorCount())
dataImage.setColorTable(orig.colorTable());
dataSwapped = std::move(dataImage).mirrored(swap_horizontal, swap_vertical);
QVERIFY(!dataSwapped.isNull());
delete[] volatileData;
}
QVERIFY2(dataSwapped.constBits() != volatileData, rw ? "non-const" : "const");
QCOMPARE(dataSwapped, orig.mirrored(swap_horizontal, swap_vertical));
}
#endif
}
void tst_QImage::inplaceMirroredOdd_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<bool>("swap_vertical");
QTest::addColumn<bool>("swap_horizontal");
QTest::newRow("Format_ARGB32, vertical") << QImage::Format_ARGB32 << true << false;
QTest::newRow("Format_RGB888, vertical") << QImage::Format_RGB888 << true << false;
QTest::newRow("Format_RGB16, vertical") << QImage::Format_RGB16 << true << false;
QTest::newRow("Format_ARGB32, horizontal") << QImage::Format_ARGB32 << false << true;
QTest::newRow("Format_RGB888, horizontal") << QImage::Format_RGB888 << false << true;
QTest::newRow("Format_RGB16, horizontal") << QImage::Format_RGB16 << false << true;
QTest::newRow("Format_ARGB32, horizontal+vertical") << QImage::Format_ARGB32 << true << true;
QTest::newRow("Format_RGB888, horizontal+vertical") << QImage::Format_RGB888 << true << true;
QTest::newRow("Format_RGB16, horizontal+vertical") << QImage::Format_RGB16 << true << true;
}
void tst_QImage::inplaceMirroredOdd()
{
#if defined(Q_COMPILER_REF_QUALIFIERS)
QFETCH(QImage::Format, format);
QFETCH(bool, swap_vertical);
QFETCH(bool, swap_horizontal);
QImage image(15, 15, format);
for (int i = 0; i < image.height(); ++i)
for (int j = 0; j < image.width(); ++j)
image.setPixel(j, i, qRgb(j*16, i*16, 0));
const uchar* originalPtr = image.constScanLine(0);
QImage imageMirrored = std::move(image).mirrored(swap_horizontal, swap_vertical);
for (int i = 0; i < imageMirrored.height(); ++i) {
int mirroredI = swap_vertical ? (imageMirrored.height() - i - 1) : i;
for (int j = 0; j < imageMirrored.width(); ++j) {
int mirroredJ = swap_horizontal ? (imageMirrored.width() - j - 1) : j;
QRgb mirroredColor = imageMirrored.pixel(mirroredJ, mirroredI);
QCOMPARE(qRed(mirroredColor) & 0xF8, j * 16);
QCOMPARE(qGreen(mirroredColor) & 0xF8, i * 16);
}
}
QCOMPARE(imageMirrored.constScanLine(0), originalPtr);
#endif
}
void tst_QImage::inplaceRgbMirrored()
{
#if defined(Q_COMPILER_REF_QUALIFIERS)
QImage image1(32, 32, QImage::Format_ARGB32);
QImage image2(32, 32, QImage::Format_ARGB32);
image1.fill(0);
image2.fill(0);
const uchar* originalPtr1 = image1.constScanLine(0);
const uchar* originalPtr2 = image2.constScanLine(0);
QCOMPARE(std::move(image1).rgbSwapped().mirrored().constScanLine(0), originalPtr1);
QCOMPARE(std::move(image2).mirrored().rgbSwapped().constScanLine(0), originalPtr2);
#endif
}
void tst_QImage::genericRgbConversion_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<QImage::Format>("dest_format");
for (int i = QImage::Format_RGB32; i < QImage::NImageFormats; ++i) {
if (i == QImage::Format_Alpha8)
continue;
const QLatin1String formatI = formatToString(QImage::Format(i));
for (int j = QImage::Format_RGB32; j < QImage::NImageFormats; ++j) {
if (j == QImage::Format_Alpha8)
continue;
if (i == j)
continue;
QTest::addRow("%s -> %s", formatI.data(), formatToString(QImage::Format(j)).data())
<< QImage::Format(i) << QImage::Format(j);
}
}
}
void tst_QImage::genericRgbConversion()
{
// Test that all RGB conversions work and maintain at least 4bit of color accuracy.
QFETCH(QImage::Format, format);
QFETCH(QImage::Format, dest_format);
bool srcGrayscale = format == QImage::Format_Grayscale8 || format == QImage::Format_Grayscale16;
bool dstGrayscale = dest_format == QImage::Format_Grayscale8 || dest_format == QImage::Format_Grayscale16;
QImage image(16, 16, format);
for (int i = 0; i < image.height(); ++i)
for (int j = 0; j < image.width(); ++j)
image.setPixel(j, i, qRgb(j*16, i*16, 0));
QImage imageConverted = image.convertToFormat(dest_format);
QCOMPARE(imageConverted.format(), dest_format);
for (int i = 0; i < imageConverted.height(); ++i) {
for (int j = 0; j < imageConverted.width(); ++j) {
QRgb convertedColor = imageConverted.pixel(j,i);
if (srcGrayscale || dstGrayscale) {
QVERIFY(qAbs(qGray(convertedColor) - qGray(qRgb(j*16, i*16, 0))) < 15);
} else {
QCOMPARE(qRed(convertedColor) & 0xF0, j * 16);
QCOMPARE(qGreen(convertedColor) & 0xF0, i * 16);
}
}
}
}
void tst_QImage::inplaceRgbConversion_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<QImage::Format>("dest_format");
for (int i = QImage::Format_RGB32; i < QImage::NImageFormats; ++i) {
if (i == QImage::Format_Alpha8
|| i == QImage::Format_Grayscale8
|| i == QImage::Format_Grayscale16) {
continue;
}
for (int j = QImage::Format_RGB32; j < QImage::NImageFormats; ++j) {
if (j == QImage::Format_Alpha8
|| j == QImage::Format_Grayscale8
|| j == QImage::Format_Grayscale16) {
continue;
}
if (i == j)
continue;
if (qt_depthForFormat(QImage::Format(i)) >= qt_depthForFormat(QImage::Format(j)))
QTest::addRow("%s -> %s", formatToString(QImage::Format(i)).data(), formatToString(QImage::Format(j)).data())
<< QImage::Format(i) << QImage::Format(j);
}
}
}
void tst_QImage::inplaceRgbConversion()
{
QFETCH(QImage::Format, format);
QFETCH(QImage::Format, dest_format);
QImage image(16, 16, format);
for (int i = 0; i < image.height(); ++i)
for (int j = 0; j < image.width(); ++j)
image.setPixel(j, i, qRgb(j*16, i*16, 0));
const uchar* originalPtr = image.constScanLine(0);
QImage imageConverted = std::move(image).convertToFormat(dest_format);
QCOMPARE(imageConverted.format(), dest_format);
for (int i = 0; i < imageConverted.height(); ++i) {
for (int j = 0; j < imageConverted.width(); ++j) {
QRgb convertedColor = imageConverted.pixel(j,i);
QCOMPARE(qRed(convertedColor) & 0xF0, j * 16);
QCOMPARE(qGreen(convertedColor) & 0xF0, i * 16);
}
}
if (qt_depthForFormat(format) == qt_depthForFormat(dest_format))
QCOMPARE(imageConverted.constScanLine(0), originalPtr);
if (qt_depthForFormat(format) <= 32) {
// Test attempted inplace conversion of images created on existing buffer
static const quint32 readOnlyData[] = { 0xff0102ffU, 0xff0506ffU, 0xff0910ffU, 0xff1314ffU };
quint32 readWriteData[] = { 0xff0102ffU, 0xff0506ffU, 0xff0910ffU, 0xff1314ffU };
QImage roInplaceConverted;
QImage rwInplaceConverted;
{
QImage roImage((const uchar *)readOnlyData, 2, 2, format);
roInplaceConverted = std::move(roImage).convertToFormat(dest_format);
QImage rwImage((uchar *)readWriteData, 2, 2, format);
rwInplaceConverted = std::move(rwImage).convertToFormat(dest_format);
}
QImage roImage2((const uchar *)readOnlyData, 2, 2, format);
QImage normalConverted = roImage2.convertToFormat(dest_format);
QVERIFY(roInplaceConverted.constBits() != (const uchar *)readOnlyData);
QCOMPARE(normalConverted, roInplaceConverted);
QVERIFY(rwInplaceConverted.constBits() != (const uchar *)readWriteData);
QCOMPARE(normalConverted, rwInplaceConverted);
}
}
void tst_QImage::largeGenericRgbConversion_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<QImage::Format>("dest_format");
QImage::Format formats[] = {
QImage::Format_RGB32,
QImage::Format_ARGB32,
QImage::Format_ARGB32_Premultiplied,
QImage::Format_RGB16,
QImage::Format_RGB888,
QImage::Format_RGBA8888,
QImage::Format_BGR30,
QImage::Format_A2RGB30_Premultiplied,
QImage::Format_RGBA64_Premultiplied,
};
for (QImage::Format src_format : formats) {
for (QImage::Format dst_format : formats) {
if (src_format == dst_format)
continue;
QTest::addRow("%s -> %s", formatToString(src_format).data(), formatToString(dst_format).data())
<< src_format << dst_format;
}
}
}
void tst_QImage::largeGenericRgbConversion()
{
// Also test a larger conversion for a few formats (here the tested precision is also higher)
QFETCH(QImage::Format, format);
QFETCH(QImage::Format, dest_format);
// Must have more than 64k pixels to trigger threaded codepath:
QImage image(512, 216, format);
for (int i = 0; i < image.height(); ++i)
for (int j = 0; j < image.width(); ++j)
image.setPixel(j, i, qRgb(j % 256, i, 0));
const bool precision_8bit = (format != QImage::Format_RGB16) && (dest_format != QImage::Format_RGB16);
QImage imageConverted = image.convertToFormat(dest_format);
QCOMPARE(imageConverted.format(), dest_format);
for (int i = 0; i < imageConverted.height(); ++i) {
for (int j = 0; j < imageConverted.width(); ++j) {
if (precision_8bit) {
QCOMPARE(imageConverted.pixel(j, i), image.pixel(j, i));
} else {
QRgb convertedColor = imageConverted.pixel(j,i);
QCOMPARE(qRed(convertedColor) & 0xF8, (j % 256) & 0xF8);
QCOMPARE(qGreen(convertedColor) & 0xFC, i & 0xFC);
}
}
}
}
void tst_QImage::largeInplaceRgbConversion_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<QImage::Format>("dest_format");
QImage::Format formats[] = {
QImage::Format_RGB32,
QImage::Format_ARGB32,
QImage::Format_ARGB32_Premultiplied,
QImage::Format_RGB16,
QImage::Format_RGB888,
QImage::Format_RGBA8888,
QImage::Format_BGR30,
QImage::Format_A2RGB30_Premultiplied,
QImage::Format_RGBA64_Premultiplied,
};
for (QImage::Format src_format : formats) {
for (QImage::Format dst_format : formats) {
if (src_format == dst_format)
continue;
if (qt_depthForFormat(src_format) < qt_depthForFormat(dst_format))
continue;
QTest::addRow("%s -> %s", formatToString(src_format).data(), formatToString(dst_format).data())
<< src_format << dst_format;
}
}
}
void tst_QImage::largeInplaceRgbConversion()
{
// Also test a larger conversion for a few formats
QFETCH(QImage::Format, format);
QFETCH(QImage::Format, dest_format);
// Must have more than 64k pixels to trigger threaded codepath:
QImage image(512, 216, format);
for (int i = 0; i < image.height(); ++i)
for (int j = 0; j < image.width(); ++j)
image.setPixel(j, i, qRgb(j % 256, i, 0));
const bool precision_8bit = (format != QImage::Format_RGB16) && (dest_format != QImage::Format_RGB16);
image.convertTo(dest_format);
QCOMPARE(image.format(), dest_format);
for (int i = 0; i < image.height(); ++i) {
for (int j = 0; j < image.width(); ++j) {
if (precision_8bit) {
QCOMPARE(image.pixel(j,i), qRgb(j % 256, i, 0));
} else {
QRgb convertedColor = image.pixel(j,i);
QCOMPARE(qRed(convertedColor) & 0xF8, (j % 256) & 0xF8);
QCOMPARE(qGreen(convertedColor) & 0xFC, i & 0xFC);
}
}
}
}
void tst_QImage::deepCopyWhenPaintingActive()
{
QImage image(64, 64, QImage::Format_ARGB32_Premultiplied);
image.fill(0);
QPainter painter(&image);
QImage copy = image;
painter.setBrush(Qt::black);
painter.drawEllipse(image.rect());
QVERIFY(copy != image);
}
void tst_QImage::scaled_QTBUG19157()
{
QImage foo(5000, 1, QImage::Format_RGB32);
foo = foo.scaled(1024, 1024, Qt::KeepAspectRatio);
QVERIFY(!foo.isNull());
}
void tst_QImage::convertOverUnPreMul()
{
QImage image(256, 256, QImage::Format_ARGB32_Premultiplied);
for (int j = 0; j < 256; j++) {
for (int i = 0; i <= j; i++) {
image.setPixel(i, j, qRgba(i, i, i, j));
}
}
QImage image2 = image.convertToFormat(QImage::Format_ARGB32).convertToFormat(QImage::Format_ARGB32_Premultiplied);
for (int j = 0; j < 256; j++) {
for (int i = 0; i <= j; i++) {
QCOMPARE(qAlpha(image2.pixel(i, j)), qAlpha(image.pixel(i, j)));
QCOMPARE(qGray(image2.pixel(i, j)), qGray(image.pixel(i, j)));
}
}
}
void tst_QImage::scaled_QTBUG35972()
{
QImage src(532,519,QImage::Format_ARGB32_Premultiplied);
src.fill(QColor(Qt::white));
QImage dest(1000,1000,QImage::Format_ARGB32_Premultiplied);
dest.fill(QColor(Qt::white));
QPainter painter1(&dest);
const QTransform trf(1.25, 0,
0, 1.25,
/*dx */ 15.900000000000034, /* dy */ 72.749999999999986);
painter1.setTransform(trf);
painter1.drawImage(QRectF(-2.6, -2.6, 425.6, 415.20000000000005), src, QRectF(0,0,532,519));
const quint32 *pixels = reinterpret_cast<const quint32 *>(dest.constBits());
int size = dest.width()*dest.height();
for (int i = 0; i < size; ++i)
QCOMPARE(pixels[i], 0xffffffff);
}
void tst_QImage::convertToPixelFormat()
{
QPixelFormat rgb565 = qPixelFormatRgba(5,6,5,0,QPixelFormat::IgnoresAlpha, QPixelFormat::AtBeginning, QPixelFormat::NotPremultiplied, QPixelFormat::UnsignedShort);
QPixelFormat rgb565ImageFormat = QImage::toPixelFormat(QImage::Format_RGB16);
QCOMPARE(rgb565, rgb565ImageFormat);
}
void tst_QImage::convertToImageFormat_data()
{
QTest::addColumn<QImage::Format>("image_format");
QTest::newRow("Convert Format_Invalid") << QImage::Format_Invalid;
QTest::newRow("Convert Format_Mono") << QImage::Format_Mono;
//This ends up being a QImage::Format_Mono since we cant specify LSB in QPixelFormat
//QTest::newRow("Convert Format_MonoLSB") << QImage::Format_MonoLSB;
QTest::newRow("Convert Format_Indexed8") << QImage::Format_Indexed8;
QTest::newRow("Convert Format_RGB32") << QImage::Format_RGB32;
QTest::newRow("Convert Format_ARGB32") << QImage::Format_ARGB32;
QTest::newRow("Convert Format_ARGB32_Premultiplied") << QImage::Format_ARGB32_Premultiplied;
QTest::newRow("Convert Format_RGB16") << QImage::Format_RGB16;
QTest::newRow("Convert Format_ARGB8565_Premultiplied") << QImage::Format_ARGB8565_Premultiplied;
QTest::newRow("Convert Format_RGB666") << QImage::Format_RGB666;
QTest::newRow("Convert Format_ARGB6666_Premultiplied") << QImage::Format_ARGB6666_Premultiplied;
QTest::newRow("Convert Format_RGB555") << QImage::Format_RGB555;
QTest::newRow("Convert Format_ARGB8555_Premultiplied") << QImage::Format_ARGB8555_Premultiplied;
QTest::newRow("Convert Format_RGB888") << QImage::Format_RGB888;
QTest::newRow("Convert Format_RGB444") << QImage::Format_RGB444;
QTest::newRow("Convert Format_ARGB4444_Premultiplied") << QImage::Format_ARGB4444_Premultiplied;
QTest::newRow("Convert Format_RGBX8888") << QImage::Format_RGBX8888;
QTest::newRow("Convert Format_RGBA8888") << QImage::Format_RGBA8888;
QTest::newRow("Convert Format_RGBA8888_Premultiplied") << QImage::Format_RGBA8888_Premultiplied;
}
void tst_QImage::convertToImageFormat()
{
QFETCH(QImage::Format, image_format);
QPixelFormat pixel_format = QImage::toPixelFormat(image_format);
QImage::Format format = QImage::toImageFormat(pixel_format);
QCOMPARE(format, image_format);
}
void tst_QImage::invertPixelsRGB_data()
{
QTest::addColumn<QImage::Format>("image_format");
for (int i = QImage::Format_RGB32; i < QImage::NImageFormats; ++i) {
if (i == QImage::Format_Alpha8
|| i == QImage::Format_Grayscale8
|| i == QImage::Format_Grayscale16) {
continue;
}
QTest::addRow("%s", formatToString(QImage::Format(i)).data()) << QImage::Format(i);
}
}
void tst_QImage::invertPixelsRGB()
{
QFETCH(QImage::Format, image_format);
QImage image(1, 1, image_format);
image.fill(QColor::fromRgb(32, 64, 96));
image.invertPixels();
QCOMPARE(image.format(), image_format);
uint pixel = image.pixel(0, 0);
QCOMPARE(qRed(pixel) >> 4, (255 - 32) >> 4);
QCOMPARE(qGreen(pixel) >> 4, (255 - 64) >> 4);
QCOMPARE(qBlue(pixel) >> 4, (255 - 96) >> 4);
}
void tst_QImage::invertPixelsIndexed()
{
{
QImage image(1, 1, QImage::Format_Mono);
image.fill(Qt::color1);
image.invertPixels();
QCOMPARE(image.pixelIndex(0, 0), 0);
}
{
QImage image(1, 1, QImage::Format_MonoLSB);
image.fill(Qt::color0);
image.invertPixels();
QCOMPARE(image.pixelIndex(0, 0), 1);
}
{
QImage image(1, 1, QImage::Format_Indexed8);
image.setColorTable({0xff000000, 0xffffffff});
image.fill(Qt::black);
image.invertPixels();
QCOMPARE(image.pixelIndex(0, 0), 255);
}
{
QImage image(1, 1, QImage::Format_Indexed8);
image.setColorTable({0xff000000, 0xffffffff, 0x80000000, 0x80ffffff, 0x00000000});
image.fill(Qt::white);
image.invertPixels();
QCOMPARE(image.pixelIndex(0, 0), 254);
}
}
void tst_QImage::exifOrientation_data()
{
QTest::addColumn<QString>("fileName");
QTest::addColumn<int>("orientation");
QTest::addColumn<int>("dpmx");
QTest::addColumn<int>("dpmy");
QTest::newRow("Orientation 1, Intel format") << m_prefix + "jpeg_exif_orientation_value_1.jpg" << (int)QImageIOHandler::TransformationNone << 39 << 39;
QTest::newRow("Orientation 2, Intel format") << m_prefix + "jpeg_exif_orientation_value_2.jpg" << (int)QImageIOHandler::TransformationMirror << 39 << 39;
QTest::newRow("Orientation 3, Intel format") << m_prefix + "jpeg_exif_orientation_value_3.jpg" << (int)QImageIOHandler::TransformationRotate180 << 39 << 39;
QTest::newRow("Orientation 4, Intel format") << m_prefix + "jpeg_exif_orientation_value_4.jpg" << (int)QImageIOHandler::TransformationFlip << 39 << 39;
QTest::newRow("Orientation 5, Intel format") << m_prefix + "jpeg_exif_orientation_value_5.jpg" << (int)QImageIOHandler::TransformationFlipAndRotate90 << 39 << 39;
QTest::newRow("Orientation 6, Intel format") << m_prefix + "jpeg_exif_orientation_value_6.jpg" << (int)QImageIOHandler::TransformationRotate90 << 39 << 39;
QTest::newRow("Orientation 6, Motorola format") << m_prefix + "jpeg_exif_orientation_value_6_motorola.jpg" << (int)QImageIOHandler::TransformationRotate90 << 39 << 39;
QTest::newRow("Orientation 7, Intel format") << m_prefix + "jpeg_exif_orientation_value_7.jpg" << (int)QImageIOHandler::TransformationMirrorAndRotate90 << 39 << 39;
QTest::newRow("Orientation 8, Intel format") << m_prefix + "jpeg_exif_orientation_value_8.jpg" << (int)QImageIOHandler::TransformationRotate270 << 39 << 39;
}
QT_BEGIN_NAMESPACE
extern void qt_imageTransform(QImage &src, QImageIOHandler::Transformations orient);
QT_END_NAMESPACE
QT_USE_NAMESPACE
void tst_QImage::exifOrientation()
{
QFETCH(QString, fileName);
QFETCH(int, orientation);
QFETCH(int, dpmx);
QFETCH(int, dpmy);
QImageReader imageReader(fileName);
imageReader.setAutoTransform(true);
QCOMPARE(imageReader.transformation(), orientation);
QImage img = imageReader.read();
QCOMPARE(img.dotsPerMeterX(), dpmx);
QCOMPARE(img.dotsPerMeterY(), dpmy);
QRgb px;
QVERIFY(!img.isNull());
px = img.pixel(0, 0);
QVERIFY(qRed(px) > 250 && qGreen(px) < 5 && qBlue(px) < 5);
px = img.pixel(img.width() - 1, 0);
QVERIFY(qRed(px) < 5 && qGreen(px) < 5 && qBlue(px) > 250);
QImageReader imageReader2(fileName);
QCOMPARE(imageReader2.autoTransform(), false);
QCOMPARE(imageReader2.transformation(), orientation);
QImage img2 = imageReader2.read();
qt_imageTransform(img2, imageReader2.transformation());
QCOMPARE(img, img2);
}
void tst_QImage::exif_QTBUG45865()
{
QFile file(m_prefix + "jpeg_exif_QTBUG-45865.jpg");
QVERIFY(file.open(QIODevice::ReadOnly));
QByteArray byteArray = file.readAll();
QImage image = QImage::fromData(byteArray);
QCOMPARE(image.size(), QSize(5, 8));
}
void tst_QImage::exifInvalidData_data()
{
QTest::addColumn<bool>("$never used");
QTest::newRow("QTBUG-46870");
QTest::newRow("back_pointers");
QTest::newRow("past_end");
QTest::newRow("too_many_ifds");
QTest::newRow("too_many_tags");
}
void tst_QImage::exifInvalidData()
{
QImage image;
QVERIFY(image.load(m_prefix + "jpeg_exif_invalid_data_" + QTest::currentDataTag() + ".jpg"));
QVERIFY(!image.isNull());
}
void tst_QImage::exifReadComments()
{
QImage image;
QVERIFY(image.load(m_prefix + "jpeg_exif_utf8_comment.jpg"));
QVERIFY(!image.isNull());
QCOMPARE(image.textKeys().size(), 1);
QCOMPARE(image.textKeys().first(), "Description");
// check if exif comment is read as utf-8
QCOMPARE(image.text("Description"), QString::fromUtf8("some unicode chars: ÖÄÜ€@"));
QByteArray ba;
{
QBuffer buf(&ba);
QVERIFY(buf.open(QIODevice::WriteOnly));
QVERIFY(image.save(&buf, "JPG"));
}
QVERIFY(!ba.isEmpty());
image = QImage();
QCOMPARE(image.textKeys().size(), 0);
{
QBuffer buf(&ba);
QVERIFY(buf.open(QIODevice::ReadOnly));
QVERIFY(image.load(&buf, "JPG"));
}
// compare written (and reread) description text
QCOMPARE(image.text("Description"), QString::fromUtf8("some unicode chars: ÖÄÜ€@"));
}
static void cleanupFunction(void* info)
{
bool *called = static_cast<bool*>(info);
*called = true;
}
void tst_QImage::cleanupFunctions()
{
QImage bufferImage(64, 64, QImage::Format_ARGB32);
bufferImage.fill(0);
bool called;
{
called = false;
{
QImage image(bufferImage.bits(), bufferImage.width(), bufferImage.height(), bufferImage.format(), cleanupFunction, &called);
}
QVERIFY(called);
}
{
called = false;
QImage *copy = 0;
{
QImage image(bufferImage.bits(), bufferImage.width(), bufferImage.height(), bufferImage.format(), cleanupFunction, &called);
copy = new QImage(image);
}
QVERIFY(!called);
delete copy;
QVERIFY(called);
}
}
// test image devicePixelRatio setting and detaching
void tst_QImage::devicePixelRatio()
{
// create image
QImage a(64, 64, QImage::Format_ARGB32);
a.fill(Qt::white);
QCOMPARE(a.devicePixelRatio(), qreal(1.0));
QCOMPARE(a.isDetached(), true);
// copy image
QImage b = a;
QCOMPARE(b.devicePixelRatio(), qreal(1.0));
QCOMPARE(a.isDetached(), false);
QCOMPARE(b.isDetached(), false);
// set devicePixelRatio to the current value: does not detach
a.setDevicePixelRatio(qreal(1.0));
QCOMPARE(a.isDetached(), false);
QCOMPARE(b.isDetached(), false);
// set devicePixelRatio to a new value: may detach (currently
// does, but we may want to avoid the data copy the future)
a.setDevicePixelRatio(qreal(2.0));
QCOMPARE(a.devicePixelRatio(), qreal(2.0));
QCOMPARE(b.devicePixelRatio(), qreal(1.0));
}
void tst_QImage::rgb30Unpremul()
{
QImage a(3, 1, QImage::Format_A2RGB30_Premultiplied);
((uint*)a.bits())[0] = (3U << 30) | (128 << 20) | (256 << 10) | 512;
((uint*)a.bits())[1] = (2U << 30) | (131 << 20) | (259 << 10) | 515;
((uint*)a.bits())[2] = (1U << 30) | ( 67 << 20) | (131 << 10) | 259;
QImage b = a.convertToFormat(QImage::Format_RGB30);
const uint* bbits = (const uint*)b.bits();
QCOMPARE(bbits[0], (3U << 30) | (128 << 20) | (256 << 10) | 512);
QCOMPARE(bbits[1], (3U << 30) | (196 << 20) | (388 << 10) | 772);
QCOMPARE(bbits[2], (3U << 30) | (201 << 20) | (393 << 10) | 777);
}
void tst_QImage::rgb30Repremul_data()
{
QTest::addColumn<uint>("color");
for (int i = 255; i > 0; i -= 15) {
QTest::addRow("100%% red=%d", i) << qRgba(i, 0, 0, 0xff);
QTest::addRow("75%% red=%d", i) << qRgba(i, 0, 0, 0xc0);
QTest::addRow("50%% red=%d", i) << qRgba(i, 0, 0, 0x80);
QTest::addRow("37.5%% red=%d", i) << qRgba(i, 0, 0, 0x60);
}
}
void tst_QImage::rgb30Repremul()
{
QFETCH(uint, color);
QImage a(1, 1, QImage::Format_ARGB32);
a.setPixel(0, 0, color);
QImage b = a.convertToFormat(QImage::Format_A2BGR30_Premultiplied);
b = b.convertToFormat(QImage::Format_ARGB32);
uint expectedColor = qUnpremultiply(qPremultiply(color));
uint newColor = b.pixel(0, 0);
QVERIFY(qAbs(qRed(newColor) - qRed(expectedColor)) <= 1);
}
void tst_QImage::metadataPassthrough()
{
QImage a(64, 64, QImage::Format_ARGB32);
a.fill(Qt::white);
a.setText(QStringLiteral("Test"), QStringLiteral("Text"));
a.setDotsPerMeterX(100);
a.setDotsPerMeterY(80);
a.setDevicePixelRatio(2.0);
QImage scaled = a.scaled(QSize(32, 32), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QCOMPARE(scaled.text(QStringLiteral("Test")), a.text(QStringLiteral("Test")));
QCOMPARE(scaled.dotsPerMeterX(), a.dotsPerMeterX());
QCOMPARE(scaled.dotsPerMeterY(), a.dotsPerMeterY());
QCOMPARE(scaled.devicePixelRatio(), a.devicePixelRatio());
scaled = a.scaled(QSize(128, 128), Qt::IgnoreAspectRatio, Qt::FastTransformation);
QCOMPARE(scaled.text(QStringLiteral("Test")), a.text(QStringLiteral("Test")));
QCOMPARE(scaled.dotsPerMeterX(), a.dotsPerMeterX());
QCOMPARE(scaled.dotsPerMeterY(), a.dotsPerMeterY());
QCOMPARE(scaled.devicePixelRatio(), a.devicePixelRatio());
QImage mirrored = a.mirrored();
QCOMPARE(mirrored.text(QStringLiteral("Test")), a.text(QStringLiteral("Test")));
QCOMPARE(mirrored.dotsPerMeterX(), a.dotsPerMeterX());
QCOMPARE(mirrored.dotsPerMeterY(), a.dotsPerMeterY());
QCOMPARE(mirrored.devicePixelRatio(), a.devicePixelRatio());
QTransform t;
t.rotate(90);
QImage rotated = a.transformed(t);
QCOMPARE(rotated.text(QStringLiteral("Test")), a.text(QStringLiteral("Test")));
QCOMPARE(rotated.dotsPerMeterX(), a.dotsPerMeterX());
QCOMPARE(rotated.dotsPerMeterY(), a.dotsPerMeterY());
QCOMPARE(rotated.devicePixelRatio(), a.devicePixelRatio());
QImage swapped = a.rgbSwapped();
QCOMPARE(swapped.text(QStringLiteral("Test")), a.text(QStringLiteral("Test")));
QCOMPARE(swapped.dotsPerMeterX(), a.dotsPerMeterX());
QCOMPARE(swapped.dotsPerMeterY(), a.dotsPerMeterY());
QCOMPARE(swapped.devicePixelRatio(), a.devicePixelRatio());
QImage converted = a.convertToFormat(QImage::Format_RGB32);
QCOMPARE(converted.text(QStringLiteral("Test")), a.text(QStringLiteral("Test")));
QCOMPARE(converted.dotsPerMeterX(), a.dotsPerMeterX());
QCOMPARE(converted.dotsPerMeterY(), a.dotsPerMeterY());
QCOMPARE(converted.devicePixelRatio(), a.devicePixelRatio());
QVector<QRgb> clut({ 0xFFFF0000, 0xFF00FF00, 0xFF0000FF });
QImage convertedWithClut = a.convertToFormat(QImage::Format_Indexed8, clut);
QCOMPARE(convertedWithClut.text(QStringLiteral("Test")), a.text(QStringLiteral("Test")));
QCOMPARE(convertedWithClut.dotsPerMeterX(), a.dotsPerMeterX());
QCOMPARE(convertedWithClut.dotsPerMeterY(), a.dotsPerMeterY());
QCOMPARE(convertedWithClut.devicePixelRatio(), a.devicePixelRatio());
QImage copied = a.copy(0, 0, a.width() / 2, a.height() / 2);
QCOMPARE(copied.text(QStringLiteral("Test")), a.text(QStringLiteral("Test")));
QCOMPARE(copied.dotsPerMeterX(), a.dotsPerMeterX());
QCOMPARE(copied.dotsPerMeterY(), a.dotsPerMeterY());
QCOMPARE(copied.devicePixelRatio(), a.devicePixelRatio());
QImage alphaMask = a.createAlphaMask();
QCOMPARE(alphaMask.dotsPerMeterX(), a.dotsPerMeterX());
QCOMPARE(alphaMask.dotsPerMeterY(), a.dotsPerMeterY());
QCOMPARE(alphaMask.devicePixelRatio(), a.devicePixelRatio());
QImage heuristicMask = a.createHeuristicMask();
QCOMPARE(heuristicMask.dotsPerMeterX(), a.dotsPerMeterX());
QCOMPARE(heuristicMask.dotsPerMeterY(), a.dotsPerMeterY());
QCOMPARE(heuristicMask.devicePixelRatio(), a.devicePixelRatio());
QImage maskFromColor = a.createMaskFromColor(qRgb(0, 0, 0));
QCOMPARE(maskFromColor.dotsPerMeterX(), a.dotsPerMeterX());
QCOMPARE(maskFromColor.dotsPerMeterY(), a.dotsPerMeterY());
QCOMPARE(maskFromColor.devicePixelRatio(), a.devicePixelRatio());
}
void tst_QImage::pixelColor()
{
QImage argb32(1, 1, QImage::Format_ARGB32);
QImage argb32pm(1, 1, QImage::Format_ARGB32_Premultiplied);
QColor c(Qt::red);
c.setAlpha(128);
argb32.setPixelColor(QPoint(0, 0), c);
argb32pm.setPixelColor(QPoint(0, 0), c);
QCOMPARE(argb32.pixelColor(QPoint(0, 0)), c);
QCOMPARE(argb32pm.pixelColor(QPoint(0, 0)), c);
QImage t = argb32.convertToFormat(QImage::Format_ARGB32_Premultiplied);
QCOMPARE(t.pixel(0,0), argb32pm.pixel(0,0));
// Try specifying an invalid position.
QTest::ignoreMessage(QtWarningMsg, "QImage::setPixelColor: coordinate (-1,-1) out of range");
argb32.setPixelColor(-1, -1, QColor(Qt::red));
// Try setting an invalid color.
QTest::ignoreMessage(QtWarningMsg, "QImage::setPixelColor: color is invalid");
argb32.setPixelColor(0, 0, QColor());
// Test correct premultiplied handling of RGBA64 as well
QImage rgba64(1, 1, QImage::Format_RGBA64);
QImage rgba64pm(1, 1, QImage::Format_RGBA64_Premultiplied);
rgba64.setPixelColor(QPoint(0, 0), c);
rgba64pm.setPixelColor(QPoint(0, 0), c);
QCOMPARE(rgba64.pixelColor(QPoint(0, 0)), c);
QCOMPARE(rgba64pm.pixelColor(QPoint(0, 0)), c);
}
void tst_QImage::pixel()
{
{
QImage mono(1, 1, QImage::Format_Mono);
QImage monolsb(1, 1, QImage::Format_MonoLSB);
QImage indexed(1, 1, QImage::Format_Indexed8);
mono.fill(0);
monolsb.fill(0);
indexed.fill(0);
QCOMPARE(QColor(mono.pixel(0, 0)), QColor(Qt::black));
QCOMPARE(QColor(monolsb.pixel(0, 0)), QColor(Qt::black));
indexed.pixel(0, 0); // Don't crash
}
{
uchar a = 0;
QImage mono(&a, 1, 1, QImage::Format_Mono);
QImage monolsb(&a, 1, 1, QImage::Format_MonoLSB);
QImage indexed(&a, 1, 1, QImage::Format_Indexed8);
mono.pixel(0, 0); // Don't crash
monolsb.pixel(0, 0); // Don't crash
indexed.pixel(0, 0); // Don't crash
}
}
void tst_QImage::ditherGradient_data()
{
QTest::addColumn<QImage>("image");
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<int>("flags");
QTest::addColumn<int>("minimumExpectedGradient");
QImage rgb32(256, 16, QImage::Format_RGB32);
QLinearGradient gradient(QRectF(rgb32.rect()).topLeft(), QRectF(rgb32.rect()).topRight());
gradient.setColorAt(0.0, QColor(0, 0, 0));
gradient.setColorAt(1.0, QColor(255, 255, 255));
QPainter p;
p.begin(&rgb32);
p.fillRect(rgb32.rect(), gradient);
p.end();
QTest::newRow("rgb32 -> rgb444 (no dither)") << rgb32 << QImage::Format_RGB444 << 0 << 16;
QTest::newRow("rgb32 -> rgb444 (dithering)") << rgb32 << QImage::Format_RGB444 << int(Qt::PreferDither | Qt::OrderedDither) << 33;
QTest::newRow("rgb32 -> argb4444pm (dithering)") << rgb32 << QImage::Format_ARGB4444_Premultiplied << int(Qt::PreferDither | Qt::OrderedDither) << 33;
QTest::newRow("rgb32 -> rgb16 (no dither)") << rgb32 << QImage::Format_RGB16 << 0 << 32;
QTest::newRow("rgb32 -> rgb16 (dithering)") << rgb32 << QImage::Format_RGB16 << int(Qt::PreferDither | Qt::OrderedDither) << 65;
QTest::newRow("rgb32 -> rgb666 (no dither)") << rgb32 << QImage::Format_RGB666 << 0 << 64;
QTest::newRow("rgb32 -> rgb666 (dithering)") << rgb32 << QImage::Format_RGB666 << int(Qt::PreferDither | Qt::OrderedDither) << 129;
// Test we get the same results for opaque input in the ARGBPM implementation.
rgb32 = std::move(rgb32).convertToFormat(QImage::Format_ARGB32_Premultiplied);
QTest::newRow("argb32pm -> argb4444pm (no dither)") << rgb32 << QImage::Format_ARGB4444_Premultiplied << 0 << 16;
QTest::newRow("argb32pm -> rgb444 (dithering)") << rgb32 << QImage::Format_RGB444 << int(Qt::PreferDither | Qt::OrderedDither) << 33;
QTest::newRow("argb32pm -> argb4444pm (dithering)") << rgb32 << QImage::Format_ARGB4444_Premultiplied << int(Qt::PreferDither | Qt::OrderedDither) << 33;
QTest::newRow("argb32pm -> argb8565pm (no dither)") << rgb32 << QImage::Format_ARGB8565_Premultiplied << 0 << 32;
QTest::newRow("argb32pm -> argb8565pm (dithering)") << rgb32 << QImage::Format_ARGB8565_Premultiplied << int(Qt::PreferDither | Qt::OrderedDither) << 65;
QTest::newRow("argb32pm -> argb6666pm (no dither)") << rgb32 << QImage::Format_ARGB6666_Premultiplied << 0 << 64;
QTest::newRow("argb32pm -> argb6666pm (dithering)") << rgb32 << QImage::Format_ARGB6666_Premultiplied << int(Qt::PreferDither | Qt::OrderedDither) << 129;
#if QT_CONFIG(raster_64bit)
QImage rgb30(1024, 16, QImage::Format_RGB30);
QLinearGradient gradient30(QRectF(rgb30.rect()).topLeft(), QRectF(rgb30.rect()).topRight());
gradient30.setColorAt(0.0, QColor(0, 0, 0));
gradient30.setColorAt(1.0, QColor(255, 255, 255));
p.begin(&rgb30);
p.fillRect(rgb30.rect(), gradient30);
p.end();
QTest::newRow("rgb30 -> rgb32 (no dither)") << rgb30 << QImage::Format_RGB32 << 0 << 256;
QTest::newRow("rgb30 -> rgb32 (dithering)") << rgb30 << QImage::Format_RGB32 << int(Qt::PreferDither | Qt::OrderedDither) << 513;
QTest::newRow("rgb30 -> rgb888 (no dither)") << rgb30 << QImage::Format_RGB888 << 0 << 256;
QTest::newRow("rgb30 -> rgb888 (dithering)") << rgb30 << QImage::Format_RGB888 << int(Qt::PreferDither | Qt::OrderedDither) << 513;
#endif
}
void tst_QImage::ditherGradient()
{
QFETCH(QImage, image);
QFETCH(QImage::Format, format);
QFETCH(int, flags);
QFETCH(int, minimumExpectedGradient);
QImage converted = image.convertToFormat(format, (Qt::ImageConversionFlags)flags);
int observedGradientSteps = 0;
int lastTotal = -1;
for (int i = 0; i < converted.width(); ++i) {
int total = 0;
for (int j = 0; j < converted.height(); ++j) {
uint c = converted.pixel(i, j);
QCOMPARE(qAlpha(c), 255);
total += qRed(c);
}
if (total > lastTotal) {
observedGradientSteps++;
lastTotal = total;
}
}
QVERIFY(observedGradientSteps >= minimumExpectedGradient);
}
void tst_QImage::reinterpretAsFormat_data()
{
QTest::addColumn<QImage::Format>("in_format");
QTest::addColumn<QImage::Format>("out_format");
QTest::addColumn<QColor>("in_color");
QTest::addColumn<QColor>("out_color");
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
QTest::newRow("rgb32 -> rgbx8888") << QImage::Format_RGB32 << QImage::Format_RGBX8888 << QColor(Qt::red) << QColor(Qt::blue);
QTest::newRow("rgba8888 -> argb32") << QImage::Format_RGBA8888 << QImage::Format_ARGB32 << QColor(Qt::red) << QColor(Qt::blue);
QTest::newRow("argb32pm -> rgba8888pm") << QImage::Format_RGBA8888_Premultiplied << QImage::Format_ARGB32_Premultiplied << QColor(Qt::green) << QColor(Qt::green);
#endif
QTest::newRow("rgb32 -> argb32") << QImage::Format_RGB32 << QImage::Format_ARGB32 << QColor(Qt::cyan) << QColor(Qt::cyan);
QTest::newRow("argb32pm -> rgb32") << QImage::Format_ARGB32_Premultiplied << QImage::Format_RGB32 << QColor(Qt::transparent) << QColor(Qt::black);
QTest::newRow("argb32 -> rgb32") << QImage::Format_ARGB32 << QImage::Format_RGB32 << QColor(255, 0, 0, 127) << QColor(255, 0, 0);
QTest::newRow("argb32pm -> rgb32") << QImage::Format_ARGB32_Premultiplied << QImage::Format_RGB32 << QColor(255, 0, 0, 127) << QColor(127, 0, 0);
}
void tst_QImage::reinterpretAsFormat()
{
QFETCH(QImage::Format, in_format);
QFETCH(QImage::Format, out_format);
QFETCH(QColor, in_color);
QFETCH(QColor, out_color);
QImage image(1, 1, in_format);
image.setPixelColor(0, 0, in_color);
QVERIFY(image.reinterpretAsFormat(out_format));
QCOMPARE(image.pixelColor(0, 0), out_color);
}
void tst_QImage::reinterpretAsFormat2()
{
const uint imageData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
{
QImage image(reinterpret_cast<const uchar*>(imageData), 4, 2, QImage::Format_RGB32);
QCOMPARE(image.pixelColor(0, 0), QColor(Qt::black));
QVERIFY(image.isDetached());
QVERIFY(image.reinterpretAsFormat(QImage::Format_ARGB32_Premultiplied));
QCOMPARE(image.constBits(), reinterpret_cast<const uchar*>(imageData));
QCOMPARE(image.pixelColor(0, 0), QColor(Qt::transparent));
QVERIFY(!image.reinterpretAsFormat(QImage::Format_Grayscale8));
}
{
QImage image(reinterpret_cast<const uchar*>(imageData), 8, 4, QImage::Format_Indexed8);
image.setColor(0, qRgb(255, 255, 255));
QCOMPARE(image.pixelColor(0, 0), QColor(Qt::white));
QVERIFY(image.reinterpretAsFormat(QImage::Format_Grayscale8));
QCOMPARE(image.pixelColor(0, 0), QColor(Qt::black));
QVERIFY(image.reinterpretAsFormat(QImage::Format_Alpha8));
QCOMPARE(image.pixelColor(0, 0), QColor(Qt::transparent));
QVERIFY(!image.reinterpretAsFormat(QImage::Format_RGB16));
}
}
void tst_QImage::complexTransform8bit()
{
QImage img1(100, 100, QImage::Format_RGB32);
img1.fill(Qt::green);
img1 = img1.convertToFormat(QImage::Format_Indexed8);
QImage img2 = img1.transformed(QTransform().rotate(45), Qt::SmoothTransformation);
// Currently the format is always QImage::Format_ARGB32_Premultiplied, but it
// doesn't have to be, and if it becomes indexed this test is no longer be valid.
QVERIFY(img2.format() > QImage::Format_Indexed8);
QCOMPARE(img2.colorCount(), 0);
}
#ifdef Q_OS_DARWIN
void tst_QImage::toCGImage_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<bool>("supported");
// Populate test data with supported status for all QImage formats.
QSet<QImage::Format> supported =
{ QImage::Format_ARGB32, QImage::Format_RGB32, QImage::Format_RGBA8888_Premultiplied,
QImage::Format_RGBA8888, QImage::Format_RGBX8888, QImage::Format_ARGB32_Premultiplied };
for (int i = QImage::Format_Invalid; i < QImage::Format_Grayscale8; ++i) {
QTest::addRow("%s", formatToString(QImage::Format(i)).data())
<< QImage::Format(i) << supported.contains(QImage::Format(i));
}
}
// Verify that toCGImage() returns a valid CGImageRef for supported image formats.
void tst_QImage::toCGImage()
{
QFETCH(QImage::Format, format);
QFETCH(bool, supported);
QImage qimage(64, 64, format);
qimage.fill(Qt::red);
CGImageRef cgimage = qimage.toCGImage();
QCOMPARE(cgimage != nullptr, supported);
CGImageRelease(cgimage);
}
#endif
void tst_QImage::hugeQImage()
{
#if Q_PROCESSOR_WORDSIZE < 8
QSKIP("Test only makes sense on 64-bit machines");
#else
QImage image(25000, 25000, QImage::Format_RGB32);
QVERIFY(!image.isNull());
QCOMPARE(image.height(), 25000);
QCOMPARE(image.width(), 25000);
QCOMPARE(image.sizeInBytes(), qsizetype(25000)*25000*4);
QCOMPARE(image.bytesPerLine(), 25000 * 4);
QCOMPARE(image.constScanLine(24990), image.constBits() + qsizetype(25000)*24990*4);
image.setPixel(20000, 24990, 0xffaabbcc);
QCOMPARE(image.pixel(20000, 24990), 0xffaabbcc);
QCOMPARE((reinterpret_cast<const unsigned int *>(image.constScanLine(24990)))[20000], 0xffaabbcc);
QImage canvas(100, 100, QImage::Format_RGB32);
QPainter painter(&canvas);
painter.drawImage(0,0, image, 19950, 24900, 100, 100);
painter.end();
QCOMPARE(reinterpret_cast<const unsigned int *>(canvas.constScanLine(90))[50], 0xffaabbcc);
#endif
}
void tst_QImage::convertColorTable()
{
QImage image(10, 10, QImage::Format_Indexed8);
image.setColor(0, 0x80ffffff);
image.fill(0);
QImage argb32 = image.convertToFormat(QImage::Format_ARGB32);
QCOMPARE(argb32.pixel(0,0), 0x80ffffff);
QImage argb32pm = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
QCOMPARE(argb32pm.pixel(0,0), 0x80808080);
QImage rgb32 = image.convertToFormat(QImage::Format_RGB32);
QCOMPARE(rgb32.pixel(0,0), 0xffffffff);
}
void tst_QImage::wideImage()
{
// QTBUG-73731 and QTBUG-73732
QImage i(538994187, 2, QImage::Format_ARGB32);
QImage i2(32, 32, QImage::Format_ARGB32);
i2.fill(Qt::white);
// Test that it doesn't crash:
QPainter painter(&i);
// With the composition mode is SourceOver out it's an invalid write
// With the composition mode is Source it's an invalid read
painter.drawImage(0, 0, i2);
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.drawImage(0, 0, i2);
// Qt6: Test that it actually works on 64bit architectures.
}
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
QT_BEGIN_NAMESPACE
Q_GUI_EXPORT HBITMAP qt_imageToWinHBITMAP(const QImage &p, int hbitmapFormat = 0);
Q_GUI_EXPORT QImage qt_imageFromWinHBITMAP(HBITMAP bitmap, int hbitmapFormat = 0);
QT_END_NAMESPACE
static inline QColor COLORREFToQColor(COLORREF cr)
{
return QColor(GetRValue(cr), GetGValue(cr), GetBValue(cr));
}
void tst_QImage::toWinHBITMAP_data()
{
QTest::addColumn<QImage::Format>("format");
QTest::addColumn<QColor>("color");
QTest::addColumn<QColor>("bottomRightColor");
const QColor red(Qt::red);
const QColor green(Qt::green);
const QColor blue(Qt::blue);
const QColor gray(Qt::gray);
const QColor gray555(0x5a, 0x5a, 0x5a); // Note: Interpolation 8<->5 bit occurs.
const QColor white(Qt::white);
const QColor black(Qt::black);
QTest::newRow("argb32p-red") << QImage::Format_ARGB32_Premultiplied << red << gray;
QTest::newRow("argb32p-green") << QImage::Format_ARGB32_Premultiplied << green << gray;
QTest::newRow("argb32p-blue") << QImage::Format_ARGB32_Premultiplied << blue << gray;
QTest::newRow("rgb888-red") << QImage::Format_RGB888 << red << gray;
QTest::newRow("rgb888-green") << QImage::Format_RGB888 << green << gray;
QTest::newRow("rgb888-blue") << QImage::Format_RGB888 << blue << gray;
QTest::newRow("indexed8-red") << QImage::Format_Indexed8 << red << gray;
QTest::newRow("indexed8-green") << QImage::Format_Indexed8 << green << gray;
QTest::newRow("indexed8-blue") << QImage::Format_Indexed8 << blue << gray;
QTest::newRow("rgb555-red") << QImage::Format_RGB555 << red << gray555;
QTest::newRow("rgb555-green") << QImage::Format_RGB555 << green << gray555;
QTest::newRow("rgb555-blue") << QImage::Format_RGB555 << blue << gray555;
QTest::newRow("mono") << QImage::Format_Mono << white << black;
}
// Test image filled with color, black pixel at botttom right corner.
static inline QImage createTestImage(QImage::Format format, int width, int height,
const QColor &fillColor, const QColor &bottomRightColor)
{
QImage image(QSize(width, height), format);
image.fill(fillColor);
QPainter painter(&image);
QPen pen = painter.pen();
pen.setColor(bottomRightColor);
painter.setPen(pen);
painter.drawPoint(width -1, height - 1);
return image;
}
void tst_QImage::toWinHBITMAP()
{
static const int width = 73;
static const int height = 57;
QFETCH(QImage::Format, format);
QFETCH(QColor, color);
QFETCH(QColor, bottomRightColor);
// Cannot paint on indexed/mono images.
const QImage image = format == QImage::Format_Indexed8 || format == QImage::Format_Mono
? createTestImage(QImage::Format_RGB32, width, height, color, bottomRightColor).convertToFormat(format)
: createTestImage(format, width, height, color, bottomRightColor);
const HBITMAP bitmap = qt_imageToWinHBITMAP(image);
QVERIFY(bitmap != 0);
// Verify size
BITMAP bitmapInfo;
memset(&bitmapInfo, 0, sizeof(BITMAP));
const int res = GetObject(bitmap, sizeof(BITMAP), &bitmapInfo);
QVERIFY(res);
QCOMPARE(width, int(bitmapInfo.bmWidth));
QCOMPARE(height, int(bitmapInfo.bmHeight));
const HDC displayDc = GetDC(0);
const HDC bitmapDc = CreateCompatibleDC(displayDc);
const HBITMAP nullBitmap = static_cast<HBITMAP>(SelectObject(bitmapDc, bitmap));
QCOMPARE(COLORREFToQColor(GetPixel(bitmapDc, 0, 0)), color);
QCOMPARE(COLORREFToQColor(GetPixel(bitmapDc, width - 1, 3)), color);
QCOMPARE(COLORREFToQColor(GetPixel(bitmapDc, 3, height - 1)), color);
QCOMPARE(COLORREFToQColor(GetPixel(bitmapDc, width - 1, height - 1)), bottomRightColor);
const QImage convertedBack = qt_imageFromWinHBITMAP(bitmap);
QCOMPARE(convertedBack.convertToFormat(QImage::Format_ARGB32_Premultiplied),
image.convertToFormat(QImage::Format_ARGB32_Premultiplied));
// Clean up
SelectObject(bitmapDc, nullBitmap);
DeleteObject(bitmap);
DeleteDC(bitmapDc);
ReleaseDC(0, displayDc);
}
void tst_QImage::fromMonoHBITMAP() // QTBUG-72343, corruption for mono bitmaps
{
enum : int { width = 32, height = 32, size = width * height / 8 }; // 32x32 mono bitmap
char bitmapData[size];
memset(bitmapData, 0, size);
const HBITMAP hbitmap = CreateBitmap(width, height, /* planes */ 1, /* bitcount */ 1, bitmapData);
const QImage image = qt_imageFromWinHBITMAP(hbitmap);
QCOMPARE(image.size(), QSize(width, height));
QCOMPARE(image.scanLine(0)[0], 0u);
DeleteObject(hbitmap);
}
#endif // Q_OS_WIN && !Q_OS_WINRT
QTEST_GUILESS_MAIN(tst_QImage)
#include "tst_qimage.moc"
|