Debian Codegraph @

src/filter/hueshift0r/matrix.h

raw symbols SCIP
1 /*
2 * matrix -
3 * Use 4x4 matrices to process color images.
4 *
5 * To compile:
6 cc matrix.c -o matrix -lgutil -limage -lgl -lm
7 *
8 * Paul Haeberli - 1993
9 */
10 #include "frei0r/math.h"cxx . . $ `<file>/include/frei0r/math.h`/
11 #include <math.h>
12 #include <stdio.h>
13
14 /*
15 //Adobe ?? luma coeffs
16 #define RLUM (0.3086)
17 #define GLUM (0.6094)
18 #define BLUM (0.0820)
19 */
20
21 //ITU_R BT 709 luma coeffs
22 #define RLUMcxx . . $ `src/filter/hueshift0r/matrix.h:22:9`! (0.2126)
23 #define GLUMcxx . . $ `src/filter/hueshift0r/matrix.h:23:9`! (0.7152)
24 #define BLUMcxx . . $ `src/filter/hueshift0r/matrix.h:24:9`! (0.0722)
25
26 #define OFFSET_Rcxx . . $ `src/filter/hueshift0r/matrix.h:26:9`! 0
27 #define OFFSET_Gcxx . . $ `src/filter/hueshift0r/matrix.h:27:9`! 1
28 #define OFFSET_Bcxx . . $ `src/filter/hueshift0r/matrix.h:28:9`! 2
29 #define OFFSET_Acxx . . $ `src/filter/hueshift0r/matrix.h:29:9`! 3
30
31 /*
32 * printmat -
33 * print a 4 by 4 matrix
34 */
35 void
36 printmatcxx . . $ printmat(9fb3ef344050b01e).(float mat[4][4])
37 {
38 int x, y;
39
40 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"\n");
41 for(y=0; y<4; y++) {
42 for(x=0; x<4; x++)
43 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"%f ",mat[y][x]);
44 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"\n");
45 }
46 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"\n");
47 }
48
49 /*
50 * applymatrix -
51 * use a matrix to transform colors.
52 */
53 void
54 applymatrixcxx . . $ applymatrix(1b0d74c2e439c20).(unsigned long *lptr,float mat[4][4],int n)
55 {
56 int ir, ig, ib, r, g, b;
57 unsigned char *cptr;
58
59 cptr = (unsigned char *)lptr;
60 while(n--) {
61 ir = cptr[OFFSET_Rcxx . . $ `src/filter/hueshift0r/matrix.h:26:9`!];
62 ig = cptr[OFFSET_Gcxx . . $ `src/filter/hueshift0r/matrix.h:27:9`!];
63 ib = cptr[OFFSET_Bcxx . . $ `src/filter/hueshift0r/matrix.h:28:9`!];
64 r = ir*mat[0][0] + ig*mat[1][0] + ib*mat[2][0] + mat[3][0];
65 g = ir*mat[0][1] + ig*mat[1][1] + ib*mat[2][1] + mat[3][1];
66 b = ir*mat[0][2] + ig*mat[1][2] + ib*mat[2][2] + mat[3][2];
67 cptr[OFFSET_Rcxx . . $ `src/filter/hueshift0r/matrix.h:26:9`!] = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(r);
68 cptr[OFFSET_Gcxx . . $ `src/filter/hueshift0r/matrix.h:27:9`!] = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(g);
69 cptr[OFFSET_Bcxx . . $ `src/filter/hueshift0r/matrix.h:28:9`!] = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(b);
70 cptr += 4;
71 }
72 }
73
74 /*
75 * matrixmult -
76 * multiply two matrices
77 */
78 void
79 matrixmultcxx . . $ matrixmult(893694c8b8ecb2b3).(float a[4][4],float b[4][4],float c[4][4])
80 {
81 int x, y;
82 float temp[4][4];
83
84 for(y=0; y<4 ; y++)
85 for(x=0 ; x<4 ; x++) {
86 temp[y][x] = b[y][0] * a[0][x]
87 + b[y][1] * a[1][x]
88 + b[y][2] * a[2][x]
89 + b[y][3] * a[3][x];
90 }
91 for(y=0; y<4; y++)
92 for(x=0; x<4; x++)
93 c[y][x] = temp[y][x];
94 }
95
96 /*
97 * identmat -
98 * make an identity matrix
99 */
100 void
101 identmatcxx . . $ identmat(e6881f009d13a851).(float *matrix)
102 {
103 *matrix++ = 1.0; /* row 1 */
104 *matrix++ = 0.0;
105 *matrix++ = 0.0;
106 *matrix++ = 0.0;
107 *matrix++ = 0.0; /* row 2 */
108 *matrix++ = 1.0;
109 *matrix++ = 0.0;
110 *matrix++ = 0.0;
111 *matrix++ = 0.0; /* row 3 */
112 *matrix++ = 0.0;
113 *matrix++ = 1.0;
114 *matrix++ = 0.0;
115 *matrix++ = 0.0; /* row 4 */
116 *matrix++ = 0.0;
117 *matrix++ = 0.0;
118 *matrix++ = 1.0;
119 }
120
121 /*
122 * xformpnt -
123 * transform a 3D point using a matrix
124 */
125 void
126 xformpntcxx . . $ xformpnt(416b3968996596ab).(float matrix[4][4],float x,float y,float z,float *tx,float *ty,float *tz)
127 {
128 *tx = x*matrix[0][0] + y*matrix[1][0] + z*matrix[2][0] + matrix[3][0];
129 *ty = x*matrix[0][1] + y*matrix[1][1] + z*matrix[2][1] + matrix[3][1];
130 *tz = x*matrix[0][2] + y*matrix[1][2] + z*matrix[2][2] + matrix[3][2];
131 }
132
133 /*
134 * cscalemat -
135 * make a color scale matrix
136 */
137 void
138 cscalematcxx . . $ cscalemat(2259ce7c40860d6a).(float mat[4][4],float rscale,float gscale,float bscale)
139 {
140 float mmat[4][4];
141
142 mmat[0][0] = rscale;
143 mmat[0][1] = 0.0;
144 mmat[0][2] = 0.0;
145 mmat[0][3] = 0.0;
146
147 mmat[1][0] = 0.0;
148 mmat[1][1] = gscale;
149 mmat[1][2] = 0.0;
150 mmat[1][3] = 0.0;
151
152
153 mmat[2][0] = 0.0;
154 mmat[2][1] = 0.0;
155 mmat[2][2] = bscale;
156 mmat[2][3] = 0.0;
157
158 mmat[3][0] = 0.0;
159 mmat[3][1] = 0.0;
160 mmat[3][2] = 0.0;
161 mmat[3][3] = 1.0;
162 matrixmultcxx . . $ matrixmult(893694c8b8ecb2b3).(mmat,mat,mat);
163 }
164
165 /*
166 * lummat -
167 * make a luminance matrix
168 */
169 void
170 lummatcxx . . $ lummat(9fb3ef344050b01e).(float mat[4][4])
171 {
172 float mmat[4][4];
173 float rwgt, gwgt, bwgt;
174
175 rwgt = RLUMcxx . . $ `src/filter/hueshift0r/matrix.h:22:9`!;
176 gwgt = GLUMcxx . . $ `src/filter/hueshift0r/matrix.h:23:9`!;
177 bwgt = BLUMcxx . . $ `src/filter/hueshift0r/matrix.h:24:9`!;
178 mmat[0][0] = rwgt;
179 mmat[0][1] = rwgt;
180 mmat[0][2] = rwgt;
181 mmat[0][3] = 0.0;
182
183 mmat[1][0] = gwgt;
184 mmat[1][1] = gwgt;
185 mmat[1][2] = gwgt;
186 mmat[1][3] = 0.0;
187
188 mmat[2][0] = bwgt;
189 mmat[2][1] = bwgt;
190 mmat[2][2] = bwgt;
191 mmat[2][3] = 0.0;
192
193 mmat[3][0] = 0.0;
194 mmat[3][1] = 0.0;
195 mmat[3][2] = 0.0;
196 mmat[3][3] = 1.0;
197 matrixmultcxx . . $ matrixmult(893694c8b8ecb2b3).(mmat,mat,mat);
198 }
199
200 /*
201 * saturatemat -
202 * make a saturation matrix
203 */
204 void
205 saturatematcxx . . $ saturatemat(eea47203a9b1114e).(float mat[4][4],float sat)
206 {
207 float mmat[4][4];
208 float a, b, c, d, e, f, g, h, i;
209 float rwgt, gwgt, bwgt;
210
211 rwgt = RLUMcxx . . $ `src/filter/hueshift0r/matrix.h:22:9`!;
212 gwgt = GLUMcxx . . $ `src/filter/hueshift0r/matrix.h:23:9`!;
213 bwgt = BLUMcxx . . $ `src/filter/hueshift0r/matrix.h:24:9`!;
214
215 a = (1.0-sat)*rwgt + sat;
216 b = (1.0-sat)*rwgt;
217 c = (1.0-sat)*rwgt;
218 d = (1.0-sat)*gwgt;
219 e = (1.0-sat)*gwgt + sat;
220 f = (1.0-sat)*gwgt;
221 g = (1.0-sat)*bwgt;
222 h = (1.0-sat)*bwgt;
223 i = (1.0-sat)*bwgt + sat;
224 mmat[0][0] = a;
225 mmat[0][1] = b;
226 mmat[0][2] = c;
227 mmat[0][3] = 0.0;
228
229 mmat[1][0] = d;
230 mmat[1][1] = e;
231 mmat[1][2] = f;
232 mmat[1][3] = 0.0;
233
234 mmat[2][0] = g;
235 mmat[2][1] = h;
236 mmat[2][2] = i;
237 mmat[2][3] = 0.0;
238
239 mmat[3][0] = 0.0;
240 mmat[3][1] = 0.0;
241 mmat[3][2] = 0.0;
242 mmat[3][3] = 1.0;
243 matrixmultcxx . . $ matrixmult(893694c8b8ecb2b3).(mmat,mat,mat);
244 }
245
246 /*
247 * offsetmat -
248 * offset r, g, and b
249 */
250 void
251 offsetmatcxx . . $ offsetmat(2259ce7c40860d6a).(float mat[4][4],float roffset,float goffset,float boffset)
252 {
253 float mmat[4][4];
254
255 mmat[0][0] = 1.0;
256 mmat[0][1] = 0.0;
257 mmat[0][2] = 0.0;
258 mmat[0][3] = 0.0;
259
260 mmat[1][0] = 0.0;
261 mmat[1][1] = 1.0;
262 mmat[1][2] = 0.0;
263 mmat[1][3] = 0.0;
264
265 mmat[2][0] = 0.0;
266 mmat[2][1] = 0.0;
267 mmat[2][2] = 1.0;
268 mmat[2][3] = 0.0;
269
270 mmat[3][0] = roffset;
271 mmat[3][1] = goffset;
272 mmat[3][2] = boffset;
273 mmat[3][3] = 1.0;
274 matrixmultcxx . . $ matrixmult(893694c8b8ecb2b3).(mmat,mat,mat);
275 }
276
277 /*
278 * xrotate -
279 * rotate about the x (red) axis
280 */
281 void
282 xrotatematcxx . . $ xrotatemat(32848217e90ba536).(float mat[4][4],float rs,float rc)
283 {
284 float mmat[4][4];
285
286 mmat[0][0] = 1.0;
287 mmat[0][1] = 0.0;
288 mmat[0][2] = 0.0;
289 mmat[0][3] = 0.0;
290
291 mmat[1][0] = 0.0;
292 mmat[1][1] = rc;
293 mmat[1][2] = rs;
294 mmat[1][3] = 0.0;
295
296 mmat[2][0] = 0.0;
297 mmat[2][1] = -rs;
298 mmat[2][2] = rc;
299 mmat[2][3] = 0.0;
300
301 mmat[3][0] = 0.0;
302 mmat[3][1] = 0.0;
303 mmat[3][2] = 0.0;
304 mmat[3][3] = 1.0;
305 matrixmultcxx . . $ matrixmult(893694c8b8ecb2b3).(mmat,mat,mat);
306 }
307
308 /*
309 * yrotate -
310 * rotate about the y (green) axis
311 */
312 void
313 yrotatematcxx . . $ yrotatemat(32848217e90ba536).(float mat[4][4],float rs,float rc)
314 {
315 float mmat[4][4];
316
317 mmat[0][0] = rc;
318 mmat[0][1] = 0.0;
319 mmat[0][2] = -rs;
320 mmat[0][3] = 0.0;
321
322 mmat[1][0] = 0.0;
323 mmat[1][1] = 1.0;
324 mmat[1][2] = 0.0;
325 mmat[1][3] = 0.0;
326
327 mmat[2][0] = rs;
328 mmat[2][1] = 0.0;
329 mmat[2][2] = rc;
330 mmat[2][3] = 0.0;
331
332 mmat[3][0] = 0.0;
333 mmat[3][1] = 0.0;
334 mmat[3][2] = 0.0;
335 mmat[3][3] = 1.0;
336 matrixmultcxx . . $ matrixmult(893694c8b8ecb2b3).(mmat,mat,mat);
337 }
338
339 /*
340 * zrotate -
341 * rotate about the z (blue) axis
342 */
343 void
344 zrotatematcxx . . $ zrotatemat(32848217e90ba536).(float mat[4][4],float rs,float rc)
345 {
346 float mmat[4][4];
347
348 mmat[0][0] = rc;
349 mmat[0][1] = rs;
350 mmat[0][2] = 0.0;
351 mmat[0][3] = 0.0;
352
353 mmat[1][0] = -rs;
354 mmat[1][1] = rc;
355 mmat[1][2] = 0.0;
356 mmat[1][3] = 0.0;
357
358 mmat[2][0] = 0.0;
359 mmat[2][1] = 0.0;
360 mmat[2][2] = 1.0;
361 mmat[2][3] = 0.0;
362
363 mmat[3][0] = 0.0;
364 mmat[3][1] = 0.0;
365 mmat[3][2] = 0.0;
366 mmat[3][3] = 1.0;
367 matrixmultcxx . . $ matrixmult(893694c8b8ecb2b3).(mmat,mat,mat);
368 }
369
370 /*
371 * zshear -
372 * shear z using x and y.
373 */
374 void
375 zshearmatcxx . . $ zshearmat(32848217e90ba536).(float mat[4][4],float dx,float dy)
376 {
377 float mmat[4][4];
378
379 mmat[0][0] = 1.0;
380 mmat[0][1] = 0.0;
381 mmat[0][2] = dx;
382 mmat[0][3] = 0.0;
383
384 mmat[1][0] = 0.0;
385 mmat[1][1] = 1.0;
386 mmat[1][2] = dy;
387 mmat[1][3] = 0.0;
388
389 mmat[2][0] = 0.0;
390 mmat[2][1] = 0.0;
391 mmat[2][2] = 1.0;
392 mmat[2][3] = 0.0;
393
394 mmat[3][0] = 0.0;
395 mmat[3][1] = 0.0;
396 mmat[3][2] = 0.0;
397 mmat[3][3] = 1.0;
398 matrixmultcxx . . $ matrixmult(893694c8b8ecb2b3).(mmat,mat,mat);
399 }
400
401 /*
402 * simplehuerotatemat -
403 * simple hue rotation. This changes luminance
404 */
405 void
406 simplehuerotatematcxx . . $ simplehuerotatemat(eea47203a9b1114e).(float mat[4][4],float rot)
407 {
408 float mag;
409 float xrs, xrc;
410 float yrs, yrc;
411 float zrs, zrc;
412
413 /* rotate the grey vector into positive Z */
414 mag = sqrt(2.0);
415 xrs = 1.0/mag;
416 xrc = 1.0/mag;
417 xrotatematcxx . . $ xrotatemat(32848217e90ba536).(mat,xrs,xrc);
418
419 mag = sqrt(3.0);
420 yrs = -1.0/mag;
421 yrc = sqrt(2.0)/mag;
422 yrotatematcxx . . $ yrotatemat(32848217e90ba536).(mat,yrs,yrc);
423
424 /* rotate the hue */
425 zrs = sin(rot*M_PIcxx . . $ `<external>/11263917310783833665/math.h:1289:10`!/180.0);
426 zrc = cos(rot*M_PIcxx . . $ `<external>/11263917310783833665/math.h:1289:10`!/180.0);
427 zrotatematcxx . . $ zrotatemat(32848217e90ba536).(mat,zrs,zrc);
428
429 /* rotate the grey vector back into place */
430 yrotatematcxx . . $ yrotatemat(32848217e90ba536).(mat,-yrs,yrc);
431 xrotatematcxx . . $ xrotatemat(32848217e90ba536).(mat,-xrs,xrc);
432 }
433
434 /*
435 * huerotatemat -
436 * rotate the hue, while maintaining luminance.
437 */
438 void
439 huerotatematcxx . . $ huerotatemat(eea47203a9b1114e).(float mat[4][4],float rot)
440 {
441 float mmat[4][4];
442 float mag;
443 float lx, ly, lz;
444 float xrs, xrc;
445 float yrs, yrc;
446 float zrs, zrc;
447 float zsx, zsy;
448
449 identmatcxx . . $ identmat(e6881f009d13a851).((float*)mmat);
450
451 /* rotate the grey vector into positive Z */
452 mag = sqrt(2.0);
453 xrs = 1.0/mag;
454 xrc = 1.0/mag;
455 xrotatematcxx . . $ xrotatemat(32848217e90ba536).(mmat,xrs,xrc);
456 mag = sqrt(3.0);
457 yrs = -1.0/mag;
458 yrc = sqrt(2.0)/mag;
459 yrotatematcxx . . $ yrotatemat(32848217e90ba536).(mmat,yrs,yrc);
460
461 /* shear the space to make the luminance plane horizontal */
462 xformpntcxx . . $ xformpnt(416b3968996596ab).(mmat,RLUMcxx . . $ `src/filter/hueshift0r/matrix.h:22:9`!,GLUMcxx . . $ `src/filter/hueshift0r/matrix.h:23:9`!,BLUMcxx . . $ `src/filter/hueshift0r/matrix.h:24:9`!,&lx,&ly,&lz);
463 zsx = lx/lz;
464 zsy = ly/lz;
465 zshearmatcxx . . $ zshearmat(32848217e90ba536).(mmat,zsx,zsy);
466
467 /* rotate the hue */
468 zrs = sin(rot*M_PIcxx . . $ `<external>/11263917310783833665/math.h:1289:10`!/180.0);
469 zrc = cos(rot*M_PIcxx . . $ `<external>/11263917310783833665/math.h:1289:10`!/180.0);
470 zrotatematcxx . . $ zrotatemat(32848217e90ba536).(mmat,zrs,zrc);
471
472 /* unshear the space to put the luminance plane back */
473 zshearmatcxx . . $ zshearmat(32848217e90ba536).(mmat,-zsx,-zsy);
474
475 /* rotate the grey vector back into place */
476 yrotatematcxx . . $ yrotatemat(32848217e90ba536).(mmat,-yrs,yrc);
477 xrotatematcxx . . $ xrotatemat(32848217e90ba536).(mmat,-xrs,xrc);
478
479 matrixmultcxx . . $ matrixmult(893694c8b8ecb2b3).(mmat,mat,mat);
480 }