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);
399 #ifdef INCLUDE_gdiplus
400 /* We don't bother having the GDI+ loaders individually selectable
401 * for building in or not.
403 load_one_builtin_module (ico);
404 load_one_builtin_module (wmf);
405 load_one_builtin_module (emf);
406 load_one_builtin_module (bmp);
407 load_one_builtin_module (gif);
408 load_one_builtin_module (jpeg);
409 load_one_builtin_module (tiff);
411 #ifdef INCLUDE_gdip_png
412 /* Except the gdip-png loader which normally isn't built at all even */
413 load_one_builtin_module (png);
416 #undef load_one_builtin_module
419 channel = g_io_channel_new_file (filename, "r", &error);
421 /* Don't bother warning if we have some built-in loaders */
422 if (file_formats == NULL)
423 g_warning ("Cannot open pixbuf loader module file '%s': %s",
424 filename, error->message);
425 g_string_free (tmp_buf, TRUE);
430 while (!have_error && g_io_channel_read_line (channel, &line_buf, NULL, &term, NULL) == G_IO_STATUS_NORMAL) {
437 if (!skip_space (&p)) {
438 /* Blank line marking the end of a module
440 if (module && *p != '#') {
442 correct_prefix (&module->module_path);
444 file_formats = g_slist_prepend (file_formats, module);
455 /* Read a module location
457 module = g_new0 (GdkPixbufModule, 1);
460 if (!scan_string (&p, tmp_buf)) {
461 g_warning ("Error parsing loader info in '%s'\n %s",
465 module->module_path = g_strdup (tmp_buf->str);
467 else if (!module->module_name) {
468 module->info = g_new0 (GdkPixbufFormat, 1);
469 if (!scan_string (&p, tmp_buf)) {
470 g_warning ("Error parsing loader info in '%s'\n %s",
474 module->info->name = g_strdup (tmp_buf->str);
475 module->module_name = module->info->name;
477 if (!scan_int (&p, &flags)) {
478 g_warning ("Error parsing loader info in '%s'\n %s",
482 module->info->flags = flags;
484 if (!scan_string (&p, tmp_buf)) {
485 g_warning ("Error parsing loader info in '%s'\n %s",
489 if (tmp_buf->str[0] != 0)
490 module->info->domain = g_strdup (tmp_buf->str);
492 if (!scan_string (&p, tmp_buf)) {
493 g_warning ("Error parsing loader info in '%s'\n %s",
497 module->info->description = g_strdup (tmp_buf->str);
499 if (scan_string (&p, tmp_buf)) {
500 module->info->license = g_strdup (tmp_buf->str);
503 else if (!module->info->mime_types) {
505 module->info->mime_types = g_new0 (gchar*, 1);
506 while (scan_string (&p, tmp_buf)) {
507 if (tmp_buf->str[0] != 0) {
508 module->info->mime_types =
509 g_realloc (module->info->mime_types, (n + 1) * sizeof (gchar*));
510 module->info->mime_types[n - 1] = g_strdup (tmp_buf->str);
511 module->info->mime_types[n] = NULL;
516 else if (!module->info->extensions) {
518 module->info->extensions = g_new0 (gchar*, 1);
519 while (scan_string (&p, tmp_buf)) {
520 if (tmp_buf->str[0] != 0) {
521 module->info->extensions =
522 g_realloc (module->info->extensions, (n + 1) * sizeof (gchar*));
523 module->info->extensions[n - 1] = g_strdup (tmp_buf->str);
524 module->info->extensions[n] = NULL;
531 module->info->signature = (GdkPixbufModulePattern *)
532 g_realloc (module->info->signature, (n_patterns + 1) * sizeof (GdkPixbufModulePattern));
533 pattern = module->info->signature + n_patterns;
534 pattern->prefix = NULL;
535 pattern->mask = NULL;
536 pattern->relevance = 0;
538 if (!scan_string (&p, tmp_buf))
540 pattern->prefix = g_strdup (tmp_buf->str);
542 if (!scan_string (&p, tmp_buf))
545 pattern->mask = g_strdup (tmp_buf->str);
547 pattern->mask = NULL;
549 if (!scan_int (&p, &pattern->relevance))
555 g_free (pattern->prefix);
556 g_free (pattern->mask);
558 g_warning ("Error parsing loader info in '%s'\n %s",
565 g_string_free (tmp_buf, TRUE);
566 g_io_channel_unref (channel);
572 #define module(type) \
573 extern void _gdk_pixbuf__##type##_fill_info (GdkPixbufFormat *info); \
574 extern void _gdk_pixbuf__##type##_fill_vtable (GdkPixbufModule *module)
603 /* actually load the image handler - gdk_pixbuf_get_module only get a */
604 /* reference to the module to load, it doesn't actually load it */
605 /* perhaps these actions should be combined in one function */
607 gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
610 GdkPixbufModuleFillInfoFunc fill_info = NULL;
611 GdkPixbufModuleFillVtableFunc fill_vtable = NULL;
613 if (image_module->module != NULL)
616 #define try_module(format,id) \
617 if (fill_info == NULL && \
618 strcmp (image_module->module_name, #format) == 0) { \
619 fill_info = _gdk_pixbuf__##id##_fill_info; \
620 fill_vtable = _gdk_pixbuf__##id##_fill_vtable; \
623 try_module (png,png);
626 try_module (bmp,bmp);
629 try_module (wbmp,wbmp);
632 try_module (gif,gif);
635 try_module (ico,ico);
638 try_module (ani,ani);
641 try_module (jpeg,jpeg);
644 try_module (pnm,pnm);
647 try_module (ras,ras);
650 try_module (tiff,tiff);
653 try_module (xpm,xpm);
656 try_module (xbm,xbm);
659 try_module (tga,tga);
662 try_module (pcx,pcx);
665 try_module (icns,icns);
667 #ifdef INCLUDE_jasper
668 try_module (jasper,jasper);
670 #ifdef INCLUDE_gdiplus
671 try_module (ico,gdip_ico);
672 try_module (wmf,gdip_wmf);
673 try_module (emf,gdip_emf);
674 try_module (bmp,gdip_bmp);
675 try_module (gif,gdip_gif);
676 try_module (jpeg,gdip_jpeg);
677 try_module (tiff,gdip_tiff);
679 #ifdef INCLUDE_gdip_png
680 try_module (png,gdip_png);
686 image_module->module = (void *) 1;
687 (* fill_vtable) (image_module);
688 if (image_module->info == NULL) {
689 image_module->info = g_new0 (GdkPixbufFormat, 1);
690 (* fill_info) (image_module->info);
701 path = image_module->module_path;
702 module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
707 GDK_PIXBUF_ERROR_FAILED,
708 _("Unable to load image-loading module: %s: %s"),
709 path, g_module_error ());
713 image_module->module = module;
715 if (g_module_symbol (module, "fill_vtable", &sym)) {
716 fill_vtable = (GdkPixbufModuleFillVtableFunc) sym;
717 (* fill_vtable) (image_module);
722 GDK_PIXBUF_ERROR_FAILED,
723 _("Image-loading module %s does not export the proper interface; perhaps it's from a different GTK version?"),
731 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
732 _("Image type '%s' is not supported"),
733 image_module->module_name);
735 #endif /* !USE_GMODULE */
740 _gdk_pixbuf_load_module (GdkPixbufModule *image_module,
744 gboolean locked = FALSE;
746 /* be extra careful, maybe the module initializes
749 if (g_threads_got_initialized) {
754 ret = gdk_pixbuf_load_module_unlocked (image_module, error);
757 G_UNLOCK (init_lock);
765 _gdk_pixbuf_get_named_module (const char *name,
770 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
771 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
773 if (module->info->disabled)
776 if (!strcmp (name, module->module_name))
782 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
783 _("Image type '%s' is not supported"),
790 _gdk_pixbuf_get_module (guchar *buffer, guint size,
791 const gchar *filename,
796 GdkPixbufModule *selected = NULL;
797 gchar *display_name = NULL;
798 #ifdef GDK_PIXBUF_USE_GIO_MIME
805 mime_type = g_content_type_guess (NULL, buffer, size, &uncertain);
807 mime_type = g_content_type_guess (filename, buffer, size, NULL);
809 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
810 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
811 GdkPixbufFormat *info = module->info;
816 mimes = info->mime_types;
817 for (j = 0; mimes[j] != NULL; j++) {
818 type = g_content_type_from_mime_type (mimes[j]);
819 if (g_ascii_strcasecmp (type, mime_type) == 0) {
829 gint score, best = 0;
831 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
832 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
834 if (module->info->disabled)
837 score = format_check (module, buffer, size);
847 if (selected != NULL)
852 display_name = g_filename_display_name (filename);
855 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
856 _("Couldn't recognize the image file format for file '%s'"),
858 g_free (display_name);
861 g_set_error_literal (error,
863 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
864 _("Unrecognized image file format"));
872 prepared_notify (GdkPixbuf *pixbuf,
873 GdkPixbufAnimation *anim,
877 g_object_ref (pixbuf);
878 *((GdkPixbuf **)user_data) = pixbuf;
882 _gdk_pixbuf_generic_image_load (GdkPixbufModule *module,
886 guchar buffer[LOAD_BUFFER_SIZE];
888 GdkPixbuf *pixbuf = NULL;
889 GdkPixbufAnimation *animation = NULL;
893 locked = _gdk_pixbuf_lock (module);
895 if (module->load != NULL) {
896 pixbuf = (* module->load) (f, error);
897 } else if (module->begin_load != NULL) {
899 context = module->begin_load (NULL, prepared_notify, NULL, &pixbuf, error);
904 while (!feof (f) && !ferror (f)) {
905 length = fread (buffer, 1, sizeof (buffer), f);
907 if (!module->load_increment (context, buffer, length, error)) {
908 module->stop_load (context, NULL);
909 if (pixbuf != NULL) {
910 g_object_unref (pixbuf);
917 if (!module->stop_load (context, error)) {
918 if (pixbuf != NULL) {
919 g_object_unref (pixbuf);
923 } else if (module->load_animation != NULL) {
924 animation = (* module->load_animation) (f, error);
925 if (animation != NULL) {
926 pixbuf = gdk_pixbuf_animation_get_static_image (animation);
928 g_object_ref (pixbuf);
929 g_object_unref (animation);
935 _gdk_pixbuf_unlock (module);
940 * gdk_pixbuf_new_from_file:
941 * @filename: Name of file to load, in the GLib file name encoding
942 * @error: Return location for an error
944 * Creates a new pixbuf by loading an image from a file. The file format is
945 * detected automatically. If %NULL is returned, then @error will be set.
946 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
948 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
949 * any of several error conditions occurred: the file could not be opened,
950 * there was no loader for the file's format, there was not enough memory to
951 * allocate the image buffer, or the image file contained invalid data.
954 gdk_pixbuf_new_from_file (const char *filename,
960 guchar buffer[SNIFF_BUFFER_SIZE];
961 GdkPixbufModule *image_module;
964 g_return_val_if_fail (filename != NULL, NULL);
965 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
967 display_name = g_filename_display_name (filename);
969 f = g_fopen (filename, "rb");
971 gint save_errno = errno;
974 g_file_error_from_errno (save_errno),
975 _("Failed to open file '%s': %s"),
977 g_strerror (save_errno));
978 g_free (display_name);
982 size = fread (&buffer, 1, sizeof (buffer), f);
986 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
987 _("Image file '%s' contains no data"),
989 g_free (display_name);
994 image_module = _gdk_pixbuf_get_module (buffer, size, filename, error);
995 if (image_module == NULL) {
996 g_free (display_name);
1001 if (!_gdk_pixbuf_load_module (image_module, error)) {
1002 g_free (display_name);
1007 fseek (f, 0, SEEK_SET);
1008 pixbuf = _gdk_pixbuf_generic_image_load (image_module, f, error);
1011 if (pixbuf == NULL && error != NULL && *error == NULL) {
1013 /* I don't trust these crufty longjmp()'ing image libs
1014 * to maintain proper error invariants, and I don't
1015 * want user code to segfault as a result. We need to maintain
1016 * the invariant that error gets set if NULL is returned.
1019 g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.", image_module->module_name);
1022 GDK_PIXBUF_ERROR_FAILED,
1023 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1025 } else if (error != NULL && *error != NULL) {
1027 /* Add the filename to the error message */
1032 e->message = g_strdup_printf (_("Failed to load image '%s': %s"),
1038 g_free (display_name);
1044 #undef gdk_pixbuf_new_from_file
1046 gdk_pixbuf_new_from_file (const char *filename,
1049 gchar *utf8_filename =
1050 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1053 if (utf8_filename == NULL)
1056 retval = gdk_pixbuf_new_from_file_utf8 (utf8_filename, error);
1058 g_free (utf8_filename);
1066 * gdk_pixbuf_new_from_file_at_size:
1067 * @filename: Name of file to load, in the GLib file name encoding
1068 * @width: The width the image should have or -1 to not constrain the width
1069 * @height: The height the image should have or -1 to not constrain the height
1070 * @error: Return location for an error
1072 * Creates a new pixbuf by loading an image from a file.
1073 * The file format is detected automatically. If %NULL is returned, then
1074 * @error will be set. Possible errors are in the #GDK_PIXBUF_ERROR and
1075 * #G_FILE_ERROR domains.
1077 * The image will be scaled to fit in the requested size, preserving
1078 * the image's aspect ratio. Note that the returned pixbuf may be smaller
1079 * than @width x @height, if the aspect ratio requires it. To load
1080 * and image at the requested size, regardless of aspect ratio, use
1081 * gdk_pixbuf_new_from_file_at_scale().
1083 * Return value: A newly-created pixbuf with a reference count of 1, or
1084 * %NULL if any of several error conditions occurred: the file could not
1085 * be opened, there was no loader for the file's format, there was not
1086 * enough memory to allocate the image buffer, or the image file contained
1092 gdk_pixbuf_new_from_file_at_size (const char *filename,
1097 return gdk_pixbuf_new_from_file_at_scale (filename,
1104 #undef gdk_pixbuf_new_from_file_at_size
1107 gdk_pixbuf_new_from_file_at_size (const char *filename,
1112 gchar *utf8_filename =
1113 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1116 if (utf8_filename == NULL)
1119 retval = gdk_pixbuf_new_from_file_at_size_utf8 (utf8_filename,
1123 g_free (utf8_filename);
1132 gboolean preserve_aspect_ratio;
1136 at_scale_size_prepared_cb (GdkPixbufLoader *loader,
1141 AtScaleData *info = data;
1143 g_return_if_fail (width > 0 && height > 0);
1145 if (info->preserve_aspect_ratio &&
1146 (info->width > 0 || info->height > 0)) {
1147 if (info->width < 0)
1149 width = width * (double)info->height/(double)height;
1150 height = info->height;
1152 else if (info->height < 0)
1154 height = height * (double)info->width/(double)width;
1155 width = info->width;
1157 else if ((double)height * (double)info->width >
1158 (double)width * (double)info->height) {
1159 width = 0.5 + (double)width * (double)info->height / (double)height;
1160 height = info->height;
1162 height = 0.5 + (double)height * (double)info->width / (double)width;
1163 width = info->width;
1166 if (info->width > 0)
1167 width = info->width;
1168 if (info->height > 0)
1169 height = info->height;
1172 width = MAX (width, 1);
1173 height = MAX (height, 1);
1175 gdk_pixbuf_loader_set_size (loader, width, height);
1179 * gdk_pixbuf_new_from_file_at_scale:
1180 * @filename: Name of file to load, in the GLib file name encoding
1181 * @width: The width the image should have or -1 to not constrain the width
1182 * @height: The height the image should have or -1 to not constrain the height
1183 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1184 * @error: Return location for an error
1186 * Creates a new pixbuf by loading an image from a file. The file format is
1187 * detected automatically. If %NULL is returned, then @error will be set.
1188 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
1189 * The image will be scaled to fit in the requested size, optionally preserving
1190 * the image's aspect ratio.
1192 * When preserving the aspect ratio, a @width of -1 will cause the image
1193 * to be scaled to the exact given height, and a @height of -1 will cause
1194 * the image to be scaled to the exact given width. When not preserving
1195 * aspect ratio, a @width or @height of -1 means to not scale the image
1196 * at all in that dimension. Negative values for @width and @height are
1197 * allowed since 2.8.
1199 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL
1200 * if any of several error conditions occurred: the file could not be opened,
1201 * there was no loader for the file's format, there was not enough memory to
1202 * allocate the image buffer, or the image file contained invalid data.
1207 gdk_pixbuf_new_from_file_at_scale (const char *filename,
1210 gboolean preserve_aspect_ratio,
1214 GdkPixbufLoader *loader;
1216 guchar buffer[LOAD_BUFFER_SIZE];
1220 GdkPixbufAnimation *animation;
1221 GdkPixbufAnimationIter *iter;
1224 g_return_val_if_fail (filename != NULL, NULL);
1225 g_return_val_if_fail (width > 0 || width == -1, NULL);
1226 g_return_val_if_fail (height > 0 || height == -1, NULL);
1228 f = g_fopen (filename, "rb");
1230 gint save_errno = errno;
1231 gchar *display_name = g_filename_display_name (filename);
1234 g_file_error_from_errno (save_errno),
1235 _("Failed to open file '%s': %s"),
1237 g_strerror (save_errno));
1238 g_free (display_name);
1242 loader = gdk_pixbuf_loader_new ();
1245 info.height = height;
1246 info.preserve_aspect_ratio = preserve_aspect_ratio;
1248 g_signal_connect (loader, "size-prepared",
1249 G_CALLBACK (at_scale_size_prepared_cb), &info);
1252 while (!has_frame && !feof (f) && !ferror (f)) {
1253 length = fread (buffer, 1, sizeof (buffer), f);
1255 if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
1256 gdk_pixbuf_loader_close (loader, NULL);
1258 g_object_unref (loader);
1262 animation = gdk_pixbuf_loader_get_animation (loader);
1264 iter = gdk_pixbuf_animation_get_iter (animation, NULL);
1265 if (!gdk_pixbuf_animation_iter_on_currently_loading_frame (iter)) {
1268 g_object_unref (iter);
1274 if (!gdk_pixbuf_loader_close (loader, error) && !has_frame) {
1275 g_object_unref (loader);
1279 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1282 gchar *display_name = g_filename_display_name (filename);
1283 g_object_unref (loader);
1286 GDK_PIXBUF_ERROR_FAILED,
1287 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1289 g_free (display_name);
1293 g_object_ref (pixbuf);
1295 g_object_unref (loader);
1302 #undef gdk_pixbuf_new_from_file_at_scale
1305 gdk_pixbuf_new_from_file_at_scale (const char *filename,
1308 gboolean preserve_aspect_ratio,
1311 gchar *utf8_filename =
1312 g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1315 if (utf8_filename == NULL)
1318 retval = gdk_pixbuf_new_from_file_at_scale_utf8 (utf8_filename,
1320 preserve_aspect_ratio,
1323 g_free (utf8_filename);
1331 load_from_stream (GdkPixbufLoader *loader,
1332 GInputStream *stream,
1333 GCancellable *cancellable,
1338 guchar buffer[LOAD_BUFFER_SIZE];
1343 n_read = g_input_stream_read (stream,
1350 error = NULL; /* Ignore further errors */
1357 if (!gdk_pixbuf_loader_write (loader,
1367 if (!gdk_pixbuf_loader_close (loader, error)) {
1374 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1376 g_object_ref (pixbuf);
1384 * gdk_pixbuf_new_from_stream_at_scale:
1385 * @stream: a #GInputStream to load the pixbuf from
1386 * @width: The width the image should have or -1 to not constrain the width
1387 * @height: The height the image should have or -1 to not constrain the height
1388 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1389 * @cancellable: optional #GCancellable object, %NULL to ignore
1390 * @error: Return location for an error
1392 * Creates a new pixbuf by loading an image from an input stream.
1394 * The file format is detected automatically. If %NULL is returned, then
1395 * @error will be set. The @cancellable can be used to abort the operation
1396 * from another thread. If the operation was cancelled, the error
1397 * %GIO_ERROR_CANCELLED will be returned. Other possible errors are in
1398 * the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
1400 * The image will be scaled to fit in the requested size, optionally
1401 * preserving the image's aspect ratio. When preserving the aspect ratio,
1402 * a @width of -1 will cause the image to be scaled to the exact given
1403 * height, and a @height of -1 will cause the image to be scaled to the
1404 * exact given width. When not preserving aspect ratio, a @width or
1405 * @height of -1 means to not scale the image at all in that dimension.
1407 * The stream is not closed.
1409 * Return value: A newly-created pixbuf, or %NULL if any of several error
1410 * conditions occurred: the file could not be opened, the image format is
1411 * not supported, there was not enough memory to allocate the image buffer,
1412 * the stream contained invalid data, or the operation was cancelled.
1417 gdk_pixbuf_new_from_stream_at_scale (GInputStream *stream,
1420 gboolean preserve_aspect_ratio,
1421 GCancellable *cancellable,
1424 GdkPixbufLoader *loader;
1428 loader = gdk_pixbuf_loader_new ();
1431 info.height = height;
1432 info.preserve_aspect_ratio = preserve_aspect_ratio;
1434 g_signal_connect (loader, "size-prepared",
1435 G_CALLBACK (at_scale_size_prepared_cb), &info);
1437 pixbuf = load_from_stream (loader, stream, cancellable, error);
1438 g_object_unref (loader);
1444 * gdk_pixbuf_new_from_stream:
1445 * @stream: a #GInputStream to load the pixbuf from
1446 * @cancellable: optional #GCancellable object, %NULL to ignore
1447 * @error: Return location for an error
1449 * Creates a new pixbuf by loading an image from an input stream.
1451 * The file format is detected automatically. If %NULL is returned, then
1452 * @error will be set. The @cancellable can be used to abort the operation
1453 * from another thread. If the operation was cancelled, the error
1454 * %GIO_ERROR_CANCELLED will be returned. Other possible errors are in
1455 * the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains.
1457 * The stream is not closed.
1459 * Return value: A newly-created pixbuf, or %NULL if any of several error
1460 * conditions occurred: the file could not be opened, the image format is
1461 * not supported, there was not enough memory to allocate the image buffer,
1462 * the stream contained invalid data, or the operation was cancelled.
1467 gdk_pixbuf_new_from_stream (GInputStream *stream,
1468 GCancellable *cancellable,
1472 GdkPixbufLoader *loader;
1474 loader = gdk_pixbuf_loader_new ();
1475 pixbuf = load_from_stream (loader, stream, cancellable, error);
1476 g_object_unref (loader);
1482 info_cb (GdkPixbufLoader *loader,
1488 GdkPixbufFormat *format;
1493 g_return_if_fail (width > 0 && height > 0);
1495 info->format = gdk_pixbuf_loader_get_format (loader);
1496 info->width = width;
1497 info->height = height;
1499 gdk_pixbuf_loader_set_size (loader, 0, 0);
1503 * gdk_pixbuf_get_file_info:
1504 * @filename: The name of the file to identify.
1505 * @width: Return location for the width of the image, or %NULL
1506 * @height: Return location for the height of the image, or %NULL
1508 * Parses an image file far enough to determine its format and size.
1510 * Returns: A #GdkPixbufFormat describing the image format of the file
1511 * or %NULL if the image format wasn't recognized. The return value
1512 * is owned by GdkPixbuf and should not be freed.
1517 gdk_pixbuf_get_file_info (const gchar *filename,
1521 GdkPixbufLoader *loader;
1522 guchar buffer[SNIFF_BUFFER_SIZE];
1526 GdkPixbufFormat *format;
1531 g_return_val_if_fail (filename != NULL, NULL);
1533 f = g_fopen (filename, "rb");
1537 loader = gdk_pixbuf_loader_new ();
1543 g_signal_connect (loader, "size-prepared", G_CALLBACK (info_cb), &info);
1545 while (!feof (f) && !ferror (f)) {
1546 length = fread (buffer, 1, sizeof (buffer), f);
1548 if (!gdk_pixbuf_loader_write (loader, buffer, length, NULL))
1551 if (info.format != NULL)
1556 gdk_pixbuf_loader_close (loader, NULL);
1557 g_object_unref (loader);
1560 *width = info.width;
1562 *height = info.height;
1568 * gdk_pixbuf_new_from_xpm_data:
1569 * @data: Pointer to inline XPM data.
1571 * Creates a new pixbuf by parsing XPM data in memory. This data is commonly
1572 * the result of including an XPM file into a program's C source.
1574 * Return value: A newly-created pixbuf with a reference count of 1.
1577 gdk_pixbuf_new_from_xpm_data (const char **data)
1579 GdkPixbuf *(* load_xpm_data) (const char **data);
1581 GError *error = NULL;
1582 GdkPixbufModule *xpm_module;
1585 g_return_val_if_fail (data != NULL, NULL);
1587 xpm_module = _gdk_pixbuf_get_named_module ("xpm", &error);
1588 if (xpm_module == NULL) {
1589 g_warning ("Error loading XPM image loader: %s", error->message);
1590 g_error_free (error);
1594 if (!_gdk_pixbuf_load_module (xpm_module, &error)) {
1595 g_warning ("Error loading XPM image loader: %s", error->message);
1596 g_error_free (error);
1600 locked = _gdk_pixbuf_lock (xpm_module);
1602 if (xpm_module->load_xpm_data == NULL) {
1603 g_warning ("gdk-pixbuf XPM module lacks XPM data capability");
1606 load_xpm_data = xpm_module->load_xpm_data;
1607 pixbuf = (* load_xpm_data) (data);
1611 _gdk_pixbuf_unlock (xpm_module);
1616 collect_save_options (va_list opts,
1629 next = va_arg (opts, gchar*);
1633 val = va_arg (opts, gchar*);
1638 *keys = g_realloc (*keys, sizeof(gchar*) * (count + 1));
1639 *vals = g_realloc (*vals, sizeof(gchar*) * (count + 1));
1641 (*keys)[count-1] = g_strdup (key);
1642 (*vals)[count-1] = g_strdup (val);
1644 (*keys)[count] = NULL;
1645 (*vals)[count] = NULL;
1647 next = va_arg (opts, gchar*);
1652 save_to_file_callback (const gchar *buf,
1657 FILE *filehandle = data;
1660 n = fwrite (buf, 1, count, filehandle);
1662 gint save_errno = errno;
1665 g_file_error_from_errno (save_errno),
1666 _("Error writing to image file: %s"),
1667 g_strerror (save_errno));
1674 gdk_pixbuf_real_save (GdkPixbuf *pixbuf,
1682 GdkPixbufModule *image_module = NULL;
1685 image_module = _gdk_pixbuf_get_named_module (type, error);
1687 if (image_module == NULL)
1690 if (!_gdk_pixbuf_load_module (image_module, error))
1693 locked = _gdk_pixbuf_lock (image_module);
1695 if (image_module->save) {
1697 ret = (* image_module->save) (filehandle, pixbuf,
1700 } else if (image_module->save_to_callback) {
1701 /* save with simple callback */
1702 ret = (* image_module->save_to_callback) (save_to_file_callback,
1710 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
1711 _("This build of gdk-pixbuf does not support saving the image format: %s"),
1717 _gdk_pixbuf_unlock (image_module);
1721 #define TMP_FILE_BUF_SIZE 4096
1724 save_to_callback_with_tmp_file (GdkPixbufModule *image_module,
1726 GdkPixbufSaveFunc save_func,
1734 gboolean retval = FALSE;
1737 gchar *filename = NULL;
1740 buf = g_try_malloc (TMP_FILE_BUF_SIZE);
1742 g_set_error_literal (error,
1744 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1745 _("Insufficient memory to save image to callback"));
1749 fd = g_file_open_tmp ("gdkpixbuf-save-tmp.XXXXXX", &filename, error);
1752 f = fdopen (fd, "wb+");
1754 gint save_errno = errno;
1755 g_set_error_literal (error,
1757 g_file_error_from_errno (save_errno),
1758 _("Failed to open temporary file"));
1762 locked = _gdk_pixbuf_lock (image_module);
1763 retval = (image_module->save) (f, pixbuf, keys, values, error);
1765 _gdk_pixbuf_unlock (image_module);
1771 n = fread (buf, 1, TMP_FILE_BUF_SIZE, f);
1773 if (!save_func (buf, n, error, user_data))
1776 if (n != TMP_FILE_BUF_SIZE)
1780 gint save_errno = errno;
1781 g_set_error_literal (error,
1783 g_file_error_from_errno (save_errno),
1784 _("Failed to read from temporary file"));
1790 /* cleanup and return retval */
1794 g_unlink (filename);
1803 gdk_pixbuf_real_save_to_callback (GdkPixbuf *pixbuf,
1804 GdkPixbufSaveFunc save_func,
1812 GdkPixbufModule *image_module = NULL;
1815 image_module = _gdk_pixbuf_get_named_module (type, error);
1817 if (image_module == NULL)
1820 if (!_gdk_pixbuf_load_module (image_module, error))
1823 locked = _gdk_pixbuf_lock (image_module);
1825 if (image_module->save_to_callback) {
1827 ret = (* image_module->save_to_callback) (save_func, user_data,
1828 pixbuf, keys, values,
1830 } else if (image_module->save) {
1831 /* use a temporary file */
1832 ret = save_to_callback_with_tmp_file (image_module, pixbuf,
1833 save_func, user_data,
1840 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
1841 _("This build of gdk-pixbuf does not support saving the image format: %s"),
1847 _gdk_pixbuf_unlock (image_module);
1854 * @pixbuf: a #GdkPixbuf.
1855 * @filename: name of file to save.
1856 * @type: name of file format.
1857 * @error: return location for error, or %NULL
1858 * @Varargs: list of key-value save options
1860 * Saves pixbuf to a file in format @type. By default, "jpeg", "png", "ico"
1861 * and "bmp" are possible file formats to save in, but more formats may be
1862 * installed. The list of all writable formats can be determined in the
1866 * void add_if_writable (GdkPixbufFormat *data, GSList **list)
1868 * if (gdk_pixbuf_format_is_writable (data))
1869 * *list = g_slist_prepend (*list, data);
1872 * GSList *formats = gdk_pixbuf_get_formats ();
1873 * GSList *writable_formats = NULL;
1874 * g_slist_foreach (formats, add_if_writable, &writable_formats);
1875 * g_slist_free (formats);
1878 * If @error is set, %FALSE will be returned. Possible errors include
1879 * those in the #GDK_PIXBUF_ERROR domain and those in the #G_FILE_ERROR domain.
1881 * The variable argument list should be %NULL-terminated; if not empty,
1882 * it should contain pairs of strings that modify the save
1883 * parameters. For example:
1884 * <informalexample><programlisting>
1885 * gdk_pixbuf_save (pixbuf, handle, "jpeg", &error,
1886 * "quality", "100", NULL);
1887 * </programlisting></informalexample>
1889 * Currently only few parameters exist. JPEG images can be saved with a
1890 * "quality" parameter; its value should be in the range [0,100].
1892 * Text chunks can be attached to PNG images by specifying parameters of
1893 * the form "tEXt::key", where key is an ASCII string of length 1-79.
1894 * The values are UTF-8 encoded strings. The PNG compression level can
1895 * be specified using the "compression" parameter; it's value is in an
1896 * integer in the range of [0,9].
1898 * ICO images can be saved in depth 16, 24, or 32, by using the "depth"
1899 * parameter. When the ICO saver is given "x_hot" and "y_hot" parameters,
1900 * it produces a CUR instead of an ICO.
1902 * Return value: whether an error was set
1906 gdk_pixbuf_save (GdkPixbuf *pixbuf,
1907 const char *filename,
1912 gchar **keys = NULL;
1913 gchar **values = NULL;
1917 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1919 va_start (args, error);
1921 collect_save_options (args, &keys, &values);
1925 result = gdk_pixbuf_savev (pixbuf, filename, type,
1930 g_strfreev (values);
1937 #undef gdk_pixbuf_save
1940 gdk_pixbuf_save (GdkPixbuf *pixbuf,
1941 const char *filename,
1946 char *utf8_filename;
1947 gchar **keys = NULL;
1948 gchar **values = NULL;
1952 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1954 utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1956 if (utf8_filename == NULL)
1959 va_start (args, error);
1961 collect_save_options (args, &keys, &values);
1965 result = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
1969 g_free (utf8_filename);
1972 g_strfreev (values);
1981 * @pixbuf: a #GdkPixbuf.
1982 * @filename: name of file to save.
1983 * @type: name of file format.
1984 * @option_keys: name of options to set, %NULL-terminated
1985 * @option_values: values for named options
1986 * @error: return location for error, or %NULL
1988 * Saves pixbuf to a file in @type, which is currently "jpeg", "png", "tiff", "ico" or "bmp".
1989 * If @error is set, %FALSE will be returned.
1990 * See gdk_pixbuf_save () for more details.
1992 * Return value: whether an error was set
1996 gdk_pixbuf_savev (GdkPixbuf *pixbuf,
1997 const char *filename,
2000 char **option_values,
2006 g_return_val_if_fail (filename != NULL, FALSE);
2007 g_return_val_if_fail (type != NULL, FALSE);
2008 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2010 f = g_fopen (filename, "wb");
2013 gint save_errno = errno;
2014 gchar *display_name = g_filename_display_name (filename);
2017 g_file_error_from_errno (save_errno),
2018 _("Failed to open '%s' for writing: %s"),
2020 g_strerror (save_errno));
2021 g_free (display_name);
2026 result = gdk_pixbuf_real_save (pixbuf, f, type,
2027 option_keys, option_values,
2032 g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
2037 if (fclose (f) < 0) {
2038 gint save_errno = errno;
2039 gchar *display_name = g_filename_display_name (filename);
2042 g_file_error_from_errno (save_errno),
2043 _("Failed to close '%s' while writing image, all data may not have been saved: %s"),
2045 g_strerror (save_errno));
2046 g_free (display_name);
2055 #undef gdk_pixbuf_savev
2058 gdk_pixbuf_savev (GdkPixbuf *pixbuf,
2059 const char *filename,
2062 char **option_values,
2065 char *utf8_filename;
2068 g_return_val_if_fail (filename != NULL, FALSE);
2070 utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2072 if (utf8_filename == NULL)
2075 retval = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
2076 option_keys, option_values, error);
2078 g_free (utf8_filename);
2086 * gdk_pixbuf_save_to_callback:
2087 * @pixbuf: a #GdkPixbuf.
2088 * @save_func: a function that is called to save each block of data that
2089 * the save routine generates.
2090 * @user_data: user data to pass to the save function.
2091 * @type: name of file format.
2092 * @error: return location for error, or %NULL
2093 * @Varargs: list of key-value save options
2095 * Saves pixbuf in format @type by feeding the produced data to a
2096 * callback. Can be used when you want to store the image to something
2097 * other than a file, such as an in-memory buffer or a socket.
2098 * If @error is set, %FALSE will be returned. Possible errors
2099 * include those in the #GDK_PIXBUF_ERROR domain and whatever the save
2100 * function generates.
2102 * See gdk_pixbuf_save() for more details.
2104 * Return value: whether an error was set
2109 gdk_pixbuf_save_to_callback (GdkPixbuf *pixbuf,
2110 GdkPixbufSaveFunc save_func,
2116 gchar **keys = NULL;
2117 gchar **values = NULL;
2121 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2123 va_start (args, error);
2125 collect_save_options (args, &keys, &values);
2129 result = gdk_pixbuf_save_to_callbackv (pixbuf, save_func, user_data,
2134 g_strfreev (values);
2140 * gdk_pixbuf_save_to_callbackv:
2141 * @pixbuf: a #GdkPixbuf.
2142 * @save_func: a function that is called to save each block of data that
2143 * the save routine generates.
2144 * @user_data: user data to pass to the save function.
2145 * @type: name of file format.
2146 * @option_keys: name of options to set, %NULL-terminated
2147 * @option_values: values for named options
2148 * @error: return location for error, or %NULL
2150 * Saves pixbuf to a callback in format @type, which is currently "jpeg",
2151 * "png", "tiff", "ico" or "bmp". If @error is set, %FALSE will be returned. See
2152 * gdk_pixbuf_save_to_callback () for more details.
2154 * Return value: whether an error was set
2159 gdk_pixbuf_save_to_callbackv (GdkPixbuf *pixbuf,
2160 GdkPixbufSaveFunc save_func,
2164 char **option_values,
2170 g_return_val_if_fail (save_func != NULL, FALSE);
2171 g_return_val_if_fail (type != NULL, FALSE);
2172 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2174 result = gdk_pixbuf_real_save_to_callback (pixbuf,
2175 save_func, user_data, type,
2176 option_keys, option_values,
2180 g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
2188 * gdk_pixbuf_save_to_buffer:
2189 * @pixbuf: a #GdkPixbuf.
2190 * @buffer: location to receive a pointer to the new buffer.
2191 * @buffer_size: location to receive the size of the new buffer.
2192 * @type: name of file format.
2193 * @error: return location for error, or %NULL
2194 * @Varargs: list of key-value save options
2196 * Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2197 * "png", "tiff", "ico" or "bmp". This is a convenience function that uses
2198 * gdk_pixbuf_save_to_callback() to do the real work. Note that the buffer
2199 * is not nul-terminated and may contain embedded nuls.
2200 * If @error is set, %FALSE will be returned and @buffer will be set to
2201 * %NULL. Possible errors include those in the #GDK_PIXBUF_ERROR
2204 * See gdk_pixbuf_save() for more details.
2206 * Return value: whether an error was set
2211 gdk_pixbuf_save_to_buffer (GdkPixbuf *pixbuf,
2218 gchar **keys = NULL;
2219 gchar **values = NULL;
2223 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2225 va_start (args, error);
2227 collect_save_options (args, &keys, &values);
2231 result = gdk_pixbuf_save_to_bufferv (pixbuf, buffer, buffer_size,
2236 g_strfreev (values);
2241 struct SaveToBufferData {
2247 save_to_buffer_callback (const gchar *data,
2252 struct SaveToBufferData *sdata = user_data;
2256 if (sdata->len + count > sdata->max) {
2257 new_max = MAX (sdata->max*2, sdata->len + count);
2258 new_buffer = g_try_realloc (sdata->buffer, new_max);
2260 g_set_error_literal (error,
2262 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2263 _("Insufficient memory to save image into a buffer"));
2266 sdata->buffer = new_buffer;
2267 sdata->max = new_max;
2269 memcpy (sdata->buffer + sdata->len, data, count);
2270 sdata->len += count;
2275 * gdk_pixbuf_save_to_bufferv:
2276 * @pixbuf: a #GdkPixbuf.
2277 * @buffer: location to receive a pointer to the new buffer.
2278 * @buffer_size: location to receive the size of the new buffer.
2279 * @type: name of file format.
2280 * @option_keys: name of options to set, %NULL-terminated
2281 * @option_values: values for named options
2282 * @error: return location for error, or %NULL
2284 * Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2285 * "tiff", "png", "ico" or "bmp". See gdk_pixbuf_save_to_buffer()
2288 * Return value: whether an error was set
2293 gdk_pixbuf_save_to_bufferv (GdkPixbuf *pixbuf,
2298 char **option_values,
2301 static const gint initial_max = 1024;
2302 struct SaveToBufferData sdata;
2307 sdata.buffer = g_try_malloc (initial_max);
2308 sdata.max = initial_max;
2310 if (!sdata.buffer) {
2311 g_set_error_literal (error,
2313 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2314 _("Insufficient memory to save image into a buffer"));
2318 if (!gdk_pixbuf_save_to_callbackv (pixbuf,
2319 save_to_buffer_callback, &sdata,
2320 type, option_keys, option_values,
2322 g_free (sdata.buffer);
2326 *buffer = sdata.buffer;
2327 *buffer_size = sdata.len;
2332 GOutputStream *stream;
2333 GCancellable *cancellable;
2337 save_to_stream (const gchar *buffer,
2342 SaveToStreamData *sdata = (SaveToStreamData *)data;
2345 GError *my_error = NULL;
2349 while (remaining > 0) {
2351 remaining -= written;
2352 written = g_output_stream_write (sdata->stream,
2358 g_set_error_literal (error,
2360 _("Error writing to image stream"));
2363 g_propagate_error (error, my_error);
2373 * gdk_pixbuf_save_to_stream:
2374 * @pixbuf: a #GdkPixbuf
2375 * @stream: a #GOutputStream to save the pixbuf to
2376 * @type: name of file format
2377 * @cancellable: optional #GCancellable object, %NULL to ignore
2378 * @error: return location for error, or %NULL
2379 * @Varargs: list of key-value save options
2381 * Saves @pixbuf to an output stream.
2383 * Supported file formats are currently "jpeg", "tiff", "png", "ico" or
2384 * "bmp". See gdk_pixbuf_save_to_buffer() for more details.
2386 * The @cancellable can be used to abort the operation from another
2387 * thread. If the operation was cancelled, the error %GIO_ERROR_CANCELLED
2388 * will be returned. Other possible errors are in the #GDK_PIXBUF_ERROR
2389 * and %G_IO_ERROR domains.
2391 * The stream is not closed.
2393 * Returns: %TRUE if the pixbuf was saved successfully, %FALSE if an
2399 gdk_pixbuf_save_to_stream (GdkPixbuf *pixbuf,
2400 GOutputStream *stream,
2402 GCancellable *cancellable,
2407 gchar **keys = NULL;
2408 gchar **values = NULL;
2410 SaveToStreamData data;
2412 va_start (args, error);
2413 collect_save_options (args, &keys, &values);
2416 data.stream = stream;
2417 data.cancellable = cancellable;
2419 res = gdk_pixbuf_save_to_callbackv (pixbuf, save_to_stream,
2425 g_strfreev (values);
2431 * gdk_pixbuf_format_get_name:
2432 * @format: a #GdkPixbufFormat
2434 * Returns the name of the format.
2436 * Return value: the name of the format.
2441 gdk_pixbuf_format_get_name (GdkPixbufFormat *format)
2443 g_return_val_if_fail (format != NULL, NULL);
2445 return g_strdup (format->name);
2449 * gdk_pixbuf_format_get_description:
2450 * @format: a #GdkPixbufFormat
2452 * Returns a description of the format.
2454 * Return value: a description of the format.
2459 gdk_pixbuf_format_get_description (GdkPixbufFormat *format)
2462 const gchar *description;
2463 g_return_val_if_fail (format != NULL, NULL);
2465 if (format->domain != NULL)
2466 domain = format->domain;
2468 domain = GETTEXT_PACKAGE;
2469 description = g_dgettext (domain, format->description);
2471 return g_strdup (description);
2475 * gdk_pixbuf_format_get_mime_types:
2476 * @format: a #GdkPixbufFormat
2478 * Returns the mime types supported by the format.
2480 * Return value: a %NULL-terminated array of mime types which must be freed with
2481 * g_strfreev() when it is no longer needed.
2486 gdk_pixbuf_format_get_mime_types (GdkPixbufFormat *format)
2488 g_return_val_if_fail (format != NULL, NULL);
2490 return g_strdupv (format->mime_types);
2494 * gdk_pixbuf_format_get_extensions:
2495 * @format: a #GdkPixbufFormat
2497 * Returns the filename extensions typically used for files in the
2500 * Return value: a %NULL-terminated array of filename extensions which must be
2501 * freed with g_strfreev() when it is no longer needed.
2506 gdk_pixbuf_format_get_extensions (GdkPixbufFormat *format)
2508 g_return_val_if_fail (format != NULL, NULL);
2510 return g_strdupv (format->extensions);
2514 * gdk_pixbuf_format_is_writable:
2515 * @format: a #GdkPixbufFormat
2517 * Returns whether pixbufs can be saved in the given format.
2519 * Return value: whether pixbufs can be saved in the given format.
2524 gdk_pixbuf_format_is_writable (GdkPixbufFormat *format)
2526 g_return_val_if_fail (format != NULL, FALSE);
2528 return (format->flags & GDK_PIXBUF_FORMAT_WRITABLE) != 0;
2532 * gdk_pixbuf_format_is_scalable:
2533 * @format: a #GdkPixbufFormat
2535 * Returns whether this image format is scalable. If a file is in a
2536 * scalable format, it is preferable to load it at the desired size,
2537 * rather than loading it at the default size and scaling the
2538 * resulting pixbuf to the desired size.
2540 * Return value: whether this image format is scalable.
2545 gdk_pixbuf_format_is_scalable (GdkPixbufFormat *format)
2547 g_return_val_if_fail (format != NULL, FALSE);
2549 return (format->flags & GDK_PIXBUF_FORMAT_SCALABLE) != 0;
2553 * gdk_pixbuf_format_is_disabled:
2554 * @format: a #GdkPixbufFormat
2556 * Returns whether this image format is disabled. See
2557 * gdk_pixbuf_format_set_disabled().
2559 * Return value: whether this image format is disabled.
2564 gdk_pixbuf_format_is_disabled (GdkPixbufFormat *format)
2566 g_return_val_if_fail (format != NULL, FALSE);
2568 return format->disabled;
2572 * gdk_pixbuf_format_set_disabled:
2573 * @format: a #GdkPixbufFormat
2574 * @disabled: %TRUE to disable the format @format
2576 * Disables or enables an image format. If a format is disabled,
2577 * gdk-pixbuf won't use the image loader for this format to load
2578 * images. Applications can use this to avoid using image loaders
2579 * with an inappropriate license, see gdk_pixbuf_format_get_license().
2584 gdk_pixbuf_format_set_disabled (GdkPixbufFormat *format,
2587 g_return_if_fail (format != NULL);
2589 format->disabled = disabled != FALSE;
2593 * gdk_pixbuf_format_get_license:
2594 * @format: a #GdkPixbufFormat
2596 * Returns information about the license of the image loader for the format. The
2597 * returned string should be a shorthand for a wellknown license, e.g. "LGPL",
2598 * "GPL", "QPL", "GPL/QPL", or "other" to indicate some other license. This
2599 * string should be freed with g_free() when it's no longer needed.
2601 * Returns: a string describing the license of @format.
2606 gdk_pixbuf_format_get_license (GdkPixbufFormat *format)
2608 g_return_val_if_fail (format != NULL, NULL);
2610 return g_strdup (format->license);
2614 _gdk_pixbuf_get_format (GdkPixbufModule *module)
2616 g_return_val_if_fail (module != NULL, NULL);
2618 return module->info;
2622 * gdk_pixbuf_get_formats:
2624 * Obtains the available information about the image formats supported
2627 * Returns: A list of #GdkPixbufFormat<!-- -->s describing the supported
2628 * image formats. The list should be freed when it is no longer needed,
2629 * but the structures themselves are owned by #GdkPixbuf and should not be
2635 gdk_pixbuf_get_formats (void)
2637 GSList *result = NULL;
2640 for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
2641 GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
2642 GdkPixbufFormat *info = _gdk_pixbuf_get_format (module);
2643 result = g_slist_prepend (result, info);
2650 #define __GDK_PIXBUF_IO_C__
2651 #include "gdk-pixbuf-aliasdef.c"