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) {
205 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
206 _("Width or height of TIFF image is zero"));
211 pixels = g_try_malloc (bytes);
216 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
217 _("Insufficient memory to open TIFF file"));
221 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
222 width, height, rowstride,
228 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
229 _("Insufficient memory to open TIFF file"));
233 /* Set the "orientation" key associated with this image. libtiff
234 orientation handling is odd, so further processing is required
235 by higher-level functions based on this tag. If the embedded
236 orientation tag is 1-4, libtiff flips/mirrors the image as
237 required, and no client processing is required - so we report
238 no orientation. Orientations 5-8 require rotations which would
239 swap the width and height of the image. libtiff does not do this.
240 Instead it interprets orientations 5-8 the same as 1-4.
241 See http://bugzilla.remotesensing.org/show_bug.cgi?id=1548.
242 To correct for this, the client must apply the transform normally
243 used for orientation 5 to both orientations 5 and 7, and apply
244 the transform normally used for orientation 7 for both
245 orientations 6 and 8. Then everythings works out OK! */
247 TIFFGetField (tiff, TIFFTAG_ORIENTATION, &orientation);
249 switch (orientation) {
263 if (transform > 0 ) {
265 snprintf (str, sizeof (str), "%d", transform);
266 gdk_pixbuf_set_option (pixbuf, "orientation", str);
269 if (context && context->prepare_func)
270 (* context->prepare_func) (pixbuf, NULL, context->user_data);
272 if (!TIFFReadRGBAImageOriented (tiff, width, height, (uint32 *)pixels, ORIENTATION_TOPLEFT, 1) || global_error) {
273 tiff_set_error (error,
274 GDK_PIXBUF_ERROR_FAILED,
275 _("Failed to load RGB data from TIFF file"));
276 g_object_unref (pixbuf);
280 #if G_BYTE_ORDER == G_BIG_ENDIAN
281 /* Turns out that the packing used by TIFFRGBAImage depends on
282 * the host byte order...
284 while (pixels < pixbuf->pixels + bytes) {
285 uint32 pixel = *(uint32 *)pixels;
286 int r = TIFFGetR(pixel);
287 int g = TIFFGetG(pixel);
288 int b = TIFFGetB(pixel);
289 int a = TIFFGetA(pixel);
297 if (context && context->update_func)
298 (* context->update_func) (pixbuf, 0, 0, width, height, context->user_data);
308 gdk_pixbuf__tiff_image_load (FILE *f, GError **error)
314 g_return_val_if_fail (f != NULL, NULL);
316 tiff_push_handlers ();
320 /* On OSF, apparently fseek() works in some on-demand way, so
321 * the fseek gdk_pixbuf_new_from_file() doesn't work here
322 * since we are using the raw file descriptor. So, we call lseek() on the fd
323 * before using it. (#60840)
325 lseek (fd, 0, SEEK_SET);
326 tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
328 if (!tiff || global_error) {
329 tiff_set_error (error,
330 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
331 _("Failed to open TIFF image"));
332 tiff_pop_handlers ();
337 pixbuf = tiff_image_parse (tiff, NULL, error);
341 tiff_set_error (error,
342 GDK_PIXBUF_ERROR_FAILED,
343 _("TIFFClose operation failed"));
346 tiff_pop_handlers ();
353 /* Progressive loader */
356 gdk_pixbuf__tiff_image_begin_load (GdkPixbufModuleSizeFunc size_func,
357 GdkPixbufModulePreparedFunc prepare_func,
358 GdkPixbufModuleUpdatedFunc update_func,
362 TiffContext *context;
364 context = g_new0 (TiffContext, 1);
365 context->size_func = size_func;
366 context->prepare_func = prepare_func;
367 context->update_func = update_func;
368 context->user_data = user_data;
369 context->buffer = NULL;
370 context->allocated = 0;
378 tiff_load_read (thandle_t handle, tdata_t buf, tsize_t size)
380 TiffContext *context = (TiffContext *)handle;
382 if (context->pos + size > context->used)
385 memcpy (buf, context->buffer + context->pos, size);
386 context->pos += size;
391 tiff_load_write (thandle_t handle, tdata_t buf, tsize_t size)
397 tiff_load_seek (thandle_t handle, toff_t offset, int whence)
399 TiffContext *context = (TiffContext *)handle;
403 if (offset > context->used)
405 context->pos = offset;
408 if (offset + context->pos >= context->used)
410 context->pos += offset;
413 if (offset + context->used > context->used)
415 context->pos = context->used + offset;
424 tiff_load_close (thandle_t context)
430 tiff_load_size (thandle_t handle)
432 TiffContext *context = (TiffContext *)handle;
434 return context->used;
438 tiff_load_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
440 TiffContext *context = (TiffContext *)handle;
442 *buf = context->buffer;
443 *size = context->used;
449 tiff_load_unmap_file (thandle_t handle, tdata_t data, toff_t offset)
454 gdk_pixbuf__tiff_image_stop_load (gpointer data,
457 TiffContext *context = data;
461 g_return_val_if_fail (data != NULL, FALSE);
463 tiff_push_handlers ();
465 tiff = TIFFClientOpen ("libtiff-pixbuf", "r", data,
466 tiff_load_read, tiff_load_write,
467 tiff_load_seek, tiff_load_close,
469 tiff_load_map_file, tiff_load_unmap_file);
470 if (!tiff || global_error) {
471 tiff_set_error (error,
472 GDK_PIXBUF_ERROR_FAILED,
473 _("Failed to load TIFF image"));
478 pixbuf = tiff_image_parse (tiff, context, error);
480 g_object_unref (pixbuf);
481 retval = pixbuf != NULL;
484 tiff_set_error (error,
485 GDK_PIXBUF_ERROR_FAILED,
486 _("Failed to load TIFF image"));
487 tiff_pop_handlers ();
496 g_assert (!global_error);
498 g_free (context->buffer);
501 tiff_pop_handlers ();
507 make_available_at_least (TiffContext *context, guint needed)
509 guchar *new_buffer = NULL;
512 need_alloc = context->used + needed;
513 if (need_alloc > context->allocated) {
515 while (new_size < need_alloc)
518 new_buffer = g_try_realloc (context->buffer, new_size);
520 context->buffer = new_buffer;
521 context->allocated = new_size;
530 gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf,
531 guint size, GError **error)
533 TiffContext *context = (TiffContext *) data;
535 g_return_val_if_fail (data != NULL, FALSE);
537 if (!make_available_at_least (context, size)) {
540 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
541 _("Insufficient memory to open TIFF file"));
545 memcpy (context->buffer + context->used, buf, size);
546 context->used += size;
558 tiff_save_read (thandle_t handle, tdata_t buf, tsize_t size)
564 tiff_save_write (thandle_t handle, tdata_t buf, tsize_t size)
566 TiffSaveContext *context = (TiffSaveContext *)handle;
568 /* Modify buffer length */
569 if (context->pos + size > context->used)
570 context->used = context->pos + size;
573 if (context->used > context->allocated) {
574 context->buffer = g_realloc (context->buffer, context->pos + size);
575 context->allocated = context->used;
578 /* Now copy the data */
579 memcpy (context->buffer + context->pos, buf, size);
582 context->pos += size;
588 tiff_save_seek (thandle_t handle, toff_t offset, int whence)
590 TiffSaveContext *context = (TiffSaveContext *)handle;
594 context->pos = offset;
597 context->pos += offset;
600 context->pos = context->used + offset;
609 tiff_save_close (thandle_t context)
615 tiff_save_size (thandle_t handle)
620 static TiffSaveContext *
621 create_save_context (void)
623 TiffSaveContext *context;
625 context = g_new (TiffSaveContext, 1);
626 context->buffer = NULL;
627 context->allocated = 0;
635 free_save_context (TiffSaveContext *context)
637 g_free (context->buffer);
642 gdk_pixbuf__tiff_image_save_to_callback (GdkPixbufSaveFunc save_func,
650 gint width, height, rowstride;
653 gushort alpha_samples[1] = { EXTRASAMPLE_UNASSALPHA };
655 TiffSaveContext *context;
658 tiff_push_handlers ();
660 context = create_save_context ();
661 tiff = TIFFClientOpen ("libtiff-pixbuf", "w", context,
662 tiff_save_read, tiff_save_write,
663 tiff_save_seek, tiff_save_close,
667 if (!tiff || global_error) {
668 tiff_set_error (error,
669 GDK_PIXBUF_ERROR_FAILED,
670 _("Failed to save TIFF image"));
672 tiff_pop_handlers ();
674 free_save_context (context);
678 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
679 pixels = gdk_pixbuf_get_pixels (pixbuf);
681 has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
683 height = gdk_pixbuf_get_height (pixbuf);
684 width = gdk_pixbuf_get_width (pixbuf);
686 TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, width);
687 TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, height);
688 TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, 8);
689 TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, has_alpha ? 4 : 3);
690 TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, height);
693 TIFFSetField (tiff, TIFFTAG_EXTRASAMPLES, 1, alpha_samples);
695 TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
696 TIFFSetField (tiff, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
697 TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
699 for (y = 0; y < height; y++) {
700 if (TIFFWriteScanline (tiff, pixels + y * rowstride, y, 0) == -1 ||
706 tiff_set_error (error,
707 GDK_PIXBUF_ERROR_FAILED,
708 _("Failed to write TIFF data"));
712 free_save_context (context);
713 tiff_pop_handlers ();
720 tiff_set_error (error,
721 GDK_PIXBUF_ERROR_FAILED,
722 _("TIFFClose operation failed"));
724 free_save_context (context);
725 tiff_pop_handlers ();
730 tiff_pop_handlers ();
732 /* Now call the callback */
733 retval = save_func (context->buffer, context->used, error, user_data);
735 free_save_context (context);
741 save_to_file_cb (const gchar *buf,
749 bytes = fwrite (buf, sizeof (gchar), count, (FILE *) data);
759 GDK_PIXBUF_ERROR_FAILED,
760 _("Couldn't write to TIFF file"));
768 gdk_pixbuf__tiff_image_save (FILE *f,
774 return gdk_pixbuf__tiff_image_save_to_callback (save_to_file_cb,
780 #define MODULE_ENTRY(type,function) function
782 #define MODULE_ENTRY(type,function) _gdk_pixbuf__ ## type ## _ ## function
786 MODULE_ENTRY (tiff, fill_vtable) (GdkPixbufModule *module)
788 module->load = gdk_pixbuf__tiff_image_load;
789 module->begin_load = gdk_pixbuf__tiff_image_begin_load;
790 module->stop_load = gdk_pixbuf__tiff_image_stop_load;
791 module->load_increment = gdk_pixbuf__tiff_image_load_increment;
792 module->save = gdk_pixbuf__tiff_image_save;
793 module->save_to_callback = gdk_pixbuf__tiff_image_save_to_callback;
797 MODULE_ENTRY (tiff, fill_info) (GdkPixbufFormat *info)
799 static GdkPixbufModulePattern signature[] = {
800 { "MM \x2a", " z ", 100 },
801 { "II\x2a ", " z", 100 },
804 static gchar * mime_types[] = {
808 static gchar * extensions[] = {
815 info->signature = signature;
816 info->description = N_("The TIFF image format");
817 info->mime_types = mime_types;
818 info->extensions = extensions;
819 /* not threadsafe, due to the error handler handling */
820 info->flags = GDK_PIXBUF_FORMAT_WRITABLE;
821 info->license = "LGPL";