-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathloader.js
executable file
·49 lines (44 loc) · 1.19 KB
/
loader.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
"use strict";
const error = require("./error");
const Bitmap = require("./types/Bitmap");
const { Jimp } = require('jimp').default || require('jimp');
const Loader = function () {
};
Loader.prototype = {
image: function (image) {
const valid = image instanceof Buffer || typeof image === 'string'
if (!valid) {
var err = error.invalidParameterError(
"image",
"buffer or path to image",
image
);
throw err;
}
return Jimp.read(image);
},
bitmap: function (image, info) {
var {
bitmap: {
data,
width,
height,
}
} = image;
var u8 = new Uint8ClampedArray(data.buffer)
var bitmap = new Bitmap(width, height, info);
var l = data.length,
i,
j,
color;
for (i = 0, j = 0; i < l; i += 4, j++) {
color =
0.2126 * data[i] +
0.7153 * data[i + 1] +
0.0721 * data[i + 2];
bitmap.data[j] = color < 128 ? 1 : 0;
}
return bitmap;
},
};
module.exports = Loader;