[gimp] app: add gimp_image_convert_color_profile()



commit 1ade7ffee5debc0016bd143e445fafc0eb558483
Author: Michael Natterer <mitch gimp org>
Date:   Wed Jun 10 01:36:19 2015 +0200

    app: add gimp_image_convert_color_profile()
    
    which converts an image to another color profile. Currently unused.

 app/core/Makefile.am         |    1 +
 app/core/gimpimage-profile.c |  218 +++++++++++++++++++++++++++++++++++++++++-
 app/core/gimpimage-profile.h |   10 ++
 3 files changed, 228 insertions(+), 1 deletions(-)
---
diff --git a/app/core/Makefile.am b/app/core/Makefile.am
index c66b61e..a84d984 100644
--- a/app/core/Makefile.am
+++ b/app/core/Makefile.am
@@ -17,6 +17,7 @@ AM_CPPFLAGS = \
        $(GEGL_CFLAGS)                                  \
        $(GDK_PIXBUF_CFLAGS)                            \
        $(GEXIV2_CFLAGS)                                \
+       $(LCMS_CFLAGS)                                  \
        -I$(includedir)                                 \
        $(xobjective_c)
 
diff --git a/app/core/gimpimage-profile.c b/app/core/gimpimage-profile.c
index 87142f4..9002ae5 100644
--- a/app/core/gimpimage-profile.c
+++ b/app/core/gimpimage-profile.c
@@ -1,6 +1,9 @@
 /* GIMP - The GNU Image Manipulation Program
  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  *
+ * gimpimage-profile.c
+ * Copyright (C) 2015 Michael Natterer <mitch gimp org>
+ *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 3 of the License, or
@@ -18,6 +21,7 @@
 #include "config.h"
 
 #include <string.h>
+#include <lcms2.h>
 
 #include <cairo.h>
 #include <gdk-pixbuf/gdk-pixbuf.h>
@@ -32,14 +36,34 @@
 #include "config/gimpcoreconfig.h"
 
 #include "gimp.h"
+#include "gimpdrawable.h"
 #include "gimperror.h"
 #include "gimpimage.h"
+#include "gimpimage-colormap.h"
 #include "gimpimage-profile.h"
+#include "gimpimage-undo.h"
+#include "gimpprogress.h"
 
 #include "gimp-intl.h"
 
 
-/* public functions */
+/*  local function prototypes  */
+
+static void   gimp_image_convert_profile_rgb     (GimpImage                *image,
+                                                  GimpColorProfile          src_profile,
+                                                  GimpColorProfile          dest_profile,
+                                                  GimpColorRenderingIntent  intent,
+                                                  gboolean                  bpc,
+                                                  GimpProgress             *progress);
+static void   gimp_image_convert_profile_indexed (GimpImage                *image,
+                                                  GimpColorProfile          src_profile,
+                                                  GimpColorProfile          dest_profile,
+                                                  GimpColorRenderingIntent  intent,
+                                                  gboolean                  bpc,
+                                                  GimpProgress             *progress);
+
+
+/*  public functions  */
 
 gboolean
 gimp_image_validate_icc_profile (GimpImage           *image,
@@ -201,3 +225,195 @@ gimp_image_set_color_profile (GimpImage         *image,
 
   return FALSE;
 }
+
+gboolean
+gimp_image_convert_color_profile (GimpImage                *image,
+                                  GimpColorProfile          dest_profile,
+                                  GimpColorRenderingIntent  intent,
+                                  gboolean                  bpc,
+                                  GimpProgress             *progress,
+                                  GError                  **error)
+{
+  GimpColorProfile  src_profile;
+  gchar            *src_label;
+  gchar            *dest_label;
+
+  g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
+  g_return_val_if_fail (dest_profile != NULL, FALSE);
+  g_return_val_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress), FALSE);
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  if (! gimp_image_validate_color_profile (image, dest_profile, error))
+    return FALSE;
+
+  src_profile = gimp_color_managed_get_color_profile (GIMP_COLOR_MANAGED (image));
+
+  if (gimp_color_profile_is_equal (src_profile, dest_profile))
+    {
+      gimp_color_profile_close (src_profile);
+      return TRUE;
+    }
+
+  src_label  = gimp_color_profile_get_label (src_profile);
+  dest_label = gimp_color_profile_get_label (dest_profile);
+
+  if (progress)
+    gimp_progress_start (progress, FALSE,
+                         _("Converting from '%s' to '%s'"),
+                         src_label, dest_label);
+
+  g_free (dest_label);
+  g_free (src_label);
+
+  gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_CONVERT,
+                               _("Color profile conversion"));
+
+  switch (gimp_image_get_base_type (image))
+    {
+    case GIMP_RGB:
+      gimp_image_convert_profile_rgb (image,
+                                      src_profile, dest_profile,
+                                      intent, bpc,
+                                      progress);
+      break;
+
+    case GIMP_GRAY:
+      break;
+
+    case GIMP_INDEXED:
+      gimp_image_convert_profile_indexed (image,
+                                          src_profile, dest_profile,
+                                          intent, bpc,
+                                          progress);
+      break;
+    }
+
+  gimp_image_undo_group_end (image);
+
+  if (progress)
+    gimp_progress_end (progress);
+
+  gimp_color_profile_close (src_profile);
+
+  return TRUE;
+}
+
+
+/*  private functions  */
+
+static void
+gimp_image_convert_profile_rgb (GimpImage                *image,
+                                GimpColorProfile          src_profile,
+                                GimpColorProfile          dest_profile,
+                                GimpColorRenderingIntent  intent,
+                                gboolean                  bpc,
+                                GimpProgress             *progress)
+{
+  GList *layers;
+  GList *list;
+  gint   n_drawables;
+  gint   nth_drawable;
+
+  layers = gimp_image_get_layer_list (image);
+
+  n_drawables = g_list_length (layers);
+
+  for (list = layers, nth_drawable = 0;
+       list;
+       list = g_list_next (list), nth_drawable++)
+    {
+      GimpDrawable    *drawable = list->data;
+      const Babl      *iter_format;
+      cmsUInt32Number  lcms_format;
+      cmsUInt32Number  flags;
+      cmsHTRANSFORM    transform;
+
+      if (gimp_viewable_get_children (GIMP_VIEWABLE (drawable)))
+        continue;
+
+      iter_format =
+        gimp_color_profile_get_format (gimp_drawable_get_format (drawable),
+                                       &lcms_format);
+
+      flags = cmsFLAGS_NOOPTIMIZE;
+
+      if (bpc)
+        flags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
+
+      transform = cmsCreateTransform (src_profile,  lcms_format,
+                                      dest_profile, lcms_format,
+                                      intent, flags);
+
+      if (transform)
+        {
+          GeglBuffer         *buffer;
+          GeglBufferIterator *iter;
+
+          buffer = gimp_drawable_get_buffer (drawable);
+
+          gimp_drawable_push_undo (drawable, NULL, NULL,
+                                   0, 0,
+                                   gegl_buffer_get_width  (buffer),
+                                   gegl_buffer_get_height (buffer));
+
+          iter = gegl_buffer_iterator_new (buffer, NULL, 0,
+                                           iter_format,
+                                           GEGL_ACCESS_READWRITE,
+                                           GEGL_ABYSS_NONE);
+
+          while (gegl_buffer_iterator_next (iter))
+            {
+              cmsDoTransform (transform,
+                              iter->data[0], iter->data[0], iter->length);
+            }
+
+          gimp_drawable_update (drawable, 0, 0,
+                                gegl_buffer_get_width  (buffer),
+                                gegl_buffer_get_height (buffer));
+
+          cmsDeleteTransform (transform);
+        }
+
+      if (progress)
+        gimp_progress_set_value (progress,
+                                 (gdouble) nth_drawable / (gdouble) n_drawables);
+    }
+
+  g_list_free (layers);
+}
+
+static void
+gimp_image_convert_profile_indexed (GimpImage                *image,
+                                    GimpColorProfile          src_profile,
+                                    GimpColorProfile          dest_profile,
+                                    GimpColorRenderingIntent  intent,
+                                    gboolean                  bpc,
+                                    GimpProgress             *progress)
+{
+  GimpColorTransform  transform;
+  guchar             *cmap;
+  gint                n_colors;
+
+  n_colors = gimp_image_get_colormap_size (image);
+  cmap     = g_memdup (gimp_image_get_colormap (image), n_colors * 3);
+
+  transform = cmsCreateTransform (src_profile,  TYPE_RGB_8,
+                                  dest_profile, TYPE_RGB_8,
+                                  intent,
+                                  cmsFLAGS_NOOPTIMIZE |
+                                  (bpc ? cmsFLAGS_BLACKPOINTCOMPENSATION : 0));
+
+  if (transform)
+    {
+      cmsDoTransform (transform, cmap, cmap, n_colors);
+      cmsDeleteTransform (transform);
+
+      gimp_image_set_colormap (image, cmap, n_colors, TRUE);
+    }
+  else
+    {
+      g_warning ("cmsCreateTransform() failed!");
+    }
+
+  g_free (cmap);
+}
diff --git a/app/core/gimpimage-profile.h b/app/core/gimpimage-profile.h
index b321b8b..00cefa3 100644
--- a/app/core/gimpimage-profile.h
+++ b/app/core/gimpimage-profile.h
@@ -1,6 +1,9 @@
 /* GIMP - The GNU Image Manipulation Program
  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  *
+ * gimpimage-profile.h
+ * Copyright (C) 2015 Michael Natterer <mitch gimp org>
+ *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 3 of the License, or
@@ -38,5 +41,12 @@ gboolean             gimp_image_set_color_profile      (GimpImage           *ima
                                                         GimpColorProfile     profile,
                                                         GError             **error);
 
+gboolean             gimp_image_convert_color_profile  (GimpImage                *image,
+                                                        GimpColorProfile          dest_profile,
+                                                        GimpColorRenderingIntent  intent,
+                                                        gboolean                  bpc,
+                                                        GimpProgress             *progress,
+                                                        GError                  **error);
+
 
 #endif /* __GIMP_IMAGE_PROFILE_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]