gdk-pixbuf-io.c 21.8 KB
Newer Older
1
/* GdkPixbuf library - Main loading interface.
Arturo Espinosa's avatar
Arturo Espinosa committed
2
 *
3 4 5 6 7 8
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Authors: Miguel de Icaza <miguel@gnu.org>
 *          Federico Mena-Quintero <federico@gimp.org>
 *
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10 11 12 13 14 15
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19 20 21
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Arturo Espinosa's avatar
Arturo Espinosa committed
22
 */
23

Arturo Espinosa's avatar
Arturo Espinosa committed
24
#include <config.h>
25
#include <string.h>
26
#include <glib.h>
27
#include <errno.h>
28
#include "gdk-pixbuf-private.h"
29
#include "gdk-pixbuf-io.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
30

31 32


33
static gboolean
34
pixbuf_check_png (guchar *buffer, int size)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
{
	if (size < 28)
		return FALSE;

	if (buffer [0] != 0x89 ||
	    buffer [1] != 'P' ||
	    buffer [2] != 'N' ||
	    buffer [3] != 'G' ||
	    buffer [4] != 0x0d ||
	    buffer [5] != 0x0a ||
	    buffer [6] != 0x1a ||
	    buffer [7] != 0x0a)
		return FALSE;

	return TRUE;
}

static gboolean
53
pixbuf_check_jpeg (guchar *buffer, int size)
54 55 56 57 58 59
{
	if (size < 10)
		return FALSE;

	if (buffer [0] != 0xff || buffer [1] != 0xd8)
		return FALSE;
60

61 62 63 64
	return TRUE;
}

static gboolean
65
pixbuf_check_tiff (guchar *buffer, int size)
66 67 68 69
{
	if (size < 10)
		return FALSE;

70 71 72
	if (buffer [0] == 'M' &&
	    buffer [1] == 'M' &&
	    buffer [2] == 0   &&
Mark Crichton's avatar
Mark Crichton committed
73
	    buffer [3] == 0x2a)
74 75
		return TRUE;

76 77 78
	if (buffer [0] == 'I' &&
	    buffer [1] == 'I' &&
	    buffer [2] == 0x2a &&
Mark Crichton's avatar
Mark Crichton committed
79
	    buffer [3] == 0)
80
		return TRUE;
81

82 83 84 85
	return FALSE;
}

static gboolean
86
pixbuf_check_gif (guchar *buffer, int size)
87 88 89
{
	if (size < 20)
		return FALSE;
90

91 92
	if (strncmp (buffer, "GIF8", 4) == 0)
		return TRUE;
93

94 95 96 97
	return FALSE;
}

static gboolean
98
pixbuf_check_xpm (guchar *buffer, int size)
99 100 101
{
	if (size < 20)
		return FALSE;
102

103 104
	if (strncmp (buffer, "/* XPM */", 9) == 0)
		return TRUE;
105

106 107 108 109
	return FALSE;
}

static gboolean
110
pixbuf_check_pnm (guchar *buffer, int size)
111 112 113 114
{
	if (size < 20)
		return FALSE;

115
	if (buffer [0] == 'P') {
116 117 118 119 120 121 122 123 124 125
		if (buffer [1] == '1' ||
		    buffer [1] == '2' ||
		    buffer [1] == '3' ||
		    buffer [1] == '4' ||
		    buffer [1] == '5' ||
		    buffer [1] == '6')
			return TRUE;
	}
	return FALSE;
}
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
static gboolean
pixbuf_check_sunras (guchar *buffer, int size)
{
	if (size < 32)
		return FALSE;

	if (buffer [0] != 0x59 ||
	    buffer [1] != 0xA6 ||
	    buffer [2] != 0x6A ||
	    buffer [3] != 0x95)
		return FALSE;

	return TRUE;
}

141 142 143 144 145 146 147 148 149
static gboolean
pixbuf_check_ico (guchar *buffer, int size)
{
	/* Note that this may cause false positives, but .ico's don't
	   have a magic number.*/
	if (size < 6)
		return FALSE;
	if (buffer [0] != 0x0 ||
	    buffer [1] != 0x0 ||
150
	    ((buffer [2] != 0x1)&&(buffer[2]!=0x2)) ||
151 152 153 154 155 156 157
	    buffer [3] != 0x0 ||
	    buffer [5] != 0x0 )
		return FALSE;

	return TRUE;
}

158 159 160 161 162 163 164 165 166 167 168 169 170

static gboolean
pixbuf_check_bmp (guchar *buffer, int size)
{
	if (size < 20)
		return FALSE;

	if (buffer [0] != 'B' || buffer [1] != 'M')
		return FALSE;

	return TRUE;
}

171 172 173 174 175 176 177 178 179 180 181 182
static gboolean
pixbuf_check_wbmp (guchar *buffer, int size)
{
  if(size < 10) /* at least */
    return FALSE;

  if(buffer[0] == '\0' /* We only handle type 0 wbmp's for now */)
    return TRUE;

  return FALSE;
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

static gboolean
pixbuf_check_xbm (guchar *buffer, int size)
{
	if (size < 20)
		return FALSE;

	if (buffer [0] != '#'
	    || buffer [1] != 'd'
	    || buffer [2] != 'e'
	    || buffer [3] != 'f'
	    || buffer [4] != 'i'
	    || buffer [5] != 'n'
	    || buffer [6] != 'e'
	    || buffer [7] != ' ')
		return FALSE;

	return TRUE;
}

203
static GdkPixbufModule file_formats [] = {
204 205 206 207
	{ "png",  pixbuf_check_png, NULL,  NULL, NULL, NULL, NULL, NULL, NULL, },
	{ "jpeg", pixbuf_check_jpeg, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
	{ "tiff", pixbuf_check_tiff, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
	{ "gif",  pixbuf_check_gif, NULL,  NULL, NULL, NULL, NULL, NULL, NULL },
208
#define XPM_FILE_FORMAT_INDEX 4
209 210 211 212 213 214
	{ "xpm",  pixbuf_check_xpm, NULL,  NULL, NULL, NULL, NULL, NULL, NULL },
	{ "pnm",  pixbuf_check_pnm, NULL,  NULL, NULL, NULL, NULL, NULL, NULL },
	{ "ras",  pixbuf_check_sunras, NULL,  NULL, NULL, NULL, NULL, NULL, NULL },
	{ "ico",  pixbuf_check_ico, NULL,  NULL, NULL, NULL, NULL, NULL, NULL },
	{ "bmp",  pixbuf_check_bmp, NULL,  NULL, NULL, NULL, NULL, NULL, NULL },
	{ "wbmp", pixbuf_check_wbmp, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
215
	{ "xbm",  pixbuf_check_xbm, NULL,  NULL, NULL, NULL, NULL, NULL, NULL },
216
	{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
Arturo Espinosa's avatar
Arturo Espinosa committed
217 218
};

219 220 221 222 223 224 225 226 227 228 229 230
#ifdef USE_GMODULE 
static gboolean
pixbuf_module_symbol (GModule *module, const char *module_name, const char *symbol_name, gpointer *symbol)
{
	char *full_symbol_name = g_strconcat ("gdk_pixbuf__", module_name, "_", symbol_name, NULL);
	gboolean return_value;

	return_value = g_module_symbol (module, full_symbol_name, symbol);
	g_free (full_symbol_name);
	
	return return_value;
}
231

Tor Lillqvist's avatar
Tor Lillqvist committed
232 233 234 235 236 237 238 239
#ifdef G_OS_WIN32

static char *
get_libdir (void)
{
  static char *libdir = NULL;

  if (libdir == NULL)
240 241 242 243 244
    libdir = g_win32_get_package_installation_subdirectory
      (GETTEXT_PACKAGE,
       g_strdup_printf ("gdk_pixbug-%d.%d.dll",
			GDK_PIXBUF_MAJOR, GDK_PIXBUF_MINOR),
       "loaders");
Tor Lillqvist's avatar
Tor Lillqvist committed
245 246 247 248 249 250 251 252

  return libdir;
}

#define PIXBUF_LIBDIR get_libdir ()

#endif

253 254 255
/* actually load the image handler - gdk_pixbuf_get_module only get a */
/* reference to the module to load, it doesn't actually load it       */
/* perhaps these actions should be combined in one function           */
Havoc Pennington's avatar
Havoc Pennington committed
256 257 258
gboolean
gdk_pixbuf_load_module (GdkPixbufModule *image_module,
                        GError         **error)
Arturo Espinosa's avatar
Arturo Espinosa committed
259
{
Mark Crichton's avatar
Mark Crichton committed
260
	char *module_name;
261 262
	char *path;
	GModule *module;
263
	gpointer sym;
264
	char *name;
265 266
        gboolean retval;
        char *dir;
267
	
Havoc Pennington's avatar
Havoc Pennington committed
268
        g_return_val_if_fail (image_module->module == NULL, FALSE);
269

270 271
	name = image_module->module_name;
	
272
	module_name = g_strconcat ("pixbufloader-", name, NULL);
273

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
        /* This would be an exploit in an suid binary using gdk-pixbuf,
         * but see http://www.gtk.org/setuid.html or the BugTraq
         * archives for why you should report this as a bug against
         * setuid apps using this library rather than the library
         * itself.
         */
        dir = g_getenv ("GDK_PIXBUF_MODULEDIR");

        if (dir == NULL || *dir == '\0') {
                
                path = g_module_build_path (PIXBUF_LIBDIR, module_name);
                module = g_module_open (path, G_MODULE_BIND_LAZY);
        } else {
                path = g_module_build_path (dir, module_name);
                module = g_module_open (path, G_MODULE_BIND_LAZY);                
        }        

        if (!module) {
                g_set_error (error,
                             GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_FAILED,
                             _("Unable to load image-loading module: %s: %s"),
                             path, g_module_error ());
                g_free (module_name);
298
                g_free (path);
299
                return FALSE;
300 301 302
        }

        g_free (module_name);
303

304 305 306 307 308 309 310 311 312 313 314 315 316 317
	image_module->module = module;        
        
        if (pixbuf_module_symbol (module, name, "fill_vtable", &sym)) {
                ModuleFillVtableFunc func = sym;
                (* func) (image_module);
                retval = TRUE;
        } else {
                g_set_error (error,
                             GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_FAILED,
                             _("Image-loading module %s does not export the proper interface; perhaps it's from a different GTK version?"),
                             path);
                retval = FALSE;
        }
318

319
        g_free (path);
Havoc Pennington's avatar
Havoc Pennington committed
320

321
        return retval;
Arturo Espinosa's avatar
Arturo Espinosa committed
322
}
323 324 325
#else

#define mname(type,fn) gdk_pixbuf__ ## type ## _image_ ##fn
326 327 328 329 330 331 332 333 334 335 336 337 338
#define m_fill_vtable(type) extern void mname(type,fill_vtable) (GdkPixbufModule *module)

m_fill_vtable (png);
m_fill_vtable (bmp);
m_fill_vtable (wbmp);
m_fill_vtable (gif);
m_fill_vtable (ico);
m_fill_vtable (jpeg);
m_fill_vtable (pnm);
m_fill_vtable (ras);
m_fill_vtable (tiff);
m_fill_vtable (xpm);
m_fill_vtable (xbm);
339

Havoc Pennington's avatar
Havoc Pennington committed
340 341 342
gboolean
gdk_pixbuf_load_module (GdkPixbufModule *image_module,
                        GError         **error)
343
{
344
        ModuleFillVtableFunc fill_vtable = NULL;
345
	image_module->module = (void *) 1;
346

347 348 349 350
        if (FALSE) {
                /* Ugly hack so we can use else if unconditionally below ;-) */
        }
        
351
#ifdef INCLUDE_png	
352 353
	else if (strcmp (image_module->module_name, "png") == 0){
                fill_vtable = mname (png, fill_vtable);
354
	}
355
#endif
356

357
#ifdef INCLUDE_bmp	
358 359
	else if (strcmp (image_module->module_name, "bmp") == 0){
                fill_vtable = mname (bmp, fill_vtable);
360
	}
361
#endif
362

363
#ifdef INCLUDE_wbmp
364 365
	else if (strcmp (image_module->module_name, "wbmp") == 0){
                fill_vtable = mname (wbmp, fill_vtable);
366
	}
367
#endif
368

369
#ifdef INCLUDE_gif
370 371
	else if (strcmp (image_module->module_name, "gif") == 0){
                fill_vtable = mname (gif, fill_vtable);
372
	}
373
#endif
374

375
#ifdef INCLUDE_ico
376 377
	else if (strcmp (image_module->module_name, "ico") == 0){
                fill_vtable = mname (ico, fill_vtable);
378
	}
379
#endif
380

381
#ifdef INCLUDE_jpeg
382 383
	else if (strcmp (image_module->module_name, "jpeg") == 0){
                fill_vtable = mname (jpeg, fill_vtable);
384
	}
385 386 387
#endif

#ifdef INCLUDE_pnm
388 389
	else if (strcmp (image_module->module_name, "pnm") == 0){
                fill_vtable = mname (pnm, fill_vtable);
390
	}
391 392 393
#endif

#ifdef INCLUDE_ras
394 395
	else if (strcmp (image_module->module_name, "ras") == 0){
                fill_vtable = mname (ras, fill_vtable);
396
	}
397 398 399
#endif

#ifdef INCLUDE_tiff
400 401
	else if (strcmp (image_module->module_name, "tiff") == 0){
                fill_vtable = mname (tiff, fill_vtable);
402
	}
403
#endif
Havoc Pennington's avatar
Havoc Pennington committed
404

405
#ifdef INCLUDE_xpm
406 407
	else if (strcmp (image_module->module_name, "xpm") == 0){
                fill_vtable = mname (xpm, fill_vtable);
408 409 410
	}
#endif

411 412 413
#ifdef INCLUDE_xbm
	else if (strcmp (image_module->module_name, "xbm") == 0){
                fill_vtable = mname (xbm, fill_vtable);
414 415 416
	}
#endif

417 418 419 420 421 422 423 424 425 426 427 428 429
        
        if (fill_vtable) {
                (* fill_vtable) (image_module);
                return TRUE;
        } else {
                g_set_error (error,
                             GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
                             _("Image type '%s' is not supported"),
                             image_module->module_name);

                return FALSE;
        }
430 431 432 433
}


#endif
Arturo Espinosa's avatar
Arturo Espinosa committed
434

435 436


437
GdkPixbufModule *
Havoc Pennington's avatar
Havoc Pennington committed
438 439
gdk_pixbuf_get_named_module (const char *name,
                             GError **error)
440 441 442 443 444 445 446 447
{
	int i;

	for (i = 0; file_formats [i].module_name; i++) {
		if (!strcmp(name, file_formats[i].module_name))
			return &(file_formats[i]);
	}

Havoc Pennington's avatar
Havoc Pennington committed
448 449 450 451 452 453
        g_set_error (error,
                     GDK_PIXBUF_ERROR,
                     GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
                     _("Image type '%s' is not supported"),
                     name);
        
454 455 456
	return NULL;
}

457
GdkPixbufModule *
Havoc Pennington's avatar
Havoc Pennington committed
458 459 460
gdk_pixbuf_get_module (guchar *buffer, guint size,
                       const gchar *filename,
                       GError **error)
461
{
462
	int i;
463 464 465 466 467

	for (i = 0; file_formats [i].module_name; i++) {
		if ((* file_formats [i].format_check) (buffer, size))
			return &(file_formats[i]);
	}
468

Havoc Pennington's avatar
Havoc Pennington committed
469 470 471 472 473 474 475 476 477 478 479 480 481
        if (filename)
                g_set_error (error,
                             GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
                             _("Couldn't recognize the image file format for file '%s'"),
                             filename);        
        else
                g_set_error (error,
                             GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
                             _("Unrecognized image file format"));

        
482 483 484
	return NULL;
}

Federico Mena Quintero's avatar
Federico Mena Quintero committed
485 486 487
/**
 * gdk_pixbuf_new_from_file:
 * @filename: Name of file to load.
Havoc Pennington's avatar
Havoc Pennington committed
488
 * @error: Return location for an error
489
 *
Federico Mena Quintero's avatar
Federico Mena Quintero committed
490
 * Creates a new pixbuf by loading an image from a file.  The file format is
Havoc Pennington's avatar
Havoc Pennington committed
491 492
 * detected automatically. If NULL is returned, then @error will be set.
 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
493 494 495 496 497
 *
 * Return value: A newly-created pixbuf with a reference count of 1, or NULL if
 * any of several error conditions occurred:  the file could not be opened,
 * there was no loader for the file's format, there was not enough memory to
 * allocate the image buffer, or the image file contained invalid data.
Federico Mena Quintero's avatar
Federico Mena Quintero committed
498
 **/
499
GdkPixbuf *
Havoc Pennington's avatar
Havoc Pennington committed
500 501
gdk_pixbuf_new_from_file (const char *filename,
                          GError    **error)
Arturo Espinosa's avatar
Arturo Espinosa committed
502
{
503
	GdkPixbuf *pixbuf;
504
	int size;
Arturo Espinosa's avatar
Arturo Espinosa committed
505
	FILE *f;
506
	guchar buffer [128];
507
	GdkPixbufModule *image_module;
Arturo Espinosa's avatar
Arturo Espinosa committed
508

509 510
	g_return_val_if_fail (filename != NULL, NULL);

Tor Lillqvist's avatar
Tor Lillqvist committed
511
	f = fopen (filename, "rb");
Havoc Pennington's avatar
Havoc Pennington committed
512 513 514 515 516 517
	if (!f) {
                g_set_error (error,
                             G_FILE_ERROR,
                             g_file_error_from_errno (errno),
                             _("Failed to open file '%s': %s"),
                             filename, g_strerror (errno));
Arturo Espinosa's avatar
Arturo Espinosa committed
518
		return NULL;
Havoc Pennington's avatar
Havoc Pennington committed
519
        }
520

521 522
	size = fread (&buffer, 1, sizeof (buffer), f);
	if (size == 0) {
Havoc Pennington's avatar
Havoc Pennington committed
523 524 525 526 527 528
                g_set_error (error,
                             GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                             _("Image file '%s' contains no data"),
                             filename);
                
529
		fclose (f);
Arturo Espinosa's avatar
Arturo Espinosa committed
530
		return NULL;
531
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
532

Havoc Pennington's avatar
Havoc Pennington committed
533 534 535 536 537
	image_module = gdk_pixbuf_get_module (buffer, size, filename, error);
        if (image_module == NULL) {
                fclose (f);
                return NULL;
        }
538

539
	if (image_module->module == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
540 541 542 543
		if (!gdk_pixbuf_load_module (image_module, error)) {
                        fclose (f);
                        return NULL;
                }
544

545
	if (image_module->load == NULL) {
Havoc Pennington's avatar
Havoc Pennington committed
546 547 548 549 550 551
                g_set_error (error,
                             GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
                             _("Don't know how to load the image in file '%s'"),
                             filename);
                
552 553
		fclose (f);
		return NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
554 555
	}

556
	fseek (f, 0, SEEK_SET);
Havoc Pennington's avatar
Havoc Pennington committed
557
	pixbuf = (* image_module->load) (f, error);
558
	fclose (f);
559

Havoc Pennington's avatar
Havoc Pennington committed
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
        if (pixbuf == NULL && error != NULL && *error == NULL) {
                /* I don't trust these crufty longjmp()'ing image libs
                 * to maintain proper error invariants, and I don't
                 * want user code to segfault as a result. We need to maintain
                 * the invariant that error gets set if NULL is returned.
                 */
                
                g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.", image_module->module_name);
                g_set_error (error,
                             GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_FAILED,
                             _("Failed to load image '%s': reason not known, probably a corrupt image file"),
                             filename);
                
        } else if (error != NULL && *error != NULL) {

          /* Add the filename to the error message */
          GError *e = *error;
          gchar *old;
          
          old = e->message;

          e->message = g_strdup_printf (_("Failed to load image '%s': %s"),
                                        filename, old);

          g_free (old);
        }
                
588
	return pixbuf;
Arturo Espinosa's avatar
Arturo Espinosa committed
589
}
590

591 592
/**
 * gdk_pixbuf_new_from_xpm_data:
Federico Mena Quintero's avatar
Federico Mena Quintero committed
593
 * @data: Pointer to inline XPM data.
594
 *
595 596
 * Creates a new pixbuf by parsing XPM data in memory.  This data is commonly
 * the result of including an XPM file into a program's C source.
597
 *
598 599
 * Return value: A newly-created pixbuf with a reference count of 1.
 **/
600
GdkPixbuf *
601
gdk_pixbuf_new_from_xpm_data (const char **data)
602
{
603 604
	GdkPixbuf *(* load_xpm_data) (const char **data);
	GdkPixbuf *pixbuf;
605

606
	if (file_formats[XPM_FILE_FORMAT_INDEX].module == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
607
		gdk_pixbuf_load_module (&file_formats[XPM_FILE_FORMAT_INDEX], NULL);
608

609 610
	if (file_formats[XPM_FILE_FORMAT_INDEX].module == NULL) {
		g_warning ("Can't find gdk-pixbuf module for parsing inline XPM data");
611
		return NULL;
612 613
	} else if (file_formats[XPM_FILE_FORMAT_INDEX].load_xpm_data == NULL) {
		g_warning ("gdk-pixbuf XPM module lacks XPM data capability");
614
		return NULL;
615 616
	} else
		load_xpm_data = file_formats[XPM_FILE_FORMAT_INDEX].load_xpm_data;
617

618 619
	pixbuf = (* load_xpm_data) (data);
	return pixbuf;
620
}
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

static void
collect_save_options (va_list   opts,
                      gchar  ***keys,
                      gchar  ***vals)
{
  gchar *key;
  gchar *val;
  gchar *next;
  gint count;

  count = 0;
  *keys = NULL;
  *vals = NULL;
  
  next = va_arg (opts, gchar*);
  while (next)
    {
      key = next;
      val = va_arg (opts, gchar*);

      ++count;

      /* woo, slow */
      *keys = g_realloc (*keys, sizeof(gchar*) * (count + 1));
      *vals = g_realloc (*vals, sizeof(gchar*) * (count + 1));
      
      (*keys)[count-1] = g_strdup (key);
      (*vals)[count-1] = g_strdup (val);

      (*keys)[count] = NULL;
      (*vals)[count] = NULL;
      
      next = va_arg (opts, gchar*);
    }
}

static gboolean
gdk_pixbuf_real_save (GdkPixbuf     *pixbuf, 
                      FILE          *filehandle, 
661
                      const char    *type, 
662 663 664 665 666
                      gchar        **keys,
                      gchar        **values,
                      GError       **error)
{
       GdkPixbufModule *image_module = NULL;       
Havoc Pennington's avatar
Havoc Pennington committed
667 668 669 670 671

       image_module = gdk_pixbuf_get_named_module (type, error);

       if (image_module == NULL)
               return FALSE;
672
       
Havoc Pennington's avatar
Havoc Pennington committed
673 674 675 676 677
       if (image_module->module == NULL)
               if (!gdk_pixbuf_load_module (image_module, error))
                       return FALSE;

       if (image_module->save == NULL) {
678 679
               g_set_error (error,
                            GDK_PIXBUF_ERROR,
Havoc Pennington's avatar
Havoc Pennington committed
680 681 682
                            GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
                            _("This build of gdk-pixbuf does not support saving the image format: %s"),
                            type);
683 684
               return FALSE;
       }
Havoc Pennington's avatar
Havoc Pennington committed
685
               
686 687 688 689 690 691 692 693 694 695
       return (* image_module->save) (filehandle, pixbuf,
                                      keys, values,
                                      error);
}

 
/**
 * gdk_pixbuf_save:
 * @pixbuf: pointer to GdkPixbuf.
 * @filename: Name of file to save.
696
 * @type: name of file format.
697 698 699
 * @error: return location for error, or NULL
 * @Varargs: list of key-value save options
 *
700
 * Saves pixbuf to a file in @type, which is currently "jpeg" or
701
 * "png".  If @error is set, FALSE will be returned. Possible errors include those
Havoc Pennington's avatar
Havoc Pennington committed
702
 * in the #GDK_PIXBUF_ERROR domain and those in the #G_FILE_ERROR domain.
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
 *
 * The variable argument list should be NULL-terminated; if not empty,
 * it should contain pairs of strings that modify the save
 * parameters. For example:
 *
 * <programlisting>
 * gdk_pixbuf_save (pixbuf, handle, "jpeg", &error,
 *                  "quality", "100", NULL);
 * </programlisting>
 *
 * The only save parameter that currently exists is the "quality" field
 * for JPEG images; its value should be in the range [0,100].
 *
 * Return value: whether an error was set
 **/

gboolean
gdk_pixbuf_save (GdkPixbuf  *pixbuf, 
                 const char *filename, 
722
                 const char *type, 
723 724 725 726 727 728 729 730 731 732 733 734 735 736
                 GError    **error,
                 ...)
{
        gchar **keys = NULL;
        gchar **values = NULL;
        va_list args;
        gboolean result;
        
        va_start (args, error);
        
        collect_save_options (args, &keys, &values);
        
        va_end (args);

737
        result = gdk_pixbuf_savev (pixbuf, filename, type,
738 739 740 741 742 743 744 745 746 747 748 749 750
                                   keys, values,
                                   error);

        g_strfreev (keys);
        g_strfreev (values);

        return result;
}

/**
 * gdk_pixbuf_savev:
 * @pixbuf: pointer to GdkPixbuf.
 * @filename: Name of file to save.
751
 * @type: name of file format.
752 753 754 755
 * @option_keys: name of options to set, NULL-terminated
 * @option_values: values for named options
 * @error: return location for error, or NULL
 *
756
 * Saves pixbuf to a file in @type, which is currently "jpeg" or "png".
757
 * If @error is set, FALSE will be returned. See gdk_pixbuf_save () for more
Havoc Pennington's avatar
Havoc Pennington committed
758
 * details.
759 760 761 762 763 764 765
 *
 * Return value: whether an error was set
 **/

gboolean
gdk_pixbuf_savev (GdkPixbuf  *pixbuf, 
                  const char *filename, 
766
                  const char *type,
767 768 769 770 771 772 773 774 775
                  char      **option_keys,
                  char      **option_values,
                  GError    **error)
{
        FILE *f = NULL;
        gboolean result;
        
       
        g_return_val_if_fail (filename != NULL, FALSE);
776
        g_return_val_if_fail (type != NULL, FALSE);
777
       
778
        f = fopen (filename, "wb");
779 780 781
        
        if (f == NULL) {
                g_set_error (error,
Havoc Pennington's avatar
Havoc Pennington committed
782 783
                             G_FILE_ERROR,
                             g_file_error_from_errno (errno),
784 785 786 787 788 789
                             _("Failed to open '%s' for writing: %s"),
                             filename, g_strerror (errno));
                return FALSE;
        }

       
790
       result = gdk_pixbuf_real_save (pixbuf, f, type,
791 792 793 794 795 796 797 798 799 800 801 802
                                      option_keys, option_values,
                                      error);
       
       
       if (!result) {
               g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
               fclose (f);
               return FALSE;
       }

       if (fclose (f) < 0) {
               g_set_error (error,
Havoc Pennington's avatar
Havoc Pennington committed
803 804
                            G_FILE_ERROR,
                            g_file_error_from_errno (errno),
805 806 807 808 809 810 811
                            _("Failed to close '%s' while writing image, all data may not have been saved: %s"),
                            filename, g_strerror (errno));
               return FALSE;
       }
       
       return TRUE;
}