/
gdk-pixbuf-io.c
3388 lines (2970 loc) · 111 KB
/
gdk-pixbuf-io.c
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
/* GdkPixbuf library - Main loading interface.
*
* Copyright (C) 1999 The Free Software Foundation
*
* Authors: Miguel de Icaza <miguel@gnu.org>
* Federico Mena-Quintero <federico@gimp.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <glib.h>
#include <gio/gio.h>
#include "gdk-pixbuf-private.h"
#include "gdk-pixbuf-loader.h"
#include "gdk-pixdata.h"
#include <glib/gstdio.h>
#ifdef G_OS_WIN32
#define STRICT
#include <windows.h>
#undef STRICT
#endif
#ifdef OS_DARWIN
#include <mach-o/dyld.h>
#endif
/**
* SECTION:file-loading
* @Short_description: Loading a pixbuf from a file.
* @Title: File Loading
* @See_also: #GdkPixbufLoader.
*
* The GdkPixBuf library provides a simple mechanism for loading
* an image from a file in synchronous fashion. This means that the
* library takes control of the application while the file is being
* loaded; from the user's point of view, the application will block
* until the image is done loading.
*
*
* This interface can be used by applications in which blocking is
* acceptable while an image is being loaded. It can also be used to
* load small images in general. Applications that need progressive
* loading can use the #GdkPixbufLoader functionality instead.
*/
/**
* SECTION:file-saving
* @Short_description: Saving a pixbuf to a file.
* @Title: File saving
*
* These functions allow to save a #GdkPixbuf in a number of
* file formats. The formatted data can be written to a file
* or to a memory buffer. GdkPixBuf can also call a user-defined
* callback on the data, which allows to e.g. write the image
* to a socket or store it in a database.
*/
/**
* SECTION:module_interface
* @Short_description: Extending GdkPixBuf
* @Title: Module Interface
*
* If GdkPixBuf has been compiled with GModule support, it can be extended by
* modules which can load (and perhaps also save) new image and animation
* formats. Each loadable module must export a
* #GdkPixbufModuleFillInfoFunc function named `fill_info` and
* a #GdkPixbufModuleFillVtableFunc function named
* `fill_vtable`.
*
* In order to make format-checking work before actually loading the modules
* (which may require dlopening image libraries), modules export their
* signatures (and other information) via the `fill_info` function. An
* external utility, gdk-pixbuf-query-loaders, uses this to create a text
* file containing a list of all available loaders and their signatures.
* This file is then read at runtime by GdkPixBuf to obtain the list of
* available loaders and their signatures.
*
* Modules may only implement a subset of the functionality available via
* #GdkPixbufModule. If a particular functionality is not implemented, the
* `fill_vtable` function will simply not set the corresponding
* function pointers of the #GdkPixbufModule structure. If a module supports
* incremental loading (i.e. provides #begin_load, #stop_load and
* #load_increment), it doesn't have to implement #load, since GdkPixBuf can
* supply a generic #load implementation wrapping the incremental loading.
*
* Installing a module is a two-step process:
* - copy the module file(s) to the loader directory (normally
* `$libdir/gdk-pixbuf-2.0/$version/loaders`, unless overridden by the
* environment variable `GDK_PIXBUF_MODULEDIR`)
* - call gdk-pixbuf-query-loaders to update the module file (normally
* `$libdir/gdk-pixbuf-2.0/$version/loaders.cache`, unless overridden by the
* environment variable `GDK_PIXBUF_MODULE_FILE`)
*
* The GdkPixBuf interfaces needed for implementing modules are contained in
* `gdk-pixbuf-io.h` (and `gdk-pixbuf-animation.h` if the module supports
* animations). They are not covered by the same stability guarantees as the
* regular GdkPixBuf API. To underline this fact, they are protected by
* `#ifdef GDK_PIXBUF_ENABLE_BACKEND`.
*/
static gint
format_check (GdkPixbufModule *module, guchar *buffer, int size)
{
int i, j;
gchar m;
GdkPixbufModulePattern *pattern;
gboolean anchored;
guchar *prefix;
gchar *mask;
for (pattern = module->info->signature; pattern->prefix; pattern++) {
if (pattern->mask && pattern->mask[0] == '*') {
prefix = (guchar *)pattern->prefix + 1;
mask = pattern->mask + 1;
anchored = FALSE;
}
else {
prefix = (guchar *)pattern->prefix;
mask = pattern->mask;
anchored = TRUE;
}
for (i = 0; i < size; i++) {
for (j = 0; i + j < size && prefix[j] != 0; j++) {
m = mask ? mask[j] : ' ';
if (m == ' ') {
if (buffer[i + j] != prefix[j])
break;
}
else if (m == '!') {
if (buffer[i + j] == prefix[j])
break;
}
else if (m == 'z') {
if (buffer[i + j] != 0)
break;
}
else if (m == 'n') {
if (buffer[i + j] == 0)
break;
}
}
if (prefix[j] == 0)
return pattern->relevance;
if (anchored)
break;
}
}
return 0;
}
G_LOCK_DEFINE_STATIC (init_lock);
static GSList *file_formats = NULL;
static void gdk_pixbuf_io_init (void);
static GSList *
get_file_formats (void)
{
G_LOCK (init_lock);
if (file_formats == NULL)
gdk_pixbuf_io_init ();
G_UNLOCK (init_lock);
return file_formats;
}
#ifdef G_OS_WIN32
/* DllMain function needed to tuck away the gdk-pixbuf DLL handle */
static HMODULE gdk_pixbuf_dll;
BOOL WINAPI
DllMain (HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved)
{
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
gdk_pixbuf_dll = (HMODULE) hinstDLL;
break;
}
return TRUE;
}
#endif
#ifdef GDK_PIXBUF_RELOCATABLE
gchar *
gdk_pixbuf_get_toplevel (void)
{
static gchar *toplevel = NULL;
if (toplevel == NULL) {
#if defined(G_OS_WIN32)
toplevel = g_win32_get_package_installation_directory_of_module (gdk_pixbuf_dll);
#elif defined(OS_DARWIN)
char pathbuf[PATH_MAX + 1];
uint32_t bufsize = sizeof(pathbuf);
gchar *bin_dir;
_NSGetExecutablePath(pathbuf, &bufsize);
bin_dir = g_dirname(pathbuf);
toplevel = g_build_path (G_DIR_SEPARATOR_S, bin_dir, "..", NULL);
g_free (bin_dir);
#elif defined (OS_LINUX)
gchar *exe_path, *bin_dir;
exe_path = g_file_read_link ("/proc/self/exe", NULL);
bin_dir = g_dirname(exe_path);
toplevel = g_build_path (G_DIR_SEPARATOR_S, bin_dir, "..", NULL);
g_free (exe_path);
g_free (bin_dir);
#else
#error "Relocations not supported for this platform"
#endif
}
return toplevel;
}
#endif /* GDK_PIXBUF_RELOCATABLE */
#ifdef USE_GMODULE
static gboolean
scan_string (const char **pos, GString *out)
{
const char *p = *pos, *q = *pos;
char *tmp, *tmp2;
gboolean quoted;
while (g_ascii_isspace (*p))
p++;
if (!*p)
return FALSE;
else if (*p == '"') {
p++;
quoted = FALSE;
for (q = p; (*q != '"') || quoted; q++) {
if (!*q)
return FALSE;
quoted = (*q == '\\') && !quoted;
}
tmp = g_strndup (p, q - p);
tmp2 = g_strcompress (tmp);
g_string_truncate (out, 0);
g_string_append (out, tmp2);
g_free (tmp);
g_free (tmp2);
}
q++;
*pos = q;
return TRUE;
}
static gboolean
scan_int (const char **pos, int *out)
{
int i = 0;
char buf[32];
const char *p = *pos;
while (g_ascii_isspace (*p))
p++;
if (*p < '0' || *p > '9')
return FALSE;
while ((*p >= '0') && (*p <= '9') && i < sizeof (buf)) {
buf[i] = *p;
i++;
p++;
}
if (i == sizeof (buf))
return FALSE;
else
buf[i] = '\0';
*out = atoi (buf);
*pos = p;
return TRUE;
}
static gboolean
skip_space (const char **pos)
{
const char *p = *pos;
while (g_ascii_isspace (*p))
p++;
*pos = p;
return !(*p == '\0');
}
#ifdef GDK_PIXBUF_RELOCATABLE
static char *
get_libdir (void)
{
static char *libdir = NULL;
if (libdir == NULL)
libdir = g_build_filename (gdk_pixbuf_get_toplevel (), "lib", NULL);
return libdir;
}
#undef GDK_PIXBUF_LIBDIR
#define GDK_PIXBUF_LIBDIR get_libdir()
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)
{
gchar *result = g_strdup (g_getenv ("GDK_PIXBUF_MODULE_FILE"));
if (!result)
result = g_build_filename (GDK_PIXBUF_LIBDIR, "gdk-pixbuf-2.0", GDK_PIXBUF_BINARY_VERSION, "loaders.cache", NULL);
return result;
}
#endif /* USE_GMODULE */
static gboolean
gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
GError **error);
static void
gdk_pixbuf_io_init (void)
{
#ifdef USE_GMODULE
GIOChannel *channel;
gchar *line_buf;
gsize term;
GString *tmp_buf = g_string_new (NULL);
gboolean have_error = FALSE;
GdkPixbufModule *module = NULL;
gchar *filename = gdk_pixbuf_get_module_file ();
int flags;
int n_patterns = 0;
GdkPixbufModulePattern *pattern;
GError *error = NULL;
#endif
GdkPixbufModule *builtin_module ;
/* initialize on separate line to avoid compiler warnings in the
* common case of no compiled-in modules.
*/
builtin_module = NULL;
#define load_one_builtin_module(format) \
builtin_module = g_new0 (GdkPixbufModule, 1); \
builtin_module->module_name = #format; \
if (gdk_pixbuf_load_module_unlocked (builtin_module, NULL)) \
file_formats = g_slist_prepend (file_formats, builtin_module);\
else \
g_free (builtin_module)
#ifdef INCLUDE_ani
load_one_builtin_module (ani);
#endif
#ifdef INCLUDE_png
load_one_builtin_module (png);
#endif
#ifdef INCLUDE_bmp
load_one_builtin_module (bmp);
#endif
#ifdef INCLUDE_gif
load_one_builtin_module (gif);
#endif
#ifdef INCLUDE_ico
load_one_builtin_module (ico);
#endif
#ifdef INCLUDE_jpeg
load_one_builtin_module (jpeg);
#endif
#ifdef INCLUDE_pnm
load_one_builtin_module (pnm);
#endif
#ifdef INCLUDE_tiff
load_one_builtin_module (tiff);
#endif
#ifdef INCLUDE_xpm
load_one_builtin_module (xpm);
#endif
#ifdef INCLUDE_xbm
load_one_builtin_module (xbm);
#endif
#ifdef INCLUDE_tga
load_one_builtin_module (tga);
#endif
#ifdef INCLUDE_icns
load_one_builtin_module (icns);
#endif
#ifdef INCLUDE_jasper
load_one_builtin_module (jasper);
#endif
#ifdef INCLUDE_qtif
load_one_builtin_module (qtif);
#endif
#ifdef INCLUDE_gdiplus
/* We don't bother having the GDI+ loaders individually selectable
* for building in or not.
*/
load_one_builtin_module (ico);
load_one_builtin_module (wmf);
load_one_builtin_module (emf);
load_one_builtin_module (bmp);
load_one_builtin_module (gif);
load_one_builtin_module (jpeg);
load_one_builtin_module (tiff);
#endif
#ifdef INCLUDE_gdip_png
/* Except the gdip-png loader which normally isn't built at all even */
load_one_builtin_module (png);
#endif
#undef load_one_builtin_module
#ifdef USE_GMODULE
channel = g_io_channel_new_file (filename, "r", &error);
if (!channel) {
/* Don't bother warning if we have some built-in loaders */
if (file_formats == NULL || file_formats->next == NULL)
g_warning ("Cannot open pixbuf loader module file '%s': %s\n\n"
"This likely means that your installation is broken.\n"
"Try running the command\n"
" gdk-pixbuf-query-loaders > %s\n"
"to make things work again for the time being.",
filename, error->message, filename);
g_string_free (tmp_buf, TRUE);
g_free (filename);
return;
}
while (!have_error && g_io_channel_read_line (channel, &line_buf, NULL, &term, NULL) == G_IO_STATUS_NORMAL) {
const char *p;
p = line_buf;
line_buf[term] = 0;
if (!skip_space (&p)) {
/* Blank line marking the end of a module
*/
if (module && *p != '#') {
#ifdef GDK_PIXBUF_RELOCATABLE
correct_prefix (&module->module_path);
#endif
file_formats = g_slist_prepend (file_formats, module);
module = NULL;
}
goto next_line;
}
if (*p == '#')
goto next_line;
if (!module) {
/* Read a module location
*/
module = g_new0 (GdkPixbufModule, 1);
n_patterns = 0;
if (!scan_string (&p, tmp_buf)) {
g_warning ("Error parsing loader info in '%s'\n %s",
filename, line_buf);
have_error = TRUE;
}
module->module_path = g_strdup (tmp_buf->str);
}
else if (!module->module_name) {
module->info = g_new0 (GdkPixbufFormat, 1);
if (!scan_string (&p, tmp_buf)) {
g_warning ("Error parsing loader info in '%s'\n %s",
filename, line_buf);
have_error = TRUE;
}
module->info->name = g_strdup (tmp_buf->str);
module->module_name = module->info->name;
if (!scan_int (&p, &flags)) {
g_warning ("Error parsing loader info in '%s'\n %s",
filename, line_buf);
have_error = TRUE;
}
module->info->flags = flags;
if (!scan_string (&p, tmp_buf)) {
g_warning ("Error parsing loader info in '%s'\n %s",
filename, line_buf);
have_error = TRUE;
}
if (tmp_buf->str[0] != 0)
module->info->domain = g_strdup (tmp_buf->str);
if (!scan_string (&p, tmp_buf)) {
g_warning ("Error parsing loader info in '%s'\n %s",
filename, line_buf);
have_error = TRUE;
}
module->info->description = g_strdup (tmp_buf->str);
if (scan_string (&p, tmp_buf)) {
module->info->license = g_strdup (tmp_buf->str);
}
}
else if (!module->info->mime_types) {
int n = 1;
module->info->mime_types = g_new0 (gchar*, 1);
while (scan_string (&p, tmp_buf)) {
if (tmp_buf->str[0] != 0) {
module->info->mime_types =
g_realloc (module->info->mime_types, (n + 1) * sizeof (gchar*));
module->info->mime_types[n - 1] = g_strdup (tmp_buf->str);
module->info->mime_types[n] = NULL;
n++;
}
}
}
else if (!module->info->extensions) {
int n = 1;
module->info->extensions = g_new0 (gchar*, 1);
while (scan_string (&p, tmp_buf)) {
if (tmp_buf->str[0] != 0) {
module->info->extensions =
g_realloc (module->info->extensions, (n + 1) * sizeof (gchar*));
module->info->extensions[n - 1] = g_strdup (tmp_buf->str);
module->info->extensions[n] = NULL;
n++;
}
}
}
else {
n_patterns++;
module->info->signature = (GdkPixbufModulePattern *)
g_realloc (module->info->signature, (n_patterns + 1) * sizeof (GdkPixbufModulePattern));
pattern = module->info->signature + n_patterns;
pattern->prefix = NULL;
pattern->mask = NULL;
pattern->relevance = 0;
pattern--;
if (!scan_string (&p, tmp_buf))
goto context_error;
pattern->prefix = g_strdup (tmp_buf->str);
if (!scan_string (&p, tmp_buf))
goto context_error;
if (*tmp_buf->str)
pattern->mask = g_strdup (tmp_buf->str);
else
pattern->mask = NULL;
if (!scan_int (&p, &pattern->relevance))
goto context_error;
goto next_line;
context_error:
g_free (pattern->prefix);
g_free (pattern->mask);
g_free (pattern);
g_warning ("Error parsing loader info in '%s'\n %s",
filename, line_buf);
have_error = TRUE;
}
next_line:
g_free (line_buf);
}
g_string_free (tmp_buf, TRUE);
g_io_channel_unref (channel);
g_free (filename);
#endif
}
#define module(type) \
extern void _gdk_pixbuf__##type##_fill_info (GdkPixbufFormat *info); \
extern void _gdk_pixbuf__##type##_fill_vtable (GdkPixbufModule *module)
module (png);
module (jpeg);
module (gif);
module (ico);
module (ani);
module (xpm);
module (tiff);
module (pnm);
module (bmp);
module (xbm);
module (tga);
module (icns);
module (jasper);
module (qtif);
module (gdip_ico);
module (gdip_wmf);
module (gdip_emf);
module (gdip_bmp);
module (gdip_gif);
module (gdip_jpeg);
module (gdip_png);
module (gdip_tiff);
#undef module
/* actually load the image handler - gdk_pixbuf_get_module only get a */
/* reference to the module to load, it doesn't actually load it */
/* perhaps these actions should be combined in one function */
static gboolean
gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
GError **error)
{
GdkPixbufModuleFillInfoFunc fill_info = NULL;
GdkPixbufModuleFillVtableFunc fill_vtable = NULL;
if (image_module->module != NULL)
return TRUE;
#define try_module(format,id) \
if (fill_info == NULL && \
strcmp (image_module->module_name, #format) == 0) { \
fill_info = _gdk_pixbuf__##id##_fill_info; \
fill_vtable = _gdk_pixbuf__##id##_fill_vtable; \
}
#ifdef INCLUDE_gdiplus
try_module (ico,gdip_ico);
try_module (wmf,gdip_wmf);
try_module (emf,gdip_emf);
try_module (bmp,gdip_bmp);
try_module (gif,gdip_gif);
try_module (jpeg,gdip_jpeg);
try_module (tiff,gdip_tiff);
#endif
#ifdef INCLUDE_gdip_png
try_module (png,gdip_png);
#endif
#ifdef INCLUDE_png
try_module (png,png);
#endif
#ifdef INCLUDE_bmp
try_module (bmp,bmp);
#endif
#ifdef INCLUDE_gif
try_module (gif,gif);
#endif
#ifdef INCLUDE_ico
try_module (ico,ico);
#endif
#ifdef INCLUDE_ani
try_module (ani,ani);
#endif
#ifdef INCLUDE_jpeg
try_module (jpeg,jpeg);
#endif
#ifdef INCLUDE_pnm
try_module (pnm,pnm);
#endif
#ifdef INCLUDE_tiff
try_module (tiff,tiff);
#endif
#ifdef INCLUDE_xpm
try_module (xpm,xpm);
#endif
#ifdef INCLUDE_xbm
try_module (xbm,xbm);
#endif
#ifdef INCLUDE_tga
try_module (tga,tga);
#endif
#ifdef INCLUDE_icns
try_module (icns,icns);
#endif
#ifdef INCLUDE_jasper
try_module (jasper,jasper);
#endif
#ifdef INCLUDE_qtif
try_module (qtif,qtif);
#endif
#undef try_module
if (fill_vtable) {
image_module->module = (void *) 1;
(* fill_vtable) (image_module);
if (image_module->info == NULL) {
image_module->info = g_new0 (GdkPixbufFormat, 1);
(* fill_info) (image_module->info);
}
return TRUE;
}
else
#ifdef USE_GMODULE
{
char *path;
GModule *module;
gpointer sym;
path = image_module->module_path;
module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
if (!module) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
_("Unable to load image-loading module: %s: %s"),
path, g_module_error ());
return FALSE;
}
image_module->module = module;
if (g_module_symbol (module, "fill_vtable", &sym)) {
fill_vtable = (GdkPixbufModuleFillVtableFunc) sym;
(* fill_vtable) (image_module);
return TRUE;
} else {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
_("Image-loading module %s does not export the proper interface; perhaps it's from a different gdk-pixbuf version?"),
path);
return FALSE;
}
}
#else
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
_("Image type '%s' is not supported"),
image_module->module_name);
return FALSE;
#endif /* !USE_GMODULE */
}
gboolean
_gdk_pixbuf_load_module (GdkPixbufModule *image_module,
GError **error)
{
gboolean ret;
G_LOCK (init_lock);
ret = gdk_pixbuf_load_module_unlocked (image_module, error);
G_UNLOCK (init_lock);
return ret;
}
GdkPixbufModule *
_gdk_pixbuf_get_named_module (const char *name,
GError **error)
{
GSList *modules;
for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
if (module->info->disabled)
continue;
if (!strcmp (name, module->module_name))
return module;
}
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
_("Image type '%s' is not supported"),
name);
return NULL;
}
GdkPixbufModule *
_gdk_pixbuf_get_module (guchar *buffer, guint size,
const gchar *filename,
GError **error)
{
GSList *modules;
GdkPixbufModule *selected = NULL;
gchar *display_name = NULL;
#ifdef GDK_PIXBUF_USE_GIO_MIME
gchar *mime_type;
gchar **mimes;
gchar *type;
gint j;
gboolean uncertain;
mime_type = g_content_type_guess (NULL, buffer, size, &uncertain);
if ((uncertain || g_str_equal (mime_type, "text/plain") || g_str_equal (mime_type, "application/gzip")) && filename != NULL) {
g_free (mime_type);
mime_type = g_content_type_guess (filename, buffer, size, NULL);
}
for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
GdkPixbufFormat *info = module->info;
if (info->disabled)
continue;
mimes = info->mime_types;
for (j = 0; mimes[j] != NULL; j++) {
type = g_content_type_from_mime_type (mimes[j]);
if (g_content_type_equals (type, mime_type)) {
g_free (type);
selected = module;
break;
}
g_free (type);
}
if (selected != NULL)
break;
/* Make sure the builtin GdkPixdata support works even without mime sniffing */
if (strcmp (info->name, "GdkPixdata") == 0 &&
format_check (module, buffer, size) == 100) {
selected = module;
break;
}
}
g_free (mime_type);
#else
gint score, best = 0;
for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
if (module->info->disabled)
continue;
score = format_check (module, buffer, size);
if (score > best) {
best = score;
selected = module;
}
if (score >= 100)
break;
}
#endif
if (selected != NULL)
return selected;
if (filename)
{
display_name = g_filename_display_name (filename);
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
_("Couldn't recognize the image file format for file '%s'"),
display_name);
g_free (display_name);
}
else
g_set_error_literal (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
_("Unrecognized image file format"));
return NULL;
}
static
GdkPixbufModule *
_gdk_pixbuf_get_module_for_file (FILE *f, const gchar *filename, GError **error)
{
guchar buffer[SNIFF_BUFFER_SIZE];
int size;
size = fread (&buffer, 1, sizeof (buffer), f);
if (size == 0) {
gchar *display_name;
display_name = g_filename_display_name (filename);
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
_("Image file '%s' contains no data"),
display_name);
g_free (display_name);
return NULL;
}
return _gdk_pixbuf_get_module (buffer, size, filename, error);
}
static void
prepared_notify (GdkPixbuf *pixbuf,
GdkPixbufAnimation *anim,
gpointer user_data)
{
if (pixbuf != NULL)
g_object_ref (pixbuf);
*((GdkPixbuf **)user_data) = pixbuf;
}
static GdkPixbuf *
generic_load_incrementally (GdkPixbufModule *module, FILE *f, GError **error)
{
GdkPixbuf *pixbuf = NULL;
gpointer context;
context = module->begin_load (NULL, prepared_notify, NULL, &pixbuf, error);
if (!context)
goto out;
while (!feof (f) && !ferror (f)) {
guchar buffer[LOAD_BUFFER_SIZE];
size_t length;
length = fread (buffer, 1, sizeof (buffer), f);
if (length > 0) {
if (!module->load_increment (context, buffer, length, error)) {
module->stop_load (context, NULL);
if (pixbuf != NULL) {
g_object_unref (pixbuf);
pixbuf = NULL;
}
goto out;
}
}
}