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)
98 cmsHPROFILE profile = NULL;
102 case COLOR_PROFILE_FILE:
107 pathl = path_from_utf8(file);
108 profile = cmsOpenProfileFromFile(pathl, "r");
112 case COLOR_PROFILE_SRGB:
113 profile = cmsCreate_sRGBProfile();
115 case COLOR_PROFILE_NONE:
123 static ColorManCache *color_man_cache_new(ColorManProfileType in_type, const gchar *in_file,
124 ColorManProfileType out_type, const gchar *out_file,
129 color_man_lib_init();
131 cc = g_new0(ColorManCache, 1);
134 cc->profile_in_type = in_type;
135 cc->profile_in_file = g_strdup(in_file);
137 cc->profile_out_type = out_type;
138 cc->profile_out_file = g_strdup(out_file);
140 cc->has_alpha = has_alpha;
142 cc->profile_in = color_man_cache_load_profile(cc->profile_in_type, cc->profile_in_file);
143 cc->profile_out = color_man_cache_load_profile(cc->profile_out_type, cc->profile_out_file);
145 if (!cc->profile_in || !cc->profile_out)
147 if (debug) printf("failed to load color profile for %s: %d %s\n",
148 (!cc->profile_in) ? "input" : "screen",
149 (!cc->profile_in) ? cc->profile_in_type : cc->profile_out_type,
150 (!cc->profile_in) ? cc->profile_in_file : cc->profile_out_file);
152 color_man_cache_unref(cc);
156 cc->transform = cmsCreateTransform(cc->profile_in,
157 (has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
159 (has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
160 INTENT_PERCEPTUAL, 0);
164 if (debug) printf("failed to create color profile transform\n");
166 color_man_cache_unref(cc);
170 cm_cache_list = g_list_append(cm_cache_list, cc);
175 static void color_man_cache_free(ColorManCache *cc)
179 cm_cache_list = g_list_remove(cm_cache_list, cc);
180 color_man_cache_unref(cc);
183 static void color_man_cache_reset(void)
185 while (cm_cache_list)
189 cc = cm_cache_list->data;
190 color_man_cache_free(cc);
194 static ColorManCache *color_man_cache_find(ColorManProfileType in_type, const gchar *in_file,
195 ColorManProfileType out_type, const gchar *out_file,
200 work = cm_cache_list;
209 if (cc->profile_in_type == in_type &&
210 cc->profile_out_type == out_type &&
211 cc->has_alpha == has_alpha)
216 if (match && cc->profile_in_type == COLOR_PROFILE_FILE)
218 match = (cc->profile_in_file && in_file &&
219 strcmp(cc->profile_in_file, in_file) == 0);
221 if (match && cc->profile_out_type == COLOR_PROFILE_FILE)
223 match = (cc->profile_out_file && out_file &&
224 strcmp(cc->profile_out_file, out_file) == 0);
227 if (match) return cc;
233 static ColorManCache *color_man_cache_get(ColorManProfileType in_type, const gchar *in_file,
234 ColorManProfileType out_type, const gchar *out_file,
239 cc = color_man_cache_find(in_type, in_file, out_type, out_file, has_alpha);
243 cc = color_man_cache_new(in_type, in_file, out_type, out_file, has_alpha);
251 *-------------------------------------------------------------------
253 *-------------------------------------------------------------------
256 static void color_man_done(ColorMan *cm, ColorManReturnType type)
260 cm->func_done(cm, type, cm->func_done_data);
264 static void color_man_correct_region(ColorMan *cm, gint x, gint y, gint w, gint h,
265 gint pixbuf_width, gint pixbuf_height)
274 pix = gdk_pixbuf_get_pixels(cm->pixbuf);
275 rs = gdk_pixbuf_get_rowstride(cm->pixbuf);
277 w = MIN(w, pixbuf_width - x);
278 h = MIN(h, pixbuf_height - y);
280 pix += x * ((cc->has_alpha) ? 4 : 3);
281 for (i = 0; i < h; i++)
285 pbuf = pix + ((y + i) * rs);
286 cmsDoTransform(cc->transform, pbuf, pbuf, w);
289 image_area_changed(cm->imd, x, y, w, h);
292 static gint color_man_idle_cb(gpointer data)
298 if (cm->pixbuf != image_get_pixbuf(cm->imd))
301 color_man_done(cm, COLOR_RETURN_IMAGE_CHANGED);
305 width = gdk_pixbuf_get_width(cm->pixbuf);
306 height = gdk_pixbuf_get_height(cm->pixbuf);
308 if (cm->row > height)
311 color_man_done(cm, COLOR_RETURN_SUCCESS);
315 rh = COLOR_MAN_CHUNK_SIZE / width + 1;
316 color_man_correct_region(cm, 0, cm->row, width, rh, width, height);
322 ColorMan *color_man_new(ImageWindow *imd,
323 ColorManProfileType input_type, const gchar *input_file,
324 ColorManProfileType screen_type, const gchar *screen_file,
325 ColorManDoneFunc done_func, gpointer done_data)
331 if (!imd) return NULL;
332 if (input_type == COLOR_PROFILE_NONE || screen_type == COLOR_PROFILE_NONE) return NULL;
334 pixbuf = image_get_pixbuf(imd);
335 if (!pixbuf) return NULL;
337 cm = g_new0(ColorMan, 1);
343 cm->func_done = done_func;
344 cm->func_done_data = done_data;
346 has_alpha = gdk_pixbuf_get_has_alpha(pixbuf);
347 cm->profile = color_man_cache_get(input_type, input_file, screen_type, screen_file, has_alpha);
354 color_man_cache_ref(cm->profile);
356 cm->idle_id = g_idle_add(color_man_idle_cb, cm);
361 void color_man_free(ColorMan *cm)
365 if (cm->idle_id != -1) g_source_remove(cm->idle_id);
367 color_man_cache_unref(cm->profile);
372 void color_man_update(void)
374 color_man_cache_reset();
378 /*** color support not enabled ***/
381 ColorMan *color_man_new(ImageWindow *imd,
382 ColorManProfileType input_type, const gchar *input_file,
383 ColorManProfileType screen_type, const gchar *screen_file,
384 ColorManDoneFunc don_func, gpointer done_data)
390 void color_man_free(ColorMan *cm)
395 void color_man_update(void)