]> git.sesse.net Git - mlt/blob - src/modules/videostab/stab/resample.c
do not use lanc_kernels as global var. moved to filter struct
[mlt] / src / modules / videostab / stab / resample.c
1 /*
2  * Video stabilizer
3  *
4  * Copyright (c) 2008 Lenny <leonardo.masoni@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 #include <stdlib.h>
19 #include <math.h>
20
21 #include "resample.h"
22 #include "utils.h"
23
24 rs_ctx *rs_init(int nc, int nr) {
25
26     rs_ctx *rs = (rs_ctx *)malloc(sizeof(rs_ctx));
27
28     rs->tf = (unsigned char *)malloc(nc * nr * 3 * sizeof(unsigned char));
29
30     rs->nc = nc;
31     rs->nr = nr;
32
33     return rs;
34 }
35
36 void rs_resample(int* lanc_kernels,rs_ctx *rs, unsigned char *f, vc *p) {
37
38     int i, x, y, c;
39
40     for (y = 0; y < rs->nr; y ++) {
41
42         int yp = y * rs->nc;
43         int xd = floor(p[y].x);
44
45         int *lk = select_lanc_kernel(lanc_kernels,p[y].x);
46
47         for (x = 0; x < rs->nc; x ++) {
48
49             int pd = (yp + x) * 3;
50             int a[3];
51
52             for (c = 0; c < 3; c ++)
53                 a[c] = 0;
54
55             for (i = -3; i < 5; i ++) {
56
57                 int ps = (yp + clamp(x + xd + i, 0, rs->nc - 1)) * 3;
58
59                 for (c = 0; c < 3; c ++)
60                     a[c] += f[ps + c] * lk[i + 3];
61             }
62
63             for (c = 0; c < 3; c ++)
64                 rs->tf[pd + c] = clamp(a[c] / 1024, 0, 255);
65         }
66     }
67
68     for (y = 0; y < rs->nr; y ++) {
69
70         int yp = y * rs->nc;
71         int yd = floor(p[y].y);
72
73         int *lk = select_lanc_kernel(lanc_kernels,p[y].y);
74
75         for (x = 0; x < rs->nc; x ++) {
76
77             int pd = (yp + x) * 3;
78             int a[3];
79
80             for (c = 0; c < 3; c ++)
81                 a[c] = 0;
82
83             for (i = -3; i < 5; i ++) {
84
85                 int ps = (clamp(y + yd + i, 0, rs->nr - 1) * rs->nc + x) * 3;
86
87                 for (c = 0; c < 3; c ++)
88                     a[c] += rs->tf[ps + c] * lk[i + 3];
89             }
90
91             for (c = 0; c < 3; c ++)
92                 f[pd + c] = clamp(a[c] / 1024, 0, 255);
93         }
94     }
95 }
96
97 void rs_free(rs_ctx *rs) {
98
99     free(rs->tf);
100
101     free(rs);
102 }
103