UNPKG

1.16 kBJavaScriptView Raw
1"use strict";
2
3let util = require("util");
4let Stream = require("stream");
5let constants = require("./constants");
6let Packer = require("./packer");
7
8let 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});
18util.inherits(PackerAsync, Stream);
19
20PackerAsync.prototype.pack = function (data, width, height, gamma) {
21 // Signature
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 // compress it
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};