1 | "use strict";
|
2 |
|
3 | let util = require("util");
|
4 | let Stream = require("stream");
|
5 | let constants = require("./constants");
|
6 | let Packer = require("./packer");
|
7 |
|
8 | let PackerAsync = (module.exports = function (opt) {
|
9 | Stream.call(this);
|
10 |
|
11 | let options = opt || {};
|
12 |
|
13 | this._packer = new Packer(options);
|
14 | this._deflate = this._packer.createDeflate();
|
15 |
|
16 | this.readable = true;
|
17 | });
|
18 | util.inherits(PackerAsync, Stream);
|
19 |
|
20 | PackerAsync.prototype.pack = function (data, width, height, gamma) {
|
21 |
|
22 | this.emit("data", Buffer.from(constants.PNG_SIGNATURE));
|
23 | this.emit("data", this._packer.packIHDR(width, height));
|
24 |
|
25 | if (gamma) {
|
26 | this.emit("data", this._packer.packGAMA(gamma));
|
27 | }
|
28 |
|
29 | let filteredData = this._packer.filterData(data, width, height);
|
30 |
|
31 |
|
32 | this._deflate.on("error", this.emit.bind(this, "error"));
|
33 |
|
34 | this._deflate.on(
|
35 | "data",
|
36 | function (compressedData) {
|
37 | this.emit("data", this._packer.packIDAT(compressedData));
|
38 | }.bind(this)
|
39 | );
|
40 |
|
41 | this._deflate.on(
|
42 | "end",
|
43 | function () {
|
44 | this.emit("data", this._packer.packIEND());
|
45 | this.emit("end");
|
46 | }.bind(this)
|
47 | );
|
48 |
|
49 | this._deflate.end(filteredData);
|
50 | };
|