gdi_primitive.c 146 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*******************************************************************************
 * Filename:
 * ---------
 *	 gdi_primitive.c
 *
 * Project:
 * --------
 *  MAUI
 *
 * Description:
 * ------------
 *  GDI Primitive related.
 *
 * Author:
 * -------
 * -------
 *
 *==============================================================================
 * 				HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *==============================================================================
 *******************************************************************************/
#include "gdi_internal.h"
#include "gd_primitive.h"
#include "stdlib.h"
#include "math.h"
#include "gdi_const.h"
#include "gdi_include.h"

#include "kal_general_types.h"
#include "kal_release.h"
#include "string.h"
#include "lcd_sw_rnd.h"
#include "setjmp.h"
#include "MMIDataType.h"
#include "DebugInitDef_Int.h"

#include "gdi_datatype.h"
#include "gdi_mutex.h"
#include "gdi_primitive.h"
#include "gd_include.h"
#include "gd_primitive_arm.h"

#include "gdi_lcd_config.h"
#include "lcd_sw_inc.h"

#include "gdi_internal.h"
#include "med_global.h"
#include "med_utility.h"


//warning
extern void gdi_img_buf_blending_color_PARGB6666(
    gdi_img_buf_struct *dst_buf, 
    gdi_color pargb8888_color,
    S32 width,
    S32 height);


#if !defined(GDI_SLIM_COLOR_FORMAT) || defined(GDI_LAYER_PARGB6666_SUPPORT)
void gdi_memset24(U8* address,U32 pixel,int size);
#endif
void gdi_memset32(U8* address,U32 pixel,int size);

/****************************************************************************
* Global variable 
*****************************************************************************/

jmp_buf						gdi_jmpbuf;
BOOL						gdi_jmpbuf_is_used=FALSE;

/****************************************************************************
* MACRO
*****************************************************************************/
#define SWAP(t,a,b) do {t tmp=a;a=b;b=tmp;} while(0)

/* check if pixel within clip, if not will return */
#define	GDI_CLIP_POINT_TEST(x, y)\
{\
	if( (x < GDI_LAYER.clipx1) || (x > GDI_LAYER.clipx2) ||\
		(y < GDI_LAYER.clipy1) || (y > GDI_LAYER.clipy2) )\
	{\
		GDI_RETURN_VOID;	\
	}\
}

#define	GDI_CLIP_TEXT_POINT_TEST(x, y)\
{\
	if( (x < GDI_LAYER.act_text_clip.x1) || (x > GDI_LAYER.act_text_clip.x2) ||\
		(y < GDI_LAYER.act_text_clip.y1) || (y > GDI_LAYER.act_text_clip.y2) )\
	{\
		GDI_RETURN_VOID;	\
	}\
}


#define GDI_CLIP_LINE_X_TEST(X1, X2)\
{\
	if(X1 < GDI_LAYER.clipx1)\
		X1 = GDI_LAYER.clipx1;\
	if(X2 > GDI_LAYER.clipx2)\
		X2 = GDI_LAYER.clipx2;\
}

#define GDI_CLIP_LINE_Y_TEST(Y1, Y2)\
{\
	if(Y1 < GDI_LAYER.clipy1)\
		Y1 = GDI_LAYER.clipy1;\
	if(Y2 > GDI_LAYER.clipy2)\
		Y2 = GDI_LAYER.clipy2;\
}

#define GDI_CLIP_LINE_XY_TEST(x1,y1,x2,y2)\
{\
	if(x1 < GDI_LAYER.clipx1)\
		x1 = GDI_LAYER.clipx1;\
	if(x2 > GDI_LAYER.clipx2)\
		x2 = GDI_LAYER.clipx2;\
	if(y1 < GDI_LAYER.clipy1)\
		y1 = GDI_LAYER.clipy1;\
	if(y2 > GDI_LAYER.clipy2)\
		y2 = GDI_LAYER.clipy2;\
}

#define AA_LINE_PRECISION_BITS 16
#define AA_LINE_FLOAT ((S32)((float)(x)*(float)(1 << AA_LINE_PRECISION_BITS)))


/*****************************************************************************
 * FUNCTION
 *  GAUSSIAN_INTEGRAL
 * DESCRIPTION
 *  return the value of gaussian integral
 * PARAMETERS
 *  dis         [IN]    the x (the distance from x to zero point)
 *  integral    [OUT]   the integral of gaussian from dis to infinity
 * RETURNS
 *  void
 *****************************************************************************/
#define GAUSSIAN_INTEGRAL(dis, integral)                    \
do                                                          \
{                                                           \
    S32 idx;                                                \
    if (dis > (2 << AA_LINE_PRECISION_BITS) ||              \
        dis < -(2 << AA_LINE_PRECISION_BITS))               \
    {                                                       \
        integral = 0;                                       \
    }                                                       \
    if (dis >= 0)                                           \
    {                                                       \
        idx = (dis * 10) >> AA_LINE_PRECISION_BITS;         \
        integral = gaussian_integral[idx];                  \
    }                                                       \
    else                                                    \
    {                                                       \
        dis = -dis;                                         \
        idx = (dis * 10) >> AA_LINE_PRECISION_BITS;         \
        integral = ((1 << AA_LINE_PRECISION_BITS) -         \
                    gaussian_integral[idx]);                \
    }                                                       \
} while(0)


/*****************************************************************************
 * FUNCTION
 *  ANTIALIASING_LINE_PUT_PIXEL
 * DESCRIPTION
 *  put pixel in gdi_draw_antialiasing_line
 * PARAMETERS
 *  x           [IN]    x (position of the point to be draw)
 *  y           [IN]    y
 *  lineR       [IN]    the red color of line
 *  lineG       [IN]    the green color of line
 *  lineB       [IN]    the blue color of line
 *  dis_to_line [IN]    the distance that the pixel to the line
 * RETURNS
 *  void
 *****************************************************************************/
#define ANTIALIASING_LINE_PUT_PIXEL(x, y, lineR, lineG, lineB, dis_to_line)                         \
do                                                                                                  \
{                                                                                                   \
    U32 bgA, bgR, bgG, bgB;                                                                         \
    /*U32 lineA, lineR, lineG, lineB;*/                                                             \
    U32 colorR, colorG, colorB;                                                                     \
    gdi_color bg_color;                                                                             \
    S32 integral, integral_inver, g1, g2, dis1, dis2;                                               \
                                                                                                    \
    dis1 = abs(dis_to_line) - (1 << (AA_LINE_PRECISION_BITS-1));                                    \
    dis2 = abs(dis_to_line) + (1 << (AA_LINE_PRECISION_BITS-1));                                    \
    GAUSSIAN_INTEGRAL(dis1, g1);                                                                    \
    GAUSSIAN_INTEGRAL(dis2, g2);                                                                    \
    integral = g1 - g2;                                                                             \
    integral_inver = (1 << AA_LINE_PRECISION_BITS) - integral;                                      \
    bg_color = gdi_act_get_pixel(x, y);                                                             \
    gdi_act_color_to_rgb(&bgA, &bgR, &bgG, &bgB, bg_color);                                         \
    /* if background color is transparent color, use color in abm_src_layer */                      \
    if (bg_color == source_key_value && source_key_enable && abm_src_layer != GDI_ERROR_HANDLE)     \
    {                                                                                               \
        bg_color = src_layer_get_pixel(src_layer_buf_ptr, src_layer_layer_width, x, y); \
        src_layer_color_to_rgb(&bgA, &bgR, &bgG, &bgB, bg_color);                               \
    }                                                                                               \
    colorR = (U32)((integral_inver * bgR) + (integral * lineR)) >> AA_LINE_PRECISION_BITS;          \
    colorG = (U32)((integral_inver * bgG) + (integral * lineG)) >> AA_LINE_PRECISION_BITS;          \
    colorB = (U32)((integral_inver * bgB) + (integral * lineB)) >> AA_LINE_PRECISION_BITS;          \
    gdi_draw_point(x, y, gdi_act_color_from_rgb(bgA, colorR, colorG, colorB));                   \
}while(0)

/* draw horizontal line */
__inline void GDI_DRAW_H_LINE(S32 x1, S32 x2, S32 y, gdi_color pixel_color)
{
	GDI_ENTER_CRITICAL_SECTION(GDI_DRAW_H_LINE)
		S32	h_index;
		for(h_index = x1; h_index <= x2; h_index++)
		{
			GDI_SET_BUFFER_PIXEL(h_index, y, pixel_color);
		}
	GDI_EXIT_CRITICAL_SECTION(GDI_DRAW_H_LINE)
}

__inline void GDI_DRAW_H_LINE_STYLE(S32 x1, S32 x2, S32 y, gdi_color pixel_color, U8 cycle, const U8 *bitvalues)
{
	GDI_ENTER_CRITICAL_SECTION(GDI_DRAW_H_LINE_STYLE)
		S32		h_index;
		kal_uint8 bitcount;
		for(h_index = x1, bitcount=0; h_index <= x2; h_index++, bitcount++, bitcount %= cycle)
		{
			if (bitvalues[bitcount])
			{
				GDI_SET_BUFFER_PIXEL(h_index, y, pixel_color);
			}
		}
	GDI_EXIT_CRITICAL_SECTION(GDI_DRAW_H_LINE_STYLE)
}

/* draw vertical line */
__inline void GDI_DRAW_V_LINE(S32 y1, S32 y2, S32 x, gdi_color pixel_color)
{
	GDI_ENTER_CRITICAL_SECTION(GDI_DRAW_V_LINE)
		S32	v_index;

		for(v_index = y1; v_index <= y2; v_index++)
		{
			GDI_SET_BUFFER_PIXEL(x,v_index,pixel_color);
		}
	GDI_EXIT_CRITICAL_SECTION(GDI_DRAW_V_LINE)
}

__inline void GDI_DRAW_V_LINE_STYLE(S32 y1, S32 y2, S32 x, gdi_color pixel_color, kal_uint8 cycle, const kal_uint8 *bitvalues)
{
	GDI_ENTER_CRITICAL_SECTION(GDI_DRAW_V_LINE_STYLE)
		S32		v_index;
		U8		bitcount;
		for(v_index = y1, bitcount=0; v_index <= y2; v_index++, bitcount++, bitcount %= cycle)
		{
			if (bitvalues[bitcount])
			{
				GDI_SET_BUFFER_PIXEL(x, v_index, pixel_color);
			}
		}
	GDI_EXIT_CRITICAL_SECTION(GDI_DRAW_V_LINE_STYLE)
}

static __inline void GDI_DRAW_RECT(S32 x1, S32 y1, S32 x2, S32 y2, gdi_color pixel_color)
{
	S32	r_index;
	for(r_index = y1; r_index <=y2; r_index++)
	{
		GDI_DRAW_H_LINE(x1, x2, r_index, pixel_color);
	}
}

static __inline void GDI_DRAW_RECT16(S32 x1, S32 y1, S32 x2, S32 y2, gdi_color pixel_color)
{
	U8* dest = GDI_LAYER.buf_ptr + ((x1+y1*GDI_LAYER.width)<<1);

	y2 -=y1; 
	y2++;

	x2-=x1;
	x2++;
	x2<<=1;
	x1 = GDI_LAYER.width<<1;
	
	while(y2--)
	{
		gdi_memset16(dest,(U16)pixel_color,x2);
		dest +=x1;
	}
}
#if !defined(GDI_SLIM_COLOR_FORMAT)

#ifdef GDI_PRIMITIVE_24_BIT_SUPPORT
static __inline void GDI_DRAW_RECT24(S32 x1, S32 y1, S32 x2, S32 y2, gdi_color pixel_color)
{
	U8* dest = GDI_LAYER.buf_ptr + ((x1+y1*GDI_LAYER.width) * 3);

	y2 -=y1; 
	y2++;

	x2-=x1;
	x2++;
	x2 *= 3;
	x1 = GDI_LAYER.width * 3;
	
	while(y2--)
	{
		gdi_memset24(dest,(U32)pixel_color,x2);
		dest +=x1;
	}
}
#endif

#endif
static __inline void GDI_DRAW_RECT32(S32 x1, S32 y1, S32 x2, S32 y2, gdi_color pixel_color)
{
	U8* dest = GDI_LAYER.buf_ptr + ((x1+y1*GDI_LAYER.width)<<2);

	y2 -=y1; 
	y2++;

	x2-=x1;
	x2++;
	x2<<=2;
	x1 = GDI_LAYER.width<<2;
	
	while(y2--)
	{
		gdi_memset32(dest,(U32)pixel_color,x2);
		dest +=x1;
	}
}

static void gdi_color_interpolate(gdi_color color0, gdi_color color1, U32 orig_ratio, gdi_color *result)
{
    U32 a0,r0,g0,b0;
    U32 a1,r1,g1,b1;
    U32 _100_RATIO = 100 - orig_ratio;
    gdi_act_color_to_rgb(&a0, &r0, &g0, &b0, color0);
    gdi_act_color_to_rgb(&a1, &r1, &g1, &b1, color1);
    *result = gdi_act_color_from_rgb(
                    (a0 * orig_ratio + a1 * _100_RATIO) / 100,
                    (r0 * orig_ratio + r1 * _100_RATIO) / 100,
                    (g0 * orig_ratio + g1 * _100_RATIO) / 100,
                    (b0 * orig_ratio + b1 * _100_RATIO) / 100);
}
/**************************************************************

	FUNCTION NAME		: gdi_draw_point()

  	PURPOSE				: draw a point

	INPUT PARAMETERS	: S32			x
							  S32			y
							  gdi_color pixel_color

	OUTPUT PARAMETERS	: nil

	RETURNS				: void

	REMARKS				: nil

***************************************************************/
__inline void gdi_draw_point(S32 x, S32 y, gdi_color pixel_color)
{
	GDI_ENTER_CRITICAL_SECTION(gdi_draw_point)
		GDI_CLIP_POINT_TEST(x,y);
		GDI_SET_BUFFER_PIXEL(x,y,pixel_color);
	GDI_EXIT_CRITICAL_SECTION(gdi_draw_point)
}

void gdi_draw_line_ext(S32 x1, S32 y1, S32 x2, S32 y2, gdi_color line_color, BOOL is_dotted)
{
    GDI_ENTER_CRITICAL_SECTION(gdi_draw_line_ext)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    BOOL steep;                                             /* y-scan or x-scan */
    S32 slope, dx, dy, d, incrE, incrNE, x, y;              /* slope is the direction that y goes */
    S32 rate, tmp;                                          /* used in cliping */
    S32 clipx1, clipx2, clipy1, clipy2;                     /* the clip area */
    BOOL draw_dot = TRUE;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT((GDI_LAYER.clipy1 <= GDI_LAYER.clipy2) && (GDI_LAYER.clipx1 <= GDI_LAYER.clipx2));

    {
        clipx1 = x1;    /* clipx1,y1,x2,y2 is only used as tmp variables here */
        clipy1 = y1;
        clipx2 = x2;
        clipy2 = y2;
        if (x1 > x2)
        {
            clipx1 = x2;
            clipx2 = x1;
        }
        if (y1 > y2)
        {
            clipy1 = y2;
            clipy2 = y1;
        }
        /* return if the line is not in the clip region */
        GDI_CLIP_RECT_TEST(clipx1, clipy1, clipx2, clipy2, GDI_RETURN_VOID);
    }
    
    if ((!is_dotted) && (y1 == y2) && (y1 >= GDI_LAYER.clipy1) && (y1 <= GDI_LAYER.clipy2)) // Horizontal Line /
	{
		if(x1 > x2)     // exchange x /
		{
		    tmp = x1;
            x1 = x2;
            x2 = tmp;
		}
        if((x1 <= GDI_LAYER.clipx2) && (x2 >= GDI_LAYER.clipx1))
        {
            GDI_CLIP_LINE_X_TEST(x1,x2);
            GDI_DRAW_H_LINE(x1, x2, y1, line_color);
    	}
	}
	else if ((!is_dotted) && (x1 == x2) && (x1 >= GDI_LAYER.clipx1) && (x1 <= GDI_LAYER.clipx2)) // Vertical Line /
    {
		if(y1 > y2)     // exchange y /
		{
			tmp = y1;
            y1 = y2;
            y2 = tmp;
		}
        if((y1 <= GDI_LAYER.clipy2) && (y2 >= GDI_LAYER.clipy1))
        {
            GDI_CLIP_LINE_Y_TEST(y1,y2);
            GDI_DRAW_V_LINE(y1, y2, x1, line_color);
	    }
	}
    else
    {
        /* clip */
        gdi_layer_get_clip(&clipx1, &clipy1, &clipx2, &clipy2);

        if (x1 > x2)    
        {
            /* exchange two points: (x1,y1) and (x2,y2)*/
            tmp = x1;
            x1 = x2;
            x2 = tmp;

            tmp = y1;
            y1 = y2;
            y2 = tmp;
        }
        dx = x2 - x1;
        dy = y2 - y1;
        
        if (dx == 0)
        {
            rate = 0;
        }
        else
        {
            rate = (dy << 20) / dx;
        }
        
        if (y1 <= y2) 
    	{
    		if (y1 < clipy1) 
    		{
    			int orgy = y1;
    			y1 = clipy1;
    			if (y1 > y2)
    				GDI_RETURN_VOID;
    			x1 = x1 - ((orgy - y1) * dx) / dy;
    		}

    		if (y2 > clipy2) 
    		{
    			int orgy = y2;
    			y2 = clipy2;
    			if (y1 > y2)
    				GDI_RETURN_VOID;
    			x2 = x2 - ((orgy - y2) * dx) / dy;
    		}
    	} 
    	else 
    	{ /* y1 > y2 */
    		if (y2 < clipy1) 
    		{
    			int orgy = y2;
    			y2 = clipy1;
    			if (y2 > y1)
    				GDI_RETURN_VOID;
    			x2 = x2 - ((orgy - y2) * dx) / dy;
    		}

    		if (y1 > clipy2) 
    		{
    			int orgy = y1;
    			y1 = clipy2;
    			if (y2 > y1)
    				GDI_RETURN_VOID;
    			x1 = x1 - ((orgy - y1) * dx) / dy;
    		}
    	}

    	if (x1 < clipx1) 
    	{
    		int orgx = x1;
    		x1 = clipx1;
    		if (x1 > x2)
    			GDI_RETURN_VOID;
    		y1 = y1 - (((orgx - x1) * rate) >> 20);
    	}

    	if (x2 > clipx2) 
    	{
    		int orgx = x2;
    		x2 = clipx2;
    		if (x1 > x2)
    			GDI_RETURN_VOID;
    		y2 = y2 - (((orgx - x2) * rate) >> 20);
    	}
        
        /* if the slope>1, need to exchange x-axis and y-axis */
        steep = (abs(y2 - y1) > abs(x2 - x1));  
        if (steep)
        {
            /* exchange x1 and y1, x2 and y2 */
            tmp = x1;
            x1 = y1;
            y1 = tmp;

            tmp = x2;
            x2= y2;
            y2 = tmp;
        }
        
        if (x1 > x2)    
        {
            /* exchange two points: (x1,y1) and (x2,y2) */
            tmp = x1;
            x1 = x2;
            x2 = tmp;

            tmp = y1;
            y1 = y2;
            y2 = tmp;
        }
        
        dx = x2 - x1;
        dy = y2 - y1;
        
        if (dy < 0)
        {
            slope = -1;
            dy = -dy;
        }
        else
        {
            slope = 1;
        }

        /* variables for mid-point draw line algorithm */
        d = (2 * dy) - dx;
        incrE = 2 * dy;
        incrNE = 2 * (dy - dx);

        for (x = x1, y = y1; x <= x2; x++)
        {
            S32 outx, outy;
            if (steep)
            {
                outx = y;
                outy = x;
            }
            else
            {
                outx = x;
                outy = y;
            }

            if (is_dotted)
            {
                if (draw_dot == FALSE)
                {
                    draw_dot = TRUE;
                }
                else
                {
                    GDI_CLIP_POINT_TEST(outx,outy);
                    gdi_act_put_pixel(outx, outy, line_color);
                    draw_dot = FALSE;
                }
            }
            else
            {
                GDI_CLIP_POINT_TEST(outx,outy);
                gdi_act_put_pixel(outx, outy, line_color);
            }
 
            if (d < 0)
            {
                d += incrE;
            }
            else
            {
                d += incrNE;
                y += slope;
            }
        }
    }
 
    GDI_EXIT_CRITICAL_SECTION(gdi_draw_line_ext)
}


/**************************************************************

	FUNCTION NAME		: gdi_draw_line()

  	PURPOSE				: draw a line

	INPUT PARAMETERS	: S32			x1
							  S32			y1
							  S32			x2
							  S32			y2
							  gdi_color line_color

	OUTPUT PARAMETERS	: nil

	RETURNS				: void

	REMARKS				: nil

***************************************************************/
void gdi_draw_line(S32 x1, S32 y1, S32 x2, S32 y2, gdi_color line_color)
{
	gdi_draw_line_ext(x1,y1,x2,y2, line_color,FALSE);
}


/**************************************************************

	FUNCTION NAME		: gdi_draw_line_style()

  	PURPOSE				: draw a style line

	INPUT PARAMETERS	: S32			x1
							  S32			y1
							  S32			x2
							  S32			y2
							  gdi_color line_color
							  U8			cycle
							  const U8  *bitvalues
	OUTPUT PARAMETERS	: nil

	RETURNS				: void

	REMARKS				: nil

***************************************************************/
void gdi_draw_line_style(S32 x1, S32 y1, S32 x2, S32 y2, gdi_color line_color, U8 cycle, const U8 *bitvalues)
{
	GDI_ENTER_CRITICAL_SECTION(gdi_draw_line_style)
		if(y1 - y2 == 0) /* Horizontal Line */
		{
			/* within valid y clip */
			if((y1 >= GDI_LAYER.clipy1) && (y1 <= GDI_LAYER.clipy2))
			{
				if((x1 < x2) && (x1 <= GDI_LAYER.clipx2) && (x2 >= GDI_LAYER.clipx1))
				{
					GDI_CLIP_LINE_X_TEST(x1,x2);
					GDI_DRAW_H_LINE_STYLE(x1, x2, y1, line_color, cycle, bitvalues);
				}
				else if((x1 >= x2) && (x2 <= GDI_LAYER.clipx2) && (x1 >= GDI_LAYER.clipx1))
				{
					GDI_CLIP_LINE_X_TEST(x2,x1);
					GDI_DRAW_H_LINE_STYLE(x2, x1, y1, line_color, cycle, bitvalues);
				}
			}
		}
		else if(x1 - x2 == 0) /* Vertical Line */
		{
			/* within valid x clip */
			if((x1 >= GDI_LAYER.clipx1) && (x1 <= GDI_LAYER.clipx2))
			{
				if((y1 < y2) && (y1 <= GDI_LAYER.clipy2) && (y2 >= GDI_LAYER.clipy1))
				{
					GDI_CLIP_LINE_Y_TEST(y1,y2);
					GDI_DRAW_V_LINE_STYLE(y1, y2, x1, line_color, cycle, bitvalues);
				}
				else if((y1 >= y2) && (y2 <= GDI_LAYER.clipy2) && (y1 >= GDI_LAYER.clipy1))
				{
					GDI_CLIP_LINE_Y_TEST(y2,y1);
					GDI_DRAW_V_LINE_STYLE(y2, y1, x1, line_color, cycle, bitvalues);
				}
			}
		}
		else
		{
			gdi_draw_line_ext(x1,y1,x2,y2, line_color,TRUE); //FIXME: should support real dotted & cycle
		}
	GDI_EXIT_CRITICAL_SECTION(gdi_draw_line_style)
}



/**************************************************************

	FUNCTION NAME		: gdi_draw_rect()

  	PURPOSE				: draw a rect (border only)

	INPUT PARAMETERS	: S32			x1
							  S32			y1
							  S32			x2
							  S32			y2
							  gdi_color frame_color

	OUTPUT PARAMETERS	: nil

	RETURNS				: void

	REMARKS				: nil

***************************************************************/
void gdi_draw_rect(S32 x1, S32 y1, S32 x2, S32 y2, gdi_color frame_color)
{
	GDI_ENTER_CRITICAL_SECTION(gdi_draw_rect)
		S32 nx1,ny1,nx2,ny2;

		nx1 = x1;
		ny1 = y1;
		nx2 = x2;
		ny2 = y2;

		GDI_CLIP_RECT_TEST(nx1,ny1,nx2,ny2,GDI_RETURN_VOID);

		if(GDI_LAYER.clipy1<= y1 && y1 <= GDI_LAYER.clipy2)
			GDI_DRAW_H_LINE(nx1, nx2, y1, frame_color);	/* up horizontal */

		if(GDI_LAYER.clipy1<= y2 && y2 <= GDI_LAYER.clipy2)
			GDI_DRAW_H_LINE(nx1, nx2, y2, frame_color);	/* down horizontal */

		if(GDI_LAYER.clipx1<= x1 && x1 <= GDI_LAYER.clipx2)
			GDI_DRAW_V_LINE(ny1, ny2, x1, frame_color);	/* left vertical */

		if(GDI_LAYER.clipx1<= x2 && x2 <= GDI_LAYER.clipx2)
			GDI_DRAW_V_LINE(ny1, ny2, x2, frame_color);	/* right vertical */
		
	GDI_EXIT_CRITICAL_SECTION(gdi_draw_rect)
}



/**************************************************************

	FUNCTION NAME		: gdi_draw_solid_rect()

  	PURPOSE				: draw a solid rect

	INPUT PARAMETERS	: S32			x1
							  S32			y1
							  S32			x2
							  S32			y2
							  gdi_color rect_color

	OUTPUT PARAMETERS	: nil

	RETURNS				: void

	REMARKS				: nil

***************************************************************/
void gdi_draw_solid_rect(S32 x1, S32 y1, S32 x2, S32 y2, gdi_color rect_color)
{
    GDI_ENTER_CRITICAL_SECTION(gdi_draw_solid_rect)
    do
    {
        GDI_CLIP_RECT_TEST(x1,y1,x2,y2,break);
    
        if(GDI_LAYER.bits_per_pixel == 16)
        {
            if (GDI_LAYER.cf == GDI_COLOR_FORMAT_UYVY422)
            {
                rect_color = 0x0080; /* Only support clear to black */
            }
        
            GDI_DRAW_RECT16(x1, y1, x2, y2, rect_color);
        }
        else if ((GDI_LAYER.bits_per_pixel == 32) && (rect_color == GDI_COLOR_TRANSPARENT))
        {   /* speed up 32-bit performance by memset(), it is safe to replace GDI_COLOR_TRANSPARENT as 0 since both alpha = 0 */
            U32 *p;
            S32 line_width, pitch; 
            S32 y;

            line_width = x2 - x1 + 1;
            pitch = GDI_LAYER.width;
            p = (U32*)GDI_LAYER.buf_ptr + x1 + y1 * pitch;

            for (y = y2 - y1; y >= 0; y--)
            {
                memset(p, 0, line_width * 4);
                p += pitch; 
            }               
        }
#if !defined(GDI_SLIM_COLOR_FORMAT)
    #ifdef GDI_PRIMITIVE_24_BIT_SUPPORT
        else if (GDI_LAYER.bits_per_pixel == 24)
        {
            kal_uint32 cbyte = (rect_color & 0xff);
            if (cbyte == (rect_color << 16 >> 24) &&
                cbyte == (rect_color << 8 >> 24))
            {
                kal_uint32 pitch;
                kal_int32 line_width;
                kal_int32 line_count;
                kal_uint8 *line_ptr;

                line_width = (x2 - x1 + 1) * 3;
                pitch = GDI_LAYER.width * 3;
                line_ptr = (kal_uint8*)GDI_LAYER.buf_ptr + y1 * pitch + x1 * 3;
                for (line_count = y2 - y1; line_count >= 0; line_count--)
                {
                    memset(line_ptr, cbyte, line_width);
                    line_ptr += pitch;
                }
            }
            else
            {
                GDI_DRAW_RECT24(x1, y1, x2, y2, rect_color);
            }
        }
    #endif
#endif
        else if(GDI_LAYER.bits_per_pixel == 32)
        {
            GDI_DRAW_RECT32(x1, y1, x2, y2, rect_color);                
        }
        else
        {
            GDI_DRAW_RECT(x1, y1, x2, y2, rect_color);
        }
    }while(0);
    GDI_EXIT_CRITICAL_SECTION(gdi_draw_solid_rect)
}


/*****************************************************************************
 * FUNCTION
 *  gdi_draw_darken_rect
 * DESCRIPTION
 *  Darken all pixels in a rectangle. Trasnparent pixels are not supported.
 * PARAMETERS
 *  x1                 [IN]     x1
 *  y1                 [IN]     y1
 *  x2                 [IN]     x2
 *  y2                 [IN]     y2
 *  brightness_value   [IN]     brightness. Range: 0 (darkest) to 100 (brightest).
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_draw_darken_rect(S32 x1, S32 y1, S32 x2, S32 y2, S32 brightness_value)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    GDI_ENTER_CRITICAL_SECTION(gdi_draw_darken_rect)	

    do
    {
        GDI_CLIP_RECT_TEST(x1, y1, x2, y2, break);

        /* use alpha blending to achieve the darken effect */
        // TODO: Use a dedicated function for darkening.
        gdi_effect_alpha_blending_rect((gdi_handle)gdi_act_layer, x1, y1, x2, y2, (100 - brightness_value) * 256 / 100, 0, 0, 0);
    } while(0);

    GDI_EXIT_CRITICAL_SECTION(gdi_draw_darken_rect)        
}


void gdi_draw_greyscale_rect(S32 x1,S32 y1,S32 x2,S32 y2, S32 white_value,S32 black_value)
{
	GDI_ENTER_CRITICAL_SECTION(gdi_draw_greyscale_rect)	
	S32 x, y;
	gdi_color color;
	U32 a,r, g, b;//, max, min;
	gd_get_pixel_func get_ptr = gd_get_pixel[gdi_act_layer->cf];
	gd_put_pixel_func put_ptr = gd_put_pixel[gdi_act_layer->cf];
	S32 BW256,WB_B;

	GDI_CLIP_RECT_TEST(x1,y1,x2,y2,GDI_RETURN_VOID);
	
		{
			black_value = 256 - black_value;
		
			BW256 = (white_value*black_value)<<8;
		
			WB_B = black_value*(256 - white_value);
		
			for (y = y1; y <= y2; y++)
			{
				for (x = x1; x <= x2; x++)
				{
					color = get_ptr(x,y);
					gdi_act_color_to_rgb(&a,&r,&g,&b,color);
					r = (r + g + b)/3;
					//r = r + (256-r) * w / 256
					//r = r * b / 256
					r = (BW256 + r*WB_B) >> 16;
					if (r > 255)
						r = 255;
					put_ptr(x,y,gdi_act_color_from_rgb(a,r,r,r));
				}
			}
		}
	GDI_EXIT_CRITICAL_SECTION(gdi_draw_greyscale_rect)	
}


void gdi_effect_alpha_blending_rect_16(
        gdi_handle src_layer_handle,
        S32 x1,
        S32 y1,
        S32 x2,
        S32 y2,
        U32 transparent_color_a,
        U32 transparent_color_r,
        U32 transparent_color_g,
        U32 transparent_color_b)
{
    GDI_ENTER_CRITICAL_SECTION(gdi_effect_alpha_blending_rect_16)
    
    do {
        gdi_layer_struct *src_layer, *dest_layer;
        kal_uint32 src_pitch, dest_pitch;
        kal_uint32 src_delta, dest_delta;
        kal_uint8 *src_buffer, *dest_buffer;
        kal_int32 pixel_count, line_count;
        kal_int32 inv_a;
        kal_uint32 color_prgb888;
        
        src_layer = (gdi_layer_struct*)src_layer_handle;
        dest_layer = gdi_act_layer;

        GDI_ASSERT(src_layer->bits_per_pixel == 16 && dest_layer->bits_per_pixel == 16);

        GDI_CLIP_RECT_TEST(x1, y1, x2, y2, break);

        src_pitch = src_layer->width * 2;
        dest_pitch = dest_layer->width * 2;
        src_buffer = src_layer->buf_ptr + src_pitch * y1 + x1 * 2;
        dest_buffer = dest_layer->buf_ptr + dest_pitch * y1 + x1 * 2;
        src_delta = src_pitch - (x2 - x1 + 1) * 2;
        dest_delta = dest_pitch - (x2 - x1 + 1) * 2;
        color_prgb888 = GDI_PARGB8888(
            0,
            GDI_DIV_255(transparent_color_r * transparent_color_a),
            GDI_DIV_255(transparent_color_g * transparent_color_a),
            GDI_DIV_255(transparent_color_b * transparent_color_a));
        inv_a = 255 - transparent_color_a;
        for (line_count = (y2 - y1 + 1); line_count > 0; line_count--)
        {
            for (pixel_count = (x2 - x1 + 1); pixel_count > 0; pixel_count--)
            {
                register kal_uint32 rgb565, rgb888;
                kal_uint32 r, g, b;
                
                rgb565 = GDI_MEM_GET_16(src_buffer);
                rgb888 = GDI_RGB888_FROM_RGB565(rgb565);
                r = GDI_DIV_255(GDI_R_OF_RGB888(rgb888) * inv_a);
                g = GDI_DIV_255(GDI_G_OF_RGB888(rgb888) * inv_a);
                b = GDI_DIV_255(GDI_B_OF_RGB888(rgb888) * inv_a);
                rgb888 = GDI_RGB888(r, g, b);
                rgb888 += color_prgb888;
                rgb565 = GDI_RGB565_FROM_RGB888(rgb888);

                GDI_MEM_PUT_16(dest_buffer, rgb565);

                src_buffer += 2;
                dest_buffer += 2;
            }

            src_buffer += src_delta;
            dest_buffer += dest_delta;
        }
    } while (0);
    
    GDI_EXIT_CRITICAL_SECTION(gdi_effect_alpha_blending_rect_16)
}


void gdi_effect_alpha_blending_rect_ex(
                                gdi_handle src_layer_handle,
                                S32 x1,S32 y1,S32 x2,S32 y2, 
                                U32 transparent_color_a,
                                U32 transparent_color_r,
                                U32 transparent_color_g,
                                U32 transparent_color_b)
{
	GDI_ENTER_CRITICAL_SECTION(gdi_effect_alpha_blending_rect_ex)	
    {
		S32 x, y;
		gdi_color color;
		U32 a, r, g, b;
        gdi_layer_struct *src_layer = (gdi_layer_struct*) src_layer_handle;
		U8* src_buf = src_layer->buf_ptr;
		S32 src_layer_width = src_layer->width;
		gd_get_buf_pixel_func get_ptr = gd_get_buf_pixel[src_layer->cf];
        gd_color_to_rgb_func src_color_to_rgb = gd_color_to_rgb[src_layer->cf];
        
		S32 opacity_value = transparent_color_a * 256 / 255;
		S32 inverse_opacity_value = 256 - transparent_color_a * 256 / 255;
		S32 offset_x = gdi_act_layer->offset_x - src_layer->offset_x;
		S32 offset_y = gdi_act_layer->offset_y - src_layer->offset_y;

        transparent_color_a *= 256;
		transparent_color_r *= opacity_value;
		transparent_color_g *= opacity_value;
		transparent_color_b *= opacity_value;

		GDI_CLIP_RECT_TEST(x1,y1,x2,y2,break);

		for (y = y1; y <= y2; y++)
		{
			for (x = x1; x <= x2; x++)
			{
				color = get_ptr(src_buf,src_layer_width,x + offset_x,y + offset_y);
				src_color_to_rgb(&a,&r,&g,&b,color);
				a = ((transparent_color_a) +  (a * inverse_opacity_value)) >> 8;
				r = ((transparent_color_r) +  (r * inverse_opacity_value)) >> 8;
				g = ((transparent_color_g) +  (g * inverse_opacity_value)) >> 8;
				b = ((transparent_color_b) +  (b * inverse_opacity_value)) >> 8;
                gdi_act_put_pixel(x,y,gdi_act_color_from_rgb(a,r,g,b));
			}
		}
    }
	GDI_EXIT_CRITICAL_SECTION(gdi_effect_alpha_blending_rect_ex)
}

void gdi_effect_alpha_blending_rect(gdi_handle src_layer_handle,S32 x1,S32 y1,S32 x2,S32 y2, U32 transparent_color_a,U32 transparent_color_r,U32 transparent_color_g,U32 transparent_color_b)
{
	GDI_ENTER_CRITICAL_SECTION(gdi_effect_alpha_blending_rect_ex)
	{
        gdi_layer_struct *src = ((gdi_layer_struct*)src_layer_handle);
        
        GDI_DEBUG_ASSERT(src_layer_handle);

        if(src->bits_per_pixel == 16 && gdi_act_layer->bits_per_pixel == 16 
        && gdi_act_color_from_rgb(0xff,0,255,0) == 0x07e0 ) // standard rgb565
            gdi_effect_alpha_blending_rect_16(src_layer_handle, x1, y1, x2, y2, transparent_color_a, transparent_color_r, transparent_color_g, transparent_color_b);
        else
            gdi_effect_alpha_blending_rect_ex(src_layer_handle, x1, y1, x2, y2, transparent_color_a, transparent_color_r, transparent_color_g, transparent_color_b);
	}
	GDI_EXIT_CRITICAL_SECTION(gdi_effect_alpha_blending_rect_ex)
}

/**************************************************************

	FUNCTION NAME		: gdi_draw_frame_rect()

  	PURPOSE				: draw a rect with frame and filling.

	INPUT PARAMETERS	: S32			x1
							  S32			y1
							  S32			x2
							  S32			y2
							  gdi_color rect_color
							  gdi_color	frame_color
							  S32			frame_width

	OUTPUT PARAMETERS	: nil

	RETURNS				: void

	REMARKS				: nil

***************************************************************/
void gdi_draw_frame_rect(	S32 x1, S32 y1, S32 x2, S32 y2, gdi_color	rect_color, gdi_color	frame_color, S32			frame_width)
{
	GDI_ENTER_CRITICAL_SECTION(gdi_draw_frame_rect)
		S32 i;

		GDI_CLIP_RECT_TEST(x1,y1,x2,y2,GDI_RETURN_VOID);

		/* draw interial color */
		GDI_DRAW_RECT(x1+frame_width, y1+frame_width, x2-frame_width, y2-frame_width, rect_color);

		/* draw frame */
		for( i = 0 ; i < frame_width; i++)
		{
			GDI_DRAW_H_LINE(x1+i, x2-i, y1+i, frame_color);	/* up horizontal */
			GDI_DRAW_H_LINE(x1+i, x2-i, y2-i, frame_color);	/* down horizontal */
			GDI_DRAW_V_LINE(y1+i, y2-i, x1+i, frame_color);	/* left vertical */
			GDI_DRAW_V_LINE(y1+i, y2-i, x2-i, frame_color);	/* right vertical */
		}
	GDI_EXIT_CRITICAL_SECTION(gdi_draw_frame_rect)
}


/**************************************************************

	FUNCTION NAME		: gdi_draw_round_rect()

  	PURPOSE				: draw a rect with round frame

	INPUT PARAMETERS	: S32			x1
							  S32			y1
							  S32			x2
							  S32			y2
							  gdi_color rect_color
							  gdi_color	frame_color
							  S32			frame_width

	OUTPUT PARAMETERS	: nil

	RETURNS				: void

	REMARKS				: nil

***************************************************************/
void gdi_draw_round_rect(	S32 x1, S32 y1, S32 x2, S32 y2, gdi_color	rect_color, gdi_color	frame_color, S32			frame_width)
{
	GDI_ENTER_CRITICAL_SECTION(gdi_draw_round_rect)
		S32			i;
		S32			shift;
		gdi_color	result;

		GDI_CLIP_RECT_TEST(x1,y1,x2,y2,GDI_RETURN_VOID);

		/* draw interial color */
		GDI_DRAW_RECT(x1+frame_width, y1+frame_width, x2-frame_width, y2-frame_width, rect_color);


		/* draw frame */
		for( i = 0 ; i < frame_width; i++)
		{
			shift = frame_width - i;
			GDI_DRAW_H_LINE(x1+ shift, x2-shift, y1+i, frame_color);	/* up horizontal */
			GDI_DRAW_H_LINE(x1+ shift, x2-shift, y2-i, frame_color);	/* down horizontal */
			GDI_DRAW_V_LINE(y1+shift, y2-shift, x1+i, frame_color);	/* left vertical */
			GDI_DRAW_V_LINE(y1+shift, y2-shift, x2-i, frame_color);	/* right vertical */
		}

		/* draw anti_aliase pixels, make it smooth */
		gdi_color_interpolate(frame_color, rect_color, 10, &result);

		GDI_SET_BUFFER_PIXEL(x1+frame_width+1, y1+frame_width, result);
		GDI_SET_BUFFER_PIXEL(x1+frame_width,	y1+frame_width+1, result);

		GDI_SET_BUFFER_PIXEL(x1+frame_width+1, y2-frame_width, result);
		GDI_SET_BUFFER_PIXEL(x1+frame_width,	y2-frame_width-1, result);

		GDI_SET_BUFFER_PIXEL(x2-frame_width-1, y1+frame_width, result);
		GDI_SET_BUFFER_PIXEL(x2-frame_width,	y1+frame_width+1, result);

		GDI_SET_BUFFER_PIXEL(x2-frame_width-1, y2-frame_width, result);
		GDI_SET_BUFFER_PIXEL(x2-frame_width,	y2-frame_width-1, result);

		gdi_color_interpolate(frame_color, rect_color, 30, &result);

		GDI_SET_BUFFER_PIXEL(x1+frame_width	, y1+frame_width, result);
		GDI_SET_BUFFER_PIXEL(x1+frame_width	, y2-frame_width, result);
		GDI_SET_BUFFER_PIXEL(x2-frame_width	, y1+frame_width, result);
		GDI_SET_BUFFER_PIXEL(x2-frame_width	, y2-frame_width, result);
	GDI_EXIT_CRITICAL_SECTION(gdi_draw_round_rect)
}



/**************************************************************

	FUNCTION NAME		: gdi_draw_button_rect()

  	PURPOSE				: draw a rect with button style

	INPUT PARAMETERS	: S32			x1
							  S32			y1
							  S32			x2
							  S32			y2
							  gdi_color rect_color
							  gdi_color	frame_color
							  S32			frame_width
							  U16			button_height
							  BOOL		invert

	OUTPUT PARAMETERS	: nil

	RETURNS				: void

	REMARKS				: nil

***************************************************************/
void gdi_draw_button_rect(	S32 x1, S32 y1, S32 x2, S32 y2, gdi_color	rect_color, gdi_color	frame_color, U16		frame_width, U16		button_height, BOOL		invert)
{
	GDI_ENTER_CRITICAL_SECTION(gdi_draw_button_rect)
		S32		i;
		U16		shift;
		U16		ratio_light;
		U16		ratio_dark;

		gdi_color result_light;
		gdi_color retult_dark;
		gdi_color black = gdi_act_color_from_rgb(0xff,0,0,0);
		gdi_color white = gdi_act_color_from_rgb(0xff,255,255,255);

		/* draw rect and frame */
		GDI_CLIP_RECT_TEST(x1,y1,x2,y2,GDI_RETURN_VOID);
		gdi_draw_frame_rect(x1, y1, x2, y2, rect_color, frame_color, frame_width);


		/* add button style */
		ratio_light = 70;
		ratio_dark	= 80;
		for(i = 0; i < button_height; i++)
		{
			shift = button_height+frame_width-i-1;
            gdi_color_interpolate(rect_color, white, ratio_light, &result_light);
            gdi_color_interpolate(rect_color, black, ratio_dark, &retult_dark);
            if(invert == TRUE)
            {
                gdi_color tmp;
                tmp = result_light;
                result_light = retult_dark;
                retult_dark = tmp;
            }

			GDI_DRAW_H_LINE(x1+shift, x2-shift-1, y1+shift, result_light);	/* up horizontal */
			GDI_DRAW_V_LINE(y1+shift, y2-shift, x1+shift, result_light);		/* left vertical */

			GDI_DRAW_H_LINE(x1+shift, x2-shift, y2-shift, retult_dark);		/* down vertical */
			GDI_DRAW_V_LINE(y1+shift, y2-shift, x2-shift, retult_dark);		/* left vertical */
			ratio_light = ratio_light - 10;
			ratio_dark	= ratio_dark  - 10;
		}
	GDI_EXIT_CRITICAL_SECTION(gdi_draw_button_rect)
}


/**************************************************************

	FUNCTION NAME		: gdi_draw_shadow_rect()

  	PURPOSE				: draw a rect with shadow style

	INPUT PARAMETERS	: S32			x1
							  S32			y1
							  S32			x2
							  S32			y2
							  gdi_color rect_color
							  gdi_color	frame_color
							  S32			frame_width
							  gdi_color	shadow_color
							  S32		shadow_height

	OUTPUT PARAMETERS	: nil

	RETURNS				: void

	REMARKS				: nil

***************************************************************/
void gdi_draw_shadow_rect(	S32 x1, S32 y1, S32 x2, S32 y2, gdi_color	rect_color, gdi_color	frame_color, S32			frame_width, gdi_color	shadow_color, S32			shadow_height)
{
	GDI_ENTER_CRITICAL_SECTION(gdi_draw_shadow_rect)
		S32		 i;

		/* draw rect and frame */
		GDI_CLIP_RECT_TEST(x1,y1,x2,y2,GDI_RETURN_VOID);
		gdi_draw_frame_rect(x1, y1, x2, y2, rect_color, frame_color, frame_width);

		for( i = 0 ; i < shadow_height; i++)
		{
			GDI_DRAW_H_LINE(x1+i+2, x2+i+1, y2+i+1, shadow_color);	/* horizontal */
			GDI_DRAW_V_LINE(y1+i+2, y2+i+1, x2+i+1, shadow_color);	/* vertical */
		}
	GDI_EXIT_CRITICAL_SECTION(gdi_draw_shadow_rect)
}


/**************************************************************

	FUNCTION NAME		: gdi_draw_gradient_rect()

  	PURPOSE				: draw a rect with gradient filling

	INPUT PARAMETERS	: S32			x1
							  S32			y1
							  S32			x2
							  S32			y2
							  gdi_color rect_color
							  gdi_color	frame_color
							  S32			frame_width
							  gdi_gradient_rect_style_enum style

	OUTPUT PARAMETERS	: nil

	RETURNS				: void

	REMARKS				: nil

***************************************************************/
void gdi_draw_gradient_rect(S32 x1, S32 y1, S32 x2, S32 y2, gdi_color rect_color_start, gdi_color rect_color_end, gdi_color frame_color, S32 frame_width, gdi_gradient_rect_style_enum style)
{
	GDI_ENTER_CRITICAL_SECTION(gdi_draw_gradient_rect)
		S32		 i,j;
		S32		 ratio;
		S32		 size;
		gdi_color result;

		GDI_CLIP_RECT_TEST(x1,y1,x2,y2,GDI_RETURN_VOID);

		switch(style)
		{
		case GDI_GRADIENT_RECT_HOR:
			{
				size = x2 - x1;
				if(size ==0) size = 1;

				for( i = x1 ; i <= x2; i++)
				{
					ratio = (i-x1)*100/size;
					gdi_color_interpolate(rect_color_end, rect_color_start, (U32)ratio, &result);	/* light */
					GDI_DRAW_V_LINE(y1, y2, i, result);	/* left vertical */
				}
				break;
			}

		case GDI_GRADIENT_RECT_VER:
			{
				size = y2 - y1;
				if(size ==0) size = 1;

				for( i = y1 ; i <= y2; i++)
				{
					ratio = (i-y1)*100/size;
					gdi_color_interpolate(rect_color_end, rect_color_start, (U32)ratio, &result);	/* light */
					GDI_DRAW_H_LINE(x1, x2, i, result);	/* left vertical */
				}
				break;
			}
    #ifndef __DISABLE_SANDBOX_LIB__
		case GDI_GRADIENT_RECT_DIA:
			{
				size = (x2-x1) + (y2-y1);

				for( i = y1 ; i <= y2; i++)
				{
					for(j = x1 ; j <= x2; j++)
					{
						ratio = ((i-y1)+(j-x1))*100/size;
						gdi_color_interpolate(rect_color_end, rect_color_start, (U32)ratio, &result);	/* light */
						GDI_SET_BUFFER_PIXEL(j, i, result);
					}
				}
				break;
			}

		case GDI_GRADIENT_RECT_DIA_INV:
			{
				size = (x2-x1) + (y2-y1);

				for( i = x1 ; i <= x2; i++)
				{
					for(j = y1 ; j <= y2; j++)
					{
						ratio = ((i-x1)+(j-y1))*100/size;
						gdi_color_interpolate(rect_color_start, rect_color_end, (U32)ratio, &result);	/* light */
						GDI_SET_BUFFER_PIXEL(i, j, result);
					}
				}
				break;
			}
		case GDI_GRADIENT_RECT_FLIP:
			{
				size = y2 - y1;
				if(size ==0) size = 1;

				for( i = y1 ; i <= y1+((y2-y1)>>1)-1; i++)
				{
					ratio = (i-y1)*2*100/size;
					if (ratio>100) ratio=100;
					gdi_color_interpolate(rect_color_start, rect_color_end, (U32)ratio, &result);	/* light */
					GDI_DRAW_H_LINE(x1, x2, i, result);	/* left vertical */
				}
				for( i = y1+((y2-y1)>>1) ; i <= y2; i++)
				{
					ratio = (y2-i)*2*100/size;
					if (ratio>100) ratio=100;
					gdi_color_interpolate(rect_color_start, rect_color_end, (U32)ratio, &result);	/* light */
					GDI_DRAW_H_LINE(x1, x2, i, result);	/* left vertical */
				}
				break;
			}
        #endif /* __DISABLE_SANDBOX_LIB__ */
		}
		/* draw frame */
		for( i = 0 ; i < frame_width; i++)
		{
			GDI_DRAW_H_LINE(x1+i, x2-i, y1+i, frame_color);	/* up horizontal */
			GDI_DRAW_H_LINE(x1+i, x2-i, y2-i, frame_color);	/* down horizontal */
			GDI_DRAW_V_LINE(y1+i, y2-i, x1+i, frame_color);	/* left vertical */
			GDI_DRAW_V_LINE(y1+i, y2-i, x2-i, frame_color);	/* right vertical */
		}
	GDI_EXIT_CRITICAL_SECTION(gdi_draw_gradient_rect)
}


/**************************************************************

	FUNCTION NAME		: gdi_2d_memory_blt()

  	PURPOSE				: 2d memory blt

	INPUT PARAMETERS	: U8 *src_ptr
							  S32 src_pitch
							  S32 src_offset_x
							  S32 src_offset_y,
							  S32 src_width
							  S32 src_height,
							  U8 *dest_ptr
							  S32 dest_pitch,
							  S32 dest_offset_x
							  S32 dest_offset_y,
							  gdi_rect_struct dest_clip)

	OUTPUT PARAMETERS	: nil

	RETURNS				: void

	REMARKS				: nil

***************************************************************/
void gdi_2d_memory_blt(	
    U8 *src_ptr, 
    S32 src_pitch, 
    S32 src_offset_x, 
    S32 src_offset_y, 
    S32 src_width, 
    S32 src_height, 

    U8 *dest_ptr, 
    S32 dest_pitch, 
    S32 dest_offset_x, 
    S32 dest_offset_y, 
    gdi_rect_struct dest_clip,
    S32 bits_per_pixel
    )
{
	GDI_ENTER_CRITICAL_SECTION(gdi_2d_memory_blt)
{
        gdi_color_format cf = gdi_get_color_format(bits_per_pixel); // not support PARGB and UYVY422
        gd_bitblt[cf](
            dest_ptr,                   // dest
            dest_pitch,                 // dw
            0,                          // dh , un-used
            dest_offset_x,              // dx1
            dest_offset_y,              // dy1
            dest_offset_x+src_width-1,  // dx2
            dest_offset_y+src_height-1, // dy2

            src_ptr,                    // src
            src_pitch,                  // sw
            0,                          // sh , un-used
            src_offset_x,               // sx1
            src_offset_y,               // sy1
            src_offset_x+src_width-1,   // sx2
            src_offset_y+src_height-1,  // sy2
            dest_clip.x1,               
            dest_clip.y1,
            dest_clip.x2,
            dest_clip.y2,
            0x01ffffff,                 // src_key , no color will be 0x1ffffff
            GDI_COLOR_TRANSPARENT);
	}
	GDI_EXIT_CRITICAL_SECTION(gdi_2d_memory_blt)
}


/**************************************************************

	FUNCTION NAME		: gdi_2d_memory_blt_without_transpant_check()

  	PURPOSE				: 2d memory blt

	INPUT PARAMETERS	: U8 *src_ptr
							  S32 src_pitch
							  S32 src_offset_x
							  S32 src_offset_y,
							  S32 src_width
							  S32 src_height,
							  U8 *dest_ptr
							  S32 dest_pitch,
							  S32 dest_offset_x
							  S32 dest_offset_y,
							  gdi_rect_struct dest_clip)

	OUTPUT PARAMETERS	: nil

	RETURNS				: void

	REMARKS				: nil

***************************************************************/
void gdi_2d_memory_blt_without_transpant_check(	U8 *src_ptr, S32 src_pitch, S32 src_offset_x, S32 src_offset_y, S32 src_width, S32 src_height, U8 *dest_ptr, S32 dest_pitch, S32 dest_offset_x, S32 dest_offset_y, gdi_rect_struct dest_clip,S32 bits_per_pixel)
{
	{
        gdi_color_format cf = gdi_get_color_format(bits_per_pixel); // not support PARGB & UYVY422
        gd_bitblt[cf](
            dest_ptr,                   // dest
            dest_pitch,                 // dw
            0,                          // dh , un-used
            dest_offset_x,              // dx1
            dest_offset_y,              // dy1
            dest_offset_x+src_width-1,  // dx2
            dest_offset_y+src_height-1, // dy2

            src_ptr,                    // src
            src_pitch,                  // sw
            0,                          // sh , un-used
            src_offset_x,               // sx1
            src_offset_y,               // sy1
            src_offset_x+src_width-1,   // sx2
            src_offset_y+src_height-1,  // sy2
            dest_clip.x1,               
            dest_clip.y1,
            dest_clip.x2,
            dest_clip.y2,
            0x01ffffff,                 // src_key , no color will be 0x1ffffff
            0x01ffffff);                // layer_key, no color will be 0x1ffffff
	}
}


#if defined(__MTK_TARGET__)

#ifndef __RVCT__

__inline int GDI_BYTE16SET(int sizeReg, int add, int a)
{
    __asm
	{
	        MOV r0, a;
	        MOV r1, r0;
	        MOV r2, r0;
	        MOV r3, r0;
	        MOV r4, add;
	        MOV r5, sizeReg;
	    CPYLOOP:
	        STMIA r4!, {r0, r1, r2, r3};
	        CMP r5, r4;
	        BGT CPYLOOP;
	        MOV add, r4;
	};

	return add;
}

#else


__asm void GDI_BYTE32SET(kal_uint32 *addr_end, kal_uint32 *addr, kal_uint32 val)
{
    ARM
    STMDB sp!, {r4-r9}

    MOV r9, r0 ; addr_end
    MOV r8, r1 ; addr

    ; prepare data registers
    MOV r0, r2
    MOV r1, r0
    MOV r2, r0
    MOV r3, r0
    MOV r4, r0
    MOV r5, r0
    MOV r6, r0
    MOV r7, r0
    
GDI_BYTE32SET_CPYLOOP
    STMIA r8!, {r0-r7}
    CMP r8, r9
    BLT GDI_BYTE32SET_CPYLOOP ; addr < addr_end

    LDMIA sp!, {r4-r9}
    
    BX lr
}


__asm int GDI_BYTE16SET_ONCE(kal_uint32 *addr, kal_uint32 val)
{
    ARM
    STMDB sp!, {r4}

    ; prepare data registers
    MOV r2, r1
    MOV r3, r1
    MOV r4, r1

    STMIA r0!, {r1-r4}
    LDMIA sp!, {r4}
    
    BX lr
}


#endif


#ifdef __MTK_TARGET__
//#pragma arm
#endif

void gdi_memset16(U8* address,U16 pixel,int size)
{
    if(size < 20)
    {
        register int add = (int)address;
        int sizeR = size;
        int postcount;
        if(add & 0x3)
        {
            *((unsigned short*)add) = pixel;
            add += 2;
            sizeR -= 2;
        }
        postcount = sizeR & 0x3;
        sizeR -= postcount;
        if(sizeR > 0)
        {
            register unsigned int a;
            register int sizeReg = sizeR;
            register int endAdd = add + sizeReg;
            a = pixel + (pixel << 16);
            do
            {
                ((unsigned int*)add)[0] = a;
                add += 4;
            }while(add < endAdd);
        }
        if(postcount != 0)
        {
            *((unsigned short*)add) = pixel;
        }
    }
    else
#ifdef __RVCT__ /* No non-RVCT version of GDI_BYTE32SET(), GDI_BYTE16SET_ONCE() */
    {
        register kal_uint8 *addr = (kal_uint8*)address;
        kal_int32 sizeR = size;
        kal_int32 postcount;
        kal_uint32 d_pixel = ((pixel << 16) | pixel);

        if ( (((kal_uint32)addr) & 0x2) != 0 )
        {
            /* Assume addr is 2-byte aligned */
            *((kal_uint16*)addr) = pixel;
            addr += 2;
            sizeR -= 2;
        }

        /* Now addr is 4-byte aligned */

        if (sizeR >= 32)
        {
            register kal_uint8 *addr_end;

            postcount = (sizeR & 0x01f);
            sizeR -= postcount;
            addr_end = addr + sizeR;
            GDI_BYTE32SET((kal_uint32*)addr_end, (kal_uint32*)addr, d_pixel);
            
            addr = addr_end;
            sizeR = postcount;
        }
        
        if (sizeR >= 16)
        {
            /* Now sizeR < 32 */
            GDI_BYTE16SET_ONCE((kal_uint32*)addr, d_pixel);
            
            addr += 16;
            sizeR -= 16;
        }

        postcount = sizeR & 0x2;
        sizeR = ((sizeR >> 2) << 2);
        if (sizeR != 0)
        {
            kal_uint8* addr_end = addr + sizeR;
            do
            {
                *((kal_uint32*)addr) = d_pixel;
                addr += 4;
            } while (addr != addr_end);
        }

        if (postcount != 0)
        {
            *((kal_uint16*)addr) = pixel;
        }
    }
#else
	{
		register int add = (int)address;
		int sizeR = size;
		int postcount;
		register unsigned int a;
		if(add & 0x3)
		{
			*((unsigned short*)add) = pixel;
			add += 2;
			sizeR -= 2;
		}
		postcount = (((unsigned int)sizeR) &0x00f);
		sizeR -= postcount;
		{
			register int sizeReg = sizeR + (int)add;
			a = pixel + (pixel << 16);
			add = GDI_BYTE16SET(sizeReg, add, a);
		}
		sizeR = ((postcount >> 2) << 2);
		if(sizeR > 0)
		{
			register int sizeReg = sizeR;
			register int endAdd = add + sizeReg;
			do
			{
				((unsigned int*)add)[0] = a;
				add += 4;
			}while(add < endAdd);
		}
		if((postcount & 0x3) != 0)
		{
			*((unsigned short*)add) = pixel;
		}
	}
#endif
}

#if !defined(GDI_SLIM_COLOR_FORMAT) || defined(GDI_LAYER_PARGB6666_SUPPORT)
void gdi_memset24(U8* address,U32 pixel,int size)
{
    register int add = (int)address;
    int sizeR = size;
    int postcount;
    unsigned int p3 = (pixel << 8) | (pixel >> 16); /* RGBR */
    unsigned int p2 = (pixel << 16) | (pixel >> 8); /* GBRG */
    unsigned int p1 = pixel | (pixel << 24); /* BRGB */
    unsigned char R = (unsigned char)(p3 & 0xFF);
    unsigned char G = (unsigned char)(p2 & 0xFF);
    unsigned char B = (unsigned char)(p1 & 0xFF);

    if (sizeR <= 2*3)
    {
        postcount = sizeR;
    }
    else
    {
        if(add & 0x1)
	    {
		    ((unsigned char*)add)[0] = B;
		    ((unsigned char*)add)[1] = G;
		    ((unsigned char*)add)[2] = R;
		    add += 3;
		    sizeR -= 3;        
	    }

        if(add & 0x2)
	    {
		    ((unsigned short*)add)[0] = (unsigned short)(p1 & 0xFFFF);
		    ((unsigned short*)add)[1] = (unsigned short)(p1 >> 16);
            ((unsigned short*)add)[2] = (unsigned short)(p2 & 0xFFFF);
		    add += 6;
		    sizeR -= 6;
	    }    
        
	    postcount = sizeR % 12;
	    sizeR -= postcount;
	    if(sizeR > 0)
	    {		
		    register int sizeReg = sizeR;
		    register int endAdd = add + sizeReg;
    		
		    do
		    {
			    ((unsigned int*)add)[0] = p1;
                ((unsigned int*)add)[1] = p2;
                ((unsigned int*)add)[2] = p3;
			    add += 12;
		    }while(add < endAdd);
	    }
    }

	if(postcount > 0)
	{
        do
		{
			((unsigned char*)add)[0] = B;
            ((unsigned char*)add)[1] = G;
            ((unsigned char*)add)[2] = R;
			add += 3;
            postcount -= 3;
		}while(postcount > 0);
	}
}
#endif


void gdi_memset32(U8* address,U32 pixel,int size)
{
    if(size < 20)
    {
        register int add = (int)address;
        int sizeR = size;
        int postcount;
        
        postcount = sizeR & 0x3;
        sizeR -= postcount;
        if(sizeR > 0)
        {
            register unsigned int a;
            register int sizeReg = sizeR;
            register int endAdd = add + sizeReg;
            a = pixel;
            do
            {
                ((unsigned int*)add)[0] = a;
                add += 4;
            }while(add < endAdd);
        }
    }
    else
#ifdef __RVCT__ /* No non-RVCT version of GDI_BYTE32SET(), GDI_BYTE16SET_ONCE() */
    {
        register kal_uint8 *addr = (kal_uint8*)address;
        kal_int32 sizeR = size;
        kal_int32 postcount;

        if (sizeR >= 32)
        {
            register kal_uint8 *addr_end;

            postcount = (sizeR & 0x01f);
            sizeR -= postcount;
            addr_end = addr + sizeR;
            GDI_BYTE32SET((kal_uint32*)addr_end, (kal_uint32*)addr, pixel);
            
            addr = addr_end;
            sizeR = postcount;
        }
        
        if (sizeR >= 16)
        {
            /* Now sizeR < 32 */
            GDI_BYTE16SET_ONCE((kal_uint32*)addr, pixel);
            
            addr += 16;
            sizeR -= 16;
        }

        /* Assume size is multiple of 4 */
        sizeR = ((sizeR >> 2) << 2);
        if (sizeR != 0)
        {
            kal_uint8* addr_end = addr + sizeR;
            do
            {
                *((kal_uint32*)addr) = pixel;
                addr += 4;
            } while (addr != addr_end);
        }
    }
#else
	{
		register int add = (int)address;
		int sizeR = size;
		int postcount;
		register unsigned int a;

		postcount = (((unsigned int)sizeR) &0x00f);
		sizeR -= postcount;
		{
			register int sizeReg = sizeR + (int)add;
			a = pixel;
			add = GDI_BYTE16SET(sizeReg, add, a);
		}
		sizeR = ((postcount >> 2) << 2);
		if(sizeR > 0)
		{
			register int sizeReg = sizeR;
			register int endAdd = add + sizeReg;
			do
			{
				((unsigned int*)add)[0] = a;
				add += 4;
			}while(add < endAdd);
		}
	}
#endif
}

#ifdef __MTK_TARGET__
//#pragma thumb
#endif


#else
void gdi_memset16(U8* address,U16 pixel,int size)
{
        register int add = (int)address;
        int sizeR = size;
        int postcount;
        if(add & 0x3)
	{
		*((unsigned short*)add) = pixel;
		add += 2;
		sizeR -= 2;
	}
	postcount = sizeR & 0x3;
	sizeR -= postcount;
	if(sizeR > 0)
	{
		register unsigned int a;
		register int sizeReg = sizeR;
		register int endAdd = add + sizeReg;
		a = pixel + (pixel << 16);
		do
		{
			((unsigned int*)add)[0] = a;
			add += 4;
		}while(add < endAdd);
	}
	if(postcount != 0)
	{
		*((unsigned short*)add) = pixel;
	}
}


#if !defined(GDI_SLIM_COLOR_FORMAT) || defined(GDI_LAYER_PARGB6666_SUPPORT)
void gdi_memset24(U8* address,U32 pixel,int size)
{
    register int add = (int)address;
    int sizeR = size;
    int postcount;
    unsigned int p3 = (pixel << 8) | (pixel >> 16); /* RGBR */
    unsigned int p2 = (pixel << 16) | (pixel >> 8); /* GBRG */
    unsigned int p1 = pixel | (pixel << 24); /* BRGB */
    unsigned char R = (unsigned char)(p3 & 0xFF);
    unsigned char G = (unsigned char)(p2 & 0xFF);
    unsigned char B = (unsigned char)(p1 & 0xFF);

    if (sizeR <= 2*3)
    {
        postcount = sizeR;
    }
    else
    {
        if(add & 0x1)
	    {
		    ((unsigned char*)add)[0] = B;
		    ((unsigned char*)add)[1] = G;
		    ((unsigned char*)add)[2] = R;
		    add += 3;
		    sizeR -= 3;        
	    }

        if(add & 0x2)
	    {
		    ((unsigned short*)add)[0] = (unsigned short)(p1 & 0xFFFF);
		    ((unsigned short*)add)[1] = (unsigned short)(p1 >> 16);
            ((unsigned short*)add)[2] = (unsigned short)(p2 & 0xFFFF);
		    add += 6;
		    sizeR -= 6;
	    }    
        
	    postcount = sizeR % 12;
	    sizeR -= postcount;
	    if(sizeR > 0)
	    {		
		    register int sizeReg = sizeR;
		    register int endAdd = add + sizeReg;
    		
		    do
		    {
			    ((unsigned int*)add)[0] = p1;
                ((unsigned int*)add)[1] = p2;
                ((unsigned int*)add)[2] = p3;
			    add += 12;
		    }while(add < endAdd);
	    }
    }

	if(postcount > 0)
	{
        do
		{
			((unsigned char*)add)[0] = B;
            ((unsigned char*)add)[1] = G;
            ((unsigned char*)add)[2] = R;
			add += 3;
            postcount -= 3;
		}while(postcount > 0);
	}
}
#endif


void gdi_memset32(U8* address,U32 pixel,int size)
{
    register int add = (int)address;
    int sizeR = size;
    int postcount;
        
	postcount = sizeR & 0x3;
	sizeR -= postcount;
	if(sizeR > 0)
	{
		register unsigned int a;
		register int sizeReg = sizeR;
		register int endAdd = add + sizeReg;
		a = pixel;
		do
		{
			((unsigned int*)add)[0] = a;
			add += 4;
		}while(add < endAdd);
	}
	if(postcount != 0)
	{
		*((unsigned int*)add) = pixel;
	}
}

#endif

static const kal_uint8 bitsperpixels[]   = { 1, 8, 16, 24, 32, 32, 16, 8, 24 };
static const kal_uint8 reserve_padding[] = { 7, 0,  0,  0,  0,  0,  0, 0,  0 };
S32 gdi_sizeof_pixels(gdi_color_format cf,S32 width,S32 height)
{
    if (cf <GDI_COLOR_FORMAT_STANDARD_END)
    {
        return ((width * gdi_bits_per_pixel(cf) + reserve_padding[cf]) >> 3) * height;
    }
	
    width  = (width+7) & (~0x7);
    height = (height+7) & (~0x7); 
    
	if(cf == GDI_COLOR_FORMAT_MAINLCD)
        return ((width*GDI_MAINLCD_BIT_PER_PIXEL)>>3)*height;
	
#ifdef __MMI_SUBLCD__
	if(cf == GDI_COLOR_FORMAT_SUBLCD)
		return ((width*GDI_SUBLCD_BIT_PER_PIXEL)>>3)*height;
#endif

	GDI_ASSERT(0);
	return 0;
}

gdi_color_format gdi_get_color_format(S32 bits)
{
    /*
     * WARNING!! This API won't return GDI_COLOR_FORMAT_32_PARGB & GDI_COLOR_FORMAT_UYVY422
     */
	if(bits == 1)
		return GDI_COLOR_FORMAT_1;
#ifdef GDI_COLORFORMAT8_PALETTE
	else if(bits == 8)
		return GDI_COLOR_FORMAT_8;
#endif /* GDI_COLORFORMAT8_PALETTE */
	else if(bits == 16)
		return GDI_COLOR_FORMAT_16;
	else if(bits == 18)
		return GDI_COLOR_FORMAT_24;
	else if(bits == 24)
		return GDI_COLOR_FORMAT_24;
	else if(bits == 32)
		return GDI_COLOR_FORMAT_32;
	
	else if(bits == DRV_MAINLCD_BIT_PER_PIXEL)
		return GDI_COLOR_FORMAT_MAINLCD;
	
#ifdef __MMI_SUBLCD__
	else if(bits == DRV_SUBLCD_BIT_PER_PIXEL)
		return GDI_COLOR_FORMAT_SUBLCD;
#endif

	else
		GDI_ASSERT(0);
	return GDI_COLOR_FORMAT_32; // default we use the maximum data type
}

S32 gdi_bits_per_pixel(gdi_color_format cf)
{
	if(cf <GDI_COLOR_FORMAT_STANDARD_END)
		return bitsperpixels[cf];
	
	if(cf == GDI_COLOR_FORMAT_MAINLCD)
		return GDI_MAINLCD_BIT_PER_PIXEL;
	
#ifdef __MMI_SUBLCD__
	if(cf == GDI_COLOR_FORMAT_SUBLCD)
		return GDI_SUBLCD_BIT_PER_PIXEL;
#endif

	GDI_ASSERT(0);
	return 0;
}


/**************************************************************

	GDI Software Resizer
	
***************************************************************/

/*
 * NOTICE: to make a region ARM9 D-cacheable, it must comply with some rules.
 * If we make other variables D-cacheable without checking these rules, 
 * it may have potential issues.
 */
#if defined(__MTK_TARGET__)
#pragma arm section zidata = "CACHEDZI",  rwdata = "CACHEDRW" 
#endif
#if defined(GDI_USING_PBM)
gdi_resizer_struct GDI_RESIZER;
gdi_resizer_put_func gdi_resizer_put;


#if defined(__MTK_TARGET__)
#pragma arm section zidata, rwdata
#endif


#if defined(__MTK_TARGET__)
#pragma arm section code = "INTERNCODE"
#endif


/*****************************************************************************
 * FUNCTION
 *  gdi_resizer_put_null
 * DESCRIPTION
 *  dummy put pixel function
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_resizer_put_null(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

	// keep empty
}

#if defined(GDI_USE_RGB565_TO_SHOW_BW)
/*****************************************************************************
 * FUNCTION
 *  gdi_resizer_put_mainlcd
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_resizer_put_1(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* 
     * W06.26 Fix the wrong position GDI_RESIZER.want_sx and GDI_RESIZER.want_sy
     * Use GDI_RESIZER.next_want_sx - GDI_RESIZER.want_sx_table instead of GDI_RESIZER.want_sx
     * as jumping to the next pixel
     * GDI_RESIZER.next_want_dy - 1 is the current y position
     */
	if(want_draw)
		GDI_SET_BUFFER_PIXEL_1(
			(GDI_RESIZER.want_dx1 + (int)(GDI_RESIZER.next_want_sx - GDI_RESIZER.want_sx_table) - 1), 
			(GDI_RESIZER.next_want_dy - 1),
			(c));

	// generate next_position
	if(GDI_RESIZER.next_want_sx >= GDI_RESIZER.want_sx_table_end)
	{
		GDI_RESIZER.next_want_sx = GDI_RESIZER.want_sx_table;

		//GDI_RESIZER.want_sy = (((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.y_scale_factor) >> 16);
		GDI_RESIZER.want_sy = ((((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.src_height_range << 1) +  GDI_RESIZER.dest_height_range) / (GDI_RESIZER.dest_height_range << 1));

		if(GDI_RESIZER.next_want_dy < GDI_RESIZER.want_dy1 || GDI_RESIZER.next_want_dy > GDI_RESIZER.want_dy2)
		{
			gdi_resizer_put=gdi_resizer_put_null;
			return;
		}
		GDI_RESIZER.next_want_dy += GDI_RESIZER.dir_y;
	}
	GDI_RESIZER.want_sx = *GDI_RESIZER.next_want_sx++;
}

/*****************************************************************************
 * FUNCTION
 *  gdi_non_resizer_put_mainlcd
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_non_resizer_put_1(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

	#define GDI_RESIZER_PUT_PIXEL(COLOR)			GDI_SET_BUFFER_PIXEL_1((GDI_RESIZER.offset_dx + GDI_RESIZER.want_sx),(GDI_RESIZER.offset_dy + GDI_RESIZER.want_sy),COLOR)
	#define GDI_RESIZER_INCREASE_DEST				
	GDI_NON_RESIZER_PUT_X(c,want_draw);
	#undef GDI_RESIZER_PUT_PIXEL
	#undef GDI_RESIZER_INCREASE_DEST
}

#endif


/*****************************************************************************
 * FUNCTION
 *  gdi_resizer_put_8
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_resizer_put_8(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
	if(want_draw)
		*((U8*)GDI_RESIZER.dest) = (U8)c;

	GDI_RESIZER.dest+=1;
	// generate next_position
	if(GDI_RESIZER.next_want_sx >= GDI_RESIZER.want_sx_table_end)
	{
		GDI_RESIZER.next_want_sx = GDI_RESIZER.want_sx_table;

		if(GDI_RESIZER.dir_x>0)
			GDI_RESIZER.dest = gdi_act_layer->buf_ptr + (GDI_RESIZER.want_dx1+ GDI_RESIZER.next_want_dy* gdi_act_layer->width )*1;
		else
			GDI_RESIZER.dest = gdi_act_layer->buf_ptr + (GDI_RESIZER.want_dx2+ GDI_RESIZER.next_want_dy* gdi_act_layer->width )*1;
		//GDI_RESIZER.want_sy = (((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.y_scale_factor) >> 16);
		GDI_RESIZER.want_sy = ((((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.src_height_range << 1) +  GDI_RESIZER.dest_height_range) / (GDI_RESIZER.dest_height_range << 1));

		if(GDI_RESIZER.next_want_dy < GDI_RESIZER.want_dy1 || GDI_RESIZER.next_want_dy > GDI_RESIZER.want_dy2)
		{
			gdi_resizer_put=gdi_resizer_put_null;
			return;
		}
		GDI_RESIZER.next_want_dy += GDI_RESIZER.dir_y;
	}
	GDI_RESIZER.want_sx = *GDI_RESIZER.next_want_sx++;
}


/*****************************************************************************
 * FUNCTION
 *  gdi_resizer_put_16
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_resizer_put_16(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
	if(want_draw)
		*((U16*)GDI_RESIZER.dest) = (U16)c;

	GDI_RESIZER.dest+=2;
	// generate next_position
	if(GDI_RESIZER.next_want_sx >= GDI_RESIZER.want_sx_table_end)
	{
		GDI_RESIZER.next_want_sx = GDI_RESIZER.want_sx_table;

		if(GDI_RESIZER.dir_x>0)
			GDI_RESIZER.dest = gdi_act_layer->buf_ptr + (GDI_RESIZER.want_dx1+ GDI_RESIZER.next_want_dy* gdi_act_layer->width )*2;
		else
			GDI_RESIZER.dest = gdi_act_layer->buf_ptr + (GDI_RESIZER.want_dx2+ GDI_RESIZER.next_want_dy* gdi_act_layer->width )*2;
		//GDI_RESIZER.want_sy = (((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.y_scale_factor) >> 16);
		GDI_RESIZER.want_sy = ((((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.src_height_range << 1) +  GDI_RESIZER.dest_height_range) / (GDI_RESIZER.dest_height_range << 1));

		if(GDI_RESIZER.next_want_dy < GDI_RESIZER.want_dy1 || GDI_RESIZER.next_want_dy > GDI_RESIZER.want_dy2)
		{
			gdi_resizer_put=gdi_resizer_put_null;
			return;
		}
		GDI_RESIZER.next_want_dy += GDI_RESIZER.dir_y;
	}
	GDI_RESIZER.want_sx = *GDI_RESIZER.next_want_sx++;
}

    
/*****************************************************************************
 * FUNCTION
 *  gdi_resizer_put_24
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_resizer_put_24(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
	if(want_draw)
	{
		U32 *ptr = (U32*)(GDI_RESIZER.dest);
		switch(((U32)ptr)&0x3)
		{
		case 0:
			*ptr = (*ptr&0xff000000)|(c&0x00ffffff);
			break;
		case 1:
			ptr= (U32*)(((U32)ptr)&(~0x3));
			*ptr = (*ptr&0x000000ff)|(c<<8);
			break;
		case 2:
			ptr= (U32*)(((U32)ptr)&(~0x3));
			*ptr = (*ptr&0x0000ffff)|(c<<16); ptr++;
			*ptr = (*ptr&0xffffff00)|((c&0x00ff0000)>>16);
			break;
		default:
			ptr= (U32*)(((U32)ptr)&(~0x3));
			*ptr = (*ptr&0x00ffffff)|(c<<24); ptr++;
			*ptr = (*ptr&0xffff0000)|((c&0x00ffff00)>>8);
			break;
		}
	}

	GDI_RESIZER.dest+=3;
	// generate next_position
	if(GDI_RESIZER.next_want_sx >= GDI_RESIZER.want_sx_table_end)
	{
		GDI_RESIZER.next_want_sx = GDI_RESIZER.want_sx_table;

		if(GDI_RESIZER.dir_x>0)
			GDI_RESIZER.dest = gdi_act_layer->buf_ptr + (GDI_RESIZER.want_dx1+ GDI_RESIZER.next_want_dy* gdi_act_layer->width )*3;
		else
			GDI_RESIZER.dest = gdi_act_layer->buf_ptr + (GDI_RESIZER.want_dx2+ GDI_RESIZER.next_want_dy* gdi_act_layer->width )*3;
		//GDI_RESIZER.want_sy = (((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.y_scale_factor) >> 16);
		GDI_RESIZER.want_sy = ((((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.src_height_range << 1) +  GDI_RESIZER.dest_height_range) / (GDI_RESIZER.dest_height_range << 1));

		if(GDI_RESIZER.next_want_dy < GDI_RESIZER.want_dy1 || GDI_RESIZER.next_want_dy > GDI_RESIZER.want_dy2)
		{
			gdi_resizer_put=gdi_resizer_put_null;
			return;
		}
		GDI_RESIZER.next_want_dy += GDI_RESIZER.dir_y;
	}
	GDI_RESIZER.want_sx = *GDI_RESIZER.next_want_sx++;
}


/*****************************************************************************
 * FUNCTION
 *  gdi_resizer_put_32
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_resizer_put_32(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
	if(want_draw)
	{
		 *(U32*)(GDI_RESIZER.dest) = (U32)c;
	}

	GDI_RESIZER.dest+=4;
	// generate next_position
	if(GDI_RESIZER.next_want_sx >= GDI_RESIZER.want_sx_table_end)
	{
		GDI_RESIZER.next_want_sx = GDI_RESIZER.want_sx_table;

		if(GDI_RESIZER.dir_x>0)
			GDI_RESIZER.dest = gdi_act_layer->buf_ptr + (GDI_RESIZER.want_dx1+ GDI_RESIZER.next_want_dy* gdi_act_layer->width )*4;
		else
			GDI_RESIZER.dest = gdi_act_layer->buf_ptr + (GDI_RESIZER.want_dx2+ GDI_RESIZER.next_want_dy* gdi_act_layer->width )*4;
		//GDI_RESIZER.want_sy = (((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.y_scale_factor) >> 16);
		GDI_RESIZER.want_sy = ((((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.src_height_range << 1) +  GDI_RESIZER.dest_height_range) / (GDI_RESIZER.dest_height_range << 1));

		if(GDI_RESIZER.next_want_dy < GDI_RESIZER.want_dy1 || GDI_RESIZER.next_want_dy > GDI_RESIZER.want_dy2)
		{
			gdi_resizer_put=gdi_resizer_put_null;
			return;
		}
		GDI_RESIZER.next_want_dy += GDI_RESIZER.dir_y;
	}
	GDI_RESIZER.want_sx = *GDI_RESIZER.next_want_sx++;
}

#if  GDI_MAINLCD_BIT_PER_PIXEL ==  1 || !defined(GDI_USING_LAYER)


/*****************************************************************************
 * FUNCTION
 *  gdi_resizer_put_mainlcd
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_resizer_put_mainlcd(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* 
     * W06.26 Fix the wrong position GDI_RESIZER.want_sx and GDI_RESIZER.want_sy
     * Use GDI_RESIZER.next_want_sx - GDI_RESIZER.want_sx_table instead of GDI_RESIZER.want_sx
     * as jumping to the next pixel
     * GDI_RESIZER.next_want_dy - 1 is the current y position
     */
	if(want_draw)
		DRV_MAINLCD_SET_BUFFER_PIXEL(
			(GDI_RESIZER.want_dx1 + (int)(GDI_RESIZER.next_want_sx - GDI_RESIZER.want_sx_table) - 1), 
			(GDI_RESIZER.next_want_dy - 1),
			(c));

	// generate next_position
	if(GDI_RESIZER.next_want_sx >= GDI_RESIZER.want_sx_table_end)
	{
		GDI_RESIZER.next_want_sx = GDI_RESIZER.want_sx_table;

		//GDI_RESIZER.want_sy = (((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.y_scale_factor) >> 16);
		GDI_RESIZER.want_sy = ((((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.src_height_range << 1) +  GDI_RESIZER.dest_height_range) / (GDI_RESIZER.dest_height_range << 1));

		if(GDI_RESIZER.next_want_dy < GDI_RESIZER.want_dy1 || GDI_RESIZER.next_want_dy > GDI_RESIZER.want_dy2)
		{
			gdi_resizer_put=gdi_resizer_put_null;
			return;
		}
		GDI_RESIZER.next_want_dy += GDI_RESIZER.dir_y;
	}
	GDI_RESIZER.want_sx = *GDI_RESIZER.next_want_sx++;
}
#elif GDI_MAINLCD_BIT_PER_PIXEL == 16
	#define gdi_resizer_put_mainlcd 	gdi_resizer_put_16
#elif GDI_MAINLCD_BIT_PER_PIXEL == 24
	#define gdi_resizer_put_mainlcd 	gdi_resizer_put_24
#elif GDI_MAINLCD_BIT_PER_PIXEL == 32
	#define gdi_resizer_put_mainlcd 	gdi_resizer_put_32
#else
	#error "Unknown MAINLCD_BIT_PER_PIXEL"
#endif

#ifdef __MMI_SUBLCD__
#if GDI_SUBLCD_BIT_PER_PIXEL ==  1 || !defined(GDI_USING_LAYER)
/*****************************************************************************
 * FUNCTION
 *  gdi_resizer_put_sublcd
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_resizer_put_sublcd(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* 
     * W06.26 Fix the wrong position GDI_RESIZER.want_sx and GDI_RESIZER.want_sy
     * Use GDI_RESIZER.next_want_sx - GDI_RESIZER.want_sx_table instead of GDI_RESIZER.want_sx
     * as jumping to the next pixel
     * GDI_RESIZER.next_want_dy - 1 is the current y position
     */
	if(want_draw)
		DRV_SUBLCD_SET_BUFFER_PIXEL(
			(GDI_RESIZER.want_dx1 + (int)(GDI_RESIZER.next_want_sx - GDI_RESIZER.want_sx_table) - 1), 
			(GDI_RESIZER.next_want_dy - 1),
			(c));

	// generate next_position
	if(GDI_RESIZER.next_want_sx >= GDI_RESIZER.want_sx_table_end)
	{
		GDI_RESIZER.next_want_sx = GDI_RESIZER.want_sx_table;

		//GDI_RESIZER.want_sy = (((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.y_scale_factor) >> 16);
		GDI_RESIZER.want_sy = ((((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * GDI_RESIZER.src_height_range << 1) +  GDI_RESIZER.dest_height_range) / (GDI_RESIZER.dest_height_range << 1));

		if(GDI_RESIZER.next_want_dy < GDI_RESIZER.want_dy1 || GDI_RESIZER.next_want_dy > GDI_RESIZER.want_dy2)
		{
			gdi_resizer_put=gdi_resizer_put_null;
			return;
		}
		GDI_RESIZER.next_want_dy += GDI_RESIZER.dir_y;
	}
	GDI_RESIZER.want_sx = *GDI_RESIZER.next_want_sx++;
}
#elif GDI_SUBLCD_BIT_PER_PIXEL == 16
	#define gdi_resizer_put_sublcd 	gdi_resizer_put_16
#elif GDI_SUBLCD_BIT_PER_PIXEL == 24
	#define gdi_resizer_put_sublcd 	gdi_resizer_put_24
#elif GDI_SUBLCD_BIT_PER_PIXEL == 32
	#define gdi_resizer_put_sublcd 	gdi_resizer_put_32
#else
		#error "Unknown SUBLCD_BIT_PER_PIXEL"
#endif
#endif

/*****************************************************************************
 * FUNCTION
 *  gdi_non_resizer_put_8
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_non_resizer_put_8(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

	#define GDI_RESIZER_PUT_PIXEL(COLOR)			*((U8*)GDI_RESIZER.dest) = (U8)COLOR
	#define GDI_RESIZER_INCREASE_DEST				GDI_RESIZER.dest ++
	GDI_NON_RESIZER_PUT_X(c,want_draw);
	#undef GDI_RESIZER_PUT_PIXEL
	#undef GDI_RESIZER_INCREASE_DEST
}


/*****************************************************************************
 * FUNCTION
 *  gdi_non_resizer_put_16
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_non_resizer_put_16(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

	#define GDI_RESIZER_PUT_PIXEL(COLOR)		*((U16*)GDI_RESIZER.dest) = (U16)COLOR
	#define GDI_RESIZER_INCREASE_DEST			GDI_RESIZER.dest +=2
	GDI_NON_RESIZER_PUT_X(c,want_draw);
	#undef GDI_RESIZER_PUT_PIXEL
	#undef GDI_RESIZER_INCREASE_DEST
}


/*****************************************************************************
 * FUNCTION
 *  gdi_non_resizer_put_24
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_non_resizer_put_24(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

	#define GDI_RESIZER_PUT_PIXEL(COLOR)			\
			do\
			{\
				U32 *ptr = (U32*)(GDI_RESIZER.dest);\
				switch(((U32)ptr)&0x3)\
				{\
				case 0:\
					*ptr = (*ptr&0xff000000)|(COLOR&0x00ffffff);\
					break;\
				case 1:\
					ptr= (U32*)(((U32)ptr)&(~0x3));\
					*ptr = (*ptr&0x000000ff)|(COLOR<<8);\
					break;\
				case 2:\
					ptr= (U32*)(((U32)ptr)&(~0x3));\
					*ptr = (*ptr&0x0000ffff)|(COLOR<<16); ptr++;\
					*ptr = (*ptr&0xffffff00)|((COLOR&0x00ff0000)>>16);\
					break;\
				default:\
					ptr= (U32*)(((U32)ptr)&(~0x3));\
					*ptr = (*ptr&0x00ffffff)|(COLOR<<24); ptr++;\
					*ptr = (*ptr&0xffff0000)|((COLOR&0x00ffff00)>>8);\
				}\
			}while(0)

	#define GDI_RESIZER_INCREASE_DEST				GDI_RESIZER.dest +=3
	GDI_NON_RESIZER_PUT_X(c,want_draw);
	#undef GDI_RESIZER_PUT_PIXEL
	#undef GDI_RESIZER_INCREASE_DEST
}


/*****************************************************************************
 * FUNCTION
 *  gdi_non_resizer_put_32
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_non_resizer_put_32(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

	#define GDI_RESIZER_PUT_PIXEL(COLOR)			*((U32*)GDI_RESIZER.dest) = (U32)COLOR
	#define GDI_RESIZER_INCREASE_DEST				GDI_RESIZER.dest +=4
	GDI_NON_RESIZER_PUT_X(c,want_draw);
	#undef GDI_RESIZER_PUT_PIXEL
	#undef GDI_RESIZER_INCREASE_DEST
}

#if  GDI_MAINLCD_BIT_PER_PIXEL ==  1 || !defined(GDI_USING_LAYER)
/*****************************************************************************
 * FUNCTION
 *  gdi_non_resizer_put_mainlcd
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_non_resizer_put_mainlcd(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

	#define GDI_RESIZER_PUT_PIXEL(COLOR)			DRV_MAINLCD_SET_BUFFER_PIXEL((GDI_RESIZER.offset_dx + GDI_RESIZER.want_sx),(GDI_RESIZER.offset_dy + GDI_RESIZER.want_sy),COLOR)
	#define GDI_RESIZER_INCREASE_DEST				
	GDI_NON_RESIZER_PUT_X(c,want_draw);
	#undef GDI_RESIZER_PUT_PIXEL
	#undef GDI_RESIZER_INCREASE_DEST
}
#elif GDI_MAINLCD_BIT_PER_PIXEL == 16
	#define gdi_non_resizer_put_mainlcd 	gdi_non_resizer_put_16
#elif GDI_MAINLCD_BIT_PER_PIXEL == 24
	#define gdi_non_resizer_put_mainlcd 	gdi_non_resizer_put_24
#elif GDI_MAINLCD_BIT_PER_PIXEL == 32
	#define gdi_non_resizer_put_mainlcd 	gdi_non_resizer_put_32
#else
	#error "Unknown MAINLCD_BIT_PER_PIXEL"
#endif

#ifdef __MMI_SUBLCD__
#if GDI_SUBLCD_BIT_PER_PIXEL ==  1 || !defined(GDI_USING_LAYER)
/*****************************************************************************
 * FUNCTION
 *  gdi_non_resizer_put_sublcd
 * DESCRIPTION
 *  put pixel
 * PARAMETERS
 *  c               [IN]    color want to output
 *  want_draw       [IN]    if false, it will not modify color of this pixel
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_non_resizer_put_sublcd(gdi_color c,BOOL want_draw)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

	#define GDI_RESIZER_PUT_PIXEL(COLOR)			DRV_SUBLCD_SET_BUFFER_PIXEL((GDI_RESIZER.offset_dx + GDI_RESIZER.want_sx),(GDI_RESIZER.offset_dy + GDI_RESIZER.want_sy),COLOR)
	#define GDI_RESIZER_INCREASE_DEST				
	GDI_NON_RESIZER_PUT_X(c,want_draw);
	#undef GDI_RESIZER_PUT_PIXEL
	#undef GDI_RESIZER_INCREASE_DEST
}
#elif GDI_MAINLCD_BIT_PER_PIXEL == 16
	#define gdi_non_resizer_put_sublcd 	gdi_non_resizer_put_16
#elif GDI_MAINLCD_BIT_PER_PIXEL == 24
	#define gdi_non_resizer_put_sublcd 	gdi_non_resizer_put_24
#elif GDI_MAINLCD_BIT_PER_PIXEL == 32
	#define gdi_non_resizer_put_sublcd 	gdi_non_resizer_put_32
#else
	#error "Unknown MAINLCD_BIT_PER_PIXEL"
#endif
#endif


/*****************************************************************************
 * FUNCTION
 *  gdi_resizer_set_want_sy
 * DESCRIPTION
 *  update resizer context depends on want_sy
 * PARAMETERS
 *  want_sy       [IN]  the next line resizer want to draw
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_resizer_set_want_sy(S32 want_sy)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/

	S32 dy;

    // out of clipping should not update want_sx, want_sy
    if( GDI_RESIZER.want_sx <0 || GDI_RESIZER.want_sy < 0) return;
    
	// sometime we need to stop auto increase dy, and manual set the position
	GDI_RESIZER.dir_y = 0; 
	// default : the pixel line (want_sy) will not be accept !!
	GDI_RESIZER.want_sy = want_sy+1; 

	// non resize handler
	if(GDI_RESIZER.want_sx_table==NULL)
	{
		if ((want_sy) >= GDI_RESIZER.want_start_sy && (want_sy) <= GDI_RESIZER.want_end_sy)
		{
			GDI_RESIZER.want_sx = GDI_RESIZER.want_start_sx;
			GDI_RESIZER.want_sy = want_sy;
			//W05.40 Compute GDI_RESIZER.dest by GDI_RESIZER.want_dx1 and GDI_RESIZER.want_dy1
			//GDI_RESIZER.dest = gdi_act_layer->buf_ptr + ((GDI_RESIZER.want_sx+GDI_RESIZER.want_sy*gdi_act_layer->width* bitsperpixels[gdi_act_layer->vcf])>>3);
			GDI_RESIZER.dest = gdi_act_layer->buf_ptr 
								+ (((GDI_RESIZER.want_dx1
								+(GDI_RESIZER.want_dy1+GDI_RESIZER.want_sy-GDI_RESIZER.want_start_sy)*gdi_act_layer->width)
								* gdi_bits_per_pixel(gdi_act_layer->cf))>>3);
		}
		return ;
	}
	
	
	for(;;)
	{
		// want_sy project to dy
		dy = ((want_sy * GDI_RESIZER.dest_height_range << 1) +  GDI_RESIZER.src_height_range) / (GDI_RESIZER.src_height_range << 1);
		
		//check this want_dy will also project to want_sy
		if( want_sy ==  ((((dy) * GDI_RESIZER.src_height_range << 1) +  GDI_RESIZER.dest_height_range) / (GDI_RESIZER.dest_height_range << 1)))
			break;

		dy ++;
		// check dy + 1 will project to want_sy
		if( want_sy == ((((dy) * GDI_RESIZER.src_height_range << 1) +  GDI_RESIZER.dest_height_range) / (GDI_RESIZER.dest_height_range << 1)))
			break;

		
		return;// this line will not be accept !!
	}

	dy += GDI_RESIZER.offset_dy;

	// check this destination is out of clipping area ?
	if(dy < GDI_RESIZER.want_dy1 || dy > GDI_RESIZER.want_dy2)
		return;// this line will not be accept !!

	GDI_RESIZER.want_sy = want_sy;
	GDI_RESIZER.next_want_dy = dy;
	GDI_RESIZER.next_want_sx = GDI_RESIZER.want_sx_table;
	GDI_RESIZER.want_sx = *GDI_RESIZER.next_want_sx++;
	
	if(GDI_RESIZER.dir_x>0)
		GDI_RESIZER.dest = gdi_act_layer->buf_ptr 
							+ ((	(	GDI_RESIZER.want_dx1
									+GDI_RESIZER.next_want_dy* gdi_act_layer->width )
									* gdi_bits_per_pixel(gdi_act_layer->cf))>>3);
	else
		GDI_RESIZER.dest = gdi_act_layer->buf_ptr 
							+ ((		(GDI_RESIZER.want_dx2
									+ GDI_RESIZER.next_want_dy* gdi_act_layer->width )
									* gdi_bits_per_pixel(gdi_act_layer->cf))>>3);
	
}
#endif

#if defined(__MTK_TARGET__)
#pragma arm section code
#endif

#if defined(GDI_USING_PBM)
typedef void (*gdi_resizer_put_func)(gdi_color c,BOOL want_draw);
const static gdi_resizer_put_func gdi_resizer_put_table[]=
{
#if defined(GDI_USE_RGB565_TO_SHOW_BW)
	gdi_resizer_put_1, //(gdi_resizer_put_func)gd_null_pointer_function, // 1
#else
    (gdi_resizer_put_func)gd_null_pointer_function, // 1
#endif
	gdi_resizer_put_8, // 8
	gdi_resizer_put_16, // 16
	gdi_resizer_put_24, // 24
	gdi_resizer_put_32, // 32
	gdi_resizer_put_32, // 32PARGB
	(gdi_resizer_put_func)gd_null_pointer_function, // UYVY422
	gdi_resizer_put_8,  /* A8 */
	gdi_resizer_put_24, /* PARGB6666 */
	gdi_resizer_put_mainlcd, // main
#ifdef __MMI_SUBLCD__
	gdi_resizer_put_sublcd
#else
	(gdi_resizer_put_func)gd_null_pointer_function, // sub
#endif
};
const static gdi_resizer_put_func gdi_non_resizer_put_table[]=
{
#if defined(GDI_USE_RGB565_TO_SHOW_BW)
    gdi_non_resizer_put_1,
#else
	(gdi_resizer_put_func)gd_null_pointer_function, // 1
#endif
	gdi_non_resizer_put_8, // 8
	gdi_non_resizer_put_16, // 16
	gdi_non_resizer_put_24, // 24
	gdi_non_resizer_put_32, // 32
	gdi_non_resizer_put_32, // 32PARGB
	(gdi_resizer_put_func)gd_null_pointer_function, // UYVY422
	gdi_non_resizer_put_8,  /* A8 */
	gdi_non_resizer_put_24,  /* PARGB6666 */
	gdi_non_resizer_put_mainlcd, // main
#ifdef __MMI_SUBLCD__
	gdi_non_resizer_put_sublcd // sub
#else
	(gdi_resizer_put_func)gd_null_pointer_function // sub
#endif
};


/*****************************************************************************
 * FUNCTION
 *  gdi_resizer_init
 * DESCRIPTION
 *  init the resizer
 * PARAMETERS
 *  src_width              [IN] srouce width
 *  src_height             [IN] source height
 *  src_clipx1             [IN] source clip x1
 *  src_clipy1             [IN] source clip y1
 *  src_clipx2             [IN] source clip x2
 *  src_clipy2             [IN] source clip y2
 *  dest_x1                [IN] destination x1
 *  dest_y1                [IN] destination y1
 *  dest_x2                [IN] destination x2
 *  dest_y2                [IN] destination y2
 * RETURNS
 *  S32, return 0 if no pixel to output, 1 if decoder need to continue to decode, -1 if error.
 *****************************************************************************/
S32 gdi_resizer_init(
				S32 src_width,S32 src_height, 
				S32 src_clipx1, S32 src_clipy1,S32 src_clipx2,S32 src_clipy2,
				S32 dest_x1,S32 dest_y1,S32 dest_x2,S32 dest_y2)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S32 dest_width;
	S32 dest_height;
	S32 dir_x;
	S32 dir_y;
	S32 src_width_range;
	S32 src_height_range;
	S32 dest_width_range;
	S32 dest_height_range;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    GDI_RESIZER.want_sx = GDI_RESIZER.want_sy = -1;
	
	// we don't support 1, 8 bites color format in this resizer
	if(src_width  <=0) return 0;
	if(src_height <=0) return 0;

	dir_x=dir_y=1;

	if(dest_x1>dest_x2)
	{
		GDI_DEBUG_ASSERT(0); // current, we only support dir_x >0
		SWAP(S32,dest_x1,dest_x2);
		dir_x = -1;
	}
	if(dest_y1>dest_y2)
	{
		SWAP(S32,dest_y1,dest_y2);
		dir_y = -1;
	}
	

	GDI_RESIZER.dest = gdi_act_layer->buf_ptr;
	GDI_RESIZER.offset_dx = dest_x1;
	GDI_RESIZER.offset_dy = dest_y1;
	dest_width = dest_x2-dest_x1+1;
	dest_height = dest_y2-dest_y1+1;
	
    gdi_image_set_update_area(dest_x1,dest_y1,dest_x2,dest_y2);
	
	GDI_RESIZER.want_dx1 = dest_x1;
	GDI_RESIZER.want_dy1 = dest_y1;
	GDI_RESIZER.want_dx2 = dest_x2;
	GDI_RESIZER.want_dy2 = dest_y2;

	GDI_RESIZER.dir_x = dir_x;
	GDI_RESIZER.dir_y = dir_y;
	
	// check the clipping area
	GDI_CLIP_RECT_TEST(GDI_RESIZER.want_dx1,GDI_RESIZER.want_dy1,GDI_RESIZER.want_dx2,GDI_RESIZER.want_dy2,return 0); // no pixel will be render !!

	/* W06.16 Use Coordinate Range instead of width and height */
	/* Use round off to compute want_sx and want_sy */
	if (1 == src_width)
	{
		src_width = 2;
	}
	if (1 == dest_width)
	{
		dest_width = 2;
	}
	if (1 == src_height)
	{
		src_height = 2;
	}
	if (1 == dest_height)
	{
		dest_height = 2;
	}
	src_width_range = src_width - 1;
	src_height_range = src_height - 1;
	dest_width_range  = dest_width - 1;
	dest_height_range  = dest_height - 1;
	//multiply_factor = ((src_width_range) <<16)/ (dest_width_range);
	// make y_scale_factor
	//GDI_RESIZER.y_scale_factor 		= ((src_height_range)<<16)  / (dest_height_range);
	//GDI_RESIZER.y_inv_scale_factory	= ((dest_height_range)<<16 )  / (src_height_range);
	GDI_RESIZER.src_height_range 		= src_height_range;
	GDI_RESIZER.dest_height_range	= dest_height_range;

	{
		int dx1,dy1,dx2,dy2;
		// check the source clipping area
		dx1 = dest_x1 + (src_clipx1*(dest_width_range<<1) + src_width_range)/(src_width_range<<1);
		dx2 = dest_x1 + (src_clipx2*(dest_width_range<<1) + src_width_range)/(src_width_range<<1);		
		
		dy1 = dest_y1 + (src_clipy1*(dest_height_range<<1) + src_height_range)/(src_height_range<<1);
		dy2 = dest_y1 + (src_clipy2*(dest_height_range)<<1) + src_height_range/(src_height_range<<1);		

		GDI_CLIP_TWO_RECT(
			GDI_RESIZER.want_dx1, GDI_RESIZER.want_dy1,
			GDI_RESIZER.want_dx2, GDI_RESIZER.want_dy2,
			dx1,dy1,
			dx2,dy2,
			return 0);

		//W05.39 Check points may be out of clipping area because of dividing
		while ( (((GDI_RESIZER.want_dx1 - GDI_RESIZER.offset_dx) * src_width_range << 1) + dest_width_range) / (dest_width_range << 1) < src_clipx1 )
			GDI_RESIZER.want_dx1++;

		while((((GDI_RESIZER.want_dy1 - GDI_RESIZER.offset_dy) * src_height_range << 1) + dest_height_range) / (dest_height_range << 1) < src_clipy1)
			GDI_RESIZER.want_dy1++;

		while ( (((GDI_RESIZER.want_dx2 - GDI_RESIZER.offset_dx) * src_width_range << 1) + dest_width_range) / (dest_width_range << 1) > src_clipx2 )
			GDI_RESIZER.want_dx2--;

		while((((GDI_RESIZER.want_dy2 - GDI_RESIZER.offset_dy) * src_height_range << 1) + dest_height_range) / (dest_height_range << 1)> src_clipy2)
			GDI_RESIZER.want_dy2--;
	}


	// non resize handler
	if(src_width == dest_width && src_height == dest_height)
	{
		U32 bytes_per_pixel = gdi_bits_per_pixel(gdi_act_layer->cf) >> 3;
		GDI_RESIZER.want_sx_table = NULL;
		GDI_RESIZER.dest = gdi_act_layer->buf_ptr;
		if(dir_x >0)
		{
			GDI_RESIZER.want_sx 		=
			GDI_RESIZER.want_start_sx = GDI_RESIZER.want_dx1 - dest_x1;
			GDI_RESIZER.want_end_sx 	= GDI_RESIZER.want_dx2 - dest_x1;
			
			GDI_RESIZER.dest 		+= GDI_RESIZER.want_dx1*bytes_per_pixel;
		}
		else
		{
			GDI_RESIZER.want_sx 		=
			GDI_RESIZER.want_start_sx = GDI_RESIZER.want_dx2 - dest_x1;
			GDI_RESIZER.want_end_sx  = GDI_RESIZER.want_dx1 - dest_x1;
			GDI_RESIZER.dest 		+= GDI_RESIZER.want_dx2*bytes_per_pixel;			
		}
		if(dir_y >0)
		{
			GDI_RESIZER.want_sy 		=
			GDI_RESIZER.want_start_sy = GDI_RESIZER.want_dy1 - dest_y1;
			GDI_RESIZER.want_end_sy 	= GDI_RESIZER.want_dy2 - dest_y1;
			
			GDI_RESIZER.dest 		+= GDI_RESIZER.want_dy1*gdi_act_layer->width*bytes_per_pixel;
			GDI_RESIZER.dest_pitch_jump = (gdi_act_layer->width - (GDI_RESIZER.want_dx2-GDI_RESIZER.want_dx1+1)) * bytes_per_pixel; ///TODO: only support dir_x>0
		}
		else
		{
			GDI_RESIZER.want_sy 		=
			GDI_RESIZER.want_start_sy = GDI_RESIZER.want_dy2 - dest_y1;
			GDI_RESIZER.want_end_sy  = GDI_RESIZER.want_dy1 - dest_y1;
			
			GDI_RESIZER.dest 		+= GDI_RESIZER.want_dy2*gdi_act_layer->width*bytes_per_pixel;
			GDI_RESIZER.dest_pitch_jump = -(gdi_act_layer->width + (GDI_RESIZER.want_dx2-GDI_RESIZER.want_dx1+1)) * bytes_per_pixel; ///TODO: only support dir_x>0
		}
		gdi_resizer_put = gdi_non_resizer_put_table[gdi_act_layer->cf];

		return 1;		
	}


	// return error when want_sx_table memory is not enough
	if ( GDI_RESIZER.want_dx2-GDI_RESIZER.want_dx1+1 > GDI_MAX_RESIZE_WIDTH)
    {
        return -1;
    }

        GDI_RESIZER.want_sx_table = (S16*)gdi_alloc_ext_mem(GDI_MAX_RESIZE_WIDTH * sizeof(S16));
        if (GDI_RESIZER.want_sx_table == NULL)
        {
            return 0;
        }

	// make want_x table
	{
		S32 x;
		S32 i;
		S32 start_dx,end_dx;

		if(dir_x>0)
		{
			start_dx = GDI_RESIZER.want_dx1;
			end_dx = GDI_RESIZER.want_dx2;
		}
		else
		{
			start_dx = GDI_RESIZER.want_dx2;
			end_dx = GDI_RESIZER.want_dx1;
		}
			
		for(i=0,x=start_dx;x<=end_dx;x+=dir_x,i++)
		{
			S32 d = x - dest_x1;
			GDI_RESIZER.want_sx_table[i] = (S16)((((d) * src_width_range << 1) + dest_width_range) / (dest_width_range << 1));
		}
		GDI_RESIZER.want_sx_table_end = GDI_RESIZER.want_sx_table+i;
		GDI_RESIZER.next_want_sx = GDI_RESIZER.want_sx_table;
	}

	if(dir_y>0)
		GDI_RESIZER.next_want_dy = GDI_RESIZER.want_dy1;
	else
		GDI_RESIZER.next_want_dy = GDI_RESIZER.want_dy2;

	// set the put pixel function pointer
	gdi_resizer_put = gdi_resizer_put_table[gdi_act_layer->cf];
	// init want position
	if(dir_x>0)
		GDI_RESIZER.dest = gdi_act_layer->buf_ptr + (((GDI_RESIZER.want_dx1+ GDI_RESIZER.next_want_dy* gdi_act_layer->width )* bitsperpixels[gdi_act_layer->vcf])>>3);
	else
		GDI_RESIZER.dest = gdi_act_layer->buf_ptr + (((GDI_RESIZER.want_dx2+ GDI_RESIZER.next_want_dy* gdi_act_layer->width )* bitsperpixels[gdi_act_layer->vcf])>>3);
	
	GDI_RESIZER.want_sx = *GDI_RESIZER.next_want_sx++;
	GDI_RESIZER.want_sy = ((((GDI_RESIZER.next_want_dy  - GDI_RESIZER.offset_dy) * src_height_range << 1) +  dest_height_range) / (dest_height_range << 1));
	GDI_RESIZER.next_want_dy +=dir_y;
	return 1;
}


/*****************************************************************************
 * FUNCTION
 *  gdi_image_resizer_deinit
 * DESCRIPTION
 *  Deinit resizer.
 * PARAMETERS
 *  void
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_resizer_deinit(void)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    // TODO: use memory allocator
    if (GDI_RESIZER.want_sx_table)
    {
        gdi_free_ext_mem((void*)&GDI_RESIZER.want_sx_table);
        GDI_RESIZER.want_sx_table = NULL;
    }
}
#endif

/*****************************************************************************
 * FUNCTION
 *  gdi_fill_dot_rect
 * DESCRIPTION
 *  
 * PARAMETERS
 * RETURNS
 *  
 *****************************************************************************/
void gdi_fill_dot_rect(S32 x1,S32 y1,S32 x2,S32 y2,gdi_color c)
{
    GDI_ENTER_CRITICAL_SECTION(gdi_layer_fill_dot_rect)
    {
        gd_fill_dot_rect[gdi_act_layer->cf](
                                                gdi_act_layer->buf_ptr,
                                                gdi_act_layer->width,
                                                gdi_act_layer->height,
                                                x1,y1,
                                                x2,y2,
                                                gdi_act_layer->clipx1,
                                                gdi_act_layer->clipy1,
                                                gdi_act_layer->clipx2,
                                                gdi_act_layer->clipy2,
                                                c);
    }
    GDI_EXIT_CRITICAL_SECTION(gdi_layer_fill_dot_rect)
}

void gdi_bitblt_internal(U8* src_ptr, U32 src_pitch, 
			S32 src_offset_x, S32 src_offset_y, 
			U32 src_width, U32 src_height,
			gdi_color_format src_cf,
			U8* dest_ptr, U32 dest_pitch,
			S32 dest_offset_x, S32 dest_offset_y,
			S32 dest_clip_x1, S32 dest_clip_y1, S32 dest_clip_x2, S32 dest_clip_y2,
			gdi_color_format dest_cf, U32 dest_buf_size,
			BOOL tilt,
			BOOL enable_src_key, gdi_color src_key,
			BOOL is_alpha_blending, U8 alpha_value,
			BOOL is_rop_mode, U8 rop_value,
			U8 transform_value,
			U8 transform_direction)
{
#if defined(GDI_USING_2D_ENGINE_V2)
	gdi_2d_buffer_struct src;
	gdi_2d_begin();
    if (GDI_TRANSFORM_DIRECTION_RB == transform_direction)
    {
	    gdi_2d_set_buffer(&src,src_ptr,src_pitch,
		    		src_width,src_height,
			    	src_offset_x + src_width - 1, src_offset_y + src_height - 1,
				    src_offset_x,src_offset_y,
				    src_offset_x + src_width - 1, src_offset_y + src_height - 1,
				    src_cf);
	    gdi_2d_set_dest_buffer(dest_ptr, dest_pitch,
		    		src_width, src_height,
			    	dest_offset_x + src_width - 1, dest_offset_y + src_height - 1,
				    dest_clip_x1, dest_clip_y1,
				    dest_clip_x2, dest_clip_y2,
				    dest_cf);
    }
    else
    {
	    gdi_2d_set_buffer(&src,src_ptr,src_pitch,
		    		src_width,src_height,
			    	src_offset_x,src_offset_y,
				    src_offset_x,src_offset_y,
				    src_offset_x + src_width - 1, src_offset_y + src_height - 1,
				    src_cf);
	    gdi_2d_set_dest_buffer(dest_ptr, dest_pitch,
		    		src_width, src_height,
			    	dest_offset_x, dest_offset_y,
				    dest_clip_x1, dest_clip_y1,
				    dest_clip_x2, dest_clip_y2,
				    dest_cf);
    }
	gdi_2d_bitblt(tilt,
			enable_src_key, src_key,
			is_alpha_blending, alpha_value,
			is_rop_mode, rop_value,
			transform_value,
			transform_direction,
			&src, 1);
	gdi_2d_end();
#endif
}

void gdi_bits_draw(S32 x,S32 y,U8* src,S32 src_size,S32 w,S32 h,S32 bits_per_pixel,gdi_color palette[])
{
    GDI_ENTER_CRITICAL_SECTION(gdi_image_bits_draw)
    {
        gd_image_bits_draw[gdi_act_layer->cf](
                                                gdi_act_layer->buf_ptr,
                                                gdi_act_layer->width,
                                                gdi_act_layer->height,
                                                x,y,
                                                x+w-1,y+h-1,
                                                src,w,h,
                                                0,0,w-1,h-1,
                                                src_size,bits_per_pixel,palette,
                                                gdi_act_layer->clipx1,
                                                gdi_act_layer->clipy1,
                                                gdi_act_layer->clipx2,
                                                gdi_act_layer->clipy2);
    }
    GDI_EXIT_CRITICAL_SECTION(gdi_image_bits_draw)
}


#define R_OF_RGB565(p)      ((p) >> 11)
#define G_OF_RGB565(p)      (((p) << 21) >> 26)
#define B_OF_RGB565(p)      (((p) << 27) >> 27)

/*****************************************************************************
 * FUNCTION
 *  gdi_rgb565_to_rgb888
 * DESCRIPTION
 *  convert RGB565 to RGB888
 * PARAMETERS
 *  p       [IN]    RGB565 pixel
 * RETURNS
 *  24-bit RGB888 pixel
 *****************************************************************************/
U32 gdi_rgb565_to_rgb888(U32 p)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/   
    U32     result;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    /* extend p's B[4:0] to result B[7:0] */
    result = B_OF_RGB565(p) << 3;

    /* extend p's G[10:5] to result G[15:8] */
    result += G_OF_RGB565(p) << (8 + 2);

    /* extend p's R[15:11] to result B[23:16] */
    result += R_OF_RGB565(p) << (16 + 3);

    return result;
}


void gdi_draw_circle(S32 x, S32 y, S32 r, gdi_color circle_color)
{
    GDI_ENTER_CRITICAL_SECTION(gdi_draw_circle)
    {
        float delta;
        S32 m, n;

        for (delta = 5.0 / 4 - r, m = 0, n = r; m <= n; m ++)
        {
            gdi_draw_point(x + m, y + n, circle_color);
            gdi_draw_point(x + m, y - n, circle_color);
            gdi_draw_point(x - m, y + n, circle_color);
            gdi_draw_point(x - m, y - n, circle_color);
            gdi_draw_point(x + n, y + m, circle_color);
            gdi_draw_point(x + n, y - m, circle_color);
            gdi_draw_point(x - n, y + m, circle_color);
            gdi_draw_point(x - n, y - m, circle_color);

            if (delta >= 0)
            {
                delta += 2.0 * (m - n) + 5;
                n --;
            }
            else
            {
                delta += m * 2.0 + 3;
            }
        }
    }
    GDI_EXIT_CRITICAL_SECTION(gdi_draw_circle)
}

void gdi_draw_solid_circle(S32 x, S32 y, S32 r, gdi_color circle_color)
{
    GDI_ENTER_CRITICAL_SECTION(gdi_draw_solid_circle)
    {
        float delta;
        S32 m, n;

        for (delta = 5.0 / 4 - r, m = 0, n = r; m <= n; m ++)
        {
            gdi_draw_line(x + m, y + n, x + m, y - n, circle_color);
            gdi_draw_line(x - m, y + n, x - m, y - n, circle_color);
            gdi_draw_line(x + n, y + m, x + n, y - m, circle_color);
            gdi_draw_line(x - n, y + m, x - n, y - m, circle_color);

            if (delta >= 0)
            {
                delta += 2.0 * (m - n) + 5;
                n --;
            }
            else
            {
                delta += m * 2.0 + 3;
            }
        }
    }
    GDI_EXIT_CRITICAL_SECTION(gdi_draw_solid_circle)
}

void gdi_draw_arc(S32 circle_x, S32 circle_y, S32 circle_r, int startAngle, int angleExtent, gdi_color color)
{
    GDI_ENTER_CRITICAL_SECTION(gdi_draw_arc)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    /* 
     * Bresenham Algorithm: Draw 1/8 circle
     */
    int x, y, d;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    x = 0;
    y = circle_r;
    d = 1 - circle_r;

    while (x < y)
    {
        if ((startAngle >= 0 || startAngle <= 45 / 2) && (angleExtent > 45 / 2))
        {   /* first 1/8 circle, angle:(0~45), coord.:(x,y) */
            gdi_draw_point(circle_x + x, circle_y + y, color);
        }
        if ((startAngle > 45 - 45 / 2 || startAngle <= 45 + 45 / 2) && (angleExtent > 90 - 45 / 2))
        {   /* second 1/8 circle, angle:(45~90), coord.:(y,x) */
            gdi_draw_point(circle_x + y, circle_y + x, color);
        }
        if ((startAngle > 90 - 45 / 2 || startAngle <= 90 + 45 / 2) && (angleExtent > 135 - 45 / 2))
        {   /* third 1/8 circle, angle:(90~135), coord.:(y,-x) */
            gdi_draw_point(circle_x + y, circle_y - x, color);
        }
        if ((startAngle > 135 - 45 / 2 || startAngle <= 135 + 45 / 2) && (angleExtent > 180 - 45 / 2))
        {   /* fourth 1/8 circle, angle:(135~180), coord.:(x,-y) */
            gdi_draw_point(circle_x + x, circle_y - y, color);
        }
        if ((startAngle > 180 - 45 / 2 || startAngle <= 180 + 45 / 2) && (angleExtent > 225 - 45 / 2))
        {   /* fifth 1/8 circle, angle:(180~225), coord.:(-x,-y) */
            gdi_draw_point(circle_x - x, circle_y - y, color);
        }
        if ((startAngle > 225 - 45 / 2 || startAngle <= 225 + 45 / 2) && (angleExtent > 270 - 45 / 2))
        {   /* sixth 1/8 circle, angle:(225~270), coord.:(-y,-x) */
            gdi_draw_point(circle_x - y, circle_y - x, color);
        }
        if ((startAngle > 270 - 45 / 2 || startAngle <= 270 + 45 / 2) && (angleExtent > 315 - 45 / 2))
        {   /* fourth 1/8 circle, angle:(270~315), coord.:(-y,x) */
            gdi_draw_point(circle_x - y, circle_y + x, color);
        }
        if ((startAngle > 315 - 45 / 2 || startAngle <= 315 + 45 / 2) && (angleExtent > 360 - 45 / 2))
        {   /* fourth 1/8 circle, angle:(315~360), coord.:(-x,y) */
            gdi_draw_point(circle_x - x, circle_y + y, color);
        }

        if (d > 0)
        {
            d += 2 * (x - y) + 5;
            y--;
        }
        else
        {
            d += 2 * x + 3;
        }
        x++;
    }
    GDI_EXIT_CRITICAL_SECTION(gdi_draw_arc)
}


void gdi_draw_solid_arc(S32 circle_x, S32 circle_y, S32 circle_r, int startAngle, int angleExtent, gdi_color color)
{
    GDI_ENTER_CRITICAL_SECTION(gdi_draw_solid_arc)
        
    gdi_draw_arc(circle_x, circle_y, circle_r, startAngle, angleExtent, color);
    circle_r--;
    while (circle_r > 0)
    {
        gdi_draw_arc(circle_x, circle_y, circle_r, startAngle, angleExtent, color);
        gdi_draw_point(circle_x + circle_r, circle_y + circle_r, color);
        gdi_draw_point(circle_x + circle_r, circle_y - circle_r, color);
        gdi_draw_point(circle_x - circle_r, circle_y - circle_r, color);
        gdi_draw_point(circle_x - circle_r, circle_y + circle_r, color);
        circle_r--;
    }
    gdi_draw_point(circle_x, circle_y, color);
    
    GDI_EXIT_CRITICAL_SECTION(gdi_draw_solid_arc)
}


/*****************************************************************************
 * FUNCTION
 *  gdi_draw_antialiasing_line
 * DESCRIPTION
 *  draw a anti-aliasing line
 * PARAMETERS
 *  x1          [IN]    the start point x1
 *  y1          [IN]    the start point y1
 *  x2          [IN]    the end point x2
 *  y2          [IN]    the end point y2
 *  line_color  [IN]    the color of line
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_draw_antialiasing_line(S32 x1, S32 y1, S32 x2, S32 y2, gdi_color line_color)
{
    GDI_ENTER_CRITICAL_SECTION(gdi_draw_antialiasing_line)
    const S32 gaussian_integral[21] =   {   32767,      26958,      21431,      16431,      12127,
                                            8603,       5859,       3825,       2393,       1432,
                                            820,        449,        234,        117,         55,
                                            25,         11,          4,          1,          0,
                                            0};
/*        __|__                         */
/*      _/  |  \_                       */
/*     /    |    \                      */
/* ___/     |     \___                  */
/* --------------------------->x-axis   */
/*          0    1     2    3           */
/* the gaussian_integral[x*10] is the integral of gaussian from x to infinity and shift left 16 */

    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    BOOL steep;                                             /* y-scan or x-scan */
    S32 slope, dx, dy, d, incrE, incrNE, x, y, two_v_dx;    /* slope is the direction that y goes */
    U32 lineA, lineR, lineG, lineB;                         /* color of line */
    double sqrt_result;                                     /* used to store result of sqrt() */
    S32 invDenom_i, two_dx_invDemon_i;                      /* used to compute distance between pixel and line */
    S32 tmp;                                                /* used in cliping */
    gdi_handle abm_src_layer;                               /* abm source layer */
    BOOL source_key_enable;                                 /* if the source layer enable the source key */
    gdi_color source_key_value;                             /* the source key value of abm source layer */
    S32 clipx1, clipx2, clipy1, clipy2;                     /* the clip area */
    U8* src_layer_buf_ptr = NULL;                           /* buf_ptr of abm_src_layer*/
    S32 src_layer_layer_width = 0;                          /* layer_width of abm_src_layer*/
    gd_get_buf_pixel_func src_layer_get_pixel = NULL;       /* get_buf_pixel() function pointer of abm_src_layer*/
    gd_color_to_rgb_func src_layer_color_to_rgb = NULL;     /* color_to_rgb() functio pointer of abm_src_layer*/
    
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    MMI_ASSERT((GDI_LAYER.clipy1 <= GDI_LAYER.clipy2) && (GDI_LAYER.clipx1 <= GDI_LAYER.clipx2));
    
    if ((y1 == y2) && (y1 >= GDI_LAYER.clipy1) && (y1 <= GDI_LAYER.clipy2)) // Horizontal Line /
	{
		if(x1 > x2)     // exchange x /
		{
		    tmp = x1;
            x1 = x2;
            x2 = tmp;
		}
        if((x1 <= GDI_LAYER.clipx2) && (x2 >= GDI_LAYER.clipx1))
        {
            GDI_CLIP_LINE_X_TEST(x1,x2);
            GDI_DRAW_H_LINE(x1, x2, y1, line_color);
    	}
	}
	else if ((x1 == x2) && (x1 >= GDI_LAYER.clipx1) && (x1 <= GDI_LAYER.clipx2)) // Vertical Line /
    {
		if(y1 > y2)     // exchange y /
		{
			tmp = y1;
            y1 = y2;
            y2 = tmp;
		}
        if((y1 <= GDI_LAYER.clipy2) && (y2 >= GDI_LAYER.clipy1))
        {
            GDI_CLIP_LINE_Y_TEST(y1,y2);
            GDI_DRAW_V_LINE(y1, y2, x1, line_color);
	    }
	}
    else
    {
        /* get abm source layer */
        gdi_layer_get_source_key(&source_key_enable, &source_key_value);
        gdi_get_alpha_blending_source_layer(&abm_src_layer);
        if (abm_src_layer != GDI_ERROR_HANDLE)
        {   
            gdi_layer_struct *tmpstruc = (gdi_layer_struct*)abm_src_layer;
            src_layer_buf_ptr = tmpstruc->buf_ptr;
            src_layer_layer_width = tmpstruc->width;
            src_layer_get_pixel = gd_get_buf_pixel[tmpstruc->cf];
            src_layer_color_to_rgb = gd_color_to_rgb[tmpstruc->cf];
        }
        
        /* clip */
        gdi_layer_get_clip(&clipx1, &clipy1, &clipx2, &clipy2);

        if (x1 > x2)    
        {
            /* exchange two points: (x1,y1) and (x2,y2)*/
            tmp = x1;
            x1 = x2;
            x2 = tmp;

            tmp = y1;
            y1 = y2;
            y2 = tmp;
        }
        dx = x2 - x1;
        dy = y2 - y1;
        
        /* if the slope>1, need to exchange x-axis and y-axis */
        steep = (abs(y2 - y1) > abs(x2 - x1));  
        if (steep)
        {
            /* exchange x1 and y1, x2 and y2 */
            tmp = x1;
            x1 = y1;
            y1 = tmp;

            tmp = x2;
            x2= y2;
            y2 = tmp;
        }
        
        if (x1 > x2)    
        {
            /* exchange two points: (x1,y1) and (x2,y2) */
            tmp = x1;
            x1 = x2;
            x2 = tmp;

            tmp = y1;
            y1 = y2;
            y2 = tmp;
        }
        
        dx = x2 - x1;
        dy = y2 - y1;
        
        if (dy < 0)
        {
            slope = -1;
            dy = -dy;
        }
        else
        {
            slope = 1;
        }

        /* variables for mid-point draw line algorithm */
        d = (2 * dy) - dx;
        incrE = 2 * dy;
        incrNE = 2 * (dy - dx);

        /* variables for anti-aliasing */
        two_v_dx = 0;
        sqrt_result = sqrt((double)(dx * dx + dy * dy));
        invDenom_i = (S32)((1.0 / (2.0 * sqrt_result)) * (1 << AA_LINE_PRECISION_BITS)); /* shift AA_LINE_PRECISION_BITS to make float to S32 */
        two_dx_invDemon_i = 2 * dx * invDenom_i;

        /* get line color rgb */
        gdi_act_color_to_rgb(&lineA, &lineR, &lineG, &lineB, line_color);

        for (x = x1, y = y1; x <= x2; x++)
        {
            S32 slope1, slope2, outx, outy, clip1, clip2;
            if (steep)
            {
                outx = y;
                outy = x;
                slope1 = slope;
                slope2 = 0;
                clip1 = clipx1;
                clip2 = clipx2;
            }
            else
            {
                outx = x;
                outy = y;
                slope1 = 0;
                slope2 = slope;
                clip1 = clipy1;
                clip2 = clipy2;
            }
            do
            {
                if ((outx < clipx1) || (outx > clipx2) ||
            	    (outy < clipy1) || (outy > clipy2))
            	{
            	    break;	
            	}
                ANTIALIASING_LINE_PUT_PIXEL(outx, outy, lineR, lineG, lineB, two_v_dx * invDenom_i);
                tmp = y + slope;
                if (tmp >= clip1 && tmp <= clip2)
                {
                    ANTIALIASING_LINE_PUT_PIXEL(outx + slope1, outy + slope2, lineR, lineG, lineB, two_dx_invDemon_i - two_v_dx * invDenom_i);
                }
                tmp = y - slope;
                if (tmp >= clip1 && tmp <= clip2)
                {
                    ANTIALIASING_LINE_PUT_PIXEL(outx - slope1, outy - slope2, lineR, lineG, lineB, two_dx_invDemon_i + two_v_dx * invDenom_i);
                }
            }while(0);
            if (d < 0)
            {
                two_v_dx = d + dx;
                d += incrE;
            }
            else
            {
                two_v_dx = d - dx;
                d += incrNE;
                y += slope;
            }
        }
    }
 
    GDI_EXIT_CRITICAL_SECTION(gdi_draw_antialiasing_line)
}


/*****************************************************************************
 * FUNCTION
 *  gdi_draw_polygon
 * DESCRIPTION
 *  draw a polygon
 * PARAMETERS
 *  point_array [IN]    point array
 *  point_num   [IN]    the number of elements of point array
 *  color       [IN]    the color of polygon
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_draw_polygon(gdi_point2D_struct point[], U32 point_num, gdi_color color)
{
    GDI_ENTER_CRITICAL_SECTION(gdi_draw_polygon)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/       
    S32 i;
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (point_num == 0)
    {
        GDI_RETURN_VOID;
    }
    if (point_num == 1)
    {
        gdi_draw_point(point[0].x, point[0].y, color);
        GDI_RETURN_VOID;
    }
    for (i = 0 ; i < point_num ; i++)
    {
        U32 idx = (i + 1);
        if (i == (point_num - 1))
        {
            idx = 0;
        }
        gdi_draw_line_ext(point[i].x, point[i].y, point[idx].x, point[idx].y, color, FALSE);
    }
    
    GDI_EXIT_CRITICAL_SECTION(gdi_draw_polygon)
}


static void gdi_draw_solid_polygon_AddNode(
                gdi_polygon_edge_struct **head, 
                gdi_polygon_edge_struct *node)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (*head == NULL)
    {
        *head = node;
        node->next = NULL;
    }
    else
    {
        gdi_polygon_edge_struct *Etmp = *head;
        if (((node->Xfra == 0) && (Etmp->Xint == node->Xint)) || 
            (Etmp->Xint > node->Xint))
        {
            node->next = Etmp;
            *head = node;
            return;
        }
        while (Etmp->next != NULL)
        {
            /* insert node with ordering by Xcur */
            if (((node->Xfra == 0) && (Etmp->next->Xint == node->Xint)) || 
                (Etmp->next->Xint > node->Xint))
            {
                node->next = Etmp->next;
                Etmp->next = node;
                break;
            }
            Etmp = Etmp->next;
        }
        if (Etmp->next == NULL)
        {
            /* insert to tail */
            Etmp->next = node;
            node->next = NULL;
        }
    }
}
static void gdi_draw_solid_polygon_RemoveNextNode(
                gdi_polygon_edge_struct **head, 
                gdi_polygon_edge_struct *node)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (node == NULL)
    {
        *head = (*head)->next;
    }
    else if (node->next !=  NULL)
    {
        node->next = node->next->next;
    }
}

static void gdi_draw_solid_polygon_UpdateX(
                gdi_polygon_edge_struct *head)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    gdi_polygon_edge_struct *Etmp = NULL;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    Etmp = head;
    while (Etmp != NULL)
    {
        if (Etmp->slope_y == 0)
        {
            Etmp = Etmp->next;
            continue;
        }
        Etmp->Xfra += Etmp->slope_x;
        if (Etmp->Xfra > 0)
        {
            while (Etmp->Xfra > Etmp->slope_y)
            {
                Etmp->Xint++;
                Etmp->Xfra -= Etmp->slope_y;
            }
        }
        else if (Etmp->Xfra < 0)
        {
            while ((0 - Etmp->Xfra) > Etmp->slope_y)
            {
                Etmp->Xint--;
                Etmp->Xfra += Etmp->slope_y;
            }
        }
        Etmp = Etmp->next;
    }
}


/*****************************************************************************
 * FUNCTION
 *  gdi_draw_solid_polygon
 * DESCRIPTION
 *  draw a solid polygon
 * PARAMETERS
 *  point_array [IN]    point array
 *  point_num   [IN]    the number of elements of point array
 *  color       [IN]    the color of polygon
 * RETURNS
 *  void
 *****************************************************************************/
GDI_RESULT gdi_draw_solid_polygon(
            gdi_point2D_struct point[], 
            U32 point_num, 
            gdi_color color)
{
    GDI_ENTER_CRITICAL_SECTION(gdi_draw_solid_polygon)
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/       
    S32 i;
    S32 Ycur;   /* Current Y */
    gdi_polygon_edge_struct *AET_head = NULL, *AET_head_sort = NULL;    /* Active Edge Table */
    gdi_polygon_edge_struct *EYmin = NULL, *EYmax = NULL;  /* Edge with the smallest Y & largest Y */
    gdi_polygon_edge_struct *ET = NULL;     /* Edge Table*/
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (point_num == 0)
    {
        GDI_RETURN(GDI_SUCCEED);
    }
    else if (point_num == 1)
    {
        gdi_draw_point(point[0].x, point[0].y, color);
        GDI_RETURN(GDI_SUCCEED);
    }
    else if (point_num == 2)
    {
        gdi_draw_line_ext(point[0].x, point[0].y, point[1].x, point[1].y, color, FALSE);
        GDI_RETURN(GDI_SUCCEED);
    }

    ET = (gdi_polygon_edge_struct*)gdi_alloc_ext_mem(sizeof(gdi_polygon_edge_struct)*point_num);
    if (ET == NULL)
    {
        GDI_RETURN(GDI_FAILED);
    }
    
    /* init Edge Table (ET) */
    for (i = 0 ; i < point_num ; i++)
    {
        gdi_point2D_struct *p1, *p2;
        U32 idx = (i + 1);

        if (i == (point_num - 1))
        {
            idx = 0;
        }
        /* p1 is the point with smaller y */
        /* If p1.y==p2.y, p1 is the point with larger x. */
        if ((point[i].y < point[idx].y) ||
            ((point[i].y == point[idx].y) && (point[i].x > point[idx].x)))
        {
            p1 = &point[i];
            p2 = &point[idx];
        }
        else
        {
            p1 = &point[idx];
            p2 = &point[i];
        }
        ET[i].Ymax = p2->y;
        ET[i].Ymin = p1->y;
        ET[i].slope_x = p2->x - p1->x;
        ET[i].slope_y = p2->y - p1->y;
        ET[i].Xint = p1->x;
        ET[i].Xfra = ET[i].slope_y;
        if (ET[i].slope_x < 0)
        {
            ET[i].Xfra = 0 - ET[i].Xfra;
        }
        ET[i].next = NULL;

        /* Find EYmin and EYmax */
        if ((EYmin == NULL) || (ET[i].Ymin < EYmin->Ymin))
        {
            EYmin = &ET[i];
        }
        if ((EYmax == NULL) || (ET[i].Ymax > EYmax->Ymax))
        {
            EYmax = &ET[i];
        }
    }

    for (Ycur = EYmin->Ymin ; Ycur <= EYmax->Ymax ; )
    {
        gdi_polygon_edge_struct *Etmp = NULL, *Epre = NULL;

        /* remove edges with Ymax==Ycur from AET */
        Etmp = AET_head;
        while (Etmp != NULL)
        {
            if ((Etmp->Ymax <= Ycur) && (Ycur != EYmax->Ymax))
            {
                gdi_draw_solid_polygon_RemoveNextNode(&AET_head, Epre);
            }
            else
            {
                Epre = Etmp;
            }
            Etmp = Etmp->next;
        }
        
        /* resort by X */
        AET_head_sort = NULL;
        while (AET_head != NULL)
        {
            Etmp = AET_head;
            AET_head = AET_head->next;
            gdi_draw_solid_polygon_AddNode(&AET_head_sort, Etmp);
        }
        AET_head = AET_head_sort;
        
        /* move edges with Y==Ycur in ET to AET */
        for (i = 0 ; i < point_num ; i++)
        {
            if (ET[i].Ymin == Ycur)
            {
                gdi_draw_solid_polygon_AddNode(&AET_head, &ET[i]);
            }
        }
        /* draw scan line */
        Etmp = AET_head;
        while (Etmp != NULL)
        {
            gdi_polygon_edge_struct *Enxt = Etmp->next;
            while (Enxt != NULL)
            {
                if (Enxt->slope_y != 0)
                {
                    break;
                }
                Enxt = Enxt->next;
            }
            if (Enxt == NULL)
            {
                break;
            }
            if (Etmp->Xint == Enxt->Xint)
            {
                gdi_draw_point(Etmp->Xint, Ycur, color);
            }
            else
            {
                gdi_draw_line_ext(Etmp->Xint, Ycur, Enxt->Xint, Ycur, color, FALSE);
            }
            
            /* go to next next edge */
            Etmp = Enxt;
            Etmp = Etmp->next;
        }

        Ycur++;
        if (Ycur > gdi_act_layer->clipy2)   /* clip y */
        {
            break;
        }
        
        /* update Xcur of edges in AET */
        gdi_draw_solid_polygon_UpdateX(AET_head);

    }
    if (ET != NULL)
    {
        gdi_free_ext_mem((void**)&ET);
    }
    GDI_EXIT_CRITICAL_SECTION(gdi_draw_solid_polygon)
    return GDI_SUCCEED;
}

/*****************************************************************************
 * FUNCTION
 *  gdi_img_buf_trans_color_format
 * DESCRIPTION
 *  
 * PARAMETERS
 *
 * RETURNS
 *  void
 *****************************************************************************/
gdi_img_buf_color_format_enum gdi_img_buf_trans_color_format(U32 gdi_color_format)
{
	switch(gdi_color_format)
	{
        case GDI_COLOR_FORMAT_16:
            return GDI_IMG_BUF_COLOR_FORMAT_RGB565;
            
        case GDI_COLOR_FORMAT_24:
            return GDI_IMG_BUF_COLOR_FORMAT_RGB888;
            
        case GDI_COLOR_FORMAT_32:
            return GDI_IMG_BUF_COLOR_FORMAT_ARGB8888;
            
        case GDI_COLOR_FORMAT_32_PARGB:
            return GDI_IMG_BUF_COLOR_FORMAT_PARGB8888;

        case GDI_COLOR_FORMAT_PARGB6666:
            return GDI_IMG_BUF_COLOR_FORMAT_PARGB6666;
            
        default:
            return GDI_IMG_BUF_COLOR_FORMAT_UNKNOWN;
    
	}
}

/*****************************************************************************
 * FUNCTION
 *  gdi_img_buf_fill_color
 * DESCRIPTION
 *  
 * PARAMETERS
 *
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_img_buf_fill_color(
    gdi_img_buf_struct *dst_buf, 
    gdi_color dst_color,
    S32 width,
    S32 height)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    GDI_DEBUG_ASSERT(dst_buf != NULL);
    
    /* Optimize for empty size */
    if (width <= 0 || height <= 0)
    {
        return;
    }

    /* Optimize for 16 bits */
    switch (dst_buf->color_format)
    {
        case GDI_IMG_BUF_COLOR_FORMAT_RGB565:
            gdi_img_buf_fill_color_16(dst_buf, dst_color, width, height);
            break;
        #if !defined(GDI_SLIM_COLOR_FORMAT)
        case GDI_IMG_BUF_COLOR_FORMAT_RGB888:
        case GDI_IMG_BUF_COLOR_FORMAT_PARGB6666:
            gdi_img_buf_fill_color_24(dst_buf, dst_color, width, height);
            break;
        #endif
        
        case GDI_IMG_BUF_COLOR_FORMAT_ARGB8888:
        case GDI_IMG_BUF_COLOR_FORMAT_PARGB8888:
            gdi_img_buf_fill_color_32(dst_buf, dst_color, width, height);
            break;
        default:
            GDI_DEBUG_ASSERT(0);
            break;
    }
}


/*****************************************************************************
 * FUNCTION
 *  gdi_img_buf_blending_color
 * DESCRIPTION
 *  
 * PARAMETERS
 *
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_img_buf_blending_color(
    gdi_img_buf_struct *dst_buf, 
    gdi_color pargb8888_color,
    S32 width,
    S32 height)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    GDI_DEBUG_ASSERT(dst_buf != NULL);
    
    /* Optimize for empty size */
    if (width <= 0 || height <= 0)
    {
        return;
    }

    /* Optimize for transparent color */
    if (GDI_A_OF_PARGB8888(pargb8888_color) == 0x00)
    {
        /* Do nothing */
        return;
    }

    /* Optimize for opaque color */
    if (GDI_A_OF_PARGB8888(pargb8888_color) == 0xff)
    {
        switch (dst_buf->color_format)
        {
            case GDI_IMG_BUF_COLOR_FORMAT_RGB565:
                gdi_img_buf_fill_color_16(dst_buf, GDI_RGB565_FROM_ARGB8888(pargb8888_color), width, height);
                break;
            #if !defined(GDI_SLIM_COLOR_FORMAT)
            case GDI_IMG_BUF_COLOR_FORMAT_RGB888:
                gdi_img_buf_fill_color_24(dst_buf, GDI_RGB888_FROM_ARGB8888(pargb8888_color), width, height);
                break;
            #endif
            #ifdef GDI_LAYER_PARGB6666_SUPPORT
            case GDI_IMG_BUF_COLOR_FORMAT_PARGB6666:
                gdi_img_buf_fill_color_24(dst_buf, GDI_PARGB6666_FROM_ARGB8888(pargb8888_color), width, height);
                break;
            #endif
            case GDI_IMG_BUF_COLOR_FORMAT_ARGB8888:
            case GDI_IMG_BUF_COLOR_FORMAT_PARGB8888:
                gdi_img_buf_fill_color_32(dst_buf, pargb8888_color, width, height);
                break;
            default:
                GDI_DEBUG_ASSERT(0);
                break;
        }
        return;
    }

    switch (dst_buf->color_format)
    {
        case GDI_IMG_BUF_COLOR_FORMAT_RGB565:
            gdi_img_buf_blending_color_16(dst_buf, pargb8888_color, width, height);
            break;
        #if !defined(GDI_SLIM_COLOR_FORMAT)
        case GDI_IMG_BUF_COLOR_FORMAT_RGB888:
            gdi_img_buf_blending_color_24(dst_buf, pargb8888_color, width, height);
            break;
        #endif

    #ifdef GDI_LAYER_PARGB6666_SUPPORT
        case GDI_IMG_BUF_COLOR_FORMAT_PARGB6666:
            gdi_img_buf_blending_color_PARGB6666(dst_buf, pargb8888_color, width, height);
            break;
    #endif
        
        case GDI_IMG_BUF_COLOR_FORMAT_ARGB8888:
            gdi_img_buf_convert_PARGB8888_from_ARGB8888(dst_buf, width, height);
            gdi_img_buf_blending_color_PARGB8888(dst_buf, pargb8888_color, width, height);
            gdi_img_buf_convert_ARGB8888_from_PARGB8888(dst_buf, width, height);
            break;
        case GDI_IMG_BUF_COLOR_FORMAT_PARGB8888:
            gdi_img_buf_blending_color_PARGB8888(dst_buf, pargb8888_color, width, height);
            break;
        default:
            // not support
            GDI_DEBUG_ASSERT(0);
            break;            
    }
}


/*****************************************************************************
 * FUNCTION
 *  gdi_img_buf_flatten
 * DESCRIPTION
 *  
 * PARAMETERS
 *
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_img_buf_flatten(
    gdi_img_buf_struct *dst_buf, 
    gdi_color dst_color_key,
    const gdi_img_buf_struct *src_buf,
    S32 width,
    S32 height)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    GDI_DEBUG_ASSERT(dst_buf != NULL);

    /* Optimize for empty size */
    if (width <= 0 || height <= 0)
    {
        return;
    }

    if (dst_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_RGB565 &&
        src_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_RGB565)
    {
        gdi_img_buf_flatten_16_16(dst_buf, dst_color_key, src_buf, width, height);
    }
    else 
    {
        gdi_img_buf_flatten_general(dst_buf, dst_color_key, src_buf, width, height);
    }
}


/*****************************************************************************
 * FUNCTION
 *  gdi_img_buf_copy
 * DESCRIPTION
 *  
 * PARAMETERS
 *
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_img_buf_copy(
    gdi_img_buf_struct *dst_buf, 
    const gdi_img_buf_struct *src_buf,
    S32 width,
    S32 height)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    GDI_DEBUG_ASSERT(dst_buf != NULL);

    /* Optimize for empty size */
    if (width <= 0 || height <= 0)
    {
        return;
    }

    /* Optimize for same color format */
    if (dst_buf->color_format == src_buf->color_format)
    {
        gdi_img_buf_copy_same_format(dst_buf, src_buf, width, height);
        return;
    }
    
    gdi_img_buf_copy_general(dst_buf, src_buf, width, height);
}


/*****************************************************************************
 * FUNCTION
 *  gdi_img_buf_copy
 * DESCRIPTION
 *  
 * PARAMETERS
 *
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_img_buf_blending(
    gdi_img_buf_struct *dst_buf, 
    const gdi_img_buf_struct *src_buf,
    S32 width,
    S32 height)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    
    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    GDI_DEBUG_ASSERT(dst_buf != NULL);

    /* Optimize for empty size */
    if (width <= 0 || height <= 0)
    {
        return;
    }

    if (src_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_ARGB8888)
    {
        // not support
        GDI_DEBUG_ASSERT(0);
        return;
    }

    /* Optimize for non-alpha source */
    if (src_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_RGB565 ||
        src_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_RGB888)
    {
        /* Optimize for same color format */
        if (dst_buf->color_format == src_buf->color_format)
        {
            gdi_img_buf_copy_same_format(dst_buf, src_buf, width, height);
            return;
        }
        
        gdi_img_buf_copy_general(dst_buf, src_buf, width, height);
        return;
    }

    if (src_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_PARGB8888)
    {
        if (dst_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_RGB565)
        {
            gdi_img_buf_blending_16_PARGB8888(dst_buf, src_buf, width, height);
        }
        #if !defined(GDI_SLIM_COLOR_FORMAT)
        else if (dst_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_RGB888)
        {
            gdi_img_buf_blending_24_PARGB8888(dst_buf, src_buf, width, height);
        }
        #endif
        
        else if (dst_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_ARGB8888)
        {
            gdi_img_buf_convert_PARGB8888_from_ARGB8888(dst_buf, width, height);
            gdi_img_buf_blending_PARGB8888_PARGB8888(dst_buf, src_buf, width, height);
            gdi_img_buf_convert_ARGB8888_from_PARGB8888(dst_buf, width, height);
        }
        else if (dst_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_PARGB8888)
        {
            gdi_img_buf_blending_PARGB8888_PARGB8888(dst_buf, src_buf, width, height);
        }
    #ifdef GDI_LAYER_PARGB6666_SUPPORT
        else if (dst_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_PARGB6666)
        {
            gdi_img_buf_blending_PARGB8888_to_PARGB6666(dst_buf, src_buf, width, height);
        }
    #endif
        else
        {
            // not support
            GDI_DEBUG_ASSERT(0);
        }
        return;
    }
#ifdef GDI_LAYER_PARGB6666_SUPPORT
    else if (src_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_PARGB6666)
    {
        if (dst_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_PARGB6666)
        {
            gdi_img_buf_blending_PARGB6666_to_PARGB6666(dst_buf, src_buf, width, height);
        }
        else if (dst_buf->color_format == GDI_IMG_BUF_COLOR_FORMAT_RGB565)
        {
            gdi_img_buf_blending_PARGB6666_to_RGB565(dst_buf, src_buf, width, height);
        }
        else
        {
            GDI_DEBUG_ASSERT(0);
        }
        return;
    }
#endif
    
    GDI_DEBUG_ASSERT(0);
}


/*****************************************************************************
 * FUNCTION
 *  gdi_img_buf_blending_scale
 * DESCRIPTION
 *  
 * PARAMETERS
 *
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_img_buf_blending_scale(
    gdi_img_buf_struct *dst_buf, 
    S32 dst_width,
    S32 dst_height,
    const gdi_img_buf_struct *src_buf,
    S32 src_width,
    S32 src_height,
    S32 offset_x,
    S32 offset_y)
{
}

/*****************************************************************************
 * FUNCTION
 *  gdi_img_buf_prepare_img_buf_struct
 * DESCRIPTION
 *  given the buffer info and dest_rect and buffer clip,
 *  return a gdi_img_buf_struct and gdi_rect_struct.
 *  If one of clip value is negative, return gdi_img_buf_struct and gdi_rect_struct without clipping.
 *  If return GDI_FAILED, means dest_rect and clip area has no intersection.
 * PARAMETERS
 *  output_img_buf_struct   [OUT]      gdi_img_buf_struct
 *  dest_rect               [IN/OUT]   destination area
 *  buf_ptr                 [IN]       buffer pointer
 *  buf_width               [IN]       buffer width
 *  buf_bpp                 [IN]       buffer byte per pixel
 *  buf_cf                  [IN]       buffer color format (ex, GDI_COLOR_FORMAT_16)
 *  clip_x1                 [IN]       clip area
 * RETURNS
 *  GDI_RESULT, return GDI_SUCCEED if succeed
 *  If return GDI_FAILED, means dest_rect and clip area has no intersection.
 *****************************************************************************/
GDI_RESULT gdi_img_buf_prepare_img_buf_struct(
                    gdi_img_buf_struct *output_img_buf_struct,
                    gdi_rect_struct *dest_rect,
                    U8 *buf_ptr,
                    S32 buf_width,
                    S32 buf_bpp,
                    U32 buf_cf,
                    S32 clip_x1,
                    S32 clip_y1,
                    S32 clip_x2,
                    S32 clip_y2)
{
    gdi_result ret = GDI_SUCCEED;

    if (output_img_buf_struct == NULL)
    {
        return GDI_SUCCEED;
    }

    do
    {
        if ((clip_x1 >= 0) && (clip_y1 >= 0) && (clip_x2 >= 0) && (clip_y2 >= 0))
        {
            ret = GDI_FAILED;
            GDI_CLIP_TWO_RECT(
                dest_rect->x1, 
                dest_rect->y1, 
                dest_rect->x2, 
                dest_rect->y2,
                clip_x1,
                clip_y1,
                clip_x2,
                clip_y2,
                break);
            ret = GDI_SUCCEED;
        }

        output_img_buf_struct->pitch_bytes = buf_width * buf_bpp;
        output_img_buf_struct->color_format = gdi_img_buf_trans_color_format(buf_cf);
        output_img_buf_struct->ptr = buf_ptr;
        output_img_buf_struct->ptr += ((dest_rect->y1 * buf_width) + dest_rect->x1) * buf_bpp;
        
    } while(0);

    return ret;
}


/*****************************************************************************
 * FUNCTION
 *  gdi_cf_get_color
 * DESCRIPTION
 *  
 * PARAMETERS
 *  cf      [IN] Color format
 *  a       [IN] Alpha (0 ~ 255)
 *  r       [IN] Red (0 ~ 255)
 *  g       [IN] Green (0 ~ 255)
 *  b       [IN] Blue (0 ~ 255)
 * RETURNS
 *  Color value in given color format
 *****************************************************************************/
gdi_color gdi_cf_get_color(
            gdi_color_format cf,
            kal_uint32 a,
            kal_uint32 r,
            kal_uint32 g,
            kal_uint32 b)
{
    return gd_color_from_rgb[cf](a, r, g, b);
}


/*****************************************************************************
 * FUNCTION
 *  gdi_cf_separate_color
 * DESCRIPTION
 *  
 * PARAMETERS
 *  cf      [IN] Color format
 *  color   [IN] Color value in given color format
 *  a       [OUT] Alpha separated from color, ranges 0 ~ 255
 *  r       [OUT] Red component separated from color, ranges 0 ~ 255
 *  g       [OUT] Green component separated from color, ranges 0 ~ 255
 *  b       [OUT] Blue component separated from color, ranges 0 ~ 255
 * RETURNS
 *  void
 *****************************************************************************/
void gdi_cf_separate_color(
        gdi_color_format cf,
        gdi_color color,
        kal_uint32 *a,
        kal_uint32 *r,
        kal_uint32 *g,
        kal_uint32 *b)
{
    gd_color_to_rgb[cf](a, r, g, b, color);
}