~kubuntu-members/kmag/4.11

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
/***************************************************************************
                          colorsim.cpp  -  description
                             -------------------
    begin                : Mon Jan 21 14:54:37 CST 2008
    copyright            : (C) 2008 by Matthew Woehlke
    email                : mw_triad@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

/***************************************************************************

Citations:

[1] H. Brettel, F. Viénot and J. D. Mollon (1997)
      "Computerized simulation of color appearance for dichromats."
      J. Opt. Soc. Am. A 14, 2647-2655.
[2] F. Viénot, H. Brettel and J. D. Mollon (1999)
      "Digital video colourmaps for checking the legibility of displays by
        dichromats."
      Color Research and Application 24, 243-252.

 ***************************************************************************/

// application specific includes
#include "colorsim.h"

// include files for Qt
#include <QtGui/QColor>

#include <math.h>

typedef qreal matrix[3][3];

#define SIMPLE_ALGORITHM

#ifndef SIMPLE_ALGORITHM
typedef qreal vector[3];

struct fcoef {
  vector k[2];
//   vector e;
  matrix s[2];
};
#endif

class xyza {
  private:
    qreal x, y, z, a;

  public:
    xyza() {}
    xyza(const QColor &c);
    QRgb rgba() const;
    xyza gamma(qreal q) const;
    xyza operator*(const matrix m) const;
#ifndef SIMPLE_ALGORITHM
    xyza(const vector v);
    qreal operator*(const vector m) const;
    xyza flatten(fcoef c) const;
#endif
};

xyza::xyza(const QColor &c) :
  x(c.redF()), y(c.greenF()), z(c.blueF()), a(c.alphaF())
{
}

inline qreal clamp(qreal n)
{
  return qMin(qreal(1.0), qMax(qreal(0.0), n));
}

QRgb xyza::rgba() const
{
  return QColor::fromRgbF(clamp(x), clamp(y), clamp(z), a).rgba();
}

xyza xyza::operator*(const matrix m) const
{
  xyza r;
  r.x = (x * m[0][0]) + (y * m[0][1]) + (z * m[0][2]);
  r.y = (x * m[1][0]) + (y * m[1][1]) + (z * m[1][2]);
  r.z = (x * m[2][0]) + (y * m[2][1]) + (z * m[2][2]);
  r.a = a;
  return r;
}

xyza xyza::gamma(qreal q) const
{
  xyza r;
  r.x = pow(x, q);
  r.y = pow(y, q);
  r.z = pow(z, q);
  r.a = a;
  return r;
}

#if !defined(SIMPLE_ALGORITHM) || !defined(STANFORD_ALGORITHM)

/***************************************************************************

 These RGB<->LMS transformation matrices are from [1].

 ***************************************************************************/

static const matrix rgb2lms = {
  {0.1992, 0.4112, 0.0742},
  {0.0353, 0.2226, 0.0574},
  {0.0185, 0.1231, 1.3550}
};

static const matrix lms2rgb = {
  { 7.4645, -13.8882,  0.1796},
  {-1.1852,   6.8053, -0.2234},
  { 0.0058,  -0.4286,  0.7558}
};

#endif

#ifdef SIMPLE_ALGORITHM

# ifndef STANFORD_ALGORITHM // from "Computerized simulation of color appearance for dichromats"

#  ifdef CRA_ALGORITHM

/***************************************************************************

 These matrices are derived from Table II in [2], for the code based on the
 Onur/Poliang algorithm below, using the LMS transformation from [1].
 No tritanopia data were provided, so that simulation does not work correctly.

 ***************************************************************************/

static const matrix coef[3] = {
  { {0.0, 2.39238646, -0.04658523}, {0.0,        1.0, 0.0       }, {0.0, 0.0, 1.0} },
  { {1.0, 0.0,         0.0       }, {0.41799267, 0.0, 0.01947228}, {0.0, 0.0, 1.0} },
  { {1.0, 0.0,         0.0       }, {0.0,        1.0, 0.0       }, {0.0, 0.0, 0.0} }
};

#  else

/***************************************************************************

 These matrices are derived from the "Color Blindness Simulation" sample
 palettes from Visolve, Ryobi System Solutions, using the LMS transformations
 from [1].
 http://www.ryobi-sol.co.jp/visolve/en/simulation.html

 ***************************************************************************/

static const matrix coef[3] = {
  { {0.0, 2.60493696, -0.08742194}, {0.0,        1.0, 0.0       }, {0.0,          0.0,        1.0} },
  { {1.0, 0.0,         0.0       }, {0.38395980, 0.0, 0.03370622}, {0.0,          0.0,        1.0} },
  { {1.0, 0.0,         0.0       }, {0.0,        1.0, 0.0       }, {-3.11932916, 12.18076308, 0.0} }
};

#  endif

# else // from the "Analysis of Color Blindness" project

/***************************************************************************

 This code is based on the matrices from [2], as presented by Onur and Poliang.
 The tritanopia simulation uses different representative wavelengths (yellow
 and blue) than those reccommended by [1] and found in most other simulations
 (red and cyan).
 http://www.stanford.edu/~ofidaner/psych221_proj/colorblindness_project.htm

 ***************************************************************************/

static const matrix coef[3] = {
  { { 0.0, 2.02344, -2.52581}, {0.0,      1.0, 0.0    }, { 0.0,      0.0,      1.0} },
  { { 1.0, 0.0,      0.0    }, {0.494207, 0.0, 1.24827}, { 0.0,      0.0,      1.0} },
  { { 1.0, 0.0,      0.0    }, {0.0,      1.0, 0.0    }, {-0.395913, 0.801109, 0.0} }
};

static const matrix rgb2lms = {
  {17.8824,   43.5161,   4.11935},
  { 3.45565,  27.1554,   3.86714},
  { 0.0299566, 0.184309, 1.46709}
};

static const matrix lms2rgb = {
  { 0.080944447905, -0.130504409160,  0.116721066440},
  {-0.010248533515,  0.054019326636, -0.113614708214},
  {-0.000365296938, -0.004121614686,  0.693511404861}
};

# endif

inline QRgb recolor(QRgb c, int mode, qreal g)
{
  if (mode > 0 && mode < 4) {
    xyza n = QColor(c);
    if (g != 1.0) {
      xyza r = n.gamma(g) * rgb2lms * coef[mode-1] * lms2rgb;
      return r.gamma(qreal(1.0) / g).rgba();
    }
    else {
      xyza r = n * rgb2lms * coef[mode-1] * lms2rgb;
      return r.rgba();
    }
  }
  else {
    return qRgb(qGray(c), qGray(c), qGray(c));
  }
}

#else // from "Computerized simulation of color appearance for dichromats"

/***************************************************************************

 This code is based on [1]. The RGB<->LMS transformation matrices are declared
 above.

 ***************************************************************************/

static const fcoef coef[3] = {
  {
    { {0.0, 0.0, 1.0}, {0.0, 1.0, 0.0} }, // k
//     { }, // e
    // a { }
    { // s
      { {0.0, 2.39238646, -0.04658523}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0} },
      { {0.0, 0.37421464, -0.02034378}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0} }
    }
  },
  {
    { {0.0, 0.0, 1.0}, {1.0, 0.0, 0.0} }, // k
//     { }, // e
    // a { }
    { // s
      { {1.0, 0.0, 0.0}, {0.41799267, 0.0, 0.01947228}, {0.0, 0.0, 1.0} },
      { {1.0, 0.0, 0.0}, {0.41799267, 0.0, 0.01947228}, {0.0, 0.0, 1.0} }
    }
  },
  {
    { {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0} }, // k
//     { }, // e
    // a { }
    { // s
      { {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 0.0} },
      { {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 0.0} }
    }
  }
  /* The 's' matrices are calculated thusly:
  u = E.y*A.z - E.z*A.y;
  v = E.z*A.x - E.x*A.z;
  w = E.x*A.y - E.y*A.x;
  r.x = (mode != 1) ? x : (-v/u) * y + (-w/u) * z;
  r.y = (mode != 2) ? y : (-u/v) * x + (-w/v) * z;
  r.z = (mode != 3) ? z : (-u/w) * x + (-v/w) * y;
  */
};

xyza::xyza(const vector v) :
    x(v[0]), y(v[1]), z(v[2]), a(0.0)
{
}

qreal xyza::operator*(const vector v) const
{
  return (x * v[0]) + (y * v[1]) + (z * v[2]);
}

xyza xyza::flatten(fcoef c) const
{
//   xyza e(c.e);
//   qreal u = (*this * c.k[0]) / (*this * c.k[1]);
//   qreal v = (e * c.k[0]) / (e * c.k[1]);
//   int i = (u < v ? 0 : 1);
  int i = 0;
  return *this * c.s[i];
}

inline QRgb recolor(QRgb c, int mode, qreal g)
{
  if (mode > 0 && mode < 4) {
    xyza n = QColor(c);
    xyza r = n.gamma(g) * rgb2lms;
    r = r.flatten(coef[mode-1]) * lms2rgb;
    return r.gamma(qreal(1.0) / g).rgba();
  }
  else {
    const int g = qGray(c);
    return qRgb(g,g,g);
  }
}

#endif

QImage ColorSim::recolor(const QImage &pm, int mode, qreal gamma)
{
  // get raw data in a format we can manipulate
  QImage i = pm;
  if (i.format() != QImage::Format_RGB32 && i.format() != QImage::Format_ARGB32)
    i = i.convertToFormat(QImage::Format_ARGB32);

  int n = i.width() * i.height();
  QRgb *d = (QRgb*)i.bits();
  for (int k = 0; k < n; ++k)
    d[k] = ::recolor(d[k], mode, gamma);

  return i;
}
// kate: indent-width 2;