-
Notifications
You must be signed in to change notification settings - Fork 0
/
brailleify.js
65 lines (55 loc) · 1.93 KB
/
brailleify.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
const KERNEL = [0,3,1,4,2,5,6,7];
function brailleify(image, options = {}) {
options.align = options.align === undefined ? true : !!options.align;
options.invert = options.invert === undefined ? false : !!options.invert;
options.threshold = options.threshold === undefined ? 120 : options.threshold;
options.scale = options.scale === undefined ? 4 : options.scale;
var w = image.width,
h = image.height,
a = options.align, // Fix alignment issue with empty braille
inv = options.invert, // Switch the dark/light values
th = +options.threshold, // the maximum value for darkness
kc = +options.scale, // kernel cell width in pixels
ka = kc * kc, // kernel cell area
kw = kc * 2, // kernel width
kh = kc * 4, // kernel height
result = '',
kernel,cell,value,
x,y,kx,ky,ox,oy,cx,cy,pos,i;
for (y = 0; y < h; y += kh) {
for (x = 0; x < w; x += kw) {
kernel = [];
for (ky = 0; ky < kh; ky += kc) {
oy = y + ky;
for (kx = 0; kx < kw; kx += kc) {
ox = x + kx;
cell = [0,0,0];
for (cy = 0; cy < kc; ++cy) {
for (cx = 0; cx < kc; ++cx) {
pos = 4 * ((oy + cy) * w + (ox + cx));
for (i = 0; i < 3; i++) {
cell[i] += ~~image.data[pos+i];
}
}
}
for (i = 0; i < 3; ++i) {
cell[i] = Math.floor(cell[i] / ka);
}
// https://en.wikipedia.org/wiki/Relative_luminance
value = 0.2126 * cell[0] + 0.7152 * cell[1] + 0.0722 * cell[2];
kernel.push(value);
}
}
value = 0;
for (var i of KERNEL) if (kernel[i] < th) value += Math.pow(2,i);
if (inv) value = 0xFF - value;
if (a && value == 0) value = 0x80;
result += String.fromCodePoint(0x2800 + value);
}
result += '\n';
}
return result;
}
if (typeof(window) === 'undefined' && typeof(navigator) === 'undefined' && typeof(module) !== 'undefined') {
module.exports = brailleify;
}