-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
48 lines (43 loc) · 1.13 KB
/
main.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
#!/usr/bin/env node
var jpeg = require('./build/Release/pixelr')
, png = require('pngjs').PNG
, fs = require('fs');
var image
, width
, height;
function read(filename, type, cb) {
image = {'pixels': [], 'width': 0, 'height': 0};
if (type === 'png') {
readPng(filename, cb);
}
else if (type === 'jpeg') {
jpeg.read(filename, function(err, image) {
if (err) cb(err);
cb(null, image);
});
}
else {
console.log('Unidentifiable image format: ' + type);
}
}
function readPng(filename, cb) {
fs.createReadStream(filename)
.pipe(new png({
filterType: 4
}))
.on('parsed', function() {
image['width'] = this.width;
image['height'] = this.height;
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
var idx = (this.width * y + x) << 2;
image['pixels'][idx] = this.data[idx];
image['pixels'][idx+1] = this.data[idx+1];
image['pixels'][idx+2] = this.data[idx+2];
image['pixels'][idx+3] = this.data[idx+3];
}
}
cb(null, image);
});
}
exports.read = read;