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 static char *global_error = NULL;
66 static TIFFErrorHandler orig_error_handler = NULL;
67 static TIFFErrorHandler orig_warning_handler = NULL;
70 tiff_warning_handler (const char *mod, const char *fmt, va_list ap)
72 /* Don't print anything; we should not be dumping junk to
73 * stderr, since that may be bad for some apps.
76 /* libTIFF seems to occasionally warn about things that
77 * are really errors, so maybe we should just call tiff_error_handler
83 tiff_error_handler (const char *mod, const char *fmt, va_list ap)
86 /* Blah, loader called us twice */
90 global_error = g_strdup_vprintf (fmt, ap);
94 tiff_push_handlers (void)
97 g_warning ("TIFF loader left crufty global_error around, FIXME");
99 orig_error_handler = TIFFSetErrorHandler (tiff_error_handler);
100 orig_warning_handler = TIFFSetWarningHandler (tiff_warning_handler);
104 tiff_pop_handlers (void)
107 g_warning ("TIFF loader left crufty global_error around, FIXME");
109 TIFFSetErrorHandler (orig_error_handler);
110 TIFFSetWarningHandler (orig_warning_handler);
114 tiff_set_error (GError **error,
118 /* Take the error message from libtiff and merge it with
119 * some context we provide.
125 "%s%s%s", msg, ": ", global_error);
127 g_free (global_error);
139 static void free_buffer (guchar *pixels, gpointer data)
144 #if TIFFLIB_VERSION >= 20031226
145 static gboolean tifflibversion (int *major, int *minor, int *revision)
147 if (sscanf (TIFFGetVersion(),
148 "LIBTIFF, Version %d.%d.%d",
149 major, minor, revision) < 3)
157 tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
159 guchar *pixels = NULL;
160 gint width, height, rowstride, bytes;
162 #if TIFFLIB_VERSION >= 20031226
163 gint major, minor, revision;
166 /* We're called with the lock held. */
168 g_return_val_if_fail (global_error == NULL, NULL);
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)"));
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)"));
184 if (width <= 0 || height <= 0) {
187 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
188 _("Width or height of TIFF image is zero"));
192 rowstride = width * 4;
193 if (rowstride / 4 != width) { /* overflow */
196 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
197 _("Dimensions of TIFF image too large"));
201 bytes = height * rowstride;
202 if (bytes / rowstride != height) { /* overflow */
205 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
206 _("Dimensions of TIFF image too large"));
210 if (context && context->size_func) {
213 (* context->size_func) (&w, &h, context->user_data);
215 if (w == 0 || h == 0)
219 pixels = g_try_malloc (bytes);
224 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
225 _("Insufficient memory to open TIFF file"));
229 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
230 width, height, rowstride,
236 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
237 _("Insufficient memory to open TIFF file"));
242 (* context->prepare_func) (pixbuf, NULL, context->user_data);
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)
249 tiff_set_error (error,
250 GDK_PIXBUF_ERROR_FAILED,
251 _("Failed to load RGB data from TIFF file"));
252 g_object_unref (pixbuf);
256 #if G_BYTE_ORDER == G_BIG_ENDIAN
257 /* Turns out that the packing used by TIFFRGBAImage depends on
258 * the host byte order...
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);
276 uint32 *rast, *tmp_rast;
280 /* Yes, it needs to be _TIFFMalloc... */
281 rast = (uint32 *) _TIFFmalloc (width * height * sizeof (uint32));
285 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
286 _("Insufficient memory to open TIFF file"));
287 g_object_unref (pixbuf);
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);
301 pixels = gdk_pixbuf_get_pixels (pixbuf);
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
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);
328 (* context->update_func) (pixbuf, 0, 0, width, height, context->user_data);
338 gdk_pixbuf__tiff_image_load (FILE *f, GError **error)
344 g_return_val_if_fail (f != NULL, NULL);
346 tiff_push_handlers ();
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)
355 lseek (fd, 0, SEEK_SET);
356 tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
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 ();
367 pixbuf = tiff_image_parse (tiff, NULL, error);
371 tiff_set_error (error,
372 GDK_PIXBUF_ERROR_FAILED,
373 _("TIFFClose operation failed"));
376 tiff_pop_handlers ();
383 /* Progressive loader */
386 gdk_pixbuf__tiff_image_begin_load (GdkPixbufModuleSizeFunc size_func,
387 GdkPixbufModulePreparedFunc prepare_func,
388 GdkPixbufModuleUpdatedFunc update_func,
392 TiffContext *context;
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;
408 tiff_read (thandle_t handle, tdata_t buf, tsize_t size)
410 TiffContext *context = (TiffContext *)handle;
412 if (context->pos + size > context->used)
415 memcpy (buf, context->buffer + context->pos, size);
416 context->pos += size;
421 tiff_write (thandle_t handle, tdata_t buf, tsize_t size)
427 tiff_seek (thandle_t handle, toff_t offset, int whence)
429 TiffContext *context = (TiffContext *)handle;
433 if (offset > context->used || offset < 0)
435 context->pos = offset;
438 if (offset + context->pos >= context->used)
440 context->pos += offset;
443 if (offset + context->used > context->used)
445 context->pos = context->used + offset;
455 tiff_close (thandle_t context)
461 tiff_size (thandle_t handle)
463 TiffContext *context = (TiffContext *)handle;
465 return context->used;
469 tiff_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
471 TiffContext *context = (TiffContext *)handle;
473 *buf = context->buffer;
474 *size = context->used;
480 tiff_unmap_file (thandle_t handle, tdata_t data, toff_t offset)
485 gdk_pixbuf__tiff_image_stop_load (gpointer data,
488 TiffContext *context = data;
492 g_return_val_if_fail (data != NULL, FALSE);
494 tiff_push_handlers ();
496 tiff = TIFFClientOpen ("libtiff-pixbuf", "r", data,
497 tiff_read, tiff_write,
498 tiff_seek, tiff_close,
500 tiff_map_file, tiff_unmap_file);
501 if (!tiff || global_error) {
502 tiff_set_error (error,
503 GDK_PIXBUF_ERROR_FAILED,
504 _("Failed to load TIFF image"));
509 pixbuf = tiff_image_parse (tiff, context, error);
511 g_object_unref (pixbuf);
512 retval = pixbuf != NULL;
515 tiff_set_error (error,
516 GDK_PIXBUF_ERROR_FAILED,
517 _("Failed to load TIFF image"));
518 tiff_pop_handlers ();
527 g_assert (!global_error);
529 g_free (context->buffer);
532 tiff_pop_handlers ();
538 make_available_at_least (TiffContext *context, guint needed)
540 guchar *new_buffer = NULL;
543 need_alloc = context->used + needed;
544 if (need_alloc > context->allocated) {
546 while (new_size < need_alloc)
549 new_buffer = g_try_realloc (context->buffer, new_size);
551 context->buffer = new_buffer;
552 context->allocated = new_size;
561 gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf,
562 guint size, GError **error)
564 TiffContext *context = (TiffContext *) data;
566 g_return_val_if_fail (data != NULL, FALSE);
568 if (!make_available_at_least (context, size)) {
571 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
572 _("Insufficient memory to open TIFF file"));
576 memcpy (context->buffer + context->used, buf, size);
577 context->used += size;
582 MODULE_ENTRY (tiff, fill_vtable) (GdkPixbufModule *module)
584 module->load = gdk_pixbuf__tiff_image_load;
585 module->begin_load = gdk_pixbuf__tiff_image_begin_load;
586 module->stop_load = gdk_pixbuf__tiff_image_stop_load;
587 module->load_increment = gdk_pixbuf__tiff_image_load_increment;
591 MODULE_ENTRY (tiff, fill_info) (GdkPixbufFormat *info)
593 static GdkPixbufModulePattern signature[] = {
594 { "MM \x2a", " z ", 100 },
595 { "II\x2a ", " z", 100 },
598 static gchar * mime_types[] = {
602 static gchar * extensions[] = {
609 info->signature = signature;
610 info->description = N_("The TIFF image format");
611 info->mime_types = mime_types;
612 info->extensions = extensions;
613 /* not threadsafe, due the the error handler handling */
615 info->license = "LGPL";