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
|
/*
* The Python Imaging Library.
*
* the imaging library bindings
*
* history:
* 1995-09-24 fl Created
* 1996-03-24 fl Ready for first public release (release 0.0)
* 1996-03-25 fl Added fromstring (for Jack's "img" library)
* 1996-03-28 fl Added channel operations
* 1996-03-31 fl Added point operation
* 1996-04-08 fl Added new/new_block/new_array factories
* 1996-04-13 fl Added decoders
* 1996-05-04 fl Added palette hack
* 1996-05-12 fl Compile cleanly as C++
* 1996-05-19 fl Added matrix conversions, gradient fills
* 1996-05-27 fl Added display_mode
* 1996-07-22 fl Added getbbox, offset
* 1996-07-23 fl Added sequence semantics
* 1996-08-13 fl Added logical operators, point mode
* 1996-08-16 fl Modified paste interface
* 1996-09-06 fl Added putdata methods, use abstract interface
* 1996-11-01 fl Added xbm encoder
* 1996-11-04 fl Added experimental path stuff, draw_lines, etc
* 1996-12-10 fl Added zip decoder, crc32 interface
* 1996-12-14 fl Added modulo arithmetics
* 1996-12-29 fl Added zip encoder
* 1997-01-03 fl Added fli and msp decoders
* 1997-01-04 fl Added experimental sun_rle and tga_rle decoders
* 1997-01-05 fl Added gif encoder, getpalette hack
* 1997-02-23 fl Added histogram mask
* 1997-05-12 fl Minor tweaks to match the IFUNC95 interface
* 1997-05-21 fl Added noise generator, spread effect
* 1997-06-05 fl Added mandelbrot generator
* 1997-08-02 fl Modified putpalette to coerce image mode if necessary
* 1998-01-11 fl Added INT32 support
* 1998-01-22 fl Fixed draw_points to draw the last point too
* 1998-06-28 fl Added getpixel, getink, draw_ink
* 1998-07-12 fl Added getextrema
* 1998-07-17 fl Added point conversion to arbitrary formats
* 1998-09-21 fl Added support for resampling filters
* 1998-09-22 fl Added support for quad transform
* 1998-12-29 fl Added support for arcs, chords, and pieslices
* 1999-01-10 fl Added some experimental arrow graphics stuff
* 1999-02-06 fl Added draw_bitmap, font acceleration stuff
* 2001-04-17 fl Fixed some egcs compiler nits
* 2001-09-17 fl Added screen grab primitives (win32)
* 2002-03-09 fl Added stretch primitive
* 2002-03-10 fl Fixed filter handling in rotate
* 2002-06-06 fl Added I, F, and RGB support to putdata
* 2002-06-08 fl Added rankfilter
* 2002-06-09 fl Added support for user-defined filter kernels
* 2002-11-19 fl Added clipboard grab primitives (win32)
* 2002-12-11 fl Added draw context
* 2003-04-26 fl Tweaks for Python 2.3 beta 1
* 2003-05-21 fl Added createwindow primitive (win32)
* 2003-09-13 fl Added thread section hooks
* 2003-09-15 fl Added expand helper
* 2003-09-26 fl Added experimental LA support
* 2004-02-21 fl Handle zero-size images in quantize
* 2004-06-05 fl Added ptr attribute (used to access Imaging objects)
* 2004-06-05 fl Don't crash when fetching pixels from zero-wide images
* 2004-09-17 fl Added getcolors
* 2004-10-04 fl Added modefilter
* 2005-10-02 fl Added access proxy
* 2006-06-18 fl Always draw last point in polyline
*
* Copyright (c) 1997-2006 by Secret Labs AB
* Copyright (c) 1995-2006 by Fredrik Lundh
*
* See the README file for information on usage and redistribution.
*/
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#ifdef HAVE_LIBJPEG
#include "jconfig.h"
#endif
#ifdef HAVE_LIBZ
#include "zlib.h"
#endif
#ifdef HAVE_LIBTIFF
#ifndef _TIFFIO_
#include <tiffio.h>
#endif
#endif
#include "libImaging/Imaging.h"
#define _USE_MATH_DEFINES
#include <math.h>
/* Configuration stuff. Feel free to undef things you don't need. */
#define WITH_IMAGECHOPS /* ImageChops support */
#define WITH_IMAGEDRAW /* ImageDraw support */
#define WITH_MAPPING /* use memory mapping to read some file formats */
#define WITH_IMAGEPATH /* ImagePath stuff */
#define WITH_ARROW /* arrow graphics stuff (experimental) */
#define WITH_EFFECTS /* special effects */
#define WITH_QUANTIZE /* quantization support */
#define WITH_RANKFILTER /* rank filter */
#define WITH_MODEFILTER /* mode filter */
#define WITH_THREADING /* "friendly" threading support */
#define WITH_UNSHARPMASK /* Kevin Cazabon's unsharpmask module */
#undef VERBOSE
#define B16(p, i) ((((int)p[(i)]) << 8) + p[(i) + 1])
#define L16(p, i) ((((int)p[(i) + 1]) << 8) + p[(i)])
#define S16(v) ((v) < 32768 ? (v) : ((v)-65536))
/* -------------------------------------------------------------------- */
/* OBJECT ADMINISTRATION */
/* -------------------------------------------------------------------- */
typedef struct {
PyObject_HEAD Imaging image;
ImagingAccess access;
} ImagingObject;
static PyTypeObject Imaging_Type;
#ifdef WITH_IMAGEDRAW
typedef struct {
/* to write a character, cut out sxy from glyph data, place
at current position plus dxy, and advance by (dx, dy) */
int dx, dy;
int dx0, dy0, dx1, dy1;
int sx0, sy0, sx1, sy1;
} Glyph;
typedef struct {
PyObject_HEAD ImagingObject *ref;
Imaging bitmap;
int ysize;
int baseline;
Glyph glyphs[256];
} ImagingFontObject;
static PyTypeObject ImagingFont_Type;
typedef struct {
PyObject_HEAD ImagingObject *image;
UINT8 ink[4];
int blend;
} ImagingDrawObject;
static PyTypeObject ImagingDraw_Type;
#endif
typedef struct {
PyObject_HEAD ImagingObject *image;
int readonly;
} PixelAccessObject;
static PyTypeObject PixelAccess_Type;
PyObject *
PyImagingNew(Imaging imOut) {
ImagingObject *imagep;
if (!imOut) {
return NULL;
}
imagep = PyObject_New(ImagingObject, &Imaging_Type);
if (imagep == NULL) {
ImagingDelete(imOut);
return NULL;
}
#ifdef VERBOSE
printf("imaging %p allocated\n", imagep);
#endif
imagep->image = imOut;
imagep->access = ImagingAccessNew(imOut);
return (PyObject *)imagep;
}
static void
_dealloc(ImagingObject *imagep) {
#ifdef VERBOSE
printf("imaging %p deleted\n", imagep);
#endif
if (imagep->access) {
ImagingAccessDelete(imagep->image, imagep->access);
}
ImagingDelete(imagep->image);
PyObject_Del(imagep);
}
#define PyImaging_Check(op) (Py_TYPE(op) == &Imaging_Type)
Imaging
PyImaging_AsImaging(PyObject *op) {
if (!PyImaging_Check(op)) {
PyErr_BadInternalCall();
return NULL;
}
return ((ImagingObject *)op)->image;
}
/* -------------------------------------------------------------------- */
/* THREAD HANDLING */
/* -------------------------------------------------------------------- */
void
ImagingSectionEnter(ImagingSectionCookie *cookie) {
#ifdef WITH_THREADING
*cookie = (PyThreadState *)PyEval_SaveThread();
#endif
}
void
ImagingSectionLeave(ImagingSectionCookie *cookie) {
#ifdef WITH_THREADING
PyEval_RestoreThread((PyThreadState *)*cookie);
#endif
}
/* -------------------------------------------------------------------- */
/* BUFFER HANDLING */
/* -------------------------------------------------------------------- */
/* Python compatibility API */
int
PyImaging_CheckBuffer(PyObject *buffer) {
return PyObject_CheckBuffer(buffer);
}
int
PyImaging_GetBuffer(PyObject *buffer, Py_buffer *view) {
/* must call check_buffer first! */
return PyObject_GetBuffer(buffer, view, PyBUF_SIMPLE);
}
/* -------------------------------------------------------------------- */
/* EXCEPTION REROUTING */
/* -------------------------------------------------------------------- */
/* error messages */
static const char *must_be_sequence = "argument must be a sequence";
static const char *must_be_two_coordinates =
"coordinate list must contain exactly 2 coordinates";
static const char *incorrectly_ordered_x_coordinate =
"x1 must be greater than or equal to x0";
static const char *incorrectly_ordered_y_coordinate =
"y1 must be greater than or equal to y0";
static const char *wrong_mode = "unrecognized image mode";
static const char *wrong_raw_mode = "unrecognized raw mode";
static const char *outside_image = "image index out of range";
static const char *outside_palette = "palette index out of range";
static const char *wrong_palette_size = "invalid palette size";
static const char *no_palette = "image has no palette";
static const char *readonly = "image is readonly";
/* static const char* no_content = "image has no content"; */
void *
ImagingError_OSError(void) {
PyErr_SetString(PyExc_OSError, "error when accessing file");
return NULL;
}
void *
ImagingError_MemoryError(void) {
return PyErr_NoMemory();
}
void *
ImagingError_Mismatch(void) {
PyErr_SetString(PyExc_ValueError, "images do not match");
return NULL;
}
void *
ImagingError_ModeError(void) {
PyErr_SetString(PyExc_ValueError, "image has wrong mode");
return NULL;
}
void *
ImagingError_ValueError(const char *message) {
PyErr_SetString(
PyExc_ValueError, (message) ? (char *)message : "unrecognized argument value");
return NULL;
}
void
ImagingError_Clear(void) {
PyErr_Clear();
}
/* -------------------------------------------------------------------- */
/* HELPERS */
/* -------------------------------------------------------------------- */
static int
getbands(const char *mode) {
Imaging im;
int bands;
/* FIXME: add primitive to libImaging to avoid extra allocation */
im = ImagingNew(mode, 0, 0);
if (!im) {
return -1;
}
bands = im->bands;
ImagingDelete(im);
return bands;
}
#define TYPE_UINT8 (0x100 | sizeof(UINT8))
#define TYPE_INT32 (0x200 | sizeof(INT32))
#define TYPE_FLOAT16 (0x500 | sizeof(FLOAT16))
#define TYPE_FLOAT32 (0x300 | sizeof(FLOAT32))
#define TYPE_DOUBLE (0x400 | sizeof(double))
static void *
getlist(PyObject *arg, Py_ssize_t *length, const char *wrong_length, int type) {
/* - allocates and returns a c array of the items in the
python sequence arg.
- the size of the returned array is in length
- all of the arg items must be numeric items of the type
specified in type
- sequence length is checked against the length parameter IF
an error parameter is passed in wrong_length
- caller is responsible for freeing the memory
*/
Py_ssize_t i, n;
int itemp;
double dtemp;
FLOAT32 ftemp;
UINT8 *list;
PyObject *seq;
PyObject *op;
if (!PySequence_Check(arg)) {
PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL;
}
n = PySequence_Size(arg);
if (length && wrong_length && n != *length) {
PyErr_SetString(PyExc_ValueError, wrong_length);
return NULL;
}
/* malloc check ok, type & ff is just a sizeof(something)
calloc checks for overflow */
list = calloc(n, type & 0xff);
if (!list) {
return ImagingError_MemoryError();
}
seq = PySequence_Fast(arg, must_be_sequence);
if (!seq) {
free(list);
return NULL;
}
for (i = 0; i < n; i++) {
op = PySequence_Fast_GET_ITEM(seq, i);
// DRY, branch prediction is going to work _really_ well
// on this switch. And 3 fewer loops to copy/paste.
switch (type) {
case TYPE_UINT8:
itemp = PyLong_AsLong(op);
list[i] = CLIP8(itemp);
break;
case TYPE_INT32:
itemp = PyLong_AsLong(op);
memcpy(list + i * sizeof(INT32), &itemp, sizeof(itemp));
break;
case TYPE_FLOAT32:
ftemp = (FLOAT32)PyFloat_AsDouble(op);
memcpy(list + i * sizeof(ftemp), &ftemp, sizeof(ftemp));
break;
case TYPE_DOUBLE:
dtemp = PyFloat_AsDouble(op);
memcpy(list + i * sizeof(dtemp), &dtemp, sizeof(dtemp));
break;
}
}
Py_DECREF(seq);
if (PyErr_Occurred()) {
free(list);
return NULL;
}
if (length) {
*length = n;
}
return list;
}
FLOAT32
float16tofloat32(const FLOAT16 in) {
UINT32 t1;
UINT32 t2;
UINT32 t3;
FLOAT32 out[1] = {0};
t1 = in & 0x7fff; // Non-sign bits
t2 = in & 0x8000; // Sign bit
t3 = in & 0x7c00; // Exponent
t1 <<= 13; // Align mantissa on MSB
t2 <<= 16; // Shift sign bit into position
t1 += 0x38000000; // Adjust bias
t1 = (t3 == 0 ? 0 : t1); // Denormals-as-zero
t1 |= t2; // Re-insert sign bit
memcpy(out, &t1, 4);
return out[0];
}
static inline PyObject *
getpixel(Imaging im, ImagingAccess access, int x, int y) {
union {
UINT8 b[4];
UINT16 h;
INT32 i;
FLOAT32 f;
} pixel;
if (x < 0) {
x = im->xsize + x;
}
if (y < 0) {
y = im->ysize + y;
}
if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
PyErr_SetString(PyExc_IndexError, outside_image);
return NULL;
}
access->get_pixel(im, x, y, &pixel);
switch (im->type) {
case IMAGING_TYPE_UINT8:
switch (im->bands) {
case 1:
return PyLong_FromLong(pixel.b[0]);
case 2:
return Py_BuildValue("BB", pixel.b[0], pixel.b[1]);
case 3:
return Py_BuildValue("BBB", pixel.b[0], pixel.b[1], pixel.b[2]);
case 4:
return Py_BuildValue(
"BBBB", pixel.b[0], pixel.b[1], pixel.b[2], pixel.b[3]);
}
break;
case IMAGING_TYPE_INT32:
return PyLong_FromLong(pixel.i);
case IMAGING_TYPE_FLOAT32:
return PyFloat_FromDouble(pixel.f);
case IMAGING_TYPE_SPECIAL:
if (im->bands == 1) {
return PyLong_FromLong(pixel.h);
} else {
return Py_BuildValue("BBB", pixel.b[0], pixel.b[1], pixel.b[2]);
}
break;
}
/* unknown type */
Py_INCREF(Py_None);
return Py_None;
}
static char *
getink(PyObject *color, Imaging im, char *ink) {
int g = 0, b = 0, a = 0;
double f = 0;
/* Windows 64 bit longs are 32 bits, and 0xFFFFFFFF (white) is a
Python long (not int) that raises an overflow error when trying
to return it into a 32 bit C long
*/
PY_LONG_LONG r = 0;
FLOAT32 ftmp;
INT32 itmp;
/* fill ink buffer (four bytes) with something that can
be cast to either UINT8 or INT32 */
int rIsInt = 0;
int tupleSize = PyTuple_Check(color) ? PyTuple_GET_SIZE(color) : -1;
if (tupleSize == 1) {
color = PyTuple_GetItem(color, 0);
}
if (im->type == IMAGING_TYPE_UINT8 || im->type == IMAGING_TYPE_INT32 ||
im->type == IMAGING_TYPE_SPECIAL) {
if (PyLong_Check(color)) {
r = PyLong_AsLongLong(color);
if (r == -1 && PyErr_Occurred()) {
return NULL;
}
rIsInt = 1;
} else if (im->bands == 1) {
PyErr_SetString(
PyExc_TypeError, "color must be int or single-element tuple");
return NULL;
} else if (tupleSize == -1) {
PyErr_SetString(PyExc_TypeError, "color must be int or tuple");
return NULL;
}
}
switch (im->type) {
case IMAGING_TYPE_UINT8:
/* unsigned integer */
if (im->bands == 1) {
/* unsigned integer, single layer */
if (rIsInt != 1) {
if (tupleSize != 1) {
PyErr_SetString(PyExc_TypeError, "color must be int or single-element tuple");
return NULL;
} else if (!PyArg_ParseTuple(color, "L", &r)) {
return NULL;
}
}
ink[0] = (char)CLIP8(r);
ink[1] = ink[2] = ink[3] = 0;
} else {
if (rIsInt) {
/* compatibility: ABGR */
a = (UINT8)(r >> 24);
b = (UINT8)(r >> 16);
g = (UINT8)(r >> 8);
r = (UINT8)r;
} else {
a = 255;
if (im->bands == 2) {
if (tupleSize != 1 && tupleSize != 2) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one or two elements");
return NULL;
} else if (!PyArg_ParseTuple(color, "L|i", &r, &a)) {
return NULL;
}
g = b = r;
} else {
if (tupleSize != 3 && tupleSize != 4) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one, three or four elements");
return NULL;
} else if (!PyArg_ParseTuple(color, "Lii|i", &r, &g, &b, &a)) {
return NULL;
}
}
}
ink[0] = (char)CLIP8(r);
ink[1] = (char)CLIP8(g);
ink[2] = (char)CLIP8(b);
ink[3] = (char)CLIP8(a);
}
return ink;
case IMAGING_TYPE_INT32:
/* signed integer */
itmp = r;
memcpy(ink, &itmp, sizeof(itmp));
return ink;
case IMAGING_TYPE_FLOAT32:
/* floating point */
f = PyFloat_AsDouble(color);
if (f == -1.0 && PyErr_Occurred()) {
return NULL;
}
ftmp = f;
memcpy(ink, &ftmp, sizeof(ftmp));
return ink;
case IMAGING_TYPE_SPECIAL:
if (strncmp(im->mode, "I;16", 4) == 0) {
ink[0] = (UINT8)r;
ink[1] = (UINT8)(r >> 8);
ink[2] = ink[3] = 0;
return ink;
} else {
if (rIsInt) {
b = (UINT8)(r >> 16);
g = (UINT8)(r >> 8);
r = (UINT8)r;
} else if (tupleSize != 3) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one or three elements");
return NULL;
} else if (!PyArg_ParseTuple(color, "iiL", &b, &g, &r)) {
return NULL;
}
if (!strcmp(im->mode, "BGR;15")) {
UINT16 v = ((((UINT16)r) << 7) & 0x7c00) +
((((UINT16)g) << 2) & 0x03e0) +
((((UINT16)b) >> 3) & 0x001f);
ink[0] = (UINT8)v;
ink[1] = (UINT8)(v >> 8);
ink[2] = ink[3] = 0;
return ink;
} else if (!strcmp(im->mode, "BGR;16")) {
UINT16 v = ((((UINT16)r) << 8) & 0xf800) +
((((UINT16)g) << 3) & 0x07e0) +
((((UINT16)b) >> 3) & 0x001f);
ink[0] = (UINT8)v;
ink[1] = (UINT8)(v >> 8);
ink[2] = ink[3] = 0;
return ink;
} else if (!strcmp(im->mode, "BGR;24")) {
ink[0] = (UINT8)b;
ink[1] = (UINT8)g;
ink[2] = (UINT8)r;
ink[3] = 0;
return ink;
}
}
}
PyErr_SetString(PyExc_ValueError, wrong_mode);
return NULL;
}
/* -------------------------------------------------------------------- */
/* FACTORIES */
/* -------------------------------------------------------------------- */
static PyObject *
_fill(PyObject *self, PyObject *args) {
char *mode;
int xsize, ysize;
PyObject *color;
char buffer[4];
Imaging im;
xsize = ysize = 256;
color = NULL;
if (!PyArg_ParseTuple(args, "s|(ii)O", &mode, &xsize, &ysize, &color)) {
return NULL;
}
im = ImagingNewDirty(mode, xsize, ysize);
if (!im) {
return NULL;
}
buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0;
if (color) {
if (!getink(color, im, buffer)) {
ImagingDelete(im);
return NULL;
}
}
(void)ImagingFill(im, buffer);
return PyImagingNew(im);
}
static PyObject *
_new(PyObject *self, PyObject *args) {
char *mode;
int xsize, ysize;
if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize)) {
return NULL;
}
return PyImagingNew(ImagingNew(mode, xsize, ysize));
}
static PyObject *
_new_block(PyObject *self, PyObject *args) {
char *mode;
int xsize, ysize;
if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize)) {
return NULL;
}
return PyImagingNew(ImagingNewBlock(mode, xsize, ysize));
}
static PyObject *
_linear_gradient(PyObject *self, PyObject *args) {
char *mode;
if (!PyArg_ParseTuple(args, "s", &mode)) {
return NULL;
}
return PyImagingNew(ImagingFillLinearGradient(mode));
}
static PyObject *
_radial_gradient(PyObject *self, PyObject *args) {
char *mode;
if (!PyArg_ParseTuple(args, "s", &mode)) {
return NULL;
}
return PyImagingNew(ImagingFillRadialGradient(mode));
}
static PyObject *
_alpha_composite(ImagingObject *self, PyObject *args) {
ImagingObject *imagep1;
ImagingObject *imagep2;
if (!PyArg_ParseTuple(
args, "O!O!", &Imaging_Type, &imagep1, &Imaging_Type, &imagep2)) {
return NULL;
}
return PyImagingNew(ImagingAlphaComposite(imagep1->image, imagep2->image));
}
static PyObject *
_blend(ImagingObject *self, PyObject *args) {
ImagingObject *imagep1;
ImagingObject *imagep2;
double alpha;
alpha = 0.5;
if (!PyArg_ParseTuple(
args, "O!O!|d", &Imaging_Type, &imagep1, &Imaging_Type, &imagep2, &alpha)) {
return NULL;
}
return PyImagingNew(ImagingBlend(imagep1->image, imagep2->image, (float)alpha));
}
/* -------------------------------------------------------------------- */
/* METHODS */
/* -------------------------------------------------------------------- */
static INT16 *
_prepare_lut_table(PyObject *table, Py_ssize_t table_size) {
int i;
Py_buffer buffer_info;
INT32 data_type = TYPE_FLOAT32;
float item = 0;
void *table_data = NULL;
int free_table_data = 0;
INT16 *prepared;
/* NOTE: This value should be the same as in ColorLUT.c */
#define PRECISION_BITS (16 - 8 - 2)
const char *wrong_size =
("The table should have table_channels * "
"size1D * size2D * size3D float items.");
if (PyObject_CheckBuffer(table)) {
if (!PyObject_GetBuffer(table, &buffer_info, PyBUF_CONTIG_RO | PyBUF_FORMAT)) {
if (buffer_info.ndim == 1 && buffer_info.shape[0] == table_size) {
if (strlen(buffer_info.format) == 1) {
switch (buffer_info.format[0]) {
case 'e':
data_type = TYPE_FLOAT16;
table_data = buffer_info.buf;
break;
case 'f':
data_type = TYPE_FLOAT32;
table_data = buffer_info.buf;
break;
case 'd':
data_type = TYPE_DOUBLE;
table_data = buffer_info.buf;
break;
}
}
}
PyBuffer_Release(&buffer_info);
}
}
if (!table_data) {
free_table_data = 1;
table_data = getlist(table, &table_size, wrong_size, TYPE_FLOAT32);
if (!table_data) {
return NULL;
}
}
/* malloc check ok, max is 2 * 4 * 65**3 = 2197000 */
prepared = (INT16 *)malloc(sizeof(INT16) * table_size);
if (!prepared) {
if (free_table_data) {
free(table_data);
}
return (INT16 *)ImagingError_MemoryError();
}
for (i = 0; i < table_size; i++) {
FLOAT16 htmp;
double dtmp;
switch (data_type) {
case TYPE_FLOAT16:
memcpy(&htmp, ((char *)table_data) + i * sizeof(htmp), sizeof(htmp));
item = float16tofloat32(htmp);
break;
case TYPE_FLOAT32:
memcpy(
&item, ((char *)table_data) + i * sizeof(FLOAT32), sizeof(FLOAT32));
break;
case TYPE_DOUBLE:
memcpy(&dtmp, ((char *)table_data) + i * sizeof(dtmp), sizeof(dtmp));
item = (FLOAT32)dtmp;
break;
}
/* Max value for INT16 */
if (item >= (0x7fff - 0.5) / (255 << PRECISION_BITS)) {
prepared[i] = 0x7fff;
continue;
}
/* Min value for INT16 */
if (item <= (-0x8000 + 0.5) / (255 << PRECISION_BITS)) {
prepared[i] = -0x8000;
continue;
}
if (item < 0) {
prepared[i] = item * (255 << PRECISION_BITS) - 0.5;
} else {
prepared[i] = item * (255 << PRECISION_BITS) + 0.5;
}
}
#undef PRECISION_BITS
if (free_table_data) {
free(table_data);
}
return prepared;
}
static PyObject *
_color_lut_3d(ImagingObject *self, PyObject *args) {
char *mode;
int filter;
int table_channels;
int size1D, size2D, size3D;
PyObject *table;
INT16 *prepared_table;
Imaging imOut;
if (!PyArg_ParseTuple(
args,
"siiiiiO:color_lut_3d",
&mode,
&filter,
&table_channels,
&size1D,
&size2D,
&size3D,
&table)) {
return NULL;
}
/* actually, it is trilinear */
if (filter != IMAGING_TRANSFORM_BILINEAR) {
PyErr_SetString(PyExc_ValueError, "Only LINEAR filter is supported.");
return NULL;
}
if (1 > table_channels || table_channels > 4) {
PyErr_SetString(PyExc_ValueError, "table_channels should be from 1 to 4");
return NULL;
}
if (2 > size1D || size1D > 65 || 2 > size2D || size2D > 65 || 2 > size3D ||
size3D > 65) {
PyErr_SetString(
PyExc_ValueError, "Table size in any dimension should be from 2 to 65");
return NULL;
}
prepared_table =
_prepare_lut_table(table, table_channels * size1D * size2D * size3D);
if (!prepared_table) {
return NULL;
}
imOut = ImagingNewDirty(mode, self->image->xsize, self->image->ysize);
if (!imOut) {
free(prepared_table);
return NULL;
}
if (!ImagingColorLUT3D_linear(
imOut,
self->image,
table_channels,
size1D,
size2D,
size3D,
prepared_table)) {
free(prepared_table);
ImagingDelete(imOut);
return NULL;
}
free(prepared_table);
return PyImagingNew(imOut);
}
static PyObject *
_convert(ImagingObject *self, PyObject *args) {
char *mode;
int dither = 0;
ImagingObject *paletteimage = NULL;
if (!PyArg_ParseTuple(args, "s|iO", &mode, &dither, &paletteimage)) {
return NULL;
}
if (paletteimage != NULL) {
if (!PyImaging_Check(paletteimage)) {
PyObject_Print((PyObject *)paletteimage, stderr, 0);
PyErr_SetString(
PyExc_ValueError, "palette argument must be image with mode 'P'");
return NULL;
}
if (paletteimage->image->palette == NULL) {
PyErr_SetString(PyExc_ValueError, "null palette");
return NULL;
}
}
return PyImagingNew(ImagingConvert(
self->image, mode, paletteimage ? paletteimage->image->palette : NULL, dither));
}
static PyObject *
_convert2(ImagingObject *self, PyObject *args) {
ImagingObject *imagep1;
ImagingObject *imagep2;
if (!PyArg_ParseTuple(
args, "O!O!", &Imaging_Type, &imagep1, &Imaging_Type, &imagep2)) {
return NULL;
}
if (!ImagingConvert2(imagep1->image, imagep2->image)) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_convert_matrix(ImagingObject *self, PyObject *args) {
char *mode;
float m[12];
if (!PyArg_ParseTuple(args, "s(ffff)", &mode, m + 0, m + 1, m + 2, m + 3)) {
PyErr_Clear();
if (!PyArg_ParseTuple(
args,
"s(ffffffffffff)",
&mode,
m + 0,
m + 1,
m + 2,
m + 3,
m + 4,
m + 5,
m + 6,
m + 7,
m + 8,
m + 9,
m + 10,
m + 11)) {
return NULL;
}
}
return PyImagingNew(ImagingConvertMatrix(self->image, mode, m));
}
static PyObject *
_convert_transparent(ImagingObject *self, PyObject *args) {
char *mode;
int r, g, b;
if (PyArg_ParseTuple(args, "s(iii)", &mode, &r, &g, &b)) {
return PyImagingNew(ImagingConvertTransparent(self->image, mode, r, g, b));
}
PyErr_Clear();
if (PyArg_ParseTuple(args, "si", &mode, &r)) {
return PyImagingNew(ImagingConvertTransparent(self->image, mode, r, 0, 0));
}
return NULL;
}
static PyObject *
_copy(ImagingObject *self, PyObject *args) {
if (!PyArg_ParseTuple(args, "")) {
return NULL;
}
return PyImagingNew(ImagingCopy(self->image));
}
static PyObject *
_crop(ImagingObject *self, PyObject *args) {
int x0, y0, x1, y1;
if (!PyArg_ParseTuple(args, "(iiii)", &x0, &y0, &x1, &y1)) {
return NULL;
}
return PyImagingNew(ImagingCrop(self->image, x0, y0, x1, y1));
}
static PyObject *
_expand_image(ImagingObject *self, PyObject *args) {
int x, y;
if (!PyArg_ParseTuple(args, "ii", &x, &y)) {
return NULL;
}
return PyImagingNew(ImagingExpand(self->image, x, y));
}
static PyObject *
_filter(ImagingObject *self, PyObject *args) {
PyObject *imOut;
Py_ssize_t kernelsize;
FLOAT32 *kerneldata;
int xsize, ysize, i;
float divisor, offset;
PyObject *kernel = NULL;
if (!PyArg_ParseTuple(
args, "(ii)ffO", &xsize, &ysize, &divisor, &offset, &kernel)) {
return NULL;
}
/* get user-defined kernel */
kerneldata = getlist(kernel, &kernelsize, NULL, TYPE_FLOAT32);
if (!kerneldata) {
return NULL;
}
if (kernelsize != (Py_ssize_t)xsize * (Py_ssize_t)ysize) {
free(kerneldata);
return ImagingError_ValueError("bad kernel size");
}
for (i = 0; i < kernelsize; ++i) {
kerneldata[i] /= divisor;
}
imOut = PyImagingNew(ImagingFilter(self->image, xsize, ysize, kerneldata, offset));
free(kerneldata);
return imOut;
}
#ifdef WITH_UNSHARPMASK
static PyObject *
_gaussian_blur(ImagingObject *self, PyObject *args) {
Imaging imIn;
Imaging imOut;
float xradius, yradius;
int passes = 3;
if (!PyArg_ParseTuple(args, "(ff)|i", &xradius, &yradius, &passes)) {
return NULL;
}
imIn = self->image;
imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize);
if (!imOut) {
return NULL;
}
if (!ImagingGaussianBlur(imOut, imIn, xradius, yradius, passes)) {
ImagingDelete(imOut);
return NULL;
}
return PyImagingNew(imOut);
}
#endif
static PyObject *
_getpalette(ImagingObject *self, PyObject *args) {
PyObject *palette;
int palettesize;
int bits;
ImagingShuffler pack;
char *mode = "RGB";
char *rawmode = "RGB";
if (!PyArg_ParseTuple(args, "|ss", &mode, &rawmode)) {
return NULL;
}
if (!self->image->palette) {
PyErr_SetString(PyExc_ValueError, no_palette);
return NULL;
}
pack = ImagingFindPacker(mode, rawmode, &bits);
if (!pack) {
PyErr_SetString(PyExc_ValueError, wrong_raw_mode);
return NULL;
}
palettesize = self->image->palette->size;
palette = PyBytes_FromStringAndSize(NULL, palettesize * bits / 8);
if (!palette) {
return NULL;
}
pack(
(UINT8 *)PyBytes_AsString(palette), self->image->palette->palette, palettesize);
return palette;
}
static PyObject *
_getpalettemode(ImagingObject *self) {
if (!self->image->palette) {
PyErr_SetString(PyExc_ValueError, no_palette);
return NULL;
}
return PyUnicode_FromString(self->image->palette->mode);
}
static inline int
_getxy(PyObject *xy, int *x, int *y) {
PyObject *value;
if (!PyTuple_Check(xy) || PyTuple_GET_SIZE(xy) != 2) {
goto badarg;
}
value = PyTuple_GET_ITEM(xy, 0);
if (PyLong_Check(value)) {
*x = PyLong_AS_LONG(value);
} else if (PyFloat_Check(value)) {
*x = (int)PyFloat_AS_DOUBLE(value);
} else {
PyObject *int_value = PyObject_CallMethod(value, "__int__", NULL);
if (int_value != NULL && PyLong_Check(int_value)) {
*x = PyLong_AS_LONG(int_value);
} else {
goto badval;
}
}
value = PyTuple_GET_ITEM(xy, 1);
if (PyLong_Check(value)) {
*y = PyLong_AS_LONG(value);
} else if (PyFloat_Check(value)) {
*y = (int)PyFloat_AS_DOUBLE(value);
} else {
PyObject *int_value = PyObject_CallMethod(value, "__int__", NULL);
if (int_value != NULL && PyLong_Check(int_value)) {
*y = PyLong_AS_LONG(int_value);
} else {
goto badval;
}
}
return 0;
badarg:
PyErr_SetString(PyExc_TypeError, "argument must be sequence of length 2");
return -1;
badval:
PyErr_SetString(PyExc_TypeError, "an integer is required");
return -1;
}
static PyObject *
_getpixel(ImagingObject *self, PyObject *args) {
PyObject *xy;
int x, y;
if (PyTuple_GET_SIZE(args) != 1) {
PyErr_SetString(PyExc_TypeError, "argument 1 must be sequence of length 2");
return NULL;
}
xy = PyTuple_GET_ITEM(args, 0);
if (_getxy(xy, &x, &y)) {
return NULL;
}
if (self->access == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
return getpixel(self->image, self->access, x, y);
}
union hist_extrema {
UINT8 u[2];
INT32 i[2];
FLOAT32 f[2];
};
static union hist_extrema *
parse_histogram_extremap(
ImagingObject *self, PyObject *extremap, union hist_extrema *ep) {
int i0, i1;
double f0, f1;
if (extremap) {
switch (self->image->type) {
case IMAGING_TYPE_UINT8:
if (!PyArg_ParseTuple(extremap, "ii", &i0, &i1)) {
return NULL;
}
ep->u[0] = CLIP8(i0);
ep->u[1] = CLIP8(i1);
break;
case IMAGING_TYPE_INT32:
if (!PyArg_ParseTuple(extremap, "ii", &i0, &i1)) {
return NULL;
}
ep->i[0] = i0;
ep->i[1] = i1;
break;
case IMAGING_TYPE_FLOAT32:
if (!PyArg_ParseTuple(extremap, "dd", &f0, &f1)) {
return NULL;
}
ep->f[0] = (FLOAT32)f0;
ep->f[1] = (FLOAT32)f1;
break;
default:
return NULL;
}
} else {
return NULL;
}
return ep;
}
static PyObject *
_histogram(ImagingObject *self, PyObject *args) {
ImagingHistogram h;
PyObject *list;
int i;
union hist_extrema extrema;
union hist_extrema *ep;
PyObject *extremap = NULL;
ImagingObject *maskp = NULL;
if (!PyArg_ParseTuple(args, "|OO!", &extremap, &Imaging_Type, &maskp)) {
return NULL;
}
/* Using a var to avoid allocations. */
ep = parse_histogram_extremap(self, extremap, &extrema);
h = ImagingGetHistogram(self->image, (maskp) ? maskp->image : NULL, ep);
if (!h) {
return NULL;
}
/* Build an integer list containing the histogram */
list = PyList_New(h->bands * 256);
if (list == NULL) {
ImagingHistogramDelete(h);
return NULL;
}
for (i = 0; i < h->bands * 256; i++) {
PyObject *item;
item = PyLong_FromLong(h->histogram[i]);
if (item == NULL) {
Py_DECREF(list);
list = NULL;
break;
}
PyList_SetItem(list, i, item);
}
/* Destroy the histogram structure */
ImagingHistogramDelete(h);
return list;
}
static PyObject *
_entropy(ImagingObject *self, PyObject *args) {
ImagingHistogram h;
int idx, length;
long sum;
double entropy, fsum, p;
union hist_extrema extrema;
union hist_extrema *ep;
PyObject *extremap = NULL;
ImagingObject *maskp = NULL;
if (!PyArg_ParseTuple(args, "|OO!", &extremap, &Imaging_Type, &maskp)) {
return NULL;
}
/* Using a local var to avoid allocations. */
ep = parse_histogram_extremap(self, extremap, &extrema);
h = ImagingGetHistogram(self->image, (maskp) ? maskp->image : NULL, ep);
if (!h) {
return NULL;
}
/* Calculate the histogram entropy */
/* First, sum the histogram data */
length = h->bands * 256;
sum = 0;
for (idx = 0; idx < length; idx++) {
sum += h->histogram[idx];
}
/* Next, normalize the histogram data, */
/* using the histogram sum value */
fsum = (double)sum;
entropy = 0.0;
for (idx = 0; idx < length; idx++) {
p = (double)h->histogram[idx] / fsum;
if (p != 0.0) {
entropy += p * log(p) * M_LOG2E;
}
}
/* Destroy the histogram structure */
ImagingHistogramDelete(h);
return PyFloat_FromDouble(-entropy);
}
#ifdef WITH_MODEFILTER
static PyObject *
_modefilter(ImagingObject *self, PyObject *args) {
int size;
if (!PyArg_ParseTuple(args, "i", &size)) {
return NULL;
}
return PyImagingNew(ImagingModeFilter(self->image, size));
}
#endif
static PyObject *
_offset(ImagingObject *self, PyObject *args) {
int xoffset, yoffset;
if (!PyArg_ParseTuple(args, "ii", &xoffset, &yoffset)) {
return NULL;
}
return PyImagingNew(ImagingOffset(self->image, xoffset, yoffset));
}
static PyObject *
_paste(ImagingObject *self, PyObject *args) {
int status;
char ink[4];
PyObject *source;
int x0, y0, x1, y1;
ImagingObject *maskp = NULL;
if (!PyArg_ParseTuple(
args, "O(iiii)|O!", &source, &x0, &y0, &x1, &y1, &Imaging_Type, &maskp)) {
return NULL;
}
if (PyImaging_Check(source)) {
status = ImagingPaste(
self->image,
PyImaging_AsImaging(source),
(maskp) ? maskp->image : NULL,
x0,
y0,
x1,
y1);
} else {
if (!getink(source, self->image, ink)) {
return NULL;
}
status = ImagingFill2(
self->image, ink, (maskp) ? maskp->image : NULL, x0, y0, x1, y1);
}
if (status < 0) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_point(ImagingObject *self, PyObject *args) {
static const char *wrong_number = "wrong number of lut entries";
Py_ssize_t n;
int i, bands;
Imaging im;
PyObject *list;
char *mode;
if (!PyArg_ParseTuple(args, "Oz", &list, &mode)) {
return NULL;
}
if (mode && !strcmp(mode, "F")) {
FLOAT32 *data;
/* map from 8-bit data to floating point */
n = 256;
data = getlist(list, &n, wrong_number, TYPE_FLOAT32);
if (!data) {
return NULL;
}
im = ImagingPoint(self->image, mode, (void *)data);
free(data);
} else if (!strcmp(self->image->mode, "I") && mode && !strcmp(mode, "L")) {
UINT8 *data;
/* map from 16-bit subset of 32-bit data to 8-bit */
/* FIXME: support arbitrary number of entries (requires API change) */
n = 65536;
data = getlist(list, &n, wrong_number, TYPE_UINT8);
if (!data) {
return NULL;
}
im = ImagingPoint(self->image, mode, (void *)data);
free(data);
} else {
INT32 *data;
UINT8 lut[1024];
if (mode) {
bands = getbands(mode);
if (bands < 0) {
return NULL;
}
} else {
bands = self->image->bands;
}
/* map to integer data */
n = 256 * bands;
data = getlist(list, &n, wrong_number, TYPE_INT32);
if (!data) {
return NULL;
}
if (mode && !strcmp(mode, "I")) {
im = ImagingPoint(self->image, mode, (void *)data);
} else if (mode && bands > 1) {
for (i = 0; i < 256; i++) {
lut[i * 4] = CLIP8(data[i]);
lut[i * 4 + 1] = CLIP8(data[i + 256]);
lut[i * 4 + 2] = CLIP8(data[i + 512]);
if (n > 768) {
lut[i * 4 + 3] = CLIP8(data[i + 768]);
}
}
im = ImagingPoint(self->image, mode, (void *)lut);
} else {
/* map individual bands */
for (i = 0; i < n; i++) {
lut[i] = CLIP8(data[i]);
}
im = ImagingPoint(self->image, mode, (void *)lut);
}
free(data);
}
return PyImagingNew(im);
}
static PyObject *
_point_transform(ImagingObject *self, PyObject *args) {
double scale = 1.0;
double offset = 0.0;
if (!PyArg_ParseTuple(args, "|dd", &scale, &offset)) {
return NULL;
}
return PyImagingNew(ImagingPointTransform(self->image, scale, offset));
}
static PyObject *
_putdata(ImagingObject *self, PyObject *args) {
Imaging image;
// i & n are # pixels, require py_ssize_t. x can be as large as n. y, just because.
Py_ssize_t n, i, x, y;
PyObject *data;
PyObject *seq = NULL;
PyObject *op;
double scale = 1.0;
double offset = 0.0;
if (!PyArg_ParseTuple(args, "O|dd", &data, &scale, &offset)) {
return NULL;
}
if (!PySequence_Check(data)) {
PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL;
}
image = self->image;
n = PyObject_Length(data);
if (n > (Py_ssize_t)image->xsize * (Py_ssize_t)image->ysize) {
PyErr_SetString(PyExc_TypeError, "too many data entries");
return NULL;
}
#define set_value_to_item(seq, i) \
op = PySequence_Fast_GET_ITEM(seq, i); \
if (PySequence_Check(op)) { \
PyErr_SetString(PyExc_TypeError, "sequence must be flattened"); \
return NULL; \
} else { \
value = PyFloat_AsDouble(op); \
}
if (image->image8) {
if (PyBytes_Check(data)) {
unsigned char *p;
p = (unsigned char *)PyBytes_AS_STRING(data);
if (scale == 1.0 && offset == 0.0) {
/* Plain string data */
for (i = y = 0; i < n; i += image->xsize, y++) {
x = n - i;
if (x > (int)image->xsize) {
x = image->xsize;
}
memcpy(image->image8[y], p + i, x);
}
} else {
/* Scaled and clipped string data */
for (i = x = y = 0; i < n; i++) {
image->image8[y][x] = CLIP8((int)(p[i] * scale + offset));
if (++x >= (int)image->xsize) {
x = 0, y++;
}
}
}
} else {
seq = PySequence_Fast(data, must_be_sequence);
if (!seq) {
PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL;
}
double value;
if (image->bands == 1) {
int bigendian = 0;
if (image->type == IMAGING_TYPE_SPECIAL) {
// I;16*
bigendian = strcmp(image->mode, "I;16B") == 0;
}
for (i = x = y = 0; i < n; i++) {
set_value_to_item(seq, i);
if (scale != 1.0 || offset != 0.0) {
value = value * scale + offset;
}
if (image->type == IMAGING_TYPE_SPECIAL) {
image->image8[y][x * 2 + (bigendian ? 1 : 0)] = CLIP8((int)value % 256);
image->image8[y][x * 2 + (bigendian ? 0 : 1)] = CLIP8((int)value >> 8);
} else {
image->image8[y][x] = (UINT8)CLIP8(value);
}
if (++x >= (int)image->xsize) {
x = 0, y++;
}
}
} else {
// BGR;*
int b;
for (i = x = y = 0; i < n; i++) {
char ink[4];
op = PySequence_Fast_GET_ITEM(seq, i);
if (!op || !getink(op, image, ink)) {
Py_DECREF(seq);
return NULL;
}
/* FIXME: what about scale and offset? */
for (b = 0; b < image->pixelsize; b++) {
image->image8[y][x * image->pixelsize + b] = ink[b];
}
if (++x >= (int)image->xsize) {
x = 0, y++;
}
}
}
PyErr_Clear(); /* Avoid weird exceptions */
}
} else {
/* 32-bit images */
seq = PySequence_Fast(data, must_be_sequence);
if (!seq) {
PyErr_SetString(PyExc_TypeError, must_be_sequence);
return NULL;
}
switch (image->type) {
case IMAGING_TYPE_INT32:
for (i = x = y = 0; i < n; i++) {
double value;
set_value_to_item(seq, i);
IMAGING_PIXEL_INT32(image, x, y) =
(INT32)(value * scale + offset);
if (++x >= (int)image->xsize) {
x = 0, y++;
}
}
PyErr_Clear(); /* Avoid weird exceptions */
break;
case IMAGING_TYPE_FLOAT32:
for (i = x = y = 0; i < n; i++) {
double value;
set_value_to_item(seq, i);
IMAGING_PIXEL_FLOAT32(image, x, y) =
(FLOAT32)(value * scale + offset);
if (++x >= (int)image->xsize) {
x = 0, y++;
}
}
PyErr_Clear(); /* Avoid weird exceptions */
break;
default:
for (i = x = y = 0; i < n; i++) {
union {
char ink[4];
INT32 inkint;
} u;
u.inkint = 0;
op = PySequence_Fast_GET_ITEM(seq, i);
if (!op || !getink(op, image, u.ink)) {
Py_DECREF(seq);
return NULL;
}
/* FIXME: what about scale and offset? */
image->image32[y][x] = u.inkint;
if (++x >= (int)image->xsize) {
x = 0, y++;
}
}
PyErr_Clear(); /* Avoid weird exceptions */
break;
}
}
Py_XDECREF(seq);
Py_INCREF(Py_None);
return Py_None;
}
#ifdef WITH_QUANTIZE
static PyObject *
_quantize(ImagingObject *self, PyObject *args) {
int colours = 256;
int method = 0;
int kmeans = 0;
if (!PyArg_ParseTuple(args, "|iii", &colours, &method, &kmeans)) {
return NULL;
}
if (!self->image->xsize || !self->image->ysize) {
/* no content; return an empty image */
return PyImagingNew(ImagingNew("P", self->image->xsize, self->image->ysize));
}
return PyImagingNew(ImagingQuantize(self->image, colours, method, kmeans));
}
#endif
static PyObject *
_putpalette(ImagingObject *self, PyObject *args) {
ImagingShuffler unpack;
int bits;
char *rawmode, *palette_mode;
UINT8 *palette;
Py_ssize_t palettesize;
if (!PyArg_ParseTuple(args, "sy#", &rawmode, &palette, &palettesize)) {
return NULL;
}
if (strcmp(self->image->mode, "L") && strcmp(self->image->mode, "LA") &&
strcmp(self->image->mode, "P") && strcmp(self->image->mode, "PA")) {
PyErr_SetString(PyExc_ValueError, wrong_mode);
return NULL;
}
palette_mode = strncmp("RGBA", rawmode, 4) == 0 ? "RGBA" : "RGB";
unpack = ImagingFindUnpacker(palette_mode, rawmode, &bits);
if (!unpack) {
PyErr_SetString(PyExc_ValueError, wrong_raw_mode);
return NULL;
}
if (palettesize * 8 / bits > 256) {
PyErr_SetString(PyExc_ValueError, wrong_palette_size);
return NULL;
}
ImagingPaletteDelete(self->image->palette);
strcpy(self->image->mode, strlen(self->image->mode) == 2 ? "PA" : "P");
self->image->palette = ImagingPaletteNew(palette_mode);
self->image->palette->size = palettesize * 8 / bits;
unpack(self->image->palette->palette, palette, self->image->palette->size);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_putpalettealpha(ImagingObject *self, PyObject *args) {
int index;
int alpha = 0;
if (!PyArg_ParseTuple(args, "i|i", &index, &alpha)) {
return NULL;
}
if (!self->image->palette) {
PyErr_SetString(PyExc_ValueError, no_palette);
return NULL;
}
if (index < 0 || index >= 256) {
PyErr_SetString(PyExc_ValueError, outside_palette);
return NULL;
}
strcpy(self->image->palette->mode, "RGBA");
self->image->palette->palette[index * 4 + 3] = (UINT8)alpha;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_putpalettealphas(ImagingObject *self, PyObject *args) {
int i;
UINT8 *values;
Py_ssize_t length;
if (!PyArg_ParseTuple(args, "y#", &values, &length)) {
return NULL;
}
if (!self->image->palette) {
PyErr_SetString(PyExc_ValueError, no_palette);
return NULL;
}
if (length > 256) {
PyErr_SetString(PyExc_ValueError, outside_palette);
return NULL;
}
strcpy(self->image->palette->mode, "RGBA");
for (i = 0; i < length; i++) {
self->image->palette->palette[i * 4 + 3] = (UINT8)values[i];
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_putpixel(ImagingObject *self, PyObject *args) {
Imaging im;
char ink[4];
int x, y;
PyObject *color;
if (!PyArg_ParseTuple(args, "(ii)O", &x, &y, &color)) {
return NULL;
}
im = self->image;
if (x < 0) {
x = im->xsize + x;
}
if (y < 0) {
y = im->ysize + y;
}
if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
PyErr_SetString(PyExc_IndexError, outside_image);
return NULL;
}
if (!getink(color, im, ink)) {
return NULL;
}
if (self->access) {
self->access->put_pixel(im, x, y, ink);
}
Py_INCREF(Py_None);
return Py_None;
}
#ifdef WITH_RANKFILTER
static PyObject *
_rankfilter(ImagingObject *self, PyObject *args) {
int size, rank;
if (!PyArg_ParseTuple(args, "ii", &size, &rank)) {
return NULL;
}
return PyImagingNew(ImagingRankFilter(self->image, size, rank));
}
#endif
static PyObject *
_resize(ImagingObject *self, PyObject *args) {
Imaging imIn;
Imaging imOut;
int xsize, ysize;
int filter = IMAGING_TRANSFORM_NEAREST;
float box[4] = {0, 0, 0, 0};
imIn = self->image;
box[2] = imIn->xsize;
box[3] = imIn->ysize;
if (!PyArg_ParseTuple(
args,
"(ii)|i(ffff)",
&xsize,
&ysize,
&filter,
&box[0],
&box[1],
&box[2],
&box[3])) {
return NULL;
}
if (xsize < 1 || ysize < 1) {
return ImagingError_ValueError("height and width must be > 0");
}
if (box[0] < 0 || box[1] < 0) {
return ImagingError_ValueError("box offset can't be negative");
}
if (box[2] > imIn->xsize || box[3] > imIn->ysize) {
return ImagingError_ValueError("box can't exceed original image size");
}
if (box[2] - box[0] < 0 || box[3] - box[1] < 0) {
return ImagingError_ValueError("box can't be empty");
}
// If box's coordinates are int and box size matches requested size
if (box[0] - (int)box[0] == 0 && box[2] - box[0] == xsize &&
box[1] - (int)box[1] == 0 && box[3] - box[1] == ysize) {
imOut = ImagingCrop(imIn, box[0], box[1], box[2], box[3]);
} else if (filter == IMAGING_TRANSFORM_NEAREST) {
double a[8];
memset(a, 0, sizeof a);
a[0] = (double)(box[2] - box[0]) / xsize;
a[4] = (double)(box[3] - box[1]) / ysize;
a[2] = box[0];
a[5] = box[1];
imOut = ImagingNewDirty(imIn->mode, xsize, ysize);
imOut = ImagingTransform(
imOut, imIn, IMAGING_TRANSFORM_AFFINE, 0, 0, xsize, ysize, a, filter, 1);
} else {
imOut = ImagingResample(imIn, xsize, ysize, filter, box);
}
return PyImagingNew(imOut);
}
static PyObject *
_reduce(ImagingObject *self, PyObject *args) {
Imaging imIn;
Imaging imOut;
int xscale, yscale;
int box[4] = {0, 0, 0, 0};
imIn = self->image;
box[2] = imIn->xsize;
box[3] = imIn->ysize;
if (!PyArg_ParseTuple(
args,
"(ii)|(iiii)",
&xscale,
&yscale,
&box[0],
&box[1],
&box[2],
&box[3])) {
return NULL;
}
if (xscale < 1 || yscale < 1) {
return ImagingError_ValueError("scale must be > 0");
}
if (box[0] < 0 || box[1] < 0) {
return ImagingError_ValueError("box offset can't be negative");
}
if (box[2] > imIn->xsize || box[3] > imIn->ysize) {
return ImagingError_ValueError("box can't exceed original image size");
}
if (box[2] <= box[0] || box[3] <= box[1]) {
return ImagingError_ValueError("box can't be empty");
}
if (xscale == 1 && yscale == 1) {
imOut = ImagingCrop(imIn, box[0], box[1], box[2], box[3]);
} else {
// Change box format: (left, top, width, height)
box[2] -= box[0];
box[3] -= box[1];
imOut = ImagingReduce(imIn, xscale, yscale, box);
}
return PyImagingNew(imOut);
}
#define IS_RGB(mode) \
(!strcmp(mode, "RGB") || !strcmp(mode, "RGBA") || !strcmp(mode, "RGBX"))
static PyObject *
im_setmode(ImagingObject *self, PyObject *args) {
/* attempt to modify the mode of an image in place */
Imaging im;
char *mode;
Py_ssize_t modelen;
if (!PyArg_ParseTuple(args, "s#:setmode", &mode, &modelen)) {
return NULL;
}
im = self->image;
/* move all logic in here to the libImaging primitive */
if (!strcmp(im->mode, mode)) {
; /* same mode; always succeeds */
} else if (IS_RGB(im->mode) && IS_RGB(mode)) {
/* color to color */
strcpy(im->mode, mode);
im->bands = modelen;
if (!strcmp(mode, "RGBA")) {
(void)ImagingFillBand(im, 3, 255);
}
} else {
/* trying doing an in-place conversion */
if (!ImagingConvertInPlace(im, mode)) {
return NULL;
}
}
if (self->access) {
ImagingAccessDelete(im, self->access);
}
self->access = ImagingAccessNew(im);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_transform2(ImagingObject *self, PyObject *args) {
static const char *wrong_number = "wrong number of matrix entries";
Imaging imOut;
Py_ssize_t n;
double *a;
ImagingObject *imagep;
int x0, y0, x1, y1;
int method;
PyObject *data;
int filter = IMAGING_TRANSFORM_NEAREST;
int fill = 1;
if (!PyArg_ParseTuple(
args,
"(iiii)O!iO|ii",
&x0,
&y0,
&x1,
&y1,
&Imaging_Type,
&imagep,
&method,
&data,
&filter,
&fill)) {
return NULL;
}
switch (method) {
case IMAGING_TRANSFORM_AFFINE:
n = 6;
break;
case IMAGING_TRANSFORM_PERSPECTIVE:
n = 8;
break;
case IMAGING_TRANSFORM_QUAD:
n = 8;
break;
default:
n = -1; /* force error */
}
a = getlist(data, &n, wrong_number, TYPE_DOUBLE);
if (!a) {
return NULL;
}
imOut = ImagingTransform(
self->image, imagep->image, method, x0, y0, x1, y1, a, filter, fill);
free(a);
if (!imOut) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_transpose(ImagingObject *self, PyObject *args) {
Imaging imIn;
Imaging imOut;
int op;
if (!PyArg_ParseTuple(args, "i", &op)) {
return NULL;
}
imIn = self->image;
switch (op) {
case 0: /* flip left right */
case 1: /* flip top bottom */
case 3: /* rotate 180 */
imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize);
break;
case 2: /* rotate 90 */
case 4: /* rotate 270 */
case 5: /* transpose */
case 6: /* transverse */
imOut = ImagingNewDirty(imIn->mode, imIn->ysize, imIn->xsize);
break;
default:
PyErr_SetString(PyExc_ValueError, "No such transpose operation");
return NULL;
}
if (imOut) {
switch (op) {
case 0:
(void)ImagingFlipLeftRight(imOut, imIn);
break;
case 1:
(void)ImagingFlipTopBottom(imOut, imIn);
break;
case 2:
(void)ImagingRotate90(imOut, imIn);
break;
case 3:
(void)ImagingRotate180(imOut, imIn);
break;
case 4:
(void)ImagingRotate270(imOut, imIn);
break;
case 5:
(void)ImagingTranspose(imOut, imIn);
break;
case 6:
(void)ImagingTransverse(imOut, imIn);
break;
}
}
return PyImagingNew(imOut);
}
#ifdef WITH_UNSHARPMASK
static PyObject *
_unsharp_mask(ImagingObject *self, PyObject *args) {
Imaging imIn;
Imaging imOut;
float radius;
int percent, threshold;
if (!PyArg_ParseTuple(args, "fii", &radius, &percent, &threshold)) {
return NULL;
}
imIn = self->image;
imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize);
if (!imOut) {
return NULL;
}
if (!ImagingUnsharpMask(imOut, imIn, radius, percent, threshold)) {
return NULL;
}
return PyImagingNew(imOut);
}
#endif
static PyObject *
_box_blur(ImagingObject *self, PyObject *args) {
Imaging imIn;
Imaging imOut;
float xradius, yradius;
int n = 1;
if (!PyArg_ParseTuple(args, "(ff)|i", &xradius, &yradius, &n)) {
return NULL;
}
imIn = self->image;
imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize);
if (!imOut) {
return NULL;
}
if (!ImagingBoxBlur(imOut, imIn, xradius, yradius, n)) {
ImagingDelete(imOut);
return NULL;
}
return PyImagingNew(imOut);
}
/* -------------------------------------------------------------------- */
static PyObject *
_isblock(ImagingObject *self) {
return PyBool_FromLong(self->image->block != NULL);
}
static PyObject *
_getbbox(ImagingObject *self, PyObject *args) {
int bbox[4];
int alpha_only = 1;
if (!PyArg_ParseTuple(args, "|i", &alpha_only)) {
return NULL;
}
if (!ImagingGetBBox(self->image, bbox, alpha_only)) {
Py_INCREF(Py_None);
return Py_None;
}
return Py_BuildValue("iiii", bbox[0], bbox[1], bbox[2], bbox[3]);
}
static PyObject *
_getcolors(ImagingObject *self, PyObject *args) {
ImagingColorItem *items;
int i, colors;
PyObject *out;
int maxcolors = 256;
if (!PyArg_ParseTuple(args, "i:getcolors", &maxcolors)) {
return NULL;
}
items = ImagingGetColors(self->image, maxcolors, &colors);
if (!items) {
return NULL;
}
if (colors > maxcolors) {
out = Py_None;
Py_INCREF(out);
} else {
out = PyList_New(colors);
if (out == NULL) {
free(items);
return NULL;
}
for (i = 0; i < colors; i++) {
ImagingColorItem *v = &items[i];
PyObject *item = Py_BuildValue(
"iN", v->count, getpixel(self->image, self->access, v->x, v->y));
PyList_SetItem(out, i, item);
}
}
free(items);
return out;
}
static PyObject *
_getextrema(ImagingObject *self) {
union {
UINT8 u[2];
INT32 i[2];
FLOAT32 f[2];
UINT16 s[2];
} extrema;
int status;
status = ImagingGetExtrema(self->image, &extrema);
if (status < 0) {
return NULL;
}
if (status) {
switch (self->image->type) {
case IMAGING_TYPE_UINT8:
return Py_BuildValue("BB", extrema.u[0], extrema.u[1]);
case IMAGING_TYPE_INT32:
return Py_BuildValue("ii", extrema.i[0], extrema.i[1]);
case IMAGING_TYPE_FLOAT32:
return Py_BuildValue("dd", extrema.f[0], extrema.f[1]);
case IMAGING_TYPE_SPECIAL:
if (strcmp(self->image->mode, "I;16") == 0) {
return Py_BuildValue("HH", extrema.s[0], extrema.s[1]);
}
}
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_getprojection(ImagingObject *self) {
unsigned char *xprofile;
unsigned char *yprofile;
PyObject *result;
/* malloc check ok */
xprofile = malloc(self->image->xsize);
yprofile = malloc(self->image->ysize);
if (xprofile == NULL || yprofile == NULL) {
free(xprofile);
free(yprofile);
return ImagingError_MemoryError();
}
ImagingGetProjection(
self->image, (unsigned char *)xprofile, (unsigned char *)yprofile);
result = Py_BuildValue(
"y#y#",
xprofile,
(Py_ssize_t)self->image->xsize,
yprofile,
(Py_ssize_t)self->image->ysize);
free(xprofile);
free(yprofile);
return result;
}
/* -------------------------------------------------------------------- */
static PyObject *
_getband(ImagingObject *self, PyObject *args) {
int band;
if (!PyArg_ParseTuple(args, "i", &band)) {
return NULL;
}
return PyImagingNew(ImagingGetBand(self->image, band));
}
static PyObject *
_fillband(ImagingObject *self, PyObject *args) {
int band;
int color;
if (!PyArg_ParseTuple(args, "ii", &band, &color)) {
return NULL;
}
if (!ImagingFillBand(self->image, band, color)) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_putband(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
int band;
if (!PyArg_ParseTuple(args, "O!i", &Imaging_Type, &imagep, &band)) {
return NULL;
}
if (!ImagingPutBand(self->image, imagep->image, band)) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_merge(PyObject *self, PyObject *args) {
char *mode;
ImagingObject *band0 = NULL;
ImagingObject *band1 = NULL;
ImagingObject *band2 = NULL;
ImagingObject *band3 = NULL;
Imaging bands[4] = {NULL, NULL, NULL, NULL};
if (!PyArg_ParseTuple(
args,
"sO!|O!O!O!",
&mode,
&Imaging_Type,
&band0,
&Imaging_Type,
&band1,
&Imaging_Type,
&band2,
&Imaging_Type,
&band3)) {
return NULL;
}
if (band0) {
bands[0] = band0->image;
}
if (band1) {
bands[1] = band1->image;
}
if (band2) {
bands[2] = band2->image;
}
if (band3) {
bands[3] = band3->image;
}
return PyImagingNew(ImagingMerge(mode, bands));
}
static PyObject *
_split(ImagingObject *self) {
int fails = 0;
Py_ssize_t i;
PyObject *list;
PyObject *imaging_object;
Imaging bands[4] = {NULL, NULL, NULL, NULL};
if (!ImagingSplit(self->image, bands)) {
return NULL;
}
list = PyTuple_New(self->image->bands);
for (i = 0; i < self->image->bands; i++) {
imaging_object = PyImagingNew(bands[i]);
if (!imaging_object) {
fails += 1;
}
PyTuple_SET_ITEM(list, i, imaging_object);
}
if (fails) {
Py_DECREF(list);
list = NULL;
}
return list;
}
/* -------------------------------------------------------------------- */
#ifdef WITH_IMAGECHOPS
static PyObject *
_chop_invert(ImagingObject *self) {
return PyImagingNew(ImagingNegative(self->image));
}
static PyObject *
_chop_lighter(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) {
return NULL;
}
return PyImagingNew(ImagingChopLighter(self->image, imagep->image));
}
static PyObject *
_chop_darker(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) {
return NULL;
}
return PyImagingNew(ImagingChopDarker(self->image, imagep->image));
}
static PyObject *
_chop_difference(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) {
return NULL;
}
return PyImagingNew(ImagingChopDifference(self->image, imagep->image));
}
static PyObject *
_chop_multiply(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) {
return NULL;
}
return PyImagingNew(ImagingChopMultiply(self->image, imagep->image));
}
static PyObject *
_chop_screen(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) {
return NULL;
}
return PyImagingNew(ImagingChopScreen(self->image, imagep->image));
}
static PyObject *
_chop_add(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
float scale;
int offset;
scale = 1.0;
offset = 0;
if (!PyArg_ParseTuple(args, "O!|fi", &Imaging_Type, &imagep, &scale, &offset)) {
return NULL;
}
return PyImagingNew(ImagingChopAdd(self->image, imagep->image, scale, offset));
}
static PyObject *
_chop_subtract(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
float scale;
int offset;
scale = 1.0;
offset = 0;
if (!PyArg_ParseTuple(args, "O!|fi", &Imaging_Type, &imagep, &scale, &offset)) {
return NULL;
}
return PyImagingNew(ImagingChopSubtract(self->image, imagep->image, scale, offset));
}
static PyObject *
_chop_and(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) {
return NULL;
}
return PyImagingNew(ImagingChopAnd(self->image, imagep->image));
}
static PyObject *
_chop_or(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) {
return NULL;
}
return PyImagingNew(ImagingChopOr(self->image, imagep->image));
}
static PyObject *
_chop_xor(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) {
return NULL;
}
return PyImagingNew(ImagingChopXor(self->image, imagep->image));
}
static PyObject *
_chop_add_modulo(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) {
return NULL;
}
return PyImagingNew(ImagingChopAddModulo(self->image, imagep->image));
}
static PyObject *
_chop_subtract_modulo(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) {
return NULL;
}
return PyImagingNew(ImagingChopSubtractModulo(self->image, imagep->image));
}
static PyObject *
_chop_soft_light(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) {
return NULL;
}
return PyImagingNew(ImagingChopSoftLight(self->image, imagep->image));
}
static PyObject *
_chop_hard_light(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) {
return NULL;
}
return PyImagingNew(ImagingChopHardLight(self->image, imagep->image));
}
static PyObject *
_chop_overlay(ImagingObject *self, PyObject *args) {
ImagingObject *imagep;
if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) {
return NULL;
}
return PyImagingNew(ImagingOverlay(self->image, imagep->image));
}
#endif
/* -------------------------------------------------------------------- */
#ifdef WITH_IMAGEDRAW
static PyObject *
_font_new(PyObject *self_, PyObject *args) {
ImagingFontObject *self;
int i, y0, y1;
static const char *wrong_length = "descriptor table has wrong size";
ImagingObject *imagep;
unsigned char *glyphdata;
Py_ssize_t glyphdata_length;
if (!PyArg_ParseTuple(
args, "O!y#", &Imaging_Type, &imagep, &glyphdata, &glyphdata_length)) {
return NULL;
}
if (glyphdata_length != 256 * 20) {
PyErr_SetString(PyExc_ValueError, wrong_length);
return NULL;
}
self = PyObject_New(ImagingFontObject, &ImagingFont_Type);
if (self == NULL) {
return NULL;
}
/* glyph bitmap */
self->bitmap = imagep->image;
y0 = y1 = 0;
/* glyph glyphs */
for (i = 0; i < 256; i++) {
self->glyphs[i].dx = S16(B16(glyphdata, 0));
self->glyphs[i].dy = S16(B16(glyphdata, 2));
self->glyphs[i].dx0 = S16(B16(glyphdata, 4));
self->glyphs[i].dy0 = S16(B16(glyphdata, 6));
self->glyphs[i].dx1 = S16(B16(glyphdata, 8));
self->glyphs[i].dy1 = S16(B16(glyphdata, 10));
self->glyphs[i].sx0 = S16(B16(glyphdata, 12));
self->glyphs[i].sy0 = S16(B16(glyphdata, 14));
self->glyphs[i].sx1 = S16(B16(glyphdata, 16));
self->glyphs[i].sy1 = S16(B16(glyphdata, 18));
if (self->glyphs[i].dy0 < y0) {
y0 = self->glyphs[i].dy0;
}
if (self->glyphs[i].dy1 > y1) {
y1 = self->glyphs[i].dy1;
}
glyphdata += 20;
}
self->baseline = -y0;
self->ysize = y1 - y0;
/* keep a reference to the bitmap object */
Py_INCREF(imagep);
self->ref = imagep;
return (PyObject *)self;
}
static void
_font_dealloc(ImagingFontObject *self) {
Py_XDECREF(self->ref);
PyObject_Del(self);
}
static inline int
textwidth(ImagingFontObject *self, const unsigned char *text) {
int xsize;
for (xsize = 0; *text; text++) {
xsize += self->glyphs[*text].dx;
}
return xsize;
}
void
_font_text_asBytes(PyObject *encoded_string, unsigned char **text) {
/* Allocates *text, returns a 'new reference'. Caller is required to free */
PyObject *bytes = NULL;
Py_ssize_t len = 0;
char *buffer;
*text = NULL;
if (PyUnicode_CheckExact(encoded_string)) {
bytes = PyUnicode_AsLatin1String(encoded_string);
if (!bytes) {
return;
}
PyBytes_AsStringAndSize(bytes, &buffer, &len);
} else if (PyBytes_Check(encoded_string)) {
PyBytes_AsStringAndSize(encoded_string, &buffer, &len);
}
*text = calloc(len + 1, 1);
if (*text) {
memcpy(*text, buffer, len);
} else {
ImagingError_MemoryError();
}
if (bytes) {
Py_DECREF(bytes);
}
return;
}
static PyObject *
_font_getmask(ImagingFontObject *self, PyObject *args) {
Imaging im;
Imaging bitmap;
int x, b;
int i = 0;
int status;
Glyph *glyph;
PyObject *encoded_string;
unsigned char *text;
char *mode = "";
if (!PyArg_ParseTuple(args, "O|s:getmask", &encoded_string, &mode)) {
return NULL;
}
_font_text_asBytes(encoded_string, &text);
if (!text) {
return NULL;
}
im = ImagingNew(self->bitmap->mode, textwidth(self, text), self->ysize);
if (!im) {
free(text);
return ImagingError_MemoryError();
}
b = 0;
(void)ImagingFill(im, &b);
b = self->baseline;
for (x = 0; text[i]; i++) {
glyph = &self->glyphs[text[i]];
bitmap =
ImagingCrop(self->bitmap, glyph->sx0, glyph->sy0, glyph->sx1, glyph->sy1);
if (!bitmap) {
goto failed;
}
status = ImagingPaste(
im,
bitmap,
NULL,
glyph->dx0 + x,
glyph->dy0 + b,
glyph->dx1 + x,
glyph->dy1 + b);
ImagingDelete(bitmap);
if (status < 0) {
goto failed;
}
x = x + glyph->dx;
b = b + glyph->dy;
}
free(text);
return PyImagingNew(im);
failed:
free(text);
ImagingDelete(im);
Py_RETURN_NONE;
}
static PyObject *
_font_getsize(ImagingFontObject *self, PyObject *args) {
unsigned char *text;
PyObject *encoded_string;
PyObject *val;
if (!PyArg_ParseTuple(args, "O:getsize", &encoded_string)) {
return NULL;
}
_font_text_asBytes(encoded_string, &text);
if (!text) {
return NULL;
}
val = Py_BuildValue("ii", textwidth(self, text), self->ysize);
free(text);
return val;
}
static struct PyMethodDef _font_methods[] = {
{"getmask", (PyCFunction)_font_getmask, METH_VARARGS},
{"getsize", (PyCFunction)_font_getsize, METH_VARARGS},
{NULL, NULL} /* sentinel */
};
/* -------------------------------------------------------------------- */
static PyObject *
_draw_new(PyObject *self_, PyObject *args) {
ImagingDrawObject *self;
ImagingObject *imagep;
int blend = 0;
if (!PyArg_ParseTuple(args, "O!|i", &Imaging_Type, &imagep, &blend)) {
return NULL;
}
self = PyObject_New(ImagingDrawObject, &ImagingDraw_Type);
if (self == NULL) {
return NULL;
}
/* keep a reference to the image object */
Py_INCREF(imagep);
self->image = imagep;
self->ink[0] = self->ink[1] = self->ink[2] = self->ink[3] = 0;
self->blend = blend;
return (PyObject *)self;
}
static void
_draw_dealloc(ImagingDrawObject *self) {
Py_XDECREF(self->image);
PyObject_Del(self);
}
extern Py_ssize_t
PyPath_Flatten(PyObject *data, double **xy);
static PyObject *
_draw_ink(ImagingDrawObject *self, PyObject *args) {
INT32 ink = 0;
PyObject *color;
if (!PyArg_ParseTuple(args, "O", &color)) {
return NULL;
}
if (!getink(color, self->image->image, (char *)&ink)) {
return NULL;
}
return PyLong_FromLong((int)ink);
}
static PyObject *
_draw_arc(ImagingDrawObject *self, PyObject *args) {
double *xy;
Py_ssize_t n;
PyObject *data;
int ink;
int width = 0;
float start, end;
if (!PyArg_ParseTuple(args, "Offi|i", &data, &start, &end, &ink, &width)) {
return NULL;
}
n = PyPath_Flatten(data, &xy);
if (n < 0) {
return NULL;
}
if (n != 2) {
PyErr_SetString(PyExc_TypeError, must_be_two_coordinates);
free(xy);
return NULL;
}
if (xy[2] < xy[0]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_x_coordinate);
free(xy);
return NULL;
}
if (xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_y_coordinate);
free(xy);
return NULL;
}
n = ImagingDrawArc(
self->image->image,
(int)xy[0],
(int)xy[1],
(int)xy[2],
(int)xy[3],
start,
end,
&ink,
width,
self->blend);
free(xy);
if (n < 0) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_draw_bitmap(ImagingDrawObject *self, PyObject *args) {
double *xy;
Py_ssize_t n;
PyObject *data;
ImagingObject *bitmap;
int ink;
if (!PyArg_ParseTuple(args, "OO!i", &data, &Imaging_Type, &bitmap, &ink)) {
return NULL;
}
n = PyPath_Flatten(data, &xy);
if (n < 0) {
return NULL;
}
if (n != 1) {
PyErr_SetString(
PyExc_TypeError, "coordinate list must contain exactly 1 coordinate");
free(xy);
return NULL;
}
n = ImagingDrawBitmap(
self->image->image, (int)xy[0], (int)xy[1], bitmap->image, &ink, self->blend);
free(xy);
if (n < 0) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_draw_chord(ImagingDrawObject *self, PyObject *args) {
double *xy;
Py_ssize_t n;
PyObject *data;
int ink, fill;
int width = 0;
float start, end;
if (!PyArg_ParseTuple(args, "Offii|i", &data, &start, &end, &ink, &fill, &width)) {
return NULL;
}
n = PyPath_Flatten(data, &xy);
if (n < 0) {
return NULL;
}
if (n != 2) {
PyErr_SetString(PyExc_TypeError, must_be_two_coordinates);
free(xy);
return NULL;
}
if (xy[2] < xy[0]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_x_coordinate);
free(xy);
return NULL;
}
if (xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_y_coordinate);
free(xy);
return NULL;
}
n = ImagingDrawChord(
self->image->image,
(int)xy[0],
(int)xy[1],
(int)xy[2],
(int)xy[3],
start,
end,
&ink,
fill,
width,
self->blend);
free(xy);
if (n < 0) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_draw_ellipse(ImagingDrawObject *self, PyObject *args) {
double *xy;
Py_ssize_t n;
PyObject *data;
int ink;
int fill = 0;
int width = 0;
if (!PyArg_ParseTuple(args, "Oi|ii", &data, &ink, &fill, &width)) {
return NULL;
}
n = PyPath_Flatten(data, &xy);
if (n < 0) {
return NULL;
}
if (n != 2) {
PyErr_SetString(PyExc_TypeError, must_be_two_coordinates);
free(xy);
return NULL;
}
if (xy[2] < xy[0]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_x_coordinate);
free(xy);
return NULL;
}
if (xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_y_coordinate);
free(xy);
return NULL;
}
n = ImagingDrawEllipse(
self->image->image,
(int)xy[0],
(int)xy[1],
(int)xy[2],
(int)xy[3],
&ink,
fill,
width,
self->blend);
free(xy);
if (n < 0) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_draw_lines(ImagingDrawObject *self, PyObject *args) {
double *xy;
Py_ssize_t i, n;
PyObject *data;
int ink;
int width = 0;
if (!PyArg_ParseTuple(args, "Oi|i", &data, &ink, &width)) {
return NULL;
}
n = PyPath_Flatten(data, &xy);
if (n < 0) {
return NULL;
}
if (width <= 1) {
double *p = NULL;
for (i = 0; i < n - 1; i++) {
p = &xy[i + i];
if (ImagingDrawLine(
self->image->image,
(int)p[0],
(int)p[1],
(int)p[2],
(int)p[3],
&ink,
self->blend) < 0) {
free(xy);
return NULL;
}
}
if (p) { /* draw last point */
ImagingDrawPoint(
self->image->image, (int)p[2], (int)p[3], &ink, self->blend);
}
} else {
for (i = 0; i < n - 1; i++) {
double *p = &xy[i + i];
if (ImagingDrawWideLine(
self->image->image,
(int)p[0],
(int)p[1],
(int)p[2],
(int)p[3],
&ink,
width,
self->blend) < 0) {
free(xy);
return NULL;
}
}
}
free(xy);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_draw_points(ImagingDrawObject *self, PyObject *args) {
double *xy;
Py_ssize_t i, n;
PyObject *data;
int ink;
if (!PyArg_ParseTuple(args, "Oi", &data, &ink)) {
return NULL;
}
n = PyPath_Flatten(data, &xy);
if (n < 0) {
return NULL;
}
for (i = 0; i < n; i++) {
double *p = &xy[i + i];
if (ImagingDrawPoint(
self->image->image, (int)p[0], (int)p[1], &ink, self->blend) < 0) {
free(xy);
return NULL;
}
}
free(xy);
Py_INCREF(Py_None);
return Py_None;
}
#ifdef WITH_ARROW
/* from outline.c */
extern ImagingOutline
PyOutline_AsOutline(PyObject *outline);
static PyObject *
_draw_outline(ImagingDrawObject *self, PyObject *args) {
ImagingOutline outline;
PyObject *outline_;
int ink;
int fill = 0;
if (!PyArg_ParseTuple(args, "Oi|i", &outline_, &ink, &fill)) {
return NULL;
}
outline = PyOutline_AsOutline(outline_);
if (!outline) {
PyErr_SetString(PyExc_TypeError, "expected outline object");
return NULL;
}
if (ImagingDrawOutline(self->image->image, outline, &ink, fill, self->blend) < 0) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
#endif
static PyObject *
_draw_pieslice(ImagingDrawObject *self, PyObject *args) {
double *xy;
Py_ssize_t n;
PyObject *data;
int ink, fill;
int width = 0;
float start, end;
if (!PyArg_ParseTuple(args, "Offii|i", &data, &start, &end, &ink, &fill, &width)) {
return NULL;
}
n = PyPath_Flatten(data, &xy);
if (n < 0) {
return NULL;
}
if (n != 2) {
PyErr_SetString(PyExc_TypeError, must_be_two_coordinates);
free(xy);
return NULL;
}
if (xy[2] < xy[0]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_x_coordinate);
free(xy);
return NULL;
}
if (xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_y_coordinate);
free(xy);
return NULL;
}
n = ImagingDrawPieslice(
self->image->image,
(int)xy[0],
(int)xy[1],
(int)xy[2],
(int)xy[3],
start,
end,
&ink,
fill,
width,
self->blend);
free(xy);
if (n < 0) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_draw_polygon(ImagingDrawObject *self, PyObject *args) {
double *xy;
int *ixy;
Py_ssize_t n, i;
PyObject *data;
int ink;
int fill = 0;
int width = 0;
if (!PyArg_ParseTuple(args, "Oi|ii", &data, &ink, &fill, &width)) {
return NULL;
}
n = PyPath_Flatten(data, &xy);
if (n < 0) {
return NULL;
}
if (n < 2) {
PyErr_SetString(
PyExc_TypeError, "coordinate list must contain at least 2 coordinates");
free(xy);
return NULL;
}
/* Copy list of vertices to array */
ixy = (int *)calloc(n, 2 * sizeof(int));
if (ixy == NULL) {
free(xy);
return ImagingError_MemoryError();
}
for (i = 0; i < n; i++) {
ixy[i + i] = (int)xy[i + i];
ixy[i + i + 1] = (int)xy[i + i + 1];
}
free(xy);
if (ImagingDrawPolygon(self->image->image, n, ixy, &ink, fill, width, self->blend) < 0) {
free(ixy);
return NULL;
}
free(ixy);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_draw_rectangle(ImagingDrawObject *self, PyObject *args) {
double *xy;
Py_ssize_t n;
PyObject *data;
int ink;
int fill = 0;
int width = 0;
if (!PyArg_ParseTuple(args, "Oi|ii", &data, &ink, &fill, &width)) {
return NULL;
}
n = PyPath_Flatten(data, &xy);
if (n < 0) {
return NULL;
}
if (n != 2) {
PyErr_SetString(PyExc_TypeError, must_be_two_coordinates);
free(xy);
return NULL;
}
if (xy[2] < xy[0]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_x_coordinate);
free(xy);
return NULL;
}
if (xy[3] < xy[1]) {
PyErr_SetString(PyExc_ValueError, incorrectly_ordered_y_coordinate);
free(xy);
return NULL;
}
n = ImagingDrawRectangle(
self->image->image,
(int)xy[0],
(int)xy[1],
(int)xy[2],
(int)xy[3],
&ink,
fill,
width,
self->blend);
free(xy);
if (n < 0) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static struct PyMethodDef _draw_methods[] = {
#ifdef WITH_IMAGEDRAW
/* Graphics (ImageDraw) */
{"draw_lines", (PyCFunction)_draw_lines, METH_VARARGS},
#ifdef WITH_ARROW
{"draw_outline", (PyCFunction)_draw_outline, METH_VARARGS},
#endif
{"draw_polygon", (PyCFunction)_draw_polygon, METH_VARARGS},
{"draw_rectangle", (PyCFunction)_draw_rectangle, METH_VARARGS},
{"draw_points", (PyCFunction)_draw_points, METH_VARARGS},
{"draw_arc", (PyCFunction)_draw_arc, METH_VARARGS},
{"draw_bitmap", (PyCFunction)_draw_bitmap, METH_VARARGS},
{"draw_chord", (PyCFunction)_draw_chord, METH_VARARGS},
{"draw_ellipse", (PyCFunction)_draw_ellipse, METH_VARARGS},
{"draw_pieslice", (PyCFunction)_draw_pieslice, METH_VARARGS},
{"draw_ink", (PyCFunction)_draw_ink, METH_VARARGS},
#endif
{NULL, NULL} /* sentinel */
};
#endif
static PyObject *
pixel_access_new(ImagingObject *imagep, PyObject *args) {
PixelAccessObject *self;
int readonly = 0;
if (!PyArg_ParseTuple(args, "|i", &readonly)) {
return NULL;
}
self = PyObject_New(PixelAccessObject, &PixelAccess_Type);
if (self == NULL) {
return NULL;
}
/* keep a reference to the image object */
Py_INCREF(imagep);
self->image = imagep;
self->readonly = readonly;
return (PyObject *)self;
}
static void
pixel_access_dealloc(PixelAccessObject *self) {
Py_XDECREF(self->image);
PyObject_Del(self);
}
static PyObject *
pixel_access_getitem(PixelAccessObject *self, PyObject *xy) {
int x, y;
if (_getxy(xy, &x, &y)) {
return NULL;
}
return getpixel(self->image->image, self->image->access, x, y);
}
static int
pixel_access_setitem(PixelAccessObject *self, PyObject *xy, PyObject *color) {
Imaging im = self->image->image;
char ink[4];
int x, y;
if (self->readonly) {
(void)ImagingError_ValueError(readonly);
return -1;
}
if (_getxy(xy, &x, &y)) {
return -1;
}
if (x < 0) {
x = im->xsize + x;
}
if (y < 0) {
y = im->ysize + y;
}
if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
PyErr_SetString(PyExc_IndexError, outside_image);
return -1;
}
if (!color) { /* FIXME: raise exception? */
return 0;
}
if (!getink(color, im, ink)) {
return -1;
}
self->image->access->put_pixel(im, x, y, ink);
return 0;
}
/* -------------------------------------------------------------------- */
/* EFFECTS (experimental) */
/* -------------------------------------------------------------------- */
#ifdef WITH_EFFECTS
static PyObject *
_effect_mandelbrot(ImagingObject *self, PyObject *args) {
int xsize = 512;
int ysize = 512;
double extent[4];
int quality = 100;
extent[0] = -3;
extent[1] = -2.5;
extent[2] = 2;
extent[3] = 2.5;
if (!PyArg_ParseTuple(
args,
"|(ii)(dddd)i",
&xsize,
&ysize,
&extent[0],
&extent[1],
&extent[2],
&extent[3],
&quality)) {
return NULL;
}
return PyImagingNew(ImagingEffectMandelbrot(xsize, ysize, extent, quality));
}
static PyObject *
_effect_noise(ImagingObject *self, PyObject *args) {
int xsize, ysize;
float sigma = 128;
if (!PyArg_ParseTuple(args, "(ii)|f", &xsize, &ysize, &sigma)) {
return NULL;
}
return PyImagingNew(ImagingEffectNoise(xsize, ysize, sigma));
}
static PyObject *
_effect_spread(ImagingObject *self, PyObject *args) {
int dist;
if (!PyArg_ParseTuple(args, "i", &dist)) {
return NULL;
}
return PyImagingNew(ImagingEffectSpread(self->image, dist));
}
#endif
/* -------------------------------------------------------------------- */
/* UTILITIES */
/* -------------------------------------------------------------------- */
static PyObject *
_getcodecstatus(PyObject *self, PyObject *args) {
int status;
char *msg;
if (!PyArg_ParseTuple(args, "i", &status)) {
return NULL;
}
switch (status) {
case IMAGING_CODEC_OVERRUN:
msg = "buffer overrun";
break;
case IMAGING_CODEC_BROKEN:
msg = "broken data stream";
break;
case IMAGING_CODEC_UNKNOWN:
msg = "unrecognized data stream contents";
break;
case IMAGING_CODEC_CONFIG:
msg = "codec configuration error";
break;
case IMAGING_CODEC_MEMORY:
msg = "out of memory";
break;
default:
Py_RETURN_NONE;
}
return PyUnicode_FromString(msg);
}
/* -------------------------------------------------------------------- */
/* DEBUGGING HELPERS */
/* -------------------------------------------------------------------- */
static PyObject *
_save_ppm(ImagingObject *self, PyObject *args) {
char *filename;
if (!PyArg_ParseTuple(args, "s", &filename)) {
return NULL;
}
if (!ImagingSavePPM(self->image, filename)) {
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
/* -------------------------------------------------------------------- */
/* methods */
static struct PyMethodDef methods[] = {
/* Put commonly used methods first */
{"getpixel", (PyCFunction)_getpixel, METH_VARARGS},
{"putpixel", (PyCFunction)_putpixel, METH_VARARGS},
{"pixel_access", (PyCFunction)pixel_access_new, METH_VARARGS},
/* Standard processing methods (Image) */
{"color_lut_3d", (PyCFunction)_color_lut_3d, METH_VARARGS},
{"convert", (PyCFunction)_convert, METH_VARARGS},
{"convert2", (PyCFunction)_convert2, METH_VARARGS},
{"convert_matrix", (PyCFunction)_convert_matrix, METH_VARARGS},
{"convert_transparent", (PyCFunction)_convert_transparent, METH_VARARGS},
{"copy", (PyCFunction)_copy, METH_VARARGS},
{"crop", (PyCFunction)_crop, METH_VARARGS},
{"expand", (PyCFunction)_expand_image, METH_VARARGS},
{"filter", (PyCFunction)_filter, METH_VARARGS},
{"histogram", (PyCFunction)_histogram, METH_VARARGS},
{"entropy", (PyCFunction)_entropy, METH_VARARGS},
#ifdef WITH_MODEFILTER
{"modefilter", (PyCFunction)_modefilter, METH_VARARGS},
#endif
{"offset", (PyCFunction)_offset, METH_VARARGS},
{"paste", (PyCFunction)_paste, METH_VARARGS},
{"point", (PyCFunction)_point, METH_VARARGS},
{"point_transform", (PyCFunction)_point_transform, METH_VARARGS},
{"putdata", (PyCFunction)_putdata, METH_VARARGS},
#ifdef WITH_QUANTIZE
{"quantize", (PyCFunction)_quantize, METH_VARARGS},
#endif
#ifdef WITH_RANKFILTER
{"rankfilter", (PyCFunction)_rankfilter, METH_VARARGS},
#endif
{"resize", (PyCFunction)_resize, METH_VARARGS},
{"reduce", (PyCFunction)_reduce, METH_VARARGS},
{"transpose", (PyCFunction)_transpose, METH_VARARGS},
{"transform2", (PyCFunction)_transform2, METH_VARARGS},
{"isblock", (PyCFunction)_isblock, METH_NOARGS},
{"getbbox", (PyCFunction)_getbbox, METH_VARARGS},
{"getcolors", (PyCFunction)_getcolors, METH_VARARGS},
{"getextrema", (PyCFunction)_getextrema, METH_NOARGS},
{"getprojection", (PyCFunction)_getprojection, METH_NOARGS},
{"getband", (PyCFunction)_getband, METH_VARARGS},
{"putband", (PyCFunction)_putband, METH_VARARGS},
{"split", (PyCFunction)_split, METH_NOARGS},
{"fillband", (PyCFunction)_fillband, METH_VARARGS},
{"setmode", (PyCFunction)im_setmode, METH_VARARGS},
{"getpalette", (PyCFunction)_getpalette, METH_VARARGS},
{"getpalettemode", (PyCFunction)_getpalettemode, METH_NOARGS},
{"putpalette", (PyCFunction)_putpalette, METH_VARARGS},
{"putpalettealpha", (PyCFunction)_putpalettealpha, METH_VARARGS},
{"putpalettealphas", (PyCFunction)_putpalettealphas, METH_VARARGS},
#ifdef WITH_IMAGECHOPS
/* Channel operations (ImageChops) */
{"chop_invert", (PyCFunction)_chop_invert, METH_NOARGS},
{"chop_lighter", (PyCFunction)_chop_lighter, METH_VARARGS},
{"chop_darker", (PyCFunction)_chop_darker, METH_VARARGS},
{"chop_difference", (PyCFunction)_chop_difference, METH_VARARGS},
{"chop_multiply", (PyCFunction)_chop_multiply, METH_VARARGS},
{"chop_screen", (PyCFunction)_chop_screen, METH_VARARGS},
{"chop_add", (PyCFunction)_chop_add, METH_VARARGS},
{"chop_subtract", (PyCFunction)_chop_subtract, METH_VARARGS},
{"chop_add_modulo", (PyCFunction)_chop_add_modulo, METH_VARARGS},
{"chop_subtract_modulo", (PyCFunction)_chop_subtract_modulo, METH_VARARGS},
{"chop_and", (PyCFunction)_chop_and, METH_VARARGS},
{"chop_or", (PyCFunction)_chop_or, METH_VARARGS},
{"chop_xor", (PyCFunction)_chop_xor, METH_VARARGS},
{"chop_soft_light", (PyCFunction)_chop_soft_light, METH_VARARGS},
{"chop_hard_light", (PyCFunction)_chop_hard_light, METH_VARARGS},
{"chop_overlay", (PyCFunction)_chop_overlay, METH_VARARGS},
#endif
#ifdef WITH_UNSHARPMASK
/* Kevin Cazabon's unsharpmask extension */
{"gaussian_blur", (PyCFunction)_gaussian_blur, METH_VARARGS},
{"unsharp_mask", (PyCFunction)_unsharp_mask, METH_VARARGS},
#endif
{"box_blur", (PyCFunction)_box_blur, METH_VARARGS},
#ifdef WITH_EFFECTS
/* Special effects */
{"effect_spread", (PyCFunction)_effect_spread, METH_VARARGS},
#endif
/* Misc. */
{"new_block", (PyCFunction)_new_block, METH_VARARGS},
{"save_ppm", (PyCFunction)_save_ppm, METH_VARARGS},
{NULL, NULL} /* sentinel */
};
/* attributes */
static PyObject *
_getattr_mode(ImagingObject *self, void *closure) {
return PyUnicode_FromString(self->image->mode);
}
static PyObject *
_getattr_size(ImagingObject *self, void *closure) {
return Py_BuildValue("ii", self->image->xsize, self->image->ysize);
}
static PyObject *
_getattr_bands(ImagingObject *self, void *closure) {
return PyLong_FromLong(self->image->bands);
}
static PyObject *
_getattr_id(ImagingObject *self, void *closure) {
return PyLong_FromSsize_t((Py_ssize_t)self->image);
}
static PyObject *
_getattr_ptr(ImagingObject *self, void *closure) {
return PyCapsule_New(self->image, IMAGING_MAGIC, NULL);
}
static PyObject *
_getattr_unsafe_ptrs(ImagingObject *self, void *closure) {
return Py_BuildValue(
"(sn)(sn)(sn)",
"image8",
self->image->image8,
"image32",
self->image->image32,
"image",
self->image->image);
};
static struct PyGetSetDef getsetters[] = {
{"mode", (getter)_getattr_mode},
{"size", (getter)_getattr_size},
{"bands", (getter)_getattr_bands},
{"id", (getter)_getattr_id},
{"ptr", (getter)_getattr_ptr},
{"unsafe_ptrs", (getter)_getattr_unsafe_ptrs},
{NULL}};
/* basic sequence semantics */
static Py_ssize_t
image_length(ImagingObject *self) {
Imaging im = self->image;
return (Py_ssize_t)im->xsize * im->ysize;
}
static PyObject *
image_item(ImagingObject *self, Py_ssize_t i) {
int x, y;
Imaging im = self->image;
if (im->xsize > 0) {
x = i % im->xsize;
y = i / im->xsize;
} else {
x = y = 0; /* leave it to getpixel to raise an exception */
}
return getpixel(im, self->access, x, y);
}
static PySequenceMethods image_as_sequence = {
(lenfunc)image_length, /*sq_length*/
(binaryfunc)NULL, /*sq_concat*/
(ssizeargfunc)NULL, /*sq_repeat*/
(ssizeargfunc)image_item, /*sq_item*/
(ssizessizeargfunc)NULL, /*sq_slice*/
(ssizeobjargproc)NULL, /*sq_ass_item*/
(ssizessizeobjargproc)NULL, /*sq_ass_slice*/
};
/* type description */
static PyTypeObject Imaging_Type = {
PyVarObject_HEAD_INIT(NULL, 0) "ImagingCore", /*tp_name*/
sizeof(ImagingObject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)_dealloc, /*tp_dealloc*/
0, /*tp_vectorcall_offset*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_as_async*/
0, /*tp_repr*/
0, /*tp_as_number*/
&image_as_sequence, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
methods, /*tp_methods*/
0, /*tp_members*/
getsetters, /*tp_getset*/
};
#ifdef WITH_IMAGEDRAW
static PyTypeObject ImagingFont_Type = {
PyVarObject_HEAD_INIT(NULL, 0) "ImagingFont", /*tp_name*/
sizeof(ImagingFontObject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)_font_dealloc, /*tp_dealloc*/
0, /*tp_vectorcall_offset*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_as_async*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
_font_methods, /*tp_methods*/
0, /*tp_members*/
0, /*tp_getset*/
};
static PyTypeObject ImagingDraw_Type = {
PyVarObject_HEAD_INIT(NULL, 0) "ImagingDraw", /*tp_name*/
sizeof(ImagingDrawObject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)_draw_dealloc, /*tp_dealloc*/
0, /*tp_vectorcall_offset*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_as_async*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
_draw_methods, /*tp_methods*/
0, /*tp_members*/
0, /*tp_getset*/
};
#endif
static PyMappingMethods pixel_access_as_mapping = {
(lenfunc)NULL, /*mp_length*/
(binaryfunc)pixel_access_getitem, /*mp_subscript*/
(objobjargproc)pixel_access_setitem, /*mp_ass_subscript*/
};
/* type description */
static PyTypeObject PixelAccess_Type = {
PyVarObject_HEAD_INIT(NULL, 0) "PixelAccess", /*tp_name*/
sizeof(PixelAccessObject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)pixel_access_dealloc, /*tp_dealloc*/
0, /*tp_vectorcall_offset*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_as_async*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
&pixel_access_as_mapping, /*tp_as_mapping*/
0 /*tp_hash*/
};
/* -------------------------------------------------------------------- */
static PyObject *
_get_stats(PyObject *self, PyObject *args) {
PyObject *d;
PyObject *v;
ImagingMemoryArena arena = &ImagingDefaultArena;
if (!PyArg_ParseTuple(args, ":get_stats")) {
return NULL;
}
d = PyDict_New();
if (!d) {
return NULL;
}
v = PyLong_FromLong(arena->stats_new_count);
PyDict_SetItemString(d, "new_count", v ? v : Py_None);
Py_XDECREF(v);
v = PyLong_FromLong(arena->stats_allocated_blocks);
PyDict_SetItemString(d, "allocated_blocks", v ? v : Py_None);
Py_XDECREF(v);
v = PyLong_FromLong(arena->stats_reused_blocks);
PyDict_SetItemString(d, "reused_blocks", v ? v : Py_None);
Py_XDECREF(v);
v = PyLong_FromLong(arena->stats_reallocated_blocks);
PyDict_SetItemString(d, "reallocated_blocks", v ? v : Py_None);
Py_XDECREF(v);
v = PyLong_FromLong(arena->stats_freed_blocks);
PyDict_SetItemString(d, "freed_blocks", v ? v : Py_None);
Py_XDECREF(v);
v = PyLong_FromLong(arena->blocks_cached);
PyDict_SetItemString(d, "blocks_cached", v ? v : Py_None);
Py_XDECREF(v);
return d;
}
static PyObject *
_reset_stats(PyObject *self, PyObject *args) {
ImagingMemoryArena arena = &ImagingDefaultArena;
if (!PyArg_ParseTuple(args, ":reset_stats")) {
return NULL;
}
arena->stats_new_count = 0;
arena->stats_allocated_blocks = 0;
arena->stats_reused_blocks = 0;
arena->stats_reallocated_blocks = 0;
arena->stats_freed_blocks = 0;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_get_alignment(PyObject *self, PyObject *args) {
if (!PyArg_ParseTuple(args, ":get_alignment")) {
return NULL;
}
return PyLong_FromLong(ImagingDefaultArena.alignment);
}
static PyObject *
_get_block_size(PyObject *self, PyObject *args) {
if (!PyArg_ParseTuple(args, ":get_block_size")) {
return NULL;
}
return PyLong_FromLong(ImagingDefaultArena.block_size);
}
static PyObject *
_get_blocks_max(PyObject *self, PyObject *args) {
if (!PyArg_ParseTuple(args, ":get_blocks_max")) {
return NULL;
}
return PyLong_FromLong(ImagingDefaultArena.blocks_max);
}
static PyObject *
_set_alignment(PyObject *self, PyObject *args) {
int alignment;
if (!PyArg_ParseTuple(args, "i:set_alignment", &alignment)) {
return NULL;
}
if (alignment < 1 || alignment > 128) {
PyErr_SetString(PyExc_ValueError, "alignment should be from 1 to 128");
return NULL;
}
/* Is power of two */
if (alignment & (alignment - 1)) {
PyErr_SetString(PyExc_ValueError, "alignment should be power of two");
return NULL;
}
ImagingDefaultArena.alignment = alignment;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_set_block_size(PyObject *self, PyObject *args) {
int block_size;
if (!PyArg_ParseTuple(args, "i:set_block_size", &block_size)) {
return NULL;
}
if (block_size <= 0) {
PyErr_SetString(PyExc_ValueError, "block_size should be greater than 0");
return NULL;
}
if (block_size & 0xfff) {
PyErr_SetString(PyExc_ValueError, "block_size should be multiple of 4096");
return NULL;
}
ImagingDefaultArena.block_size = block_size;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_set_blocks_max(PyObject *self, PyObject *args) {
int blocks_max;
if (!PyArg_ParseTuple(args, "i:set_blocks_max", &blocks_max)) {
return NULL;
}
if (blocks_max < 0) {
PyErr_SetString(PyExc_ValueError, "blocks_max should be greater than 0");
return NULL;
} else if (
(unsigned long)blocks_max >
SIZE_MAX / sizeof(ImagingDefaultArena.blocks_pool[0])) {
PyErr_SetString(PyExc_ValueError, "blocks_max is too large");
return NULL;
}
if (!ImagingMemorySetBlocksMax(&ImagingDefaultArena, blocks_max)) {
return ImagingError_MemoryError();
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_clear_cache(PyObject *self, PyObject *args) {
int i = 0;
if (!PyArg_ParseTuple(args, "|i:clear_cache", &i)) {
return NULL;
}
ImagingMemoryClearCache(&ImagingDefaultArena, i);
Py_INCREF(Py_None);
return Py_None;
}
/* -------------------------------------------------------------------- */
/* FIXME: this is something of a mess. Should replace this with
pluggable codecs, but not before PIL 1.2 */
/* Decoders (in decode.c) */
extern PyObject *
PyImaging_BcnDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_BitDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_FliDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_GifDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_HexDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_JpegDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_Jpeg2KDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_LibTiffDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_PackbitsDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_PcdDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_PcxDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_RawDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_SgiRleDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_SunRleDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_TgaRleDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_XbmDecoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_ZipDecoderNew(PyObject *self, PyObject *args);
/* Encoders (in encode.c) */
extern PyObject *
PyImaging_EpsEncoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_GifEncoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_JpegEncoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_PcxEncoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_RawEncoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_TgaRleEncoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_XbmEncoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_ZipEncoderNew(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args);
/* Display support etc (in display.c) */
#ifdef _WIN32
extern PyObject *
PyImaging_CreateWindowWin32(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_DisplayWin32(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_DisplayModeWin32(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_GrabScreenWin32(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_GrabClipboardWin32(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_EventLoopWin32(PyObject *self, PyObject *args);
extern PyObject *
PyImaging_DrawWmf(PyObject *self, PyObject *args);
#endif
#ifdef HAVE_XCB
extern PyObject *
PyImaging_GrabScreenX11(PyObject *self, PyObject *args);
#endif
/* Experimental path stuff (in path.c) */
extern PyObject *
PyPath_Create(ImagingObject *self, PyObject *args);
/* Experimental outline stuff (in outline.c) */
extern PyObject *
PyOutline_Create(ImagingObject *self, PyObject *args);
extern PyObject *
PyImaging_MapBuffer(PyObject *self, PyObject *args);
static PyMethodDef functions[] = {
/* Object factories */
{"alpha_composite", (PyCFunction)_alpha_composite, METH_VARARGS},
{"blend", (PyCFunction)_blend, METH_VARARGS},
{"fill", (PyCFunction)_fill, METH_VARARGS},
{"new", (PyCFunction)_new, METH_VARARGS},
{"merge", (PyCFunction)_merge, METH_VARARGS},
/* Functions */
{"convert", (PyCFunction)_convert2, METH_VARARGS},
/* Codecs */
{"bcn_decoder", (PyCFunction)PyImaging_BcnDecoderNew, METH_VARARGS},
{"bit_decoder", (PyCFunction)PyImaging_BitDecoderNew, METH_VARARGS},
{"eps_encoder", (PyCFunction)PyImaging_EpsEncoderNew, METH_VARARGS},
{"fli_decoder", (PyCFunction)PyImaging_FliDecoderNew, METH_VARARGS},
{"gif_decoder", (PyCFunction)PyImaging_GifDecoderNew, METH_VARARGS},
{"gif_encoder", (PyCFunction)PyImaging_GifEncoderNew, METH_VARARGS},
{"hex_decoder", (PyCFunction)PyImaging_HexDecoderNew, METH_VARARGS},
{"hex_encoder", (PyCFunction)PyImaging_EpsEncoderNew, METH_VARARGS}, /* EPS=HEX! */
#ifdef HAVE_LIBJPEG
{"jpeg_decoder", (PyCFunction)PyImaging_JpegDecoderNew, METH_VARARGS},
{"jpeg_encoder", (PyCFunction)PyImaging_JpegEncoderNew, METH_VARARGS},
#endif
#ifdef HAVE_OPENJPEG
{"jpeg2k_decoder", (PyCFunction)PyImaging_Jpeg2KDecoderNew, METH_VARARGS},
{"jpeg2k_encoder", (PyCFunction)PyImaging_Jpeg2KEncoderNew, METH_VARARGS},
#endif
#ifdef HAVE_LIBTIFF
{"libtiff_decoder", (PyCFunction)PyImaging_LibTiffDecoderNew, METH_VARARGS},
{"libtiff_encoder", (PyCFunction)PyImaging_LibTiffEncoderNew, METH_VARARGS},
#endif
{"packbits_decoder", (PyCFunction)PyImaging_PackbitsDecoderNew, METH_VARARGS},
{"pcd_decoder", (PyCFunction)PyImaging_PcdDecoderNew, METH_VARARGS},
{"pcx_decoder", (PyCFunction)PyImaging_PcxDecoderNew, METH_VARARGS},
{"pcx_encoder", (PyCFunction)PyImaging_PcxEncoderNew, METH_VARARGS},
{"raw_decoder", (PyCFunction)PyImaging_RawDecoderNew, METH_VARARGS},
{"raw_encoder", (PyCFunction)PyImaging_RawEncoderNew, METH_VARARGS},
{"sgi_rle_decoder", (PyCFunction)PyImaging_SgiRleDecoderNew, METH_VARARGS},
{"sun_rle_decoder", (PyCFunction)PyImaging_SunRleDecoderNew, METH_VARARGS},
{"tga_rle_decoder", (PyCFunction)PyImaging_TgaRleDecoderNew, METH_VARARGS},
{"tga_rle_encoder", (PyCFunction)PyImaging_TgaRleEncoderNew, METH_VARARGS},
{"xbm_decoder", (PyCFunction)PyImaging_XbmDecoderNew, METH_VARARGS},
{"xbm_encoder", (PyCFunction)PyImaging_XbmEncoderNew, METH_VARARGS},
#ifdef HAVE_LIBZ
{"zip_decoder", (PyCFunction)PyImaging_ZipDecoderNew, METH_VARARGS},
{"zip_encoder", (PyCFunction)PyImaging_ZipEncoderNew, METH_VARARGS},
#endif
/* Memory mapping */
#ifdef WITH_MAPPING
{"map_buffer", (PyCFunction)PyImaging_MapBuffer, METH_VARARGS},
#endif
/* Display support */
#ifdef _WIN32
{"display", (PyCFunction)PyImaging_DisplayWin32, METH_VARARGS},
{"display_mode", (PyCFunction)PyImaging_DisplayModeWin32, METH_VARARGS},
{"grabscreen_win32", (PyCFunction)PyImaging_GrabScreenWin32, METH_VARARGS},
{"grabclipboard_win32", (PyCFunction)PyImaging_GrabClipboardWin32, METH_VARARGS},
{"createwindow", (PyCFunction)PyImaging_CreateWindowWin32, METH_VARARGS},
{"eventloop", (PyCFunction)PyImaging_EventLoopWin32, METH_VARARGS},
{"drawwmf", (PyCFunction)PyImaging_DrawWmf, METH_VARARGS},
#endif
#ifdef HAVE_XCB
{"grabscreen_x11", (PyCFunction)PyImaging_GrabScreenX11, METH_VARARGS},
#endif
/* Utilities */
{"getcodecstatus", (PyCFunction)_getcodecstatus, METH_VARARGS},
/* Special effects (experimental) */
#ifdef WITH_EFFECTS
{"effect_mandelbrot", (PyCFunction)_effect_mandelbrot, METH_VARARGS},
{"effect_noise", (PyCFunction)_effect_noise, METH_VARARGS},
{"linear_gradient", (PyCFunction)_linear_gradient, METH_VARARGS},
{"radial_gradient", (PyCFunction)_radial_gradient, METH_VARARGS},
{"wedge", (PyCFunction)_linear_gradient, METH_VARARGS}, /* Compatibility */
#endif
/* Drawing support stuff */
#ifdef WITH_IMAGEDRAW
{"font", (PyCFunction)_font_new, METH_VARARGS},
{"draw", (PyCFunction)_draw_new, METH_VARARGS},
#endif
/* Experimental path stuff */
#ifdef WITH_IMAGEPATH
{"path", (PyCFunction)PyPath_Create, METH_VARARGS},
#endif
/* Experimental arrow graphics stuff */
#ifdef WITH_ARROW
{"outline", (PyCFunction)PyOutline_Create, METH_VARARGS},
#endif
/* Resource management */
{"get_stats", (PyCFunction)_get_stats, METH_VARARGS},
{"reset_stats", (PyCFunction)_reset_stats, METH_VARARGS},
{"get_alignment", (PyCFunction)_get_alignment, METH_VARARGS},
{"get_block_size", (PyCFunction)_get_block_size, METH_VARARGS},
{"get_blocks_max", (PyCFunction)_get_blocks_max, METH_VARARGS},
{"set_alignment", (PyCFunction)_set_alignment, METH_VARARGS},
{"set_block_size", (PyCFunction)_set_block_size, METH_VARARGS},
{"set_blocks_max", (PyCFunction)_set_blocks_max, METH_VARARGS},
{"clear_cache", (PyCFunction)_clear_cache, METH_VARARGS},
{NULL, NULL} /* sentinel */
};
static int
setup_module(PyObject *m) {
PyObject *d = PyModule_GetDict(m);
const char *version = (char *)PILLOW_VERSION;
/* Ready object types */
if (PyType_Ready(&Imaging_Type) < 0) {
return -1;
}
#ifdef WITH_IMAGEDRAW
if (PyType_Ready(&ImagingFont_Type) < 0) {
return -1;
}
if (PyType_Ready(&ImagingDraw_Type) < 0) {
return -1;
}
#endif
if (PyType_Ready(&PixelAccess_Type) < 0) {
return -1;
}
ImagingAccessInit();
#ifdef HAVE_LIBJPEG
{
extern const char *ImagingJpegVersion(void);
PyObject *v = PyUnicode_FromString(ImagingJpegVersion());
PyDict_SetItemString(d, "jpeglib_version", v ? v : Py_None);
Py_XDECREF(v);
}
#endif
#ifdef HAVE_OPENJPEG
{
extern const char *ImagingJpeg2KVersion(void);
PyObject *v = PyUnicode_FromString(ImagingJpeg2KVersion());
PyDict_SetItemString(d, "jp2klib_version", v ? v : Py_None);
Py_XDECREF(v);
}
#endif
PyObject *have_libjpegturbo;
#ifdef LIBJPEG_TURBO_VERSION
have_libjpegturbo = Py_True;
{
#define tostr1(a) #a
#define tostr(a) tostr1(a)
PyObject *v = PyUnicode_FromString(tostr(LIBJPEG_TURBO_VERSION));
PyDict_SetItemString(d, "libjpeg_turbo_version", v ? v : Py_None);
Py_XDECREF(v);
#undef tostr
#undef tostr1
}
#else
have_libjpegturbo = Py_False;
#endif
Py_INCREF(have_libjpegturbo);
PyModule_AddObject(m, "HAVE_LIBJPEGTURBO", have_libjpegturbo);
PyObject *have_libimagequant;
#ifdef HAVE_LIBIMAGEQUANT
have_libimagequant = Py_True;
{
extern const char *ImagingImageQuantVersion(void);
PyObject *v = PyUnicode_FromString(ImagingImageQuantVersion());
PyDict_SetItemString(d, "imagequant_version", v ? v : Py_None);
Py_XDECREF(v);
}
#else
have_libimagequant = Py_False;
#endif
Py_INCREF(have_libimagequant);
PyModule_AddObject(m, "HAVE_LIBIMAGEQUANT", have_libimagequant);
#ifdef HAVE_LIBZ
/* zip encoding strategies */
PyModule_AddIntConstant(m, "DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
PyModule_AddIntConstant(m, "FILTERED", Z_FILTERED);
PyModule_AddIntConstant(m, "HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
PyModule_AddIntConstant(m, "RLE", Z_RLE);
PyModule_AddIntConstant(m, "FIXED", Z_FIXED);
{
extern const char *ImagingZipVersion(void);
PyObject *v = PyUnicode_FromString(ImagingZipVersion());
PyDict_SetItemString(d, "zlib_version", v ? v : Py_None);
Py_XDECREF(v);
}
#endif
#ifdef HAVE_LIBTIFF
{
extern const char *ImagingTiffVersion(void);
PyObject *v = PyUnicode_FromString(ImagingTiffVersion());
PyDict_SetItemString(d, "libtiff_version", v ? v : Py_None);
Py_XDECREF(v);
// Test for libtiff 4.0 or later, excluding libtiff 3.9.6 and 3.9.7
PyObject *support_custom_tags;
#if TIFFLIB_VERSION >= 20111221 && TIFFLIB_VERSION != 20120218 && \
TIFFLIB_VERSION != 20120922
support_custom_tags = Py_True;
#else
support_custom_tags = Py_False;
#endif
PyDict_SetItemString(d, "libtiff_support_custom_tags", support_custom_tags);
}
#endif
PyObject *have_xcb;
#ifdef HAVE_XCB
have_xcb = Py_True;
#else
have_xcb = Py_False;
#endif
Py_INCREF(have_xcb);
PyModule_AddObject(m, "HAVE_XCB", have_xcb);
PyObject *pillow_version = PyUnicode_FromString(version);
PyDict_SetItemString(d, "PILLOW_VERSION", pillow_version ? pillow_version : Py_None);
Py_XDECREF(pillow_version);
return 0;
}
PyMODINIT_FUNC
PyInit__imaging(void) {
PyObject *m;
static PyModuleDef module_def = {
PyModuleDef_HEAD_INIT,
"_imaging", /* m_name */
NULL, /* m_doc */
-1, /* m_size */
functions, /* m_methods */
};
m = PyModule_Create(&module_def);
if (setup_module(m) < 0) {
Py_DECREF(m);
return NULL;
}
return m;
}
|