-
Notifications
You must be signed in to change notification settings - Fork 1
/
ff_warp.c
282 lines (240 loc) · 6.01 KB
/
ff_warp.c
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
#include "func.h"
#include "parser.h"
/**
*** Cheezy bi-linear interpolation.
*** Need something both faster and better
**/
float interp_nn(float x1, float y1, Var* obj, float ignore)
{
int w = GetX(obj);
int h = GetY(obj);
float ix = floor(x1);
float iy = floor(y1);
if (x1 < 0 || x1 >= w || y1 < 0 || y1 >= h) return (ignore);
return (extract_float(obj, cpos(ix, iy, 0, obj)));
}
float interp_bilinear(float x1, float y1, Var* obj, float ignore)
{
int w = GetX(obj);
int h = GetY(obj);
float ix1, iy1, ix2, iy2, px, py;
float xv;
float a1, a2, a3, a4;
if (x1 < 0 || x1 >= w || y1 < 0 || y1 >= h) return (ignore);
/* pixel centers are assumed to be on steps of 0.5 */
x1 = max(x1 - 0.5, 0);
y1 = max(y1 - 0.5, 0);
ix1 = floor(x1);
iy1 = floor(y1);
ix2 = min(ix1 + 1, w - 1);
iy2 = min(iy1 + 1, h - 1);
px = x1 - ix1;
py = y1 - iy1;
a1 = extract_float(obj, cpos(ix1, iy1, 0, obj));
a2 = extract_float(obj, cpos(ix2, iy1, 0, obj));
a3 = extract_float(obj, cpos(ix1, iy2, 0, obj));
a4 = extract_float(obj, cpos(ix2, iy2, 0, obj));
if (a1 == ignore) {
if (px < 0.5)
return (ignore);
else
a1 = a2;
}
if (a2 == ignore) {
if (px >= 0.5)
return (ignore);
else
a2 = a1;
}
if (a3 == ignore) {
if (px < 0.5)
return (ignore);
else
a3 = a4;
}
if (a4 == ignore) {
if (px >= 0.5)
return (ignore);
else
a4 = a3;
}
if (a1 == ignore) {
if (py < 0.5)
return (ignore);
else {
a1 = a3;
a2 = a4;
}
}
if (a3 == ignore) {
if (py >= 0.5)
return (ignore);
else {
a3 = a1;
a4 = a2;
}
}
xv = (1.0 - py) * (((1.0 - px) * a1) + ((px)*a2)) + (py) * (((1.0 - px) * a3) + ((px)*a4));
return (xv);
}
void kenel_interpolation()
{
}
// compute inverse of 3x3 matrix.
float* m_inverse(float* m)
{
// the inverse is the adjoint divided through the determinant
float* o = calloc(9, sizeof(float));
o[0] = m[4] * m[8] - m[5] * m[7];
o[1] = m[2] * m[7] - m[1] * m[8];
o[2] = m[1] * m[5] - m[2] * m[4];
o[3] = m[5] * m[6] - m[3] * m[8];
o[4] = m[0] * m[8] - m[2] * m[6];
o[5] = m[2] * m[3] - m[0] * m[5];
o[6] = m[3] * m[7] - m[4] * m[6];
o[7] = m[1] * m[6] - m[0] * m[7];
o[8] = m[0] * m[4] - m[1] * m[3];
return (o);
}
float* vxm(float* v, float* m)
{
float out[3];
out[0] = v[0] * m[0] + v[1] * m[3] + v[2] * m[6];
out[1] = v[0] * m[1] + v[1] * m[4] + v[2] * m[7];
out[2] = v[0] * m[2] + v[1] * m[5] + v[2] * m[8];
v[0] = out[0] / out[2];
v[1] = out[1] / out[2];
v[2] = out[2] / out[2];
return (v);
}
float* new_v(float x, float y)
{
float* out = calloc(3, sizeof(float));
out[0] = x;
out[1] = y;
out[2] = 1;
return (out);
}
/*
** This function can take a transformation matrix,
** or it could take some keywords to do the same things:
**
** xtranslate (xpos)
** ytranslate (ypos)
** xscale
** yscale
** xshear
** yshear
** rotate
**
** alternatively, we could just write handler functions to
** generate the M matrix for the user for each of the above operations
**
** This can be run two ways:
** 1) make an output image the same size as the input image and crop
** 2) Make an output image the scaled size of the input image, and
** don't crop.
** To do part 2, we need to find the inverse of M, so we can figure out
** where the input corners end up in the output space, and then fit
** everything to the right limits.
**
** This function will eventually want something more than just my
** cheap bilinear interpolation algorithm too.
*/
Var* ff_warp(vfuncptr func, Var* arg)
{
Var *obj = NULL, *xm = NULL, *oval;
float ignore = FLT_MIN;
int i, j;
float* out;
int x, y, n;
int grow = 0;
float m[9];
float* minverse;
float xmax, xmin, ymax, ymin;
float v[3];
int dsize;
const char* options[] = {"nearest", "bilinear", 0};
char* interp = NULL;
float (*interp_f)(float, float, Var*, float);
Alist alist[6];
alist[0] = make_alist("object", ID_VAL, NULL, &obj);
alist[1] = make_alist("matrix", ID_VAL, NULL, &xm);
alist[2] = make_alist("ignore", DV_FLOAT, NULL, &ignore);
alist[3] = make_alist("grow", DV_INT32, NULL, &grow);
alist[4] = make_alist("interp", ID_ENUM, options, &interp);
alist[5].name = NULL;
if (parse_args(func, arg, alist) == 0) return (NULL);
if (obj == NULL) {
parse_error("%s: No object specified\n", func->name);
return (NULL);
}
if (ignore == FLT_MIN) ignore = -32768;
x = GetX(obj);
y = GetY(obj);
n = V_SIZE(xm)[2];
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++) {
m[i + j * 3] = extract_float(xm, cpos(i, j, 0, xm));
}
}
xmin = ymin = 0;
xmax = x;
ymax = y;
if (grow) {
/* figure out the size of the output array */
float* out;
minverse = m_inverse(m);
out = vxm(new_v(0, 0), minverse);
xmin = out[0];
xmax = out[0];
ymin = out[1];
ymax = out[1];
free(out);
out = vxm(new_v(x, 0), minverse);
xmin = min(xmin, out[0]);
xmax = max(xmax, out[0]);
ymin = min(ymin, out[1]);
ymax = max(ymax, out[1]);
free(out);
out = vxm(new_v(0, y), minverse);
xmin = min(xmin, out[0]);
xmax = max(xmax, out[0]);
ymin = min(ymin, out[1]);
ymax = max(ymax, out[1]);
free(out);
out = vxm(new_v(x, y), minverse);
xmin = min(xmin, out[0]);
xmax = max(xmax, out[0]);
ymin = min(ymin, out[1]);
ymax = max(ymax, out[1]);
free(out);
xmax = ceil(xmax);
xmin = floor(xmin);
ymax = ceil(ymax);
ymin = floor(ymin);
printf("new array corners:\n");
printf(" %fx%f , %fx%f\n", xmin, ymin, xmax, ymax);
}
if (interp == NULL || !strcmp(interp, "nearest")) {
interp_f = interp_nn;
} else if (!strcmp(interp, "bilinear")) {
interp_f = interp_bilinear;
} else {
parse_error("Invalid interpolation function\n");
return (NULL);
}
dsize = (xmax - xmin) * (ymax - ymin);
out = calloc(dsize, sizeof(float));
oval = newVal(BSQ, xmax - xmin, ymax - ymin, 1, DV_FLOAT, out);
for (j = ymin; j < ymax; j++) {
for (i = xmin; i < xmax; i++) {
v[0] = i + 0.5;
v[1] = j + 0.5;
v[2] = 1;
vxm(v, m);
out[cpos((int)(i - xmin), (int)(j - ymin), 0, oval)] = interp_f(v[0], v[1], obj, ignore);
}
}
return (oval);
}