-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrasterman-pics.js
53 lines (46 loc) · 1.76 KB
/
rasterman-pics.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
'use strict';
var fs = require('fs');
var pics = require('pics');
var concat = require('concat-frames');
// register some image codecs
pics.use(require('gif-stream'));
pics.use(require('jpg-stream'));
pics.use(require('png-stream'));
exports.init = function (rasterImage) {
rasterImage.load = function (what, cb) {
var file = fs.createReadStream(what);
var decoder = pics.decode();
var concatter = concat(function (frames) {
var frame = frames[0];
if (frame.colorSpace == 'rgb') {
var tmp = rasterImage.fromRgb(frame.width, frame.height, frame.pixels);
cb(null, tmp);
} else if (frame.colorSpace == 'rgba') {
var tmp = rasterImage.fromRgba(frame.width, frame.height, frame.pixels);
cb(null, tmp);
} else {
cb(new Error('ColorSpace ' + frame.colorSpace + ' is not supported'), null);
}
});
file.pipe(decoder).pipe(concatter);
};
rasterImage.prototype.save = function (where, format, cb) {
var stream = null;
if (typeof (where) === "string") {
stream = fs.createWriteStream(where);
} else if (where instanceof (stream.Writable)) {
stream = where;
}
if (stream !== null) {
var encoder = pics.encode(format, { width: this.width, height: this.height, colorSpace: 'rgba' });
var buf = new Buffer(this.data8);
encoder.write(buf);
encoder.pipe(stream);
encoder.end(null, null, function (err) {
cb(err);
});
} else {
cb(new Error("Cannot write to target of type " + typeof (where)));
}
};
};