]> Pileus Git - ~andy/gtk/blobdiff - gdk-pixbuf/io-tiff.c
[quartz] Delete the typedef of GdkDevicePrivate
[~andy/gtk] / gdk-pixbuf / io-tiff.c
index 455d3691d7f953577c14d48eee504ec4ce9bd862..7128a5c080b496509539b71ffd36752aea25307f 100644 (file)
@@ -7,7 +7,7 @@
  * Authors: Mark Crichton <crichton@gimp.org>
  *          Federico Mena-Quintero <federico@gimp.org>
  *          Jonathan Blandford <jrb@redhat.com>
- *          Søren Sandmann <sandmann@daimi.au.dk>
+ *          Sren Sandmann <sandmann@daimi.au.dk>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -27,7 +27,7 @@
 
 /* Following code (almost) blatantly ripped from Imlib */
 
-#include <config.h>
+#include "config.h"
 #include <stdlib.h>
 #include <string.h>
 #ifdef HAVE_UNISTD_H
@@ -128,9 +128,9 @@ tiff_set_error (GError    **error,
                 global_error = NULL;
         }
         else {
-                g_set_error (error,
-                             GDK_PIXBUF_ERROR,
-                             error_code, msg);
+                g_set_error_literal (error,
+                                     GDK_PIXBUF_ERROR,
+                                     error_code, msg);
         }
 }
 
@@ -141,27 +141,19 @@ static void free_buffer (guchar *pixels, gpointer data)
        g_free (pixels);
 }
 
-#if TIFFLIB_VERSION >= 20031226
-static gboolean tifflibversion (int *major, int *minor, int *revision)
-{
-        if (sscanf (TIFFGetVersion(), 
-                    "LIBTIFF, Version %d.%d.%d", 
-                    major, minor, revision) < 3)
-                return FALSE;
-
-        return TRUE;
-}
-#endif
-
 static GdkPixbuf *
 tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
 {
        guchar *pixels = NULL;
        gint width, height, rowstride, bytes;
        GdkPixbuf *pixbuf;
-#if TIFFLIB_VERSION >= 20031226
-        gint major, minor, revision;
-#endif
+       uint16 orientation = 0;
+       uint16 transform = 0;
+        uint16 codec;
+        gchar *icc_profile_base64;
+        const gchar *icc_profile;
+        guint icc_profile_size;
+        gint retval;
 
         /* We're called with the lock held. */
         
@@ -182,28 +174,28 @@ tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
         }
 
         if (width <= 0 || height <= 0) {
-                g_set_error (error,
-                             GDK_PIXBUF_ERROR,
-                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
-                             _("Width or height of TIFF image is zero"));
+                g_set_error_literal (error,
+                                     GDK_PIXBUF_ERROR,
+                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                                     _("Width or height of TIFF image is zero"));
                 return NULL;                
         }
         
         rowstride = width * 4;
         if (rowstride / 4 != width) { /* overflow */
-                g_set_error (error,
-                             GDK_PIXBUF_ERROR,
-                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
-                             _("Dimensions of TIFF image too large"));
+                g_set_error_literal (error,
+                                     GDK_PIXBUF_ERROR,
+                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                                     _("Dimensions of TIFF image too large"));
                 return NULL;                
         }
         
         bytes = height * rowstride;
         if (bytes / rowstride != height) { /* overflow */
-                g_set_error (error,
-                             GDK_PIXBUF_ERROR,
-                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
-                             _("Dimensions of TIFF image too large"));
+                g_set_error_literal (error,
+                                     GDK_PIXBUF_ERROR,
+                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                                     _("Dimensions of TIFF image too large"));
                 return NULL;                
         }
 
@@ -212,6 +204,11 @@ tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
                 gint h = height;
                (* context->size_func) (&w, &h, context->user_data);
                 
+               /* This is a signal that this function is being called
+                  to support gdk_pixbuf_get_file_info, so we can stop
+                  parsing the tiff file at this point. It is not an
+                  error condition. */
+
                 if (w == 0 || h == 0)
                     return NULL;
         }
@@ -219,10 +216,10 @@ tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
         pixels = g_try_malloc (bytes);
 
         if (!pixels) {
-                g_set_error (error,
-                             GDK_PIXBUF_ERROR,
-                             GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
-                             _("Insufficient memory to open TIFF file"));
+                g_set_error_literal (error,
+                                     GDK_PIXBUF_ERROR,
+                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
+                                     _("Insufficient memory to open TIFF file"));
                 return NULL;
         }
 
@@ -231,102 +228,95 @@ tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
                                            free_buffer, NULL);
         if (!pixbuf) {
                 g_free (pixels);
-                g_set_error (error,
-                             GDK_PIXBUF_ERROR,
-                             GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
-                             _("Insufficient memory to open TIFF file"));
+                g_set_error_literal (error,
+                                     GDK_PIXBUF_ERROR,
+                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
+                                     _("Insufficient memory to open TIFF file"));
                 return NULL;
         }
 
+       /* Set the "orientation" key associated with this image. libtiff 
+          orientation handling is odd, so further processing is required
+          by higher-level functions based on this tag. If the embedded
+          orientation tag is 1-4, libtiff flips/mirrors the image as
+          required, and no client processing is required - so we report 
+          no orientation. Orientations 5-8 require rotations which would 
+          swap the width and height of the image. libtiff does not do this. 
+          Instead it interprets orientations 5-8 the same as 1-4. 
+          See http://bugzilla.remotesensing.org/show_bug.cgi?id=1548.
+          To correct for this, the client must apply the transform normally
+          used for orientation 5 to both orientations 5 and 7, and apply
+          the transform normally used for orientation 7 for both
+          orientations 6 and 8. Then everythings works out OK! */
+       
+       TIFFGetField (tiff, TIFFTAG_ORIENTATION, &orientation);
+
+       switch (orientation) {
+               case 5:
+               case 7:
+                       transform = 5;
+                       break;
+               case 6:
+               case 8:
+                       transform = 7;
+                       break;
+               default:
+                       transform = 0;
+                       break;
+       }
+
+       if (transform > 0 ) {
+               gchar str[5];
+               g_snprintf (str, sizeof (str), "%d", transform);
+               gdk_pixbuf_set_option (pixbuf, "orientation", str);
+       }
+
+        TIFFGetField (tiff, TIFFTAG_COMPRESSION, &codec);
+        if (codec > 0) {
+          gchar str[5];
+          g_snprintf (str, sizeof (str), "%d", codec);
+          gdk_pixbuf_set_option (pixbuf, "compression", str);
+        }
+
+        /* Extract embedded ICC profile */
+        retval = TIFFGetField (tiff, TIFFTAG_ICCPROFILE, &icc_profile_size, &icc_profile);
+        if (retval == 1) {
+                icc_profile_base64 = g_base64_encode ((const guchar *) icc_profile, icc_profile_size);
+                gdk_pixbuf_set_option (pixbuf, "icc-profile", icc_profile_base64);
+                g_free (icc_profile_base64);
+        }
+
        if (context && context->prepare_func)
                (* context->prepare_func) (pixbuf, NULL, context->user_data);
 
-#if TIFFLIB_VERSION >= 20031226
-        if (tifflibversion(&major, &minor, &revision) && major == 3 &&
-            (minor > 6 || (minor == 6 && revision > 0))) {                
-                if (!TIFFReadRGBAImageOriented (tiff, width, height, (uint32 *)pixels, ORIENTATION_TOPLEFT, 1) || global_error) 
-                {
-                        tiff_set_error (error,
-                                        GDK_PIXBUF_ERROR_FAILED,
-                                        _("Failed to load RGB data from TIFF file"));
-                        g_object_unref (pixbuf);
-                        return NULL;
-                }
+       if (!TIFFReadRGBAImageOriented (tiff, width, height, (uint32 *)pixels, ORIENTATION_TOPLEFT, 1) || global_error) {
+               tiff_set_error (error,
+                                GDK_PIXBUF_ERROR_FAILED,
+                                _("Failed to load RGB data from TIFF file"));
+               g_object_unref (pixbuf);
+               return NULL;
+       }
 
 #if G_BYTE_ORDER == G_BIG_ENDIAN
-                /* Turns out that the packing used by TIFFRGBAImage depends on 
-                 * the host byte order... 
-                 */ 
-                while (pixels < pixbuf->pixels + bytes) {
-                        uint32 pixel = *(uint32 *)pixels;
-                        int r = TIFFGetR(pixel);
-                        int g = TIFFGetG(pixel);
-                        int b = TIFFGetB(pixel);
-                        int a = TIFFGetA(pixel);
-                        *pixels++ = r;
-                        *pixels++ = g;
-                        *pixels++ = b;
-                        *pixels++ = a;
-                }
-#endif
-        }
-        else 
+       /* Turns out that the packing used by TIFFRGBAImage depends on 
+         * the host byte order... 
+         */ 
+       while (pixels < pixbuf->pixels + bytes) {
+               uint32 pixel = *(uint32 *)pixels;
+               int r = TIFFGetR(pixel);
+               int g = TIFFGetG(pixel);
+               int b = TIFFGetB(pixel);
+               int a = TIFFGetA(pixel);
+               *pixels++ = r;
+               *pixels++ = g;
+               *pixels++ = b;
+               *pixels++ = a;
+       }
 #endif
-              {
-                uint32 *rast, *tmp_rast;
-                gint x, y;
-                guchar *tmppix;
-
-                /* Yes, it needs to be _TIFFMalloc... */
-                rast = (uint32 *) _TIFFmalloc (width * height * sizeof (uint32));
-                if (!rast) {
-                        g_set_error (error,
-                                     GDK_PIXBUF_ERROR,
-                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
-                                     _("Insufficient memory to open TIFF file"));
-                        g_object_unref (pixbuf);
-                        
-                        return NULL;
-                }
-                if (!TIFFReadRGBAImage (tiff, width, height, rast, 1) || global_error) {
-                        tiff_set_error (error,
-                                        GDK_PIXBUF_ERROR_FAILED,
-                                        _("Failed to load RGB data from TIFF file"));
-                        g_object_unref (pixbuf);
-                        _TIFFfree (rast);
-                        
-                        return NULL;
-                }
-                
-                pixels = gdk_pixbuf_get_pixels (pixbuf);
-                
-                g_assert (pixels);
-                
-                tmppix = pixels;
-                
-                for (y = 0; y < height; y++) {
-                        /* Unexplainable...are tiffs backwards? */
-                        /* Also looking at the GIMP plugin, this
-                         * whole reading thing can be a bit more
-                         * robust.
-                         */
-                        tmp_rast = rast + ((height - y - 1) * width);
-                        for (x = 0; x < width; x++) {
-                                tmppix[0] = TIFFGetR (*tmp_rast);
-                                tmppix[1] = TIFFGetG (*tmp_rast);
-                                tmppix[2] = TIFFGetB (*tmp_rast);
-                                tmppix[3] = TIFFGetA (*tmp_rast);
-                                tmp_rast++;
-                                tmppix += 4;
-                        }
-                }
-                
-                _TIFFfree (rast);
-             }
 
        if (context && context->update_func)
                (* context->update_func) (pixbuf, 0, 0, width, height, context->user_data);
-        
+
         return pixbuf;
 }
 
@@ -565,10 +555,10 @@ gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf,
        g_return_val_if_fail (data != NULL, FALSE);
         
         if (!make_available_at_least (context, size)) {
-                g_set_error (error,
-                             GDK_PIXBUF_ERROR,
-                             GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
-                             _("Insufficient memory to open TIFF file"));
+                g_set_error_literal (error,
+                                     GDK_PIXBUF_ERROR,
+                                     GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
+                                     _("Insufficient memory to open TIFF file"));
                 return FALSE;
         }
         
@@ -684,6 +674,8 @@ gdk_pixbuf__tiff_image_save_to_callback (GdkPixbufSaveFunc   save_func,
         int y;
         TiffSaveContext *context;
         gboolean retval;
+        guchar *icc_profile = NULL;
+        gsize icc_profile_size = 0;
 
         tiff_push_handlers ();
 
@@ -719,6 +711,40 @@ gdk_pixbuf__tiff_image_save_to_callback (GdkPixbufSaveFunc   save_func,
         TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, has_alpha ? 4 : 3);
         TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, height);
 
+        /* libtiff supports a number of 'codecs' such as:
+           1 None, 2 Huffman, 5 LZW, 7 JPEG, 8 Deflate, see tiff.h */
+        if (keys && *keys && values && *values) {
+            guint i = 0;
+
+            while (keys[i]) {
+                if (g_str_equal (keys[i], "compression")) {
+                    guint16 codec = strtol (values[i], NULL, 0);
+                    if (TIFFIsCODECConfigured (codec))
+                        TIFFSetField (tiff, TIFFTAG_COMPRESSION, codec);
+                    else {
+                        tiff_set_error (error,
+                                        GDK_PIXBUF_ERROR_FAILED,
+                                        _("TIFF compression doesn't refer to a valid codec."));
+                        retval = FALSE;
+                        goto cleanup;
+                    }
+                } else if (g_str_equal (keys[i], "icc-profile")) {
+                        /* decode from base64 */
+                        icc_profile = g_base64_decode (values[i], &icc_profile_size);
+                        if (icc_profile_size < 127) {
+                            g_set_error (error,
+                                         GDK_PIXBUF_ERROR,
+                                         GDK_PIXBUF_ERROR_BAD_OPTION,
+                                         _("Color profile has invalid length %d."),
+                                         (gint)icc_profile_size);
+                            retval = FALSE;
+                            goto cleanup;
+                        }
+                }
+                i++;
+            }
+        }
+
         if (has_alpha)
                 TIFFSetField (tiff, TIFFTAG_EXTRASAMPLES, 1, alpha_samples);
 
@@ -726,6 +752,9 @@ gdk_pixbuf__tiff_image_save_to_callback (GdkPixbufSaveFunc   save_func,
         TIFFSetField (tiff, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);        
         TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
 
+        if (icc_profile != NULL)
+                TIFFSetField (tiff, TIFFTAG_ICCPROFILE, icc_profile_size, icc_profile);
+
         for (y = 0; y < height; y++) {
                 if (TIFFWriteScanline (tiff, pixels + y * rowstride, y, 0) == -1 ||
                     global_error)
@@ -738,11 +767,8 @@ gdk_pixbuf__tiff_image_save_to_callback (GdkPixbufSaveFunc   save_func,
                                 _("Failed to write TIFF data"));
 
                 TIFFClose (tiff);
-
-                free_save_context (context);               
-                tiff_pop_handlers ();
-                
-                return FALSE;
+                retval = FALSE;
+                goto cleanup;
         }
 
         TIFFClose (tiff);
@@ -750,20 +776,18 @@ gdk_pixbuf__tiff_image_save_to_callback (GdkPixbufSaveFunc   save_func,
                 tiff_set_error (error,
                                 GDK_PIXBUF_ERROR_FAILED,
                                 _("TIFFClose operation failed"));
-
-                free_save_context (context);               
-                tiff_pop_handlers ();
-                
-                return FALSE;
+                retval = FALSE;
+                goto cleanup;
         }
 
-        tiff_pop_handlers ();
 
         /* Now call the callback */
         retval = save_func (context->buffer, context->used, error, user_data);
 
-        free_save_context (context);               
-        
+cleanup:
+        g_free (icc_profile);
+        tiff_pop_handlers ();
+        free_save_context (context);
         return retval;
 }
 
@@ -784,10 +808,10 @@ save_to_file_cb (const gchar *buf,
        }
 
        if (count) {
-               g_set_error (error,
-                            GDK_PIXBUF_ERROR,
-                            GDK_PIXBUF_ERROR_FAILED,
-                            _("Couldn't write to TIFF file"));
+               g_set_error_literal (error,
+                                     GDK_PIXBUF_ERROR,
+                                     GDK_PIXBUF_ERROR_FAILED,
+                                     _("Couldn't write to TIFF file"));
                return FALSE;
        }
        
@@ -806,8 +830,13 @@ gdk_pixbuf__tiff_image_save (FILE          *f,
                                                         values, error);
 }
 
-void
-MODULE_ENTRY (tiff, fill_vtable) (GdkPixbufModule *module)
+#ifndef INCLUDE_tiff
+#define MODULE_ENTRY(function) G_MODULE_EXPORT void function
+#else
+#define MODULE_ENTRY(function) void _gdk_pixbuf__tiff_ ## function
+#endif
+
+MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
 {
         module->load = gdk_pixbuf__tiff_image_load;
         module->begin_load = gdk_pixbuf__tiff_image_begin_load;
@@ -817,12 +846,12 @@ MODULE_ENTRY (tiff, fill_vtable) (GdkPixbufModule *module)
         module->save_to_callback = gdk_pixbuf__tiff_image_save_to_callback;
 }
 
-void
-MODULE_ENTRY (tiff, fill_info) (GdkPixbufFormat *info)
+MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
 {
         static GdkPixbufModulePattern signature[] = {
                 { "MM \x2a", "  z ", 100 },
                 { "II\x2a ", "   z", 100 },
+                { "II* \020   CR\002 ", "   z zzz   z", 0 },
                 { NULL, NULL, 0 }
         };
        static gchar * mime_types[] = {