1 /* GdkPixbuf library - Main loading interface.
3 * Copyright (C) 1999 The Free Software Foundation
5 * Authors: Miguel de Icaza <miguel@gnu.org>
6 * Federico Mena-Quintero <federico@gimp.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
28 #include "gdk-pixbuf-private.h"
29 #include "gdk-pixbuf-io.h"
39 pixbuf_check_png (guchar *buffer, int size)
44 if (buffer [0] != 0x89 ||
58 pixbuf_check_jpeg (guchar *buffer, int size)
63 if (buffer [0] != 0xff || buffer [1] != 0xd8)
70 pixbuf_check_tiff (guchar *buffer, int size)
75 if (buffer [0] == 'M' &&
81 if (buffer [0] == 'I' &&
91 pixbuf_check_gif (guchar *buffer, int size)
96 if (strncmp (buffer, "GIF8", 4) == 0)
103 pixbuf_check_xpm (guchar *buffer, int size)
108 if (strncmp (buffer, "/* XPM */", 9) == 0)
115 pixbuf_check_pnm (guchar *buffer, int size)
120 if (buffer [0] == 'P') {
121 if (buffer [1] == '1' ||
132 pixbuf_check_sunras (guchar *buffer, int size)
137 if (buffer [0] != 0x59 ||
138 buffer [1] != 0xA6 ||
139 buffer [2] != 0x6A ||
147 pixbuf_check_ico (guchar *buffer, int size)
149 /* Note that this may cause false positives, but .ico's don't
150 have a magic number.*/
153 if (buffer [0] != 0x0 ||
155 ((buffer [2] != 0x1)&&(buffer[2]!=0x2)) ||
165 pixbuf_check_bmp (guchar *buffer, int size)
170 if (buffer [0] != 'B' || buffer [1] != 'M')
177 pixbuf_check_wbmp (guchar *buffer, int size)
179 if(size < 10) /* at least */
182 if(buffer[0] == '\0' /* We only handle type 0 wbmp's for now */)
188 static GdkPixbufModule file_formats [] = {
189 { "png", pixbuf_check_png, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
190 { "jpeg", pixbuf_check_jpeg, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
191 { "tiff", pixbuf_check_tiff, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
192 { "gif", pixbuf_check_gif, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
193 #define XPM_FILE_FORMAT_INDEX 4
194 { "xpm", pixbuf_check_xpm, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
195 { "pnm", pixbuf_check_pnm, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
196 { "ras", pixbuf_check_sunras, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
197 { "ico", pixbuf_check_ico, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
198 { "bmp", pixbuf_check_bmp, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
199 { "wbmp", pixbuf_check_wbmp, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
200 { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
205 pixbuf_module_symbol (GModule *module, const char *module_name, const char *symbol_name, gpointer *symbol)
207 char *full_symbol_name = g_strconcat ("gdk_pixbuf__", module_name, "_", symbol_name, NULL);
208 gboolean return_value;
210 return_value = g_module_symbol (module, full_symbol_name, symbol);
211 g_free (full_symbol_name);
221 static char *libdir = NULL;
224 libdir = g_win32_get_package_installation_subdirectory
226 g_strdup_printf ("gdk_pixbug-%d.%d.dll",
227 GDK_PIXBUF_MAJOR, GDK_PIXBUF_MINOR),
233 #define PIXBUF_LIBDIR get_libdir ()
237 /* actually load the image handler - gdk_pixbuf_get_module only get a */
238 /* reference to the module to load, it doesn't actually load it */
239 /* perhaps these actions should be combined in one function */
241 gdk_pixbuf_load_module (GdkPixbufModule *image_module,
251 g_return_val_if_fail (image_module->module == NULL, FALSE);
253 name = image_module->module_name;
255 module_name = g_strconcat ("pixbufloader-", name, NULL);
256 path = g_module_build_path (PIXBUF_LIBDIR, module_name);
258 module = g_module_open (path, G_MODULE_BIND_LAZY);
260 /* Debug feature, check in GDK_PIXBUF_MODULEDIR, or working directory */
261 char *dir = g_getenv ("GDK_PIXBUF_MODULEDIR");
266 path = g_module_build_path (dir, module_name);
267 module = g_module_open (path, G_MODULE_BIND_LAZY);
272 GDK_PIXBUF_ERROR_FAILED,
273 _("Unable to load image-loading module: %s: %s"),
274 path, g_module_error ());
275 g_free (module_name);
284 g_free (module_name);
286 image_module->module = module;
288 if (pixbuf_module_symbol (module, name, "image_load", &load_sym))
289 image_module->load = load_sym;
291 if (pixbuf_module_symbol (module, name, "image_load_xpm_data", &load_sym))
292 image_module->load_xpm_data = load_sym;
294 if (pixbuf_module_symbol (module, name, "image_begin_load", &load_sym))
295 image_module->begin_load = load_sym;
297 if (pixbuf_module_symbol (module, name, "image_stop_load", &load_sym))
298 image_module->stop_load = load_sym;
300 if (pixbuf_module_symbol (module, name, "image_load_increment", &load_sym))
301 image_module->load_increment = load_sym;
303 if (pixbuf_module_symbol (module, name, "image_load_animation", &load_sym))
304 image_module->load_animation = load_sym;
306 if (pixbuf_module_symbol (module, name, "image_save", &save_sym))
307 image_module->save = save_sym;
313 #define mname(type,fn) gdk_pixbuf__ ## type ## _image_ ##fn
314 #define m_load(type) extern GdkPixbuf * mname(type,load) (FILE *f, GError **error);
315 #define m_load_xpm_data(type) extern GdkPixbuf * mname(type,load_xpm_data) (const char **data);
316 #define m_begin_load(type) \
317 extern gpointer mname(type,begin_load) (ModulePreparedNotifyFunc prepare_func, \
318 ModuleUpdatedNotifyFunc update_func, \
319 ModuleFrameDoneNotifyFunc frame_done_func,\
320 ModuleAnimationDoneNotifyFunc anim_done_func,\
323 #define m_stop_load(type) extern void mname(type,stop_load) (gpointer context);
324 #define m_load_increment(type) extern gboolean mname(type,load_increment) (gpointer context, const guchar *buf, guint size, GError **error);
325 #define m_load_animation(type) extern GdkPixbufAnimation * mname(type,load_animation) (FILE *f, GError **error);
326 #define m_save(type) \
327 extern gboolean mname(type,save) (FILE *f, \
336 m_load_increment (png);
342 m_load_increment (bmp);
347 m_load_increment (wbmp);
352 m_load_increment (gif);
354 m_load_animation (gif);
358 m_load_increment (ico);
363 m_load_increment (jpeg);
369 m_load_increment (pnm);
374 m_load_increment (ras);
379 m_load_increment (tiff);
383 m_load_xpm_data (xpm);
386 gdk_pixbuf_load_module (GdkPixbufModule *image_module,
389 image_module->module = (void *) 1;
392 if (strcmp (image_module->module_name, "png") == 0){
393 image_module->load = mname (png,load);
394 image_module->begin_load = mname (png,begin_load);
395 image_module->load_increment = mname (png,load_increment);
396 image_module->stop_load = mname (png,stop_load);
397 image_module->save = mname (png,save);
403 if (strcmp (image_module->module_name, "bmp") == 0){
404 image_module->load = mname (bmp,load);
405 image_module->begin_load = mname (bmp,begin_load);
406 image_module->load_increment = mname (bmp,load_increment);
407 image_module->stop_load = mname (bmp,stop_load);
413 if (strcmp (image_module->module_name, "wbmp") == 0){
414 image_module->load = mname (wbmp,load);
415 image_module->begin_load = mname (wbmp,begin_load);
416 image_module->load_increment = mname (wbmp,load_increment);
417 image_module->stop_load = mname (wbmp,stop_load);
423 if (strcmp (image_module->module_name, "gif") == 0){
424 image_module->load = mname (gif,load);
425 image_module->begin_load = mname (gif,begin_load);
426 image_module->load_increment = mname (gif,load_increment);
427 image_module->stop_load = mname (gif,stop_load);
428 image_module->load_animation = mname (gif,load_animation);
434 if (strcmp (image_module->module_name, "ico") == 0){
435 image_module->load = mname (ico,load);
436 image_module->begin_load = mname (ico,begin_load);
437 image_module->load_increment = mname (ico,load_increment);
438 image_module->stop_load = mname (ico,stop_load);
444 if (strcmp (image_module->module_name, "jpeg") == 0){
445 image_module->load = mname (jpeg,load);
446 image_module->begin_load = mname (jpeg,begin_load);
447 image_module->load_increment = mname (jpeg,load_increment);
448 image_module->stop_load = mname (jpeg,stop_load);
449 image_module->save = mname (jpeg,save);
455 if (strcmp (image_module->module_name, "pnm") == 0){
456 image_module->load = mname (pnm,load);
457 image_module->begin_load = mname (pnm,begin_load);
458 image_module->load_increment = mname (pnm,load_increment);
459 image_module->stop_load = mname (pnm,stop_load);
465 if (strcmp (image_module->module_name, "ras") == 0){
466 image_module->load = mname (ras,load);
467 image_module->begin_load = mname (ras,begin_load);
468 image_module->load_increment = mname (ras,load_increment);
469 image_module->stop_load = mname (ras,stop_load);
475 if (strcmp (image_module->module_name, "tiff") == 0){
476 image_module->load = mname (tiff,load);
477 image_module->begin_load = mname (tiff,begin_load);
478 image_module->load_increment = mname (tiff,load_increment);
479 image_module->stop_load = mname (tiff,stop_load);
485 if (strcmp (image_module->module_name, "xpm") == 0){
486 image_module->load = mname (xpm,load);
487 image_module->load_xpm_data = mname (xpm,load_xpm_data);
494 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
495 _("Image type '%s' is not supported"),
496 image_module->module_name);
507 gdk_pixbuf_get_named_module (const char *name,
512 for (i = 0; file_formats [i].module_name; i++) {
513 if (!strcmp(name, file_formats[i].module_name))
514 return &(file_formats[i]);
519 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
520 _("Image type '%s' is not supported"),
527 gdk_pixbuf_get_module (guchar *buffer, guint size,
528 const gchar *filename,
533 for (i = 0; file_formats [i].module_name; i++) {
534 if ((* file_formats [i].format_check) (buffer, size))
535 return &(file_formats[i]);
541 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
542 _("Couldn't recognize the image file format for file '%s'"),
547 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
548 _("Unrecognized image file format"));
555 * gdk_pixbuf_new_from_file:
556 * @filename: Name of file to load.
557 * @error: Return location for an error
559 * Creates a new pixbuf by loading an image from a file. The file format is
560 * detected automatically. If NULL is returned, then @error will be set.
561 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
563 * Return value: A newly-created pixbuf with a reference count of 1, or NULL if
564 * any of several error conditions occurred: the file could not be opened,
565 * there was no loader for the file's format, there was not enough memory to
566 * allocate the image buffer, or the image file contained invalid data.
569 gdk_pixbuf_new_from_file (const char *filename,
576 GdkPixbufModule *image_module;
578 g_return_val_if_fail (filename != NULL, NULL);
580 f = fopen (filename, "rb");
584 g_file_error_from_errno (errno),
585 _("Failed to open file '%s': %s"),
586 filename, g_strerror (errno));
590 size = fread (&buffer, 1, sizeof (buffer), f);
594 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
595 _("Image file '%s' contains no data"),
602 image_module = gdk_pixbuf_get_module (buffer, size, filename, error);
603 if (image_module == NULL) {
608 if (image_module->module == NULL)
609 if (!gdk_pixbuf_load_module (image_module, error)) {
614 if (image_module->load == NULL) {
617 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
618 _("Don't know how to load the image in file '%s'"),
625 fseek (f, 0, SEEK_SET);
626 pixbuf = (* image_module->load) (f, error);
629 if (pixbuf == NULL && error != NULL && *error == NULL) {
630 /* I don't trust these crufty longjmp()'ing image libs
631 * to maintain proper error invariants, and I don't
632 * want user code to segfault as a result. We need to maintain
633 * the invariant that error gets set if NULL is returned.
636 g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.", image_module->module_name);
639 GDK_PIXBUF_ERROR_FAILED,
640 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
643 } else if (error != NULL && *error != NULL) {
645 /* Add the filename to the error message */
651 e->message = g_strdup_printf (_("Failed to load image '%s': %s"),
661 * gdk_pixbuf_new_from_xpm_data:
662 * @data: Pointer to inline XPM data.
664 * Creates a new pixbuf by parsing XPM data in memory. This data is commonly
665 * the result of including an XPM file into a program's C source.
667 * Return value: A newly-created pixbuf with a reference count of 1.
670 gdk_pixbuf_new_from_xpm_data (const char **data)
672 GdkPixbuf *(* load_xpm_data) (const char **data);
675 if (file_formats[XPM_FILE_FORMAT_INDEX].module == NULL)
676 gdk_pixbuf_load_module (&file_formats[XPM_FILE_FORMAT_INDEX], NULL);
678 if (file_formats[XPM_FILE_FORMAT_INDEX].module == NULL) {
679 g_warning ("Can't find gdk-pixbuf module for parsing inline XPM data");
681 } else if (file_formats[XPM_FILE_FORMAT_INDEX].load_xpm_data == NULL) {
682 g_warning ("gdk-pixbuf XPM module lacks XPM data capability");
685 load_xpm_data = file_formats[XPM_FILE_FORMAT_INDEX].load_xpm_data;
687 pixbuf = (* load_xpm_data) (data);
692 collect_save_options (va_list opts,
705 next = va_arg (opts, gchar*);
709 val = va_arg (opts, gchar*);
714 *keys = g_realloc (*keys, sizeof(gchar*) * (count + 1));
715 *vals = g_realloc (*vals, sizeof(gchar*) * (count + 1));
717 (*keys)[count-1] = g_strdup (key);
718 (*vals)[count-1] = g_strdup (val);
720 (*keys)[count] = NULL;
721 (*vals)[count] = NULL;
723 next = va_arg (opts, gchar*);
728 gdk_pixbuf_real_save (GdkPixbuf *pixbuf,
735 GdkPixbufModule *image_module = NULL;
737 image_module = gdk_pixbuf_get_named_module (type, error);
739 if (image_module == NULL)
742 if (image_module->module == NULL)
743 if (!gdk_pixbuf_load_module (image_module, error))
746 if (image_module->save == NULL) {
749 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
750 _("This build of gdk-pixbuf does not support saving the image format: %s"),
755 return (* image_module->save) (filehandle, pixbuf,
763 * @pixbuf: pointer to GdkPixbuf.
764 * @filename: Name of file to save.
765 * @type: name of file format.
766 * @error: return location for error, or NULL
767 * @Varargs: list of key-value save options
769 * Saves pixbuf to a file in @type, which is currently "jpeg" or
770 * "png". If @error is set, FALSE will be returned. Possible errors include those
771 * in the #GDK_PIXBUF_ERROR domain and those in the #G_FILE_ERROR domain.
773 * The variable argument list should be NULL-terminated; if not empty,
774 * it should contain pairs of strings that modify the save
775 * parameters. For example:
778 * gdk_pixbuf_save (pixbuf, handle, "jpeg", &error,
779 * "quality", "100", NULL);
782 * The only save parameter that currently exists is the "quality" field
783 * for JPEG images; its value should be in the range [0,100].
785 * Return value: whether an error was set
789 gdk_pixbuf_save (GdkPixbuf *pixbuf,
790 const char *filename,
796 gchar **values = NULL;
800 va_start (args, error);
802 collect_save_options (args, &keys, &values);
806 result = gdk_pixbuf_savev (pixbuf, filename, type,
818 * @pixbuf: pointer to GdkPixbuf.
819 * @filename: Name of file to save.
820 * @type: name of file format.
821 * @option_keys: name of options to set, NULL-terminated
822 * @option_values: values for named options
823 * @error: return location for error, or NULL
825 * Saves pixbuf to a file in @type, which is currently "jpeg" or "png".
826 * If @error is set, FALSE will be returned. See gdk_pixbuf_save () for more
829 * Return value: whether an error was set
833 gdk_pixbuf_savev (GdkPixbuf *pixbuf,
834 const char *filename,
837 char **option_values,
844 g_return_val_if_fail (filename != NULL, FALSE);
845 g_return_val_if_fail (type != NULL, FALSE);
847 f = fopen (filename, "wb");
852 g_file_error_from_errno (errno),
853 _("Failed to open '%s' for writing: %s"),
854 filename, g_strerror (errno));
859 result = gdk_pixbuf_real_save (pixbuf, f, type,
860 option_keys, option_values,
865 g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
870 if (fclose (f) < 0) {
873 g_file_error_from_errno (errno),
874 _("Failed to close '%s' while writing image, all data may not have been saved: %s"),
875 filename, g_strerror (errno));