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"
44 #define lseek(a,b,c) _lseek(a,b,c)
45 #define O_RDWR _O_RDWR
49 typedef struct _TiffContext TiffContext;
52 GdkPixbufModuleSizeFunc size_func;
53 GdkPixbufModulePreparedFunc prepare_func;
54 GdkPixbufModuleUpdatedFunc update_func;
65 /* There's no user data for the error handlers, so we just have to
66 * put a big-ass lock on the whole TIFF loader
68 G_LOCK_DEFINE_STATIC (tiff_loader);
69 static char *global_error = NULL;
70 static TIFFErrorHandler orig_error_handler = NULL;
71 static TIFFErrorHandler orig_warning_handler = NULL;
74 tiff_warning_handler (const char *mod, const char *fmt, va_list ap)
76 /* Don't print anything; we should not be dumping junk to
77 * stderr, since that may be bad for some apps.
80 /* libTIFF seems to occasionally warn about things that
81 * are really errors, so maybe we should just call tiff_error_handler
87 tiff_error_handler (const char *mod, const char *fmt, va_list ap)
90 /* Blah, loader called us twice */
94 global_error = g_strdup_vprintf (fmt, ap);
98 tiff_push_handlers (void)
101 g_warning ("TIFF loader left crufty global_error around, FIXME");
103 orig_error_handler = TIFFSetErrorHandler (tiff_error_handler);
104 orig_warning_handler = TIFFSetWarningHandler (tiff_warning_handler);
108 tiff_pop_handlers (void)
111 g_warning ("TIFF loader left crufty global_error around, FIXME");
113 TIFFSetErrorHandler (orig_error_handler);
114 TIFFSetWarningHandler (orig_warning_handler);
118 tiff_set_error (GError **error,
122 /* Take the error message from libtiff and merge it with
123 * some context we provide.
129 "%s%s%s", msg, ": ", global_error);
131 g_free (global_error);
143 static void free_buffer (guchar *pixels, gpointer data)
148 #if TIFFLIB_VERSION >= 20031226
149 static gboolean tifflibversion (int *major, int *minor, int *revision)
151 if (sscanf (TIFFGetVersion(),
152 "LIBTIFF, Version %d.%d.%d",
153 major, minor, revision) < 3)
161 tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
163 guchar *pixels = NULL;
164 gint width, height, rowstride, bytes;
166 #if TIFFLIB_VERSION >= 20031226
167 gint major, minor, revision;
170 /* We're called with the lock held. */
172 g_return_val_if_fail (global_error == NULL, NULL);
174 if (!TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width) || global_error) {
175 tiff_set_error (error,
176 GDK_PIXBUF_ERROR_FAILED,
177 _("Could not get image width (bad TIFF file)"));
181 if (!TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height) || global_error) {
182 tiff_set_error (error,
183 GDK_PIXBUF_ERROR_FAILED,
184 _("Could not get image height (bad TIFF file)"));
188 if (width <= 0 || height <= 0) {
191 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
192 _("Width or height of TIFF image is zero"));
196 rowstride = width * 4;
197 if (rowstride / 4 != width) { /* overflow */
200 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
201 _("Dimensions of TIFF image too large"));
205 bytes = height * rowstride;
206 if (bytes / rowstride != height) { /* overflow */
209 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
210 _("Dimensions of TIFF image too large"));
214 if (context && context->size_func) {
217 (* context->size_func) (&w, &h, context->user_data);
219 if (w == 0 || h == 0)
223 pixels = g_try_malloc (bytes);
228 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
229 _("Insufficient memory to open TIFF file"));
233 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
234 width, height, rowstride,
240 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
241 _("Insufficient memory to open TIFF file"));
245 G_UNLOCK (tiff_loader);
247 (* context->prepare_func) (pixbuf, NULL, context->user_data);
248 G_LOCK (tiff_loader);
250 #if TIFFLIB_VERSION >= 20031226
251 if (tifflibversion(&major, &minor, &revision) && major == 3 &&
252 (minor > 6 || (minor == 6 && revision > 0))) {
253 if (!TIFFReadRGBAImageOriented (tiff, width, height, (uint32 *)pixels, ORIENTATION_TOPLEFT, 1) || global_error)
255 tiff_set_error (error,
256 GDK_PIXBUF_ERROR_FAILED,
257 _("Failed to load RGB data from TIFF file"));
258 g_object_unref (pixbuf);
262 #if G_BYTE_ORDER == G_BIG_ENDIAN
263 /* Turns out that the packing used by TIFFRGBAImage depends on
264 * the host byte order...
266 while (pixels < pixbuf->pixels + bytes) {
267 uint32 pixel = *(uint32 *)pixels;
268 int r = TIFFGetR(pixel);
269 int g = TIFFGetG(pixel);
270 int b = TIFFGetB(pixel);
271 int a = TIFFGetA(pixel);
282 uint32 *rast, *tmp_rast;
286 /* Yes, it needs to be _TIFFMalloc... */
287 rast = (uint32 *) _TIFFmalloc (width * height * sizeof (uint32));
291 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
292 _("Insufficient memory to open TIFF file"));
293 g_object_unref (pixbuf);
297 if (!TIFFReadRGBAImage (tiff, width, height, rast, 1) || global_error) {
298 tiff_set_error (error,
299 GDK_PIXBUF_ERROR_FAILED,
300 _("Failed to load RGB data from TIFF file"));
301 g_object_unref (pixbuf);
307 pixels = gdk_pixbuf_get_pixels (pixbuf);
313 for (y = 0; y < height; y++) {
314 /* Unexplainable...are tiffs backwards? */
315 /* Also looking at the GIMP plugin, this
316 * whole reading thing can be a bit more
319 tmp_rast = rast + ((height - y - 1) * width);
320 for (x = 0; x < width; x++) {
321 tmppix[0] = TIFFGetR (*tmp_rast);
322 tmppix[1] = TIFFGetG (*tmp_rast);
323 tmppix[2] = TIFFGetB (*tmp_rast);
324 tmppix[3] = TIFFGetA (*tmp_rast);
333 G_UNLOCK (tiff_loader);
335 (* context->update_func) (pixbuf, 0, 0, width, height, context->user_data);
336 G_LOCK (tiff_loader);
346 gdk_pixbuf__tiff_image_load (FILE *f, GError **error)
352 g_return_val_if_fail (f != NULL, NULL);
354 G_LOCK (tiff_loader);
356 tiff_push_handlers ();
360 /* On OSF, apparently fseek() works in some on-demand way, so
361 * the fseek gdk_pixbuf_new_from_file() doesn't work here
362 * since we are using the raw file descriptor. So, we call lseek() on the fd
363 * before using it. (#60840)
365 lseek (fd, 0, SEEK_SET);
366 tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
368 if (!tiff || global_error) {
369 tiff_set_error (error,
370 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
371 _("Failed to open TIFF image"));
372 tiff_pop_handlers ();
374 G_UNLOCK (tiff_loader);
378 pixbuf = tiff_image_parse (tiff, NULL, error);
382 tiff_set_error (error,
383 GDK_PIXBUF_ERROR_FAILED,
384 _("TIFFClose operation failed"));
387 tiff_pop_handlers ();
389 G_UNLOCK (tiff_loader);
396 /* Progressive loader */
399 gdk_pixbuf__tiff_image_begin_load (GdkPixbufModuleSizeFunc size_func,
400 GdkPixbufModulePreparedFunc prepare_func,
401 GdkPixbufModuleUpdatedFunc update_func,
405 TiffContext *context;
407 context = g_new0 (TiffContext, 1);
408 context->size_func = size_func;
409 context->prepare_func = prepare_func;
410 context->update_func = update_func;
411 context->user_data = user_data;
412 context->buffer = NULL;
413 context->allocated = 0;
421 tiff_read (thandle_t handle, tdata_t buf, tsize_t size)
423 TiffContext *context = (TiffContext *)handle;
425 if (context->pos + size > context->used)
428 memcpy (buf, context->buffer + context->pos, size);
429 context->pos += size;
434 tiff_write (thandle_t handle, tdata_t buf, tsize_t size)
440 tiff_seek (thandle_t handle, toff_t offset, int whence)
442 TiffContext *context = (TiffContext *)handle;
446 if (offset > context->used || offset < 0)
448 context->pos = offset;
451 if (offset + context->pos >= context->used)
453 context->pos += offset;
456 if (offset + context->used > context->used)
458 context->pos = context->used + offset;
468 tiff_close (thandle_t context)
474 tiff_size (thandle_t handle)
476 TiffContext *context = (TiffContext *)handle;
478 return context->used;
482 tiff_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
484 TiffContext *context = (TiffContext *)handle;
486 *buf = context->buffer;
487 *size = context->used;
493 tiff_unmap_file (thandle_t handle, tdata_t data, toff_t offset)
498 gdk_pixbuf__tiff_image_stop_load (gpointer data,
501 TiffContext *context = data;
505 g_return_val_if_fail (data != NULL, FALSE);
507 G_LOCK (tiff_loader);
509 tiff_push_handlers ();
511 tiff = TIFFClientOpen ("libtiff-pixbuf", "r", data,
512 tiff_read, tiff_write,
513 tiff_seek, tiff_close,
515 tiff_map_file, tiff_unmap_file);
516 if (!tiff || global_error) {
517 tiff_set_error (error,
518 GDK_PIXBUF_ERROR_FAILED,
519 _("Failed to load TIFF image"));
524 pixbuf = tiff_image_parse (tiff, context, error);
526 g_object_unref (pixbuf);
527 retval = pixbuf != NULL;
530 tiff_set_error (error,
531 GDK_PIXBUF_ERROR_FAILED,
532 _("Failed to load TIFF image"));
533 tiff_pop_handlers ();
542 g_assert (!global_error);
544 g_free (context->buffer);
547 tiff_pop_handlers ();
549 G_UNLOCK (tiff_loader);
555 make_available_at_least (TiffContext *context, guint needed)
557 guchar *new_buffer = NULL;
560 need_alloc = context->used + needed;
561 if (need_alloc > context->allocated) {
563 while (new_size < need_alloc)
566 new_buffer = g_try_realloc (context->buffer, new_size);
568 context->buffer = new_buffer;
569 context->allocated = new_size;
578 gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf,
579 guint size, GError **error)
581 TiffContext *context = (TiffContext *) data;
583 g_return_val_if_fail (data != NULL, FALSE);
585 if (!make_available_at_least (context, size)) {
588 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
589 _("Insufficient memory to open TIFF file"));
593 memcpy (context->buffer + context->used, buf, size);
594 context->used += size;
599 MODULE_ENTRY (tiff, fill_vtable) (GdkPixbufModule *module)
601 module->load = gdk_pixbuf__tiff_image_load;
602 module->begin_load = gdk_pixbuf__tiff_image_begin_load;
603 module->stop_load = gdk_pixbuf__tiff_image_stop_load;
604 module->load_increment = gdk_pixbuf__tiff_image_load_increment;
608 MODULE_ENTRY (tiff, fill_info) (GdkPixbufFormat *info)
610 static GdkPixbufModulePattern signature[] = {
611 { "MM \x2a", " z ", 100 },
612 { "II\x2a ", " z", 100 },
615 static gchar * mime_types[] = {
619 static gchar * extensions[] = {
626 info->signature = signature;
627 info->description = N_("The TIFF image format");
628 info->mime_types = mime_types;
629 info->extensions = extensions;
631 info->license = "LGPL";