summaryrefslogtreecommitdiff
path: root/gdk-pixbuf/gdk-pixbuf.c
blob: 2594b99e91ee66811a7083584d1828f3ecbbf0fe (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/* GdkPixbuf library - Basic memory management
 *
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Authors: Mark Crichton <crichton@gimp.org>
 *          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 <math.h>
#include <stdlib.h>
#include <string.h>

#define GDK_PIXBUF_C_COMPILATION
#include "gdk-pixbuf-private.h"
#include "gdk-pixbuf-features.h"
#include "gdk-pixbuf-enum-types.h"

/* Include the marshallers */
#include <glib-object.h>
#include <gio/gio.h>
#include "gdk-pixbuf-marshal.h"

/**
 * SECTION:creating
 * @Short_description: Creating a pixbuf from image data that is already in memory.
 * @Title: Image Data in Memory
 * @See_also: gdk_pixbuf_finalize().
 * 
 * The most basic way to create a pixbuf is to wrap an existing pixel
 * buffer with a #GdkPixbuf structure.  You can use the
 * gdk_pixbuf_new_from_data() function to do this You need to specify
 * the destroy notification function that will be called when the
 * data buffer needs to be freed; this will happen when a #GdkPixbuf
 * is finalized by the reference counting functions If you have a
 * chunk of static data compiled into your application, you can pass
 * in %NULL as the destroy notification function so that the data
 * will not be freed.
 * 
 * The gdk_pixbuf_new() function can be used as a convenience to
 * create a pixbuf with an empty buffer.  This is equivalent to
 * allocating a data buffer using malloc() and then wrapping it with
 * gdk_pixbuf_new_from_data(). The gdk_pixbuf_new() function will
 * compute an optimal rowstride so that rendering can be performed
 * with an efficient algorithm.
 * 
 * As a special case, you can use the gdk_pixbuf_new_from_xpm_data()
 * function to create a pixbuf from inline XPM image data.
 * 
 * You can also copy an existing pixbuf with the gdk_pixbuf_copy()
 * function.  This is not the same as just doing a g_object_ref()
 * on the old pixbuf; the copy function will actually duplicate the
 * pixel data in memory and create a new #GdkPixbuf structure for it.
 */

/**
 * SECTION:refcounting
 * @Short_description: Functions for reference counting and memory management on pixbufs.
 * @Title: Reference Counting and Memory Mangement
 * @See_also: #GdkPixbuf, gdk_pixbuf_new_from_data().
 * 
 * #GdkPixbuf structures are reference counted.  This means that an
 * application can share a single pixbuf among many parts of the
 * code.  When a piece of the program needs to keep a pointer to a
 * pixbuf, it should add a reference to it by calling g_object_ref().
 * When it no longer needs the pixbuf, it should subtract a reference
 * by calling g_object_unref().  The pixbuf will be destroyed when
 * its reference count drops to zero.  Newly-created #GdkPixbuf
 * structures start with a reference count of one.
 * 
 * > As #GdkPixbuf is derived from #GObject now, gdk_pixbuf_ref() and
 * > gdk_pixbuf_unref() are deprecated in favour of g_object_ref()
 * > and g_object_unref() resp.
 * 
 * Finalizing a pixbuf means to free its pixel data and to free the
 * #GdkPixbuf structure itself.  Most of the library functions that
 * create #GdkPixbuf structures create the pixel data by themselves
 * and define the way it should be freed; you do not need to worry
 * about those.
 *
 * To provide preallocated pixel data, use
 * gdk_pixbuf_new_from_bytes().  The gdk_pixbuf_new_from_data() API is
 * an older variant that predates the existence of #GBytes.
 */

/**
 * SECTION:gdk-pixbuf
 * @Short_description: Information that describes an image.
 * @Title: The GdkPixbuf Structure
 *
 * The #GdkPixbuf structure contains
 * information that describes an image in memory.
 *
 * ## Image Data ## {#image-data}
 *
 * Image data in a pixbuf is stored in memory in uncompressed,
 * packed format.  Rows in the image are stored top to bottom, and
 * in each row pixels are stored from left to right.  There may be
 * padding at the end of a row.  The "rowstride" value of a pixbuf,
 * as returned by gdk_pixbuf_get_rowstride(), indicates the number
 * of bytes between rows.
 *
 * ## put_pixel() Example ## {#put-pixel}
 *
 * The following code illustrates a simple put_pixel()
 * function for RGB pixbufs with 8 bits per channel with an alpha
 * channel.  It is not included in the gdk-pixbuf library for
 * performance reasons; rather than making several function calls
 * for each pixel, your own code can take shortcuts.
 *
 * |[<!-- language="C" -->
 * static void
 * put_pixel (GdkPixbuf *pixbuf, int x, int y, guchar red, guchar green, guchar blue, guchar alpha)
 * {
 *   int width, height, rowstride, n_channels;
 *   guchar *pixels, *p;
 *
 *   n_channels = gdk_pixbuf_get_n_channels (pixbuf);
 *
 *   g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
 *   g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
 *   g_assert (gdk_pixbuf_get_has_alpha (pixbuf));
 *   g_assert (n_channels == 4);
 *
 *   width = gdk_pixbuf_get_width (pixbuf);
 *   height = gdk_pixbuf_get_height (pixbuf);
 *
 *   g_assert (x >= 0 && x < width);
 *   g_assert (y >= 0 && y < height);
 *
 *   rowstride = gdk_pixbuf_get_rowstride (pixbuf);
 *   pixels = gdk_pixbuf_get_pixels (pixbuf);
 *
 *   p = pixels + y * rowstride + x * n_channels;
 *   p[0] = red;
 *   p[1] = green;
 *   p[2] = blue;
 *   p[3] = alpha;
 * }
 * ]|
 *
 * This function will not work for pixbufs with images that are
 * other than 8 bits per sample or channel, but it will work for
 * most of the pixbufs that GTK+ uses.
 *
 * If you are doing memcpy() of raw pixbuf data, note that the last row
 * in the pixbuf may not be as wide as the full rowstride, but rather
 * just as wide as the pixel data needs to be. That is, it is unsafe to
 * do `memcpy (dest, pixels, rowstride * height)` to copy a whole pixbuf.
 * Use gdk_pixbuf_copy() instead, or compute the width in bytes of the
 * last row as `width * ((n_channels * bits_per_sample + 7) / 8)`.
 */

static void gdk_pixbuf_finalize     (GObject        *object);
static void gdk_pixbuf_set_property (GObject        *object,
				     guint           prop_id,
				     const GValue   *value,
				     GParamSpec     *pspec);
static void gdk_pixbuf_get_property (GObject        *object,
				     guint           prop_id,
				     GValue         *value,
				     GParamSpec     *pspec);
static void gdk_pixbuf_constructed  (GObject        *object);


enum 
{
  PROP_0,
  PROP_COLORSPACE,
  PROP_N_CHANNELS,
  PROP_HAS_ALPHA,
  PROP_BITS_PER_SAMPLE,
  PROP_WIDTH,
  PROP_HEIGHT,
  PROP_ROWSTRIDE,
  PROP_PIXELS,
  PROP_PIXEL_BYTES
};

static void gdk_pixbuf_icon_iface_init (GIconIface *iface);
static void gdk_pixbuf_loadable_icon_iface_init (GLoadableIconIface *iface);

G_DEFINE_TYPE_WITH_CODE (GdkPixbuf, gdk_pixbuf, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_ICON, gdk_pixbuf_icon_iface_init)
                         G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON, gdk_pixbuf_loadable_icon_iface_init))

static void 
gdk_pixbuf_init (GdkPixbuf *pixbuf)
{
  pixbuf->colorspace = GDK_COLORSPACE_RGB;
  pixbuf->n_channels = 3;
  pixbuf->bits_per_sample = 8;
  pixbuf->has_alpha = FALSE;
  pixbuf->storage = STORAGE_UNINITIALIZED;
}

static void
gdk_pixbuf_class_init (GdkPixbufClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);

        _gdk_pixbuf_init_gettext ();

        object_class->finalize = gdk_pixbuf_finalize;
        object_class->set_property = gdk_pixbuf_set_property;
        object_class->get_property = gdk_pixbuf_get_property;
        object_class->constructed = gdk_pixbuf_constructed;

#define PIXBUF_PARAM_FLAGS G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY|\
                           G_PARAM_EXPLICIT_NOTIFY|\
                           G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
        /**
         * GdkPixbuf:n-channels:
         *
         * The number of samples per pixel. 
         * Currently, only 3 or 4 samples per pixel are supported.
         */
        g_object_class_install_property (object_class,
                                         PROP_N_CHANNELS,
                                         g_param_spec_int ("n-channels",
                                                           _("Number of Channels"),
                                                           _("The number of samples per pixel"),
                                                           0,
                                                           G_MAXINT,
                                                           3,
                                                           PIXBUF_PARAM_FLAGS));

        g_object_class_install_property (object_class,
                                         PROP_COLORSPACE,
                                         g_param_spec_enum ("colorspace",
                                                            _("Colorspace"),
                                                            _("The colorspace in which the samples are interpreted"),
                                                            GDK_TYPE_COLORSPACE,
                                                            GDK_COLORSPACE_RGB,
                                                            PIXBUF_PARAM_FLAGS));

        g_object_class_install_property (object_class,
                                         PROP_HAS_ALPHA,
                                         g_param_spec_boolean ("has-alpha",
                                                               _("Has Alpha"),
                                                               _("Whether the pixbuf has an alpha channel"),
                                                               FALSE,
                                                               PIXBUF_PARAM_FLAGS));

        /**
         * GdkPixbuf:bits-per-sample:
         *
         * The number of bits per sample. 
         * Currently only 8 bit per sample are supported.
         */
        g_object_class_install_property (object_class,
                                         PROP_BITS_PER_SAMPLE,
                                         g_param_spec_int ("bits-per-sample",
                                                           _("Bits per Sample"),
                                                           _("The number of bits per sample"),
                                                           1,
                                                           16,
                                                           8,
                                                           PIXBUF_PARAM_FLAGS));

        g_object_class_install_property (object_class,
                                         PROP_WIDTH,
                                         g_param_spec_int ("width",
                                                           _("Width"),
                                                           _("The number of columns of the pixbuf"),
                                                           1,
                                                           G_MAXINT,
                                                           1,
                                                           PIXBUF_PARAM_FLAGS));

        g_object_class_install_property (object_class,
                                         PROP_HEIGHT,
                                         g_param_spec_int ("height",
                                                           _("Height"),
                                                           _("The number of rows of the pixbuf"),
                                                           1,
                                                           G_MAXINT,
                                                           1,
                                                           PIXBUF_PARAM_FLAGS));

        /**
         * GdkPixbuf:rowstride:
         *
         * The number of bytes between the start of a row and 
         * the start of the next row. This number must (obviously)
         * be at least as large as the width of the pixbuf.
         */
        g_object_class_install_property (object_class,
                                         PROP_ROWSTRIDE,
                                         g_param_spec_int ("rowstride",
                                                           _("Rowstride"),
                                                           _("The number of bytes between the start of a row and the start of the next row"),
                                                           1,
                                                           G_MAXINT,
                                                           1,
                                                           PIXBUF_PARAM_FLAGS));

        g_object_class_install_property (object_class,
                                         PROP_PIXELS,
                                         g_param_spec_pointer ("pixels",
                                                               _("Pixels"),
                                                               _("A pointer to the pixel data of the pixbuf"),
                                                               PIXBUF_PARAM_FLAGS));

        /**
         * GdkPixbuf::pixel-bytes:
         *
         * If set, this pixbuf was created from read-only #GBytes.
         * Replaces GdkPixbuf::pixels.
         * 
         * Since: 2.32
         */
        g_object_class_install_property (object_class,
                                         PROP_PIXEL_BYTES,
                                         g_param_spec_boxed ("pixel-bytes",
                                                             _("Pixel Bytes"),
                                                             _("Readonly pixel data"),
                                                             G_TYPE_BYTES,
                                                             PIXBUF_PARAM_FLAGS));
}

static void
free_pixels (GdkPixbuf *pixbuf)
{
        g_assert (pixbuf->storage == STORAGE_PIXELS);

        if (pixbuf->s.pixels.pixels && pixbuf->s.pixels.destroy_fn) {
                (* pixbuf->s.pixels.destroy_fn) (pixbuf->s.pixels.pixels, pixbuf->s.pixels.destroy_fn_data);
        }

        pixbuf->s.pixels.pixels = NULL;
}

static void
free_bytes (GdkPixbuf *pixbuf)
{
        g_assert (pixbuf->storage == STORAGE_BYTES);

        g_clear_pointer (&pixbuf->s.bytes.bytes, g_bytes_unref);
}

static void
gdk_pixbuf_finalize (GObject *object)
{
        GdkPixbuf *pixbuf = GDK_PIXBUF (object);

        switch (pixbuf->storage) {
        case STORAGE_PIXELS:
                free_pixels (pixbuf);
                break;

        case STORAGE_BYTES:
                free_bytes (pixbuf);
                break;

        default:
                g_assert_not_reached ();
        }
        
        G_OBJECT_CLASS (gdk_pixbuf_parent_class)->finalize (object);
}


/**
 * gdk_pixbuf_ref: (skip)
 * @pixbuf: A pixbuf.
 *
 * Adds a reference to a pixbuf.
 *
 * Return value: The same as the @pixbuf argument.
 *
 * Deprecated: 2.0: Use g_object_ref().
 **/
GdkPixbuf *
gdk_pixbuf_ref (GdkPixbuf *pixbuf)
{
        return (GdkPixbuf *) g_object_ref (pixbuf);
}

/**
 * gdk_pixbuf_unref: (skip)
 * @pixbuf: A pixbuf.
 *
 * Removes a reference from a pixbuf.
 *
 * Deprecated: 2.0: Use g_object_unref().
 **/
void
gdk_pixbuf_unref (GdkPixbuf *pixbuf)
{
        g_object_unref (pixbuf);
}

static GBytes *
gdk_pixbuf_make_bytes (GdkPixbuf  *pixbuf,
                       GError    **error)
{
  gchar *buffer;
  gsize size;

  if (!gdk_pixbuf_save_to_buffer (pixbuf, &buffer, &size, "png", error, NULL))
    return NULL;

  return g_bytes_new_take (buffer, size);
}

static GVariant *
gdk_pixbuf_serialize (GIcon *icon)
{
  GError *error = NULL;
  GVariant *result;
  GBytes *bytes;

  bytes = gdk_pixbuf_make_bytes (GDK_PIXBUF (icon), &error);
  if (!bytes)
    {
      g_critical ("Unable to serialise GdkPixbuf to png (via g_icon_serialize()): %s", error->message);
      g_error_free (error);
      return NULL;
    }
  result = g_variant_new_from_bytes (G_VARIANT_TYPE_BYTESTRING, bytes, TRUE);
  g_bytes_unref (bytes);

  return g_variant_new ("(sv)", "bytes", result);
}

static GInputStream *
gdk_pixbuf_load (GLoadableIcon  *icon,
                 int             size,
                 char          **type,
                 GCancellable   *cancellable,
                 GError        **error)
{
  GInputStream *stream;
  GBytes *bytes;

  bytes = gdk_pixbuf_make_bytes (GDK_PIXBUF (icon), error);
  if (!bytes)
    return NULL;

  stream = g_memory_input_stream_new_from_bytes (bytes);
  g_bytes_unref (bytes);

  if (type)
    *type = g_strdup ("image/png");

  return stream;
}

static void
gdk_pixbuf_load_async (GLoadableIcon       *icon,
                       int                  size,
                       GCancellable        *cancellable,
                       GAsyncReadyCallback  callback,
                       gpointer             user_data)
{
  GTask *task;

  task = g_task_new (icon, cancellable, callback, user_data);
  g_task_return_pointer (task, icon, NULL);
  g_object_unref (task);
}

static GInputStream *
gdk_pixbuf_load_finish (GLoadableIcon  *icon,
                        GAsyncResult   *res,
                        char          **type,
                        GError        **error)
{
  g_return_val_if_fail (g_task_is_valid (res, icon), NULL);

  if (!g_task_propagate_pointer (G_TASK (res), error))
    return NULL;

  return gdk_pixbuf_load (icon, 0, type, NULL, error);
}

static void
gdk_pixbuf_loadable_icon_iface_init (GLoadableIconIface *iface)
{
  iface->load = gdk_pixbuf_load;

  /* In theory encoding a png could be time-consuming but we're talking
   * about icons here, so assume it's probably going to be OK and handle
   * the async variant of the call in-thread instead of having the
   * default implementation dispatch it to a worker.
   */
  iface->load_async = gdk_pixbuf_load_async;
  iface->load_finish = gdk_pixbuf_load_finish;
}

static void
gdk_pixbuf_icon_iface_init (GIconIface *iface)
{
        iface->hash = (guint (*) (GIcon *)) g_direct_hash;
        iface->equal = (gboolean (*) (GIcon *, GIcon *)) g_direct_equal;
        iface->serialize = gdk_pixbuf_serialize;
}

/* Used as the destroy notification function for gdk_pixbuf_new() */
static void
free_buffer (guchar *pixels, gpointer data)
{
	g_free (pixels);
}

/**
 * gdk_pixbuf_calculate_rowstride:
 * @colorspace: Color space for image
 * @has_alpha: Whether the image should have transparency information
 * @bits_per_sample: Number of bits per color sample
 * @width: Width of image in pixels, must be > 0
 * @height: Height of image in pixels, must be > 0
 *
 * Calculates the rowstride that an image created with those values would
 * have. This is useful for front-ends and backends that want to sanity
 * check image values without needing to create them.
 *
 * Return value: the rowstride for the given values, or -1 in case of error.
 *
 * Since: 2.36.8
 */
gint
gdk_pixbuf_calculate_rowstride (GdkColorspace colorspace,
				gboolean      has_alpha,
				int           bits_per_sample,
				int           width,
				int           height)
{
	unsigned int channels;

	g_return_val_if_fail (colorspace == GDK_COLORSPACE_RGB, -1);
	g_return_val_if_fail (bits_per_sample == 8, -1);
	g_return_val_if_fail (width > 0, -1);
	g_return_val_if_fail (height > 0, -1);

	channels = has_alpha ? 4 : 3;

	/* Overflow? */
	if (width > (G_MAXINT - 3) / channels)
		return -1;

	/* Always align rows to 32-bit boundaries */
	return (width * channels + 3) & ~3;
}

/**
 * gdk_pixbuf_new:
 * @colorspace: Color space for image
 * @has_alpha: Whether the image should have transparency information
 * @bits_per_sample: Number of bits per color sample
 * @width: Width of image in pixels, must be > 0
 * @height: Height of image in pixels, must be > 0
 *
 * Creates a new #GdkPixbuf structure and allocates a buffer for it.  The 
 * buffer has an optimal rowstride.  Note that the buffer is not cleared;
 * you will have to fill it completely yourself.
 *
 * Return value: (nullable): A newly-created #GdkPixbuf with a reference count of 1, or
 * %NULL if not enough memory could be allocated for the image buffer.
 **/
GdkPixbuf *
gdk_pixbuf_new (GdkColorspace colorspace, 
                gboolean      has_alpha,
                int           bits_per_sample,
                int           width,
                int           height)
{
	guchar *buf;
	int rowstride;

	rowstride = gdk_pixbuf_calculate_rowstride (colorspace,
						    has_alpha,
						    bits_per_sample,
						    width,
						    height);
	if (rowstride <= 0)
		return NULL;

	buf = g_try_malloc_n (height, rowstride);
	if (!buf)
		return NULL;

	return gdk_pixbuf_new_from_data (buf, colorspace, has_alpha, bits_per_sample,
					 width, height, rowstride,
					 free_buffer, NULL);
}

/**
 * gdk_pixbuf_copy:
 * @pixbuf: A pixbuf.
 * 
 * Creates a new #GdkPixbuf with a copy of the information in the specified
 * @pixbuf. Note that this does not copy the options set on the original #GdkPixbuf,
 * use gdk_pixbuf_copy_options() for this.
 * 
 * Return value: (nullable) (transfer full): A newly-created pixbuf with a reference count of 1, or %NULL if
 * not enough memory could be allocated.
 **/
GdkPixbuf *
gdk_pixbuf_copy (const GdkPixbuf *pixbuf)
{
	guchar *buf;
	int size;

	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);

	/* Calculate a semi-exact size.  Here we copy with full rowstrides;
	 * maybe we should copy each row individually with the minimum
	 * rowstride?
	 */

	size = gdk_pixbuf_get_byte_length (pixbuf);

	buf = g_try_malloc (size);
	if (!buf)
		return NULL;

	memcpy (buf, gdk_pixbuf_read_pixels (pixbuf), size);

	return gdk_pixbuf_new_from_data (buf,
					 pixbuf->colorspace, pixbuf->has_alpha,
					 pixbuf->bits_per_sample,
					 pixbuf->width, pixbuf->height,
					 pixbuf->rowstride,
					 free_buffer,
					 NULL);
}

/**
 * gdk_pixbuf_new_subpixbuf:
 * @src_pixbuf: a #GdkPixbuf
 * @src_x: X coord in @src_pixbuf
 * @src_y: Y coord in @src_pixbuf
 * @width: width of region in @src_pixbuf
 * @height: height of region in @src_pixbuf
 * 
 * Creates a new pixbuf which represents a sub-region of @src_pixbuf.
 * The new pixbuf shares its pixels with the original pixbuf, so
 * writing to one affects both.  The new pixbuf holds a reference to
 * @src_pixbuf, so @src_pixbuf will not be finalized until the new
 * pixbuf is finalized.
 *
 * Note that if @src_pixbuf is read-only, this function will force it
 * to be mutable.
 *
 * Return value: (transfer full): a new pixbuf 
 **/
GdkPixbuf*
gdk_pixbuf_new_subpixbuf (GdkPixbuf *src_pixbuf,
                          int        src_x,
                          int        src_y,
                          int        width,
                          int        height)
{
        guchar *pixels;
        GdkPixbuf *sub;

        g_return_val_if_fail (GDK_IS_PIXBUF (src_pixbuf), NULL);
        g_return_val_if_fail (src_x >= 0 && src_x + width <= src_pixbuf->width, NULL);
        g_return_val_if_fail (src_y >= 0 && src_y + height <= src_pixbuf->height, NULL);
        
        /* Note causes an implicit copy where src_pixbuf owns the data */
        pixels = (gdk_pixbuf_get_pixels (src_pixbuf)
                  + src_y * src_pixbuf->rowstride
                  + src_x * src_pixbuf->n_channels);

        sub = gdk_pixbuf_new_from_data (pixels,
                                        src_pixbuf->colorspace,
                                        src_pixbuf->has_alpha,
                                        src_pixbuf->bits_per_sample,
                                        width, height,
                                        src_pixbuf->rowstride,
                                        NULL, NULL);

        /* Keep a reference to src_pixbuf */
        g_object_ref (src_pixbuf);
  
        g_object_set_qdata_full (G_OBJECT (sub),
                                 g_quark_from_static_string ("gdk-pixbuf-subpixbuf-src"),
                                 src_pixbuf,
                                 (GDestroyNotify) g_object_unref);

        return sub;
}



/* Accessors */

/**
 * gdk_pixbuf_get_colorspace:
 * @pixbuf: A pixbuf.
 *
 * Queries the color space of a pixbuf.
 *
 * Return value: Color space.
 **/
GdkColorspace
gdk_pixbuf_get_colorspace (const GdkPixbuf *pixbuf)
{
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), GDK_COLORSPACE_RGB);

	return pixbuf->colorspace;
}

/**
 * gdk_pixbuf_get_n_channels:
 * @pixbuf: A pixbuf.
 *
 * Queries the number of channels of a pixbuf.
 *
 * Return value: Number of channels.
 **/
int
gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)
{
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);

	return pixbuf->n_channels;
}

/**
 * gdk_pixbuf_get_has_alpha:
 * @pixbuf: A pixbuf.
 *
 * Queries whether a pixbuf has an alpha channel (opacity information).
 *
 * Return value: %TRUE if it has an alpha channel, %FALSE otherwise.
 **/
gboolean
gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)
{
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);

	return pixbuf->has_alpha ? TRUE : FALSE;
}

/**
 * gdk_pixbuf_get_bits_per_sample:
 * @pixbuf: A pixbuf.
 *
 * Queries the number of bits per color sample in a pixbuf.
 *
 * Return value: Number of bits per color sample.
 **/
int
gdk_pixbuf_get_bits_per_sample (const GdkPixbuf *pixbuf)
{
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);

	return pixbuf->bits_per_sample;
}

/**
 * gdk_pixbuf_get_pixels:
 * @pixbuf: A pixbuf.
 *
 * Queries a pointer to the pixel data of a pixbuf.
 *
 * Return value: (array): A pointer to the pixbuf's pixel data.
 * Please see the section on [image data][image-data] for information
 * about how the pixel data is stored in memory.
 *
 * This function will cause an implicit copy of the pixbuf data if the
 * pixbuf was created from read-only data.
 **/
guchar *
gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)
{
        return gdk_pixbuf_get_pixels_with_length (pixbuf, NULL);
}

static void
downgrade_to_pixels (const GdkPixbuf *pixbuf)
{
        switch (pixbuf->storage) {
        case STORAGE_PIXELS:
                return;

        case STORAGE_BYTES: {
                GdkPixbuf *mut_pixbuf = (GdkPixbuf *) pixbuf;
                gsize len;
                Pixels pixels;

                pixels.pixels = g_bytes_unref_to_data (pixbuf->s.bytes.bytes, &len);
                pixels.destroy_fn = free_buffer;
                pixels.destroy_fn_data = NULL;

                mut_pixbuf->storage = STORAGE_PIXELS;
                mut_pixbuf->s.pixels = pixels;
                break;
        }

        default:
                g_assert_not_reached ();
        }
}

/**
 * gdk_pixbuf_get_pixels_with_length: (rename-to gdk_pixbuf_get_pixels)
 * @pixbuf: A pixbuf.
 * @length: (out): The length of the binary data.
 *
 * Queries a pointer to the pixel data of a pixbuf.
 *
 * Return value: (array length=length): A pointer to the pixbuf's
 * pixel data.  Please see the section on [image data][image-data]
 * for information about how the pixel data is stored in memory.
 *
 * This function will cause an implicit copy of the pixbuf data if the
 * pixbuf was created from read-only data.
 *
 * Since: 2.26
 */
guchar *
gdk_pixbuf_get_pixels_with_length (const GdkPixbuf *pixbuf,
                                   guint           *length)
{
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);

        downgrade_to_pixels (pixbuf);
        g_assert (pixbuf->storage == STORAGE_PIXELS);

        if (length)
                *length = gdk_pixbuf_get_byte_length (pixbuf);

	return pixbuf->s.pixels.pixels;
}

/**
 * gdk_pixbuf_read_pixels:
 * @pixbuf: A pixbuf
 *
 * Provides a read-only pointer to the raw pixel data; must not be
 * modified.  This function allows skipping the implicit copy that
 * must be made if gdk_pixbuf_get_pixels() is called on a read-only
 * pixbuf.
 *
 * Returns: a read-only pointer to the raw pixel data
 *
 * Since: 2.32
 */
const guint8*
gdk_pixbuf_read_pixels (const GdkPixbuf  *pixbuf)
{
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);

        switch (pixbuf->storage) {
        case STORAGE_PIXELS:
                return pixbuf->s.pixels.pixels;

        case STORAGE_BYTES: {
                gsize len;
                /* Ignore len; callers know the size via other variables */
                return g_bytes_get_data (pixbuf->s.bytes.bytes, &len);
        }

        default:
                g_assert_not_reached ();
                return NULL;
        }
}

/**
 * gdk_pixbuf_read_pixel_bytes:
 * @pixbuf: A pixbuf
 *
 * Provides a #GBytes buffer containing the raw pixel data; the data
 * must not be modified.  This function allows skipping the implicit
 * copy that must be made if gdk_pixbuf_get_pixels() is called on a
 * read-only pixbuf.
 *
 * Returns: (transfer full): A new reference to a read-only copy of
 *   the pixel data.  Note that for mutable pixbufs, this function will
 *   incur a one-time copy of the pixel data for conversion into the
 *   returned #GBytes.
 *
 * Since: 2.32
 */
GBytes *
gdk_pixbuf_read_pixel_bytes (const GdkPixbuf  *pixbuf)
{
        g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);

        switch (pixbuf->storage) {
        case STORAGE_PIXELS:
                return g_bytes_new (pixbuf->s.pixels.pixels,
                                    gdk_pixbuf_get_byte_length (pixbuf));

        case STORAGE_BYTES:
                return g_bytes_ref (pixbuf->s.bytes.bytes);

        default:
                g_assert_not_reached ();
        }
}

/**
 * gdk_pixbuf_get_width:
 * @pixbuf: A pixbuf.
 *
 * Queries the width of a pixbuf.
 *
 * Return value: Width in pixels.
 **/
int
gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)
{
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);

	return pixbuf->width;
}

/**
 * gdk_pixbuf_get_height:
 * @pixbuf: A pixbuf.
 *
 * Queries the height of a pixbuf.
 *
 * Return value: Height in pixels.
 **/
int
gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)
{
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);

	return pixbuf->height;
}

/**
 * gdk_pixbuf_get_rowstride:
 * @pixbuf: A pixbuf.
 *
 * Queries the rowstride of a pixbuf, which is the number of bytes between
 * the start of a row and the start of the next row.
 *
 * Return value: Distance between row starts.
 **/
int
gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf)
{
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);

	return pixbuf->rowstride;
}

/**
 * gdk_pixbuf_get_byte_length:
 * @pixbuf: A pixbuf
 *
 * Returns the length of the pixel data, in bytes.
 *
 * Return value: The length of the pixel data.
 *
 * Since: 2.26
 */
gsize
gdk_pixbuf_get_byte_length (const GdkPixbuf *pixbuf)
{
	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), -1);

        return ((pixbuf->height - 1) * pixbuf->rowstride +
                pixbuf->width * ((pixbuf->n_channels * pixbuf->bits_per_sample + 7) / 8));
}



/* General initialization hooks */
const guint gdk_pixbuf_major_version = GDK_PIXBUF_MAJOR;
const guint gdk_pixbuf_minor_version = GDK_PIXBUF_MINOR;
const guint gdk_pixbuf_micro_version = GDK_PIXBUF_MICRO;

const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;

/* Error quark */
GQuark
gdk_pixbuf_error_quark (void)
{
  return g_quark_from_static_string ("gdk-pixbuf-error-quark");
}

/**
 * gdk_pixbuf_fill:
 * @pixbuf: a #GdkPixbuf
 * @pixel: RGBA pixel to clear to
 *         (0xffffffff is opaque white, 0x00000000 transparent black)
 *
 * Clears a pixbuf to the given RGBA value, converting the RGBA value into
 * the pixbuf's pixel format. The alpha will be ignored if the pixbuf
 * doesn't have an alpha channel.
 * 
 **/
void
gdk_pixbuf_fill (GdkPixbuf *pixbuf,
                 guint32    pixel)
{
        guchar *pixels;
        guint r, g, b, a;
        guchar *p;
        guint w, h;

        g_return_if_fail (GDK_IS_PIXBUF (pixbuf));

        if (pixbuf->width == 0 || pixbuf->height == 0)
                return;

        /* Force an implicit copy */
        pixels = gdk_pixbuf_get_pixels (pixbuf);

        r = (pixel & 0xff000000) >> 24;
        g = (pixel & 0x00ff0000) >> 16;
        b = (pixel & 0x0000ff00) >> 8;
        a = (pixel & 0x000000ff);

        h = pixbuf->height;
        
        while (h--) {
                w = pixbuf->width;
                p = pixels;

                switch (pixbuf->n_channels) {
                case 3:
                        while (w--) {
                                p[0] = r;
                                p[1] = g;
                                p[2] = b;
                                p += 3;
                        }
                        break;
                case 4:
                        while (w--) {
                                p[0] = r;
                                p[1] = g;
                                p[2] = b;
                                p[3] = a;
                                p += 4;
                        }
                        break;
                default:
                        break;
                }
                
                pixels += pixbuf->rowstride;
        }
}



/**
 * gdk_pixbuf_get_option:
 * @pixbuf: a #GdkPixbuf
 * @key: a nul-terminated string.
 * 
 * Looks up @key in the list of options that may have been attached to the
 * @pixbuf when it was loaded, or that may have been attached by another
 * function using gdk_pixbuf_set_option().
 *
 * For instance, the ANI loader provides "Title" and "Artist" options. 
 * The ICO, XBM, and XPM loaders provide "x_hot" and "y_hot" hot-spot 
 * options for cursor definitions. The PNG loader provides the tEXt ancillary
 * chunk key/value pairs as options. Since 2.12, the TIFF and JPEG loaders
 * return an "orientation" option string that corresponds to the embedded 
 * TIFF/Exif orientation tag (if present). Since 2.32, the TIFF loader sets
 * the "multipage" option string to "yes" when a multi-page TIFF is loaded.
 * Since 2.32 the JPEG and PNG loaders set "x-dpi" and "y-dpi" if the file
 * contains image density information in dots per inch.
 * Since 2.36.6, the JPEG loader sets the "comment" option with the comment
 * EXIF tag.
 * 
 * Return value: the value associated with @key. This is a nul-terminated 
 * string that should not be freed or %NULL if @key was not found.
 **/
const gchar *
gdk_pixbuf_get_option (GdkPixbuf   *pixbuf,
                       const gchar *key)
{
        gchar **options;
        gint i;

        g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
        g_return_val_if_fail (key != NULL, NULL);
  
        options = g_object_get_qdata (G_OBJECT (pixbuf), 
                                      g_quark_from_static_string ("gdk_pixbuf_options"));
        if (options) {
                for (i = 0; options[2*i]; i++) {
                        if (strcmp (options[2*i], key) == 0)
                                return options[2*i+1];
                }
        }
        
        return NULL;
}

/**
 * gdk_pixbuf_get_options:
 * @pixbuf: a #GdkPixbuf
 *
 * Returns a #GHashTable with a list of all the options that may have been
 * attached to the @pixbuf when it was loaded, or that may have been
 * attached by another function using gdk_pixbuf_set_option().
 *
 * See gdk_pixbuf_get_option() for more details.
 *
 * Return value: (transfer container) (element-type utf8 utf8): a #GHashTable of key/values
 *
 * Since: 2.32
 **/
GHashTable *
gdk_pixbuf_get_options (GdkPixbuf *pixbuf)
{
        GHashTable *ht;
        gchar **options;

        g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);

        ht = g_hash_table_new (g_str_hash, g_str_equal);

        options = g_object_get_qdata (G_OBJECT (pixbuf),
                                      g_quark_from_static_string ("gdk_pixbuf_options"));
        if (options) {
                gint i;

                for (i = 0; options[2*i]; i++)
                        g_hash_table_insert (ht, options[2*i], options[2*i+1]);
        }

        return ht;
}

/**
 * gdk_pixbuf_remove_option:
 * @pixbuf: a #GdkPixbuf
 * @key: a nul-terminated string representing the key to remove.
 *
 * Remove the key/value pair option attached to a #GdkPixbuf.
 *
 * Return value: %TRUE if an option was removed, %FALSE if not.
 *
 * Since: 2.36
 **/
gboolean
gdk_pixbuf_remove_option (GdkPixbuf   *pixbuf,
                          const gchar *key)
{
        GQuark  quark;
        gchar **options;
        guint n;
        GPtrArray *array;
        gboolean found;

        g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
        g_return_val_if_fail (key != NULL, FALSE);

        quark = g_quark_from_static_string ("gdk_pixbuf_options");

        options = g_object_get_qdata (G_OBJECT (pixbuf), quark);
        if (!options)
                return FALSE;

        g_object_steal_qdata (G_OBJECT (pixbuf), quark);

        /* There's at least a nul-terminator */
        array = g_ptr_array_new_full (1, g_free);

        found = FALSE;
        for (n = 0; options[2*n]; n++) {
                if (strcmp (options[2*n], key) != 0) {
                        g_ptr_array_add (array, g_strdup (options[2*n]));   /* key */
                        g_ptr_array_add (array, g_strdup (options[2*n+1])); /* value */
                } else {
                        found = TRUE;
                }
        }

        if (array->len == 0) {
                g_ptr_array_unref (array);
                g_strfreev (options);
                return found;
        }

        if (!found) {
                g_ptr_array_free (array, TRUE);
                g_object_set_qdata_full (G_OBJECT (pixbuf), quark,
                                         options, (GDestroyNotify) g_strfreev);
                return FALSE;
        }

        g_ptr_array_add (array, NULL);
        g_object_set_qdata_full (G_OBJECT (pixbuf), quark,
                                 g_ptr_array_free (array, FALSE), (GDestroyNotify) g_strfreev);
        g_strfreev (options);

        return TRUE;
}

/**
 * gdk_pixbuf_set_option:
 * @pixbuf: a #GdkPixbuf
 * @key: a nul-terminated string.
 * @value: a nul-terminated string.
 * 
 * Attaches a key/value pair as an option to a #GdkPixbuf. If @key already
 * exists in the list of options attached to @pixbuf, the new value is 
 * ignored and %FALSE is returned.
 *
 * Return value: %TRUE on success.
 *
 * Since: 2.2
 **/
gboolean
gdk_pixbuf_set_option (GdkPixbuf   *pixbuf,
                       const gchar *key,
                       const gchar *value)
{
        GQuark  quark;
        gchar **options;
        gint n = 0;
 
        g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), FALSE);
        g_return_val_if_fail (key != NULL, FALSE);
        g_return_val_if_fail (value != NULL, FALSE);

        quark = g_quark_from_static_string ("gdk_pixbuf_options");

        options = g_object_get_qdata (G_OBJECT (pixbuf), quark);

        if (options) {
                for (n = 0; options[2*n]; n++) {
                        if (strcmp (options[2*n], key) == 0)
                                return FALSE;
                }

                g_object_steal_qdata (G_OBJECT (pixbuf), quark);
                options = g_renew (gchar *, options, 2*(n+1) + 1);
        } else {
                options = g_new (gchar *, 3);
        }
        
        options[2*n]   = g_strdup (key);
        options[2*n+1] = g_strdup (value);
        options[2*n+2] = NULL;

        g_object_set_qdata_full (G_OBJECT (pixbuf), quark,
                                 options, (GDestroyNotify) g_strfreev);
        
        return TRUE;
}

/**
 * gdk_pixbuf_copy_options:
 * @src_pixbuf: a #GdkPixbuf to copy options from
 * @dest_pixbuf: the #GdkPixbuf to copy options to
 *
 * Copy the key/value pair options attached to a #GdkPixbuf to another.
 * This is useful to keep original metadata after having manipulated
 * a file. However be careful to remove metadata which you've already
 * applied, such as the "orientation" option after rotating the image.
 *
 * Return value: %TRUE on success.
 *
 * Since: 2.36
 **/
gboolean
gdk_pixbuf_copy_options (GdkPixbuf *src_pixbuf,
                         GdkPixbuf *dest_pixbuf)
{
        GQuark  quark;
        gchar **options;

        g_return_val_if_fail (GDK_IS_PIXBUF (src_pixbuf), FALSE);
        g_return_val_if_fail (GDK_IS_PIXBUF (dest_pixbuf), FALSE);

        quark = g_quark_from_static_string ("gdk_pixbuf_options");

        options = g_object_dup_qdata (G_OBJECT (src_pixbuf),
                                      quark,
                                      (GDuplicateFunc) g_strdupv,
                                      NULL);

        if (options == NULL)
                return TRUE;

        g_object_set_qdata_full (G_OBJECT (dest_pixbuf), quark,
                                 options, (GDestroyNotify) g_strfreev);

        return TRUE;
}

static void
gdk_pixbuf_set_property (GObject         *object,
			 guint            prop_id,
			 const GValue    *value,
			 GParamSpec      *pspec)
{
        GdkPixbuf *pixbuf = GDK_PIXBUF (object);
        gboolean notify = TRUE;

        switch (prop_id) {
        case PROP_COLORSPACE:
                notify = pixbuf->colorspace != g_value_get_enum (value);
                pixbuf->colorspace = g_value_get_enum (value);
                break;
        case PROP_N_CHANNELS:
                notify = pixbuf->n_channels != g_value_get_int (value);
                pixbuf->n_channels = g_value_get_int (value);
                break;
        case PROP_HAS_ALPHA:
                notify = pixbuf->has_alpha != g_value_get_boolean (value);
                pixbuf->has_alpha = g_value_get_boolean (value);
                break;
        case PROP_BITS_PER_SAMPLE:
                notify = pixbuf->bits_per_sample != g_value_get_int (value);
                pixbuf->bits_per_sample = g_value_get_int (value);
                break;
        case PROP_WIDTH:
                notify = pixbuf->width != g_value_get_int (value);
                pixbuf->width = g_value_get_int (value);
                break;
        case PROP_HEIGHT:
                notify = pixbuf->height != g_value_get_int (value);
                pixbuf->height = g_value_get_int (value);
                break;
        case PROP_ROWSTRIDE:
                notify = pixbuf->rowstride != g_value_get_int (value);
                pixbuf->rowstride = g_value_get_int (value);
                break;

        /* The following two are a bit strange.  Both PROP_PIXELS and
         * PROP_PIXEL_BYTES are G_PARAM_CONSTRUCT_ONLY properties, which means
         * that GObject will generate default values for any missing one and
         * call us for *both*.  So, we need to check whether the passed value is
         * not NULL before actually setting pixbuf->storage.
         */
        case PROP_PIXELS: {
                guchar *pixels = g_value_get_pointer (value);

                if (pixels) {
                        g_assert (pixbuf->storage == STORAGE_UNINITIALIZED);

                        pixbuf->storage = STORAGE_PIXELS;
                        pixbuf->s.pixels.pixels = pixels;
                        pixbuf->s.pixels.destroy_fn = NULL;
                        pixbuf->s.pixels.destroy_fn_data = NULL;
                } else {
                        notify = FALSE;
                }

                break;
        }

        case PROP_PIXEL_BYTES: {
                GBytes *bytes = g_value_get_boxed (value);

                if (bytes) {
                        g_assert (pixbuf->storage == STORAGE_UNINITIALIZED);

                        pixbuf->storage = STORAGE_BYTES;
                        pixbuf->s.bytes.bytes = g_value_dup_boxed (value);
                } else {
                        notify = FALSE;
                }

                break;
        }

        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }

        if (notify)
                g_object_notify_by_pspec (G_OBJECT (object), pspec);
}

static void
gdk_pixbuf_get_property (GObject         *object,
			 guint            prop_id,
			 GValue          *value,
			 GParamSpec      *pspec)
{
        GdkPixbuf *pixbuf = GDK_PIXBUF (object);
  
        switch (prop_id) {
        case PROP_COLORSPACE:
                g_value_set_enum (value, gdk_pixbuf_get_colorspace (pixbuf));
                break;
        case PROP_N_CHANNELS:
                g_value_set_int (value, gdk_pixbuf_get_n_channels (pixbuf));
                break;
        case PROP_HAS_ALPHA:
                g_value_set_boolean (value, gdk_pixbuf_get_has_alpha (pixbuf));
                break;
        case PROP_BITS_PER_SAMPLE:
                g_value_set_int (value, gdk_pixbuf_get_bits_per_sample (pixbuf));
                break;
        case PROP_WIDTH:
                g_value_set_int (value, gdk_pixbuf_get_width (pixbuf));
                break;
        case PROP_HEIGHT:
                g_value_set_int (value, gdk_pixbuf_get_height (pixbuf));
                break;
        case PROP_ROWSTRIDE:
                g_value_set_int (value, gdk_pixbuf_get_rowstride (pixbuf));
                break;
        case PROP_PIXELS:
                g_value_set_pointer (value, gdk_pixbuf_get_pixels (pixbuf));
                break;
        case PROP_PIXEL_BYTES:
                g_value_set_boxed (value, gdk_pixbuf_read_pixel_bytes (pixbuf));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}

static void
make_storage_invalid (GdkPixbuf *pixbuf)
{
        char *buf;
        gsize bufsize = 3;

        buf = g_new0(char, bufsize);

        pixbuf->storage = STORAGE_BYTES;
        pixbuf->s.bytes.bytes = g_bytes_new_with_free_func (buf, bufsize, g_free, NULL);

        pixbuf->colorspace = GDK_COLORSPACE_RGB;
        pixbuf->n_channels = 3;
        pixbuf->bits_per_sample = 8;
        pixbuf->width = 1;
        pixbuf->height = 1;
        pixbuf->rowstride = 3;
        pixbuf->has_alpha = FALSE;
}

static void
gdk_pixbuf_constructed (GObject *object)
{
        GdkPixbuf *pixbuf = GDK_PIXBUF (object);

        G_OBJECT_CLASS (gdk_pixbuf_parent_class)->constructed (object);

        switch (pixbuf->storage) {
        case STORAGE_UNINITIALIZED:
                /* This means that neither of the construct properties "pixels" nor "pixel-bytes"
                 * was specified during a call to g_object_new().
                 *
                 * To avoid breaking ABI, we don't emit this warning.  We'll want
                 * to emit it once we can have fallible construction.
                 *
                 * g_warning ("pixbuf needs to be constructed with the 'pixels' or 'pixel-bytes' properties");
                 */

                make_storage_invalid (pixbuf);
                break;

        case STORAGE_PIXELS:
                g_assert (pixbuf->s.pixels.pixels != NULL);
                break;

        case STORAGE_BYTES: {
                gsize bytes_size;
                gint width, height;
                gboolean has_alpha;

                g_assert (pixbuf->s.bytes.bytes != NULL);

                bytes_size = g_bytes_get_size (pixbuf->s.bytes.bytes);
                width = pixbuf->width;
                height = pixbuf->height;
                has_alpha = pixbuf->has_alpha;

                /* This is the same check as in gdk_pixbuf_new_from_bytes() */
                if (!(bytes_size >= width * height * (has_alpha ? 4 : 3))) {
                        g_error ("GBytes is too small to fit the pixbuf's declared width and height");
                }
                break;
        }

        default:
                g_assert_not_reached ();
        }

        g_assert (pixbuf->storage != STORAGE_UNINITIALIZED);
}