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);
218 /* What would be the right place for this function? Also
219 * gtk needs this function (to find the gtkrc and themes).
220 * But it seems stupid for the gdk-pixbuf DLL to depend
221 * on the gtk DLL. Should it be in the gdk DLL? Or should we
222 * have a small static library at the top gtk+ level?
226 gtk_win32_get_installation_directory (void)
228 static gboolean been_here = FALSE;
229 static gchar gtk_installation_dir[200];
233 DWORD nbytes = sizeof (gtk_installation_dir);
236 return gtk_installation_dir;
240 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Software\\GNU\\GTk+", 0,
241 KEY_QUERY_VALUE, ®_key) != ERROR_SUCCESS
242 || RegQueryValueEx (reg_key, "InstallationDirectory", 0,
243 &type, gtk_installation_dir, &nbytes) != ERROR_SUCCESS
246 /* Uh oh. Use hard-coded %WinDir%\gtk+ value */
247 GetWindowsDirectory (win_dir, sizeof (win_dir));
248 sprintf (gtk_installation_dir, "%s\\gtk+", win_dir);
252 RegCloseKey (reg_key);
254 return gtk_installation_dir;
260 static char *libdir = NULL;
263 libdir = g_strdup_printf (gtk_win32_get_installation_directory (),
271 #define PIXBUF_LIBDIR get_libdir ()
275 /* actually load the image handler - gdk_pixbuf_get_module only get a */
276 /* reference to the module to load, it doesn't actually load it */
277 /* perhaps these actions should be combined in one function */
279 gdk_pixbuf_load_module (GdkPixbufModule *image_module,
289 g_return_val_if_fail (image_module->module == NULL, FALSE);
291 name = image_module->module_name;
293 module_name = g_strconcat ("pixbufloader-", name, NULL);
294 path = g_module_build_path (PIXBUF_LIBDIR, module_name);
296 module = g_module_open (path, G_MODULE_BIND_LAZY);
298 /* Debug feature, check in GDK_PIXBUF_MODULEDIR, or working directory */
299 char *dir = g_getenv ("GDK_PIXBUF_MODULEDIR");
304 path = g_module_build_path (dir, module_name);
305 module = g_module_open (path, G_MODULE_BIND_LAZY);
310 GDK_PIXBUF_ERROR_FAILED,
311 _("Unable to load image-loading module: %s: %s"),
312 path, g_module_error ());
313 g_free (module_name);
322 g_free (module_name);
324 image_module->module = module;
326 if (pixbuf_module_symbol (module, name, "image_load", &load_sym))
327 image_module->load = load_sym;
329 if (pixbuf_module_symbol (module, name, "image_load_xpm_data", &load_sym))
330 image_module->load_xpm_data = load_sym;
332 if (pixbuf_module_symbol (module, name, "image_begin_load", &load_sym))
333 image_module->begin_load = load_sym;
335 if (pixbuf_module_symbol (module, name, "image_stop_load", &load_sym))
336 image_module->stop_load = load_sym;
338 if (pixbuf_module_symbol (module, name, "image_load_increment", &load_sym))
339 image_module->load_increment = load_sym;
341 if (pixbuf_module_symbol (module, name, "image_load_animation", &load_sym))
342 image_module->load_animation = load_sym;
344 if (pixbuf_module_symbol (module, name, "image_save", &save_sym))
345 image_module->save = save_sym;
351 #define mname(type,fn) gdk_pixbuf__ ## type ## _image_ ##fn
352 #define m_load(type) extern GdkPixbuf * mname(type,load) (FILE *f);
353 #define m_load_xpm_data(type) extern GdkPixbuf * mname(type,load_xpm_data) (const char **data);
354 #define m_begin_load(type) \
355 extern gpointer mname(type,begin_load) (ModulePreparedNotifyFunc prepare_func, \
356 ModuleUpdatedNotifyFunc update_func, \
357 ModuleFrameDoneNotifyFunc frame_done_func,\
358 ModuleAnimationDoneNotifyFunc anim_done_func,\
360 #define m_stop_load(type) extern void mname(type,stop_load) (gpointer context);
361 #define m_load_increment(type) extern gboolean mname(type,load_increment) (gpointer context, const guchar *buf, guint size);
362 #define m_load_animation(type) extern GdkPixbufAnimation * mname(type,load_animation) (FILE *f);
363 #define m_save(type) \
364 extern gboolean mname(type,save) (FILE *f, \
373 m_load_increment (png);
379 m_load_increment (bmp);
384 m_load_increment (wbmp);
389 m_load_increment (gif);
391 m_load_animation (gif);
395 m_load_increment (ico);
400 m_load_increment (jpeg);
406 m_load_increment (pnm);
411 m_load_increment (ras);
416 m_load_increment (tiff);
420 m_load_xpm_data (xpm);
423 gdk_pixbuf_load_module (GdkPixbufModule *image_module,
426 image_module->module = (void *) 1;
428 if (strcmp (image_module->module_name, "png") == 0){
429 image_module->load = mname (png,load);
430 image_module->begin_load = mname (png,begin_load);
431 image_module->load_increment = mname (png,load_increment);
432 image_module->stop_load = mname (png,stop_load);
433 image_module->save = mname (png,save);
437 if (strcmp (image_module->module_name, "bmp") == 0){
438 image_module->load = mname (bmp,load);
439 image_module->begin_load = mname (bmp,begin_load);
440 image_module->load_increment = mname (bmp,load_increment);
441 image_module->stop_load = mname (bmp,stop_load);
445 if (strcmp (image_module->module_name, "wbmp") == 0){
446 image_module->load = mname (wbmp,load);
447 image_module->begin_load = mname (wbmp,begin_load);
448 image_module->load_increment = mname (wbmp,load_increment);
449 image_module->stop_load = mname (wbmp,stop_load);
453 if (strcmp (image_module->module_name, "gif") == 0){
454 image_module->load = mname (gif,load);
455 image_module->begin_load = mname (gif,begin_load);
456 image_module->load_increment = mname (gif,load_increment);
457 image_module->stop_load = mname (gif,stop_load);
458 image_module->load_animation = mname (gif,load_animation);
462 if (strcmp (image_module->module_name, "ico") == 0){
463 image_module->load = mname (ico,load);
464 image_module->begin_load = mname (ico,begin_load);
465 image_module->load_increment = mname (ico,load_increment);
466 image_module->stop_load = mname (ico,stop_load);
470 if (strcmp (image_module->module_name, "jpeg") == 0){
471 image_module->load = mname (jpeg,load);
472 image_module->begin_load = mname (jpeg,begin_load);
473 image_module->load_increment = mname (jpeg,load_increment);
474 image_module->stop_load = mname (jpeg,stop_load);
475 image_module->save = mname (jpeg,save);
478 if (strcmp (image_module->module_name, "pnm") == 0){
479 image_module->load = mname (pnm,load);
480 image_module->begin_load = mname (pnm,begin_load);
481 image_module->load_increment = mname (pnm,load_increment);
482 image_module->stop_load = mname (pnm,stop_load);
485 if (strcmp (image_module->module_name, "ras") == 0){
486 image_module->load = mname (ras,load);
487 image_module->begin_load = mname (ras,begin_load);
488 image_module->load_increment = mname (ras,load_increment);
489 image_module->stop_load = mname (ras,stop_load);
492 if (strcmp (image_module->module_name, "tiff") == 0){
493 image_module->load = mname (tiff,load);
494 image_module->begin_load = mname (tiff,begin_load);
495 image_module->load_increment = mname (tiff,load_increment);
496 image_module->stop_load = mname (tiff,stop_load);
499 if (strcmp (image_module->module_name, "xpm") == 0){
500 image_module->load = mname (xpm,load);
501 image_module->load_xpm_data = mname (xpm,load_xpm_data);
507 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
508 _("Image type '%s' is not supported"),
509 image_module->module_name);
520 gdk_pixbuf_get_named_module (const char *name,
525 for (i = 0; file_formats [i].module_name; i++) {
526 if (!strcmp(name, file_formats[i].module_name))
527 return &(file_formats[i]);
532 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
533 _("Image type '%s' is not supported"),
540 gdk_pixbuf_get_module (guchar *buffer, guint size,
541 const gchar *filename,
546 for (i = 0; file_formats [i].module_name; i++) {
547 if ((* file_formats [i].format_check) (buffer, size))
548 return &(file_formats[i]);
554 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
555 _("Couldn't recognize the image file format for file '%s'"),
560 GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
561 _("Unrecognized image file format"));
568 * gdk_pixbuf_new_from_file:
569 * @filename: Name of file to load.
570 * @error: Return location for an error
572 * Creates a new pixbuf by loading an image from a file. The file format is
573 * detected automatically. If NULL is returned, then @error will be set.
574 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
576 * Return value: A newly-created pixbuf with a reference count of 1, or NULL if
577 * any of several error conditions occurred: the file could not be opened,
578 * there was no loader for the file's format, there was not enough memory to
579 * allocate the image buffer, or the image file contained invalid data.
582 gdk_pixbuf_new_from_file (const char *filename,
589 GdkPixbufModule *image_module;
591 g_return_val_if_fail (filename != NULL, NULL);
593 f = fopen (filename, "rb");
597 g_file_error_from_errno (errno),
598 _("Failed to open file '%s': %s"),
599 filename, g_strerror (errno));
603 size = fread (&buffer, 1, sizeof (buffer), f);
607 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
608 _("Image file '%s' contains no data"),
615 image_module = gdk_pixbuf_get_module (buffer, size, filename, error);
616 if (image_module == NULL) {
621 if (image_module->module == NULL)
622 if (!gdk_pixbuf_load_module (image_module, error)) {
627 if (image_module->load == NULL) {
630 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
631 _("Don't know how to load the image in file '%s'"),
638 fseek (f, 0, SEEK_SET);
639 pixbuf = (* image_module->load) (f, error);
642 if (pixbuf == NULL && error != NULL && *error == NULL) {
643 /* I don't trust these crufty longjmp()'ing image libs
644 * to maintain proper error invariants, and I don't
645 * want user code to segfault as a result. We need to maintain
646 * the invariant that error gets set if NULL is returned.
649 g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.", image_module->module_name);
652 GDK_PIXBUF_ERROR_FAILED,
653 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
656 } else if (error != NULL && *error != NULL) {
658 /* Add the filename to the error message */
664 e->message = g_strdup_printf (_("Failed to load image '%s': %s"),
674 * gdk_pixbuf_new_from_xpm_data:
675 * @data: Pointer to inline XPM data.
677 * Creates a new pixbuf by parsing XPM data in memory. This data is commonly
678 * the result of including an XPM file into a program's C source.
680 * Return value: A newly-created pixbuf with a reference count of 1.
683 gdk_pixbuf_new_from_xpm_data (const char **data)
685 GdkPixbuf *(* load_xpm_data) (const char **data);
688 if (file_formats[XPM_FILE_FORMAT_INDEX].module == NULL)
689 gdk_pixbuf_load_module (&file_formats[XPM_FILE_FORMAT_INDEX], NULL);
691 if (file_formats[XPM_FILE_FORMAT_INDEX].module == NULL) {
692 g_warning ("Can't find gdk-pixbuf module for parsing inline XPM data");
694 } else if (file_formats[XPM_FILE_FORMAT_INDEX].load_xpm_data == NULL) {
695 g_warning ("gdk-pixbuf XPM module lacks XPM data capability");
698 load_xpm_data = file_formats[XPM_FILE_FORMAT_INDEX].load_xpm_data;
700 pixbuf = (* load_xpm_data) (data);
705 collect_save_options (va_list opts,
718 next = va_arg (opts, gchar*);
722 val = va_arg (opts, gchar*);
727 *keys = g_realloc (*keys, sizeof(gchar*) * (count + 1));
728 *vals = g_realloc (*vals, sizeof(gchar*) * (count + 1));
730 (*keys)[count-1] = g_strdup (key);
731 (*vals)[count-1] = g_strdup (val);
733 (*keys)[count] = NULL;
734 (*vals)[count] = NULL;
736 next = va_arg (opts, gchar*);
741 gdk_pixbuf_real_save (GdkPixbuf *pixbuf,
748 GdkPixbufModule *image_module = NULL;
750 image_module = gdk_pixbuf_get_named_module (type, error);
752 if (image_module == NULL)
755 if (image_module->module == NULL)
756 if (!gdk_pixbuf_load_module (image_module, error))
759 if (image_module->save == NULL) {
762 GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
763 _("This build of gdk-pixbuf does not support saving the image format: %s"),
768 return (* image_module->save) (filehandle, pixbuf,
776 * @pixbuf: pointer to GdkPixbuf.
777 * @filename: Name of file to save.
778 * @type: name of file format.
779 * @error: return location for error, or NULL
780 * @Varargs: list of key-value save options
782 * Saves pixbuf to a file in @type, which is currently "jpeg" or
783 * "png". If @error is set, FALSE will be returned. Possible errors include those
784 * in the #GDK_PIXBUF_ERROR domain and those in the #G_FILE_ERROR domain.
786 * The variable argument list should be NULL-terminated; if not empty,
787 * it should contain pairs of strings that modify the save
788 * parameters. For example:
791 * gdk_pixbuf_save (pixbuf, handle, "jpeg", &error,
792 * "quality", "100", NULL);
795 * The only save parameter that currently exists is the "quality" field
796 * for JPEG images; its value should be in the range [0,100].
798 * Return value: whether an error was set
802 gdk_pixbuf_save (GdkPixbuf *pixbuf,
803 const char *filename,
809 gchar **values = NULL;
813 va_start (args, error);
815 collect_save_options (args, &keys, &values);
819 result = gdk_pixbuf_savev (pixbuf, filename, type,
831 * @pixbuf: pointer to GdkPixbuf.
832 * @filename: Name of file to save.
833 * @type: name of file format.
834 * @option_keys: name of options to set, NULL-terminated
835 * @option_values: values for named options
836 * @error: return location for error, or NULL
838 * Saves pixbuf to a file in @type, which is currently "jpeg" or "png".
839 * If @error is set, FALSE will be returned. See gdk_pixbuf_save () for more
842 * Return value: whether an error was set
846 gdk_pixbuf_savev (GdkPixbuf *pixbuf,
847 const char *filename,
850 char **option_values,
857 g_return_val_if_fail (filename != NULL, FALSE);
858 g_return_val_if_fail (type != NULL, FALSE);
860 f = fopen (filename, "w");
865 g_file_error_from_errno (errno),
866 _("Failed to open '%s' for writing: %s"),
867 filename, g_strerror (errno));
872 result = gdk_pixbuf_real_save (pixbuf, f, type,
873 option_keys, option_values,
878 g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
883 if (fclose (f) < 0) {
886 g_file_error_from_errno (errno),
887 _("Failed to close '%s' while writing image, all data may not have been saved: %s"),
888 filename, g_strerror (errno));