forked from samsonmking/epaper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.js
147 lines (140 loc) · 5.29 KB
/
image.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
const PNGReader = require('png.js');
const sharp = require('sharp');
// https://www.w3.org/TR/AERT/#color-contrast
const getLuma = (r, g, b) => r * 0.299 + g * 0.587 + b * 0.114;
const allocBuffer_8 = (devWidth, devHeight) =>
Buffer.alloc(Math.ceil(devWidth / 8) * devHeight, 0xff);
const allocBuffer_4 = (devWidth, devHeight) =>
Buffer.alloc(Math.ceil(devWidth / 4) * devHeight, 0xff);
function convertPNGto1BitBW(pngBytes) {
const reader = new PNGReader(pngBytes);
return new Promise((resolve, reject) => {
reader.parse((err, png) => {
if (err) {
return reject(err);
}
const height = png.getHeight();
const width = png.getWidth();
const outBuffer = allocBuffer_8(width, height);
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const [r, g, b, alpha] = png.getPixel(x, y);
const luma = getLuma(r, g, b);
if (luma < 50) {
out_index = Math.floor((x + y * width) / 8);
outBuffer[out_index] &= ~(0x80 >> Math.floor(x % 8));
}
}
}
resolve(outBuffer);
});
});
}
function convertPNGto1BitBWRotated(pngBytes) {
const reader = new PNGReader(pngBytes);
return new Promise((resolve, reject) => {
reader.parse((err, png) => {
if (err) {
return reject(err);
}
const height = png.getHeight();
const width = png.getWidth();
const devHeight = width;
const devWidth = height;
const outBuffer = allocBuffer_8(devWidth, devHeight);
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const outX = y;
const outY = devHeight - x - 1;
const [r, g, b, alpha] = png.getPixel(x, y);
const luma = getLuma(r, g, b);
if (luma < 50) {
out_index = Math.floor((outX + outY * devWidth) / 8);
outBuffer[out_index] &= ~(0x80 >> Math.floor(y % 8));
}
}
}
resolve(outBuffer);
});
});
}
async function convertPNGto4Grey(pngBytes) {
const pngBytes_L = await sharp(pngBytes).greyscale().png().toBuffer();
const reader = new PNGReader(pngBytes_L);
return new Promise((resolve, reject) => {
reader.parse((err, png) => {
if (err) {
return reject(err);
}
const height = png.getHeight();
const width = png.getWidth();
const outBuffer = allocBuffer_4(width, height);
let i = 0;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
if (++i % 4 == 0) {
out_index = Math.floor((x + y * width) / 4);
outBuffer[out_index] =
(getGrayPixel(png.getPixel(x - 3, y)) & 0xc0) |
((getGrayPixel(png.getPixel(x - 2, y)) & 0xc0) >>
2) |
((getGrayPixel(png.getPixel(x - 1, y)) & 0xc0) >>
4) |
((getGrayPixel(png.getPixel(x, y)) & 0xc0) >> 6);
}
}
}
resolve(outBuffer);
});
});
}
async function convertPNGto4GreyRotated(pngBytes) {
const pngBytes_L = await sharp(pngBytes).greyscale().png().toBuffer();
const reader = new PNGReader(pngBytes_L);
return new Promise((resolve, reject) => {
reader.parse((err, png) => {
if (err) {
return reject(err);
}
const height = png.getHeight();
const width = png.getWidth();
const devHeight = width;
const devWidth = height;
const outBuffer = allocBuffer_4(devWidth, devHeight);
let i = 0;
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
const outX = y;
const outY = devHeight - x - 1;
if (++i % 4 == 0) {
out_index = Math.floor((outX + outY * devWidth) / 4);
outBuffer[out_index] =
(getGrayPixel(png.getPixel(x, y - 3)) & 0xc0) |
((getGrayPixel(png.getPixel(x, y - 2)) & 0xc0) >>
2) |
((getGrayPixel(png.getPixel(x, y - 1)) & 0xc0) >>
4) |
((getGrayPixel(png.getPixel(x, y)) & 0xc0) >> 6);
}
}
}
resolve(outBuffer);
});
});
}
function getGrayPixel(rgba) {
// In a grayscale image: r, g, b are all set to the same value and a == 255
[pixel] = rgba;
if (pixel === 0xc0) {
pixel = 0x80;
} else if (pixel === 0x80) {
pixel = 0x40;
}
return pixel;
}
module.exports = {
convertPNGto1BitBW,
convertPNGto1BitBWRotated,
convertPNGto4Grey,
convertPNGto4GreyRotated,
};