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 "%s%s%s", msg, ": ", global_error);
128 g_free (global_error);
140 static void free_buffer (guchar *pixels, gpointer data)
145 static tileContigRoutine tiff_put_contig;
146 static tileSeparateRoutine tiff_put_separate;
148 /* We're lucky that TIFFRGBAImage uses the same RGBA packing
149 as gdk-pixbuf, thus we can simple reuse the default libtiff
150 put routines, only adjusting the coordinate system.
153 put_contig (TIFFRGBAImage *img, uint32 *raster,
154 uint32 x, uint32 y, uint32 w, uint32 h,
155 int32 fromskew, int32 toskew, unsigned char *cp)
157 uint32 *data = raster - y * img->width - x;
159 tiff_put_contig (img, data + img->width * (img->height - 1 - y) + x,
160 x, y, w, h, fromskew, -toskew - 2*(int32)w, cp);
164 put_separate (TIFFRGBAImage *img, uint32 *raster,
165 uint32 x, uint32 y, uint32 w, uint32 h,
166 int32 fromskew, int32 toskew,
167 unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a)
169 uint32 *data = raster - y * img->width - x;
171 tiff_put_separate (img, data + img->width * (img->height - 1 - y) + x,
172 x, y, w, h, fromskew, -toskew - 2*w, r, g, b, a);
176 tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
178 guchar *pixels = NULL;
179 gint width, height, rowstride, bytes;
184 /* We're called with the lock held. */
186 g_return_val_if_fail (global_error == NULL, NULL);
188 if (!TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width) || global_error) {
189 tiff_set_error (error,
190 GDK_PIXBUF_ERROR_FAILED,
191 _("Could not get image width (bad TIFF file)"));
195 if (!TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height) || global_error) {
196 tiff_set_error (error,
197 GDK_PIXBUF_ERROR_FAILED,
198 _("Could not get image height (bad TIFF file)"));
202 if (width <= 0 || height <= 0) {
205 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
206 _("Width or height of TIFF image is zero"));
210 rowstride = width * 4;
211 if (rowstride / 4 != width) { /* overflow */
214 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
215 _("Dimensions of TIFF image too large"));
219 bytes = height * rowstride;
220 if (bytes / rowstride != height) { /* overflow */
223 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
224 _("Dimensions of TIFF image too large"));
228 pixels = g_try_malloc (bytes);
233 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
234 _("Insufficient memory to open TIFF file"));
238 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
239 width, height, rowstride,
245 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
246 _("Insufficient memory to open TIFF file"));
250 G_UNLOCK (tiff_loader);
252 (* context->prepare_func) (pixbuf, NULL, context->user_data);
253 G_LOCK (tiff_loader);
255 if (!TIFFRGBAImageBegin (&img, tiff, 1, emsg) || global_error) {
256 tiff_set_error (error,
257 GDK_PIXBUF_ERROR_FAILED,
258 _("Failed to load RGB data from TIFF file"));
259 g_object_unref (pixbuf);
263 if (img.put.any == NULL) {
264 tiff_set_error (error,
265 GDK_PIXBUF_ERROR_FAILED,
266 _("Unsupported TIFF variant"));
267 g_object_unref (pixbuf);
272 tiff_put_contig = img.put.contig;
273 img.put.contig = put_contig;
276 tiff_put_separate = img.put.separate;
277 img.put.separate = put_separate;
280 TIFFRGBAImageGet (&img, (uint32 *)pixels, width, height);
281 TIFFRGBAImageEnd (&img);
283 #ifdef WORDS_BIGENDIAN
284 /* Turns out that the packing used by TIFFRGBAImage depends on the host byte order... */
285 while (pixels < pixbuf->pixels + bytes) {
286 uint32 pixel = *(uint32 *)pixels;
287 int r = TIFFGetR(pixel);
288 int g = TIFFGetG(pixel);
289 int b = TIFFGetB(pixel);
290 int a = TIFFGetA(pixel);
298 G_UNLOCK (tiff_loader);
300 (* context->update_func) (pixbuf, 0, 0, width, height, context->user_data);
301 G_LOCK (tiff_loader);
311 gdk_pixbuf__tiff_image_load (FILE *f, GError **error)
317 g_return_val_if_fail (f != NULL, NULL);
319 G_LOCK (tiff_loader);
321 tiff_push_handlers ();
325 /* On OSF, apparently fseek() works in some on-demand way, so
326 * the fseek gdk_pixbuf_new_from_file() doesn't work here
327 * since we are using the raw file descriptor. So, we call lseek() on the fd
328 * before using it. (#60840)
330 lseek (fd, 0, SEEK_SET);
331 tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
333 if (!tiff || global_error) {
334 tiff_set_error (error,
335 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
336 _("Failed to open TIFF image"));
337 tiff_pop_handlers ();
339 G_UNLOCK (tiff_loader);
343 pixbuf = tiff_image_parse (tiff, NULL, error);
347 tiff_set_error (error,
348 GDK_PIXBUF_ERROR_FAILED,
349 _("TIFFClose operation failed"));
352 tiff_pop_handlers ();
354 G_UNLOCK (tiff_loader);
361 /* Progressive loader */
364 gdk_pixbuf__tiff_image_begin_load (ModuleSizeFunc size_func,
365 ModulePreparedNotifyFunc prepare_func,
366 ModuleUpdatedNotifyFunc update_func,
370 TiffContext *context;
372 context = g_new0 (TiffContext, 1);
373 context->prepare_func = prepare_func;
374 context->update_func = update_func;
375 context->user_data = user_data;
376 context->buffer = NULL;
377 context->allocated = 0;
385 tiff_read (thandle_t handle, tdata_t buf, tsize_t size)
387 TiffContext *context = (TiffContext *)handle;
389 if (context->pos + size > context->used)
392 memcpy (buf, context->buffer + context->pos, size);
393 context->pos += size;
398 tiff_write (thandle_t handle, tdata_t buf, tsize_t size)
404 tiff_seek (thandle_t handle, toff_t offset, int whence)
406 TiffContext *context = (TiffContext *)handle;
410 if (offset > context->used || offset < 0)
412 context->pos = offset;
415 if (offset + context->pos >= context->used)
417 context->pos += offset;
420 if (offset + context->used > context->used)
422 context->pos = context->used + offset;
432 tiff_close (thandle_t context)
438 tiff_size (thandle_t handle)
440 TiffContext *context = (TiffContext *)handle;
442 return context->used;
446 tiff_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
448 TiffContext *context = (TiffContext *)handle;
450 *buf = context->buffer;
451 *size = context->used;
457 tiff_unmap_file (thandle_t handle, tdata_t data, toff_t offset)
462 gdk_pixbuf__tiff_image_stop_load (gpointer data,
465 TiffContext *context = data;
469 g_return_val_if_fail (data != NULL, FALSE);
471 G_LOCK (tiff_loader);
473 tiff_push_handlers ();
475 tiff = TIFFClientOpen ("libtiff-pixbuf", "r", data,
476 tiff_read, tiff_write,
477 tiff_seek, tiff_close,
479 tiff_map_file, tiff_unmap_file);
480 if (!tiff || global_error) {
481 tiff_set_error (error,
482 GDK_PIXBUF_ERROR_FAILED,
483 _("Failed to load TIFF image"));
488 pixbuf = tiff_image_parse (tiff, context, error);
490 g_object_unref (pixbuf);
491 retval = pixbuf != NULL;
494 tiff_set_error (error,
495 GDK_PIXBUF_ERROR_FAILED,
496 _("Failed to load TIFF image"));
497 tiff_pop_handlers ();
506 g_assert (!global_error);
508 g_free (context->buffer);
511 tiff_pop_handlers ();
513 G_UNLOCK (tiff_loader);
519 make_available_at_least (TiffContext *context, guint needed)
521 guchar *new_buffer = NULL;
524 need_alloc = context->used + needed;
525 if (need_alloc > context->allocated) {
527 while (new_size < need_alloc)
530 new_buffer = g_try_realloc (context->buffer, new_size);
532 context->buffer = new_buffer;
533 context->allocated = new_size;
542 gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf,
543 guint size, GError **error)
545 TiffContext *context = (TiffContext *) data;
547 g_return_val_if_fail (data != NULL, FALSE);
549 if (!make_available_at_least (context, size)) {
552 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
553 _("Insufficient memory to open TIFF file"));
557 memcpy (context->buffer + context->used, buf, size);
558 context->used += size;
563 gdk_pixbuf__tiff_fill_vtable (GdkPixbufModule *module)
565 module->load = gdk_pixbuf__tiff_image_load;
566 module->begin_load = gdk_pixbuf__tiff_image_begin_load;
567 module->stop_load = gdk_pixbuf__tiff_image_stop_load;
568 module->load_increment = gdk_pixbuf__tiff_image_load_increment;