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
|
/* GdkPixbuf library - Main loading interface.
*
* Copyright (C) 1999 The Free Software Foundation
*
* Authors: Miguel de Icaza <miguel@gnu.org>
* Federico Mena-Quintero <federico@gimp.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <glib.h>
#include <gio/gio.h>
#include "gdk-pixbuf-private.h"
#include "gdk-pixbuf-loader.h"
#include "gdk-pixdata.h"
#include <glib/gstdio.h>
#ifdef G_OS_WIN32
#define STRICT
#include <windows.h>
#undef STRICT
#endif
#ifdef OS_DARWIN
#include <mach-o/dyld.h>
#endif
/**
* SECTION:file-loading
* @Short_description: Loading a pixbuf from a file.
* @Title: File Loading
* @See_also: #GdkPixbufLoader.
*
* The GdkPixBuf library provides a simple mechanism for loading
* an image from a file in synchronous fashion. This means that the
* library takes control of the application while the file is being
* loaded; from the user's point of view, the application will block
* until the image is done loading.
*
*
* This interface can be used by applications in which blocking is
* acceptable while an image is being loaded. It can also be used to
* load small images in general. Applications that need progressive
* loading can use the #GdkPixbufLoader functionality instead.
*/
/**
* SECTION:file-saving
* @Short_description: Saving a pixbuf to a file.
* @Title: File saving
*
* These functions allow to save a #GdkPixbuf in a number of
* file formats. The formatted data can be written to a file
* or to a memory buffer. GdkPixBuf can also call a user-defined
* callback on the data, which allows to e.g. write the image
* to a socket or store it in a database.
*/
/**
* SECTION:module_interface
* @Short_description: Extending GdkPixBuf
* @Title: Module Interface
*
* If GdkPixBuf has been compiled with GModule support, it can be extended by
* modules which can load (and perhaps also save) new image and animation
* formats. Each loadable module must export a
* #GdkPixbufModuleFillInfoFunc function named `fill_info` and
* a #GdkPixbufModuleFillVtableFunc function named
* `fill_vtable`.
*
* In order to make format-checking work before actually loading the modules
* (which may require dlopening image libraries), modules export their
* signatures (and other information) via the `fill_info` function. An
* external utility, gdk-pixbuf-query-loaders, uses this to create a text
* file containing a list of all available loaders and their signatures.
* This file is then read at runtime by GdkPixBuf to obtain the list of
* available loaders and their signatures.
*
* Modules may only implement a subset of the functionality available via
* #GdkPixbufModule. If a particular functionality is not implemented, the
* `fill_vtable` function will simply not set the corresponding
* function pointers of the #GdkPixbufModule structure. If a module supports
* incremental loading (i.e. provides #begin_load, #stop_load and
* #load_increment), it doesn't have to implement #load, since GdkPixBuf can
* supply a generic #load implementation wrapping the incremental loading.
*
* Installing a module is a two-step process:
* - copy the module file(s) to the loader directory (normally
* `$libdir/gdk-pixbuf-2.0/$version/loaders`, unless overridden by the
* environment variable `GDK_PIXBUF_MODULEDIR`)
* - call gdk-pixbuf-query-loaders to update the module file (normally
* `$libdir/gdk-pixbuf-2.0/$version/loaders.cache`, unless overridden by the
* environment variable `GDK_PIXBUF_MODULE_FILE`)
*
* The GdkPixBuf interfaces needed for implementing modules are contained in
* `gdk-pixbuf-io.h` (and `gdk-pixbuf-animation.h` if the module supports
* animations). They are not covered by the same stability guarantees as the
* regular GdkPixBuf API. To underline this fact, they are protected by
* `#ifdef GDK_PIXBUF_ENABLE_BACKEND`.
*/
static gint
format_check (GdkPixbufModule *module, guchar *buffer, int size)
{
int i, j;
gchar m;
GdkPixbufModulePattern *pattern;
gboolean anchored;
guchar *prefix;
gchar *mask;
for (pattern = module->info->signature; pattern->prefix; pattern++) {
if (pattern->mask && pattern->mask[0] == '*') {
prefix = (guchar *)pattern->prefix + 1;
mask = pattern->mask + 1;
anchored = FALSE;
}
else {
prefix = (guchar *)pattern->prefix;
mask = pattern->mask;
anchored = TRUE;
}
for (i = 0; i < size; i++) {
for (j = 0; i + j < size && prefix[j] != 0; j++) {
m = mask ? mask[j] : ' ';
if (m == ' ') {
if (buffer[i + j] != prefix[j])
break;
}
else if (m == '!') {
if (buffer[i + j] == prefix[j])
break;
}
else if (m == 'z') {
if (buffer[i + j] != 0)
break;
}
else if (m == 'n') {
if (buffer[i + j] == 0)
break;
}
}
if (prefix[j] == 0)
return pattern->relevance;
if (anchored)
break;
}
}
return 0;
}
G_LOCK_DEFINE_STATIC (init_lock);
static gboolean file_formats_inited;
static GSList *file_formats = NULL;
static gboolean gdk_pixbuf_io_init (void);
static GSList *
get_file_formats (void)
{
G_LOCK (init_lock);
if (file_formats == NULL ||
!file_formats_inited)
file_formats_inited = gdk_pixbuf_io_init ();
G_UNLOCK (init_lock);
return file_formats;
}
#ifdef G_OS_WIN32
/* DllMain function needed to tuck away the gdk-pixbuf DLL handle */
static HMODULE gdk_pixbuf_dll;
BOOL WINAPI
DllMain (HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved)
{
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
gdk_pixbuf_dll = (HMODULE) hinstDLL;
break;
}
return TRUE;
}
#endif
#ifdef GDK_PIXBUF_RELOCATABLE
gchar *
gdk_pixbuf_get_toplevel (void)
{
static gchar *toplevel = NULL;
if (toplevel == NULL) {
#if defined(G_OS_WIN32)
toplevel = g_win32_get_package_installation_directory_of_module (gdk_pixbuf_dll);
#elif defined(OS_DARWIN)
char pathbuf[PATH_MAX + 1];
uint32_t bufsize = sizeof(pathbuf);
gchar *bin_dir;
_NSGetExecutablePath(pathbuf, &bufsize);
bin_dir = g_dirname(pathbuf);
toplevel = g_build_path (G_DIR_SEPARATOR_S, bin_dir, "..", NULL);
g_free (bin_dir);
#elif defined (OS_LINUX)
gchar *exe_path, *bin_dir;
exe_path = g_file_read_link ("/proc/self/exe", NULL);
bin_dir = g_dirname(exe_path);
toplevel = g_build_path (G_DIR_SEPARATOR_S, bin_dir, "..", NULL);
g_free (exe_path);
g_free (bin_dir);
#else
#error "Relocations not supported for this platform"
#endif
}
return toplevel;
}
#endif /* GDK_PIXBUF_RELOCATABLE */
#ifdef USE_GMODULE
static gboolean
scan_string (const char **pos, GString *out)
{
const char *p = *pos, *q = *pos;
char *tmp, *tmp2;
gboolean quoted;
while (g_ascii_isspace (*p))
p++;
if (!*p)
return FALSE;
else if (*p == '"') {
p++;
quoted = FALSE;
for (q = p; (*q != '"') || quoted; q++) {
if (!*q)
return FALSE;
quoted = (*q == '\\') && !quoted;
}
tmp = g_strndup (p, q - p);
tmp2 = g_strcompress (tmp);
g_string_truncate (out, 0);
g_string_append (out, tmp2);
g_free (tmp);
g_free (tmp2);
}
q++;
*pos = q;
return TRUE;
}
static gboolean
scan_int (const char **pos, int *out)
{
int i = 0;
char buf[32];
const char *p = *pos;
while (g_ascii_isspace (*p))
p++;
if (*p < '0' || *p > '9')
return FALSE;
while ((*p >= '0') && (*p <= '9') && i < sizeof (buf)) {
buf[i] = *p;
i++;
p++;
}
if (i == sizeof (buf))
return FALSE;
else
buf[i] = '\0';
*out = atoi (buf);
*pos = p;
return TRUE;
}
static gboolean
skip_space (const char **pos)
{
const char *p = *pos;
while (g_ascii_isspace (*p))
p++;
*pos = p;
return !(*p == '\0');
}
#ifdef GDK_PIXBUF_RELOCATABLE
static char *
get_libdir (void)
{
static char *libdir = NULL;
if (libdir == NULL)
libdir = g_build_filename (gdk_pixbuf_get_toplevel (), "lib", NULL);
return libdir;
}
#undef GDK_PIXBUF_LIBDIR
#define GDK_PIXBUF_LIBDIR get_libdir()
#endif /* GDK_PIXBUF_RELOCATABLE */
/* In case we have a relative module path in the loaders cache
* prepend the toplevel dir */
static gchar *
build_module_path (const gchar *path)
{
#ifdef GDK_PIXBUF_RELOCATABLE
if (g_path_is_absolute (path)) {
return g_strdup (path);
} else {
return g_build_filename (gdk_pixbuf_get_toplevel (), path, NULL);
}
#else
return g_strdup (path);
#endif
}
static gchar *
gdk_pixbuf_get_module_file (void)
{
gchar *result = g_strdup (g_getenv ("GDK_PIXBUF_MODULE_FILE"));
if (!result)
result = g_build_filename (GDK_PIXBUF_LIBDIR, "gdk-pixbuf-2.0", GDK_PIXBUF_BINARY_VERSION, "loaders.cache", NULL);
return result;
}
#endif /* USE_GMODULE */
static gboolean
gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
GError **error);
static gboolean
gdk_pixbuf_io_init_modules (const char *filename,
GError **error)
{
#ifdef USE_GMODULE
GIOChannel *channel;
gchar *line_buf;
gsize term;
GString *tmp_buf = g_string_new (NULL);
gboolean have_error = FALSE;
GdkPixbufModule *module = NULL;
int flags;
int n_patterns = 0;
GdkPixbufModulePattern *pattern;
GError *local_error = NULL;
guint num_formats;
channel = g_io_channel_new_file (filename, "r", &local_error);
if (!channel) {
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
"Cannot open pixbuf loader module file '%s': %s\n\n"
"This likely means that your installation is broken.\n"
"Try running the command\n"
" gdk-pixbuf-query-loaders > %s\n"
"to make things work again for the time being.",
filename, local_error->message, filename);
g_clear_error (&local_error);
g_string_free (tmp_buf, TRUE);
return FALSE;
}
num_formats = g_slist_length (file_formats);
while (!have_error && g_io_channel_read_line (channel, &line_buf, NULL, &term, NULL) == G_IO_STATUS_NORMAL) {
const char *p;
p = line_buf;
line_buf[term] = 0;
if (!skip_space (&p)) {
/* Blank line marking the end of a module
*/
if (module && *p != '#') {
file_formats = g_slist_prepend (file_formats, module);
module = NULL;
}
goto next_line;
}
if (*p == '#')
goto next_line;
if (!module) {
/* Read a module location
*/
module = g_new0 (GdkPixbufModule, 1);
n_patterns = 0;
if (!scan_string (&p, tmp_buf)) {
g_warning ("Error parsing loader info in '%s'\n %s",
filename, line_buf);
have_error = TRUE;
}
module->module_path = build_module_path (tmp_buf->str);
}
else if (!module->module_name) {
module->info = g_new0 (GdkPixbufFormat, 1);
if (!scan_string (&p, tmp_buf)) {
g_warning ("Error parsing loader info in '%s'\n %s",
filename, line_buf);
have_error = TRUE;
}
module->info->name = g_strdup (tmp_buf->str);
module->module_name = module->info->name;
if (!scan_int (&p, &flags)) {
g_warning ("Error parsing loader info in '%s'\n %s",
filename, line_buf);
have_error = TRUE;
}
module->info->flags = flags;
if (!scan_string (&p, tmp_buf)) {
g_warning ("Error parsing loader info in '%s'\n %s",
filename, line_buf);
have_error = TRUE;
}
if (tmp_buf->str[0] != 0)
module->info->domain = g_strdup (tmp_buf->str);
if (!scan_string (&p, tmp_buf)) {
g_warning ("Error parsing loader info in '%s'\n %s",
filename, line_buf);
have_error = TRUE;
}
module->info->description = g_strdup (tmp_buf->str);
if (scan_string (&p, tmp_buf)) {
module->info->license = g_strdup (tmp_buf->str);
}
}
else if (!module->info->mime_types) {
int n = 1;
module->info->mime_types = g_new0 (gchar*, 1);
while (scan_string (&p, tmp_buf)) {
if (tmp_buf->str[0] != 0) {
module->info->mime_types =
g_realloc (module->info->mime_types, (n + 1) * sizeof (gchar*));
module->info->mime_types[n - 1] = g_strdup (tmp_buf->str);
module->info->mime_types[n] = NULL;
n++;
}
}
}
else if (!module->info->extensions) {
int n = 1;
module->info->extensions = g_new0 (gchar*, 1);
while (scan_string (&p, tmp_buf)) {
if (tmp_buf->str[0] != 0) {
module->info->extensions =
g_realloc (module->info->extensions, (n + 1) * sizeof (gchar*));
module->info->extensions[n - 1] = g_strdup (tmp_buf->str);
module->info->extensions[n] = NULL;
n++;
}
}
}
else {
n_patterns++;
module->info->signature = (GdkPixbufModulePattern *)
g_realloc (module->info->signature, (n_patterns + 1) * sizeof (GdkPixbufModulePattern));
pattern = module->info->signature + n_patterns;
pattern->prefix = NULL;
pattern->mask = NULL;
pattern->relevance = 0;
pattern--;
if (!scan_string (&p, tmp_buf))
goto context_error;
pattern->prefix = g_strdup (tmp_buf->str);
if (!scan_string (&p, tmp_buf))
goto context_error;
if (*tmp_buf->str)
pattern->mask = g_strdup (tmp_buf->str);
else
pattern->mask = NULL;
if (!scan_int (&p, &pattern->relevance))
goto context_error;
goto next_line;
context_error:
g_free (pattern->prefix);
g_free (pattern->mask);
g_free (pattern);
g_warning ("Error parsing loader info in '%s'\n %s",
filename, line_buf);
have_error = TRUE;
}
next_line:
g_free (line_buf);
}
g_string_free (tmp_buf, TRUE);
g_io_channel_unref (channel);
if (g_slist_length (file_formats) <= num_formats) {
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_NOT_INITIALIZED,
"No new GdkPixbufModule loaded from '%s'",
filename);
return FALSE;
}
#endif
return TRUE;
}
/**
* gdk_pixbuf_init_modules:
* @path: Path to directory where the loaders.cache is installed
*
* Initalizes the gdk-pixbuf loader modules referenced by the loaders.cache
* file present inside that directory.
*
* This is to be used by applications that want to ship certain loaders
* in a different location from the system ones.
*
* This is needed when the OS or runtime ships a minimal number of loaders
* so as to reduce the potential attack surface of carefully crafted image
* files, especially for uncommon file types. Applications that require
* broader image file types coverage, such as image viewers, would be
* expected to ship the gdk-pixbuf modules in a separate location, bundled
* with the application in a separate directory from the OS or runtime-
* provided modules.
*
* Since: 2.40
*/
gboolean
gdk_pixbuf_init_modules (const char *path,
GError **error)
{
char *filename;
gboolean ret;
g_return_val_if_fail (path != NULL, FALSE);
filename = g_build_filename (path, "loaders.cache", NULL);
ret = gdk_pixbuf_io_init_modules (filename, error);
g_free (filename);
return ret;
}
static void
gdk_pixbuf_io_init_builtin (void)
{
#define load_one_builtin_module(format) G_STMT_START { \
GdkPixbufModule *__builtin_module = g_new0 (GdkPixbufModule, 1); \
__builtin_module->module_name = #format; \
if (gdk_pixbuf_load_module_unlocked (__builtin_module, NULL)) \
file_formats = g_slist_prepend (file_formats, __builtin_module); \
else \
g_free (__builtin_module); } G_STMT_END
#ifdef INCLUDE_ani
load_one_builtin_module (ani);
#endif
#ifdef INCLUDE_png
load_one_builtin_module (png);
#endif
#ifdef INCLUDE_bmp
load_one_builtin_module (bmp);
#endif
#ifdef INCLUDE_gif
load_one_builtin_module (gif);
#endif
#ifdef INCLUDE_ico
load_one_builtin_module (ico);
#endif
#ifdef INCLUDE_jpeg
load_one_builtin_module (jpeg);
#endif
#ifdef INCLUDE_pnm
load_one_builtin_module (pnm);
#endif
#ifdef INCLUDE_tiff
load_one_builtin_module (tiff);
#endif
#ifdef INCLUDE_xpm
load_one_builtin_module (xpm);
#endif
#ifdef INCLUDE_xbm
load_one_builtin_module (xbm);
#endif
#ifdef INCLUDE_tga
load_one_builtin_module (tga);
#endif
#ifdef INCLUDE_icns
load_one_builtin_module (icns);
#endif
#ifdef INCLUDE_jasper
load_one_builtin_module (jasper);
#endif
#ifdef INCLUDE_qtif
load_one_builtin_module (qtif);
#endif
#ifdef INCLUDE_gdiplus
/* We don't bother having the GDI+ loaders individually selectable
* for building in or not.
*/
load_one_builtin_module (ico);
load_one_builtin_module (wmf);
load_one_builtin_module (emf);
load_one_builtin_module (bmp);
load_one_builtin_module (gif);
load_one_builtin_module (jpeg);
load_one_builtin_module (tiff);
#endif
#ifdef INCLUDE_gdip_png
/* Except the gdip-png loader which normally isn't built at all even */
load_one_builtin_module (png);
#endif
#undef load_one_builtin_module
}
static gboolean
gdk_pixbuf_io_init (void)
{
char *module_file;
gboolean ret;
gdk_pixbuf_io_init_builtin ();
module_file = gdk_pixbuf_get_module_file ();
ret = gdk_pixbuf_io_init_modules (module_file, NULL);
g_free (module_file);
return ret;
}
#define module(type) \
extern void _gdk_pixbuf__##type##_fill_info (GdkPixbufFormat *info); \
extern void _gdk_pixbuf__##type##_fill_vtable (GdkPixbufModule *module)
module (png);
module (jpeg);
module (gif);
module (ico);
module (ani);
module (xpm);
module (tiff);
module (pnm);
module (bmp);
module (xbm);
module (tga);
module (icns);
module (jasper);
module (qtif);
module (gdip_ico);
module (gdip_wmf);
module (gdip_emf);
module (gdip_bmp);
module (gdip_gif);
module (gdip_jpeg);
module (gdip_png);
module (gdip_tiff);
#undef module
/* actually load the image handler - gdk_pixbuf_get_module only get a */
/* reference to the module to load, it doesn't actually load it */
/* perhaps these actions should be combined in one function */
static gboolean
gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
GError **error)
{
GdkPixbufModuleFillInfoFunc fill_info = NULL;
GdkPixbufModuleFillVtableFunc fill_vtable = NULL;
if (image_module->module != NULL)
return TRUE;
#define try_module(format,id) \
if (fill_info == NULL && \
strcmp (image_module->module_name, #format) == 0) { \
fill_info = _gdk_pixbuf__##id##_fill_info; \
fill_vtable = _gdk_pixbuf__##id##_fill_vtable; \
}
#ifdef INCLUDE_gdiplus
try_module (ico,gdip_ico);
try_module (wmf,gdip_wmf);
try_module (emf,gdip_emf);
try_module (bmp,gdip_bmp);
try_module (gif,gdip_gif);
try_module (jpeg,gdip_jpeg);
try_module (tiff,gdip_tiff);
#endif
#ifdef INCLUDE_gdip_png
try_module (png,gdip_png);
#endif
#ifdef INCLUDE_png
try_module (png,png);
#endif
#ifdef INCLUDE_bmp
try_module (bmp,bmp);
#endif
#ifdef INCLUDE_gif
try_module (gif,gif);
#endif
#ifdef INCLUDE_ico
try_module (ico,ico);
#endif
#ifdef INCLUDE_ani
try_module (ani,ani);
#endif
#ifdef INCLUDE_jpeg
try_module (jpeg,jpeg);
#endif
#ifdef INCLUDE_pnm
try_module (pnm,pnm);
#endif
#ifdef INCLUDE_tiff
try_module (tiff,tiff);
#endif
#ifdef INCLUDE_xpm
try_module (xpm,xpm);
#endif
#ifdef INCLUDE_xbm
try_module (xbm,xbm);
#endif
#ifdef INCLUDE_tga
try_module (tga,tga);
#endif
#ifdef INCLUDE_icns
try_module (icns,icns);
#endif
#ifdef INCLUDE_jasper
try_module (jasper,jasper);
#endif
#ifdef INCLUDE_qtif
try_module (qtif,qtif);
#endif
#undef try_module
if (fill_vtable) {
image_module->module = (void *) 1;
(* fill_vtable) (image_module);
if (image_module->info == NULL) {
image_module->info = g_new0 (GdkPixbufFormat, 1);
(* fill_info) (image_module->info);
}
return TRUE;
}
else
#ifdef USE_GMODULE
{
char *path;
GModule *module;
gpointer sym;
path = image_module->module_path;
module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
if (!module) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
_("Unable to load image-loading module: %s: %s"),
path, g_module_error ());
return FALSE;
}
image_module->module = module;
if (g_module_symbol (module, "fill_vtable", &sym)) {
fill_vtable = (GdkPixbufModuleFillVtableFunc) sym;
(* fill_vtable) (image_module);
return TRUE;
} else {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
_("Image-loading module %s does not export the proper interface; perhaps itās from a different gdk-pixbuf version?"),
path);
return FALSE;
}
}
#else
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
_("Image type ā%sā is not supported"),
image_module->module_name);
return FALSE;
#endif /* !USE_GMODULE */
}
gboolean
_gdk_pixbuf_load_module (GdkPixbufModule *image_module,
GError **error)
{
gboolean ret;
G_LOCK (init_lock);
ret = gdk_pixbuf_load_module_unlocked (image_module, error);
G_UNLOCK (init_lock);
return ret;
}
GdkPixbufModule *
_gdk_pixbuf_get_named_module (const char *name,
GError **error)
{
GSList *modules;
for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
if (module->info->disabled)
continue;
if (!strcmp (name, module->module_name))
return module;
}
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
_("Image type ā%sā is not supported"),
name);
return NULL;
}
GdkPixbufModule *
_gdk_pixbuf_get_module (guchar *buffer, guint size,
const gchar *filename,
GError **error)
{
GSList *modules;
GdkPixbufModule *selected = NULL;
gchar *display_name = NULL;
#ifdef GDK_PIXBUF_USE_GIO_MIME
gchar *mime_type;
gchar **mimes;
gchar *type;
gint j;
gboolean uncertain;
mime_type = g_content_type_guess (NULL, buffer, size, &uncertain);
if ((uncertain || g_str_equal (mime_type, "text/plain") || g_str_equal (mime_type, "application/gzip")) && filename != NULL) {
g_free (mime_type);
mime_type = g_content_type_guess (filename, buffer, size, NULL);
}
for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
GdkPixbufFormat *info = module->info;
if (info->disabled)
continue;
mimes = info->mime_types;
for (j = 0; mimes[j] != NULL; j++) {
type = g_content_type_from_mime_type (mimes[j]);
if (g_content_type_equals (type, mime_type)) {
g_free (type);
selected = module;
break;
}
g_free (type);
}
if (selected != NULL)
break;
/* Make sure the builtin GdkPixdata support works even without mime sniffing */
if (strcmp (info->name, "GdkPixdata") == 0 &&
format_check (module, buffer, size) == 100) {
selected = module;
break;
}
}
g_free (mime_type);
#else
gint score, best = 0;
for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
if (module->info->disabled)
continue;
score = format_check (module, buffer, size);
if (score > best) {
best = score;
selected = module;
}
if (score >= 100)
break;
}
#endif
if (selected != NULL)
return selected;
if (filename)
{
display_name = g_filename_display_name (filename);
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
_("Couldnāt recognize the image file format for file ā%sā"),
display_name);
g_free (display_name);
}
else
g_set_error_literal (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
_("Unrecognized image file format"));
return NULL;
}
static
GdkPixbufModule *
_gdk_pixbuf_get_module_for_file (FILE *f, const gchar *filename, GError **error)
{
guchar buffer[SNIFF_BUFFER_SIZE];
int size;
size = fread (&buffer, 1, sizeof (buffer), f);
if (size == 0) {
gchar *display_name;
display_name = g_filename_display_name (filename);
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
_("Image file ā%sā contains no data"),
display_name);
g_free (display_name);
return NULL;
}
return _gdk_pixbuf_get_module (buffer, size, filename, error);
}
static void
prepared_notify (GdkPixbuf *pixbuf,
GdkPixbufAnimation *anim,
gpointer user_data)
{
if (pixbuf != NULL)
g_object_ref (pixbuf);
*((GdkPixbuf **)user_data) = pixbuf;
}
static GdkPixbuf *
generic_load_incrementally (GdkPixbufModule *module, FILE *f, GError **error)
{
GdkPixbuf *pixbuf = NULL;
gpointer context;
context = module->begin_load (NULL, prepared_notify, NULL, &pixbuf, error);
if (!context)
goto out;
while (!feof (f) && !ferror (f)) {
guchar buffer[LOAD_BUFFER_SIZE];
size_t length;
length = fread (buffer, 1, sizeof (buffer), f);
if (length > 0) {
if (!module->load_increment (context, buffer, length, error)) {
module->stop_load (context, NULL);
if (pixbuf != NULL) {
g_object_unref (pixbuf);
pixbuf = NULL;
}
goto out;
}
}
}
if (!module->stop_load (context, error)) {
if (pixbuf != NULL) {
g_object_unref (pixbuf);
pixbuf = NULL;
}
}
out:
return pixbuf;
}
GdkPixbuf *
_gdk_pixbuf_generic_image_load (GdkPixbufModule *module, FILE *f, GError **error)
{
GdkPixbuf *pixbuf = NULL;
if (module->load != NULL) {
pixbuf = (* module->load) (f, error);
} else if (module->begin_load != NULL) {
pixbuf = generic_load_incrementally (module, f, error);
} else if (module->load_animation != NULL) {
GdkPixbufAnimation *animation;
animation = (* module->load_animation) (f, error);
if (animation != NULL) {
pixbuf = gdk_pixbuf_animation_get_static_image (animation);
g_object_ref (pixbuf);
g_object_unref (animation);
}
}
return pixbuf;
}
/**
* gdk_pixbuf_new_from_file:
* @filename: (type filename): Name of file to load, in the GLib file
* name encoding
* @error: Return location for an error
*
* Creates a new pixbuf by loading an image from a file. The file format is
* detected automatically. If %NULL is returned, then @error will be set.
* Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
*
* Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
* any of several error conditions occurred: the file could not be opened,
* there was no loader for the file's format, there was not enough memory to
* allocate the image buffer, or the image file contained invalid data.
**/
GdkPixbuf *
gdk_pixbuf_new_from_file (const char *filename,
GError **error)
{
GdkPixbuf *pixbuf;
FILE *f;
GdkPixbufModule *image_module;
g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
f = g_fopen (filename, "rb");
if (!f) {
gint save_errno = errno;
gchar *display_name;
display_name = g_filename_display_name (filename);
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to open file ā%sā: %s"),
display_name,
g_strerror (save_errno));
g_free (display_name);
return NULL;
}
image_module = _gdk_pixbuf_get_module_for_file (f, filename, error);
if (image_module == NULL) {
fclose (f);
return NULL;
}
if (!_gdk_pixbuf_load_module (image_module, error)) {
fclose (f);
return NULL;
}
fseek (f, 0, SEEK_SET);
pixbuf = _gdk_pixbuf_generic_image_load (image_module, f, error);
fclose (f);
if (pixbuf == NULL && error != NULL && *error == NULL) {
/* I don't trust these crufty longjmp()'ing image libs
* to maintain proper error invariants, and I don't
* want user code to segfault as a result. We need to maintain
* the invariant that error gets set if NULL is returned.
*/
gchar *display_name;
display_name = g_filename_display_name (filename);
g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.", image_module->module_name);
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
_("Failed to load image ā%sā: reason not known, probably a corrupt image file"),
display_name);
g_free (display_name);
} else if (error != NULL && *error != NULL) {
/* Add the filename to the error message */
GError *e = *error;
gchar *old;
gchar *display_name;
display_name = g_filename_display_name (filename);
old = e->message;
e->message = g_strdup_printf (_("Failed to load image ā%sā: %s"),
display_name,
old);
g_free (old);
g_free (display_name);
}
return pixbuf;
}
#ifdef G_OS_WIN32
/**
* gdk_pixbuf_new_from_file_utf8:
* @filename: (type filename): Name of file to load, in the GLib file name encoding
* @error: Return location for an error
*
* Same as gdk_pixbuf_new_from_file()
*
* Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
* any of several error conditions occurred: the file could not be opened,
* there was no loader for the file's format, there was not enough memory to
* allocate the image buffer, or the image file contained invalid data.
**/
GdkPixbuf *
gdk_pixbuf_new_from_file_utf8 (const char *filename,
GError **error)
{
return gdk_pixbuf_new_from_file (filename, error);
}
#endif
/**
* gdk_pixbuf_new_from_file_at_size:
* @filename: (type filename): Name of file to load, in the GLib file
* name encoding
* @width: The width the image should have or -1 to not constrain the width
* @height: The height the image should have or -1 to not constrain the height
* @error: Return location for an error
*
* Creates a new pixbuf by loading an image from a file.
* The file format is detected automatically. If %NULL is returned, then
* @error will be set. Possible errors are in the #GDK_PIXBUF_ERROR and
* #G_FILE_ERROR domains.
*
* The image will be scaled to fit in the requested size, preserving
* the image's aspect ratio. Note that the returned pixbuf may be smaller
* than @width x @height, if the aspect ratio requires it. To load
* and image at the requested size, regardless of aspect ratio, use
* gdk_pixbuf_new_from_file_at_scale().
*
* Return value: A newly-created pixbuf with a reference count of 1, or
* %NULL if any of several error conditions occurred: the file could not
* be opened, there was no loader for the file's format, there was not
* enough memory to allocate the image buffer, or the image file contained
* invalid data.
*
* Since: 2.4
**/
GdkPixbuf *
gdk_pixbuf_new_from_file_at_size (const char *filename,
int width,
int height,
GError **error)
{
return gdk_pixbuf_new_from_file_at_scale (filename,
width, height,
TRUE, error);
}
#ifdef G_OS_WIN32
/**
* gdk_pixbuf_new_from_file_at_size_utf8:
* @filename: (type filename): Name of file to load, in the GLib file name encoding
* @width: The width the image should have or -1 to not constrain the width
* @height: The height the image should have or -1 to not constrain the height
* @error: Return location for an error
*
* Same as gdk_pixbuf_new_from_file_at_size()
*
* Return value: A newly-created pixbuf with a reference count of 1, or
* %NULL if any of several error conditions occurred: the file could not
* be opened, there was no loader for the file's format, there was not
* enough memory to allocate the image buffer, or the image file contained
* invalid data.
*
* Since: 2.4
**/
GdkPixbuf *
gdk_pixbuf_new_from_file_at_size_utf8 (const char *filename,
int width,
int height,
GError **error)
{
return gdk_pixbuf_new_from_file_at_size (filename, width, height, error);
}
#endif
typedef struct {
gint width;
gint height;
gboolean preserve_aspect_ratio;
} AtScaleData;
static void
at_scale_data_async_data_free (AtScaleData *data)
{
g_slice_free (AtScaleData, data);
}
static void
at_scale_size_prepared_cb (GdkPixbufLoader *loader,
int width,
int height,
gpointer data)
{
AtScaleData *info = data;
g_return_if_fail (width > 0 && height > 0);
if (info->preserve_aspect_ratio &&
(info->width > 0 || info->height > 0)) {
if (info->width < 0)
{
width = width * (double)info->height/(double)height;
height = info->height;
}
else if (info->height < 0)
{
height = height * (double)info->width/(double)width;
width = info->width;
}
else if ((double)height * (double)info->width >
(double)width * (double)info->height) {
width = 0.5 + (double)width * (double)info->height / (double)height;
height = info->height;
} else {
height = 0.5 + (double)height * (double)info->width / (double)width;
width = info->width;
}
} else {
if (info->width > 0)
width = info->width;
if (info->height > 0)
height = info->height;
}
width = MAX (width, 1);
height = MAX (height, 1);
gdk_pixbuf_loader_set_size (loader, width, height);
}
/**
* gdk_pixbuf_new_from_file_at_scale:
* @filename: (type filename): Name of file to load, in the GLib file
* name encoding
* @width: The width the image should have or -1 to not constrain the width
* @height: The height the image should have or -1 to not constrain the height
* @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
* @error: Return location for an error
*
* Creates a new pixbuf by loading an image from a file. The file format is
* detected automatically. If %NULL is returned, then @error will be set.
* Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
* The image will be scaled to fit in the requested size, optionally preserving
* the image's aspect ratio.
*
* When preserving the aspect ratio, a @width of -1 will cause the image
* to be scaled to the exact given height, and a @height of -1 will cause
* the image to be scaled to the exact given width. When not preserving
* aspect ratio, a @width or @height of -1 means to not scale the image
* at all in that dimension. Negative values for @width and @height are
* allowed since 2.8.
*
* Return value: A newly-created pixbuf with a reference count of 1, or %NULL
* if any of several error conditions occurred: the file could not be opened,
* there was no loader for the file's format, there was not enough memory to
* allocate the image buffer, or the image file contained invalid data.
*
* Since: 2.6
**/
GdkPixbuf *
gdk_pixbuf_new_from_file_at_scale (const char *filename,
int width,
int height,
gboolean preserve_aspect_ratio,
GError **error)
{
GdkPixbufLoader *loader;
GdkPixbuf *pixbuf;
guchar buffer[LOAD_BUFFER_SIZE];
int length;
FILE *f;
AtScaleData info;
GdkPixbufAnimation *animation;
GdkPixbufAnimationIter *iter;
gboolean has_frame;
g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (width > 0 || width == -1, NULL);
g_return_val_if_fail (height > 0 || height == -1, NULL);
f = g_fopen (filename, "rb");
if (!f) {
gint save_errno = errno;
gchar *display_name = g_filename_display_name (filename);
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to open file ā%sā: %s"),
display_name,
g_strerror (save_errno));
g_free (display_name);
return NULL;
}
loader = _gdk_pixbuf_loader_new_with_filename (filename);
info.width = width;
info.height = height;
info.preserve_aspect_ratio = preserve_aspect_ratio;
g_signal_connect (loader, "size-prepared",
G_CALLBACK (at_scale_size_prepared_cb), &info);
has_frame = FALSE;
while (!has_frame && !feof (f) && !ferror (f)) {
length = fread (buffer, 1, sizeof (buffer), f);
if (length > 0)
if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
gdk_pixbuf_loader_close (loader, NULL);
fclose (f);
g_object_unref (loader);
return NULL;
}
animation = gdk_pixbuf_loader_get_animation (loader);
if (animation) {
iter = gdk_pixbuf_animation_get_iter (animation, NULL);
if (!gdk_pixbuf_animation_iter_on_currently_loading_frame (iter)) {
has_frame = TRUE;
}
g_object_unref (iter);
}
}
fclose (f);
if (!gdk_pixbuf_loader_close (loader, error) && !has_frame) {
g_object_unref (loader);
return NULL;
}
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
if (!pixbuf) {
gchar *display_name = g_filename_display_name (filename);
g_object_unref (loader);
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
_("Failed to load image ā%sā: reason not known, probably a corrupt image file"),
display_name);
g_free (display_name);
return NULL;
}
g_object_ref (pixbuf);
g_object_unref (loader);
return pixbuf;
}
#ifdef G_OS_WIN32
/**
* gdk_pixbuf_new_from_file_at_scale_utf8:
* @filename: (type filename): Name of file to load, in the GLib file name encoding
* @width: The width the image should have or -1 to not constrain the width
* @height: The height the image should have or -1 to not constrain the height
* @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
* @error: Return location for an error
*
* Same as gdk_pixbuf_new_from_file_at_scale().
*
* Return value: A newly-created pixbuf with a reference count of 1, or %NULL
* if any of several error conditions occurred: the file could not be opened,
* there was no loader for the file's format, there was not enough memory to
* allocate the image buffer, or the image file contained invalid data.
*
* Since: 2.6
**/
GdkPixbuf *
gdk_pixbuf_new_from_file_at_scale_utf8 (const char *filename,
int width,
int height,
gboolean preserve_aspect_ratio,
GError **error)
{
return gdk_pixbuf_new_from_file_at_scale (filename, width, height,
preserve_aspect_ratio, error);
}
#endif
static GdkPixbuf *
load_from_stream (GdkPixbufLoader *loader,
GInputStream *stream,
GCancellable *cancellable,
GError **error)
{
GdkPixbuf *pixbuf;
gssize n_read;
guchar buffer[LOAD_BUFFER_SIZE];
while (1) {
n_read = g_input_stream_read (stream,
buffer,
sizeof (buffer),
cancellable,
error);
if (n_read < 0) {
gdk_pixbuf_loader_close (loader, NULL);
return NULL;
}
if (n_read == 0)
break;
if (!gdk_pixbuf_loader_write (loader,
buffer,
n_read,
error)) {
gdk_pixbuf_loader_close (loader, NULL);
return NULL;
}
}
if (!gdk_pixbuf_loader_close (loader, error))
return NULL;
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
if (pixbuf == NULL)
return NULL;
return g_object_ref (pixbuf);
}
/**
* gdk_pixbuf_new_from_stream_at_scale:
* @stream: a #GInputStream to load the pixbuf from
* @width: The width the image should have or -1 to not constrain the width
* @height: The height the image should have or -1 to not constrain the height
* @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
* @error: Return location for an error
*
* Creates a new pixbuf by loading an image from an input stream.
*
* The file format is detected automatically. If %NULL is returned, then
* @error will be set. The @cancellable can be used to abort the operation
* from another thread. If the operation was cancelled, the error
* %G_IO_ERROR_CANCELLED will be returned. Other possible errors are in
* the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
*
* The image will be scaled to fit in the requested size, optionally
* preserving the image's aspect ratio.
*
* When preserving the aspect ratio, a @width of -1 will cause the image to be
* scaled to the exact given height, and a @height of -1 will cause the image
* to be scaled to the exact given width. If both @width and @height are
* given, this function will behave as if the smaller of the two values
* is passed as -1.
*
* When not preserving aspect ratio, a @width or @height of -1 means to not
* scale the image at all in that dimension.
*
* The stream is not closed.
*
* Return value: A newly-created pixbuf, or %NULL if any of several error
* conditions occurred: the file could not be opened, the image format is
* not supported, there was not enough memory to allocate the image buffer,
* the stream contained invalid data, or the operation was cancelled.
*
* Since: 2.14
*/
GdkPixbuf *
gdk_pixbuf_new_from_stream_at_scale (GInputStream *stream,
gint width,
gint height,
gboolean preserve_aspect_ratio,
GCancellable *cancellable,
GError **error)
{
GdkPixbufLoader *loader;
GdkPixbuf *pixbuf;
AtScaleData info;
loader = gdk_pixbuf_loader_new ();
info.width = width;
info.height = height;
info.preserve_aspect_ratio = preserve_aspect_ratio;
g_signal_connect (loader, "size-prepared",
G_CALLBACK (at_scale_size_prepared_cb), &info);
pixbuf = load_from_stream (loader, stream, cancellable, error);
g_object_unref (loader);
return pixbuf;
}
static void
load_from_stream_async_cb (GObject *stream,
GAsyncResult *res,
gpointer data)
{
GTask *task = data;
GdkPixbufLoader *loader;
GdkPixbuf *pixbuf;
GError *error = NULL;
GBytes *bytes = NULL;
loader = g_task_get_task_data (task);
bytes = g_input_stream_read_bytes_finish (G_INPUT_STREAM (stream), res, &error);
if (bytes == NULL) {
gdk_pixbuf_loader_close (loader, NULL);
g_task_return_error (task, error);
} else if (g_bytes_get_size (bytes) > 0) {
if (!gdk_pixbuf_loader_write (loader,
g_bytes_get_data (bytes, NULL),
g_bytes_get_size (bytes),
&error)) {
gdk_pixbuf_loader_close (loader, NULL);
g_task_return_error (task, error);
goto out;
}
g_input_stream_read_bytes_async (G_INPUT_STREAM (stream),
LOAD_BUFFER_SIZE,
G_PRIORITY_DEFAULT,
g_task_get_cancellable (task),
load_from_stream_async_cb,
g_object_ref (task));
} else {
if (!gdk_pixbuf_loader_close (loader, &error)) {
g_task_return_error (task, error);
goto out;
}
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
g_task_return_pointer (task, g_object_ref (pixbuf), g_object_unref);
}
out:
g_bytes_unref (bytes);
g_object_unref (task);
}
/**
* gdk_pixbuf_new_from_stream_at_scale_async:
* @stream: a #GInputStream from which to load the pixbuf
* @width: the width the image should have or -1 to not constrain the width
* @height: the height the image should have or -1 to not constrain the height
* @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
* @callback: a #GAsyncReadyCallback to call when the pixbuf is loaded
* @user_data: the data to pass to the callback function
*
* Creates a new pixbuf by asynchronously loading an image from an input stream.
*
* For more details see gdk_pixbuf_new_from_stream_at_scale(), which is the synchronous
* version of this function.
*
* When the operation is finished, @callback will be called in the main thread.
* You can then call gdk_pixbuf_new_from_stream_finish() to get the result of the operation.
*
* Since: 2.24
**/
void
gdk_pixbuf_new_from_stream_at_scale_async (GInputStream *stream,
gint width,
gint height,
gboolean preserve_aspect_ratio,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
AtScaleData *data;
GdkPixbufLoader *loader;
g_return_if_fail (G_IS_INPUT_STREAM (stream));
g_return_if_fail (callback != NULL);
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
data = g_slice_new (AtScaleData);
data->width = width;
data->height = height;
data->preserve_aspect_ratio = preserve_aspect_ratio;
loader = gdk_pixbuf_loader_new ();
g_signal_connect (loader, "size-prepared",
G_CALLBACK (at_scale_size_prepared_cb), data);
g_object_set_data_full (G_OBJECT (loader),
"gdk-pixbuf-please-kill-me-later",
data,
(GDestroyNotify) at_scale_data_async_data_free);
task = g_task_new (stream, cancellable, callback, user_data);
g_task_set_source_tag (task, gdk_pixbuf_new_from_stream_at_scale_async);
g_task_set_task_data (task, loader, g_object_unref);
g_input_stream_read_bytes_async (stream,
LOAD_BUFFER_SIZE,
G_PRIORITY_DEFAULT,
cancellable,
load_from_stream_async_cb,
task);
}
/**
* gdk_pixbuf_new_from_stream:
* @stream: a #GInputStream to load the pixbuf from
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
* @error: Return location for an error
*
* Creates a new pixbuf by loading an image from an input stream.
*
* The file format is detected automatically. If %NULL is returned, then
* @error will be set. The @cancellable can be used to abort the operation
* from another thread. If the operation was cancelled, the error
* %G_IO_ERROR_CANCELLED will be returned. Other possible errors are in
* the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
*
* The stream is not closed.
*
* Return value: A newly-created pixbuf, or %NULL if any of several error
* conditions occurred: the file could not be opened, the image format is
* not supported, there was not enough memory to allocate the image buffer,
* the stream contained invalid data, or the operation was cancelled.
*
* Since: 2.14
**/
GdkPixbuf *
gdk_pixbuf_new_from_stream (GInputStream *stream,
GCancellable *cancellable,
GError **error)
{
GdkPixbuf *pixbuf;
GdkPixbufLoader *loader;
loader = gdk_pixbuf_loader_new ();
pixbuf = load_from_stream (loader, stream, cancellable, error);
g_object_unref (loader);
return pixbuf;
}
GdkPixbuf *
_gdk_pixbuf_new_from_resource_try_pixdata (const char *resource_path)
{
gsize data_size;
GBytes *bytes;
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
/* We specialize GdkPixdata files, making these a reference to the
* compiled-in resource data, whether uncompressed and mmap'ed, or
* compressed, and uncompressed on-the-fly.
*/
if (g_resources_get_info (resource_path, 0, &data_size, NULL, NULL) &&
data_size > sizeof(guint32) &&
(bytes = g_resources_lookup_data (resource_path, 0, NULL)) != NULL) {
GdkPixbuf*pixbuf = NULL;
const guint8 *stream = g_bytes_get_data (bytes, NULL);
GdkPixdata pixdata;
guint32 magic;
magic = (stream[0] << 24) + (stream[1] << 16) + (stream[2] << 8) + stream[3];
if (magic == GDK_PIXBUF_MAGIC_NUMBER &&
gdk_pixdata_deserialize (&pixdata, data_size, stream, NULL)) {
pixbuf = gdk_pixbuf_from_pixdata (&pixdata, FALSE, NULL);
}
if (pixbuf) {
/* Free the GBytes with the pixbuf */
g_object_set_data_full (G_OBJECT (pixbuf), "gdk-pixbuf-resource-bytes", bytes, (GDestroyNotify) g_bytes_unref);
return pixbuf;
} else {
g_bytes_unref (bytes);
}
}
G_GNUC_END_IGNORE_DEPRECATIONS
return NULL;
}
/**
* gdk_pixbuf_new_from_resource:
* @resource_path: the path of the resource file
* @error: Return location for an error
*
* Creates a new pixbuf by loading an image from an resource.
*
* The file format is detected automatically. If %NULL is returned, then
* @error will be set.
*
* Return value: A newly-created pixbuf, or %NULL if any of several error
* conditions occurred: the file could not be opened, the image format is
* not supported, there was not enough memory to allocate the image buffer,
* the stream contained invalid data, or the operation was cancelled.
*
* Since: 2.26
**/
GdkPixbuf *
gdk_pixbuf_new_from_resource (const gchar *resource_path,
GError **error)
{
GInputStream *stream;
GdkPixbuf *pixbuf;
pixbuf = _gdk_pixbuf_new_from_resource_try_pixdata (resource_path);
if (pixbuf)
return pixbuf;
stream = g_resources_open_stream (resource_path, 0, error);
if (stream == NULL)
return NULL;
pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
g_object_unref (stream);
return pixbuf;
}
/**
* gdk_pixbuf_new_from_resource_at_scale:
* @resource_path: the path of the resource file
* @width: The width the image should have or -1 to not constrain the width
* @height: The height the image should have or -1 to not constrain the height
* @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
* @error: Return location for an error
*
* Creates a new pixbuf by loading an image from an resource.
*
* The file format is detected automatically. If %NULL is returned, then
* @error will be set.
*
* The image will be scaled to fit in the requested size, optionally
* preserving the image's aspect ratio. When preserving the aspect ratio,
* a @width of -1 will cause the image to be scaled to the exact given
* height, and a @height of -1 will cause the image to be scaled to the
* exact given width. When not preserving aspect ratio, a @width or
* @height of -1 means to not scale the image at all in that dimension.
*
* The stream is not closed.
*
* Return value: A newly-created pixbuf, or %NULL if any of several error
* conditions occurred: the file could not be opened, the image format is
* not supported, there was not enough memory to allocate the image buffer,
* the stream contained invalid data, or the operation was cancelled.
*
* Since: 2.26
*/
GdkPixbuf *
gdk_pixbuf_new_from_resource_at_scale (const char *resource_path,
int width,
int height,
gboolean preserve_aspect_ratio,
GError **error)
{
GInputStream *stream;
GdkPixbuf *pixbuf;
stream = g_resources_open_stream (resource_path, 0, error);
if (stream == NULL)
return NULL;
pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream, width, height, preserve_aspect_ratio, NULL, error);
g_object_unref (stream);
return pixbuf;
}
/**
* gdk_pixbuf_new_from_stream_async:
* @stream: a #GInputStream from which to load the pixbuf
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
* @callback: a #GAsyncReadyCallback to call when the pixbuf is loaded
* @user_data: the data to pass to the callback function
*
* Creates a new pixbuf by asynchronously loading an image from an input stream.
*
* For more details see gdk_pixbuf_new_from_stream(), which is the synchronous
* version of this function.
*
* When the operation is finished, @callback will be called in the main thread.
* You can then call gdk_pixbuf_new_from_stream_finish() to get the result of the operation.
*
* Since: 2.24
**/
void
gdk_pixbuf_new_from_stream_async (GInputStream *stream,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
g_return_if_fail (G_IS_INPUT_STREAM (stream));
g_return_if_fail (callback != NULL);
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
task = g_task_new (stream, cancellable, callback, user_data);
g_task_set_source_tag (task, gdk_pixbuf_new_from_stream_async);
g_task_set_task_data (task, gdk_pixbuf_loader_new (), g_object_unref);
g_input_stream_read_bytes_async (stream,
LOAD_BUFFER_SIZE,
G_PRIORITY_DEFAULT,
cancellable,
load_from_stream_async_cb,
task);
}
/**
* gdk_pixbuf_new_from_stream_finish:
* @async_result: a #GAsyncResult
* @error: a #GError, or %NULL
*
* Finishes an asynchronous pixbuf creation operation started with
* gdk_pixbuf_new_from_stream_async().
*
* Return value: a #GdkPixbuf or %NULL on error. Free the returned
* object with g_object_unref().
*
* Since: 2.24
**/
GdkPixbuf *
gdk_pixbuf_new_from_stream_finish (GAsyncResult *async_result,
GError **error)
{
GTask *task;
g_return_val_if_fail (G_IS_TASK (async_result), NULL);
g_return_val_if_fail (!error || (error && !*error), NULL);
task = G_TASK (async_result);
g_warn_if_fail (g_task_get_source_tag (task) == gdk_pixbuf_new_from_stream_async ||
g_task_get_source_tag (task) == gdk_pixbuf_new_from_stream_at_scale_async);
return g_task_propagate_pointer (task, error);
}
static void
info_cb (GdkPixbufLoader *loader,
int width,
int height,
gpointer data)
{
struct {
GdkPixbufFormat *format;
int width;
int height;
} *info = data;
g_return_if_fail (width > 0 && height > 0);
info->format = gdk_pixbuf_loader_get_format (loader);
info->width = width;
info->height = height;
gdk_pixbuf_loader_set_size (loader, 0, 0);
}
/**
* gdk_pixbuf_get_file_info:
* @filename: (type filename): The name of the file to identify.
* @width: (optional) (out): Return location for the width of the
* image, or %NULL
* @height: (optional) (out): Return location for the height of the
* image, or %NULL
*
* Parses an image file far enough to determine its format and size.
*
* Returns: (nullable) (transfer none): A #GdkPixbufFormat describing
* the image format of the file or %NULL if the image format wasn't
* recognized. The return value is owned by #GdkPixbuf and should
* not be freed.
*
* Since: 2.4
**/
GdkPixbufFormat *
gdk_pixbuf_get_file_info (const gchar *filename,
gint *width,
gint *height)
{
GdkPixbufLoader *loader;
guchar buffer[SNIFF_BUFFER_SIZE];
int length;
FILE *f;
struct {
GdkPixbufFormat *format;
gint width;
gint height;
} info;
g_return_val_if_fail (filename != NULL, NULL);
f = g_fopen (filename, "rb");
if (!f)
return NULL;
loader = _gdk_pixbuf_loader_new_with_filename (filename);
info.format = NULL;
info.width = -1;
info.height = -1;
g_signal_connect (loader, "size-prepared", G_CALLBACK (info_cb), &info);
while (!feof (f) && !ferror (f)) {
length = fread (buffer, 1, sizeof (buffer), f);
if (length > 0) {
if (!gdk_pixbuf_loader_write (loader, buffer, length, NULL))
break;
}
if (info.format != NULL)
break;
}
fclose (f);
gdk_pixbuf_loader_close (loader, NULL);
g_object_unref (loader);
if (width)
*width = info.width;
if (height)
*height = info.height;
return info.format;
}
typedef struct {
gchar *filename;
gint width;
gint height;
} GetFileInfoAsyncData;
static void
get_file_info_async_data_free (GetFileInfoAsyncData *data)
{
g_free (data->filename);
g_slice_free (GetFileInfoAsyncData, data);
}
static void
get_file_info_thread (GTask *task,
gpointer source_object,
GetFileInfoAsyncData *data,
GCancellable *cancellable)
{
GdkPixbufFormat *format;
format = gdk_pixbuf_get_file_info (data->filename, &data->width, &data->height);
if (format == NULL) {
g_task_return_new_error (task,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
"Failed to recognize image format");
} else {
g_task_return_pointer (task,
gdk_pixbuf_format_copy (format),
(GDestroyNotify) gdk_pixbuf_format_free);
}
}
/**
* gdk_pixbuf_get_file_info_async:
* @filename: (type filename): The name of the file to identify
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
* @callback: a #GAsyncReadyCallback to call when the file info is available
* @user_data: the data to pass to the callback function
*
* Asynchronously parses an image file far enough to determine its
* format and size.
*
* For more details see gdk_pixbuf_get_file_info(), which is the synchronous
* version of this function.
*
* When the operation is finished, @callback will be called in the
* main thread. You can then call gdk_pixbuf_get_file_info_finish() to
* get the result of the operation.
*
* Since: 2.32
**/
void
gdk_pixbuf_get_file_info_async (const gchar *filename,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GetFileInfoAsyncData *data;
GTask *task;
g_return_if_fail (filename != NULL);
g_return_if_fail (callback != NULL);
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
data = g_slice_new0 (GetFileInfoAsyncData);
data->filename = g_strdup (filename);
task = g_task_new (NULL, cancellable, callback, user_data);
g_task_set_return_on_cancel (task, TRUE);
g_task_set_source_tag (task, gdk_pixbuf_get_file_info_async);
g_task_set_task_data (task, data, (GDestroyNotify) get_file_info_async_data_free);
g_task_run_in_thread (task, (GTaskThreadFunc) get_file_info_thread);
g_object_unref (task);
}
/**
* gdk_pixbuf_get_file_info_finish:
* @async_result: a #GAsyncResult
* @width: (out): Return location for the width of the image, or %NULL
* @height: (out): Return location for the height of the image, or %NULL
* @error: a #GError, or %NULL
*
* Finishes an asynchronous pixbuf parsing operation started with
* gdk_pixbuf_get_file_info_async().
*
* Returns: (transfer none): A #GdkPixbufFormat describing the image
* format of the file or %NULL if the image format wasn't
* recognized. The return value is owned by GdkPixbuf and should
* not be freed.
*
* Since: 2.32
**/
GdkPixbufFormat *
gdk_pixbuf_get_file_info_finish (GAsyncResult *async_result,
gint *width,
gint *height,
GError **error)
{
GetFileInfoAsyncData *data;
GTask *task;
g_return_val_if_fail (g_task_is_valid (async_result, NULL), NULL);
task = G_TASK (async_result);
g_return_val_if_fail (!error || (error && !*error), NULL);
g_warn_if_fail (g_task_get_source_tag (task) == gdk_pixbuf_get_file_info_async);
data = g_task_get_task_data (task);
if (!g_task_had_error (task)) {
if (width)
*width = data->width;
if (height)
*height = data->height;
}
return g_task_propagate_pointer (task, error);
}
/**
* gdk_pixbuf_new_from_xpm_data:
* @data: (array zero-terminated=1): Pointer to inline XPM data.
*
* Creates a new pixbuf by parsing XPM data in memory. This data is commonly
* the result of including an XPM file into a program's C source.
*
* Return value: A newly-created pixbuf with a reference count of 1.
**/
GdkPixbuf *
gdk_pixbuf_new_from_xpm_data (const char **data)
{
GdkPixbuf *(* load_xpm_data) (const char **data);
GdkPixbuf *pixbuf;
GError *error = NULL;
GdkPixbufModule *xpm_module;
g_return_val_if_fail (data != NULL, NULL);
xpm_module = _gdk_pixbuf_get_named_module ("xpm", &error);
if (xpm_module == NULL) {
g_warning ("Error loading XPM image loader: %s", error->message);
g_error_free (error);
return NULL;
}
if (!_gdk_pixbuf_load_module (xpm_module, &error)) {
g_warning ("Error loading XPM image loader: %s", error->message);
g_error_free (error);
return NULL;
}
if (xpm_module->load_xpm_data == NULL) {
g_warning ("gdk-pixbuf XPM module lacks XPM data capability");
pixbuf = NULL;
} else {
load_xpm_data = xpm_module->load_xpm_data;
pixbuf = (* load_xpm_data) (data);
}
return pixbuf;
}
static void
collect_save_options (va_list opts,
gchar ***keys,
gchar ***vals)
{
gchar *key;
gchar *val;
gchar *next;
gint count;
count = 0;
*keys = NULL;
*vals = NULL;
next = va_arg (opts, gchar*);
while (next)
{
key = next;
val = va_arg (opts, gchar*);
++count;
/* woo, slow */
*keys = g_realloc (*keys, sizeof(gchar*) * (count + 1));
*vals = g_realloc (*vals, sizeof(gchar*) * (count + 1));
(*keys)[count-1] = g_strdup (key);
(*vals)[count-1] = g_strdup (val);
(*keys)[count] = NULL;
(*vals)[count] = NULL;
next = va_arg (opts, gchar*);
}
}
static gboolean
save_to_file_callback (const gchar *buf,
gsize count,
GError **error,
gpointer data)
{
FILE *filehandle = data;
gsize n;
n = fwrite (buf, 1, count, filehandle);
if (n != count) {
gint save_errno = errno;
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Error writing to image file: %s"),
g_strerror (save_errno));
return FALSE;
}
return TRUE;
}
static gboolean
gdk_pixbuf_real_save (GdkPixbuf *pixbuf,
FILE *filehandle,
const char *type,
gchar **keys,
gchar **values,
GError **error)
{
gboolean ret;
GdkPixbufModule *image_module = NULL;
image_module = _gdk_pixbuf_get_named_module (type, error);
if (image_module == NULL)
return FALSE;
if (!_gdk_pixbuf_load_module (image_module, error))
return FALSE;
if (image_module->save) {
/* save normally */
ret = (* image_module->save) (filehandle, pixbuf,
keys, values,
error);
} else if (image_module->save_to_callback) {
/* save with simple callback */
ret = (* image_module->save_to_callback) (save_to_file_callback,
filehandle, pixbuf,
keys, values,
error);
} else {
/* can't save */
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
_("This build of gdk-pixbuf does not support saving the image format: %s"),
type);
ret = FALSE;
}
return ret;
}
#define TMP_FILE_BUF_SIZE 4096
static gboolean
save_to_callback_with_tmp_file (GdkPixbufModule *image_module,
GdkPixbuf *pixbuf,
GdkPixbufSaveFunc save_func,
gpointer user_data,
gchar **keys,
gchar **values,
GError **error)
{
int fd;
FILE *f = NULL;
gboolean retval = FALSE;
gchar *buf = NULL;
gsize n;
gchar *filename = NULL;
buf = g_try_malloc (TMP_FILE_BUF_SIZE);
if (buf == NULL) {
g_set_error_literal (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
_("Insufficient memory to save image to callback"));
goto end;
}
fd = g_file_open_tmp ("gdkpixbuf-save-tmp.XXXXXX", &filename, error);
if (fd == -1)
goto end;
f = fdopen (fd, "wb+");
if (f == NULL) {
gint save_errno = errno;
g_set_error_literal (error,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to open temporary file"));
goto end;
}
retval = (image_module->save) (f, pixbuf, keys, values, error);
if (!retval)
goto end;
rewind (f);
for (;;) {
n = fread (buf, 1, TMP_FILE_BUF_SIZE, f);
if (n > 0) {
if (!save_func (buf, n, error, user_data))
goto end;
}
if (n != TMP_FILE_BUF_SIZE)
break;
}
if (ferror (f)) {
gint save_errno = errno;
g_set_error_literal (error,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to read from temporary file"));
goto end;
}
retval = TRUE;
end:
/* cleanup and return retval */
if (f)
fclose (f);
if (filename) {
g_unlink (filename);
g_free (filename);
}
g_free (buf);
return retval;
}
static gboolean
gdk_pixbuf_real_save_to_callback (GdkPixbuf *pixbuf,
GdkPixbufSaveFunc save_func,
gpointer user_data,
const char *type,
gchar **keys,
gchar **values,
GError **error)
{
gboolean ret;
GdkPixbufModule *image_module = NULL;
image_module = _gdk_pixbuf_get_named_module (type, error);
if (image_module == NULL)
return FALSE;
if (!_gdk_pixbuf_load_module (image_module, error))
return FALSE;
if (image_module->save_to_callback) {
/* save normally */
ret = (* image_module->save_to_callback) (save_func, user_data,
pixbuf, keys, values,
error);
} else if (image_module->save) {
/* use a temporary file */
ret = save_to_callback_with_tmp_file (image_module, pixbuf,
save_func, user_data,
keys, values,
error);
} else {
/* can't save */
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
_("This build of gdk-pixbuf does not support saving the image format: %s"),
type);
ret = FALSE;
}
return ret;
}
/**
* gdk_pixbuf_save:
* @pixbuf: a #GdkPixbuf.
* @filename: (type filename): name of file to save.
* @type: name of file format.
* @error: (allow-none): return location for error, or %NULL
* @...: list of key-value save options, followed by %NULL
*
* Saves pixbuf to a file in format @type. By default, "jpeg", "png", "ico"
* and "bmp" are possible file formats to save in, but more formats may be
* installed. The list of all writable formats can be determined in the
* following way:
*
* |[
* void add_if_writable (GdkPixbufFormat *data, GSList **list)
* {
* if (gdk_pixbuf_format_is_writable (data))
* *list = g_slist_prepend (*list, data);
* }
*
* GSList *formats = gdk_pixbuf_get_formats ();
* GSList *writable_formats = NULL;
* g_slist_foreach (formats, add_if_writable, &writable_formats);
* g_slist_free (formats);
* ]|
*
* If @error is set, %FALSE will be returned. Possible errors include
* those in the #GDK_PIXBUF_ERROR domain and those in the #G_FILE_ERROR domain.
*
* The variable argument list should be %NULL-terminated; if not empty,
* it should contain pairs of strings that modify the save
* parameters. For example:
* |[
* gdk_pixbuf_save (pixbuf, handle, "jpeg", &error, "quality", "100", NULL);
* ]|
*
* Currently only few parameters exist. JPEG images can be saved with a
* "quality" parameter; its value should be in the range [0,100]. JPEG
* and PNG density can be set by setting the "x-dpi" and "y-dpi" parameters
* to the appropriate values in dots per inch.
*
* Text chunks can be attached to PNG images by specifying parameters of
* the form "tEXt::key", where key is an ASCII string of length 1-79.
* The values are UTF-8 encoded strings. The PNG compression level can
* be specified using the "compression" parameter; it's value is in an
* integer in the range of [0,9].
*
* ICC color profiles can also be embedded into PNG, JPEG and TIFF images.
* The "icc-profile" value should be the complete ICC profile encoded
* into base64.
*
* |[
* gchar *contents;
* gchar *contents_encode;
* gsize length;
* g_file_get_contents ("/home/hughsie/.color/icc/L225W.icm", &contents, &length, NULL);
* contents_encode = g_base64_encode ((const guchar *) contents, length);
* gdk_pixbuf_save (pixbuf, handle, "png", &error, "icc-profile", contents_encode, NULL);
* ]|
*
* TIFF images recognize: (1) a "bits-per-sample" option (integer) which
* can be either 1 for saving bi-level CCITTFAX4 images, or 8 for saving
* 8-bits per sample; (2) a "compression" option (integer) which can be
* 1 for no compression, 2 for Huffman, 5 for LZW, 7 for JPEG and 8 for
* DEFLATE (see the libtiff documentation and tiff.h for all supported
* codec values); (3) an "icc-profile" option (zero-terminated string)
* containing a base64 encoded ICC color profile.
*
* ICO images can be saved in depth 16, 24, or 32, by using the "depth"
* parameter. When the ICO saver is given "x_hot" and "y_hot" parameters,
* it produces a CUR instead of an ICO.
*
* Return value: whether an error was set
**/
gboolean
gdk_pixbuf_save (GdkPixbuf *pixbuf,
const char *filename,
const char *type,
GError **error,
...)
{
gchar **keys = NULL;
gchar **values = NULL;
va_list args;
gboolean result;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
va_start (args, error);
collect_save_options (args, &keys, &values);
va_end (args);
result = gdk_pixbuf_savev (pixbuf, filename, type,
keys, values,
error);
g_strfreev (keys);
g_strfreev (values);
return result;
}
/**
* gdk_pixbuf_savev:
* @pixbuf: a #GdkPixbuf.
* @filename: (type filename): name of file to save.
* @type: name of file format.
* @option_keys: (array zero-terminated=1): name of options to set, %NULL-terminated
* @option_values: (array zero-terminated=1): values for named options
* @error: (allow-none): return location for error, or %NULL
*
* Saves pixbuf to a file in @type, which is currently "jpeg", "png", "tiff", "ico" or "bmp".
* If @error is set, %FALSE will be returned.
* See gdk_pixbuf_save () for more details.
*
* Return value: whether an error was set
**/
gboolean
gdk_pixbuf_savev (GdkPixbuf *pixbuf,
const char *filename,
const char *type,
char **option_keys,
char **option_values,
GError **error)
{
FILE *f = NULL;
gboolean result;
g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
g_return_val_if_fail (gdk_pixbuf_get_width (pixbuf) >= 0, FALSE);
g_return_val_if_fail (gdk_pixbuf_get_height (pixbuf) >= 0, FALSE);
g_return_val_if_fail (gdk_pixbuf_get_n_channels (pixbuf) >= 0, FALSE);
g_return_val_if_fail (filename != NULL, FALSE);
g_return_val_if_fail (type != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
f = g_fopen (filename, "wb");
if (f == NULL) {
gint save_errno = errno;
gchar *display_name = g_filename_display_name (filename);
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to open ā%sā for writing: %s"),
display_name,
g_strerror (save_errno));
g_free (display_name);
return FALSE;
}
result = gdk_pixbuf_real_save (pixbuf, f, type,
option_keys, option_values,
error);
if (!result) {
g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
fclose (f);
g_unlink (filename);
return FALSE;
}
if (fclose (f) < 0) {
gint save_errno = errno;
gchar *display_name = g_filename_display_name (filename);
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to close ā%sā while writing image, all data may not have been saved: %s"),
display_name,
g_strerror (save_errno));
g_free (display_name);
return FALSE;
}
return TRUE;
}
#ifdef G_OS_WIN32
/**
* gdk_pixbuf_savev_utf8:
* @pixbuf: a #GdkPixbuf.
* @filename: name of file to save.
* @type: name of file format.
* @option_keys: (array zero-terminated=1): name of options to set, %NULL-terminated
* @option_values: (array zero-terminated=1): values for named options
* @error: (allow-none): return location for error, or %NULL
*
* Same as gdk_pixbuf_savev()
*
* Return value: whether an error was set
**/
gboolean
gdk_pixbuf_savev_utf8 (GdkPixbuf *pixbuf,
const char *filename,
const char *type,
char **option_keys,
char **option_values,
GError **error)
{
return gdk_pixbuf_savev (pixbuf, filename, type, option_keys,
option_values, error);
}
#endif
/**
* gdk_pixbuf_save_to_callback:
* @pixbuf: a #GdkPixbuf.
* @save_func: (scope call): a function that is called to save each block of data that
* the save routine generates.
* @user_data: user data to pass to the save function.
* @type: name of file format.
* @error: (allow-none): return location for error, or %NULL
* @...: list of key-value save options
*
* Saves pixbuf in format @type by feeding the produced data to a
* callback. Can be used when you want to store the image to something
* other than a file, such as an in-memory buffer or a socket.
* If @error is set, %FALSE will be returned. Possible errors
* include those in the #GDK_PIXBUF_ERROR domain and whatever the save
* function generates.
*
* See gdk_pixbuf_save() for more details.
*
* Return value: whether an error was set
*
* Since: 2.4
**/
gboolean
gdk_pixbuf_save_to_callback (GdkPixbuf *pixbuf,
GdkPixbufSaveFunc save_func,
gpointer user_data,
const char *type,
GError **error,
...)
{
gchar **keys = NULL;
gchar **values = NULL;
va_list args;
gboolean result;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
va_start (args, error);
collect_save_options (args, &keys, &values);
va_end (args);
result = gdk_pixbuf_save_to_callbackv (pixbuf, save_func, user_data,
type, keys, values,
error);
g_strfreev (keys);
g_strfreev (values);
return result;
}
/**
* gdk_pixbuf_save_to_callbackv:
* @pixbuf: a #GdkPixbuf.
* @save_func: (scope call): a function that is called to save each block of data that
* the save routine generates.
* @user_data: (closure): user data to pass to the save function.
* @type: name of file format.
* @option_keys: (array zero-terminated=1) (element-type utf8): name of options to set, %NULL-terminated
* @option_values: (array zero-terminated=1) (element-type utf8): values for named options
* @error: (allow-none): return location for error, or %NULL
*
* Saves pixbuf to a callback in format @type, which is currently "jpeg",
* "png", "tiff", "ico" or "bmp". If @error is set, %FALSE will be returned. See
* gdk_pixbuf_save_to_callback () for more details.
*
* Return value: whether an error was set
*
* Since: 2.4
**/
gboolean
gdk_pixbuf_save_to_callbackv (GdkPixbuf *pixbuf,
GdkPixbufSaveFunc save_func,
gpointer user_data,
const char *type,
char **option_keys,
char **option_values,
GError **error)
{
gboolean result;
g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
g_return_val_if_fail (gdk_pixbuf_get_width (pixbuf) >= 0, FALSE);
g_return_val_if_fail (gdk_pixbuf_get_height (pixbuf) >= 0, FALSE);
g_return_val_if_fail (gdk_pixbuf_get_n_channels (pixbuf) >= 0, FALSE);
g_return_val_if_fail (save_func != NULL, FALSE);
g_return_val_if_fail (type != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
result = gdk_pixbuf_real_save_to_callback (pixbuf,
save_func, user_data, type,
option_keys, option_values,
error);
if (!result) {
g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
return FALSE;
}
return TRUE;
}
/**
* gdk_pixbuf_save_to_buffer:
* @pixbuf: a #GdkPixbuf.
* @buffer: (array length=buffer_size) (out) (element-type guint8): location to receive a pointer
* to the new buffer.
* @buffer_size: location to receive the size of the new buffer.
* @type: name of file format.
* @error: (allow-none): return location for error, or %NULL
* @...: list of key-value save options
*
* Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
* "png", "tiff", "ico" or "bmp". This is a convenience function that uses
* gdk_pixbuf_save_to_callback() to do the real work. Note that the buffer
* is not nul-terminated and may contain embedded nuls.
* If @error is set, %FALSE will be returned and @buffer will be set to
* %NULL. Possible errors include those in the #GDK_PIXBUF_ERROR
* domain.
*
* See gdk_pixbuf_save() for more details.
*
* Return value: whether an error was set
*
* Since: 2.4
**/
gboolean
gdk_pixbuf_save_to_buffer (GdkPixbuf *pixbuf,
gchar **buffer,
gsize *buffer_size,
const char *type,
GError **error,
...)
{
gchar **keys = NULL;
gchar **values = NULL;
va_list args;
gboolean result;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
va_start (args, error);
collect_save_options (args, &keys, &values);
va_end (args);
result = gdk_pixbuf_save_to_bufferv (pixbuf, buffer, buffer_size,
type, keys, values,
error);
g_strfreev (keys);
g_strfreev (values);
return result;
}
struct SaveToBufferData {
gchar *buffer;
gsize len, max;
};
static gboolean
save_to_buffer_callback (const gchar *data,
gsize count,
GError **error,
gpointer user_data)
{
struct SaveToBufferData *sdata = user_data;
gchar *new_buffer;
gsize new_max;
if (sdata->len + count > sdata->max) {
new_max = MAX (sdata->max*2, sdata->len + count);
new_buffer = g_try_realloc (sdata->buffer, new_max);
if (!new_buffer) {
g_set_error_literal (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
_("Insufficient memory to save image into a buffer"));
return FALSE;
}
sdata->buffer = new_buffer;
sdata->max = new_max;
}
memcpy (sdata->buffer + sdata->len, data, count);
sdata->len += count;
return TRUE;
}
/**
* gdk_pixbuf_save_to_bufferv:
* @pixbuf: a #GdkPixbuf.
* @buffer: (array length=buffer_size) (out) (element-type guint8):
* location to receive a pointer to the new buffer.
* @buffer_size: location to receive the size of the new buffer.
* @type: name of file format.
* @option_keys: (array zero-terminated=1): name of options to set, %NULL-terminated
* @option_values: (array zero-terminated=1): values for named options
* @error: (allow-none): return location for error, or %NULL
*
* Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
* "tiff", "png", "ico" or "bmp". See gdk_pixbuf_save_to_buffer()
* for more details.
*
* Return value: whether an error was set
*
* Since: 2.4
**/
gboolean
gdk_pixbuf_save_to_bufferv (GdkPixbuf *pixbuf,
gchar **buffer,
gsize *buffer_size,
const char *type,
char **option_keys,
char **option_values,
GError **error)
{
static const gint initial_max = 1024;
struct SaveToBufferData sdata;
*buffer = NULL;
*buffer_size = 0;
sdata.buffer = g_try_malloc (initial_max);
sdata.max = initial_max;
sdata.len = 0;
if (!sdata.buffer) {
g_set_error_literal (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
_("Insufficient memory to save image into a buffer"));
return FALSE;
}
if (!gdk_pixbuf_save_to_callbackv (pixbuf,
save_to_buffer_callback, &sdata,
type, option_keys, option_values,
error)) {
g_free (sdata.buffer);
return FALSE;
}
*buffer = sdata.buffer;
*buffer_size = sdata.len;
return TRUE;
}
typedef struct {
GOutputStream *stream;
GCancellable *cancellable;
} SaveToStreamData;
static gboolean
save_to_stream (const gchar *buffer,
gsize count,
GError **error,
gpointer data)
{
SaveToStreamData *sdata = (SaveToStreamData *)data;
gsize remaining;
gssize written;
GError *my_error = NULL;
remaining = count;
written = 0;
while (remaining > 0) {
buffer += written;
remaining -= written;
written = g_output_stream_write (sdata->stream,
buffer, remaining,
sdata->cancellable,
&my_error);
if (written < 0) {
if (!my_error) {
g_set_error_literal (error,
G_IO_ERROR, 0,
_("Error writing to image stream"));
}
else {
g_propagate_error (error, my_error);
}
return FALSE;
}
}
return TRUE;
}
/**
* gdk_pixbuf_save_to_streamv:
* @pixbuf: a #GdkPixbuf
* @stream: a #GOutputStream to save the pixbuf to
* @type: name of file format
* @option_keys: (array zero-terminated=1): name of options to set, %NULL-terminated
* @option_values: (array zero-terminated=1): values for named options
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
* @error: (allow-none): return location for error, or %NULL
*
* Saves @pixbuf to an output stream.
*
* Supported file formats are currently "jpeg", "tiff", "png", "ico" or
* "bmp". See gdk_pixbuf_save_to_stream() for more details.
*
* Returns: %TRUE if the pixbuf was saved successfully, %FALSE if an
* error was set.
*
* Since: 2.36
*/
gboolean
gdk_pixbuf_save_to_streamv (GdkPixbuf *pixbuf,
GOutputStream *stream,
const char *type,
char **option_keys,
char **option_values,
GCancellable *cancellable,
GError **error)
{
SaveToStreamData data;
data.stream = stream;
data.cancellable = cancellable;
return gdk_pixbuf_save_to_callbackv (pixbuf, save_to_stream,
&data, type,
option_keys, option_values,
error);
}
/**
* gdk_pixbuf_save_to_stream:
* @pixbuf: a #GdkPixbuf
* @stream: a #GOutputStream to save the pixbuf to
* @type: name of file format
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
* @error: (allow-none): return location for error, or %NULL
* @...: list of key-value save options
*
* Saves @pixbuf to an output stream.
*
* Supported file formats are currently "jpeg", "tiff", "png", "ico" or
* "bmp". See gdk_pixbuf_save_to_buffer() for more details.
*
* The @cancellable can be used to abort the operation from another
* thread. If the operation was cancelled, the error %G_IO_ERROR_CANCELLED
* will be returned. Other possible errors are in the #GDK_PIXBUF_ERROR
* and %G_IO_ERROR domains.
*
* The stream is not closed.
*
* Returns: %TRUE if the pixbuf was saved successfully, %FALSE if an
* error was set.
*
* Since: 2.14
*/
gboolean
gdk_pixbuf_save_to_stream (GdkPixbuf *pixbuf,
GOutputStream *stream,
const char *type,
GCancellable *cancellable,
GError **error,
...)
{
gboolean res;
gchar **keys = NULL;
gchar **values = NULL;
va_list args;
va_start (args, error);
collect_save_options (args, &keys, &values);
va_end (args);
res = gdk_pixbuf_save_to_streamv (pixbuf, stream, type,
keys, values,
cancellable, error);
g_strfreev (keys);
g_strfreev (values);
return res;
}
typedef struct {
GOutputStream *stream;
gchar *type;
gchar **keys;
gchar **values;
} SaveToStreamAsyncData;
static void
save_to_stream_async_data_free (SaveToStreamAsyncData *data)
{
if (data->stream)
g_object_unref (data->stream);
g_strfreev (data->keys);
g_strfreev (data->values);
g_free (data->type);
g_slice_free (SaveToStreamAsyncData, data);
}
static void
save_to_stream_thread (GTask *task,
GdkPixbuf *pixbuf,
SaveToStreamAsyncData *data,
GCancellable *cancellable)
{
SaveToStreamData sync_data;
gboolean retval;
GError *error = NULL;
sync_data.stream = data->stream;
sync_data.cancellable = cancellable;
retval = gdk_pixbuf_save_to_callbackv (pixbuf, save_to_stream,
&sync_data, data->type,
data->keys, data->values,
&error);
if (retval == FALSE) {
g_task_return_error (task, error);
} else {
g_task_return_boolean (task, TRUE);
}
}
/**
* gdk_pixbuf_save_to_streamv_async:
* @pixbuf: a #GdkPixbuf
* @stream: a #GOutputStream to which to save the pixbuf
* @type: name of file format
* @option_keys: (array zero-terminated=1): name of options to set, %NULL-terminated
* @option_values: (array zero-terminated=1): values for named options
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
* @callback: a #GAsyncReadyCallback to call when the pixbuf is saved
* @user_data: the data to pass to the callback function
*
* Saves @pixbuf to an output stream asynchronously.
*
* For more details see gdk_pixbuf_save_to_streamv(), which is the synchronous
* version of this function.
*
* When the operation is finished, @callback will be called in the main thread.
* You can then call gdk_pixbuf_save_to_stream_finish() to get the result of the operation.
*
* Since: 2.36
**/
void
gdk_pixbuf_save_to_streamv_async (GdkPixbuf *pixbuf,
GOutputStream *stream,
const gchar *type,
gchar **option_keys,
gchar **option_values,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
SaveToStreamAsyncData *data;
g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
g_return_if_fail (gdk_pixbuf_get_width (pixbuf) >= 0);
g_return_if_fail (gdk_pixbuf_get_height (pixbuf) >= 0);
g_return_if_fail (gdk_pixbuf_get_n_channels (pixbuf) >= 0);
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
g_return_if_fail (type != NULL);
g_return_if_fail (callback != NULL);
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
data = g_slice_new (SaveToStreamAsyncData);
data->stream = g_object_ref (stream);
data->type = g_strdup (type);
data->keys = g_strdupv (option_keys);
data->values = g_strdupv (option_values);
task = g_task_new (pixbuf, cancellable, callback, user_data);
g_task_set_source_tag (task, gdk_pixbuf_save_to_streamv_async);
g_task_set_task_data (task, data, (GDestroyNotify) save_to_stream_async_data_free);
g_task_run_in_thread (task, (GTaskThreadFunc) save_to_stream_thread);
g_object_unref (task);
}
/**
* gdk_pixbuf_save_to_stream_async:
* @pixbuf: a #GdkPixbuf
* @stream: a #GOutputStream to which to save the pixbuf
* @type: name of file format
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
* @callback: a #GAsyncReadyCallback to call when the pixbuf is saved
* @user_data: the data to pass to the callback function
* @...: list of key-value save options
*
* Saves @pixbuf to an output stream asynchronously.
*
* For more details see gdk_pixbuf_save_to_stream(), which is the synchronous
* version of this function.
*
* When the operation is finished, @callback will be called in the main thread.
* You can then call gdk_pixbuf_save_to_stream_finish() to get the result of the operation.
*
* Since: 2.24
**/
void
gdk_pixbuf_save_to_stream_async (GdkPixbuf *pixbuf,
GOutputStream *stream,
const gchar *type,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data,
...)
{
gchar **keys = NULL;
gchar **values = NULL;
va_list args;
g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
g_return_if_fail (gdk_pixbuf_get_width (pixbuf) >= 0);
g_return_if_fail (gdk_pixbuf_get_height (pixbuf) >= 0);
g_return_if_fail (gdk_pixbuf_get_n_channels (pixbuf) >= 0);
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
g_return_if_fail (type != NULL);
g_return_if_fail (callback != NULL);
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
va_start (args, user_data);
collect_save_options (args, &keys, &values);
va_end (args);
gdk_pixbuf_save_to_streamv_async (pixbuf, stream, type,
keys, values,
cancellable, callback, user_data);
g_strfreev (keys);
g_strfreev (values);
}
/**
* gdk_pixbuf_save_to_stream_finish:
* @async_result: a #GAsyncResult
* @error: a #GError, or %NULL
*
* Finishes an asynchronous pixbuf save operation started with
* gdk_pixbuf_save_to_stream_async().
*
* Return value: %TRUE if the pixbuf was saved successfully, %FALSE if an error was set.
*
* Since: 2.24
**/
gboolean
gdk_pixbuf_save_to_stream_finish (GAsyncResult *async_result,
GError **error)
{
GTask *task;
/* Can not use g_task_is_valid because our GTask has a
* source_object which is not available to us anymore.
*/
g_return_val_if_fail (G_IS_TASK (async_result), FALSE);
task = G_TASK (async_result);
g_return_val_if_fail (!error || (error && !*error), FALSE);
g_warn_if_fail (g_task_get_source_tag (task) == gdk_pixbuf_save_to_stream_async ||
g_task_get_source_tag (task) == gdk_pixbuf_save_to_streamv_async);
return g_task_propagate_boolean (task, error);
}
/**
* gdk_pixbuf_format_get_name:
* @format: a #GdkPixbufFormat
*
* Returns the name of the format.
*
* Return value: the name of the format.
*
* Since: 2.2
*/
gchar *
gdk_pixbuf_format_get_name (GdkPixbufFormat *format)
{
g_return_val_if_fail (format != NULL, NULL);
return g_strdup (format->name);
}
/**
* gdk_pixbuf_format_get_description:
* @format: a #GdkPixbufFormat
*
* Returns a description of the format.
*
* Return value: a description of the format.
*
* Since: 2.2
*/
gchar *
gdk_pixbuf_format_get_description (GdkPixbufFormat *format)
{
gchar *domain;
const gchar *description;
g_return_val_if_fail (format != NULL, NULL);
if (format->domain != NULL)
domain = format->domain;
else
domain = GETTEXT_PACKAGE;
description = g_dgettext (domain, format->description);
return g_strdup (description);
}
/**
* gdk_pixbuf_format_get_mime_types:
* @format: a #GdkPixbufFormat
*
* Returns the mime types supported by the format.
*
* Return value: (transfer full): a %NULL-terminated array of mime types which must be freed with
* g_strfreev() when it is no longer needed.
*
* Since: 2.2
*/
gchar **
gdk_pixbuf_format_get_mime_types (GdkPixbufFormat *format)
{
g_return_val_if_fail (format != NULL, NULL);
return g_strdupv (format->mime_types);
}
/**
* gdk_pixbuf_format_get_extensions:
* @format: a #GdkPixbufFormat
*
* Returns the filename extensions typically used for files in the
* given format.
*
* Return value: (transfer full): a %NULL-terminated array of filename extensions which must be
* freed with g_strfreev() when it is no longer needed.
*
* Since: 2.2
*/
gchar **
gdk_pixbuf_format_get_extensions (GdkPixbufFormat *format)
{
g_return_val_if_fail (format != NULL, NULL);
return g_strdupv (format->extensions);
}
/**
* gdk_pixbuf_format_is_writable:
* @format: a #GdkPixbufFormat
*
* Returns whether pixbufs can be saved in the given format.
*
* Return value: whether pixbufs can be saved in the given format.
*
* Since: 2.2
*/
gboolean
gdk_pixbuf_format_is_writable (GdkPixbufFormat *format)
{
g_return_val_if_fail (format != NULL, FALSE);
return (format->flags & GDK_PIXBUF_FORMAT_WRITABLE) != 0;
}
/**
* gdk_pixbuf_format_is_scalable:
* @format: a #GdkPixbufFormat
*
* Returns whether this image format is scalable. If a file is in a
* scalable format, it is preferable to load it at the desired size,
* rather than loading it at the default size and scaling the
* resulting pixbuf to the desired size.
*
* Return value: whether this image format is scalable.
*
* Since: 2.6
*/
gboolean
gdk_pixbuf_format_is_scalable (GdkPixbufFormat *format)
{
g_return_val_if_fail (format != NULL, FALSE);
return (format->flags & GDK_PIXBUF_FORMAT_SCALABLE) != 0;
}
/**
* gdk_pixbuf_format_is_disabled:
* @format: a #GdkPixbufFormat
*
* Returns whether this image format is disabled. See
* gdk_pixbuf_format_set_disabled().
*
* Return value: whether this image format is disabled.
*
* Since: 2.6
*/
gboolean
gdk_pixbuf_format_is_disabled (GdkPixbufFormat *format)
{
g_return_val_if_fail (format != NULL, FALSE);
return format->disabled;
}
/**
* gdk_pixbuf_format_set_disabled:
* @format: a #GdkPixbufFormat
* @disabled: %TRUE to disable the format @format
*
* Disables or enables an image format. If a format is disabled,
* gdk-pixbuf won't use the image loader for this format to load
* images. Applications can use this to avoid using image loaders
* with an inappropriate license, see gdk_pixbuf_format_get_license().
*
* Since: 2.6
*/
void
gdk_pixbuf_format_set_disabled (GdkPixbufFormat *format,
gboolean disabled)
{
g_return_if_fail (format != NULL);
format->disabled = disabled != FALSE;
}
/**
* gdk_pixbuf_format_get_license:
* @format: a #GdkPixbufFormat
*
* Returns information about the license of the image loader for the format. The
* returned string should be a shorthand for a wellknown license, e.g. "LGPL",
* "GPL", "QPL", "GPL/QPL", or "other" to indicate some other license. This
* string should be freed with g_free() when it's no longer needed.
*
* Returns: a string describing the license of @format.
*
* Since: 2.6
*/
gchar*
gdk_pixbuf_format_get_license (GdkPixbufFormat *format)
{
g_return_val_if_fail (format != NULL, NULL);
return g_strdup (format->license);
}
GdkPixbufFormat *
_gdk_pixbuf_get_format (GdkPixbufModule *module)
{
g_return_val_if_fail (module != NULL, NULL);
return module->info;
}
/**
* gdk_pixbuf_get_formats:
*
* Obtains the available information about the image formats supported
* by GdkPixbuf.
*
* Returns: (transfer container) (element-type GdkPixbufFormat): A list of
* #GdkPixbufFormats describing the supported image formats. The list should
* be freed when it is no longer needed, but the structures themselves are
* owned by #GdkPixbuf and should not be freed.
*
* Since: 2.2
*/
GSList *
gdk_pixbuf_get_formats (void)
{
GSList *result = NULL;
GSList *modules;
for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
GdkPixbufFormat *info = _gdk_pixbuf_get_format (module);
result = g_slist_prepend (result, info);
}
return result;
}
/**
* gdk_pixbuf_format_copy:
* @format: a #GdkPixbufFormat
*
* Creates a copy of @format
*
* Return value: the newly allocated copy of a #GdkPixbufFormat. Use
* gdk_pixbuf_format_free() to free the resources when done
*
* Since: 2.22
*/
GdkPixbufFormat *
gdk_pixbuf_format_copy (const GdkPixbufFormat *format)
{
if (G_LIKELY (format != NULL))
return g_slice_dup (GdkPixbufFormat, format);
return NULL;
}
/**
* gdk_pixbuf_format_free:
* @format: a #GdkPixbufFormat
*
* Frees the resources allocated when copying a #GdkPixbufFormat
* using gdk_pixbuf_format_copy()
*
* Since: 2.22
*/
void
gdk_pixbuf_format_free (GdkPixbufFormat *format)
{
if (G_LIKELY (format != NULL))
g_slice_free (GdkPixbufFormat, format);
}
/**
* gdk_pixbuf_format_is_save_option_supported:
* @format: a #GdkPixbufFormat
* @option_key: the name of an option
*
* Returns %TRUE if the save option specified by @option_key is supported when
* saving a pixbuf using the module implementing @format.
* See gdk_pixbuf_save() for more information about option keys.
*
* Returns: %TRUE if the specified option is supported
*
* Since: 2.36
*/
gboolean
gdk_pixbuf_format_is_save_option_supported (GdkPixbufFormat *format,
const gchar *option_key)
{
GdkPixbufModule *module;
g_return_val_if_fail (format != NULL, FALSE);
g_return_val_if_fail (option_key != NULL, FALSE);
module = _gdk_pixbuf_get_named_module (format->name, NULL);
if (!module)
return FALSE;
if (!_gdk_pixbuf_load_module (module, NULL))
return FALSE;
if (!module->is_save_option_supported)
return FALSE;
return (* module->is_save_option_supported) (option_key);
}
G_DEFINE_BOXED_TYPE (GdkPixbufFormat, gdk_pixbuf_format,
gdk_pixbuf_format_copy,
gdk_pixbuf_format_free)
|