-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathutils.js
172 lines (144 loc) · 4.4 KB
/
utils.js
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
const clamp = (x, min, max) => Math.min(max, Math.max(x, min));
const mod = (x, n) => ((x % n) + n) % n;
const mimeType = {
'jpg': 'image/jpeg',
'png': 'image/png'
};
const copyPixelNearest = (read, write) => {
const { width, height, data } = read;
const readIndex = (x, y) => 4 * (y * width + x);
return (xFrom, yFrom, to) => {
const nearest = readIndex(
clamp(Math.round(xFrom), 0, width - 1),
clamp(Math.round(yFrom), 0, height - 1)
);
for (let channel = 0; channel < 3; channel++) {
write.data[to + channel] = data[nearest + channel];
}
};
}
const copyPixelBilinear = (read, write) => {
const { width, height, data } = read;
const readIndex = (x, y) => 4 * (y * width + x);
return (xFrom, yFrom, to) => {
const xl = clamp(Math.floor(xFrom), 0, width - 1);
const xr = clamp(Math.ceil(xFrom), 0, width - 1);
const xf = xFrom - xl;
const yl = clamp(Math.floor(yFrom), 0, height - 1);
const yr = clamp(Math.ceil(yFrom), 0, height - 1);
const yf = yFrom - yl;
const p00 = readIndex(xl, yl);
const p10 = readIndex(xr, yl);
const p01 = readIndex(xl, yr);
const p11 = readIndex(xr, yr);
for (let channel = 0; channel < 3; channel++) {
const p0 = data[p00 + channel] * (1 - xf) + data[p10 + channel] * xf;
const p1 = data[p01 + channel] * (1 - xf) + data[p11 + channel] * xf;
write.data[to + channel] = Math.ceil(p0 * (1 - yf) + p1 * yf);
}
};
}
// performs a discrete convolution with a provided kernel
const kernelResample = (read, write, filterSize, kernel) => {
const { width, height, data } = read;
const readIndex = (x, y) => 4 * (y * width + x);
const twoFilterSize = 2 * filterSize;
const xMax = width - 1;
const yMax = height - 1;
const xKernel = new Array(4);
const yKernel = new Array(4);
return (xFrom, yFrom, to) => {
const xl = Math.floor(xFrom);
const yl = Math.floor(yFrom);
const xStart = xl - filterSize + 1;
const yStart = yl - filterSize + 1;
for (let i = 0; i < twoFilterSize; i++) {
xKernel[i] = kernel(xFrom - (xStart + i));
yKernel[i] = kernel(yFrom - (yStart + i));
}
for (let channel = 0; channel < 3; channel++) {
let q = 0;
for (let i = 0; i < twoFilterSize; i++) {
const y = yStart + i;
const yClamped = clamp(y, 0, yMax);
let p = 0;
for (let j = 0; j < twoFilterSize; j++) {
const x = xStart + j;
const index = readIndex(clamp(x, 0, xMax), yClamped);
p += data[index + channel] * xKernel[j];
}
q += p * yKernel[i];
}
write.data[to + channel] = Math.round(q);
}
};
}
const copyPixelBicubic = (read, write) => {
const b = -0.5;
const kernel = x => {
x = Math.abs(x);
const x2 = x * x;
const x3 = x * x * x;
return x <= 1 ?
(b + 2) * x3 - (b + 3) * x2 + 1 :
b * x3 - 5 * b * x2 + 8 * b * x - 4 * b;
};
return kernelResample(read, write, 2, kernel);
}
const copyPixelLanczos = (read, write) => {
const filterSize = 5;
const kernel = x => {
if (x === 0) {
return 1;
}
else {
const xp = Math.PI * x;
return filterSize * Math.sin(xp) * Math.sin(xp / filterSize) / (xp * xp);
}
};
return kernelResample(read, write, filterSize, kernel);
}
const orientations = {
pz: (out, x, y) => {
out.x = -1;
out.y = -x;
out.z = -y;
},
nz: (out, x, y) => {
out.x = 1;
out.y = x;
out.z = -y;
},
px: (out, x, y) => {
out.x = x;
out.y = -1;
out.z = -y;
},
nx: (out, x, y) => {
out.x = -x;
out.y = 1;
out.z = -y;
},
py: (out, x, y) => {
out.x = -y;
out.y = -x;
out.z = 1;
},
ny: (out, x, y) => {
out.x = y;
out.y = -x;
out.z = -1;
}
};
const interpolations = {
linear: copyPixelBilinear,
cubic: copyPixelBicubic,
lanczos: copyPixelLanczos,
nearest: copyPixelNearest
}
module.exports = {
mod,
interpolations,
orientations,
mimeType
}