-
Notifications
You must be signed in to change notification settings - Fork 4
/
libDaltonLens.c
340 lines (286 loc) · 12.8 KB
/
libDaltonLens.c
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
/* libDaltonLens - public domain library - http://daltonlens.org
no warranty implied; use at your own risk
Author: Nicolas Burrus <nicolas@burrus.name>
LICENSE information at the end of the file.
*/
/*
For more information about the math of these two algorithms
see https://daltonlens.org/understanding-cvd-simulation/
The notebook used to dump the precomputed matrices can be found
in the DaltonLens-Python project:
https://github.com/DaltonLens/DaltonLens-Python/blob/master/research/for-desktop/precomputed_matrices.ipynb
*/
#include "libDaltonLens.h"
#include <math.h>
/*
From https://en.wikipedia.org/wiki/SRGB
This is the standard used by most images and displays. This is the textbook
implementation that uses powf.
Optimizations are beyond the scope of this library, but it is possible to
make it faster by using a lookup-table (only 256 values), but then it's less
SIMD-friendly. It's also unclear if it'll be much faster on recent CPUs as
they are often memory-bound.
Another option is to use a fast pow implementation, e.g. with a Chebychev
approximation:
https://stackoverflow.com/questions/6475373/optimizations-for-pow-with-const-non-integer-exponent
*/
static inline float linearRGB_from_sRGB(unsigned char v)
{
float fv = v / 255.f;
if (fv < 0.04045f) return fv / 12.92f;
return powf((fv + 0.055f) / 1.055f, 2.4f);
}
static inline unsigned char sRGB_from_linearRGB(float v)
{
if (v <= 0.f) return 0;
if (v >= 1.f) return 255;
if (v < 0.0031308f) return 0.5f + (v * 12.92 * 255.f);
return 0.f + 255.f * (powf(v, 1.f / 2.4f) * 1.055f - 0.055f);
}
/*
Brettel 1997 precomputed parameters.
LMS model
=========
These values use the sRGB standard to go from linearRGB to CIE XYZ, and the
Smith & Pokorny 1975 model for CIE XYZ to LMS. This is the LMS model used by
Viénot, Brettel and Mollon, but upgraded to use the sRGB standard used by
modern monitors.
Projection Planes
=================
These were computed using RGB white as the neutral element, not the
equal-energy E. This option is commonly chosen by the Brettel
implementations (including Vischeck), and it increases the range of colors
that project within the sRGB gamut and avoids clipping too much.
DaltonLens-Python Code to regenerate
====================================
simulator = simulate.Simulator_Brettel1997(convert.LMSModel_sRGB_SmithPokorny75())
simulator.dumpPrecomputedValues = True
simulator.simulate_cvd(np.zeros((1,1,3), dtype=np.uint8), simulate.Deficiency.PROTAN, severity=1.0)
simulator.simulate_cvd(np.zeros((1,1,3), dtype=np.uint8), simulate.Deficiency.DEUTAN, severity=1.0)
simulator.simulate_cvd(np.zeros((1,1,3), dtype=np.uint8), simulate.Deficiency.TRITAN, severity=1.0)
Alternative code to get the same output as Vischeck (as implemented in GIMP display filters):
simulator = simulate.Simulator_Vischeck()
simulator.dumpPrecomputedValues = True
simulator.simulate_cvd(np.zeros((1,1,3), dtype=np.uint8), simulate.Deficiency.PROTAN, severity=1.0)
simulator.simulate_cvd(np.zeros((1,1,3), dtype=np.uint8), simulate.Deficiency.DEUTAN, severity=1.0)
simulator.simulate_cvd(np.zeros((1,1,3), dtype=np.uint8), simulate.Deficiency.TRITAN, severity=1.0)
The implementation got simplified to minimize compute (with the
exact same output), refer to
https://daltonlens.org/cvd-simulation-svg-filters/ for more details.
*/
/*
These parameters combine the full projection pipeline for each half-plane so
we don't need an explicit transform to the LMS space.
To check on which plane the LMS point should project we also don't need to
actually compute the LMS coordinates, and can do it directly in the RGB space.
*/
struct DLBrettel1997Params
{
// Transformation using plane 1 == rgbFromLms . projection1 . lmsFromRgb
float rgbCvdFromRgb_1[9];
// Full transformation using plane 2 == rgbFromLms . projection2 . lmsFromRgb
float rgbCvdFromRgb_2[9];
// Normal of the separation plane to pick the right transform, already in the RGB space.
// == normalInLms . lmsFromRgb
float separationPlaneNormalInRgb[3];
};
static struct DLBrettel1997Params brettel_protan_params = {
{
0.14980, 1.19548, -0.34528,
0.10764, 0.84864, 0.04372,
0.00384, -0.00540, 1.00156,
},
{
0.14570, 1.16172, -0.30742,
0.10816, 0.85291, 0.03892,
0.00386, -0.00524, 1.00139,
},
{ 0.00048, 0.00393, -0.00441 }
};
static struct DLBrettel1997Params brettel_deutan_params = {
{
0.36477, 0.86381, -0.22858,
0.26294, 0.64245, 0.09462,
-0.02006, 0.02728, 0.99278,
},
{
0.37298, 0.88166, -0.25464,
0.25954, 0.63506, 0.10540,
-0.01980, 0.02784, 0.99196,
},
{ -0.00281, -0.00611, 0.00892 }
};
static struct DLBrettel1997Params brettel_tritan_params = {
{
1.01277, 0.13548, -0.14826,
-0.01243, 0.86812, 0.14431,
0.07589, 0.80500, 0.11911,
},
{
0.93678, 0.18979, -0.12657,
0.06154, 0.81526, 0.12320,
-0.37562, 1.12767, 0.24796,
},
{ 0.03901, -0.02788, -0.01113 }
};
void dl_simulate_cvd_brettel1997 (enum DLDeficiency deficiency, float severity, unsigned char* srgba_image, size_t width, size_t height, size_t bytesPerRow)
{
// Compute a default bytesPerRow if it wasn't specified.
if (bytesPerRow == 0)
{
bytesPerRow = width * 4;
}
const struct DLBrettel1997Params* params = NULL;
switch (deficiency)
{
case DLDeficiency_Protan: params = &brettel_protan_params; break;
case DLDeficiency_Deutan: params = &brettel_deutan_params; break;
case DLDeficiency_Tritan: params = &brettel_tritan_params; break;
}
for (size_t row = 0; row < height; ++row)
{
unsigned char* rowPtr = srgba_image + bytesPerRow*row;
for (size_t col = 0; col < width*4; col += 4)
{
// rgb = linearRGB_from_sRGB(srgb)
// alpha is untouched.
const float rgb[3] = {
linearRGB_from_sRGB(rowPtr[col + 0]),
linearRGB_from_sRGB(rowPtr[col + 1]),
linearRGB_from_sRGB(rowPtr[col + 2])
};
// Check on which plane we should project by comparing wih the separation plane normal.
const float* n = params->separationPlaneNormalInRgb;
const float dotWithSepPlane = rgb[0]*n[0] + rgb[1]*n[1] + rgb[2]*n[2];
const float* rgbCvdFromRgb = (dotWithSepPlane >= 0 ? params->rgbCvdFromRgb_1 : params->rgbCvdFromRgb_2);
float rgb_cvd[3] = {
rgbCvdFromRgb[0]*rgb[0] + rgbCvdFromRgb[1]*rgb[1] + rgbCvdFromRgb[2]*rgb[2],
rgbCvdFromRgb[3]*rgb[0] + rgbCvdFromRgb[4]*rgb[1] + rgbCvdFromRgb[5]*rgb[2],
rgbCvdFromRgb[6]*rgb[0] + rgbCvdFromRgb[7]*rgb[1] + rgbCvdFromRgb[8]*rgb[2]
};
// Apply the severity factor as a linear interpolation.
// It's the same to do it in the RGB space or in the LMS
// space since it's a linear transform.
rgb_cvd[0] = rgb_cvd[0]*severity + rgb[0]*(1.f-severity);
rgb_cvd[1] = rgb_cvd[1]*severity + rgb[1]*(1.f-severity);
rgb_cvd[2] = rgb_cvd[2]*severity + rgb[2]*(1.f-severity);
// Encode as sRGB and write the result.
rowPtr[col + 0] = sRGB_from_linearRGB(rgb_cvd[0]);
rowPtr[col + 1] = sRGB_from_linearRGB(rgb_cvd[1]);
rowPtr[col + 2] = sRGB_from_linearRGB(rgb_cvd[2]);
}
}
}
/*
Viénot 1999 precomputed parameters.
This follows the paper exactly, but using the modern sRGB standard to decode
the input RGB values.
Since there is only one projection plane, the entire pipeline can be reduced
to a single 3x3 matrix multiplication in the linearRGB space.
DaltonLens-Python Code to regenerate
====================================
simulator =
simulate.Simulator_Vienot1999(convert.LMSModel_sRGB_SmithPokorny75())
simulator.dumpPrecomputedValues = True
simulator.simulate_cvd(np.zeros((1,1,3), dtype=np.uint8), simulate.Deficiency.PROTAN, severity=1.0)
simulator.simulate_cvd(np.zeros((1,1,3), dtype=np.uint8), simulate.Deficiency.DEUTAN, severity=1.0)
simulator.simulate_cvd(np.zeros((1,1,3), dtype=np.uint8), simulate.Deficiency.TRITAN, severity=1.0)
*/
static float dl_vienot_protan_rgbCvd_from_rgb[] = {
0.11238, 0.88762, 0.00000,
0.11238, 0.88762, -0.00000,
0.00401, -0.00401, 1.00000
};
static float dl_vienot_deutan_rgbCvd_from_rgb[] = {
0.29275, 0.70725, 0.00000,
0.29275, 0.70725, -0.00000,
-0.02234, 0.02234, 1.00000
};
// WARNING: Viénot 1999 is not accurate for tritanopia. Use Brettel 1997 instead.
static float dl_vienot_tritan_rgbCvd_from_rgb[] = {
1.00000, 0.14461, -0.14461,
0.00000, 0.85924, 0.14076,
-0.00000, 0.85924, 0.14076
};
void dl_simulate_cvd_vienot1999 (enum DLDeficiency deficiency, float severity, unsigned char* srgba_image, size_t width, size_t height, size_t bytesPerRow)
{
// Compute a default bytesPerRow if it wasn't specified.
if (bytesPerRow == 0)
{
bytesPerRow = width * 4;
}
const float* rgbCvd_from_rgb = NULL;
switch (deficiency)
{
case DLDeficiency_Protan: rgbCvd_from_rgb = dl_vienot_protan_rgbCvd_from_rgb; break;
case DLDeficiency_Deutan: rgbCvd_from_rgb = dl_vienot_deutan_rgbCvd_from_rgb; break;
case DLDeficiency_Tritan: rgbCvd_from_rgb = dl_vienot_tritan_rgbCvd_from_rgb; break;
}
for (size_t row = 0; row < height; ++row)
{
unsigned char* rowPtr = srgba_image + bytesPerRow*row;
for (size_t col = 0; col < width*4; col += 4)
{
// rgb = linearRGB_from_sRGB(srgb)
// alpha is untouched.
float rgb[3] = {
linearRGB_from_sRGB(rowPtr[col + 0]),
linearRGB_from_sRGB(rowPtr[col + 1]),
linearRGB_from_sRGB(rowPtr[col + 2])
};
// rgb_cvd = rgbCvd_from_rgb * rgb
float rgb_cvd[3] = {
rgbCvd_from_rgb[0]*rgb[0] + rgbCvd_from_rgb[1]*rgb[1] + rgbCvd_from_rgb[2]*rgb[2],
rgbCvd_from_rgb[3]*rgb[0] + rgbCvd_from_rgb[4]*rgb[1] + rgbCvd_from_rgb[5]*rgb[2],
rgbCvd_from_rgb[6]*rgb[0] + rgbCvd_from_rgb[7]*rgb[1] + rgbCvd_from_rgb[8]*rgb[2]
};
// Implement the severity factor as a linear interpolation.
if (severity < 0.999f)
{
rgb_cvd[0] = severity*rgb_cvd[0] + (1.f - severity)*rgb[0];
rgb_cvd[1] = severity*rgb_cvd[1] + (1.f - severity)*rgb[1];
rgb_cvd[2] = severity*rgb_cvd[2] + (1.f - severity)*rgb[2];
}
// Write the result, encoded to sRGB
rowPtr[col + 0] = sRGB_from_linearRGB(rgb_cvd[0]);
rowPtr[col + 1] = sRGB_from_linearRGB(rgb_cvd[1]);
rowPtr[col + 2] = sRGB_from_linearRGB(rgb_cvd[2]);
}
}
}
void dl_simulate_cvd (enum DLDeficiency deficiency, float severity, unsigned char* srgba_image, size_t width, size_t height, size_t bytesPerRow)
{
// Viénot 1999 is not accurate for tritanopia, so use Brettel in that case.
// Otherwise use Viénot 1999 because it's a bit faster.
if (deficiency == DLDeficiency_Tritan)
{
dl_simulate_cvd_brettel1997(deficiency, severity, srgba_image, width, height, bytesPerRow);
}
else
{
dl_simulate_cvd_vienot1999(deficiency, severity, srgba_image, width, height, bytesPerRow);
}
}
/*
LICENSE
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
*/