1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - TIFF image loader
4 * Copyright (C) 1999 Mark Crichton
5 * Copyright (C) 1999 The Free Software Foundation
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>
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.
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.
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.
28 /* Following code (almost) blatantly ripped from Imlib */
38 #include "gdk-pixbuf-private.h"
39 #include "gdk-pixbuf-io.h"
43 #define O_RDWR _O_RDWR
47 typedef struct _TiffContext TiffContext;
50 ModulePreparedNotifyFunc prepare_func;
51 ModuleUpdatedNotifyFunc update_func;
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
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;
71 tiff_warning_handler (const char *mod, const char *fmt, va_list ap)
73 /* Don't print anything; we should not be dumping junk to
74 * stderr, since that may be bad for some apps.
77 /* libTIFF seems to occasionally warn about things that
78 * are really errors, so maybe we should just call tiff_error_handler
84 tiff_error_handler (const char *mod, const char *fmt, va_list ap)
87 /* Blah, loader called us twice */
91 global_error = g_strdup_vprintf (fmt, ap);
95 tiff_push_handlers (void)
98 g_warning ("TIFF loader left crufty global_error around, FIXME");
100 orig_error_handler = TIFFSetErrorHandler (tiff_error_handler);
101 orig_warning_handler = TIFFSetWarningHandler (tiff_warning_handler);
105 tiff_pop_handlers (void)
108 g_warning ("TIFF loader left crufty global_error around, FIXME");
110 TIFFSetErrorHandler (orig_error_handler);
111 TIFFSetWarningHandler (orig_warning_handler);
115 tiff_set_error (GError **error,
119 /* Take the error message from libtiff and merge it with
120 * some context we provide.
126 msg, global_error ? ": " : "", global_error);
129 g_free (global_error);
137 tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
139 guchar *pixels = NULL;
141 uint32 *rast, *tmp_rast;
142 gint w, h, x, y, num_pixs;
145 /* We're called with the lock held. */
147 g_return_val_if_fail (global_error == NULL, NULL);
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)"));
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)"));
168 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
169 _("Width or height of TIFF image is zero"));
173 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
177 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
178 _("Insufficient memory to open TIFF file"));
183 G_UNLOCK (tiff_loader);
185 (* context->prepare_func) (pixbuf, NULL, context->user_data);
186 G_LOCK (tiff_loader);
188 /* Yes, it needs to be _TIFFMalloc... */
189 rast = (uint32 *) _TIFFmalloc (num_pixs * sizeof (uint32));
193 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
194 _("Insufficient memory to open TIFF file"));
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"));
207 pixels = gdk_pixbuf_get_pixels (pixbuf);
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
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);
232 G_UNLOCK (tiff_loader);
234 (* context->update_func) (pixbuf, 0, 0, w, h, context->user_data);
235 G_LOCK (tiff_loader);
245 gdk_pixbuf__tiff_image_load (FILE *f, GError **error)
251 g_return_val_if_fail (f != NULL, NULL);
253 G_LOCK (tiff_loader);
255 tiff_push_handlers ();
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)
264 lseek (fd, 0, SEEK_SET);
265 tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
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 ();
273 G_UNLOCK (tiff_loader);
277 pixbuf = tiff_image_parse (tiff, NULL, error);
281 tiff_set_error (error,
282 GDK_PIXBUF_ERROR_FAILED,
283 _("TIFFClose operation failed"));
286 tiff_pop_handlers ();
288 G_UNLOCK (tiff_loader);
295 /* Progressive loader */
298 gdk_pixbuf__tiff_image_begin_load (ModulePreparedNotifyFunc prepare_func,
299 ModuleUpdatedNotifyFunc update_func,
303 TiffContext *context;
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;
318 tiff_read (thandle_t handle, tdata_t buf, tsize_t size)
320 TiffContext *context = (TiffContext *)handle;
322 if (context->pos + size > context->used)
325 memcpy (buf, context->buffer + context->pos, size);
326 context->pos += size;
331 tiff_write (thandle_t handle, tdata_t buf, tsize_t size)
337 tiff_seek (thandle_t handle, toff_t offset, int whence)
339 TiffContext *context = (TiffContext *)handle;
343 if (offset > context->used || offset < 0)
345 context->pos = offset;
348 if (offset + context->pos >= context->used)
350 context->pos += offset;
353 if (offset + context->used > context->used)
355 context->pos = context->used + offset;
365 tiff_close (thandle_t context)
371 tiff_size (thandle_t handle)
373 TiffContext *context = (TiffContext *)handle;
375 return context->used;
379 tiff_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
381 TiffContext *context = (TiffContext *)handle;
383 *buf = context->buffer;
384 *size = context->used;
390 tiff_unmap_file (thandle_t handle, tdata_t data, toff_t offset)
395 gdk_pixbuf__tiff_image_stop_load (gpointer data,
398 TiffContext *context = data;
402 g_return_val_if_fail (data != NULL, FALSE);
404 G_LOCK (tiff_loader);
406 tiff_push_handlers ();
408 tiff = TIFFClientOpen ("libtiff-pixbuf", "r", data,
409 tiff_read, tiff_write,
410 tiff_seek, tiff_close,
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"));
421 pixbuf = tiff_image_parse (tiff, context, error);
423 g_object_unref (pixbuf);
424 retval = pixbuf != NULL;
428 tiff_set_error (error,
429 GDK_PIXBUF_ERROR_FAILED,
430 _("Failed to load TIFF image"));
435 g_assert (!global_error);
437 g_free (context->buffer);
440 tiff_pop_handlers ();
442 G_UNLOCK (tiff_loader);
448 make_available_at_least (TiffContext *context, guint needed)
450 guchar *new_buffer = NULL;
453 need_alloc = context->used + needed;
454 if (need_alloc > context->allocated) {
456 while (new_size < need_alloc)
459 new_buffer = g_try_realloc (context->buffer, new_size);
461 context->buffer = new_buffer;
462 context->allocated = new_size;
471 gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf,
472 guint size, GError **error)
474 TiffContext *context = (TiffContext *) data;
476 g_return_val_if_fail (data != NULL, FALSE);
478 if (!make_available_at_least (context, size)) {
481 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
482 _("Insufficient memory to open TIFF file"));
486 memcpy (context->buffer + context->used, buf, size);
487 context->used += size;
492 gdk_pixbuf__tiff_fill_vtable (GdkPixbufModule *module)
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;