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)
145 tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
147 guchar *pixels = NULL;
148 gint width, height, rowstride, bytes;
150 uint16 orientation = 0;
151 uint16 transform = 0;
153 /* We're called with the lock held. */
155 g_return_val_if_fail (global_error == NULL, NULL);
157 if (!TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width) || global_error) {
158 tiff_set_error (error,
159 GDK_PIXBUF_ERROR_FAILED,
160 _("Could not get image width (bad TIFF file)"));
164 if (!TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height) || global_error) {
165 tiff_set_error (error,
166 GDK_PIXBUF_ERROR_FAILED,
167 _("Could not get image height (bad TIFF file)"));
171 if (width <= 0 || height <= 0) {
174 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
175 _("Width or height of TIFF image is zero"));
179 rowstride = width * 4;
180 if (rowstride / 4 != width) { /* overflow */
183 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
184 _("Dimensions of TIFF image too large"));
188 bytes = height * rowstride;
189 if (bytes / rowstride != height) { /* overflow */
192 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
193 _("Dimensions of TIFF image too large"));
197 if (context && context->size_func) {
200 (* context->size_func) (&w, &h, context->user_data);
202 if (w == 0 || h == 0)
206 pixels = g_try_malloc (bytes);
211 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
212 _("Insufficient memory to open TIFF file"));
216 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
217 width, height, rowstride,
223 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
224 _("Insufficient memory to open TIFF file"));
228 /* Set the "orientation" key associated with this image. libtiff
229 orientation handling is odd, so further processing is required
230 by higher-level functions based on this tag. If the embedded
231 orientation tag is 1-4, libtiff flips/mirrors the image as
232 required, and no client processing is required - so we report
233 no orientation. Orientations 5-8 require rotations which would
234 swap the width and height of the image. libtiff does not do this.
235 Instead it interprets orientations 5-8 the same as 1-4.
236 See http://bugzilla.remotesensing.org/show_bug.cgi?id=1548.
237 To correct for this, the client must apply the transform normally
238 used for orientation 5 to both orientations 5 and 7, and apply
239 the transform normally used for orientation 7 for both
240 orientations 6 and 8. Then everythings works out OK! */
242 TIFFGetField (tiff, TIFFTAG_ORIENTATION, &orientation);
244 switch (orientation) {
258 if (transform > 0 ) {
260 snprintf (str, sizeof (str), "%d", transform);
261 gdk_pixbuf_set_option (pixbuf, "orientation", str);
264 if (context && context->prepare_func)
265 (* context->prepare_func) (pixbuf, NULL, context->user_data);
267 if (!TIFFReadRGBAImageOriented (tiff, width, height, (uint32 *)pixels, ORIENTATION_TOPLEFT, 1) || global_error) {
268 tiff_set_error (error,
269 GDK_PIXBUF_ERROR_FAILED,
270 _("Failed to load RGB data from TIFF file"));
271 g_object_unref (pixbuf);
275 #if G_BYTE_ORDER == G_BIG_ENDIAN
276 /* Turns out that the packing used by TIFFRGBAImage depends on
277 * the host byte order...
279 while (pixels < pixbuf->pixels + bytes) {
280 uint32 pixel = *(uint32 *)pixels;
281 int r = TIFFGetR(pixel);
282 int g = TIFFGetG(pixel);
283 int b = TIFFGetB(pixel);
284 int a = TIFFGetA(pixel);
292 if (context && context->update_func)
293 (* context->update_func) (pixbuf, 0, 0, width, height, context->user_data);
303 gdk_pixbuf__tiff_image_load (FILE *f, GError **error)
309 g_return_val_if_fail (f != NULL, NULL);
311 tiff_push_handlers ();
315 /* On OSF, apparently fseek() works in some on-demand way, so
316 * the fseek gdk_pixbuf_new_from_file() doesn't work here
317 * since we are using the raw file descriptor. So, we call lseek() on the fd
318 * before using it. (#60840)
320 lseek (fd, 0, SEEK_SET);
321 tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
323 if (!tiff || global_error) {
324 tiff_set_error (error,
325 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
326 _("Failed to open TIFF image"));
327 tiff_pop_handlers ();
332 pixbuf = tiff_image_parse (tiff, NULL, error);
336 tiff_set_error (error,
337 GDK_PIXBUF_ERROR_FAILED,
338 _("TIFFClose operation failed"));
341 tiff_pop_handlers ();
348 /* Progressive loader */
351 gdk_pixbuf__tiff_image_begin_load (GdkPixbufModuleSizeFunc size_func,
352 GdkPixbufModulePreparedFunc prepare_func,
353 GdkPixbufModuleUpdatedFunc update_func,
357 TiffContext *context;
359 context = g_new0 (TiffContext, 1);
360 context->size_func = size_func;
361 context->prepare_func = prepare_func;
362 context->update_func = update_func;
363 context->user_data = user_data;
364 context->buffer = NULL;
365 context->allocated = 0;
373 tiff_load_read (thandle_t handle, tdata_t buf, tsize_t size)
375 TiffContext *context = (TiffContext *)handle;
377 if (context->pos + size > context->used)
380 memcpy (buf, context->buffer + context->pos, size);
381 context->pos += size;
386 tiff_load_write (thandle_t handle, tdata_t buf, tsize_t size)
392 tiff_load_seek (thandle_t handle, toff_t offset, int whence)
394 TiffContext *context = (TiffContext *)handle;
398 if (offset > context->used)
400 context->pos = offset;
403 if (offset + context->pos >= context->used)
405 context->pos += offset;
408 if (offset + context->used > context->used)
410 context->pos = context->used + offset;
419 tiff_load_close (thandle_t context)
425 tiff_load_size (thandle_t handle)
427 TiffContext *context = (TiffContext *)handle;
429 return context->used;
433 tiff_load_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
435 TiffContext *context = (TiffContext *)handle;
437 *buf = context->buffer;
438 *size = context->used;
444 tiff_load_unmap_file (thandle_t handle, tdata_t data, toff_t offset)
449 gdk_pixbuf__tiff_image_stop_load (gpointer data,
452 TiffContext *context = data;
456 g_return_val_if_fail (data != NULL, FALSE);
458 tiff_push_handlers ();
460 tiff = TIFFClientOpen ("libtiff-pixbuf", "r", data,
461 tiff_load_read, tiff_load_write,
462 tiff_load_seek, tiff_load_close,
464 tiff_load_map_file, tiff_load_unmap_file);
465 if (!tiff || global_error) {
466 tiff_set_error (error,
467 GDK_PIXBUF_ERROR_FAILED,
468 _("Failed to load TIFF image"));
473 pixbuf = tiff_image_parse (tiff, context, error);
475 g_object_unref (pixbuf);
476 retval = pixbuf != NULL;
479 tiff_set_error (error,
480 GDK_PIXBUF_ERROR_FAILED,
481 _("Failed to load TIFF image"));
482 tiff_pop_handlers ();
491 g_assert (!global_error);
493 g_free (context->buffer);
496 tiff_pop_handlers ();
502 make_available_at_least (TiffContext *context, guint needed)
504 guchar *new_buffer = NULL;
507 need_alloc = context->used + needed;
508 if (need_alloc > context->allocated) {
510 while (new_size < need_alloc)
513 new_buffer = g_try_realloc (context->buffer, new_size);
515 context->buffer = new_buffer;
516 context->allocated = new_size;
525 gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf,
526 guint size, GError **error)
528 TiffContext *context = (TiffContext *) data;
530 g_return_val_if_fail (data != NULL, FALSE);
532 if (!make_available_at_least (context, size)) {
535 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
536 _("Insufficient memory to open TIFF file"));
540 memcpy (context->buffer + context->used, buf, size);
541 context->used += size;
553 tiff_save_read (thandle_t handle, tdata_t buf, tsize_t size)
559 tiff_save_write (thandle_t handle, tdata_t buf, tsize_t size)
561 TiffSaveContext *context = (TiffSaveContext *)handle;
563 /* Modify buffer length */
564 if (context->pos + size > context->used)
565 context->used = context->pos + size;
568 if (context->used > context->allocated) {
569 context->buffer = g_realloc (context->buffer, context->pos + size);
570 context->allocated = context->used;
573 /* Now copy the data */
574 memcpy (context->buffer + context->pos, buf, size);
577 context->pos += size;
583 tiff_save_seek (thandle_t handle, toff_t offset, int whence)
585 TiffSaveContext *context = (TiffSaveContext *)handle;
589 context->pos = offset;
592 context->pos += offset;
595 context->pos = context->used + offset;
604 tiff_save_close (thandle_t context)
610 tiff_save_size (thandle_t handle)
615 static TiffSaveContext *
616 create_save_context (void)
618 TiffSaveContext *context;
620 context = g_new (TiffSaveContext, 1);
621 context->buffer = NULL;
622 context->allocated = 0;
630 free_save_context (TiffSaveContext *context)
632 g_free (context->buffer);
637 gdk_pixbuf__tiff_image_save_to_callback (GdkPixbufSaveFunc save_func,
645 gint width, height, rowstride;
648 gushort alpha_samples[1] = { EXTRASAMPLE_UNASSALPHA };
650 TiffSaveContext *context;
653 tiff_push_handlers ();
655 context = create_save_context ();
656 tiff = TIFFClientOpen ("libtiff-pixbuf", "w", context,
657 tiff_save_read, tiff_save_write,
658 tiff_save_seek, tiff_save_close,
662 if (!tiff || global_error) {
663 tiff_set_error (error,
664 GDK_PIXBUF_ERROR_FAILED,
665 _("Failed to save TIFF image"));
667 tiff_pop_handlers ();
669 free_save_context (context);
673 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
674 pixels = gdk_pixbuf_get_pixels (pixbuf);
676 has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
678 height = gdk_pixbuf_get_height (pixbuf);
679 width = gdk_pixbuf_get_width (pixbuf);
681 TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, width);
682 TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, height);
683 TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, 8);
684 TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, has_alpha ? 4 : 3);
685 TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, height);
688 TIFFSetField (tiff, TIFFTAG_EXTRASAMPLES, 1, alpha_samples);
690 TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
691 TIFFSetField (tiff, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
692 TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
694 for (y = 0; y < height; y++) {
695 if (TIFFWriteScanline (tiff, pixels + y * rowstride, y, 0) == -1 ||
701 tiff_set_error (error,
702 GDK_PIXBUF_ERROR_FAILED,
703 _("Failed to write TIFF data"));
707 free_save_context (context);
708 tiff_pop_handlers ();
715 tiff_set_error (error,
716 GDK_PIXBUF_ERROR_FAILED,
717 _("TIFFClose operation failed"));
719 free_save_context (context);
720 tiff_pop_handlers ();
725 tiff_pop_handlers ();
727 /* Now call the callback */
728 retval = save_func (context->buffer, context->used, error, user_data);
730 free_save_context (context);
736 save_to_file_cb (const gchar *buf,
744 bytes = fwrite (buf, sizeof (gchar), count, (FILE *) data);
754 GDK_PIXBUF_ERROR_FAILED,
755 _("Couldn't write to TIFF file"));
763 gdk_pixbuf__tiff_image_save (FILE *f,
769 return gdk_pixbuf__tiff_image_save_to_callback (save_to_file_cb,
775 #define MODULE_ENTRY(type,function) function
777 #define MODULE_ENTRY(type,function) _gdk_pixbuf__ ## type ## _ ## function
781 MODULE_ENTRY (tiff, fill_vtable) (GdkPixbufModule *module)
783 module->load = gdk_pixbuf__tiff_image_load;
784 module->begin_load = gdk_pixbuf__tiff_image_begin_load;
785 module->stop_load = gdk_pixbuf__tiff_image_stop_load;
786 module->load_increment = gdk_pixbuf__tiff_image_load_increment;
787 module->save = gdk_pixbuf__tiff_image_save;
788 module->save_to_callback = gdk_pixbuf__tiff_image_save_to_callback;
792 MODULE_ENTRY (tiff, fill_info) (GdkPixbufFormat *info)
794 static GdkPixbufModulePattern signature[] = {
795 { "MM \x2a", " z ", 100 },
796 { "II\x2a ", " z", 100 },
799 static gchar * mime_types[] = {
803 static gchar * extensions[] = {
810 info->signature = signature;
811 info->description = N_("The TIFF image format");
812 info->mime_types = mime_types;
813 info->extensions = extensions;
814 /* not threadsafe, due to the error handler handling */
815 info->flags = GDK_PIXBUF_FORMAT_WRITABLE;
816 info->license = "LGPL";