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"));
241 if (context && context->prepare_func)
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);
327 if (context && context->update_func)
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_load_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_load_write (thandle_t handle, tdata_t buf, tsize_t size)
427 tiff_load_seek (thandle_t handle, toff_t offset, int whence)
429 TiffContext *context = (TiffContext *)handle;
433 if (offset > context->used)
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;
454 tiff_load_close (thandle_t context)
460 tiff_load_size (thandle_t handle)
462 TiffContext *context = (TiffContext *)handle;
464 return context->used;
468 tiff_load_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
470 TiffContext *context = (TiffContext *)handle;
472 *buf = context->buffer;
473 *size = context->used;
479 tiff_load_unmap_file (thandle_t handle, tdata_t data, toff_t offset)
484 gdk_pixbuf__tiff_image_stop_load (gpointer data,
487 TiffContext *context = data;
491 g_return_val_if_fail (data != NULL, FALSE);
493 tiff_push_handlers ();
495 tiff = TIFFClientOpen ("libtiff-pixbuf", "r", data,
496 tiff_load_read, tiff_load_write,
497 tiff_load_seek, tiff_load_close,
499 tiff_load_map_file, tiff_load_unmap_file);
500 if (!tiff || global_error) {
501 tiff_set_error (error,
502 GDK_PIXBUF_ERROR_FAILED,
503 _("Failed to load TIFF image"));
508 pixbuf = tiff_image_parse (tiff, context, error);
510 g_object_unref (pixbuf);
511 retval = pixbuf != NULL;
514 tiff_set_error (error,
515 GDK_PIXBUF_ERROR_FAILED,
516 _("Failed to load TIFF image"));
517 tiff_pop_handlers ();
526 g_assert (!global_error);
528 g_free (context->buffer);
531 tiff_pop_handlers ();
537 make_available_at_least (TiffContext *context, guint needed)
539 guchar *new_buffer = NULL;
542 need_alloc = context->used + needed;
543 if (need_alloc > context->allocated) {
545 while (new_size < need_alloc)
548 new_buffer = g_try_realloc (context->buffer, new_size);
550 context->buffer = new_buffer;
551 context->allocated = new_size;
560 gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf,
561 guint size, GError **error)
563 TiffContext *context = (TiffContext *) data;
565 g_return_val_if_fail (data != NULL, FALSE);
567 if (!make_available_at_least (context, size)) {
570 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
571 _("Insufficient memory to open TIFF file"));
575 memcpy (context->buffer + context->used, buf, size);
576 context->used += size;
588 tiff_save_read (thandle_t handle, tdata_t buf, tsize_t size)
594 tiff_save_write (thandle_t handle, tdata_t buf, tsize_t size)
596 TiffSaveContext *context = (TiffSaveContext *)handle;
598 /* Modify buffer length */
599 if (context->pos + size > context->used)
600 context->used = context->pos + size;
603 if (context->used > context->allocated) {
604 context->buffer = g_realloc (context->buffer, context->pos + size);
605 context->allocated = context->used;
608 /* Now copy the data */
609 memcpy (context->buffer + context->pos, buf, size);
612 context->pos += size;
618 tiff_save_seek (thandle_t handle, toff_t offset, int whence)
620 TiffSaveContext *context = (TiffSaveContext *)handle;
624 context->pos = offset;
627 context->pos += offset;
630 context->pos = context->used + offset;
639 tiff_save_close (thandle_t context)
645 tiff_save_size (thandle_t handle)
650 static TiffSaveContext *
651 create_save_context (void)
653 TiffSaveContext *context;
655 context = g_new (TiffSaveContext, 1);
656 context->buffer = NULL;
657 context->allocated = 0;
665 free_save_context (TiffSaveContext *context)
667 g_free (context->buffer);
672 gdk_pixbuf__tiff_image_save_to_callback (GdkPixbufSaveFunc save_func,
680 gint width, height, rowstride;
683 gushort alpha_samples[1] = { EXTRASAMPLE_UNASSALPHA };
685 TiffSaveContext *context;
688 tiff_push_handlers ();
690 context = create_save_context ();
691 tiff = TIFFClientOpen ("libtiff-pixbuf", "w", context,
692 tiff_save_read, tiff_save_write,
693 tiff_save_seek, tiff_save_close,
697 if (!tiff || global_error) {
698 tiff_set_error (error,
699 GDK_PIXBUF_ERROR_FAILED,
700 _("Failed to save TIFF image"));
702 tiff_pop_handlers ();
704 free_save_context (context);
708 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
709 pixels = gdk_pixbuf_get_pixels (pixbuf);
711 has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
713 height = gdk_pixbuf_get_height (pixbuf);
714 width = gdk_pixbuf_get_width (pixbuf);
716 TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, width);
717 TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, height);
718 TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, 8);
719 TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, has_alpha ? 4 : 3);
720 TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, height);
723 TIFFSetField (tiff, TIFFTAG_EXTRASAMPLES, 1, alpha_samples);
725 TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
726 TIFFSetField (tiff, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
727 TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
729 for (y = 0; y < height; y++) {
730 if (TIFFWriteScanline (tiff, pixels + y * rowstride, y, 0) == -1 ||
736 tiff_set_error (error,
737 GDK_PIXBUF_ERROR_FAILED,
738 _("Failed to write TIFF data"));
742 free_save_context (context);
743 tiff_pop_handlers ();
750 tiff_set_error (error,
751 GDK_PIXBUF_ERROR_FAILED,
752 _("TIFFClose operation failed"));
754 free_save_context (context);
755 tiff_pop_handlers ();
760 tiff_pop_handlers ();
762 /* Now call the callback */
763 retval = save_func (context->buffer, context->used, error, user_data);
765 free_save_context (context);
771 save_to_file_cb (const gchar *buf,
779 bytes = fwrite (buf, sizeof (gchar), count, (FILE *) data);
789 GDK_PIXBUF_ERROR_FAILED,
790 _("Couldn't write to TIFF file"));
798 gdk_pixbuf__tiff_image_save (FILE *f,
804 return gdk_pixbuf__tiff_image_save_to_callback (save_to_file_cb,
810 MODULE_ENTRY (tiff, fill_vtable) (GdkPixbufModule *module)
812 module->load = gdk_pixbuf__tiff_image_load;
813 module->begin_load = gdk_pixbuf__tiff_image_begin_load;
814 module->stop_load = gdk_pixbuf__tiff_image_stop_load;
815 module->load_increment = gdk_pixbuf__tiff_image_load_increment;
816 module->save = gdk_pixbuf__tiff_image_save;
817 module->save_to_callback = gdk_pixbuf__tiff_image_save_to_callback;
821 MODULE_ENTRY (tiff, fill_info) (GdkPixbufFormat *info)
823 static GdkPixbufModulePattern signature[] = {
824 { "MM \x2a", " z ", 100 },
825 { "II\x2a ", " z", 100 },
828 static gchar * mime_types[] = {
832 static gchar * extensions[] = {
839 info->signature = signature;
840 info->description = N_("The TIFF image format");
841 info->mime_types = mime_types;
842 info->extensions = extensions;
843 /* not threadsafe, due to the error handler handling */
844 info->flags = GDK_PIXBUF_FORMAT_WRITABLE;
845 info->license = "LGPL";