]> Pileus Git - ~andy/gtk/blobdiff - gdk-pixbuf/io-tiff.c
Add icc-profile option to gdk-pixbuf for the TIFF image format
[~andy/gtk] / gdk-pixbuf / io-tiff.c
index 7f8b79318e7dcad7c09c9b03ae17e709afce5948..221bcac62659fdbe53da286c7f0a0c1ead79480c 100644 (file)
@@ -150,6 +150,10 @@ tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
        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. */
         
@@ -274,6 +278,14 @@ tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
           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);
 
@@ -662,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 ();
 
@@ -710,13 +724,22 @@ gdk_pixbuf__tiff_image_save_to_callback (GdkPixbufSaveFunc   save_func,
                     else {
                         tiff_set_error (error,
                                         GDK_PIXBUF_ERROR_FAILED,
-                            _("TIFF compression doesn't refer to a valid codec."));
-
-                        tiff_pop_handlers ();
-
-                        free_save_context (context);
-                        return FALSE;
+                                        _("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'."),
+                                         icc_profile_size);
+                            retval = FALSE;
+                            goto cleanup;
+                        }
                 }
                 i++;
             }
@@ -729,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)
@@ -741,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);
@@ -753,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;
 }