1
/* -*- mode: C; c-file-style: "linux" -*- */
2
/* GdkPixbuf library - Main loading interface.
4
* Copyright (C) 1999 The Free Software Foundation
6
* Authors: Miguel de Icaza <miguel@gnu.org>
7
* Federico Mena-Quintero <federico@gimp.org>
9
* This library is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU Lesser General Public
11
* License as published by the Free Software Foundation; either
12
* version 2 of the License, or (at your option) any later version.
14
* This library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
* Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the
21
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22
* Boston, MA 02111-1307, USA.
38
#include "gdk-pixbuf-private.h"
39
#include "gdk-pixbuf-loader.h"
41
#include <glib/gstdio.h>
49
#define SNIFF_BUFFER_SIZE 4096
50
#define LOAD_BUFFER_SIZE 65536
53
* SECTION:file-loading
54
* @Short_description: Loading a pixbuf from a file.
55
* @Title: File Loading
56
* @See_also: #GdkPixbufLoader.
58
* The &gdk-pixbuf; library provides a simple mechanism for loading
59
* an image from a file in synchronous fashion. This means that the
60
* library takes control of the application while the file is being
61
* loaded; from the user's point of view, the application will block
62
* until the image is done loading.
65
* This interface can be used by applications in which blocking is
66
* acceptable while an image is being loaded. It can also be used to
67
* load small images in general. Applications that need progressive
68
* loading can use the #GdkPixbufLoader functionality instead.
73
* @Short_description: Saving a pixbuf to a file.
76
* These functions allow to save a #GdkPixbuf in a number of
77
* file formats. The formatted data can be written to a file
78
* or to a memory buffer. &gdk-pixbuf; can also call a user-defined
79
* callback on the data, which allows to e.g. write the image
80
* to a socket or store it in a database.
84
* SECTION:module_interface
85
* @Short_description: Extending &gdk-pixbuf;
86
* @Title: Module Interface
88
* If &gdk-pixbuf; has been compiled with GModule support, it can be extended by
89
* modules which can load (and perhaps also save) new image and animation
90
* formats. Each loadable module must export a
91
* #GdkPixbufModuleFillInfoFunc function named <function>fill_info</function> and
92
* a #GdkPixbufModuleFillVtableFunc function named
93
* <function>fill_vtable</function>.
96
* In order to make format-checking work before actually loading the modules
97
* (which may require dlopening image libraries), modules export their
98
* signatures (and other information) via the <function>fill_info</function>
99
* function. An external utility, <command>gdk-pixbuf-query-loaders</command>,
100
* uses this to create a text file containing a list of all available loaders and
101
* their signatures. This file is then read at runtime by &gdk-pixbuf; to obtain
102
* the list of available loaders and their signatures.
105
* Modules may only implement a subset of the functionality available via
106
* #GdkPixbufModule. If a particular functionality is not implemented, the
107
* <function>fill_vtable</function> function will simply not set the corresponding
108
* function pointers of the #GdkPixbufModule structure. If a module supports
109
* incremental loading (i.e. provides #begin_load, #stop_load and
110
* #load_increment), it doesn't have to implement #load, since &gdk-pixbuf; can
111
* supply a generic #load implementation wrapping the incremental loading.
114
* Installing a module is a two-step process:
116
* <listitem><para>copy the module file(s) to the loader directory (normally
117
* <filename><replaceable>libdir</replaceable>/gtk-2.0/<replaceable>version</replaceable>/loaders</filename>,
118
* unless overridden by the environment variable
119
* <envar>GDK_PIXBUF_MODULEDIR</envar>)
121
* <listitem><para>call <command>gdk-pixbuf-query-loaders</command> to update the
122
* module file (normally
123
* <filename><replaceable>sysconfdir</replaceable>/gtk-2.0/gdk-pixbuf.loaders</filename>,
124
* unless overridden by the environment variable
125
* <envar>GDK_PIXBUF_MODULE_FILE</envar>)
130
* The &gdk-pixbuf; interfaces needed for implementing modules are contained in
131
* <filename>gdk-pixbuf-io.h</filename> (and
132
* <filename>gdk-pixbuf-animation.h</filename> if the module supports animations).
133
* They are not covered by the same stability guarantees as the regular
134
* &gdk-pixbuf; API. To underline this fact, they are protected by
135
* <literal>#ifdef GDK_PIXBUF_ENABLE_BACKEND</literal>.
139
#ifndef GDK_PIXBUF_USE_GIO_MIME
141
format_check (GdkPixbufModule *module, guchar *buffer, int size)
145
GdkPixbufModulePattern *pattern;
150
for (pattern = module->info->signature; pattern->prefix; pattern++) {
151
if (pattern->mask && pattern->mask[0] == '*') {
152
prefix = (guchar *)pattern->prefix + 1;
153
mask = pattern->mask + 1;
157
prefix = (guchar *)pattern->prefix;
158
mask = pattern->mask;
161
for (i = 0; i < size; i++) {
162
for (j = 0; i + j < size && prefix[j] != 0; j++) {
163
m = mask ? mask[j] : ' ';
165
if (buffer[i + j] != prefix[j])
169
if (buffer[i + j] == prefix[j])
173
if (buffer[i + j] != 0)
177
if (buffer[i + j] == 0)
183
return pattern->relevance;
193
G_LOCK_DEFINE_STATIC (init_lock);
194
G_LOCK_DEFINE_STATIC (threadunsafe_loader_lock);
197
_gdk_pixbuf_lock (GdkPixbufModule *image_module)
199
if (g_threads_got_initialized &&
200
!(image_module->info->flags & GDK_PIXBUF_FORMAT_THREADSAFE)) {
201
G_LOCK (threadunsafe_loader_lock);
210
_gdk_pixbuf_unlock (GdkPixbufModule *image_module)
212
if (!(image_module->info->flags & GDK_PIXBUF_FORMAT_THREADSAFE)) {
213
G_UNLOCK (threadunsafe_loader_lock);
217
static GSList *file_formats = NULL;
219
static void gdk_pixbuf_io_init (void);
222
get_file_formats (void)
225
if (file_formats == NULL)
226
gdk_pixbuf_io_init ();
227
G_UNLOCK (init_lock);
234
/* DllMain function needed to tuck away the gdk-pixbuf DLL handle */
236
static HMODULE gdk_pixbuf_dll;
239
DllMain (HINSTANCE hinstDLL,
244
case DLL_PROCESS_ATTACH:
245
gdk_pixbuf_dll = (HMODULE) hinstDLL;
253
_gdk_pixbuf_win32_get_toplevel (void)
255
static char *toplevel = NULL;
257
if (toplevel == NULL)
258
toplevel = g_win32_get_package_installation_directory_of_module (gdk_pixbuf_dll);
268
scan_string (const char **pos, GString *out)
270
const char *p = *pos, *q = *pos;
274
while (g_ascii_isspace (*p))
279
else if (*p == '"') {
282
for (q = p; (*q != '"') || quoted; q++) {
285
quoted = (*q == '\\') && !quoted;
288
tmp = g_strndup (p, q - p);
289
tmp2 = g_strcompress (tmp);
290
g_string_truncate (out, 0);
291
g_string_append (out, tmp2);
303
scan_int (const char **pos, int *out)
307
const char *p = *pos;
309
while (g_ascii_isspace (*p))
312
if (*p < '0' || *p > '9')
315
while ((*p >= '0') && (*p <= '9') && i < sizeof (buf)) {
321
if (i == sizeof (buf))
334
skip_space (const char **pos)
336
const char *p = *pos;
338
while (g_ascii_isspace (*p))
343
return !(*p == '\0');
351
static char *libdir = NULL;
354
libdir = g_build_filename (_gdk_pixbuf_win32_get_toplevel (), "lib", NULL);
359
#undef GDK_PIXBUF_LIBDIR
360
#define GDK_PIXBUF_LIBDIR get_libdir()
363
correct_prefix (gchar **path)
365
if (strncmp (*path, GDK_PIXBUF_PREFIX "/", strlen (GDK_PIXBUF_PREFIX "/")) == 0 ||
366
strncmp (*path, GDK_PIXBUF_PREFIX "\\", strlen (GDK_PIXBUF_PREFIX "\\")) == 0)
369
if (strlen(*path) > 5 && strncmp (*path - 5, ".libs", 5) == 0)
371
/* We are being run from inside the build tree, and shouldn't mess about. */
375
/* This is an entry put there by gdk-pixbuf-query-loaders on the
376
* packager's system. On Windows a prebuilt gdk-pixbuf package can be
377
* installed in a random location. The gdk-pixbuf.loaders file
378
* distributed in such a package contains paths from the package
379
* builder's machine. Replace the build-time prefix with the
380
* installation prefix on this machine.
383
*path = g_strconcat (_gdk_pixbuf_win32_get_toplevel (), tem + strlen (GDK_PIXBUF_PREFIX), NULL);
388
#endif /* G_OS_WIN32 */
391
gdk_pixbuf_get_module_file (void)
393
gchar *result = g_strdup (g_getenv ("GDK_PIXBUF_MODULE_FILE"));
396
result = g_build_filename (GDK_PIXBUF_LIBDIR, "gdk-pixbuf-2.0", GDK_PIXBUF_BINARY_VERSION, "loaders.cache", NULL);
401
#endif /* USE_GMODULE */
405
gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
409
gdk_pixbuf_io_init (void)
415
GString *tmp_buf = g_string_new (NULL);
416
gboolean have_error = FALSE;
417
GdkPixbufModule *module = NULL;
418
gchar *filename = gdk_pixbuf_get_module_file ();
421
GdkPixbufModulePattern *pattern;
422
GError *error = NULL;
424
GdkPixbufModule *builtin_module ;
426
/* initialize on separate line to avoid compiler warnings in the
427
* common case of no compiled-in modules.
429
builtin_module = NULL;
431
#define load_one_builtin_module(format) \
432
builtin_module = g_new0 (GdkPixbufModule, 1); \
433
builtin_module->module_name = #format; \
434
if (gdk_pixbuf_load_module_unlocked (builtin_module, NULL)) \
435
file_formats = g_slist_prepend (file_formats, builtin_module);\
437
g_free (builtin_module)
440
load_one_builtin_module (ani);
443
load_one_builtin_module (png);
446
load_one_builtin_module (bmp);
449
load_one_builtin_module (wbmp);
452
load_one_builtin_module (gif);
455
load_one_builtin_module (ico);
458
load_one_builtin_module (jpeg);
461
load_one_builtin_module (pnm);
464
load_one_builtin_module (ras);
467
load_one_builtin_module (tiff);
470
load_one_builtin_module (xpm);
473
load_one_builtin_module (xbm);
476
load_one_builtin_module (tga);
479
load_one_builtin_module (pcx);
482
load_one_builtin_module (icns);
484
#ifdef INCLUDE_jasper
485
load_one_builtin_module (jasper);
488
load_one_builtin_module (qtif);
490
#ifdef INCLUDE_gdiplus
491
/* We don't bother having the GDI+ loaders individually selectable
492
* for building in or not.
494
load_one_builtin_module (ico);
495
load_one_builtin_module (wmf);
496
load_one_builtin_module (emf);
497
load_one_builtin_module (bmp);
498
load_one_builtin_module (gif);
499
load_one_builtin_module (jpeg);
500
load_one_builtin_module (tiff);
502
#ifdef INCLUDE_gdip_png
503
/* Except the gdip-png loader which normally isn't built at all even */
504
load_one_builtin_module (png);
507
#undef load_one_builtin_module
510
channel = g_io_channel_new_file (filename, "r", &error);
512
/* Don't bother warning if we have some built-in loaders */
513
if (file_formats == NULL)
514
g_warning ("Cannot open pixbuf loader module file '%s': %s",
515
filename, error->message);
516
g_string_free (tmp_buf, TRUE);
521
while (!have_error && g_io_channel_read_line (channel, &line_buf, NULL, &term, NULL) == G_IO_STATUS_NORMAL) {
528
if (!skip_space (&p)) {
529
/* Blank line marking the end of a module
531
if (module && *p != '#') {
533
correct_prefix (&module->module_path);
535
file_formats = g_slist_prepend (file_formats, module);
546
/* Read a module location
548
module = g_new0 (GdkPixbufModule, 1);
551
if (!scan_string (&p, tmp_buf)) {
552
g_warning ("Error parsing loader info in '%s'\n %s",
556
module->module_path = g_strdup (tmp_buf->str);
558
else if (!module->module_name) {
559
module->info = g_new0 (GdkPixbufFormat, 1);
560
if (!scan_string (&p, tmp_buf)) {
561
g_warning ("Error parsing loader info in '%s'\n %s",
565
module->info->name = g_strdup (tmp_buf->str);
566
module->module_name = module->info->name;
568
if (!scan_int (&p, &flags)) {
569
g_warning ("Error parsing loader info in '%s'\n %s",
573
module->info->flags = flags;
575
if (!scan_string (&p, tmp_buf)) {
576
g_warning ("Error parsing loader info in '%s'\n %s",
580
if (tmp_buf->str[0] != 0)
581
module->info->domain = g_strdup (tmp_buf->str);
583
if (!scan_string (&p, tmp_buf)) {
584
g_warning ("Error parsing loader info in '%s'\n %s",
588
module->info->description = g_strdup (tmp_buf->str);
590
if (scan_string (&p, tmp_buf)) {
591
module->info->license = g_strdup (tmp_buf->str);
594
else if (!module->info->mime_types) {
596
module->info->mime_types = g_new0 (gchar*, 1);
597
while (scan_string (&p, tmp_buf)) {
598
if (tmp_buf->str[0] != 0) {
599
module->info->mime_types =
600
g_realloc (module->info->mime_types, (n + 1) * sizeof (gchar*));
601
module->info->mime_types[n - 1] = g_strdup (tmp_buf->str);
602
module->info->mime_types[n] = NULL;
607
else if (!module->info->extensions) {
609
module->info->extensions = g_new0 (gchar*, 1);
610
while (scan_string (&p, tmp_buf)) {
611
if (tmp_buf->str[0] != 0) {
612
module->info->extensions =
613
g_realloc (module->info->extensions, (n + 1) * sizeof (gchar*));
614
module->info->extensions[n - 1] = g_strdup (tmp_buf->str);
615
module->info->extensions[n] = NULL;
622
module->info->signature = (GdkPixbufModulePattern *)
623
g_realloc (module->info->signature, (n_patterns + 1) * sizeof (GdkPixbufModulePattern));
624
pattern = module->info->signature + n_patterns;
625
pattern->prefix = NULL;
626
pattern->mask = NULL;
627
pattern->relevance = 0;
629
if (!scan_string (&p, tmp_buf))
631
pattern->prefix = g_strdup (tmp_buf->str);
633
if (!scan_string (&p, tmp_buf))
636
pattern->mask = g_strdup (tmp_buf->str);
638
pattern->mask = NULL;
640
if (!scan_int (&p, &pattern->relevance))
646
g_free (pattern->prefix);
647
g_free (pattern->mask);
649
g_warning ("Error parsing loader info in '%s'\n %s",
656
g_string_free (tmp_buf, TRUE);
657
g_io_channel_unref (channel);
663
#define module(type) \
664
extern void _gdk_pixbuf__##type##_fill_info (GdkPixbufFormat *info); \
665
extern void _gdk_pixbuf__##type##_fill_vtable (GdkPixbufModule *module)
695
/* actually load the image handler - gdk_pixbuf_get_module only get a */
696
/* reference to the module to load, it doesn't actually load it */
697
/* perhaps these actions should be combined in one function */
699
gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
702
GdkPixbufModuleFillInfoFunc fill_info = NULL;
703
GdkPixbufModuleFillVtableFunc fill_vtable = NULL;
705
if (image_module->module != NULL)
708
#define try_module(format,id) \
709
if (fill_info == NULL && \
710
strcmp (image_module->module_name, #format) == 0) { \
711
fill_info = _gdk_pixbuf__##id##_fill_info; \
712
fill_vtable = _gdk_pixbuf__##id##_fill_vtable; \
715
try_module (png,png);
718
try_module (bmp,bmp);
721
try_module (wbmp,wbmp);
724
try_module (gif,gif);
727
try_module (ico,ico);
730
try_module (ani,ani);
733
try_module (jpeg,jpeg);
736
try_module (pnm,pnm);
739
try_module (ras,ras);
742
try_module (tiff,tiff);
745
try_module (xpm,xpm);
748
try_module (xbm,xbm);
751
try_module (tga,tga);
754
try_module (pcx,pcx);
757
try_module (icns,icns);
759
#ifdef INCLUDE_jasper
760
try_module (jasper,jasper);
763
try_module (qtif,qtif);
765
#ifdef INCLUDE_gdiplus
766
try_module (ico,gdip_ico);
767
try_module (wmf,gdip_wmf);
768
try_module (emf,gdip_emf);
769
try_module (bmp,gdip_bmp);
770
try_module (gif,gdip_gif);
771
try_module (jpeg,gdip_jpeg);
772
try_module (tiff,gdip_tiff);
774
#ifdef INCLUDE_gdip_png
775
try_module (png,gdip_png);
781
image_module->module = (void *) 1;
782
(* fill_vtable) (image_module);
783
if (image_module->info == NULL) {
784
image_module->info = g_new0 (GdkPixbufFormat, 1);
785
(* fill_info) (image_module->info);
796
path = image_module->module_path;
797
module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
802
GDK_PIXBUF_ERROR_FAILED,
803
_("Unable to load image-loading module: %s: %s"),
804
path, g_module_error ());
808
image_module->module = module;
810
if (g_module_symbol (module, "fill_vtable", &sym)) {
811
fill_vtable = (GdkPixbufModuleFillVtableFunc) sym;
812
(* fill_vtable) (image_module);
817
GDK_PIXBUF_ERROR_FAILED,
818
_("Image-loading module %s does not export the proper interface; perhaps it's from a different gdk-pixbuf version?"),
826
GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
827
_("Image type '%s' is not supported"),
828
image_module->module_name);
830
#endif /* !USE_GMODULE */
835
_gdk_pixbuf_load_module (GdkPixbufModule *image_module,
839
gboolean locked = FALSE;
841
/* be extra careful, maybe the module initializes
844
if (g_threads_got_initialized) {
849
ret = gdk_pixbuf_load_module_unlocked (image_module, error);
852
G_UNLOCK (init_lock);
860
_gdk_pixbuf_get_named_module (const char *name,
865
for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
866
GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
868
if (module->info->disabled)
871
if (!strcmp (name, module->module_name))
877
GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
878
_("Image type '%s' is not supported"),
885
_gdk_pixbuf_get_module (guchar *buffer, guint size,
886
const gchar *filename,
891
GdkPixbufModule *selected = NULL;
892
gchar *display_name = NULL;
893
#ifdef GDK_PIXBUF_USE_GIO_MIME
900
mime_type = g_content_type_guess (NULL, buffer, size, &uncertain);
902
mime_type = g_content_type_guess (filename, buffer, size, NULL);
904
for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
905
GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
906
GdkPixbufFormat *info = module->info;
911
mimes = info->mime_types;
912
for (j = 0; mimes[j] != NULL; j++) {
913
type = g_content_type_from_mime_type (mimes[j]);
914
if (g_content_type_equals (type, mime_type)) {
924
gint score, best = 0;
926
for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
927
GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
929
if (module->info->disabled)
932
score = format_check (module, buffer, size);
942
if (selected != NULL)
947
display_name = g_filename_display_name (filename);
950
GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
951
_("Couldn't recognize the image file format for file '%s'"),
953
g_free (display_name);
956
g_set_error_literal (error,
958
GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
959
_("Unrecognized image file format"));
967
prepared_notify (GdkPixbuf *pixbuf,
968
GdkPixbufAnimation *anim,
972
g_object_ref (pixbuf);
973
*((GdkPixbuf **)user_data) = pixbuf;
977
_gdk_pixbuf_generic_image_load (GdkPixbufModule *module,
981
guchar buffer[LOAD_BUFFER_SIZE];
983
GdkPixbuf *pixbuf = NULL;
984
GdkPixbufAnimation *animation = NULL;
988
locked = _gdk_pixbuf_lock (module);
990
if (module->load != NULL) {
991
pixbuf = (* module->load) (f, error);
992
} else if (module->begin_load != NULL) {
994
context = module->begin_load (NULL, prepared_notify, NULL, &pixbuf, error);
999
while (!feof (f) && !ferror (f)) {
1000
length = fread (buffer, 1, sizeof (buffer), f);
1002
if (!module->load_increment (context, buffer, length, error)) {
1003
module->stop_load (context, NULL);
1004
if (pixbuf != NULL) {
1005
g_object_unref (pixbuf);
1012
if (!module->stop_load (context, error)) {
1013
if (pixbuf != NULL) {
1014
g_object_unref (pixbuf);
1018
} else if (module->load_animation != NULL) {
1019
animation = (* module->load_animation) (f, error);
1020
if (animation != NULL) {
1021
pixbuf = gdk_pixbuf_animation_get_static_image (animation);
1023
g_object_ref (pixbuf);
1024
g_object_unref (animation);
1030
_gdk_pixbuf_unlock (module);
1035
* gdk_pixbuf_new_from_file:
1036
* @filename: Name of file to load, in the GLib file name encoding
1037
* @error: Return location for an error
1039
* Creates a new pixbuf by loading an image from a file. The file format is
1040
* detected automatically. If %NULL is returned, then @error will be set.
1041
* Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
1043
* Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
1044
* any of several error conditions occurred: the file could not be opened,
1045
* there was no loader for the file's format, there was not enough memory to
1046
* allocate the image buffer, or the image file contained invalid data.
1049
gdk_pixbuf_new_from_file (const char *filename,
1055
guchar buffer[SNIFF_BUFFER_SIZE];
1056
GdkPixbufModule *image_module;
1057
gchar *display_name;
1059
g_return_val_if_fail (filename != NULL, NULL);
1060
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1062
display_name = g_filename_display_name (filename);
1064
f = g_fopen (filename, "rb");
1066
gint save_errno = errno;
1069
g_file_error_from_errno (save_errno),
1070
_("Failed to open file '%s': %s"),
1072
g_strerror (save_errno));
1073
g_free (display_name);
1077
size = fread (&buffer, 1, sizeof (buffer), f);
1081
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1082
_("Image file '%s' contains no data"),
1084
g_free (display_name);
1089
image_module = _gdk_pixbuf_get_module (buffer, size, filename, error);
1090
if (image_module == NULL) {
1091
g_free (display_name);
1096
if (!_gdk_pixbuf_load_module (image_module, error)) {
1097
g_free (display_name);
1102
fseek (f, 0, SEEK_SET);
1103
pixbuf = _gdk_pixbuf_generic_image_load (image_module, f, error);
1106
if (pixbuf == NULL && error != NULL && *error == NULL) {
1108
/* I don't trust these crufty longjmp()'ing image libs
1109
* to maintain proper error invariants, and I don't
1110
* want user code to segfault as a result. We need to maintain
1111
* the invariant that error gets set if NULL is returned.
1114
g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.", image_module->module_name);
1117
GDK_PIXBUF_ERROR_FAILED,
1118
_("Failed to load image '%s': reason not known, probably a corrupt image file"),
1120
} else if (error != NULL && *error != NULL) {
1122
/* Add the filename to the error message */
1127
e->message = g_strdup_printf (_("Failed to load image '%s': %s"),
1133
g_free (display_name);
1139
#undef gdk_pixbuf_new_from_file
1141
gdk_pixbuf_new_from_file (const char *filename,
1144
gchar *utf8_filename =
1145
g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1148
if (utf8_filename == NULL)
1151
retval = gdk_pixbuf_new_from_file_utf8 (utf8_filename, error);
1153
g_free (utf8_filename);
1161
* gdk_pixbuf_new_from_file_at_size:
1162
* @filename: Name of file to load, in the GLib file name encoding
1163
* @width: The width the image should have or -1 to not constrain the width
1164
* @height: The height the image should have or -1 to not constrain the height
1165
* @error: Return location for an error
1167
* Creates a new pixbuf by loading an image from a file.
1168
* The file format is detected automatically. If %NULL is returned, then
1169
* @error will be set. Possible errors are in the #GDK_PIXBUF_ERROR and
1170
* #G_FILE_ERROR domains.
1172
* The image will be scaled to fit in the requested size, preserving
1173
* the image's aspect ratio. Note that the returned pixbuf may be smaller
1174
* than @width x @height, if the aspect ratio requires it. To load
1175
* and image at the requested size, regardless of aspect ratio, use
1176
* gdk_pixbuf_new_from_file_at_scale().
1178
* Return value: A newly-created pixbuf with a reference count of 1, or
1179
* %NULL if any of several error conditions occurred: the file could not
1180
* be opened, there was no loader for the file's format, there was not
1181
* enough memory to allocate the image buffer, or the image file contained
1187
gdk_pixbuf_new_from_file_at_size (const char *filename,
1192
return gdk_pixbuf_new_from_file_at_scale (filename,
1199
#undef gdk_pixbuf_new_from_file_at_size
1202
gdk_pixbuf_new_from_file_at_size (const char *filename,
1207
gchar *utf8_filename =
1208
g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1211
if (utf8_filename == NULL)
1214
retval = gdk_pixbuf_new_from_file_at_size_utf8 (utf8_filename,
1218
g_free (utf8_filename);
1227
gboolean preserve_aspect_ratio;
1231
at_scale_data_async_data_free (AtScaleData *data)
1233
g_slice_free (AtScaleData, data);
1237
at_scale_size_prepared_cb (GdkPixbufLoader *loader,
1242
AtScaleData *info = data;
1244
g_return_if_fail (width > 0 && height > 0);
1246
if (info->preserve_aspect_ratio &&
1247
(info->width > 0 || info->height > 0)) {
1248
if (info->width < 0)
1250
width = width * (double)info->height/(double)height;
1251
height = info->height;
1253
else if (info->height < 0)
1255
height = height * (double)info->width/(double)width;
1256
width = info->width;
1258
else if ((double)height * (double)info->width >
1259
(double)width * (double)info->height) {
1260
width = 0.5 + (double)width * (double)info->height / (double)height;
1261
height = info->height;
1263
height = 0.5 + (double)height * (double)info->width / (double)width;
1264
width = info->width;
1267
if (info->width > 0)
1268
width = info->width;
1269
if (info->height > 0)
1270
height = info->height;
1273
width = MAX (width, 1);
1274
height = MAX (height, 1);
1276
gdk_pixbuf_loader_set_size (loader, width, height);
1280
* gdk_pixbuf_new_from_file_at_scale:
1281
* @filename: Name of file to load, in the GLib file name encoding
1282
* @width: The width the image should have or -1 to not constrain the width
1283
* @height: The height the image should have or -1 to not constrain the height
1284
* @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1285
* @error: Return location for an error
1287
* Creates a new pixbuf by loading an image from a file. The file format is
1288
* detected automatically. If %NULL is returned, then @error will be set.
1289
* Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
1290
* The image will be scaled to fit in the requested size, optionally preserving
1291
* the image's aspect ratio.
1293
* When preserving the aspect ratio, a @width of -1 will cause the image
1294
* to be scaled to the exact given height, and a @height of -1 will cause
1295
* the image to be scaled to the exact given width. When not preserving
1296
* aspect ratio, a @width or @height of -1 means to not scale the image
1297
* at all in that dimension. Negative values for @width and @height are
1298
* allowed since 2.8.
1300
* Return value: A newly-created pixbuf with a reference count of 1, or %NULL
1301
* if any of several error conditions occurred: the file could not be opened,
1302
* there was no loader for the file's format, there was not enough memory to
1303
* allocate the image buffer, or the image file contained invalid data.
1308
gdk_pixbuf_new_from_file_at_scale (const char *filename,
1311
gboolean preserve_aspect_ratio,
1315
GdkPixbufLoader *loader;
1317
guchar buffer[LOAD_BUFFER_SIZE];
1321
GdkPixbufAnimation *animation;
1322
GdkPixbufAnimationIter *iter;
1325
g_return_val_if_fail (filename != NULL, NULL);
1326
g_return_val_if_fail (width > 0 || width == -1, NULL);
1327
g_return_val_if_fail (height > 0 || height == -1, NULL);
1329
f = g_fopen (filename, "rb");
1331
gint save_errno = errno;
1332
gchar *display_name = g_filename_display_name (filename);
1335
g_file_error_from_errno (save_errno),
1336
_("Failed to open file '%s': %s"),
1338
g_strerror (save_errno));
1339
g_free (display_name);
1343
loader = gdk_pixbuf_loader_new ();
1346
info.height = height;
1347
info.preserve_aspect_ratio = preserve_aspect_ratio;
1349
g_signal_connect (loader, "size-prepared",
1350
G_CALLBACK (at_scale_size_prepared_cb), &info);
1353
while (!has_frame && !feof (f) && !ferror (f)) {
1354
length = fread (buffer, 1, sizeof (buffer), f);
1356
if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
1357
gdk_pixbuf_loader_close (loader, NULL);
1359
g_object_unref (loader);
1363
animation = gdk_pixbuf_loader_get_animation (loader);
1365
iter = gdk_pixbuf_animation_get_iter (animation, NULL);
1366
if (!gdk_pixbuf_animation_iter_on_currently_loading_frame (iter)) {
1369
g_object_unref (iter);
1375
if (!gdk_pixbuf_loader_close (loader, error) && !has_frame) {
1376
g_object_unref (loader);
1380
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1383
gchar *display_name = g_filename_display_name (filename);
1384
g_object_unref (loader);
1387
GDK_PIXBUF_ERROR_FAILED,
1388
_("Failed to load image '%s': reason not known, probably a corrupt image file"),
1390
g_free (display_name);
1394
g_object_ref (pixbuf);
1396
g_object_unref (loader);
1403
#undef gdk_pixbuf_new_from_file_at_scale
1406
gdk_pixbuf_new_from_file_at_scale (const char *filename,
1409
gboolean preserve_aspect_ratio,
1412
gchar *utf8_filename =
1413
g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1416
if (utf8_filename == NULL)
1419
retval = gdk_pixbuf_new_from_file_at_scale_utf8 (utf8_filename,
1421
preserve_aspect_ratio,
1424
g_free (utf8_filename);
1432
load_from_stream (GdkPixbufLoader *loader,
1433
GInputStream *stream,
1434
GCancellable *cancellable,
1439
guchar buffer[LOAD_BUFFER_SIZE];
1444
n_read = g_input_stream_read (stream,
1451
error = NULL; /* Ignore further errors */
1458
if (!gdk_pixbuf_loader_write (loader,
1468
if (!gdk_pixbuf_loader_close (loader, error)) {
1475
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1477
g_object_ref (pixbuf);
1485
* gdk_pixbuf_new_from_stream_at_scale:
1486
* @stream: a #GInputStream to load the pixbuf from
1487
* @width: The width the image should have or -1 to not constrain the width
1488
* @height: The height the image should have or -1 to not constrain the height
1489
* @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1490
* @cancellable: optional #GCancellable object, %NULL to ignore
1491
* @error: Return location for an error
1493
* Creates a new pixbuf by loading an image from an input stream.
1495
* The file format is detected automatically. If %NULL is returned, then
1496
* @error will be set. The @cancellable can be used to abort the operation
1497
* from another thread. If the operation was cancelled, the error
1498
* %GIO_ERROR_CANCELLED will be returned. Other possible errors are in
1499
* the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
1501
* The image will be scaled to fit in the requested size, optionally
1502
* preserving the image's aspect ratio. When preserving the aspect ratio,
1503
* a @width of -1 will cause the image to be scaled to the exact given
1504
* height, and a @height of -1 will cause the image to be scaled to the
1505
* exact given width. When not preserving aspect ratio, a @width or
1506
* @height of -1 means to not scale the image at all in that dimension.
1508
* The stream is not closed.
1510
* Return value: A newly-created pixbuf, or %NULL if any of several error
1511
* conditions occurred: the file could not be opened, the image format is
1512
* not supported, there was not enough memory to allocate the image buffer,
1513
* the stream contained invalid data, or the operation was cancelled.
1518
gdk_pixbuf_new_from_stream_at_scale (GInputStream *stream,
1521
gboolean preserve_aspect_ratio,
1522
GCancellable *cancellable,
1525
GdkPixbufLoader *loader;
1529
loader = gdk_pixbuf_loader_new ();
1532
info.height = height;
1533
info.preserve_aspect_ratio = preserve_aspect_ratio;
1535
g_signal_connect (loader, "size-prepared",
1536
G_CALLBACK (at_scale_size_prepared_cb), &info);
1538
pixbuf = load_from_stream (loader, stream, cancellable, error);
1539
g_object_unref (loader);
1545
new_from_stream_thread (GSimpleAsyncResult *result,
1546
GInputStream *stream,
1547
GCancellable *cancellable)
1551
GError *error = NULL;
1553
/* If data != NULL, we're scaling the pixbuf while loading it */
1554
data = g_simple_async_result_get_op_res_gpointer (result);
1556
pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream, data->width, data->height, data->preserve_aspect_ratio, cancellable, &error);
1558
pixbuf = gdk_pixbuf_new_from_stream (stream, cancellable, &error);
1560
g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
1562
/* Set the new pixbuf as the result, or error out */
1563
if (pixbuf == NULL) {
1564
g_simple_async_result_take_error (result, error);
1566
g_simple_async_result_set_op_res_gpointer (result, g_object_ref (pixbuf), g_object_unref);
1571
* gdk_pixbuf_new_from_stream_at_scale_async:
1572
* @stream: a #GInputStream from which to load the pixbuf
1573
* @width: the width the image should have or -1 to not constrain the width
1574
* @height: the height the image should have or -1 to not constrain the height
1575
* @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1576
* @cancellable: optional #GCancellable object, %NULL to ignore
1577
* @callback: a #GAsyncReadyCallback to call when the the pixbuf is loaded
1578
* @user_data: the data to pass to the callback function
1580
* Creates a new pixbuf by asynchronously loading an image from an input stream.
1582
* For more details see gdk_pixbuf_new_from_stream_at_scale(), which is the synchronous
1583
* version of this function.
1585
* When the operation is finished, @callback will be called in the main thread.
1586
* You can then call gdk_pixbuf_new_from_stream_finish() to get the result of the operation.
1591
gdk_pixbuf_new_from_stream_at_scale_async (GInputStream *stream,
1594
gboolean preserve_aspect_ratio,
1595
GCancellable *cancellable,
1596
GAsyncReadyCallback callback,
1599
GSimpleAsyncResult *result;
1602
g_return_if_fail (G_IS_INPUT_STREAM (stream));
1603
g_return_if_fail (callback != NULL);
1604
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
1606
data = g_slice_new (AtScaleData);
1607
data->width = width;
1608
data->height = height;
1609
data->preserve_aspect_ratio = preserve_aspect_ratio;
1611
result = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, gdk_pixbuf_new_from_stream_at_scale_async);
1612
g_simple_async_result_set_op_res_gpointer (result, data, (GDestroyNotify) at_scale_data_async_data_free);
1613
g_simple_async_result_run_in_thread (result, (GSimpleAsyncThreadFunc) new_from_stream_thread, G_PRIORITY_DEFAULT, cancellable);
1614
g_object_unref (result);
1618
* gdk_pixbuf_new_from_stream:
1619
* @stream: a #GInputStream to load the pixbuf from
1620
* @cancellable: optional #GCancellable object, %NULL to ignore
1621
* @error: Return location for an error
1623
* Creates a new pixbuf by loading an image from an input stream.
1625
* The file format is detected automatically. If %NULL is returned, then
1626
* @error will be set. The @cancellable can be used to abort the operation
1627
* from another thread. If the operation was cancelled, the error
1628
* %GIO_ERROR_CANCELLED will be returned. Other possible errors are in
1629
* the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
1631
* The stream is not closed.
1633
* Return value: A newly-created pixbuf, or %NULL if any of several error
1634
* conditions occurred: the file could not be opened, the image format is
1635
* not supported, there was not enough memory to allocate the image buffer,
1636
* the stream contained invalid data, or the operation was cancelled.
1641
gdk_pixbuf_new_from_stream (GInputStream *stream,
1642
GCancellable *cancellable,
1646
GdkPixbufLoader *loader;
1648
loader = gdk_pixbuf_loader_new ();
1649
pixbuf = load_from_stream (loader, stream, cancellable, error);
1650
g_object_unref (loader);
1656
* gdk_pixbuf_new_from_stream_async:
1657
* @stream: a #GInputStream from which to load the pixbuf
1658
* @cancellable: optional #GCancellable object, %NULL to ignore
1659
* @callback: a #GAsyncReadyCallback to call when the the pixbuf is loaded
1660
* @user_data: the data to pass to the callback function
1662
* Creates a new pixbuf by asynchronously loading an image from an input stream.
1664
* For more details see gdk_pixbuf_new_from_stream(), which is the synchronous
1665
* version of this function.
1667
* When the operation is finished, @callback will be called in the main thread.
1668
* You can then call gdk_pixbuf_new_from_stream_finish() to get the result of the operation.
1673
gdk_pixbuf_new_from_stream_async (GInputStream *stream,
1674
GCancellable *cancellable,
1675
GAsyncReadyCallback callback,
1678
GSimpleAsyncResult *result;
1680
g_return_if_fail (G_IS_INPUT_STREAM (stream));
1681
g_return_if_fail (callback != NULL);
1682
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
1684
result = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, gdk_pixbuf_new_from_stream_async);
1685
g_simple_async_result_run_in_thread (result, (GSimpleAsyncThreadFunc) new_from_stream_thread, G_PRIORITY_DEFAULT, cancellable);
1686
g_object_unref (result);
1690
* gdk_pixbuf_new_from_stream_finish:
1691
* @async_result: a #GAsyncResult
1692
* @error: a #GError, or %NULL
1694
* Finishes an asynchronous pixbuf creation operation started with
1695
* gdk_pixbuf_new_from_stream_async().
1697
* Return value: a #GdkPixbuf or %NULL on error. Free the returned
1698
* object with g_object_unref().
1703
gdk_pixbuf_new_from_stream_finish (GAsyncResult *async_result,
1706
GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (async_result);
1708
g_return_val_if_fail (G_IS_ASYNC_RESULT (async_result), NULL);
1709
g_return_val_if_fail (!error || (error && !*error), NULL);
1710
g_warn_if_fail (g_simple_async_result_get_source_tag (result) == gdk_pixbuf_new_from_stream_async ||
1711
g_simple_async_result_get_source_tag (result) == gdk_pixbuf_new_from_stream_at_scale_async);
1713
if (g_simple_async_result_propagate_error (result, error))
1716
return g_simple_async_result_get_op_res_gpointer (result);
1720
info_cb (GdkPixbufLoader *loader,
1726
GdkPixbufFormat *format;
1731
g_return_if_fail (width > 0 && height > 0);
1733
info->format = gdk_pixbuf_loader_get_format (loader);
1734
info->width = width;
1735
info->height = height;
1737
gdk_pixbuf_loader_set_size (loader, 0, 0);
1741
* gdk_pixbuf_get_file_info:
1742
* @filename: The name of the file to identify.
1743
* @width: Return location for the width of the image, or %NULL
1744
* @height: Return location for the height of the image, or %NULL
1746
* Parses an image file far enough to determine its format and size.
1748
* Returns: A #GdkPixbufFormat describing the image format of the file
1749
* or %NULL if the image format wasn't recognized. The return value
1750
* is owned by GdkPixbuf and should not be freed.
1755
gdk_pixbuf_get_file_info (const gchar *filename,
1759
GdkPixbufLoader *loader;
1760
guchar buffer[SNIFF_BUFFER_SIZE];
1764
GdkPixbufFormat *format;
1769
g_return_val_if_fail (filename != NULL, NULL);
1771
f = g_fopen (filename, "rb");
1775
loader = gdk_pixbuf_loader_new ();
1781
g_signal_connect (loader, "size-prepared", G_CALLBACK (info_cb), &info);
1783
while (!feof (f) && !ferror (f)) {
1784
length = fread (buffer, 1, sizeof (buffer), f);
1786
if (!gdk_pixbuf_loader_write (loader, buffer, length, NULL))
1789
if (info.format != NULL)
1794
gdk_pixbuf_loader_close (loader, NULL);
1795
g_object_unref (loader);
1798
*width = info.width;
1800
*height = info.height;
1806
* gdk_pixbuf_new_from_xpm_data:
1807
* @data: Pointer to inline XPM data.
1809
* Creates a new pixbuf by parsing XPM data in memory. This data is commonly
1810
* the result of including an XPM file into a program's C source.
1812
* Return value: A newly-created pixbuf with a reference count of 1.
1815
gdk_pixbuf_new_from_xpm_data (const char **data)
1817
GdkPixbuf *(* load_xpm_data) (const char **data);
1819
GError *error = NULL;
1820
GdkPixbufModule *xpm_module;
1823
g_return_val_if_fail (data != NULL, NULL);
1825
xpm_module = _gdk_pixbuf_get_named_module ("xpm", &error);
1826
if (xpm_module == NULL) {
1827
g_warning ("Error loading XPM image loader: %s", error->message);
1828
g_error_free (error);
1832
if (!_gdk_pixbuf_load_module (xpm_module, &error)) {
1833
g_warning ("Error loading XPM image loader: %s", error->message);
1834
g_error_free (error);
1838
locked = _gdk_pixbuf_lock (xpm_module);
1840
if (xpm_module->load_xpm_data == NULL) {
1841
g_warning ("gdk-pixbuf XPM module lacks XPM data capability");
1844
load_xpm_data = xpm_module->load_xpm_data;
1845
pixbuf = (* load_xpm_data) (data);
1849
_gdk_pixbuf_unlock (xpm_module);
1854
collect_save_options (va_list opts,
1867
next = va_arg (opts, gchar*);
1871
val = va_arg (opts, gchar*);
1876
*keys = g_realloc (*keys, sizeof(gchar*) * (count + 1));
1877
*vals = g_realloc (*vals, sizeof(gchar*) * (count + 1));
1879
(*keys)[count-1] = g_strdup (key);
1880
(*vals)[count-1] = g_strdup (val);
1882
(*keys)[count] = NULL;
1883
(*vals)[count] = NULL;
1885
next = va_arg (opts, gchar*);
1890
save_to_file_callback (const gchar *buf,
1895
FILE *filehandle = data;
1898
n = fwrite (buf, 1, count, filehandle);
1900
gint save_errno = errno;
1903
g_file_error_from_errno (save_errno),
1904
_("Error writing to image file: %s"),
1905
g_strerror (save_errno));
1912
gdk_pixbuf_real_save (GdkPixbuf *pixbuf,
1920
GdkPixbufModule *image_module = NULL;
1923
image_module = _gdk_pixbuf_get_named_module (type, error);
1925
if (image_module == NULL)
1928
if (!_gdk_pixbuf_load_module (image_module, error))
1931
locked = _gdk_pixbuf_lock (image_module);
1933
if (image_module->save) {
1935
ret = (* image_module->save) (filehandle, pixbuf,
1938
} else if (image_module->save_to_callback) {
1939
/* save with simple callback */
1940
ret = (* image_module->save_to_callback) (save_to_file_callback,
1948
GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
1949
_("This build of gdk-pixbuf does not support saving the image format: %s"),
1955
_gdk_pixbuf_unlock (image_module);
1959
#define TMP_FILE_BUF_SIZE 4096
1962
save_to_callback_with_tmp_file (GdkPixbufModule *image_module,
1964
GdkPixbufSaveFunc save_func,
1972
gboolean retval = FALSE;
1975
gchar *filename = NULL;
1978
buf = g_try_malloc (TMP_FILE_BUF_SIZE);
1980
g_set_error_literal (error,
1982
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1983
_("Insufficient memory to save image to callback"));
1987
fd = g_file_open_tmp ("gdkpixbuf-save-tmp.XXXXXX", &filename, error);
1990
f = fdopen (fd, "wb+");
1992
gint save_errno = errno;
1993
g_set_error_literal (error,
1995
g_file_error_from_errno (save_errno),
1996
_("Failed to open temporary file"));
2000
locked = _gdk_pixbuf_lock (image_module);
2001
retval = (image_module->save) (f, pixbuf, keys, values, error);
2003
_gdk_pixbuf_unlock (image_module);
2009
n = fread (buf, 1, TMP_FILE_BUF_SIZE, f);
2011
if (!save_func (buf, n, error, user_data))
2014
if (n != TMP_FILE_BUF_SIZE)
2018
gint save_errno = errno;
2019
g_set_error_literal (error,
2021
g_file_error_from_errno (save_errno),
2022
_("Failed to read from temporary file"));
2028
/* cleanup and return retval */
2032
g_unlink (filename);
2041
gdk_pixbuf_real_save_to_callback (GdkPixbuf *pixbuf,
2042
GdkPixbufSaveFunc save_func,
2050
GdkPixbufModule *image_module = NULL;
2053
image_module = _gdk_pixbuf_get_named_module (type, error);
2055
if (image_module == NULL)
2058
if (!_gdk_pixbuf_load_module (image_module, error))
2061
locked = _gdk_pixbuf_lock (image_module);
2063
if (image_module->save_to_callback) {
2065
ret = (* image_module->save_to_callback) (save_func, user_data,
2066
pixbuf, keys, values,
2068
} else if (image_module->save) {
2069
/* use a temporary file */
2070
ret = save_to_callback_with_tmp_file (image_module, pixbuf,
2071
save_func, user_data,
2078
GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
2079
_("This build of gdk-pixbuf does not support saving the image format: %s"),
2085
_gdk_pixbuf_unlock (image_module);
2092
* @pixbuf: a #GdkPixbuf.
2093
* @filename: name of file to save.
2094
* @type: name of file format.
2095
* @error: (allow-none): return location for error, or %NULL
2096
* @Varargs: list of key-value save options
2098
* Saves pixbuf to a file in format @type. By default, "jpeg", "png", "ico"
2099
* and "bmp" are possible file formats to save in, but more formats may be
2100
* installed. The list of all writable formats can be determined in the
2104
* void add_if_writable (GdkPixbufFormat *data, GSList **list)
2106
* if (gdk_pixbuf_format_is_writable (data))
2107
* *list = g_slist_prepend (*list, data);
2110
* GSList *formats = gdk_pixbuf_get_formats ();
2111
* GSList *writable_formats = NULL;
2112
* g_slist_foreach (formats, add_if_writable, &writable_formats);
2113
* g_slist_free (formats);
2116
* If @error is set, %FALSE will be returned. Possible errors include
2117
* those in the #GDK_PIXBUF_ERROR domain and those in the #G_FILE_ERROR domain.
2119
* The variable argument list should be %NULL-terminated; if not empty,
2120
* it should contain pairs of strings that modify the save
2121
* parameters. For example:
2122
* <informalexample><programlisting>
2123
* gdk_pixbuf_save (pixbuf, handle, "jpeg", &error,
2124
* "quality", "100", NULL);
2125
* </programlisting></informalexample>
2127
* Currently only few parameters exist. JPEG images can be saved with a
2128
* "quality" parameter; its value should be in the range [0,100].
2130
* Text chunks can be attached to PNG images by specifying parameters of
2131
* the form "tEXt::key", where key is an ASCII string of length 1-79.
2132
* The values are UTF-8 encoded strings. The PNG compression level can
2133
* be specified using the "compression" parameter; it's value is in an
2134
* integer in the range of [0,9].
2136
* ICC color profiles can also be embedded into PNG and TIFF images.
2137
* The "icc-profile" value should be the complete ICC profile encoded
2140
* <informalexample><programlisting>
2142
* gchar *contents_encode;
2144
* g_file_get_contents ("/home/hughsie/.color/icc/L225W.icm", &contents, &length, NULL);
2145
* contents_encode = g_base64_encode ((const guchar *) contents, length);
2146
* gdk_pixbuf_save (pixbuf, handle, "png", &error,
2147
* "icc-profile", contents_encode,
2149
* </programlisting></informalexample>
2151
* TIFF images recognize a "compression" option which acceps an integer value.
2152
* Among the codecs are 1 None, 2 Huffman, 5 LZW, 7 JPEG and 8 Deflate, see
2153
* the libtiff documentation and tiff.h for all supported codec values.
2155
* ICO images can be saved in depth 16, 24, or 32, by using the "depth"
2156
* parameter. When the ICO saver is given "x_hot" and "y_hot" parameters,
2157
* it produces a CUR instead of an ICO.
2159
* Return value: whether an error was set
2163
gdk_pixbuf_save (GdkPixbuf *pixbuf,
2164
const char *filename,
2169
gchar **keys = NULL;
2170
gchar **values = NULL;
2174
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2176
va_start (args, error);
2178
collect_save_options (args, &keys, &values);
2182
result = gdk_pixbuf_savev (pixbuf, filename, type,
2187
g_strfreev (values);
2194
#undef gdk_pixbuf_save
2197
gdk_pixbuf_save (GdkPixbuf *pixbuf,
2198
const char *filename,
2203
char *utf8_filename;
2204
gchar **keys = NULL;
2205
gchar **values = NULL;
2209
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2211
utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2213
if (utf8_filename == NULL)
2216
va_start (args, error);
2218
collect_save_options (args, &keys, &values);
2222
result = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
2226
g_free (utf8_filename);
2229
g_strfreev (values);
2238
* @pixbuf: a #GdkPixbuf.
2239
* @filename: name of file to save.
2240
* @type: name of file format.
2241
* @option_keys: (array zero-terminated=1): name of options to set, %NULL-terminated
2242
* @option_values: (array zero-terminated=1): values for named options
2243
* @error: (allow-none): return location for error, or %NULL
2245
* Saves pixbuf to a file in @type, which is currently "jpeg", "png", "tiff", "ico" or "bmp".
2246
* If @error is set, %FALSE will be returned.
2247
* See gdk_pixbuf_save () for more details.
2249
* Return value: whether an error was set
2253
gdk_pixbuf_savev (GdkPixbuf *pixbuf,
2254
const char *filename,
2257
char **option_values,
2263
g_return_val_if_fail (filename != NULL, FALSE);
2264
g_return_val_if_fail (type != NULL, FALSE);
2265
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2267
f = g_fopen (filename, "wb");
2270
gint save_errno = errno;
2271
gchar *display_name = g_filename_display_name (filename);
2274
g_file_error_from_errno (save_errno),
2275
_("Failed to open '%s' for writing: %s"),
2277
g_strerror (save_errno));
2278
g_free (display_name);
2283
result = gdk_pixbuf_real_save (pixbuf, f, type,
2284
option_keys, option_values,
2289
g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
2291
g_unlink (filename);
2295
if (fclose (f) < 0) {
2296
gint save_errno = errno;
2297
gchar *display_name = g_filename_display_name (filename);
2300
g_file_error_from_errno (save_errno),
2301
_("Failed to close '%s' while writing image, all data may not have been saved: %s"),
2303
g_strerror (save_errno));
2304
g_free (display_name);
2313
#undef gdk_pixbuf_savev
2316
gdk_pixbuf_savev (GdkPixbuf *pixbuf,
2317
const char *filename,
2320
char **option_values,
2323
char *utf8_filename;
2326
g_return_val_if_fail (filename != NULL, FALSE);
2328
utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2330
if (utf8_filename == NULL)
2333
retval = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
2334
option_keys, option_values, error);
2336
g_free (utf8_filename);
2344
* gdk_pixbuf_save_to_callback:
2345
* @pixbuf: a #GdkPixbuf.
2346
* @save_func: (scope call): a function that is called to save each block of data that
2347
* the save routine generates.
2348
* @user_data: user data to pass to the save function.
2349
* @type: name of file format.
2350
* @error: (allow-none): return location for error, or %NULL
2351
* @Varargs: list of key-value save options
2353
* Saves pixbuf in format @type by feeding the produced data to a
2354
* callback. Can be used when you want to store the image to something
2355
* other than a file, such as an in-memory buffer or a socket.
2356
* If @error is set, %FALSE will be returned. Possible errors
2357
* include those in the #GDK_PIXBUF_ERROR domain and whatever the save
2358
* function generates.
2360
* See gdk_pixbuf_save() for more details.
2362
* Return value: whether an error was set
2367
gdk_pixbuf_save_to_callback (GdkPixbuf *pixbuf,
2368
GdkPixbufSaveFunc save_func,
2374
gchar **keys = NULL;
2375
gchar **values = NULL;
2379
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2381
va_start (args, error);
2383
collect_save_options (args, &keys, &values);
2387
result = gdk_pixbuf_save_to_callbackv (pixbuf, save_func, user_data,
2392
g_strfreev (values);
2398
* gdk_pixbuf_save_to_callbackv:
2399
* @pixbuf: a #GdkPixbuf.
2400
* @save_func: (scope call): a function that is called to save each block of data that
2401
* the save routine generates.
2402
* @user_data: (closure save_func): user data to pass to the save function.
2403
* @type: name of file format.
2404
* @option_keys: (array zero-terminated=1) (element-type utf8): name of options to set, %NULL-terminated
2405
* @option_values: (array zero-terminated=1) (element-type utf8): values for named options
2406
* @error: (allow-none): return location for error, or %NULL
2408
* Saves pixbuf to a callback in format @type, which is currently "jpeg",
2409
* "png", "tiff", "ico" or "bmp". If @error is set, %FALSE will be returned. See
2410
* gdk_pixbuf_save_to_callback () for more details.
2412
* Return value: whether an error was set
2417
gdk_pixbuf_save_to_callbackv (GdkPixbuf *pixbuf,
2418
GdkPixbufSaveFunc save_func,
2422
char **option_values,
2428
g_return_val_if_fail (save_func != NULL, FALSE);
2429
g_return_val_if_fail (type != NULL, FALSE);
2430
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2432
result = gdk_pixbuf_real_save_to_callback (pixbuf,
2433
save_func, user_data, type,
2434
option_keys, option_values,
2438
g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
2446
* gdk_pixbuf_save_to_buffer:
2447
* @pixbuf: a #GdkPixbuf.
2448
* @buffer: location to receive a pointer to the new buffer.
2449
* @buffer_size: location to receive the size of the new buffer.
2450
* @type: name of file format.
2451
* @error: (allow-none): return location for error, or %NULL
2452
* @Varargs: list of key-value save options
2454
* Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2455
* "png", "tiff", "ico" or "bmp". This is a convenience function that uses
2456
* gdk_pixbuf_save_to_callback() to do the real work. Note that the buffer
2457
* is not nul-terminated and may contain embedded nuls.
2458
* If @error is set, %FALSE will be returned and @buffer will be set to
2459
* %NULL. Possible errors include those in the #GDK_PIXBUF_ERROR
2462
* See gdk_pixbuf_save() for more details.
2464
* Return value: whether an error was set
2469
gdk_pixbuf_save_to_buffer (GdkPixbuf *pixbuf,
2476
gchar **keys = NULL;
2477
gchar **values = NULL;
2481
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2483
va_start (args, error);
2485
collect_save_options (args, &keys, &values);
2489
result = gdk_pixbuf_save_to_bufferv (pixbuf, buffer, buffer_size,
2494
g_strfreev (values);
2499
struct SaveToBufferData {
2505
save_to_buffer_callback (const gchar *data,
2510
struct SaveToBufferData *sdata = user_data;
2514
if (sdata->len + count > sdata->max) {
2515
new_max = MAX (sdata->max*2, sdata->len + count);
2516
new_buffer = g_try_realloc (sdata->buffer, new_max);
2518
g_set_error_literal (error,
2520
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2521
_("Insufficient memory to save image into a buffer"));
2524
sdata->buffer = new_buffer;
2525
sdata->max = new_max;
2527
memcpy (sdata->buffer + sdata->len, data, count);
2528
sdata->len += count;
2533
* gdk_pixbuf_save_to_bufferv:
2534
* @pixbuf: a #GdkPixbuf.
2535
* @buffer: location to receive a pointer to the new buffer.
2536
* @buffer_size: location to receive the size of the new buffer.
2537
* @type: name of file format.
2538
* @option_keys: (array zero-terminated=1): name of options to set, %NULL-terminated
2539
* @option_values: (array zero-terminated=1): values for named options
2540
* @error: (allow-none): return location for error, or %NULL
2542
* Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2543
* "tiff", "png", "ico" or "bmp". See gdk_pixbuf_save_to_buffer()
2546
* Return value: whether an error was set
2551
gdk_pixbuf_save_to_bufferv (GdkPixbuf *pixbuf,
2556
char **option_values,
2559
static const gint initial_max = 1024;
2560
struct SaveToBufferData sdata;
2565
sdata.buffer = g_try_malloc (initial_max);
2566
sdata.max = initial_max;
2568
if (!sdata.buffer) {
2569
g_set_error_literal (error,
2571
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2572
_("Insufficient memory to save image into a buffer"));
2576
if (!gdk_pixbuf_save_to_callbackv (pixbuf,
2577
save_to_buffer_callback, &sdata,
2578
type, option_keys, option_values,
2580
g_free (sdata.buffer);
2584
*buffer = sdata.buffer;
2585
*buffer_size = sdata.len;
2590
GOutputStream *stream;
2591
GCancellable *cancellable;
2595
save_to_stream (const gchar *buffer,
2600
SaveToStreamData *sdata = (SaveToStreamData *)data;
2603
GError *my_error = NULL;
2607
while (remaining > 0) {
2609
remaining -= written;
2610
written = g_output_stream_write (sdata->stream,
2616
g_set_error_literal (error,
2618
_("Error writing to image stream"));
2621
g_propagate_error (error, my_error);
2631
* gdk_pixbuf_save_to_stream:
2632
* @pixbuf: a #GdkPixbuf
2633
* @stream: a #GOutputStream to save the pixbuf to
2634
* @type: name of file format
2635
* @cancellable: optional #GCancellable object, %NULL to ignore
2636
* @error: (allow-none): return location for error, or %NULL
2637
* @Varargs: list of key-value save options
2639
* Saves @pixbuf to an output stream.
2641
* Supported file formats are currently "jpeg", "tiff", "png", "ico" or
2642
* "bmp". See gdk_pixbuf_save_to_buffer() for more details.
2644
* The @cancellable can be used to abort the operation from another
2645
* thread. If the operation was cancelled, the error %GIO_ERROR_CANCELLED
2646
* will be returned. Other possible errors are in the #GDK_PIXBUF_ERROR
2647
* and %G_IO_ERROR domains.
2649
* The stream is not closed.
2651
* Returns: %TRUE if the pixbuf was saved successfully, %FALSE if an
2657
gdk_pixbuf_save_to_stream (GdkPixbuf *pixbuf,
2658
GOutputStream *stream,
2660
GCancellable *cancellable,
2665
gchar **keys = NULL;
2666
gchar **values = NULL;
2668
SaveToStreamData data;
2670
va_start (args, error);
2671
collect_save_options (args, &keys, &values);
2674
data.stream = stream;
2675
data.cancellable = cancellable;
2677
res = gdk_pixbuf_save_to_callbackv (pixbuf, save_to_stream,
2683
g_strfreev (values);
2689
GOutputStream *stream;
2693
} SaveToStreamAsyncData;
2696
save_to_stream_async_data_free (SaveToStreamAsyncData *data)
2699
g_object_unref (data->stream);
2700
g_strfreev (data->keys);
2701
g_strfreev (data->values);
2702
g_free (data->type);
2703
g_slice_free (SaveToStreamAsyncData, data);
2707
save_to_stream_thread (GSimpleAsyncResult *result,
2709
GCancellable *cancellable)
2711
SaveToStreamAsyncData *data;
2712
SaveToStreamData sync_data;
2714
GError *error = NULL;
2716
data = g_simple_async_result_get_op_res_gpointer (result);
2717
sync_data.stream = data->stream;
2718
sync_data.cancellable = cancellable;
2720
retval = gdk_pixbuf_save_to_callbackv (pixbuf, save_to_stream,
2721
&sync_data, data->type,
2722
data->keys, data->values,
2725
/* Set the new pixbuf as the result, or error out */
2726
if (retval == FALSE) {
2727
g_simple_async_result_take_error (result, error);
2729
g_simple_async_result_set_op_res_gboolean (result, TRUE);
2734
* gdk_pixbuf_save_to_stream_async:
2735
* @pixbuf: a #GdkPixbuf
2736
* @stream: a #GOutputStream to which to save the pixbuf
2737
* @type: name of file format
2738
* @cancellable: optional #GCancellable object, %NULL to ignore
2739
* @callback: a #GAsyncReadyCallback to call when the the pixbuf is loaded
2740
* @user_data: the data to pass to the callback function
2741
* @Varargs: list of key-value save options
2743
* Saves @pixbuf to an output stream asynchronously.
2745
* For more details see gdk_pixbuf_save_to_stream(), which is the synchronous
2746
* version of this function.
2748
* When the operation is finished, @callback will be called in the main thread.
2749
* You can then call gdk_pixbuf_save_to_stream_finish() to get the result of the operation.
2754
gdk_pixbuf_save_to_stream_async (GdkPixbuf *pixbuf,
2755
GOutputStream *stream,
2757
GCancellable *cancellable,
2758
GAsyncReadyCallback callback,
2762
GSimpleAsyncResult *result;
2763
gchar **keys = NULL;
2764
gchar **values = NULL;
2766
SaveToStreamAsyncData *data;
2768
g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2769
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
2770
g_return_if_fail (type != NULL);
2771
g_return_if_fail (callback != NULL);
2772
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
2774
va_start (args, user_data);
2775
collect_save_options (args, &keys, &values);
2778
data = g_slice_new (SaveToStreamAsyncData);
2779
data->stream = g_object_ref (stream);
2780
data->type = g_strdup (type);
2782
data->values = values;
2784
result = g_simple_async_result_new (G_OBJECT (pixbuf), callback, user_data, gdk_pixbuf_save_to_stream_async);
2785
g_simple_async_result_set_op_res_gpointer (result, data, (GDestroyNotify) save_to_stream_async_data_free);
2786
g_simple_async_result_run_in_thread (result, (GSimpleAsyncThreadFunc) save_to_stream_thread, G_PRIORITY_DEFAULT, cancellable);
2787
g_object_unref (result);
2791
* gdk_pixbuf_save_to_stream_finish:
2792
* @async_result: a #GAsyncResult
2793
* @error: a #GError, or %NULL
2795
* Finishes an asynchronous pixbuf save operation started with
2796
* gdk_pixbuf_save_to_stream_async().
2798
* Return value: %TRUE if the pixbuf was saved successfully, %FALSE if an error was set.
2803
gdk_pixbuf_save_to_stream_finish (GAsyncResult *async_result,
2806
GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (async_result);
2808
g_return_val_if_fail (G_IS_ASYNC_RESULT (async_result), FALSE);
2809
g_return_val_if_fail (!error || (error && !*error), FALSE);
2810
g_warn_if_fail (g_simple_async_result_get_source_tag (result) == gdk_pixbuf_save_to_stream_async);
2812
if (g_simple_async_result_propagate_error (result, error))
2815
return g_simple_async_result_get_op_res_gboolean (result);
2819
* gdk_pixbuf_format_get_name:
2820
* @format: a #GdkPixbufFormat
2822
* Returns the name of the format.
2824
* Return value: the name of the format.
2829
gdk_pixbuf_format_get_name (GdkPixbufFormat *format)
2831
g_return_val_if_fail (format != NULL, NULL);
2833
return g_strdup (format->name);
2837
* gdk_pixbuf_format_get_description:
2838
* @format: a #GdkPixbufFormat
2840
* Returns a description of the format.
2842
* Return value: a description of the format.
2847
gdk_pixbuf_format_get_description (GdkPixbufFormat *format)
2850
const gchar *description;
2851
g_return_val_if_fail (format != NULL, NULL);
2853
if (format->domain != NULL)
2854
domain = format->domain;
2856
domain = GETTEXT_PACKAGE;
2857
description = g_dgettext (domain, format->description);
2859
return g_strdup (description);
2863
* gdk_pixbuf_format_get_mime_types:
2864
* @format: a #GdkPixbufFormat
2866
* Returns the mime types supported by the format.
2868
* Return value: (transfer full): a %NULL-terminated array of mime types which must be freed with
2869
* g_strfreev() when it is no longer needed.
2874
gdk_pixbuf_format_get_mime_types (GdkPixbufFormat *format)
2876
g_return_val_if_fail (format != NULL, NULL);
2878
return g_strdupv (format->mime_types);
2882
* gdk_pixbuf_format_get_extensions:
2883
* @format: a #GdkPixbufFormat
2885
* Returns the filename extensions typically used for files in the
2888
* Return value: (transfer full): a %NULL-terminated array of filename extensions which must be
2889
* freed with g_strfreev() when it is no longer needed.
2894
gdk_pixbuf_format_get_extensions (GdkPixbufFormat *format)
2896
g_return_val_if_fail (format != NULL, NULL);
2898
return g_strdupv (format->extensions);
2902
* gdk_pixbuf_format_is_writable:
2903
* @format: a #GdkPixbufFormat
2905
* Returns whether pixbufs can be saved in the given format.
2907
* Return value: whether pixbufs can be saved in the given format.
2912
gdk_pixbuf_format_is_writable (GdkPixbufFormat *format)
2914
g_return_val_if_fail (format != NULL, FALSE);
2916
return (format->flags & GDK_PIXBUF_FORMAT_WRITABLE) != 0;
2920
* gdk_pixbuf_format_is_scalable:
2921
* @format: a #GdkPixbufFormat
2923
* Returns whether this image format is scalable. If a file is in a
2924
* scalable format, it is preferable to load it at the desired size,
2925
* rather than loading it at the default size and scaling the
2926
* resulting pixbuf to the desired size.
2928
* Return value: whether this image format is scalable.
2933
gdk_pixbuf_format_is_scalable (GdkPixbufFormat *format)
2935
g_return_val_if_fail (format != NULL, FALSE);
2937
return (format->flags & GDK_PIXBUF_FORMAT_SCALABLE) != 0;
2941
* gdk_pixbuf_format_is_disabled:
2942
* @format: a #GdkPixbufFormat
2944
* Returns whether this image format is disabled. See
2945
* gdk_pixbuf_format_set_disabled().
2947
* Return value: whether this image format is disabled.
2952
gdk_pixbuf_format_is_disabled (GdkPixbufFormat *format)
2954
g_return_val_if_fail (format != NULL, FALSE);
2956
return format->disabled;
2960
* gdk_pixbuf_format_set_disabled:
2961
* @format: a #GdkPixbufFormat
2962
* @disabled: %TRUE to disable the format @format
2964
* Disables or enables an image format. If a format is disabled,
2965
* gdk-pixbuf won't use the image loader for this format to load
2966
* images. Applications can use this to avoid using image loaders
2967
* with an inappropriate license, see gdk_pixbuf_format_get_license().
2972
gdk_pixbuf_format_set_disabled (GdkPixbufFormat *format,
2975
g_return_if_fail (format != NULL);
2977
format->disabled = disabled != FALSE;
2981
* gdk_pixbuf_format_get_license:
2982
* @format: a #GdkPixbufFormat
2984
* Returns information about the license of the image loader for the format. The
2985
* returned string should be a shorthand for a wellknown license, e.g. "LGPL",
2986
* "GPL", "QPL", "GPL/QPL", or "other" to indicate some other license. This
2987
* string should be freed with g_free() when it's no longer needed.
2989
* Returns: a string describing the license of @format.
2994
gdk_pixbuf_format_get_license (GdkPixbufFormat *format)
2996
g_return_val_if_fail (format != NULL, NULL);
2998
return g_strdup (format->license);
3002
_gdk_pixbuf_get_format (GdkPixbufModule *module)
3004
g_return_val_if_fail (module != NULL, NULL);
3006
return module->info;
3010
* gdk_pixbuf_get_formats:
3012
* Obtains the available information about the image formats supported
3015
* Returns: (transfer container) (element-type GdkPixbufFormat): A list of
3016
* #GdkPixbufFormat<!-- -->s describing the supported
3017
* image formats. The list should be freed when it is no longer needed,
3018
* but the structures themselves are owned by #GdkPixbuf and should not be
3024
gdk_pixbuf_get_formats (void)
3026
GSList *result = NULL;
3029
for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
3030
GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
3031
GdkPixbufFormat *info = _gdk_pixbuf_get_format (module);
3032
result = g_slist_prepend (result, info);
3039
* gdk_pixbuf_format_copy:
3040
* @format: a #GdkPixbufFormat
3042
* Creates a copy of @format
3044
* Return value: the newly allocated copy of a #GdkPixbufFormat. Use
3045
* gdk_pixbuf_format_free() to free the resources when done
3050
gdk_pixbuf_format_copy (const GdkPixbufFormat *format)
3052
if (G_LIKELY (format != NULL))
3053
return g_slice_dup (GdkPixbufFormat, format);
3059
* gdk_pixbuf_format_free:
3060
* @format: a #GdkPixbufFormat
3062
* Frees the resources allocated when copying a #GdkPixbufFormat
3063
* using gdk_pixbuf_format_copy()
3068
gdk_pixbuf_format_free (GdkPixbufFormat *format)
3070
if (G_LIKELY (format != NULL))
3071
g_slice_free (GdkPixbufFormat, format);
3074
G_DEFINE_BOXED_TYPE (GdkPixbufFormat, gdk_pixbuf_format,
3075
gdk_pixbuf_format_copy,
3076
gdk_pixbuf_format_free)