1 | "use strict";
|
2 |
|
3 | let util = require("util");
|
4 | let zlib = require("zlib");
|
5 | let ChunkStream = require("./chunkstream");
|
6 | let FilterAsync = require("./filter-parse-async");
|
7 | let Parser = require("./parser");
|
8 | let bitmapper = require("./bitmapper");
|
9 | let formatNormaliser = require("./format-normaliser");
|
10 |
|
11 | let ParserAsync = (module.exports = function (options) {
|
12 | ChunkStream.call(this);
|
13 |
|
14 | this._parser = new Parser(options, {
|
15 | read: this.read.bind(this),
|
16 | error: this._handleError.bind(this),
|
17 | metadata: this._handleMetaData.bind(this),
|
18 | gamma: this.emit.bind(this, "gamma"),
|
19 | palette: this._handlePalette.bind(this),
|
20 | transColor: this._handleTransColor.bind(this),
|
21 | finished: this._finished.bind(this),
|
22 | inflateData: this._inflateData.bind(this),
|
23 | simpleTransparency: this._simpleTransparency.bind(this),
|
24 | headersFinished: this._headersFinished.bind(this),
|
25 | });
|
26 | this._options = options;
|
27 | this.writable = true;
|
28 |
|
29 | this._parser.start();
|
30 | });
|
31 | util.inherits(ParserAsync, ChunkStream);
|
32 |
|
33 | ParserAsync.prototype._handleError = function (err) {
|
34 | this.emit("error", err);
|
35 |
|
36 | this.writable = false;
|
37 |
|
38 | this.destroy();
|
39 |
|
40 | if (this._inflate && this._inflate.destroy) {
|
41 | this._inflate.destroy();
|
42 | }
|
43 |
|
44 | if (this._filter) {
|
45 | this._filter.destroy();
|
46 |
|
47 |
|
48 |
|
49 | this._filter.on("error", function () {});
|
50 | }
|
51 |
|
52 | this.errord = true;
|
53 | };
|
54 |
|
55 | ParserAsync.prototype._inflateData = function (data) {
|
56 | if (!this._inflate) {
|
57 | if (this._bitmapInfo.interlace) {
|
58 | this._inflate = zlib.createInflate();
|
59 |
|
60 | this._inflate.on("error", this.emit.bind(this, "error"));
|
61 | this._filter.on("complete", this._complete.bind(this));
|
62 |
|
63 | this._inflate.pipe(this._filter);
|
64 | } else {
|
65 | let rowSize =
|
66 | ((this._bitmapInfo.width *
|
67 | this._bitmapInfo.bpp *
|
68 | this._bitmapInfo.depth +
|
69 | 7) >>
|
70 | 3) +
|
71 | 1;
|
72 | let imageSize = rowSize * this._bitmapInfo.height;
|
73 | let chunkSize = Math.max(imageSize, zlib.Z_MIN_CHUNK);
|
74 |
|
75 | this._inflate = zlib.createInflate({ chunkSize: chunkSize });
|
76 | let leftToInflate = imageSize;
|
77 |
|
78 | let emitError = this.emit.bind(this, "error");
|
79 | this._inflate.on("error", function (err) {
|
80 | if (!leftToInflate) {
|
81 | return;
|
82 | }
|
83 |
|
84 | emitError(err);
|
85 | });
|
86 | this._filter.on("complete", this._complete.bind(this));
|
87 |
|
88 | let filterWrite = this._filter.write.bind(this._filter);
|
89 | this._inflate.on("data", function (chunk) {
|
90 | if (!leftToInflate) {
|
91 | return;
|
92 | }
|
93 |
|
94 | if (chunk.length > leftToInflate) {
|
95 | chunk = chunk.slice(0, leftToInflate);
|
96 | }
|
97 |
|
98 | leftToInflate -= chunk.length;
|
99 |
|
100 | filterWrite(chunk);
|
101 | });
|
102 |
|
103 | this._inflate.on("end", this._filter.end.bind(this._filter));
|
104 | }
|
105 | }
|
106 | this._inflate.write(data);
|
107 | };
|
108 |
|
109 | ParserAsync.prototype._handleMetaData = function (metaData) {
|
110 | this._metaData = metaData;
|
111 | this._bitmapInfo = Object.create(metaData);
|
112 |
|
113 | this._filter = new FilterAsync(this._bitmapInfo);
|
114 | };
|
115 |
|
116 | ParserAsync.prototype._handleTransColor = function (transColor) {
|
117 | this._bitmapInfo.transColor = transColor;
|
118 | };
|
119 |
|
120 | ParserAsync.prototype._handlePalette = function (palette) {
|
121 | this._bitmapInfo.palette = palette;
|
122 | };
|
123 |
|
124 | ParserAsync.prototype._simpleTransparency = function () {
|
125 | this._metaData.alpha = true;
|
126 | };
|
127 |
|
128 | ParserAsync.prototype._headersFinished = function () {
|
129 |
|
130 |
|
131 | this.emit("metadata", this._metaData);
|
132 | };
|
133 |
|
134 | ParserAsync.prototype._finished = function () {
|
135 | if (this.errord) {
|
136 | return;
|
137 | }
|
138 |
|
139 | if (!this._inflate) {
|
140 | this.emit("error", "No Inflate block");
|
141 | } else {
|
142 |
|
143 | this._inflate.end();
|
144 | }
|
145 | };
|
146 |
|
147 | ParserAsync.prototype._complete = function (filteredData) {
|
148 | if (this.errord) {
|
149 | return;
|
150 | }
|
151 |
|
152 | let normalisedBitmapData;
|
153 |
|
154 | try {
|
155 | let bitmapData = bitmapper.dataToBitMap(filteredData, this._bitmapInfo);
|
156 |
|
157 | normalisedBitmapData = formatNormaliser(
|
158 | bitmapData,
|
159 | this._bitmapInfo,
|
160 | this._options.skipRescale
|
161 | );
|
162 | bitmapData = null;
|
163 | } catch (ex) {
|
164 | this._handleError(ex);
|
165 | return;
|
166 | }
|
167 |
|
168 | this.emit("parsed", normalisedBitmapData);
|
169 | };
|