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
|
/*
* This file is part of the KDE libraries
* Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
**/
#include <tqimage.h>
#include <math.h>
void
mult(float a[3][3], /* I - First matrix */
float b[3][3], /* I - Second matrix */
float c[3][3]) /* I - Destination matrix */
{
int x, y; /* Looping vars */
float temp[3][3]; /* Temporary matrix */
/*
* Multiply a and b, putting the result in temp...
*/
for (y = 0; y < 3; y ++)
for (x = 0; x < 3; x ++)
temp[y][x] = b[y][0] * a[0][x] +
b[y][1] * a[1][x] +
b[y][2] * a[2][x];
/*
* Copy temp to c (that way c can be a pointer to a or b).
*/
memcpy(c, temp, sizeof(temp));
}
void
saturate(float mat[3][3], /* I - Matrix to append to */
float sat) /* I - Desired color saturation */
{
float smat[3][3]; /* Saturation matrix */
smat[0][0] = (1.0 - sat) * 0.3086 + sat;
smat[0][1] = (1.0 - sat) * 0.3086;
smat[0][2] = (1.0 - sat) * 0.3086;
smat[1][0] = (1.0 - sat) * 0.6094;
smat[1][1] = (1.0 - sat) * 0.6094 + sat;
smat[1][2] = (1.0 - sat) * 0.6094;
smat[2][0] = (1.0 - sat) * 0.0820;
smat[2][1] = (1.0 - sat) * 0.0820;
smat[2][2] = (1.0 - sat) * 0.0820 + sat;
mult(smat, mat, mat);
}
void
xform(float mat[3][3], /* I - Matrix */
float x, /* I - Input X coordinate */
float y, /* I - Input Y coordinate */
float z, /* I - Input Z coordinate */
float *tx, /* O - Output X coordinate */
float *ty, /* O - Output Y coordinate */
float *tz) /* O - Output Z coordinate */
{
*tx = x * mat[0][0] + y * mat[1][0] + z * mat[2][0];
*ty = x * mat[0][1] + y * mat[1][1] + z * mat[2][1];
*tz = x * mat[0][2] + y * mat[1][2] + z * mat[2][2];
}
void
xrotate(float mat[3][3], /* I - Matrix */
float rs, /* I - Rotation angle sine */
float rc) /* I - Rotation angle cosine */
{
float rmat[3][3]; /* I - Rotation matrix */
rmat[0][0] = 1.0;
rmat[0][1] = 0.0;
rmat[0][2] = 0.0;
rmat[1][0] = 0.0;
rmat[1][1] = rc;
rmat[1][2] = rs;
rmat[2][0] = 0.0;
rmat[2][1] = -rs;
rmat[2][2] = rc;
mult(rmat, mat, mat);
}
void
yrotate(float mat[3][3], /* I - Matrix */
float rs, /* I - Rotation angle sine */
float rc) /* I - Rotation angle cosine */
{
float rmat[3][3]; /* I - Rotation matrix */
rmat[0][0] = rc;
rmat[0][1] = 0.0;
rmat[0][2] = -rs;
rmat[1][0] = 0.0;
rmat[1][1] = 1.0;
rmat[1][2] = 0.0;
rmat[2][0] = rs;
rmat[2][1] = 0.0;
rmat[2][2] = rc;
mult(rmat,mat,mat);
}
void
zrotate(float mat[3][3], /* I - Matrix */
float rs, /* I - Rotation angle sine */
float rc) /* I - Rotation angle cosine */
{
float rmat[3][3]; /* I - Rotation matrix */
rmat[0][0] = rc;
rmat[0][1] = rs;
rmat[0][2] = 0.0;
rmat[1][0] = -rs;
rmat[1][1] = rc;
rmat[1][2] = 0.0;
rmat[2][0] = 0.0;
rmat[2][1] = 0.0;
rmat[2][2] = 1.0;
mult(rmat,mat,mat);
}
void
zshear(float mat[3][3], /* I - Matrix */
float dx, /* I - X shear */
float dy) /* I - Y shear */
{
float smat[3][3]; /* Shear matrix */
smat[0][0] = 1.0;
smat[0][1] = 0.0;
smat[0][2] = dx;
smat[1][0] = 0.0;
smat[1][1] = 1.0;
smat[1][2] = dy;
smat[2][0] = 0.0;
smat[2][1] = 0.0;
smat[2][2] = 1.0;
mult(smat, mat, mat);
}
void
huerotate(float mat[3][3], /* I - Matrix to append to */
float rot) /* I - Hue rotation in degrees */
{
float hmat[3][3] = {{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0}}; /* Hue matrix */
float lx, ly, lz; /* Luminance vector */
float xrs, xrc; /* X rotation sine/cosine */
float yrs, yrc; /* Y rotation sine/cosine */
float zrs, zrc; /* Z rotation sine/cosine */
float zsx, zsy; /* Z shear x/y */
/*
* Rotate the gray vector into positive Z...
*/
xrs = M_SQRT1_2;
xrc = M_SQRT1_2;
xrotate(hmat,xrs,xrc);
yrs = -1.0 / sqrt(3.0);
yrc = -M_SQRT2 * yrs;
yrotate(hmat,yrs,yrc);
/*
* Shear the space to make the luminance plane horizontal...
*/
xform(hmat, 0.3086, 0.6094, 0.0820, &lx, &ly, &lz);
zsx = lx / lz;
zsy = ly / lz;
zshear(hmat, zsx, zsy);
/*
* Rotate the hue...
*/
zrs = sin(rot * M_PI / 180.0);
zrc = cos(rot * M_PI / 180.0);
zrotate(hmat, zrs, zrc);
/*
* Unshear the space to put the luminance plane back...
*/
zshear(hmat, -zsx, -zsy);
/*
* Rotate the gray vector back into place...
*/
yrotate(hmat, -yrs, yrc);
xrotate(hmat, -xrs, xrc);
/*
* Append it to the current matrix...
*/
mult(hmat, mat, mat);
}
void
bright(float mat[3][3],
float scale)
{
for (int i=0;i<3;i++)
for (int j=0;j<3;j++)
mat[i][j] *= scale;
}
//----------------------------------------------------------------------------------------------------
TQImage convertImage(const TQImage& image, int hue, int saturation, int brightness, int gamma)
{
float mat[3][3] = {{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0}};
int lut[3][3][256];
QRgb c;
int r,g,b,v,r2,g2,b2;
float gam = 1.0/(float(gamma)/1000.0);
QImage img(image);
saturate(mat,saturation*0.01);
huerotate(mat,(float)hue);
bright(mat,brightness*0.01);
for (int i = 0; i < 3; i ++)
for (int j = 0; j < 3; j ++)
for (int k = 0; k < 256; k ++)
lut[i][j][k] = (int)(mat[i][j] * k + 0.5);
img.detach();
for (int i=0;i<image.width();i++)
for (int j=0;j<image.height();j++)
{
c = image.pixel(i,j);
r = tqRed(c);
g = tqGreen(c);
b = tqBlue(c);
v = lut[0][0][r] + lut[1][0][g] + lut[2][0][b];
if (gamma != 1000) v = (int)rint(pow(v,gam));
if (v < 0) r2 = 0;
else if (v > 255) r2 = 255;
else r2 = v;
v = lut[0][1][r] + lut[1][1][g] + lut[2][1][b];
if (gamma != 1000) v = (int)rint(pow(v,gam));
if (v < 0) g2 = 0;
else if (v > 255) g2 = 255;
else g2 = v;
v = lut[0][2][r] + lut[1][2][g] + lut[2][2][b];
if (gamma != 1000) v = (int)rint(pow(v,gam));
if (v < 0) b2 = 0;
else if (v > 255) b2 = 255;
else b2 = v;
img.setPixel(i,j,tqRgb(r2,g2,b2));
}
return img;
}
|