generalized pixel format handling in 3d pipeline
[bootcensus] / src / census / logo.c
1 #include <math.h>
2 #include "cgmath/cgmath.h"
3 #include "logo.h"
4 #include "logodata.h"
5
6 struct cpnode {
7         cgm_vec2 p;
8         struct cpnode *next;
9 };
10
11 #define CPLERP(res, a, b, t) \
12         do { \
13                 (res).x = (a).x + ((b).x - (a).x) * (t); \
14                 (res).y = (a).y + ((b).y - (a).y) * (t); \
15         } while(0)
16
17 static cgm_vec2 *cp;
18 static int numcp;
19
20 int init_logo(const char *fname)
21 {
22         cp = logocp;
23         numcp = LOGO_NUM_CP;
24         return 0;
25 }
26
27 static void eval_seg(float *res, int a, int b, float t)
28 {
29         int prev, next;
30
31         if(numcp == 2) {
32                 res[0] = cp[a].x + (cp[b].x - cp[a].x) * t;
33                 res[1] = cp[a].y + (cp[b].y - cp[a].y) * t;
34                 return;
35         }
36
37         prev = a <= 0 ? a : a - 1;
38         next = b >= numcp - 1 ? b : b + 1;
39
40         res[0] = cgm_bspline(cp[prev].x, cp[a].x, cp[b].x, cp[next].x, t);
41         res[1] = cgm_bspline(cp[prev].y, cp[a].y, cp[b].y, cp[next].y, t);
42 }
43
44 void eval_logo(float *res, float t)
45 {
46         int idx0, idx1;
47         float t0, t1, dt;
48
49         if(!cp || numcp <= 0) {
50                 res[0] = res[1] = 0;
51                 return;
52         }
53         if(numcp == 1) {
54                 res[0] = cp[0].x;
55                 res[1] = cp[0].y;
56                 return;
57         }
58
59         if(t < 0.0f) t = 0.0f;
60         if(t > 1.0f) t = 1.0f;
61
62         idx0 = (int)(t * (numcp - 1));
63         if(idx0 > numcp - 2) idx0 = numcp - 2;
64         idx1 = idx0 + 1;
65
66         dt = 1.0f / (float)(numcp - 1);
67         t0 = (float)idx0 * dt;
68         t1 = (float)idx1 * dt;
69         t = (t - t0) / (t1 - t0);
70
71         eval_seg(res, idx0, idx1, t);
72 }