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)
281 if (strlen(*path) > 5 && strncmp (*path - 5, ".libs", 5) == 0)
283 /* We are being run from inside the build tree, and shouldn't mess about. */
287 /* This is an entry put there by gdk-pixbuf-query-loaders on the
288 * packager's system. On Windows a prebuilt GTK+ package can be
289 * installed in a random location. The gdk-pixbuf.loaders file
290 * distributed in such a package contains paths from the package
291 * builder's machine. Replace the build-time prefix with the
292 * installation prefix on this machine.
295 *path = g_strconcat (get_toplevel (), tem + strlen (GTK_PREFIX), NULL);
300 #endif /* G_OS_WIN32 */
303 gdk_pixbuf_get_module_file (void)
305 gchar *result = g_strdup (g_getenv ("GDK_PIXBUF_MODULE_FILE"));
308 result = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gdk-pixbuf.loaders", NULL);
313 #endif /* USE_GMODULE */
317 gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
321 gdk_pixbuf_io_init (void)
327 GString *tmp_buf = g_string_new (NULL);
328 gboolean have_error = FALSE;
329 GdkPixbufModule *module = NULL;
330 gchar *filename = gdk_pixbuf_get_module_file ();
333 GdkPixbufModulePattern *pattern;
334 GError *error = NULL;
336 GdkPixbufModule *builtin_module ;
338 /* initialize on separate line to avoid compiler warnings in the
339 * common case of no compiled-in modules.
341 builtin_module = NULL;
343 #define load_one_builtin_module(format) \
344 builtin_module = g_new0 (GdkPixbufModule, 1); \
345 builtin_module->module_name = #format; \
346 if (gdk_pixbuf_load_module_unlocked (builtin_module, NULL)) \
347 file_formats = g_slist_prepend (file_formats, builtin_module);\
349 g_free (builtin_module)
352 load_one_builtin_module (ani);
355 load_one_builtin_module (png);
358 load_one_builtin_module (bmp);
361 load_one_builtin_module (wbmp);
364 load_one_builtin_module (gif);
367 load_one_builtin_module (ico);
370 load_one_builtin_module (jpeg);
373 load_one_builtin_module (pnm);
376 load_one_builtin_module (ras);
379 load_one_builtin_module (tiff);
382 load_one_builtin_module (xpm);
385 load_one_builtin_module (xbm);
388 load_one_builtin_module (tga);
391 load_one_builtin_module (pcx);
394 load_one_builtin_module (icns);
396 #ifdef INCLUDE_jasper
397 load_one_builtin_module (jasper);
400 load_one_builtin_module (qtif);
402 #ifdef INCLUDE_gdiplus
403 /* We don't bother having the GDI+ loaders individually selectable
404 * for building in or not.
406 load_one_builtin_module (ico);
407 load_one_builtin_module (wmf);
408 load_one_builtin_module (emf);
409 load_one_builtin_module (bmp);
410 load_one_builtin_module (gif);
411 load_one_builtin_module (jpeg);
412 load_one_builtin_module (tiff);
414 #ifdef INCLUDE_gdip_png
415 /* Except the gdip-png loader which normally isn't built at all even */
416 load_one_builtin_module (png);
419 #undef load_one_builtin_module
422 channel = g_io_channel_new_file (filename, "r", &error);
424 /* Don't bother warning if we have some built-in loaders */
425 if (file_formats == NULL)
426 g_warning ("Cannot open pixbuf loader module file '%s': %s",
427 filename, error->message);
428 g_string_free (tmp_buf, TRUE);
433 while (!have_error && g_io_channel_read_line (channel, &line_buf, NULL, &term, NULL) == G_IO_STATUS_NORMAL) {
440 if (!skip_space (&p)) {
441 /* Blank line marking the end of a module
443 if (module && *p != '#') {
445 correct_prefix (&module->module_path);
447 file_formats = g_slist_prepend (file_formats, module);
458 /* Read a module location
460 module = g_new0 (GdkPixbufModule, 1);
463 if (!scan_string (&p, tmp_buf)) {
464 g_warning ("Error parsing loader info in '%s'\n %s",
468 module->module_path = g_strdup (tmp_buf->str);
470 else if (!module->module_name) {
471 module->info = g_new0 (GdkPixbufFormat, 1);
472 if (!scan_string (&p, tmp_buf)) {
473 g_warning ("Error parsing loader info in '%s'\n %s",
477 module->info->name = g_strdup (tmp_buf->str);
478 module->module_name = module->info->name;
480 if (!scan_int (&p, &flags)) {
481 g_warning ("Error parsing loader info in '%s'\n %s",
485 module->info->flags = flags;
487 if (!scan_string (&p, tmp_buf)) {
488 g_warning ("Error parsing loader info in '%s'\n %s",
492 if (tmp_buf->str[0] != 0)
493 module->info->domain = g_strdup (tmp_buf->str);
495 if (!scan_string (&p, tmp_buf)) {
496 g_warning ("Error parsing loader info in '%s'\n %s",
500 module->info->description = g_strdup (tmp_buf->str);
502 if (scan_string (&p, tmp_buf)) {
503 module->info->license = g_strdup (tmp_buf->str);
506 else if (!module->info->mime_types) {
508 module->info->mime_types = g_new0 (gchar*, 1);
509 while (scan_string (&p, tmp_buf)) {
510 if (tmp_buf->str[0] != 0) {
511 module->info->mime_types =
512 g_realloc (module->info->mime_types, (n + 1) * sizeof (gchar*));
513 module->info->mime_types[n - 1] = g_strdup (tmp_buf->str);
514 module->info->mime_types[n] = NULL;
519 else if (!module->info->extensions) {
521 module->info->extensions = g_new0 (gchar*, 1);
522 while (scan_string (&p, tmp_buf)) {
523 if (tmp_buf->str[0] != 0) {
524 module->info->extensions =
525 g_realloc (module->info->extensions, (n + 1) * sizeof (gchar*));
526 module->info->extensions[n - 1] = g_strdup (tmp_buf->str);
527 module->info->extensions[n] = NULL;
534 module->info->signature = (GdkPixbufModulePattern *)
535 g_realloc (module->info->signature, (n_patterns + 1) * sizeof (GdkPixbufModulePattern));
536 pattern = module->info->signature + n_patterns;
537 pattern->prefix = NULL;
538 pattern->mask = NULL;
539 pattern->relevance = 0;
541 if (!scan_string (&p, tmp_buf))
543 pattern->prefix = g_strdup (tmp_buf->str);
545 if (!scan_string (&p, tmp_buf))
548 pattern->mask = g_strdup (tmp_buf->str);
550 pattern->mask = NULL;
552 if (!scan_int (&p, &pattern->relevance))
558 g_free (pattern->prefix);
559 g_free (pattern->mask);
561 g_warning ("Error parsing loader info in '%s'\n %s",
568 g_string_free (tmp_buf, TRUE);
569 g_io_channel_unref (channel);
575 #define module(type) \
576 extern void _gdk_pixbuf__##type##_fill_info (GdkPixbufFormat *info); \
577 extern void _gdk_pixbuf__##type##_fill_vtable (GdkPixbufModule *module)
607 /* actually load the image handler - gdk_pixbuf_get_module only get a */
608 /* reference to the module to load, it doesn't actually load it */
609 /* perhaps these actions should be combined in one function */
611 gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
614 GdkPixbufModuleFillInfoFunc fill_info = NULL;
615 GdkPixbufModuleFillVtableFunc fill_vtable = NULL;
617 if (image_module->module != NULL)
620 #define try_module(format,id) \
621 if (fill_info == NULL && \
622 strcmp (image_module->module_name, #format) == 0) { \
623 fill_info = _gdk_pixbuf__##id##_fill_info; \
624 fill_vtable = _gdk_pixbuf__##id##_fill_vtable; \
627 try_module (png,png);
630 try_module (bmp,bmp);
633 try_module (wbmp,wbmp);
636 try_module (gif,gif);
639 try_module (ico,ico);
642 try_module (ani,ani);
645 try_module (jpeg,jpeg);
648 try_module (pnm,pnm);
651 try_module (ras,ras);
654 try_module (tiff,tiff);
657 try_module (xpm,xpm);
660 try_module (xbm,xbm);
663 try_module (tga,tga);
666 try_module (pcx,pcx);
669 try_module (icns,icns);
671 #ifdef INCLUDE_jasper
672 try_module (jasper,jasper);
675 try_module (qtif,qtif);
677 #ifdef INCLUDE_gdiplus
678 try_module (ico,gdip_ico);
679 try_module (wmf,gdip_wmf);
680 try_module (emf,gdip_emf);
681 try_module (bmp,gdip_bmp);
682 try_module (gif,gdip_gif);
683 try_module (jpeg,gdip_jpeg);
684 try_module (tiff,gdip_tiff);
686 #ifdef INCLUDE_gdip_png
687 try_module (png,gdip_png);
693 image_module->module = (void *) 1;
694 (* fill_vtable) (image_module);
695 if (image_module->info == NULL) {
696 image_module->info = g_new0 (GdkPixbufFormat, 1);
697 (* fill_info) (image_module->info);
708 path = image_module->module_path;
709 module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
714 GDK_PIXBUF_ERROR_FAILED,
715 _("Unable to load image-loading module: %s: %s"),
716 path, g_module_error ());
720 image_module->module = module;
722 if (g_module_symbol (module, "fill_vtable", &sym)) {
723 fill_vtable = (GdkPixbufModuleFillVtableFunc) sym;
724 (* fill_vtable) (image_module);
729 GDK_PIXBUF_ERROR_FAILED,
730 _("Image-loading module %s does not export the proper interface; perhaps it's from a different GTK version?"),
738 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
739 _("Image type '%s' is not supported"),
740 image_module->module_name);
742 #endif /* !USE_GMODULE */
747 _gdk_pixbuf_load_module (GdkPixbufModule *image_module,
751 gboolean locked = FALSE;
753 /* be extra careful, maybe the module initializes
756 if (g_threads_got_initialized) {
761 ret = gdk_pixbuf_load_module_unlocked (image_module, error);
764 G_UNLOCK (init_lock);
772 _gdk_pixbuf_get_named_module (const char *name,
777 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
778 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
780 if (module->info->disabled)
783 if (!strcmp (name, module->module_name))
789 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
790 _("Image type '%s' is not supported"),
797 _gdk_pixbuf_get_module (guchar *buffer, guint size,
798 const gchar *filename,
803 GdkPixbufModule *selected = NULL;
804 gchar *display_name = NULL;
805 #ifdef GDK_PIXBUF_USE_GIO_MIME
812 mime_type = g_content_type_guess (NULL, buffer, size, &uncertain);
814 mime_type = g_content_type_guess (filename, buffer, size, NULL);
816 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
817 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
818 GdkPixbufFormat *info = module->info;
823 mimes = info->mime_types;
824 for (j = 0; mimes[j] != NULL; j++) {
825 type = g_content_type_from_mime_type (mimes[j]);
826 if (g_ascii_strcasecmp (type, mime_type) == 0) {
836 gint score, best = 0;
838 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
839 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
841 if (module->info->disabled)
844 score = format_check (module, buffer, size);
854 if (selected != NULL)
859 display_name = g_filename_display_name (filename);
862 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
863 _("Couldn't recognize the image file format for file '%s'"),
865 g_free (display_name);
868 g_set_error_literal (error,
870 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
871 _("Unrecognized image file format"));
879 prepared_notify (GdkPixbuf *pixbuf,
880 GdkPixbufAnimation *anim,
884 g_object_ref (pixbuf);
885 *((GdkPixbuf **)user_data) = pixbuf;
889 _gdk_pixbuf_generic_image_load (GdkPixbufModule *module,
893 guchar buffer[LOAD_BUFFER_SIZE];
895 GdkPixbuf *pixbuf = NULL;
896 GdkPixbufAnimation *animation = NULL;
900 locked = _gdk_pixbuf_lock (module);
902 if (module->load != NULL) {
903 pixbuf = (* module->load) (f, error);
904 } else if (module->begin_load != NULL) {
906 context = module->begin_load (NULL, prepared_notify, NULL, &pixbuf, error);
911 while (!feof (f) && !ferror (f)) {
912 length = fread (buffer, 1, sizeof (buffer), f);
914 if (!module->load_increment (context, buffer, length, error)) {
915 module->stop_load (context, NULL);
916 if (pixbuf != NULL) {
917 g_object_unref (pixbuf);
924 if (!module->stop_load (context, error)) {
925 if (pixbuf != NULL) {
926 g_object_unref (pixbuf);
930 } else if (module->load_animation != NULL) {
931 animation = (* module->load_animation) (f, error);
932 if (animation != NULL) {
933 pixbuf = gdk_pixbuf_animation_get_static_image (animation);
935 g_object_ref (pixbuf);
936 g_object_unref (animation);
942 _gdk_pixbuf_unlock (module);
947 * gdk_pixbuf_new_from_file:
948 * @filename: Name of file to load, in the GLib file name encoding
949 * @error: Return location for an error
951 * Creates a new pixbuf by loading an image from a file. The file format is
952 * detected automatically. If %NULL is returned, then @error will be set.
953 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
955 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
956 * any of several error conditions occurred: the file could not be opened,
957 * there was no loader for the file's format, there was not enough memory to
958 * allocate the image buffer, or the image file contained invalid data.
961 gdk_pixbuf_new_from_file (const char *filename,
967 guchar buffer[SNIFF_BUFFER_SIZE];
968 GdkPixbufModule *image_module;
971 g_return_val_if_fail (filename != NULL, NULL);
972 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
974 display_name = g_filename_display_name (filename);
976 f = g_fopen (filename, "rb");
978 gint save_errno = errno;
981 g_file_error_from_errno (save_errno),
982 _("Failed to open file '%s': %s"),
984 g_strerror (save_errno));
985 g_free (display_name);
989 size = fread (&buffer, 1, sizeof (buffer), f);
993 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
994 _("Image file '%s' contains no data"),
996 g_free (display_name);
1001 image_module = _gdk_pixbuf_get_module (buffer, size, filename, error);
1002 if (image_module == NULL) {
1003 g_free (display_name);
1008 if (!_gdk_pixbuf_load_module (image_module, error)) {
1009 g_free (display_name);
1014 fseek (f, 0, SEEK_SET);
1015 pixbuf = _gdk_pixbuf_generic_image_load (image_module, f, error);
1018 if (pixbuf == NULL && error != NULL && *error == NULL) {
1020 /* I don't trust these crufty longjmp()'ing image libs
1021 * to maintain proper error invariants, and I don't
1022 * want user code to segfault as a result. We need to maintain
1023 * the invariant that error gets set if NULL is returned.
1026 g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.", image_module->module_name);
1029 GDK_PIXBUF_ERROR_FAILED,
1030 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1032 } else if (error != NULL && *error != NULL) {
1034 /* Add the filename to the error message */
1039 e->message = g_strdup_printf (_("Failed to load image '%s': %s"),
1045 g_free (display_name);
1051 #undef gdk_pixbuf_new_from_file
1053 gdk_pixbuf_new_from_file (const char *filename,
1056 gchar *utf8_filename =
1057 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1060 if (utf8_filename == NULL)
1063 retval = gdk_pixbuf_new_from_file_utf8 (utf8_filename, error);
1065 g_free (utf8_filename);
1073 * gdk_pixbuf_new_from_file_at_size:
1074 * @filename: Name of file to load, in the GLib file name encoding
1075 * @width: The width the image should have or -1 to not constrain the width
1076 * @height: The height the image should have or -1 to not constrain the height
1077 * @error: Return location for an error
1079 * Creates a new pixbuf by loading an image from a file.
1080 * The file format is detected automatically. If %NULL is returned, then
1081 * @error will be set. Possible errors are in the #GDK_PIXBUF_ERROR and
1082 * #G_FILE_ERROR domains.
1084 * The image will be scaled to fit in the requested size, preserving
1085 * the image's aspect ratio. Note that the returned pixbuf may be smaller
1086 * than @width x @height, if the aspect ratio requires it. To load
1087 * and image at the requested size, regardless of aspect ratio, use
1088 * gdk_pixbuf_new_from_file_at_scale().
1090 * Return value: A newly-created pixbuf with a reference count of 1, or
1091 * %NULL if any of several error conditions occurred: the file could not
1092 * be opened, there was no loader for the file's format, there was not
1093 * enough memory to allocate the image buffer, or the image file contained
1099 gdk_pixbuf_new_from_file_at_size (const char *filename,
1104 return gdk_pixbuf_new_from_file_at_scale (filename,
1111 #undef gdk_pixbuf_new_from_file_at_size
1114 gdk_pixbuf_new_from_file_at_size (const char *filename,
1119 gchar *utf8_filename =
1120 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1123 if (utf8_filename == NULL)
1126 retval = gdk_pixbuf_new_from_file_at_size_utf8 (utf8_filename,
1130 g_free (utf8_filename);
1139 gboolean preserve_aspect_ratio;
1143 at_scale_size_prepared_cb (GdkPixbufLoader *loader,
1148 AtScaleData *info = data;
1150 g_return_if_fail (width > 0 && height > 0);
1152 if (info->preserve_aspect_ratio &&
1153 (info->width > 0 || info->height > 0)) {
1154 if (info->width < 0)
1156 width = width * (double)info->height/(double)height;
1157 height = info->height;
1159 else if (info->height < 0)
1161 height = height * (double)info->width/(double)width;
1162 width = info->width;
1164 else if ((double)height * (double)info->width >
1165 (double)width * (double)info->height) {
1166 width = 0.5 + (double)width * (double)info->height / (double)height;
1167 height = info->height;
1169 height = 0.5 + (double)height * (double)info->width / (double)width;
1170 width = info->width;
1173 if (info->width > 0)
1174 width = info->width;
1175 if (info->height > 0)
1176 height = info->height;
1179 width = MAX (width, 1);
1180 height = MAX (height, 1);
1182 gdk_pixbuf_loader_set_size (loader, width, height);
1186 * gdk_pixbuf_new_from_file_at_scale:
1187 * @filename: Name of file to load, in the GLib file name encoding
1188 * @width: The width the image should have or -1 to not constrain the width
1189 * @height: The height the image should have or -1 to not constrain the height
1190 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1191 * @error: Return location for an error
1193 * Creates a new pixbuf by loading an image from a file. The file format is
1194 * detected automatically. If %NULL is returned, then @error will be set.
1195 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
1196 * The image will be scaled to fit in the requested size, optionally preserving
1197 * the image's aspect ratio.
1199 * When preserving the aspect ratio, a @width of -1 will cause the image
1200 * to be scaled to the exact given height, and a @height of -1 will cause
1201 * the image to be scaled to the exact given width. When not preserving
1202 * aspect ratio, a @width or @height of -1 means to not scale the image
1203 * at all in that dimension. Negative values for @width and @height are
1204 * allowed since 2.8.
1206 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL
1207 * if any of several error conditions occurred: the file could not be opened,
1208 * there was no loader for the file's format, there was not enough memory to
1209 * allocate the image buffer, or the image file contained invalid data.
1214 gdk_pixbuf_new_from_file_at_scale (const char *filename,
1217 gboolean preserve_aspect_ratio,
1221 GdkPixbufLoader *loader;
1223 guchar buffer[LOAD_BUFFER_SIZE];
1227 GdkPixbufAnimation *animation;
1228 GdkPixbufAnimationIter *iter;
1231 g_return_val_if_fail (filename != NULL, NULL);
1232 g_return_val_if_fail (width > 0 || width == -1, NULL);
1233 g_return_val_if_fail (height > 0 || height == -1, NULL);
1235 f = g_fopen (filename, "rb");
1237 gint save_errno = errno;
1238 gchar *display_name = g_filename_display_name (filename);
1241 g_file_error_from_errno (save_errno),
1242 _("Failed to open file '%s': %s"),
1244 g_strerror (save_errno));
1245 g_free (display_name);
1249 loader = gdk_pixbuf_loader_new ();
1252 info.height = height;
1253 info.preserve_aspect_ratio = preserve_aspect_ratio;
1255 g_signal_connect (loader, "size-prepared",
1256 G_CALLBACK (at_scale_size_prepared_cb), &info);
1259 while (!has_frame && !feof (f) && !ferror (f)) {
1260 length = fread (buffer, 1, sizeof (buffer), f);
1262 if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
1263 gdk_pixbuf_loader_close (loader, NULL);
1265 g_object_unref (loader);
1269 animation = gdk_pixbuf_loader_get_animation (loader);
1271 iter = gdk_pixbuf_animation_get_iter (animation, NULL);
1272 if (!gdk_pixbuf_animation_iter_on_currently_loading_frame (iter)) {
1275 g_object_unref (iter);
1281 if (!gdk_pixbuf_loader_close (loader, error) && !has_frame) {
1282 g_object_unref (loader);
1286 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1289 gchar *display_name = g_filename_display_name (filename);
1290 g_object_unref (loader);
1293 GDK_PIXBUF_ERROR_FAILED,
1294 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1296 g_free (display_name);
1300 g_object_ref (pixbuf);
1302 g_object_unref (loader);
1309 #undef gdk_pixbuf_new_from_file_at_scale
1312 gdk_pixbuf_new_from_file_at_scale (const char *filename,
1315 gboolean preserve_aspect_ratio,
1318 gchar *utf8_filename =
1319 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1322 if (utf8_filename == NULL)
1325 retval = gdk_pixbuf_new_from_file_at_scale_utf8 (utf8_filename,
1327 preserve_aspect_ratio,
1330 g_free (utf8_filename);
1338 load_from_stream (GdkPixbufLoader *loader,
1339 GInputStream *stream,
1340 GCancellable *cancellable,
1345 guchar buffer[LOAD_BUFFER_SIZE];
1350 n_read = g_input_stream_read (stream,
1357 error = NULL; /* Ignore further errors */
1364 if (!gdk_pixbuf_loader_write (loader,
1374 if (!gdk_pixbuf_loader_close (loader, error)) {
1381 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1383 g_object_ref (pixbuf);
1391 * gdk_pixbuf_new_from_stream_at_scale:
1392 * @stream: a #GInputStream to load the pixbuf from
1393 * @width: The width the image should have or -1 to not constrain the width
1394 * @height: The height the image should have or -1 to not constrain the height
1395 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1396 * @cancellable: optional #GCancellable object, %NULL to ignore
1397 * @error: Return location for an error
1399 * Creates a new pixbuf by loading an image from an input stream.
1401 * The file format is detected automatically. If %NULL is returned, then
1402 * @error will be set. The @cancellable can be used to abort the operation
1403 * from another thread. If the operation was cancelled, the error
1404 * %GIO_ERROR_CANCELLED will be returned. Other possible errors are in
1405 * the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
1407 * The image will be scaled to fit in the requested size, optionally
1408 * preserving the image's aspect ratio. When preserving the aspect ratio,
1409 * a @width of -1 will cause the image to be scaled to the exact given
1410 * height, and a @height of -1 will cause the image to be scaled to the
1411 * exact given width. When not preserving aspect ratio, a @width or
1412 * @height of -1 means to not scale the image at all in that dimension.
1414 * The stream is not closed.
1416 * Return value: A newly-created pixbuf, or %NULL if any of several error
1417 * conditions occurred: the file could not be opened, the image format is
1418 * not supported, there was not enough memory to allocate the image buffer,
1419 * the stream contained invalid data, or the operation was cancelled.
1424 gdk_pixbuf_new_from_stream_at_scale (GInputStream *stream,
1427 gboolean preserve_aspect_ratio,
1428 GCancellable *cancellable,
1431 GdkPixbufLoader *loader;
1435 loader = gdk_pixbuf_loader_new ();
1438 info.height = height;
1439 info.preserve_aspect_ratio = preserve_aspect_ratio;
1441 g_signal_connect (loader, "size-prepared",
1442 G_CALLBACK (at_scale_size_prepared_cb), &info);
1444 pixbuf = load_from_stream (loader, stream, cancellable, error);
1445 g_object_unref (loader);
1451 * gdk_pixbuf_new_from_stream:
1452 * @stream: a #GInputStream to load the pixbuf from
1453 * @cancellable: optional #GCancellable object, %NULL to ignore
1454 * @error: Return location for an error
1456 * Creates a new pixbuf by loading an image from an input stream.
1458 * The file format is detected automatically. If %NULL is returned, then
1459 * @error will be set. The @cancellable can be used to abort the operation
1460 * from another thread. If the operation was cancelled, the error
1461 * %GIO_ERROR_CANCELLED will be returned. Other possible errors are in
1462 * the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
1464 * The stream is not closed.
1466 * Return value: A newly-created pixbuf, or %NULL if any of several error
1467 * conditions occurred: the file could not be opened, the image format is
1468 * not supported, there was not enough memory to allocate the image buffer,
1469 * the stream contained invalid data, or the operation was cancelled.
1474 gdk_pixbuf_new_from_stream (GInputStream *stream,
1475 GCancellable *cancellable,
1479 GdkPixbufLoader *loader;
1481 loader = gdk_pixbuf_loader_new ();
1482 pixbuf = load_from_stream (loader, stream, cancellable, error);
1483 g_object_unref (loader);
1489 info_cb (GdkPixbufLoader *loader,
1495 GdkPixbufFormat *format;
1500 g_return_if_fail (width > 0 && height > 0);
1502 info->format = gdk_pixbuf_loader_get_format (loader);
1503 info->width = width;
1504 info->height = height;
1506 gdk_pixbuf_loader_set_size (loader, 0, 0);
1510 * gdk_pixbuf_get_file_info:
1511 * @filename: The name of the file to identify.
1512 * @width: Return location for the width of the image, or %NULL
1513 * @height: Return location for the height of the image, or %NULL
1515 * Parses an image file far enough to determine its format and size.
1517 * Returns: A #GdkPixbufFormat describing the image format of the file
1518 * or %NULL if the image format wasn't recognized. The return value
1519 * is owned by GdkPixbuf and should not be freed.
1524 gdk_pixbuf_get_file_info (const gchar *filename,
1528 GdkPixbufLoader *loader;
1529 guchar buffer[SNIFF_BUFFER_SIZE];
1533 GdkPixbufFormat *format;
1538 g_return_val_if_fail (filename != NULL, NULL);
1540 f = g_fopen (filename, "rb");
1544 loader = gdk_pixbuf_loader_new ();
1550 g_signal_connect (loader, "size-prepared", G_CALLBACK (info_cb), &info);
1552 while (!feof (f) && !ferror (f)) {
1553 length = fread (buffer, 1, sizeof (buffer), f);
1555 if (!gdk_pixbuf_loader_write (loader, buffer, length, NULL))
1558 if (info.format != NULL)
1563 gdk_pixbuf_loader_close (loader, NULL);
1564 g_object_unref (loader);
1567 *width = info.width;
1569 *height = info.height;
1575 * gdk_pixbuf_new_from_xpm_data:
1576 * @data: Pointer to inline XPM data.
1578 * Creates a new pixbuf by parsing XPM data in memory. This data is commonly
1579 * the result of including an XPM file into a program's C source.
1581 * Return value: A newly-created pixbuf with a reference count of 1.
1584 gdk_pixbuf_new_from_xpm_data (const char **data)
1586 GdkPixbuf *(* load_xpm_data) (const char **data);
1588 GError *error = NULL;
1589 GdkPixbufModule *xpm_module;
1592 g_return_val_if_fail (data != NULL, NULL);
1594 xpm_module = _gdk_pixbuf_get_named_module ("xpm", &error);
1595 if (xpm_module == NULL) {
1596 g_warning ("Error loading XPM image loader: %s", error->message);
1597 g_error_free (error);
1601 if (!_gdk_pixbuf_load_module (xpm_module, &error)) {
1602 g_warning ("Error loading XPM image loader: %s", error->message);
1603 g_error_free (error);
1607 locked = _gdk_pixbuf_lock (xpm_module);
1609 if (xpm_module->load_xpm_data == NULL) {
1610 g_warning ("gdk-pixbuf XPM module lacks XPM data capability");
1613 load_xpm_data = xpm_module->load_xpm_data;
1614 pixbuf = (* load_xpm_data) (data);
1618 _gdk_pixbuf_unlock (xpm_module);
1623 collect_save_options (va_list opts,
1636 next = va_arg (opts, gchar*);
1640 val = va_arg (opts, gchar*);
1645 *keys = g_realloc (*keys, sizeof(gchar*) * (count + 1));
1646 *vals = g_realloc (*vals, sizeof(gchar*) * (count + 1));
1648 (*keys)[count-1] = g_strdup (key);
1649 (*vals)[count-1] = g_strdup (val);
1651 (*keys)[count] = NULL;
1652 (*vals)[count] = NULL;
1654 next = va_arg (opts, gchar*);
1659 save_to_file_callback (const gchar *buf,
1664 FILE *filehandle = data;
1667 n = fwrite (buf, 1, count, filehandle);
1669 gint save_errno = errno;
1672 g_file_error_from_errno (save_errno),
1673 _("Error writing to image file: %s"),
1674 g_strerror (save_errno));
1681 gdk_pixbuf_real_save (GdkPixbuf *pixbuf,
1689 GdkPixbufModule *image_module = NULL;
1692 image_module = _gdk_pixbuf_get_named_module (type, error);
1694 if (image_module == NULL)
1697 if (!_gdk_pixbuf_load_module (image_module, error))
1700 locked = _gdk_pixbuf_lock (image_module);
1702 if (image_module->save) {
1704 ret = (* image_module->save) (filehandle, pixbuf,
1707 } else if (image_module->save_to_callback) {
1708 /* save with simple callback */
1709 ret = (* image_module->save_to_callback) (save_to_file_callback,
1717 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
1718 _("This build of gdk-pixbuf does not support saving the image format: %s"),
1724 _gdk_pixbuf_unlock (image_module);
1728 #define TMP_FILE_BUF_SIZE 4096
1731 save_to_callback_with_tmp_file (GdkPixbufModule *image_module,
1733 GdkPixbufSaveFunc save_func,
1741 gboolean retval = FALSE;
1744 gchar *filename = NULL;
1747 buf = g_try_malloc (TMP_FILE_BUF_SIZE);
1749 g_set_error_literal (error,
1751 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1752 _("Insufficient memory to save image to callback"));
1756 fd = g_file_open_tmp ("gdkpixbuf-save-tmp.XXXXXX", &filename, error);
1759 f = fdopen (fd, "wb+");
1761 gint save_errno = errno;
1762 g_set_error_literal (error,
1764 g_file_error_from_errno (save_errno),
1765 _("Failed to open temporary file"));
1769 locked = _gdk_pixbuf_lock (image_module);
1770 retval = (image_module->save) (f, pixbuf, keys, values, error);
1772 _gdk_pixbuf_unlock (image_module);
1778 n = fread (buf, 1, TMP_FILE_BUF_SIZE, f);
1780 if (!save_func (buf, n, error, user_data))
1783 if (n != TMP_FILE_BUF_SIZE)
1787 gint save_errno = errno;
1788 g_set_error_literal (error,
1790 g_file_error_from_errno (save_errno),
1791 _("Failed to read from temporary file"));
1797 /* cleanup and return retval */
1801 g_unlink (filename);
1810 gdk_pixbuf_real_save_to_callback (GdkPixbuf *pixbuf,
1811 GdkPixbufSaveFunc save_func,
1819 GdkPixbufModule *image_module = NULL;
1822 image_module = _gdk_pixbuf_get_named_module (type, error);
1824 if (image_module == NULL)
1827 if (!_gdk_pixbuf_load_module (image_module, error))
1830 locked = _gdk_pixbuf_lock (image_module);
1832 if (image_module->save_to_callback) {
1834 ret = (* image_module->save_to_callback) (save_func, user_data,
1835 pixbuf, keys, values,
1837 } else if (image_module->save) {
1838 /* use a temporary file */
1839 ret = save_to_callback_with_tmp_file (image_module, pixbuf,
1840 save_func, user_data,
1847 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
1848 _("This build of gdk-pixbuf does not support saving the image format: %s"),
1854 _gdk_pixbuf_unlock (image_module);
1861 * @pixbuf: a #GdkPixbuf.
1862 * @filename: name of file to save.
1863 * @type: name of file format.
1864 * @error: return location for error, or %NULL
1865 * @Varargs: list of key-value save options
1867 * Saves pixbuf to a file in format @type. By default, "jpeg", "png", "ico"
1868 * and "bmp" are possible file formats to save in, but more formats may be
1869 * installed. The list of all writable formats can be determined in the
1873 * void add_if_writable (GdkPixbufFormat *data, GSList **list)
1875 * if (gdk_pixbuf_format_is_writable (data))
1876 * *list = g_slist_prepend (*list, data);
1879 * GSList *formats = gdk_pixbuf_get_formats ();
1880 * GSList *writable_formats = NULL;
1881 * g_slist_foreach (formats, add_if_writable, &writable_formats);
1882 * g_slist_free (formats);
1885 * If @error is set, %FALSE will be returned. Possible errors include
1886 * those in the #GDK_PIXBUF_ERROR domain and those in the #G_FILE_ERROR domain.
1888 * The variable argument list should be %NULL-terminated; if not empty,
1889 * it should contain pairs of strings that modify the save
1890 * parameters. For example:
1891 * <informalexample><programlisting>
1892 * gdk_pixbuf_save (pixbuf, handle, "jpeg", &error,
1893 * "quality", "100", NULL);
1894 * </programlisting></informalexample>
1896 * Currently only few parameters exist. JPEG images can be saved with a
1897 * "quality" parameter; its value should be in the range [0,100].
1899 * Text chunks can be attached to PNG images by specifying parameters of
1900 * the form "tEXt::key", where key is an ASCII string of length 1-79.
1901 * The values are UTF-8 encoded strings. The PNG compression level can
1902 * be specified using the "compression" parameter; it's value is in an
1903 * integer in the range of [0,9].
1905 * TIFF images recognize a "compression" option which acceps an integer value.
1906 * Among the codecs are 1 None, 2 Huffman, 5 LZW, 7 JPEG and 8 Deflate, see
1907 * the libtiff documentation and tiff.h for all supported codec values.
1909 * ICO images can be saved in depth 16, 24, or 32, by using the "depth"
1910 * parameter. When the ICO saver is given "x_hot" and "y_hot" parameters,
1911 * it produces a CUR instead of an ICO.
1913 * Return value: whether an error was set
1917 gdk_pixbuf_save (GdkPixbuf *pixbuf,
1918 const char *filename,
1923 gchar **keys = NULL;
1924 gchar **values = NULL;
1928 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1930 va_start (args, error);
1932 collect_save_options (args, &keys, &values);
1936 result = gdk_pixbuf_savev (pixbuf, filename, type,
1941 g_strfreev (values);
1948 #undef gdk_pixbuf_save
1951 gdk_pixbuf_save (GdkPixbuf *pixbuf,
1952 const char *filename,
1957 char *utf8_filename;
1958 gchar **keys = NULL;
1959 gchar **values = NULL;
1963 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1965 utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1967 if (utf8_filename == NULL)
1970 va_start (args, error);
1972 collect_save_options (args, &keys, &values);
1976 result = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
1980 g_free (utf8_filename);
1983 g_strfreev (values);
1992 * @pixbuf: a #GdkPixbuf.
1993 * @filename: name of file to save.
1994 * @type: name of file format.
1995 * @option_keys: name of options to set, %NULL-terminated
1996 * @option_values: values for named options
1997 * @error: return location for error, or %NULL
1999 * Saves pixbuf to a file in @type, which is currently "jpeg", "png", "tiff", "ico" or "bmp".
2000 * If @error is set, %FALSE will be returned.
2001 * See gdk_pixbuf_save () for more details.
2003 * Return value: whether an error was set
2007 gdk_pixbuf_savev (GdkPixbuf *pixbuf,
2008 const char *filename,
2011 char **option_values,
2017 g_return_val_if_fail (filename != NULL, FALSE);
2018 g_return_val_if_fail (type != NULL, FALSE);
2019 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2021 f = g_fopen (filename, "wb");
2024 gint save_errno = errno;
2025 gchar *display_name = g_filename_display_name (filename);
2028 g_file_error_from_errno (save_errno),
2029 _("Failed to open '%s' for writing: %s"),
2031 g_strerror (save_errno));
2032 g_free (display_name);
2037 result = gdk_pixbuf_real_save (pixbuf, f, type,
2038 option_keys, option_values,
2043 g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
2048 if (fclose (f) < 0) {
2049 gint save_errno = errno;
2050 gchar *display_name = g_filename_display_name (filename);
2053 g_file_error_from_errno (save_errno),
2054 _("Failed to close '%s' while writing image, all data may not have been saved: %s"),
2056 g_strerror (save_errno));
2057 g_free (display_name);
2066 #undef gdk_pixbuf_savev
2069 gdk_pixbuf_savev (GdkPixbuf *pixbuf,
2070 const char *filename,
2073 char **option_values,
2076 char *utf8_filename;
2079 g_return_val_if_fail (filename != NULL, FALSE);
2081 utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2083 if (utf8_filename == NULL)
2086 retval = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
2087 option_keys, option_values, error);
2089 g_free (utf8_filename);
2097 * gdk_pixbuf_save_to_callback:
2098 * @pixbuf: a #GdkPixbuf.
2099 * @save_func: a function that is called to save each block of data that
2100 * the save routine generates.
2101 * @user_data: user data to pass to the save function.
2102 * @type: name of file format.
2103 * @error: return location for error, or %NULL
2104 * @Varargs: list of key-value save options
2106 * Saves pixbuf in format @type by feeding the produced data to a
2107 * callback. Can be used when you want to store the image to something
2108 * other than a file, such as an in-memory buffer or a socket.
2109 * If @error is set, %FALSE will be returned. Possible errors
2110 * include those in the #GDK_PIXBUF_ERROR domain and whatever the save
2111 * function generates.
2113 * See gdk_pixbuf_save() for more details.
2115 * Return value: whether an error was set
2120 gdk_pixbuf_save_to_callback (GdkPixbuf *pixbuf,
2121 GdkPixbufSaveFunc save_func,
2127 gchar **keys = NULL;
2128 gchar **values = NULL;
2132 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2134 va_start (args, error);
2136 collect_save_options (args, &keys, &values);
2140 result = gdk_pixbuf_save_to_callbackv (pixbuf, save_func, user_data,
2145 g_strfreev (values);
2151 * gdk_pixbuf_save_to_callbackv:
2152 * @pixbuf: a #GdkPixbuf.
2153 * @save_func: a function that is called to save each block of data that
2154 * the save routine generates.
2155 * @user_data: user data to pass to the save function.
2156 * @type: name of file format.
2157 * @option_keys: name of options to set, %NULL-terminated
2158 * @option_values: values for named options
2159 * @error: return location for error, or %NULL
2161 * Saves pixbuf to a callback in format @type, which is currently "jpeg",
2162 * "png", "tiff", "ico" or "bmp". If @error is set, %FALSE will be returned. See
2163 * gdk_pixbuf_save_to_callback () for more details.
2165 * Return value: whether an error was set
2170 gdk_pixbuf_save_to_callbackv (GdkPixbuf *pixbuf,
2171 GdkPixbufSaveFunc save_func,
2175 char **option_values,
2181 g_return_val_if_fail (save_func != NULL, FALSE);
2182 g_return_val_if_fail (type != NULL, FALSE);
2183 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2185 result = gdk_pixbuf_real_save_to_callback (pixbuf,
2186 save_func, user_data, type,
2187 option_keys, option_values,
2191 g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
2199 * gdk_pixbuf_save_to_buffer:
2200 * @pixbuf: a #GdkPixbuf.
2201 * @buffer: location to receive a pointer to the new buffer.
2202 * @buffer_size: location to receive the size of the new buffer.
2203 * @type: name of file format.
2204 * @error: return location for error, or %NULL
2205 * @Varargs: list of key-value save options
2207 * Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2208 * "png", "tiff", "ico" or "bmp". This is a convenience function that uses
2209 * gdk_pixbuf_save_to_callback() to do the real work. Note that the buffer
2210 * is not nul-terminated and may contain embedded nuls.
2211 * If @error is set, %FALSE will be returned and @buffer will be set to
2212 * %NULL. Possible errors include those in the #GDK_PIXBUF_ERROR
2215 * See gdk_pixbuf_save() for more details.
2217 * Return value: whether an error was set
2222 gdk_pixbuf_save_to_buffer (GdkPixbuf *pixbuf,
2229 gchar **keys = NULL;
2230 gchar **values = NULL;
2234 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2236 va_start (args, error);
2238 collect_save_options (args, &keys, &values);
2242 result = gdk_pixbuf_save_to_bufferv (pixbuf, buffer, buffer_size,
2247 g_strfreev (values);
2252 struct SaveToBufferData {
2258 save_to_buffer_callback (const gchar *data,
2263 struct SaveToBufferData *sdata = user_data;
2267 if (sdata->len + count > sdata->max) {
2268 new_max = MAX (sdata->max*2, sdata->len + count);
2269 new_buffer = g_try_realloc (sdata->buffer, new_max);
2271 g_set_error_literal (error,
2273 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2274 _("Insufficient memory to save image into a buffer"));
2277 sdata->buffer = new_buffer;
2278 sdata->max = new_max;
2280 memcpy (sdata->buffer + sdata->len, data, count);
2281 sdata->len += count;
2286 * gdk_pixbuf_save_to_bufferv:
2287 * @pixbuf: a #GdkPixbuf.
2288 * @buffer: location to receive a pointer to the new buffer.
2289 * @buffer_size: location to receive the size of the new buffer.
2290 * @type: name of file format.
2291 * @option_keys: name of options to set, %NULL-terminated
2292 * @option_values: values for named options
2293 * @error: return location for error, or %NULL
2295 * Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2296 * "tiff", "png", "ico" or "bmp". See gdk_pixbuf_save_to_buffer()
2299 * Return value: whether an error was set
2304 gdk_pixbuf_save_to_bufferv (GdkPixbuf *pixbuf,
2309 char **option_values,
2312 static const gint initial_max = 1024;
2313 struct SaveToBufferData sdata;
2318 sdata.buffer = g_try_malloc (initial_max);
2319 sdata.max = initial_max;
2321 if (!sdata.buffer) {
2322 g_set_error_literal (error,
2324 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2325 _("Insufficient memory to save image into a buffer"));
2329 if (!gdk_pixbuf_save_to_callbackv (pixbuf,
2330 save_to_buffer_callback, &sdata,
2331 type, option_keys, option_values,
2333 g_free (sdata.buffer);
2337 *buffer = sdata.buffer;
2338 *buffer_size = sdata.len;
2343 GOutputStream *stream;
2344 GCancellable *cancellable;
2348 save_to_stream (const gchar *buffer,
2353 SaveToStreamData *sdata = (SaveToStreamData *)data;
2356 GError *my_error = NULL;
2360 while (remaining > 0) {
2362 remaining -= written;
2363 written = g_output_stream_write (sdata->stream,
2369 g_set_error_literal (error,
2371 _("Error writing to image stream"));
2374 g_propagate_error (error, my_error);
2384 * gdk_pixbuf_save_to_stream:
2385 * @pixbuf: a #GdkPixbuf
2386 * @stream: a #GOutputStream to save the pixbuf to
2387 * @type: name of file format
2388 * @cancellable: optional #GCancellable object, %NULL to ignore
2389 * @error: return location for error, or %NULL
2390 * @Varargs: list of key-value save options
2392 * Saves @pixbuf to an output stream.
2394 * Supported file formats are currently "jpeg", "tiff", "png", "ico" or
2395 * "bmp". See gdk_pixbuf_save_to_buffer() for more details.
2397 * The @cancellable can be used to abort the operation from another
2398 * thread. If the operation was cancelled, the error %GIO_ERROR_CANCELLED
2399 * will be returned. Other possible errors are in the #GDK_PIXBUF_ERROR
2400 * and %G_IO_ERROR domains.
2402 * The stream is not closed.
2404 * Returns: %TRUE if the pixbuf was saved successfully, %FALSE if an
2410 gdk_pixbuf_save_to_stream (GdkPixbuf *pixbuf,
2411 GOutputStream *stream,
2413 GCancellable *cancellable,
2418 gchar **keys = NULL;
2419 gchar **values = NULL;
2421 SaveToStreamData data;
2423 va_start (args, error);
2424 collect_save_options (args, &keys, &values);
2427 data.stream = stream;
2428 data.cancellable = cancellable;
2430 res = gdk_pixbuf_save_to_callbackv (pixbuf, save_to_stream,
2436 g_strfreev (values);
2442 * gdk_pixbuf_format_get_name:
2443 * @format: a #GdkPixbufFormat
2445 * Returns the name of the format.
2447 * Return value: the name of the format.
2452 gdk_pixbuf_format_get_name (GdkPixbufFormat *format)
2454 g_return_val_if_fail (format != NULL, NULL);
2456 return g_strdup (format->name);
2460 * gdk_pixbuf_format_get_description:
2461 * @format: a #GdkPixbufFormat
2463 * Returns a description of the format.
2465 * Return value: a description of the format.
2470 gdk_pixbuf_format_get_description (GdkPixbufFormat *format)
2473 const gchar *description;
2474 g_return_val_if_fail (format != NULL, NULL);
2476 if (format->domain != NULL)
2477 domain = format->domain;
2479 domain = GETTEXT_PACKAGE;
2480 description = g_dgettext (domain, format->description);
2482 return g_strdup (description);
2486 * gdk_pixbuf_format_get_mime_types:
2487 * @format: a #GdkPixbufFormat
2489 * Returns the mime types supported by the format.
2491 * Return value: a %NULL-terminated array of mime types which must be freed with
2492 * g_strfreev() when it is no longer needed.
2497 gdk_pixbuf_format_get_mime_types (GdkPixbufFormat *format)
2499 g_return_val_if_fail (format != NULL, NULL);
2501 return g_strdupv (format->mime_types);
2505 * gdk_pixbuf_format_get_extensions:
2506 * @format: a #GdkPixbufFormat
2508 * Returns the filename extensions typically used for files in the
2511 * Return value: a %NULL-terminated array of filename extensions which must be
2512 * freed with g_strfreev() when it is no longer needed.
2517 gdk_pixbuf_format_get_extensions (GdkPixbufFormat *format)
2519 g_return_val_if_fail (format != NULL, NULL);
2521 return g_strdupv (format->extensions);
2525 * gdk_pixbuf_format_is_writable:
2526 * @format: a #GdkPixbufFormat
2528 * Returns whether pixbufs can be saved in the given format.
2530 * Return value: whether pixbufs can be saved in the given format.
2535 gdk_pixbuf_format_is_writable (GdkPixbufFormat *format)
2537 g_return_val_if_fail (format != NULL, FALSE);
2539 return (format->flags & GDK_PIXBUF_FORMAT_WRITABLE) != 0;
2543 * gdk_pixbuf_format_is_scalable:
2544 * @format: a #GdkPixbufFormat
2546 * Returns whether this image format is scalable. If a file is in a
2547 * scalable format, it is preferable to load it at the desired size,
2548 * rather than loading it at the default size and scaling the
2549 * resulting pixbuf to the desired size.
2551 * Return value: whether this image format is scalable.
2556 gdk_pixbuf_format_is_scalable (GdkPixbufFormat *format)
2558 g_return_val_if_fail (format != NULL, FALSE);
2560 return (format->flags & GDK_PIXBUF_FORMAT_SCALABLE) != 0;
2564 * gdk_pixbuf_format_is_disabled:
2565 * @format: a #GdkPixbufFormat
2567 * Returns whether this image format is disabled. See
2568 * gdk_pixbuf_format_set_disabled().
2570 * Return value: whether this image format is disabled.
2575 gdk_pixbuf_format_is_disabled (GdkPixbufFormat *format)
2577 g_return_val_if_fail (format != NULL, FALSE);
2579 return format->disabled;
2583 * gdk_pixbuf_format_set_disabled:
2584 * @format: a #GdkPixbufFormat
2585 * @disabled: %TRUE to disable the format @format
2587 * Disables or enables an image format. If a format is disabled,
2588 * gdk-pixbuf won't use the image loader for this format to load
2589 * images. Applications can use this to avoid using image loaders
2590 * with an inappropriate license, see gdk_pixbuf_format_get_license().
2595 gdk_pixbuf_format_set_disabled (GdkPixbufFormat *format,
2598 g_return_if_fail (format != NULL);
2600 format->disabled = disabled != FALSE;
2604 * gdk_pixbuf_format_get_license:
2605 * @format: a #GdkPixbufFormat
2607 * Returns information about the license of the image loader for the format. The
2608 * returned string should be a shorthand for a wellknown license, e.g. "LGPL",
2609 * "GPL", "QPL", "GPL/QPL", or "other" to indicate some other license. This
2610 * string should be freed with g_free() when it's no longer needed.
2612 * Returns: a string describing the license of @format.
2617 gdk_pixbuf_format_get_license (GdkPixbufFormat *format)
2619 g_return_val_if_fail (format != NULL, NULL);
2621 return g_strdup (format->license);
2625 _gdk_pixbuf_get_format (GdkPixbufModule *module)
2627 g_return_val_if_fail (module != NULL, NULL);
2629 return module->info;
2633 * gdk_pixbuf_get_formats:
2635 * Obtains the available information about the image formats supported
2638 * Returns: A list of #GdkPixbufFormat<!-- -->s describing the supported
2639 * image formats. The list should be freed when it is no longer needed,
2640 * but the structures themselves are owned by #GdkPixbuf and should not be
2646 gdk_pixbuf_get_formats (void)
2648 GSList *result = NULL;
2651 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
2652 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
2653 GdkPixbufFormat *info = _gdk_pixbuf_get_format (module);
2654 result = g_slist_prepend (result, info);
2661 #define __GDK_PIXBUF_IO_C__
2662 #include "gdk-pixbuf-aliasdef.c"