1 | "use strict";
|
2 |
|
3 | let hasSyncZlib = true;
|
4 | let zlib = require("zlib");
|
5 | let inflateSync = require("./sync-inflate");
|
6 | if (!zlib.deflateSync) {
|
7 | hasSyncZlib = false;
|
8 | }
|
9 | let SyncReader = require("./sync-reader");
|
10 | let FilterSync = require("./filter-parse-sync");
|
11 | let Parser = require("./parser");
|
12 | let bitmapper = require("./bitmapper");
|
13 | let formatNormaliser = require("./format-normaliser");
|
14 |
|
15 | module.exports = function (buffer, options) {
|
16 | if (!hasSyncZlib) {
|
17 | throw new Error(
|
18 | "To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0"
|
19 | );
|
20 | }
|
21 |
|
22 | let err;
|
23 | function handleError(_err_) {
|
24 | err = _err_;
|
25 | }
|
26 |
|
27 | let metaData;
|
28 | function handleMetaData(_metaData_) {
|
29 | metaData = _metaData_;
|
30 | }
|
31 |
|
32 | function handleTransColor(transColor) {
|
33 | metaData.transColor = transColor;
|
34 | }
|
35 |
|
36 | function handlePalette(palette) {
|
37 | metaData.palette = palette;
|
38 | }
|
39 |
|
40 | function handleSimpleTransparency() {
|
41 | metaData.alpha = true;
|
42 | }
|
43 |
|
44 | let gamma;
|
45 | function handleGamma(_gamma_) {
|
46 | gamma = _gamma_;
|
47 | }
|
48 |
|
49 | let inflateDataList = [];
|
50 | function handleInflateData(inflatedData) {
|
51 | inflateDataList.push(inflatedData);
|
52 | }
|
53 |
|
54 | let reader = new SyncReader(buffer);
|
55 |
|
56 | let parser = new Parser(options, {
|
57 | read: reader.read.bind(reader),
|
58 | error: handleError,
|
59 | metadata: handleMetaData,
|
60 | gamma: handleGamma,
|
61 | palette: handlePalette,
|
62 | transColor: handleTransColor,
|
63 | inflateData: handleInflateData,
|
64 | simpleTransparency: handleSimpleTransparency,
|
65 | });
|
66 |
|
67 | parser.start();
|
68 | reader.process();
|
69 |
|
70 | if (err) {
|
71 | throw err;
|
72 | }
|
73 |
|
74 |
|
75 | let inflateData = Buffer.concat(inflateDataList);
|
76 | inflateDataList.length = 0;
|
77 |
|
78 | let inflatedData;
|
79 | if (metaData.interlace) {
|
80 | inflatedData = zlib.inflateSync(inflateData);
|
81 | } else {
|
82 | let rowSize =
|
83 | ((metaData.width * metaData.bpp * metaData.depth + 7) >> 3) + 1;
|
84 | let imageSize = rowSize * metaData.height;
|
85 | inflatedData = inflateSync(inflateData, {
|
86 | chunkSize: imageSize,
|
87 | maxLength: imageSize,
|
88 | });
|
89 | }
|
90 | inflateData = null;
|
91 |
|
92 | if (!inflatedData || !inflatedData.length) {
|
93 | throw new Error("bad png - invalid inflate data response");
|
94 | }
|
95 |
|
96 | let unfilteredData = FilterSync.process(inflatedData, metaData);
|
97 | inflateData = null;
|
98 |
|
99 | let bitmapData = bitmapper.dataToBitMap(unfilteredData, metaData);
|
100 | unfilteredData = null;
|
101 |
|
102 | let normalisedBitmapData = formatNormaliser(
|
103 | bitmapData,
|
104 | metaData,
|
105 | options.skipRescale
|
106 | );
|
107 |
|
108 | metaData.data = normalisedBitmapData;
|
109 | metaData.gamma = gamma || 0;
|
110 |
|
111 | return metaData;
|
112 | };
|