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"
18 #include "ui_fileops.h"
22 /*** color support enabled ***/
24 #ifdef HAVE_LCMS_LCMS_H
25 #include <lcms/lcms.h>
31 typedef struct _ColorManCache ColorManCache;
32 struct _ColorManCache {
33 cmsHPROFILE profile_in;
34 cmsHPROFILE profile_out;
35 cmsHTRANSFORM transform;
37 ColorManProfileType profile_in_type;
38 gchar *profile_in_file;
40 ColorManProfileType profile_out_type;
41 gchar *profile_out_file;
48 /* pixels to transform per idle call */
49 #define COLOR_MAN_CHUNK_SIZE 81900
52 static void color_man_lib_init(void)
54 static gint init_done = FALSE;
56 if (init_done) return;
59 cmsErrorAction(LCMS_ERROR_IGNORE);
62 static cmsHPROFILE color_man_create_adobe_comp(void)
64 /* ClayRGB1998 is AdobeRGB compatible */
65 #include "ClayRGB1998_icc.h"
66 return cmsOpenProfileFromMem(ClayRGB1998_icc, ClayRGB1998_icc_len);
70 *-------------------------------------------------------------------
71 * color transform cache
72 *-------------------------------------------------------------------
75 static GList *cm_cache_list = NULL;
78 static void color_man_cache_ref(ColorManCache *cc)
85 static void color_man_cache_unref(ColorManCache *cc)
92 if (cc->transform) cmsDeleteTransform(cc->transform);
93 if (cc->profile_in) cmsCloseProfile(cc->profile_in);
94 if (cc->profile_out) cmsCloseProfile(cc->profile_out);
96 g_free(cc->profile_in_file);
97 g_free(cc->profile_out_file);
103 static cmsHPROFILE color_man_cache_load_profile(ColorManProfileType type, const gchar *file,
104 unsigned char *data, guint data_len)
106 cmsHPROFILE profile = NULL;
110 case COLOR_PROFILE_FILE:
115 pathl = path_from_utf8(file);
116 profile = cmsOpenProfileFromFile(pathl, "r");
120 case COLOR_PROFILE_SRGB:
121 profile = cmsCreate_sRGBProfile();
123 case COLOR_PROFILE_ADOBERGB:
124 profile = color_man_create_adobe_comp();
126 case COLOR_PROFILE_MEM:
129 profile = cmsOpenProfileFromMem(data, data_len);
132 case COLOR_PROFILE_NONE:
140 static ColorManCache *color_man_cache_new(ColorManProfileType in_type, const gchar *in_file,
141 unsigned char *in_data, guint in_data_len,
142 ColorManProfileType out_type, const gchar *out_file,
147 color_man_lib_init();
149 cc = g_new0(ColorManCache, 1);
152 cc->profile_in_type = in_type;
153 cc->profile_in_file = g_strdup(in_file);
155 cc->profile_out_type = out_type;
156 cc->profile_out_file = g_strdup(out_file);
158 cc->has_alpha = has_alpha;
160 cc->profile_in = color_man_cache_load_profile(cc->profile_in_type, cc->profile_in_file,
161 in_data, in_data_len);
162 cc->profile_out = color_man_cache_load_profile(cc->profile_out_type, cc->profile_out_file,
165 if (!cc->profile_in || !cc->profile_out)
167 DEBUG_1("failed to load color profile for %s: %d %s",
168 (!cc->profile_in) ? "input" : "screen",
169 (!cc->profile_in) ? cc->profile_in_type : cc->profile_out_type,
170 (!cc->profile_in) ? cc->profile_in_file : cc->profile_out_file);
172 color_man_cache_unref(cc);
176 cc->transform = cmsCreateTransform(cc->profile_in,
177 (has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
179 (has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
180 INTENT_PERCEPTUAL, 0);
184 DEBUG_1("failed to create color profile transform");
186 color_man_cache_unref(cc);
190 if (cc->profile_in_type != COLOR_PROFILE_MEM)
192 cm_cache_list = g_list_append(cm_cache_list, cc);
193 color_man_cache_ref(cc);
199 static void color_man_cache_free(ColorManCache *cc)
203 cm_cache_list = g_list_remove(cm_cache_list, cc);
204 color_man_cache_unref(cc);
207 static void color_man_cache_reset(void)
209 while (cm_cache_list)
213 cc = cm_cache_list->data;
214 color_man_cache_free(cc);
218 static ColorManCache *color_man_cache_find(ColorManProfileType in_type, const gchar *in_file,
219 ColorManProfileType out_type, const gchar *out_file,
224 work = cm_cache_list;
233 if (cc->profile_in_type == in_type &&
234 cc->profile_out_type == out_type &&
235 cc->has_alpha == has_alpha)
240 if (match && cc->profile_in_type == COLOR_PROFILE_FILE)
242 match = (cc->profile_in_file && in_file &&
243 strcmp(cc->profile_in_file, in_file) == 0);
245 if (match && cc->profile_out_type == COLOR_PROFILE_FILE)
247 match = (cc->profile_out_file && out_file &&
248 strcmp(cc->profile_out_file, out_file) == 0);
251 if (match) return cc;
257 static ColorManCache *color_man_cache_get(ColorManProfileType in_type, const gchar *in_file,
258 unsigned char *in_data, guint in_data_len,
259 ColorManProfileType out_type, const gchar *out_file,
264 cc = color_man_cache_find(in_type, in_file, out_type, out_file, has_alpha);
267 color_man_cache_ref(cc);
271 return color_man_cache_new(in_type, in_file, in_data, in_data_len,
272 out_type, out_file, has_alpha);
277 *-------------------------------------------------------------------
279 *-------------------------------------------------------------------
282 static void color_man_done(ColorMan *cm, ColorManReturnType type)
286 cm->func_done(cm, type, cm->func_done_data);
290 void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h)
296 gint pixbuf_width, pixbuf_height;
299 pixbuf_width = gdk_pixbuf_get_width(pixbuf);
300 pixbuf_height = gdk_pixbuf_get_height(pixbuf);
304 pix = gdk_pixbuf_get_pixels(pixbuf);
305 rs = gdk_pixbuf_get_rowstride(pixbuf);
307 w = MIN(w, pixbuf_width - x);
308 h = MIN(h, pixbuf_height - y);
310 pix += x * ((cc->has_alpha) ? 4 : 3);
311 for (i = 0; i < h; i++)
315 pbuf = pix + ((y + i) * rs);
317 cmsDoTransform(cc->transform, pbuf, pbuf, w);
322 static gint color_man_idle_cb(gpointer data)
327 if (!cm->pixbuf) return FALSE;
330 cm->pixbuf != image_get_pixbuf(cm->imd))
333 color_man_done(cm, COLOR_RETURN_IMAGE_CHANGED);
337 width = gdk_pixbuf_get_width(cm->pixbuf);
338 height = gdk_pixbuf_get_height(cm->pixbuf);
340 if (cm->row > height)
342 if (!cm->incremental_sync && cm->imd)
344 image_area_changed(cm->imd, 0, 0, width, height);
348 color_man_done(cm, COLOR_RETURN_SUCCESS);
352 rh = COLOR_MAN_CHUNK_SIZE / width + 1;
353 color_man_correct_region(cm, cm->pixbuf, 0, cm->row, width, rh);
354 if (cm->incremental_sync && cm->imd) image_area_changed(cm->imd, 0, cm->row, width, rh);
360 static ColorMan *color_man_new_real(ImageWindow *imd, GdkPixbuf *pixbuf,
361 ColorManProfileType input_type, const gchar *input_file,
362 unsigned char *input_data, guint input_data_len,
363 ColorManProfileType screen_type, const gchar *screen_file)
368 if (imd) pixbuf = image_get_pixbuf(imd);
370 cm = g_new0(ColorMan, 1);
373 if (cm->pixbuf) g_object_ref(cm->pixbuf);
375 cm->incremental_sync = FALSE;
379 has_alpha = pixbuf ? gdk_pixbuf_get_has_alpha(pixbuf) : FALSE;
381 cm->profile = color_man_cache_get(input_type, input_file, input_data, input_data_len,
382 screen_type, screen_file, has_alpha);
392 ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
393 ColorManProfileType input_type, const gchar *input_file,
394 ColorManProfileType screen_type, const gchar *screen_file)
396 return color_man_new_real(imd, pixbuf,
397 input_type, input_file, NULL, 0,
398 screen_type, screen_file);
401 void color_man_start_bg(ColorMan *cm, ColorManDoneFunc done_func, gpointer done_data)
403 cm->func_done = done_func;
404 cm->func_done_data = done_data;
405 cm->idle_id = g_idle_add(color_man_idle_cb, cm);
408 ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
409 unsigned char *input_data, guint input_data_len,
410 ColorManProfileType screen_type, const gchar *screen_file)
412 return color_man_new_real(imd, pixbuf,
413 COLOR_PROFILE_MEM, NULL, input_data, input_data_len,
414 screen_type, screen_file);
417 void color_man_free(ColorMan *cm)
421 if (cm->idle_id != -1) g_source_remove(cm->idle_id);
422 if (cm->pixbuf) g_object_unref(cm->pixbuf);
424 color_man_cache_unref(cm->profile);
429 void color_man_update(void)
431 color_man_cache_reset();
435 /*** color support not enabled ***/
438 ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
439 ColorManProfileType input_type, const gchar *input_file,
440 ColorManProfileType screen_type, const gchar *screen_file)
446 ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
447 unsigned char *input_data, guint input_data_len,
448 ColorManProfileType screen_type, const gchar *screen_file)
454 void color_man_free(ColorMan *cm)
459 void color_man_update(void)
464 void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h)
469 void color_man_start_bg(ColorMan *cm, ColorManDoneFunc done_func, gpointer done_data)