forked from lsongdev/node-escpos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.js
156 lines (133 loc) · 3.08 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
148
149
150
151
152
153
154
155
156
'use strict';
const getPixels = require('get-pixels');
/**
* [Image description]
* @param {[type]} pixels [description]
*/
function Image(pixels){
if(!(this instanceof Image))
return new Image(pixels);
this.pixels = pixels;
this.data = [];
function rgb(pixel) {
return {
r: pixel[0],
g: pixel[1],
b: pixel[2],
a: pixel[3]
};
};
var self = this;
for(var i=0;i<this.pixels.data.length;i+=this.size.colors){
this.data.push(rgb(new Array(this.size.colors).fill(0).map(function(_, b){
return self.pixels.data[ i + b ];
})));
};
this.data = this.data.map(function(pixel) {
if (pixel.a == 0) return 0;
var shouldBeWhite = pixel.r > 200 && pixel.g > 200 && pixel.b > 200;
return shouldBeWhite ? 0 : 1;
});
};
/**
* [load description]
* @param {[type]} url [description]
* @param {[type]} type [description]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
Image.load = function(url, type, callback){
if(typeof type == 'function'){
callback = type;
type = null;
}
getPixels(url, type, function(err, pixels){
if(err) return callback(err);
callback(new Image(pixels));
});
};
/**
* [description]
* @return {[type]} [description]
*/
Image.prototype.__defineGetter__('size', function(){
return {
width : this.pixels.shape[0],
height: this.pixels.shape[1],
colors: this.pixels.shape[2],
};
});
/**
* [toBitmap description]
* @param {[type]} density [description]
* @return {[type]} [description]
*/
Image.prototype.toBitmap = function(density) {
density = density || 24;
var ld, result = [];
var x, y, b, l, i;
var c = density / 8;
// n blocks of lines
var n = Math.ceil(this.size.height / density);
for (y = 0; y < n; y++) {
// line data
ld = result[y] = [];
for (x = 0; x < this.size.width; x++) {
for (b = 0; b < density; b++) {
i = x * c + (b >> 3);
if (ld[i] === undefined) {
ld[i] = 0;
}
l = y * density + b;
if (l < this.size.height) {
if (this.data[l * this.size.width + x]) {
ld[i] += (0x80 >> (b & 0x7));
}
}
}
}
}
return {
data: result,
density: density
};
};
/**
* [toRaster description]
* @return {[type]} [description]
*/
Image.prototype.toRaster = function () {
var result = [];
var width = this.size.width;
var height = this.size.height;
var data = this.data;
// n blocks of lines
var n = Math.ceil(width / 8);
var x, y, b, c, i;
for (y = 0; y < height; y++) {
for (x = 0; x < n; x++) {
for (b = 0; b < 8; b++) {
i = x * 8 + b;
if (result[y * n + x] === undefined) {
result[y * n + x] = 0;
}
c = x * 8 + b;
if (c < width) {
if (data[y * width + i]) {
result[y * n + x] += (0x80 >> (b & 0x7));
}
}
}
}
}
return {
data: result,
width: n,
height: height
};
}
/**
* [exports description]
* @type {[type]}
*/
module.exports = Image;