Fixed one wrongly function call with pointer
[geeqie.git] / src / color-man.c
index d52296f..8e485bc 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * Geeqie
  * (C) 2006 John Ellis
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Author: John Ellis
  *
 #ifdef HAVE_LCMS
 /*** color support enabled ***/
 
-#ifdef HAVE_LCMS_LCMS_H
-  #include <lcms/lcms.h>
+#ifdef HAVE_LCMS2
+#include <lcms2.h>
 #else
-  #include <lcms.h>
+#include <lcms.h>
 #endif
 
 
@@ -39,7 +40,7 @@ struct _ColorManCache {
        ColorManProfileType profile_out_type;
        gchar *profile_out_file;
 
-       gint has_alpha;
+       gboolean has_alpha;
 
        gint refcount;
 };
@@ -50,15 +51,17 @@ struct _ColorManCache {
 
 static void color_man_lib_init(void)
 {
-       static gint init_done = FALSE;
+       static gboolean init_done = FALSE;
 
        if (init_done) return;
        init_done = TRUE;
 
+#ifndef HAVE_LCMS2
        cmsErrorAction(LCMS_ERROR_IGNORE);
+#endif
 }
 
-static cmsHPROFILE color_man_create_adobe_comp()
+static cmsHPROFILE color_man_create_adobe_comp(void)
 {
        /* ClayRGB1998 is AdobeRGB compatible */
 #include "ClayRGB1998_icc.h"
@@ -100,7 +103,7 @@ static void color_man_cache_unref(ColorManCache *cc)
 }
 
 static cmsHPROFILE color_man_cache_load_profile(ColorManProfileType type, const gchar *file,
-                                               unsigned char *data, guint data_len)
+                                               guchar *data, guint data_len)
 {
        cmsHPROFILE profile = NULL;
 
@@ -137,9 +140,10 @@ static cmsHPROFILE color_man_cache_load_profile(ColorManProfileType type, const
 }
 
 static ColorManCache *color_man_cache_new(ColorManProfileType in_type, const gchar *in_file,
-                                         unsigned char *in_data, guint in_data_len,
+                                         guchar *in_data, guint in_data_len,
                                          ColorManProfileType out_type, const gchar *out_file,
-                                         gint has_alpha)
+                                         guchar *out_data, guint out_data_len,
+                                         gboolean has_alpha)
 {
        ColorManCache *cc;
 
@@ -159,11 +163,11 @@ static ColorManCache *color_man_cache_new(ColorManProfileType in_type, const gch
        cc->profile_in = color_man_cache_load_profile(cc->profile_in_type, cc->profile_in_file,
                                                      in_data, in_data_len);
        cc->profile_out = color_man_cache_load_profile(cc->profile_out_type, cc->profile_out_file,
-                                                      NULL, 0);
+                                                      out_data, out_data_len);
 
        if (!cc->profile_in || !cc->profile_out)
                {
-               if (debug) printf("failed to load color profile for %s: %d %s\n",
+               DEBUG_1("failed to load color profile for %s: %d %s",
                                  (!cc->profile_in) ? "input" : "screen",
                                  (!cc->profile_in) ? cc->profile_in_type : cc->profile_out_type,
                                  (!cc->profile_in) ? cc->profile_in_file : cc->profile_out_file);
@@ -180,13 +184,13 @@ static ColorManCache *color_man_cache_new(ColorManProfileType in_type, const gch
 
        if (!cc->transform)
                {
-               if (debug) printf("failed to create color profile transform\n");
+               DEBUG_1("failed to create color profile transform");
 
                color_man_cache_unref(cc);
                return NULL;
                }
 
-       if (cc->profile_in_type != COLOR_PROFILE_MEM)
+       if (cc->profile_in_type != COLOR_PROFILE_MEM && cc->profile_out_type != COLOR_PROFILE_MEM )
                {
                cm_cache_list = g_list_append(cm_cache_list, cc);
                color_man_cache_ref(cc);
@@ -216,7 +220,7 @@ static void color_man_cache_reset(void)
 
 static ColorManCache *color_man_cache_find(ColorManProfileType in_type, const gchar *in_file,
                                           ColorManProfileType out_type, const gchar *out_file,
-                                          gint has_alpha)
+                                          gboolean has_alpha)
 {
        GList *work;
 
@@ -224,7 +228,7 @@ static ColorManCache *color_man_cache_find(ColorManProfileType in_type, const gc
        while (work)
                {
                ColorManCache *cc;
-               gint match = FALSE;
+               gboolean match = FALSE;
 
                cc = work->data;
                work = work->next;
@@ -254,9 +258,10 @@ static ColorManCache *color_man_cache_find(ColorManProfileType in_type, const gc
 }
 
 static ColorManCache *color_man_cache_get(ColorManProfileType in_type, const gchar *in_file,
-                                         unsigned char *in_data, guint in_data_len,
+                                         guchar *in_data, guint in_data_len,
                                          ColorManProfileType out_type, const gchar *out_file,
-                                         gint has_alpha)
+                                         guchar *out_data, guint out_data_len,
+                                         gboolean has_alpha)
 {
        ColorManCache *cc;
 
@@ -268,7 +273,7 @@ static ColorManCache *color_man_cache_get(ColorManProfileType in_type, const gch
                }
 
        return color_man_cache_new(in_type, in_file, in_data, in_data_len,
-                                  out_type, out_file, has_alpha);
+                                  out_type, out_file, out_data, out_data_len, has_alpha);
 }
 
 
@@ -293,7 +298,7 @@ void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, g
        gint rs;
        gint i;
        gint pixbuf_width, pixbuf_height;
-       
+
 
        pixbuf_width = gdk_pixbuf_get_width(pixbuf);
        pixbuf_height = gdk_pixbuf_get_height(pixbuf);
@@ -312,23 +317,24 @@ void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, g
                guchar *pbuf;
 
                pbuf = pix + ((y + i) * rs);
-               
+
                cmsDoTransform(cc->transform, pbuf, pbuf, w);
                }
 
 }
 
-static gint color_man_idle_cb(gpointer data)
+static gboolean color_man_idle_cb(gpointer data)
 {
        ColorMan *cm = data;
        gint width, height;
        gint rh;
+
        if (!cm->pixbuf) return FALSE;
 
        if (cm->imd &&
            cm->pixbuf != image_get_pixbuf(cm->imd))
                {
-               cm->idle_id = -1;
+               cm->idle_id = 0;
                color_man_done(cm, COLOR_RETURN_IMAGE_CHANGED);
                return FALSE;
                }
@@ -343,7 +349,7 @@ static gint color_man_idle_cb(gpointer data)
                        image_area_changed(cm->imd, 0, 0, width, height);
                        }
 
-               cm->idle_id = -1;
+               cm->idle_id = 0;
                color_man_done(cm, COLOR_RETURN_SUCCESS);
                return FALSE;
                }
@@ -358,11 +364,12 @@ static gint color_man_idle_cb(gpointer data)
 
 static ColorMan *color_man_new_real(ImageWindow *imd, GdkPixbuf *pixbuf,
                                    ColorManProfileType input_type, const gchar *input_file,
-                                   unsigned char *input_data, guint input_data_len,
-                                   ColorManProfileType screen_type, const gchar *screen_file)
+                                   guchar *input_data, guint input_data_len,
+                                   ColorManProfileType screen_type, const gchar *screen_file,
+                                   guchar *screen_data, guint screen_data_len)
 {
        ColorMan *cm;
-       gint has_alpha;
+       gboolean has_alpha;
 
        if (imd) pixbuf = image_get_pixbuf(imd);
 
@@ -371,14 +378,10 @@ static ColorMan *color_man_new_real(ImageWindow *imd, GdkPixbuf *pixbuf,
        cm->pixbuf = pixbuf;
        if (cm->pixbuf) g_object_ref(cm->pixbuf);
 
-       cm->incremental_sync = FALSE;
-       cm->row = 0;
-       cm->idle_id = -1;
-
        has_alpha = pixbuf ? gdk_pixbuf_get_has_alpha(pixbuf) : FALSE;
 
        cm->profile = color_man_cache_get(input_type, input_file, input_data, input_data_len,
-                                         screen_type, screen_file, has_alpha);
+                                         screen_type, screen_file, screen_data, screen_data_len, has_alpha);
        if (!cm->profile)
                {
                color_man_free(cm);
@@ -390,11 +393,12 @@ static ColorMan *color_man_new_real(ImageWindow *imd, GdkPixbuf *pixbuf,
 
 ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
                        ColorManProfileType input_type, const gchar *input_file,
-                       ColorManProfileType screen_type, const gchar *screen_file)
+                       ColorManProfileType screen_type, const gchar *screen_file,
+                       guchar *screen_data, guint screen_data_len)
 {
        return color_man_new_real(imd, pixbuf,
                                  input_type, input_file, NULL, 0,
-                                 screen_type, screen_file);
+                                 screen_type, screen_file, screen_data, screen_data_len);
 }
 
 void color_man_start_bg(ColorMan *cm, ColorManDoneFunc done_func, gpointer done_data)
@@ -405,19 +409,64 @@ void color_man_start_bg(ColorMan *cm, ColorManDoneFunc done_func, gpointer done_
 }
 
 ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
-                                unsigned char *input_data, guint input_data_len,
-                                ColorManProfileType screen_type, const gchar *screen_file)
+                                guchar *input_data, guint input_data_len,
+                                ColorManProfileType screen_type, const gchar *screen_file,
+                                guchar *screen_data, guint screen_data_len)
 {
        return color_man_new_real(imd, pixbuf,
                                  COLOR_PROFILE_MEM, NULL, input_data, input_data_len,
-                                 screen_type, screen_file);
+                                 screen_type, screen_file, screen_data, screen_data_len);
+}
+
+static gchar *color_man_get_profile_name(ColorManProfileType type, cmsHPROFILE profile)
+{
+       switch (type)
+               {
+               case COLOR_PROFILE_SRGB:
+                       return g_strdup(_("sRGB"));
+               case COLOR_PROFILE_ADOBERGB:
+                       return g_strdup(_("Adobe RGB compatible"));
+                       break;
+               case COLOR_PROFILE_MEM:
+               case COLOR_PROFILE_FILE:
+                       if (profile)
+                               {
+#ifdef HAVE_LCMS2
+                               cmsUInt32Number r;
+                               char buffer[20];
+                               buffer[0] = '\0';
+                               r = cmsGetProfileInfoASCII(profile, cmsInfoDescription, "en", "US", buffer, 20);
+                               buffer[19] = '\0'; /* Just to be sure */
+                               return g_strdup(buffer);
+#else
+                               return g_strdup(cmsTakeProductName(profile));
+#endif
+                               }
+                       return g_strdup(_("Custom profile"));
+                       break;
+               case COLOR_PROFILE_NONE:
+               default:
+                       return g_strdup("");
+               }
+}
+
+gboolean color_man_get_status(ColorMan *cm, gchar **image_profile, gchar **screen_profile)
+{
+       ColorManCache *cc;
+       if (!cm) return FALSE;
+
+       cc = cm->profile;
+
+       if (image_profile) *image_profile = color_man_get_profile_name(cc->profile_in_type, cc->profile_in);
+       if (screen_profile) *screen_profile = color_man_get_profile_name(cc->profile_out_type, cc->profile_out);
+       return TRUE;
 }
 
 void color_man_free(ColorMan *cm)
 {
        if (!cm) return;
 
-       if (cm->idle_id != -1) g_source_remove(cm->idle_id);
+       if (cm->idle_id) g_source_remove(cm->idle_id);
        if (cm->pixbuf) g_object_unref(cm->pixbuf);
 
        color_man_cache_unref(cm->profile);
@@ -430,21 +479,23 @@ void color_man_update(void)
        color_man_cache_reset();
 }
 
-#else
+#else /* define HAVE_LCMS */
 /*** color support not enabled ***/
 
 
 ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
                        ColorManProfileType input_type, const gchar *input_file,
-                       ColorManProfileType screen_type, const gchar *screen_file)
+                       ColorManProfileType screen_type, const gchar *screen_file,
+                       guchar *screen_data, guint screen_data_len)
 {
        /* no op */
        return NULL;
 }
 
 ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
-                                unsigned char *input_data, guint input_data_len,
-                                ColorManProfileType screen_type, const gchar *screen_file)
+                                guchar *input_data, guint input_data_len,
+                                ColorManProfileType screen_type, const gchar *screen_file,
+                                guchar *screen_data, guint screen_data_len)
 {
        /* no op */
        return NULL;
@@ -470,6 +521,10 @@ void color_man_start_bg(ColorMan *cm, ColorManDoneFunc done_func, gpointer done_
        /* no op */
 }
 
-#endif
-
+gboolean color_man_get_status(ColorMan *cm, gchar **image_profile, gchar **screen_profile)
+{
+       return FALSE;
+}
 
+#endif /* define HAVE_LCMS */
+/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */