-
Notifications
You must be signed in to change notification settings - Fork 4
/
gdk-pixbuf-2.36.x.patch
4108 lines (4062 loc) · 132 KB
/
gdk-pixbuf-2.36.x.patch
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
--- gdk-pixbuf/gdk-pixbuf-core.h Wed Aug 03 01:49:36 2016
+++ gdk-pixbuf/gdk-pixbuf-core.h Sun Sep 25 14:31:35 2016
@@ -284,16 +284,6 @@
/* Simple loading */
-#ifndef __GTK_DOC_IGNORE__
-#ifdef G_OS_WIN32
-/* DLL ABI stability hack. */
-#define gdk_pixbuf_new_from_file gdk_pixbuf_new_from_file_utf8
-#define gdk_pixbuf_new_from_file_at_size gdk_pixbuf_new_from_file_at_size_utf8
-#define gdk_pixbuf_new_from_file_at_scale gdk_pixbuf_new_from_file_at_scale_utf8
-#endif
-#endif
-
-GDK_PIXBUF_AVAILABLE_IN_ALL
GdkPixbuf *gdk_pixbuf_new_from_file (const char *filename,
GError **error);
GDK_PIXBUF_AVAILABLE_IN_2_4
@@ -346,35 +336,77 @@
GError **error);
#endif
+#ifndef __GTK_DOC_IGNORE__
+#ifdef G_OS_WIN32
+/* DLL ABI stability hack. */
+#define gdk_pixbuf_new_from_file gdk_pixbuf_new_from_file_utf8
+#define gdk_pixbuf_new_from_file_at_size gdk_pixbuf_new_from_file_at_size_utf8
+#define gdk_pixbuf_new_from_file_at_scale gdk_pixbuf_new_from_file_at_scale_utf8
+
+GDK_PIXBUF_AVAILABLE_IN_ALL
+GdkPixbuf *gdk_pixbuf_new_from_file_utf8 (const char *filename,
+ GError **error);
+
+GDK_PIXBUF_AVAILABLE_IN_ALL
+GdkPixbuf *gdk_pixbuf_new_from_file_at_size_utf8 (const char *filename,
+ int width,
+ int height,
+ GError **error);
+
+GDK_PIXBUF_AVAILABLE_IN_ALL
+GdkPixbuf *gdk_pixbuf_new_from_file_at_scale_utf8 (const char *filename,
+ int width,
+ int height,
+ gboolean preserve_aspect_ratio,
+ GError **error);
+
+#endif
+#endif
/* Mutations */
+
GDK_PIXBUF_AVAILABLE_IN_ALL
void gdk_pixbuf_fill (GdkPixbuf *pixbuf,
guint32 pixel);
/* Saving */
+GDK_PIXBUF_AVAILABLE_IN_ALL
+gboolean gdk_pixbuf_save (GdkPixbuf *pixbuf,
+ const char *filename,
+ const char *type,
+ GError **error,
+ ...) G_GNUC_NULL_TERMINATED;
+
+GDK_PIXBUF_AVAILABLE_IN_ALL
+gboolean gdk_pixbuf_savev (GdkPixbuf *pixbuf,
+ const char *filename,
+ const char *type,
+ char **option_keys,
+ char **option_values,
+ GError **error);
+
#ifndef __GTK_DOC_IGNORE__
#ifdef G_OS_WIN32
-/* DLL ABI stability hack. */
#define gdk_pixbuf_save gdk_pixbuf_save_utf8
#define gdk_pixbuf_savev gdk_pixbuf_savev_utf8
-#endif
-#endif
GDK_PIXBUF_AVAILABLE_IN_ALL
-gboolean gdk_pixbuf_save (GdkPixbuf *pixbuf,
+gboolean gdk_pixbuf_save_utf8 (GdkPixbuf *pixbuf,
const char *filename,
const char *type,
GError **error,
...) G_GNUC_NULL_TERMINATED;
GDK_PIXBUF_AVAILABLE_IN_ALL
-gboolean gdk_pixbuf_savev (GdkPixbuf *pixbuf,
+gboolean gdk_pixbuf_savev_utf8 (GdkPixbuf *pixbuf,
const char *filename,
const char *type,
char **option_keys,
char **option_values,
GError **error);
+
+#endif
+#endif
/* Saving to a callback function */
--- gdk-pixbuf/gdk-pixbuf-io.c Wed Aug 03 02:19:21 2016
+++ gdk-pixbuf/gdk-pixbuf-io.c Sun Sep 25 14:34:41 2016
@@ -247,8 +247,47 @@
return toplevel;
}
-#endif /* GDK_PIXBUF_RELOCATABLE */
+static char *
+get_libdir (void)
+{
+ static char *libdir = NULL;
+
+ if (libdir == NULL)
+ libdir = g_build_filename (gdk_pixbuf_get_toplevel (), "lib", NULL);
+
+ return libdir;
+}
+
+#undef GDK_PIXBUF_LIBDIR
+#define GDK_PIXBUF_LIBDIR get_libdir()
+
+static void
+correct_prefix (gchar **path)
+{
+ if (strncmp (*path, GDK_PIXBUF_PREFIX "/", strlen (GDK_PIXBUF_PREFIX "/")) == 0 ||
+ strncmp (*path, GDK_PIXBUF_PREFIX "\\", strlen (GDK_PIXBUF_PREFIX "\\")) == 0)
+ {
+ gchar *tem = NULL;
+ if (g_str_has_suffix (*path, ".libs"))
+ {
+ /* We are being run from inside the build tree, and shouldn't mess about. */
+ return;
+ }
+
+ /* This is an entry put there by gdk-pixbuf-query-loaders on the
+ * packager's system. On Windows a prebuilt gdk-pixbuf package can be
+ * installed in a random location. The loaders.cache file
+ * distributed in such a package contains paths from the package
+ * builder's machine. Replace the build-time prefix with the
+ * installation prefix on this machine.
+ */
+ tem = *path;
+ *path = g_strconcat (gdk_pixbuf_get_toplevel (), tem + strlen (GDK_PIXBUF_PREFIX), NULL);
+ g_free (tem);
+ }
+}
+#endif /* GDK_PIXBUF_RELOCATABLE */
#ifdef USE_GMODULE
@@ -331,50 +370,6 @@
return !(*p == '\0');
}
-#ifdef GDK_PIXBUF_RELOCATABLE
-
-static char *
-get_libdir (void)
-{
- static char *libdir = NULL;
-
- if (libdir == NULL)
- libdir = g_build_filename (gdk_pixbuf_get_toplevel (), "lib", NULL);
-
- return libdir;
-}
-
-#undef GDK_PIXBUF_LIBDIR
-#define GDK_PIXBUF_LIBDIR get_libdir()
-
-static void
-correct_prefix (gchar **path)
-{
- if (strncmp (*path, GDK_PIXBUF_PREFIX "/", strlen (GDK_PIXBUF_PREFIX "/")) == 0 ||
- strncmp (*path, GDK_PIXBUF_PREFIX "\\", strlen (GDK_PIXBUF_PREFIX "\\")) == 0)
- {
- gchar *tem = NULL;
- if (g_str_has_suffix (*path, ".libs"))
- {
- /* We are being run from inside the build tree, and shouldn't mess about. */
- return;
- }
-
- /* This is an entry put there by gdk-pixbuf-query-loaders on the
- * packager's system. On Windows a prebuilt gdk-pixbuf package can be
- * installed in a random location. The loaders.cache file
- * distributed in such a package contains paths from the package
- * builder's machine. Replace the build-time prefix with the
- * installation prefix on this machine.
- */
- tem = *path;
- *path = g_strconcat (gdk_pixbuf_get_toplevel (), tem + strlen (GDK_PIXBUF_PREFIX), NULL);
- g_free (tem);
- }
-}
-
-#endif /* GDK_PIXBUF_RELOCATABLE */
-
static gchar *
gdk_pixbuf_get_module_file (void)
{
@@ -436,6 +431,9 @@
#ifdef INCLUDE_bmp
load_one_builtin_module (bmp);
#endif
+#ifdef INCLUDE_wbmp
+ load_one_builtin_module (wbmp);
+#endif
#ifdef INCLUDE_gif
load_one_builtin_module (gif);
#endif
@@ -448,6 +446,9 @@
#ifdef INCLUDE_pnm
load_one_builtin_module (pnm);
#endif
+#ifdef INCLUDE_ras
+ load_one_builtin_module (ras);
+#endif
#ifdef INCLUDE_tiff
load_one_builtin_module (tiff);
#endif
@@ -460,12 +461,27 @@
#ifdef INCLUDE_tga
load_one_builtin_module (tga);
#endif
+#ifdef INCLUDE_pcx
+ load_one_builtin_module (pcx);
+#endif
#ifdef INCLUDE_icns
load_one_builtin_module (icns);
#endif
#ifdef INCLUDE_jasper
load_one_builtin_module (jasper);
#endif
+#ifdef INCLUDE_svg
+ load_one_builtin_module (svg);
+#endif
+#ifdef INCLUDE_webp
+ load_one_builtin_module (webp);
+#endif
+#ifdef INCLUDE_psd
+ load_one_builtin_module (psd);
+#endif
+#ifdef INCLUDE_openraw
+ load_one_builtin_module (openraw);
+#endif
#ifdef INCLUDE_qtif
load_one_builtin_module (qtif);
#endif
@@ -478,9 +494,13 @@
load_one_builtin_module (emf);
load_one_builtin_module (bmp);
load_one_builtin_module (gif);
+#ifndef INCLUDE_jpeg
load_one_builtin_module (jpeg);
+#endif
+#ifndef INCLUDE_tiff
load_one_builtin_module (tiff);
#endif
+#endif
#ifdef INCLUDE_gdip_png
/* Except the gdip-png loader which normally isn't built at all even */
load_one_builtin_module (png);
@@ -656,14 +676,21 @@
module (gif);
module (ico);
module (ani);
+module (ras);
module (xpm);
module (tiff);
module (pnm);
module (bmp);
+module (wbmp);
module (xbm);
module (tga);
+module (pcx);
module (icns);
module (jasper);
+module (svg);
+module (webp);
+module (psd);
+module (openraw);
module (qtif);
module (gdip_ico);
module (gdip_wmf);
@@ -704,9 +731,13 @@
try_module (emf,gdip_emf);
try_module (bmp,gdip_bmp);
try_module (gif,gdip_gif);
+#ifndef INCLUDE_jpeg
try_module (jpeg,gdip_jpeg);
+#endif
+#ifndef INCLUDE_tiff
try_module (tiff,gdip_tiff);
#endif
+#endif
#ifdef INCLUDE_gdip_png
try_module (png,gdip_png);
#endif
@@ -716,6 +747,9 @@
#ifdef INCLUDE_bmp
try_module (bmp,bmp);
#endif
+#ifdef INCLUDE_wbmp
+ try_module (wbmp,wbmp);
+#endif
#ifdef INCLUDE_gif
try_module (gif,gif);
#endif
@@ -731,6 +765,9 @@
#ifdef INCLUDE_pnm
try_module (pnm,pnm);
#endif
+#ifdef INCLUDE_ras
+ try_module (ras,ras);
+#endif
#ifdef INCLUDE_tiff
try_module (tiff,tiff);
#endif
@@ -743,11 +780,26 @@
#ifdef INCLUDE_tga
try_module (tga,tga);
#endif
+#ifdef INCLUDE_pcx
+ try_module (pcx,pcx);
+#endif
#ifdef INCLUDE_icns
try_module (icns,icns);
#endif
#ifdef INCLUDE_jasper
try_module (jasper,jasper);
+#endif
+#ifdef INCLUDE_svg
+ try_module (svg,svg);
+#endif
+#ifdef INCLUDE_webp
+ try_module (webp,webp);
+#endif
+#ifdef INCLUDE_psd
+ try_module (psd,psd);
+#endif
+#ifdef INCLUDE_openraw
+ try_module (openraw,openraw);
#endif
#ifdef INCLUDE_qtif
try_module (qtif,qtif);
--- gdk-pixbuf/io-gdip-jpeg.c Wed Aug 03 02:19:21 2016
+++ gdk-pixbuf/io-gdip-jpeg.c Sun Sep 25 14:36:12 2016
@@ -132,6 +132,8 @@
static const gchar *mime_types[] = {
"image/jpeg",
+ "image/pjpeg",
+ "image/jpg",
NULL
};
--- gdk-pixbuf/io-jpeg.c Wed Aug 03 02:19:21 2016
+++ gdk-pixbuf/io-jpeg.c Sun Sep 25 14:35:40 2016
@@ -1572,6 +1572,8 @@
};
static const gchar *mime_types[] = {
"image/jpeg",
+ "image/pjpeg",
+ "image/jpg",
NULL
};
static const gchar *extensions[] = {
--- gdk-pixbuf/io-pcx.c Thu Oct 06 18:17:20 2016
+++ gdk-pixbuf/io-pcx.c Sat Nov 08 00:25:32 2014
@@ -0,0 +1,758 @@
+/*
+ * GdkPixbuf library - PCX image loader
+ *
+ * Copyright (C) 2003 Josh A. Beam
+ *
+ * Authors: Josh A. Beam <josh@joshbeam.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include <stdio.h>
+#include <string.h>
+#include "gdk-pixbuf-private.h"
+
+#undef PCX_DEBUG
+
+#define PCX_TASK_DONE 0
+#define PCX_TASK_LOAD_HEADER 1
+#define PCX_TASK_LOAD_DATA 2
+#define PCX_TASK_LOAD_PALETTE 3
+
+struct pcx_header {
+ guint8 manufacturer;
+ guint8 version;
+ guint8 encoding;
+ guint8 bitsperpixel;
+ gint16 xmin;
+ gint16 ymin;
+ gint16 xmax;
+ gint16 ymax;
+ guint16 horizdpi;
+ guint16 vertdpi;
+ guint8 palette[48];
+ guint8 reserved;
+ guint8 colorplanes;
+ guint16 bytesperline;
+ guint16 palettetype;
+ guint16 hscrsize;
+ guint16 vscrsize;
+ guint8 filler[54];
+};
+
+struct pcx_context {
+ GdkPixbuf *pixbuf;
+ gint rowstride;
+
+ GdkPixbufModuleSizeFunc size_func;
+ GdkPixbufModuleUpdatedFunc updated_func;
+ GdkPixbufModulePreparedFunc prepared_func;
+ gpointer user_data;
+
+ guchar current_task;
+
+ gboolean header_loaded;
+ struct pcx_header *header;
+ guint bpp;
+ gint width, height;
+ guint num_planes;
+ guint bytesperline;
+
+ guchar *buf;
+ guint buf_size;
+ guint buf_pos;
+ guchar *data;
+ guchar *line;
+ guint current_line;
+ guchar *p_data;
+};
+
+/*
+ * set context's image information based on the header
+ */
+static void
+fill_pcx_context(struct pcx_context *context)
+{
+ struct pcx_header *header = context->header;
+
+ context->bpp = header->bitsperpixel;
+ context->width = header->xmax - header->xmin + 1;
+ context->height = header->ymax - header->ymin + 1;
+ context->num_planes = header->colorplanes;
+ context->bytesperline = header->bytesperline;
+
+ if(header->version == 5 && context->bpp == 8 && context->num_planes == 3)
+ context->bpp = 24;
+}
+
+static void
+free_pcx_context(struct pcx_context *context, gboolean unref_pixbuf)
+{
+ g_free(context->header);
+ g_free(context->buf);
+ if(unref_pixbuf && context->pixbuf)
+ g_object_unref(context->pixbuf);
+ g_free(context->line);
+ g_free(context->p_data);
+
+ g_free(context);
+}
+
+/*
+ * read each plane of a single scanline. store_planes is
+ * the number of planes that can be stored in the planes array.
+ * data is the pointer to the block of memory to read
+ * from, size is the length of that data, and line_bytes
+ * is where the number of bytes read will be stored.
+ */
+static gboolean
+read_scanline_data(guchar *data, guint size, guchar *planes[],
+ guint store_planes, guint num_planes, guint bytesperline,
+ guint *line_bytes)
+{
+ guint i, j;
+ guint p, count;
+ guint d = 0;
+ guint8 byte;
+
+ for(p = 0; p < num_planes; p++) {
+ for(i = 0; i < bytesperline;) { /* i incremented when line byte set */
+ if(d >= size)
+ return FALSE;
+ byte = data[d++];
+
+ if(byte >> 6 == 0x3) {
+ count = byte & ~(0x3 << 6);
+ if(count == 0)
+ return FALSE;
+ if(d >= size)
+ return FALSE;
+ byte = data[d++];
+ } else {
+ count = 1;
+ }
+
+ for(j = 0; j < count; j++) {
+ if(p < store_planes)
+ planes[p][i++] = byte;
+ else
+ i++;
+
+ if(i >= bytesperline) {
+ p++;
+ if(p >= num_planes) {
+ *line_bytes = d;
+ return TRUE;
+ }
+ i = 0;
+ }
+ }
+ }
+ }
+
+ *line_bytes = d; /* number of bytes read for scanline */
+ return TRUE;
+}
+
+static gpointer
+gdk_pixbuf__pcx_begin_load(GdkPixbufModuleSizeFunc size_func,
+ GdkPixbufModulePreparedFunc prepared_func,
+ GdkPixbufModuleUpdatedFunc updated_func,
+ gpointer user_data, GError **error)
+{
+ struct pcx_context *context;
+
+ context = g_new0(struct pcx_context, 1);
+ if(!context)
+ return NULL;
+
+ context->header = g_try_malloc(sizeof(struct pcx_header));
+ if(!context->header) {
+ g_free(context);
+ g_set_error_literal(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't allocate memory for header"));
+ return NULL;
+ }
+
+ context->size_func = size_func;
+ context->updated_func = updated_func;
+ context->prepared_func = prepared_func;
+ context->user_data = user_data;
+
+ context->current_task = PCX_TASK_LOAD_HEADER;
+
+ context->buf = g_try_malloc(sizeof(guchar) * 512);
+ if(!context->buf) {
+ g_free(context->header);
+ g_free(context);
+ g_set_error_literal(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't allocate memory for context buffer"));
+ return NULL;
+ }
+ context->buf_size = 512;
+
+ context->header_loaded = FALSE;
+
+ return context;
+}
+
+static gboolean
+pcx_resize_context_buf(struct pcx_context *context, guint size)
+{
+ guchar *new_buf;
+
+ new_buf = g_try_realloc(context->buf, size);
+ if(!new_buf)
+ return FALSE;
+
+ context->buf = new_buf;
+ context->buf_size = size;
+ return TRUE;
+}
+
+/*
+ * remove a number of bytes (specified by size) from the
+ * beginning of a context's buf
+ */
+static gboolean
+pcx_chop_context_buf(struct pcx_context *context, guint size)
+{
+ guint i, j;
+
+ if (size > context->buf_pos)
+ return FALSE;
+ else if (size == 0)
+ return TRUE;
+
+ for (i = 0, j = size; j < context->buf_pos; i++, j++)
+ context->buf[i] = context->buf[j];
+
+ context->buf_pos -= size;
+
+ return TRUE;
+}
+
+static guchar
+read_pixel_1(guchar *data, guint offset)
+{
+ guchar retval;
+ guint bit_offset;
+
+ if(!(offset % 8)) {
+ offset /= 8;
+ retval = data[offset] >> 7;
+ } else {
+ bit_offset = offset % 8;
+ offset -= bit_offset;
+ offset /= 8;
+ retval = (data[offset] >> (7 - bit_offset)) & 0x1;
+ }
+
+ return retval;
+}
+
+static guchar
+read_pixel_4(guchar *data, guint offset)
+{
+ guchar retval;
+
+ if(!(offset % 2)) {
+ offset /= 2;
+ retval = data[offset] >> 4;
+ } else {
+ offset--;
+ offset /= 2;
+ retval = data[offset] & 0xf;
+ }
+
+ return retval;
+}
+
+static gboolean
+pcx_increment_load_data_1(struct pcx_context *context)
+{
+ guint i;
+ guchar *planes[4];
+ guint line_bytes;
+ guint store_planes;
+
+ if(context->num_planes == 4) {
+ planes[0] = context->line;
+ planes[1] = planes[0] + context->bytesperline;
+ planes[2] = planes[1] + context->bytesperline;
+ planes[3] = planes[2] + context->bytesperline;
+ store_planes = 4;
+ } else if(context->num_planes == 3) {
+ planes[0] = context->line;
+ planes[1] = planes[0] + context->bytesperline;
+ planes[2] = planes[1] + context->bytesperline;
+ store_planes = 3;
+ } else if(context->num_planes == 2) {
+ planes[0] = context->line;
+ planes[1] = planes[0] + context->bytesperline;
+ store_planes = 2;
+ } else if(context->num_planes == 1) {
+ planes[0] = context->line;
+ store_planes = 1;
+ } else {
+ return FALSE;
+ }
+
+ while(read_scanline_data(context->buf, context->buf_pos, planes, store_planes, context->num_planes, context->bytesperline, &line_bytes)) {
+ pcx_chop_context_buf(context, line_bytes);
+
+ for(i = 0; i < context->width; i++) {
+ guchar p;
+
+ if(context->num_planes == 4) {
+ p = read_pixel_1(planes[3], i);
+ p <<= 1;
+ p |= read_pixel_1(planes[2], i);
+ p <<= 1;
+ p |= read_pixel_1(planes[1], i);
+ p <<= 1;
+ p |= read_pixel_1(planes[0], i);
+ } else if(context->num_planes == 3) {
+ p = read_pixel_1(planes[2], i);
+ p <<= 1;
+ p |= read_pixel_1(planes[1], i);
+ p <<= 1;
+ p |= read_pixel_1(planes[0], i);
+ } else if(context->num_planes == 2) {
+ p = read_pixel_1(planes[1], i);
+ p <<= 1;
+ p |= read_pixel_1(planes[0], i);
+ } else if(context->num_planes == 1) {
+ p = read_pixel_1(planes[0], i);
+ } else {
+ return FALSE;
+ }
+ p &= 0xf;
+ context->data[context->current_line * context->rowstride + i * 3 + 0] = context->header->palette[p * 3 + 0];
+ context->data[context->current_line * context->rowstride + i * 3 + 1] = context->header->palette[p * 3 + 1];
+ context->data[context->current_line * context->rowstride + i * 3 + 2] = context->header->palette[p * 3 + 2];
+ }
+
+ if(context->updated_func)
+ context->updated_func(context->pixbuf, 0, context->current_line, context->width, 1, context->user_data);
+
+ context->current_line++;
+
+ if(context->current_line == context->height) {
+ context->current_task = PCX_TASK_DONE;
+ return TRUE;
+ }
+ }
+
+ return TRUE;
+}
+
+static gboolean
+pcx_increment_load_data_2(struct pcx_context *context)
+{
+ guint i;
+ guchar *planes[1];
+ guint line_bytes;
+ guint shift, h;
+
+ planes[0] = context->line;
+
+ while(read_scanline_data(context->buf, context->buf_pos, planes, 1, context->num_planes, context->bytesperline, &line_bytes)) {
+ pcx_chop_context_buf(context, line_bytes);
+
+ for(i = 0; i < context->width; i++) {
+ shift = 6 - 2 * (i % 4);
+ h = (planes[0][i / 4] >> shift) & 0x3;
+ context->data[context->current_line * context->rowstride + i * 3 + 0] = context->header->palette[h * 3 + 0];
+ context->data[context->current_line * context->rowstride + i * 3 + 1] = context->header->palette[h * 3 + 1];
+ context->data[context->current_line * context->rowstride + i * 3 + 2] = context->header->palette[h * 3 + 2];
+ }
+
+ if(context->updated_func)
+ context->updated_func(context->pixbuf, 0, context->current_line, context->width, 1, context->user_data);
+
+ context->current_line++;
+
+ if(context->current_line == context->height) {
+ context->current_task = PCX_TASK_DONE;
+ return TRUE;
+ }
+ }
+
+ return TRUE;
+}
+
+static gboolean
+pcx_increment_load_data_4(struct pcx_context *context)
+{
+ guint i;
+ guchar *planes[1];
+ guint line_bytes;
+
+ planes[0] = context->line;
+
+ while(read_scanline_data(context->buf, context->buf_pos, planes, 1, context->num_planes, context->bytesperline, &line_bytes)) {
+ pcx_chop_context_buf(context, line_bytes);
+
+ for(i = 0; i < context->width; i++) {
+ guchar p;
+
+ p = read_pixel_4(planes[0], i) & 0xf;
+ context->data[context->current_line * context->rowstride + i * 3 + 0] = context->header->palette[p * 3 + 0];
+ context->data[context->current_line * context->rowstride + i * 3 + 1] = context->header->palette[p * 3 + 1];
+ context->data[context->current_line * context->rowstride + i * 3 + 2] = context->header->palette[p * 3 + 2];
+ }
+
+ if(context->updated_func)
+ context->updated_func(context->pixbuf, 0, context->current_line, context->width, 1, context->user_data);
+
+ context->current_line++;
+
+ if(context->current_line == context->height) {
+ context->current_task = PCX_TASK_DONE;
+ return TRUE;
+ }
+ }
+
+ return TRUE;
+}
+
+/*
+ * for loading 8-bit pcx images, we keep a buffer containing
+ * each pixel's palette number; once we've loaded each scanline,
+ * we wait for loading to stop and call pcx_load_palette_8,
+ * which finds the palette at the end of the pcx data and sets the
+ * RGB data.
+ */
+static gboolean
+pcx_increment_load_data_8(struct pcx_context *context)
+{
+ guint i;
+ guchar *planes[1];
+ guint line_bytes;
+
+ planes[0] = context->line;
+
+ while(read_scanline_data(context->buf, context->buf_pos, planes, 1, context->num_planes, context->bytesperline, &line_bytes)) {
+ pcx_chop_context_buf(context, line_bytes);
+
+ for(i = 0; i < context->width; i++)
+ context->p_data[context->current_line * context->width + i + 0] = planes[0][i];
+
+ context->current_line++;
+
+ if(context->current_line == context->height) {
+ context->current_task = PCX_TASK_LOAD_PALETTE;
+ return TRUE;
+ }
+ }
+
+ return TRUE;
+}
+
+/*
+ * read the palette and set the RGB data
+ */
+static gboolean
+pcx_load_palette_8(struct pcx_context *context)
+{
+ guint i, j;
+
+ if(context->current_line < context->height)
+ return FALSE;
+
+ if(context->buf_pos >= 769) {
+ guchar *palette = context->buf + (context->buf_pos - 769);
+
+ if(palette[0] == 12) {
+ palette++;
+ for(i = 0; i < context->height; i++) {
+ for(j = 0; j < context->width; j++) {
+ context->data[i * context->rowstride + j * 3 + 0] = palette[(context->p_data[i * context->width + j]) * 3 + 0];
+ context->data[i * context->rowstride + j * 3 + 1] = palette[(context->p_data[i * context->width + j]) * 3 + 1];
+ context->data[i * context->rowstride + j * 3 + 2] = palette[(context->p_data[i * context->width + j]) * 3 + 2];
+ }
+
+ if(context->updated_func)
+ context->updated_func(context->pixbuf, 0, i, context->width, 1, context->user_data);
+ }
+
+#ifdef PCX_DEBUG
+ g_print("read palette\n");
+#endif
+
+ context->current_task = PCX_TASK_DONE;
+ return TRUE;
+ } else {
+#ifdef PCX_DEBUG
+ g_print("this ain't a palette\n");
+#endif
+ return FALSE;
+ }
+ }
+
+ return FALSE;
+}
+
+/*
+ * in 24-bit images, each scanline has three color planes
+ * for red, green, and blue, respectively.
+ */
+static gboolean
+pcx_increment_load_data_24(struct pcx_context *context)
+{
+ guint i;
+ guchar *planes[3];
+ guint line_bytes;
+
+ planes[0] = context->line;
+ planes[1] = planes[0] + context->bytesperline;
+ planes[2] = planes[1] + context->bytesperline;
+
+ while(read_scanline_data(context->buf, context->buf_pos, planes, 3, context->num_planes, context->bytesperline, &line_bytes)) {
+ pcx_chop_context_buf(context, line_bytes);
+
+ for(i = 0; i < context->width; i++) {
+ context->data[context->current_line * context->rowstride + i * 3 + 0] = planes[0][i];
+ context->data[context->current_line * context->rowstride + i * 3 + 1] = planes[1][i];
+ context->data[context->current_line * context->rowstride + i * 3 + 2] = planes[2][i];
+ }
+
+ if(context->updated_func)
+ context->updated_func(context->pixbuf, 0, context->current_line, context->width, 1, context->user_data);
+
+ context->current_line++;
+
+ if(context->current_line == context->height) {
+ context->current_task = PCX_TASK_DONE;
+ return TRUE;
+ }
+ }
+
+ return TRUE;
+}
+
+static gboolean
+gdk_pixbuf__pcx_load_increment(gpointer data, const guchar *buf, guint size,
+ GError **error)
+{
+ struct pcx_context *context = (struct pcx_context *)data;
+ struct pcx_header *header;
+ guint i;
+ gboolean retval = TRUE;
+
+ /* if context's buf isn't large enough to hold its current data plus the passed buf, increase its size */
+ if(context->buf_pos + size > context->buf_size) {
+ if(!pcx_resize_context_buf(context, sizeof(guchar) * (context->buf_pos + size))) {
+ g_set_error_literal(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't allocate memory for context buffer"));
+ return FALSE;
+ }
+ }
+
+ for(i = 0; i < size; i++)
+ context->buf[context->buf_pos++] = buf[i];
+
+ if(context->current_task == PCX_TASK_LOAD_HEADER) {
+ if(!context->header_loaded && context->buf_pos > sizeof(struct pcx_header)) { /* set header */
+ gint width, height;
+
+ memcpy(context->header, context->buf, sizeof(struct pcx_header));
+ pcx_chop_context_buf(context, sizeof(struct pcx_header));
+ header = context->header;
+
+ /* convert the multi-byte header variables that will be used */
+ header->xmin = GINT16_FROM_LE(header->xmin);
+ header->ymin = GINT16_FROM_LE(header->ymin);
+ header->xmax = GINT16_FROM_LE(header->xmax);
+ header->ymax = GINT16_FROM_LE(header->ymax);
+ header->bytesperline = GUINT16_FROM_LE(header->bytesperline);
+
+#ifdef PCX_DEBUG
+ g_print ("Manufacturer %d\n"
+ "Version %d\n"
+ "Encoding %d\n"
+ "Bits/Pixel %d\n"
+ "Planes %d\n"
+ "Palette %d\n",
+ header->manufacturer, header->version,
+ header->encoding, header->bitsperpixel,
+ header->colorplanes, header->palettetype);
+#endif
+
+ context->header_loaded = TRUE;
+ fill_pcx_context(context);
+
+ width = context->width;
+ height = context->height;
+ if(width <= 0 || height <= 0) {
+ g_set_error_literal(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE, _("Image has invalid width and/or height"));
+ return FALSE;
+ }
+ if (context->size_func)
+ {
+ (*context->size_func) (&width, &height, context->user_data);
+ if (width == 0 || height == 0)
+ return TRUE;
+ }
+
+ switch(context->bpp) {
+ default:
+ g_set_error_literal(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE, _("Image has unsupported bpp"));
+ return FALSE;
+ break;
+ case 1:
+ if(context->num_planes < 1 || context->num_planes > 4) {
+ g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE, _("Image has unsupported number of %d-bit planes"), 1);
+ return FALSE;
+ }
+ break;
+ case 2:
+ case 4:
+ case 8:
+ if(context->num_planes != 1) {
+ g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE, _("Image has unsupported number of %d-bit planes"), (gint)context->bpp);
+ return FALSE;
+ }
+ break;