forked from mnhrdt/imscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bicubic.c
115 lines (96 loc) · 2.43 KB
/
bicubic.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
#ifndef _BICUBIC_C
#define _BICUBIC_C
#include "getpixel.c"
static float cubic_interpolation(float v[4], float x)
{
return v[1] + 0.5 * x*(v[2] - v[0]
+ x*(2.0*v[0] - 5.0*v[1] + 4.0*v[2] - v[3]
+ x*(3.0*(v[1] - v[2]) + v[3] - v[0])));
}
static float bicubic_interpolation_cell(float p[4][4], float x, float y)
{
float v[4];
v[0] = cubic_interpolation(p[0], y);
v[1] = cubic_interpolation(p[1], y);
v[2] = cubic_interpolation(p[2], y);
v[3] = cubic_interpolation(p[3], y);
return cubic_interpolation(v, x);
}
void bicubic_interpolation(float *result,
float *img, int w, int h, int pd, float x, float y)
{
x -= 1;
y -= 1;
getsample_operator p = getsample_0;
int ix = floor(x);
int iy = floor(y);
for (int l = 0; l < pd; l++) {
float c[4][4];
for (int j = 0; j < 4; j++)
for (int i = 0; i < 4; i++)
c[i][j] = p(img, w, h, pd, ix + i, iy + j, l);
float r = bicubic_interpolation_cell(c, x - ix, y - iy);
result[l] = r;
}
}
void bicubic_interpolation_nans(float *result,
float *img, int w, int h, int pd, float x, float y)
{
x -= 1;
y -= 1;
getsample_operator p = getsample_nan;
int ix = floor(x);
int iy = floor(y);
for (int l = 0; l < pd; l++) {
float c[4][4];
for (int j = 0; j < 4; j++)
for (int i = 0; i < 4; i++)
c[i][j] = p(img, w, h, pd, ix + i, iy + j, l);
float r = bicubic_interpolation_cell(c, x - ix, y - iy);
result[l] = r;
}
}
void bicubic_interpolation_boundary(float *result,
float *img, int w, int h, int pd, float x, float y,
int boundary)
{
x -= 1;
y -= 1;
getsample_operator p;
switch(boundary)
{
default:
case 0: p = getsample_0; break;
case 1: p = getsample_1; break;
case 2: p = getsample_2; break;
case -1: p = getsample_error; break;
}
int ix = floor(x);
int iy = floor(y);
for (int l = 0; l < pd; l++) {
float c[4][4];
for (int j = 0; j < 4; j++)
for (int i = 0; i < 4; i++)
c[i][j] = p(img, w, h, pd, ix + i, iy + j, l);
float r = bicubic_interpolation_cell(c, x - ix, y - iy);
result[l] = r;
}
}
void bicubic_interpolation_boundary2(float *result,
float *img, int w, int h, int pd, float x, float y,
getsample_operator p)
{
x -= 1;
y -= 1;
int ix = floor(x);
int iy = floor(y);
for (int l = 0; l < pd; l++) {
float c[4][4];
for (int j = 0; j < 4; j++)
for (int i = 0; i < 4; i++)
c[i][j] = p(img, w, h, pd, ix + i, iy + j, l);
float r = bicubic_interpolation_cell(c, x - ix, y - iy);
result[l] = r;
}
}
#endif//_BICUBIC_C