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 handle */
233 static HMODULE gdk_pixbuf_dll;
236 DllMain (HINSTANCE hinstDLL,
241 case DLL_PROCESS_ATTACH:
242 gdk_pixbuf_dll = (HMODULE) hinstDLL;
252 static char *toplevel = NULL;
254 if (toplevel == NULL)
255 toplevel = g_win32_get_package_installation_directory_of_module (gdk_pixbuf_dll);
261 get_sysconfdir (void)
263 static char *sysconfdir = NULL;
265 if (sysconfdir == NULL)
266 sysconfdir = g_build_filename (get_toplevel (), "etc", NULL);
271 #undef GTK_SYSCONFDIR
272 #define GTK_SYSCONFDIR get_sysconfdir()
275 correct_prefix (gchar **path)
277 if (strncmp (*path, GTK_PREFIX "/", strlen (GTK_PREFIX "/")) == 0 ||
278 strncmp (*path, GTK_PREFIX "\\", strlen (GTK_PREFIX "\\")) == 0)
280 /* This is an entry put there by gdk-pixbuf-query-loaders on the
281 * packager's system. On Windows a prebuilt GTK+ package can be
282 * installed in a random location. The gdk-pixbuf.loaders file
283 * distributed in such a package contains paths from the package
284 * builder's machine. Replace the build-time prefix with the
285 * installation prefix on this machine.
288 *path = g_strconcat (get_toplevel (), tem + strlen (GTK_PREFIX), NULL);
293 #endif /* G_OS_WIN32 */
296 gdk_pixbuf_get_module_file (void)
298 gchar *result = g_strdup (g_getenv ("GDK_PIXBUF_MODULE_FILE"));
301 result = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gdk-pixbuf.loaders", NULL);
306 #endif /* USE_GMODULE */
310 gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
314 gdk_pixbuf_io_init (void)
320 GString *tmp_buf = g_string_new (NULL);
321 gboolean have_error = FALSE;
322 GdkPixbufModule *module = NULL;
323 gchar *filename = gdk_pixbuf_get_module_file ();
326 GdkPixbufModulePattern *pattern;
327 GError *error = NULL;
329 GdkPixbufModule *builtin_module ;
331 /* initialize on separate line to avoid compiler warnings in the
332 * common case of no compiled-in modules.
334 builtin_module = NULL;
336 #define load_one_builtin_module(format) \
337 builtin_module = g_new0 (GdkPixbufModule, 1); \
338 builtin_module->module_name = #format; \
339 if (gdk_pixbuf_load_module_unlocked (builtin_module, NULL)) \
340 file_formats = g_slist_prepend (file_formats, builtin_module);\
342 g_free (builtin_module)
345 load_one_builtin_module (ani);
348 load_one_builtin_module (png);
351 load_one_builtin_module (bmp);
354 load_one_builtin_module (wbmp);
357 load_one_builtin_module (gif);
360 load_one_builtin_module (ico);
363 load_one_builtin_module (jpeg);
366 load_one_builtin_module (pnm);
369 load_one_builtin_module (ras);
372 load_one_builtin_module (tiff);
375 load_one_builtin_module (xpm);
378 load_one_builtin_module (xbm);
381 load_one_builtin_module (tga);
384 load_one_builtin_module (pcx);
387 load_one_builtin_module (icns);
389 #ifdef INCLUDE_jasper
390 load_one_builtin_module (jasper);
392 #ifdef INCLUDE_gdiplus
393 /* We don't bother having the GDI+ loaders individually selectable
394 * for building in or not.
396 load_one_builtin_module (ico);
397 load_one_builtin_module (wmf);
398 load_one_builtin_module (emf);
399 load_one_builtin_module (bmp);
400 load_one_builtin_module (gif);
401 load_one_builtin_module (jpeg);
402 load_one_builtin_module (tiff);
404 #ifdef INCLUDE_gdip_png
405 /* Except the gdip-png loader which normally isn't built at all even */
406 load_one_builtin_module (png);
409 #undef load_one_builtin_module
412 channel = g_io_channel_new_file (filename, "r", &error);
414 /* Don't bother warning if we have some built-in loaders */
415 if (file_formats == NULL)
416 g_warning ("Cannot open pixbuf loader module file '%s': %s",
417 filename, error->message);
418 g_string_free (tmp_buf, TRUE);
423 while (!have_error && g_io_channel_read_line (channel, &line_buf, NULL, &term, NULL) == G_IO_STATUS_NORMAL) {
430 if (!skip_space (&p)) {
431 /* Blank line marking the end of a module
433 if (module && *p != '#') {
435 correct_prefix (&module->module_path);
437 file_formats = g_slist_prepend (file_formats, module);
448 /* Read a module location
450 module = g_new0 (GdkPixbufModule, 1);
453 if (!scan_string (&p, tmp_buf)) {
454 g_warning ("Error parsing loader info in '%s'\n %s",
458 module->module_path = g_strdup (tmp_buf->str);
460 else if (!module->module_name) {
461 module->info = g_new0 (GdkPixbufFormat, 1);
462 if (!scan_string (&p, tmp_buf)) {
463 g_warning ("Error parsing loader info in '%s'\n %s",
467 module->info->name = g_strdup (tmp_buf->str);
468 module->module_name = module->info->name;
470 if (!scan_int (&p, &flags)) {
471 g_warning ("Error parsing loader info in '%s'\n %s",
475 module->info->flags = flags;
477 if (!scan_string (&p, tmp_buf)) {
478 g_warning ("Error parsing loader info in '%s'\n %s",
482 if (tmp_buf->str[0] != 0)
483 module->info->domain = g_strdup (tmp_buf->str);
485 if (!scan_string (&p, tmp_buf)) {
486 g_warning ("Error parsing loader info in '%s'\n %s",
490 module->info->description = g_strdup (tmp_buf->str);
492 if (scan_string (&p, tmp_buf)) {
493 module->info->license = g_strdup (tmp_buf->str);
496 else if (!module->info->mime_types) {
498 module->info->mime_types = g_new0 (gchar*, 1);
499 while (scan_string (&p, tmp_buf)) {
500 if (tmp_buf->str[0] != 0) {
501 module->info->mime_types =
502 g_realloc (module->info->mime_types, (n + 1) * sizeof (gchar*));
503 module->info->mime_types[n - 1] = g_strdup (tmp_buf->str);
504 module->info->mime_types[n] = NULL;
509 else if (!module->info->extensions) {
511 module->info->extensions = g_new0 (gchar*, 1);
512 while (scan_string (&p, tmp_buf)) {
513 if (tmp_buf->str[0] != 0) {
514 module->info->extensions =
515 g_realloc (module->info->extensions, (n + 1) * sizeof (gchar*));
516 module->info->extensions[n - 1] = g_strdup (tmp_buf->str);
517 module->info->extensions[n] = NULL;
524 module->info->signature = (GdkPixbufModulePattern *)
525 g_realloc (module->info->signature, (n_patterns + 1) * sizeof (GdkPixbufModulePattern));
526 pattern = module->info->signature + n_patterns;
527 pattern->prefix = NULL;
528 pattern->mask = NULL;
529 pattern->relevance = 0;
531 if (!scan_string (&p, tmp_buf))
533 pattern->prefix = g_strdup (tmp_buf->str);
535 if (!scan_string (&p, tmp_buf))
538 pattern->mask = g_strdup (tmp_buf->str);
540 pattern->mask = NULL;
542 if (!scan_int (&p, &pattern->relevance))
548 g_free (pattern->prefix);
549 g_free (pattern->mask);
551 g_warning ("Error parsing loader info in '%s'\n %s",
558 g_string_free (tmp_buf, TRUE);
559 g_io_channel_unref (channel);
565 #define module(type) \
566 extern void _gdk_pixbuf__##type##_fill_info (GdkPixbufFormat *info); \
567 extern void _gdk_pixbuf__##type##_fill_vtable (GdkPixbufModule *module)
596 /* actually load the image handler - gdk_pixbuf_get_module only get a */
597 /* reference to the module to load, it doesn't actually load it */
598 /* perhaps these actions should be combined in one function */
600 gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
603 GdkPixbufModuleFillInfoFunc fill_info = NULL;
604 GdkPixbufModuleFillVtableFunc fill_vtable = NULL;
606 if (image_module->module != NULL)
609 #define try_module(format,id) \
610 if (fill_info == NULL && \
611 strcmp (image_module->module_name, #format) == 0) { \
612 fill_info = _gdk_pixbuf__##id##_fill_info; \
613 fill_vtable = _gdk_pixbuf__##id##_fill_vtable; \
616 try_module (png,png);
619 try_module (bmp,bmp);
622 try_module (wbmp,wbmp);
625 try_module (gif,gif);
628 try_module (ico,ico);
631 try_module (ani,ani);
634 try_module (jpeg,jpeg);
637 try_module (pnm,pnm);
640 try_module (ras,ras);
643 try_module (tiff,tiff);
646 try_module (xpm,xpm);
649 try_module (xbm,xbm);
652 try_module (tga,tga);
655 try_module (pcx,pcx);
658 try_module (icns,icns);
660 #ifdef INCLUDE_jasper
661 try_module (jasper,jasper);
663 #ifdef INCLUDE_gdiplus
664 try_module (ico,gdip_ico);
665 try_module (wmf,gdip_wmf);
666 try_module (emf,gdip_emf);
667 try_module (bmp,gdip_bmp);
668 try_module (gif,gdip_gif);
669 try_module (jpeg,gdip_jpeg);
670 try_module (tiff,gdip_tiff);
672 #ifdef INCLUDE_gdip_png
673 try_module (png,gdip_png);
679 image_module->module = (void *) 1;
680 (* fill_vtable) (image_module);
681 if (image_module->info == NULL) {
682 image_module->info = g_new0 (GdkPixbufFormat, 1);
683 (* fill_info) (image_module->info);
694 path = image_module->module_path;
695 module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
700 GDK_PIXBUF_ERROR_FAILED,
701 _("Unable to load image-loading module: %s: %s"),
702 path, g_module_error ());
706 image_module->module = module;
708 if (g_module_symbol (module, "fill_vtable", &sym)) {
709 fill_vtable = (GdkPixbufModuleFillVtableFunc) sym;
710 (* fill_vtable) (image_module);
715 GDK_PIXBUF_ERROR_FAILED,
716 _("Image-loading module %s does not export the proper interface; perhaps it's from a different GTK version?"),
724 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
725 _("Image type '%s' is not supported"),
726 image_module->module_name);
728 #endif /* !USE_GMODULE */
733 _gdk_pixbuf_load_module (GdkPixbufModule *image_module,
737 gboolean locked = FALSE;
739 /* be extra careful, maybe the module initializes
742 if (g_threads_got_initialized) {
747 ret = gdk_pixbuf_load_module_unlocked (image_module, error);
750 G_UNLOCK (init_lock);
758 _gdk_pixbuf_get_named_module (const char *name,
763 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
764 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
766 if (module->info->disabled)
769 if (!strcmp (name, module->module_name))
775 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
776 _("Image type '%s' is not supported"),
783 _gdk_pixbuf_get_module (guchar *buffer, guint size,
784 const gchar *filename,
789 GdkPixbufModule *selected = NULL;
790 gchar *display_name = NULL;
791 #ifdef GDK_PIXBUF_USE_GIO_MIME
798 mime_type = g_content_type_guess (NULL, buffer, size, &uncertain);
800 mime_type = g_content_type_guess (filename, buffer, size, NULL);
802 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
803 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
804 GdkPixbufFormat *info = module->info;
809 mimes = info->mime_types;
810 for (j = 0; mimes[j] != NULL; j++) {
811 type = g_content_type_from_mime_type (mimes[j]);
812 if (g_ascii_strcasecmp (type, mime_type) == 0) {
822 gint score, best = 0;
824 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
825 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
827 if (module->info->disabled)
830 score = format_check (module, buffer, size);
840 if (selected != NULL)
845 display_name = g_filename_display_name (filename);
848 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
849 _("Couldn't recognize the image file format for file '%s'"),
851 g_free (display_name);
854 g_set_error_literal (error,
856 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
857 _("Unrecognized image file format"));
865 prepared_notify (GdkPixbuf *pixbuf,
866 GdkPixbufAnimation *anim,
870 g_object_ref (pixbuf);
871 *((GdkPixbuf **)user_data) = pixbuf;
875 _gdk_pixbuf_generic_image_load (GdkPixbufModule *module,
879 guchar buffer[LOAD_BUFFER_SIZE];
881 GdkPixbuf *pixbuf = NULL;
882 GdkPixbufAnimation *animation = NULL;
886 locked = _gdk_pixbuf_lock (module);
888 if (module->load != NULL) {
889 pixbuf = (* module->load) (f, error);
890 } else if (module->begin_load != NULL) {
892 context = module->begin_load (NULL, prepared_notify, NULL, &pixbuf, error);
897 while (!feof (f) && !ferror (f)) {
898 length = fread (buffer, 1, sizeof (buffer), f);
900 if (!module->load_increment (context, buffer, length, error)) {
901 module->stop_load (context, NULL);
902 if (pixbuf != NULL) {
903 g_object_unref (pixbuf);
910 if (!module->stop_load (context, error)) {
911 if (pixbuf != NULL) {
912 g_object_unref (pixbuf);
916 } else if (module->load_animation != NULL) {
917 animation = (* module->load_animation) (f, error);
918 if (animation != NULL) {
919 pixbuf = gdk_pixbuf_animation_get_static_image (animation);
921 g_object_ref (pixbuf);
922 g_object_unref (animation);
928 _gdk_pixbuf_unlock (module);
933 * gdk_pixbuf_new_from_file:
934 * @filename: Name of file to load, in the GLib file name encoding
935 * @error: Return location for an error
937 * Creates a new pixbuf by loading an image from a file. The file format is
938 * detected automatically. If %NULL is returned, then @error will be set.
939 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
941 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
942 * any of several error conditions occurred: the file could not be opened,
943 * there was no loader for the file's format, there was not enough memory to
944 * allocate the image buffer, or the image file contained invalid data.
947 gdk_pixbuf_new_from_file (const char *filename,
953 guchar buffer[SNIFF_BUFFER_SIZE];
954 GdkPixbufModule *image_module;
957 g_return_val_if_fail (filename != NULL, NULL);
958 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
960 display_name = g_filename_display_name (filename);
962 f = g_fopen (filename, "rb");
964 gint save_errno = errno;
967 g_file_error_from_errno (save_errno),
968 _("Failed to open file '%s': %s"),
970 g_strerror (save_errno));
971 g_free (display_name);
975 size = fread (&buffer, 1, sizeof (buffer), f);
979 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
980 _("Image file '%s' contains no data"),
982 g_free (display_name);
987 image_module = _gdk_pixbuf_get_module (buffer, size, filename, error);
988 if (image_module == NULL) {
989 g_free (display_name);
994 if (!_gdk_pixbuf_load_module (image_module, error)) {
995 g_free (display_name);
1000 fseek (f, 0, SEEK_SET);
1001 pixbuf = _gdk_pixbuf_generic_image_load (image_module, f, error);
1004 if (pixbuf == NULL && error != NULL && *error == NULL) {
1006 /* I don't trust these crufty longjmp()'ing image libs
1007 * to maintain proper error invariants, and I don't
1008 * want user code to segfault as a result. We need to maintain
1009 * the invariant that error gets set if NULL is returned.
1012 g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.", image_module->module_name);
1015 GDK_PIXBUF_ERROR_FAILED,
1016 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1018 } else if (error != NULL && *error != NULL) {
1020 /* Add the filename to the error message */
1025 e->message = g_strdup_printf (_("Failed to load image '%s': %s"),
1031 g_free (display_name);
1037 #undef gdk_pixbuf_new_from_file
1039 gdk_pixbuf_new_from_file (const char *filename,
1042 gchar *utf8_filename =
1043 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1046 if (utf8_filename == NULL)
1049 retval = gdk_pixbuf_new_from_file_utf8 (utf8_filename, error);
1051 g_free (utf8_filename);
1059 * gdk_pixbuf_new_from_file_at_size:
1060 * @filename: Name of file to load, in the GLib file name encoding
1061 * @width: The width the image should have or -1 to not constrain the width
1062 * @height: The height the image should have or -1 to not constrain the height
1063 * @error: Return location for an error
1065 * Creates a new pixbuf by loading an image from a file.
1066 * The file format is detected automatically. If %NULL is returned, then
1067 * @error will be set. Possible errors are in the #GDK_PIXBUF_ERROR and
1068 * #G_FILE_ERROR domains.
1070 * The image will be scaled to fit in the requested size, preserving
1071 * the image's aspect ratio. Note that the returned pixbuf may be smaller
1072 * than @width x @height, if the aspect ratio requires it. To load
1073 * and image at the requested size, regardless of aspect ratio, use
1074 * gdk_pixbuf_new_from_file_at_scale().
1076 * Return value: A newly-created pixbuf with a reference count of 1, or
1077 * %NULL if any of several error conditions occurred: the file could not
1078 * be opened, there was no loader for the file's format, there was not
1079 * enough memory to allocate the image buffer, or the image file contained
1085 gdk_pixbuf_new_from_file_at_size (const char *filename,
1090 return gdk_pixbuf_new_from_file_at_scale (filename,
1097 #undef gdk_pixbuf_new_from_file_at_size
1100 gdk_pixbuf_new_from_file_at_size (const char *filename,
1105 gchar *utf8_filename =
1106 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1109 if (utf8_filename == NULL)
1112 retval = gdk_pixbuf_new_from_file_at_size_utf8 (utf8_filename,
1116 g_free (utf8_filename);
1125 gboolean preserve_aspect_ratio;
1129 at_scale_size_prepared_cb (GdkPixbufLoader *loader,
1134 AtScaleData *info = data;
1136 g_return_if_fail (width > 0 && height > 0);
1138 if (info->preserve_aspect_ratio &&
1139 (info->width > 0 || info->height > 0)) {
1140 if (info->width < 0)
1142 width = width * (double)info->height/(double)height;
1143 height = info->height;
1145 else if (info->height < 0)
1147 height = height * (double)info->width/(double)width;
1148 width = info->width;
1150 else if ((double)height * (double)info->width >
1151 (double)width * (double)info->height) {
1152 width = 0.5 + (double)width * (double)info->height / (double)height;
1153 height = info->height;
1155 height = 0.5 + (double)height * (double)info->width / (double)width;
1156 width = info->width;
1159 if (info->width > 0)
1160 width = info->width;
1161 if (info->height > 0)
1162 height = info->height;
1165 width = MAX (width, 1);
1166 height = MAX (height, 1);
1168 gdk_pixbuf_loader_set_size (loader, width, height);
1172 * gdk_pixbuf_new_from_file_at_scale:
1173 * @filename: Name of file to load, in the GLib file name encoding
1174 * @width: The width the image should have or -1 to not constrain the width
1175 * @height: The height the image should have or -1 to not constrain the height
1176 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1177 * @error: Return location for an error
1179 * Creates a new pixbuf by loading an image from a file. The file format is
1180 * detected automatically. If %NULL is returned, then @error will be set.
1181 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
1182 * The image will be scaled to fit in the requested size, optionally preserving
1183 * the image's aspect ratio.
1185 * When preserving the aspect ratio, a @width of -1 will cause the image
1186 * to be scaled to the exact given height, and a @height of -1 will cause
1187 * the image to be scaled to the exact given width. When not preserving
1188 * aspect ratio, a @width or @height of -1 means to not scale the image
1189 * at all in that dimension. Negative values for @width and @height are
1190 * allowed since 2.8.
1192 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL
1193 * if any of several error conditions occurred: the file could not be opened,
1194 * there was no loader for the file's format, there was not enough memory to
1195 * allocate the image buffer, or the image file contained invalid data.
1200 gdk_pixbuf_new_from_file_at_scale (const char *filename,
1203 gboolean preserve_aspect_ratio,
1207 GdkPixbufLoader *loader;
1209 guchar buffer[LOAD_BUFFER_SIZE];
1213 GdkPixbufAnimation *animation;
1214 GdkPixbufAnimationIter *iter;
1217 g_return_val_if_fail (filename != NULL, NULL);
1218 g_return_val_if_fail (width > 0 || width == -1, NULL);
1219 g_return_val_if_fail (height > 0 || height == -1, NULL);
1221 f = g_fopen (filename, "rb");
1223 gint save_errno = errno;
1224 gchar *display_name = g_filename_display_name (filename);
1227 g_file_error_from_errno (save_errno),
1228 _("Failed to open file '%s': %s"),
1230 g_strerror (save_errno));
1231 g_free (display_name);
1235 loader = gdk_pixbuf_loader_new ();
1238 info.height = height;
1239 info.preserve_aspect_ratio = preserve_aspect_ratio;
1241 g_signal_connect (loader, "size-prepared",
1242 G_CALLBACK (at_scale_size_prepared_cb), &info);
1245 while (!has_frame && !feof (f) && !ferror (f)) {
1246 length = fread (buffer, 1, sizeof (buffer), f);
1248 if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
1249 gdk_pixbuf_loader_close (loader, NULL);
1251 g_object_unref (loader);
1255 animation = gdk_pixbuf_loader_get_animation (loader);
1257 iter = gdk_pixbuf_animation_get_iter (animation, NULL);
1258 if (!gdk_pixbuf_animation_iter_on_currently_loading_frame (iter)) {
1261 g_object_unref (iter);
1267 if (!gdk_pixbuf_loader_close (loader, error) && !has_frame) {
1268 g_object_unref (loader);
1272 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1275 gchar *display_name = g_filename_display_name (filename);
1276 g_object_unref (loader);
1279 GDK_PIXBUF_ERROR_FAILED,
1280 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1282 g_free (display_name);
1286 g_object_ref (pixbuf);
1288 g_object_unref (loader);
1295 #undef gdk_pixbuf_new_from_file_at_scale
1298 gdk_pixbuf_new_from_file_at_scale (const char *filename,
1301 gboolean preserve_aspect_ratio,
1304 gchar *utf8_filename =
1305 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1308 if (utf8_filename == NULL)
1311 retval = gdk_pixbuf_new_from_file_at_scale_utf8 (utf8_filename,
1313 preserve_aspect_ratio,
1316 g_free (utf8_filename);
1324 load_from_stream (GdkPixbufLoader *loader,
1325 GInputStream *stream,
1326 GCancellable *cancellable,
1331 guchar buffer[LOAD_BUFFER_SIZE];
1336 n_read = g_input_stream_read (stream,
1343 error = NULL; /* Ignore further errors */
1350 if (!gdk_pixbuf_loader_write (loader,
1360 if (!gdk_pixbuf_loader_close (loader, error)) {
1367 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1369 g_object_ref (pixbuf);
1377 * gdk_pixbuf_new_from_stream_at_scale:
1378 * @stream: a #GInputStream to load the pixbuf from
1379 * @width: The width the image should have or -1 to not constrain the width
1380 * @height: The height the image should have or -1 to not constrain the height
1381 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1382 * @cancellable: optional #GCancellable object, %NULL to ignore
1383 * @error: Return location for an error
1385 * Creates a new pixbuf by loading an image from an input stream.
1387 * The file format is detected automatically. If %NULL is returned, then
1388 * @error will be set. The @cancellable can be used to abort the operation
1389 * from another thread. If the operation was cancelled, the error
1390 * %GIO_ERROR_CANCELLED will be returned. Other possible errors are in
1391 * the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
1393 * The image will be scaled to fit in the requested size, optionally
1394 * preserving the image's aspect ratio. When preserving the aspect ratio,
1395 * a @width of -1 will cause the image to be scaled to the exact given
1396 * height, and a @height of -1 will cause the image to be scaled to the
1397 * exact given width. When not preserving aspect ratio, a @width or
1398 * @height of -1 means to not scale the image at all in that dimension.
1400 * The stream is not closed.
1402 * Return value: A newly-created pixbuf, or %NULL if any of several error
1403 * conditions occurred: the file could not be opened, the image format is
1404 * not supported, there was not enough memory to allocate the image buffer,
1405 * the stream contained invalid data, or the operation was cancelled.
1410 gdk_pixbuf_new_from_stream_at_scale (GInputStream *stream,
1413 gboolean preserve_aspect_ratio,
1414 GCancellable *cancellable,
1417 GdkPixbufLoader *loader;
1421 loader = gdk_pixbuf_loader_new ();
1424 info.height = height;
1425 info.preserve_aspect_ratio = preserve_aspect_ratio;
1427 g_signal_connect (loader, "size-prepared",
1428 G_CALLBACK (at_scale_size_prepared_cb), &info);
1430 pixbuf = load_from_stream (loader, stream, cancellable, error);
1431 g_object_unref (loader);
1437 * gdk_pixbuf_new_from_stream:
1438 * @stream: a #GInputStream to load the pixbuf from
1439 * @cancellable: optional #GCancellable object, %NULL to ignore
1440 * @error: Return location for an error
1442 * Creates a new pixbuf by loading an image from an input stream.
1444 * The file format is detected automatically. If %NULL is returned, then
1445 * @error will be set. The @cancellable can be used to abort the operation
1446 * from another thread. If the operation was cancelled, the error
1447 * %GIO_ERROR_CANCELLED will be returned. Other possible errors are in
1448 * the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
1450 * The stream is not closed.
1452 * Return value: A newly-created pixbuf, or %NULL if any of several error
1453 * conditions occurred: the file could not be opened, the image format is
1454 * not supported, there was not enough memory to allocate the image buffer,
1455 * the stream contained invalid data, or the operation was cancelled.
1460 gdk_pixbuf_new_from_stream (GInputStream *stream,
1461 GCancellable *cancellable,
1465 GdkPixbufLoader *loader;
1467 loader = gdk_pixbuf_loader_new ();
1468 pixbuf = load_from_stream (loader, stream, cancellable, error);
1469 g_object_unref (loader);
1475 info_cb (GdkPixbufLoader *loader,
1481 GdkPixbufFormat *format;
1486 g_return_if_fail (width > 0 && height > 0);
1488 info->format = gdk_pixbuf_loader_get_format (loader);
1489 info->width = width;
1490 info->height = height;
1492 gdk_pixbuf_loader_set_size (loader, 0, 0);
1496 * gdk_pixbuf_get_file_info:
1497 * @filename: The name of the file to identify.
1498 * @width: Return location for the width of the image, or %NULL
1499 * @height: Return location for the height of the image, or %NULL
1501 * Parses an image file far enough to determine its format and size.
1503 * Returns: A #GdkPixbufFormat describing the image format of the file
1504 * or %NULL if the image format wasn't recognized. The return value
1505 * is owned by GdkPixbuf and should not be freed.
1510 gdk_pixbuf_get_file_info (const gchar *filename,
1514 GdkPixbufLoader *loader;
1515 guchar buffer[SNIFF_BUFFER_SIZE];
1519 GdkPixbufFormat *format;
1524 g_return_val_if_fail (filename != NULL, NULL);
1526 f = g_fopen (filename, "rb");
1530 loader = gdk_pixbuf_loader_new ();
1536 g_signal_connect (loader, "size-prepared", G_CALLBACK (info_cb), &info);
1538 while (!feof (f) && !ferror (f)) {
1539 length = fread (buffer, 1, sizeof (buffer), f);
1541 if (!gdk_pixbuf_loader_write (loader, buffer, length, NULL))
1544 if (info.format != NULL)
1549 gdk_pixbuf_loader_close (loader, NULL);
1550 g_object_unref (loader);
1553 *width = info.width;
1555 *height = info.height;
1561 * gdk_pixbuf_new_from_xpm_data:
1562 * @data: Pointer to inline XPM data.
1564 * Creates a new pixbuf by parsing XPM data in memory. This data is commonly
1565 * the result of including an XPM file into a program's C source.
1567 * Return value: A newly-created pixbuf with a reference count of 1.
1570 gdk_pixbuf_new_from_xpm_data (const char **data)
1572 GdkPixbuf *(* load_xpm_data) (const char **data);
1574 GError *error = NULL;
1575 GdkPixbufModule *xpm_module;
1578 g_return_val_if_fail (data != NULL, NULL);
1580 xpm_module = _gdk_pixbuf_get_named_module ("xpm", &error);
1581 if (xpm_module == NULL) {
1582 g_warning ("Error loading XPM image loader: %s", error->message);
1583 g_error_free (error);
1587 if (!_gdk_pixbuf_load_module (xpm_module, &error)) {
1588 g_warning ("Error loading XPM image loader: %s", error->message);
1589 g_error_free (error);
1593 locked = _gdk_pixbuf_lock (xpm_module);
1595 if (xpm_module->load_xpm_data == NULL) {
1596 g_warning ("gdk-pixbuf XPM module lacks XPM data capability");
1599 load_xpm_data = xpm_module->load_xpm_data;
1600 pixbuf = (* load_xpm_data) (data);
1604 _gdk_pixbuf_unlock (xpm_module);
1609 collect_save_options (va_list opts,
1622 next = va_arg (opts, gchar*);
1626 val = va_arg (opts, gchar*);
1631 *keys = g_realloc (*keys, sizeof(gchar*) * (count + 1));
1632 *vals = g_realloc (*vals, sizeof(gchar*) * (count + 1));
1634 (*keys)[count-1] = g_strdup (key);
1635 (*vals)[count-1] = g_strdup (val);
1637 (*keys)[count] = NULL;
1638 (*vals)[count] = NULL;
1640 next = va_arg (opts, gchar*);
1645 save_to_file_callback (const gchar *buf,
1650 FILE *filehandle = data;
1653 n = fwrite (buf, 1, count, filehandle);
1655 gint save_errno = errno;
1658 g_file_error_from_errno (save_errno),
1659 _("Error writing to image file: %s"),
1660 g_strerror (save_errno));
1667 gdk_pixbuf_real_save (GdkPixbuf *pixbuf,
1675 GdkPixbufModule *image_module = NULL;
1678 image_module = _gdk_pixbuf_get_named_module (type, error);
1680 if (image_module == NULL)
1683 if (!_gdk_pixbuf_load_module (image_module, error))
1686 locked = _gdk_pixbuf_lock (image_module);
1688 if (image_module->save) {
1690 ret = (* image_module->save) (filehandle, pixbuf,
1693 } else if (image_module->save_to_callback) {
1694 /* save with simple callback */
1695 ret = (* image_module->save_to_callback) (save_to_file_callback,
1703 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
1704 _("This build of gdk-pixbuf does not support saving the image format: %s"),
1710 _gdk_pixbuf_unlock (image_module);
1714 #define TMP_FILE_BUF_SIZE 4096
1717 save_to_callback_with_tmp_file (GdkPixbufModule *image_module,
1719 GdkPixbufSaveFunc save_func,
1727 gboolean retval = FALSE;
1730 gchar *filename = NULL;
1733 buf = g_try_malloc (TMP_FILE_BUF_SIZE);
1735 g_set_error_literal (error,
1737 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1738 _("Insufficient memory to save image to callback"));
1742 fd = g_file_open_tmp ("gdkpixbuf-save-tmp.XXXXXX", &filename, error);
1745 f = fdopen (fd, "wb+");
1747 gint save_errno = errno;
1748 g_set_error_literal (error,
1750 g_file_error_from_errno (save_errno),
1751 _("Failed to open temporary file"));
1755 locked = _gdk_pixbuf_lock (image_module);
1756 retval = (image_module->save) (f, pixbuf, keys, values, error);
1758 _gdk_pixbuf_unlock (image_module);
1764 n = fread (buf, 1, TMP_FILE_BUF_SIZE, f);
1766 if (!save_func (buf, n, error, user_data))
1769 if (n != TMP_FILE_BUF_SIZE)
1773 gint save_errno = errno;
1774 g_set_error_literal (error,
1776 g_file_error_from_errno (save_errno),
1777 _("Failed to read from temporary file"));
1783 /* cleanup and return retval */
1787 g_unlink (filename);
1796 gdk_pixbuf_real_save_to_callback (GdkPixbuf *pixbuf,
1797 GdkPixbufSaveFunc save_func,
1805 GdkPixbufModule *image_module = NULL;
1808 image_module = _gdk_pixbuf_get_named_module (type, error);
1810 if (image_module == NULL)
1813 if (!_gdk_pixbuf_load_module (image_module, error))
1816 locked = _gdk_pixbuf_lock (image_module);
1818 if (image_module->save_to_callback) {
1820 ret = (* image_module->save_to_callback) (save_func, user_data,
1821 pixbuf, keys, values,
1823 } else if (image_module->save) {
1824 /* use a temporary file */
1825 ret = save_to_callback_with_tmp_file (image_module, pixbuf,
1826 save_func, user_data,
1833 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
1834 _("This build of gdk-pixbuf does not support saving the image format: %s"),
1840 _gdk_pixbuf_unlock (image_module);
1847 * @pixbuf: a #GdkPixbuf.
1848 * @filename: name of file to save.
1849 * @type: name of file format.
1850 * @error: return location for error, or %NULL
1851 * @Varargs: list of key-value save options
1853 * Saves pixbuf to a file in format @type. By default, "jpeg", "png", "ico"
1854 * and "bmp" are possible file formats to save in, but more formats may be
1855 * installed. The list of all writable formats can be determined in the
1859 * void add_if_writable (GdkPixbufFormat *data, GSList **list)
1861 * if (gdk_pixbuf_format_is_writable (data))
1862 * *list = g_slist_prepend (*list, data);
1865 * GSList *formats = gdk_pixbuf_get_formats ();
1866 * GSList *writable_formats = NULL;
1867 * g_slist_foreach (formats, add_if_writable, &writable_formats);
1868 * g_slist_free (formats);
1871 * If @error is set, %FALSE will be returned. Possible errors include
1872 * those in the #GDK_PIXBUF_ERROR domain and those in the #G_FILE_ERROR domain.
1874 * The variable argument list should be %NULL-terminated; if not empty,
1875 * it should contain pairs of strings that modify the save
1876 * parameters. For example:
1877 * <informalexample><programlisting>
1878 * gdk_pixbuf_save (pixbuf, handle, "jpeg", &error,
1879 * "quality", "100", NULL);
1880 * </programlisting></informalexample>
1882 * Currently only few parameters exist. JPEG images can be saved with a
1883 * "quality" parameter; its value should be in the range [0,100].
1885 * Text chunks can be attached to PNG images by specifying parameters of
1886 * the form "tEXt::key", where key is an ASCII string of length 1-79.
1887 * The values are UTF-8 encoded strings. The PNG compression level can
1888 * be specified using the "compression" parameter; it's value is in an
1889 * integer in the range of [0,9].
1891 * ICO images can be saved in depth 16, 24, or 32, by using the "depth"
1892 * parameter. When the ICO saver is given "x_hot" and "y_hot" parameters,
1893 * it produces a CUR instead of an ICO.
1895 * Return value: whether an error was set
1899 gdk_pixbuf_save (GdkPixbuf *pixbuf,
1900 const char *filename,
1905 gchar **keys = NULL;
1906 gchar **values = NULL;
1910 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1912 va_start (args, error);
1914 collect_save_options (args, &keys, &values);
1918 result = gdk_pixbuf_savev (pixbuf, filename, type,
1923 g_strfreev (values);
1930 #undef gdk_pixbuf_save
1933 gdk_pixbuf_save (GdkPixbuf *pixbuf,
1934 const char *filename,
1939 char *utf8_filename;
1940 gchar **keys = NULL;
1941 gchar **values = NULL;
1945 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1947 utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1949 if (utf8_filename == NULL)
1952 va_start (args, error);
1954 collect_save_options (args, &keys, &values);
1958 result = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
1962 g_free (utf8_filename);
1965 g_strfreev (values);
1974 * @pixbuf: a #GdkPixbuf.
1975 * @filename: name of file to save.
1976 * @type: name of file format.
1977 * @option_keys: name of options to set, %NULL-terminated
1978 * @option_values: values for named options
1979 * @error: return location for error, or %NULL
1981 * Saves pixbuf to a file in @type, which is currently "jpeg", "png", "tiff", "ico" or "bmp".
1982 * If @error is set, %FALSE will be returned.
1983 * See gdk_pixbuf_save () for more details.
1985 * Return value: whether an error was set
1989 gdk_pixbuf_savev (GdkPixbuf *pixbuf,
1990 const char *filename,
1993 char **option_values,
1999 g_return_val_if_fail (filename != NULL, FALSE);
2000 g_return_val_if_fail (type != NULL, FALSE);
2001 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2003 f = g_fopen (filename, "wb");
2006 gint save_errno = errno;
2007 gchar *display_name = g_filename_display_name (filename);
2010 g_file_error_from_errno (save_errno),
2011 _("Failed to open '%s' for writing: %s"),
2013 g_strerror (save_errno));
2014 g_free (display_name);
2019 result = gdk_pixbuf_real_save (pixbuf, f, type,
2020 option_keys, option_values,
2025 g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
2030 if (fclose (f) < 0) {
2031 gint save_errno = errno;
2032 gchar *display_name = g_filename_display_name (filename);
2035 g_file_error_from_errno (save_errno),
2036 _("Failed to close '%s' while writing image, all data may not have been saved: %s"),
2038 g_strerror (save_errno));
2039 g_free (display_name);
2048 #undef gdk_pixbuf_savev
2051 gdk_pixbuf_savev (GdkPixbuf *pixbuf,
2052 const char *filename,
2055 char **option_values,
2058 char *utf8_filename;
2061 g_return_val_if_fail (filename != NULL, FALSE);
2063 utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2065 if (utf8_filename == NULL)
2068 retval = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
2069 option_keys, option_values, error);
2071 g_free (utf8_filename);
2079 * gdk_pixbuf_save_to_callback:
2080 * @pixbuf: a #GdkPixbuf.
2081 * @save_func: a function that is called to save each block of data that
2082 * the save routine generates.
2083 * @user_data: user data to pass to the save function.
2084 * @type: name of file format.
2085 * @error: return location for error, or %NULL
2086 * @Varargs: list of key-value save options
2088 * Saves pixbuf in format @type by feeding the produced data to a
2089 * callback. Can be used when you want to store the image to something
2090 * other than a file, such as an in-memory buffer or a socket.
2091 * If @error is set, %FALSE will be returned. Possible errors
2092 * include those in the #GDK_PIXBUF_ERROR domain and whatever the save
2093 * function generates.
2095 * See gdk_pixbuf_save() for more details.
2097 * Return value: whether an error was set
2102 gdk_pixbuf_save_to_callback (GdkPixbuf *pixbuf,
2103 GdkPixbufSaveFunc save_func,
2109 gchar **keys = NULL;
2110 gchar **values = NULL;
2114 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2116 va_start (args, error);
2118 collect_save_options (args, &keys, &values);
2122 result = gdk_pixbuf_save_to_callbackv (pixbuf, save_func, user_data,
2127 g_strfreev (values);
2133 * gdk_pixbuf_save_to_callbackv:
2134 * @pixbuf: a #GdkPixbuf.
2135 * @save_func: a function that is called to save each block of data that
2136 * the save routine generates.
2137 * @user_data: user data to pass to the save function.
2138 * @type: name of file format.
2139 * @option_keys: name of options to set, %NULL-terminated
2140 * @option_values: values for named options
2141 * @error: return location for error, or %NULL
2143 * Saves pixbuf to a callback in format @type, which is currently "jpeg",
2144 * "png", "tiff", "ico" or "bmp". If @error is set, %FALSE will be returned. See
2145 * gdk_pixbuf_save_to_callback () for more details.
2147 * Return value: whether an error was set
2152 gdk_pixbuf_save_to_callbackv (GdkPixbuf *pixbuf,
2153 GdkPixbufSaveFunc save_func,
2157 char **option_values,
2163 g_return_val_if_fail (save_func != NULL, FALSE);
2164 g_return_val_if_fail (type != NULL, FALSE);
2165 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2167 result = gdk_pixbuf_real_save_to_callback (pixbuf,
2168 save_func, user_data, type,
2169 option_keys, option_values,
2173 g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
2181 * gdk_pixbuf_save_to_buffer:
2182 * @pixbuf: a #GdkPixbuf.
2183 * @buffer: location to receive a pointer to the new buffer.
2184 * @buffer_size: location to receive the size of the new buffer.
2185 * @type: name of file format.
2186 * @error: return location for error, or %NULL
2187 * @Varargs: list of key-value save options
2189 * Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2190 * "png", "tiff", "ico" or "bmp". This is a convenience function that uses
2191 * gdk_pixbuf_save_to_callback() to do the real work. Note that the buffer
2192 * is not nul-terminated and may contain embedded nuls.
2193 * If @error is set, %FALSE will be returned and @buffer will be set to
2194 * %NULL. Possible errors include those in the #GDK_PIXBUF_ERROR
2197 * See gdk_pixbuf_save() for more details.
2199 * Return value: whether an error was set
2204 gdk_pixbuf_save_to_buffer (GdkPixbuf *pixbuf,
2211 gchar **keys = NULL;
2212 gchar **values = NULL;
2216 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2218 va_start (args, error);
2220 collect_save_options (args, &keys, &values);
2224 result = gdk_pixbuf_save_to_bufferv (pixbuf, buffer, buffer_size,
2229 g_strfreev (values);
2234 struct SaveToBufferData {
2240 save_to_buffer_callback (const gchar *data,
2245 struct SaveToBufferData *sdata = user_data;
2249 if (sdata->len + count > sdata->max) {
2250 new_max = MAX (sdata->max*2, sdata->len + count);
2251 new_buffer = g_try_realloc (sdata->buffer, new_max);
2253 g_set_error_literal (error,
2255 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2256 _("Insufficient memory to save image into a buffer"));
2259 sdata->buffer = new_buffer;
2260 sdata->max = new_max;
2262 memcpy (sdata->buffer + sdata->len, data, count);
2263 sdata->len += count;
2268 * gdk_pixbuf_save_to_bufferv:
2269 * @pixbuf: a #GdkPixbuf.
2270 * @buffer: location to receive a pointer to the new buffer.
2271 * @buffer_size: location to receive the size of the new buffer.
2272 * @type: name of file format.
2273 * @option_keys: name of options to set, %NULL-terminated
2274 * @option_values: values for named options
2275 * @error: return location for error, or %NULL
2277 * Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2278 * "tiff", "png", "ico" or "bmp". See gdk_pixbuf_save_to_buffer()
2281 * Return value: whether an error was set
2286 gdk_pixbuf_save_to_bufferv (GdkPixbuf *pixbuf,
2291 char **option_values,
2294 static const gint initial_max = 1024;
2295 struct SaveToBufferData sdata;
2300 sdata.buffer = g_try_malloc (initial_max);
2301 sdata.max = initial_max;
2303 if (!sdata.buffer) {
2304 g_set_error_literal (error,
2306 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2307 _("Insufficient memory to save image into a buffer"));
2311 if (!gdk_pixbuf_save_to_callbackv (pixbuf,
2312 save_to_buffer_callback, &sdata,
2313 type, option_keys, option_values,
2315 g_free (sdata.buffer);
2319 *buffer = sdata.buffer;
2320 *buffer_size = sdata.len;
2325 GOutputStream *stream;
2326 GCancellable *cancellable;
2330 save_to_stream (const gchar *buffer,
2335 SaveToStreamData *sdata = (SaveToStreamData *)data;
2338 GError *my_error = NULL;
2342 while (remaining > 0) {
2344 remaining -= written;
2345 written = g_output_stream_write (sdata->stream,
2351 g_set_error_literal (error,
2353 _("Error writing to image stream"));
2356 g_propagate_error (error, my_error);
2366 * gdk_pixbuf_save_to_stream:
2367 * @pixbuf: a #GdkPixbuf
2368 * @stream: a #GOutputStream to save the pixbuf to
2369 * @type: name of file format
2370 * @cancellable: optional #GCancellable object, %NULL to ignore
2371 * @error: return location for error, or %NULL
2372 * @Varargs: list of key-value save options
2374 * Saves @pixbuf to an output stream.
2376 * Supported file formats are currently "jpeg", "tiff", "png", "ico" or
2377 * "bmp". See gdk_pixbuf_save_to_buffer() for more details.
2379 * The @cancellable can be used to abort the operation from another
2380 * thread. If the operation was cancelled, the error %GIO_ERROR_CANCELLED
2381 * will be returned. Other possible errors are in the #GDK_PIXBUF_ERROR
2382 * and %G_IO_ERROR domains.
2384 * The stream is not closed.
2386 * Returns: %TRUE if the pixbuf was saved successfully, %FALSE if an
2392 gdk_pixbuf_save_to_stream (GdkPixbuf *pixbuf,
2393 GOutputStream *stream,
2395 GCancellable *cancellable,
2400 gchar **keys = NULL;
2401 gchar **values = NULL;
2403 SaveToStreamData data;
2405 va_start (args, error);
2406 collect_save_options (args, &keys, &values);
2409 data.stream = stream;
2410 data.cancellable = cancellable;
2412 res = gdk_pixbuf_save_to_callbackv (pixbuf, save_to_stream,
2418 g_strfreev (values);
2424 * gdk_pixbuf_format_get_name:
2425 * @format: a #GdkPixbufFormat
2427 * Returns the name of the format.
2429 * Return value: the name of the format.
2434 gdk_pixbuf_format_get_name (GdkPixbufFormat *format)
2436 g_return_val_if_fail (format != NULL, NULL);
2438 return g_strdup (format->name);
2442 * gdk_pixbuf_format_get_description:
2443 * @format: a #GdkPixbufFormat
2445 * Returns a description of the format.
2447 * Return value: a description of the format.
2452 gdk_pixbuf_format_get_description (GdkPixbufFormat *format)
2456 g_return_val_if_fail (format != NULL, NULL);
2458 if (format->domain != NULL)
2459 domain = format->domain;
2461 domain = GETTEXT_PACKAGE;
2462 description = dgettext (domain, format->description);
2464 return g_strdup (description);
2468 * gdk_pixbuf_format_get_mime_types:
2469 * @format: a #GdkPixbufFormat
2471 * Returns the mime types supported by the format.
2473 * Return value: a %NULL-terminated array of mime types which must be freed with
2474 * g_strfreev() when it is no longer needed.
2479 gdk_pixbuf_format_get_mime_types (GdkPixbufFormat *format)
2481 g_return_val_if_fail (format != NULL, NULL);
2483 return g_strdupv (format->mime_types);
2487 * gdk_pixbuf_format_get_extensions:
2488 * @format: a #GdkPixbufFormat
2490 * Returns the filename extensions typically used for files in the
2493 * Return value: a %NULL-terminated array of filename extensions which must be
2494 * freed with g_strfreev() when it is no longer needed.
2499 gdk_pixbuf_format_get_extensions (GdkPixbufFormat *format)
2501 g_return_val_if_fail (format != NULL, NULL);
2503 return g_strdupv (format->extensions);
2507 * gdk_pixbuf_format_is_writable:
2508 * @format: a #GdkPixbufFormat
2510 * Returns whether pixbufs can be saved in the given format.
2512 * Return value: whether pixbufs can be saved in the given format.
2517 gdk_pixbuf_format_is_writable (GdkPixbufFormat *format)
2519 g_return_val_if_fail (format != NULL, FALSE);
2521 return (format->flags & GDK_PIXBUF_FORMAT_WRITABLE) != 0;
2525 * gdk_pixbuf_format_is_scalable:
2526 * @format: a #GdkPixbufFormat
2528 * Returns whether this image format is scalable. If a file is in a
2529 * scalable format, it is preferable to load it at the desired size,
2530 * rather than loading it at the default size and scaling the
2531 * resulting pixbuf to the desired size.
2533 * Return value: whether this image format is scalable.
2538 gdk_pixbuf_format_is_scalable (GdkPixbufFormat *format)
2540 g_return_val_if_fail (format != NULL, FALSE);
2542 return (format->flags & GDK_PIXBUF_FORMAT_SCALABLE) != 0;
2546 * gdk_pixbuf_format_is_disabled:
2547 * @format: a #GdkPixbufFormat
2549 * Returns whether this image format is disabled. See
2550 * gdk_pixbuf_format_set_disabled().
2552 * Return value: whether this image format is disabled.
2557 gdk_pixbuf_format_is_disabled (GdkPixbufFormat *format)
2559 g_return_val_if_fail (format != NULL, FALSE);
2561 return format->disabled;
2565 * gdk_pixbuf_format_set_disabled:
2566 * @format: a #GdkPixbufFormat
2567 * @disabled: %TRUE to disable the format @format
2569 * Disables or enables an image format. If a format is disabled,
2570 * gdk-pixbuf won't use the image loader for this format to load
2571 * images. Applications can use this to avoid using image loaders
2572 * with an inappropriate license, see gdk_pixbuf_format_get_license().
2577 gdk_pixbuf_format_set_disabled (GdkPixbufFormat *format,
2580 g_return_if_fail (format != NULL);
2582 format->disabled = disabled != FALSE;
2586 * gdk_pixbuf_format_get_license:
2587 * @format: a #GdkPixbufFormat
2589 * Returns information about the license of the image loader for the format. The
2590 * returned string should be a shorthand for a wellknown license, e.g. "LGPL",
2591 * "GPL", "QPL", "GPL/QPL", or "other" to indicate some other license. This
2592 * string should be freed with g_free() when it's no longer needed.
2594 * Returns: a string describing the license of @format.
2599 gdk_pixbuf_format_get_license (GdkPixbufFormat *format)
2601 g_return_val_if_fail (format != NULL, NULL);
2603 return g_strdup (format->license);
2607 _gdk_pixbuf_get_format (GdkPixbufModule *module)
2609 g_return_val_if_fail (module != NULL, NULL);
2611 return module->info;
2615 * gdk_pixbuf_get_formats:
2617 * Obtains the available information about the image formats supported
2620 * Returns: A list of #GdkPixbufFormat<!-- -->s describing the supported
2621 * image formats. The list should be freed when it is no longer needed,
2622 * but the structures themselves are owned by #GdkPixbuf and should not be
2628 gdk_pixbuf_get_formats (void)
2630 GSList *result = NULL;
2633 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
2634 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
2635 GdkPixbufFormat *info = _gdk_pixbuf_get_format (module);
2636 result = g_slist_prepend (result, info);
2643 #define __GDK_PIXBUF_IO_C__
2644 #include "gdk-pixbuf-aliasdef.c"