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

#include <math.h>
#include <string.h>
#include <stdarg.h>

#include "Imlib2.h"
#include "blend.h"
#include "colormod.h"
#include "color_helpers.h"
#include "common.h"
#include "dynamic_filters.h"
#include "file.h"
#include "filter.h"
#include "font.h"
#include "grad.h"
#include "image.h"
#include "rgbadraw.h"
#include "rotate.h"
#include "scale.h"
#include "script.h"
#include "span.h"
#include "updates.h"
#ifdef BUILD_X11
#include "x11_color.h"
#include "x11_draw.h"
#include "x11_grab.h"
#include "x11_pixmap.h"
#include "x11_rend.h"
#include "x11_ximage.h"
#endif

/* convenience macros */
#define   CAST_IMAGE(im, image) (im) = (ImlibImage *)(image)
#define   CHECK_PARAM_POINTER_RETURN(sparam, param, ret) \
if (!(param)) \
{ \
  fprintf(stderr, "***** Imlib2 Developer Warning ***** :\n" \
                  "\tThis program is calling the Imlib call:\n\n" \
                  "\t%s();\n\n" \
                  "\tWith the parameter:\n\n" \
                  "\t%s\n\n" \
                  "\tbeing NULL. Please fix your program.\n", __func__, sparam); \
  return ret; \
}

#define   CHECK_PARAM_POINTER(sparam, param) \
if (!(param)) \
{ \
  fprintf(stderr, "***** Imlib2 Developer Warning ***** :\n" \
                  "\tThis program is calling the Imlib call:\n\n" \
                  "\t%s();\n\n" \
                  "\tWith the parameter:\n\n" \
                  "\t%s\n\n" \
                  "\tbeing NULL. Please fix your program.\n", __func__, sparam); \
  return; \
}

#define ILA0(ctx, imm, noc) \
   .pfunc = (ImlibProgressFunction)(ctx)->progress_func, \
   .pgran = (ctx)->progress_granularity, \
   .immed = imm, .nocache = noc

/* internal typedefs for function pointers */
typedef void        (*Imlib_Internal_Progress_Function)(void *, char, int, int,
                                                        int, int);
typedef void        (*Imlib_Internal_Data_Destructor_Function)(void *, void *);

typedef struct {
#ifdef BUILD_X11
   Display            *display;
   Visual             *visual;
   Colormap            colormap;
   int                 depth;
   Drawable            drawable;
   Pixmap              mask;
#endif
   char                anti_alias;
   char                dither;
   char                blend;
   Imlib_Color_Modifier color_modifier;
   ImlibOp             operation;
   Imlib_Font          font;
   Imlib_Text_Direction direction;
   double              angle;
   Imlib_Color         color;
   DATA32              pixel;
   Imlib_Color_Range   color_range;
   Imlib_Image         image;
   Imlib_Image_Data_Memory_Function image_data_memory_func;
   Imlib_Progress_Function progress_func;
   char                progress_granularity;
   char                dither_mask;
   int                 mask_alpha_threshold;
   Imlib_Filter        filter;
   Imlib_Rectangle     cliprect;
   Imlib_TTF_Encoding  encoding;

   int                 references;
   char                dirty;
} ImlibContext;

typedef struct _ImlibContextItem {
   ImlibContext       *context;
   struct _ImlibContextItem *below;
} ImlibContextItem;

#define DefaultContext { \
   .anti_alias = 1,				\
   .dither = 0,					\
   .blend = 1,					\
   .operation = (ImlibOp) IMLIB_OP_COPY,	\
   .direction = IMLIB_TEXT_TO_RIGHT,		\
   .angle = 0.0,				\
   .color.alpha = 255,				\
   .color.red = 255,				\
   .color.green = 255,				\
   .color.blue = 255,				\
   .pixel = 0xffffffff,				\
   .mask_alpha_threshold = 128,			\
   .encoding = IMLIB_TTF_ENCODING_ISO_8859_1,	\
}

/* A default context, only used for initialization */
static const ImlibContext ctx_default = DefaultContext;

/* The initial context */
static ImlibContext ctx0 = DefaultContext;

/* Current context */
static ImlibContext *ctx = &ctx0;

/* a stack of contexts -- only used by context-handling functions. */
static ImlibContextItem contexts0 = {.context = &ctx0 };
static ImlibContextItem *contexts = &contexts0;

/* frees the given context including all its members */
static void
__imlib_free_context(ImlibContext * context)
{
   ImlibContextItem   *next = contexts;

   if (ctx == context)
     {
        next = contexts->below;
        free(contexts);
        contexts = next;
     }

   ctx = context;

   if (ctx->image)
     {
        imlib_free_image();
        ctx->image = NULL;
     }
   if (ctx->font)
     {
        imlib_free_font();
        ctx->font = NULL;
     }
   if (ctx->color_modifier)
     {
        imlib_free_color_modifier();
        ctx->color_modifier = NULL;
     }
   if (ctx->filter)
     {
        imlib_free_filter();
        ctx->filter = NULL;
     }

   free(ctx);
   ctx = next->context;
}

EAPI                Imlib_Context
imlib_context_new(void)
{
   ImlibContext       *context = malloc(sizeof(ImlibContext));

   *context = ctx_default;

   return (Imlib_Context) context;
}

/* frees the given context if it doesn't have any reference anymore. The
   last (default) context can never be freed.
   If context is the current context, the context below will be made the
   current context.
*/
EAPI void
imlib_context_free(Imlib_Context context)
{
   ImlibContext       *c = (ImlibContext *) context;

   CHECK_PARAM_POINTER("context", context);
   if (c == ctx && !contexts->below)
      return;

   if (c->references == 0)
      __imlib_free_context(c);
   else
      c->dirty = 1;
}

EAPI void
imlib_context_push(Imlib_Context context)
{
   ImlibContextItem   *item;

   CHECK_PARAM_POINTER("context", context);
   ctx = (ImlibContext *) context;

   item = malloc(sizeof(ImlibContextItem));
   item->context = ctx;
   item->below = contexts;
   contexts = item;

   ctx->references++;
}

EAPI void
imlib_context_pop(void)
{
   ImlibContextItem   *item = contexts;
   ImlibContext       *current_ctx = item->context;

   if (!item->below)
      return;

   contexts = item->below;
   ctx = contexts->context;
   current_ctx->references--;
   if (current_ctx->dirty && current_ctx->references <= 0)
      __imlib_free_context(current_ctx);

   free(item);
}

EAPI                Imlib_Context
imlib_context_get(void)
{
   return (Imlib_Context) ctx;
}

/* context setting/getting functions */

/**
 * @param x The top left x coordinate of the rectangle.
 * @param y The top left y coordinate of the rectangle.
 * @param w The width of the rectangle.
 * @param h The height of the rectangle.
 *
 * Sets the rectangle of the current context.
 **/
EAPI void
imlib_context_set_cliprect(int x, int y, int w, int h)
{
   ctx->cliprect.x = x;
   ctx->cliprect.y = y;
   ctx->cliprect.w = w;
   ctx->cliprect.h = h;
}

EAPI void
imlib_context_get_cliprect(int *x, int *y, int *w, int *h)
{
   *x = ctx->cliprect.x;
   *y = ctx->cliprect.y;
   *w = ctx->cliprect.w;
   *h = ctx->cliprect.h;
}

#ifdef BUILD_X11
/**
 * @param display Current display to be used.
 *
 * Sets the current X display to be used for rendering of images to
 * drawables. You do not need to set this if you do not intend to
 * render an image to an X drawable. If you do you will need to set
 * this. If you change displays just set this to the new display
 * pointer. Do not use a Display pointer if you have closed that
 * display already - also note that if you close a display connection
 * and continue to render using Imlib2 without setting the display
 * pointer to NULL or something new, crashes may occur.
 */
EAPI void
imlib_context_set_display(Display * display)
{
   ctx->display = display;
}

/**
 * @return The current display.
 *
 * Returns the current display used for Imlib2's display context.
 */
EAPI Display       *
imlib_context_get_display(void)
{
   return ctx->display;
}

/**
 * Tell Imlib2 that the current display connection has been closed.
 *
 * Call when (and only when) you close a display connection but continue
 * using Imlib2 on a different connection.
 */
EAPI void
imlib_context_disconnect_display(void)
{
   if (!ctx->display)
      return;
   __imlib_RenderDisconnect(ctx->display);
   ctx->display = NULL;
}

/**
 * @param visual Current visual to use.
 *
 * Sets the current visual to use when rendering images to
 * drawables or producing pixmaps. You need to set this for anything to
 * render to a drawable or produce any pixmaps (this can be the default
 * visual).
 */
EAPI void
imlib_context_set_visual(Visual * visual)
{
   ctx->visual = visual;
   ctx->depth = imlib_get_visual_depth(ctx->display, ctx->visual);
}

/**
 * @return The current visual.
 *
 * Returns the current visual used for Imlib2's context.
 */
EAPI Visual        *
imlib_context_get_visual(void)
{
   return ctx->visual;
}

/**
 * @param colormap Colormap to use.
 *
 * Sets the colormap to use when rendering to drawables and allocating
 * colors. You must set this to the colormap you are using to render any
 * images or produce any pixmaps (this can be the default colormap).
 */
EAPI void
imlib_context_set_colormap(Colormap colormap)
{
   ctx->colormap = colormap;
}

/**
 * @return The current colormap.
 *
 * Returns the current Colormap used for Imlib2's context.
 */
EAPI                Colormap
imlib_context_get_colormap(void)
{
   return ctx->colormap;
}

/**
 * @param drawable An X drawable.
 *
 * Sets the X drawable to which images will be rendered when you call
 * a render call in Imlib2. This may be either a pixmap or a
 * window. You must set this to render anything.
 */
EAPI void
imlib_context_set_drawable(Drawable drawable)
{
   ctx->drawable = drawable;
}

/**
 * @return The current drawable.
 *
 * Returns the current Drawable used for Imlib2's context.
 */
EAPI                Drawable
imlib_context_get_drawable(void)
{
   return ctx->drawable;
}

/**
 * @param mask An 1-bit deep pixmap.
 *
 * Sets the 1-bit deep pixmap to be drawn to when rendering to generate
 * a mask pixmap. This is only useful if the image you are rendering
 * has alpha. Set this to 0 to not render a pixmap mask.
 */
EAPI void
imlib_context_set_mask(Pixmap mask)
{
   ctx->mask = mask;
}

/**
 * @return The current pixmap.
 *
 * Returns the current pixmap destination to be used to render a mask into.
 */
EAPI                Pixmap
imlib_context_get_mask(void)
{
   return ctx->mask;
}

/**
 * @return The current number of cached XImages.
 */
EAPI int
imlib_get_ximage_cache_count_used(void)
{
   return __imlib_GetXImageCacheCountUsed(ctx->display);
}

/**
 * @return The current XImage cache max count.
 */
EAPI int
imlib_get_ximage_cache_count_max(void)
{
   return __imlib_GetXImageCacheCountMax(ctx->display);
}

/**
 * @param count XImage cache max count.
 *
 * Sets the maximum number of XImages to cache.
 * Setting the cache size to 0 effectively flushes the cache and keeps
 * the cached XImage count at 0 until set to another value.
 * Whenever you set the max count Imlib2 will flush as many old XImages
 * from the cache as possible until the current cached XImage count is
 * less than or equal to the cache max count.
 */
EAPI void
imlib_set_ximage_cache_count_max(int count)
{
   __imlib_SetXImageCacheCountMax(ctx->display, count);
}

/**
 * @return The current XImage cache memory usage.
 */
EAPI int
imlib_get_ximage_cache_size_used(void)
{
   return __imlib_GetXImageCacheSizeUsed(ctx->display);
}

/**
 * @return The current XImage cache max size.
 */
EAPI int
imlib_get_ximage_cache_size_max(void)
{
   return __imlib_GetXImageCacheSizeMax(ctx->display);
}

/**
 * @param bytes XImage cache max size.
 *
 * Sets the XImage cache maximum size. The size is in bytes.
 * Setting the cache size to 0 effectively flushes the cache and keeps
 * the cache size at 0 until set to another value.
 * Whenever you set the max size Imlib2 will flush as many old XImages
 * from the cache as possible until the current XImage cache usage is
 * less than or equal to the cache max size.
 */
EAPI void
imlib_set_ximage_cache_size_max(int bytes)
{
   __imlib_SetXImageCacheSizeMax(ctx->display, bytes);
}
#endif

/**
 * @param dither_mask The dither mask flag.
 *
 * Selects if, you are rendering to a mask, or producing pixmap masks
 * from images, if the mask is to be dithered or not. passing in 1 for
 * dither_mask means the mask pixmap will be dithered, 0 means it will
 * not be dithered.
 */
EAPI void
imlib_context_set_dither_mask(char dither_mask)
{
   ctx->dither_mask = dither_mask;
}

/**
 * @return The current dither mask flag.
 *
 * Returns the current mode for dithering pixmap masks. 1 means
 * dithering is enabled and 0 means it is not.
 */
EAPI char
imlib_context_get_dither_mask(void)
{
   return ctx->dither_mask;
}

/**
 * @param mask_alpha_threshold The mask alpha threshold.
 *
 * Selects, if you are rendering to a mask, the alpha threshold above which
 * mask bits are set. The default mask alpha threshold is 128, meaning that
 * a mask bit will be set if the pixel alpha is >= 128.
 */
EAPI void
imlib_context_set_mask_alpha_threshold(int mask_alpha_threshold)
{
   ctx->mask_alpha_threshold = mask_alpha_threshold;
}

/**
 * @return The current mask mask alpha threshold.
 *
 * Returns the current mask alpha threshold.
 */
EAPI int
imlib_context_get_mask_alpha_threshold(void)
{
   return ctx->mask_alpha_threshold;
}

/**
 * @param anti_alias The anti alias flag.
 *
 * Toggles "anti-aliased" scaling of images. This
 * isn't quite correct since it's actually super and sub pixel
 * sampling that it turns on and off, but anti-aliasing is used for
 * having "smooth" edges to lines and shapes and this means when
 * images are scaled they will keep their smooth appearance. Passing
 * in 1 turns this on and 0 turns it off.
 */
EAPI void
imlib_context_set_anti_alias(char anti_alias)
{
   ctx->anti_alias = anti_alias;
}

/**
 * @return The current anti alias flag.
 *
 * Returns if Imlib2 currently will smoothly scale images. 1 means it
 * will and 0 means it will not.
 */
EAPI char
imlib_context_get_anti_alias(void)
{
   return ctx->anti_alias;
}

/**
 * @param dither The dithering flag.
 *
 * Sets the dithering flag for rendering to a drawable or when pixmaps
 * are produced. This affects the color image appearance by enabling
 * dithering. Dithering slows down rendering but produces considerably
 * better results. this option has no effect foe rendering in 24 bit
 * and up, but in 16 bit and lower it will dither, producing smooth
 * gradients and much better quality images. setting dither to 1
 * enables it and 0 disables it.
 */
EAPI void
imlib_context_set_dither(char dither)
{
   ctx->dither = dither;
}

/**
 * @return The current dithering flag.
 *
 * Returns if image data is rendered with dithering currently. 1 means
 * yes and 0 means no.
 */
EAPI char
imlib_context_get_dither(void)
{
   return ctx->dither;
}

/**
 * @param blend The blending flag.
 *
 * When rendering an image to a drawable, Imlib2 is able to blend the
 * image directly onto the drawable during rendering. Setting this to 1
 * will enable this. If the image has no alpha channel this has no
 * effect. Setting it to 0 will disable this.
 */
EAPI void
imlib_context_set_blend(char blend)
{
   ctx->blend = blend;
}

/**
 * @return The current blending flag.
 *
 * Returns if Imlib2 will blend images onto a drawable whilst
 * rendering to that drawable. 1 means yes and 0 means no.
 */
EAPI char
imlib_context_get_blend(void)
{
   return ctx->blend;
}

/**
 * @param color_modifier Current color modifier.
 *
 * Sets the current color modifier used for rendering pixmaps or
 * images to a drawable or images onto other images. Color modifiers
 * are lookup tables that map the values in the red, green, blue and
 * alpha channels to other values in the same channel when rendering,
 * allowing for fades, color correction etc. to be done whilst
 * rendering. pass in NULL as the color_modifier to disable the color
 * modifier for rendering.
 */
EAPI void
imlib_context_set_color_modifier(Imlib_Color_Modifier color_modifier)
{
   ctx->color_modifier = color_modifier;
}

/**
 * @return The current color modifier.
 *
 * Returns the current color modifier being used.
 */
EAPI                Imlib_Color_Modifier
imlib_context_get_color_modifier(void)
{
   return ctx->color_modifier;
}

/**
 * @param operation
 *
 * When Imlib2 draws an image onto another or an image onto a drawable
 * it is able to do more than just blend the result on using the given
 * alpha channel of the image. It is also able to do saturating
 * additive, subtractive and a combination of the both (called reshade)
 * rendering. The default mode is IMLIB_OP_COPY. you can also set it to
 * IMLIB_OP_ADD, IMLIB_OP_SUBTRACT or IMLIB_OP_RESHADE. Use this
 * function to set the rendering operation. IMLIB_OP_COPY performs
 * basic alpha blending: DST = (SRC * A) + (DST * (1 -
 * A)). IMLIB_OP_ADD does DST = DST + (SRC * A). IMLIB_OP_SUBTRACT does
 * DST = DST - (SRC * A) and IMLIB_OP_RESHADE does DST = DST + (((SRC -
 * 0.5) / 2) * A).
 */
EAPI void
imlib_context_set_operation(Imlib_Operation operation)
{
   ctx->operation = (ImlibOp) operation;
}

/**
 * @return The current operation mode.
 *
 * Returns the current operation mode.
 */
EAPI                Imlib_Operation
imlib_context_get_operation(void)
{
   return (Imlib_Operation) ctx->operation;
}

/**
 * @param font Current font.
 *
 * Sets the current font to use when rendering text. you should load
 * the font first with imlib_load_font().
 */
EAPI void
imlib_context_set_font(Imlib_Font font)
{
   ctx->font = font;
}

/**
 * @return The current font.
 *
 * Returns the current font.
 */
EAPI                Imlib_Font
imlib_context_get_font(void)
{
   return ctx->font;
}

/**
 * @param direction Text direction.
 *
 * Sets the direction in which to draw text in terms of simple 90
 * degree orientations or an arbitrary angle. The direction can be one
 * of IMLIB_TEXT_TO_RIGHT, IMLIB_TEXT_TO_LEFT, IMLIB_TEXT_TO_DOWN,
 * IMLIB_TEXT_TO_UP or IMLIB_TEXT_TO_ANGLE. The default is
 * IMLIB_TEXT_TO_RIGHT. If you use IMLIB_TEXT_TO_ANGLE, you will also
 * have to set the angle with imlib_context_set_angle().
 */
EAPI void
imlib_context_set_direction(Imlib_Text_Direction direction)
{
   ctx->direction = direction;
}

/**
 * @param angle Angle of the text strings.
 *
 * Sets the angle at which text strings will be drawn if the text
 * direction has been set to IMLIB_TEXT_TO_ANGLE with
 * imlib_context_set_direction().
 */
EAPI void
imlib_context_set_angle(double angle)
{
   ctx->angle = angle;
}

/**
 * @return The current angle of the text strings.
 *
 * Returns the current angle used to render text at if the direction
 * is IMLIB_TEXT_TO_ANGLE.
 */
EAPI double
imlib_context_get_angle(void)
{
   return ctx->angle;
}

/**
 * @return The current direction of the text.
 *
 * Returns the current direction to render text in.
 */
EAPI                Imlib_Text_Direction
imlib_context_get_direction(void)
{
   return ctx->direction;
}

/**
 * @param red Red channel of the current color.
 * @param green Green channel of the current color.
 * @param blue Blue channel of the current color.
 * @param alpha Alpha channel of the current color.
 *
 * Sets the color with which text, lines and rectangles are drawn when
 * being rendered onto an image. Values for @p red, @p green, @p blue
 * and @p alpha are between 0 and 255 - any other values have
 * undefined results.
 */
EAPI void
imlib_context_set_color(int red, int green, int blue, int alpha)
{
   DATA8               r, g, b, a;

   r = red;
   g = green;
   b = blue;
   a = alpha;

   ctx->color.red = r;
   ctx->color.green = g;
   ctx->color.blue = b;
   ctx->color.alpha = a;

   ctx->pixel = PIXEL_ARGB(a, r, g, b);
}

/**
 * @param red Red channel of the current color.
 * @param green Green channel of the current color.
 * @param blue Blue channel of the current color.
 * @param alpha Alpha channel of the current color.
 *
 * Returns the current color for rendering text, rectangles and lines.
 */
EAPI void
imlib_context_get_color(int *red, int *green, int *blue, int *alpha)
{
   *red = ctx->color.red;
   *green = ctx->color.green;
   *blue = ctx->color.blue;
   *alpha = ctx->color.alpha;
}

/**
 * @return The current color.
 *
 * Returns the current color as a color struct. Do NOT free this
 * pointer.
 */
EAPI Imlib_Color   *
imlib_context_get_imlib_color(void)
{
   return &ctx->color;
}

/**
 * @param hue Hue channel of the current color.
 * @param saturation Saturation channel of the current color.
 * @param value Value channel of the current color.
 * @param alpha Alpha channel of the current color.
 *
 * Sets the color in HSVA space. Values for @p hue are between 0 and 360,
 * values for @p saturation and @p value between 0 and 1, and values for
 * @p alpha are between 0 and 255 - any other values have undefined
 * results.
 */
EAPI void
imlib_context_set_color_hsva(float hue, float saturation, float value,
                             int alpha)
{
   int                 r, g, b;

   __imlib_hsv_to_rgb(hue, saturation, value, &r, &g, &b);
   imlib_context_set_color(r, g, b, alpha);
}

/**
 * @param hue Hue channel of the current color.
 * @param saturation Saturation channel of the current color.
 * @param value Value channel of the current color.
 * @param alpha Alpha channel of the current color.
 *
 * Returns the current color for rendering text, rectangles and lines
 * in HSVA space.
 */
EAPI void
imlib_context_get_color_hsva(float *hue, float *saturation, float *value,
                             int *alpha)
{
   int                 r, g, b;

   imlib_context_get_color(&r, &g, &b, alpha);
   __imlib_rgb_to_hsv(r, g, b, hue, saturation, value);
}

/**
 * @param hue Hue channel of the current color.
 * @param lightness Lightness channel of the current color.
 * @param saturation Saturation channel of the current color.
 * @param alpha Alpha channel of the current color.
 *
 * Sets the color in HLSA space. Values for @p hue are between 0 and 360,
 * values for @p lightness and @p saturation between 0 and 1, and values for
 * @p alpha are between 0 and 255 - any other values have undefined
 * results.
 */
EAPI void
imlib_context_set_color_hlsa(float hue, float lightness, float saturation,
                             int alpha)
{
   int                 r, g, b;

   __imlib_hls_to_rgb(hue, lightness, saturation, &r, &g, &b);
   imlib_context_set_color(r, g, b, alpha);
}

/**
 * @param hue Hue channel of the current color.
 * @param lightness Lightness channel of the current color.
 * @param saturation Saturation channel of the current color.
 * @param alpha Alpha channel of the current color.
 *
 * Returns the current color for rendering text, rectangles and lines
 * in HLSA space.
 */
EAPI void
imlib_context_get_color_hlsa(float *hue, float *lightness, float *saturation,
                             int *alpha)
{
   int                 r, g, b;

   imlib_context_get_color(&r, &g, &b, alpha);
   __imlib_rgb_to_hls(r, g, b, hue, lightness, saturation);
}

/**
 * @param cyan Cyan channel of the current color.
 * @param magenta Magenta channel of the current color.
 * @param yellow Yellow channel of the current color.
 * @param alpha Alpha channel of the current color.
 *
 * Sets the color in CMYA space. Values for @p cyan, @p magenta, @p yellow and
 * @p alpha are between 0 and 255 - any other values have undefined
 * results.
 */
EAPI void
imlib_context_set_color_cmya(int cyan, int magenta, int yellow, int alpha)
{
   DATA8               r, g, b, a;

   r = 255 - cyan;
   g = 255 - magenta;
   b = 255 - yellow;
   a = alpha;

   ctx->color.red = r;
   ctx->color.green = g;
   ctx->color.blue = b;
   ctx->color.alpha = a;

   ctx->pixel = PIXEL_ARGB(a, r, g, b);
}

/**
 * @param cyan Cyan channel of the current color.
 * @param magenta Magenta channel of the current color.
 * @param yellow Yellow channel of the current color.
 * @param alpha Alpha channel of the current color.
 *
 * Returns the current color for rendering text, rectangles and lines
 * in CMYA space.
 */
EAPI void
imlib_context_get_color_cmya(int *cyan, int *magenta, int *yellow, int *alpha)
{
   *cyan = 255 - ctx->color.red;
   *magenta = 255 - ctx->color.green;
   *yellow = 255 - ctx->color.blue;
   *alpha = ctx->color.alpha;
}

/**
 * @param color_range Color range.
 *
 * Sets the current color range to use for rendering gradients.
 */
EAPI void
imlib_context_set_color_range(Imlib_Color_Range color_range)
{
   ctx->color_range = color_range;
}

/**
 * @return The current color range.
 *
 * Returns the current color range being used for gradients.
 */
EAPI                Imlib_Color_Range
imlib_context_get_color_range(void)
{
   return ctx->color_range;
}

/**
 * @param memory_function An image data memory management function.
 *
 * Sets the image data memory management function.
 */
EAPI void
imlib_context_set_image_data_memory_function(Imlib_Image_Data_Memory_Function
                                             memory_function)
{
   ctx->image_data_memory_func = memory_function;
}

/**
 * @param progress_function A progress function.
 *
 * Sets the progress function to be called back whilst loading
 * images. Set this to the function to be called, or set it to NULL to
 * disable progress callbacks whilst loading.
 */
EAPI void
imlib_context_set_progress_function(Imlib_Progress_Function progress_function)
{
   ctx->progress_func = progress_function;
}

/**
 * @return The image data memory management function.
 *
 * Returns the current image data memeory management function being used.
 */
EAPI                Imlib_Image_Data_Memory_Function
imlib_context_get_image_data_memory_function(void)
{
   return ctx->image_data_memory_func;
}

/**
 * @return The current progress function.
 *
 * Returns the current progress function being used.
 */
EAPI                Imlib_Progress_Function
imlib_context_get_progress_function(void)
{
   return ctx->progress_func;
}

/**
 * @param progress_granularity A char.
 *
 * This hints as to how often to call the progress callback. 0 means
 * as often as possible. 1 means whenever 15 more of the image has been
 * decoded, 10 means every 10% of the image decoding, 50 means every
 * 50% and 100 means only call at the end. Values outside of the range
 * 0-100 are undefined.
 */
EAPI void
imlib_context_set_progress_granularity(char progress_granularity)
{
   ctx->progress_granularity = progress_granularity;
}

/**
 * @return The current progress granularity
 *
 * Returns the current progress granularity being used.
 */
EAPI char
imlib_context_get_progress_granularity(void)
{
   return ctx->progress_granularity;
}

/**
 * @param image Current image.
 *
 * Sets the current image Imlib2 will be using with its function calls.
 */
EAPI void
imlib_context_set_image(Imlib_Image image)
{
   ctx->image = image;
}

/**
 * @return The current image.
 *
 * Returns the current context image.
 */
EAPI                Imlib_Image
imlib_context_get_image(void)
{
   return ctx->image;
}

EAPI void
imlib_context_set_TTF_encoding(Imlib_TTF_Encoding encoding)
{
   ctx->encoding = encoding;
}

EAPI                Imlib_TTF_Encoding
imlib_context_get_TTF_encoding(void)
{
   return ctx->encoding;
}

/* imlib api */

/**
 * @return The current image cache memory usage.
 *
 * Returns the current size of the image cache in bytes.
 * The cache is a unified cache used for image data AND pixmaps.
 */
EAPI int
imlib_get_cache_used(void)
{
   return __imlib_CurrentCacheSize();
}

/**
 * @return The current image cache max size.
 *
 * Returns the current maximum size of the image cache in bytes.
 * The cache is a unified cache used for image data AND pixmaps.
 */
EAPI int
imlib_get_cache_size(void)
{
   return __imlib_GetCacheSize();
}

/**
 * @param bytes Image cache max size.
 *
 * Sets the cache size. The size is in bytes. Setting the cache size to
 * 0 effectively flushes the cache and keeps the cache size at 0 until
 * set to another value. Whenever you set the cache size Imlib2 will
 * flush as many old images and pixmap from the cache as needed until
 * the current cache usage is less than or equal to the cache size.
 */
EAPI void
imlib_set_cache_size(int bytes)
{
   __imlib_SetCacheSize(bytes);
}

/**
 * @return The current number of colors.
 *
 * Gets the number of colors Imlib2 currently at a maximum is allowed
 * to allocate for rendering. The default is 256.
 */
EAPI int
imlib_get_color_usage(void)
{
#ifdef BUILD_X11
   return (int)_max_colors;
#else
   return 256;
#endif
}

/**
 * @param max Maximum number of colors.
 *
 * Sets the maximum number of colors you would like Imlib2 to allocate
 * for you when rendering. The default is 256. This has no effect in
 * depths greater than 8 bit.
 */
EAPI void
imlib_set_color_usage(int max)
{
#ifdef BUILD_X11
   if (max < 2)
      max = 2;
   else if (max > 256)
      max = 256;
   _max_colors = max;
#endif
}

/**
 * If you want Imlib2 to forcibly flush any cached loaders it has and
 * re-load them from disk (this is useful if the program just
 * installed a new loader and does not want to wait till Imlib2 deems
 * it an optimal time to rescan the loaders)
 */
EAPI void
imlib_flush_loaders(void)
{
   __imlib_RemoveAllLoaders();
}

#ifdef BUILD_X11
/**
 * @param display The current display
 * @param visual The current visual
 * @return
 *
 * Convenience function that returns the depth of a visual for that
 * display.
 */
EAPI int
imlib_get_visual_depth(Display * display, Visual * visual)
{
   CHECK_PARAM_POINTER_RETURN("display", display, 0);
   CHECK_PARAM_POINTER_RETURN("visual", visual, 0);
   return __imlib_XActualDepth(display, visual);
}

/**
 * @param display The current display
 * @param screen The screen
 * @param depth_return The depth of the returned visual.
 * @return The best visual.
 *
 * Returns the visual for the display @p display and the screen @p
 * screen that Imlib2 thinks
 * will give you the best quality output. @p depth_return should point to
 * an int that will be filled with the depth of that visual too.
 */
EAPI Visual        *
imlib_get_best_visual(Display * display, int screen, int *depth_return)
{
   CHECK_PARAM_POINTER_RETURN("display", display, NULL);
   CHECK_PARAM_POINTER_RETURN("depth_return", depth_return, NULL);
   return __imlib_BestVisual(display, screen, depth_return);
}
#endif

/**
 * @param file Image file.
 * @return An image handle.
 *
 * Loads an image from disk located at the path specified by
 * @p file. Please see the section \ref loading for more
 * detail. Returns an image handle on success or NULL on failure.
 */
EAPI                Imlib_Image
imlib_load_image(const char *file)
{
   Imlib_Image         im;
   ImlibLoadArgs       ila = { ILA0(ctx, 0, 0) };

   CHECK_PARAM_POINTER_RETURN("file", file, NULL);

   im = __imlib_LoadImage(file, &ila);

   return (Imlib_Image) im;
}

/**
 * @param file Image file.
 * @return An image handle.
 *
 * Loads an image from disk located at the path specified by
 * @p file. This forces the image data to be decoded at load time too,
 * instead of decoding being deferred until it is needed. Returns an
 * image handle on success or NULL on failure.
 */
EAPI                Imlib_Image
imlib_load_image_immediately(const char *file)
{
   Imlib_Image         im;
   ImlibLoadArgs       ila = { ILA0(ctx, 1, 0) };

   CHECK_PARAM_POINTER_RETURN("file", file, NULL);

   im = __imlib_LoadImage(file, &ila);

   return (Imlib_Image) im;
}

/**
 * @param file Image file.
 * @return An image handle.
 *
 * Loads the image without looking in the cache first. Returns an
 * image handle on success or NULL on failure.
 */
EAPI                Imlib_Image
imlib_load_image_without_cache(const char *file)
{
   Imlib_Image         im;
   ImlibLoadArgs       ila = { ILA0(ctx, 0, 1) };

   CHECK_PARAM_POINTER_RETURN("file", file, NULL);

   im = __imlib_LoadImage(file, &ila);

   return (Imlib_Image) im;
}

/**
 * @param file Image file.
 * @return An image handle.
 *
 * Loads the image without deferred image data decoding (i.e. it is
 * decoded straight away) and without looking in the cache. Returns an
 * image handle on success or NULL on failure.
 */
EAPI                Imlib_Image
imlib_load_image_immediately_without_cache(const char *file)
{
   Imlib_Image         im;
   ImlibLoadArgs       ila = { ILA0(ctx, 1, 1) };

   CHECK_PARAM_POINTER_RETURN("file", file, NULL);

   im = __imlib_LoadImage(file, &ila);

   return (Imlib_Image) im;
}

/**
 * @param fd Image file descriptor.
 * @param file Image file.
 * @return An image handle.
 *
 * Reaasd image from file descriptor.
 * The file name @file is only used to guess the file format.
 * The image is loaded without deferred image data decoding (i.e. it is
 * decoded straight away) and without looking in the cache.
 * @fd must be mmap'able (so it cannot be a pipe).
 * @fd will be closed after calling this function.
 * Returns an image handle on success or NULL on failure.
 */
EAPI                Imlib_Image
imlib_load_image_fd(int fd, const char *file)
{
   Imlib_Image         im;
   ImlibLoadArgs       ila = { ILA0(ctx, 1, 1) };

   CHECK_PARAM_POINTER_RETURN("file", file, NULL);

   ila.fp = fdopen(fd, "rb");
   if (ila.fp)
     {
        im = __imlib_LoadImage(file, &ila);
        fclose(ila.fp);
     }
   else
     {
        im = NULL;
        close(fd);
     }

   return (Imlib_Image) im;
}

/**
 * @param file Image file.
 * @param error_return The returned error.
 * @return An image handle.
 *
 * Loads an image at the path @p file on disk. If it succeeds it returns
 * a valid image handle, if not NULL is returned and @p error_return
 * is set to the detail of the error.
 */
EAPI                Imlib_Image
imlib_load_image_with_error_return(const char *file,
                                   Imlib_Load_Error * error_return)
{
   Imlib_Image         im;
   ImlibLoadArgs       ila = { ILA0(ctx, 1, 0) };

   CHECK_PARAM_POINTER_RETURN("file", file, NULL);

   im = __imlib_LoadImage(file, &ila);
   if (error_return)
      *error_return = (Imlib_Load_Error) ila.err;

   return im;
}

/**
 * @param file Image file.
 * @param frame Frame number.
 * @return An image handle.
 *
 * Loads the specified frame within the image.
 * On success an image handle is returned, otherwise NULL is returned
 * (e.g. if the requested frame does not exist).
 * The image is loaded immediately.
 */
EAPI                Imlib_Image
imlib_load_image_frame(const char *file, int frame)
{
   ImlibImage         *im;
   ImlibLoadArgs       ila = { ILA0(ctx, 1, 0),.frame = frame };

   CHECK_PARAM_POINTER_RETURN("file", file, NULL);

   im = __imlib_LoadImage(file, &ila);

   return (Imlib_Image) im;
}

EAPI void
imlib_image_get_frame_info(Imlib_Frame_Info * info)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);

   info->frame_count = im->frame_count;
   info->frame_num = im->frame_num;
   info->canvas_w = im->canvas_w ? im->canvas_w : im->w;
   info->canvas_h = im->canvas_h ? im->canvas_h : im->h;
   info->frame_x = im->frame_x;
   info->frame_y = im->frame_y;
   info->frame_w = im->w;
   info->frame_h = im->h;
   info->frame_flags = im->frame_flags;
   info->frame_delay = im->frame_delay ? im->frame_delay : 100;
}

/**
 * Frees the image that is set as the current image in Imlib2's context.
 */
EAPI void
imlib_free_image(void)
{
   CHECK_PARAM_POINTER("image", ctx->image);
   __imlib_FreeImage(ctx->image);
   ctx->image = NULL;
}

/**
 * Frees the current image in Imlib2's context AND removes it from the
 * cache.
 */
EAPI void
imlib_free_image_and_decache(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   SET_FLAG(im->flags, F_INVALID);
   __imlib_FreeImage(im);
   ctx->image = NULL;
}

/**
 * Returns the width in pixels of the current image in Imlib2's context.
 */
EAPI int
imlib_image_get_width(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, 0);
   CAST_IMAGE(im, ctx->image);
   return im->w;
}

/**
 * Returns the height in pixels of the current image in Imlib2's context.
 */
EAPI int
imlib_image_get_height(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, 0);
   CAST_IMAGE(im, ctx->image);
   return im->h;
}

/**
 * @return The current filename.
 *
 * Returns the filename for the file that is set as the current
 * context. The pointer returned is only valid as long as no operations
 * cause the filename of the image to change. Saving the file with a
 * different name would cause this. It is suggested you duplicate the
 * string if you wish to continue to use the string for later
 * processing. Do not free the string pointer returned by this
 * function.
 */
EAPI const char    *
imlib_image_get_filename(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, 0);
   CAST_IMAGE(im, ctx->image);
   /* strdup() the returned value if you want to alter it! */
   return (const char *)(im->file);
}

/**
 * @return A pointer to the image data.
 *
 * Returns a pointer to the image data in the image set as the image
 * for the current context. When you get this pointer it is assumed you
 * are planning on writing to the data, thus once you do this the image
 * can no longer be used for caching - in fact all images cached from
 * this one will also be affected when you put the data back. If this
 * matters it is suggested you clone the image first before playing
 * with the image data. The image data is returned in the format of a
 * DATA32 (32 bits) per pixel in a linear array ordered from the top
 * left of the image to the bottom right going from left to right each
 * line. Each pixel has the upper 8 bits as the alpha channel and the
 * lower 8 bits are the blue channel - so a pixel's bits are ARGB (from
 * most to least significant, 8 bits per channel). You must put the
 * data back at some point.
 */
EAPI DATA32        *
imlib_image_get_data(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, NULL);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return NULL;
   __imlib_DirtyImage(im);
   return im->data;
}

/**
 * @return A pointer to the image data.
 *
 * Functions the same way as imlib_image_get_data(), but returns a
 * pointer expecting the program to NOT write to the data returned (it
 * is for inspection purposes only). Writing to this data has undefined
 * results. The data does not need to be put back.
 */
EAPI DATA32        *
imlib_image_get_data_for_reading_only(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, NULL);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return NULL;
   return im->data;
}

/**
 * @param data The pointer to the image data.
 *
 * Puts back @p data when it was obtained by
 * imlib_image_get_data(). @p data must be the same pointer returned
 * by imlib_image_get_data(). This operated on the current context
 * image.
 */
EAPI void
imlib_image_put_back_data(DATA32 * data)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("data", data);
   CAST_IMAGE(im, ctx->image);
   __imlib_DirtyImage(im);
   data = NULL;
}

/**
 * @return Current alpha channel flag.
 *
 * Returns 1 if the current context image has an alpha channel, or 0
 * if it does not (the alpha data space is still there and available -
 * just "unused").
 */
EAPI char
imlib_image_has_alpha(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, 0);
   CAST_IMAGE(im, ctx->image);
   if (IMAGE_HAS_ALPHA(im))
      return 1;
   return 0;
}

/**
 * By default Imlib2 will not check the timestamp of an image on disk
 * and compare it with the image in its cache - this is to minimize
 * disk activity when using the cache. Call this function and it will
 * flag the current context image as being liable to change on disk
 * and Imlib2 will check the timestamp of the image file on disk and
 * compare it with the cached image when it next needs to use this
 * image in the cache.
 */
EAPI void
imlib_image_set_changes_on_disk(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   SET_FLAG(im->flags, F_ALWAYS_CHECK_DISK);
}

/**
 * @param border The border of the image.
 *
 * Fills the Imlib_Border structure to which @p border points to with the
 * values of the border of the current context image. The border is the
 * area at the edge of the image that does not scale with the rest of
 * the image when resized - the borders remain constant in size. This
 * is useful for scaling bevels at the edge of images differently to
 * the image center.
 */
EAPI void
imlib_image_get_border(Imlib_Border * border)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("border", border);
   CAST_IMAGE(im, ctx->image);
   border->left = im->border.left;
   border->right = im->border.right;
   border->top = im->border.top;
   border->bottom = im->border.bottom;
}

/**
 * @param border The border of the image.
 *
 * Sets the border of the current context image to the values contained
 * in the Imlib_Border structure @p border points to.
 */
EAPI void
imlib_image_set_border(Imlib_Border * border)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("border", border);
   CAST_IMAGE(im, ctx->image);
   if ((im->border.left == border->left)
       && (im->border.right == border->right)
       && (im->border.top == border->top)
       && (im->border.bottom == border->bottom))
      return;
   im->border.left = MAX(0, border->left);
   im->border.right = MAX(0, border->right);
   im->border.top = MAX(0, border->top);
   im->border.bottom = MAX(0, border->bottom);
#ifdef BUILD_X11
   __imlib_DirtyPixmapsForImage(im);
#endif
}

/**
 * @param format Format of the image.
 *
 * Sets the format of the current image. This is used for when you
 * wish to save an image in a different format that it was loaded in,
 * or if the image currently has no file format associated with it.
 */
EAPI void
imlib_image_set_format(const char *format)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("format", format);
   CAST_IMAGE(im, ctx->image);
   free(im->format);
   im->format = (format) ? strdup(format) : NULL;
   if (!(im->flags & F_FORMAT_IRRELEVANT))
     {
        __imlib_DirtyImage(im);
     }
}

/**
 * @param irrelevant Irrelevant format flag.
 *
 * Sets if the format value of the current image is irrelevant for
 * caching purposes - by default it is. pass irrelevant as 1 to make it
 * irrelevant and 0 to make it relevant for caching.
 */
EAPI void
imlib_image_set_irrelevant_format(char irrelevant)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   UPDATE_FLAG(im->flags, F_FORMAT_IRRELEVANT, irrelevant);
}

/**
 * @param irrelevant Irrelevant border flag.
 *
 * Sets if the border of the current image is irrelevant for caching
 * purposes. By default it is. Set irrelevant to 1 to make it
 * irrelevant, and 0 to make it relevant.
 */
EAPI void
imlib_image_set_irrelevant_border(char irrelevant)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   UPDATE_FLAG(im->flags, F_BORDER_IRRELEVANT, irrelevant);
}

/**
 * @param irrelevant Irrelevant alpha flag.
 *
 * Sets if the alpha channel status of the current image (i.e. if
 * there is or is not one) is important for caching purposes. By
 * default it is not. Set irrelevant to 1 to make it irrelevant and 0
 * to make it relevant.
 */
EAPI void
imlib_image_set_irrelevant_alpha(char irrelevant)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   UPDATE_FLAG(im->flags, F_ALPHA_IRRELEVANT, irrelevant);
}

/**
 * @return Current image format.
 *
 * Returns the current image's format. Do not free this
 * string. Duplicate it if you need it for later use.
 */
EAPI char          *
imlib_image_format(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, NULL);
   CAST_IMAGE(im, ctx->image);
   return im->format;
}

/**
 * @param has_alpha Alpha flag.
 *
 * Sets the alpha flag for the current image. Set @p has_alpha to 1 to
 * enable the alpha channel in the current image, or 0 to disable it.
 */
EAPI void
imlib_image_set_has_alpha(char has_alpha)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   UPDATE_FLAG(im->flags, F_HAS_ALPHA, has_alpha);
}

#ifdef BUILD_X11
/**
 * @param pixmap_return The returned pixmap.
 * @param mask_return The returned mask.
 *
 * Creates a pixmap of the current image (and a mask if the image has
 * an alpha value) and return the id's of the pixmap and mask to
 * @p pixmap_return and @p mask_return pixmap id's. You must free these
 * pixmaps using Imlib2's free function imlib_free_pixmap_and_mask().
 */
EAPI void
imlib_render_pixmaps_for_whole_image(Pixmap * pixmap_return,
                                     Pixmap * mask_return)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("pixmap_return", pixmap_return);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_CreatePixmapsForImage(ctx->display, ctx->drawable, ctx->visual,
                                 ctx->depth, ctx->colormap, im, pixmap_return,
                                 mask_return, 0, 0, im->w, im->h, im->w,
                                 im->h, 0, ctx->dither, ctx->dither_mask,
                                 ctx->mask_alpha_threshold,
                                 ctx->color_modifier);
}

/**
 * @param pixmap_return The returned pixmap.
 * @param mask_return The returned mask.
 * @param width Width of the pixmap.
 * @param height Height of the pixmap.
 *
 * Works just like imlib_render_pixmaps_for_whole_image(), but will
 * scale the output result to the width @p width and height @p height
 * specified. Scaling
 * is done before depth conversion so pixels used for dithering don't
 * grow large.
 */
EAPI void
imlib_render_pixmaps_for_whole_image_at_size(Pixmap * pixmap_return,
                                             Pixmap * mask_return, int width,
                                             int height)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("pixmap_return", pixmap_return);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_CreatePixmapsForImage(ctx->display, ctx->drawable, ctx->visual,
                                 ctx->depth, ctx->colormap, im, pixmap_return,
                                 mask_return, 0, 0, im->w, im->h, width,
                                 height, ctx->anti_alias, ctx->dither,
                                 ctx->dither_mask, ctx->mask_alpha_threshold,
                                 ctx->color_modifier);
}

/**
 * @param pixmap The pixmap.
 *
 * Frees @p pixmap (and any mask generated in association with that
 * pixmap). The pixmap will remain cached until the image the pixmap
 * was generated from is dirtied or decached, or the cache is flushed.
 */
EAPI void
imlib_free_pixmap_and_mask(Pixmap pixmap)
{
   __imlib_FreePixmap(ctx->display, pixmap);
}

/**
 * @param x X coordinate of the pixel.
 * @param y Y coordinate of the pixel.
 *
 * Renders the current image onto the current drawable at the (@p x, @p y)
 * pixel location specified without scaling.
 */
EAPI void
imlib_render_image_on_drawable(int x, int y)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_RenderImage(ctx->display, im, ctx->drawable, ctx->mask,
                       ctx->visual, ctx->colormap, ctx->depth, 0, 0, im->w,
                       im->h, x, y, im->w, im->h, 0, ctx->dither, ctx->blend,
                       ctx->dither_mask, ctx->mask_alpha_threshold,
                       ctx->color_modifier, ctx->operation);
}

/**
 * @param x X coordinate of the pixel.
 * @param y Y coordinate of the pixel.
 * @param width Width of the rendered image.
 * @param height Height of the rendered image.
 *
 * Renders the current image onto the current drawable at the (@p x, @p y)
 * location specified AND scale the image to the width @p width and height
 * @p height.
 */
EAPI void
imlib_render_image_on_drawable_at_size(int x, int y, int width, int height)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_RenderImage(ctx->display, im, ctx->drawable, ctx->mask,
                       ctx->visual, ctx->colormap, ctx->depth, 0, 0, im->w,
                       im->h, x, y, width, height, ctx->anti_alias,
                       ctx->dither, ctx->blend, ctx->dither_mask,
                       ctx->mask_alpha_threshold, ctx->color_modifier,
                       ctx->operation);
}

/**
 * @param source_x X coordinate of the source image.
 * @param source_y Y coordinate of the source image.
 * @param source_width Width of the source image.
 * @param source_height Height of the source image.
 * @param x X coordinate of the destination image.
 * @param y Y coordinate of the destination image.
 * @param width Width of the destination image.
 * @param height Height of the destination image.
 *
 * Renders the source (@p source_x, @p source_y, @p source_width, @p source_height) pixel
 * rectangle from the
 * current image onto the current drawable at the (@p x, @p y) location scaled
 * to the width @p width and height @p height.
 */
EAPI void
imlib_render_image_part_on_drawable_at_size(int source_x, int source_y,
                                            int source_width,
                                            int source_height, int x, int y,
                                            int width, int height)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_RenderImage(ctx->display, im, ctx->drawable, 0, ctx->visual,
                       ctx->colormap, ctx->depth, source_x, source_y,
                       source_width, source_height, x, y, width, height,
                       ctx->anti_alias, ctx->dither, ctx->blend, 0,
                       0, ctx->color_modifier, ctx->operation);
}

EAPI                DATA32
imlib_render_get_pixel_color(void)
{
   return __imlib_RenderGetPixel(ctx->display, ctx->drawable, ctx->visual,
                                 ctx->colormap, ctx->depth,
                                 (DATA8) ctx->color.red,
                                 (DATA8) ctx->color.green,
                                 (DATA8) ctx->color.blue);
}

#endif

/**
 * @param source_image The source image.
 * @param merge_alpha Alpha flag.
 * @param source_x X coordinate of the source image.
 * @param source_y Y coordinate of the source image.
 * @param source_width Width of the source image.
 * @param source_height Height of the source image.
 * @param destination_x X coordinate of the destination image.
 * @param destination_y Y coordinate of the destination image.
 * @param destination_width Width of the destination image.
 * @param destination_height Height of the destination image.
 *
 * Blends the source rectangle (@p source_x, @p source_y, @p
 * source_width, @p source_height) from
 * @p source_image onto the current image at the destination (@p
 * destination_x, @p destination_y) location
 * scaled to the width @p destination_width and height @p
 * destination_height. If @p merge_alpha is set to 1
 * it will also modify the destination image alpha channel, otherwise
 * the destination alpha channel is left untouched.
 */
EAPI void
imlib_blend_image_onto_image(Imlib_Image source_image, char merge_alpha,
                             int source_x, int source_y, int source_width,
                             int source_height, int destination_x,
                             int destination_y, int destination_width,
                             int destination_height)
{
   ImlibImage         *im_src, *im_dst;
   int                 aa;

   CHECK_PARAM_POINTER("source_image", source_image);
   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im_src, source_image);
   CAST_IMAGE(im_dst, ctx->image);
   if (__imlib_LoadImageData(im_src))
      return;
   if (__imlib_LoadImageData(im_dst))
      return;
   __imlib_DirtyImage(im_dst);
   /* FIXME: hack to get around infinite loops for scaling down too far */
   aa = ctx->anti_alias;
   if ((abs(destination_width) < (source_width >> 7))
       || (abs(destination_height) < (source_height >> 7)))
      aa = 0;
   __imlib_BlendImageToImage(im_src, im_dst, aa, ctx->blend,
                             merge_alpha, source_x, source_y, source_width,
                             source_height, destination_x, destination_y,
                             destination_width, destination_height,
                             ctx->color_modifier, ctx->operation,
                             ctx->cliprect.x, ctx->cliprect.y,
                             ctx->cliprect.w, ctx->cliprect.h);
}

/**
 * @param width The width of the image.
 * @param height The height of the image.
 * @return A new blank image.
 *
 * Creates a new blank image of size @p width and @p height. The contents of
 * this image at creation time are undefined (they could be garbage
 * memory). You are free to do whatever you like with this image. It
 * is not cached. On success an image handle is returned - on failure
 * NULL is returned.
 **/
EAPI                Imlib_Image
imlib_create_image(int width, int height)
{
   DATA32             *data;

   if (!IMAGE_DIMENSIONS_OK(width, height))
      return NULL;
   data = malloc(width * height * sizeof(DATA32));
   if (data)
      return (Imlib_Image) __imlib_CreateImage(width, height, data);
   return NULL;
}

/**
 * @param width The width of the image.
 * @param height The height of the image.
 * @param data The data.
 * @return A valid image, otherwise NULL.
 *
 * Creates an image from the image data specified with the width @p width and
 * the height @p height specified. The image data @p data must be in the same format as
 * imlib_image_get_data() would return. You are responsible for
 * freeing this image data once the image is freed - Imlib2 will not
 * do that for you. This is useful for when you already have static
 * buffers of the same format Imlib2 uses (many video grabbing devices
 * use such a format) and wish to use Imlib2 to render the results
 * onto another image, or X drawable. You should free the image when
 * you are done with it. Imlib2 returns a valid image handle on
 * success or NULL on failure
 *
 **/
EAPI                Imlib_Image
imlib_create_image_using_data(int width, int height, DATA32 * data)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("data", data, NULL);
   if (!IMAGE_DIMENSIONS_OK(width, height))
      return NULL;
   im = __imlib_CreateImage(width, height, data);
   if (im)
      SET_FLAG(im->flags, F_DONT_FREE_DATA);
   return (Imlib_Image) im;
}

/**
 * @param width The width of the image.
 * @param height The height of the image.
 * @param data The data.
 * @param func The memory management function.
 * @return A valid image, otherwise NULL.
 *
 * Creates an image from the image data specified with the width @p width and
 * the height @p height specified. The image data @p data must be in the same format as
 * imlib_image_get_data() would return. The memory management function @p func is
 * responsible for freeing this image data once the image is freed. Imlib2 returns a
 * valid image handle on success or NULL on failure.
 *
 **/
EAPI                Imlib_Image
   imlib_create_image_using_data_and_memory_function
   (int width, int height, DATA32 * data, Imlib_Image_Data_Memory_Function func)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("data", data, NULL);
   if (!IMAGE_DIMENSIONS_OK(width, height))
      return NULL;
   im = __imlib_CreateImage(width, height, data);
   if (im)
      im->data_memory_func = func;

   return (Imlib_Image) im;
}

/**
 * @param width The width of the image.
 * @param height The height of the image.
 * @param data The data.
 * @return A valid image, otherwise NULL.
 *
 * Works the same way as imlib_create_image_using_data() but Imlib2
 * copies the image data to the image structure. You may now do
 * whatever you wish with the original data as it will not be needed
 * anymore. Imlib2 returns a valid image handle on success or NULL on
 * failure.
 *
 **/
EAPI                Imlib_Image
imlib_create_image_using_copied_data(int width, int height, DATA32 * data)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("data", data, NULL);
   if (!IMAGE_DIMENSIONS_OK(width, height))
      return NULL;
   im = __imlib_CreateImage(width, height, NULL);
   if (!im)
      return NULL;
   im->data = malloc(width * height * sizeof(DATA32));
   if (data)
     {
        memcpy(im->data, data, width * height * sizeof(DATA32));
        return (Imlib_Image) im;
     }
   else
      __imlib_FreeImage(im);
   return NULL;
}

#ifdef BUILD_X11
/**
 * @param mask A mask.
 * @param x The top left x coordinate of the rectangle.
 * @param y The top left y coordinate of the rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 * @param need_to_grab_x Grab flag.
 * @return a valid image, otherwise NULL.
 *
 * Return an image (using the mask @p mask to determine the alpha channel)
 * from the current drawable.
 * If @p mask is 0 it will not create a useful alpha channel in the image.
 * If @p mask is 1 the mask will be set to the shape mask of the drawable.
 * It will create an image from the
 * (@p x, @p y, @p width , @p height) rectangle in the drawable. If @p
 * need_to_grab_x is 1 it will also grab the X Server to avoid possible race
 * conditions in grabbing. If you have not already grabbed the server
 * you MUST set this to 1. Imlib2 returns a valid image handle on
 * success or NULL on failure.
 *
 **/
EAPI                Imlib_Image
imlib_create_image_from_drawable(Pixmap mask, int x, int y, int width,
                                 int height, char need_to_grab_x)
{
   ImlibImage         *im;
   char                domask = 0;

   if (!IMAGE_DIMENSIONS_OK(width, height))
      return NULL;
   if (mask)
     {
        domask = 1;
        if (mask == (Pixmap) 1)
           mask = None;
     }
   im = __imlib_CreateImage(width, height, NULL);
   im->data = malloc(width * height * sizeof(DATA32));
   if (__imlib_GrabDrawableToRGBA(im->data, 0, 0, width, height, ctx->display,
                                  ctx->drawable, mask, ctx->visual,
                                  ctx->colormap, ctx->depth, x, y, width,
                                  height, &domask, need_to_grab_x))
     {
        UPDATE_FLAG(im->flags, F_HAS_ALPHA, domask);
     }
   else
     {
        __imlib_FreeImage(im);
        im = NULL;
     }

   return (Imlib_Image) im;
}

/**
 * @param image An image.
 * @param mask A mask.
 * @param x The top left x coordinate of the rectangle.
 * @param y The top left y coordinate of the rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 * @param need_to_grab_x Grab flag.
 * @return a valid image, otherwise NULL.
 *
 *
 **/
EAPI                Imlib_Image
imlib_create_image_from_ximage(XImage * image, XImage * mask, int x, int y,
                               int width, int height, char need_to_grab_x)
{
   ImlibImage         *im;

   if (!IMAGE_DIMENSIONS_OK(width, height))
      return NULL;
   im = __imlib_CreateImage(width, height, NULL);
   im->data = malloc(width * height * sizeof(DATA32));
   __imlib_GrabXImageToRGBA(im->data, 0, 0, width, height,
                            ctx->display, image, mask, ctx->visual,
                            ctx->depth, x, y, width, height, need_to_grab_x);
   return (Imlib_Image) im;
}

/**
 * @param mask A mask.
 * @param source_x The top left x coordinate of the rectangle.
 * @param source_y The top left y coordinate of the rectangle.
 * @param source_width The width of the rectangle.
 * @param source_height The height of the rectangle.
 * @param destination_width The width of the returned image.
 * @param destination_height The height of the returned image.
 * @param need_to_grab_x Grab flag.
 * @param get_mask_from_shape A char.
 * @return A valid image, otherwise NULL.
 *
 * Creates an image from the current drawable (optionally using the
 * @p mask pixmap specified to determine alpha transparency) and scale
 * the grabbed data first before converting to an actual image (to
 * minimize reads from the frame buffer which can be slow). The source
 * (@p source_x, @p source_y, @p source_width, @p source_height) rectangle will be grabbed, scaled to the
 * destination @p destination_width and @p destination_height, then converted to an image. If
 * @p need_to_grab_x is set to 1, X is grabbed (set this to 1 unless you
 * have already grabbed the server) and if @p get_mask_from_shape and the
 * current drawable is a window its shape is used for determining the
 * alpha channel. If successful this function will return a valid
 * image handle, otherwise NULL is returned.
 *
 **/
EAPI                Imlib_Image
imlib_create_scaled_image_from_drawable(Pixmap mask, int source_x,
                                        int source_y, int source_width,
                                        int source_height,
                                        int destination_width,
                                        int destination_height,
                                        char need_to_grab_x,
                                        char get_mask_from_shape)
{
   ImlibImage         *im;
   char                domask;

   if (!IMAGE_DIMENSIONS_OK(source_width, source_height))
      return NULL;
   if (!IMAGE_DIMENSIONS_OK(destination_width, destination_height))
      return NULL;

   domask = mask != 0 || get_mask_from_shape;

   im = __imlib_CreateImage(destination_width, destination_height, NULL);
   im->data = malloc(destination_width * destination_height * sizeof(DATA32));

   __imlib_GrabDrawableScaledToRGBA(im->data, 0, 0,
                                    destination_width, destination_height,
                                    ctx->display, ctx->drawable, mask,
                                    ctx->visual, ctx->colormap, ctx->depth,
                                    source_x, source_y,
                                    source_width, source_height,
                                    &domask, need_to_grab_x);

   UPDATE_FLAG(im->flags, F_HAS_ALPHA, domask);

   return (Imlib_Image) im;
}

/**
 * @param mask A mask.
 * @param x The top left x coordinate of the rectangle.
 * @param y The top left y coordinate of the rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 * @param destination_x The x coordinate of the new location.
 * @param destination_y The x coordinate of the new location.
 * @param need_to_grab_x Grab flag.
 * @return A char.
 *
 * Grabs a section of the current drawable (optionally using the pixmap @p mask
 * provided as a corresponding mask for that drawable - if @p mask is 0
 * this is not used).
 * If @p mask is 1 the mask will be set to the shape mask of the drawable.
 * It grabs the (@p x, @p y, @p width, @p height) rectangle and
 * places it at the destination (@p destination_x, @p destination_y) location in the current image. If
 * @p need_to_grab_x is 1 it will grab and ungrab the server whilst doing
 * this - you need to do this if you have not already grabbed the
 * server.
 *
 **/
EAPI char
imlib_copy_drawable_to_image(Pixmap mask, int x, int y, int width, int height,
                             int destination_x, int destination_y,
                             char need_to_grab_x)
{
   ImlibImage         *im;
   char                domask = 0;
   int                 pre_adj;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, 0);
   if (mask)
     {
        domask = 1;
        if (mask == (Pixmap) 1)
           mask = None;
     }
   CAST_IMAGE(im, ctx->image);

   if (__imlib_LoadImageData(im))
      return 0;

   pre_adj = 0;
   if (x < 0)
     {
        width += x;
        pre_adj = x;
        x = 0;
     }
   if (width < 0)
      width = 0;
   if (destination_x < 0)
     {
        width += destination_x;
        x -= destination_x - pre_adj;
        destination_x = 0;
     }
   if ((destination_x + width) >= im->w)
      width = im->w - destination_x;

   pre_adj = 0;
   if (y < 0)
     {
        height += y;
        pre_adj = y;
        y = 0;
     }
   if (height < 0)
      height = 0;
   if (destination_y < 0)
     {
        height += destination_y;
        y -= destination_y - pre_adj;
        destination_y = 0;
     }
   if ((destination_y + height) >= im->h)
      height = im->h - destination_y;

   if ((width <= 0) || (height <= 0))
      return 0;
   __imlib_DirtyImage(im);
   return __imlib_GrabDrawableToRGBA(im->data, destination_x, destination_y,
                                     im->w, im->h, ctx->display,
                                     ctx->drawable, mask, ctx->visual,
                                     ctx->colormap, ctx->depth, x, y, width,
                                     height, &domask, need_to_grab_x);
}
#endif

/**
 * @return A valid image, otherwise NULL.
 *
 * Creates an exact duplicate of the current image and returns a valid
 * image handle on success, or NULL on failure.
 *
 **/
EAPI                Imlib_Image
imlib_clone_image(void)
{
   ImlibImage         *im, *im_old;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, NULL);
   CAST_IMAGE(im_old, ctx->image);
   if (__imlib_LoadImageData(im_old))
      return NULL;
   /* Note: below check should've ensured by original image allocation,
    * but better safe than sorry. */
   if (!IMAGE_DIMENSIONS_OK(im_old->w, im_old->h))
      return NULL;
   im = __imlib_CreateImage(im_old->w, im_old->h, NULL);
   if (!(im))
      return NULL;
   im->data = malloc(im->w * im->h * sizeof(DATA32));
   if (!(im->data))
     {
        __imlib_FreeImage(im);
        return NULL;
     }
   memcpy(im->data, im_old->data, im->w * im->h * sizeof(DATA32));
   im->flags = im_old->flags;
   SET_FLAG(im->flags, F_UNCACHEABLE);
   im->moddate = im_old->moddate;
   im->border = im_old->border;
   im->loader = im_old->loader;
   if (im_old->format)
      im->format = strdup(im_old->format);
   if (im_old->file)
      im->file = strdup(im_old->file);
   return (Imlib_Image) im;
}

/**
 * @param x The top left x coordinate of the rectangle.
 * @param y The top left y coordinate of the rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 * @return A valid image, otherwise NULL.
 *
 * Creates a duplicate of a (@p x, @p y, @p width, @p height) rectangle in the
 * current image and returns a valid image handle on success, or NULL
 * on failure.
 *
 **/
EAPI                Imlib_Image
imlib_create_cropped_image(int x, int y, int width, int height)
{
   ImlibImage         *im, *im_old;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, NULL);
   if (!IMAGE_DIMENSIONS_OK(abs(width), abs(height)))
      return NULL;
   CAST_IMAGE(im_old, ctx->image);
   if (__imlib_LoadImageData(im_old))
      return NULL;
   im = __imlib_CreateImage(abs(width), abs(height), NULL);
   im->data = malloc(abs(width * height) * sizeof(DATA32));
   if (!(im->data))
     {
        __imlib_FreeImage(im);
        return NULL;
     }
   if (IMAGE_HAS_ALPHA(im_old))
     {
        SET_FLAG(im->flags, F_HAS_ALPHA);
        __imlib_BlendImageToImage(im_old, im, 0, 0, 1, x, y, abs(width),
                                  abs(height), 0, 0, width, height, NULL,
                                  (ImlibOp) IMLIB_OP_COPY,
                                  ctx->cliprect.x, ctx->cliprect.y,
                                  ctx->cliprect.w, ctx->cliprect.h);
     }
   else
     {
        __imlib_BlendImageToImage(im_old, im, 0, 0, 0, x, y, abs(width),
                                  abs(height), 0, 0, width, height, NULL,
                                  (ImlibOp) IMLIB_OP_COPY,
                                  ctx->cliprect.x, ctx->cliprect.y,
                                  ctx->cliprect.w, ctx->cliprect.h);
     }
   return (Imlib_Image) im;
}

/**
 * @param source_x The top left x coordinate of the source rectangle.
 * @param source_y The top left y coordinate of the source rectangle.
 * @param source_width The width of the source rectangle.
 * @param source_height The height of the source rectangle.
 * @param destination_width The width of the destination image.
 * @param destination_height The height of the destination image.
 * @return A valid image, otherwise NULL.
 *
 * Works the same as imlib_create_cropped_image() but will scale the
 * new image to the new destination @p destination_width and
 * @p destination_height whilst cropping.
 *
 **/
EAPI                Imlib_Image
imlib_create_cropped_scaled_image(int source_x, int source_y,
                                  int source_width, int source_height,
                                  int destination_width, int destination_height)
{
   ImlibImage         *im, *im_old;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, NULL);
   if (!IMAGE_DIMENSIONS_OK(abs(destination_width), abs(destination_height)))
      return NULL;
   CAST_IMAGE(im_old, ctx->image);
   if (__imlib_LoadImageData(im_old))
      return NULL;
   im = __imlib_CreateImage(abs(destination_width), abs(destination_height),
                            NULL);
   im->data =
      malloc(abs(destination_width * destination_height) * sizeof(DATA32));
   if (!(im->data))
     {
        __imlib_FreeImage(im);
        return NULL;
     }
   if (IMAGE_HAS_ALPHA(im_old))
     {
        SET_FLAG(im->flags, F_HAS_ALPHA);
        __imlib_BlendImageToImage(im_old, im, ctx->anti_alias, 0, 1, source_x,
                                  source_y, source_width, source_height, 0, 0,
                                  destination_width, destination_height, NULL,
                                  (ImlibOp) IMLIB_OP_COPY,
                                  ctx->cliprect.x, ctx->cliprect.y,
                                  ctx->cliprect.w, ctx->cliprect.h);
     }
   else
     {
        __imlib_BlendImageToImage(im_old, im, ctx->anti_alias, 0, 0, source_x,
                                  source_y, source_width, source_height, 0, 0,
                                  destination_width, destination_height, NULL,
                                  (ImlibOp) IMLIB_OP_COPY,
                                  ctx->cliprect.x, ctx->cliprect.y,
                                  ctx->cliprect.w, ctx->cliprect.h);
     }
   return (Imlib_Image) im;
}

/**
 * @param updates An updates list.
 * @return Duplicate of @p updates.
 *
 * Creates a duplicate of the updates list passed into the function.
 **/
EAPI                Imlib_Updates
imlib_updates_clone(Imlib_Updates updates)
{
   ImlibUpdate        *u;

   u = (ImlibUpdate *) updates;
   return (Imlib_Updates) __imlib_DupUpdates(u);
}

/**
 * @param updates An updates list.
 * @param x The top left x coordinate of the rectangle.
 * @param y The top left y coordinate of the rectangle.
 * @param w The width of the rectangle.
 * @param h The height of the rectangle.
 * @return The updates handle.
 *
 * Appends an update rectangle to the updates list passed in (if the
 * updates is NULL it will create a new updates list) and returns a
 * handle to the modified updates list (the handle may be modified so
 * only use the new updates handle returned).
 **/
EAPI                Imlib_Updates
imlib_update_append_rect(Imlib_Updates updates, int x, int y, int w, int h)
{
   ImlibUpdate        *u;

   u = (ImlibUpdate *) updates;
   return (Imlib_Updates) __imlib_AddUpdate(u, x, y, w, h);
}

/**
 * @param updates An updates list.
 * @param w The width of the rectangle.
 * @param h The height of the rectangle.
 * @return The updates handle.
 *
 * Takes an updates list, and modifies it by merging overlapped
 * rectangles and lots of tiny rectangles into larger rectangles to
 * minimize the number of rectangles in the list for optimized
 * redrawing. The new updates handle is now valid and the old one
 * passed in is not.
 **/
EAPI                Imlib_Updates
imlib_updates_merge(Imlib_Updates updates, int w, int h)
{
   ImlibUpdate        *u;

   u = (ImlibUpdate *) updates;
   return (Imlib_Updates) __imlib_MergeUpdate(u, w, h, 0);
}

/**
 * @param updates An updates list.
 * @param w The width of the rectangle.
 * @param h The height of the rectangle.
 * @return The updates handle.
 *
 * Works almost exactly as imlib_updates_merge() but is more lenient
 * on the spacing between update rectangles - if they are very close it
 * amalgamates 2 smaller rectangles into 1 larger one.
 **/
EAPI                Imlib_Updates
imlib_updates_merge_for_rendering(Imlib_Updates updates, int w, int h)
{
   ImlibUpdate        *u;

   u = (ImlibUpdate *) updates;
   return (Imlib_Updates) __imlib_MergeUpdate(u, w, h, 3);
}

/**
 * @param updates An updates list.
 *
 * Frees an updates list.
 **/
EAPI void
imlib_updates_free(Imlib_Updates updates)
{
   ImlibUpdate        *u;

   u = (ImlibUpdate *) updates;
   __imlib_FreeUpdates(u);
}

/**
 * @param updates An updates list.
 * @return The next updates.
 *
 * Gets the next update in the updates list relative to the one passed
 * in.
 **/
EAPI                Imlib_Updates
imlib_updates_get_next(Imlib_Updates updates)
{
   ImlibUpdate        *u;

   u = (ImlibUpdate *) updates;
   return (Imlib_Updates) (u->next);
}

/**
 * @param updates An updates list.
 * @param x_return The top left x coordinate of the update.
 * @param y_return The top left y coordinate of the update.
 * @param width_return The width of the update.
 * @param height_return The height of the update.
 *
 * Returns the coordinates of an update.
 **/
EAPI void
imlib_updates_get_coordinates(Imlib_Updates updates, int *x_return,
                              int *y_return, int *width_return,
                              int *height_return)
{
   ImlibUpdate        *u;

   CHECK_PARAM_POINTER("updates", updates);
   u = (ImlibUpdate *) updates;
   if (x_return)
      *x_return = u->x;
   if (y_return)
      *y_return = u->y;
   if (width_return)
      *width_return = u->w;
   if (height_return)
      *height_return = u->h;
}

/**
 * @param updates An updates list.
 * @param x The top left x coordinate of the update.
 * @param y The top left y coordinate of the update.
 * @param width The width of the update.
 * @param height The height of the update.
 *
 * Modifies the coordinates of an update in @p update.
 **/
EAPI void
imlib_updates_set_coordinates(Imlib_Updates updates, int x, int y, int width,
                              int height)
{
   ImlibUpdate        *u;

   CHECK_PARAM_POINTER("updates", updates);
   u = (ImlibUpdate *) updates;
   u->x = x;
   u->y = y;
   u->w = width;
   u->h = height;
}

#ifdef BUILD_X11
/**
 * @param updates An updates list.
 * @param x The top left x coordinate of the update.
 * @param y The top left y coordinate of the update.
 *
 * Given an updates list (preferable already merged for rendering)
 * this will render the corresponding parts of the image to the current
 * drawable at an offset of @p x, @p y in the drawable.
 **/
EAPI void
imlib_render_image_updates_on_drawable(Imlib_Updates updates, int x, int y)
{
   ImlibUpdate        *u;
   ImlibImage         *im;
   int                 ximcs;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   u = (ImlibUpdate *) updates;
   if (!updates)
      return;
   if (__imlib_LoadImageData(im))
      return;
   ximcs = __imlib_GetXImageCacheCountMax(ctx->display);        /* Save */
   if (ximcs == 0)              /* Only if we don't set this up elsewhere */
      __imlib_SetXImageCacheCountMax(ctx->display, 10);
   for (; u; u = u->next)
     {
        __imlib_RenderImage(ctx->display, im, ctx->drawable, 0, ctx->visual,
                            ctx->colormap, ctx->depth, u->x, u->y, u->w, u->h,
                            x + u->x, y + u->y, u->w, u->h, 0, ctx->dither, 0,
                            0, 0, ctx->color_modifier, OP_COPY);
     }
   if (ximcs == 0)
      __imlib_SetXImageCacheCountMax(ctx->display, ximcs);
}
#endif

/**
 * @return The initialized updates list.
 *
 * Initializes an updates list before you add any updates to it or
 * merge it for rendering etc.
 **/
EAPI                Imlib_Updates
imlib_updates_init(void)
{
   return (Imlib_Updates) NULL;
}

/**
 * @param updates An updates list.
 * @param appended_updates The updates list to append.
 * @return The new updates list.
 *
 * Appends @p appended_updates to the updates list @p updates and
 * returns the new list.
 **/
EAPI                Imlib_Updates
imlib_updates_append_updates(Imlib_Updates updates,
                             Imlib_Updates appended_updates)
{
   ImlibUpdate        *u, *uu;

   u = (ImlibUpdate *) updates;
   uu = (ImlibUpdate *) appended_updates;
   if (!uu)
      return (Imlib_Updates) u;
   if (!u)
      return (Imlib_Updates) uu;
   while (u)
     {
        if (!(u->next))
          {
             u->next = uu;
             return updates;
          }
        u = u->next;
     }
   return (Imlib_Updates) u;
}

/**
 * Flips/mirrors the current image horizontally.
 **/
EAPI void
imlib_image_flip_horizontal(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_FlipImageHoriz(im);
}

/**
 * Flips/mirrors the current image vertically.
 **/
EAPI void
imlib_image_flip_vertical(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_FlipImageVert(im);
}

/**
 * Flips/mirrors the current image diagonally (good for quick and dirty
 * 90 degree rotations if used before to after a horizontal or vertical
 * flip).
 **/
EAPI void
imlib_image_flip_diagonal(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_FlipImageDiagonal(im, 0);
}

/**
 * @param orientation The orientation.
 *
 * Performs 90 degree rotations on the current image. Passing in
 * @p orientation does not rotate, 1 rotates clockwise by 90 degree, 2,
 * rotates clockwise by 180 degrees, 3 rotates clockwise by 270
 * degrees.
 **/
EAPI void
imlib_image_orientate(int orientation)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   switch (orientation)
     {
     default:
     case 0:
        break;
     case 1:
        __imlib_FlipImageDiagonal(im, 1);
        break;
     case 2:
        __imlib_FlipImageBoth(im);
        break;
     case 3:
        __imlib_FlipImageDiagonal(im, 2);
        break;
     case 4:
        __imlib_FlipImageHoriz(im);
        break;
     case 5:
        __imlib_FlipImageDiagonal(im, 3);
        break;
     case 6:
        __imlib_FlipImageVert(im);
        break;
     case 7:
        __imlib_FlipImageDiagonal(im, 0);
        break;
     }
}

/**
 * @param radius The radius.
 *
 * Blurs the current image. A @p radius value of 0 has no effect, 1 and above
 * determine the blur matrix radius that determine how much to blur the
 * image.
 **/
EAPI void
imlib_image_blur(int radius)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_BlurImage(im, radius);
}

/**
 * @param radius The radius.
 *
 * Sharpens the current image. The @p radius value affects how much to sharpen
 * by.
 **/
EAPI void
imlib_image_sharpen(int radius)
{
   ImlibImage         *im;

   CAST_IMAGE(im, ctx->image);
   CHECK_PARAM_POINTER("image", ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_SharpenImage(im, radius);
}

/**
 * Modifies an image so it will tile seamlessly horizontally if used
 * as a tile (i.e. drawn multiple times horizontally).
 **/
EAPI void
imlib_image_tile_horizontal(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_TileImageHoriz(im);
}

/**
 * Modifies an image so it will tile seamlessly vertically if used as
 * a tile (i.e. drawn multiple times vertically).
 **/
EAPI void
imlib_image_tile_vertical(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_TileImageVert(im);
}

/**
 * Modifies an image so it will tile seamlessly horizontally and
 * vertically if used as a tile (i.e. drawn multiple times horizontally
 * and vertically).
 **/
EAPI void
imlib_image_tile(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_TileImageHoriz(im);
   __imlib_TileImageVert(im);
}

/**
 * @param font_name The font name with the size.
 * @return NULL if no font found.
 *
 * Loads a truetype font from the first directory in the font path that
 * contains that font. The font name @p font_name format is "font_name/size". For
 * example. If there is a font file called blum.ttf somewhere in the
 * font path you might use "blum/20" to load a 20 pixel sized font of
 * blum. If the font cannot be found NULL is returned.
 *
 **/
EAPI                Imlib_Font
imlib_load_font(const char *font_name)
{
   return __imlib_font_load_joined(font_name);
}

/**
 * Removes the current font from any fallback chain it's in and frees it.
 **/
EAPI void
imlib_free_font(void)
{
   CHECK_PARAM_POINTER("font", ctx->font);
   imlib_remove_font_from_fallback_chain(ctx->font);
   __imlib_font_free(ctx->font);
   ctx->font = NULL;
}

/**
 * @param font A previously loaded font.
 * @param fallback_font A previously loaded font to be chained to the given font.
 * @return 0 on success.
 *
 * This arranges for the given fallback font to be used if a glyph does not
 * exist in the given font when text is being rendered.
 * Fonts can be arranged in an aribitrarily long chain and attempts will be
 * made in order on the chain.
 * Cycles in the chain are not possible since the given fallback font is
 * removed from any chain it's already in.
 * A fallback font may be a member of only one chain. Adding it as the
 * fallback font to another font will remove it from it's first fallback chain.
 *
 * @deprecated This function may be removed.
 **/
EAPI int
imlib_insert_font_into_fallback_chain(Imlib_Font font, Imlib_Font fallback_font)
{
   CHECK_PARAM_POINTER_RETURN("font", font, 1);
   CHECK_PARAM_POINTER_RETURN("fallback_font", fallback_font, 1);
   return __imlib_font_insert_into_fallback_chain_imp(font, fallback_font);
}

/**
 * @param fallback_font A font previously added to a fallback chain.
 * @return 0 on success.
 *
 * This removes the given font from any fallback chain it may be in.
 * Removing this font joins its previous and next font together in the fallback
 * chain.
 *
 * @deprecated This function may be removed.
 **/
EAPI void
imlib_remove_font_from_fallback_chain(Imlib_Font fallback_font)
{
   CHECK_PARAM_POINTER("fallback_font", fallback_font);
   __imlib_font_remove_from_fallback_chain_imp(fallback_font);
}

/**
 * @deprecated This function may be removed.
 **/
EAPI                Imlib_Font
imlib_get_prev_font_in_fallback_chain(Imlib_Font fn)
{
   CHECK_PARAM_POINTER_RETURN("fn", fn, 0);
   return ((ImlibFont *) fn)->fallback_prev;
}

/**
 * @deprecated This function may be removed.
 **/
EAPI                Imlib_Font
imlib_get_next_font_in_fallback_chain(Imlib_Font fn)
{
   CHECK_PARAM_POINTER_RETURN("fn", fn, 0);
   return ((ImlibFont *) fn)->fallback_next;
}

/**
 * @param x The x coordinate of the top left  corner.
 * @param y The y coordinate of the top left  corner.
 * @param text A null-byte terminated string.
 *
 * Draws the null-byte terminated string @p text using the current font on
 * the current image at the (@p x, @p y) location (@p x, @p y denoting the top left
 * corner of the font string)
 **/
EAPI void
imlib_text_draw(int x, int y, const char *text)
{
   imlib_text_draw_with_return_metrics(x, y, text, NULL, NULL, NULL, NULL);
}

/**
 * @param x The x coordinate of the top left  corner.
 * @param y The y coordinate of the top left  corner.
 * @param text A null-byte terminated string.
 * @param width_return The width of the string.
 * @param height_return The height of the string.
 * @param horizontal_advance_return Horizontal offset.
 * @param vertical_advance_return Vertical offset.
 *
 * Works just like imlib_text_draw() but also returns the width and
 * height of the string drawn, and @p horizontal_advance_return returns
 * the number of pixels you should advance horizontally to draw another
 * string (useful if you are drawing a line of text word by word) and
 * @p vertical_advance_return does the same for the vertical direction
 * (i.e. drawing text line by line).
 **/
EAPI void
imlib_text_draw_with_return_metrics(int x, int y, const char *text,
                                    int *width_return, int *height_return,
                                    int *horizontal_advance_return,
                                    int *vertical_advance_return)
{
   ImlibImage         *im;
   ImlibFont          *fn;
   int                 dir;

   CHECK_PARAM_POINTER("font", ctx->font);
   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("text", text);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   fn = (ImlibFont *) ctx->font;
   __imlib_DirtyImage(im);

   dir = ctx->direction;
   if (ctx->direction == IMLIB_TEXT_TO_ANGLE && ctx->angle == 0.0)
      dir = IMLIB_TEXT_TO_RIGHT;

   __imlib_render_str(im, fn, x, y, text, ctx->pixel, dir,
                      ctx->angle, width_return, height_return, 0,
                      horizontal_advance_return, vertical_advance_return,
                      ctx->operation,
                      ctx->cliprect.x, ctx->cliprect.y,
                      ctx->cliprect.w, ctx->cliprect.h);
}

/**
 * @param text A string.
 * @param width_return The width of the text.
 * @param height_return The height of the text.
 *
 * Gets the width and height in pixels the @p text string would use up
 * if drawn with the current font.
 **/
EAPI void
imlib_get_text_size(const char *text, int *width_return, int *height_return)
{
   ImlibFont          *fn;
   int                 w, h;
   int                 dir;

   CHECK_PARAM_POINTER("font", ctx->font);
   CHECK_PARAM_POINTER("text", text);
   fn = (ImlibFont *) ctx->font;

   dir = ctx->direction;
   if (ctx->direction == IMLIB_TEXT_TO_ANGLE && ctx->angle == 0.0)
      dir = IMLIB_TEXT_TO_RIGHT;

   __imlib_font_query_size(fn, text, &w, &h);

   switch (dir)
     {
     case IMLIB_TEXT_TO_RIGHT:
     case IMLIB_TEXT_TO_LEFT:
        if (width_return)
           *width_return = w;
        if (height_return)
           *height_return = h;
        break;
     case IMLIB_TEXT_TO_DOWN:
     case IMLIB_TEXT_TO_UP:
        if (width_return)
           *width_return = h;
        if (height_return)
           *height_return = w;
        break;
     case IMLIB_TEXT_TO_ANGLE:
        if (width_return || height_return)
          {
             double              sa, ca;

             sa = sin(ctx->angle);
             ca = cos(ctx->angle);

             if (width_return)
               {
                  double              x1, x2, xt;

                  x1 = x2 = 0.0;
                  xt = ca * w;
                  if (xt < x1)
                     x1 = xt;
                  if (xt > x2)
                     x2 = xt;
                  xt = -(sa * h);
                  if (xt < x1)
                     x1 = xt;
                  if (xt > x2)
                     x2 = xt;
                  xt = ca * w - sa * h;
                  if (xt < x1)
                     x1 = xt;
                  if (xt > x2)
                     x2 = xt;
                  *width_return = (int)(x2 - x1);
               }
             if (height_return)
               {
                  double              y1, y2, yt;

                  y1 = y2 = 0.0;
                  yt = sa * w;
                  if (yt < y1)
                     y1 = yt;
                  if (yt > y2)
                     y2 = yt;
                  yt = ca * h;
                  if (yt < y1)
                     y1 = yt;
                  if (yt > y2)
                     y2 = yt;
                  yt = sa * w + ca * h;
                  if (yt < y1)
                     y1 = yt;
                  if (yt > y2)
                     y2 = yt;
                  *height_return = (int)(y2 - y1);
               }
          }
        break;
     default:
        break;
     }
}

/**
 * @param text A string.
 * @param horizontal_advance_return Horizontal offset.
 * @param vertical_advance_return Vertical offset.
 *
 * Gets the advance horizontally and vertically in pixels the next
 * text string would need to be placed at for the current font. The
 * advances are not adjusted for rotation so you will have to translate
 * the advances (which are calculated as if the text was drawn
 * horizontally from left to right) depending on the text orientation.
 **/
EAPI void
imlib_get_text_advance(const char *text, int *horizontal_advance_return,
                       int *vertical_advance_return)
{
   ImlibFont          *fn;
   int                 w, h;

   CHECK_PARAM_POINTER("font", ctx->font);
   CHECK_PARAM_POINTER("text", text);
   fn = (ImlibFont *) ctx->font;
   __imlib_font_query_advance(fn, text, &w, &h);
   if (horizontal_advance_return)
      *horizontal_advance_return = w;
   if (vertical_advance_return)
      *vertical_advance_return = h;
}

/**
 * @param text A string.
 * @return The inset value of @text.
 *
 * Returns the inset of the first character of @p text
 * in using the current font and returns that value in pixels.
 *
 **/
EAPI int
imlib_get_text_inset(const char *text)
{
   ImlibFont          *fn;

   CHECK_PARAM_POINTER_RETURN("font", ctx->font, 0);
   CHECK_PARAM_POINTER_RETURN("text", text, 0);
   fn = (ImlibFont *) ctx->font;
   return __imlib_font_query_inset(fn, text);
}

/**
 * @param path A directory path.
 *
 * Adds the directory @p path to the end of the current list of
 * directories to scan for fonts.
 **/
EAPI void
imlib_add_path_to_font_path(const char *path)
{
   CHECK_PARAM_POINTER("path", path);
   if (!__imlib_font_path_exists(path))
      __imlib_font_add_font_path(path);
}

/**
 * @param path A directory path.
 *
 * Removes all directories in the font path that match @p path.
 **/
EAPI void
imlib_remove_path_from_font_path(const char *path)
{
   CHECK_PARAM_POINTER("path", path);
   __imlib_font_del_font_path(path);
}

/**
 * @param number_return Number of paths in the list.
 * @return A list of strings.
 *
 * Returns a list of strings that are the directories in the font
 * path. Do not free this list or change it in any way. If you add or
 * delete members of the font path this list will be invalid. If you
 * intend to use this list later duplicate it for your own use. The
 * number of elements in the array of strings is put into
 * @p number_return.
 *
 **/
EAPI char         **
imlib_list_font_path(int *number_return)
{
   CHECK_PARAM_POINTER_RETURN("number_return", number_return, NULL);
   return __imlib_font_list_font_path(number_return);
}

/**
 * @param text A string.
 * @param x The x offset.
 * @param y The y offset.
 * @param char_x_return The x coordinate of the character.
 * @param char_y_return The x coordinate of the character.
 * @param char_width_return The width of the character.
 * @param char_height_return The height of the character.
 * @return -1 if no character found.
 *
 * Returns the character number in the string @p text using the current
 * font at the (@p x, @p y) pixel location which is an offset relative to the
 * top left of that string. -1 is returned if there is no character
 * there. If there is a character, @p char_x_return, @p char_y_return,
 * @p char_width_return and @p char_height_return (respectively the
 * character x, y, width and height)  are also filled in.
 *
 **/
EAPI int
imlib_text_get_index_and_location(const char *text, int x, int y,
                                  int *char_x_return, int *char_y_return,
                                  int *char_width_return,
                                  int *char_height_return)
{
   ImlibFont          *fn;
   int                 w, h, cx, cy, cw, ch, cp, xx, yy;
   int                 dir;

   CHECK_PARAM_POINTER_RETURN("font", ctx->font, -1);
   CHECK_PARAM_POINTER_RETURN("text", text, -1);
   fn = (ImlibFont *) ctx->font;

   dir = ctx->direction;
   if (ctx->direction == IMLIB_TEXT_TO_ANGLE && ctx->angle == 0.0)
      dir = IMLIB_TEXT_TO_RIGHT;

   imlib_get_text_size(text, &w, &h);

   switch (dir)
     {
     case IMLIB_TEXT_TO_RIGHT:
        xx = x;
        yy = y;
        break;
     case IMLIB_TEXT_TO_LEFT:
        xx = w - x;
        yy = h - y;
        break;
     case IMLIB_TEXT_TO_DOWN:
        xx = y;
        yy = w - x;
        break;
     case IMLIB_TEXT_TO_UP:
        xx = h - y;
        yy = x;
        break;
     default:
        return -1;
     }

   cp = __imlib_font_query_text_at_pos(fn, text, xx, yy, &cx, &cy, &cw, &ch);

   switch (dir)
     {
     case IMLIB_TEXT_TO_RIGHT:
        if (char_x_return)
           *char_x_return = cx;
        if (char_y_return)
           *char_y_return = cy;
        if (char_width_return)
           *char_width_return = cw;
        if (char_height_return)
           *char_height_return = ch;
        return cp;
        break;
     case IMLIB_TEXT_TO_LEFT:
        cx = 1 + w - cx - cw;
        if (char_x_return)
           *char_x_return = cx;
        if (char_y_return)
           *char_y_return = cy;
        if (char_width_return)
           *char_width_return = cw;
        if (char_height_return)
           *char_height_return = ch;
        return cp;
        break;
     case IMLIB_TEXT_TO_DOWN:
        if (char_x_return)
           *char_x_return = cy;
        if (char_y_return)
           *char_y_return = cx;
        if (char_width_return)
           *char_width_return = ch;
        if (char_height_return)
           *char_height_return = cw;
        return cp;
        break;
     case IMLIB_TEXT_TO_UP:
        cy = 1 + h - cy - ch;
        if (char_x_return)
           *char_x_return = cy;
        if (char_y_return)
           *char_y_return = cx;
        if (char_width_return)
           *char_width_return = ch;
        if (char_height_return)
           *char_height_return = cw;
        return cp;
        break;
     default:
        return -1;
        break;
     }
   return -1;
}

/**
 * @param text A string.
 * @param index The index of @text.
 * @param char_x_return The x coordinate of the character.
 * @param char_y_return The y coordinate of the character.
 * @param char_width_return The width of the character.
 * @param char_height_return The height of the character.
 *
 * Gets the geometry of the character at index @p index in the @p text
 * string using the current font.
 **/
EAPI void
imlib_text_get_location_at_index(const char *text, int index,
                                 int *char_x_return, int *char_y_return,
                                 int *char_width_return,
                                 int *char_height_return)
{
   ImlibFont          *fn;
   int                 cx, cy, cw, ch, w, h;

   CHECK_PARAM_POINTER("font", ctx->font);
   CHECK_PARAM_POINTER("text", text);
   fn = (ImlibFont *) ctx->font;

   __imlib_font_query_char_coords(fn, text, index, &cx, &cy, &cw, &ch);

   w = h = 0;
   imlib_get_text_size(text, &w, &h);

   switch (ctx->direction)
     {
     case IMLIB_TEXT_TO_RIGHT:
        if (char_x_return)
           *char_x_return = cx;
        if (char_y_return)
           *char_y_return = cy;
        if (char_width_return)
           *char_width_return = cw;
        if (char_height_return)
           *char_height_return = ch;
        return;
        break;
     case IMLIB_TEXT_TO_LEFT:
        cx = 1 + w - cx - cw;
        if (char_x_return)
           *char_x_return = cx;
        if (char_y_return)
           *char_y_return = cy;
        if (char_width_return)
           *char_width_return = cw;
        if (char_height_return)
           *char_height_return = ch;
        return;
        break;
     case IMLIB_TEXT_TO_DOWN:
        if (char_x_return)
           *char_x_return = cy;
        if (char_y_return)
           *char_y_return = cx;
        if (char_width_return)
           *char_width_return = ch;
        if (char_height_return)
           *char_height_return = cw;
        return;
        break;
     case IMLIB_TEXT_TO_UP:
        cy = 1 + h - cy - ch;
        if (char_x_return)
           *char_x_return = cy;
        if (char_y_return)
           *char_y_return = cx;
        if (char_width_return)
           *char_width_return = ch;
        if (char_height_return)
           *char_height_return = cw;
        return;
        break;
     default:
        return;
        break;
     }
}

/**
 * @param number_return Number of fonts in the list.
 * @return A list of fonts.
 *
 * Returns a list of fonts imlib2 can find in its font path.
 *
 **/
EAPI char         **
imlib_list_fonts(int *number_return)
{
   CHECK_PARAM_POINTER_RETURN("number_return", number_return, NULL);
   return __imlib_font_list_fonts(number_return);
}

/**
 * @param font_list The font list.
 * @param number Number of fonts in the list.
 *
 * Frees the font list returned by imlib_list_fonts().
 *
 **/
EAPI void
imlib_free_font_list(char **font_list, int number)
{
   __imlib_FileFreeDirList(font_list, number);
}

/**
 * @return The font cache size.
 *
 * Returns the font cache size in bytes.
 *
 **/
EAPI int
imlib_get_font_cache_size(void)
{
   return __imlib_font_cache_get();
}

/**
 * @param bytes The font cache size.
 *
 * Sets the font cache in bytes. Whenever you set the font cache size
 * Imlib2 will flush fonts from the cache until the memory used by
 * fonts is less than or equal to the font cache size. Setting the size
 * to 0 effectively frees all speculatively cached fonts.
 **/
EAPI void
imlib_set_font_cache_size(int bytes)
{
   __imlib_font_cache_set(bytes);
}

/**
 * Causes a flush of all speculatively cached fonts from the font
 * cache.
 **/
EAPI void
imlib_flush_font_cache(void)
{
   __imlib_font_flush();
}

/**
 * @return The font's ascent.
 *
 * Returns the current font's ascent value in pixels.
 *
 **/
EAPI int
imlib_get_font_ascent(void)
{
   CHECK_PARAM_POINTER_RETURN("font", ctx->font, 0);
   return __imlib_font_ascent_get(ctx->font);
}

/**
 * @return The font's descent.
 *
 * Returns the current font's descent value in pixels.
 *
 **/
EAPI int
imlib_get_font_descent(void)
{
   CHECK_PARAM_POINTER_RETURN("font", ctx->font, 0);
   return __imlib_font_descent_get(ctx->font);
}

/**
 * @return The font's maximum ascent.
 *
 * Returns the current font's maximum ascent extent.
 *
 **/
EAPI int
imlib_get_maximum_font_ascent(void)
{
   CHECK_PARAM_POINTER_RETURN("font", ctx->font, 0);
   return __imlib_font_max_ascent_get(ctx->font);
}

/**
 * @return The font's maximum descent.
 *
 * Returns the current font's maximum descent extent.
 *
 **/
EAPI int
imlib_get_maximum_font_descent(void)
{
   CHECK_PARAM_POINTER_RETURN("font", ctx->font, 0);
   return __imlib_font_max_descent_get(ctx->font);
}

/**
 * @return Valid handle.
 *
 * Creates a new empty color modifier and returns a
 * valid handle on success. NULL is returned on failure.
 *
 **/
EAPI                Imlib_Color_Modifier
imlib_create_color_modifier(void)
{
   return (Imlib_Color_Modifier) __imlib_CreateCmod();
}

/**
 * Frees the current color modifier.
 **/
EAPI void
imlib_free_color_modifier(void)
{
   CHECK_PARAM_POINTER("color_modifier", ctx->color_modifier);
   __imlib_FreeCmod((ImlibColorModifier *) ctx->color_modifier);
   ctx->color_modifier = NULL;
}

/**
 * @param gamma_value Value of gamma.
 *
 * Modifies the current color modifier by adjusting the gamma by the
 * value specified @p gamma_value. The color modifier is modified not set, so calling
 * this repeatedly has cumulative effects. A gamma of 1.0 is normal
 * linear, 2.0 brightens and 0.5 darkens etc. Negative values are not
 * allowed.
 **/
EAPI void
imlib_modify_color_modifier_gamma(double gamma_value)
{
   CHECK_PARAM_POINTER("color_modifier", ctx->color_modifier);
   __imlib_CmodModGamma((ImlibColorModifier *) ctx->color_modifier,
                        gamma_value);
}

/**
 * @param brightness_value Value of brightness.
 *
 * Modifies the current color modifier by adjusting the brightness by
 * the value @p brightness_value. The color modifier is modified not set, so
 * calling this repeatedly has cumulative effects. brightness values
 * of 0 do not affect anything. -1.0 will make things completely black
 * and 1.0 will make things all white. Values in-between vary
 * brightness linearly.
 **/
EAPI void
imlib_modify_color_modifier_brightness(double brightness_value)
{
   CHECK_PARAM_POINTER("color_modifier", ctx->color_modifier);
   __imlib_CmodModBrightness((ImlibColorModifier *) ctx->color_modifier,
                             brightness_value);
}

/**
 * @param contrast_value Value of contrast.
 *
 * Modifies the current color modifier by adjusting the contrast by
 * the value @p contrast_value. The color modifier is modified not set, so
 * calling this repeatedly has cumulative effects. Contrast of 1.0 does
 * nothing. 0.0 will merge to gray, 2.0 will double contrast etc.
 **/
EAPI void
imlib_modify_color_modifier_contrast(double contrast_value)
{
   CHECK_PARAM_POINTER("color_modifier", ctx->color_modifier);
   __imlib_CmodModContrast((ImlibColorModifier *) ctx->color_modifier,
                           contrast_value);
}

/**
 * @param red_table An array of #DATA8.
 * @param green_table An array of #DATA8.
 * @param blue_table An array of #DATA8.
 * @param alpha_table An array of #DATA8.
 *
 * Explicitly copies the mapping tables from the table pointers passed
 * into this function into those of the current color modifier. Tables
 * are 256 entry arrays of DATA8 which are a mapping of that channel
 * value to a new channel value. A normal mapping would be linear (v[0]
 * = 0, v[10] = 10, v[50] = 50, v[200] = 200, v[255] = 255).
 **/
EAPI void
imlib_set_color_modifier_tables(DATA8 * red_table, DATA8 * green_table,
                                DATA8 * blue_table, DATA8 * alpha_table)
{
   CHECK_PARAM_POINTER("color_modifier", ctx->color_modifier);
   __imlib_CmodSetTables((ImlibColorModifier *) ctx->color_modifier,
                         red_table, green_table, blue_table, alpha_table);
}

/**
 * @param red_table: an array of #DATA8.
 * @param green_table: an array of #DATA8.
 * @param blue_table: an array of #DATA8.
 * @param alpha_table: an array of #DATA8.
 *
 * Copies the table values from the current color modifier into the
 * pointers to mapping tables specified. They must have 256 entries and
 * be DATA8 format.
 **/
EAPI void
imlib_get_color_modifier_tables(DATA8 * red_table, DATA8 * green_table,
                                DATA8 * blue_table, DATA8 * alpha_table)
{
   CHECK_PARAM_POINTER("color_modifier", ctx->color_modifier);
   __imlib_CmodGetTables((ImlibColorModifier *) ctx->color_modifier,
                         red_table, green_table, blue_table, alpha_table);
}

/**
 * Resets the current color modifier to have linear mapping tables.
 **/
EAPI void
imlib_reset_color_modifier(void)
{
   CHECK_PARAM_POINTER("color_modifier", ctx->color_modifier);
   __imlib_CmodReset((ImlibColorModifier *) ctx->color_modifier);
}

/**
 * Uses the current color modifier and modifies the current image using
 * the mapping tables in the current color modifier.
 **/
EAPI void
imlib_apply_color_modifier(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("color_modifier", ctx->color_modifier);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_DataCmodApply(im->data, im->w, im->h, 0, &(im->flags),
                         (ImlibColorModifier *) ctx->color_modifier);
}

/**
 * @param x The x coordinate of the left edge of the rectangle.
 * @param y  The y coordinate of the top edge of the rectangle.
 * @param width  The width of the rectangle.
 * @param height  The height of the rectangle.
 *
 * Works the same way as imlib_apply_color_modifier() but only modifies
 * a selected rectangle in the current image.
 **/
EAPI void
imlib_apply_color_modifier_to_rectangle(int x, int y, int width, int height)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("color_modifier", ctx->color_modifier);
   CAST_IMAGE(im, ctx->image);
   if (x < 0)
     {
        width += x;
        x = 0;
     }
   if (width <= 0)
      return;
   if ((x + width) > im->w)
      width = (im->w - x);
   if (width <= 0)
      return;
   if (y < 0)
     {
        height += y;
        y = 0;
     }
   if (height <= 0)
      return;
   if ((y + height) > im->h)
      height = (im->h - y);
   if (height <= 0)
      return;
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_DataCmodApply(im->data + (y * im->w) + x, width, height,
                         im->w - width, &(im->flags),
                         (ImlibColorModifier *) ctx->color_modifier);
}

EAPI                Imlib_Updates
imlib_image_draw_pixel(int x, int y, char make_updates)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, NULL);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return NULL;
   __imlib_DirtyImage(im);
   return (Imlib_Updates) __imlib_Point_DrawToImage(x, y, ctx->pixel, im,
                                                    ctx->cliprect.x,
                                                    ctx->cliprect.y,
                                                    ctx->cliprect.w,
                                                    ctx->cliprect.h,
                                                    ctx->operation, ctx->blend,
                                                    make_updates);
}

/**
 * @param x1 The x coordinate of the first point.
 * @param y1 The y coordinate of the first point.
 * @param x2 The x coordinate of the second point.
 * @param y2 The y coordinate of the second point.
 * @param make_updates: a char.
 * @return An updates list.
 *
 * Draws a line using the current color on the current image from
 * coordinates (@p x1, @p y1) to (@p x2, @p y2). If @p make_updates is 1 it will also
 * return an update you can use for an updates list, otherwise it
 * returns NULL.
 *
 **/
EAPI                Imlib_Updates
imlib_image_draw_line(int x1, int y1, int x2, int y2, char make_updates)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, NULL);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return NULL;
   __imlib_DirtyImage(im);
   return (Imlib_Updates) __imlib_Line_DrawToImage(x1, y1, x2, y2, ctx->pixel,
                                                   im, ctx->cliprect.x,
                                                   ctx->cliprect.y,
                                                   ctx->cliprect.w,
                                                   ctx->cliprect.h,
                                                   ctx->operation, ctx->blend,
                                                   ctx->anti_alias,
                                                   make_updates);
}

/**
 * @param x The top left x coordinate of the rectangle.
 * @param y The top left y coordinate of the rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 *
 * Draws the outline of a rectangle on the current image at the (@p x,
 * @p y)
 * coordinates with a size of @p width and @p height pixels, using the
 * current color.
 **/
EAPI void
imlib_image_draw_rectangle(int x, int y, int width, int height)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_Rectangle_DrawToImage(x, y, width, height, ctx->pixel,
                                 im, ctx->cliprect.x, ctx->cliprect.y,
                                 ctx->cliprect.w, ctx->cliprect.h,
                                 ctx->operation, ctx->blend);
}

/**
 * @param x The top left x coordinate of the rectangle.
 * @param y The top left y coordinate of the rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 *
 * Draws a filled rectangle on the current image at the (@p x, @p y)
 * coordinates with a size of @p width and @p height pixels, using the
 * current color.
 **/
EAPI void
imlib_image_fill_rectangle(int x, int y, int width, int height)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_Rectangle_FillToImage(x, y, width, height, ctx->pixel,
                                 im, ctx->cliprect.x, ctx->cliprect.y,
                                 ctx->cliprect.w, ctx->cliprect.h,
                                 ctx->operation, ctx->blend);
}

/**
 * @param image_source An image.
 * @param x The x coordinate.
 * @param y The y coordinate.
 *
 * Copies the alpha channel of the source image @p image_source to the
 * (@p x, @p y) coordinates
 * of the current image, replacing the alpha channel there.
 **/
EAPI void
imlib_image_copy_alpha_to_image(Imlib_Image image_source, int x, int y)
{
   ImlibImage         *im, *im2;

   CHECK_PARAM_POINTER("image_source", image_source);
   CHECK_PARAM_POINTER("image_destination", ctx->image);
   CAST_IMAGE(im, image_source);
   CAST_IMAGE(im2, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   if (__imlib_LoadImageData(im2))
      return;
   __imlib_DirtyImage(im);
   __imlib_copy_alpha_data(im, im2, 0, 0, im->w, im->h, x, y);
}

/**
 * @param image_source An image.
 * @param x The top left x coordinate of the rectangle.
 * @param y The top left y coordinate of the rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 * @param destination_x The top left x coordinate of the destination rectangle.
 * @param destination_y The top left x coordinate of the destination rectangle.
 *
 * Copies the source (@p x, @p y, @p width, @p height) rectangle alpha channel from
 * the source image @p image_source and replaces the alpha channel on the destination
 * image at the (@p destination_x, @p destination_y) coordinates.
 **/
EAPI void
imlib_image_copy_alpha_rectangle_to_image(Imlib_Image image_source, int x,
                                          int y, int width, int height,
                                          int destination_x, int destination_y)
{
   ImlibImage         *im, *im2;

   CHECK_PARAM_POINTER("image_source", image_source);
   CHECK_PARAM_POINTER("image_destination", ctx->image);
   CAST_IMAGE(im, image_source);
   CAST_IMAGE(im2, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   if (__imlib_LoadImageData(im2))
      return;
   __imlib_DirtyImage(im);
   __imlib_copy_alpha_data(im, im2, x, y, width, height, destination_x,
                           destination_y);
}

/**
 * @param x The top left x coordinate of the rectangle.
 * @param y The top left y coordinate of the rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 * @param delta_x Distance along the x coordinates.
 * @param delta_y Distance along the y coordinates.
 *
 * Scrolls a rectangle of size @p width, @p height at the (@p x, @p y)
 * location within the current image
 * by the @p delta_x, @p delta_y distance (in pixels).
 **/
EAPI void
imlib_image_scroll_rect(int x, int y, int width, int height, int delta_x,
                        int delta_y)
{
   ImlibImage         *im;
   int                 xx, yy, w, h, nx, ny;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   if (delta_x > 0)
     {
        xx = x;
        nx = x + delta_x;
        w = width - delta_x;
     }
   else
     {
        xx = x - delta_x;
        nx = x;
        w = width + delta_x;
     }
   if (delta_y > 0)
     {
        yy = y;
        ny = y + delta_y;
        h = height - delta_y;
     }
   else
     {
        yy = y - delta_y;
        ny = y;
        h = height + delta_y;
     }
   __imlib_DirtyImage(im);
   __imlib_copy_image_data(im, xx, yy, w, h, nx, ny);
}

/**
 * @param x The top left x coordinate of the rectangle.
 * @param y The top left y coordinate of the rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 * @param new_x The top left x coordinate of the new location.
 * @param new_y The top left y coordinate of the new location.
 *
 * Copies a rectangle of size @p width, @p height at the (@p x, @p y) location
 * specified in the current image to a new location (@p new_x, @p new_y) in the same
 * image.
 **/
EAPI void
imlib_image_copy_rect(int x, int y, int width, int height, int new_x, int new_y)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_copy_image_data(im, x, y, width, height, new_x, new_y);
}

/**
 * @return valid handle.
 *
 * Creates a new empty color range and returns a valid handle to that
 * color range.
 **/
EAPI                Imlib_Color_Range
imlib_create_color_range(void)
{
   return (Imlib_Color_Range) __imlib_CreateRange();
}

/**
 * Frees the current color range.
 **/
EAPI void
imlib_free_color_range(void)
{
   CHECK_PARAM_POINTER("color_range", ctx->color_range);
   __imlib_FreeRange((ImlibRange *) ctx->color_range);
   ctx->color_range = NULL;
}

/**
 * @param distance_away Distance from the previous color.
 *
 * Adds the current color to the current color range at a @p distance_away
 * distance from the previous color in the range (if it's the first
 * color in the range this is irrelevant).
 **/
EAPI void
imlib_add_color_to_color_range(int distance_away)
{
   CHECK_PARAM_POINTER("color_range", ctx->color_range);
   __imlib_AddRangeColor((ImlibRange *) ctx->color_range, ctx->color.red,
                         ctx->color.green, ctx->color.blue, ctx->color.alpha,
                         distance_away);
}

/**
 * @param x The x coordinate of the left edge of the rectangle.
 * @param y The y coordinate of the top edge of the rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 * @param angle Angle of gradient.
 *
 * Fills a rectangle of width @p width and height @p height at the (@p x, @p y) location
 * specified in the current image with a linear gradient of the
 * current color range at an angle of @p angle degrees with 0 degrees
 * being vertical from top to bottom going clockwise from there.
 **/
EAPI void
imlib_image_fill_color_range_rectangle(int x, int y, int width, int height,
                                       double angle)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("color_range", ctx->color_range);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_DrawGradient(im, x, y, width, height,
                        (ImlibRange *) ctx->color_range, angle,
                        ctx->operation,
                        ctx->cliprect.x, ctx->cliprect.y,
                        ctx->cliprect.w, ctx->cliprect.h);
}

/**
 * @param x The x coordinate of the left edge of the rectangle.
 * @param y The y coordinate of the top edge of the rectangle.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 * @param angle Angle of gradient.
 *
 * Fills a rectangle of width @p width and height @p height at the (@p
 * x, @p y) location
 * specified in the current image with a linear gradient in HSVA color
 * space of the current color range at an angle of @p angle degrees with
 * 0 degrees being vertical from top to bottom going clockwise from
 * there.
 **/
EAPI void
imlib_image_fill_hsva_color_range_rectangle(int x, int y, int width, int height,
                                            double angle)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("color_range", ctx->color_range);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_DrawHsvaGradient(im, x, y, width, height,
                            (ImlibRange *) ctx->color_range, angle,
                            ctx->operation,
                            ctx->cliprect.x, ctx->cliprect.y,
                            ctx->cliprect.w, ctx->cliprect.h);
}

/**
 * @param x The x coordinate of the pixel.
 * @param y The y coordinate of the pixel.
 * @param color_return The returned color.
 *
 * Fills the @p color_return color structure with the color of the pixel
 * in the current image that is at the (@p x, @p y) location specified.
 **/
EAPI void
imlib_image_query_pixel(int x, int y, Imlib_Color * color_return)
{
   ImlibImage         *im;
   DATA32             *p;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("color_return", color_return);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   if ((x < 0) || (x >= im->w) || (y < 0) || (y >= im->h))
     {
        color_return->red = 0;
        color_return->green = 0;
        color_return->blue = 0;
        color_return->alpha = 0;
        return;
     }
   p = im->data + (im->w * y) + x;
   color_return->red = ((*p) >> 16) & 0xff;
   color_return->green = ((*p) >> 8) & 0xff;
   color_return->blue = (*p) & 0xff;
   color_return->alpha = ((*p) >> 24) & 0xff;
}

/**
 * @param x The x coordinate of the pixel.
 * @param y The y coordinate of the pixel.
 * @param hue The returned hue channel.
 * @param saturation The returned saturation channel.
 * @param value The returned value channel.
 * @param alpha The returned alpha channel.
 *
 * Gets the HSVA color of the pixel from the current image that is at
 * the (@p x, @p y) location specified.
 **/
EAPI void
imlib_image_query_pixel_hsva(int x, int y, float *hue, float *saturation,
                             float *value, int *alpha)
{
   ImlibImage         *im;
   DATA32             *p;
   int                 r, g, b;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   if ((x < 0) || (x >= im->w) || (y < 0) || (y >= im->h))
     {
        *hue = 0;
        *saturation = 0;
        *value = 0;
        *alpha = 0;
        return;
     }
   p = im->data + (im->w * y) + x;
   r = ((*p) >> 16) & 0xff;
   g = ((*p) >> 8) & 0xff;
   b = (*p) & 0xff;
   *alpha = ((*p) >> 24) & 0xff;

   __imlib_rgb_to_hsv(r, g, b, hue, saturation, value);
}

/**
 * @param x The x coordinate of the pixel.
 * @param y The y coordinate of the pixel.
 * @param hue The returned hue channel.
 * @param lightness The returned lightness channel.
 * @param saturation The returned saturation channel.
 * @param alpha The returned alpha channel.
 *
 * Gets the HLSA color of the pixel from the current image that is at
 * the (@p x, @p y) location specified.
 **/
EAPI void
imlib_image_query_pixel_hlsa(int x, int y, float *hue, float *lightness,
                             float *saturation, int *alpha)
{
   ImlibImage         *im;
   DATA32             *p;
   int                 r, g, b;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   if ((x < 0) || (x >= im->w) || (y < 0) || (y >= im->h))
     {
        *hue = 0;
        *lightness = 0;
        *saturation = 0;
        *alpha = 0;
        return;
     }
   p = im->data + (im->w * y) + x;
   r = ((*p) >> 16) & 0xff;
   g = ((*p) >> 8) & 0xff;
   b = (*p) & 0xff;
   *alpha = ((*p) >> 24) & 0xff;

   __imlib_rgb_to_hls(r, g, b, hue, lightness, saturation);
}

/**
 * @param x Tthe x coordinate of the pixel.
 * @param y The y coordinate of the pixel.
 * @param cyan The returned cyan channel.
 * @param magenta The returned magenta channel.
 * @param yellow The returned yellow channel.
 * @param alpha The returned alpha channel.
 *
 * Gets the CMYA color of the pixel from the current image that is at
 * the (@p x, @p y) location specified.
 *
 **/
EAPI void
imlib_image_query_pixel_cmya(int x, int y, int *cyan, int *magenta, int *yellow,
                             int *alpha)
{
   ImlibImage         *im;
   DATA32             *p;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   if ((x < 0) || (x >= im->w) || (y < 0) || (y >= im->h))
     {
        *cyan = 0;
        *magenta = 0;
        *yellow = 0;
        *alpha = 0;
        return;
     }
   p = im->data + (im->w * y) + x;
   *cyan = 255 - (((*p) >> 16) & 0xff);
   *magenta = 255 - (((*p) >> 8) & 0xff);
   *yellow = 255 - ((*p) & 0xff);
   *alpha = ((*p) >> 24) & 0xff;
}

/**
 * @param key A string.
 * @param data A pointer.
 * @param value A value.
 * @param destructor_function An Imlib internal function.
 *
 * Attaches data to the current image with the string key of @p key, and
 * the data of @p data and an integer of @p value. The
 * @p destructor_function function, if not NULL is called when this
 * image is freed so the destructor can free @p data, if this is needed.
 **/
EAPI void
imlib_image_attach_data_value(const char *key, void *data, int value,
                              Imlib_Internal_Data_Destructor_Function
                              destructor_function)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("key", key);
   CAST_IMAGE(im, ctx->image);
   __imlib_AttachTag(im, key, value, data,
                     (ImlibDataDestructorFunction) destructor_function);
}

/**
 * @param key A string.
 * @return The attached data as a pointer, or NULL if none.
 *
 * Gets the data attached to the current image with the key @p key
 * specified. NULL is returned if no data could be found with that key
 * on the current image.
 *
 **/
EAPI void          *
imlib_image_get_attached_data(const char *key)
{
   ImlibImageTag      *t;
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, NULL);
   CHECK_PARAM_POINTER_RETURN("key", key, NULL);
   CAST_IMAGE(im, ctx->image);
   t = __imlib_GetTag(im, key);
   if (t)
      return t->data;
   return NULL;
}

/**
 * @param key A string.
 * @return The attached value as an integer, or 0 if none.
 *
 * Returns the value attached to the current image with the specified
 * key @p key. If none could be found 0 is returned.
 *
 **/
EAPI int
imlib_image_get_attached_value(const char *key)
{
   ImlibImageTag      *t;
   ImlibImage         *im;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, 0);
   CHECK_PARAM_POINTER_RETURN("key", key, 0);
   CAST_IMAGE(im, ctx->image);
   t = __imlib_GetTag(im, key);
   if (t)
      return t->val;
   return 0;
}

/**
 * @param key A string.
 *
 * Detaches the data & value attached with the specified key @p key from the
 * current image.
 **/
EAPI void
imlib_image_remove_attached_data_value(const char *key)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("key", key);
   CAST_IMAGE(im, ctx->image);
   __imlib_RemoveTag(im, key);
}

/**
 * @param key A string.
 *
 * Removes the data and value attached to the current image with the
 * specified key @p key and also calls the destructor function that was
 * supplied when attaching it (see imlib_image_attach_data_value()).
 **/
EAPI void
imlib_image_remove_and_free_attached_data_value(const char *key)
{
   ImlibImageTag      *t;
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("key", key);
   CAST_IMAGE(im, ctx->image);
   t = __imlib_RemoveTag(im, key);
   __imlib_FreeTag(im, t);
}

/**
 * @param filename The file name.
 *
 * Saves the current image in the format specified by the current
 * image's format settings to the filename @p filename.
 **/
EAPI void
imlib_save_image(const char *filename)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("filename", filename);
   CAST_IMAGE(im, ctx->image);

   if (__imlib_LoadImageData(im))
      return;

   __imlib_SaveImage(im, filename, (ImlibProgressFunction) ctx->progress_func,
                     ctx->progress_granularity, NULL);
}

/**
 * @param filename The file name.
 * @param error_return The returned error.
 *
 * Works the same way imlib_save_image() works, but will set the
 * @p error_return to an error value if the save fails.
 **/
EAPI void
imlib_save_image_with_error_return(const char *filename,
                                   Imlib_Load_Error * error_return)
{
   ImlibImage         *im;
   int                 er;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("filename", filename);
   CHECK_PARAM_POINTER("error_return", error_return);
   CAST_IMAGE(im, ctx->image);

   if (__imlib_LoadImageData(im))
      return;

   __imlib_SaveImage(im, filename, (ImlibProgressFunction) ctx->progress_func,
                     ctx->progress_granularity, &er);
   *error_return = er;
}

/**
 * @param angle An angle in radians.
 * @return A new image, or NULL.
 *
 * Creates an new copy of the current image, but rotated by @p angle
 * radians. On success it returns a valid image handle, otherwise
 * NULL.
 **/
EAPI                Imlib_Image
imlib_create_rotated_image(double angle)
{
   ImlibImage         *im, *im_old;
   int                 x, y, dx, dy, sz;
   double              x1, y1, d;

   CHECK_PARAM_POINTER_RETURN("image", ctx->image, NULL);
   CAST_IMAGE(im_old, ctx->image);
   if (__imlib_LoadImageData(im_old))
      return NULL;

   d = hypot((double)(im_old->w + 4), (double)(im_old->h + 4)) / sqrt(2.0);

   x1 = (double)(im_old->w) / 2.0 - sin(angle + atan(1.0)) * d;
   y1 = (double)(im_old->h) / 2.0 - cos(angle + atan(1.0)) * d;

   sz = (int)(d * sqrt(2.0));
   x = (int)(x1 * _ROTATE_PREC_MAX);
   y = (int)(y1 * _ROTATE_PREC_MAX);
   dx = (int)(cos(angle) * _ROTATE_PREC_MAX);
   dy = -(int)(sin(angle) * _ROTATE_PREC_MAX);

   if (!IMAGE_DIMENSIONS_OK(sz, sz))
      return NULL;

   im = __imlib_CreateImage(sz, sz, NULL);
   im->data = calloc(sz * sz, sizeof(DATA32));
   if (!(im->data))
     {
        __imlib_FreeImage(im);
        return NULL;
     }

   if (ctx->anti_alias)
     {
        __imlib_RotateAA(im_old->data, im->data, im_old->w, im_old->w,
                         im_old->h, im->w, sz, sz, x, y, dx, dy, -dy, dx);
     }
   else
     {
        __imlib_RotateSample(im_old->data, im->data, im_old->w, im_old->w,
                             im_old->h, im->w, sz, sz, x, y, dx, dy, -dy, dx);
     }
   SET_FLAG(im->flags, F_HAS_ALPHA);

   return (Imlib_Image) im;
}

void
imlib_rotate_image_from_buffer(double angle, Imlib_Image source_image)
{
   ImlibImage         *im, *im_old;
   int                 x, y, dx, dy, sz;
   double              x1, y1, d;

   // source image (to rotate)
   CHECK_PARAM_POINTER("source_image", source_image);
   CAST_IMAGE(im_old, source_image);

   // current context image
   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);

   if (__imlib_LoadImageData(im_old))
      return;

   d = hypot((double)(im_old->w + 4), (double)(im_old->h + 4)) / sqrt(2.0);

   x1 = (double)(im_old->w) / 2.0 - sin(angle + atan(1.0)) * d;
   y1 = (double)(im_old->h) / 2.0 - cos(angle + atan(1.0)) * d;

   sz = (int)(d * sqrt(2.0));
   x = (int)(x1 * _ROTATE_PREC_MAX);
   y = (int)(y1 * _ROTATE_PREC_MAX);
   dx = (int)(cos(angle) * _ROTATE_PREC_MAX);
   dy = -(int)(sin(angle) * _ROTATE_PREC_MAX);

   if ((im->w != im->h) || ((im->w < sz) && (im->h < sz)))
      return;                   // If size is wrong
   else
      sz = im->w;               // update sz with real width

#if 0                           /* Not necessary 'cause destination is context */
   im = __imlib_CreateImage(sz, sz, NULL);
   im->data = calloc(sz * sz, sizeof(DATA32));
   if (!(im->data))
     {
        __imlib_FreeImage(im);
        return;
     }
#endif

   if (ctx->anti_alias)
     {
        __imlib_RotateAA(im_old->data, im->data, im_old->w, im_old->w,
                         im_old->h, im->w, sz, sz, x, y, dx, dy, -dy, dx);
     }
   else
     {
        __imlib_RotateSample(im_old->data, im->data, im_old->w, im_old->w,
                             im_old->h, im->w, sz, sz, x, y, dx, dy, -dy, dx);
     }
   SET_FLAG(im->flags, F_HAS_ALPHA);

   return;
}

/**
 * @param source_image The image source.
 * @param merge_alpha A char.
 * @param source_x The source x coordinate.
 * @param source_y The source y coordinate.
 * @param source_width The source width.
 * @param source_height The source height.
 * @param destination_x The destination x coordinate.
 * @param destination_y The destination y coordinate.
 * @param angle_x An angle.
 * @param angle_y An angle.
 *
 * Works just like imlib_blend_image_onto_image_skewed() except you
 * cannot skew an image (@p v_angle_x and @p v_angle_y are 0).
 **/
EAPI void
imlib_blend_image_onto_image_at_angle(Imlib_Image source_image,
                                      char merge_alpha, int source_x,
                                      int source_y, int source_width,
                                      int source_height, int destination_x,
                                      int destination_y, int angle_x,
                                      int angle_y)
{
   ImlibImage         *im_src, *im_dst;

   CHECK_PARAM_POINTER("source_image", source_image);
   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im_src, source_image);
   CAST_IMAGE(im_dst, ctx->image);
   if (__imlib_LoadImageData(im_src))
      return;
   if (__imlib_LoadImageData(im_dst))
      return;
   __imlib_DirtyImage(im_dst);
   __imlib_BlendImageToImageSkewed(im_src, im_dst, ctx->anti_alias,
                                   ctx->blend, merge_alpha, source_x,
                                   source_y, source_width, source_height,
                                   destination_x, destination_y, angle_x,
                                   angle_y, 0, 0, ctx->color_modifier,
                                   ctx->operation,
                                   ctx->cliprect.x, ctx->cliprect.y,
                                   ctx->cliprect.w, ctx->cliprect.h);
}

/**
 * @param source_image The source image.
 * @param merge_alpha A char
 * @param source_x The source x coordinate.
 * @param source_y The source y coordinate.
 * @param source_width The source width.
 * @param source_height The source height.
 * @param destination_x The destination x coordinate.
 * @param destination_y The destination y coordinate.
 * @param h_angle_x An angle.
 * @param h_angle_y An angle.
 * @param v_angle_x An angle.
 * @param v_angle_y An angle.
 *
 * Blends the source rectangle (@p source_x, @p source_y, @p source_width,
 * @p source_height) from the
 * @p source_image onto the current image at the destination
 * (@p destination_x, @p destination_y)
 * location. It will be rotated and scaled so that the upper right
 * corner will be positioned @p h_angle_x pixels to the right (or left,
 * if negative) and @p h_angle_y pixels down (from (@p destination_x,
 * @p destination_y). If
 * @p v_angle_x and @p v_angle_y are not 0, the image will also be skewed so
 * that the lower left corner will be positioned @p v_angle_x pixels to
 * the right and @p v_angle_y pixels down. The at_angle versions simply
 * have the @p v_angle_x and @p v_angle_y set to 0 so the rotation doesn't
 * get skewed, and the render_..._on_drawable ones seem obvious
 * enough; they do the same on a drawable.
 *
 * Example:
 * @code
 * imlib_blend_image_onto_image_skewed(..., 0, 0, 100, 0, 0, 100);
 * @endcode
 * will simply scale the image to be 100x100.
 * @code
 * imlib_blend_image_onto_image_skewed(..., 0, 0, 0, 100, 100, 0);
 * @endcode
 * will scale the image to be 100x100, and flip it diagonally.
 * @code
 * imlib_blend_image_onto_image_skewed(..., 100, 0, 0, 100, -100, 0);
 * @endcode
 * will scale the image and rotate it 90 degrees clockwise.
 * @code
 * imlib_blend_image_onto_image_skewed(..., 50, 0, 50, 50, -50, 50);
 * @endcode
 * will rotate the image 45 degrees clockwise, and will scale it so
 * its corners are at (50,0)-(100,50)-(50,100)-(0,50) i.e. it fits
 * into the 100x100 square, so it's scaled down to 70.7% (sqrt(2)/2).
 * @code
 * imlib_blend_image_onto_image_skewed(..., 50, 50, 100 * cos(a), 100 * sin(a), 0);
 * @endcode
 * will rotate the image `a' degrees, with its upper left corner at (50,50).
 **/
EAPI void
imlib_blend_image_onto_image_skewed(Imlib_Image source_image,
                                    char merge_alpha, int source_x,
                                    int source_y, int source_width,
                                    int source_height, int destination_x,
                                    int destination_y, int h_angle_x,
                                    int h_angle_y, int v_angle_x, int v_angle_y)
{
   ImlibImage         *im_src, *im_dst;

   CHECK_PARAM_POINTER("source_image", source_image);
   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im_src, source_image);
   CAST_IMAGE(im_dst, ctx->image);
   if (__imlib_LoadImageData(im_src))
      return;
   if (__imlib_LoadImageData(im_dst))
      return;
   __imlib_DirtyImage(im_dst);
   __imlib_BlendImageToImageSkewed(im_src, im_dst, ctx->anti_alias,
                                   ctx->blend, merge_alpha, source_x,
                                   source_y, source_width, source_height,
                                   destination_x, destination_y, h_angle_x,
                                   h_angle_y, v_angle_x, v_angle_y,
                                   ctx->color_modifier, ctx->operation,
                                   ctx->cliprect.x, ctx->cliprect.y,
                                   ctx->cliprect.w, ctx->cliprect.h);
}

#ifdef BUILD_X11
/**
 * @param source_x The source x coordinate.
 * @param source_y The source y coordinate.
 * @param source_width The source width.
 * @param source_height The source height.
 * @param destination_x The destination x coordinate.
 * @param destination_y The destination y coordinate.
 * @param h_angle_x An angle.
 * @param h_angle_y An angle.
 * @param v_angle_x An angle.
 * @param v_angle_y An angle.
 *
 *
 * Works just like imlib_blend_image_onto_image_skewed(), except it
 * blends the image onto the current drawable instead of the current
 * image.
 **/
EAPI void
imlib_render_image_on_drawable_skewed(int source_x, int source_y,
                                      int source_width, int source_height,
                                      int destination_x, int destination_y,
                                      int h_angle_x, int h_angle_y,
                                      int v_angle_x, int v_angle_y)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_RenderImageSkewed(ctx->display, im, ctx->drawable, ctx->mask,
                             ctx->visual, ctx->colormap, ctx->depth, source_x,
                             source_y, source_width, source_height,
                             destination_x, destination_y, h_angle_x,
                             h_angle_y, v_angle_x, v_angle_y, ctx->anti_alias,
                             ctx->dither, ctx->blend, ctx->dither_mask,
                             ctx->mask_alpha_threshold, ctx->color_modifier,
                             ctx->operation);
}

/**
 * @param source_x The source x coordinate.
 * @param source_y The source y coordinate.
 * @param source_width The source width.
 * @param source_height The source height.
 * @param destination_x The destination x coordinate.
 * @param destination_y The destination y coordinate.
 * @param angle_x An angle.
 * @param angle_y An angle.
 *
 *
 * Works just like imlib_render_image_on_drawable_skewed() except you
 * cannot skew an image (@p v_angle_x and @p v_angle_y are 0).
 **/
EAPI void
imlib_render_image_on_drawable_at_angle(int source_x, int source_y,
                                        int source_width, int source_height,
                                        int destination_x, int destination_y,
                                        int angle_x, int angle_y)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_RenderImageSkewed(ctx->display, im, ctx->drawable, ctx->mask,
                             ctx->visual, ctx->colormap, ctx->depth, source_x,
                             source_y, source_width, source_height,
                             destination_x, destination_y, angle_x, angle_y,
                             0, 0, ctx->anti_alias, ctx->dither, ctx->blend,
                             ctx->dither_mask, ctx->mask_alpha_threshold,
                             ctx->color_modifier, ctx->operation);
}
#endif

EAPI void
imlib_image_filter(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CHECK_PARAM_POINTER("filter", ctx->filter);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_FilterImage(im, (ImlibFilter *) ctx->filter);
}

EAPI                Imlib_Filter
imlib_create_filter(int initsize)
{
   return (Imlib_Filter) __imlib_CreateFilter(initsize);
}

EAPI void
imlib_free_filter(void)
{
   CHECK_PARAM_POINTER("filter", ctx->filter);
   __imlib_FreeFilter((ImlibFilter *) ctx->filter);
   ctx->filter = NULL;
}

/**
 * @param filter Current filter.
 *
 * Sets the current filter to be used when applying filters to
 * images. Set this to NULL to disable filters.
 */
EAPI void
imlib_context_set_filter(Imlib_Filter filter)
{
   ctx->filter = filter;
}

/**
 * @return
 *
 * Gets the current context image filter.
 */
EAPI                Imlib_Filter
imlib_context_get_filter(void)
{
   return ctx->filter;
}

EAPI void
imlib_filter_set(int xoff, int yoff, int a, int r, int g, int b)
{
   ImlibFilter        *fil;

   CHECK_PARAM_POINTER("filter", ctx->filter);
   fil = (ImlibFilter *) ctx->filter;
   __imlib_FilterSetColor(&fil->alpha, xoff, yoff, a, 0, 0, 0);
   __imlib_FilterSetColor(&fil->red, xoff, yoff, 0, r, 0, 0);
   __imlib_FilterSetColor(&fil->green, xoff, yoff, 0, 0, g, 0);
   __imlib_FilterSetColor(&fil->blue, xoff, yoff, 0, 0, 0, b);
}

EAPI void
imlib_filter_set_alpha(int xoff, int yoff, int a, int r, int g, int b)
{
   ImlibFilter        *fil;

   CHECK_PARAM_POINTER("filter", ctx->filter);
   fil = (ImlibFilter *) ctx->filter;
   __imlib_FilterSetColor(&fil->alpha, xoff, yoff, a, r, g, b);
}

EAPI void
imlib_filter_set_red(int xoff, int yoff, int a, int r, int g, int b)
{
   ImlibFilter        *fil;

   CHECK_PARAM_POINTER("filter", ctx->filter);
   fil = (ImlibFilter *) ctx->filter;
   __imlib_FilterSetColor(&fil->red, xoff, yoff, a, r, g, b);
}

EAPI void
imlib_filter_set_green(int xoff, int yoff, int a, int r, int g, int b)
{
   ImlibFilter        *fil;

   CHECK_PARAM_POINTER("filter", ctx->filter);
   fil = (ImlibFilter *) ctx->filter;
   __imlib_FilterSetColor(&fil->green, xoff, yoff, a, r, g, b);
}

EAPI void
imlib_filter_set_blue(int xoff, int yoff, int a, int r, int g, int b)
{
   ImlibFilter        *fil;

   CHECK_PARAM_POINTER("filter", ctx->filter);
   fil = (ImlibFilter *) ctx->filter;
   __imlib_FilterSetColor(&fil->blue, xoff, yoff, a, r, g, b);
}

EAPI void
imlib_filter_constants(int a, int r, int g, int b)
{
   CHECK_PARAM_POINTER("filter", ctx->filter);
   __imlib_FilterConstants((ImlibFilter *) ctx->filter, a, r, g, b);
}

EAPI void
imlib_filter_divisors(int a, int r, int g, int b)
{
   CHECK_PARAM_POINTER("filter", ctx->filter);
   __imlib_FilterDivisors((ImlibFilter *) ctx->filter, a, r, g, b);
}

EAPI void
imlib_apply_filter(const char *script, ...)
{
   va_list             param_list;
   ImlibImage         *im;

   __imlib_dynamic_filters_init();
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   va_start(param_list, script);
   __imlib_script_parse(im, script, param_list);
   va_end(param_list);
}

/**
 * Returns a new polygon object with no points set.
 **/
EAPI                ImlibPolygon
imlib_polygon_new(void)
{
   return (ImlibPolygon) __imlib_polygon_new();
}

/**
 * @param poly A polygon
 * @param x The X coordinate.
 * @param y The Y coordinate.
 *
 * Adds the point (@p x, @p y) to the polygon @p poly. The point will be added
 * to the end of the polygon's internal point list. The points are
 * drawn in order, from the first to the last.
 **/
EAPI void
imlib_polygon_add_point(ImlibPolygon poly, int x, int y)
{
   CHECK_PARAM_POINTER("polygon", poly);
   __imlib_polygon_add_point((ImlibPoly *) poly, x, y);
}

/**
 * @param poly A polygon.
 *
 * Frees a polygon object.
 **/
EAPI void
imlib_polygon_free(ImlibPolygon poly)
{
   CHECK_PARAM_POINTER("polygon", poly);
   __imlib_polygon_free((ImlibPoly *) poly);
}

/**
 * @param poly A polygon.
 * @param closed Closed polygon flag.
 *
 * Draws the polygon @p poly onto the current context image. Points which have
 * been added to the polygon are drawn in sequence, first to last. The
 * final point will be joined with the first point if @p closed is
 * non-zero.
 **/
EAPI void
imlib_image_draw_polygon(ImlibPolygon poly, unsigned char closed)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_Polygon_DrawToImage((ImlibPoly *) poly, closed, ctx->pixel,
                               im, ctx->cliprect.x, ctx->cliprect.y,
                               ctx->cliprect.w, ctx->cliprect.h,
                               ctx->operation, ctx->blend, ctx->anti_alias);
}

/**
 * @param poly A polygon.
 *
 * Fills the area defined by the polygon @p polyon the current context image
 * with the current context color.
 **/
EAPI void
imlib_image_fill_polygon(ImlibPolygon poly)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_Polygon_FillToImage((ImlibPoly *) poly, ctx->pixel,
                               im, ctx->cliprect.x, ctx->cliprect.y,
                               ctx->cliprect.w, ctx->cliprect.h,
                               ctx->operation, ctx->blend, ctx->anti_alias);
}

/**
 * @param poly A polygon.
 * @param px1 X coordinate of the upper left corner.
 * @param py1 Y coordinate of the upper left corner.
 * @param px2 X coordinate of the lower right corner.
 * @param py2 Y coordinate of the lower right corner.
 *
 * Calculates the bounding area of the polygon @p poly. (@p px1, @p py1) defines the
 * upper left corner of the bounding box and (@p px2, @p py2) defines it's
 * lower right corner.
 **/
EAPI void
imlib_polygon_get_bounds(ImlibPolygon poly, int *px1, int *py1, int *px2,
                         int *py2)
{
   CHECK_PARAM_POINTER("polygon", poly);
   __imlib_polygon_get_bounds((ImlibPoly *) poly, px1, py1, px2, py2);
}

/**
 * @param xc X coordinate of the center of the ellipse.
 * @param yc Y coordinate of the center of the ellipse.
 * @param a The horizontal amplitude of the ellipse.
 * @param b The vertical amplitude of the ellipse.
 *
 * Draws an ellipse on the current context image. The ellipse is
 * defined as (@p x-@p xc)^2/@p a^2 + (@p y-@p yc)^2/@p b^2 = 1. This means that the
 * point (@p xc, @p yc) marks the center of the ellipse, @p a defines the
 * horizontal amplitude of the ellipse, and @p b defines the vertical
 * amplitude.
 **/
EAPI void
imlib_image_draw_ellipse(int xc, int yc, int a, int b)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_Ellipse_DrawToImage(xc, yc, a, b, ctx->pixel,
                               im, ctx->cliprect.x, ctx->cliprect.y,
                               ctx->cliprect.w, ctx->cliprect.h,
                               ctx->operation, ctx->blend, ctx->anti_alias);
}

/**
 * @param xc X coordinate of the center of the ellipse.
 * @param yc Y coordinate of the center of the ellipse.
 * @param a The horizontal amplitude of the ellipse.
 * @param b The vertical amplitude of the ellipse.
 *
 * Fills an ellipse on the current context image using the current
 * context color. The ellipse is
 * defined as (@p x-@p xc)^2/@p a^2 + (@p y-@p yc)^2/@p b^2 = 1. This means that the
 * point (@p xc, @p yc) marks the center of the ellipse, @p a defines the
 * horizontal amplitude of the ellipse, and @p b defines the vertical
 * amplitude.
 **/
EAPI void
imlib_image_fill_ellipse(int xc, int yc, int a, int b)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   __imlib_Ellipse_FillToImage(xc, yc, a, b, ctx->pixel,
                               im, ctx->cliprect.x, ctx->cliprect.y,
                               ctx->cliprect.w, ctx->cliprect.h,
                               ctx->operation, ctx->blend, ctx->anti_alias);
}

/**
 * @param poly A polygon
 * @param x The X coordinate.
 * @param y The Y coordinate.
 *
 * Returns non-zero if the point (@p x, @p y) is within the area defined by
 * the polygon @p poly.
 **/
EAPI unsigned char
imlib_polygon_contains_point(ImlibPolygon poly, int x, int y)
{
   CHECK_PARAM_POINTER_RETURN("polygon", poly, 0);
   return __imlib_polygon_contains_point((ImlibPoly *) poly, x, y);
}

EAPI void
imlib_image_clear(void)
{
   ImlibImage         *im;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   memset(im->data, 0, im->w * im->h * sizeof(DATA32));
}

EAPI void
imlib_image_clear_color(int r, int g, int b, int a)
{
   ImlibImage         *im;
   int                 i, max;
   DATA32              col;

   CHECK_PARAM_POINTER("image", ctx->image);
   CAST_IMAGE(im, ctx->image);
   if (__imlib_LoadImageData(im))
      return;
   __imlib_DirtyImage(im);
   max = im->w * im->h;
   col = PIXEL_ARGB(a, r, g, b);
   for (i = 0; i < max; i++)
      im->data[i] = col;
}