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-io.h"
40 #include "gdk-pixbuf-loader.h"
41 #include "gdk-pixbuf-alias.h"
43 #include <glib/gstdio.h>
51 #define SNIFF_BUFFER_SIZE 4096
52 #define LOAD_BUFFER_SIZE 65536
55 format_check (GdkPixbufModule *module, guchar *buffer, int size)
59 GdkPixbufModulePattern *pattern;
64 for (pattern = module->info->signature; pattern->prefix; pattern++) {
65 if (pattern->mask && pattern->mask[0] == '*') {
66 prefix = (guchar *)pattern->prefix + 1;
67 mask = pattern->mask + 1;
71 prefix = (guchar *)pattern->prefix;
75 for (i = 0; i < size; i++) {
76 for (j = 0; i + j < size && prefix[j] != 0; j++) {
77 m = mask ? mask[j] : ' ';
79 if (buffer[i + j] != prefix[j])
83 if (buffer[i + j] == prefix[j])
87 if (buffer[i + j] != 0)
91 if (buffer[i + j] == 0)
97 return pattern->relevance;
106 G_LOCK_DEFINE_STATIC (init_lock);
107 G_LOCK_DEFINE_STATIC (threadunsafe_loader_lock);
110 _gdk_pixbuf_lock (GdkPixbufModule *image_module)
112 if (g_threads_got_initialized &&
113 !(image_module->info->flags & GDK_PIXBUF_FORMAT_THREADSAFE)) {
114 G_LOCK (threadunsafe_loader_lock);
123 _gdk_pixbuf_unlock (GdkPixbufModule *image_module)
125 if (!(image_module->info->flags & GDK_PIXBUF_FORMAT_THREADSAFE)) {
126 G_UNLOCK (threadunsafe_loader_lock);
130 static GSList *file_formats = NULL;
132 static void gdk_pixbuf_io_init (void);
135 get_file_formats (void)
138 if (file_formats == NULL)
139 gdk_pixbuf_io_init ();
140 G_UNLOCK (init_lock);
149 scan_string (const char **pos, GString *out)
151 const char *p = *pos, *q = *pos;
155 while (g_ascii_isspace (*p))
160 else if (*p == '"') {
163 for (q = p; (*q != '"') || quoted; q++) {
166 quoted = (*q == '\\') && !quoted;
169 tmp = g_strndup (p, q - p);
170 tmp2 = g_strcompress (tmp);
171 g_string_truncate (out, 0);
172 g_string_append (out, tmp2);
184 scan_int (const char **pos, int *out)
188 const char *p = *pos;
190 while (g_ascii_isspace (*p))
193 if (*p < '0' || *p > '9')
196 while ((*p >= '0') && (*p <= '9') && i < sizeof (buf)) {
202 if (i == sizeof (buf))
215 skip_space (const char **pos)
217 const char *p = *pos;
219 while (g_ascii_isspace (*p))
224 return !(*p == '\0');
229 /* DllMain function needed to tuck away the gdk-pixbuf DLL name */
230 G_WIN32_DLLMAIN_FOR_DLL_NAME (static, dll_name)
235 static char *toplevel = NULL;
237 if (toplevel == NULL)
238 toplevel = g_win32_get_package_installation_subdirectory
239 (GETTEXT_PACKAGE, dll_name, "");
245 get_sysconfdir (void)
247 static char *sysconfdir = NULL;
249 if (sysconfdir == NULL)
250 sysconfdir = g_win32_get_package_installation_subdirectory
251 (GETTEXT_PACKAGE, dll_name, "etc");
256 #undef GTK_SYSCONFDIR
257 #define GTK_SYSCONFDIR get_sysconfdir()
260 correct_prefix (gchar **path)
262 if (strncmp (*path, GTK_PREFIX "/", strlen (GTK_PREFIX "/")) == 0 ||
263 strncmp (*path, GTK_PREFIX "\\", strlen (GTK_PREFIX "\\")) == 0)
265 /* This is an entry put there by gdk-pixbuf-query-loaders on the
266 * packager's system. On Windows a prebuilt GTK+ package can be
267 * installed in a random location. The gdk-pixbuf.loaders file
268 * distributed in such a package contains paths from the package
269 * builder's machine. Replace the build-time prefix with the
270 * installation prefix on this machine.
273 *path = g_strconcat (get_toplevel (), tem + strlen (GTK_PREFIX), NULL);
278 #endif /* G_OS_WIN32 */
281 gdk_pixbuf_get_module_file (void)
283 gchar *result = g_strdup (g_getenv ("GDK_PIXBUF_MODULE_FILE"));
286 result = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gdk-pixbuf.loaders", NULL);
291 #endif /* USE_GMODULE */
294 gdk_pixbuf_io_init (void)
300 GString *tmp_buf = g_string_new (NULL);
301 gboolean have_error = FALSE;
302 GdkPixbufModule *module = NULL;
303 gchar *filename = gdk_pixbuf_get_module_file ();
306 GdkPixbufModulePattern *pattern;
307 GError *error = NULL;
309 GdkPixbufModule *builtin_module ;
311 /* initialize on separate line to avoid compiler warnings in the
312 * common case of no compiled-in modules.
314 builtin_module = NULL;
316 #define load_one_builtin_module(format) \
317 builtin_module = g_new0 (GdkPixbufModule, 1); \
318 builtin_module->module_name = #format; \
319 if (_gdk_pixbuf_load_module (builtin_module, NULL)) \
320 file_formats = g_slist_prepend (file_formats, builtin_module);\
322 g_free (builtin_module)
325 load_one_builtin_module (ani);
328 load_one_builtin_module (png);
331 load_one_builtin_module (bmp);
334 load_one_builtin_module (wbmp);
337 load_one_builtin_module (gif);
340 load_one_builtin_module (ico);
343 load_one_builtin_module (jpeg);
346 load_one_builtin_module (pnm);
349 load_one_builtin_module (ras);
352 load_one_builtin_module (tiff);
355 load_one_builtin_module (xpm);
358 load_one_builtin_module (xbm);
361 load_one_builtin_module (tga);
364 load_one_builtin_module (pcx);
367 load_one_builtin_module (icns);
369 #ifdef INCLUDE_jasper
370 load_one_builtin_module (jasper);
372 #ifdef INCLUDE_gdiplus
373 /* We don't bother having the GDI+ loaders individually selectable
374 * for building in or not.
376 load_one_builtin_module (ico);
377 load_one_builtin_module (wmf);
378 load_one_builtin_module (emf);
379 load_one_builtin_module (bmp);
380 load_one_builtin_module (gif);
381 load_one_builtin_module (jpeg);
382 load_one_builtin_module (tiff);
384 #ifdef INCLUDE_gdip_png
385 /* Except the gdip-png loader which normally isn't built at all even */
386 load_one_builtin_module (png);
389 #undef load_one_builtin_module
392 channel = g_io_channel_new_file (filename, "r", &error);
394 /* Don't bother warning if we have some built-in loaders */
395 if (file_formats == NULL)
396 g_warning ("Cannot open pixbuf loader module file '%s': %s",
397 filename, error->message);
401 while (!have_error && g_io_channel_read_line (channel, &line_buf, NULL, &term, NULL) == G_IO_STATUS_NORMAL) {
408 if (!skip_space (&p)) {
409 /* Blank line marking the end of a module
411 if (module && *p != '#') {
413 correct_prefix (&module->module_path);
415 file_formats = g_slist_prepend (file_formats, module);
426 /* Read a module location
428 module = g_new0 (GdkPixbufModule, 1);
431 if (!scan_string (&p, tmp_buf)) {
432 g_warning ("Error parsing loader info in '%s'\n %s",
436 module->module_path = g_strdup (tmp_buf->str);
438 else if (!module->module_name) {
439 module->info = g_new0 (GdkPixbufFormat, 1);
440 if (!scan_string (&p, tmp_buf)) {
441 g_warning ("Error parsing loader info in '%s'\n %s",
445 module->info->name = g_strdup (tmp_buf->str);
446 module->module_name = module->info->name;
448 if (!scan_int (&p, &flags)) {
449 g_warning ("Error parsing loader info in '%s'\n %s",
453 module->info->flags = flags;
455 if (!scan_string (&p, tmp_buf)) {
456 g_warning ("Error parsing loader info in '%s'\n %s",
460 if (tmp_buf->str[0] != 0)
461 module->info->domain = g_strdup (tmp_buf->str);
463 if (!scan_string (&p, tmp_buf)) {
464 g_warning ("Error parsing loader info in '%s'\n %s",
468 module->info->description = g_strdup (tmp_buf->str);
470 else if (!module->info->mime_types) {
472 module->info->mime_types = g_new0 (gchar*, 1);
473 while (scan_string (&p, tmp_buf)) {
474 if (tmp_buf->str[0] != 0) {
475 module->info->mime_types =
476 g_realloc (module->info->mime_types, (n + 1) * sizeof (gchar*));
477 module->info->mime_types[n - 1] = g_strdup (tmp_buf->str);
478 module->info->mime_types[n] = NULL;
483 else if (!module->info->extensions) {
485 module->info->extensions = g_new0 (gchar*, 1);
486 while (scan_string (&p, tmp_buf)) {
487 if (tmp_buf->str[0] != 0) {
488 module->info->extensions =
489 g_realloc (module->info->extensions, (n + 1) * sizeof (gchar*));
490 module->info->extensions[n - 1] = g_strdup (tmp_buf->str);
491 module->info->extensions[n] = NULL;
498 module->info->signature = (GdkPixbufModulePattern *)
499 g_realloc (module->info->signature, (n_patterns + 1) * sizeof (GdkPixbufModulePattern));
500 pattern = module->info->signature + n_patterns;
501 pattern->prefix = NULL;
502 pattern->mask = NULL;
503 pattern->relevance = 0;
505 if (!scan_string (&p, tmp_buf))
507 pattern->prefix = g_strdup (tmp_buf->str);
509 if (!scan_string (&p, tmp_buf))
512 pattern->mask = g_strdup (tmp_buf->str);
514 pattern->mask = NULL;
516 if (!scan_int (&p, &pattern->relevance))
522 g_free (pattern->prefix);
523 g_free (pattern->mask);
525 g_warning ("Error parsing loader info in '%s'\n %s",
532 g_string_free (tmp_buf, TRUE);
533 g_io_channel_unref (channel);
540 /* actually load the image handler - gdk_pixbuf_get_module only get a */
541 /* reference to the module to load, it doesn't actually load it */
542 /* perhaps these actions should be combined in one function */
544 _gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
551 g_return_val_if_fail (image_module->module == NULL, FALSE);
553 path = image_module->module_path;
554 module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
559 GDK_PIXBUF_ERROR_FAILED,
560 _("Unable to load image-loading module: %s: %s"),
561 path, g_module_error ());
565 image_module->module = module;
567 if (g_module_symbol (module, "fill_vtable", &sym)) {
568 GdkPixbufModuleFillVtableFunc func = (GdkPixbufModuleFillVtableFunc) sym;
569 (* func) (image_module);
574 GDK_PIXBUF_ERROR_FAILED,
575 _("Image-loading module %s does not export the proper interface; perhaps it's from a different GTK version?"),
581 #endif /* !USE_GMODULE */
583 #define module(type) \
584 extern void _gdk_pixbuf__##type##_fill_info (GdkPixbufFormat *info); \
585 extern void _gdk_pixbuf__##type##_fill_vtable (GdkPixbufModule *module)
615 _gdk_pixbuf_load_module (GdkPixbufModule *image_module,
619 gboolean locked = FALSE;
620 GdkPixbufModuleFillInfoFunc fill_info = NULL;
621 GdkPixbufModuleFillVtableFunc fill_vtable = NULL;
623 #define try_module(format,id) \
624 if (fill_info == NULL && \
625 strcmp (image_module->module_name, #format) == 0) { \
626 fill_info = _gdk_pixbuf__##id##_fill_info; \
627 fill_vtable = _gdk_pixbuf__##id##_fill_vtable; \
630 try_module (png,png);
633 try_module (bmp,bmp);
636 try_module (wbmp,wbmp);
639 try_module (gif,gif);
642 try_module (ico,ico);
645 try_module (ani,ani);
648 try_module (jpeg,jpeg);
651 try_module (pnm,pnm);
654 try_module (ras,ras);
657 try_module (tiff,tiff);
660 try_module (xpm,xpm);
663 try_module (xbm,xbm);
666 try_module (tga,tga);
669 try_module (pcx,pcx);
672 try_module (icns,icns);
674 #ifdef INCLUDE_jasper
675 try_module (jasper,jasper);
677 #ifdef INCLUDE_gdiplus
678 try_module (ico,gdip_ico);
679 try_module (wmf,gdip_wmf);
680 try_module (emf,gdip_emf);
681 try_module (bmp,gdip_bmp);
682 try_module (gif,gdip_gif);
683 try_module (jpeg,gdip_jpeg);
684 try_module (tiff,gdip_tiff);
686 #ifdef INCLUDE_gdip_png
687 try_module (png,gdip_png);
693 image_module->module = (void *) 1;
694 (* fill_vtable) (image_module);
695 image_module->info = g_new0 (GdkPixbufFormat, 1);
696 (* fill_info) (image_module->info);
703 /* be extra careful, maybe the module initializes
706 if (g_threads_got_initialized)
711 ret = _gdk_pixbuf_load_module_unlocked (image_module, error);
713 G_UNLOCK (init_lock);
719 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
720 _("Image type '%s' is not supported"),
721 image_module->module_name);
731 _gdk_pixbuf_get_named_module (const char *name,
736 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
737 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
739 if (module->info->disabled)
742 if (!strcmp (name, module->module_name))
748 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
749 _("Image type '%s' is not supported"),
756 _gdk_pixbuf_get_module (guchar *buffer, guint size,
757 const gchar *filename,
762 gint score, best = 0;
763 GdkPixbufModule *selected = NULL;
764 gchar *display_name = NULL;
766 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
767 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
769 if (module->info->disabled)
772 score = format_check (module, buffer, size);
780 if (selected != NULL)
785 display_name = g_filename_display_name (filename);
788 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
789 _("Couldn't recognize the image file format for file '%s'"),
791 g_free (display_name);
796 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
797 _("Unrecognized image file format"));
805 prepared_notify (GdkPixbuf *pixbuf,
806 GdkPixbufAnimation *anim,
810 g_object_ref (pixbuf);
811 *((GdkPixbuf **)user_data) = pixbuf;
815 _gdk_pixbuf_generic_image_load (GdkPixbufModule *module,
819 guchar buffer[LOAD_BUFFER_SIZE];
821 GdkPixbuf *pixbuf = NULL;
822 GdkPixbufAnimation *animation = NULL;
826 locked = _gdk_pixbuf_lock (module);
828 if (module->load != NULL) {
829 pixbuf = (* module->load) (f, error);
830 } else if (module->begin_load != NULL) {
832 context = module->begin_load (NULL, prepared_notify, NULL, &pixbuf, error);
837 while (!feof (f) && !ferror (f)) {
838 length = fread (buffer, 1, sizeof (buffer), f);
840 if (!module->load_increment (context, buffer, length, error)) {
841 module->stop_load (context, NULL);
842 if (pixbuf != NULL) {
843 g_object_unref (pixbuf);
850 if (!module->stop_load (context, error)) {
851 if (pixbuf != NULL) {
852 g_object_unref (pixbuf);
856 } else if (module->load_animation != NULL) {
857 animation = (* module->load_animation) (f, error);
858 if (animation != NULL) {
859 pixbuf = gdk_pixbuf_animation_get_static_image (animation);
861 g_object_ref (pixbuf);
862 g_object_unref (animation);
868 _gdk_pixbuf_unlock (module);
873 * gdk_pixbuf_new_from_file:
874 * @filename: Name of file to load, in the GLib file name encoding
875 * @error: Return location for an error
877 * Creates a new pixbuf by loading an image from a file. The file format is
878 * detected automatically. If %NULL is returned, then @error will be set.
879 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
881 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
882 * any of several error conditions occurred: the file could not be opened,
883 * there was no loader for the file's format, there was not enough memory to
884 * allocate the image buffer, or the image file contained invalid data.
887 gdk_pixbuf_new_from_file (const char *filename,
893 guchar buffer[SNIFF_BUFFER_SIZE];
894 GdkPixbufModule *image_module;
897 g_return_val_if_fail (filename != NULL, NULL);
898 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
900 display_name = g_filename_display_name (filename);
902 f = g_fopen (filename, "rb");
904 gint save_errno = errno;
907 g_file_error_from_errno (save_errno),
908 _("Failed to open file '%s': %s"),
910 g_strerror (save_errno));
911 g_free (display_name);
915 size = fread (&buffer, 1, sizeof (buffer), f);
919 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
920 _("Image file '%s' contains no data"),
922 g_free (display_name);
927 image_module = _gdk_pixbuf_get_module (buffer, size, filename, error);
928 if (image_module == NULL) {
929 g_free (display_name);
934 if (image_module->module == NULL)
935 if (!_gdk_pixbuf_load_module (image_module, error)) {
936 g_free (display_name);
941 fseek (f, 0, SEEK_SET);
942 pixbuf = _gdk_pixbuf_generic_image_load (image_module, f, error);
945 if (pixbuf == NULL && error != NULL && *error == NULL) {
947 /* I don't trust these crufty longjmp()'ing image libs
948 * to maintain proper error invariants, and I don't
949 * want user code to segfault as a result. We need to maintain
950 * the invariant that error gets set if NULL is returned.
953 g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.", image_module->module_name);
956 GDK_PIXBUF_ERROR_FAILED,
957 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
959 } else if (error != NULL && *error != NULL) {
961 /* Add the filename to the error message */
966 e->message = g_strdup_printf (_("Failed to load image '%s': %s"),
972 g_free (display_name);
978 #undef gdk_pixbuf_new_from_file
980 gdk_pixbuf_new_from_file (const char *filename,
983 gchar *utf8_filename =
984 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
987 if (utf8_filename == NULL)
990 retval = gdk_pixbuf_new_from_file_utf8 (utf8_filename, error);
992 g_free (utf8_filename);
1000 * gdk_pixbuf_new_from_file_at_size:
1001 * @filename: Name of file to load, in the GLib file name encoding
1002 * @width: The width the image should have or -1 to not constrain the width
1003 * @height: The height the image should have or -1 to not constrain the height
1004 * @error: Return location for an error
1006 * Creates a new pixbuf by loading an image from a file. The file format is
1007 * detected automatically. If %NULL is returned, then @error will be set.
1008 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
1009 * The image will be scaled to fit in the requested size, preserving
1010 * the image's aspect ratio.
1012 * Return value: A newly-created pixbuf with a reference count of 1, or
1013 * %NULL if any of several error conditions occurred: the file could not
1014 * be opened, there was no loader for the file's format, there was not
1015 * enough memory to allocate the image buffer, or the image file contained
1021 gdk_pixbuf_new_from_file_at_size (const char *filename,
1026 return gdk_pixbuf_new_from_file_at_scale (filename,
1033 #undef gdk_pixbuf_new_from_file_at_size
1036 gdk_pixbuf_new_from_file_at_size (const char *filename,
1041 gchar *utf8_filename =
1042 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1045 if (utf8_filename == NULL)
1048 retval = gdk_pixbuf_new_from_file_at_size_utf8 (utf8_filename,
1052 g_free (utf8_filename);
1061 gboolean preserve_aspect_ratio;
1065 at_scale_size_prepared_cb (GdkPixbufLoader *loader,
1070 AtScaleData *info = data;
1072 g_return_if_fail (width > 0 && height > 0);
1074 if (info->preserve_aspect_ratio &&
1075 (info->width > 0 || info->height > 0)) {
1076 if (info->width < 0)
1078 width = width * (double)info->height/(double)height;
1079 height = info->height;
1081 else if (info->height < 0)
1083 height = height * (double)info->width/(double)width;
1084 width = info->width;
1086 else if ((double)height * (double)info->width >
1087 (double)width * (double)info->height) {
1088 width = 0.5 + (double)width * (double)info->height / (double)height;
1089 height = info->height;
1091 height = 0.5 + (double)height * (double)info->width / (double)width;
1092 width = info->width;
1095 if (info->width > 0)
1096 width = info->width;
1097 if (info->height > 0)
1098 height = info->height;
1101 width = MAX (width, 1);
1102 height = MAX (height, 1);
1104 gdk_pixbuf_loader_set_size (loader, width, height);
1108 * gdk_pixbuf_new_from_file_at_scale:
1109 * @filename: Name of file to load, in the GLib file name encoding
1110 * @width: The width the image should have or -1 to not constrain the width
1111 * @height: The height the image should have or -1 to not constrain the height
1112 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1113 * @error: Return location for an error
1115 * Creates a new pixbuf by loading an image from a file. The file format is
1116 * detected automatically. If %NULL is returned, then @error will be set.
1117 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
1118 * The image will be scaled to fit in the requested size, optionally preserving
1119 * the image's aspect ratio.
1121 * When preserving the aspect ratio, a @width of -1 will cause the image
1122 * to be scaled to the exact given height, and a @height of -1 will cause
1123 * the image to be scaled to the exact given width. When not preserving
1124 * aspect ratio, a @width or @height of -1 means to not scale the image
1125 * at all in that dimension. Negative values for @width and @height are
1126 * allowed since 2.8.
1128 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL
1129 * if any of several error conditions occurred: the file could not be opened,
1130 * there was no loader for the file's format, there was not enough memory to
1131 * allocate the image buffer, or the image file contained invalid data.
1136 gdk_pixbuf_new_from_file_at_scale (const char *filename,
1139 gboolean preserve_aspect_ratio,
1143 GdkPixbufLoader *loader;
1145 guchar buffer[LOAD_BUFFER_SIZE];
1149 GdkPixbufAnimation *animation;
1150 GdkPixbufAnimationIter *iter;
1153 g_return_val_if_fail (filename != NULL, NULL);
1154 g_return_val_if_fail (width > 0 || width == -1, NULL);
1155 g_return_val_if_fail (height > 0 || height == -1, NULL);
1157 f = g_fopen (filename, "rb");
1159 gint save_errno = errno;
1160 gchar *display_name = g_filename_display_name (filename);
1163 g_file_error_from_errno (save_errno),
1164 _("Failed to open file '%s': %s"),
1166 g_strerror (save_errno));
1167 g_free (display_name);
1171 loader = gdk_pixbuf_loader_new ();
1174 info.height = height;
1175 info.preserve_aspect_ratio = preserve_aspect_ratio;
1177 g_signal_connect (loader, "size-prepared",
1178 G_CALLBACK (at_scale_size_prepared_cb), &info);
1181 while (!has_frame && !feof (f) && !ferror (f)) {
1182 length = fread (buffer, 1, sizeof (buffer), f);
1184 if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
1185 gdk_pixbuf_loader_close (loader, NULL);
1187 g_object_unref (loader);
1191 animation = gdk_pixbuf_loader_get_animation (loader);
1193 iter = gdk_pixbuf_animation_get_iter (animation, NULL);
1194 if (!gdk_pixbuf_animation_iter_on_currently_loading_frame (iter)) {
1197 g_object_unref (iter);
1203 if (!gdk_pixbuf_loader_close (loader, error) && !has_frame) {
1204 g_object_unref (loader);
1208 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1211 gchar *display_name = g_filename_display_name (filename);
1212 g_object_unref (loader);
1215 GDK_PIXBUF_ERROR_FAILED,
1216 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1218 g_free (display_name);
1222 g_object_ref (pixbuf);
1224 g_object_unref (loader);
1231 #undef gdk_pixbuf_new_from_file_at_scale
1234 gdk_pixbuf_new_from_file_at_scale (const char *filename,
1237 gboolean preserve_aspect_ratio,
1240 gchar *utf8_filename =
1241 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1244 if (utf8_filename == NULL)
1247 retval = gdk_pixbuf_new_from_file_at_scale_utf8 (utf8_filename,
1249 preserve_aspect_ratio,
1252 g_free (utf8_filename);
1260 load_from_stream (GdkPixbufLoader *loader,
1261 GInputStream *stream,
1262 GCancellable *cancellable,
1267 guchar buffer[LOAD_BUFFER_SIZE];
1272 n_read = g_input_stream_read (stream,
1279 error = NULL; /* Ignore further errors */
1286 if (!gdk_pixbuf_loader_write (loader,
1296 if (!gdk_pixbuf_loader_close (loader, error)) {
1303 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1305 g_object_ref (pixbuf);
1313 * gdk_pixbuf_new_from_stream_at_scale:
1314 * @stream: a #GInputStream to load the pixbuf from
1315 * @width: The width the image should have or -1 to not constrain the width
1316 * @height: The height the image should have or -1 to not constrain the height
1317 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1318 * @cancellable: optional #GCancellable object, %NULL to ignore
1319 * @error: Return location for an error
1321 * Creates a new pixbuf by loading an image from an input stream.
1323 * The file format is detected automatically. If %NULL is returned, then
1324 * @error will be set. The @cancellable can be used to abort the operation
1325 * from another thread. If the operation was cancelled, the error
1326 * %GIO_ERROR_CANCELLED will be returned. Other possible errors are in
1327 * the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
1329 * The image will be scaled to fit in the requested size, optionally
1330 * preserving the image's aspect ratio. When preserving the aspect ratio,
1331 * a @width of -1 will cause the image to be scaled to the exact given
1332 * height, and a @height of -1 will cause the image to be scaled to the
1333 * exact given width. When not preserving aspect ratio, a @width or
1334 * @height of -1 means to not scale the image at all in that dimension.
1336 * The stream is not closed.
1338 * Return value: A newly-created pixbuf, or %NULL if any of several error
1339 * conditions occurred: the file could not be opened, the image format is
1340 * not supported, there was not enough memory to allocate the image buffer,
1341 * the stream contained invalid data, or the operation was cancelled.
1346 gdk_pixbuf_new_from_stream_at_scale (GInputStream *stream,
1349 gboolean preserve_aspect_ratio,
1350 GCancellable *cancellable,
1353 GdkPixbufLoader *loader;
1357 loader = gdk_pixbuf_loader_new ();
1360 info.height = height;
1361 info.preserve_aspect_ratio = preserve_aspect_ratio;
1363 g_signal_connect (loader, "size-prepared",
1364 G_CALLBACK (at_scale_size_prepared_cb), &info);
1366 pixbuf = load_from_stream (loader, stream, cancellable, error);
1367 g_object_unref (loader);
1373 * gdk_pixbuf_new_from_stream:
1374 * @stream: a #GInputStream to load the pixbuf from
1375 * @cancellable: optional #GCancellable object, %NULL to ignore
1376 * @error: Return location for an error
1378 * Creates a new pixbuf by loading an image from an input stream.
1380 * The file format is detected automatically. If %NULL is returned, then
1381 * @error will be set. The @cancellable can be used to abort the operation
1382 * from another thread. If the operation was cancelled, the error
1383 * %GIO_ERROR_CANCELLED will be returned. Other possible errors are in
1384 * the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
1386 * The stream is not closed.
1388 * Return value: A newly-created pixbuf, or %NULL if any of several error
1389 * conditions occurred: the file could not be opened, the image format is
1390 * not supported, there was not enough memory to allocate the image buffer,
1391 * the stream contained invalid data, or the operation was cancelled.
1396 gdk_pixbuf_new_from_stream (GInputStream *stream,
1397 GCancellable *cancellable,
1401 GdkPixbufLoader *loader;
1403 loader = gdk_pixbuf_loader_new ();
1404 pixbuf = load_from_stream (loader, stream, cancellable, error);
1405 g_object_unref (loader);
1411 info_cb (GdkPixbufLoader *loader,
1417 GdkPixbufFormat *format;
1422 g_return_if_fail (width > 0 && height > 0);
1424 info->format = gdk_pixbuf_loader_get_format (loader);
1425 info->width = width;
1426 info->height = height;
1428 gdk_pixbuf_loader_set_size (loader, 0, 0);
1432 * gdk_pixbuf_get_file_info:
1433 * @filename: The name of the file to identify.
1434 * @width: Return location for the width of the image, or %NULL
1435 * @height: Return location for the height of the image, or %NULL
1437 * Parses an image file far enough to determine its format and size.
1439 * Returns: A #GdkPixbufFormat describing the image format of the file
1440 * or %NULL if the image format wasn't recognized. The return value
1441 * is owned by GdkPixbuf and should not be freed.
1446 gdk_pixbuf_get_file_info (const gchar *filename,
1450 GdkPixbufLoader *loader;
1451 guchar buffer[SNIFF_BUFFER_SIZE];
1455 GdkPixbufFormat *format;
1460 g_return_val_if_fail (filename != NULL, NULL);
1462 f = g_fopen (filename, "rb");
1466 loader = gdk_pixbuf_loader_new ();
1472 g_signal_connect (loader, "size-prepared", G_CALLBACK (info_cb), &info);
1474 while (!feof (f) && !ferror (f)) {
1475 length = fread (buffer, 1, sizeof (buffer), f);
1477 if (!gdk_pixbuf_loader_write (loader, buffer, length, NULL))
1480 if (info.format != NULL)
1485 gdk_pixbuf_loader_close (loader, NULL);
1486 g_object_unref (loader);
1489 *width = info.width;
1491 *height = info.height;
1497 * gdk_pixbuf_new_from_xpm_data:
1498 * @data: Pointer to inline XPM data.
1500 * Creates a new pixbuf by parsing XPM data in memory. This data is commonly
1501 * the result of including an XPM file into a program's C source.
1503 * Return value: A newly-created pixbuf with a reference count of 1.
1506 gdk_pixbuf_new_from_xpm_data (const char **data)
1508 GdkPixbuf *(* load_xpm_data) (const char **data);
1510 GError *error = NULL;
1511 GdkPixbufModule *xpm_module;
1514 g_return_val_if_fail (data != NULL, NULL);
1516 xpm_module = _gdk_pixbuf_get_named_module ("xpm", &error);
1517 if (xpm_module == NULL) {
1518 g_warning ("Error loading XPM image loader: %s", error->message);
1519 g_error_free (error);
1523 if (xpm_module->module == NULL) {
1524 if (!_gdk_pixbuf_load_module (xpm_module, &error)) {
1525 g_warning ("Error loading XPM image loader: %s", error->message);
1526 g_error_free (error);
1531 locked = _gdk_pixbuf_lock (xpm_module);
1533 if (xpm_module->load_xpm_data == NULL) {
1534 g_warning ("gdk-pixbuf XPM module lacks XPM data capability");
1537 load_xpm_data = xpm_module->load_xpm_data;
1538 pixbuf = (* load_xpm_data) (data);
1542 _gdk_pixbuf_unlock (xpm_module);
1547 collect_save_options (va_list opts,
1560 next = va_arg (opts, gchar*);
1564 val = va_arg (opts, gchar*);
1569 *keys = g_realloc (*keys, sizeof(gchar*) * (count + 1));
1570 *vals = g_realloc (*vals, sizeof(gchar*) * (count + 1));
1572 (*keys)[count-1] = g_strdup (key);
1573 (*vals)[count-1] = g_strdup (val);
1575 (*keys)[count] = NULL;
1576 (*vals)[count] = NULL;
1578 next = va_arg (opts, gchar*);
1583 save_to_file_callback (const gchar *buf,
1588 FILE *filehandle = data;
1591 n = fwrite (buf, 1, count, filehandle);
1593 gint save_errno = errno;
1596 g_file_error_from_errno (save_errno),
1597 _("Error writing to image file: %s"),
1598 g_strerror (save_errno));
1605 gdk_pixbuf_real_save (GdkPixbuf *pixbuf,
1613 GdkPixbufModule *image_module = NULL;
1616 image_module = _gdk_pixbuf_get_named_module (type, error);
1618 if (image_module == NULL)
1621 if (image_module->module == NULL)
1622 if (!_gdk_pixbuf_load_module (image_module, error))
1625 locked = _gdk_pixbuf_lock (image_module);
1627 if (image_module->save) {
1629 ret = (* image_module->save) (filehandle, pixbuf,
1632 } else if (image_module->save_to_callback) {
1633 /* save with simple callback */
1634 ret = (* image_module->save_to_callback) (save_to_file_callback,
1642 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
1643 _("This build of gdk-pixbuf does not support saving the image format: %s"),
1649 _gdk_pixbuf_unlock (image_module);
1653 #define TMP_FILE_BUF_SIZE 4096
1656 save_to_callback_with_tmp_file (GdkPixbufModule *image_module,
1658 GdkPixbufSaveFunc save_func,
1666 gboolean retval = FALSE;
1669 gchar *filename = NULL;
1672 buf = g_try_malloc (TMP_FILE_BUF_SIZE);
1676 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1677 _("Insufficient memory to save image to callback"));
1681 fd = g_file_open_tmp ("gdkpixbuf-save-tmp.XXXXXX", &filename, error);
1684 f = fdopen (fd, "wb+");
1686 gint save_errno = errno;
1689 g_file_error_from_errno (save_errno),
1690 _("Failed to open temporary file"));
1694 locked = _gdk_pixbuf_lock (image_module);
1695 retval = (image_module->save) (f, pixbuf, keys, values, error);
1697 _gdk_pixbuf_unlock (image_module);
1703 n = fread (buf, 1, TMP_FILE_BUF_SIZE, f);
1705 if (!save_func (buf, n, error, user_data))
1708 if (n != TMP_FILE_BUF_SIZE)
1712 gint save_errno = errno;
1715 g_file_error_from_errno (save_errno),
1716 _("Failed to read from temporary file"));
1722 /* cleanup and return retval */
1726 g_unlink (filename);
1735 gdk_pixbuf_real_save_to_callback (GdkPixbuf *pixbuf,
1736 GdkPixbufSaveFunc save_func,
1744 GdkPixbufModule *image_module = NULL;
1747 image_module = _gdk_pixbuf_get_named_module (type, error);
1749 if (image_module == NULL)
1752 if (image_module->module == NULL)
1753 if (!_gdk_pixbuf_load_module (image_module, error))
1756 locked = _gdk_pixbuf_lock (image_module);
1758 if (image_module->save_to_callback) {
1760 ret = (* image_module->save_to_callback) (save_func, user_data,
1761 pixbuf, keys, values,
1763 } else if (image_module->save) {
1764 /* use a temporary file */
1765 ret = save_to_callback_with_tmp_file (image_module, pixbuf,
1766 save_func, user_data,
1773 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
1774 _("This build of gdk-pixbuf does not support saving the image format: %s"),
1780 _gdk_pixbuf_unlock (image_module);
1787 * @pixbuf: a #GdkPixbuf.
1788 * @filename: name of file to save.
1789 * @type: name of file format.
1790 * @error: return location for error, or %NULL
1791 * @Varargs: list of key-value save options
1793 * Saves pixbuf to a file in format @type. By default, "jpeg", "png", "ico"
1794 * and "bmp" are possible file formats to save in, but more formats may be
1795 * installed. The list of all writable formats can be determined in the
1799 * void add_if_writable (GdkPixbufFormat *data, GSList **list)
1801 * if (gdk_pixbuf_format_is_writable (data))
1802 * *list = g_slist_prepend (*list, data);
1805 * GSList *formats = gdk_pixbuf_get_formats ();
1806 * GSList *writable_formats = NULL;
1807 * g_slist_foreach (formats, add_if_writable, &writable_formats);
1808 * g_slist_free (formats);
1811 * If @error is set, %FALSE will be returned. Possible errors include
1812 * those in the #GDK_PIXBUF_ERROR domain and those in the #G_FILE_ERROR domain.
1814 * The variable argument list should be %NULL-terminated; if not empty,
1815 * it should contain pairs of strings that modify the save
1816 * parameters. For example:
1817 * <informalexample><programlisting>
1818 * gdk_pixbuf_save (pixbuf, handle, "jpeg", &error,
1819 * "quality", "100", NULL);
1820 * </programlisting></informalexample>
1822 * Currently only few parameters exist. JPEG images can be saved with a
1823 * "quality" parameter; its value should be in the range [0,100].
1825 * Text chunks can be attached to PNG images by specifying parameters of
1826 * the form "tEXt::key", where key is an ASCII string of length 1-79.
1827 * The values are UTF-8 encoded strings. The PNG compression level can
1828 * be specified using the "compression" parameter; it's value is in an
1829 * integer in the range of [0,9].
1831 * ICO images can be saved in depth 16, 24, or 32, by using the "depth"
1832 * parameter. When the ICO saver is given "x_hot" and "y_hot" parameters,
1833 * it produces a CUR instead of an ICO.
1835 * Return value: whether an error was set
1839 gdk_pixbuf_save (GdkPixbuf *pixbuf,
1840 const char *filename,
1845 gchar **keys = NULL;
1846 gchar **values = NULL;
1850 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1852 va_start (args, error);
1854 collect_save_options (args, &keys, &values);
1858 result = gdk_pixbuf_savev (pixbuf, filename, type,
1863 g_strfreev (values);
1870 #undef gdk_pixbuf_save
1873 gdk_pixbuf_save (GdkPixbuf *pixbuf,
1874 const char *filename,
1879 char *utf8_filename;
1880 gchar **keys = NULL;
1881 gchar **values = NULL;
1885 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1887 utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1889 if (utf8_filename == NULL)
1892 va_start (args, error);
1894 collect_save_options (args, &keys, &values);
1898 result = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
1902 g_free (utf8_filename);
1905 g_strfreev (values);
1914 * @pixbuf: a #GdkPixbuf.
1915 * @filename: name of file to save.
1916 * @type: name of file format.
1917 * @option_keys: name of options to set, %NULL-terminated
1918 * @option_values: values for named options
1919 * @error: return location for error, or %NULL
1921 * Saves pixbuf to a file in @type, which is currently "jpeg", "png", "tiff", "ico" or "bmp".
1922 * If @error is set, %FALSE will be returned.
1923 * See gdk_pixbuf_save () for more details.
1925 * Return value: whether an error was set
1929 gdk_pixbuf_savev (GdkPixbuf *pixbuf,
1930 const char *filename,
1933 char **option_values,
1939 g_return_val_if_fail (filename != NULL, FALSE);
1940 g_return_val_if_fail (type != NULL, FALSE);
1941 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1943 f = g_fopen (filename, "wb");
1946 gint save_errno = errno;
1947 gchar *display_name = g_filename_display_name (filename);
1950 g_file_error_from_errno (save_errno),
1951 _("Failed to open '%s' for writing: %s"),
1953 g_strerror (save_errno));
1954 g_free (display_name);
1959 result = gdk_pixbuf_real_save (pixbuf, f, type,
1960 option_keys, option_values,
1965 g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
1970 if (fclose (f) < 0) {
1971 gint save_errno = errno;
1972 gchar *display_name = g_filename_display_name (filename);
1975 g_file_error_from_errno (save_errno),
1976 _("Failed to close '%s' while writing image, all data may not have been saved: %s"),
1978 g_strerror (save_errno));
1979 g_free (display_name);
1988 #undef gdk_pixbuf_savev
1991 gdk_pixbuf_savev (GdkPixbuf *pixbuf,
1992 const char *filename,
1995 char **option_values,
1998 char *utf8_filename;
2001 g_return_val_if_fail (filename != NULL, FALSE);
2003 utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2005 if (utf8_filename == NULL)
2008 retval = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
2009 option_keys, option_values, error);
2011 g_free (utf8_filename);
2019 * gdk_pixbuf_save_to_callback:
2020 * @pixbuf: a #GdkPixbuf.
2021 * @save_func: a function that is called to save each block of data that
2022 * the save routine generates.
2023 * @user_data: user data to pass to the save function.
2024 * @type: name of file format.
2025 * @error: return location for error, or %NULL
2026 * @Varargs: list of key-value save options
2028 * Saves pixbuf in format @type by feeding the produced data to a
2029 * callback. Can be used when you want to store the image to something
2030 * other than a file, such as an in-memory buffer or a socket.
2031 * If @error is set, %FALSE will be returned. Possible errors
2032 * include those in the #GDK_PIXBUF_ERROR domain and whatever the save
2033 * function generates.
2035 * See gdk_pixbuf_save() for more details.
2037 * Return value: whether an error was set
2042 gdk_pixbuf_save_to_callback (GdkPixbuf *pixbuf,
2043 GdkPixbufSaveFunc save_func,
2049 gchar **keys = NULL;
2050 gchar **values = NULL;
2054 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2056 va_start (args, error);
2058 collect_save_options (args, &keys, &values);
2062 result = gdk_pixbuf_save_to_callbackv (pixbuf, save_func, user_data,
2067 g_strfreev (values);
2073 * gdk_pixbuf_save_to_callbackv:
2074 * @pixbuf: a #GdkPixbuf.
2075 * @save_func: a function that is called to save each block of data that
2076 * the save routine generates.
2077 * @user_data: user data to pass to the save function.
2078 * @type: name of file format.
2079 * @option_keys: name of options to set, %NULL-terminated
2080 * @option_values: values for named options
2081 * @error: return location for error, or %NULL
2083 * Saves pixbuf to a callback in format @type, which is currently "jpeg",
2084 * "png", "tiff", "ico" or "bmp". If @error is set, %FALSE will be returned. See
2085 * gdk_pixbuf_save_to_callback () for more details.
2087 * Return value: whether an error was set
2092 gdk_pixbuf_save_to_callbackv (GdkPixbuf *pixbuf,
2093 GdkPixbufSaveFunc save_func,
2097 char **option_values,
2103 g_return_val_if_fail (save_func != NULL, FALSE);
2104 g_return_val_if_fail (type != NULL, FALSE);
2105 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2107 result = gdk_pixbuf_real_save_to_callback (pixbuf,
2108 save_func, user_data, type,
2109 option_keys, option_values,
2113 g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
2121 * gdk_pixbuf_save_to_buffer:
2122 * @pixbuf: a #GdkPixbuf.
2123 * @buffer: location to receive a pointer to the new buffer.
2124 * @buffer_size: location to receive the size of the new buffer.
2125 * @type: name of file format.
2126 * @error: return location for error, or %NULL
2127 * @Varargs: list of key-value save options
2129 * Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2130 * "png", "tiff", "ico" or "bmp". This is a convenience function that uses
2131 * gdk_pixbuf_save_to_callback() to do the real work. Note that the buffer
2132 * is not nul-terminated and may contain embedded nuls.
2133 * If @error is set, %FALSE will be returned and @string will be set to
2134 * %NULL. Possible errors include those in the #GDK_PIXBUF_ERROR
2137 * See gdk_pixbuf_save() for more details.
2139 * Return value: whether an error was set
2144 gdk_pixbuf_save_to_buffer (GdkPixbuf *pixbuf,
2151 gchar **keys = NULL;
2152 gchar **values = NULL;
2156 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2158 va_start (args, error);
2160 collect_save_options (args, &keys, &values);
2164 result = gdk_pixbuf_save_to_bufferv (pixbuf, buffer, buffer_size,
2169 g_strfreev (values);
2174 struct SaveToBufferData {
2180 save_to_buffer_callback (const gchar *data,
2185 struct SaveToBufferData *sdata = user_data;
2189 if (sdata->len + count > sdata->max) {
2190 new_max = MAX (sdata->max*2, sdata->len + count);
2191 new_buffer = g_try_realloc (sdata->buffer, new_max);
2195 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2196 _("Insufficient memory to save image into a buffer"));
2199 sdata->buffer = new_buffer;
2200 sdata->max = new_max;
2202 memcpy (sdata->buffer + sdata->len, data, count);
2203 sdata->len += count;
2208 * gdk_pixbuf_save_to_bufferv:
2209 * @pixbuf: a #GdkPixbuf.
2210 * @buffer: location to receive a pointer to the new buffer.
2211 * @buffer_size: location to receive the size of the new buffer.
2212 * @type: name of file format.
2213 * @option_keys: name of options to set, %NULL-terminated
2214 * @option_values: values for named options
2215 * @error: return location for error, or %NULL
2217 * Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2218 * "tiff", "png", "ico" or "bmp". See gdk_pixbuf_save_to_buffer()
2221 * Return value: whether an error was set
2226 gdk_pixbuf_save_to_bufferv (GdkPixbuf *pixbuf,
2231 char **option_values,
2234 static const gint initial_max = 1024;
2235 struct SaveToBufferData sdata;
2240 sdata.buffer = g_try_malloc (initial_max);
2241 sdata.max = initial_max;
2243 if (!sdata.buffer) {
2246 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2247 _("Insufficient memory to save image into a buffer"));
2251 if (!gdk_pixbuf_save_to_callbackv (pixbuf,
2252 save_to_buffer_callback, &sdata,
2253 type, option_keys, option_values,
2255 g_free (sdata.buffer);
2259 *buffer = sdata.buffer;
2260 *buffer_size = sdata.len;
2265 GOutputStream *stream;
2266 GCancellable *cancellable;
2270 save_to_stream (const gchar *buffer,
2275 SaveToStreamData *sdata = (SaveToStreamData *)data;
2278 GError *my_error = NULL;
2282 while (remaining > 0) {
2284 remaining -= written;
2285 written = g_output_stream_write (sdata->stream,
2293 _("Error writing to image stream"));
2296 g_propagate_error (error, my_error);
2306 * gdk_pixbuf_save_to_stream:
2307 * @pixbuf: a #GdkPixbuf
2308 * @stream: a #GOutputStream to save the pixbuf to
2309 * @type: name of file format
2310 * @cancellable: optional #GCancellable object, %NULL to ignore
2311 * @error: return location for error, or %NULL
2312 * @Varargs: list of key-value save options
2314 * Saves @pixbuf to an output stream.
2316 * Supported file formats are currently "jpeg", "tiff", "png", "ico" or
2317 * "bmp". See gdk_pixbuf_save_to_buffer() for more details.
2319 * The @cancellable can be used to abort the operation from another
2320 * thread. If the operation was cancelled, the error %GIO_ERROR_CANCELLED
2321 * will be returned. Other possible errors are in the #GDK_PIXBUF_ERROR
2322 * and %G_IO_ERROR domains.
2324 * The stream is not closed.
2326 * Returns: %TRUE if the pixbuf was saved successfully, %FALSE if an
2332 gdk_pixbuf_save_to_stream (GdkPixbuf *pixbuf,
2333 GOutputStream *stream,
2335 GCancellable *cancellable,
2340 gchar **keys = NULL;
2341 gchar **values = NULL;
2343 SaveToStreamData data;
2345 va_start (args, error);
2346 collect_save_options (args, &keys, &values);
2349 data.stream = stream;
2350 data.cancellable = cancellable;
2352 if (!gdk_pixbuf_save_to_callbackv (pixbuf, save_to_stream,
2356 error = NULL; /* Ignore further errors */
2361 g_strfreev (values);
2367 * gdk_pixbuf_format_get_name:
2368 * @format: a #GdkPixbufFormat
2370 * Returns the name of the format.
2372 * Return value: the name of the format.
2377 gdk_pixbuf_format_get_name (GdkPixbufFormat *format)
2379 g_return_val_if_fail (format != NULL, NULL);
2381 return g_strdup (format->name);
2385 * gdk_pixbuf_format_get_description:
2386 * @format: a #GdkPixbufFormat
2388 * Returns a description of the format.
2390 * Return value: a description of the format.
2395 gdk_pixbuf_format_get_description (GdkPixbufFormat *format)
2399 g_return_val_if_fail (format != NULL, NULL);
2401 if (format->domain != NULL)
2402 domain = format->domain;
2404 domain = GETTEXT_PACKAGE;
2405 description = dgettext (domain, format->description);
2407 return g_strdup (description);
2411 * gdk_pixbuf_format_get_mime_types:
2412 * @format: a #GdkPixbufFormat
2414 * Returns the mime types supported by the format.
2416 * Return value: a %NULL-terminated array of mime types which must be freed with
2417 * g_strfreev() when it is no longer needed.
2422 gdk_pixbuf_format_get_mime_types (GdkPixbufFormat *format)
2424 g_return_val_if_fail (format != NULL, NULL);
2426 return g_strdupv (format->mime_types);
2430 * gdk_pixbuf_format_get_extensions:
2431 * @format: a #GdkPixbufFormat
2433 * Returns the filename extensions typically used for files in the
2436 * Return value: a %NULL-terminated array of filename extensions which must be
2437 * freed with g_strfreev() when it is no longer needed.
2442 gdk_pixbuf_format_get_extensions (GdkPixbufFormat *format)
2444 g_return_val_if_fail (format != NULL, NULL);
2446 return g_strdupv (format->extensions);
2450 * gdk_pixbuf_format_is_writable:
2451 * @format: a #GdkPixbufFormat
2453 * Returns whether pixbufs can be saved in the given format.
2455 * Return value: whether pixbufs can be saved in the given format.
2460 gdk_pixbuf_format_is_writable (GdkPixbufFormat *format)
2462 g_return_val_if_fail (format != NULL, FALSE);
2464 return (format->flags & GDK_PIXBUF_FORMAT_WRITABLE) != 0;
2468 * gdk_pixbuf_format_is_scalable:
2469 * @format: a #GdkPixbufFormat
2471 * Returns whether this image format is scalable. If a file is in a
2472 * scalable format, it is preferable to load it at the desired size,
2473 * rather than loading it at the default size and scaling the
2474 * resulting pixbuf to the desired size.
2476 * Return value: whether this image format is scalable.
2481 gdk_pixbuf_format_is_scalable (GdkPixbufFormat *format)
2483 g_return_val_if_fail (format != NULL, FALSE);
2485 return (format->flags & GDK_PIXBUF_FORMAT_SCALABLE) != 0;
2489 * gdk_pixbuf_format_is_disabled:
2490 * @format: a #GdkPixbufFormat
2492 * Returns whether this image format is disabled. See
2493 * gdk_pixbuf_format_set_disabled().
2495 * Return value: whether this image format is disabled.
2500 gdk_pixbuf_format_is_disabled (GdkPixbufFormat *format)
2502 g_return_val_if_fail (format != NULL, FALSE);
2504 return format->disabled;
2508 * gdk_pixbuf_format_set_disabled:
2509 * @format: a #GdkPixbufFormat
2510 * @disabled: %TRUE to disable the format @format
2512 * Disables or enables an image format. If a format is disabled,
2513 * gdk-pixbuf won't use the image loader for this format to load
2514 * images. Applications can use this to avoid using image loaders
2515 * with an inappropriate license, see gdk_pixbuf_format_get_license().
2520 gdk_pixbuf_format_set_disabled (GdkPixbufFormat *format,
2523 g_return_if_fail (format != NULL);
2525 format->disabled = disabled != FALSE;
2529 * gdk_pixbuf_format_get_license:
2530 * @format: a #GdkPixbufFormat
2532 * Returns information about the license of the image loader for the format. The
2533 * returned string should be a shorthand for a wellknown license, e.g. "LGPL",
2534 * "GPL", "QPL", "GPL/QPL", or "other" to indicate some other license. This
2535 * string should be freed with g_free() when it's no longer needed.
2537 * Returns: a string describing the license of @format.
2542 gdk_pixbuf_format_get_license (GdkPixbufFormat *format)
2544 g_return_val_if_fail (format != NULL, NULL);
2546 return g_strdup (format->license);
2550 _gdk_pixbuf_get_format (GdkPixbufModule *module)
2552 g_return_val_if_fail (module != NULL, NULL);
2554 return module->info;
2558 * gdk_pixbuf_get_formats:
2560 * Obtains the available information about the image formats supported
2563 * Returns: A list of #GdkPixbufFormat<!-- -->s describing the supported
2564 * image formats. The list should be freed when it is no longer needed,
2565 * but the structures themselves are owned by #GdkPixbuf and should not be
2571 gdk_pixbuf_get_formats (void)
2573 GSList *result = NULL;
2576 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
2577 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
2578 GdkPixbufFormat *info = _gdk_pixbuf_get_format (module);
2579 result = g_slist_prepend (result, info);
2586 #define __GDK_PIXBUF_IO_C__
2587 #include "gdk-pixbuf-aliasdef.c"