-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathcolor-man.cc
490 lines (390 loc) · 12 KB
/
color-man.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
* Copyright (C) 2006 John Ellis
* Copyright (C) 2008 - 2016 The Geeqie Team
*
* Author: John Ellis
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "color-man.h"
#include <config.h>
#if HAVE_LCMS
/*** color support enabled ***/
#include <algorithm>
#include <cstring>
#include <glib-object.h>
#include <lcms2.h>
#include "image.h"
#include "intl.h"
#include "layout.h"
#include "options.h"
#include "ui-fileops.h"
namespace
{
#define GQ_RESOURCE_PATH_ICC "/org/geeqie/icc"
struct ColorManCache {
cmsHPROFILE profile_in;
cmsHPROFILE profile_out;
cmsHTRANSFORM transform;
ColorManProfileType profile_in_type;
gchar *profile_in_file;
ColorManProfileType profile_out_type;
gchar *profile_out_file;
gboolean has_alpha;
gint refcount;
};
cmsHPROFILE color_man_create_adobe_comp()
{
/* ClayRGB1998 is AdobeRGB compatible */
g_autoptr(GBytes) ClayRGB1998_icc_bytes = g_resources_lookup_data(GQ_RESOURCE_PATH_ICC "/ClayRGB1998.icc",
G_RESOURCE_LOOKUP_FLAGS_NONE, nullptr);
gsize ClayRGB1998_icc_len;
gconstpointer ClayRGB1998_icc = g_bytes_get_data(ClayRGB1998_icc_bytes, &ClayRGB1998_icc_len);
return cmsOpenProfileFromMem(ClayRGB1998_icc, ClayRGB1998_icc_len);
}
/**
* @brief Retrieves the internal scale factor that maps from window coordinates to the actual device pixels
* @param -
* @returns scale factor
*/
gint scale_factor()
{
LayoutWindow *lw = get_current_layout();
return gtk_widget_get_scale_factor(lw->window);
}
/*
*-------------------------------------------------------------------
* color transform cache
*-------------------------------------------------------------------
*/
GList *cm_cache_list = nullptr;
void color_man_cache_ref(ColorManCache *cc)
{
if (!cc) return;
cc->refcount++;
}
void color_man_cache_unref(ColorManCache *cc)
{
if (!cc) return;
cc->refcount--;
if (cc->refcount < 1)
{
if (cc->transform) cmsDeleteTransform(cc->transform);
if (cc->profile_in) cmsCloseProfile(cc->profile_in);
if (cc->profile_out) cmsCloseProfile(cc->profile_out);
g_free(cc->profile_in_file);
g_free(cc->profile_out_file);
g_free(cc);
}
}
cmsHPROFILE color_man_cache_load_profile(ColorManProfileType type, const gchar *file,
guchar *data, guint data_len)
{
cmsHPROFILE profile = nullptr;
switch (type)
{
case COLOR_PROFILE_FILE:
if (file)
{
g_autofree gchar *pathl = path_from_utf8(file);
profile = cmsOpenProfileFromFile(pathl, "r");
}
break;
case COLOR_PROFILE_SRGB:
profile = cmsCreate_sRGBProfile();
break;
case COLOR_PROFILE_ADOBERGB:
profile = color_man_create_adobe_comp();
break;
case COLOR_PROFILE_MEM:
if (data)
{
profile = cmsOpenProfileFromMem(data, data_len);
}
break;
case COLOR_PROFILE_NONE:
default:
break;
}
return profile;
}
ColorManCache *color_man_cache_new(ColorManProfileType in_type, const gchar *in_file,
guchar *in_data, guint in_data_len,
ColorManProfileType out_type, const gchar *out_file,
guchar *out_data, guint out_data_len,
gboolean has_alpha)
{
ColorManCache *cc;
cc = g_new0(ColorManCache, 1);
cc->refcount = 1;
cc->profile_in_type = in_type;
cc->profile_in_file = g_strdup(in_file);
cc->profile_out_type = out_type;
cc->profile_out_file = g_strdup(out_file);
cc->has_alpha = has_alpha;
cc->profile_in = color_man_cache_load_profile(cc->profile_in_type, cc->profile_in_file,
in_data, in_data_len);
cc->profile_out = color_man_cache_load_profile(cc->profile_out_type, cc->profile_out_file,
out_data, out_data_len);
if (!cc->profile_in || !cc->profile_out)
{
DEBUG_1("failed to load color profile for %s: %d %s",
(!cc->profile_in) ? "input" : "screen",
(!cc->profile_in) ? cc->profile_in_type : cc->profile_out_type,
(!cc->profile_in) ? cc->profile_in_file : cc->profile_out_file);
color_man_cache_unref(cc);
return nullptr;
}
cc->transform = cmsCreateTransform(cc->profile_in,
(has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
cc->profile_out,
(has_alpha) ? TYPE_RGBA_8 : TYPE_RGB_8,
options->color_profile.render_intent, 0);
if (!cc->transform)
{
DEBUG_1("failed to create color profile transform");
color_man_cache_unref(cc);
return nullptr;
}
if (cc->profile_in_type != COLOR_PROFILE_MEM && cc->profile_out_type != COLOR_PROFILE_MEM )
{
cm_cache_list = g_list_append(cm_cache_list, cc);
color_man_cache_ref(cc);
}
return cc;
}
void color_man_cache_reset()
{
g_list_free_full(cm_cache_list, reinterpret_cast<GDestroyNotify>(color_man_cache_unref));
}
ColorManCache *color_man_cache_find(ColorManProfileType in_type, const gchar *in_file,
ColorManProfileType out_type, const gchar *out_file,
gboolean has_alpha)
{
GList *work;
work = cm_cache_list;
while (work)
{
ColorManCache *cc;
gboolean match = FALSE;
cc = static_cast<ColorManCache *>(work->data);
work = work->next;
if (cc->profile_in_type == in_type &&
cc->profile_out_type == out_type &&
cc->has_alpha == has_alpha)
{
match = TRUE;
}
if (match && cc->profile_in_type == COLOR_PROFILE_FILE)
{
match = (cc->profile_in_file && in_file &&
strcmp(cc->profile_in_file, in_file) == 0);
}
if (match && cc->profile_out_type == COLOR_PROFILE_FILE)
{
match = (cc->profile_out_file && out_file &&
strcmp(cc->profile_out_file, out_file) == 0);
}
if (match) return cc;
}
return nullptr;
}
ColorManCache *color_man_cache_get(ColorManProfileType in_type, const gchar *in_file,
guchar *in_data, guint in_data_len,
ColorManProfileType out_type, const gchar *out_file,
guchar *out_data, guint out_data_len,
gboolean has_alpha)
{
ColorManCache *cc;
cc = color_man_cache_find(in_type, in_file, out_type, out_file, has_alpha);
if (cc)
{
color_man_cache_ref(cc);
return cc;
}
return color_man_cache_new(in_type, in_file, in_data, in_data_len,
out_type, out_file, out_data, out_data_len, has_alpha);
}
} // namespace
/*
*-------------------------------------------------------------------
* color manager
*-------------------------------------------------------------------
*/
void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h)
{
ColorManCache *cc;
guchar *pix;
gint rs;
gint i;
gint pixbuf_width;
gint pixbuf_height;
pixbuf_width = gdk_pixbuf_get_width(pixbuf);
pixbuf_height = gdk_pixbuf_get_height(pixbuf);
cc = static_cast<ColorManCache *>(cm->profile);
pix = gdk_pixbuf_get_pixels(pixbuf);
rs = gdk_pixbuf_get_rowstride(pixbuf);
/** @FIXME: x,y expected to be = 0. Maybe this is not the right place for scaling */
w = w * scale_factor();
h = h * scale_factor();
w = std::min(w, pixbuf_width - x);
h = std::min(h, pixbuf_height - y);
pix += x * ((cc->has_alpha) ? 4 : 3);
for (i = 0; i < h; i++)
{
guchar *pbuf;
pbuf = pix + ((y + i) * rs);
cmsDoTransform(cc->transform, pbuf, pbuf, w);
}
}
static ColorMan *color_man_new_real(ImageWindow *imd, GdkPixbuf *pixbuf,
ColorManProfileType input_type, const gchar *input_file,
guchar *input_data, guint input_data_len,
ColorManProfileType screen_type, const gchar *screen_file,
guchar *screen_data, guint screen_data_len)
{
ColorMan *cm;
gboolean has_alpha;
if (imd) pixbuf = image_get_pixbuf(imd);
cm = g_new0(ColorMan, 1);
cm->imd = imd;
cm->pixbuf = pixbuf;
if (cm->pixbuf) g_object_ref(cm->pixbuf);
has_alpha = pixbuf ? gdk_pixbuf_get_has_alpha(pixbuf) : FALSE;
cm->profile = color_man_cache_get(input_type, input_file, input_data, input_data_len,
screen_type, screen_file, screen_data, screen_data_len, has_alpha);
if (!cm->profile)
{
color_man_free(cm);
return nullptr;
}
return cm;
}
ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
ColorManProfileType input_type, const gchar *input_file,
ColorManProfileType screen_type, const gchar *screen_file,
guchar *screen_data, guint screen_data_len)
{
return color_man_new_real(imd, pixbuf,
input_type, input_file, nullptr, 0,
screen_type, screen_file, screen_data, screen_data_len);
}
ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
guchar *input_data, guint input_data_len,
ColorManProfileType screen_type, const gchar *screen_file,
guchar *screen_data, guint screen_data_len)
{
return color_man_new_real(imd, pixbuf,
COLOR_PROFILE_MEM, nullptr, input_data, input_data_len,
screen_type, screen_file, screen_data, screen_data_len);
}
static gchar *color_man_get_profile_name(ColorManProfileType type, cmsHPROFILE profile)
{
switch (type)
{
case COLOR_PROFILE_SRGB:
return g_strdup(_("sRGB"));
case COLOR_PROFILE_ADOBERGB:
return g_strdup(_("Adobe RGB compatible"));
break;
case COLOR_PROFILE_MEM:
case COLOR_PROFILE_FILE:
if (profile)
{
char buffer[20];
buffer[0] = '\0';
cmsGetProfileInfoASCII(profile, cmsInfoDescription, "en", "US", buffer, 20);
buffer[19] = '\0'; /* Just to be sure */
return g_strdup(buffer);
}
return g_strdup(_("Custom profile"));
break;
case COLOR_PROFILE_NONE:
default:
return g_strdup("");
}
}
gboolean color_man_get_status(ColorMan *cm, gchar **image_profile, gchar **screen_profile)
{
ColorManCache *cc;
if (!cm) return FALSE;
cc = static_cast<ColorManCache *>(cm->profile);
if (image_profile) *image_profile = color_man_get_profile_name(cc->profile_in_type, cc->profile_in);
if (screen_profile) *screen_profile = color_man_get_profile_name(cc->profile_out_type, cc->profile_out);
return TRUE;
}
void color_man_free(ColorMan *cm)
{
if (!cm) return;
if (cm->idle_id) g_source_remove(cm->idle_id);
if (cm->pixbuf) g_object_unref(cm->pixbuf);
color_man_cache_unref(static_cast<ColorManCache *>(cm->profile));
g_free(cm);
}
void color_man_update()
{
color_man_cache_reset();
}
const gchar *get_profile_name(const guchar *profile_data, guint profile_len)
{
cmsHPROFILE profile = cmsOpenProfileFromMem(profile_data, profile_len);
if (!profile) return nullptr;
static cmsUInt8Number profileID[17];
memset(profileID, 0, sizeof(profileID));
cmsGetHeaderProfileID(profile, profileID);
cmsCloseProfile(profile);
return reinterpret_cast<gchar *>(profileID);
}
#else /* define HAVE_LCMS */
/*** color support not enabled ***/
ColorMan *color_man_new(ImageWindow *, GdkPixbuf *,
ColorManProfileType, const gchar *,
ColorManProfileType, const gchar *,
guchar *, guint)
{
/* no op */
return nullptr;
}
ColorMan *color_man_new_embedded(ImageWindow *, GdkPixbuf *,
guchar *, guint,
ColorManProfileType, const gchar *,
guchar *, guint)
{
/* no op */
return nullptr;
}
void color_man_free(ColorMan *)
{
/* no op */
}
void color_man_update()
{
/* no op */
}
void color_man_correct_region(ColorMan *, GdkPixbuf *, gint, gint, gint, gint)
{
/* no op */
}
gboolean color_man_get_status(ColorMan *, gchar **, gchar **)
{
return FALSE;
}
const gchar *get_profile_name(const guchar *, guint)
{
/* no op */
return nullptr;
}
#endif /* define HAVE_LCMS */
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */