-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathimageSmoother.js
More file actions
60 lines (55 loc) · 1.08 KB
/
imageSmoother.js
File metadata and controls
60 lines (55 loc) · 1.08 KB
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
/**
* @param {number[][]} M
* @return {number[][]}
*/
var imageSmoother = function(M) {
let result = [];
for (let i = 0; i < M.length; i++) {
result.push([]);
}
for (let y = 0; y < M.length; y++) {
for (let x = 0; x < M[0].length; x++) {
result[y][x] = getAverage(x, y, M);
}
}
return result;
};
function getAverage(x, y, M) {
let count = 0;
let total = 0;
if (M[y-1] !== undefined) {
if (M[y-1][x-1] !== undefined) {
count++;
total += M[y-1][x-1];
}
if (M[y-1][x+1] !== undefined) {
count++;
total += M[y-1][x+1];
}
count++;
total += M[y-1][x];
}
if (M[y+1] !== undefined) {
if (M[y+1][x-1] !== undefined) {
count++;
total += M[y+1][x-1];
}
if (M[y+1][x+1] !== undefined) {
count++;
total += M[y+1][x+1];
}
count++;
total += M[y+1][x];
}
if (M[y][x-1] !== undefined) {
count++;
total += M[y][x-1];
}
if (M[y][x+1] !== undefined) {
count++;
total += M[y][x+1];
}
count++;
total += M[y][x]
return Math.floor(total / count);
}