4 * Copyright (C) 2008 - 2009 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
15 #include "color-man.h"
18 #include "ui_fileops.h"
22 /*** color support enabled ***/
27 typedef struct _ColorManCache ColorManCache;
28 struct _ColorManCache {
29 cmsHPROFILE profile_in;
30 cmsHPROFILE profile_out;
31 cmsHTRANSFORM transform;
33 ColorManProfileType profile_in_type;
34 gchar *profile_in_file;
36 ColorManProfileType profile_out_type;
37 gchar *profile_out_file;
44 /* pixels to transform per idle call */
45 #define COLOR_MAN_CHUNK_SIZE 81900
48 static void color_man_lib_init(void)
50 static gboolean init_done = FALSE;
52 if (init_done) return;
55 cmsErrorAction(LCMS_ERROR_IGNORE);
58 static cmsHPROFILE color_man_create_adobe_comp(void)
60 /* ClayRGB1998 is AdobeRGB compatible */
61 #include "ClayRGB1998_icc.h"
62 return cmsOpenProfileFromMem(ClayRGB1998_icc, ClayRGB1998_icc_len);
66 *-------------------------------------------------------------------
67 * color transform cache
68 *-------------------------------------------------------------------
71 static GList *cm_cache_list = NULL;
74 static void color_man_cache_ref(ColorManCache *cc)
81 static void color_man_cache_unref(ColorManCache *cc)
88 if (cc->transform) cmsDeleteTransform(cc->transform);
89 if (cc->profile_in) cmsCloseProfile(cc->profile_in);
90 if (cc->profile_out) cmsCloseProfile(cc->profile_out);
92 g_free(cc->profile_in_file);
93 g_free(cc->profile_out_file);
99 static cmsHPROFILE color_man_cache_load_profile(ColorManProfileType type, const gchar *file,
100 guchar *data, guint data_len)
102 cmsHPROFILE profile = NULL;
106 case COLOR_PROFILE_FILE:
111 pathl = path_from_utf8(file);
112 profile = cmsOpenProfileFromFile(pathl, "r");
116 case COLOR_PROFILE_SRGB:
117 profile = cmsCreate_sRGBProfile();
119 case COLOR_PROFILE_ADOBERGB:
120 profile = color_man_create_adobe_comp();
122 case COLOR_PROFILE_MEM:
125 profile = cmsOpenProfileFromMem(data, data_len);
128 case COLOR_PROFILE_NONE:
136 static ColorManCache *color_man_cache_new(ColorManProfileType in_type, const gchar *in_file,
137 guchar *in_data, guint in_data_len,
138 ColorManProfileType out_type, const gchar *out_file,
139 guchar *out_data, guint out_data_len,
144 color_man_lib_init();
146 cc = g_new0(ColorManCache, 1);
149 cc->profile_in_type = in_type;
150 cc->profile_in_file = g_strdup(in_file);
152 cc->profile_out_type = out_type;
153 cc->profile_out_file = g_strdup(out_file);
155 cc->has_alpha = has_alpha;
157 cc->profile_in = color_man_cache_load_profile(cc->profile_in_type, cc->profile_in_file,
158 in_data, in_data_len);
159 cc->profile_out = color_man_cache_load_profile(cc->profile_out_type, cc->profile_out_file,
160 out_data, out_data_len);
162 if (!cc->profile_in || !cc->profile_out)
164 DEBUG_1("failed to load color profile for %s: %d %s",
165 (!cc->profile_in) ? "input" : "screen",
166 (!cc->profile_in) ? cc->profile_in_type : cc->profile_out_type,
167 (!cc->profile_in) ? cc->profile_in_file : cc->profile_out_file);
169 color_man_cache_unref(cc);
173 cc->transform = cmsCreateTransform(cc->profile_in,
174 (has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
176 (has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
177 INTENT_PERCEPTUAL, 0);
181 DEBUG_1("failed to create color profile transform");
183 color_man_cache_unref(cc);
187 if (cc->profile_in_type != COLOR_PROFILE_MEM && cc->profile_out_type != COLOR_PROFILE_MEM )
189 cm_cache_list = g_list_append(cm_cache_list, cc);
190 color_man_cache_ref(cc);
196 static void color_man_cache_free(ColorManCache *cc)
200 cm_cache_list = g_list_remove(cm_cache_list, cc);
201 color_man_cache_unref(cc);
204 static void color_man_cache_reset(void)
206 while (cm_cache_list)
210 cc = cm_cache_list->data;
211 color_man_cache_free(cc);
215 static ColorManCache *color_man_cache_find(ColorManProfileType in_type, const gchar *in_file,
216 ColorManProfileType out_type, const gchar *out_file,
221 work = cm_cache_list;
225 gboolean match = FALSE;
230 if (cc->profile_in_type == in_type &&
231 cc->profile_out_type == out_type &&
232 cc->has_alpha == has_alpha)
237 if (match && cc->profile_in_type == COLOR_PROFILE_FILE)
239 match = (cc->profile_in_file && in_file &&
240 strcmp(cc->profile_in_file, in_file) == 0);
242 if (match && cc->profile_out_type == COLOR_PROFILE_FILE)
244 match = (cc->profile_out_file && out_file &&
245 strcmp(cc->profile_out_file, out_file) == 0);
248 if (match) return cc;
254 static ColorManCache *color_man_cache_get(ColorManProfileType in_type, const gchar *in_file,
255 guchar *in_data, guint in_data_len,
256 ColorManProfileType out_type, const gchar *out_file,
257 guchar *out_data, guint out_data_len,
262 cc = color_man_cache_find(in_type, in_file, out_type, out_file, has_alpha);
265 color_man_cache_ref(cc);
269 return color_man_cache_new(in_type, in_file, in_data, in_data_len,
270 out_type, out_file, out_data, out_data_len, has_alpha);
275 *-------------------------------------------------------------------
277 *-------------------------------------------------------------------
280 static void color_man_done(ColorMan *cm, ColorManReturnType type)
284 cm->func_done(cm, type, cm->func_done_data);
288 void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h)
294 gint pixbuf_width, pixbuf_height;
297 pixbuf_width = gdk_pixbuf_get_width(pixbuf);
298 pixbuf_height = gdk_pixbuf_get_height(pixbuf);
302 pix = gdk_pixbuf_get_pixels(pixbuf);
303 rs = gdk_pixbuf_get_rowstride(pixbuf);
305 w = MIN(w, pixbuf_width - x);
306 h = MIN(h, pixbuf_height - y);
308 pix += x * ((cc->has_alpha) ? 4 : 3);
309 for (i = 0; i < h; i++)
313 pbuf = pix + ((y + i) * rs);
315 cmsDoTransform(cc->transform, pbuf, pbuf, w);
320 static gboolean color_man_idle_cb(gpointer data)
326 if (!cm->pixbuf) return FALSE;
329 cm->pixbuf != image_get_pixbuf(cm->imd))
332 color_man_done(cm, COLOR_RETURN_IMAGE_CHANGED);
336 width = gdk_pixbuf_get_width(cm->pixbuf);
337 height = gdk_pixbuf_get_height(cm->pixbuf);
339 if (cm->row > height)
341 if (!cm->incremental_sync && cm->imd)
343 image_area_changed(cm->imd, 0, 0, width, height);
347 color_man_done(cm, COLOR_RETURN_SUCCESS);
351 rh = COLOR_MAN_CHUNK_SIZE / width + 1;
352 color_man_correct_region(cm, cm->pixbuf, 0, cm->row, width, rh);
353 if (cm->incremental_sync && cm->imd) image_area_changed(cm->imd, 0, cm->row, width, rh);
359 static ColorMan *color_man_new_real(ImageWindow *imd, GdkPixbuf *pixbuf,
360 ColorManProfileType input_type, const gchar *input_file,
361 guchar *input_data, guint input_data_len,
362 ColorManProfileType screen_type, const gchar *screen_file,
363 guchar *screen_data, guint screen_data_len)
368 if (imd) pixbuf = image_get_pixbuf(imd);
370 cm = g_new0(ColorMan, 1);
373 if (cm->pixbuf) g_object_ref(cm->pixbuf);
375 has_alpha = pixbuf ? gdk_pixbuf_get_has_alpha(pixbuf) : FALSE;
377 cm->profile = color_man_cache_get(input_type, input_file, input_data, input_data_len,
378 screen_type, screen_file, screen_data, screen_data_len, has_alpha);
388 ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
389 ColorManProfileType input_type, const gchar *input_file,
390 ColorManProfileType screen_type, const gchar *screen_file,
391 guchar *screen_data, guint screen_data_len)
393 return color_man_new_real(imd, pixbuf,
394 input_type, input_file, NULL, 0,
395 screen_type, screen_file, screen_data, screen_data_len);
398 void color_man_start_bg(ColorMan *cm, ColorManDoneFunc done_func, gpointer done_data)
400 cm->func_done = done_func;
401 cm->func_done_data = done_data;
402 cm->idle_id = g_idle_add(color_man_idle_cb, cm);
405 ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
406 guchar *input_data, guint input_data_len,
407 ColorManProfileType screen_type, const gchar *screen_file,
408 guchar *screen_data, guint screen_data_len)
410 return color_man_new_real(imd, pixbuf,
411 COLOR_PROFILE_MEM, NULL, input_data, input_data_len,
412 screen_type, screen_file, screen_data, screen_data_len);
415 static gchar *color_man_get_profile_name(ColorManProfileType type, cmsHPROFILE profile)
419 case COLOR_PROFILE_SRGB:
420 return g_strdup(_("sRGB"));
421 case COLOR_PROFILE_ADOBERGB:
422 return g_strdup(_("Adobe RGB compatible"));
424 case COLOR_PROFILE_MEM:
425 case COLOR_PROFILE_FILE:
428 return g_strdup(cmsTakeProductName(profile));
430 return g_strdup(_("Custom profile"));
432 case COLOR_PROFILE_NONE:
438 gboolean color_man_get_status(ColorMan *cm, gchar **image_profile, gchar **screen_profile)
441 if (!cm) return FALSE;
445 if (image_profile) *image_profile = color_man_get_profile_name(cc->profile_in_type, cc->profile_in);
446 if (screen_profile) *screen_profile = color_man_get_profile_name(cc->profile_out_type, cc->profile_out);
450 void color_man_free(ColorMan *cm)
454 if (cm->idle_id) g_source_remove(cm->idle_id);
455 if (cm->pixbuf) g_object_unref(cm->pixbuf);
457 color_man_cache_unref(cm->profile);
462 void color_man_update(void)
464 color_man_cache_reset();
467 #else /* define HAVE_LCMS */
468 /*** color support not enabled ***/
471 ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
472 ColorManProfileType input_type, const gchar *input_file,
473 ColorManProfileType screen_type, const gchar *screen_file,
474 guchar *screen_data, guint screen_data_len)
480 ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
481 guchar *input_data, guint input_data_len,
482 ColorManProfileType screen_type, const gchar *screen_file,
483 guchar *screen_data, guint screen_data_len)
489 void color_man_free(ColorMan *cm)
494 void color_man_update(void)
499 void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h)
504 void color_man_start_bg(ColorMan *cm, ColorManDoneFunc done_func, gpointer done_data)
509 gboolean color_man_get_status(ColorMan *cm, gchar **image_profile, gchar **screen_profile)
514 #endif /* define HAVE_LCMS */
515 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */