]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-tiff.c
Remove assorted G_OBJECT casts where unnecessary.
[~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 #define O_RDWR _O_RDWR
44 #endif
45 \f
46
47 typedef struct _TiffContext TiffContext;
48 struct _TiffContext
49 {
50         ModulePreparedNotifyFunc prepare_func;
51         ModuleUpdatedNotifyFunc update_func;
52         gpointer user_data;
53         
54         guchar *buffer;
55         guint allocated;
56         guint used;
57         guint pos;
58 };
59
60 \f
61
62 /* There's no user data for the error handlers, so we just have to
63  * put a big-ass lock on the whole TIFF loader
64  */
65 G_LOCK_DEFINE_STATIC (tiff_loader);
66 static char *global_error = NULL;
67 static TIFFErrorHandler orig_error_handler = NULL;
68 static TIFFErrorHandler orig_warning_handler = NULL;
69
70 static void
71 tiff_warning_handler (const char *mod, const char *fmt, va_list ap)
72 {
73         /* Don't print anything; we should not be dumping junk to
74          * stderr, since that may be bad for some apps.
75          */
76
77         /* libTIFF seems to occasionally warn about things that
78          * are really errors, so maybe we should just call tiff_error_handler
79          * here.
80          */
81 }
82
83 static void
84 tiff_error_handler (const char *mod, const char *fmt, va_list ap)
85 {
86         if (global_error) {                
87                 /* Blah, loader called us twice */
88                 return;
89         }
90
91         global_error = g_strdup_vprintf (fmt, ap);
92 }
93
94 static void
95 tiff_push_handlers (void)
96 {
97         if (global_error)
98                 g_warning ("TIFF loader left crufty global_error around, FIXME");
99         
100         orig_error_handler = TIFFSetErrorHandler (tiff_error_handler);
101         orig_warning_handler = TIFFSetWarningHandler (tiff_warning_handler);
102 }
103
104 static void
105 tiff_pop_handlers (void)
106 {
107         if (global_error)
108                 g_warning ("TIFF loader left crufty global_error around, FIXME");
109         
110         TIFFSetErrorHandler (orig_error_handler);
111         TIFFSetWarningHandler (orig_warning_handler);
112 }
113
114 static void
115 tiff_set_error (GError    **error,
116                 int         error_code,
117                 const char *msg)
118 {
119         /* Take the error message from libtiff and merge it with
120          * some context we provide.
121          */
122         g_set_error (error,
123                      GDK_PIXBUF_ERROR,
124                      error_code,
125                      "%s%s%s",
126                      msg, global_error ? ": " : "", global_error);
127
128         if (global_error) {
129                 g_free (global_error);
130                 global_error = NULL;
131         }
132 }
133
134 \f
135
136 static GdkPixbuf *
137 tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
138 {
139         guchar *pixels = NULL;
140         guchar *tmppix;
141         uint32 *rast, *tmp_rast;
142         gint w, h, x, y, num_pixs;
143         GdkPixbuf *pixbuf;
144
145         /* We're called with the lock held. */
146         
147         g_return_val_if_fail (global_error == NULL, NULL);
148
149         if (!TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &w) || global_error) {
150                 tiff_set_error (error,
151                                 GDK_PIXBUF_ERROR_FAILED,
152                                 _("Could not get image width (bad TIFF file)"));
153                 return NULL;
154         }
155         
156         if (!TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &h) || global_error) {
157                 tiff_set_error (error,
158                                 GDK_PIXBUF_ERROR_FAILED,
159                                 _("Could not get image height (bad TIFF file)"));
160                 return NULL;
161         }
162
163         num_pixs = w * h;
164
165         if (num_pixs == 0) {
166                 g_set_error (error,
167                              GDK_PIXBUF_ERROR,
168                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
169                              _("Width or height of TIFF image is zero"));
170                 return NULL;                
171         }
172         
173         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
174         if (!pixbuf) {
175                 g_set_error (error,
176                              GDK_PIXBUF_ERROR,
177                              GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
178                              _("Insufficient memory to open TIFF file"));
179                 return NULL;
180         }
181
182
183         G_UNLOCK (tiff_loader);
184         if (context)
185                 (* context->prepare_func) (pixbuf, NULL, context->user_data);
186         G_LOCK (tiff_loader);
187         
188         /* Yes, it needs to be _TIFFMalloc... */
189         rast = (uint32 *) _TIFFmalloc (num_pixs * sizeof (uint32));
190         if (!rast) {
191                 g_set_error (error,
192                              GDK_PIXBUF_ERROR,
193                              GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
194                              _("Insufficient memory to open TIFF file"));
195                 return NULL;
196         }
197         
198         if (!TIFFReadRGBAImage (tiff, w, h, rast, 0) || global_error) {
199                 tiff_set_error (error,
200                                 GDK_PIXBUF_ERROR_FAILED,
201                                 _("Failed to load RGB data from TIFF file"));
202                 _TIFFfree (rast);
203
204                 return NULL;
205         }
206         
207         pixels = gdk_pixbuf_get_pixels (pixbuf);
208         
209         g_assert (pixels);
210         
211         tmppix = pixels;
212         
213         for (y = 0; y < h; y++) {
214                 /* Unexplainable...are tiffs backwards? */
215                 /* Also looking at the GIMP plugin, this
216                  * whole reading thing can be a bit more
217                  * robust.
218                  */
219                 tmp_rast = rast + ((h - y - 1) * w);
220                 for (x = 0; x < w; x++) {
221                         tmppix[0] = TIFFGetR (*tmp_rast);
222                         tmppix[1] = TIFFGetG (*tmp_rast);
223                         tmppix[2] = TIFFGetB (*tmp_rast);
224                         tmppix[3] = TIFFGetA (*tmp_rast);
225                         tmp_rast++;
226                         tmppix += 4;
227                 }
228         }
229
230         _TIFFfree (rast);
231
232         G_UNLOCK (tiff_loader);
233         if (context)
234                 (* context->update_func) (pixbuf, 0, 0, w, h, context->user_data);
235         G_LOCK (tiff_loader);
236         
237         return pixbuf;
238 }
239
240 \f
241
242 /* Static loader */
243
244 static GdkPixbuf *
245 gdk_pixbuf__tiff_image_load (FILE *f, GError **error)
246 {
247         TIFF *tiff;
248         int fd;
249         GdkPixbuf *pixbuf;
250         
251         g_return_val_if_fail (f != NULL, NULL);
252
253         G_LOCK (tiff_loader);
254
255         tiff_push_handlers ();
256         
257         fd = fileno (f);
258
259         /* On OSF, apparently fseek() works in some on-demand way, so
260          * the fseek gdk_pixbuf_new_from_file() doesn't work here
261          * since we are using the raw file descriptor. So, we call lseek() on the fd
262          * before using it. (#60840)
263          */
264         lseek (fd, 0, SEEK_SET);
265         tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
266         
267         if (!tiff || global_error) {
268                 tiff_set_error (error,
269                                 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
270                                 _("Failed to open TIFF image"));
271                 tiff_pop_handlers ();
272
273                 G_UNLOCK (tiff_loader);
274                 return NULL;
275         }
276
277         pixbuf = tiff_image_parse (tiff, NULL, error);
278         
279         TIFFClose (tiff);
280         if (global_error) {
281                 tiff_set_error (error,
282                                 GDK_PIXBUF_ERROR_FAILED,
283                                 _("TIFFClose operation failed"));
284         }
285         
286         tiff_pop_handlers ();
287
288         G_UNLOCK (tiff_loader);
289         
290         return pixbuf;
291 }
292
293 \f
294
295 /* Progressive loader */
296
297 static gpointer
298 gdk_pixbuf__tiff_image_begin_load (ModulePreparedNotifyFunc prepare_func,
299                                    ModuleUpdatedNotifyFunc update_func,
300                                    gpointer user_data,
301                                    GError **error)
302 {
303         TiffContext *context;
304         
305         context = g_new0 (TiffContext, 1);
306         context->prepare_func = prepare_func;
307         context->update_func = update_func;
308         context->user_data = user_data;
309         context->buffer = NULL;
310         context->allocated = 0;
311         context->used = 0;
312         context->pos = 0;
313         
314         return context;
315 }
316
317 static tsize_t
318 tiff_read (thandle_t handle, tdata_t buf, tsize_t size)
319 {
320         TiffContext *context = (TiffContext *)handle;
321         
322         if (context->pos + size > context->used)
323                 return 0;
324         
325         memcpy (buf, context->buffer + context->pos, size);
326         context->pos += size;
327         return size;
328 }
329
330 static tsize_t
331 tiff_write (thandle_t handle, tdata_t buf, tsize_t size)
332 {
333         return -1;
334 }
335
336 static toff_t
337 tiff_seek (thandle_t handle, toff_t offset, int whence)
338 {
339         TiffContext *context = (TiffContext *)handle;
340         
341         switch (whence) {
342         case SEEK_SET:
343                 if (offset > context->used || offset < 0)
344                         return -1;
345                 context->pos = offset;
346                 break;
347         case SEEK_CUR:
348                 if (offset + context->pos >= context->used)
349                         return -1;
350                 context->pos += offset;
351                 break;
352         case SEEK_END:
353                 if (offset + context->used > context->used)
354                         return -1;
355                 context->pos = context->used + offset;
356                 break;
357         default:
358                 return -1;
359                 break;
360         }
361         return context->pos;
362 }
363
364 static int
365 tiff_close (thandle_t context)
366 {
367         return 0;
368 }
369
370 static toff_t
371 tiff_size (thandle_t handle)
372 {
373         TiffContext *context = (TiffContext *)handle;
374         
375         return context->used;
376 }
377
378 static int
379 tiff_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
380 {
381         TiffContext *context = (TiffContext *)handle;
382         
383         *buf = context->buffer;
384         *size = context->used;
385         
386         return 0;
387 }
388
389 static void
390 tiff_unmap_file (thandle_t handle, tdata_t data, toff_t offset)
391 {
392 }
393
394 static gboolean
395 gdk_pixbuf__tiff_image_stop_load (gpointer data,
396                                   GError **error)
397 {
398         TiffContext *context = data;
399         TIFF *tiff;
400         gboolean retval;
401         
402         g_return_val_if_fail (data != NULL, FALSE);
403
404         G_LOCK (tiff_loader);
405         
406         tiff_push_handlers ();
407         
408         tiff = TIFFClientOpen ("libtiff-pixbuf", "r", data, 
409                                tiff_read, tiff_write, 
410                                tiff_seek, tiff_close, 
411                                tiff_size, 
412                                tiff_map_file, tiff_unmap_file);
413         if (!tiff || global_error) {
414                 tiff_set_error (error,
415                                 GDK_PIXBUF_ERROR_FAILED,
416                                 _("Failed to load TIFF image"));
417                 retval = FALSE;
418         } else {
419                 GdkPixbuf *pixbuf;
420                 
421                 pixbuf = tiff_image_parse (tiff, context, error);
422                 if (pixbuf)
423                         g_object_unref (pixbuf);
424                 retval = pixbuf != NULL;
425                 TIFFClose (tiff);
426                 if (global_error)
427                         {
428                                 tiff_set_error (error,
429                                                 GDK_PIXBUF_ERROR_FAILED,
430                                                 _("Failed to load TIFF image"));
431                                 retval = FALSE;
432                         }
433         }
434
435         g_assert (!global_error);
436         
437         g_free (context->buffer);
438         g_free (context);
439
440         tiff_pop_handlers ();
441
442         G_UNLOCK (tiff_loader);
443
444         return retval;
445 }
446
447 static gboolean
448 make_available_at_least (TiffContext *context, guint needed)
449 {
450         guchar *new_buffer = NULL;
451         guint need_alloc;
452         
453         need_alloc = context->used + needed;
454         if (need_alloc > context->allocated) {
455                 guint new_size = 1;
456                 while (new_size < need_alloc)
457                         new_size *= 2;
458                 
459                 new_buffer = g_try_realloc (context->buffer, new_size);
460                 if (new_buffer) {
461                         context->buffer = new_buffer;
462                         context->allocated = new_size;
463                         return TRUE;
464                 }
465                 return FALSE;
466         }
467         return TRUE;
468 }
469
470 static gboolean
471 gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf,
472                                        guint size, GError **error)
473 {
474         TiffContext *context = (TiffContext *) data;
475         
476         g_return_val_if_fail (data != NULL, FALSE);
477         
478         if (!make_available_at_least (context, size)) {
479                 g_set_error (error,
480                              GDK_PIXBUF_ERROR,
481                              GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
482                              _("Insufficient memory to open TIFF file"));
483                 return FALSE;
484         }
485         
486         memcpy (context->buffer + context->used, buf, size);
487         context->used += size;
488         return TRUE;
489 }
490
491 void
492 gdk_pixbuf__tiff_fill_vtable (GdkPixbufModule *module)
493 {
494         module->load = gdk_pixbuf__tiff_image_load;
495         module->begin_load = gdk_pixbuf__tiff_image_begin_load;
496         module->stop_load = gdk_pixbuf__tiff_image_stop_load;
497         module->load_increment = gdk_pixbuf__tiff_image_load_increment;
498 }