1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
4
* cdisplay_colorblind.c
5
* Copyright (C) 2002-2003 Michael Natterer <mitch@gimp.org>,
6
* Sven Neumann <sven@gimp.org>,
7
* Robert Dougherty <bob@vischeck.com> and
8
* Alex Wade <alex@vischeck.com>
10
* This code is an implementation of an algorithm described by Hans Brettel,
11
* Francoise Vienot and John Mollon in the Journal of the Optical Society of
12
* America V14(10), pg 2647. (See http://vischeck.com/ for more info.)
14
* This program is free software; you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation; either version 2 of the License, or
17
* (at your option) any later version.
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
24
* You should have received a copy of the GNU General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35
#include "libgimpconfig/gimpconfig.h"
36
#include "libgimpmath/gimpmath.h"
37
#include "libgimpmodule/gimpmodule.h"
38
#include "libgimpwidgets/gimpwidgets.h"
40
#include "libgimp/libgimp-intl.h"
45
COLORBLIND_DEFICIENCY_PROTANOPIA,
46
COLORBLIND_DEFICIENCY_DEUTERANOPIA,
47
COLORBLIND_DEFICIENCY_TRITANOPIA
48
} ColorblindDeficiency;
50
#define CDISPLAY_TYPE_COLORBLIND_DEFICIENCY (cdisplay_colorblind_deficiency_type)
51
static GType cdisplay_colorblind_deficiency_get_type (GTypeModule *module);
53
static const GEnumValue enum_values[] =
55
{ COLORBLIND_DEFICIENCY_PROTANOPIA,
56
"COLORBLIND_DEFICIENCY_PROTANOPIA", "protanopia" },
57
{ COLORBLIND_DEFICIENCY_DEUTERANOPIA,
58
"COLORBLIND_DEFICIENCY_DEUTERANOPIA", "deuteranopia" },
59
{ COLORBLIND_DEFICIENCY_TRITANOPIA,
60
"COLORBLIND_DEFICIENCY_TRITANOPIA", "tritanopia" },
64
static const GimpEnumDesc enum_descs[] =
66
{ COLORBLIND_DEFICIENCY_PROTANOPIA,
67
N_("Protanopia (insensitivity to red)"), NULL },
68
{ COLORBLIND_DEFICIENCY_DEUTERANOPIA,
69
N_("Deuteranopia (insensitivity to green)"), NULL },
70
{ COLORBLIND_DEFICIENCY_TRITANOPIA,
71
N_("Tritanopia (insensitivity to blue)"), NULL },
75
#define DEFAULT_DEFICIENCY COLORBLIND_DEFICIENCY_DEUTERANOPIA
76
#define COLOR_CACHE_SIZE 1021
79
#define CDISPLAY_TYPE_COLORBLIND (cdisplay_colorblind_type)
80
#define CDISPLAY_COLORBLIND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CDISPLAY_TYPE_COLORBLIND, CdisplayColorblind))
81
#define CDISPLAY_COLORBLIND_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CDISPLAY_TYPE_COLORBLIND, CdisplayColorblindClass))
82
#define CDISPLAY_IS_COLORBLIND(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CDISPLAY_TYPE_COLORBLIND))
83
#define CDISPLAY_IS_COLORBLIND_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CDISPLAY_TYPE_COLORBLIND))
86
typedef struct _CdisplayColorblind CdisplayColorblind;
87
typedef struct _CdisplayColorblindClass CdisplayColorblindClass;
89
struct _CdisplayColorblind
91
GimpColorDisplay parent_instance;
93
ColorblindDeficiency deficiency;
99
guint32 cache[2 * COLOR_CACHE_SIZE];
100
gfloat gamma_lut[256];
103
struct _CdisplayColorblindClass
105
GimpColorDisplayClass parent_instance;
116
static GType cdisplay_colorblind_get_type (GTypeModule *module);
117
static void cdisplay_colorblind_class_init (CdisplayColorblindClass *klass);
118
static void cdisplay_colorblind_init (CdisplayColorblind *colorblind);
120
static void cdisplay_colorblind_set_property (GObject *object,
124
static void cdisplay_colorblind_get_property (GObject *object,
129
static void cdisplay_colorblind_convert (GimpColorDisplay *display,
135
static GtkWidget * cdisplay_colorblind_configure (GimpColorDisplay *display);
136
static void cdisplay_colorblind_changed (GimpColorDisplay *display);
138
static void cdisplay_colorblind_set_deficiency (CdisplayColorblind *colorblind,
139
ColorblindDeficiency value);
142
/* The RGB<->LMS transforms above are computed from the human cone
143
* photo-pigment absorption spectra and the monitor phosphor
144
* emission spectra. These parameters are fairly constant for most
145
* humans and most montiors (at least for modern CRTs). However,
146
* gamma will vary quite a bit, as it is a property of the monitor
147
* (eg. amplifier gain), the video card, and even the
148
* software. Further, users can adjust their gammas (either via
149
* adjusting the monitor amp gains or in software). That said, the
150
* following are the gamma estimates that we have used in the
151
* Vischeck code. Many colorblind users have viewed our simulations
152
* and told us that they "work" (simulated and original images are
153
* indistinguishabled).
156
static const gfloat gammaRGB = 2.1;
159
/* For most modern Cathode-Ray Tube monitors (CRTs), the following
160
* are good estimates of the RGB->LMS and LMS->RGB transform
161
* matrices. They are based on spectra measured on a typical CRT
162
* with a PhotoResearch PR650 spectral photometer and the Stockman
163
* human cone fundamentals. NOTE: these estimates will NOT work well
166
static const gfloat rgb2lms[9] =
181
static const gfloat lms2rgb[9] =
197
static const GimpModuleInfo cdisplay_colorblind_info =
199
GIMP_MODULE_ABI_VERSION,
200
N_("Color deficit simulation filter (Brettel-Vienot-Mollon algorithm)"),
201
"Michael Natterer <mitch@gimp.org>, Bob Dougherty <bob@vischeck.com>, "
202
"Alex Wade <alex@vischeck.com>",
204
"(c) 2002-2004, released under the GPL",
208
static GType cdisplay_colorblind_type = 0;
209
static GType cdisplay_colorblind_deficiency_type = 0;
210
static GimpColorDisplayClass *parent_class = NULL;
213
G_MODULE_EXPORT const GimpModuleInfo *
214
gimp_module_query (GTypeModule *module)
216
return &cdisplay_colorblind_info;
219
G_MODULE_EXPORT gboolean
220
gimp_module_register (GTypeModule *module)
222
cdisplay_colorblind_get_type (module);
223
cdisplay_colorblind_deficiency_get_type (module);
229
cdisplay_colorblind_get_type (GTypeModule *module)
231
if (! cdisplay_colorblind_type)
233
const GTypeInfo display_info =
235
sizeof (CdisplayColorblindClass),
236
(GBaseInitFunc) NULL,
237
(GBaseFinalizeFunc) NULL,
238
(GClassInitFunc) cdisplay_colorblind_class_init,
239
NULL, /* class_finalize */
240
NULL, /* class_data */
241
sizeof (CdisplayColorblind),
243
(GInstanceInitFunc) cdisplay_colorblind_init,
246
cdisplay_colorblind_type =
247
g_type_module_register_type (module,
248
GIMP_TYPE_COLOR_DISPLAY,
249
"CdisplayColorblind",
253
return cdisplay_colorblind_type;
258
cdisplay_colorblind_deficiency_get_type (GTypeModule *module)
260
if (! cdisplay_colorblind_deficiency_type)
262
cdisplay_colorblind_deficiency_type =
263
g_type_module_register_enum (module, "CDisplayColorblindDeficiency",
266
gimp_type_set_translation_domain (cdisplay_colorblind_deficiency_type,
267
GETTEXT_PACKAGE "-libgimp");
268
gimp_enum_set_value_descriptions (cdisplay_colorblind_deficiency_type,
272
return cdisplay_colorblind_deficiency_type;
276
cdisplay_colorblind_class_init (CdisplayColorblindClass *klass)
278
GObjectClass *object_class = G_OBJECT_CLASS (klass);
279
GimpColorDisplayClass *display_class = GIMP_COLOR_DISPLAY_CLASS (klass);
281
parent_class = g_type_class_peek_parent (klass);
283
object_class->get_property = cdisplay_colorblind_get_property;
284
object_class->set_property = cdisplay_colorblind_set_property;
286
GIMP_CONFIG_INSTALL_PROP_ENUM (object_class, PROP_DEFICIENCY,
288
CDISPLAY_TYPE_COLORBLIND_DEFICIENCY,
292
display_class->name = _("Color Deficient Vision");
293
display_class->help_id = "gimp-colordisplay-colorblind";
294
display_class->stock_id = GIMP_STOCK_DISPLAY_FILTER_COLORBLIND;
296
display_class->convert = cdisplay_colorblind_convert;
297
display_class->configure = cdisplay_colorblind_configure;
298
display_class->changed = cdisplay_colorblind_changed;
302
cdisplay_colorblind_init (CdisplayColorblind *colorblind)
306
for (i = 0; i < 256; i++)
307
colorblind->gamma_lut[i] = pow (i, 1.0 / gammaRGB);
311
cdisplay_colorblind_get_property (GObject *object,
316
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (object);
320
case PROP_DEFICIENCY:
321
g_value_set_enum (value, colorblind->deficiency);
324
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
330
cdisplay_colorblind_set_property (GObject *object,
335
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (object);
339
case PROP_DEFICIENCY:
340
cdisplay_colorblind_set_deficiency (colorblind,
341
g_value_get_enum (value));
344
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
351
* This function performs a binary search in the gamma LUT. It
352
* assumes a monotone gamma function and it simply clips out of gamut
353
* values. It would be better to desaturate instead of clipping.
356
lut_lookup (gfloat value,
364
if (lut[offset] > value)
370
if (lut[offset + 1] > value)
379
/* the algorithm above can't reach 255 */
380
if (offset == 254 && lut[255] < value)
387
cdisplay_colorblind_convert (GimpColorDisplay *display,
394
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (display);
395
const gfloat a1 = colorblind->a1;
396
const gfloat b1 = colorblind->b1;
397
const gfloat c1 = colorblind->c1;
398
const gfloat a2 = colorblind->a2;
399
const gfloat b2 = colorblind->b2;
400
const gfloat c2 = colorblind->c2;
402
gfloat red, green, blue;
403
gfloat redOld, greenOld;
407
/* Require 3 bytes per pixel (assume RGB) */
411
for (y = 0; y < height; y++, buf += bpl)
412
for (x = 0, b = buf; x < width; x++, b += bpp)
417
/* First check our cache */
418
pixel = b[0] << 16 | b[1] << 8 | b[2];
419
index = pixel % COLOR_CACHE_SIZE;
421
if (colorblind->cache[2 * index] == pixel)
423
pixel = colorblind->cache[2 * index + 1];
425
b[2] = pixel & 0xFF; pixel >>= 8;
426
b[1] = pixel & 0xFF; pixel >>= 8;
432
/* Remove gamma to linearize RGB intensities */
433
red = colorblind->gamma_lut[b[0]];
434
green = colorblind->gamma_lut[b[1]];
435
blue = colorblind->gamma_lut[b[2]];
437
/* Convert to LMS (dot product with transform matrix) */
441
red = redOld * rgb2lms[0] + greenOld * rgb2lms[1] + blue * rgb2lms[2];
442
green = redOld * rgb2lms[3] + greenOld * rgb2lms[4] + blue * rgb2lms[5];
443
blue = redOld * rgb2lms[6] + greenOld * rgb2lms[7] + blue * rgb2lms[8];
445
switch (colorblind->deficiency)
447
case COLORBLIND_DEFICIENCY_DEUTERANOPIA:
449
/* See which side of the inflection line we fall... */
450
if (tmp < colorblind->inflection)
451
green = -(a1 * red + c1 * blue) / b1;
453
green = -(a2 * red + c2 * blue) / b2;
456
case COLORBLIND_DEFICIENCY_PROTANOPIA:
458
/* See which side of the inflection line we fall... */
459
if (tmp < colorblind->inflection)
460
red = -(b1 * green + c1 * blue) / a1;
462
red = -(b2 * green + c2 * blue) / a2;
465
case COLORBLIND_DEFICIENCY_TRITANOPIA:
467
/* See which side of the inflection line we fall... */
468
if (tmp < colorblind->inflection)
469
blue = -(a1 * red + b1 * green) / c1;
471
blue = -(a2 * red + b2 * green) / c2;
478
/* Convert back to RGB (cross product with transform matrix) */
482
red = redOld * lms2rgb[0] + greenOld * lms2rgb[1] + blue * lms2rgb[2];
483
green = redOld * lms2rgb[3] + greenOld * lms2rgb[4] + blue * lms2rgb[5];
484
blue = redOld * lms2rgb[6] + greenOld * lms2rgb[7] + blue * lms2rgb[8];
486
/* Apply gamma to go back to non-linear intensities */
487
b[0] = lut_lookup (red, colorblind->gamma_lut);
488
b[1] = lut_lookup (green, colorblind->gamma_lut);
489
b[2] = lut_lookup (blue, colorblind->gamma_lut);
491
/* Put the result into our cache */
492
colorblind->cache[2 * index] = pixel;
493
colorblind->cache[2 * index + 1] = b[0] << 16 | b[1] << 8 | b[2];
498
cdisplay_colorblind_configure (GimpColorDisplay *display)
500
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (display);
505
hbox = gtk_hbox_new (FALSE, 6);
507
label = gtk_label_new_with_mnemonic (_("Color _deficiency type:"));
508
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
509
gtk_widget_show (label);
512
gimp_prop_enum_combo_box_new (G_OBJECT (colorblind), "deficiency", 0, 0);
514
gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0);
515
gtk_widget_show (combo);
517
gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
523
cdisplay_colorblind_changed (GimpColorDisplay *display)
525
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (display);
529
/* This function performs initialisations that are dependant
530
* on the type of color deficiency.
533
/* Performs protan, deutan or tritan color image simulation based on
534
* Brettel, Vienot and Mollon JOSA 14/10 1997
535
* L,M,S for lambda=475,485,575,660
537
* Load the LMS anchor-point values for lambda = 475 & 485 nm (for
538
* protans & deutans) and the LMS values for lambda = 575 & 660 nm
541
anchor[0] = 0.08008; anchor[1] = 0.1579; anchor[2] = 0.5897;
542
anchor[3] = 0.1284; anchor[4] = 0.2237; anchor[5] = 0.3636;
543
anchor[6] = 0.9856; anchor[7] = 0.7325; anchor[8] = 0.001079;
544
anchor[9] = 0.0914; anchor[10] = 0.007009; anchor[11] = 0.0;
546
/* We also need LMS for RGB=(1,1,1)- the equal-energy point (one of
547
* our anchors) (we can just peel this out of the rgb2lms transform
550
anchor_e[0] = rgb2lms[0] + rgb2lms[1] + rgb2lms[2];
551
anchor_e[1] = rgb2lms[3] + rgb2lms[4] + rgb2lms[5];
552
anchor_e[2] = rgb2lms[6] + rgb2lms[7] + rgb2lms[8];
554
switch (colorblind->deficiency)
556
case COLORBLIND_DEFICIENCY_DEUTERANOPIA:
557
/* find a,b,c for lam=575nm and lam=475 */
558
colorblind->a1 = anchor_e[1] * anchor[8] - anchor_e[2] * anchor[7];
559
colorblind->b1 = anchor_e[2] * anchor[6] - anchor_e[0] * anchor[8];
560
colorblind->c1 = anchor_e[0] * anchor[7] - anchor_e[1] * anchor[6];
561
colorblind->a2 = anchor_e[1] * anchor[2] - anchor_e[2] * anchor[1];
562
colorblind->b2 = anchor_e[2] * anchor[0] - anchor_e[0] * anchor[2];
563
colorblind->c2 = anchor_e[0] * anchor[1] - anchor_e[1] * anchor[0];
564
colorblind->inflection = (anchor_e[2] / anchor_e[0]);
567
case COLORBLIND_DEFICIENCY_PROTANOPIA:
568
/* find a,b,c for lam=575nm and lam=475 */
569
colorblind->a1 = anchor_e[1] * anchor[8] - anchor_e[2] * anchor[7];
570
colorblind->b1 = anchor_e[2] * anchor[6] - anchor_e[0] * anchor[8];
571
colorblind->c1 = anchor_e[0] * anchor[7] - anchor_e[1] * anchor[6];
572
colorblind->a2 = anchor_e[1] * anchor[2] - anchor_e[2] * anchor[1];
573
colorblind->b2 = anchor_e[2] * anchor[0] - anchor_e[0] * anchor[2];
574
colorblind->c2 = anchor_e[0] * anchor[1] - anchor_e[1] * anchor[0];
575
colorblind->inflection = (anchor_e[2] / anchor_e[1]);
578
case COLORBLIND_DEFICIENCY_TRITANOPIA:
579
/* Set 1: regions where lambda_a=575, set 2: lambda_a=475 */
580
colorblind->a1 = anchor_e[1] * anchor[11] - anchor_e[2] * anchor[10];
581
colorblind->b1 = anchor_e[2] * anchor[9] - anchor_e[0] * anchor[11];
582
colorblind->c1 = anchor_e[0] * anchor[10] - anchor_e[1] * anchor[9];
583
colorblind->a2 = anchor_e[1] * anchor[5] - anchor_e[2] * anchor[4];
584
colorblind->b2 = anchor_e[2] * anchor[3] - anchor_e[0] * anchor[5];
585
colorblind->c2 = anchor_e[0] * anchor[4] - anchor_e[1] * anchor[3];
586
colorblind->inflection = (anchor_e[1] / anchor_e[0]);
590
/* Invalidate the cache */
591
memset (colorblind->cache, 0, sizeof (colorblind->cache));
595
cdisplay_colorblind_set_deficiency (CdisplayColorblind *colorblind,
596
ColorblindDeficiency value)
598
if (value != colorblind->deficiency)
600
GEnumClass *enum_class;
602
enum_class = g_type_class_peek (CDISPLAY_TYPE_COLORBLIND_DEFICIENCY);
604
if (! g_enum_get_value (enum_class, value))
607
colorblind->deficiency = value;
609
g_object_notify (G_OBJECT (colorblind), "deficiency");
610
gimp_color_display_changed (GIMP_COLOR_DISPLAY (colorblind));