gdk-pixbuf-io.c 23.5 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 34 35 36
#ifdef G_OS_WIN32
#define STRICT
#include <windows.h>
#undef STRICT
#endif

37 38


39
static gboolean
40
pixbuf_check_png (guchar *buffer, int size)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
{
	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
59
pixbuf_check_jpeg (guchar *buffer, int size)
60 61 62 63 64 65
{
	if (size < 10)
		return FALSE;

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

67 68 69 70
	return TRUE;
}

static gboolean
71
pixbuf_check_tiff (guchar *buffer, int size)
72 73 74 75
{
	if (size < 10)
		return FALSE;

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

82 83 84
	if (buffer [0] == 'I' &&
	    buffer [1] == 'I' &&
	    buffer [2] == 0x2a &&
Mark Crichton's avatar
Mark Crichton committed
85
	    buffer [3] == 0)
86
		return TRUE;
87

88 89 90 91
	return FALSE;
}

static gboolean
92
pixbuf_check_gif (guchar *buffer, int size)
93 94 95
{
	if (size < 20)
		return FALSE;
96

97 98
	if (strncmp (buffer, "GIF8", 4) == 0)
		return TRUE;
99

100 101 102 103
	return FALSE;
}

static gboolean
104
pixbuf_check_xpm (guchar *buffer, int size)
105 106 107
{
	if (size < 20)
		return FALSE;
108

109 110
	if (strncmp (buffer, "/* XPM */", 9) == 0)
		return TRUE;
111

112 113 114 115
	return FALSE;
}

static gboolean
116
pixbuf_check_pnm (guchar *buffer, int size)
117 118 119 120
{
	if (size < 20)
		return FALSE;

121
	if (buffer [0] == 'P') {
122 123 124 125 126 127 128 129 130 131
		if (buffer [1] == '1' ||
		    buffer [1] == '2' ||
		    buffer [1] == '3' ||
		    buffer [1] == '4' ||
		    buffer [1] == '5' ||
		    buffer [1] == '6')
			return TRUE;
	}
	return FALSE;
}
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
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;
}

147 148 149 150 151 152 153 154 155
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 ||
156
	    ((buffer [2] != 0x1)&&(buffer[2]!=0x2)) ||
157 158 159 160 161 162 163
	    buffer [3] != 0x0 ||
	    buffer [5] != 0x0 )
		return FALSE;

	return TRUE;
}

164 165 166 167 168 169 170 171 172 173 174 175 176

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;
}

177 178 179 180 181 182 183 184 185 186 187 188
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;
}

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

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;
}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
static gboolean
pixbuf_check_tga (guchar *buffer, int size)
{
        if (size < 18)
                return FALSE;
        /* buffer[1] is a boolean telling if in the file a colormap is
           present, while buffer[2] is the byte which specifies the image
           type. (GrayScale/PseudoColor/TrueColor/RLE) */
        if ((buffer[2] == 1) || (buffer[2] == 9)) {
                if (buffer[1] != 1)
                        return FALSE;
        } else {
                if (buffer[1] != 0)
			return FALSE;
        }
        return TRUE;
}

Federico Mena Quintero's avatar
Federico Mena Quintero committed
227
static GdkPixbufModule file_formats [] = {
228 229 230 231
	{ "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 },
232
#define XPM_FILE_FORMAT_INDEX 4
233 234 235 236
	{ "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 },
	{ "bmp",  pixbuf_check_bmp, NULL,  NULL, NULL, NULL, NULL, NULL, NULL },
237
	{ "xbm",  pixbuf_check_xbm, NULL,  NULL, NULL, NULL, NULL, NULL, NULL },
238 239 240
	{ "tga", pixbuf_check_tga, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
	/* Moved at the bottom, because it causes false positives against many
	   of my TGA files. */
241 242
	{ "ico",  pixbuf_check_ico, NULL,  NULL, NULL, NULL, NULL, NULL, NULL },	
	{ "wbmp", pixbuf_check_wbmp, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
243
	{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
Arturo Espinosa's avatar
Arturo Espinosa committed
244 245
};

Federico Mena Quintero's avatar
Federico Mena Quintero committed
246 247 248 249 250 251 252 253 254 255 256 257
#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;
}
258

Tor Lillqvist's avatar
Tor Lillqvist committed
259 260
#ifdef G_OS_WIN32

261 262 263 264
/* DllMain function needed to tuck away the gdk-pixbuf DLL name */

G_WIN32_DLLMAIN_FOR_DLL_NAME (static, dll_name)

Tor Lillqvist's avatar
Tor Lillqvist committed
265 266 267 268 269 270
static char *
get_libdir (void)
{
  static char *libdir = NULL;

  if (libdir == NULL)
Tor Lillqvist's avatar
Tor Lillqvist committed
271
    libdir = g_win32_get_package_installation_subdirectory
272
      (GETTEXT_PACKAGE, dll_name, "lib\\gtk-2.0\\" GTK_VERSION "\\loaders");
Tor Lillqvist's avatar
Tor Lillqvist committed
273 274 275 276

  return libdir;
}

277
#undef PIXBUF_LIBDIR
Tor Lillqvist's avatar
Tor Lillqvist committed
278 279 280 281
#define PIXBUF_LIBDIR get_libdir ()

#endif

282 283 284
/* 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
285
gboolean
286 287
_gdk_pixbuf_load_module (GdkPixbufModule *image_module,
                         GError         **error)
Arturo Espinosa's avatar
Arturo Espinosa committed
288
{
Mark Crichton's avatar
Mark Crichton committed
289
	char *module_name;
290 291
	char *path;
	GModule *module;
292
	gpointer sym;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
293
	char *name;
294
        gboolean retval;
295
        const char *dir;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
296
	
Havoc Pennington's avatar
Havoc Pennington committed
297
        g_return_val_if_fail (image_module->module == NULL, FALSE);
298

Federico Mena Quintero's avatar
Federico Mena Quintero committed
299 300
	name = image_module->module_name;
	
301
	module_name = g_strconcat ("pixbufloader-", name, NULL);
302

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        /* 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);
327
                g_free (path);
328
                return FALSE;
329 330 331
        }

        g_free (module_name);
332

333 334 335 336 337 338 339 340 341 342 343 344 345 346
	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;
        }
347

348
        g_free (path);
Havoc Pennington's avatar
Havoc Pennington committed
349

350
        return retval;
Arturo Espinosa's avatar
Arturo Espinosa committed
351
}
Federico Mena Quintero's avatar
Federico Mena Quintero committed
352 353
#else

Havoc Pennington's avatar
Havoc Pennington committed
354
#define mname(type,fn) gdk_pixbuf__ ## type ## _ ##fn
355 356 357 358 359 360 361 362 363 364 365 366 367
#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);
368
m_fill_vtable (tga);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
369

Havoc Pennington's avatar
Havoc Pennington committed
370
gboolean
371 372
_gdk_pixbuf_load_module (GdkPixbufModule *image_module,
                         GError         **error)
Federico Mena Quintero's avatar
Federico Mena Quintero committed
373
{
374
        ModuleFillVtableFunc fill_vtable = NULL;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
375
	image_module->module = (void *) 1;
376

377 378 379 380
        if (FALSE) {
                /* Ugly hack so we can use else if unconditionally below ;-) */
        }
        
381
#ifdef INCLUDE_png	
382 383
	else if (strcmp (image_module->module_name, "png") == 0){
                fill_vtable = mname (png, fill_vtable);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
384
	}
385
#endif
Federico Mena Quintero's avatar
Federico Mena Quintero committed
386

387
#ifdef INCLUDE_bmp	
388 389
	else if (strcmp (image_module->module_name, "bmp") == 0){
                fill_vtable = mname (bmp, fill_vtable);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
390
	}
391
#endif
Federico Mena Quintero's avatar
Federico Mena Quintero committed
392

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

399
#ifdef INCLUDE_gif
400 401
	else if (strcmp (image_module->module_name, "gif") == 0){
                fill_vtable = mname (gif, fill_vtable);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
402
	}
403
#endif
Federico Mena Quintero's avatar
Federico Mena Quintero committed
404

405
#ifdef INCLUDE_ico
406 407
	else if (strcmp (image_module->module_name, "ico") == 0){
                fill_vtable = mname (ico, fill_vtable);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
408
	}
409
#endif
Federico Mena Quintero's avatar
Federico Mena Quintero committed
410

411
#ifdef INCLUDE_jpeg
412 413
	else if (strcmp (image_module->module_name, "jpeg") == 0){
                fill_vtable = mname (jpeg, fill_vtable);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
414
	}
415 416 417
#endif

#ifdef INCLUDE_pnm
418 419
	else if (strcmp (image_module->module_name, "pnm") == 0){
                fill_vtable = mname (pnm, fill_vtable);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
420
	}
421 422 423
#endif

#ifdef INCLUDE_ras
424 425
	else if (strcmp (image_module->module_name, "ras") == 0){
                fill_vtable = mname (ras, fill_vtable);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
426
	}
427 428 429
#endif

#ifdef INCLUDE_tiff
430 431
	else if (strcmp (image_module->module_name, "tiff") == 0){
                fill_vtable = mname (tiff, fill_vtable);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
432
	}
433
#endif
Havoc Pennington's avatar
Havoc Pennington committed
434

435
#ifdef INCLUDE_xpm
436 437
	else if (strcmp (image_module->module_name, "xpm") == 0){
                fill_vtable = mname (xpm, fill_vtable);
438 439 440
	}
#endif

441 442 443
#ifdef INCLUDE_xbm
	else if (strcmp (image_module->module_name, "xbm") == 0){
                fill_vtable = mname (xbm, fill_vtable);
444 445 446
	}
#endif

447 448 449 450 451
#ifdef INCLUDE_tga
	else if (strcmp (image_module->module_name, "tga") == 0){
		fill_vtable = mname (tga, fill_vtable);
	}
#endif
452 453 454 455 456 457 458 459 460 461 462 463 464
        
        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;
        }
Federico Mena Quintero's avatar
Federico Mena Quintero committed
465 466 467 468
}


#endif
Arturo Espinosa's avatar
Arturo Espinosa committed
469

470 471


472
GdkPixbufModule *
473 474
_gdk_pixbuf_get_named_module (const char *name,
                              GError **error)
475 476 477 478 479 480 481 482
{
	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
483 484 485 486 487 488
        g_set_error (error,
                     GDK_PIXBUF_ERROR,
                     GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
                     _("Image type '%s' is not supported"),
                     name);
        
489 490 491
	return NULL;
}

492
GdkPixbufModule *
493 494 495
_gdk_pixbuf_get_module (guchar *buffer, guint size,
                        const gchar *filename,
                        GError **error)
496
{
497
	int i;
498 499 500 501 502

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

Havoc Pennington's avatar
Havoc Pennington committed
504 505 506 507 508 509 510 511 512 513 514 515 516
        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"));

        
517 518 519
	return NULL;
}

Federico Mena Quintero's avatar
Federico Mena Quintero committed
520 521 522
/**
 * gdk_pixbuf_new_from_file:
 * @filename: Name of file to load.
Havoc Pennington's avatar
Havoc Pennington committed
523
 * @error: Return location for an error
Federico Mena Quintero's avatar
Federico Mena Quintero committed
524
 *
Federico Mena Quintero's avatar
Federico Mena Quintero committed
525
 * Creates a new pixbuf by loading an image from a file.  The file format is
Matthias Clasen's avatar
Matthias Clasen committed
526
 * detected automatically. If %NULL is returned, then @error will be set.
Havoc Pennington's avatar
Havoc Pennington committed
527
 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
Federico Mena Quintero's avatar
Federico Mena Quintero committed
528
 *
Matthias Clasen's avatar
Matthias Clasen committed
529
 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
Federico Mena Quintero's avatar
Federico Mena Quintero committed
530 531 532
 * 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
533
 **/
534
GdkPixbuf *
Havoc Pennington's avatar
Havoc Pennington committed
535 536
gdk_pixbuf_new_from_file (const char *filename,
                          GError    **error)
Arturo Espinosa's avatar
Arturo Espinosa committed
537
{
538
	GdkPixbuf *pixbuf;
539
	int size;
Arturo Espinosa's avatar
Arturo Espinosa committed
540
	FILE *f;
541
	guchar buffer [128];
542
	GdkPixbufModule *image_module;
Arturo Espinosa's avatar
Arturo Espinosa committed
543

544 545
	g_return_val_if_fail (filename != NULL, NULL);

Tor Lillqvist's avatar
Tor Lillqvist committed
546
	f = fopen (filename, "rb");
Havoc Pennington's avatar
Havoc Pennington committed
547 548 549 550 551 552
	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
553
		return NULL;
Havoc Pennington's avatar
Havoc Pennington committed
554
        }
555

556 557
	size = fread (&buffer, 1, sizeof (buffer), f);
	if (size == 0) {
Havoc Pennington's avatar
Havoc Pennington committed
558 559 560 561 562 563
                g_set_error (error,
                             GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                             _("Image file '%s' contains no data"),
                             filename);
                
564
		fclose (f);
Arturo Espinosa's avatar
Arturo Espinosa committed
565
		return NULL;
566
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
567

568
	image_module = _gdk_pixbuf_get_module (buffer, size, filename, error);
Havoc Pennington's avatar
Havoc Pennington committed
569 570 571 572
        if (image_module == NULL) {
                fclose (f);
                return NULL;
        }
573

574
	if (image_module->module == NULL)
575
                if (!_gdk_pixbuf_load_module (image_module, error)) {
Havoc Pennington's avatar
Havoc Pennington committed
576 577 578
                        fclose (f);
                        return NULL;
                }
579

580
	if (image_module->load == NULL) {
Havoc Pennington's avatar
Havoc Pennington committed
581 582 583 584 585 586
                g_set_error (error,
                             GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
                             _("Don't know how to load the image in file '%s'"),
                             filename);
                
587 588
		fclose (f);
		return NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
589 590
	}

591
	fseek (f, 0, SEEK_SET);
Havoc Pennington's avatar
Havoc Pennington committed
592
	pixbuf = (* image_module->load) (f, error);
593
	fclose (f);
594

Havoc Pennington's avatar
Havoc Pennington committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
        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);
        }
                
623
	return pixbuf;
Arturo Espinosa's avatar
Arturo Espinosa committed
624
}
625

Federico Mena Quintero's avatar
Federico Mena Quintero committed
626 627
/**
 * gdk_pixbuf_new_from_xpm_data:
Federico Mena Quintero's avatar
Federico Mena Quintero committed
628
 * @data: Pointer to inline XPM data.
629
 *
Federico Mena Quintero's avatar
Federico Mena Quintero committed
630 631
 * 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.
632
 *
Federico Mena Quintero's avatar
Federico Mena Quintero committed
633 634
 * Return value: A newly-created pixbuf with a reference count of 1.
 **/
635
GdkPixbuf *
636
gdk_pixbuf_new_from_xpm_data (const char **data)
637
{
638 639
	GdkPixbuf *(* load_xpm_data) (const char **data);
	GdkPixbuf *pixbuf;
Havoc Pennington's avatar
Havoc Pennington committed
640
        GError *error = NULL;
641

Havoc Pennington's avatar
Havoc Pennington committed
642
	if (file_formats[XPM_FILE_FORMAT_INDEX].module == NULL) {
643
                if (!_gdk_pixbuf_load_module (&file_formats[XPM_FILE_FORMAT_INDEX], &error)) {
Havoc Pennington's avatar
Havoc Pennington committed
644 645 646 647 648 649
                        g_warning ("Error loading XPM image loader: %s", error->message);
                        g_error_free (error);
                        return FALSE;
                }
        }
          
650 651
	if (file_formats[XPM_FILE_FORMAT_INDEX].module == NULL) {
		g_warning ("Can't find gdk-pixbuf module for parsing inline XPM data");
652
		return NULL;
653 654
	} else if (file_formats[XPM_FILE_FORMAT_INDEX].load_xpm_data == NULL) {
		g_warning ("gdk-pixbuf XPM module lacks XPM data capability");
655
		return NULL;
656 657
	} else
		load_xpm_data = file_formats[XPM_FILE_FORMAT_INDEX].load_xpm_data;
658

659 660
	pixbuf = (* load_xpm_data) (data);
	return pixbuf;
661
}
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701

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, 
702
                      const char    *type, 
703 704 705 706 707
                      gchar        **keys,
                      gchar        **values,
                      GError       **error)
{
       GdkPixbufModule *image_module = NULL;       
Havoc Pennington's avatar
Havoc Pennington committed
708

709
       image_module = _gdk_pixbuf_get_named_module (type, error);
Havoc Pennington's avatar
Havoc Pennington committed
710 711 712

       if (image_module == NULL)
               return FALSE;
713
       
Havoc Pennington's avatar
Havoc Pennington committed
714
       if (image_module->module == NULL)
715
               if (!_gdk_pixbuf_load_module (image_module, error))
Havoc Pennington's avatar
Havoc Pennington committed
716 717 718
                       return FALSE;

       if (image_module->save == NULL) {
719 720
               g_set_error (error,
                            GDK_PIXBUF_ERROR,
Havoc Pennington's avatar
Havoc Pennington committed
721 722 723
                            GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
                            _("This build of gdk-pixbuf does not support saving the image format: %s"),
                            type);
724 725
               return FALSE;
       }
Havoc Pennington's avatar
Havoc Pennington committed
726
               
727 728 729 730 731 732 733 734 735 736
       return (* image_module->save) (filehandle, pixbuf,
                                      keys, values,
                                      error);
}

 
/**
 * gdk_pixbuf_save:
 * @pixbuf: pointer to GdkPixbuf.
 * @filename: Name of file to save.
737
 * @type: name of file format.
738 739 740
 * @error: return location for error, or NULL
 * @Varargs: list of key-value save options
 *
741
 * Saves pixbuf to a file in @type, which is currently "jpeg" or
Matthias Clasen's avatar
Matthias Clasen committed
742
 * "png".  If @error is set, %FALSE will be returned. Possible errors include 
743
 * those in the #GDK_PIXBUF_ERROR domain and those in the #G_FILE_ERROR domain.
744
 *
Matthias Clasen's avatar
Matthias Clasen committed
745
 * The variable argument list should be %NULL-terminated; if not empty,
746 747
 * it should contain pairs of strings that modify the save
 * parameters. For example:
Matthias Clasen's avatar
Matthias Clasen committed
748
 * <informalexample><programlisting>
749 750
 * gdk_pixbuf_save (pixbuf, handle, "jpeg", &error,
 *                  "quality", "100", NULL);
Matthias Clasen's avatar
Matthias Clasen committed
751
 * </programlisting></informalexample>
752
 *
753 754 755 756 757 758 759
 * Currently only few parameters exist. JPEG images can be saved with a 
 * "quality" parameter; its value should be in the range [0,100]. 
 * Text chunks can be attached to PNG images by specifying parameters of
 * the form "tEXt::key", where key is an ASCII string of length 1-79.
 * The values are UTF-8 encoded strings. Note however that PNG text
 * chunks are stored in ISO-8859-1 encoding, so you can only set texts
 * that can be represented in this encoding.
760 761 762 763 764 765 766
 *
 * Return value: whether an error was set
 **/

gboolean
gdk_pixbuf_save (GdkPixbuf  *pixbuf, 
                 const char *filename, 
767
                 const char *type, 
768 769 770 771 772 773 774 775 776 777 778 779 780 781
                 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);

782
        result = gdk_pixbuf_savev (pixbuf, filename, type,
783 784 785 786 787 788 789 790 791 792 793 794 795
                                   keys, values,
                                   error);

        g_strfreev (keys);
        g_strfreev (values);

        return result;
}

/**
 * gdk_pixbuf_savev:
 * @pixbuf: pointer to GdkPixbuf.
 * @filename: Name of file to save.
796
 * @type: name of file format.
Matthias Clasen's avatar
Matthias Clasen committed
797
 * @option_keys: name of options to set, %NULL-terminated
798
 * @option_values: values for named options
Matthias Clasen's avatar
Matthias Clasen committed
799
 * @error: return location for error, or %NULL
800
 *
801
 * Saves pixbuf to a file in @type, which is currently "jpeg" or "png".
Matthias Clasen's avatar
Matthias Clasen committed
802
 * If @error is set, %FALSE will be returned. See gdk_pixbuf_save () for more
Havoc Pennington's avatar
Havoc Pennington committed
803
 * details.
804 805 806 807 808 809 810
 *
 * Return value: whether an error was set
 **/

gboolean
gdk_pixbuf_savev (GdkPixbuf  *pixbuf, 
                  const char *filename, 
811
                  const char *type,
812 813 814 815 816 817 818 819 820
                  char      **option_keys,
                  char      **option_values,
                  GError    **error)
{
        FILE *f = NULL;
        gboolean result;
        
       
        g_return_val_if_fail (filename != NULL, FALSE);
821
        g_return_val_if_fail (type != NULL, FALSE);
822
       
823
        f = fopen (filename, "wb");
824 825 826
        
        if (f == NULL) {
                g_set_error (error,
Havoc Pennington's avatar
Havoc Pennington committed
827 828
                             G_FILE_ERROR,
                             g_file_error_from_errno (errno),
829 830 831 832 833 834
                             _("Failed to open '%s' for writing: %s"),
                             filename, g_strerror (errno));
                return FALSE;
        }

       
835
       result = gdk_pixbuf_real_save (pixbuf, f, type,
836 837 838 839 840 841 842 843 844 845 846 847
                                      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
848 849
                            G_FILE_ERROR,
                            g_file_error_from_errno (errno),
850 851 852 853 854 855 856
                            _("Failed to close '%s' while writing image, all data may not have been saved: %s"),
                            filename, g_strerror (errno));
               return FALSE;
       }
       
       return TRUE;
}