7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
14 #include "color-man.h"
17 #include "ui_fileops.h"
21 /*** color support enabled ***/
23 #ifdef HAVE_LCMS_LCMS_H
24 #include <lcms/lcms.h>
30 typedef struct _ColorManCache ColorManCache;
31 struct _ColorManCache {
32 cmsHPROFILE profile_in;
33 cmsHPROFILE profile_out;
34 cmsHTRANSFORM transform;
36 ColorManProfileType profile_in_type;
37 gchar *profile_in_file;
39 ColorManProfileType profile_out_type;
40 gchar *profile_out_file;
47 /* pixels to transform per idle call */
48 #define COLOR_MAN_CHUNK_SIZE 81900
51 static void color_man_lib_init(void)
53 static gint init_done = FALSE;
55 if (init_done) return;
58 cmsErrorAction(LCMS_ERROR_IGNORE);
61 static cmsHPROFILE color_man_create_adobe_comp()
63 /* ClayRGB1998 is AdobeRGB compatible */
64 #include "ClayRGB1998_icc.h"
65 return cmsOpenProfileFromMem(ClayRGB1998_icc, ClayRGB1998_icc_len);
69 *-------------------------------------------------------------------
70 * color transform cache
71 *-------------------------------------------------------------------
74 static GList *cm_cache_list = NULL;
77 static void color_man_cache_ref(ColorManCache *cc)
84 static void color_man_cache_unref(ColorManCache *cc)
91 if (cc->transform) cmsDeleteTransform(cc->transform);
92 if (cc->profile_in) cmsCloseProfile(cc->profile_in);
93 if (cc->profile_out) cmsCloseProfile(cc->profile_out);
95 g_free(cc->profile_in_file);
96 g_free(cc->profile_out_file);
102 static cmsHPROFILE color_man_cache_load_profile(ColorManProfileType type, const gchar *file,
103 unsigned char *data, guint data_len)
105 cmsHPROFILE profile = NULL;
109 case COLOR_PROFILE_FILE:
114 pathl = path_from_utf8(file);
115 profile = cmsOpenProfileFromFile(pathl, "r");
119 case COLOR_PROFILE_SRGB:
120 profile = cmsCreate_sRGBProfile();
122 case COLOR_PROFILE_ADOBERGB:
123 profile = color_man_create_adobe_comp();
125 case COLOR_PROFILE_MEM:
128 profile = cmsOpenProfileFromMem(data, data_len);
131 case COLOR_PROFILE_NONE:
139 static ColorManCache *color_man_cache_new(ColorManProfileType in_type, const gchar *in_file,
140 unsigned char *in_data, guint in_data_len,
141 ColorManProfileType out_type, const gchar *out_file,
146 color_man_lib_init();
148 cc = g_new0(ColorManCache, 1);
151 cc->profile_in_type = in_type;
152 cc->profile_in_file = g_strdup(in_file);
154 cc->profile_out_type = out_type;
155 cc->profile_out_file = g_strdup(out_file);
157 cc->has_alpha = has_alpha;
159 cc->profile_in = color_man_cache_load_profile(cc->profile_in_type, cc->profile_in_file,
160 in_data, in_data_len);
161 cc->profile_out = color_man_cache_load_profile(cc->profile_out_type, cc->profile_out_file,
164 if (!cc->profile_in || !cc->profile_out)
166 if (debug) printf("failed to load color profile for %s: %d %s\n",
167 (!cc->profile_in) ? "input" : "screen",
168 (!cc->profile_in) ? cc->profile_in_type : cc->profile_out_type,
169 (!cc->profile_in) ? cc->profile_in_file : cc->profile_out_file);
171 color_man_cache_unref(cc);
175 cc->transform = cmsCreateTransform(cc->profile_in,
176 (has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
178 (has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
179 INTENT_PERCEPTUAL, 0);
183 if (debug) printf("failed to create color profile transform\n");
185 color_man_cache_unref(cc);
189 if (cc->profile_in_type != COLOR_PROFILE_MEM)
191 cm_cache_list = g_list_append(cm_cache_list, cc);
192 color_man_cache_ref(cc);
198 static void color_man_cache_free(ColorManCache *cc)
202 cm_cache_list = g_list_remove(cm_cache_list, cc);
203 color_man_cache_unref(cc);
206 static void color_man_cache_reset(void)
208 while (cm_cache_list)
212 cc = cm_cache_list->data;
213 color_man_cache_free(cc);
217 static ColorManCache *color_man_cache_find(ColorManProfileType in_type, const gchar *in_file,
218 ColorManProfileType out_type, const gchar *out_file,
223 work = cm_cache_list;
232 if (cc->profile_in_type == in_type &&
233 cc->profile_out_type == out_type &&
234 cc->has_alpha == has_alpha)
239 if (match && cc->profile_in_type == COLOR_PROFILE_FILE)
241 match = (cc->profile_in_file && in_file &&
242 strcmp(cc->profile_in_file, in_file) == 0);
244 if (match && cc->profile_out_type == COLOR_PROFILE_FILE)
246 match = (cc->profile_out_file && out_file &&
247 strcmp(cc->profile_out_file, out_file) == 0);
250 if (match) return cc;
256 static ColorManCache *color_man_cache_get(ColorManProfileType in_type, const gchar *in_file,
257 unsigned char *in_data, guint in_data_len,
258 ColorManProfileType out_type, const gchar *out_file,
263 cc = color_man_cache_find(in_type, in_file, out_type, out_file, has_alpha);
266 color_man_cache_ref(cc);
270 return color_man_cache_new(in_type, in_file, in_data, in_data_len,
271 out_type, out_file, has_alpha);
276 *-------------------------------------------------------------------
278 *-------------------------------------------------------------------
281 static void color_man_done(ColorMan *cm, ColorManReturnType type)
285 cm->func_done(cm, type, cm->func_done_data);
289 void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h)
295 gint pixbuf_width, pixbuf_height;
298 pixbuf_width = gdk_pixbuf_get_width(pixbuf);
299 pixbuf_height = gdk_pixbuf_get_height(pixbuf);
303 pix = gdk_pixbuf_get_pixels(pixbuf);
304 rs = gdk_pixbuf_get_rowstride(pixbuf);
306 w = MIN(w, pixbuf_width - x);
307 h = MIN(h, pixbuf_height - y);
309 pix += x * ((cc->has_alpha) ? 4 : 3);
310 for (i = 0; i < h; i++)
314 pbuf = pix + ((y + i) * rs);
316 cmsDoTransform(cc->transform, pbuf, pbuf, w);
321 static gint 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 unsigned char *input_data, guint input_data_len,
362 ColorManProfileType screen_type, const gchar *screen_file)
367 if (imd) pixbuf = image_get_pixbuf(imd);
369 cm = g_new0(ColorMan, 1);
372 if (cm->pixbuf) g_object_ref(cm->pixbuf);
374 cm->incremental_sync = FALSE;
378 has_alpha = pixbuf ? gdk_pixbuf_get_has_alpha(pixbuf) : FALSE;
380 cm->profile = color_man_cache_get(input_type, input_file, input_data, input_data_len,
381 screen_type, screen_file, has_alpha);
391 ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
392 ColorManProfileType input_type, const gchar *input_file,
393 ColorManProfileType screen_type, const gchar *screen_file)
395 return color_man_new_real(imd, pixbuf,
396 input_type, input_file, NULL, 0,
397 screen_type, screen_file);
400 void color_man_start_bg(ColorMan *cm, ColorManDoneFunc done_func, gpointer done_data)
402 cm->func_done = done_func;
403 cm->func_done_data = done_data;
404 cm->idle_id = g_idle_add(color_man_idle_cb, cm);
407 ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
408 unsigned char *input_data, guint input_data_len,
409 ColorManProfileType screen_type, const gchar *screen_file)
411 return color_man_new_real(imd, pixbuf,
412 COLOR_PROFILE_MEM, NULL, input_data, input_data_len,
413 screen_type, screen_file);
416 void color_man_free(ColorMan *cm)
420 if (cm->idle_id != -1) g_source_remove(cm->idle_id);
421 if (cm->pixbuf) g_object_unref(cm->pixbuf);
423 color_man_cache_unref(cm->profile);
428 void color_man_update(void)
430 color_man_cache_reset();
434 /*** color support not enabled ***/
437 ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
438 ColorManProfileType input_type, const gchar *input_file,
439 ColorManProfileType screen_type, const gchar *screen_file)
445 ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
446 unsigned char *input_data, guint input_data_len,
447 ColorManProfileType screen_type, const gchar *screen_file)
453 void color_man_free(ColorMan *cm)
458 void color_man_update(void)
463 void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h)
468 void color_man_start_bg(ColorMan *cm, ColorManDoneFunc done_func, gpointer done_data)