1
/* The GIMP -- an 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.
36
#include "libgimpbase/gimpbase.h"
37
#include "libgimpmodule/gimpmodule.h"
38
#include "libgimpwidgets/gimpwidgets.h"
39
#include "libgimpmath/gimpmath.h"
41
#include "libgimp/libgimp-intl.h"
46
COLORBLIND_DEFICIENCY_PROTANOPIA,
47
COLORBLIND_DEFICIENCY_DEUTERANOPIA,
48
COLORBLIND_DEFICIENCY_TRITANOPIA
49
} ColorblindDeficiency;
51
#define CDISPLAY_TYPE_COLORBLIND_DEFICIENCY (cdisplay_colorblind_deficiency_type)
52
static GType cdisplay_colorblind_deficiency_get_type (GTypeModule *module);
54
static const GEnumValue cdisplay_colorblind_deficiency_enum_values[] =
56
{ COLORBLIND_DEFICIENCY_PROTANOPIA,
57
N_("Protanopia (insensitivity to red)"), "protanopia" },
58
{ COLORBLIND_DEFICIENCY_DEUTERANOPIA,
59
N_("Deuteranopia (insensitivity to green)"), "deuteranopia" },
60
{ COLORBLIND_DEFICIENCY_TRITANOPIA,
61
N_("Tritanopia (insensitivity to blue)"), "tritanopia" },
66
#define DEFAULT_DEFICIENCY COLORBLIND_DEFICIENCY_DEUTERANOPIA
67
#define COLOR_CACHE_SIZE 1021
70
#define CDISPLAY_TYPE_COLORBLIND (cdisplay_colorblind_type)
71
#define CDISPLAY_COLORBLIND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CDISPLAY_TYPE_COLORBLIND, CdisplayColorblind))
72
#define CDISPLAY_COLORBLIND_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CDISPLAY_TYPE_COLORBLIND, CdisplayColorblindClass))
73
#define CDISPLAY_IS_COLORBLIND(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CDISPLAY_TYPE_COLORBLIND))
74
#define CDISPLAY_IS_COLORBLIND_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CDISPLAY_TYPE_COLORBLIND))
77
typedef struct _CdisplayColorblind CdisplayColorblind;
78
typedef struct _CdisplayColorblindClass CdisplayColorblindClass;
80
struct _CdisplayColorblind
82
GimpColorDisplay parent_instance;
84
ColorblindDeficiency deficiency;
94
guint32 cache[2 * COLOR_CACHE_SIZE];
100
struct _CdisplayColorblindClass
102
GimpColorDisplayClass parent_instance;
113
static GType cdisplay_colorblind_get_type (GTypeModule *module);
114
static void cdisplay_colorblind_class_init (CdisplayColorblindClass *klass);
115
static void cdisplay_colorblind_init (CdisplayColorblind *colorblind);
117
static void cdisplay_colorblind_dispose (GObject *object);
118
static void cdisplay_colorblind_set_property (GObject *object,
122
static void cdisplay_colorblind_get_property (GObject *object,
128
static GimpColorDisplay * cdisplay_colorblind_clone (GimpColorDisplay *display);
129
static void cdisplay_colorblind_convert (GimpColorDisplay *display,
135
static void cdisplay_colorblind_load_state (GimpColorDisplay *display,
136
GimpParasite *state);
137
static GimpParasite * cdisplay_colorblind_save_state (GimpColorDisplay *display);
138
static GtkWidget * cdisplay_colorblind_configure (GimpColorDisplay *display);
139
static void cdisplay_colorblind_configure_reset (GimpColorDisplay *display);
141
static void cdisplay_colorblind_changed (GimpColorDisplay *display);
143
static void cdisplay_colorblind_set_deficiency (CdisplayColorblind *colorblind,
144
ColorblindDeficiency value);
146
static void colorblind_deficiency_callback (GtkWidget *widget,
147
CdisplayColorblind *colorblind);
150
static const GimpModuleInfo cdisplay_colorblind_info =
152
GIMP_MODULE_ABI_VERSION,
153
N_("Color deficit simulation filter (Brettel-Vienot-Mollon algorithm)"),
154
"Michael Natterer <mitch@gimp.org>, Bob Dougherty <bob@vischeck.com>, "
155
"Alex Wade <alex@vischeck.com>",
157
"(c) 2002-2004, released under the GPL",
161
static GType cdisplay_colorblind_type = 0;
162
static GType cdisplay_colorblind_deficiency_type = 0;
163
static GimpColorDisplayClass *parent_class = NULL;
166
G_MODULE_EXPORT const GimpModuleInfo *
167
gimp_module_query (GTypeModule *module)
169
return &cdisplay_colorblind_info;
172
G_MODULE_EXPORT gboolean
173
gimp_module_register (GTypeModule *module)
175
cdisplay_colorblind_get_type (module);
176
cdisplay_colorblind_deficiency_get_type (module);
182
cdisplay_colorblind_get_type (GTypeModule *module)
184
if (! cdisplay_colorblind_type)
186
static const GTypeInfo display_info =
188
sizeof (CdisplayColorblindClass),
189
(GBaseInitFunc) NULL,
190
(GBaseFinalizeFunc) NULL,
191
(GClassInitFunc) cdisplay_colorblind_class_init,
192
NULL, /* class_finalize */
193
NULL, /* class_data */
194
sizeof (CdisplayColorblind),
196
(GInstanceInitFunc) cdisplay_colorblind_init,
199
cdisplay_colorblind_type =
200
g_type_module_register_type (module,
201
GIMP_TYPE_COLOR_DISPLAY,
202
"CdisplayColorblind",
206
return cdisplay_colorblind_type;
211
cdisplay_colorblind_deficiency_get_type (GTypeModule *module)
213
if (! cdisplay_colorblind_deficiency_type)
214
cdisplay_colorblind_deficiency_type =
215
gimp_module_register_enum (module,
216
"CDisplayColorblindDeficiency",
217
cdisplay_colorblind_deficiency_enum_values);
219
return cdisplay_colorblind_deficiency_type;
223
cdisplay_colorblind_class_init (CdisplayColorblindClass *klass)
225
GObjectClass *object_class = G_OBJECT_CLASS (klass);
226
GimpColorDisplayClass *display_class = GIMP_COLOR_DISPLAY_CLASS (klass);
228
parent_class = g_type_class_peek_parent (klass);
230
object_class->dispose = cdisplay_colorblind_dispose;
231
object_class->get_property = cdisplay_colorblind_get_property;
232
object_class->set_property = cdisplay_colorblind_set_property;
234
g_object_class_install_property (object_class, PROP_DEFICIENCY,
235
g_param_spec_enum ("deficiency", NULL, NULL,
236
CDISPLAY_TYPE_COLORBLIND_DEFICIENCY,
240
GIMP_MODULE_PARAM_SERIALIZE));
242
display_class->name = _("Color Deficient Vision");
243
display_class->help_id = "gimp-colordisplay-colorblind";
244
display_class->clone = cdisplay_colorblind_clone;
245
display_class->convert = cdisplay_colorblind_convert;
246
display_class->load_state = cdisplay_colorblind_load_state;
247
display_class->save_state = cdisplay_colorblind_save_state;
248
display_class->configure = cdisplay_colorblind_configure;
249
display_class->configure_reset = cdisplay_colorblind_configure_reset;
250
display_class->changed = cdisplay_colorblind_changed;
254
cdisplay_colorblind_init (CdisplayColorblind *colorblind)
256
/* For most modern Cathode-Ray Tube monitors (CRTs), the following
257
* are good estimates of the RGB->LMS and LMS->RGB transform
258
* matrices. They are based on spectra measured on a typical CRT
259
* with a PhotoResearch PR650 spectral photometer and the Stockman
260
* human cone fundamentals. NOTE: these estimates will NOT work well
263
colorblind->rgb2lms[0] = 0.05059983;
264
colorblind->rgb2lms[1] = 0.08585369;
265
colorblind->rgb2lms[2] = 0.00952420;
267
colorblind->rgb2lms[3] = 0.01893033;
268
colorblind->rgb2lms[4] = 0.08925308;
269
colorblind->rgb2lms[5] = 0.01370054;
271
colorblind->rgb2lms[6] = 0.00292202;
272
colorblind->rgb2lms[7] = 0.00975732;
273
colorblind->rgb2lms[8] = 0.07145979;
275
colorblind->lms2rgb[0] = 30.830854;
276
colorblind->lms2rgb[1] = -29.832659;
277
colorblind->lms2rgb[2] = 1.610474;
279
colorblind->lms2rgb[3] = -6.481468;
280
colorblind->lms2rgb[4] = 17.715578;
281
colorblind->lms2rgb[5] = -2.532642;
283
colorblind->lms2rgb[6] = -0.375690;
284
colorblind->lms2rgb[7] = -1.199062;
285
colorblind->lms2rgb[8] = 14.273846;
287
/* The RGB<->LMS transforms above are computed from the human cone
288
* photo-pigment absorption spectra and the monitor phosphor
289
* emission spectra. These parameters are fairly constant for most
290
* humans and most montiors (at least for modern CRTs). However,
291
* gamma will vary quite a bit, as it is a property of the monitor
292
* (eg. amplifier gain), the video card, and even the
293
* software. Further, users can adjust their gammas (either via
294
* adjusting the monitor amp gains or in software). That said, the
295
* following are the gamma estimates that we have used in the
296
* Vischeck code. Many colorblind users have viewed our simulations
297
* and told us that they "work" (simulated and original images are
298
* indistinguishabled).
300
colorblind->gammaRGB[0] = 2.1;
301
colorblind->gammaRGB[1] = 2.0;
302
colorblind->gammaRGB[2] = 2.1;
306
cdisplay_colorblind_dispose (GObject *object)
308
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (object);
310
if (colorblind->hbox)
311
gtk_widget_destroy (colorblind->hbox);
313
G_OBJECT_CLASS (parent_class)->dispose (object);
317
cdisplay_colorblind_get_property (GObject *object,
322
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (object);
326
case PROP_DEFICIENCY:
327
g_value_set_enum (value, colorblind->deficiency);
330
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
336
cdisplay_colorblind_set_property (GObject *object,
341
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (object);
345
case PROP_DEFICIENCY:
346
cdisplay_colorblind_set_deficiency (colorblind,
347
g_value_get_enum (value));
350
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
355
static GimpColorDisplay *
356
cdisplay_colorblind_clone (GimpColorDisplay *display)
358
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (display);
359
CdisplayColorblind *copy;
361
copy = CDISPLAY_COLORBLIND (gimp_color_display_new (G_TYPE_FROM_INSTANCE (colorblind)));
363
copy->deficiency = colorblind->deficiency;
365
return GIMP_COLOR_DISPLAY (copy);
369
cdisplay_colorblind_convert (GimpColorDisplay *display,
376
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (display);
378
gfloat rgb2lms[9],lms2rgb[9];
379
gfloat a1, b1, c1, a2, b2, c2;
381
gfloat red, green, blue, redOld, greenOld;
384
/* Require 3 bytes per pixel (assume RGB) */
388
/* to improve readability, copy the parameters into local variables */
389
memcpy (rgb2lms, colorblind->rgb2lms, sizeof (rgb2lms));
390
memcpy (lms2rgb, colorblind->lms2rgb, sizeof (lms2rgb));
391
a1 = colorblind->a1; b1 = colorblind->b1; c1 = colorblind->c1;
392
a2 = colorblind->a2; b2 = colorblind->b2; c2 = colorblind->c2;
394
for (y = 0; y < height; y++, buf += bpl)
395
for (x = 0, b = buf; x < width; x++, b += bpp)
400
/* First check our cache */
401
pixel = b[0] << 16 | b[1] << 8 | b[2];
402
index = pixel % COLOR_CACHE_SIZE;
404
if (colorblind->cache[2 * index] == pixel)
406
pixel = colorblind->cache[2 * index + 1];
408
b[2] = pixel & 0xFF; pixel >>= 8;
409
b[1] = pixel & 0xFF; pixel >>= 8;
419
/* Remove gamma to linearize RGB intensities */
420
red = pow (red, 1.0 / colorblind->gammaRGB[0]);
421
green = pow (green, 1.0 / colorblind->gammaRGB[1]);
422
blue = pow (blue, 1.0 / colorblind->gammaRGB[2]);
424
/* Convert to LMS (dot product with transform matrix) */
428
red = redOld * rgb2lms[0] + greenOld * rgb2lms[1] + blue * rgb2lms[2];
429
green = redOld * rgb2lms[3] + greenOld * rgb2lms[4] + blue * rgb2lms[5];
430
blue = redOld * rgb2lms[6] + greenOld * rgb2lms[7] + blue * rgb2lms[8];
432
switch (colorblind->deficiency)
434
case COLORBLIND_DEFICIENCY_DEUTERANOPIA:
436
/* See which side of the inflection line we fall... */
437
if (tmp < colorblind->inflection)
438
green = -(a1 * red + c1 * blue) / b1;
440
green = -(a2 * red + c2 * blue) / b2;
443
case COLORBLIND_DEFICIENCY_PROTANOPIA:
445
/* See which side of the inflection line we fall... */
446
if (tmp < colorblind->inflection)
447
red = -(b1 * green + c1 * blue) / a1;
449
red = -(b2 * green + c2 * blue) / a2;
452
case COLORBLIND_DEFICIENCY_TRITANOPIA:
454
/* See which side of the inflection line we fall... */
455
if (tmp < colorblind->inflection)
456
blue = -(a1 * red + b1 * green) / c1;
458
blue = -(a2 * red + b2 * green) / c2;
465
/* Convert back to RGB (cross product with transform matrix) */
469
red = redOld * lms2rgb[0] + greenOld * lms2rgb[1] + blue * lms2rgb[2];
470
green = redOld * lms2rgb[3] + greenOld * lms2rgb[4] + blue * lms2rgb[5];
471
blue = redOld * lms2rgb[6] + greenOld * lms2rgb[7] + blue * lms2rgb[8];
473
/* Apply gamma to go back to non-linear intensities */
474
red = pow (red, colorblind->gammaRGB[0]);
475
green = pow (green, colorblind->gammaRGB[1]);
476
blue = pow (blue, colorblind->gammaRGB[2]);
478
/* Ensure that we stay within the RGB gamut */
479
/* *** FIX THIS: it would be better to desaturate than blindly clip. */
480
red = CLAMP (red, 0, 255);
481
green = CLAMP (green, 0, 255);
482
blue = CLAMP (blue, 0, 255);
484
/* Stuff result back into buffer */
486
b[1] = (guchar) green;
487
b[2] = (guchar) blue;
489
/* Put the result into our cache */
490
colorblind->cache[2 * index] = pixel;
491
colorblind->cache[2 * index + 1] = b[0] << 16 | b[1] << 8 | b[2];
496
cdisplay_colorblind_load_state (GimpColorDisplay *display,
499
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (display);
502
str = gimp_parasite_data (state);
504
if (str[gimp_parasite_data_size (state) - 1] == '\0')
508
if (sscanf (str, "%d", &value) == 1)
509
cdisplay_colorblind_set_deficiency (colorblind, value);
513
static GimpParasite *
514
cdisplay_colorblind_save_state (GimpColorDisplay *display)
516
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (display);
519
g_snprintf (buf, sizeof (buf), "%d", colorblind->deficiency);
521
return gimp_parasite_new ("Display/Colorblind", GIMP_PARASITE_PERSISTENT,
522
strlen (buf) + 1, buf);
526
cdisplay_colorblind_configure (GimpColorDisplay *display)
528
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (display);
531
if (colorblind->hbox)
532
gtk_widget_destroy (colorblind->hbox);
534
colorblind->hbox = gtk_hbox_new (FALSE, 6);
536
g_signal_connect (colorblind->hbox, "destroy",
537
G_CALLBACK (gtk_widget_destroyed),
540
label = gtk_label_new_with_mnemonic (_("Color _Deficiency Type:"));
541
gtk_box_pack_start (GTK_BOX (colorblind->hbox), label, FALSE, FALSE, 0);
542
gtk_widget_show (label);
545
gimp_int_combo_box_new (_("Protanopia (insensitivity to red)"),
546
COLORBLIND_DEFICIENCY_PROTANOPIA,
547
_("Deuteranopia (insensitivity to green)"),
548
COLORBLIND_DEFICIENCY_DEUTERANOPIA,
549
_("Tritanopia (insensitivity to blue)"),
550
COLORBLIND_DEFICIENCY_TRITANOPIA,
553
gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (colorblind->combo),
554
colorblind->deficiency);
556
g_signal_connect (colorblind->combo, "changed",
557
G_CALLBACK (colorblind_deficiency_callback),
560
gtk_box_pack_start (GTK_BOX (colorblind->hbox), colorblind->combo,
562
gtk_widget_show (colorblind->combo);
564
gtk_label_set_mnemonic_widget (GTK_LABEL (label), colorblind->combo);
566
return colorblind->hbox;
570
cdisplay_colorblind_configure_reset (GimpColorDisplay *display)
572
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (display);
574
if (colorblind->combo)
576
gimp_int_combo_box_set_active (GIMP_INT_COMBO_BOX (colorblind->combo),
578
colorblind->deficiency = DEFAULT_DEFICIENCY;
580
gimp_color_display_changed (GIMP_COLOR_DISPLAY (colorblind));
585
cdisplay_colorblind_changed (GimpColorDisplay *display)
587
CdisplayColorblind *colorblind = CDISPLAY_COLORBLIND (display);
591
/* This function performs initialisations that are dependant
592
* on the type of color deficiency.
595
/* Performs protan, deutan or tritan color image simulation based on
596
* Brettel, Vienot and Mollon JOSA 14/10 1997
597
* L,M,S for lambda=475,485,575,660
599
* Load the LMS anchor-point values for lambda = 475 & 485 nm (for
600
* protans & deutans) and the LMS values for lambda = 575 & 660 nm
603
anchor[0] = 0.08008; anchor[1] = 0.1579; anchor[2] = 0.5897;
604
anchor[3] = 0.1284; anchor[4] = 0.2237; anchor[5] = 0.3636;
605
anchor[6] = 0.9856; anchor[7] = 0.7325; anchor[8] = 0.001079;
606
anchor[9] = 0.0914; anchor[10] = 0.007009; anchor[11] = 0.0;
608
/* We also need LMS for RGB=(1,1,1)- the equal-energy point (one of
609
* our anchors) (we can just peel this out of the rgb2lms transform
613
colorblind->rgb2lms[0] + colorblind->rgb2lms[1] + colorblind->rgb2lms[2];
615
colorblind->rgb2lms[3] + colorblind->rgb2lms[4] + colorblind->rgb2lms[5];
617
colorblind->rgb2lms[6] + colorblind->rgb2lms[7] + colorblind->rgb2lms[8];
619
switch (colorblind->deficiency)
621
case COLORBLIND_DEFICIENCY_DEUTERANOPIA:
622
/* find a,b,c for lam=575nm and lam=475 */
623
colorblind->a1 = anchor_e[1] * anchor[8] - anchor_e[2] * anchor[7];
624
colorblind->b1 = anchor_e[2] * anchor[6] - anchor_e[0] * anchor[8];
625
colorblind->c1 = anchor_e[0] * anchor[7] - anchor_e[1] * anchor[6];
626
colorblind->a2 = anchor_e[1] * anchor[2] - anchor_e[2] * anchor[1];
627
colorblind->b2 = anchor_e[2] * anchor[0] - anchor_e[0] * anchor[2];
628
colorblind->c2 = anchor_e[0] * anchor[1] - anchor_e[1] * anchor[0];
629
colorblind->inflection = (anchor_e[2] / anchor_e[0]);
632
case COLORBLIND_DEFICIENCY_PROTANOPIA:
633
/* find a,b,c for lam=575nm and lam=475 */
634
colorblind->a1 = anchor_e[1] * anchor[8] - anchor_e[2] * anchor[7];
635
colorblind->b1 = anchor_e[2] * anchor[6] - anchor_e[0] * anchor[8];
636
colorblind->c1 = anchor_e[0] * anchor[7] - anchor_e[1] * anchor[6];
637
colorblind->a2 = anchor_e[1] * anchor[2] - anchor_e[2] * anchor[1];
638
colorblind->b2 = anchor_e[2] * anchor[0] - anchor_e[0] * anchor[2];
639
colorblind->c2 = anchor_e[0] * anchor[1] - anchor_e[1] * anchor[0];
640
colorblind->inflection = (anchor_e[2] / anchor_e[1]);
643
case COLORBLIND_DEFICIENCY_TRITANOPIA:
644
/* Set 1: regions where lambda_a=575, set 2: lambda_a=475 */
645
colorblind->a1 = anchor_e[1] * anchor[11] - anchor_e[2] * anchor[10];
646
colorblind->b1 = anchor_e[2] * anchor[9] - anchor_e[0] * anchor[11];
647
colorblind->c1 = anchor_e[0] * anchor[10] - anchor_e[1] * anchor[9];
648
colorblind->a2 = anchor_e[1] * anchor[5] - anchor_e[2] * anchor[4];
649
colorblind->b2 = anchor_e[2] * anchor[3] - anchor_e[0] * anchor[5];
650
colorblind->c2 = anchor_e[0] * anchor[4] - anchor_e[1] * anchor[3];
651
colorblind->inflection = (anchor_e[1] / anchor_e[0]);
655
/* Invalidate the cache */
656
memset (colorblind->cache, 0, sizeof (colorblind->cache));
660
cdisplay_colorblind_set_deficiency (CdisplayColorblind *colorblind,
661
ColorblindDeficiency value)
663
if (value != colorblind->deficiency)
665
GEnumClass *enum_class;
667
enum_class = g_type_class_peek (CDISPLAY_TYPE_COLORBLIND_DEFICIENCY);
669
if (! g_enum_get_value (enum_class, value))
672
colorblind->deficiency = value;
674
g_object_notify (G_OBJECT (colorblind), "deficiency");
675
gimp_color_display_changed (GIMP_COLOR_DISPLAY (colorblind));
680
colorblind_deficiency_callback (GtkWidget *widget,
681
CdisplayColorblind *colorblind)
685
gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), &value);
687
cdisplay_colorblind_set_deficiency (colorblind, value);