Debian Codegraph @

ccast/filt.c

raw symbols SCIP
1
2 /*
3 * Argyll Color Correction System
4 * ChromCast up filter test code.
5 *
6 * Author: Graeme W. Gill
7 * Date: 28/8/2014
8 *
9 * Copyright 2014 Graeme W. Gill
10 * All rights reserved.
11 *
12 * This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
13 * see the License2.txt file for licencing details.
14 *
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <math.h>
20 #include <sys/types.h>
21 #include <time.h>
22 #include "copyright.h"
23 #include "aconfig.h"
24 #ifndef SALONEINSTLIB
25 #include "numlib.h"
26 #else
27 #include "numsup.h"
28 #endif
29 #include "yajl.h"
30 #include "conv.h"
31 #include "base64.h"
32 #include "ccmdns.h"
33 #include "ccpacket.h"
34 #include "ccmes.h"
35 #include "yajl.h"
36 #include "ccast.h"
37
38 #define DO_WEIGHTING
39 #define SUM_CONSTRAINT
40
41 //#define VERT 1 /* 1 for vertical */
42
43 #ifndef DBL_PI
44 # define DBL_PI 3.1415926535897932384626433832795
45 #endif
46
47 double lanczos3(double wi, double x) {
48 double y;
49
50 x = fabs(1.0 * x/wi);
51 if (x >= 3.0)
52 return 0.0;
53 if (x < 1e-6)
54 return 1.0;
55 y = sin(DBL_PI * x)/(DBL_PI * x) * sin(DBL_PI * x/3.0)/(DBL_PI * x/3.0);
56
57 return y;
58 }
59
60 double lanczos2(double wi, double x) {
61 double y;
62
63 x = fabs(1.0 * x/wi);
64 if (x >= 2.0)
65 return 0.0;
66 if (x < 1e-6)
67 return 1.0;
68 y = sin(DBL_PI * x)/(DBL_PI * x) * sin(DBL_PI * x/2.0)/(DBL_PI * x/2.0);
69
70 return y;
71 }
72
73 double in[2][72] = {
74 { // Horizontal
75 255, 0, 0, 0, 0, 0, 0, 0, 0,
76 255, 0, 0, 0, 0, 0, 0, 0, 0,
77 254, 0, 0, 0, 0, 0, 0, 0, 0,
78 254, 0, 0, 0, 0, 0, 0, 0, 0,
79 253, 0, 0, 0, 0, 0, 0, 0, 0,
80 253, 0, 0, 0, 0, 0, 0, 0, 0,
81 252, 0, 0, 0, 0, 0, 0, 0, 0,
82 252, 0, 0, 0, 0, 0, 0, 0, 0
83 }, { // Vertical slice input target
84 255, 0, 0, 0, 0, 0, 0, 0, 0,
85 255, 0, 0, 0, 0, 0, 0, 0, 0,
86 254, 0, 0, 0, 0, 0, 0, 0, 0,
87 254, 0, 0, 0, 0, 0, 0, 0, 0,
88 253, 0, 0, 0, 0, 0, 0, 0, 0,
89 253, 0, 0, 0, 0, 0, 0, 0, 0,
90 252, 0, 0, 0, 0, 0, 0, 0, 0,
91 252, 0, 0, 0, 0, 0, 0, 0, 0
92 } };
93
94 #ifdef NEVER
95 double out[2][] = {
96 { // Horizontal
97 170, 110, 23, 5, 15, 17, 16, 16, 16, 17, 10, 6, 57,
98 151, 153, 61, 7, 10, 16, 16, 16, 16, 17, 15, 5, 22,
99 107, 169, 109, 23, 5, 15, 17, 16, 16, 16, 17, 10, 6, 57,
100 150, 152, 61, 7, 10, 16, 16, 16, 16, 17, 15, 5, 22,
101 107, 168, 109, 23, 5, 15, 17, 16, 16, 16, 17, 10, 6, 56,
102 149, 151, 61, 7, 10, 16, 16, 16, 16, 17, 15, 6, 22,
103 106, 167, 108, 23, 5, 15, 17, 16, 16, 16, 17, 10, 6, 56,
104 148, 150
105 },
106 { // Vertical slice target output
107 235, 100, 0, 16, 16, 16, 16, 16, 16, 16, 20, 1, 13,
108 191, 194, 19, 0, 20, 16, 16, 16, 16, 16, 16, 16, 0, 95,
109 234, 99, 0, 16, 16, 16, 16, 16, 16, 16, 20, 1, 13,
110 190, 194, 19, 0, 20, 16, 16, 16, 16, 16, 16, 16, 0, 94,
111 233, 99, 0, 16, 16, 16, 16, 16, 16, 16, 20, 1, 13,
112 189, 193, 19, 0, 20, 16, 16, 16, 16, 16, 16, 16, 0, 94,
113 232, 99, 0, 16, 16, 16, 16, 16, 16, 16, 20, 1, 13,
114 188, 192
115 }
116 };
117 #else
118 //#define N1 -9
119 //#define N2 -8
120 #define N1 0
121 #define N2 0
122
123 //#define N3 1
124 #define N4 0
125
126 double out[2][96] = {
127 { // Horozontal
128 170, 110, 23, 5, 15, 17, 16, 16, 16, 17, 10, 6, 57,
129 151, 153, 61, 7, 10, 16, 16, 16, 16, 17, 15, 5, 22,
130 107, 169, 109, 23, 5, 15, 17, 16, 16, 16, 17, 10, 6, 57,
131 150, 152, 61, 7, 10, 16, 16, 16, 16, 17, 15, 5, 22,
132 107, 168, 109, 23, 5, 15, 17, 16, 16, 16, 17, 10, 6, 56,
133 149, 151, 61, 7, 10, 16, 16, 16, 16, 17, 15, 6, 22,
134 106, 167, 108, 23, 5, 15, 17, 16, 16, 16, 17, 10, 6, 56,
135 148, 150
136 },
137 { // Vertical slice target output
138 235, 100, N2, 16, 16, 16, 16, 16, 16, 16, 20, 1, 13,
139 191, 194, 19, N4, 20, 16, 16, 16, 16, 16, 16, 16, N1, 95,
140 234, 99, N2, 16, 16, 16, 16, 16, 16, 16, 20, 1, 13,
141 190, 194, 19, N4, 20, 16, 16, 16, 16, 16, 16, 16, N1, 94,
142 233, 99, N2, 16, 16, 16, 16, 16, 16, 16, 20, 1, 13,
143 189, 193, 19, N4, 20, 16, 16, 16, 16, 16, 16, 16, N1, 94,
144 232, 99, N2, 16, 16, 16, 16, 16, 16, 16, 20, 1, 13,
145 188, 192
146 }
147 };
148 #endif
149
150 // Computed the input to output filters.
151 // There will be two phases, depending on whether
152 // the input pixel has an even or odd address.
153 // Although in this case they seem like they
154 // almost interleave, they are actually mirror
155 // images with offset, so it's easier to keep them
156 // separate, rather than trying to figure the offset out.
157 // The index is the output pixel around the closest one
158 // to the scaled input pixel - ie. floor(in * 1.5 + 0.5)
159 #define FWIDTH 6
160 #define NWIDTH (2 * FWIDTH + 1)
161
162 double filt_v[2][2][NWIDTH];
163 double filt_vx[2][2][NWIDTH]; /* max */
164 double filt_vn[2][2][NWIDTH]; /* min */
165
166 double *filt[2][2] = { { &filt_v[0][0][FWIDTH], &filt_v[0][1][FWIDTH] },
167 { &filt_v[1][0][FWIDTH], &filt_v[1][1][FWIDTH] } };
168
169 double *filtx[2][2] = { { &filt_vx[0][0][FWIDTH], &filt_vx[0][1][FWIDTH] },
170 { &filt_vx[1][0][FWIDTH], &filt_vx[1][1][FWIDTH] } };
171
172 double *filtn[2][2] = { { &filt_vn[0][0][FWIDTH], &filt_vn[0][1][FWIDTH] },
173 { &filt_vn[1][0][FWIDTH], &filt_vn[1][1][FWIDTH] } };
174
175 //int fneg[2] = { -5, -4 }; /* Negative index range (inclusive) */
176 //int fpos[2] = { 5, 3 }; /* Positive index range (inclusive) */
177 int fneg[2][2] = { { -4, -4 }, /* Negative index range (inclusive) */
178 { -4, -4 } };
179 int fpos[2][2] = { { 4, 3 }, /* Positive index range (inclusive) */
180 { 4, 3 } };
181
182 /* Weightings [horiz/vert][phase] */
183 double filtw_v[2][2][NWIDTH] = {
184 /* -6, -5, -4, -3, -2, -1, 0, +1, +2, +3, +4, +5, +6 */
185 // { { 1.0, 1.0, 1.0, 5.0, 3.0, 42.0, 80.0, 43.0, 3.0, 5.0, 1.0, 1.0, 1.0 },
186 // { 1.0, 1.0, 3.0, 4.0, 19.0, 62.0, 62.0, 21.0, 4.0, 3.0, 1.0, 1.0, 1.0 } },
187 { { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 },
188 { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 } },
189 /* -6, -5, -4, -3, -2, -1, 0, +1, +2, +3, +4, +5, +6 */
190 // { { 1.0, 1.0, 1.0, 3.0, 17.0, 36.0, 100.0, 40.0, 15.0, 1.0, 3.0, 1.0, 1.0 },
191 // { 1.0, 1.0, 1.0, 8.0, 2.0, 80.0, 81.0, 2.0, 8.0, 2.0, 1.0, 1.0, 1.0 } }
192 { { 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 4.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0 },
193 { 1.0, 1.0, 1.0, 1.0, 1.0, 3.0, 3.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 } },
194 };
195 double *filtw[2][2] = { { &filtw_v[0][0][FWIDTH], &filtw_v[0][1][FWIDTH] },
196 { &filtw_v[1][0][FWIDTH], &filtw_v[1][1][FWIDTH] } };
197
198 #define MX 2.0
199 #define MN -0.5
200
201 // Compute the filter shapes
202 // vv = 0 for horizontal, 1 for vertical
203 static void compute(int vv) {
204 int ph, ii, i, jj, j;
205 double iv, ov;
206 int fcount[2];
207 int niv = sizeof(in[vv])/sizeof(double);
208 int nov = sizeof(out[vv])/sizeof(double);
209
210 // Clear the filters
211 for (ph = 0; ph < 2; ph++) {
212 for (j = -FWIDTH; j <= FWIDTH; j++) {
213 filt[vv][ph][j] = 0.0;
214 filtx[vv][ph][j] = MX;
215 filtn[vv][ph][j] = MN;
216 }
217 fcount[ph] = 0;
218 }
219
220 // Discover an input value
221 for (ii = 0; ii < niv; ii++) {
222 double rgb[3], ycc[3];
223 double prop, propx, propn;
224
225 if (in[ii] == 0)
226 continue;
227
228 iv = in[vv][ii];
229 rgb[0] = rgb[1] = rgb[2] = iv;
230 ccast2YCbCr(NULL, ycc, rgb);
231 iv = ycc[0];
232
233 ph = ii & 1;
234 jj = (int)floor(ii * 1.5 + 0.5);
235
236 for (j = -FWIDTH; j <= FWIDTH; j++) {
237 int k = jj + j;
238 if (k < 0 || k >= nov)
239 continue;
240 ov = out[vv][k];
241
242 prop = ((ov - 16.0)/219.0)/((iv - 16.0)/219.0);
243 propx = ((ov - 16.0)/219.0)/((iv - 0.5 - 16.0)/219.0);
244 propn = ((ov - 16.0)/219.0)/((iv + 0.5 - 16.0)/219.0);
245
246 if (propx < propn) {
247 double tt = propn;
248 propn = propx;
249 propx = tt;
250 }
251
252 //printf("~1 phase %d, off %d, iv %f ov %f, prop %f\n",ph,j,iv,ov,prop);
253 filt[vv][ph][j] += prop;
254 if (propx < filtx[vv][ph][j])
255 filtx[vv][ph][j] = propx;
256 if (propn > filtn[vv][ph][j])
257 filtn[vv][ph][j] = propn;
258 }
259 fcount[ph]++;
260 }
261
262 // Compute average values
263 for (ph = 0; ph < 2; ph++) {
264 for (j = -FWIDTH; j <= FWIDTH; j++)
265 filt[vv][ph][j] /= (double)fcount[ph];
266 }
267 }
268
269 #define FCO2IX(bank, off) (bank ? fpos[vv][0] - fneg[vv][0] + 1 + off - fneg[vv][1] : off - fneg[vv][0])
270
271 // Compute the filter shapes using SVD
272 // vv = 0 for horizontal, 1 for vertical
273 static void compute2(int vv) {
274 int niv = sizeof(in[vv])/sizeof(double); /* Number of input values */
275 int nov = sizeof(out[vv])/sizeof(double); /* Number of output values */
276 int novextra = 0;
277 int nfc = fpos[vv][0] - fneg[vv][0] + 1
278 + fpos[vv][1] - fneg[vv][1] + 1; /* Number of filter coeficients */
279 double **A, *b;
280 int oe; /* Even or odd */
281 int j, i;
282
283 #ifdef SUM_CONSTRAINT
284 novextra = 3;
285 #endif
286
287 nov += novextra; /* Extra constraint of sum */
288
289 /* We assume nov > nfc */
290 A = dmatrixz(0, nov-1, 0, nfc-1);
291 b = dvectorz(0, nov-1);
292
293 /* For each output value */
294 for (j = 0; j < (nov-novextra); j++) {
295 double ww = 1.0;
296
297 #ifdef DO_WEIGHTING
298 /* Figure out the weighting */
299 for (oe = 0; oe < 2; oe++) {
300 int fix; /* Filter index */
301
302 /* For offset range of filter */
303 for (fix = fneg[vv][oe]; fix <= fpos[vv][oe]; fix++) {
304 int ocx, icx, ph;
305
306 ocx = j - fix; /* Output center of filter */
307 if (ocx < 0 || ocx >= nov)
308 continue; /* Filter would never get applied */
309 if (((2 * ocx) % 3) == 2)
310 continue; /* Would never get applied */
311 icx = (int)floor(ocx / 1.5); /* Input center index for this output */
312 if (icx < 0 || icx >= niv)
313 continue; /* Filter would never get applied */
314 ph = icx & 1; /* Phase of filter */
315 if (ph != oe) /* Not a filter that would appear at this ouput */
316 continue;
317 if (in[vv][icx] >= 200.0)
318 ww = filtw[vv][ph][fix];
319 }
320 }
321 #endif /* DO_WEIGHTING */
322
323 /* For even and odd filters */
324 for (oe = 0; oe < 2; oe++) {
325 int fix; /* Filter index */
326
327 /* For offset range of filter */
328 for (fix = fneg[vv][oe]; fix <= fpos[vv][oe]; fix++) {
329 double rgb[3], ycc[3];
330 int ocx, icx, ph;
331
332 ocx = j - fix; /* Output center of filter */
333 if (ocx < 0 || ocx >= nov)
334 continue; /* Filter would never get applied */
335 if (((2 * ocx) % 3) == 2)
336 continue; /* Would never get applied */
337 icx = (int)floor(ocx / 1.5); /* Input center index for this output */
338 if (icx < 0 || icx >= niv)
339 continue; /* Filter would never get applied */
340 ph = icx & 1; /* Phase of filter */
341 if (ph != oe) /* Not a filter that would appear at this ouput */
342 continue;
343 //printf("j = %d/%d, k = %d/%d, ix = %d/%d\n",j,nov,oe * NWIDTH + FWIDTH + fix,nfc,ix,niv);
344 rgb[0] = rgb[1] = rgb[2] = in[vv][icx];
345 ccast2YCbCr(NULL, ycc, rgb);
346 A[j][FCO2IX(oe, fix)] += ww * ycc[0];
347 //printf("A[%d][%d] = %f\n",j,FCO2IX(oe, fix),A[j][FCO2IX(oe, fix)]);
348 }
349 }
350 b[j] = ww * out[vv][j];
351 //printf("b[%d] = %f\n",j,b[j]);
352 }
353
354 #ifdef SUM_CONSTRAINT
355 /* Add sum constraints */
356 /* For 3 repeating output slots */
357 for (j = nov-novextra; j < nov; j++) {
358 double ww = 10000.0;
359 int jj = j - (nov-novextra);
360
361 b[j] = ww;
362
363 /* For even and odd filters */
364 for (oe = 0; oe < 2; oe++) {
365 int fix; /* Filter index */
366
367 /* For offset range of filter */
368 for (fix = fneg[vv][oe]; fix <= fpos[vv][oe]; fix++) {
369 double rgb[3], ycc[3];
370 int ocx, ocx2, icx, ph;
371
372 ocx = j - fix; /* Output center of filter */
373 ocx2 = 2 * ocx;
374
375 while (ocx2 < 0)
376 ocx2 += 3;
377 while (ocx2 >= 3)
378 ocx2 -= 3;
379
380 if (ocx2 == 2)
381 continue; /* Would never get applied */
382
383 while (ocx < 0)
384 ocx += 3;
385 while (ocx >= 3)
386 ocx -= 3;
387
388 icx = (int)floor(ocx / 1.5); /* Input center index for this output */
389 ph = icx & 1; /* Phase of filter */
390 if (ph != oe) /* Not a filter that would appear at this ouput */
391 continue;
392 A[j][FCO2IX(oe, fix)] = ww;
393 printf("A[%d][%d] = %f\n",j,FCO2IX(oe, fix),A[j][FCO2IX(oe, fix)]);
394 }
395 }
396 }
397 #endif /* SUM_CONSTRAINT */
398
399 /* Solve the equation A.x = b using SVD */
400 /* (The w[] values are thresholded for best accuracy) */
401 /* Return non-zero if no solution found */
402 if (svdsolve(A, b, nov, nfc))
403 error("svdsolve failed");
404
405 /* Print the filter shape */
406 /* and copy to the filter */
407 printf("SVD computed for %s:\n", vv ? "vertical" : "horizontal");
408 for (oe = 0; oe < 2; oe++) {
409 int fix; /* Filter index */
410 double sum = 0.0;
411
412 printf("Phase %d\n",oe);
413 // for (fix = -FWIDTH; fix <= FWIDTH; fix++) {
414 for (fix = fneg[vv][oe]; fix <= fpos[vv][oe]; fix++) {
415 printf(" %d -> %f\n",fix, b[FCO2IX(oe, fix)]);
416 sum += b[FCO2IX(oe, fix)];
417 filt[vv][oe][fix] = b[FCO2IX(oe, fix)];
418 }
419 printf("sum = %f\n",sum);
420 }
421 }
422
423 void check(int vv) {
424 double *chout;
425 int niv = sizeof(in[vv])/sizeof(double); /* Number of input values */
426 int nov = sizeof(out[vv])/sizeof(double); /* Number of output values */
427
428 int range, i, ii, j;
429 double xc, x, iv, tw, w, y;
430 double cout, terr = 0.0;
431
432 printf("~1 nov = %d\n",nov);
433 if ((chout = (double *)malloc(sizeof(double) * nov)) == NULL)
434 error("Malloc failed");
435
436 // Clear the output
437 for (i = 0; i < nov; i++)
438 chout[i] = 0.0;
439
440 // For all the input value
441 for (ii = 0; ii < niv; ii++) {
442 int ph, jj;
443 double rgb[3], ycc[3];
444 double prop;
445
446 iv = in[vv][ii];
447 rgb[0] = rgb[1] = rgb[2] = iv;
448 ccast2YCbCr(NULL, ycc, rgb);
449 iv = ycc[0];
450
451 ph = ii & 1;
452 jj = (int)floor(ii * 1.5 + 0.5);
453
454 for (j = -FWIDTH; j <= FWIDTH; j++) {
455 int k = jj + j;
456 if (k < 0 || k >= nov)
457 continue;
458
459 //if ((jj + j) == 4) printf("[%d] += w %f * iv %f\n",jj + j, filt[ph][j], iv);
460 chout[k] += filt[vv][ph][j] * iv;
461 }
462 }
463
464 for (i = 0; i < nov; i++) {
465 double ov, ee;
466 ov = chout[i];
467 ov = floor(ov + 0.5);
468 #ifdef NEVER
469 if (ov < 0.0)
470 ov = 0.0;
471 else if (ov > 255.0)
472 ov = 255.0;
473 #endif
474 ee = ov - out[vv][i];
475 terr += ee * ee;
476 printf("out %d = %f should be %f err %f\n",i,chout[i],out[vv][i],ee);
477 }
478
479 printf("Total err = %f RMS\n",sqrt(terr));
480 }
481
482 int
483 main(int argc,
484 char *argv[]
485 ) {
486 int ph, vv, j;
487
488 double err;
489 double cp[2]; /* Initial starting point */
490 double s[2]; /* Size of initial search area */
491
492 printf("Hi there\n");
493
494 #ifdef NEVER
495 compute(1);
496
497 // Print filter shape
498 printf("Directly computed:\n");
499 for (ph = 0; ph < 2; ph++) {
500 printf("Phase %d\n",ph);
501 for (j = -FWIDTH; j <= FWIDTH; j++)
502 printf(" %d -> min %f, avg %f, max %f\n",j,filtn[1][ph][j],filt[1][ph][j],filtx[1][ph][j]);
503 }
504 #endif
505
506 for (vv = 0; vv < 2; vv++) {
507 compute2(vv);
508 check(vv);
509 }
510
511 /* Output code to stdout */
512 for (ph = 0; ph < 2; ph++) {
513 fprintf(stderr,"/* Weightings [horiz/vert] */\n");
514 fprintf(stderr,"double filt_v_%s[2][%s_WIDTH] = {\n",ph ? "od" : "ev", ph ? "OD" : "EV");
515
516 for (vv = 0; vv < 2; vv++) {
517 int fix; /* Filter index */
518
519 fprintf(stderr,"{ ");
520 for (fix = fneg[vv][ph]; fix <= fpos[vv][ph]; fix++) {
521 if (fix > fneg[vv][ph])
522 fprintf(stderr,", ");
523 fprintf(stderr,"%f", filt[vv][ph][fix]);
524 }
525 fprintf(stderr," }%s\n",vv == 0 ? "," : "");
526 }
527 fprintf(stderr,"};\n\n");
528 }
529
530 return 0;
531 }
532