~ubuntu-branches/ubuntu/oneiric/gdk-pixbuf/oneiric-201105060911

« back to all changes in this revision

Viewing changes to .pc/041_ia32-libs.patch/gdk-pixbuf/gdk-pixbuf-io.c

  • Committer: Steve Langasek
  • Date: 2011-05-06 06:07:23 UTC
  • mfrom: (25.1.7 multiarch)
  • Revision ID: steve.langasek@linaro.org-20110506060723-ltaxw4r3hutx1ape
merge multiarch support

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: C; c-file-style: "linux" -*- */
2
 
/* GdkPixbuf library - Main loading interface.
3
 
 *
4
 
 * Copyright (C) 1999 The Free Software Foundation
5
 
 *
6
 
 * Authors: Miguel de Icaza <miguel@gnu.org>
7
 
 *          Federico Mena-Quintero <federico@gimp.org>
8
 
 *
9
 
 * This library is free software; you can redistribute it and/or
10
 
 * modify it under the terms of the GNU Lesser General Public
11
 
 * License as published by the Free Software Foundation; either
12
 
 * version 2 of the License, or (at your option) any later version.
13
 
 *
14
 
 * This library is distributed in the hope that it will be useful,
15
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 
 * Lesser General Public License for more details.
18
 
 *
19
 
 * You should have received a copy of the GNU Lesser General Public
20
 
 * License along with this library; if not, write to the
21
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22
 
 * Boston, MA 02111-1307, USA.
23
 
 */
24
 
 
25
 
#include "config.h"
26
 
 
27
 
#include <stdlib.h>
28
 
#include <stdio.h>
29
 
#include <string.h>
30
 
#include <errno.h>
31
 
#ifdef HAVE_UNISTD_H
32
 
#include <unistd.h>
33
 
#endif
34
 
 
35
 
#include <glib.h>
36
 
#include <gio/gio.h>
37
 
 
38
 
#include "gdk-pixbuf-private.h"
39
 
#include "gdk-pixbuf-loader.h"
40
 
 
41
 
#include <glib/gstdio.h>
42
 
 
43
 
#ifdef G_OS_WIN32
44
 
#define STRICT
45
 
#include <windows.h>
46
 
#undef STRICT
47
 
#endif
48
 
 
49
 
#define SNIFF_BUFFER_SIZE 4096
50
 
#define LOAD_BUFFER_SIZE 65536
51
 
 
52
 
/**
53
 
 * SECTION:file-loading
54
 
 * @Short_description: Loading a pixbuf from a file.
55
 
 * @Title: File Loading
56
 
 * @See_also: #GdkPixbufLoader.
57
 
 * 
58
 
 * The &gdk-pixbuf; library provides a simple mechanism for loading
59
 
 * an image from a file in synchronous fashion.  This means that the
60
 
 * library takes control of the application while the file is being
61
 
 * loaded; from the user's point of view, the application will block
62
 
 * until the image is done loading.
63
 
 * 
64
 
 * 
65
 
 * This interface can be used by applications in which blocking is
66
 
 * acceptable while an image is being loaded.  It can also be used to
67
 
 * load small images in general.  Applications that need progressive
68
 
 * loading can use the #GdkPixbufLoader functionality instead.
69
 
 */
70
 
 
71
 
/**
72
 
 * SECTION:file-saving
73
 
 * @Short_description: Saving a pixbuf to a file.
74
 
 * @Title: File saving
75
 
 * 
76
 
 * These functions allow to save a #GdkPixbuf in a number of 
77
 
 * file formats. The formatted data can be written to a file
78
 
 * or to a memory buffer. &gdk-pixbuf; can also call a user-defined
79
 
 * callback on the data, which allows to e.g. write the image 
80
 
 * to a socket or store it in a database.
81
 
 */
82
 
 
83
 
/**
84
 
 * SECTION:module_interface
85
 
 * @Short_description: Extending &gdk-pixbuf;
86
 
 * @Title: Module Interface
87
 
 * 
88
 
 * If &gdk-pixbuf; has been compiled with GModule support, it can be extended by
89
 
 * modules which can load (and perhaps also save) new image and animation
90
 
 * formats. Each loadable module must export a
91
 
 * #GdkPixbufModuleFillInfoFunc function named <function>fill_info</function> and
92
 
 * a #GdkPixbufModuleFillVtableFunc function named
93
 
 * <function>fill_vtable</function>.
94
 
 * 
95
 
 * 
96
 
 * In order to make format-checking work before actually loading the modules
97
 
 * (which may require dlopening image libraries), modules export their 
98
 
 * signatures (and other information) via the <function>fill_info</function>
99
 
 * function. An external utility, <command>gdk-pixbuf-query-loaders</command>, 
100
 
 * uses this to create a text file containing a list of all available loaders and 
101
 
 * their signatures. This file is then read at runtime by &gdk-pixbuf; to obtain
102
 
 * the list of available loaders and their signatures. 
103
 
 * 
104
 
 * 
105
 
 * Modules may only implement a subset of the functionality available via
106
 
 * #GdkPixbufModule. If a particular functionality is not implemented, the
107
 
 * <function>fill_vtable</function> function will simply not set the corresponding
108
 
 * function pointers of the #GdkPixbufModule structure. If a module supports
109
 
 * incremental loading (i.e. provides #begin_load, #stop_load and
110
 
 * #load_increment), it doesn't have to implement #load, since &gdk-pixbuf; can 
111
 
 * supply a generic #load implementation wrapping the incremental loading. 
112
 
 * 
113
 
 * 
114
 
 * Installing a module is a two-step process:
115
 
 * <itemizedlist>
116
 
 * <listitem><para>copy the module file(s) to the loader directory (normally
117
 
 * <filename><replaceable>libdir</replaceable>/gtk-2.0/<replaceable>version</replaceable>/loaders</filename>,
118
 
 * unless overridden by the environment variable
119
 
 * <envar>GDK_PIXBUF_MODULEDIR</envar>) 
120
 
 * </para></listitem>
121
 
 * <listitem><para>call <command>gdk-pixbuf-query-loaders</command> to update the
122
 
 * module file (normally
123
 
 * <filename><replaceable>sysconfdir</replaceable>/gtk-2.0/gdk-pixbuf.loaders</filename>,
124
 
 * unless overridden by the environment variable
125
 
 * <envar>GDK_PIXBUF_MODULE_FILE</envar>)
126
 
 * </para></listitem>
127
 
 * </itemizedlist>
128
 
 * 
129
 
 * 
130
 
 * The &gdk-pixbuf; interfaces needed for implementing modules are contained in 
131
 
 * <filename>gdk-pixbuf-io.h</filename> (and
132
 
 * <filename>gdk-pixbuf-animation.h</filename> if the module supports animations).
133
 
 * They are not covered by the same stability guarantees as the regular 
134
 
 * &gdk-pixbuf; API. To underline this fact, they are protected by 
135
 
 * <literal>#ifdef GDK_PIXBUF_ENABLE_BACKEND</literal>.
136
 
 */
137
 
 
138
 
 
139
 
#ifndef GDK_PIXBUF_USE_GIO_MIME 
140
 
static gint 
141
 
format_check (GdkPixbufModule *module, guchar *buffer, int size)
142
 
{
143
 
        int i, j;
144
 
        gchar m;
145
 
        GdkPixbufModulePattern *pattern;
146
 
        gboolean anchored;
147
 
        guchar *prefix;
148
 
        gchar *mask;
149
 
 
150
 
        for (pattern = module->info->signature; pattern->prefix; pattern++) {
151
 
                if (pattern->mask && pattern->mask[0] == '*') {
152
 
                        prefix = (guchar *)pattern->prefix + 1;
153
 
                        mask = pattern->mask + 1;
154
 
                        anchored = FALSE;
155
 
                }
156
 
                else {
157
 
                        prefix = (guchar *)pattern->prefix;
158
 
                        mask = pattern->mask;
159
 
                        anchored = TRUE;
160
 
                }
161
 
                for (i = 0; i < size; i++) {
162
 
                        for (j = 0; i + j < size && prefix[j] != 0; j++) {
163
 
                                m = mask ? mask[j] : ' ';
164
 
                                if (m == ' ') {
165
 
                                        if (buffer[i + j] != prefix[j])
166
 
                                                break;
167
 
                                }
168
 
                                else if (m == '!') {
169
 
                                        if (buffer[i + j] == prefix[j])
170
 
                                                break;
171
 
                                }
172
 
                                else if (m == 'z') {
173
 
                                        if (buffer[i + j] != 0)
174
 
                                                break;
175
 
                                }
176
 
                                else if (m == 'n') {
177
 
                                        if (buffer[i + j] == 0)
178
 
                                                break;
179
 
                                }
180
 
                        } 
181
 
 
182
 
                        if (prefix[j] == 0) 
183
 
                                return pattern->relevance;
184
 
 
185
 
                        if (anchored)
186
 
                                break;
187
 
                }
188
 
        }
189
 
        return 0;
190
 
}
191
 
#endif
192
 
 
193
 
G_LOCK_DEFINE_STATIC (init_lock);
194
 
G_LOCK_DEFINE_STATIC (threadunsafe_loader_lock);
195
 
 
196
 
gboolean
197
 
_gdk_pixbuf_lock (GdkPixbufModule *image_module)
198
 
{
199
 
        if (g_threads_got_initialized &&
200
 
            !(image_module->info->flags & GDK_PIXBUF_FORMAT_THREADSAFE)) {
201
 
                G_LOCK (threadunsafe_loader_lock);
202
 
 
203
 
                return TRUE;
204
 
        }
205
 
 
206
 
        return FALSE;
207
 
}
208
 
 
209
 
void
210
 
_gdk_pixbuf_unlock (GdkPixbufModule *image_module)
211
 
{
212
 
        if (!(image_module->info->flags & GDK_PIXBUF_FORMAT_THREADSAFE)) {
213
 
                G_UNLOCK (threadunsafe_loader_lock);
214
 
        }
215
 
}
216
 
 
217
 
static GSList *file_formats = NULL;
218
 
 
219
 
static void gdk_pixbuf_io_init (void);
220
 
 
221
 
static GSList *
222
 
get_file_formats (void)
223
 
{
224
 
        G_LOCK (init_lock);
225
 
        if (file_formats == NULL)
226
 
                gdk_pixbuf_io_init ();
227
 
        G_UNLOCK (init_lock);
228
 
        
229
 
        return file_formats;
230
 
}
231
 
 
232
 
#ifdef G_OS_WIN32
233
 
 
234
 
/* DllMain function needed to tuck away the gdk-pixbuf DLL handle */
235
 
 
236
 
static HMODULE gdk_pixbuf_dll;
237
 
 
238
 
BOOL WINAPI
239
 
DllMain (HINSTANCE hinstDLL,
240
 
         DWORD     fdwReason,
241
 
         LPVOID    lpvReserved)
242
 
{
243
 
        switch (fdwReason) {
244
 
        case DLL_PROCESS_ATTACH:
245
 
                gdk_pixbuf_dll = (HMODULE) hinstDLL;
246
 
                break;
247
 
        }
248
 
 
249
 
  return TRUE;
250
 
}
251
 
 
252
 
char *
253
 
_gdk_pixbuf_win32_get_toplevel (void)
254
 
{
255
 
  static char *toplevel = NULL;
256
 
 
257
 
  if (toplevel == NULL)
258
 
          toplevel = g_win32_get_package_installation_directory_of_module (gdk_pixbuf_dll);
259
 
 
260
 
  return toplevel;
261
 
}
262
 
#endif
263
 
 
264
 
 
265
 
#ifdef USE_GMODULE 
266
 
 
267
 
static gboolean
268
 
scan_string (const char **pos, GString *out)
269
 
{
270
 
        const char *p = *pos, *q = *pos;
271
 
        char *tmp, *tmp2;
272
 
        gboolean quoted;
273
 
        
274
 
        while (g_ascii_isspace (*p))
275
 
                p++;
276
 
        
277
 
        if (!*p)
278
 
                return FALSE;
279
 
        else if (*p == '"') {
280
 
                p++;
281
 
                quoted = FALSE;
282
 
                for (q = p; (*q != '"') || quoted; q++) {
283
 
                        if (!*q)
284
 
                                return FALSE;
285
 
                        quoted = (*q == '\\') && !quoted;
286
 
                }
287
 
                
288
 
                tmp = g_strndup (p, q - p);
289
 
                tmp2 = g_strcompress (tmp);
290
 
                g_string_truncate (out, 0);
291
 
                g_string_append (out, tmp2);
292
 
                g_free (tmp);
293
 
                g_free (tmp2);
294
 
        }
295
 
        
296
 
        q++;
297
 
        *pos = q;
298
 
        
299
 
        return TRUE;
300
 
}
301
 
 
302
 
static gboolean
303
 
scan_int (const char **pos, int *out)
304
 
{
305
 
        int i = 0;
306
 
        char buf[32];
307
 
        const char *p = *pos;
308
 
        
309
 
        while (g_ascii_isspace (*p))
310
 
                p++;
311
 
        
312
 
        if (*p < '0' || *p > '9')
313
 
                return FALSE;
314
 
        
315
 
        while ((*p >= '0') && (*p <= '9') && i < sizeof (buf)) {
316
 
                buf[i] = *p;
317
 
                i++;
318
 
                p++;
319
 
        }
320
 
        
321
 
        if (i == sizeof (buf))
322
 
                return FALSE;
323
 
        else
324
 
                buf[i] = '\0';
325
 
        
326
 
        *out = atoi (buf);
327
 
        
328
 
        *pos = p;
329
 
 
330
 
        return TRUE;
331
 
}
332
 
 
333
 
static gboolean
334
 
skip_space (const char **pos)
335
 
{
336
 
        const char *p = *pos;
337
 
        
338
 
        while (g_ascii_isspace (*p))
339
 
                p++;
340
 
  
341
 
        *pos = p;
342
 
        
343
 
        return !(*p == '\0');
344
 
}
345
 
  
346
 
#ifdef G_OS_WIN32
347
 
 
348
 
static char *
349
 
get_libdir (void)
350
 
{
351
 
  static char *libdir = NULL;
352
 
 
353
 
  if (libdir == NULL)
354
 
          libdir = g_build_filename (_gdk_pixbuf_win32_get_toplevel (), "lib", NULL);
355
 
 
356
 
  return libdir;
357
 
}
358
 
 
359
 
#undef GDK_PIXBUF_LIBDIR
360
 
#define GDK_PIXBUF_LIBDIR get_libdir()
361
 
 
362
 
static void
363
 
correct_prefix (gchar **path)
364
 
{
365
 
  if (strncmp (*path, GDK_PIXBUF_PREFIX "/", strlen (GDK_PIXBUF_PREFIX "/")) == 0 ||
366
 
      strncmp (*path, GDK_PIXBUF_PREFIX "\\", strlen (GDK_PIXBUF_PREFIX "\\")) == 0)
367
 
    {
368
 
          gchar *tem = NULL;
369
 
      if (strlen(*path) > 5 && strncmp (*path - 5, ".libs", 5) == 0)
370
 
        {
371
 
          /* We are being run from inside the build tree, and shouldn't mess about. */
372
 
          return;
373
 
        }
374
 
 
375
 
      /* This is an entry put there by gdk-pixbuf-query-loaders on the
376
 
       * packager's system. On Windows a prebuilt gdk-pixbuf package can be
377
 
       * installed in a random location. The gdk-pixbuf.loaders file
378
 
       * distributed in such a package contains paths from the package
379
 
       * builder's machine. Replace the build-time prefix with the
380
 
       * installation prefix on this machine.
381
 
       */
382
 
      tem = *path;
383
 
      *path = g_strconcat (_gdk_pixbuf_win32_get_toplevel (), tem + strlen (GDK_PIXBUF_PREFIX), NULL);
384
 
      g_free (tem);
385
 
    }
386
 
}
387
 
 
388
 
#endif  /* G_OS_WIN32 */
389
 
 
390
 
static gchar *
391
 
gdk_pixbuf_get_module_file (void)
392
 
{
393
 
  gchar *result = g_strdup (g_getenv ("GDK_PIXBUF_MODULE_FILE"));
394
 
 
395
 
  if (!result)
396
 
          result = g_build_filename (GDK_PIXBUF_LIBDIR, "gdk-pixbuf-2.0", GDK_PIXBUF_BINARY_VERSION, "loaders.cache", NULL);
397
 
 
398
 
  return result;
399
 
}
400
 
 
401
 
#endif  /* USE_GMODULE */
402
 
 
403
 
 
404
 
static gboolean
405
 
gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
406
 
                                 GError         **error);
407
 
 
408
 
static void 
409
 
gdk_pixbuf_io_init (void)
410
 
{
411
 
#ifdef USE_GMODULE
412
 
        GIOChannel *channel;
413
 
        gchar *line_buf;
414
 
        gsize term;
415
 
        GString *tmp_buf = g_string_new (NULL);
416
 
        gboolean have_error = FALSE;
417
 
        GdkPixbufModule *module = NULL;
418
 
        gchar *filename = gdk_pixbuf_get_module_file ();
419
 
        int flags;
420
 
        int n_patterns = 0;
421
 
        GdkPixbufModulePattern *pattern;
422
 
        GError *error = NULL;
423
 
#endif
424
 
        GdkPixbufModule *builtin_module ;
425
 
 
426
 
        /*  initialize on separate line to avoid compiler warnings in the
427
 
         *  common case of no compiled-in modules.
428
 
         */
429
 
        builtin_module = NULL;
430
 
 
431
 
#define load_one_builtin_module(format)                                 \
432
 
        builtin_module = g_new0 (GdkPixbufModule, 1);                   \
433
 
        builtin_module->module_name = #format;                          \
434
 
        if (gdk_pixbuf_load_module_unlocked (builtin_module, NULL))             \
435
 
                file_formats = g_slist_prepend (file_formats, builtin_module);\
436
 
        else                                                            \
437
 
                g_free (builtin_module)
438
 
 
439
 
#ifdef INCLUDE_ani
440
 
        load_one_builtin_module (ani);
441
 
#endif
442
 
#ifdef INCLUDE_png
443
 
        load_one_builtin_module (png);
444
 
#endif
445
 
#ifdef INCLUDE_bmp
446
 
        load_one_builtin_module (bmp);
447
 
#endif
448
 
#ifdef INCLUDE_wbmp
449
 
        load_one_builtin_module (wbmp);
450
 
#endif
451
 
#ifdef INCLUDE_gif
452
 
        load_one_builtin_module (gif);
453
 
#endif
454
 
#ifdef INCLUDE_ico
455
 
        load_one_builtin_module (ico);
456
 
#endif
457
 
#ifdef INCLUDE_jpeg
458
 
        load_one_builtin_module (jpeg);
459
 
#endif
460
 
#ifdef INCLUDE_pnm
461
 
        load_one_builtin_module (pnm);
462
 
#endif
463
 
#ifdef INCLUDE_ras
464
 
        load_one_builtin_module (ras);
465
 
#endif
466
 
#ifdef INCLUDE_tiff
467
 
        load_one_builtin_module (tiff);
468
 
#endif
469
 
#ifdef INCLUDE_xpm
470
 
        load_one_builtin_module (xpm);
471
 
#endif
472
 
#ifdef INCLUDE_xbm
473
 
        load_one_builtin_module (xbm);
474
 
#endif
475
 
#ifdef INCLUDE_tga
476
 
        load_one_builtin_module (tga);
477
 
#endif
478
 
#ifdef INCLUDE_pcx
479
 
        load_one_builtin_module (pcx);
480
 
#endif
481
 
#ifdef INCLUDE_icns
482
 
        load_one_builtin_module (icns);
483
 
#endif
484
 
#ifdef INCLUDE_jasper
485
 
        load_one_builtin_module (jasper);
486
 
#endif
487
 
#ifdef INCLUDE_qtif
488
 
        load_one_builtin_module (qtif);
489
 
#endif
490
 
#ifdef INCLUDE_gdiplus
491
 
        /* We don't bother having the GDI+ loaders individually selectable
492
 
         * for building in or not.
493
 
         */
494
 
        load_one_builtin_module (ico);
495
 
        load_one_builtin_module (wmf);
496
 
        load_one_builtin_module (emf);
497
 
        load_one_builtin_module (bmp);
498
 
        load_one_builtin_module (gif);
499
 
        load_one_builtin_module (jpeg);
500
 
        load_one_builtin_module (tiff);
501
 
#endif
502
 
#ifdef INCLUDE_gdip_png
503
 
        /* Except the gdip-png loader which normally isn't built at all even */
504
 
        load_one_builtin_module (png);
505
 
#endif
506
 
 
507
 
#undef load_one_builtin_module
508
 
 
509
 
#ifdef USE_GMODULE
510
 
        channel = g_io_channel_new_file (filename, "r",  &error);
511
 
        if (!channel) {
512
 
                /* Don't bother warning if we have some built-in loaders */
513
 
                if (file_formats == NULL)
514
 
                        g_warning ("Cannot open pixbuf loader module file '%s': %s",
515
 
                                   filename, error->message);
516
 
                g_string_free (tmp_buf, TRUE);
517
 
                g_free (filename);
518
 
                return;
519
 
        }
520
 
        
521
 
        while (!have_error && g_io_channel_read_line (channel, &line_buf, NULL, &term, NULL) == G_IO_STATUS_NORMAL) {
522
 
                const char *p;
523
 
                
524
 
                p = line_buf;
525
 
 
526
 
                line_buf[term] = 0;
527
 
 
528
 
                if (!skip_space (&p)) {
529
 
                                /* Blank line marking the end of a module
530
 
                                 */
531
 
                        if (module && *p != '#') {
532
 
#ifdef G_OS_WIN32
533
 
                                correct_prefix (&module->module_path);
534
 
#endif
535
 
                                file_formats = g_slist_prepend (file_formats, module);
536
 
                                module = NULL;
537
 
                        }
538
 
                        
539
 
                        goto next_line;
540
 
                }
541
 
 
542
 
                if (*p == '#') 
543
 
                        goto next_line;
544
 
                
545
 
                if (!module) {
546
 
                                /* Read a module location
547
 
                                 */
548
 
                        module = g_new0 (GdkPixbufModule, 1);
549
 
                        n_patterns = 0;
550
 
                        
551
 
                        if (!scan_string (&p, tmp_buf)) {
552
 
                                g_warning ("Error parsing loader info in '%s'\n  %s", 
553
 
                                           filename, line_buf);
554
 
                                have_error = TRUE;
555
 
                        }
556
 
                        module->module_path = g_strdup (tmp_buf->str);
557
 
                }
558
 
                else if (!module->module_name) {
559
 
                        module->info = g_new0 (GdkPixbufFormat, 1);
560
 
                        if (!scan_string (&p, tmp_buf)) {
561
 
                                g_warning ("Error parsing loader info in '%s'\n  %s", 
562
 
                                           filename, line_buf);
563
 
                                have_error = TRUE;
564
 
                        }
565
 
                        module->info->name =  g_strdup (tmp_buf->str);
566
 
                        module->module_name = module->info->name;
567
 
 
568
 
                        if (!scan_int (&p, &flags)) {
569
 
                                g_warning ("Error parsing loader info in '%s'\n  %s", 
570
 
                                           filename, line_buf);
571
 
                                have_error = TRUE;
572
 
                        }
573
 
                        module->info->flags = flags;
574
 
                        
575
 
                        if (!scan_string (&p, tmp_buf)) {
576
 
                                g_warning ("Error parsing loader info in '%s'\n  %s", 
577
 
                                           filename, line_buf);
578
 
                                have_error = TRUE;
579
 
                        }                       
580
 
                        if (tmp_buf->str[0] != 0)
581
 
                                module->info->domain = g_strdup (tmp_buf->str);
582
 
 
583
 
                        if (!scan_string (&p, tmp_buf)) {
584
 
                                g_warning ("Error parsing loader info in '%s'\n  %s", 
585
 
                                           filename, line_buf);
586
 
                                have_error = TRUE;
587
 
                        }                       
588
 
                        module->info->description = g_strdup (tmp_buf->str);
589
 
 
590
 
                        if (scan_string (&p, tmp_buf)) {
591
 
                                module->info->license = g_strdup (tmp_buf->str);
592
 
                        }
593
 
                }
594
 
                else if (!module->info->mime_types) {
595
 
                        int n = 1;
596
 
                        module->info->mime_types = g_new0 (gchar*, 1);
597
 
                        while (scan_string (&p, tmp_buf)) {
598
 
                                if (tmp_buf->str[0] != 0) {
599
 
                                        module->info->mime_types =
600
 
                                                g_realloc (module->info->mime_types, (n + 1) * sizeof (gchar*));
601
 
                                        module->info->mime_types[n - 1] = g_strdup (tmp_buf->str);
602
 
                                        module->info->mime_types[n] = NULL;
603
 
                                        n++;
604
 
                                }
605
 
                        }
606
 
                }
607
 
                else if (!module->info->extensions) {
608
 
                        int n = 1;
609
 
                        module->info->extensions = g_new0 (gchar*, 1);
610
 
                        while (scan_string (&p, tmp_buf)) {
611
 
                                if (tmp_buf->str[0] != 0) {
612
 
                                        module->info->extensions =
613
 
                                                g_realloc (module->info->extensions, (n + 1) * sizeof (gchar*));
614
 
                                        module->info->extensions[n - 1] = g_strdup (tmp_buf->str);
615
 
                                        module->info->extensions[n] = NULL;
616
 
                                        n++;
617
 
                                }
618
 
                        }
619
 
                }
620
 
                else {
621
 
                        n_patterns++;
622
 
                        module->info->signature = (GdkPixbufModulePattern *)
623
 
                                g_realloc (module->info->signature, (n_patterns + 1) * sizeof (GdkPixbufModulePattern));
624
 
                        pattern = module->info->signature + n_patterns;
625
 
                        pattern->prefix = NULL;
626
 
                        pattern->mask = NULL;
627
 
                        pattern->relevance = 0;
628
 
                        pattern--;
629
 
                        if (!scan_string (&p, tmp_buf))
630
 
                                goto context_error;
631
 
                        pattern->prefix = g_strdup (tmp_buf->str);
632
 
                        
633
 
                        if (!scan_string (&p, tmp_buf))
634
 
                                goto context_error;
635
 
                        if (*tmp_buf->str)
636
 
                                pattern->mask = g_strdup (tmp_buf->str);
637
 
                        else
638
 
                                pattern->mask = NULL;
639
 
                        
640
 
                        if (!scan_int (&p, &pattern->relevance))
641
 
                                goto context_error;
642
 
                        
643
 
                        goto next_line;
644
 
 
645
 
                context_error:
646
 
                        g_free (pattern->prefix);
647
 
                        g_free (pattern->mask);
648
 
                        g_free (pattern);
649
 
                        g_warning ("Error parsing loader info in '%s'\n  %s", 
650
 
                                   filename, line_buf);
651
 
                        have_error = TRUE;
652
 
                }
653
 
        next_line:
654
 
                g_free (line_buf);
655
 
        }
656
 
        g_string_free (tmp_buf, TRUE);
657
 
        g_io_channel_unref (channel);
658
 
        g_free (filename);
659
 
#endif
660
 
}
661
 
 
662
 
 
663
 
#define module(type) \
664
 
  extern void _gdk_pixbuf__##type##_fill_info   (GdkPixbufFormat *info);   \
665
 
  extern void _gdk_pixbuf__##type##_fill_vtable (GdkPixbufModule *module)
666
 
 
667
 
module (png);
668
 
module (jpeg);
669
 
module (gif);
670
 
module (ico);
671
 
module (ani);
672
 
module (ras);
673
 
module (xpm);
674
 
module (tiff);
675
 
module (pnm);
676
 
module (bmp);
677
 
module (wbmp);
678
 
module (xbm);
679
 
module (tga);
680
 
module (pcx);
681
 
module (icns);
682
 
module (jasper);
683
 
module (qtif);
684
 
module (gdip_ico);
685
 
module (gdip_wmf);
686
 
module (gdip_emf);
687
 
module (gdip_bmp);
688
 
module (gdip_gif);
689
 
module (gdip_jpeg);
690
 
module (gdip_png);
691
 
module (gdip_tiff);
692
 
 
693
 
#undef module
694
 
 
695
 
/* actually load the image handler - gdk_pixbuf_get_module only get a */
696
 
/* reference to the module to load, it doesn't actually load it       */
697
 
/* perhaps these actions should be combined in one function           */
698
 
static gboolean
699
 
gdk_pixbuf_load_module_unlocked (GdkPixbufModule *image_module,
700
 
                                 GError         **error)
701
 
{
702
 
        GdkPixbufModuleFillInfoFunc fill_info = NULL;
703
 
        GdkPixbufModuleFillVtableFunc fill_vtable = NULL;
704
 
                
705
 
        if (image_module->module != NULL)
706
 
               return TRUE;
707
 
 
708
 
#define try_module(format,id)                                           \
709
 
        if (fill_info == NULL &&                                        \
710
 
            strcmp (image_module->module_name, #format) == 0) {         \
711
 
                fill_info = _gdk_pixbuf__##id##_fill_info;              \
712
 
                fill_vtable = _gdk_pixbuf__##id##_fill_vtable;  \
713
 
        }
714
 
#ifdef INCLUDE_png      
715
 
        try_module (png,png);
716
 
#endif
717
 
#ifdef INCLUDE_bmp
718
 
        try_module (bmp,bmp);
719
 
#endif
720
 
#ifdef INCLUDE_wbmp
721
 
        try_module (wbmp,wbmp);
722
 
#endif
723
 
#ifdef INCLUDE_gif
724
 
        try_module (gif,gif);
725
 
#endif
726
 
#ifdef INCLUDE_ico
727
 
        try_module (ico,ico);
728
 
#endif
729
 
#ifdef INCLUDE_ani
730
 
        try_module (ani,ani);
731
 
#endif
732
 
#ifdef INCLUDE_jpeg
733
 
        try_module (jpeg,jpeg);
734
 
#endif
735
 
#ifdef INCLUDE_pnm
736
 
        try_module (pnm,pnm);
737
 
#endif
738
 
#ifdef INCLUDE_ras
739
 
        try_module (ras,ras);
740
 
#endif
741
 
#ifdef INCLUDE_tiff
742
 
        try_module (tiff,tiff);
743
 
#endif
744
 
#ifdef INCLUDE_xpm
745
 
        try_module (xpm,xpm);
746
 
#endif
747
 
#ifdef INCLUDE_xbm
748
 
        try_module (xbm,xbm);
749
 
#endif
750
 
#ifdef INCLUDE_tga
751
 
        try_module (tga,tga);
752
 
#endif
753
 
#ifdef INCLUDE_pcx
754
 
        try_module (pcx,pcx);
755
 
#endif
756
 
#ifdef INCLUDE_icns
757
 
        try_module (icns,icns);
758
 
#endif
759
 
#ifdef INCLUDE_jasper
760
 
        try_module (jasper,jasper);
761
 
#endif
762
 
#ifdef INCLUDE_qtif
763
 
        try_module (qtif,qtif);
764
 
#endif
765
 
#ifdef INCLUDE_gdiplus
766
 
        try_module (ico,gdip_ico);
767
 
        try_module (wmf,gdip_wmf);
768
 
        try_module (emf,gdip_emf);
769
 
        try_module (bmp,gdip_bmp);
770
 
        try_module (gif,gdip_gif);
771
 
        try_module (jpeg,gdip_jpeg);
772
 
        try_module (tiff,gdip_tiff);
773
 
#endif
774
 
#ifdef INCLUDE_gdip_png
775
 
        try_module (png,gdip_png);
776
 
#endif
777
 
 
778
 
#undef try_module
779
 
        
780
 
        if (fill_vtable) {
781
 
                image_module->module = (void *) 1;
782
 
                (* fill_vtable) (image_module);
783
 
                if (image_module->info == NULL) {
784
 
                        image_module->info = g_new0 (GdkPixbufFormat, 1);
785
 
                        (* fill_info) (image_module->info);
786
 
                }
787
 
                return TRUE;
788
 
        }
789
 
        else 
790
 
#ifdef USE_GMODULE
791
 
        {
792
 
                char *path;
793
 
                GModule *module;
794
 
                gpointer sym;
795
 
 
796
 
                path = image_module->module_path;
797
 
                module = g_module_open (path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
798
 
 
799
 
                if (!module) {
800
 
                        g_set_error (error,
801
 
                                     GDK_PIXBUF_ERROR,
802
 
                                     GDK_PIXBUF_ERROR_FAILED,
803
 
                                     _("Unable to load image-loading module: %s: %s"),
804
 
                                     path, g_module_error ());
805
 
                        return FALSE;
806
 
                }
807
 
 
808
 
                image_module->module = module;        
809
 
        
810
 
                if (g_module_symbol (module, "fill_vtable", &sym)) {
811
 
                        fill_vtable = (GdkPixbufModuleFillVtableFunc) sym;
812
 
                        (* fill_vtable) (image_module);
813
 
                        return TRUE;
814
 
                } else {
815
 
                        g_set_error (error,
816
 
                                     GDK_PIXBUF_ERROR,
817
 
                                     GDK_PIXBUF_ERROR_FAILED,
818
 
                                     _("Image-loading module %s does not export the proper interface; perhaps it's from a different gdk-pixbuf version?"),
819
 
                                     path);
820
 
                        return FALSE;
821
 
                }
822
 
        }
823
 
#else
824
 
        g_set_error (error,
825
 
                     GDK_PIXBUF_ERROR,
826
 
                     GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
827
 
                     _("Image type '%s' is not supported"),
828
 
                     image_module->module_name);
829
 
        return FALSE;
830
 
#endif  /* !USE_GMODULE */
831
 
}
832
 
 
833
 
 
834
 
gboolean
835
 
_gdk_pixbuf_load_module (GdkPixbufModule *image_module,
836
 
                         GError         **error)
837
 
{
838
 
        gboolean ret;
839
 
        gboolean locked = FALSE;
840
 
 
841
 
        /* be extra careful, maybe the module initializes
842
 
         * the thread system
843
 
         */
844
 
        if (g_threads_got_initialized) {
845
 
                G_LOCK (init_lock);
846
 
                locked = TRUE;
847
 
        }
848
 
 
849
 
        ret = gdk_pixbuf_load_module_unlocked (image_module, error);
850
 
 
851
 
        if (locked)
852
 
                G_UNLOCK (init_lock);
853
 
 
854
 
        return ret;
855
 
}
856
 
 
857
 
 
858
 
 
859
 
GdkPixbufModule *
860
 
_gdk_pixbuf_get_named_module (const char *name,
861
 
                              GError **error)
862
 
{
863
 
        GSList *modules;
864
 
 
865
 
        for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
866
 
                GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
867
 
 
868
 
                if (module->info->disabled)
869
 
                        continue;
870
 
 
871
 
                if (!strcmp (name, module->module_name))
872
 
                        return module;
873
 
        }
874
 
 
875
 
        g_set_error (error,
876
 
                     GDK_PIXBUF_ERROR,
877
 
                     GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
878
 
                     _("Image type '%s' is not supported"),
879
 
                     name);
880
 
        
881
 
        return NULL;
882
 
}
883
 
 
884
 
GdkPixbufModule *
885
 
_gdk_pixbuf_get_module (guchar *buffer, guint size,
886
 
                        const gchar *filename,
887
 
                        GError **error)
888
 
{
889
 
        GSList *modules;
890
 
 
891
 
        GdkPixbufModule *selected = NULL;
892
 
        gchar *display_name = NULL;
893
 
#ifdef GDK_PIXBUF_USE_GIO_MIME
894
 
        gchar *mime_type;
895
 
        gchar **mimes;
896
 
        gchar *type;
897
 
        gint j;
898
 
        gboolean uncertain;
899
 
 
900
 
        mime_type = g_content_type_guess (NULL, buffer, size, &uncertain);
901
 
        if (uncertain)
902
 
                mime_type = g_content_type_guess (filename, buffer, size, NULL);
903
 
 
904
 
        for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
905
 
                GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
906
 
                GdkPixbufFormat *info = module->info;
907
 
 
908
 
                if (info->disabled)
909
 
                        continue;
910
 
 
911
 
                mimes = info->mime_types;
912
 
                for (j = 0; mimes[j] != NULL; j++) {
913
 
                        type = g_content_type_from_mime_type (mimes[j]);
914
 
                        if (g_content_type_equals (type, mime_type)) {
915
 
                                g_free (type);
916
 
                                selected = module;
917
 
                                break;
918
 
                        }
919
 
                        g_free (type);
920
 
                }
921
 
        }
922
 
        g_free (mime_type);
923
 
#else
924
 
        gint score, best = 0;
925
 
 
926
 
        for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
927
 
                GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
928
 
 
929
 
                if (module->info->disabled)
930
 
                        continue;
931
 
 
932
 
                score = format_check (module, buffer, size);
933
 
                if (score > best) {
934
 
                        best = score; 
935
 
                        selected = module;
936
 
                }
937
 
                if (score >= 100) 
938
 
                        break;
939
 
        }
940
 
#endif
941
 
 
942
 
        if (selected != NULL)
943
 
                return selected;
944
 
 
945
 
        if (filename)
946
 
        {
947
 
                display_name = g_filename_display_name (filename);
948
 
                g_set_error (error,
949
 
                             GDK_PIXBUF_ERROR,
950
 
                             GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
951
 
                             _("Couldn't recognize the image file format for file '%s'"),
952
 
                             display_name);
953
 
                g_free (display_name);
954
 
        }
955
 
        else
956
 
                g_set_error_literal (error,
957
 
                                     GDK_PIXBUF_ERROR,
958
 
                                     GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
959
 
                                     _("Unrecognized image file format"));
960
 
 
961
 
 
962
 
        return NULL;
963
 
}
964
 
 
965
 
 
966
 
static void
967
 
prepared_notify (GdkPixbuf *pixbuf, 
968
 
                 GdkPixbufAnimation *anim, 
969
 
                 gpointer user_data)
970
 
{
971
 
        if (pixbuf != NULL)
972
 
                g_object_ref (pixbuf);
973
 
        *((GdkPixbuf **)user_data) = pixbuf;
974
 
}
975
 
 
976
 
GdkPixbuf *
977
 
_gdk_pixbuf_generic_image_load (GdkPixbufModule *module,
978
 
                                FILE *f,
979
 
                                GError **error)
980
 
{
981
 
        guchar buffer[LOAD_BUFFER_SIZE];
982
 
        size_t length;
983
 
        GdkPixbuf *pixbuf = NULL;
984
 
        GdkPixbufAnimation *animation = NULL;
985
 
        gpointer context;
986
 
        gboolean locked;
987
 
 
988
 
        locked = _gdk_pixbuf_lock (module);
989
 
 
990
 
        if (module->load != NULL) {
991
 
                pixbuf = (* module->load) (f, error);
992
 
        } else if (module->begin_load != NULL) {
993
 
                
994
 
                context = module->begin_load (NULL, prepared_notify, NULL, &pixbuf, error);
995
 
        
996
 
                if (!context)
997
 
                        goto out;
998
 
                
999
 
                while (!feof (f) && !ferror (f)) {
1000
 
                        length = fread (buffer, 1, sizeof (buffer), f);
1001
 
                        if (length > 0)
1002
 
                                if (!module->load_increment (context, buffer, length, error)) {
1003
 
                                        module->stop_load (context, NULL);
1004
 
                                        if (pixbuf != NULL) {
1005
 
                                                g_object_unref (pixbuf);
1006
 
                                                pixbuf = NULL;
1007
 
                                        }
1008
 
                                        goto out;
1009
 
                                }
1010
 
                }
1011
 
                
1012
 
                if (!module->stop_load (context, error)) {
1013
 
                        if (pixbuf != NULL) {
1014
 
                                g_object_unref (pixbuf);
1015
 
                                pixbuf = NULL;
1016
 
                        }
1017
 
                }
1018
 
        } else if (module->load_animation != NULL) {
1019
 
                animation = (* module->load_animation) (f, error);
1020
 
                if (animation != NULL) {
1021
 
                        pixbuf = gdk_pixbuf_animation_get_static_image (animation);
1022
 
 
1023
 
                        g_object_ref (pixbuf);
1024
 
                        g_object_unref (animation);
1025
 
                }
1026
 
        }
1027
 
 
1028
 
 out:
1029
 
        if (locked)
1030
 
                _gdk_pixbuf_unlock (module);
1031
 
        return pixbuf;
1032
 
}
1033
 
 
1034
 
/**
1035
 
 * gdk_pixbuf_new_from_file:
1036
 
 * @filename: Name of file to load, in the GLib file name encoding
1037
 
 * @error: Return location for an error
1038
 
 *
1039
 
 * Creates a new pixbuf by loading an image from a file.  The file format is
1040
 
 * detected automatically. If %NULL is returned, then @error will be set.
1041
 
 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
1042
 
 *
1043
 
 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL if
1044
 
 * any of several error conditions occurred:  the file could not be opened,
1045
 
 * there was no loader for the file's format, there was not enough memory to
1046
 
 * allocate the image buffer, or the image file contained invalid data.
1047
 
 **/
1048
 
GdkPixbuf *
1049
 
gdk_pixbuf_new_from_file (const char *filename,
1050
 
                          GError    **error)
1051
 
{
1052
 
        GdkPixbuf *pixbuf;
1053
 
        int size;
1054
 
        FILE *f;
1055
 
        guchar buffer[SNIFF_BUFFER_SIZE];
1056
 
        GdkPixbufModule *image_module;
1057
 
        gchar *display_name;
1058
 
 
1059
 
        g_return_val_if_fail (filename != NULL, NULL);
1060
 
        g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1061
 
        
1062
 
        display_name = g_filename_display_name (filename);      
1063
 
 
1064
 
        f = g_fopen (filename, "rb");
1065
 
        if (!f) {
1066
 
                gint save_errno = errno;
1067
 
                g_set_error (error,
1068
 
                             G_FILE_ERROR,
1069
 
                             g_file_error_from_errno (save_errno),
1070
 
                             _("Failed to open file '%s': %s"),
1071
 
                             display_name,
1072
 
                             g_strerror (save_errno));
1073
 
                g_free (display_name);
1074
 
                return NULL;
1075
 
        }
1076
 
 
1077
 
        size = fread (&buffer, 1, sizeof (buffer), f);
1078
 
        if (size == 0) {
1079
 
                g_set_error (error,
1080
 
                             GDK_PIXBUF_ERROR,
1081
 
                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
1082
 
                             _("Image file '%s' contains no data"),
1083
 
                             display_name);
1084
 
                g_free (display_name);
1085
 
                fclose (f);
1086
 
                return NULL;
1087
 
        }
1088
 
 
1089
 
        image_module = _gdk_pixbuf_get_module (buffer, size, filename, error);
1090
 
        if (image_module == NULL) {
1091
 
                g_free (display_name);
1092
 
                fclose (f);
1093
 
                return NULL;
1094
 
        }
1095
 
 
1096
 
        if (!_gdk_pixbuf_load_module (image_module, error)) {
1097
 
                g_free (display_name);
1098
 
                fclose (f);
1099
 
                return NULL;
1100
 
        }
1101
 
 
1102
 
        fseek (f, 0, SEEK_SET);
1103
 
        pixbuf = _gdk_pixbuf_generic_image_load (image_module, f, error);
1104
 
        fclose (f);
1105
 
 
1106
 
        if (pixbuf == NULL && error != NULL && *error == NULL) {
1107
 
 
1108
 
                /* I don't trust these crufty longjmp()'ing image libs
1109
 
                 * to maintain proper error invariants, and I don't
1110
 
                 * want user code to segfault as a result. We need to maintain
1111
 
                 * the invariant that error gets set if NULL is returned.
1112
 
                 */
1113
 
 
1114
 
                g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.", image_module->module_name);
1115
 
                g_set_error (error,
1116
 
                             GDK_PIXBUF_ERROR,
1117
 
                             GDK_PIXBUF_ERROR_FAILED,
1118
 
                             _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1119
 
                             display_name);
1120
 
        } else if (error != NULL && *error != NULL) {
1121
 
 
1122
 
          /* Add the filename to the error message */
1123
 
          GError *e = *error;
1124
 
          gchar  *old;
1125
 
 
1126
 
          old = e->message;
1127
 
          e->message = g_strdup_printf (_("Failed to load image '%s': %s"),
1128
 
                                        display_name,
1129
 
                                        old);
1130
 
          g_free (old);
1131
 
        }
1132
 
 
1133
 
        g_free (display_name);
1134
 
        return pixbuf;
1135
 
}
1136
 
 
1137
 
#ifdef G_OS_WIN32
1138
 
 
1139
 
#undef gdk_pixbuf_new_from_file
1140
 
GdkPixbuf *
1141
 
gdk_pixbuf_new_from_file (const char *filename,
1142
 
                          GError    **error)
1143
 
{
1144
 
        gchar *utf8_filename =
1145
 
                g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1146
 
        GdkPixbuf *retval;
1147
 
 
1148
 
        if (utf8_filename == NULL)
1149
 
                return NULL;
1150
 
 
1151
 
        retval = gdk_pixbuf_new_from_file_utf8 (utf8_filename, error);
1152
 
 
1153
 
        g_free (utf8_filename);
1154
 
 
1155
 
        return retval;
1156
 
}
1157
 
#endif
1158
 
 
1159
 
 
1160
 
/**
1161
 
 * gdk_pixbuf_new_from_file_at_size:
1162
 
 * @filename: Name of file to load, in the GLib file name encoding
1163
 
 * @width: The width the image should have or -1 to not constrain the width
1164
 
 * @height: The height the image should have or -1 to not constrain the height
1165
 
 * @error: Return location for an error
1166
 
 *
1167
 
 * Creates a new pixbuf by loading an image from a file.  
1168
 
 * The file format is detected automatically. If %NULL is returned, then 
1169
 
 * @error will be set. Possible errors are in the #GDK_PIXBUF_ERROR and 
1170
 
 * #G_FILE_ERROR domains.
1171
 
 *
1172
 
 * The image will be scaled to fit in the requested size, preserving
1173
 
 * the image's aspect ratio. Note that the returned pixbuf may be smaller
1174
 
 * than @width x @height, if the aspect ratio requires it. To load
1175
 
 * and image at the requested size, regardless of aspect ratio, use
1176
 
 * gdk_pixbuf_new_from_file_at_scale().
1177
 
 *
1178
 
 * Return value: A newly-created pixbuf with a reference count of 1, or 
1179
 
 * %NULL if any of several error conditions occurred:  the file could not 
1180
 
 * be opened, there was no loader for the file's format, there was not 
1181
 
 * enough memory to allocate the image buffer, or the image file contained 
1182
 
 * invalid data.
1183
 
 *
1184
 
 * Since: 2.4
1185
 
 **/
1186
 
GdkPixbuf *
1187
 
gdk_pixbuf_new_from_file_at_size (const char *filename,
1188
 
                                  int         width, 
1189
 
                                  int         height,
1190
 
                                  GError    **error)
1191
 
{
1192
 
        return gdk_pixbuf_new_from_file_at_scale (filename, 
1193
 
                                                  width, height, 
1194
 
                                                  TRUE, error);
1195
 
}
1196
 
 
1197
 
#ifdef G_OS_WIN32
1198
 
 
1199
 
#undef gdk_pixbuf_new_from_file_at_size
1200
 
 
1201
 
GdkPixbuf *
1202
 
gdk_pixbuf_new_from_file_at_size (const char *filename,
1203
 
                                  int         width, 
1204
 
                                  int         height,
1205
 
                                  GError    **error)
1206
 
{
1207
 
        gchar *utf8_filename =
1208
 
                g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1209
 
        GdkPixbuf *retval;
1210
 
 
1211
 
        if (utf8_filename == NULL)
1212
 
                return NULL;
1213
 
 
1214
 
        retval = gdk_pixbuf_new_from_file_at_size_utf8 (utf8_filename,
1215
 
                                                        width, height,
1216
 
                                                        error);
1217
 
 
1218
 
        g_free (utf8_filename);
1219
 
 
1220
 
        return retval;
1221
 
}
1222
 
#endif
1223
 
 
1224
 
typedef struct {
1225
 
        gint width;
1226
 
        gint height;
1227
 
        gboolean preserve_aspect_ratio;
1228
 
} AtScaleData; 
1229
 
 
1230
 
static void
1231
 
at_scale_data_async_data_free (AtScaleData *data)
1232
 
{
1233
 
        g_slice_free (AtScaleData, data);
1234
 
}
1235
 
 
1236
 
static void
1237
 
at_scale_size_prepared_cb (GdkPixbufLoader *loader, 
1238
 
                           int              width,
1239
 
                           int              height,
1240
 
                           gpointer         data)
1241
 
{
1242
 
        AtScaleData *info = data;
1243
 
 
1244
 
        g_return_if_fail (width > 0 && height > 0);
1245
 
 
1246
 
        if (info->preserve_aspect_ratio && 
1247
 
            (info->width > 0 || info->height > 0)) {
1248
 
                if (info->width < 0)
1249
 
                {
1250
 
                        width = width * (double)info->height/(double)height;
1251
 
                        height = info->height;
1252
 
                }
1253
 
                else if (info->height < 0)
1254
 
                {
1255
 
                        height = height * (double)info->width/(double)width;
1256
 
                        width = info->width;
1257
 
                }
1258
 
                else if ((double)height * (double)info->width >
1259
 
                         (double)width * (double)info->height) {
1260
 
                        width = 0.5 + (double)width * (double)info->height / (double)height;
1261
 
                        height = info->height;
1262
 
                } else {
1263
 
                        height = 0.5 + (double)height * (double)info->width / (double)width;
1264
 
                        width = info->width;
1265
 
                }
1266
 
        } else {
1267
 
                if (info->width > 0)
1268
 
                        width = info->width;
1269
 
                if (info->height > 0)
1270
 
                        height = info->height;
1271
 
        }
1272
 
        
1273
 
        width = MAX (width, 1);
1274
 
        height = MAX (height, 1);
1275
 
 
1276
 
        gdk_pixbuf_loader_set_size (loader, width, height);
1277
 
}
1278
 
 
1279
 
/**
1280
 
 * gdk_pixbuf_new_from_file_at_scale:
1281
 
 * @filename: Name of file to load, in the GLib file name encoding
1282
 
 * @width: The width the image should have or -1 to not constrain the width
1283
 
 * @height: The height the image should have or -1 to not constrain the height
1284
 
 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1285
 
 * @error: Return location for an error
1286
 
 *
1287
 
 * Creates a new pixbuf by loading an image from a file.  The file format is
1288
 
 * detected automatically. If %NULL is returned, then @error will be set.
1289
 
 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
1290
 
 * The image will be scaled to fit in the requested size, optionally preserving
1291
 
 * the image's aspect ratio. 
1292
 
 *
1293
 
 * When preserving the aspect ratio, a @width of -1 will cause the image
1294
 
 * to be scaled to the exact given height, and a @height of -1 will cause
1295
 
 * the image to be scaled to the exact given width. When not preserving
1296
 
 * aspect ratio, a @width or @height of -1 means to not scale the image 
1297
 
 * at all in that dimension. Negative values for @width and @height are 
1298
 
 * allowed since 2.8.
1299
 
 *
1300
 
 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL 
1301
 
 * if any of several error conditions occurred:  the file could not be opened,
1302
 
 * there was no loader for the file's format, there was not enough memory to
1303
 
 * allocate the image buffer, or the image file contained invalid data.
1304
 
 *
1305
 
 * Since: 2.6
1306
 
 **/
1307
 
GdkPixbuf *
1308
 
gdk_pixbuf_new_from_file_at_scale (const char *filename,
1309
 
                                   int         width, 
1310
 
                                   int         height,
1311
 
                                   gboolean    preserve_aspect_ratio,
1312
 
                                   GError    **error)
1313
 
{
1314
 
 
1315
 
        GdkPixbufLoader *loader;
1316
 
        GdkPixbuf       *pixbuf;
1317
 
        guchar buffer[LOAD_BUFFER_SIZE];
1318
 
        int length;
1319
 
        FILE *f;
1320
 
        AtScaleData info;
1321
 
        GdkPixbufAnimation *animation;
1322
 
        GdkPixbufAnimationIter *iter;
1323
 
        gboolean has_frame;
1324
 
 
1325
 
        g_return_val_if_fail (filename != NULL, NULL);
1326
 
        g_return_val_if_fail (width > 0 || width == -1, NULL);
1327
 
        g_return_val_if_fail (height > 0 || height == -1, NULL);
1328
 
 
1329
 
        f = g_fopen (filename, "rb");
1330
 
        if (!f) {
1331
 
                gint save_errno = errno;
1332
 
                gchar *display_name = g_filename_display_name (filename);
1333
 
                g_set_error (error,
1334
 
                             G_FILE_ERROR,
1335
 
                             g_file_error_from_errno (save_errno),
1336
 
                             _("Failed to open file '%s': %s"),
1337
 
                             display_name,
1338
 
                             g_strerror (save_errno));
1339
 
                g_free (display_name);
1340
 
                return NULL;
1341
 
        }
1342
 
 
1343
 
        loader = gdk_pixbuf_loader_new ();
1344
 
 
1345
 
        info.width = width;
1346
 
        info.height = height;
1347
 
        info.preserve_aspect_ratio = preserve_aspect_ratio;
1348
 
 
1349
 
        g_signal_connect (loader, "size-prepared", 
1350
 
                          G_CALLBACK (at_scale_size_prepared_cb), &info);
1351
 
 
1352
 
        has_frame = FALSE;
1353
 
        while (!has_frame && !feof (f) && !ferror (f)) {
1354
 
                length = fread (buffer, 1, sizeof (buffer), f);
1355
 
                if (length > 0)
1356
 
                        if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
1357
 
                                gdk_pixbuf_loader_close (loader, NULL);
1358
 
                                fclose (f);
1359
 
                                g_object_unref (loader);
1360
 
                                return NULL;
1361
 
                        }
1362
 
                
1363
 
                animation = gdk_pixbuf_loader_get_animation (loader);
1364
 
                if (animation) {
1365
 
                        iter = gdk_pixbuf_animation_get_iter (animation, NULL);
1366
 
                        if (!gdk_pixbuf_animation_iter_on_currently_loading_frame (iter)) {
1367
 
                                has_frame = TRUE;
1368
 
                        }
1369
 
                        g_object_unref (iter);
1370
 
                }
1371
 
        }
1372
 
 
1373
 
        fclose (f);
1374
 
 
1375
 
        if (!gdk_pixbuf_loader_close (loader, error) && !has_frame) {
1376
 
                g_object_unref (loader);
1377
 
                return NULL;
1378
 
        }
1379
 
 
1380
 
        pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1381
 
 
1382
 
        if (!pixbuf) {
1383
 
                gchar *display_name = g_filename_display_name (filename);
1384
 
                g_object_unref (loader);
1385
 
                g_set_error (error,
1386
 
                             GDK_PIXBUF_ERROR,
1387
 
                             GDK_PIXBUF_ERROR_FAILED,
1388
 
                             _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1389
 
                             display_name);
1390
 
                g_free (display_name);
1391
 
                return NULL;
1392
 
        }
1393
 
 
1394
 
        g_object_ref (pixbuf);
1395
 
 
1396
 
        g_object_unref (loader);
1397
 
 
1398
 
        return pixbuf;
1399
 
}
1400
 
 
1401
 
#ifdef G_OS_WIN32
1402
 
 
1403
 
#undef gdk_pixbuf_new_from_file_at_scale
1404
 
 
1405
 
GdkPixbuf *
1406
 
gdk_pixbuf_new_from_file_at_scale (const char *filename,
1407
 
                                   int         width, 
1408
 
                                   int         height,
1409
 
                                   gboolean    preserve_aspect_ratio,
1410
 
                                   GError    **error)
1411
 
{
1412
 
        gchar *utf8_filename =
1413
 
                g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1414
 
        GdkPixbuf *retval;
1415
 
 
1416
 
        if (utf8_filename == NULL)
1417
 
                return NULL;
1418
 
 
1419
 
        retval = gdk_pixbuf_new_from_file_at_scale_utf8 (utf8_filename,
1420
 
                                                         width, height,
1421
 
                                                         preserve_aspect_ratio,
1422
 
                                                         error);
1423
 
 
1424
 
        g_free (utf8_filename);
1425
 
 
1426
 
        return retval;
1427
 
}
1428
 
#endif
1429
 
 
1430
 
 
1431
 
static GdkPixbuf *
1432
 
load_from_stream (GdkPixbufLoader  *loader,
1433
 
                  GInputStream     *stream,
1434
 
                  GCancellable     *cancellable,
1435
 
                  GError          **error)
1436
 
{
1437
 
        GdkPixbuf *pixbuf;
1438
 
        gssize n_read;
1439
 
        guchar buffer[LOAD_BUFFER_SIZE];
1440
 
        gboolean res;
1441
 
 
1442
 
        res = TRUE;
1443
 
        while (1) { 
1444
 
                n_read = g_input_stream_read (stream, 
1445
 
                                              buffer, 
1446
 
                                              sizeof (buffer), 
1447
 
                                              cancellable, 
1448
 
                                              error);
1449
 
                if (n_read < 0) {
1450
 
                        res = FALSE;
1451
 
                        error = NULL; /* Ignore further errors */
1452
 
                        break;
1453
 
                }
1454
 
 
1455
 
                if (n_read == 0)
1456
 
                        break;
1457
 
 
1458
 
                if (!gdk_pixbuf_loader_write (loader, 
1459
 
                                              buffer, 
1460
 
                                              n_read, 
1461
 
                                              error)) {
1462
 
                        res = FALSE;
1463
 
                        error = NULL;
1464
 
                        break;
1465
 
                }
1466
 
        }
1467
 
 
1468
 
        if (!gdk_pixbuf_loader_close (loader, error)) {
1469
 
                res = FALSE;
1470
 
                error = NULL;
1471
 
        }
1472
 
 
1473
 
        pixbuf = NULL;
1474
 
        if (res) {
1475
 
                pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1476
 
                if (pixbuf)
1477
 
                        g_object_ref (pixbuf);
1478
 
        }
1479
 
 
1480
 
        return pixbuf;
1481
 
}
1482
 
 
1483
 
 
1484
 
/**
1485
 
 * gdk_pixbuf_new_from_stream_at_scale:
1486
 
 * @stream:  a #GInputStream to load the pixbuf from
1487
 
 * @width: The width the image should have or -1 to not constrain the width
1488
 
 * @height: The height the image should have or -1 to not constrain the height
1489
 
 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1490
 
 * @cancellable: optional #GCancellable object, %NULL to ignore
1491
 
 * @error: Return location for an error
1492
 
 *
1493
 
 * Creates a new pixbuf by loading an image from an input stream.  
1494
 
 *
1495
 
 * The file format is detected automatically. If %NULL is returned, then 
1496
 
 * @error will be set. The @cancellable can be used to abort the operation
1497
 
 * from another thread. If the operation was cancelled, the error 
1498
 
 * %GIO_ERROR_CANCELLED will be returned. Other possible errors are in 
1499
 
 * the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains. 
1500
 
 *
1501
 
 * The image will be scaled to fit in the requested size, optionally 
1502
 
 * preserving the image's aspect ratio. When preserving the aspect ratio, 
1503
 
 * a @width of -1 will cause the image to be scaled to the exact given 
1504
 
 * height, and a @height of -1 will cause the image to be scaled to the 
1505
 
 * exact given width. When not preserving aspect ratio, a @width or 
1506
 
 * @height of -1 means to not scale the image at all in that dimension.
1507
 
 *
1508
 
 * The stream is not closed.
1509
 
 *
1510
 
 * Return value: A newly-created pixbuf, or %NULL if any of several error 
1511
 
 * conditions occurred: the file could not be opened, the image format is 
1512
 
 * not supported, there was not enough memory to allocate the image buffer, 
1513
 
 * the stream contained invalid data, or the operation was cancelled.
1514
 
 *
1515
 
 * Since: 2.14
1516
 
 */
1517
 
GdkPixbuf *
1518
 
gdk_pixbuf_new_from_stream_at_scale (GInputStream  *stream,
1519
 
                                     gint           width,
1520
 
                                     gint           height,
1521
 
                                     gboolean       preserve_aspect_ratio,
1522
 
                                     GCancellable  *cancellable,
1523
 
                                     GError       **error)
1524
 
{
1525
 
        GdkPixbufLoader *loader;
1526
 
        GdkPixbuf *pixbuf;
1527
 
        AtScaleData info;
1528
 
 
1529
 
        loader = gdk_pixbuf_loader_new ();
1530
 
 
1531
 
        info.width = width;
1532
 
        info.height = height;
1533
 
        info.preserve_aspect_ratio = preserve_aspect_ratio;
1534
 
 
1535
 
        g_signal_connect (loader, "size-prepared", 
1536
 
                          G_CALLBACK (at_scale_size_prepared_cb), &info);
1537
 
 
1538
 
        pixbuf = load_from_stream (loader, stream, cancellable, error);
1539
 
        g_object_unref (loader);
1540
 
 
1541
 
        return pixbuf;
1542
 
}
1543
 
 
1544
 
static void
1545
 
new_from_stream_thread (GSimpleAsyncResult *result,
1546
 
                        GInputStream       *stream,
1547
 
                        GCancellable       *cancellable)
1548
 
{
1549
 
        GdkPixbuf *pixbuf;
1550
 
        AtScaleData *data;
1551
 
        GError *error = NULL;
1552
 
 
1553
 
        /* If data != NULL, we're scaling the pixbuf while loading it */
1554
 
        data = g_simple_async_result_get_op_res_gpointer (result);
1555
 
        if (data != NULL)
1556
 
                pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream, data->width, data->height, data->preserve_aspect_ratio, cancellable, &error);
1557
 
        else
1558
 
                pixbuf = gdk_pixbuf_new_from_stream (stream, cancellable, &error);
1559
 
 
1560
 
        g_simple_async_result_set_op_res_gpointer (result, NULL, NULL);
1561
 
 
1562
 
        /* Set the new pixbuf as the result, or error out */
1563
 
        if (pixbuf == NULL) {
1564
 
                g_simple_async_result_take_error (result, error);
1565
 
        } else {
1566
 
                g_simple_async_result_set_op_res_gpointer (result, g_object_ref (pixbuf), g_object_unref);
1567
 
        }
1568
 
}
1569
 
 
1570
 
/**
1571
 
 * gdk_pixbuf_new_from_stream_at_scale_async:
1572
 
 * @stream: a #GInputStream from which to load the pixbuf
1573
 
 * @width: the width the image should have or -1 to not constrain the width
1574
 
 * @height: the height the image should have or -1 to not constrain the height
1575
 
 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1576
 
 * @cancellable: optional #GCancellable object, %NULL to ignore
1577
 
 * @callback: a #GAsyncReadyCallback to call when the the pixbuf is loaded
1578
 
 * @user_data: the data to pass to the callback function
1579
 
 *
1580
 
 * Creates a new pixbuf by asynchronously loading an image from an input stream.
1581
 
 *
1582
 
 * For more details see gdk_pixbuf_new_from_stream_at_scale(), which is the synchronous
1583
 
 * version of this function.
1584
 
 *
1585
 
 * When the operation is finished, @callback will be called in the main thread.
1586
 
 * You can then call gdk_pixbuf_new_from_stream_finish() to get the result of the operation.
1587
 
 *
1588
 
 * Since: 2.24
1589
 
 **/
1590
 
void
1591
 
gdk_pixbuf_new_from_stream_at_scale_async (GInputStream        *stream,
1592
 
                                           gint                 width,
1593
 
                                           gint                 height,
1594
 
                                           gboolean             preserve_aspect_ratio,
1595
 
                                           GCancellable        *cancellable,
1596
 
                                           GAsyncReadyCallback  callback,
1597
 
                                           gpointer             user_data)
1598
 
{
1599
 
        GSimpleAsyncResult *result;
1600
 
        AtScaleData *data;
1601
 
 
1602
 
        g_return_if_fail (G_IS_INPUT_STREAM (stream));
1603
 
        g_return_if_fail (callback != NULL);
1604
 
        g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
1605
 
 
1606
 
        data = g_slice_new (AtScaleData);
1607
 
        data->width = width;
1608
 
        data->height = height;
1609
 
        data->preserve_aspect_ratio = preserve_aspect_ratio;
1610
 
 
1611
 
        result = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, gdk_pixbuf_new_from_stream_at_scale_async);
1612
 
        g_simple_async_result_set_op_res_gpointer (result, data, (GDestroyNotify) at_scale_data_async_data_free);
1613
 
        g_simple_async_result_run_in_thread (result, (GSimpleAsyncThreadFunc) new_from_stream_thread, G_PRIORITY_DEFAULT, cancellable);
1614
 
        g_object_unref (result);
1615
 
}
1616
 
 
1617
 
/**
1618
 
 * gdk_pixbuf_new_from_stream:
1619
 
 * @stream:  a #GInputStream to load the pixbuf from
1620
 
 * @cancellable: optional #GCancellable object, %NULL to ignore
1621
 
 * @error: Return location for an error
1622
 
 *
1623
 
 * Creates a new pixbuf by loading an image from an input stream.  
1624
 
 *
1625
 
 * The file format is detected automatically. If %NULL is returned, then 
1626
 
 * @error will be set. The @cancellable can be used to abort the operation
1627
 
 * from another thread. If the operation was cancelled, the error 
1628
 
 * %GIO_ERROR_CANCELLED will be returned. Other possible errors are in 
1629
 
 * the #GDK_PIXBUF_ERROR and %G_IO_ERROR domains. 
1630
 
 *
1631
 
 * The stream is not closed.
1632
 
 *
1633
 
 * Return value: A newly-created pixbuf, or %NULL if any of several error 
1634
 
 * conditions occurred: the file could not be opened, the image format is 
1635
 
 * not supported, there was not enough memory to allocate the image buffer, 
1636
 
 * the stream contained invalid data, or the operation was cancelled.
1637
 
 *
1638
 
 * Since: 2.14
1639
 
 **/
1640
 
GdkPixbuf *
1641
 
gdk_pixbuf_new_from_stream (GInputStream  *stream,
1642
 
                            GCancellable  *cancellable,
1643
 
                            GError       **error)
1644
 
{
1645
 
        GdkPixbuf *pixbuf;
1646
 
        GdkPixbufLoader *loader;
1647
 
 
1648
 
        loader = gdk_pixbuf_loader_new ();
1649
 
        pixbuf = load_from_stream (loader, stream, cancellable, error);
1650
 
        g_object_unref (loader);
1651
 
 
1652
 
        return pixbuf;
1653
 
}
1654
 
 
1655
 
/**
1656
 
 * gdk_pixbuf_new_from_stream_async:
1657
 
 * @stream: a #GInputStream from which to load the pixbuf
1658
 
 * @cancellable: optional #GCancellable object, %NULL to ignore
1659
 
 * @callback: a #GAsyncReadyCallback to call when the the pixbuf is loaded
1660
 
 * @user_data: the data to pass to the callback function
1661
 
 *
1662
 
 * Creates a new pixbuf by asynchronously loading an image from an input stream.
1663
 
 *
1664
 
 * For more details see gdk_pixbuf_new_from_stream(), which is the synchronous
1665
 
 * version of this function.
1666
 
 *
1667
 
 * When the operation is finished, @callback will be called in the main thread.
1668
 
 * You can then call gdk_pixbuf_new_from_stream_finish() to get the result of the operation.
1669
 
 *
1670
 
 * Since: 2.24
1671
 
 **/
1672
 
void
1673
 
gdk_pixbuf_new_from_stream_async (GInputStream        *stream,
1674
 
                                  GCancellable        *cancellable,
1675
 
                                  GAsyncReadyCallback  callback,
1676
 
                                  gpointer             user_data)
1677
 
{
1678
 
        GSimpleAsyncResult *result;
1679
 
 
1680
 
        g_return_if_fail (G_IS_INPUT_STREAM (stream));
1681
 
        g_return_if_fail (callback != NULL);
1682
 
        g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
1683
 
 
1684
 
        result = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, gdk_pixbuf_new_from_stream_async);
1685
 
        g_simple_async_result_run_in_thread (result, (GSimpleAsyncThreadFunc) new_from_stream_thread, G_PRIORITY_DEFAULT, cancellable);
1686
 
        g_object_unref (result);
1687
 
}
1688
 
 
1689
 
/**
1690
 
 * gdk_pixbuf_new_from_stream_finish:
1691
 
 * @async_result: a #GAsyncResult
1692
 
 * @error: a #GError, or %NULL
1693
 
 *
1694
 
 * Finishes an asynchronous pixbuf creation operation started with
1695
 
 * gdk_pixbuf_new_from_stream_async().
1696
 
 *
1697
 
 * Return value: a #GdkPixbuf or %NULL on error. Free the returned
1698
 
 * object with g_object_unref().
1699
 
 *
1700
 
 * Since: 2.24
1701
 
 **/
1702
 
GdkPixbuf *
1703
 
gdk_pixbuf_new_from_stream_finish (GAsyncResult  *async_result,
1704
 
                                   GError       **error)
1705
 
{
1706
 
        GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (async_result);
1707
 
 
1708
 
        g_return_val_if_fail (G_IS_ASYNC_RESULT (async_result), NULL);
1709
 
        g_return_val_if_fail (!error || (error && !*error), NULL);
1710
 
        g_warn_if_fail (g_simple_async_result_get_source_tag (result) == gdk_pixbuf_new_from_stream_async ||
1711
 
                        g_simple_async_result_get_source_tag (result) == gdk_pixbuf_new_from_stream_at_scale_async);
1712
 
 
1713
 
        if (g_simple_async_result_propagate_error (result, error))
1714
 
                return NULL;
1715
 
 
1716
 
        return g_simple_async_result_get_op_res_gpointer (result);
1717
 
}
1718
 
 
1719
 
static void
1720
 
info_cb (GdkPixbufLoader *loader, 
1721
 
         int              width,
1722
 
         int              height,
1723
 
         gpointer         data)
1724
 
{
1725
 
        struct {
1726
 
                GdkPixbufFormat *format;
1727
 
                int width;
1728
 
                int height;
1729
 
        } *info = data;
1730
 
 
1731
 
        g_return_if_fail (width > 0 && height > 0);
1732
 
 
1733
 
        info->format = gdk_pixbuf_loader_get_format (loader);
1734
 
        info->width = width;
1735
 
        info->height = height;
1736
 
 
1737
 
        gdk_pixbuf_loader_set_size (loader, 0, 0);
1738
 
}
1739
 
 
1740
 
/**
1741
 
 * gdk_pixbuf_get_file_info:
1742
 
 * @filename: The name of the file to identify.
1743
 
 * @width: Return location for the width of the image, or %NULL
1744
 
 * @height: Return location for the height of the image, or %NULL
1745
 
 * 
1746
 
 * Parses an image file far enough to determine its format and size.
1747
 
 * 
1748
 
 * Returns: A #GdkPixbufFormat describing the image format of the file 
1749
 
 *    or %NULL if the image format wasn't recognized. The return value 
1750
 
 *    is owned by GdkPixbuf and should not be freed.
1751
 
 *
1752
 
 * Since: 2.4
1753
 
 **/
1754
 
GdkPixbufFormat *
1755
 
gdk_pixbuf_get_file_info (const gchar  *filename,
1756
 
                          gint         *width, 
1757
 
                          gint         *height)
1758
 
{
1759
 
        GdkPixbufLoader *loader;
1760
 
        guchar buffer[SNIFF_BUFFER_SIZE];
1761
 
        int length;
1762
 
        FILE *f;
1763
 
        struct {
1764
 
                GdkPixbufFormat *format;
1765
 
                gint width;
1766
 
                gint height;
1767
 
        } info;
1768
 
 
1769
 
        g_return_val_if_fail (filename != NULL, NULL);
1770
 
 
1771
 
        f = g_fopen (filename, "rb");
1772
 
        if (!f)
1773
 
                return NULL;
1774
 
 
1775
 
        loader = gdk_pixbuf_loader_new ();
1776
 
 
1777
 
        info.format = NULL;
1778
 
        info.width = -1;
1779
 
        info.height = -1;
1780
 
                
1781
 
        g_signal_connect (loader, "size-prepared", G_CALLBACK (info_cb), &info);
1782
 
 
1783
 
        while (!feof (f) && !ferror (f)) {
1784
 
                length = fread (buffer, 1, sizeof (buffer), f);
1785
 
                if (length > 0) {
1786
 
                        if (!gdk_pixbuf_loader_write (loader, buffer, length, NULL))
1787
 
                                break;
1788
 
                }
1789
 
                if (info.format != NULL)
1790
 
                        break;
1791
 
        }
1792
 
 
1793
 
        fclose (f);
1794
 
        gdk_pixbuf_loader_close (loader, NULL);
1795
 
        g_object_unref (loader);
1796
 
 
1797
 
        if (width) 
1798
 
                *width = info.width;
1799
 
        if (height) 
1800
 
                *height = info.height;
1801
 
 
1802
 
        return info.format;
1803
 
}
1804
 
 
1805
 
/**
1806
 
 * gdk_pixbuf_new_from_xpm_data:
1807
 
 * @data: Pointer to inline XPM data.
1808
 
 *
1809
 
 * Creates a new pixbuf by parsing XPM data in memory.  This data is commonly
1810
 
 * the result of including an XPM file into a program's C source.
1811
 
 *
1812
 
 * Return value: A newly-created pixbuf with a reference count of 1.
1813
 
 **/
1814
 
GdkPixbuf *
1815
 
gdk_pixbuf_new_from_xpm_data (const char **data)
1816
 
{
1817
 
        GdkPixbuf *(* load_xpm_data) (const char **data);
1818
 
        GdkPixbuf *pixbuf;
1819
 
        GError *error = NULL;
1820
 
        GdkPixbufModule *xpm_module;
1821
 
        gboolean locked;
1822
 
 
1823
 
        g_return_val_if_fail (data != NULL, NULL);
1824
 
 
1825
 
        xpm_module = _gdk_pixbuf_get_named_module ("xpm", &error);
1826
 
        if (xpm_module == NULL) {
1827
 
                g_warning ("Error loading XPM image loader: %s", error->message);
1828
 
                g_error_free (error);
1829
 
                return NULL;
1830
 
        }
1831
 
 
1832
 
        if (!_gdk_pixbuf_load_module (xpm_module, &error)) {
1833
 
                g_warning ("Error loading XPM image loader: %s", error->message);
1834
 
                g_error_free (error);
1835
 
                return NULL;
1836
 
        }
1837
 
 
1838
 
        locked = _gdk_pixbuf_lock (xpm_module);
1839
 
 
1840
 
        if (xpm_module->load_xpm_data == NULL) {
1841
 
                g_warning ("gdk-pixbuf XPM module lacks XPM data capability");
1842
 
                pixbuf = NULL;
1843
 
        } else {
1844
 
                load_xpm_data = xpm_module->load_xpm_data;
1845
 
                pixbuf = (* load_xpm_data) (data);
1846
 
        }
1847
 
        
1848
 
        if (locked)
1849
 
                _gdk_pixbuf_unlock (xpm_module);
1850
 
        return pixbuf;
1851
 
}
1852
 
 
1853
 
static void
1854
 
collect_save_options (va_list   opts,
1855
 
                      gchar  ***keys,
1856
 
                      gchar  ***vals)
1857
 
{
1858
 
  gchar *key;
1859
 
  gchar *val;
1860
 
  gchar *next;
1861
 
  gint count;
1862
 
 
1863
 
  count = 0;
1864
 
  *keys = NULL;
1865
 
  *vals = NULL;
1866
 
  
1867
 
  next = va_arg (opts, gchar*);
1868
 
  while (next)
1869
 
    {
1870
 
      key = next;
1871
 
      val = va_arg (opts, gchar*);
1872
 
 
1873
 
      ++count;
1874
 
 
1875
 
      /* woo, slow */
1876
 
      *keys = g_realloc (*keys, sizeof(gchar*) * (count + 1));
1877
 
      *vals = g_realloc (*vals, sizeof(gchar*) * (count + 1));
1878
 
      
1879
 
      (*keys)[count-1] = g_strdup (key);
1880
 
      (*vals)[count-1] = g_strdup (val);
1881
 
 
1882
 
      (*keys)[count] = NULL;
1883
 
      (*vals)[count] = NULL;
1884
 
      
1885
 
      next = va_arg (opts, gchar*);
1886
 
    }
1887
 
}
1888
 
 
1889
 
static gboolean
1890
 
save_to_file_callback (const gchar *buf,
1891
 
                       gsize        count,
1892
 
                       GError     **error,
1893
 
                       gpointer     data)
1894
 
{
1895
 
        FILE *filehandle = data;
1896
 
        gsize n;
1897
 
 
1898
 
        n = fwrite (buf, 1, count, filehandle);
1899
 
        if (n != count) {
1900
 
                gint save_errno = errno;
1901
 
                g_set_error (error,
1902
 
                             G_FILE_ERROR,
1903
 
                             g_file_error_from_errno (save_errno),
1904
 
                             _("Error writing to image file: %s"),
1905
 
                             g_strerror (save_errno));
1906
 
                return FALSE;
1907
 
        }
1908
 
        return TRUE;
1909
 
}
1910
 
 
1911
 
static gboolean
1912
 
gdk_pixbuf_real_save (GdkPixbuf     *pixbuf, 
1913
 
                      FILE          *filehandle, 
1914
 
                      const char    *type, 
1915
 
                      gchar        **keys,
1916
 
                      gchar        **values,
1917
 
                      GError       **error)
1918
 
{
1919
 
        gboolean ret;
1920
 
        GdkPixbufModule *image_module = NULL;       
1921
 
        gboolean locked;
1922
 
 
1923
 
        image_module = _gdk_pixbuf_get_named_module (type, error);
1924
 
 
1925
 
        if (image_module == NULL)
1926
 
                return FALSE;
1927
 
       
1928
 
        if (!_gdk_pixbuf_load_module (image_module, error))
1929
 
                return FALSE;
1930
 
 
1931
 
        locked = _gdk_pixbuf_lock (image_module);
1932
 
 
1933
 
        if (image_module->save) {
1934
 
                /* save normally */
1935
 
                ret = (* image_module->save) (filehandle, pixbuf,
1936
 
                                              keys, values,
1937
 
                                              error);
1938
 
        } else if (image_module->save_to_callback) {
1939
 
                /* save with simple callback */
1940
 
                ret = (* image_module->save_to_callback) (save_to_file_callback,
1941
 
                                                          filehandle, pixbuf,
1942
 
                                                          keys, values,
1943
 
                                                          error);
1944
 
        } else {
1945
 
                /* can't save */
1946
 
                g_set_error (error,
1947
 
                             GDK_PIXBUF_ERROR,
1948
 
                             GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
1949
 
                             _("This build of gdk-pixbuf does not support saving the image format: %s"),
1950
 
                             type);
1951
 
                ret = FALSE;
1952
 
        }
1953
 
 
1954
 
        if (locked)
1955
 
                _gdk_pixbuf_unlock (image_module);
1956
 
        return ret;
1957
 
}
1958
 
 
1959
 
#define TMP_FILE_BUF_SIZE 4096
1960
 
 
1961
 
static gboolean
1962
 
save_to_callback_with_tmp_file (GdkPixbufModule   *image_module,
1963
 
                                GdkPixbuf         *pixbuf,
1964
 
                                GdkPixbufSaveFunc  save_func,
1965
 
                                gpointer           user_data,
1966
 
                                gchar            **keys,
1967
 
                                gchar            **values,
1968
 
                                GError           **error)
1969
 
{
1970
 
        int fd;
1971
 
        FILE *f = NULL;
1972
 
        gboolean retval = FALSE;
1973
 
        gchar *buf = NULL;
1974
 
        gsize n;
1975
 
        gchar *filename = NULL;
1976
 
        gboolean locked;
1977
 
 
1978
 
        buf = g_try_malloc (TMP_FILE_BUF_SIZE);
1979
 
        if (buf == NULL) {
1980
 
                g_set_error_literal (error,
1981
 
                                     GDK_PIXBUF_ERROR,
1982
 
                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
1983
 
                                     _("Insufficient memory to save image to callback"));
1984
 
                goto end;
1985
 
        }
1986
 
 
1987
 
        fd = g_file_open_tmp ("gdkpixbuf-save-tmp.XXXXXX", &filename, error);
1988
 
        if (fd == -1)
1989
 
                goto end;
1990
 
        f = fdopen (fd, "wb+");
1991
 
        if (f == NULL) {
1992
 
                gint save_errno = errno;
1993
 
                g_set_error_literal (error,
1994
 
                                     G_FILE_ERROR,
1995
 
                                     g_file_error_from_errno (save_errno),
1996
 
                                     _("Failed to open temporary file"));
1997
 
                goto end;
1998
 
        }
1999
 
 
2000
 
        locked = _gdk_pixbuf_lock (image_module);
2001
 
        retval = (image_module->save) (f, pixbuf, keys, values, error);
2002
 
        if (locked)
2003
 
                _gdk_pixbuf_unlock (image_module);
2004
 
        if (!retval)
2005
 
                goto end;
2006
 
 
2007
 
        rewind (f);
2008
 
        for (;;) {
2009
 
                n = fread (buf, 1, TMP_FILE_BUF_SIZE, f);
2010
 
                if (n > 0) {
2011
 
                        if (!save_func (buf, n, error, user_data))
2012
 
                                goto end;
2013
 
                }
2014
 
                if (n != TMP_FILE_BUF_SIZE) 
2015
 
                        break;
2016
 
        }
2017
 
        if (ferror (f)) {
2018
 
                gint save_errno = errno;
2019
 
                g_set_error_literal (error,
2020
 
                                     G_FILE_ERROR,
2021
 
                                     g_file_error_from_errno (save_errno),
2022
 
                                     _("Failed to read from temporary file"));
2023
 
                goto end;
2024
 
        }
2025
 
        retval = TRUE;
2026
 
 
2027
 
 end:
2028
 
        /* cleanup and return retval */
2029
 
        if (f)
2030
 
                fclose (f);
2031
 
        if (filename) {
2032
 
                g_unlink (filename);
2033
 
                g_free (filename);
2034
 
        }
2035
 
        g_free (buf);
2036
 
 
2037
 
        return retval;
2038
 
}
2039
 
 
2040
 
static gboolean
2041
 
gdk_pixbuf_real_save_to_callback (GdkPixbuf         *pixbuf,
2042
 
                                  GdkPixbufSaveFunc  save_func,
2043
 
                                  gpointer           user_data,
2044
 
                                  const char        *type, 
2045
 
                                  gchar            **keys,
2046
 
                                  gchar            **values,
2047
 
                                  GError           **error)
2048
 
{
2049
 
        gboolean ret;
2050
 
        GdkPixbufModule *image_module = NULL;       
2051
 
        gboolean locked;
2052
 
 
2053
 
        image_module = _gdk_pixbuf_get_named_module (type, error);
2054
 
 
2055
 
        if (image_module == NULL)
2056
 
                return FALSE;
2057
 
       
2058
 
        if (!_gdk_pixbuf_load_module (image_module, error))
2059
 
                return FALSE;
2060
 
 
2061
 
        locked = _gdk_pixbuf_lock (image_module);
2062
 
 
2063
 
        if (image_module->save_to_callback) {
2064
 
                /* save normally */
2065
 
                ret = (* image_module->save_to_callback) (save_func, user_data, 
2066
 
                                                          pixbuf, keys, values,
2067
 
                                                          error);
2068
 
        } else if (image_module->save) {
2069
 
                /* use a temporary file */
2070
 
                ret = save_to_callback_with_tmp_file (image_module, pixbuf,
2071
 
                                                      save_func, user_data, 
2072
 
                                                      keys, values,
2073
 
                                                      error);
2074
 
        } else {
2075
 
                /* can't save */
2076
 
                g_set_error (error,
2077
 
                             GDK_PIXBUF_ERROR,
2078
 
                             GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
2079
 
                             _("This build of gdk-pixbuf does not support saving the image format: %s"),
2080
 
                             type);
2081
 
                ret = FALSE;
2082
 
        }
2083
 
 
2084
 
        if (locked)
2085
 
                _gdk_pixbuf_unlock (image_module);
2086
 
        return ret;
2087
 
}
2088
 
 
2089
 
 
2090
 
/**
2091
 
 * gdk_pixbuf_save:
2092
 
 * @pixbuf: a #GdkPixbuf.
2093
 
 * @filename: name of file to save.
2094
 
 * @type: name of file format.
2095
 
 * @error: (allow-none): return location for error, or %NULL
2096
 
 * @Varargs: list of key-value save options
2097
 
 *
2098
 
 * Saves pixbuf to a file in format @type. By default, "jpeg", "png", "ico" 
2099
 
 * and "bmp" are possible file formats to save in, but more formats may be
2100
 
 * installed. The list of all writable formats can be determined in the 
2101
 
 * following way:
2102
 
 *
2103
 
 * |[
2104
 
 * void add_if_writable (GdkPixbufFormat *data, GSList **list)
2105
 
 * {
2106
 
 *   if (gdk_pixbuf_format_is_writable (data))
2107
 
 *     *list = g_slist_prepend (*list, data);
2108
 
 * }
2109
 
 * 
2110
 
 * GSList *formats = gdk_pixbuf_get_formats ();
2111
 
 * GSList *writable_formats = NULL;
2112
 
 * g_slist_foreach (formats, add_if_writable, &writable_formats);
2113
 
 * g_slist_free (formats);
2114
 
 * ]|
2115
 
 *
2116
 
 * If @error is set, %FALSE will be returned. Possible errors include 
2117
 
 * those in the #GDK_PIXBUF_ERROR domain and those in the #G_FILE_ERROR domain.
2118
 
 *
2119
 
 * The variable argument list should be %NULL-terminated; if not empty,
2120
 
 * it should contain pairs of strings that modify the save
2121
 
 * parameters. For example:
2122
 
 * <informalexample><programlisting>
2123
 
 * gdk_pixbuf_save (pixbuf, handle, "jpeg", &amp;error,
2124
 
 *                  "quality", "100", NULL);
2125
 
 * </programlisting></informalexample>
2126
 
 *
2127
 
 * Currently only few parameters exist. JPEG images can be saved with a
2128
 
 * "quality" parameter; its value should be in the range [0,100].
2129
 
 *
2130
 
 * Text chunks can be attached to PNG images by specifying parameters of
2131
 
 * the form "tEXt::key", where key is an ASCII string of length 1-79.
2132
 
 * The values are UTF-8 encoded strings. The PNG compression level can
2133
 
 * be specified using the "compression" parameter; it's value is in an
2134
 
 * integer in the range of [0,9].
2135
 
 *
2136
 
 * ICC color profiles can also be embedded into PNG and TIFF images.
2137
 
 * The "icc-profile" value should be the complete ICC profile encoded
2138
 
 * into base64.
2139
 
 *
2140
 
 * <informalexample><programlisting>
2141
 
 * gchar *contents;
2142
 
 * gchar *contents_encode;
2143
 
 * gsize length;
2144
 
 * g_file_get_contents ("/home/hughsie/.color/icc/L225W.icm", &contents, &length, NULL);
2145
 
 * contents_encode = g_base64_encode ((const guchar *) contents, length);
2146
 
 * gdk_pixbuf_save (pixbuf, handle, "png", &amp;error,
2147
 
 *                  "icc-profile", contents_encode,
2148
 
 *                  NULL);
2149
 
 * </programlisting></informalexample>
2150
 
 *
2151
 
 * TIFF images recognize a "compression" option which acceps an integer value.
2152
 
 * Among the codecs are 1 None, 2 Huffman, 5 LZW, 7 JPEG and 8 Deflate, see
2153
 
 * the libtiff documentation and tiff.h for all supported codec values.
2154
 
 *
2155
 
 * ICO images can be saved in depth 16, 24, or 32, by using the "depth"
2156
 
 * parameter. When the ICO saver is given "x_hot" and "y_hot" parameters,
2157
 
 * it produces a CUR instead of an ICO.
2158
 
 *
2159
 
 * Return value: whether an error was set
2160
 
 **/
2161
 
 
2162
 
gboolean
2163
 
gdk_pixbuf_save (GdkPixbuf  *pixbuf, 
2164
 
                 const char *filename, 
2165
 
                 const char *type, 
2166
 
                 GError    **error,
2167
 
                 ...)
2168
 
{
2169
 
        gchar **keys = NULL;
2170
 
        gchar **values = NULL;
2171
 
        va_list args;
2172
 
        gboolean result;
2173
 
 
2174
 
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2175
 
        
2176
 
        va_start (args, error);
2177
 
        
2178
 
        collect_save_options (args, &keys, &values);
2179
 
        
2180
 
        va_end (args);
2181
 
 
2182
 
        result = gdk_pixbuf_savev (pixbuf, filename, type,
2183
 
                                   keys, values,
2184
 
                                   error);
2185
 
 
2186
 
        g_strfreev (keys);
2187
 
        g_strfreev (values);
2188
 
 
2189
 
        return result;
2190
 
}
2191
 
 
2192
 
#ifdef G_OS_WIN32
2193
 
 
2194
 
#undef gdk_pixbuf_save
2195
 
 
2196
 
gboolean
2197
 
gdk_pixbuf_save (GdkPixbuf  *pixbuf, 
2198
 
                 const char *filename, 
2199
 
                 const char *type, 
2200
 
                 GError    **error,
2201
 
                 ...)
2202
 
{
2203
 
        char *utf8_filename;
2204
 
        gchar **keys = NULL;
2205
 
        gchar **values = NULL;
2206
 
        va_list args;
2207
 
        gboolean result;
2208
 
 
2209
 
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2210
 
        
2211
 
        utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2212
 
 
2213
 
        if (utf8_filename == NULL)
2214
 
                return FALSE;
2215
 
 
2216
 
        va_start (args, error);
2217
 
        
2218
 
        collect_save_options (args, &keys, &values);
2219
 
        
2220
 
        va_end (args);
2221
 
 
2222
 
        result = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
2223
 
                                        keys, values,
2224
 
                                        error);
2225
 
 
2226
 
        g_free (utf8_filename);
2227
 
 
2228
 
        g_strfreev (keys);
2229
 
        g_strfreev (values);
2230
 
 
2231
 
        return result;
2232
 
}
2233
 
 
2234
 
#endif
2235
 
 
2236
 
/**
2237
 
 * gdk_pixbuf_savev:
2238
 
 * @pixbuf: a #GdkPixbuf.
2239
 
 * @filename: name of file to save.
2240
 
 * @type: name of file format.
2241
 
 * @option_keys: (array zero-terminated=1): name of options to set, %NULL-terminated
2242
 
 * @option_values: (array zero-terminated=1): values for named options
2243
 
 * @error: (allow-none): return location for error, or %NULL
2244
 
 *
2245
 
 * Saves pixbuf to a file in @type, which is currently "jpeg", "png", "tiff", "ico" or "bmp".
2246
 
 * If @error is set, %FALSE will be returned. 
2247
 
 * See gdk_pixbuf_save () for more details.
2248
 
 *
2249
 
 * Return value: whether an error was set
2250
 
 **/
2251
 
 
2252
 
gboolean
2253
 
gdk_pixbuf_savev (GdkPixbuf  *pixbuf, 
2254
 
                  const char *filename, 
2255
 
                  const char *type,
2256
 
                  char      **option_keys,
2257
 
                  char      **option_values,
2258
 
                  GError    **error)
2259
 
{
2260
 
        FILE *f = NULL;
2261
 
        gboolean result;
2262
 
       
2263
 
        g_return_val_if_fail (filename != NULL, FALSE);
2264
 
        g_return_val_if_fail (type != NULL, FALSE);
2265
 
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2266
 
       
2267
 
        f = g_fopen (filename, "wb");
2268
 
        
2269
 
        if (f == NULL) {
2270
 
                gint save_errno = errno;
2271
 
                gchar *display_name = g_filename_display_name (filename);
2272
 
                g_set_error (error,
2273
 
                             G_FILE_ERROR,
2274
 
                             g_file_error_from_errno (save_errno),
2275
 
                             _("Failed to open '%s' for writing: %s"),
2276
 
                             display_name,
2277
 
                             g_strerror (save_errno));
2278
 
                g_free (display_name);
2279
 
                return FALSE;
2280
 
        }
2281
 
 
2282
 
       
2283
 
       result = gdk_pixbuf_real_save (pixbuf, f, type,
2284
 
                                      option_keys, option_values,
2285
 
                                      error);
2286
 
       
2287
 
       
2288
 
       if (!result) {
2289
 
               g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
2290
 
               fclose (f);
2291
 
               g_unlink (filename);
2292
 
               return FALSE;
2293
 
       }
2294
 
 
2295
 
       if (fclose (f) < 0) {
2296
 
               gint save_errno = errno;
2297
 
               gchar *display_name = g_filename_display_name (filename);
2298
 
               g_set_error (error,
2299
 
                            G_FILE_ERROR,
2300
 
                            g_file_error_from_errno (save_errno),
2301
 
                            _("Failed to close '%s' while writing image, all data may not have been saved: %s"),
2302
 
                            display_name,
2303
 
                            g_strerror (save_errno));
2304
 
               g_free (display_name);
2305
 
               return FALSE;
2306
 
       }
2307
 
       
2308
 
       return TRUE;
2309
 
}
2310
 
 
2311
 
#ifdef G_OS_WIN32
2312
 
 
2313
 
#undef gdk_pixbuf_savev
2314
 
 
2315
 
gboolean
2316
 
gdk_pixbuf_savev (GdkPixbuf  *pixbuf, 
2317
 
                  const char *filename, 
2318
 
                  const char *type,
2319
 
                  char      **option_keys,
2320
 
                  char      **option_values,
2321
 
                  GError    **error)
2322
 
{
2323
 
        char *utf8_filename;
2324
 
        gboolean retval;
2325
 
 
2326
 
        g_return_val_if_fail (filename != NULL, FALSE);
2327
 
       
2328
 
        utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
2329
 
 
2330
 
        if (utf8_filename == NULL)
2331
 
                return FALSE;
2332
 
 
2333
 
        retval = gdk_pixbuf_savev_utf8 (pixbuf, utf8_filename, type,
2334
 
                                        option_keys, option_values, error);
2335
 
 
2336
 
        g_free (utf8_filename);
2337
 
 
2338
 
        return retval;
2339
 
}
2340
 
 
2341
 
#endif
2342
 
 
2343
 
/**
2344
 
 * gdk_pixbuf_save_to_callback:
2345
 
 * @pixbuf: a #GdkPixbuf.
2346
 
 * @save_func: (scope call): a function that is called to save each block of data that
2347
 
 *   the save routine generates.
2348
 
 * @user_data: user data to pass to the save function.
2349
 
 * @type: name of file format.
2350
 
 * @error: (allow-none): return location for error, or %NULL
2351
 
 * @Varargs: list of key-value save options
2352
 
 *
2353
 
 * Saves pixbuf in format @type by feeding the produced data to a 
2354
 
 * callback. Can be used when you want to store the image to something 
2355
 
 * other than a file, such as an in-memory buffer or a socket.  
2356
 
 * If @error is set, %FALSE will be returned. Possible errors
2357
 
 * include those in the #GDK_PIXBUF_ERROR domain and whatever the save
2358
 
 * function generates.
2359
 
 *
2360
 
 * See gdk_pixbuf_save() for more details.
2361
 
 *
2362
 
 * Return value: whether an error was set
2363
 
 *
2364
 
 * Since: 2.4
2365
 
 **/
2366
 
gboolean
2367
 
gdk_pixbuf_save_to_callback    (GdkPixbuf  *pixbuf,
2368
 
                                GdkPixbufSaveFunc save_func,
2369
 
                                gpointer user_data,
2370
 
                                const char *type, 
2371
 
                                GError    **error,
2372
 
                                ...)
2373
 
{
2374
 
        gchar **keys = NULL;
2375
 
        gchar **values = NULL;
2376
 
        va_list args;
2377
 
        gboolean result;
2378
 
        
2379
 
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2380
 
        
2381
 
        va_start (args, error);
2382
 
        
2383
 
        collect_save_options (args, &keys, &values);
2384
 
        
2385
 
        va_end (args);
2386
 
 
2387
 
        result = gdk_pixbuf_save_to_callbackv (pixbuf, save_func, user_data,
2388
 
                                               type, keys, values,
2389
 
                                               error);
2390
 
 
2391
 
        g_strfreev (keys);
2392
 
        g_strfreev (values);
2393
 
 
2394
 
        return result;
2395
 
}
2396
 
 
2397
 
/**
2398
 
 * gdk_pixbuf_save_to_callbackv:
2399
 
 * @pixbuf: a #GdkPixbuf.
2400
 
 * @save_func: (scope call): a function that is called to save each block of data that
2401
 
 *   the save routine generates.
2402
 
 * @user_data: (closure save_func): user data to pass to the save function.
2403
 
 * @type: name of file format.
2404
 
 * @option_keys: (array zero-terminated=1) (element-type utf8): name of options to set, %NULL-terminated
2405
 
 * @option_values: (array zero-terminated=1) (element-type utf8): values for named options
2406
 
 * @error: (allow-none): return location for error, or %NULL
2407
 
 *
2408
 
 * Saves pixbuf to a callback in format @type, which is currently "jpeg",
2409
 
 * "png", "tiff", "ico" or "bmp".  If @error is set, %FALSE will be returned. See
2410
 
 * gdk_pixbuf_save_to_callback () for more details.
2411
 
 *
2412
 
 * Return value: whether an error was set
2413
 
 *
2414
 
 * Since: 2.4
2415
 
 **/
2416
 
gboolean
2417
 
gdk_pixbuf_save_to_callbackv   (GdkPixbuf  *pixbuf, 
2418
 
                                GdkPixbufSaveFunc save_func,
2419
 
                                gpointer user_data,
2420
 
                                const char *type,
2421
 
                                char      **option_keys,
2422
 
                                char      **option_values,
2423
 
                                GError    **error)
2424
 
{
2425
 
        gboolean result;
2426
 
        
2427
 
       
2428
 
        g_return_val_if_fail (save_func != NULL, FALSE);
2429
 
        g_return_val_if_fail (type != NULL, FALSE);
2430
 
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2431
 
       
2432
 
       result = gdk_pixbuf_real_save_to_callback (pixbuf,
2433
 
                                                  save_func, user_data, type,
2434
 
                                                  option_keys, option_values,
2435
 
                                                  error);
2436
 
       
2437
 
       if (!result) {
2438
 
               g_return_val_if_fail (error == NULL || *error != NULL, FALSE);
2439
 
               return FALSE;
2440
 
       }
2441
 
 
2442
 
       return TRUE;
2443
 
}
2444
 
 
2445
 
/**
2446
 
 * gdk_pixbuf_save_to_buffer:
2447
 
 * @pixbuf: a #GdkPixbuf.
2448
 
 * @buffer: location to receive a pointer to the new buffer.
2449
 
 * @buffer_size: location to receive the size of the new buffer.
2450
 
 * @type: name of file format.
2451
 
 * @error: (allow-none): return location for error, or %NULL
2452
 
 * @Varargs: list of key-value save options
2453
 
 *
2454
 
 * Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2455
 
 * "png", "tiff", "ico" or "bmp".  This is a convenience function that uses
2456
 
 * gdk_pixbuf_save_to_callback() to do the real work. Note that the buffer 
2457
 
 * is not nul-terminated and may contain embedded  nuls.
2458
 
 * If @error is set, %FALSE will be returned and @buffer will be set to
2459
 
 * %NULL. Possible errors include those in the #GDK_PIXBUF_ERROR
2460
 
 * domain.
2461
 
 *
2462
 
 * See gdk_pixbuf_save() for more details.
2463
 
 *
2464
 
 * Return value: whether an error was set
2465
 
 *
2466
 
 * Since: 2.4
2467
 
 **/
2468
 
gboolean
2469
 
gdk_pixbuf_save_to_buffer      (GdkPixbuf  *pixbuf,
2470
 
                                gchar     **buffer,
2471
 
                                gsize      *buffer_size,
2472
 
                                const char *type, 
2473
 
                                GError    **error,
2474
 
                                ...)
2475
 
{
2476
 
        gchar **keys = NULL;
2477
 
        gchar **values = NULL;
2478
 
        va_list args;
2479
 
        gboolean result;
2480
 
        
2481
 
        g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2482
 
        
2483
 
        va_start (args, error);
2484
 
        
2485
 
        collect_save_options (args, &keys, &values);
2486
 
        
2487
 
        va_end (args);
2488
 
 
2489
 
        result = gdk_pixbuf_save_to_bufferv (pixbuf, buffer, buffer_size,
2490
 
                                             type, keys, values,
2491
 
                                             error);
2492
 
 
2493
 
        g_strfreev (keys);
2494
 
        g_strfreev (values);
2495
 
 
2496
 
        return result;
2497
 
}
2498
 
 
2499
 
struct SaveToBufferData {
2500
 
        gchar *buffer;
2501
 
        gsize len, max;
2502
 
};
2503
 
 
2504
 
static gboolean
2505
 
save_to_buffer_callback (const gchar *data,
2506
 
                         gsize count,
2507
 
                         GError **error,
2508
 
                         gpointer user_data)
2509
 
{
2510
 
        struct SaveToBufferData *sdata = user_data;
2511
 
        gchar *new_buffer;
2512
 
        gsize new_max;
2513
 
 
2514
 
        if (sdata->len + count > sdata->max) {
2515
 
                new_max = MAX (sdata->max*2, sdata->len + count);
2516
 
                new_buffer = g_try_realloc (sdata->buffer, new_max);
2517
 
                if (!new_buffer) {
2518
 
                        g_set_error_literal (error,
2519
 
                                             GDK_PIXBUF_ERROR,
2520
 
                                             GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2521
 
                                             _("Insufficient memory to save image into a buffer"));
2522
 
                        return FALSE;
2523
 
                }
2524
 
                sdata->buffer = new_buffer;
2525
 
                sdata->max = new_max;
2526
 
        }
2527
 
        memcpy (sdata->buffer + sdata->len, data, count);
2528
 
        sdata->len += count;
2529
 
        return TRUE;
2530
 
}
2531
 
 
2532
 
/**
2533
 
 * gdk_pixbuf_save_to_bufferv:
2534
 
 * @pixbuf: a #GdkPixbuf.
2535
 
 * @buffer: location to receive a pointer to the new buffer.
2536
 
 * @buffer_size: location to receive the size of the new buffer.
2537
 
 * @type: name of file format.
2538
 
 * @option_keys: (array zero-terminated=1): name of options to set, %NULL-terminated
2539
 
 * @option_values: (array zero-terminated=1): values for named options
2540
 
 * @error: (allow-none): return location for error, or %NULL
2541
 
 *
2542
 
 * Saves pixbuf to a new buffer in format @type, which is currently "jpeg",
2543
 
 * "tiff", "png", "ico" or "bmp".  See gdk_pixbuf_save_to_buffer() 
2544
 
 * for more details.
2545
 
 *
2546
 
 * Return value: whether an error was set
2547
 
 *
2548
 
 * Since: 2.4
2549
 
 **/
2550
 
gboolean
2551
 
gdk_pixbuf_save_to_bufferv     (GdkPixbuf  *pixbuf,
2552
 
                                gchar     **buffer,
2553
 
                                gsize      *buffer_size,
2554
 
                                const char *type, 
2555
 
                                char      **option_keys,
2556
 
                                char      **option_values,
2557
 
                                GError    **error)
2558
 
{
2559
 
        static const gint initial_max = 1024;
2560
 
        struct SaveToBufferData sdata;
2561
 
 
2562
 
        *buffer = NULL;
2563
 
        *buffer_size = 0;
2564
 
 
2565
 
        sdata.buffer = g_try_malloc (initial_max);
2566
 
        sdata.max = initial_max;
2567
 
        sdata.len = 0;
2568
 
        if (!sdata.buffer) {
2569
 
                g_set_error_literal (error,
2570
 
                                     GDK_PIXBUF_ERROR,
2571
 
                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
2572
 
                                     _("Insufficient memory to save image into a buffer"));
2573
 
                return FALSE;
2574
 
        }
2575
 
 
2576
 
        if (!gdk_pixbuf_save_to_callbackv (pixbuf,
2577
 
                                           save_to_buffer_callback, &sdata,
2578
 
                                           type, option_keys, option_values,
2579
 
                                           error)) {
2580
 
                g_free (sdata.buffer);
2581
 
                return FALSE;
2582
 
        }
2583
 
 
2584
 
        *buffer = sdata.buffer;
2585
 
        *buffer_size = sdata.len;
2586
 
        return TRUE;
2587
 
}
2588
 
 
2589
 
typedef struct {
2590
 
        GOutputStream *stream;
2591
 
        GCancellable  *cancellable;
2592
 
} SaveToStreamData;
2593
 
 
2594
 
static gboolean
2595
 
save_to_stream (const gchar  *buffer,
2596
 
                gsize         count,
2597
 
                GError      **error,
2598
 
                gpointer      data)
2599
 
{
2600
 
        SaveToStreamData *sdata = (SaveToStreamData *)data;
2601
 
        gsize remaining;
2602
 
        gssize written;
2603
 
        GError *my_error = NULL;
2604
 
 
2605
 
        remaining = count;
2606
 
        written = 0;
2607
 
        while (remaining > 0) {
2608
 
                buffer += written;
2609
 
                remaining -= written;
2610
 
                written = g_output_stream_write (sdata->stream, 
2611
 
                                                 buffer, remaining, 
2612
 
                                                 sdata->cancellable, 
2613
 
                                                 &my_error);
2614
 
                if (written < 0) {
2615
 
                        if (!my_error) {
2616
 
                                g_set_error_literal (error,
2617
 
                                                     G_IO_ERROR, 0,
2618
 
                                                     _("Error writing to image stream"));
2619
 
                        }
2620
 
                        else {
2621
 
                                g_propagate_error (error, my_error);
2622
 
                        }
2623
 
                        return FALSE;
2624
 
                }
2625
 
        }
2626
 
 
2627
 
        return TRUE;
2628
 
}
2629
 
 
2630
 
/** 
2631
 
 * gdk_pixbuf_save_to_stream:
2632
 
 * @pixbuf: a #GdkPixbuf
2633
 
 * @stream: a #GOutputStream to save the pixbuf to
2634
 
 * @type: name of file format
2635
 
 * @cancellable: optional #GCancellable object, %NULL to ignore
2636
 
 * @error: (allow-none): return location for error, or %NULL
2637
 
 * @Varargs: list of key-value save options
2638
 
 *
2639
 
 * Saves @pixbuf to an output stream.
2640
 
 *
2641
 
 * Supported file formats are currently "jpeg", "tiff", "png", "ico" or 
2642
 
 * "bmp". See gdk_pixbuf_save_to_buffer() for more details.
2643
 
 *
2644
 
 * The @cancellable can be used to abort the operation from another 
2645
 
 * thread. If the operation was cancelled, the error %GIO_ERROR_CANCELLED 
2646
 
 * will be returned. Other possible errors are in the #GDK_PIXBUF_ERROR 
2647
 
 * and %G_IO_ERROR domains. 
2648
 
 *
2649
 
 * The stream is not closed.
2650
 
 *
2651
 
 * Returns: %TRUE if the pixbuf was saved successfully, %FALSE if an
2652
 
 *     error was set.
2653
 
 *
2654
 
 * Since: 2.14
2655
 
 */
2656
 
gboolean
2657
 
gdk_pixbuf_save_to_stream (GdkPixbuf      *pixbuf,
2658
 
                           GOutputStream  *stream,
2659
 
                           const char     *type,
2660
 
                           GCancellable   *cancellable,
2661
 
                           GError        **error,
2662
 
                           ...)
2663
 
{
2664
 
        gboolean res;
2665
 
        gchar **keys = NULL;
2666
 
        gchar **values = NULL;
2667
 
        va_list args;
2668
 
        SaveToStreamData data;
2669
 
 
2670
 
        va_start (args, error);
2671
 
        collect_save_options (args, &keys, &values);
2672
 
        va_end (args);
2673
 
 
2674
 
        data.stream = stream;
2675
 
        data.cancellable = cancellable;
2676
 
 
2677
 
        res = gdk_pixbuf_save_to_callbackv (pixbuf, save_to_stream, 
2678
 
                                            &data, type, 
2679
 
                                            keys, values, 
2680
 
                                            error);
2681
 
 
2682
 
        g_strfreev (keys);
2683
 
        g_strfreev (values);
2684
 
 
2685
 
        return res;
2686
 
}
2687
 
 
2688
 
typedef struct {
2689
 
        GOutputStream *stream;
2690
 
        gchar *type;
2691
 
        gchar **keys;
2692
 
        gchar **values;
2693
 
} SaveToStreamAsyncData;
2694
 
 
2695
 
static void
2696
 
save_to_stream_async_data_free (SaveToStreamAsyncData *data)
2697
 
{
2698
 
        if (data->stream)
2699
 
                g_object_unref (data->stream);
2700
 
        g_strfreev (data->keys);
2701
 
        g_strfreev (data->values);
2702
 
        g_free (data->type);
2703
 
        g_slice_free (SaveToStreamAsyncData, data);
2704
 
}
2705
 
 
2706
 
static void
2707
 
save_to_stream_thread (GSimpleAsyncResult *result,
2708
 
                       GdkPixbuf          *pixbuf,
2709
 
                       GCancellable       *cancellable)
2710
 
{
2711
 
        SaveToStreamAsyncData *data;
2712
 
        SaveToStreamData sync_data;
2713
 
        gboolean retval;
2714
 
        GError *error = NULL;
2715
 
 
2716
 
        data = g_simple_async_result_get_op_res_gpointer (result);
2717
 
        sync_data.stream = data->stream;
2718
 
        sync_data.cancellable = cancellable;
2719
 
 
2720
 
        retval = gdk_pixbuf_save_to_callbackv (pixbuf, save_to_stream,
2721
 
                                               &sync_data, data->type,
2722
 
                                               data->keys, data->values,
2723
 
                                               &error);
2724
 
 
2725
 
        /* Set the new pixbuf as the result, or error out */
2726
 
        if (retval == FALSE) {
2727
 
                g_simple_async_result_take_error (result, error);
2728
 
        } else {
2729
 
                g_simple_async_result_set_op_res_gboolean (result, TRUE);
2730
 
        }
2731
 
}
2732
 
 
2733
 
/**
2734
 
 * gdk_pixbuf_save_to_stream_async:
2735
 
 * @pixbuf: a #GdkPixbuf
2736
 
 * @stream: a #GOutputStream to which to save the pixbuf
2737
 
 * @type: name of file format
2738
 
 * @cancellable: optional #GCancellable object, %NULL to ignore
2739
 
 * @callback: a #GAsyncReadyCallback to call when the the pixbuf is loaded
2740
 
 * @user_data: the data to pass to the callback function
2741
 
 * @Varargs: list of key-value save options
2742
 
 *
2743
 
 * Saves @pixbuf to an output stream asynchronously.
2744
 
 *
2745
 
 * For more details see gdk_pixbuf_save_to_stream(), which is the synchronous
2746
 
 * version of this function.
2747
 
 *
2748
 
 * When the operation is finished, @callback will be called in the main thread.
2749
 
 * You can then call gdk_pixbuf_save_to_stream_finish() to get the result of the operation.
2750
 
 *
2751
 
 * Since: 2.24
2752
 
 **/
2753
 
void
2754
 
gdk_pixbuf_save_to_stream_async (GdkPixbuf           *pixbuf,
2755
 
                                 GOutputStream       *stream,
2756
 
                                 const gchar         *type,
2757
 
                                 GCancellable        *cancellable,
2758
 
                                 GAsyncReadyCallback  callback,
2759
 
                                 gpointer             user_data,
2760
 
                                 ...)
2761
 
{
2762
 
        GSimpleAsyncResult *result;
2763
 
        gchar **keys = NULL;
2764
 
        gchar **values = NULL;
2765
 
        va_list args;
2766
 
        SaveToStreamAsyncData *data;
2767
 
 
2768
 
        g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
2769
 
        g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
2770
 
        g_return_if_fail (type != NULL);
2771
 
        g_return_if_fail (callback != NULL);
2772
 
        g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
2773
 
 
2774
 
        va_start (args, user_data);
2775
 
        collect_save_options (args, &keys, &values);
2776
 
        va_end (args);
2777
 
 
2778
 
        data = g_slice_new (SaveToStreamAsyncData);
2779
 
        data->stream = g_object_ref (stream);
2780
 
        data->type = g_strdup (type);
2781
 
        data->keys = keys;
2782
 
        data->values = values;
2783
 
 
2784
 
        result = g_simple_async_result_new (G_OBJECT (pixbuf), callback, user_data, gdk_pixbuf_save_to_stream_async);
2785
 
        g_simple_async_result_set_op_res_gpointer (result, data, (GDestroyNotify) save_to_stream_async_data_free);
2786
 
        g_simple_async_result_run_in_thread (result, (GSimpleAsyncThreadFunc) save_to_stream_thread, G_PRIORITY_DEFAULT, cancellable);
2787
 
        g_object_unref (result);
2788
 
}
2789
 
 
2790
 
/**
2791
 
 * gdk_pixbuf_save_to_stream_finish:
2792
 
 * @async_result: a #GAsyncResult
2793
 
 * @error: a #GError, or %NULL
2794
 
 *
2795
 
 * Finishes an asynchronous pixbuf save operation started with
2796
 
 * gdk_pixbuf_save_to_stream_async().
2797
 
 *
2798
 
 * Return value: %TRUE if the pixbuf was saved successfully, %FALSE if an error was set.
2799
 
 *
2800
 
 * Since: 2.24
2801
 
 **/
2802
 
gboolean
2803
 
gdk_pixbuf_save_to_stream_finish (GAsyncResult  *async_result,
2804
 
                                  GError       **error)
2805
 
{
2806
 
        GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT (async_result);
2807
 
 
2808
 
        g_return_val_if_fail (G_IS_ASYNC_RESULT (async_result), FALSE);
2809
 
        g_return_val_if_fail (!error || (error && !*error), FALSE);
2810
 
        g_warn_if_fail (g_simple_async_result_get_source_tag (result) == gdk_pixbuf_save_to_stream_async);
2811
 
 
2812
 
        if (g_simple_async_result_propagate_error (result, error))
2813
 
                return FALSE;
2814
 
 
2815
 
        return g_simple_async_result_get_op_res_gboolean (result);
2816
 
}
2817
 
 
2818
 
/**
2819
 
 * gdk_pixbuf_format_get_name:
2820
 
 * @format: a #GdkPixbufFormat
2821
 
 *
2822
 
 * Returns the name of the format.
2823
 
 * 
2824
 
 * Return value: the name of the format. 
2825
 
 *
2826
 
 * Since: 2.2
2827
 
 */
2828
 
gchar *
2829
 
gdk_pixbuf_format_get_name (GdkPixbufFormat *format)
2830
 
{
2831
 
        g_return_val_if_fail (format != NULL, NULL);
2832
 
 
2833
 
        return g_strdup (format->name);
2834
 
}
2835
 
 
2836
 
/**
2837
 
 * gdk_pixbuf_format_get_description:
2838
 
 * @format: a #GdkPixbufFormat
2839
 
 *
2840
 
 * Returns a description of the format.
2841
 
 * 
2842
 
 * Return value: a description of the format.
2843
 
 *
2844
 
 * Since: 2.2
2845
 
 */
2846
 
gchar *
2847
 
gdk_pixbuf_format_get_description (GdkPixbufFormat *format)
2848
 
{
2849
 
        gchar *domain;
2850
 
        const gchar *description;
2851
 
        g_return_val_if_fail (format != NULL, NULL);
2852
 
 
2853
 
        if (format->domain != NULL) 
2854
 
                domain = format->domain;
2855
 
        else 
2856
 
                domain = GETTEXT_PACKAGE;
2857
 
        description = g_dgettext (domain, format->description);
2858
 
 
2859
 
        return g_strdup (description);
2860
 
}
2861
 
 
2862
 
/**
2863
 
 * gdk_pixbuf_format_get_mime_types:
2864
 
 * @format: a #GdkPixbufFormat
2865
 
 *
2866
 
 * Returns the mime types supported by the format.
2867
 
 * 
2868
 
 * Return value: (transfer full): a %NULL-terminated array of mime types which must be freed with 
2869
 
 * g_strfreev() when it is no longer needed.
2870
 
 *
2871
 
 * Since: 2.2
2872
 
 */
2873
 
gchar **
2874
 
gdk_pixbuf_format_get_mime_types (GdkPixbufFormat *format)
2875
 
{
2876
 
        g_return_val_if_fail (format != NULL, NULL);
2877
 
 
2878
 
        return g_strdupv (format->mime_types);
2879
 
}
2880
 
 
2881
 
/**
2882
 
 * gdk_pixbuf_format_get_extensions:
2883
 
 * @format: a #GdkPixbufFormat
2884
 
 *
2885
 
 * Returns the filename extensions typically used for files in the 
2886
 
 * given format.
2887
 
 * 
2888
 
 * Return value: (transfer full): a %NULL-terminated array of filename extensions which must be
2889
 
 * freed with g_strfreev() when it is no longer needed.
2890
 
 *
2891
 
 * Since: 2.2
2892
 
 */
2893
 
gchar **
2894
 
gdk_pixbuf_format_get_extensions (GdkPixbufFormat *format)
2895
 
{
2896
 
        g_return_val_if_fail (format != NULL, NULL);
2897
 
 
2898
 
        return g_strdupv (format->extensions);
2899
 
}
2900
 
 
2901
 
/**
2902
 
 * gdk_pixbuf_format_is_writable:
2903
 
 * @format: a #GdkPixbufFormat
2904
 
 *
2905
 
 * Returns whether pixbufs can be saved in the given format.
2906
 
 * 
2907
 
 * Return value: whether pixbufs can be saved in the given format.
2908
 
 *
2909
 
 * Since: 2.2
2910
 
 */
2911
 
gboolean
2912
 
gdk_pixbuf_format_is_writable (GdkPixbufFormat *format)
2913
 
{
2914
 
        g_return_val_if_fail (format != NULL, FALSE);
2915
 
 
2916
 
        return (format->flags & GDK_PIXBUF_FORMAT_WRITABLE) != 0;
2917
 
}
2918
 
 
2919
 
/**
2920
 
 * gdk_pixbuf_format_is_scalable:
2921
 
 * @format: a #GdkPixbufFormat
2922
 
 *
2923
 
 * Returns whether this image format is scalable. If a file is in a 
2924
 
 * scalable format, it is preferable to load it at the desired size, 
2925
 
 * rather than loading it at the default size and scaling the 
2926
 
 * resulting pixbuf to the desired size.
2927
 
 * 
2928
 
 * Return value: whether this image format is scalable.
2929
 
 *
2930
 
 * Since: 2.6
2931
 
 */
2932
 
gboolean
2933
 
gdk_pixbuf_format_is_scalable (GdkPixbufFormat *format)
2934
 
{
2935
 
        g_return_val_if_fail (format != NULL, FALSE);
2936
 
 
2937
 
        return (format->flags & GDK_PIXBUF_FORMAT_SCALABLE) != 0;
2938
 
}
2939
 
 
2940
 
/**
2941
 
 * gdk_pixbuf_format_is_disabled:
2942
 
 * @format: a #GdkPixbufFormat
2943
 
 *
2944
 
 * Returns whether this image format is disabled. See
2945
 
 * gdk_pixbuf_format_set_disabled().
2946
 
 * 
2947
 
 * Return value: whether this image format is disabled.
2948
 
 *
2949
 
 * Since: 2.6
2950
 
 */
2951
 
gboolean   
2952
 
gdk_pixbuf_format_is_disabled (GdkPixbufFormat *format)
2953
 
{
2954
 
        g_return_val_if_fail (format != NULL, FALSE);
2955
 
 
2956
 
        return format->disabled;        
2957
 
}
2958
 
 
2959
 
/**
2960
 
 * gdk_pixbuf_format_set_disabled:
2961
 
 * @format: a #GdkPixbufFormat
2962
 
 * @disabled: %TRUE to disable the format @format
2963
 
 *
2964
 
 * Disables or enables an image format. If a format is disabled, 
2965
 
 * gdk-pixbuf won't use the image loader for this format to load 
2966
 
 * images. Applications can use this to avoid using image loaders 
2967
 
 * with an inappropriate license, see gdk_pixbuf_format_get_license().
2968
 
 *
2969
 
 * Since: 2.6
2970
 
 */
2971
 
void 
2972
 
gdk_pixbuf_format_set_disabled (GdkPixbufFormat *format,
2973
 
                                gboolean         disabled)
2974
 
{
2975
 
        g_return_if_fail (format != NULL);
2976
 
        
2977
 
        format->disabled = disabled != FALSE;
2978
 
}
2979
 
 
2980
 
/**
2981
 
 * gdk_pixbuf_format_get_license:
2982
 
 * @format: a #GdkPixbufFormat
2983
 
 *
2984
 
 * Returns information about the license of the image loader for the format. The
2985
 
 * returned string should be a shorthand for a wellknown license, e.g. "LGPL",
2986
 
 * "GPL", "QPL", "GPL/QPL", or "other" to indicate some other license.  This
2987
 
 * string should be freed with g_free() when it's no longer needed.
2988
 
 *
2989
 
 * Returns: a string describing the license of @format. 
2990
 
 *
2991
 
 * Since: 2.6
2992
 
 */
2993
 
gchar*
2994
 
gdk_pixbuf_format_get_license (GdkPixbufFormat *format)
2995
 
{
2996
 
        g_return_val_if_fail (format != NULL, NULL);
2997
 
 
2998
 
        return g_strdup (format->license);
2999
 
}
3000
 
 
3001
 
GdkPixbufFormat *
3002
 
_gdk_pixbuf_get_format (GdkPixbufModule *module)
3003
 
{
3004
 
        g_return_val_if_fail (module != NULL, NULL);
3005
 
 
3006
 
        return module->info;
3007
 
}
3008
 
 
3009
 
/**
3010
 
 * gdk_pixbuf_get_formats:
3011
 
 *
3012
 
 * Obtains the available information about the image formats supported
3013
 
 * by GdkPixbuf.
3014
 
 *
3015
 
 * Returns: (transfer container) (element-type GdkPixbufFormat): A list of
3016
 
 * #GdkPixbufFormat<!-- -->s describing the supported
3017
 
 * image formats.  The list should be freed when it is no longer needed,
3018
 
 * but the structures themselves are owned by #GdkPixbuf and should not be
3019
 
 * freed.
3020
 
 *
3021
 
 * Since: 2.2
3022
 
 */
3023
 
GSList *
3024
 
gdk_pixbuf_get_formats (void)
3025
 
{
3026
 
        GSList *result = NULL;
3027
 
        GSList *modules;
3028
 
 
3029
 
        for (modules = get_file_formats (); modules; modules = g_slist_next (modules)) {
3030
 
                GdkPixbufModule *module = (GdkPixbufModule *)modules->data;
3031
 
                GdkPixbufFormat *info = _gdk_pixbuf_get_format (module);
3032
 
                result = g_slist_prepend (result, info);
3033
 
        }
3034
 
 
3035
 
        return result;
3036
 
}
3037
 
 
3038
 
/**
3039
 
 * gdk_pixbuf_format_copy:
3040
 
 * @format: a #GdkPixbufFormat
3041
 
 *
3042
 
 * Creates a copy of @format
3043
 
 *
3044
 
 * Return value: the newly allocated copy of a #GdkPixbufFormat. Use
3045
 
 *   gdk_pixbuf_format_free() to free the resources when done
3046
 
 *
3047
 
 * Since: 2.22
3048
 
 */
3049
 
GdkPixbufFormat *
3050
 
gdk_pixbuf_format_copy (const GdkPixbufFormat *format)
3051
 
{
3052
 
        if (G_LIKELY (format != NULL))
3053
 
                return g_slice_dup (GdkPixbufFormat, format);
3054
 
 
3055
 
        return NULL;
3056
 
}
3057
 
 
3058
 
/**
3059
 
 * gdk_pixbuf_format_free:
3060
 
 * @format: a #GdkPixbufFormat
3061
 
 *
3062
 
 * Frees the resources allocated when copying a #GdkPixbufFormat
3063
 
 * using gdk_pixbuf_format_copy()
3064
 
 *
3065
 
 * Since: 2.22
3066
 
 */
3067
 
void
3068
 
gdk_pixbuf_format_free (GdkPixbufFormat *format)
3069
 
{
3070
 
        if (G_LIKELY (format != NULL))
3071
 
                g_slice_free (GdkPixbufFormat, format);
3072
 
}
3073
 
 
3074
 
G_DEFINE_BOXED_TYPE (GdkPixbufFormat, gdk_pixbuf_format,
3075
 
                     gdk_pixbuf_format_copy,
3076
 
                     gdk_pixbuf_format_free)