4 * Copyright (C) 2008 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"
19 #include "ui_fileops.h"
23 /*** color support enabled ***/
25 #ifdef HAVE_LCMS_LCMS_H
26 #include <lcms/lcms.h>
32 typedef struct _ColorManCache ColorManCache;
33 struct _ColorManCache {
34 cmsHPROFILE profile_in;
35 cmsHPROFILE profile_out;
36 cmsHTRANSFORM transform;
38 ColorManProfileType profile_in_type;
39 gchar *profile_in_file;
41 ColorManProfileType profile_out_type;
42 gchar *profile_out_file;
49 /* pixels to transform per idle call */
50 #define COLOR_MAN_CHUNK_SIZE 81900
53 static void color_man_lib_init(void)
55 static gint init_done = FALSE;
57 if (init_done) return;
60 cmsErrorAction(LCMS_ERROR_IGNORE);
63 static cmsHPROFILE color_man_create_adobe_comp(void)
65 /* ClayRGB1998 is AdobeRGB compatible */
66 #include "ClayRGB1998_icc.h"
67 return cmsOpenProfileFromMem(ClayRGB1998_icc, ClayRGB1998_icc_len);
71 *-------------------------------------------------------------------
72 * color transform cache
73 *-------------------------------------------------------------------
76 static GList *cm_cache_list = NULL;
79 static void color_man_cache_ref(ColorManCache *cc)
86 static void color_man_cache_unref(ColorManCache *cc)
93 if (cc->transform) cmsDeleteTransform(cc->transform);
94 if (cc->profile_in) cmsCloseProfile(cc->profile_in);
95 if (cc->profile_out) cmsCloseProfile(cc->profile_out);
97 g_free(cc->profile_in_file);
98 g_free(cc->profile_out_file);
104 static cmsHPROFILE color_man_cache_load_profile(ColorManProfileType type, const gchar *file,
105 unsigned char *data, guint data_len)
107 cmsHPROFILE profile = NULL;
111 case COLOR_PROFILE_FILE:
116 pathl = path_from_utf8(file);
117 profile = cmsOpenProfileFromFile(pathl, "r");
121 case COLOR_PROFILE_SRGB:
122 profile = cmsCreate_sRGBProfile();
124 case COLOR_PROFILE_ADOBERGB:
125 profile = color_man_create_adobe_comp();
127 case COLOR_PROFILE_MEM:
130 profile = cmsOpenProfileFromMem(data, data_len);
133 case COLOR_PROFILE_NONE:
141 static ColorManCache *color_man_cache_new(ColorManProfileType in_type, const gchar *in_file,
142 unsigned char *in_data, guint in_data_len,
143 ColorManProfileType out_type, const gchar *out_file,
148 color_man_lib_init();
150 cc = g_new0(ColorManCache, 1);
153 cc->profile_in_type = in_type;
154 cc->profile_in_file = g_strdup(in_file);
156 cc->profile_out_type = out_type;
157 cc->profile_out_file = g_strdup(out_file);
159 cc->has_alpha = has_alpha;
161 cc->profile_in = color_man_cache_load_profile(cc->profile_in_type, cc->profile_in_file,
162 in_data, in_data_len);
163 cc->profile_out = color_man_cache_load_profile(cc->profile_out_type, cc->profile_out_file,
166 if (!cc->profile_in || !cc->profile_out)
168 DEBUG_1("failed to load color profile for %s: %d %s",
169 (!cc->profile_in) ? "input" : "screen",
170 (!cc->profile_in) ? cc->profile_in_type : cc->profile_out_type,
171 (!cc->profile_in) ? cc->profile_in_file : cc->profile_out_file);
173 color_man_cache_unref(cc);
177 cc->transform = cmsCreateTransform(cc->profile_in,
178 (has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
180 (has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
181 INTENT_PERCEPTUAL, 0);
185 DEBUG_1("failed to create color profile transform");
187 color_man_cache_unref(cc);
191 if (cc->profile_in_type != COLOR_PROFILE_MEM)
193 cm_cache_list = g_list_append(cm_cache_list, cc);
194 color_man_cache_ref(cc);
200 static void color_man_cache_free(ColorManCache *cc)
204 cm_cache_list = g_list_remove(cm_cache_list, cc);
205 color_man_cache_unref(cc);
208 static void color_man_cache_reset(void)
210 while (cm_cache_list)
214 cc = cm_cache_list->data;
215 color_man_cache_free(cc);
219 static ColorManCache *color_man_cache_find(ColorManProfileType in_type, const gchar *in_file,
220 ColorManProfileType out_type, const gchar *out_file,
225 work = cm_cache_list;
234 if (cc->profile_in_type == in_type &&
235 cc->profile_out_type == out_type &&
236 cc->has_alpha == has_alpha)
241 if (match && cc->profile_in_type == COLOR_PROFILE_FILE)
243 match = (cc->profile_in_file && in_file &&
244 strcmp(cc->profile_in_file, in_file) == 0);
246 if (match && cc->profile_out_type == COLOR_PROFILE_FILE)
248 match = (cc->profile_out_file && out_file &&
249 strcmp(cc->profile_out_file, out_file) == 0);
252 if (match) return cc;
258 static ColorManCache *color_man_cache_get(ColorManProfileType in_type, const gchar *in_file,
259 unsigned char *in_data, guint in_data_len,
260 ColorManProfileType out_type, const gchar *out_file,
265 cc = color_man_cache_find(in_type, in_file, out_type, out_file, has_alpha);
268 color_man_cache_ref(cc);
272 return color_man_cache_new(in_type, in_file, in_data, in_data_len,
273 out_type, out_file, has_alpha);
278 *-------------------------------------------------------------------
280 *-------------------------------------------------------------------
283 static void color_man_done(ColorMan *cm, ColorManReturnType type)
287 cm->func_done(cm, type, cm->func_done_data);
291 void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h)
297 gint pixbuf_width, pixbuf_height;
300 pixbuf_width = gdk_pixbuf_get_width(pixbuf);
301 pixbuf_height = gdk_pixbuf_get_height(pixbuf);
305 pix = gdk_pixbuf_get_pixels(pixbuf);
306 rs = gdk_pixbuf_get_rowstride(pixbuf);
308 w = MIN(w, pixbuf_width - x);
309 h = MIN(h, pixbuf_height - y);
311 pix += x * ((cc->has_alpha) ? 4 : 3);
312 for (i = 0; i < h; i++)
316 pbuf = pix + ((y + i) * rs);
318 cmsDoTransform(cc->transform, pbuf, pbuf, w);
323 static gint color_man_idle_cb(gpointer data)
328 if (!cm->pixbuf) return FALSE;
331 cm->pixbuf != image_get_pixbuf(cm->imd))
334 color_man_done(cm, COLOR_RETURN_IMAGE_CHANGED);
338 width = gdk_pixbuf_get_width(cm->pixbuf);
339 height = gdk_pixbuf_get_height(cm->pixbuf);
341 if (cm->row > height)
343 if (!cm->incremental_sync && cm->imd)
345 image_area_changed(cm->imd, 0, 0, width, height);
349 color_man_done(cm, COLOR_RETURN_SUCCESS);
353 rh = COLOR_MAN_CHUNK_SIZE / width + 1;
354 color_man_correct_region(cm, cm->pixbuf, 0, cm->row, width, rh);
355 if (cm->incremental_sync && cm->imd) image_area_changed(cm->imd, 0, cm->row, width, rh);
361 static ColorMan *color_man_new_real(ImageWindow *imd, GdkPixbuf *pixbuf,
362 ColorManProfileType input_type, const gchar *input_file,
363 unsigned char *input_data, guint input_data_len,
364 ColorManProfileType screen_type, const gchar *screen_file)
369 if (imd) pixbuf = image_get_pixbuf(imd);
371 cm = g_new0(ColorMan, 1);
374 if (cm->pixbuf) g_object_ref(cm->pixbuf);
376 cm->incremental_sync = FALSE;
380 has_alpha = pixbuf ? gdk_pixbuf_get_has_alpha(pixbuf) : FALSE;
382 cm->profile = color_man_cache_get(input_type, input_file, input_data, input_data_len,
383 screen_type, screen_file, has_alpha);
393 ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
394 ColorManProfileType input_type, const gchar *input_file,
395 ColorManProfileType screen_type, const gchar *screen_file)
397 return color_man_new_real(imd, pixbuf,
398 input_type, input_file, NULL, 0,
399 screen_type, screen_file);
402 void color_man_start_bg(ColorMan *cm, ColorManDoneFunc done_func, gpointer done_data)
404 cm->func_done = done_func;
405 cm->func_done_data = done_data;
406 cm->idle_id = g_idle_add(color_man_idle_cb, cm);
409 ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
410 unsigned char *input_data, guint input_data_len,
411 ColorManProfileType screen_type, const gchar *screen_file)
413 return color_man_new_real(imd, pixbuf,
414 COLOR_PROFILE_MEM, NULL, input_data, input_data_len,
415 screen_type, screen_file);
418 void color_man_free(ColorMan *cm)
422 if (cm->idle_id != -1) g_source_remove(cm->idle_id);
423 if (cm->pixbuf) g_object_unref(cm->pixbuf);
425 color_man_cache_unref(cm->profile);
430 void color_man_update(void)
432 color_man_cache_reset();
436 /*** color support not enabled ***/
439 ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
440 ColorManProfileType input_type, const gchar *input_file,
441 ColorManProfileType screen_type, const gchar *screen_file)
447 ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
448 unsigned char *input_data, guint input_data_len,
449 ColorManProfileType screen_type, const gchar *screen_file)
455 void color_man_free(ColorMan *cm)
460 void color_man_update(void)
465 void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h)
470 void color_man_start_bg(ColorMan *cm, ColorManDoneFunc done_func, gpointer done_data)