summaryrefslogtreecommitdiffstats
path: root/src/display/cairo-utils.cpp
blob: 68a7e4ac4634207332bbc62bd30794fc1adca9e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Helper functions to use cairo with inkscape
 *
 * Copyright (C) 2007 bulia byak
 * Copyright (C) 2008 Johan Engelen
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 *
 */

#include "display/cairo-utils.h"

#include <stdexcept>

#include <glib/gstdio.h>
#include <glibmm/fileutils.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include <2geom/pathvector.h>
#include <2geom/curves.h>
#include <2geom/affine.h>
#include <2geom/point.h>
#include <2geom/path.h>
#include <2geom/transforms.h>
#include <2geom/sbasis-to-bezier.h>

#include <boost/algorithm/string.hpp>
#include <boost/operators.hpp>
#include <boost/optional/optional.hpp>

#include "color.h"
#include "cairo-templates.h"
#include "document.h"
#include "preferences.h"
#include "util/units.h"
#include "helper/pixbuf-ops.h"


/**
 * Key for cairo_surface_t to keep track of current color interpolation value
 * Only the address of the structure is used, it is never initialized. See:
 * http://www.cairographics.org/manual/cairo-Types.html#cairo-user-data-key-t
 */
cairo_user_data_key_t ink_color_interpolation_key;
cairo_user_data_key_t ink_pixbuf_key;

namespace Inkscape {

CairoGroup::CairoGroup(cairo_t *_ct) : ct(_ct), pushed(false) {}
CairoGroup::~CairoGroup() {
    if (pushed) {
        cairo_pattern_t *p = cairo_pop_group(ct);
        cairo_pattern_destroy(p);
    }
}
void CairoGroup::push() {
    cairo_push_group(ct);
    pushed = true;
}
void CairoGroup::push_with_content(cairo_content_t content) {
    cairo_push_group_with_content(ct, content);
    pushed = true;
}
cairo_pattern_t *CairoGroup::pop() {
    if (pushed) {
        cairo_pattern_t *ret = cairo_pop_group(ct);
        pushed = false;
        return ret;
    } else {
        throw std::logic_error("Cairo group popped without pushing it first");
    }
}
Cairo::RefPtr<Cairo::Pattern> CairoGroup::popmm() {
    if (pushed) {
        cairo_pattern_t *ret = cairo_pop_group(ct);
        Cairo::RefPtr<Cairo::Pattern> retmm(new Cairo::Pattern(ret, true));
        pushed = false;
        return retmm;
    } else {
        throw std::logic_error("Cairo group popped without pushing it first");
    }
}
void CairoGroup::pop_to_source() {
    if (pushed) {
        cairo_pop_group_to_source(ct);
        pushed = false;
    }
}

CairoContext::CairoContext(cairo_t *obj, bool ref)
    : Cairo::Context(obj, ref)
{}

void CairoContext::transform(Geom::Affine const &m)
{
    cairo_matrix_t cm;
    cm.xx = m[0];
    cm.xy = m[2];
    cm.x0 = m[4];
    cm.yx = m[1];
    cm.yy = m[3];
    cm.y0 = m[5];
    cairo_transform(cobj(), &cm);
}

void CairoContext::set_source_rgba32(guint32 color)
{
    double red = SP_RGBA32_R_F(color);
    double gre = SP_RGBA32_G_F(color);
    double blu = SP_RGBA32_B_F(color);
    double alp = SP_RGBA32_A_F(color);
    cairo_set_source_rgba(cobj(), red, gre, blu, alp);
}

void CairoContext::append_path(Geom::PathVector const &pv)
{
    feed_pathvector_to_cairo(cobj(), pv);
}

Cairo::RefPtr<CairoContext> CairoContext::create(Cairo::RefPtr<Cairo::Surface> const &target)
{
    cairo_t *ct = cairo_create(target->cobj());
    Cairo::RefPtr<CairoContext> ret(new CairoContext(ct, true));
    return ret;
}


/* The class below implement the following hack:
 * 
 * The pixels formats of Cairo and GdkPixbuf are different.
 * GdkPixbuf accesses pixels as bytes, alpha is not premultiplied,
 * and successive bytes of a single pixel contain R, G, B and A components.
 * Cairo accesses pixels as 32-bit ints, alpha is premultiplied,
 * and each int contains as 0xAARRGGBB, accessed with bitwise operations.
 *
 * In other words, on a little endian system, a GdkPixbuf will contain:
 *   char *data = "rgbargbargba...."
 *   int *data = { 0xAABBGGRR, 0xAABBGGRR, 0xAABBGGRR, ... }
 * while a Cairo image surface will contain:
 *   char *data = "bgrabgrabgra...."
 *   int *data = { 0xAARRGGBB, 0xAARRGGBB, 0xAARRGGBB, ... }
 *
 * It is possible to convert between these two formats (almost) losslessly.
 * Some color information from partially transparent regions of the image
 * is lost, but the result when displaying this image will remain the same.
 *
 * The class allows interoperation between GdkPixbuf
 * and Cairo surfaces without creating a copy of the image.
 * This is implemented by creating a GdkPixbuf and a Cairo image surface
 * which share their data. Depending on what is needed at a given time,
 * the pixels are converted in place to the Cairo or the GdkPixbuf format.
 */

/** Create a pixbuf from a Cairo surface.
 * The constructor takes ownership of the passed surface,
 * so it should not be destroyed. */
Pixbuf::Pixbuf(cairo_surface_t *s)
    : _pixbuf(gdk_pixbuf_new_from_data(
        cairo_image_surface_get_data(s), GDK_COLORSPACE_RGB, TRUE, 8,
        cairo_image_surface_get_width(s), cairo_image_surface_get_height(s),
        cairo_image_surface_get_stride(s),
        ink_cairo_pixbuf_cleanup, s))
    , _surface(s)
    , _mod_time(0)
    , _pixel_format(PF_CAIRO)
    , _cairo_store(true)
{}

/** Create a pixbuf from a GdkPixbuf.
 * The constructor takes ownership of the passed GdkPixbuf reference,
 * so it should not be unrefed. */
Pixbuf::Pixbuf(GdkPixbuf *pb)
    : _pixbuf(pb)
    , _surface(nullptr)
    , _mod_time(0)
    , _pixel_format(PF_GDK)
    , _cairo_store(false)
{
    _forceAlpha();
    _surface = cairo_image_surface_create_for_data(
        gdk_pixbuf_get_pixels(_pixbuf), CAIRO_FORMAT_ARGB32,
        gdk_pixbuf_get_width(_pixbuf), gdk_pixbuf_get_height(_pixbuf), gdk_pixbuf_get_rowstride(_pixbuf));
}

Pixbuf::Pixbuf(Inkscape::Pixbuf const &other)
    : _pixbuf(gdk_pixbuf_copy(other._pixbuf))
    , _surface(cairo_image_surface_create_for_data(
        gdk_pixbuf_get_pixels(_pixbuf), CAIRO_FORMAT_ARGB32,
        gdk_pixbuf_get_width(_pixbuf), gdk_pixbuf_get_height(_pixbuf), gdk_pixbuf_get_rowstride(_pixbuf)))
    , _mod_time(other._mod_time)
    , _path(other._path)
    , _pixel_format(other._pixel_format)
    , _cairo_store(false)
{}

Pixbuf::~Pixbuf()
{
    if (_cairo_store) {
        g_object_unref(_pixbuf);
    } else {
        cairo_surface_destroy(_surface);
        g_object_unref(_pixbuf);
    }
}

#if !GDK_PIXBUF_CHECK_VERSION(2, 41, 0)
/**
 * Incremental file read introduced to workaround
 * https://gitlab.gnome.org/GNOME/gdk-pixbuf/issues/70
 */
static bool _workaround_issue_70__gdk_pixbuf_loader_write( //
    GdkPixbufLoader *loader, guchar *decoded, gsize decoded_len, GError **error)
{
    bool success = true;
    gsize bytes_left = decoded_len;
    gsize secret_limit = 0xffff;
    guchar *decoded_head = decoded;
    while (bytes_left && success) {
        gsize bytes = (bytes_left > secret_limit) ? secret_limit : bytes_left;
        success = gdk_pixbuf_loader_write(loader, decoded_head, bytes, error);
        decoded_head += bytes;
        bytes_left -= bytes;
    }

    return success;
}
#define gdk_pixbuf_loader_write _workaround_issue_70__gdk_pixbuf_loader_write
#endif

Pixbuf *Pixbuf::create_from_data_uri(gchar const *uri_data, double svgdpi)
{
    Pixbuf *pixbuf = nullptr;

    bool data_is_image = false;
    bool data_is_svg = false;
    bool data_is_base64 = false;

    gchar const *data = uri_data;

    while (*data) {
        if (strncmp(data,"base64",6) == 0) {
            /* base64-encoding */
            data_is_base64 = true;
            data_is_image = true; // Illustrator produces embedded images without MIME type, so we assume it's image no matter what
            data += 6;
        }
        else if (strncmp(data,"image/png",9) == 0) {
            /* PNG image */
            data_is_image = true;
            data += 9;
        }
        else if (strncmp(data,"image/jpg",9) == 0) {
            /* JPEG image */
            data_is_image = true;
            data += 9;
        }
        else if (strncmp(data,"image/jpeg",10) == 0) {
            /* JPEG image */
            data_is_image = true;
            data += 10;
        }
        else if (strncmp(data,"image/jp2",9) == 0) {
            /* JPEG2000 image */
            data_is_image = true;
            data += 9;
        }
        else if (strncmp(data,"image/svg+xml",13) == 0) {
            /* JPEG2000 image */
            data_is_svg = true;
            data_is_image = true;
            data += 13;
        }
        else { /* unrecognized option; skip it */
            while (*data) {
                if (((*data) == ';') || ((*data) == ',')) {
                    break;
                }
                data++;
            }
        }
        if ((*data) == ';') {
            data++;
            continue;
        }
        if ((*data) == ',') {
            data++;
            break;
        }
    }

    if ((*data) && data_is_image && !data_is_svg && data_is_base64) {
        GdkPixbufLoader *loader = gdk_pixbuf_loader_new();

        if (!loader) return nullptr;

        gsize decoded_len = 0;
        guchar *decoded = g_base64_decode(data, &decoded_len);

        if (gdk_pixbuf_loader_write(loader, decoded, decoded_len, nullptr)) {
            gdk_pixbuf_loader_close(loader, nullptr);
            GdkPixbuf *buf = gdk_pixbuf_loader_get_pixbuf(loader);
            if (buf) {
                g_object_ref(buf);
                buf = Pixbuf::apply_embedded_orientation(buf);
                pixbuf = new Pixbuf(buf);

                GdkPixbufFormat *fmt = gdk_pixbuf_loader_get_format(loader);
                gchar *fmt_name = gdk_pixbuf_format_get_name(fmt);
                pixbuf->_setMimeData(decoded, decoded_len, fmt_name);
                g_free(fmt_name);
            } else {
                g_free(decoded);
            }
        } else {
            g_free(decoded);
        }
        g_object_unref(loader);
    }
    
    if ((*data) && data_is_image && data_is_svg && data_is_base64) {
        gsize decoded_len = 0;
        guchar *decoded = g_base64_decode(data, &decoded_len);
        std::unique_ptr<SPDocument> svgDoc(
            SPDocument::createNewDocFromMem(reinterpret_cast<gchar const *>(decoded), decoded_len, false));
        // Check the document loaded properly
        if (svgDoc == nullptr) {
            return nullptr;
        }
        if (svgDoc->getRoot() == nullptr)
        {
            return nullptr;
        }
        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
        double dpi = prefs->getDouble("/dialogs/import/defaultxdpi/value", 96.0);
        if (svgdpi && svgdpi > 0) {
            dpi = svgdpi;
        }

        // Get the size of the document
        Inkscape::Util::Quantity svgWidth = svgDoc->getWidth();
        Inkscape::Util::Quantity svgHeight = svgDoc->getHeight();
        const double svgWidth_px = svgWidth.value("px");
        const double svgHeight_px = svgHeight.value("px");
        if (svgWidth_px < 0 || svgHeight_px < 0) {
            g_warning("create_from_data_uri: malformed document: svgWidth_px=%f, svgHeight_px=%f", svgWidth_px,
                      svgHeight_px);
            return nullptr;
        }
        
        assert(!pixbuf);
        Geom::Rect area(0, 0, svgWidth_px, svgHeight_px);
        pixbuf = sp_generate_internal_bitmap(svgDoc.get(), area, dpi);
        GdkPixbuf const *buf = pixbuf->getPixbufRaw();

        // Tidy up
        if (buf == nullptr) {
            std::cerr << "Pixbuf::create_from_data: failed to load contents: " << std::endl;
            delete pixbuf;
            g_free(decoded);
            return nullptr;
        } else {
            pixbuf->_setMimeData(decoded, decoded_len, "svg+xml");
        }
    }

    return pixbuf;
}

Pixbuf *Pixbuf::create_from_file(std::string const &fn, double svgdpi)
{
    Pixbuf *pb = nullptr;
    // test correctness of filename
    if (!g_file_test(fn.c_str(), G_FILE_TEST_EXISTS)) { 
        return nullptr;
    }
    GStatBuf stdir;
    int val = g_stat(fn.c_str(), &stdir);
    if (val == 0 && stdir.st_mode & S_IFDIR){
        return nullptr;
    }
    // we need to load the entire file into memory,
    // since we'll store it as MIME data
    gchar *data = nullptr;
    gsize len = 0;
    GError *error = nullptr;

    if (g_file_get_contents(fn.c_str(), &data, &len, &error)) {

        if (error != nullptr) {
            std::cerr << "Pixbuf::create_from_file: " << error->message << std::endl;
            std::cerr << "   (" << fn << ")" << std::endl;
            return nullptr;
        }

        pb = Pixbuf::create_from_buffer(std::move(data), len, svgdpi, fn);

        if (pb) {
            pb->_mod_time = stdir.st_mtime;
        }
    } else {
        std::cerr << "Pixbuf::create_from_file: failed to get contents: " << fn << std::endl;
        return nullptr;
    }

    return pb;
}

GdkPixbuf *Pixbuf::apply_embedded_orientation(GdkPixbuf *buf)
{
    GdkPixbuf *old = buf;
    buf = gdk_pixbuf_apply_embedded_orientation(buf);
    g_object_unref(old);
    return buf;
}

Pixbuf *Pixbuf::create_from_buffer(std::string const &buffer, double svgdpi, std::string const &fn)
{
#if GLIB_CHECK_VERSION(2,67,3)
    auto datacopy = (gchar *)g_memdup2(buffer.data(), buffer.size());
#else
    auto datacopy = (gchar *)g_memdup(buffer.data(), buffer.size());
#endif
    return Pixbuf::create_from_buffer(std::move(datacopy), buffer.size(), svgdpi, fn);
}

Pixbuf *Pixbuf::create_from_buffer(gchar *&&data, gsize len, double svgdpi, std::string const &fn)
{
    Pixbuf *pb = nullptr;
    GError *error = nullptr;
    {
        GdkPixbuf *buf = nullptr;
        GdkPixbufLoader *loader = nullptr;
        std::string::size_type idx;
        idx = fn.rfind('.');
        bool is_svg = false;    
        if(idx != std::string::npos)
        {
            if (boost::iequals(fn.substr(idx+1).c_str(), "svg")) {

                std::unique_ptr<SPDocument> svgDoc(SPDocument::createNewDocFromMem(data, len, true));

                // Check the document loaded properly
                if (svgDoc == nullptr) {
                    return nullptr;
                }
                if (svgDoc->getRoot() == nullptr)
                {
                    return nullptr;
                }

                Inkscape::Preferences *prefs = Inkscape::Preferences::get();
                double dpi = prefs->getDouble("/dialogs/import/defaultxdpi/value", 96.0);
                if (svgdpi && svgdpi > 0) {
                    dpi = svgdpi;
                }

                // Get the size of the document
                Inkscape::Util::Quantity svgWidth = svgDoc->getWidth();
                Inkscape::Util::Quantity svgHeight = svgDoc->getHeight();
                const double svgWidth_px = svgWidth.value("px");
                const double svgHeight_px = svgHeight.value("px");
                if (svgWidth_px < 0 || svgHeight_px < 0) {
                    g_warning("create_from_buffer: malformed document: svgWidth_px=%f, svgHeight_px=%f", svgWidth_px,
                              svgHeight_px);
                    return nullptr;
                }

                Geom::Rect area(0, 0, svgWidth_px, svgHeight_px);
                pb = sp_generate_internal_bitmap(svgDoc.get(), area, dpi);
                buf = pb->getPixbufRaw();

                // Tidy up
                if (buf == nullptr) {
                    delete pb;
                    return nullptr;
                }
                buf = Pixbuf::apply_embedded_orientation(buf);
                is_svg = true;
            }
        }
        if (!is_svg) {
            loader = gdk_pixbuf_loader_new();
            gdk_pixbuf_loader_write(loader, (guchar *) data, len, &error);
            if (error != nullptr) {
                std::cerr << "Pixbuf::create_from_file: " << error->message << std::endl;
                std::cerr << "   (" << fn << ")" << std::endl;
                g_free(data);
                g_object_unref(loader);
                return nullptr;
            }

            gdk_pixbuf_loader_close(loader, &error);
            if (error != nullptr) {
                std::cerr << "Pixbuf::create_from_file: " << error->message << std::endl;
                std::cerr << "   (" << fn << ")" << std::endl;
                g_free(data);
                g_object_unref(loader);
                return nullptr;
            }
            
            buf = gdk_pixbuf_loader_get_pixbuf(loader);
            if (buf) {
                // gdk_pixbuf_loader_get_pixbuf returns a borrowed reference
                g_object_ref(buf);
                buf = Pixbuf::apply_embedded_orientation(buf);
                pb = new Pixbuf(buf);
            }
        }

        if (pb) {
            pb->_path = fn;
            if (!is_svg) {
                GdkPixbufFormat *fmt = gdk_pixbuf_loader_get_format(loader);
                gchar *fmt_name = gdk_pixbuf_format_get_name(fmt);
                pb->_setMimeData((guchar *) data, len, fmt_name);
                g_free(fmt_name);
                g_object_unref(loader);
            } else {
                pb->_setMimeData((guchar *) data, len, "svg");
            }
        } else {
            std::cerr << "Pixbuf::create_from_file: failed to load contents: " << fn << std::endl;
            g_free(data);
        }

        // TODO: we could also read DPI, ICC profile, gamma correction, and other information
        // from the file. This can be done by using format-specific libraries e.g. libpng.
    }

    return pb;
}

/**
 * Converts the pixbuf to GdkPixbuf pixel format.
 * The returned pixbuf can be used e.g. in calls to gdk_pixbuf_save().
 */
GdkPixbuf *Pixbuf::getPixbufRaw(bool convert_format)
{
    if (convert_format) {
        ensurePixelFormat(PF_GDK);
    }
    return _pixbuf;
}

/**
 * Converts the pixbuf to Cairo pixel format and returns an image surface
 * which can be used as a source.
 *
 * The returned surface is owned by the GdkPixbuf and should not be freed.
 * Calling this function causes the pixbuf to be unsuitable for use
 * with GTK drawing functions until ensurePixelFormat(Pixbuf::PIXEL_FORMAT_PIXBUF) is called.
 */
cairo_surface_t *Pixbuf::getSurfaceRaw(bool convert_format)
{
    if (convert_format) {
        ensurePixelFormat(PF_CAIRO);
    }
    return _surface;
}

/* Declaring this function in the header requires including <gdkmm/pixbuf.h>,
 * which stupidly includes <glibmm.h> which in turn pulls in <glibmm/threads.h>.
 * However, since glib 2.32, <glibmm/threads.h> has to be included before <glib.h>
 * when compiling with G_DISABLE_DEPRECATED, as we do in non-release builds.
 * This necessitates spamming a lot of files with #include <glibmm/threads.h>
 * at the top.
 *
 * Since we don't really use gdkmm, do not define this function for now. */

/*
Glib::RefPtr<Gdk::Pixbuf> Pixbuf::getPixbuf(bool convert_format = true)
{
    g_object_ref(_pixbuf);
    Glib::RefPtr<Gdk::Pixbuf> p(getPixbuf(convert_format));
    return p;
}
*/

Cairo::RefPtr<Cairo::Surface> Pixbuf::getSurface(bool convert_format)
{
    Cairo::RefPtr<Cairo::Surface> p(new Cairo::Surface(getSurfaceRaw(convert_format), false));
    return p;
}

/** Retrieves the original compressed data for the surface, if any.
 * The returned data belongs to the object and should not be freed. */
guchar const *Pixbuf::getMimeData(gsize &len, std::string &mimetype) const
{
    static gchar const *mimetypes[] = {
        CAIRO_MIME_TYPE_JPEG, CAIRO_MIME_TYPE_JP2, CAIRO_MIME_TYPE_PNG, nullptr };
    static guint mimetypes_len = g_strv_length(const_cast<gchar**>(mimetypes));

    guchar const *data = nullptr;

    for (guint i = 0; i < mimetypes_len; ++i) {
        unsigned long len_long = 0;
        cairo_surface_get_mime_data(const_cast<cairo_surface_t*>(_surface), mimetypes[i], &data, &len_long);
        if (data != nullptr) {
			len = len_long;
            mimetype = mimetypes[i];
            break;
        }
    }

    return data;
}

int Pixbuf::width() const {
    return gdk_pixbuf_get_width(const_cast<GdkPixbuf*>(_pixbuf));
}
int Pixbuf::height() const {
    return gdk_pixbuf_get_height(const_cast<GdkPixbuf*>(_pixbuf));
}
int Pixbuf::rowstride() const {
    return gdk_pixbuf_get_rowstride(const_cast<GdkPixbuf*>(_pixbuf));
}
guchar const *Pixbuf::pixels() const {
    return gdk_pixbuf_get_pixels(const_cast<GdkPixbuf*>(_pixbuf));
}
guchar *Pixbuf::pixels() {
    return gdk_pixbuf_get_pixels(_pixbuf);
}
void Pixbuf::markDirty() {
    cairo_surface_mark_dirty(_surface);
}

void Pixbuf::_forceAlpha()
{
    if (gdk_pixbuf_get_has_alpha(_pixbuf)) return;

    GdkPixbuf *old = _pixbuf;
    _pixbuf = gdk_pixbuf_add_alpha(old, FALSE, 0, 0, 0);
    g_object_unref(old);
}

void Pixbuf::_setMimeData(guchar *data, gsize len, Glib::ustring const &format)
{
    gchar const *mimetype = nullptr;

    if (format == "jpeg") {
        mimetype = CAIRO_MIME_TYPE_JPEG;
    } else if (format == "jpeg2000") {
        mimetype = CAIRO_MIME_TYPE_JP2;
    } else if (format == "png") {
        mimetype = CAIRO_MIME_TYPE_PNG;
    }

    if (mimetype != nullptr) {
        cairo_surface_set_mime_data(_surface, mimetype, data, len, g_free, data);
        //g_message("Setting Cairo MIME data: %s", mimetype);
    } else {
        g_free(data);
        //g_message("Not setting Cairo MIME data: unknown format %s", name.c_str());
    }
}

void Pixbuf::ensurePixelFormat(PixelFormat fmt)
{
    if (_pixel_format == PF_GDK) {
        if (fmt == PF_GDK) {
            return;
        }
        if (fmt == PF_CAIRO) {
            convert_pixels_pixbuf_to_argb32(
                gdk_pixbuf_get_pixels(_pixbuf),
                gdk_pixbuf_get_width(_pixbuf),
                gdk_pixbuf_get_height(_pixbuf),
                gdk_pixbuf_get_rowstride(_pixbuf));
            _pixel_format = fmt;
            return;
        }
        g_assert_not_reached();
    }
    if (_pixel_format == PF_CAIRO) {
        if (fmt == PF_GDK) {
            convert_pixels_argb32_to_pixbuf(
                gdk_pixbuf_get_pixels(_pixbuf),
                gdk_pixbuf_get_width(_pixbuf),
                gdk_pixbuf_get_height(_pixbuf),
                gdk_pixbuf_get_rowstride(_pixbuf));
            _pixel_format = fmt;
            return;
        }
        if (fmt == PF_CAIRO) {
            return;
        }
        g_assert_not_reached();
    }
    g_assert_not_reached();
}

} // namespace Inkscape

/*
 * Can be called recursively.
 * If optimize_stroke == false, the view Rect is not used.
 */
static void
feed_curve_to_cairo(cairo_t *cr, Geom::Curve const &c, Geom::Affine const & trans, Geom::Rect view, bool optimize_stroke)
{
    using Geom::X;
    using Geom::Y;

    unsigned order = 0;
    if (Geom::BezierCurve const* b = dynamic_cast<Geom::BezierCurve const*>(&c)) {
        order = b->order();
    }

    // handle the three typical curve cases
    switch (order) {
    case 1:
    {
        Geom::Point end_tr = c.finalPoint() * trans;
        if (!optimize_stroke) {
            cairo_line_to(cr, end_tr[0], end_tr[1]);
        } else {
            Geom::Rect swept(c.initialPoint()*trans, end_tr);
            if (swept.intersects(view)) {
                cairo_line_to(cr, end_tr[0], end_tr[1]);
            } else {
                cairo_move_to(cr, end_tr[0], end_tr[1]);
            }
        }
    }
    break;
    case 2:
    {
        Geom::QuadraticBezier const *quadratic_bezier = static_cast<Geom::QuadraticBezier const*>(&c);
        std::vector<Geom::Point> points = quadratic_bezier->controlPoints();
        points[0] *= trans;
        points[1] *= trans;
        points[2] *= trans;
        // degree-elevate to cubic Bezier, since Cairo doesn't do quadratic Beziers
        Geom::Point b1 = points[0] + (2./3) * (points[1] - points[0]);
        Geom::Point b2 = b1 + (1./3) * (points[2] - points[0]);
        if (!optimize_stroke) {
            cairo_curve_to(cr, b1[X], b1[Y], b2[X], b2[Y], points[2][X], points[2][Y]);
        } else {
            Geom::Rect swept(points[0], points[2]);
            swept.expandTo(points[1]);
            if (swept.intersects(view)) {
                cairo_curve_to(cr, b1[X], b1[Y], b2[X], b2[Y], points[2][X], points[2][Y]);
            } else {
                cairo_move_to(cr, points[2][X], points[2][Y]);
            }
        }
    }
    break;
    case 3:
    {
        Geom::CubicBezier const *cubic_bezier = static_cast<Geom::CubicBezier const*>(&c);
        std::vector<Geom::Point> points = cubic_bezier->controlPoints();
        //points[0] *= trans; // don't do this one here for fun: it is only needed for optimized strokes
        points[1] *= trans;
        points[2] *= trans;
        points[3] *= trans;
        if (!optimize_stroke) {
            cairo_curve_to(cr, points[1][X], points[1][Y], points[2][X], points[2][Y], points[3][X], points[3][Y]);
        } else {
            points[0] *= trans;  // didn't transform this point yet
            Geom::Rect swept(points[0], points[3]);
            swept.expandTo(points[1]);
            swept.expandTo(points[2]);
            if (swept.intersects(view)) {
                cairo_curve_to(cr, points[1][X], points[1][Y], points[2][X], points[2][Y], points[3][X], points[3][Y]);
            } else {
                cairo_move_to(cr, points[3][X], points[3][Y]);
            }
        }
    }
    break;
    default:
    {
        if (Geom::EllipticalArc const *arc = dynamic_cast<Geom::EllipticalArc const*>(&c)) {
            if (arc->isChord()) {
                Geom::Point endPoint(arc->finalPoint());
                cairo_line_to(cr, endPoint[0], endPoint[1]);
            } else {
                Geom::Affine xform = arc->unitCircleTransform() * trans;
                // Don't draw anything if the angle is borked
                if(std::isnan(arc->initialAngle()) || std::isnan(arc->finalAngle())) {
                    g_warning("Bad angle while drawing EllipticalArc");
                    break;
                }

                // Apply the transformation to the current context
                cairo_matrix_t cm;
                cm.xx = xform[0];
                cm.xy = xform[2];
                cm.x0 = xform[4];
                cm.yx = xform[1];
                cm.yy = xform[3];
                cm.y0 = xform[5];

                cairo_save(cr);
                cairo_transform(cr, &cm);

                // Draw the circle
                if (arc->sweep()) {
                    cairo_arc(cr, 0, 0, 1, arc->initialAngle(), arc->finalAngle());
                } else {
                    cairo_arc_negative(cr, 0, 0, 1, arc->initialAngle(), arc->finalAngle());
                }
                // Revert the current context
                cairo_restore(cr);
            }
        } else {
            // handles sbasis as well as all other curve types
            // this is very slow
            Geom::Path sbasis_path = Geom::cubicbezierpath_from_sbasis(c.toSBasis(), 0.1);

            // recurse to convert the new path resulting from the sbasis to svgd
            for (const auto & iter : sbasis_path) {
                feed_curve_to_cairo(cr, iter, trans, view, optimize_stroke);
            }
        }
    }
    break;
    }
}


/** Feeds path-creating calls to the cairo context translating them from the Path */
static void
feed_path_to_cairo (cairo_t *ct, Geom::Path const &path)
{
    if (path.empty())
        return;

    cairo_move_to(ct, path.initialPoint()[0], path.initialPoint()[1] );

    for (Geom::Path::const_iterator cit = path.begin(); cit != path.end_open(); ++cit) {
        feed_curve_to_cairo(ct, *cit, Geom::identity(), Geom::Rect(), false); // optimize_stroke is false, so the view rect is not used
    }

    if (path.closed()) {
        cairo_close_path(ct);
    }
}

/** Feeds path-creating calls to the cairo context translating them from the Path, with the given transform and shift */
static void
feed_path_to_cairo (cairo_t *ct, Geom::Path const &path, Geom::Affine trans, Geom::OptRect area, bool optimize_stroke, double stroke_width)
{
    if (!area)
        return;
    if (path.empty())
        return;

    // Transform all coordinates to coords within "area"
    Geom::Point shift = area->min();
    Geom::Rect view = *area;
    view.expandBy (stroke_width);
    view = view * (Geom::Affine)Geom::Translate(-shift);
    //  Pass transformation to feed_curve, so that we don't need to create a whole new path.
    Geom::Affine transshift(trans * Geom::Translate(-shift));

    Geom::Point initial = path.initialPoint() * transshift;
    cairo_move_to(ct, initial[0], initial[1] );

    for(Geom::Path::const_iterator cit = path.begin(); cit != path.end_open(); ++cit) {
        feed_curve_to_cairo(ct, *cit, transshift, view, optimize_stroke);
    }

    if (path.closed()) {
        if (!optimize_stroke) {
            cairo_close_path(ct);
        } else {
            cairo_line_to(ct, initial[0], initial[1]);
            /* We cannot use cairo_close_path(ct) here because some parts of the path may have been
               clipped and not drawn (maybe the before last segment was outside view area), which 
               would result in closing the "subpath" after the last interruption, not the entire path.

               However, according to cairo documentation:
               The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
               in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
               there is a line join connecting the final and initial segments of the sub-path. 

               The correct fix will be possible when cairo introduces methods for moving without
               ending/starting subpaths, which we will use for skipping invisible segments; then we
               will be able to use cairo_close_path here. This issue also affects ps/eps/pdf export,
               see bug 168129
            */
        }
    }
}

/** Feeds path-creating calls to the cairo context translating them from the PathVector, with the given transform and shift
 *  One must have done cairo_new_path(ct); before calling this function. */
void
feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv, Geom::Affine trans, Geom::OptRect area, bool optimize_stroke, double stroke_width)
{
    if (!area)
        return;
    if (pathv.empty())
        return;

    for(const auto & it : pathv) {
        feed_path_to_cairo(ct, it, trans, area, optimize_stroke, stroke_width);
    }
}

/** Feeds path-creating calls to the cairo context translating them from the PathVector
 *  One must have done cairo_new_path(ct); before calling this function. */
void
feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv)
{
    if (pathv.empty())
        return;

    for(const auto & it : pathv) {
        feed_path_to_cairo(ct, it);
    }
}

SPColorInterpolation
get_cairo_surface_ci(cairo_surface_t *surface) {
    void* data = cairo_surface_get_user_data( surface, &ink_color_interpolation_key );
    if( data != nullptr ) {
        return (SPColorInterpolation)GPOINTER_TO_INT( data );
    } else {
        return SP_CSS_COLOR_INTERPOLATION_AUTO;
    }
}

/** Set the color_interpolation_value for a Cairo surface.
 *  Transform the surface between sRGB and linearRGB if necessary. */
void
set_cairo_surface_ci(cairo_surface_t *surface, SPColorInterpolation ci) {

    if( cairo_surface_get_content( surface ) != CAIRO_CONTENT_ALPHA ) {

        SPColorInterpolation ci_in = get_cairo_surface_ci( surface );

        if( ci_in == SP_CSS_COLOR_INTERPOLATION_SRGB &&
            ci    == SP_CSS_COLOR_INTERPOLATION_LINEARRGB ) {
            ink_cairo_surface_srgb_to_linear( surface );
        }
        if( ci_in == SP_CSS_COLOR_INTERPOLATION_LINEARRGB &&
            ci    == SP_CSS_COLOR_INTERPOLATION_SRGB ) {
            ink_cairo_surface_linear_to_srgb( surface );
        }

        cairo_surface_set_user_data(surface, &ink_color_interpolation_key, GINT_TO_POINTER (ci), nullptr);
    }
}

void
copy_cairo_surface_ci(cairo_surface_t *in, cairo_surface_t *out) {
    cairo_surface_set_user_data(out, &ink_color_interpolation_key, cairo_surface_get_user_data(in, &ink_color_interpolation_key), nullptr);
}

void
ink_cairo_set_source_rgba32(cairo_t *ct, guint32 rgba)
{
    cairo_set_source_rgba(ct, SP_RGBA32_R_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_B_F(rgba), SP_RGBA32_A_F(rgba));
}

void
ink_cairo_set_source_color(cairo_t *ct, SPColor const &c, double opacity)
{
    cairo_set_source_rgba(ct, c.v.c[0], c.v.c[1], c.v.c[2], opacity);
}

void ink_matrix_to_2geom(Geom::Affine &m, cairo_matrix_t const &cm)
{
    m[0] = cm.xx;
    m[2] = cm.xy;
    m[4] = cm.x0;
    m[1] = cm.yx;
    m[3] = cm.yy;
    m[5] = cm.y0;
}

void ink_matrix_to_cairo(cairo_matrix_t &cm, Geom::Affine const &m)
{
    cm.xx = m[0];
    cm.xy = m[2];
    cm.x0 = m[4];
    cm.yx = m[1];
    cm.yy = m[3];
    cm.y0 = m[5];
}

void
ink_cairo_transform(cairo_t *ct, Geom::Affine const &m)
{
    cairo_matrix_t cm;
    ink_matrix_to_cairo(cm, m);
    cairo_transform(ct, &cm);
}

void
ink_cairo_pattern_set_matrix(cairo_pattern_t *cp, Geom::Affine const &m)
{
    cairo_matrix_t cm;
    ink_matrix_to_cairo(cm, m);
    cairo_pattern_set_matrix(cp, &cm);
}

void
ink_cairo_set_hairline(cairo_t *ct)
{
#ifdef CAIRO_HAS_HAIRLINE
    cairo_set_hairline(ct);
#else
    // As a backup, use a device unit of 1
    double x = 1, y = 1;
    cairo_device_to_user_distance(ct, &x, &y);
    cairo_set_line_width(ct, std::min(x, y));
#endif
}

/**
 * Create an exact copy of a surface.
 * Creates a surface that has the same type, content type, dimensions and contents
 * as the specified surface.
 */
cairo_surface_t *
ink_cairo_surface_copy(cairo_surface_t *s)
{
    cairo_surface_t *ns = ink_cairo_surface_create_identical(s);

    if (cairo_surface_get_type(s) == CAIRO_SURFACE_TYPE_IMAGE) {
        // use memory copy instead of using a Cairo context
        cairo_surface_flush(s);
        int stride = cairo_image_surface_get_stride(s);
        int h = cairo_image_surface_get_height(s);
        memcpy(cairo_image_surface_get_data(ns), cairo_image_surface_get_data(s), stride * h);
        cairo_surface_mark_dirty(ns);
    } else {
        // generic implementation
        cairo_t *ct = cairo_create(ns);
        cairo_set_source_surface(ct, s, 0, 0);
        cairo_set_operator(ct, CAIRO_OPERATOR_SOURCE);
        cairo_paint(ct);
        cairo_destroy(ct);
    }

    return ns;
}

/**
 * Create an exact copy of an image surface.
 */
Cairo::RefPtr<Cairo::ImageSurface>
ink_cairo_surface_copy(Cairo::RefPtr<Cairo::ImageSurface> surface )
{
    int width  = surface->get_width();
    int height = surface->get_height();
    int stride = surface->get_stride();
    auto new_surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, width, height); // device scale?

    surface->flush();
    memcpy(new_surface->get_data(), surface->get_data(), stride * height);
    new_surface->mark_dirty(); // Clear caches. Mandatory after messing directly with contents.

    return new_surface;
}

/**
 * Create a surface that differs only in pixel content.
 * Creates a surface that has the same type, content type and dimensions
 * as the specified surface. Pixel contents are not copied.
 */
cairo_surface_t *
ink_cairo_surface_create_identical(cairo_surface_t *s)
{
    cairo_surface_t *ns = ink_cairo_surface_create_same_size(s, cairo_surface_get_content(s));
    cairo_surface_set_user_data(ns, &ink_color_interpolation_key, cairo_surface_get_user_data(s, &ink_color_interpolation_key), nullptr);
    return ns;
}

cairo_surface_t *
ink_cairo_surface_create_same_size(cairo_surface_t *s, cairo_content_t c)
{
    // ink_cairo_surface_get_width()/height() returns value in pixels
    // cairo_surface_create_similar() uses device units
    double x_scale = 0;
    double y_scale = 0;
    cairo_surface_get_device_scale( s, &x_scale, &y_scale );

    assert (x_scale > 0);
    assert (y_scale > 0);

    cairo_surface_t *ns =
        cairo_surface_create_similar(s, c,
                                     ink_cairo_surface_get_width(s)/x_scale,
                                     ink_cairo_surface_get_height(s)/y_scale);
    return ns;
}

/**
 * Extract the alpha channel into a new surface.
 * Creates a surface with a content type of CAIRO_CONTENT_ALPHA that contains
 * the alpha values of pixels from @a s.
 */
cairo_surface_t *
ink_cairo_extract_alpha(cairo_surface_t *s)
{
    cairo_surface_t *alpha = ink_cairo_surface_create_same_size(s, CAIRO_CONTENT_ALPHA);

    cairo_t *ct = cairo_create(alpha);
    cairo_set_source_surface(ct, s, 0, 0);
    cairo_set_operator(ct, CAIRO_OPERATOR_SOURCE);
    cairo_paint(ct);
    cairo_destroy(ct);

    return alpha;
}

cairo_surface_t *
ink_cairo_surface_create_output(cairo_surface_t *image, cairo_surface_t *bg)
{
    cairo_content_t imgt = cairo_surface_get_content(image);
    cairo_content_t bgt = cairo_surface_get_content(bg);
    cairo_surface_t *out = nullptr;

    if (bgt == CAIRO_CONTENT_ALPHA && imgt == CAIRO_CONTENT_ALPHA) {
        out = ink_cairo_surface_create_identical(bg);
    } else {
        out = ink_cairo_surface_create_same_size(bg, CAIRO_CONTENT_COLOR_ALPHA);
    }

    return out;
}

void
ink_cairo_surface_blit(cairo_surface_t *src, cairo_surface_t *dest)
{
    if (cairo_surface_get_type(src) == CAIRO_SURFACE_TYPE_IMAGE &&
        cairo_surface_get_type(dest) == CAIRO_SURFACE_TYPE_IMAGE &&
        cairo_image_surface_get_format(src) == cairo_image_surface_get_format(dest) &&
        cairo_image_surface_get_height(src) == cairo_image_surface_get_height(dest) &&
        cairo_image_surface_get_width(src) == cairo_image_surface_get_width(dest) &&
        cairo_image_surface_get_stride(src) == cairo_image_surface_get_stride(dest))
    {
        // use memory copy instead of using a Cairo context
        cairo_surface_flush(src);
        int stride = cairo_image_surface_get_stride(src);
        int h = cairo_image_surface_get_height(src);
        memcpy(cairo_image_surface_get_data(dest), cairo_image_surface_get_data(src), stride * h);
        cairo_surface_mark_dirty(dest);
    } else {
        // generic implementation
        cairo_t *ct = cairo_create(dest);
        cairo_set_source_surface(ct, src, 0, 0);
        cairo_set_operator(ct, CAIRO_OPERATOR_SOURCE);
        cairo_paint(ct);
        cairo_destroy(ct);
    }
}

/**
 * Return width in pixels.
 */
int
ink_cairo_surface_get_width(cairo_surface_t *surface)
{
    // For now only image surface is handled.
    // Later add others, e.g. cairo-gl
    assert(cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_IMAGE);
    return cairo_image_surface_get_width(surface);
}

/**
 * Return height in pixels.
 */
int
ink_cairo_surface_get_height(cairo_surface_t *surface)
{
    assert(cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_IMAGE);
    return cairo_image_surface_get_height(surface);
}

static int ink_cairo_surface_average_color_internal(cairo_surface_t *surface, double &rf, double &gf, double &bf, double &af)
{
    rf = gf = bf = af = 0.0;
    cairo_surface_flush(surface);
    int width = cairo_image_surface_get_width(surface);
    int height = cairo_image_surface_get_height(surface);
    int stride = cairo_image_surface_get_stride(surface);
    unsigned char *data = cairo_image_surface_get_data(surface);

    /* TODO convert this to OpenMP somehow */
    for (int y = 0; y < height; ++y, data += stride) {
        for (int x = 0; x < width; ++x) {
            guint32 px = *reinterpret_cast<guint32*>(data + 4*x);
            EXTRACT_ARGB32(px, a,r,g,b)
            rf += r / 255.0;
            gf += g / 255.0;
            bf += b / 255.0;
            af += a / 255.0;
        }
    }
    return width * height;
}

guint32 ink_cairo_surface_average_color(cairo_surface_t *surface)
{
    double rf,gf,bf,af;
    ink_cairo_surface_average_color_premul(surface, rf,gf,bf,af);
    guint32 r = round(rf * 255);
    guint32 g = round(gf * 255);
    guint32 b = round(bf * 255);
    guint32 a = round(af * 255);
    ASSEMBLE_ARGB32(px, a,r,g,b);
    return px;
}
// We extract colors from pattern background, if we need to extract sometimes from a gradient we can add
// a extra parameter with the spot number and use cairo_pattern_get_color_stop_rgba
// also if the pattern is a image we can pass a boolean like solid = false to get the color by image average ink_cairo_surface_average_color
guint32 ink_cairo_pattern_get_argb32(cairo_pattern_t *pattern)
{
    double red = 0;
    double green = 0;
    double blue = 0;
    double alpha = 0;
    auto status = cairo_pattern_get_rgba(pattern, &red, &green, &blue, &alpha);
    if (status != CAIRO_STATUS_PATTERN_TYPE_MISMATCH) {
        // in ARGB32 format
        return SP_RGBA32_F_COMPOSE(alpha, red, green, blue);
    }
        
    cairo_surface_t *surface;
    status = cairo_pattern_get_surface (pattern, &surface);
    if (status != CAIRO_STATUS_PATTERN_TYPE_MISMATCH) {
        // first pixel only
        auto *pxbsurface =  cairo_image_surface_get_data(surface);
        return *reinterpret_cast<guint32 const *>(pxbsurface);
    }
    return 0;
}

void ink_cairo_surface_average_color(cairo_surface_t *surface, double &r, double &g, double &b, double &a)
{
    int count = ink_cairo_surface_average_color_internal(surface, r,g,b,a);

    r /= a;
    g /= a;
    b /= a;
    a /= count;

    r = CLAMP(r, 0.0, 1.0);
    g = CLAMP(g, 0.0, 1.0);
    b = CLAMP(b, 0.0, 1.0);
    a = CLAMP(a, 0.0, 1.0);
}

void ink_cairo_surface_average_color_premul(cairo_surface_t *surface, double &r, double &g, double &b, double &a)
{
    int count = ink_cairo_surface_average_color_internal(surface, r,g,b,a);

    r /= count;
    g /= count;
    b /= count;
    a /= count;

    r = CLAMP(r, 0.0, 1.0);
    g = CLAMP(g, 0.0, 1.0);
    b = CLAMP(b, 0.0, 1.0);
    a = CLAMP(a, 0.0, 1.0);
}

static guint32 srgb_to_linear( const guint32 c, const guint32 a ) {

    const guint32 c1 = unpremul_alpha( c, a );

    double cc = c1/255.0;

    if( cc < 0.04045 ) {
        cc /= 12.92;
    } else {
        cc = pow( (cc+0.055)/1.055, 2.4 );
    }
    cc *= 255.0;

    const guint32 c2 = (int)cc;

    return premul_alpha( c2, a );
}

static guint32 linear_to_srgb( const guint32 c, const guint32 a ) {

    const guint32 c1 = unpremul_alpha( c, a );

    double cc = c1/255.0;

    if( cc < 0.0031308 ) {
        cc *= 12.92;
    } else {
        cc = pow( cc, 1.0/2.4 )*1.055-0.055;
    }
    cc *= 255.0;

    const guint32 c2 = (int)cc;

    return premul_alpha( c2, a );
}

struct SurfaceSrgbToLinear {

    guint32 operator()(guint32 in) {
        EXTRACT_ARGB32(in, a,r,g,b)    ; // Unneeded semi-colon for indenting
        if( a != 0 ) {
            r = srgb_to_linear( r, a );
            g = srgb_to_linear( g, a );
            b = srgb_to_linear( b, a );
        }
        ASSEMBLE_ARGB32(out, a,r,g,b);
        return out;
    }
private:
    /* None */
};

int ink_cairo_surface_srgb_to_linear(cairo_surface_t *surface)
{
    cairo_surface_flush(surface);
    int width = cairo_image_surface_get_width(surface);
    int height = cairo_image_surface_get_height(surface);
    // int stride = cairo_image_surface_get_stride(surface);
    // unsigned char *data = cairo_image_surface_get_data(surface);

    ink_cairo_surface_filter( surface, surface, SurfaceSrgbToLinear() );

    /* TODO convert this to OpenMP somehow */
    // for (int y = 0; y < height; ++y, data += stride) {
    //     for (int x = 0; x < width; ++x) {
    //         guint32 px = *reinterpret_cast<guint32*>(data + 4*x);
    //         EXTRACT_ARGB32(px, a,r,g,b)    ; // Unneeded semi-colon for indenting
    //         if( a != 0 ) {
    //             r = srgb_to_linear( r, a );
    //             g = srgb_to_linear( g, a );
    //             b = srgb_to_linear( b, a );
    //         }
    //         ASSEMBLE_ARGB32(px2, a,r,g,b);
    //         *reinterpret_cast<guint32*>(data + 4*x) = px2;
    //     }
    // }
    return width * height;
}

struct SurfaceLinearToSrgb {

    guint32 operator()(guint32 in) {
        EXTRACT_ARGB32(in, a,r,g,b)    ; // Unneeded semi-colon for indenting
        if( a != 0 ) {
            r = linear_to_srgb( r, a );
            g = linear_to_srgb( g, a );
            b = linear_to_srgb( b, a );
        }
        ASSEMBLE_ARGB32(out, a,r,g,b);
        return out;
    }
private:
    /* None */
};

SPBlendMode ink_cairo_operator_to_css_blend(cairo_operator_t cairo_operator)
{
    // All of the blend modes are implemented in Cairo as of 1.10.
    // For a detailed description, see:
    // http://cairographics.org/operators/
    auto res = SP_CSS_BLEND_NORMAL;
    switch (cairo_operator) {
        case CAIRO_OPERATOR_MULTIPLY:
            res = SP_CSS_BLEND_MULTIPLY;
            break;
        case CAIRO_OPERATOR_SCREEN:
            res = SP_CSS_BLEND_SCREEN;
            break;
        case CAIRO_OPERATOR_DARKEN:
            res = SP_CSS_BLEND_DARKEN;
            break;
        case CAIRO_OPERATOR_LIGHTEN:
            res = SP_CSS_BLEND_LIGHTEN;
            break;
        case CAIRO_OPERATOR_OVERLAY:
            res = SP_CSS_BLEND_OVERLAY;
            break;
        case CAIRO_OPERATOR_COLOR_DODGE:
            res = SP_CSS_BLEND_COLORDODGE;
            break;
        case CAIRO_OPERATOR_COLOR_BURN:
            res = SP_CSS_BLEND_COLORBURN;
            break;
        case CAIRO_OPERATOR_HARD_LIGHT:
            res = SP_CSS_BLEND_HARDLIGHT;
            break;
        case CAIRO_OPERATOR_SOFT_LIGHT:
            res = SP_CSS_BLEND_SOFTLIGHT;
            break;
        case CAIRO_OPERATOR_DIFFERENCE:
            res = SP_CSS_BLEND_DIFFERENCE;
            break;
        case CAIRO_OPERATOR_EXCLUSION:
            res = SP_CSS_BLEND_EXCLUSION;
            break;
        case CAIRO_OPERATOR_HSL_HUE:
            res = SP_CSS_BLEND_HUE;
            break;
        case CAIRO_OPERATOR_HSL_SATURATION:
            res = SP_CSS_BLEND_SATURATION;
            break;
        case CAIRO_OPERATOR_HSL_COLOR:
            res = SP_CSS_BLEND_COLOR;
            break;
        case CAIRO_OPERATOR_HSL_LUMINOSITY:
            res = SP_CSS_BLEND_LUMINOSITY;
            break;
        case CAIRO_OPERATOR_OVER:
        default:
            res = SP_CSS_BLEND_NORMAL;
            break;
    }
    return res;
}

cairo_operator_t ink_css_blend_to_cairo_operator(SPBlendMode css_blend)
{
    // All of the blend modes are implemented in Cairo as of 1.10.
    // For a detailed description, see:
    // http://cairographics.org/operators/

    cairo_operator_t res = CAIRO_OPERATOR_OVER;
    switch (css_blend) {
        case SP_CSS_BLEND_MULTIPLY:
            res = CAIRO_OPERATOR_MULTIPLY;
            break;
        case SP_CSS_BLEND_SCREEN:
            res = CAIRO_OPERATOR_SCREEN;
            break;
        case SP_CSS_BLEND_DARKEN:
            res = CAIRO_OPERATOR_DARKEN;
            break;
        case SP_CSS_BLEND_LIGHTEN:
            res = CAIRO_OPERATOR_LIGHTEN;
            break;
        case SP_CSS_BLEND_OVERLAY:
            res = CAIRO_OPERATOR_OVERLAY;
            break;
        case SP_CSS_BLEND_COLORDODGE:
            res = CAIRO_OPERATOR_COLOR_DODGE;
            break;
        case SP_CSS_BLEND_COLORBURN:
            res = CAIRO_OPERATOR_COLOR_BURN;
            break;
        case SP_CSS_BLEND_HARDLIGHT:
            res = CAIRO_OPERATOR_HARD_LIGHT;
            break;
        case SP_CSS_BLEND_SOFTLIGHT:
            res = CAIRO_OPERATOR_SOFT_LIGHT;
            break;
        case SP_CSS_BLEND_DIFFERENCE:
            res = CAIRO_OPERATOR_DIFFERENCE;
            break;
        case SP_CSS_BLEND_EXCLUSION:
            res = CAIRO_OPERATOR_EXCLUSION;
            break;
        case SP_CSS_BLEND_HUE:
            res = CAIRO_OPERATOR_HSL_HUE;
            break;
        case SP_CSS_BLEND_SATURATION:
            res = CAIRO_OPERATOR_HSL_SATURATION;
            break;
        case SP_CSS_BLEND_COLOR:
            res = CAIRO_OPERATOR_HSL_COLOR;
            break;
        case SP_CSS_BLEND_LUMINOSITY:
            res = CAIRO_OPERATOR_HSL_LUMINOSITY;
            break;
        case SP_CSS_BLEND_NORMAL:
            res = CAIRO_OPERATOR_OVER;
            break;
        default:
            g_error("Invalid SPBlendMode %d", css_blend);
    }
    return res;
}



int ink_cairo_surface_linear_to_srgb(cairo_surface_t *surface)
{
    cairo_surface_flush(surface);
    int width = cairo_image_surface_get_width(surface);
    int height = cairo_image_surface_get_height(surface);
    // int stride = cairo_image_surface_get_stride(surface);
    // unsigned char *data = cairo_image_surface_get_data(surface);

    ink_cairo_surface_filter( surface, surface, SurfaceLinearToSrgb() );

    // /* TODO convert this to OpenMP somehow */
    // for (int y = 0; y < height; ++y, data += stride) {
    //     for (int x = 0; x < width; ++x) {
    //         guint32 px = *reinterpret_cast<guint32*>(data + 4*x);
    //         EXTRACT_ARGB32(px, a,r,g,b)    ; // Unneeded semi-colon for indenting
    //         if( a != 0 ) {
    //             r = linear_to_srgb( r, a );
    //             g = linear_to_srgb( g, a );
    //             b = linear_to_srgb( b, a );
    //         }
    //         ASSEMBLE_ARGB32(px2, a,r,g,b);
    //         *reinterpret_cast<guint32*>(data + 4*x) = px2;
    //     }
    // }
    return width * height;
}

cairo_pattern_t *
ink_cairo_pattern_create_checkerboard(guint32 rgba, bool use_alpha)
{
    int const w = 6;
    int const h = 6;

    double r = SP_RGBA32_R_F(rgba);
    double g = SP_RGBA32_G_F(rgba);
    double b = SP_RGBA32_B_F(rgba);

    float hsl[3];
    SPColor::rgb_to_hsl_floatv(hsl, r, g, b);    
    hsl[2] += hsl[2] < 0.08 ? 0.08 : -0.08; // 0.08 = 0.77-0.69, the original checkerboard colors.

    float rgb2[3];
    SPColor::hsl_to_rgb_floatv(rgb2, hsl[0], hsl[1], hsl[2]);

    cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 2*w, 2*h);

    cairo_t *ct = cairo_create(s);
    cairo_set_operator(ct, CAIRO_OPERATOR_SOURCE);
    cairo_set_source_rgb(ct, r, g, b);
    cairo_paint(ct);
    cairo_set_source_rgb(ct, rgb2[0], rgb2[1], rgb2[2]);
    cairo_rectangle(ct, 0, 0, w, h);
    cairo_rectangle(ct, w, h, w, h);
    cairo_fill(ct);
    if (use_alpha) {
        // use alpha to show opacity cover checkerboard
        double a = SP_RGBA32_A_F(rgba);
        if (a > 0.0) {
            cairo_set_operator(ct, CAIRO_OPERATOR_OVER);
            cairo_rectangle(ct, 0, 0, 2 * w, 2 * h);
            cairo_set_source_rgba(ct, r, g, b, a);
            cairo_fill(ct);
        }
    }
    cairo_destroy(ct);

    cairo_pattern_t *p = cairo_pattern_create_for_surface(s);
    cairo_pattern_set_extend(p, CAIRO_EXTEND_REPEAT);
    cairo_pattern_set_filter(p, CAIRO_FILTER_NEAREST);

    cairo_surface_destroy(s);
    return p;
}


/** 
 * Draw drop shadow around the 'rect' with given 'size' and 'color'; shadow extends to the right and bottom of rect.
 */
void ink_cairo_draw_drop_shadow(Cairo::RefPtr<Cairo::Context> ctx, const Geom::Rect& rect, double size, guint32 color, double color_alpha) {
    // draw fake drop shadow built from gradients
    const auto r = SP_RGBA32_R_F(color);
    const auto g = SP_RGBA32_G_F(color);
    const auto b = SP_RGBA32_B_F(color);
    const auto a = color_alpha;
    const Geom::Point corners[] = { rect.corner(0), rect.corner(1), rect.corner(2), rect.corner(3) };
    // space for gradient shadow
    double sw = size;
    double half = sw / 2;
    using Geom::X;
    using Geom::Y;
    // 8 gradients total: 4 sides + 4 corners
    auto grad_top    = Cairo::LinearGradient::create(0, corners[0][Y] + half, 0, corners[0][Y] - half);
    auto grad_right  = Cairo::LinearGradient::create(corners[1][X], 0, corners[1][X] + sw, 0);
    auto grad_bottom = Cairo::LinearGradient::create(0, corners[2][Y], 0, corners[2][Y] + sw);
    auto grad_left   = Cairo::LinearGradient::create(corners[0][X] + half, 0, corners[0][X] - half, 0);
    auto grad_btm_right = Cairo::RadialGradient::create(corners[2][X], corners[2][Y], 0, corners[2][X], corners[2][Y], sw);
    auto grad_top_right = Cairo::RadialGradient::create(corners[1][X], corners[1][Y] + half, 0, corners[1][X], corners[1][Y] + half, sw);
    auto grad_btm_left  = Cairo::RadialGradient::create(corners[3][X] + half, corners[3][Y], 0, corners[3][X] + half, corners[3][Y], sw);
    auto grad_top_left  = Cairo::RadialGradient::create(corners[0][X], corners[0][Y], 0, corners[0][X], corners[0][Y], half);
    const int N = 15; // number of gradient stops; stops used to make it non-linear
    // using easing function here: (exp(a*(1-t)) - 1) / (exp(a) - 1);
    // it has a nice property of growing from 0 to 1 for t in [0..1]
    const auto A = 4.0; // this coefficient changes how steep the curve is and controls shadow drop-off
    const auto denominator = exp(A) - 1;
    for (int i = 0; i <= N; ++i) {
        auto pos = static_cast<double>(i) / N;
        // exponential decay for drop shadow - long tail, with values from 100% down to 0% opacity
        auto t = 1 - pos; // reverse 't' so alpha drops from 1 to 0
        auto alpha = (exp(A * t) - 1) / denominator;
        grad_top->add_color_stop_rgba(pos, r, g, b, alpha * a);
        grad_bottom->add_color_stop_rgba(pos, r, g, b, alpha * a);
        grad_right->add_color_stop_rgba(pos, r, g, b, alpha * a);
        grad_left->add_color_stop_rgba(pos, r, g, b, alpha * a);
        grad_btm_right->add_color_stop_rgba(pos, r, g, b, alpha * a);
        grad_top_right->add_color_stop_rgba(pos, r, g, b, alpha * a);
        grad_btm_left->add_color_stop_rgba(pos, r, g, b, alpha * a);
        // this left/top corner is just a silver of the shadow: half of it is "hidden" beneath the page
        if (pos >= 0.5) {
            grad_top_left->add_color_stop_rgba(2 * (pos - 0.5), r, g, b, alpha * a);
        }
    }

    // shadow at the top (faint)
    ctx->rectangle(corners[0][X], corners[0][Y] - half, std::max(corners[1][X] - corners[0][X], 0.0), half);
    ctx->set_source(grad_top);
    ctx->fill();

    // right side
    ctx->rectangle(corners[1][X], corners[1][Y] + half, sw, std::max(corners[2][Y] - corners[1][Y] - half, 0.0));
    ctx->set_source(grad_right);
    ctx->fill();

    // bottom side
    ctx->rectangle(corners[0][X] + half, corners[2][Y], std::max(corners[1][X] - corners[0][X] - half, 0.0), sw);
    ctx->set_source(grad_bottom);
    ctx->fill();

    // left side (faint)
    ctx->rectangle(corners[0][X] - half, corners[0][Y], half, std::max(corners[2][Y] - corners[1][Y], 0.0));
    ctx->set_source(grad_left);
    ctx->fill();

    // bottom corners
    ctx->rectangle(corners[2][X], corners[2][Y], sw, sw);
    ctx->set_source(grad_btm_right);
    ctx->fill();

    ctx->rectangle(corners[3][X] - half, corners[3][Y], std::min(sw, rect.width() + half), sw);
    ctx->set_source(grad_btm_left);
    ctx->fill();

    // top corners
    ctx->rectangle(corners[1][X], corners[1][Y] - half, sw, std::min(sw, rect.height() + half));
    ctx->set_source(grad_top_right);
    ctx->fill();

    ctx->rectangle(corners[0][X] - half, corners[0][Y] - half, half, half);
    ctx->set_source(grad_top_left);
    ctx->fill();
}

/**
 * Converts the Cairo surface to a GdkPixbuf pixel format,
 * without allocating extra memory.
 *
 * This function is intended mainly for creating previews displayed by GTK.
 * For loading images for display on the canvas, use the Inkscape::Pixbuf object.
 *
 * The returned GdkPixbuf takes ownership of the passed surface reference,
 * so it should NOT be freed after calling this function.
 */
GdkPixbuf *ink_pixbuf_create_from_cairo_surface(cairo_surface_t *s)
{
    guchar *pixels = cairo_image_surface_get_data(s);
    int w = cairo_image_surface_get_width(s);
    int h = cairo_image_surface_get_height(s);
    int rs = cairo_image_surface_get_stride(s);

    convert_pixels_argb32_to_pixbuf(pixels, w, h, rs);

    GdkPixbuf *pb = gdk_pixbuf_new_from_data(
        pixels, GDK_COLORSPACE_RGB, TRUE, 8,
        w, h, rs, ink_cairo_pixbuf_cleanup, s);

    return pb;
}

/**
 * Cleanup function for GdkPixbuf.
 * This function should be passed as the GdkPixbufDestroyNotify parameter
 * to gdk_pixbuf_new_from_data when creating a GdkPixbuf backed by
 * a Cairo surface.
 */
void ink_cairo_pixbuf_cleanup(guchar * /*pixels*/, void *data)
{
    cairo_surface_t *surface = static_cast<cairo_surface_t*>(data);
    cairo_surface_destroy(surface);
}

/* The following two functions use "from" instead of "to", because when you write:
   val1 = argb32_from_pixbuf(val1);
   the name of the format is closer to the value in that format. */

guint32 argb32_from_pixbuf(guint32 c)
{
    guint32 o = 0;
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
    guint32 a = (c & 0xff000000) >> 24;
#else
    guint32 a = (c & 0x000000ff);
#endif
    if (a != 0) {
        // extract color components
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
        guint32 r = (c & 0x000000ff);
        guint32 g = (c & 0x0000ff00) >> 8;
        guint32 b = (c & 0x00ff0000) >> 16;
#else
        guint32 r = (c & 0xff000000) >> 24;
        guint32 g = (c & 0x00ff0000) >> 16;
        guint32 b = (c & 0x0000ff00) >> 8;
#endif
        // premultiply
        r = premul_alpha(r, a);
        b = premul_alpha(b, a);
        g = premul_alpha(g, a);
        // combine into output
        o = (a << 24) | (r << 16) | (g << 8) | (b);
    }
    return o;
}

/**
 * Convert one pixel from ARGB to GdkPixbuf format.
 *
 * @param c ARGB color
 * @param bgcolor Color to use if c.alpha is zero (bgcolor.alpha is ignored)
 */
guint32 pixbuf_from_argb32(guint32 c, guint32 bgcolor)
{
    guint32 a = (c & 0xff000000) >> 24;
    if (a == 0) {
        assert(c == 0);
        c = bgcolor;
    }

    // extract color components
    guint32 r = (c & 0x00ff0000) >> 16;
    guint32 g = (c & 0x0000ff00) >> 8;
    guint32 b = (c & 0x000000ff);

    if (a != 0) {
        r = unpremul_alpha(r, a);
        g = unpremul_alpha(g, a);
        b = unpremul_alpha(b, a);
    }

    // combine into output
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
    guint32 o = (r) | (g << 8) | (b << 16) | (a << 24);
#else
    guint32 o = (r << 24) | (g << 16) | (b << 8) | (a);
#endif
    return o;
}

/**
 * Convert pixel data from GdkPixbuf format to ARGB.
 * This will convert pixel data from GdkPixbuf format to Cairo's native pixel format.
 * This involves premultiplying alpha and shuffling around the channels.
 * Pixbuf data must have an alpha channel, otherwise the results are undefined
 * (usually a segfault).
 */
void
convert_pixels_pixbuf_to_argb32(guchar *data, int w, int h, int stride)
{
    if (!data || w < 1 || h < 1 || stride < 1) {
        return;
    }

    for (size_t i = 0; i < h; ++i) {
        guint32 *px = reinterpret_cast<guint32*>(data + i*stride);
        for (size_t j = 0; j < w; ++j) {
            *px = argb32_from_pixbuf(*px);
            ++px;
        }
    }
}

/**
 * Convert pixel data from ARGB to GdkPixbuf format.
 * This will convert pixel data from GdkPixbuf format to Cairo's native pixel format.
 * This involves premultiplying alpha and shuffling around the channels.
 */
void
convert_pixels_argb32_to_pixbuf(guchar *data, int w, int h, int stride, guint32 bgcolor)
{
    if (!data || w < 1 || h < 1 || stride < 1) {
        return;
    }
    for (size_t i = 0; i < h; ++i) {
        guint32 *px = reinterpret_cast<guint32*>(data + i*stride);
        for (size_t j = 0; j < w; ++j) {
            *px = pixbuf_from_argb32(*px, bgcolor);
            ++px;
        }
    }
}

/**
 * Converts GdkPixbuf's data to premultiplied ARGB.
 * This function will convert a GdkPixbuf in place into Cairo's native pixel format.
 * Note that this is a hack intended to save memory. When the pixbuf is in Cairo's format,
 * using it with GTK will result in corrupted drawings.
 */
void
ink_pixbuf_ensure_argb32(GdkPixbuf *pb)
{
    gchar *pixel_format = reinterpret_cast<gchar*>(g_object_get_data(G_OBJECT(pb), "pixel_format"));
    if (pixel_format != nullptr && strcmp(pixel_format, "argb32") == 0) {
        // nothing to do
        return;
    }

    convert_pixels_pixbuf_to_argb32(
        gdk_pixbuf_get_pixels(pb),
        gdk_pixbuf_get_width(pb),
        gdk_pixbuf_get_height(pb),
        gdk_pixbuf_get_rowstride(pb));
    g_object_set_data_full(G_OBJECT(pb), "pixel_format", g_strdup("argb32"), g_free);
}

/**
 * Converts GdkPixbuf's data back to its native format.
 * Once this is done, the pixbuf can be used with GTK again.
 */
void
ink_pixbuf_ensure_normal(GdkPixbuf *pb)
{
    gchar *pixel_format = reinterpret_cast<gchar*>(g_object_get_data(G_OBJECT(pb), "pixel_format"));
    if (pixel_format == nullptr || strcmp(pixel_format, "pixbuf") == 0) {
        // nothing to do
        return;
    }

    convert_pixels_argb32_to_pixbuf(
        gdk_pixbuf_get_pixels(pb),
        gdk_pixbuf_get_width(pb),
        gdk_pixbuf_get_height(pb),
        gdk_pixbuf_get_rowstride(pb));
    g_object_set_data_full(G_OBJECT(pb), "pixel_format", g_strdup("pixbuf"), g_free);
}

guint32 argb32_from_rgba(guint32 in)
{
    guint32 r, g, b, a;
    a = (in & 0x000000ff);
    r = premul_alpha((in & 0xff000000) >> 24, a);
    g = premul_alpha((in & 0x00ff0000) >> 16, a);
    b = premul_alpha((in & 0x0000ff00) >> 8,  a);
    ASSEMBLE_ARGB32(px, a, r, g, b)
    return px;
}


/**
 * Converts a pixbuf to a PNG data structure.
 * For 8-but RGBA png, this is like copying.
 *
 */
const guchar* pixbuf_to_png(guchar const**rows, guchar* px, int num_rows, int num_cols, int stride, int color_type, int bit_depth)
{
    int n_fields = 1 + (color_type&2) + (color_type&4)/4;
    const guchar* new_data = (const guchar*)malloc(((n_fields * bit_depth * num_cols + 7)/8) * num_rows);
    char* ptr = (char*) new_data;
    // Used when we write image data smaller than one byte (for instance in
    // black and white images where 1px = 1bit). Only possible with greyscale.
    int pad = 0;
    for (int row = 0; row < num_rows; ++row) {
        rows[row] = (const guchar*)ptr;
        for (int col = 0; col < num_cols; ++col) {
            guint32 *pixel = reinterpret_cast<guint32*>(px + row*stride)+col;

            guint64 pix3 = (*pixel & 0xff000000) >> 24;
            guint64 pix2 = (*pixel & 0x00ff0000) >> 16;
            guint64 pix1 = (*pixel & 0x0000ff00) >> 8;
            guint64 pix0 = (*pixel & 0x000000ff);

#if G_BYTE_ORDER == G_LITTLE_ENDIAN
            guint64 a = pix3, b = pix2, g = pix1, r = pix0;
#else
            guint64 r = pix3, g = pix2, b = pix1, a = pix0;
#endif

            // One of possible rgb to greyscale formulas. This one is called "luminance", "luminosity" or "luma" 
            guint16 gray = (guint16)((guint32)((0.2126*(r<<24) + 0.7152*(g<<24) + 0.0722*(b<<24)))>>16); 
            
            if (color_type & 2) { // RGB or RGBA
                // for 8bit->16bit transition, I take the FF -> FFFF convention (multiplication by 0x101). 
                // If you prefer FF -> FF00 (multiplication by 0x100), remove the <<8, <<24, <<40 and <<56
                // for little-endian, and remove the <<0, <<16, <<32 and <<48 for big-endian.
                if (color_type & 4) { // RGBA
                    if (bit_depth == 8)
                        *((guint32*)ptr) = *pixel; 
                    else 
                        // This uses the samples in the order they appear in pixel rather than
                        // normalised to abgr or rgba in order to make it endian agnostic,
                        // exploiting the symmetry of the expression (0x101 is the same in both
                        // endiannesses and each sample is multiplied by that).
                        *((guint64*)ptr) = (guint64)((pix3<<56)+(pix3<<48)+(pix2<<40)+(pix2<<32)+(pix1<<24)+(pix1<<16)+(pix0<<8)+(pix0));
                } else { // RGB
                    if (bit_depth == 8) {
                        *ptr = r;
                        *(ptr+1) = g;
                        *(ptr+2) = b;
                    } else {
                        *((guint16*)ptr) = (r<<8)+r;
                        *((guint16*)(ptr+2)) = (g<<8)+g;
                        *((guint16*)(ptr+4)) = (b<<8)+b;
                    }
                }
            } else { // Grayscale
                if (bit_depth == 16) {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
                    *(guint16*)ptr = ((gray & 0xff00)>>8) + ((gray & 0x00ff)<<8);
#else
                    *(guint16*)ptr = gray;
#endif
                    // For 8bit->16bit this mirrors RGB(A), multiplying by
                    // 0x101; if you prefer multiplying by 0x100, remove the
                    // <<8 for little-endian, and remove the unshifted value
                    // for big-endian.
                    if (color_type & 4) // Alpha channel
                        *((guint16*)(ptr+2)) = a + (a<<8);
                } else if (bit_depth == 8) {
                    *ptr = guint8(gray >> 8);
                    if (color_type & 4) // Alpha channel
                        *((guint8*)(ptr+1)) = a;
                } else {
                    if (!pad) *ptr=0;
                    // In PNG numbers are stored left to right, but in most significant bits first, so the first one processed is the ``big'' mask, etc.
                    int realpad = 8 - bit_depth - pad;
                    *ptr += guint8((gray >> (16-bit_depth))<<realpad); // Note the "+="
                    if (color_type & 4) // Alpha channel
                        *(ptr+1) += guint8((a >> (8-bit_depth))<<(bit_depth + realpad));
                }
            }

            pad += bit_depth*n_fields;
            ptr += pad/8;
            pad %= 8;
        }
        // Align bytes on rows
        if (pad) {
            pad = 0;
            ptr++;
        }
    }
    return new_data; 
}








/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :