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);
63 *-------------------------------------------------------------------
64 * color transform cache
65 *-------------------------------------------------------------------
68 static GList *cm_cache_list = NULL;
71 static void color_man_cache_ref(ColorManCache *cc)
78 static void color_man_cache_unref(ColorManCache *cc)
85 if (cc->transform) cmsDeleteTransform(cc->transform);
86 if (cc->profile_in) cmsCloseProfile(cc->profile_in);
87 if (cc->profile_out) cmsCloseProfile(cc->profile_out);
89 g_free(cc->profile_in_file);
90 g_free(cc->profile_out_file);
96 static cmsHPROFILE color_man_cache_load_profile(ColorManProfileType type, const gchar *file,
97 unsigned char *data, guint data_len)
99 cmsHPROFILE profile = NULL;
103 case COLOR_PROFILE_FILE:
108 pathl = path_from_utf8(file);
109 profile = cmsOpenProfileFromFile(pathl, "r");
113 case COLOR_PROFILE_SRGB:
114 profile = cmsCreate_sRGBProfile();
116 case COLOR_PROFILE_MEM:
119 profile = cmsOpenProfileFromMem(data, data_len);
122 case COLOR_PROFILE_NONE:
130 static ColorManCache *color_man_cache_new(ColorManProfileType in_type, const gchar *in_file,
131 unsigned char *in_data, guint in_data_len,
132 ColorManProfileType out_type, const gchar *out_file,
137 color_man_lib_init();
139 cc = g_new0(ColorManCache, 1);
142 cc->profile_in_type = in_type;
143 cc->profile_in_file = g_strdup(in_file);
145 cc->profile_out_type = out_type;
146 cc->profile_out_file = g_strdup(out_file);
148 cc->has_alpha = has_alpha;
150 cc->profile_in = color_man_cache_load_profile(cc->profile_in_type, cc->profile_in_file,
151 in_data, in_data_len);
152 cc->profile_out = color_man_cache_load_profile(cc->profile_out_type, cc->profile_out_file,
155 if (!cc->profile_in || !cc->profile_out)
157 if (debug) printf("failed to load color profile for %s: %d %s\n",
158 (!cc->profile_in) ? "input" : "screen",
159 (!cc->profile_in) ? cc->profile_in_type : cc->profile_out_type,
160 (!cc->profile_in) ? cc->profile_in_file : cc->profile_out_file);
162 color_man_cache_unref(cc);
166 cc->transform = cmsCreateTransform(cc->profile_in,
167 (has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
169 (has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
170 INTENT_PERCEPTUAL, 0);
174 if (debug) printf("failed to create color profile transform\n");
176 color_man_cache_unref(cc);
180 if (cc->profile_in_type != COLOR_PROFILE_MEM)
182 cm_cache_list = g_list_append(cm_cache_list, cc);
183 color_man_cache_ref(cc);
189 static void color_man_cache_free(ColorManCache *cc)
193 cm_cache_list = g_list_remove(cm_cache_list, cc);
194 color_man_cache_unref(cc);
197 static void color_man_cache_reset(void)
199 while (cm_cache_list)
203 cc = cm_cache_list->data;
204 color_man_cache_free(cc);
208 static ColorManCache *color_man_cache_find(ColorManProfileType in_type, const gchar *in_file,
209 ColorManProfileType out_type, const gchar *out_file,
214 work = cm_cache_list;
223 if (cc->profile_in_type == in_type &&
224 cc->profile_out_type == out_type &&
225 cc->has_alpha == has_alpha)
230 if (match && cc->profile_in_type == COLOR_PROFILE_FILE)
232 match = (cc->profile_in_file && in_file &&
233 strcmp(cc->profile_in_file, in_file) == 0);
235 if (match && cc->profile_out_type == COLOR_PROFILE_FILE)
237 match = (cc->profile_out_file && out_file &&
238 strcmp(cc->profile_out_file, out_file) == 0);
241 if (match) return cc;
247 static ColorManCache *color_man_cache_get(ColorManProfileType in_type, const gchar *in_file,
248 unsigned char *in_data, guint in_data_len,
249 ColorManProfileType out_type, const gchar *out_file,
254 cc = color_man_cache_find(in_type, in_file, out_type, out_file, has_alpha);
257 color_man_cache_ref(cc);
261 return color_man_cache_new(in_type, in_file, in_data, in_data_len,
262 out_type, out_file, has_alpha);
267 *-------------------------------------------------------------------
269 *-------------------------------------------------------------------
272 static void color_man_done(ColorMan *cm, ColorManReturnType type)
276 cm->func_done(cm, type, cm->func_done_data);
280 static void color_man_correct_region(ColorMan *cm, gint x, gint y, gint w, gint h,
281 gint pixbuf_width, gint pixbuf_height)
290 pix = gdk_pixbuf_get_pixels(cm->pixbuf);
291 rs = gdk_pixbuf_get_rowstride(cm->pixbuf);
293 w = MIN(w, pixbuf_width - x);
294 h = MIN(h, pixbuf_height - y);
296 pix += x * ((cc->has_alpha) ? 4 : 3);
297 for (i = 0; i < h; i++)
301 pbuf = pix + ((y + i) * rs);
302 cmsDoTransform(cc->transform, pbuf, pbuf, w);
305 if (cm->incremental_sync && cm->imd) image_area_changed(cm->imd, x, y, w, h);
308 static gint color_man_idle_cb(gpointer data)
315 cm->pixbuf != image_get_pixbuf(cm->imd))
318 color_man_done(cm, COLOR_RETURN_IMAGE_CHANGED);
322 width = gdk_pixbuf_get_width(cm->pixbuf);
323 height = gdk_pixbuf_get_height(cm->pixbuf);
325 if (cm->row > height)
327 if (!cm->incremental_sync && cm->imd)
329 image_area_changed(cm->imd, 0, 0, width, height);
333 color_man_done(cm, COLOR_RETURN_SUCCESS);
337 rh = COLOR_MAN_CHUNK_SIZE / width + 1;
338 color_man_correct_region(cm, 0, cm->row, width, rh, width, height);
344 static ColorMan *color_man_new_real(ImageWindow *imd, GdkPixbuf *pixbuf,
345 ColorManProfileType input_type, const gchar *input_file,
346 unsigned char *input_data, guint input_data_len,
347 ColorManProfileType screen_type, const gchar *screen_file,
348 ColorManDoneFunc done_func, gpointer done_data)
353 if (imd) pixbuf = image_get_pixbuf(imd);
354 if (!pixbuf) return NULL;
356 cm = g_new0(ColorMan, 1);
359 g_object_ref(cm->pixbuf);
361 cm->incremental_sync = FALSE;
365 cm->func_done = done_func;
366 cm->func_done_data = done_data;
368 has_alpha = gdk_pixbuf_get_has_alpha(pixbuf);
369 cm->profile = color_man_cache_get(input_type, input_file, input_data, input_data_len,
370 screen_type, screen_file, has_alpha);
377 cm->idle_id = g_idle_add(color_man_idle_cb, cm);
382 ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
383 ColorManProfileType input_type, const gchar *input_file,
384 ColorManProfileType screen_type, const gchar *screen_file,
385 ColorManDoneFunc done_func, gpointer done_data)
387 return color_man_new_real(imd, pixbuf,
388 input_type, input_file, NULL, 0,
389 screen_type, screen_file,
390 done_func, done_data);
393 ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
394 unsigned char *input_data, guint input_data_len,
395 ColorManProfileType screen_type, const gchar *screen_file,
396 ColorManDoneFunc done_func, gpointer done_data)
398 return color_man_new_real(imd, pixbuf,
399 COLOR_PROFILE_MEM, NULL, input_data, input_data_len,
400 screen_type, screen_file,
401 done_func, done_data);
404 void color_man_free(ColorMan *cm)
408 if (cm->idle_id != -1) g_source_remove(cm->idle_id);
409 if (cm->pixbuf) g_object_unref(cm->pixbuf);
411 color_man_cache_unref(cm->profile);
416 void color_man_update(void)
418 color_man_cache_reset();
422 /*** color support not enabled ***/
425 ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
426 ColorManProfileType input_type, const gchar *input_file,
427 ColorManProfileType screen_type, const gchar *screen_file,
428 ColorManDoneFunc don_func, gpointer done_data)
434 ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
435 unsigned char *input_data, guint input_data_len,
436 ColorManProfileType screen_type, const gchar *screen_file,
437 ColorManDoneFunc done_func, gpointer done_data)
443 void color_man_free(ColorMan *cm)
448 void color_man_update(void)