-
Notifications
You must be signed in to change notification settings - Fork 96
/
tools.js
62 lines (51 loc) · 1.26 KB
/
tools.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
(function() {
'use strict';
module.exports.binary = function (threshold) {
return function (pixel) {
return pixel > threshold ? 1 : 0;
};
};
// bounding box centering
module.exports.center = function(chunk) {
var size = Math.sqrt(chunk.length),
min = {
x: size,
y: size
},
max = {
x: 0,
y: 0
},
x, y, j, k;
for(y = 0; y < size; y++) {
for(x = 0; x < size; x++) {
if(chunk[size * y + x]) {
if(min.x > x)
min.x = x;
if(min.y > y)
min.y = y;
if(max.x < x)
max.x = x;
if(max.y < y)
max.y = y;
}
}
}
var diff = {
x: Math.floor((size / 2) - (min.x + (max.x - min.x) / 2)),
y: Math.floor((size / 2) - (min.y + (max.y - min.y) / 2))
};
// fill array with size * size zeros
var clone = Array.apply(null, new Array(size * size)).map(Number.prototype.valueOf, 0);
// move character to center
for(y = 0; y < size; y++) {
for(x = 0; x < size; x++) {
j = size * y + x;
k = size * (y + diff.y) + (x + diff.x);
if(chunk[j])
clone[k] = chunk[j];
}
}
return clone;
};
})();