]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-tiff.c
Cleanups
[~andy/gtk] / gdk-pixbuf / io-tiff.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - TIFF image loader
3  *
4  * Copyright (C) 1999 Mark Crichton
5  * Copyright (C) 1999 The Free Software Foundation
6  *
7  * Authors: Mark Crichton <crichton@gimp.org>
8  *          Federico Mena-Quintero <federico@gimp.org>
9  *          Jonathan Blandford <jrb@redhat.com>
10  *          Søren Sandmann <sandmann@daimi.au.dk>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the
24  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25  * Boston, MA 02111-1307, USA.
26  */
27
28 /* Following code (almost) blatantly ripped from Imlib */
29
30 #include <config.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <tiffio.h>
37 #include <errno.h>
38 #include "gdk-pixbuf-private.h"
39 #include "gdk-pixbuf-io.h"
40
41 #ifdef G_OS_WIN32
42 #include <fcntl.h>
43 #include <io.h>
44 #define lseek(a,b,c) _lseek(a,b,c)
45 #define O_RDWR _O_RDWR
46 #endif
47 \f
48
49 typedef struct _TiffContext TiffContext;
50 struct _TiffContext
51 {
52         GdkPixbufModuleSizeFunc size_func;
53         GdkPixbufModulePreparedFunc prepare_func;
54         GdkPixbufModuleUpdatedFunc update_func;
55         gpointer user_data;
56         
57         guchar *buffer;
58         guint allocated;
59         guint used;
60         guint pos;
61 };
62
63 \f
64
65 static char *global_error = NULL;
66 static TIFFErrorHandler orig_error_handler = NULL;
67 static TIFFErrorHandler orig_warning_handler = NULL;
68
69 static void
70 tiff_warning_handler (const char *mod, const char *fmt, va_list ap)
71 {
72         /* Don't print anything; we should not be dumping junk to
73          * stderr, since that may be bad for some apps.
74          */
75
76         /* libTIFF seems to occasionally warn about things that
77          * are really errors, so maybe we should just call tiff_error_handler
78          * here.
79          */
80 }
81
82 static void
83 tiff_error_handler (const char *mod, const char *fmt, va_list ap)
84 {
85         if (global_error) {                
86                 /* Blah, loader called us twice */
87                 return;
88         }
89
90         global_error = g_strdup_vprintf (fmt, ap);
91 }
92
93 static void
94 tiff_push_handlers (void)
95 {
96         if (global_error)
97                 g_warning ("TIFF loader left crufty global_error around, FIXME");
98         
99         orig_error_handler = TIFFSetErrorHandler (tiff_error_handler);
100         orig_warning_handler = TIFFSetWarningHandler (tiff_warning_handler);
101 }
102
103 static void
104 tiff_pop_handlers (void)
105 {
106         if (global_error)
107                 g_warning ("TIFF loader left crufty global_error around, FIXME");
108         
109         TIFFSetErrorHandler (orig_error_handler);
110         TIFFSetWarningHandler (orig_warning_handler);
111 }
112
113 static void
114 tiff_set_error (GError    **error,
115                 int         error_code,
116                 const char *msg)
117 {
118         /* Take the error message from libtiff and merge it with
119          * some context we provide.
120          */
121         if (global_error) {
122                 g_set_error (error,
123                              GDK_PIXBUF_ERROR,
124                              error_code,
125                              "%s%s%s", msg, ": ", global_error);
126
127                 g_free (global_error);
128                 global_error = NULL;
129         }
130         else {
131                 g_set_error (error,
132                              GDK_PIXBUF_ERROR,
133                              error_code, msg);
134         }
135 }
136
137 \f
138
139 static void free_buffer (guchar *pixels, gpointer data)
140 {
141         g_free (pixels);
142 }
143
144 #if TIFFLIB_VERSION >= 20031226
145 static gboolean tifflibversion (int *major, int *minor, int *revision)
146 {
147         if (sscanf (TIFFGetVersion(), 
148                     "LIBTIFF, Version %d.%d.%d", 
149                     major, minor, revision) < 3)
150                 return FALSE;
151
152         return TRUE;
153 }
154 #endif
155
156 static GdkPixbuf *
157 tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
158 {
159         guchar *pixels = NULL;
160         gint width, height, rowstride, bytes;
161         GdkPixbuf *pixbuf;
162 #if TIFFLIB_VERSION >= 20031226
163         gint major, minor, revision;
164 #endif
165
166         /* We're called with the lock held. */
167         
168         g_return_val_if_fail (global_error == NULL, NULL);
169
170         if (!TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width) || global_error) {
171                 tiff_set_error (error,
172                                 GDK_PIXBUF_ERROR_FAILED,
173                                 _("Could not get image width (bad TIFF file)"));
174                 return NULL;
175         }
176         
177         if (!TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height) || global_error) {
178                 tiff_set_error (error,
179                                 GDK_PIXBUF_ERROR_FAILED,
180                                 _("Could not get image height (bad TIFF file)"));
181                 return NULL;
182         }
183
184         if (width <= 0 || height <= 0) {
185                 g_set_error (error,
186                              GDK_PIXBUF_ERROR,
187                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
188                              _("Width or height of TIFF image is zero"));
189                 return NULL;                
190         }
191         
192         rowstride = width * 4;
193         if (rowstride / 4 != width) { /* overflow */
194                 g_set_error (error,
195                              GDK_PIXBUF_ERROR,
196                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
197                              _("Dimensions of TIFF image too large"));
198                 return NULL;                
199         }
200         
201         bytes = height * rowstride;
202         if (bytes / rowstride != height) { /* overflow */
203                 g_set_error (error,
204                              GDK_PIXBUF_ERROR,
205                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
206                              _("Dimensions of TIFF image too large"));
207                 return NULL;                
208         }
209
210         if (context && context->size_func) {
211                 gint w = width;
212                 gint h = height;
213                 (* context->size_func) (&w, &h, context->user_data);
214                 
215                 if (w == 0 || h == 0)
216                     return NULL;
217         }
218
219         pixels = g_try_malloc (bytes);
220
221         if (!pixels) {
222                 g_set_error (error,
223                              GDK_PIXBUF_ERROR,
224                              GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
225                              _("Insufficient memory to open TIFF file"));
226                 return NULL;
227         }
228
229         pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8, 
230                                            width, height, rowstride,
231                                            free_buffer, NULL);
232         if (!pixbuf) {
233                 g_free (pixels);
234                 g_set_error (error,
235                              GDK_PIXBUF_ERROR,
236                              GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
237                              _("Insufficient memory to open TIFF file"));
238                 return NULL;
239         }
240
241         if (context && context->prepare_func)
242                 (* context->prepare_func) (pixbuf, NULL, context->user_data);
243
244 #if TIFFLIB_VERSION >= 20031226
245         if (tifflibversion(&major, &minor, &revision) && major == 3 &&
246             (minor > 6 || (minor == 6 && revision > 0))) {                
247                 if (!TIFFReadRGBAImageOriented (tiff, width, height, (uint32 *)pixels, ORIENTATION_TOPLEFT, 1) || global_error) 
248                 {
249                         tiff_set_error (error,
250                                         GDK_PIXBUF_ERROR_FAILED,
251                                         _("Failed to load RGB data from TIFF file"));
252                         g_object_unref (pixbuf);
253                         return NULL;
254                 }
255
256 #if G_BYTE_ORDER == G_BIG_ENDIAN
257                 /* Turns out that the packing used by TIFFRGBAImage depends on 
258                  * the host byte order... 
259                  */ 
260                 while (pixels < pixbuf->pixels + bytes) {
261                         uint32 pixel = *(uint32 *)pixels;
262                         int r = TIFFGetR(pixel);
263                         int g = TIFFGetG(pixel);
264                         int b = TIFFGetB(pixel);
265                         int a = TIFFGetA(pixel);
266                         *pixels++ = r;
267                         *pixels++ = g;
268                         *pixels++ = b;
269                         *pixels++ = a;
270                 }
271 #endif
272         }
273         else 
274 #endif
275               {
276                 uint32 *rast, *tmp_rast;
277                 gint x, y;
278                 guchar *tmppix;
279
280                 /* Yes, it needs to be _TIFFMalloc... */
281                 rast = (uint32 *) _TIFFmalloc (width * height * sizeof (uint32));
282                 if (!rast) {
283                         g_set_error (error,
284                                      GDK_PIXBUF_ERROR,
285                                      GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
286                                      _("Insufficient memory to open TIFF file"));
287                         g_object_unref (pixbuf);
288                         
289                         return NULL;
290                 }
291                 if (!TIFFReadRGBAImage (tiff, width, height, rast, 1) || global_error) {
292                         tiff_set_error (error,
293                                         GDK_PIXBUF_ERROR_FAILED,
294                                         _("Failed to load RGB data from TIFF file"));
295                         g_object_unref (pixbuf);
296                         _TIFFfree (rast);
297                         
298                         return NULL;
299                 }
300                 
301                 pixels = gdk_pixbuf_get_pixels (pixbuf);
302                 
303                 g_assert (pixels);
304                 
305                 tmppix = pixels;
306                 
307                 for (y = 0; y < height; y++) {
308                         /* Unexplainable...are tiffs backwards? */
309                         /* Also looking at the GIMP plugin, this
310                          * whole reading thing can be a bit more
311                          * robust.
312                          */
313                         tmp_rast = rast + ((height - y - 1) * width);
314                         for (x = 0; x < width; x++) {
315                                 tmppix[0] = TIFFGetR (*tmp_rast);
316                                 tmppix[1] = TIFFGetG (*tmp_rast);
317                                 tmppix[2] = TIFFGetB (*tmp_rast);
318                                 tmppix[3] = TIFFGetA (*tmp_rast);
319                                 tmp_rast++;
320                                 tmppix += 4;
321                         }
322                 }
323                 
324                 _TIFFfree (rast);
325              }
326
327         if (context && context->update_func)
328                 (* context->update_func) (pixbuf, 0, 0, width, height, context->user_data);
329         
330         return pixbuf;
331 }
332
333 \f
334
335 /* Static loader */
336
337 static GdkPixbuf *
338 gdk_pixbuf__tiff_image_load (FILE *f, GError **error)
339 {
340         TIFF *tiff;
341         int fd;
342         GdkPixbuf *pixbuf;
343         
344         g_return_val_if_fail (f != NULL, NULL);
345
346         tiff_push_handlers ();
347         
348         fd = fileno (f);
349
350         /* On OSF, apparently fseek() works in some on-demand way, so
351          * the fseek gdk_pixbuf_new_from_file() doesn't work here
352          * since we are using the raw file descriptor. So, we call lseek() on the fd
353          * before using it. (#60840)
354          */
355         lseek (fd, 0, SEEK_SET);
356         tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
357         
358         if (!tiff || global_error) {
359                 tiff_set_error (error,
360                                 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
361                                 _("Failed to open TIFF image"));
362                 tiff_pop_handlers ();
363
364                 return NULL;
365         }
366
367         pixbuf = tiff_image_parse (tiff, NULL, error);
368         
369         TIFFClose (tiff);
370         if (global_error) {
371                 tiff_set_error (error,
372                                 GDK_PIXBUF_ERROR_FAILED,
373                                 _("TIFFClose operation failed"));
374         }
375         
376         tiff_pop_handlers ();
377
378         return pixbuf;
379 }
380
381 \f
382
383 /* Progressive loader */
384
385 static gpointer
386 gdk_pixbuf__tiff_image_begin_load (GdkPixbufModuleSizeFunc size_func,
387                                    GdkPixbufModulePreparedFunc prepare_func,
388                                    GdkPixbufModuleUpdatedFunc update_func,
389                                    gpointer user_data,
390                                    GError **error)
391 {
392         TiffContext *context;
393         
394         context = g_new0 (TiffContext, 1);
395         context->size_func = size_func;
396         context->prepare_func = prepare_func;
397         context->update_func = update_func;
398         context->user_data = user_data;
399         context->buffer = NULL;
400         context->allocated = 0;
401         context->used = 0;
402         context->pos = 0;
403         
404         return context;
405 }
406
407 static tsize_t
408 tiff_load_read (thandle_t handle, tdata_t buf, tsize_t size)
409 {
410         TiffContext *context = (TiffContext *)handle;
411         
412         if (context->pos + size > context->used)
413                 return 0;
414         
415         memcpy (buf, context->buffer + context->pos, size);
416         context->pos += size;
417         return size;
418 }
419
420 static tsize_t
421 tiff_load_write (thandle_t handle, tdata_t buf, tsize_t size)
422 {
423         return -1;
424 }
425
426 static toff_t
427 tiff_load_seek (thandle_t handle, toff_t offset, int whence)
428 {
429         TiffContext *context = (TiffContext *)handle;
430         
431         switch (whence) {
432         case SEEK_SET:
433                 if (offset > context->used)
434                         return -1;
435                 context->pos = offset;
436                 break;
437         case SEEK_CUR:
438                 if (offset + context->pos >= context->used)
439                         return -1;
440                 context->pos += offset;
441                 break;
442         case SEEK_END:
443                 if (offset + context->used > context->used)
444                         return -1;
445                 context->pos = context->used + offset;
446                 break;
447         default:
448                 return -1;
449         }
450         return context->pos;
451 }
452
453 static int
454 tiff_load_close (thandle_t context)
455 {
456         return 0;
457 }
458
459 static toff_t
460 tiff_load_size (thandle_t handle)
461 {
462         TiffContext *context = (TiffContext *)handle;
463         
464         return context->used;
465 }
466
467 static int
468 tiff_load_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
469 {
470         TiffContext *context = (TiffContext *)handle;
471         
472         *buf = context->buffer;
473         *size = context->used;
474         
475         return 0;
476 }
477
478 static void
479 tiff_load_unmap_file (thandle_t handle, tdata_t data, toff_t offset)
480 {
481 }
482
483 static gboolean
484 gdk_pixbuf__tiff_image_stop_load (gpointer data,
485                                   GError **error)
486 {
487         TiffContext *context = data;
488         TIFF *tiff;
489         gboolean retval;
490         
491         g_return_val_if_fail (data != NULL, FALSE);
492
493         tiff_push_handlers ();
494         
495         tiff = TIFFClientOpen ("libtiff-pixbuf", "r", data, 
496                                tiff_load_read, tiff_load_write, 
497                                tiff_load_seek, tiff_load_close, 
498                                tiff_load_size, 
499                                tiff_load_map_file, tiff_load_unmap_file);
500         if (!tiff || global_error) {
501                 tiff_set_error (error,
502                                 GDK_PIXBUF_ERROR_FAILED,
503                                 _("Failed to load TIFF image"));
504                 retval = FALSE;
505         } else {
506                 GdkPixbuf *pixbuf;
507                 
508                 pixbuf = tiff_image_parse (tiff, context, error);
509                 if (pixbuf)
510                         g_object_unref (pixbuf);
511                 retval = pixbuf != NULL;
512                 if (global_error)
513                         {
514                                 tiff_set_error (error,
515                                                 GDK_PIXBUF_ERROR_FAILED,
516                                                 _("Failed to load TIFF image"));
517                                 tiff_pop_handlers ();
518
519                                 retval = FALSE;
520                         }
521         }
522
523         if (tiff)
524                 TIFFClose (tiff);
525
526         g_assert (!global_error);
527         
528         g_free (context->buffer);
529         g_free (context);
530
531         tiff_pop_handlers ();
532
533         return retval;
534 }
535
536 static gboolean
537 make_available_at_least (TiffContext *context, guint needed)
538 {
539         guchar *new_buffer = NULL;
540         guint need_alloc;
541         
542         need_alloc = context->used + needed;
543         if (need_alloc > context->allocated) {
544                 guint new_size = 1;
545                 while (new_size < need_alloc)
546                         new_size *= 2;
547                 
548                 new_buffer = g_try_realloc (context->buffer, new_size);
549                 if (new_buffer) {
550                         context->buffer = new_buffer;
551                         context->allocated = new_size;
552                         return TRUE;
553                 }
554                 return FALSE;
555         }
556         return TRUE;
557 }
558
559 static gboolean
560 gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf,
561                                        guint size, GError **error)
562 {
563         TiffContext *context = (TiffContext *) data;
564         
565         g_return_val_if_fail (data != NULL, FALSE);
566         
567         if (!make_available_at_least (context, size)) {
568                 g_set_error (error,
569                              GDK_PIXBUF_ERROR,
570                              GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
571                              _("Insufficient memory to open TIFF file"));
572                 return FALSE;
573         }
574         
575         memcpy (context->buffer + context->used, buf, size);
576         context->used += size;
577         return TRUE;
578 }
579
580 typedef struct {
581         gchar *buffer;
582         guint allocated;
583         guint used;
584         guint pos;
585 } TiffSaveContext;
586
587 static tsize_t
588 tiff_save_read (thandle_t handle, tdata_t buf, tsize_t size)
589 {
590         return -1;
591 }
592
593 static tsize_t
594 tiff_save_write (thandle_t handle, tdata_t buf, tsize_t size)
595 {
596         TiffSaveContext *context = (TiffSaveContext *)handle;
597
598         /* Modify buffer length */
599         if (context->pos + size > context->used)
600                 context->used = context->pos + size;
601
602         /* Realloc */
603         if (context->used > context->allocated) {
604                 context->buffer = g_realloc (context->buffer, context->pos + size);
605                 context->allocated = context->used;
606         }
607
608         /* Now copy the data */
609         memcpy (context->buffer + context->pos, buf, size);
610
611         /* Update pos */
612         context->pos += size;
613
614         return size;
615 }
616
617 static toff_t
618 tiff_save_seek (thandle_t handle, toff_t offset, int whence)
619 {
620         TiffSaveContext *context = (TiffSaveContext *)handle;
621
622         switch (whence) {
623         case SEEK_SET:
624                 context->pos = offset;
625                 break;
626         case SEEK_CUR:
627                 context->pos += offset;
628                 break;
629         case SEEK_END:
630                 context->pos = context->used + offset;
631                 break;
632         default:
633                 return -1;
634         }
635         return context->pos;
636 }
637
638 static int
639 tiff_save_close (thandle_t context)
640 {
641         return 0;
642 }
643
644 static toff_t
645 tiff_save_size (thandle_t handle)
646 {
647         return -1;
648 }
649
650 static TiffSaveContext *
651 create_save_context (void)
652 {
653         TiffSaveContext *context;
654
655         context = g_new (TiffSaveContext, 1);
656         context->buffer = NULL;
657         context->allocated = 0;
658         context->used = 0;
659         context->pos = 0;
660
661         return context;
662 }
663
664 static void
665 free_save_context (TiffSaveContext *context)
666 {
667         g_free (context->buffer);
668         g_free (context);
669 }
670
671 static gboolean
672 gdk_pixbuf__tiff_image_save_to_callback (GdkPixbufSaveFunc   save_func,
673                                          gpointer            user_data,
674                                          GdkPixbuf          *pixbuf, 
675                                          gchar             **keys,
676                                          gchar             **values,
677                                          GError            **error)
678 {
679         TIFF *tiff;
680         gint width, height, rowstride;
681         guchar *pixels;
682         gboolean has_alpha;
683         gushort alpha_samples[1] = { EXTRASAMPLE_UNASSALPHA };
684         int y;
685         TiffSaveContext *context;
686         gboolean retval;
687
688         tiff_push_handlers ();
689
690         context = create_save_context ();
691         tiff = TIFFClientOpen ("libtiff-pixbuf", "w", context,  
692                                tiff_save_read, tiff_save_write, 
693                                tiff_save_seek, tiff_save_close, 
694                                tiff_save_size, 
695                                NULL, NULL);
696
697         if (!tiff || global_error) {
698                 tiff_set_error (error,
699                                 GDK_PIXBUF_ERROR_FAILED,
700                                 _("Failed to save TIFF image"));
701
702                 tiff_pop_handlers ();
703
704                 free_save_context (context);
705                 return FALSE;
706         }
707
708         rowstride = gdk_pixbuf_get_rowstride (pixbuf);
709         pixels = gdk_pixbuf_get_pixels (pixbuf);
710
711         has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
712
713         height = gdk_pixbuf_get_height (pixbuf);
714         width = gdk_pixbuf_get_width (pixbuf);
715
716         TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, width);
717         TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, height);
718         TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, 8);
719         TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, has_alpha ? 4 : 3);
720         TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, height);
721
722         if (has_alpha)
723                 TIFFSetField (tiff, TIFFTAG_EXTRASAMPLES, 1, alpha_samples);
724
725         TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
726         TIFFSetField (tiff, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);        
727         TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
728
729         for (y = 0; y < height; y++) {
730                 if (TIFFWriteScanline (tiff, pixels + y * rowstride, y, 0) == -1 ||
731                     global_error)
732                         break;
733         }
734
735         if (global_error) {
736                 tiff_set_error (error,
737                                 GDK_PIXBUF_ERROR_FAILED,
738                                 _("Failed to write TIFF data"));
739
740                 TIFFClose (tiff);
741
742                 free_save_context (context);               
743                 tiff_pop_handlers ();
744                 
745                 return FALSE;
746         }
747
748         TIFFClose (tiff);
749         if (global_error) {
750                 tiff_set_error (error,
751                                 GDK_PIXBUF_ERROR_FAILED,
752                                 _("TIFFClose operation failed"));
753
754                 free_save_context (context);               
755                 tiff_pop_handlers ();
756                 
757                 return FALSE;
758         }
759
760         tiff_pop_handlers ();
761
762         /* Now call the callback */
763         retval = save_func (context->buffer, context->used, error, user_data);
764
765         free_save_context (context);               
766         
767         return retval;
768 }
769
770 static gboolean
771 save_to_file_cb (const gchar *buf,
772                  gsize count,
773                  GError **error,
774                  gpointer data)
775 {
776         gint bytes;
777         
778         while (count > 0) {
779                 bytes = fwrite (buf, sizeof (gchar), count, (FILE *) data);
780                 if (bytes <= 0)
781                         break;
782                 count -= bytes;
783                 buf += bytes;
784         }
785
786         if (count) {
787                 g_set_error (error,
788                              GDK_PIXBUF_ERROR,
789                              GDK_PIXBUF_ERROR_FAILED,
790                              _("Couldn't write to TIFF file"));
791                 return FALSE;
792         }
793         
794         return TRUE;
795 }
796
797 static gboolean
798 gdk_pixbuf__tiff_image_save (FILE          *f, 
799                              GdkPixbuf     *pixbuf, 
800                              gchar        **keys,
801                              gchar        **values,
802                              GError       **error)
803 {
804         return gdk_pixbuf__tiff_image_save_to_callback (save_to_file_cb,
805                                                         f, pixbuf, keys,
806                                                         values, error);
807 }
808
809 void
810 MODULE_ENTRY (tiff, fill_vtable) (GdkPixbufModule *module)
811 {
812         module->load = gdk_pixbuf__tiff_image_load;
813         module->begin_load = gdk_pixbuf__tiff_image_begin_load;
814         module->stop_load = gdk_pixbuf__tiff_image_stop_load;
815         module->load_increment = gdk_pixbuf__tiff_image_load_increment;
816         module->save = gdk_pixbuf__tiff_image_save;
817         module->save_to_callback = gdk_pixbuf__tiff_image_save_to_callback;
818 }
819
820 void
821 MODULE_ENTRY (tiff, fill_info) (GdkPixbufFormat *info)
822 {
823         static GdkPixbufModulePattern signature[] = {
824                 { "MM \x2a", "  z ", 100 },
825                 { "II\x2a ", "   z", 100 },
826                 { NULL, NULL, 0 }
827         };
828         static gchar * mime_types[] = {
829                 "image/tiff",
830                 NULL
831         };
832         static gchar * extensions[] = {
833                 "tiff",
834                 "tif",
835                 NULL
836         };
837
838         info->name = "tiff";
839         info->signature = signature;
840         info->description = N_("The TIFF image format");
841         info->mime_types = mime_types;
842         info->extensions = extensions;
843         /* not threadsafe, due to the error handler handling */
844         info->flags = GDK_PIXBUF_FORMAT_WRITABLE;
845         info->license = "LGPL";
846 }