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
54 #ifndef GDK_PIXBUF_USE_GIO_MIME
56 format_check (GdkPixbufModule *module, guchar *buffer, int size)
60 GdkPixbufModulePattern *pattern;
65 for (pattern = module->info->signature; pattern->prefix; pattern++) {
66 if (pattern->mask && pattern->mask[0] == '*') {
67 prefix = (guchar *)pattern->prefix + 1;
68 mask = pattern->mask + 1;
72 prefix = (guchar *)pattern->prefix;
76 for (i = 0; i < size; i++) {
77 for (j = 0; i + j < size && prefix[j] != 0; j++) {
78 m = mask ? mask[j] : ' ';
80 if (buffer[i + j] != prefix[j])
84 if (buffer[i + j] == prefix[j])
88 if (buffer[i + j] != 0)
92 if (buffer[i + j] == 0)
98 return pattern->relevance;
108 G_LOCK_DEFINE_STATIC (init_lock);
109 G_LOCK_DEFINE_STATIC (threadunsafe_loader_lock);
112 _gdk_pixbuf_lock (GdkPixbufModule *image_module)
114 if (g_threads_got_initialized &&
115 !(image_module->info->flags & GDK_PIXBUF_FORMAT_THREADSAFE)) {
116 G_LOCK (threadunsafe_loader_lock);
125 _gdk_pixbuf_unlock (GdkPixbufModule *image_module)
127 if (!(image_module->info->flags & GDK_PIXBUF_FORMAT_THREADSAFE)) {
128 G_UNLOCK (threadunsafe_loader_lock);
132 static GSList *file_formats = NULL;
134 static void gdk_pixbuf_io_init (void);
137 get_file_formats (void)
140 if (file_formats == NULL)
141 gdk_pixbuf_io_init ();
142 G_UNLOCK (init_lock);
151 scan_string (const char **pos, GString *out)
153 const char *p = *pos, *q = *pos;
157 while (g_ascii_isspace (*p))
162 else if (*p == '"') {
165 for (q = p; (*q != '"') || quoted; q++) {
168 quoted = (*q == '\\') && !quoted;
171 tmp = g_strndup (p, q - p);
172 tmp2 = g_strcompress (tmp);
173 g_string_truncate (out, 0);
174 g_string_append (out, tmp2);
186 scan_int (const char **pos, int *out)
190 const char *p = *pos;
192 while (g_ascii_isspace (*p))
195 if (*p < '0' || *p > '9')
198 while ((*p >= '0') && (*p <= '9') && i < sizeof (buf)) {
204 if (i == sizeof (buf))
217 skip_space (const char **pos)
219 const char *p = *pos;
221 while (g_ascii_isspace (*p))
226 return !(*p == '\0');
231 /* DllMain function needed to tuck away the gdk-pixbuf DLL name */
232 G_WIN32_DLLMAIN_FOR_DLL_NAME (static, dll_name)
237 static char *toplevel = NULL;
239 if (toplevel == NULL)
240 toplevel = g_win32_get_package_installation_subdirectory
241 (GETTEXT_PACKAGE, dll_name, "");
247 get_sysconfdir (void)
249 static char *sysconfdir = NULL;
251 if (sysconfdir == NULL)
252 sysconfdir = g_win32_get_package_installation_subdirectory
253 (GETTEXT_PACKAGE, dll_name, "etc");
258 #undef GTK_SYSCONFDIR
259 #define GTK_SYSCONFDIR get_sysconfdir()
262 correct_prefix (gchar **path)
264 if (strncmp (*path, GTK_PREFIX "/", strlen (GTK_PREFIX "/")) == 0 ||
265 strncmp (*path, GTK_PREFIX "\\", strlen (GTK_PREFIX "\\")) == 0)
267 /* This is an entry put there by gdk-pixbuf-query-loaders on the
268 * packager's system. On Windows a prebuilt GTK+ package can be
269 * installed in a random location. The gdk-pixbuf.loaders file
270 * distributed in such a package contains paths from the package
271 * builder's machine. Replace the build-time prefix with the
272 * installation prefix on this machine.
275 *path = g_strconcat (get_toplevel (), tem + strlen (GTK_PREFIX), NULL);
280 #endif /* G_OS_WIN32 */
283 gdk_pixbuf_get_module_file (void)
285 gchar *result = g_strdup (g_getenv ("GDK_PIXBUF_MODULE_FILE"));
288 result = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gdk-pixbuf.loaders", NULL);
293 #endif /* USE_GMODULE */
296 gdk_pixbuf_io_init (void)
302 GString *tmp_buf = g_string_new (NULL);
303 gboolean have_error = FALSE;
304 GdkPixbufModule *module = NULL;
305 gchar *filename = gdk_pixbuf_get_module_file ();
308 GdkPixbufModulePattern *pattern;
309 GError *error = NULL;
311 GdkPixbufModule *builtin_module ;
313 /* initialize on separate line to avoid compiler warnings in the
314 * common case of no compiled-in modules.
316 builtin_module = NULL;
318 #define load_one_builtin_module(format) \
319 builtin_module = g_new0 (GdkPixbufModule, 1); \
320 builtin_module->module_name = #format; \
321 if (_gdk_pixbuf_load_module (builtin_module, NULL)) \
322 file_formats = g_slist_prepend (file_formats, builtin_module);\
324 g_free (builtin_module)
327 load_one_builtin_module (ani);
330 load_one_builtin_module (png);
333 load_one_builtin_module (bmp);
336 load_one_builtin_module (wbmp);
339 load_one_builtin_module (gif);
342 load_one_builtin_module (ico);
345 load_one_builtin_module (jpeg);
348 load_one_builtin_module (pnm);
351 load_one_builtin_module (ras);
354 load_one_builtin_module (tiff);
357 load_one_builtin_module (xpm);
360 load_one_builtin_module (xbm);
363 load_one_builtin_module (tga);
366 load_one_builtin_module (pcx);
369 load_one_builtin_module (icns);
371 #ifdef INCLUDE_jasper
372 load_one_builtin_module (jasper);
374 #ifdef INCLUDE_gdiplus
375 /* We don't bother having the GDI+ loaders individually selectable
376 * for building in or not.
378 load_one_builtin_module (ico);
379 load_one_builtin_module (wmf);
380 load_one_builtin_module (emf);
381 load_one_builtin_module (bmp);
382 load_one_builtin_module (gif);
383 load_one_builtin_module (jpeg);
384 load_one_builtin_module (tiff);
386 #ifdef INCLUDE_gdip_png
387 /* Except the gdip-png loader which normally isn't built at all even */
388 load_one_builtin_module (png);
391 #undef load_one_builtin_module
394 channel = g_io_channel_new_file (filename, "r", &error);
396 /* Don't bother warning if we have some built-in loaders */
397 if (file_formats == NULL)
398 g_warning ("Cannot open pixbuf loader module file '%s': %s",
399 filename, error->message);
403 while (!have_error && g_io_channel_read_line (channel, &line_buf, NULL, &term, NULL) == G_IO_STATUS_NORMAL) {
410 if (!skip_space (&p)) {
411 /* Blank line marking the end of a module
413 if (module && *p != '#') {
415 correct_prefix (&module->module_path);
417 file_formats = g_slist_prepend (file_formats, module);
428 /* Read a module location
430 module = g_new0 (GdkPixbufModule, 1);
433 if (!scan_string (&p, tmp_buf)) {
434 g_warning ("Error parsing loader info in '%s'\n %s",
438 module->module_path = g_strdup (tmp_buf->str);
440 else if (!module->module_name) {
441 module->info = g_new0 (GdkPixbufFormat, 1);
442 if (!scan_string (&p, tmp_buf)) {
443 g_warning ("Error parsing loader info in '%s'\n %s",
447 module->info->name = g_strdup (tmp_buf->str);
448 module->module_name = module->info->name;
450 if (!scan_int (&p, &flags)) {
451 g_warning ("Error parsing loader info in '%s'\n %s",
455 module->info->flags = flags;
457 if (!scan_string (&p, tmp_buf)) {
458 g_warning ("Error parsing loader info in '%s'\n %s",
462 if (tmp_buf->str[0] != 0)
463 module->info->domain = g_strdup (tmp_buf->str);
465 if (!scan_string (&p, tmp_buf)) {
466 g_warning ("Error parsing loader info in '%s'\n %s",
470 module->info->description = g_strdup (tmp_buf->str);
472 if (scan_string (&p, tmp_buf)) {
473 module->info->license = g_strdup (tmp_buf->str);
476 else if (!module->info->mime_types) {
478 module->info->mime_types = g_new0 (gchar*, 1);
479 while (scan_string (&p, tmp_buf)) {
480 if (tmp_buf->str[0] != 0) {
481 module->info->mime_types =
482 g_realloc (module->info->mime_types, (n + 1) * sizeof (gchar*));
483 module->info->mime_types[n - 1] = g_strdup (tmp_buf->str);
484 module->info->mime_types[n] = NULL;
489 else if (!module->info->extensions) {
491 module->info->extensions = g_new0 (gchar*, 1);
492 while (scan_string (&p, tmp_buf)) {
493 if (tmp_buf->str[0] != 0) {
494 module->info->extensions =
495 g_realloc (module->info->extensions, (n + 1) * sizeof (gchar*));
496 module->info->extensions[n - 1] = g_strdup (tmp_buf->str);
497 module->info->extensions[n] = NULL;
504 module->info->signature = (GdkPixbufModulePattern *)
505 g_realloc (module->info->signature, (n_patterns + 1) * sizeof (GdkPixbufModulePattern));
506 pattern = module->info->signature + n_patterns;
507 pattern->prefix = NULL;
508 pattern->mask = NULL;
509 pattern->relevance = 0;
511 if (!scan_string (&p, tmp_buf))
513 pattern->prefix = g_strdup (tmp_buf->str);
515 if (!scan_string (&p, tmp_buf))
518 pattern->mask = g_strdup (tmp_buf->str);
520 pattern->mask = NULL;
522 if (!scan_int (&p, &pattern->relevance))
528 g_free (pattern->prefix);
529 g_free (pattern->mask);
531 g_warning ("Error parsing loader info in '%s'\n %s",
538 g_string_free (tmp_buf, TRUE);
539 g_io_channel_unref (channel);
546 /* actually load the image handler - gdk_pixbuf_get_module only get a */
547 /* reference to the module to load, it doesn't actually load it */
548 /* perhaps these actions should be combined in one function */
550 _gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
557 g_return_val_if_fail (image_module->module == NULL, FALSE);
559 path = image_module->module_path;
560 module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
565 GDK_PIXBUF_ERROR_FAILED,
566 _("Unable to load image-loading module: %s: %s"),
567 path, g_module_error ());
571 image_module->module = module;
573 if (g_module_symbol (module, "fill_vtable", &sym)) {
574 GdkPixbufModuleFillVtableFunc func = (GdkPixbufModuleFillVtableFunc) sym;
575 (* func) (image_module);
580 GDK_PIXBUF_ERROR_FAILED,
581 _("Image-loading module %s does not export the proper interface; perhaps it's from a different GTK version?"),
587 #endif /* !USE_GMODULE */
589 #define module(type) \
590 extern void _gdk_pixbuf__##type##_fill_info (GdkPixbufFormat *info); \
591 extern void _gdk_pixbuf__##type##_fill_vtable (GdkPixbufModule *module)
621 _gdk_pixbuf_load_module (GdkPixbufModule *image_module,
625 gboolean locked = FALSE;
626 GdkPixbufModuleFillInfoFunc fill_info = NULL;
627 GdkPixbufModuleFillVtableFunc fill_vtable = NULL;
629 #define try_module(format,id) \
630 if (fill_info == NULL && \
631 strcmp (image_module->module_name, #format) == 0) { \
632 fill_info = _gdk_pixbuf__##id##_fill_info; \
633 fill_vtable = _gdk_pixbuf__##id##_fill_vtable; \
636 try_module (png,png);
639 try_module (bmp,bmp);
642 try_module (wbmp,wbmp);
645 try_module (gif,gif);
648 try_module (ico,ico);
651 try_module (ani,ani);
654 try_module (jpeg,jpeg);
657 try_module (pnm,pnm);
660 try_module (ras,ras);
663 try_module (tiff,tiff);
666 try_module (xpm,xpm);
669 try_module (xbm,xbm);
672 try_module (tga,tga);
675 try_module (pcx,pcx);
678 try_module (icns,icns);
680 #ifdef INCLUDE_jasper
681 try_module (jasper,jasper);
683 #ifdef INCLUDE_gdiplus
684 try_module (ico,gdip_ico);
685 try_module (wmf,gdip_wmf);
686 try_module (emf,gdip_emf);
687 try_module (bmp,gdip_bmp);
688 try_module (gif,gdip_gif);
689 try_module (jpeg,gdip_jpeg);
690 try_module (tiff,gdip_tiff);
692 #ifdef INCLUDE_gdip_png
693 try_module (png,gdip_png);
699 image_module->module = (void *) 1;
700 (* fill_vtable) (image_module);
701 image_module->info = g_new0 (GdkPixbufFormat, 1);
702 (* fill_info) (image_module->info);
709 /* be extra careful, maybe the module initializes
712 if (g_threads_got_initialized)
717 ret = _gdk_pixbuf_load_module_unlocked (image_module, error);
719 G_UNLOCK (init_lock);
725 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
726 _("Image type '%s' is not supported"),
727 image_module->module_name);
737 _gdk_pixbuf_get_named_module (const char *name,
742 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
743 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
745 if (module->info->disabled)
748 if (!strcmp (name, module->module_name))
754 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
755 _("Image type '%s' is not supported"),
762 _gdk_pixbuf_get_module (guchar *buffer, guint size,
763 const gchar *filename,
768 GdkPixbufModule *selected = NULL;
769 gchar *display_name = NULL;
770 #ifdef GDK_PIXBUF_USE_GIO_MIME
776 mime_type = g_content_type_guess (filename, buffer, size, NULL);
778 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
779 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
780 GdkPixbufFormat *info = module->info;
785 mimes = info->mime_types;
786 for (j = 0; mimes[j] != NULL; j++) {
787 type = g_content_type_from_mime_type (mimes[j]);
788 if (g_ascii_strcasecmp (type, mime_type) == 0) {
798 gint score, best = 0;
800 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
801 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
803 if (module->info->disabled)
806 score = format_check (module, buffer, size);
816 if (selected != NULL)
821 display_name = g_filename_display_name (filename);
824 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
825 _("Couldn't recognize the image file format for file '%s'"),
827 g_free (display_name);
830 g_set_error_literal (error,
832 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
833 _("Unrecognized image file format"));
841 prepared_notify (GdkPixbuf *pixbuf,
842 GdkPixbufAnimation *anim,
846 g_object_ref (pixbuf);
847 *((GdkPixbuf **)user_data) = pixbuf;
851 _gdk_pixbuf_generic_image_load (GdkPixbufModule *module,
855 guchar buffer[LOAD_BUFFER_SIZE];
857 GdkPixbuf *pixbuf = NULL;
858 GdkPixbufAnimation *animation = NULL;
862 locked = _gdk_pixbuf_lock (module);
864 if (module->load != NULL) {
865 pixbuf = (* module->load) (f, error);
866 } else if (module->begin_load != NULL) {
868 context = module->begin_load (NULL, prepared_notify, NULL, &pixbuf, error);
873 while (!feof (f) && !ferror (f)) {
874 length = fread (buffer, 1, sizeof (buffer), f);
876 if (!module->load_increment (context, buffer, length, error)) {
877 module->stop_load (context, NULL);
878 if (pixbuf != NULL) {
879 g_object_unref (pixbuf);
886 if (!module->stop_load (context, error)) {
887 if (pixbuf != NULL) {
888 g_object_unref (pixbuf);
892 } else if (module->load_animation != NULL) {
893 animation = (* module->load_animation) (f, error);
894 if (animation != NULL) {
895 pixbuf = gdk_pixbuf_animation_get_static_image (animation);
897 g_object_ref (pixbuf);
898 g_object_unref (animation);
904 _gdk_pixbuf_unlock (module);
909 * gdk_pixbuf_new_from_file:
910 * @filename: Name of file to load, in the GLib file name encoding
911 * @error: Return location for an error
913 * Creates a new pixbuf by loading an image from a file. The file format is
914 * detected automatically. If %NULL is returned, then @error will be set.
915 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
917 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
918 * any of several error conditions occurred: the file could not be opened,
919 * there was no loader for the file's format, there was not enough memory to
920 * allocate the image buffer, or the image file contained invalid data.
923 gdk_pixbuf_new_from_file (const char *filename,
929 guchar buffer[SNIFF_BUFFER_SIZE];
930 GdkPixbufModule *image_module;
933 g_return_val_if_fail (filename != NULL, NULL);
934 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
936 display_name = g_filename_display_name (filename);
938 f = g_fopen (filename, "rb");
940 gint save_errno = errno;
943 g_file_error_from_errno (save_errno),
944 _("Failed to open file '%s': %s"),
946 g_strerror (save_errno));
947 g_free (display_name);
951 size = fread (&buffer, 1, sizeof (buffer), f);
955 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
956 _("Image file '%s' contains no data"),
958 g_free (display_name);
963 image_module = _gdk_pixbuf_get_module (buffer, size, filename, error);
964 if (image_module == NULL) {
965 g_free (display_name);
970 if (image_module->module == NULL)
971 if (!_gdk_pixbuf_load_module (image_module, error)) {
972 g_free (display_name);
977 fseek (f, 0, SEEK_SET);
978 pixbuf = _gdk_pixbuf_generic_image_load (image_module, f, error);
981 if (pixbuf == NULL && error != NULL && *error == NULL) {
983 /* I don't trust these crufty longjmp()'ing image libs
984 * to maintain proper error invariants, and I don't
985 * want user code to segfault as a result. We need to maintain
986 * the invariant that error gets set if NULL is returned.
989 g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.", image_module->module_name);
992 GDK_PIXBUF_ERROR_FAILED,
993 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
995 } else if (error != NULL && *error != NULL) {
997 /* Add the filename to the error message */
1002 e->message = g_strdup_printf (_("Failed to load image '%s': %s"),
1008 g_free (display_name);
1014 #undef gdk_pixbuf_new_from_file
1016 gdk_pixbuf_new_from_file (const char *filename,
1019 gchar *utf8_filename =
1020 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1023 if (utf8_filename == NULL)
1026 retval = gdk_pixbuf_new_from_file_utf8 (utf8_filename, error);
1028 g_free (utf8_filename);
1036 * gdk_pixbuf_new_from_file_at_size:
1037 * @filename: Name of file to load, in the GLib file name encoding
1038 * @width: The width the image should have or -1 to not constrain the width
1039 * @height: The height the image should have or -1 to not constrain the height
1040 * @error: Return location for an error
1042 * Creates a new pixbuf by loading an image from a file.
1043 * The file format is detected automatically. If %NULL is returned, then
1044 * @error will be set. Possible errors are in the #GDK_PIXBUF_ERROR and
1045 * #G_FILE_ERROR domains.
1047 * The image will be scaled to fit in the requested size, preserving
1048 * the image's aspect ratio. Note that the returned pixbuf may be smaller
1049 * than @width x @height, if the aspect ratio requires it. To load
1050 * and image at the requested size, regardless of aspect ratio, use
1051 * gdk_pixbuf_new_from_file_at_scale().
1053 * Return value: A newly-created pixbuf with a reference count of 1, or
1054 * %NULL if any of several error conditions occurred: the file could not
1055 * be opened, there was no loader for the file's format, there was not
1056 * enough memory to allocate the image buffer, or the image file contained
1062 gdk_pixbuf_new_from_file_at_size (const char *filename,
1067 return gdk_pixbuf_new_from_file_at_scale (filename,
1074 #undef gdk_pixbuf_new_from_file_at_size
1077 gdk_pixbuf_new_from_file_at_size (const char *filename,
1082 gchar *utf8_filename =
1083 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1086 if (utf8_filename == NULL)
1089 retval = gdk_pixbuf_new_from_file_at_size_utf8 (utf8_filename,
1093 g_free (utf8_filename);
1102 gboolean preserve_aspect_ratio;
1106 at_scale_size_prepared_cb (GdkPixbufLoader *loader,
1111 AtScaleData *info = data;
1113 g_return_if_fail (width > 0 && height > 0);
1115 if (info->preserve_aspect_ratio &&
1116 (info->width > 0 || info->height > 0)) {
1117 if (info->width < 0)
1119 width = width * (double)info->height/(double)height;
1120 height = info->height;
1122 else if (info->height < 0)
1124 height = height * (double)info->width/(double)width;
1125 width = info->width;
1127 else if ((double)height * (double)info->width >
1128 (double)width * (double)info->height) {
1129 width = 0.5 + (double)width * (double)info->height / (double)height;
1130 height = info->height;
1132 height = 0.5 + (double)height * (double)info->width / (double)width;
1133 width = info->width;
1136 if (info->width > 0)
1137 width = info->width;
1138 if (info->height > 0)
1139 height = info->height;
1142 width = MAX (width, 1);
1143 height = MAX (height, 1);
1145 gdk_pixbuf_loader_set_size (loader, width, height);
1149 * gdk_pixbuf_new_from_file_at_scale:
1150 * @filename: Name of file to load, in the GLib file name encoding
1151 * @width: The width the image should have or -1 to not constrain the width
1152 * @height: The height the image should have or -1 to not constrain the height
1153 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1154 * @error: Return location for an error
1156 * Creates a new pixbuf by loading an image from a file. The file format is
1157 * detected automatically. If %NULL is returned, then @error will be set.
1158 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
1159 * The image will be scaled to fit in the requested size, optionally preserving
1160 * the image's aspect ratio.
1162 * When preserving the aspect ratio, a @width of -1 will cause the image
1163 * to be scaled to the exact given height, and a @height of -1 will cause
1164 * the image to be scaled to the exact given width. When not preserving
1165 * aspect ratio, a @width or @height of -1 means to not scale the image
1166 * at all in that dimension. Negative values for @width and @height are
1167 * allowed since 2.8.
1169 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL
1170 * if any of several error conditions occurred: the file could not be opened,
1171 * there was no loader for the file's format, there was not enough memory to
1172 * allocate the image buffer, or the image file contained invalid data.
1177 gdk_pixbuf_new_from_file_at_scale (const char *filename,
1180 gboolean preserve_aspect_ratio,
1184 GdkPixbufLoader *loader;
1186 guchar buffer[LOAD_BUFFER_SIZE];
1190 GdkPixbufAnimation *animation;
1191 GdkPixbufAnimationIter *iter;
1194 g_return_val_if_fail (filename != NULL, NULL);
1195 g_return_val_if_fail (width > 0 || width == -1, NULL);
1196 g_return_val_if_fail (height > 0 || height == -1, NULL);
1198 f = g_fopen (filename, "rb");
1200 gint save_errno = errno;
1201 gchar *display_name = g_filename_display_name (filename);
1204 g_file_error_from_errno (save_errno),
1205 _("Failed to open file '%s': %s"),
1207 g_strerror (save_errno));
1208 g_free (display_name);
1212 loader = gdk_pixbuf_loader_new ();
1215 info.height = height;
1216 info.preserve_aspect_ratio = preserve_aspect_ratio;
1218 g_signal_connect (loader, "size-prepared",
1219 G_CALLBACK (at_scale_size_prepared_cb), &info);
1222 while (!has_frame && !feof (f) && !ferror (f)) {
1223 length = fread (buffer, 1, sizeof (buffer), f);
1225 if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
1226 gdk_pixbuf_loader_close (loader, NULL);
1228 g_object_unref (loader);
1232 animation = gdk_pixbuf_loader_get_animation (loader);
1234 iter = gdk_pixbuf_animation_get_iter (animation, NULL);
1235 if (!gdk_pixbuf_animation_iter_on_currently_loading_frame (iter)) {
1238 g_object_unref (iter);
1244 if (!gdk_pixbuf_loader_close (loader, error) && !has_frame) {
1245 g_object_unref (loader);
1249 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1252 gchar *display_name = g_filename_display_name (filename);
1253 g_object_unref (loader);
1256 GDK_PIXBUF_ERROR_FAILED,
1257 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1259 g_free (display_name);
1263 g_object_ref (pixbuf);
1265 g_object_unref (loader);
1272 #undef gdk_pixbuf_new_from_file_at_scale
1275 gdk_pixbuf_new_from_file_at_scale (const char *filename,
1278 gboolean preserve_aspect_ratio,
1281 gchar *utf8_filename =
1282 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1285 if (utf8_filename == NULL)
1288 retval = gdk_pixbuf_new_from_file_at_scale_utf8 (utf8_filename,
1290 preserve_aspect_ratio,
1293 g_free (utf8_filename);
1301 load_from_stream (GdkPixbufLoader *loader,
1302 GInputStream *stream,
1303 GCancellable *cancellable,
1308 guchar buffer[LOAD_BUFFER_SIZE];
1313 n_read = g_input_stream_read (stream,
1320 error = NULL; /* Ignore further errors */
1327 if (!gdk_pixbuf_loader_write (loader,
1337 if (!gdk_pixbuf_loader_close (loader, error)) {
1344 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1346 g_object_ref (pixbuf);
1354 * gdk_pixbuf_new_from_stream_at_scale:
1355 * @stream: a #GInputStream to load the pixbuf from
1356 * @width: The width the image should have or -1 to not constrain the width
1357 * @height: The height the image should have or -1 to not constrain the height
1358 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1359 * @cancellable: optional #GCancellable object, %NULL to ignore
1360 * @error: Return location for an error
1362 * Creates a new pixbuf by loading an image from an input stream.
1364 * The file format is detected automatically. If %NULL is returned, then
1365 * @error will be set. The @cancellable can be used to abort the operation
1366 * from another thread. If the operation was cancelled, the error
1367 * %GIO_ERROR_CANCELLED will be returned. Other possible errors are in
1368 * the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
1370 * The image will be scaled to fit in the requested size, optionally
1371 * preserving the image's aspect ratio. When preserving the aspect ratio,
1372 * a @width of -1 will cause the image to be scaled to the exact given
1373 * height, and a @height of -1 will cause the image to be scaled to the
1374 * exact given width. When not preserving aspect ratio, a @width or
1375 * @height of -1 means to not scale the image at all in that dimension.
1377 * The stream is not closed.
1379 * Return value: A newly-created pixbuf, or %NULL if any of several error
1380 * conditions occurred: the file could not be opened, the image format is
1381 * not supported, there was not enough memory to allocate the image buffer,
1382 * the stream contained invalid data, or the operation was cancelled.
1387 gdk_pixbuf_new_from_stream_at_scale (GInputStream *stream,
1390 gboolean preserve_aspect_ratio,
1391 GCancellable *cancellable,
1394 GdkPixbufLoader *loader;
1398 loader = gdk_pixbuf_loader_new ();
1401 info.height = height;
1402 info.preserve_aspect_ratio = preserve_aspect_ratio;
1404 g_signal_connect (loader, "size-prepared",
1405 G_CALLBACK (at_scale_size_prepared_cb), &info);
1407 pixbuf = load_from_stream (loader, stream, cancellable, error);
1408 g_object_unref (loader);
1414 * gdk_pixbuf_new_from_stream:
1415 * @stream: a #GInputStream to load the pixbuf from
1416 * @cancellable: optional #GCancellable object, %NULL to ignore
1417 * @error: Return location for an error
1419 * Creates a new pixbuf by loading an image from an input stream.
1421 * The file format is detected automatically. If %NULL is returned, then
1422 * @error will be set. The @cancellable can be used to abort the operation
1423 * from another thread. If the operation was cancelled, the error
1424 * %GIO_ERROR_CANCELLED will be returned. Other possible errors are in
1425 * the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
1427 * The stream is not closed.
1429 * Return value: A newly-created pixbuf, or %NULL if any of several error
1430 * conditions occurred: the file could not be opened, the image format is
1431 * not supported, there was not enough memory to allocate the image buffer,
1432 * the stream contained invalid data, or the operation was cancelled.
1437 gdk_pixbuf_new_from_stream (GInputStream *stream,
1438 GCancellable *cancellable,
1442 GdkPixbufLoader *loader;
1444 loader = gdk_pixbuf_loader_new ();
1445 pixbuf = load_from_stream (loader, stream, cancellable, error);
1446 g_object_unref (loader);
1452 info_cb (GdkPixbufLoader *loader,
1458 GdkPixbufFormat *format;
1463 g_return_if_fail (width > 0 && height > 0);
1465 info->format = gdk_pixbuf_loader_get_format (loader);
1466 info->width = width;
1467 info->height = height;
1469 gdk_pixbuf_loader_set_size (loader, 0, 0);
1473 * gdk_pixbuf_get_file_info:
1474 * @filename: The name of the file to identify.
1475 * @width: Return location for the width of the image, or %NULL
1476 * @height: Return location for the height of the image, or %NULL
1478 * Parses an image file far enough to determine its format and size.
1480 * Returns: A #GdkPixbufFormat describing the image format of the file
1481 * or %NULL if the image format wasn't recognized. The return value
1482 * is owned by GdkPixbuf and should not be freed.
1487 gdk_pixbuf_get_file_info (const gchar *filename,
1491 GdkPixbufLoader *loader;
1492 guchar buffer[SNIFF_BUFFER_SIZE];
1496 GdkPixbufFormat *format;
1501 g_return_val_if_fail (filename != NULL, NULL);
1503 f = g_fopen (filename, "rb");
1507 loader = gdk_pixbuf_loader_new ();
1513 g_signal_connect (loader, "size-prepared", G_CALLBACK (info_cb), &info);
1515 while (!feof (f) && !ferror (f)) {
1516 length = fread (buffer, 1, sizeof (buffer), f);
1518 if (!gdk_pixbuf_loader_write (loader, buffer, length, NULL))
1521 if (info.format != NULL)
1526 gdk_pixbuf_loader_close (loader, NULL);
1527 g_object_unref (loader);
1530 *width = info.width;
1532 *height = info.height;
1538 * gdk_pixbuf_new_from_xpm_data:
1539 * @data: Pointer to inline XPM data.
1541 * Creates a new pixbuf by parsing XPM data in memory. This data is commonly
1542 * the result of including an XPM file into a program's C source.
1544 * Return value: A newly-created pixbuf with a reference count of 1.
1547 gdk_pixbuf_new_from_xpm_data (const char **data)
1549 GdkPixbuf *(* load_xpm_data) (const char **data);
1551 GError *error = NULL;
1552 GdkPixbufModule *xpm_module;
1555 g_return_val_if_fail (data != NULL, NULL);
1557 xpm_module = _gdk_pixbuf_get_named_module ("xpm", &error);
1558 if (xpm_module == NULL) {
1559 g_warning ("Error loading XPM image loader: %s", error->message);
1560 g_error_free (error);
1564 if (xpm_module->module == NULL) {
1565 if (!_gdk_pixbuf_load_module (xpm_module, &error)) {
1566 g_warning ("Error loading XPM image loader: %s", error->message);
1567 g_error_free (error);
1572 locked = _gdk_pixbuf_lock (xpm_module);
1574 if (xpm_module->load_xpm_data == NULL) {
1575 g_warning ("gdk-pixbuf XPM module lacks XPM data capability");
1578 load_xpm_data = xpm_module->load_xpm_data;
1579 pixbuf = (* load_xpm_data) (data);
1583 _gdk_pixbuf_unlock (xpm_module);
1588 collect_save_options (va_list opts,
1601 next = va_arg (opts, gchar*);
1605 val = va_arg (opts, gchar*);
1610 *keys = g_realloc (*keys, sizeof(gchar*) * (count + 1));
1611 *vals = g_realloc (*vals, sizeof(gchar*) * (count + 1));
1613 (*keys)[count-1] = g_strdup (key);
1614 (*vals)[count-1] = g_strdup (val);
1616 (*keys)[count] = NULL;
1617 (*vals)[count] = NULL;
1619 next = va_arg (opts, gchar*);
1624 save_to_file_callback (const gchar *buf,
1629 FILE *filehandle = data;
1632 n = fwrite (buf, 1, count, filehandle);
1634 gint save_errno = errno;
1637 g_file_error_from_errno (save_errno),
1638 _("Error writing to image file: %s"),
1639 g_strerror (save_errno));
1646 gdk_pixbuf_real_save (GdkPixbuf *pixbuf,
1654 GdkPixbufModule *image_module = NULL;
1657 image_module = _gdk_pixbuf_get_named_module (type, error);
1659 if (image_module == NULL)
1662 if (image_module->module == NULL)
1663 if (!_gdk_pixbuf_load_module (image_module, error))
1666 locked = _gdk_pixbuf_lock (image_module);
1668 if (image_module->save) {
1670 ret = (* image_module->save) (filehandle, pixbuf,
1673 } else if (image_module->save_to_callback) {
1674 /* save with simple callback */
1675 ret = (* image_module->save_to_callback) (save_to_file_callback,
1683 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
1684 _("This build of gdk-pixbuf does not support saving the image format: %s"),
1690 _gdk_pixbuf_unlock (image_module);
1694 #define TMP_FILE_BUF_SIZE 4096
1697 save_to_callback_with_tmp_file (GdkPixbufModule *image_module,
1699 GdkPixbufSaveFunc save_func,
1707 gboolean retval = FALSE;
1710 gchar *filename = NULL;
1713 buf = g_try_malloc (TMP_FILE_BUF_SIZE);
1715 g_set_error_literal (error,
1717 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1718 _("Insufficient memory to save image to callback"));
1722 fd = g_file_open_tmp ("gdkpixbuf-save-tmp.XXXXXX", &filename, error);
1725 f = fdopen (fd, "wb+");
1727 gint save_errno = errno;
1728 g_set_error_literal (error,
1730 g_file_error_from_errno (save_errno),
1731 _("Failed to open temporary file"));
1735 locked = _gdk_pixbuf_lock (image_module);
1736 retval = (image_module->save) (f, pixbuf, keys, values, error);
1738 _gdk_pixbuf_unlock (image_module);
1744 n = fread (buf, 1, TMP_FILE_BUF_SIZE, f);
1746 if (!save_func (buf, n, error, user_data))
1749 if (n != TMP_FILE_BUF_SIZE)
1753 gint save_errno = errno;
1754 g_set_error_literal (error,
1756 g_file_error_from_errno (save_errno),
1757 _("Failed to read from temporary file"));
1763 /* cleanup and return retval */
1767 g_unlink (filename);
1776 gdk_pixbuf_real_save_to_callback (GdkPixbuf *pixbuf,
1777 GdkPixbufSaveFunc save_func,
1785 GdkPixbufModule *image_module = NULL;
1788 image_module = _gdk_pixbuf_get_named_module (type, error);
1790 if (image_module == NULL)
1793 if (image_module->module == NULL)
1794 if (!_gdk_pixbuf_load_module (image_module, error))
1797 locked = _gdk_pixbuf_lock (image_module);
1799 if (image_module->save_to_callback) {
1801 ret = (* image_module->save_to_callback) (save_func, user_data,
1802 pixbuf, keys, values,
1804 } else if (image_module->save) {
1805 /* use a temporary file */
1806 ret = save_to_callback_with_tmp_file (image_module, pixbuf,
1807 save_func, user_data,
1814 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
1815 _("This build of gdk-pixbuf does not support saving the image format: %s"),
1821 _gdk_pixbuf_unlock (image_module);
1828 * @pixbuf: a #GdkPixbuf.
1829 * @filename: name of file to save.
1830 * @type: name of file format.
1831 * @error: return location for error, or %NULL
1832 * @Varargs: list of key-value save options
1834 * Saves pixbuf to a file in format @type. By default, "jpeg", "png", "ico"
1835 * and "bmp" are possible file formats to save in, but more formats may be
1836 * installed. The list of all writable formats can be determined in the
1840 * void add_if_writable (GdkPixbufFormat *data, GSList **list)
1842 * if (gdk_pixbuf_format_is_writable (data))
1843 * *list = g_slist_prepend (*list, data);
1846 * GSList *formats = gdk_pixbuf_get_formats ();
1847 * GSList *writable_formats = NULL;
1848 * g_slist_foreach (formats, add_if_writable, &writable_formats);
1849 * g_slist_free (formats);
1852 * If @error is set, %FALSE will be returned. Possible errors include
1853 * those in the #GDK_PIXBUF_ERROR domain and those in the #G_FILE_ERROR domain.
1855 * The variable argument list should be %NULL-terminated; if not empty,
1856 * it should contain pairs of strings that modify the save
1857 * parameters. For example:
1858 * <informalexample><programlisting>
1859 * gdk_pixbuf_save (pixbuf, handle, "jpeg", &error,
1860 * "quality", "100", NULL);
1861 * </programlisting></informalexample>
1863 * Currently only few parameters exist. JPEG images can be saved with a
1864 * "quality" parameter; its value should be in the range [0,100].
1866 * Text chunks can be attached to PNG images by specifying parameters of
1867 * the form "tEXt::key", where key is an ASCII string of length 1-79.
1868 * The values are UTF-8 encoded strings. The PNG compression level can
1869 * be specified using the "compression" parameter; it's value is in an
1870 * integer in the range of [0,9].
1872 * ICO images can be saved in depth 16, 24, or 32, by using the "depth"
1873 * parameter. When the ICO saver is given "x_hot" and "y_hot" parameters,
1874 * it produces a CUR instead of an ICO.
1876 * Return value: whether an error was set
1880 gdk_pixbuf_save (GdkPixbuf *pixbuf,
1881 const char *filename,
1886 gchar **keys = NULL;
1887 gchar **values = NULL;
1891 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1893 va_start (args, error);
1895 collect_save_options (args, &keys, &values);
1899 result = gdk_pixbuf_savev (pixbuf, filename, type,
1904 g_strfreev (values);
1911 #undef gdk_pixbuf_save
1914 gdk_pixbuf_save (GdkPixbuf *pixbuf,
1915 const char *filename,
1920 char *utf8_filename;
1921 gchar **keys = NULL;
1922 gchar **values = NULL;
1926 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1928 utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1930 if (utf8_filename == NULL)
1933 va_start (args, error);
1935 collect_save_options (args, &keys, &values);
1939 result = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
1943 g_free (utf8_filename);
1946 g_strfreev (values);
1955 * @pixbuf: a #GdkPixbuf.
1956 * @filename: name of file to save.
1957 * @type: name of file format.
1958 * @option_keys: name of options to set, %NULL-terminated
1959 * @option_values: values for named options
1960 * @error: return location for error, or %NULL
1962 * Saves pixbuf to a file in @type, which is currently "jpeg", "png", "tiff", "ico" or "bmp".
1963 * If @error is set, %FALSE will be returned.
1964 * See gdk_pixbuf_save () for more details.
1966 * Return value: whether an error was set
1970 gdk_pixbuf_savev (GdkPixbuf *pixbuf,
1971 const char *filename,
1974 char **option_values,
1980 g_return_val_if_fail (filename != NULL, FALSE);
1981 g_return_val_if_fail (type != NULL, FALSE);
1982 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1984 f = g_fopen (filename, "wb");
1987 gint save_errno = errno;
1988 gchar *display_name = g_filename_display_name (filename);
1991 g_file_error_from_errno (save_errno),
1992 _("Failed to open '%s' for writing: %s"),
1994 g_strerror (save_errno));
1995 g_free (display_name);
2000 result = gdk_pixbuf_real_save (pixbuf, f, type,
2001 option_keys, option_values,
2006 g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
2011 if (fclose (f) < 0) {
2012 gint save_errno = errno;
2013 gchar *display_name = g_filename_display_name (filename);
2016 g_file_error_from_errno (save_errno),
2017 _("Failed to close '%s' while writing image, all data may not have been saved: %s"),
2019 g_strerror (save_errno));
2020 g_free (display_name);
2029 #undef gdk_pixbuf_savev
2032 gdk_pixbuf_savev (GdkPixbuf *pixbuf,
2033 const char *filename,
2036 char **option_values,
2039 char *utf8_filename;
2042 g_return_val_if_fail (filename != NULL, FALSE);
2044 utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2046 if (utf8_filename == NULL)
2049 retval = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
2050 option_keys, option_values, error);
2052 g_free (utf8_filename);
2060 * gdk_pixbuf_save_to_callback:
2061 * @pixbuf: a #GdkPixbuf.
2062 * @save_func: a function that is called to save each block of data that
2063 * the save routine generates.
2064 * @user_data: user data to pass to the save function.
2065 * @type: name of file format.
2066 * @error: return location for error, or %NULL
2067 * @Varargs: list of key-value save options
2069 * Saves pixbuf in format @type by feeding the produced data to a
2070 * callback. Can be used when you want to store the image to something
2071 * other than a file, such as an in-memory buffer or a socket.
2072 * If @error is set, %FALSE will be returned. Possible errors
2073 * include those in the #GDK_PIXBUF_ERROR domain and whatever the save
2074 * function generates.
2076 * See gdk_pixbuf_save() for more details.
2078 * Return value: whether an error was set
2083 gdk_pixbuf_save_to_callback (GdkPixbuf *pixbuf,
2084 GdkPixbufSaveFunc save_func,
2090 gchar **keys = NULL;
2091 gchar **values = NULL;
2095 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2097 va_start (args, error);
2099 collect_save_options (args, &keys, &values);
2103 result = gdk_pixbuf_save_to_callbackv (pixbuf, save_func, user_data,
2108 g_strfreev (values);
2114 * gdk_pixbuf_save_to_callbackv:
2115 * @pixbuf: a #GdkPixbuf.
2116 * @save_func: a function that is called to save each block of data that
2117 * the save routine generates.
2118 * @user_data: user data to pass to the save function.
2119 * @type: name of file format.
2120 * @option_keys: name of options to set, %NULL-terminated
2121 * @option_values: values for named options
2122 * @error: return location for error, or %NULL
2124 * Saves pixbuf to a callback in format @type, which is currently "jpeg",
2125 * "png", "tiff", "ico" or "bmp". If @error is set, %FALSE will be returned. See
2126 * gdk_pixbuf_save_to_callback () for more details.
2128 * Return value: whether an error was set
2133 gdk_pixbuf_save_to_callbackv (GdkPixbuf *pixbuf,
2134 GdkPixbufSaveFunc save_func,
2138 char **option_values,
2144 g_return_val_if_fail (save_func != NULL, FALSE);
2145 g_return_val_if_fail (type != NULL, FALSE);
2146 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2148 result = gdk_pixbuf_real_save_to_callback (pixbuf,
2149 save_func, user_data, type,
2150 option_keys, option_values,
2154 g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
2162 * gdk_pixbuf_save_to_buffer:
2163 * @pixbuf: a #GdkPixbuf.
2164 * @buffer: location to receive a pointer to the new buffer.
2165 * @buffer_size: location to receive the size of the new buffer.
2166 * @type: name of file format.
2167 * @error: return location for error, or %NULL
2168 * @Varargs: list of key-value save options
2170 * Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2171 * "png", "tiff", "ico" or "bmp". This is a convenience function that uses
2172 * gdk_pixbuf_save_to_callback() to do the real work. Note that the buffer
2173 * is not nul-terminated and may contain embedded nuls.
2174 * If @error is set, %FALSE will be returned and @buffer will be set to
2175 * %NULL. Possible errors include those in the #GDK_PIXBUF_ERROR
2178 * See gdk_pixbuf_save() for more details.
2180 * Return value: whether an error was set
2185 gdk_pixbuf_save_to_buffer (GdkPixbuf *pixbuf,
2192 gchar **keys = NULL;
2193 gchar **values = NULL;
2197 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2199 va_start (args, error);
2201 collect_save_options (args, &keys, &values);
2205 result = gdk_pixbuf_save_to_bufferv (pixbuf, buffer, buffer_size,
2210 g_strfreev (values);
2215 struct SaveToBufferData {
2221 save_to_buffer_callback (const gchar *data,
2226 struct SaveToBufferData *sdata = user_data;
2230 if (sdata->len + count > sdata->max) {
2231 new_max = MAX (sdata->max*2, sdata->len + count);
2232 new_buffer = g_try_realloc (sdata->buffer, new_max);
2234 g_set_error_literal (error,
2236 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2237 _("Insufficient memory to save image into a buffer"));
2240 sdata->buffer = new_buffer;
2241 sdata->max = new_max;
2243 memcpy (sdata->buffer + sdata->len, data, count);
2244 sdata->len += count;
2249 * gdk_pixbuf_save_to_bufferv:
2250 * @pixbuf: a #GdkPixbuf.
2251 * @buffer: location to receive a pointer to the new buffer.
2252 * @buffer_size: location to receive the size of the new buffer.
2253 * @type: name of file format.
2254 * @option_keys: name of options to set, %NULL-terminated
2255 * @option_values: values for named options
2256 * @error: return location for error, or %NULL
2258 * Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2259 * "tiff", "png", "ico" or "bmp". See gdk_pixbuf_save_to_buffer()
2262 * Return value: whether an error was set
2267 gdk_pixbuf_save_to_bufferv (GdkPixbuf *pixbuf,
2272 char **option_values,
2275 static const gint initial_max = 1024;
2276 struct SaveToBufferData sdata;
2281 sdata.buffer = g_try_malloc (initial_max);
2282 sdata.max = initial_max;
2284 if (!sdata.buffer) {
2285 g_set_error_literal (error,
2287 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2288 _("Insufficient memory to save image into a buffer"));
2292 if (!gdk_pixbuf_save_to_callbackv (pixbuf,
2293 save_to_buffer_callback, &sdata,
2294 type, option_keys, option_values,
2296 g_free (sdata.buffer);
2300 *buffer = sdata.buffer;
2301 *buffer_size = sdata.len;
2306 GOutputStream *stream;
2307 GCancellable *cancellable;
2311 save_to_stream (const gchar *buffer,
2316 SaveToStreamData *sdata = (SaveToStreamData *)data;
2319 GError *my_error = NULL;
2323 while (remaining > 0) {
2325 remaining -= written;
2326 written = g_output_stream_write (sdata->stream,
2332 g_set_error_literal (error,
2334 _("Error writing to image stream"));
2337 g_propagate_error (error, my_error);
2347 * gdk_pixbuf_save_to_stream:
2348 * @pixbuf: a #GdkPixbuf
2349 * @stream: a #GOutputStream to save the pixbuf to
2350 * @type: name of file format
2351 * @cancellable: optional #GCancellable object, %NULL to ignore
2352 * @error: return location for error, or %NULL
2353 * @Varargs: list of key-value save options
2355 * Saves @pixbuf to an output stream.
2357 * Supported file formats are currently "jpeg", "tiff", "png", "ico" or
2358 * "bmp". See gdk_pixbuf_save_to_buffer() for more details.
2360 * The @cancellable can be used to abort the operation from another
2361 * thread. If the operation was cancelled, the error %GIO_ERROR_CANCELLED
2362 * will be returned. Other possible errors are in the #GDK_PIXBUF_ERROR
2363 * and %G_IO_ERROR domains.
2365 * The stream is not closed.
2367 * Returns: %TRUE if the pixbuf was saved successfully, %FALSE if an
2373 gdk_pixbuf_save_to_stream (GdkPixbuf *pixbuf,
2374 GOutputStream *stream,
2376 GCancellable *cancellable,
2381 gchar **keys = NULL;
2382 gchar **values = NULL;
2384 SaveToStreamData data;
2386 va_start (args, error);
2387 collect_save_options (args, &keys, &values);
2390 data.stream = stream;
2391 data.cancellable = cancellable;
2393 if (!gdk_pixbuf_save_to_callbackv (pixbuf, save_to_stream,
2397 error = NULL; /* Ignore further errors */
2402 g_strfreev (values);
2408 * gdk_pixbuf_format_get_name:
2409 * @format: a #GdkPixbufFormat
2411 * Returns the name of the format.
2413 * Return value: the name of the format.
2418 gdk_pixbuf_format_get_name (GdkPixbufFormat *format)
2420 g_return_val_if_fail (format != NULL, NULL);
2422 return g_strdup (format->name);
2426 * gdk_pixbuf_format_get_description:
2427 * @format: a #GdkPixbufFormat
2429 * Returns a description of the format.
2431 * Return value: a description of the format.
2436 gdk_pixbuf_format_get_description (GdkPixbufFormat *format)
2440 g_return_val_if_fail (format != NULL, NULL);
2442 if (format->domain != NULL)
2443 domain = format->domain;
2445 domain = GETTEXT_PACKAGE;
2446 description = dgettext (domain, format->description);
2448 return g_strdup (description);
2452 * gdk_pixbuf_format_get_mime_types:
2453 * @format: a #GdkPixbufFormat
2455 * Returns the mime types supported by the format.
2457 * Return value: a %NULL-terminated array of mime types which must be freed with
2458 * g_strfreev() when it is no longer needed.
2463 gdk_pixbuf_format_get_mime_types (GdkPixbufFormat *format)
2465 g_return_val_if_fail (format != NULL, NULL);
2467 return g_strdupv (format->mime_types);
2471 * gdk_pixbuf_format_get_extensions:
2472 * @format: a #GdkPixbufFormat
2474 * Returns the filename extensions typically used for files in the
2477 * Return value: a %NULL-terminated array of filename extensions which must be
2478 * freed with g_strfreev() when it is no longer needed.
2483 gdk_pixbuf_format_get_extensions (GdkPixbufFormat *format)
2485 g_return_val_if_fail (format != NULL, NULL);
2487 return g_strdupv (format->extensions);
2491 * gdk_pixbuf_format_is_writable:
2492 * @format: a #GdkPixbufFormat
2494 * Returns whether pixbufs can be saved in the given format.
2496 * Return value: whether pixbufs can be saved in the given format.
2501 gdk_pixbuf_format_is_writable (GdkPixbufFormat *format)
2503 g_return_val_if_fail (format != NULL, FALSE);
2505 return (format->flags & GDK_PIXBUF_FORMAT_WRITABLE) != 0;
2509 * gdk_pixbuf_format_is_scalable:
2510 * @format: a #GdkPixbufFormat
2512 * Returns whether this image format is scalable. If a file is in a
2513 * scalable format, it is preferable to load it at the desired size,
2514 * rather than loading it at the default size and scaling the
2515 * resulting pixbuf to the desired size.
2517 * Return value: whether this image format is scalable.
2522 gdk_pixbuf_format_is_scalable (GdkPixbufFormat *format)
2524 g_return_val_if_fail (format != NULL, FALSE);
2526 return (format->flags & GDK_PIXBUF_FORMAT_SCALABLE) != 0;
2530 * gdk_pixbuf_format_is_disabled:
2531 * @format: a #GdkPixbufFormat
2533 * Returns whether this image format is disabled. See
2534 * gdk_pixbuf_format_set_disabled().
2536 * Return value: whether this image format is disabled.
2541 gdk_pixbuf_format_is_disabled (GdkPixbufFormat *format)
2543 g_return_val_if_fail (format != NULL, FALSE);
2545 return format->disabled;
2549 * gdk_pixbuf_format_set_disabled:
2550 * @format: a #GdkPixbufFormat
2551 * @disabled: %TRUE to disable the format @format
2553 * Disables or enables an image format. If a format is disabled,
2554 * gdk-pixbuf won't use the image loader for this format to load
2555 * images. Applications can use this to avoid using image loaders
2556 * with an inappropriate license, see gdk_pixbuf_format_get_license().
2561 gdk_pixbuf_format_set_disabled (GdkPixbufFormat *format,
2564 g_return_if_fail (format != NULL);
2566 format->disabled = disabled != FALSE;
2570 * gdk_pixbuf_format_get_license:
2571 * @format: a #GdkPixbufFormat
2573 * Returns information about the license of the image loader for the format. The
2574 * returned string should be a shorthand for a wellknown license, e.g. "LGPL",
2575 * "GPL", "QPL", "GPL/QPL", or "other" to indicate some other license. This
2576 * string should be freed with g_free() when it's no longer needed.
2578 * Returns: a string describing the license of @format.
2583 gdk_pixbuf_format_get_license (GdkPixbufFormat *format)
2585 g_return_val_if_fail (format != NULL, NULL);
2587 return g_strdup (format->license);
2591 _gdk_pixbuf_get_format (GdkPixbufModule *module)
2593 g_return_val_if_fail (module != NULL, NULL);
2595 return module->info;
2599 * gdk_pixbuf_get_formats:
2601 * Obtains the available information about the image formats supported
2604 * Returns: A list of #GdkPixbufFormat<!-- -->s describing the supported
2605 * image formats. The list should be freed when it is no longer needed,
2606 * but the structures themselves are owned by #GdkPixbuf and should not be
2612 gdk_pixbuf_get_formats (void)
2614 GSList *result = NULL;
2617 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
2618 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
2619 GdkPixbufFormat *info = _gdk_pixbuf_get_format (module);
2620 result = g_slist_prepend (result, info);
2627 #define __GDK_PIXBUF_IO_C__
2628 #include "gdk-pixbuf-aliasdef.c"