-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathencoder.js
61 lines (49 loc) · 1.42 KB
/
encoder.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
var jpeg = require('./build/jpeg');
var Encoder = jpeg.JPEGEncoder;
var util = require('util');
var PixelStream = require('pixel-stream');
function JPEGEncoder(width, height, opts) {
PixelStream.apply(this, arguments);
if (typeof width === 'object')
opts = width;
this.encoder = new Encoder;
this.ended = false;
var self = this;
this.encoder.callback = function(type, ptr, len) {
switch (type) {
case 'data':
self.push(new Buffer(jpeg.HEAPU8.subarray(ptr, ptr + len)));
break;
case 'error':
self.ended = true;
self.encoder.delete();
self.emit('error', new Error(ptr));
break;
}
};
}
util.inherits(JPEGEncoder, PixelStream);
JPEGEncoder.prototype.supportedColorSpaces = ['rgb', 'gray', 'cmyk'];
JPEGEncoder.prototype._start = function(done) {
this.encoder.width = this.format.width;
this.encoder.height = this.format.height;
this.encoder.colorSpace = this.format.colorSpace;
this.encoder.quality = this.format.quality || 100;
done();
};
JPEGEncoder.prototype._writePixels = function(data, done) {
if (!this.ended) {
var buf = data instanceof Uint8Array ? data : new Uint8Array(data);
this.encoder.encode(buf);
}
done();
};
JPEGEncoder.prototype._endFrame = function(done) {
if (!this.ended) {
this.ended = true;
this.encoder.end();
this.encoder.delete();
}
done();
};
module.exports = JPEGEncoder;