@hiroosy/baileys-lite 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -0
- package/WAProto/index.d.ts +15932 -0
- package/WAProto/index.js +115856 -0
- package/engine-requirements.js +11 -0
- package/package.json +27 -0
- package/src/binary/wa-binary-constants.js +1300 -0
- package/src/binary/wa-binary.js +649 -0
- package/src/constants.js +143 -0
- package/src/defaults.js +102 -0
- package/src/foundation/audio-duration.js +530 -0
- package/src/foundation/audio-samples.js +268 -0
- package/src/foundation/boom.js +46 -0
- package/src/foundation/cache.js +142 -0
- package/src/foundation/concurrency.js +87 -0
- package/src/foundation/curve25519.js +221 -0
- package/src/foundation/link-preview.js +320 -0
- package/src/foundation/logger.js +69 -0
- package/src/foundation/long.js +140 -0
- package/src/foundation/protobuf-minimal.js +619 -0
- package/src/foundation/wa-crypto.js +76 -0
- package/src/foundation/zip.js +124 -0
- package/src/index.js +48 -0
- package/src/signal/libsignal.js +1051 -0
- package/src/signal/signal-group.js +487 -0
- package/src/signal/signal-repository.js +646 -0
- package/src/socket/business-communities.js +830 -0
- package/src/socket/chats.js +674 -0
- package/src/socket/groups-newsletter.js +342 -0
- package/src/socket/messages-recv.js +1835 -0
- package/src/socket/messages-send.js +1202 -0
- package/src/socket/socket-core.js +833 -0
- package/src/socket/usync.js +291 -0
- package/src/socket-client/websocket-client.js +124 -0
- package/src/utils/auth-state-core.js +764 -0
- package/src/utils/auth-state-storage.js +316 -0
- package/src/utils/chat-sync.js +1045 -0
- package/src/utils/media.js +841 -0
- package/src/utils/message-compose.js +1609 -0
- package/src/utils/message-processing.js +958 -0
- package/src/utils/wa-protocol-core.js +1283 -0
- package/src/vendor/jpeg-js/LICENSE +25 -0
- package/src/vendor/jpeg-js/decoder.js +1152 -0
- package/src/vendor/jpeg-js/encoder.js +824 -0
- package/src/vendor/pngjs/LICENSE +20 -0
- package/src/vendor/pngjs/bitmapper.js +273 -0
- package/src/vendor/pngjs/bitpacker.js +167 -0
- package/src/vendor/pngjs/constants.js +38 -0
- package/src/vendor/pngjs/crc.js +47 -0
- package/src/vendor/pngjs/filter-pack.js +179 -0
- package/src/vendor/pngjs/filter-parse-sync.js +27 -0
- package/src/vendor/pngjs/filter-parse.js +185 -0
- package/src/vendor/pngjs/format-normaliser.js +101 -0
- package/src/vendor/pngjs/interlace.js +101 -0
- package/src/vendor/pngjs/packer-sync.js +65 -0
- package/src/vendor/pngjs/packer.js +138 -0
- package/src/vendor/pngjs/paeth-predictor.js +22 -0
- package/src/vendor/pngjs/parser-sync.js +121 -0
- package/src/vendor/pngjs/parser.js +299 -0
- package/src/vendor/pngjs/png-sync.js +19 -0
- package/src/vendor/pngjs/sync-inflate.js +174 -0
- package/src/vendor/pngjs/sync-reader.js +54 -0
- package/src/wam/wam-constants.js +22853 -0
- package/src/wam/wam.js +151 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Applies PNG scanline filters during encode, picking the best filter per row by sum-of-abs heuristic.
|
|
3
|
+
* Vendored from `pngjs` 7.0.0 (MIT). Only require()/module.exports converted to import/export. See ./LICENSE.
|
|
4
|
+
* Algorithm body unchanged vs. upstream md5 5e371b5b268f253d3b1e3ffc81c2731b.
|
|
5
|
+
* https://github.com/pngjs/pngjs
|
|
6
|
+
*/
|
|
7
|
+
"use strict";
|
|
8
|
+
|
|
9
|
+
import paethPredictor from './paeth-predictor.js'
|
|
10
|
+
|
|
11
|
+
function filterNone(pxData, pxPos, byteWidth, rawData, rawPos) {
|
|
12
|
+
for (let x = 0; x < byteWidth; x++) {
|
|
13
|
+
rawData[rawPos + x] = pxData[pxPos + x];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function filterSumNone(pxData, pxPos, byteWidth) {
|
|
18
|
+
let sum = 0;
|
|
19
|
+
let length = pxPos + byteWidth;
|
|
20
|
+
|
|
21
|
+
for (let i = pxPos; i < length; i++) {
|
|
22
|
+
sum += Math.abs(pxData[i]);
|
|
23
|
+
}
|
|
24
|
+
return sum;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function filterSub(pxData, pxPos, byteWidth, rawData, rawPos, bpp) {
|
|
28
|
+
for (let x = 0; x < byteWidth; x++) {
|
|
29
|
+
let left = x >= bpp ? pxData[pxPos + x - bpp] : 0;
|
|
30
|
+
let val = pxData[pxPos + x] - left;
|
|
31
|
+
|
|
32
|
+
rawData[rawPos + x] = val;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function filterSumSub(pxData, pxPos, byteWidth, bpp) {
|
|
37
|
+
let sum = 0;
|
|
38
|
+
for (let x = 0; x < byteWidth; x++) {
|
|
39
|
+
let left = x >= bpp ? pxData[pxPos + x - bpp] : 0;
|
|
40
|
+
let val = pxData[pxPos + x] - left;
|
|
41
|
+
|
|
42
|
+
sum += Math.abs(val);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return sum;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function filterUp(pxData, pxPos, byteWidth, rawData, rawPos) {
|
|
49
|
+
for (let x = 0; x < byteWidth; x++) {
|
|
50
|
+
let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0;
|
|
51
|
+
let val = pxData[pxPos + x] - up;
|
|
52
|
+
|
|
53
|
+
rawData[rawPos + x] = val;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function filterSumUp(pxData, pxPos, byteWidth) {
|
|
58
|
+
let sum = 0;
|
|
59
|
+
let length = pxPos + byteWidth;
|
|
60
|
+
for (let x = pxPos; x < length; x++) {
|
|
61
|
+
let up = pxPos > 0 ? pxData[x - byteWidth] : 0;
|
|
62
|
+
let val = pxData[x] - up;
|
|
63
|
+
|
|
64
|
+
sum += Math.abs(val);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return sum;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function filterAvg(pxData, pxPos, byteWidth, rawData, rawPos, bpp) {
|
|
71
|
+
for (let x = 0; x < byteWidth; x++) {
|
|
72
|
+
let left = x >= bpp ? pxData[pxPos + x - bpp] : 0;
|
|
73
|
+
let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0;
|
|
74
|
+
let val = pxData[pxPos + x] - ((left + up) >> 1);
|
|
75
|
+
|
|
76
|
+
rawData[rawPos + x] = val;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function filterSumAvg(pxData, pxPos, byteWidth, bpp) {
|
|
81
|
+
let sum = 0;
|
|
82
|
+
for (let x = 0; x < byteWidth; x++) {
|
|
83
|
+
let left = x >= bpp ? pxData[pxPos + x - bpp] : 0;
|
|
84
|
+
let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0;
|
|
85
|
+
let val = pxData[pxPos + x] - ((left + up) >> 1);
|
|
86
|
+
|
|
87
|
+
sum += Math.abs(val);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return sum;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function filterPaeth(pxData, pxPos, byteWidth, rawData, rawPos, bpp) {
|
|
94
|
+
for (let x = 0; x < byteWidth; x++) {
|
|
95
|
+
let left = x >= bpp ? pxData[pxPos + x - bpp] : 0;
|
|
96
|
+
let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0;
|
|
97
|
+
let upleft =
|
|
98
|
+
pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0;
|
|
99
|
+
let val = pxData[pxPos + x] - paethPredictor(left, up, upleft);
|
|
100
|
+
|
|
101
|
+
rawData[rawPos + x] = val;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function filterSumPaeth(pxData, pxPos, byteWidth, bpp) {
|
|
106
|
+
let sum = 0;
|
|
107
|
+
for (let x = 0; x < byteWidth; x++) {
|
|
108
|
+
let left = x >= bpp ? pxData[pxPos + x - bpp] : 0;
|
|
109
|
+
let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0;
|
|
110
|
+
let upleft =
|
|
111
|
+
pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0;
|
|
112
|
+
let val = pxData[pxPos + x] - paethPredictor(left, up, upleft);
|
|
113
|
+
|
|
114
|
+
sum += Math.abs(val);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return sum;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
let filters = {
|
|
121
|
+
0: filterNone,
|
|
122
|
+
1: filterSub,
|
|
123
|
+
2: filterUp,
|
|
124
|
+
3: filterAvg,
|
|
125
|
+
4: filterPaeth,
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
let filterSums = {
|
|
129
|
+
0: filterSumNone,
|
|
130
|
+
1: filterSumSub,
|
|
131
|
+
2: filterSumUp,
|
|
132
|
+
3: filterSumAvg,
|
|
133
|
+
4: filterSumPaeth,
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const filterData = function (pxData, width, height, options, bpp) {
|
|
137
|
+
let filterTypes;
|
|
138
|
+
if (!("filterType" in options) || options.filterType === -1) {
|
|
139
|
+
filterTypes = [0, 1, 2, 3, 4];
|
|
140
|
+
} else if (typeof options.filterType === "number") {
|
|
141
|
+
filterTypes = [options.filterType];
|
|
142
|
+
} else {
|
|
143
|
+
throw new Error("unrecognised filter types");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (options.bitDepth === 16) {
|
|
147
|
+
bpp *= 2;
|
|
148
|
+
}
|
|
149
|
+
let byteWidth = width * bpp;
|
|
150
|
+
let rawPos = 0;
|
|
151
|
+
let pxPos = 0;
|
|
152
|
+
let rawData = Buffer.alloc((byteWidth + 1) * height);
|
|
153
|
+
|
|
154
|
+
let sel = filterTypes[0];
|
|
155
|
+
|
|
156
|
+
for (let y = 0; y < height; y++) {
|
|
157
|
+
if (filterTypes.length > 1) {
|
|
158
|
+
// find best filter for this line (with lowest sum of values)
|
|
159
|
+
let min = Infinity;
|
|
160
|
+
|
|
161
|
+
for (let i = 0; i < filterTypes.length; i++) {
|
|
162
|
+
let sum = filterSums[filterTypes[i]](pxData, pxPos, byteWidth, bpp);
|
|
163
|
+
if (sum < min) {
|
|
164
|
+
sel = filterTypes[i];
|
|
165
|
+
min = sum;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
rawData[rawPos] = sel;
|
|
171
|
+
rawPos++;
|
|
172
|
+
filters[sel](pxData, pxPos, byteWidth, rawData, rawPos, bpp);
|
|
173
|
+
rawPos += byteWidth;
|
|
174
|
+
pxPos += byteWidth;
|
|
175
|
+
}
|
|
176
|
+
return rawData;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export default filterData;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synchronous driver for filter-parse.js: unfilters a whole IDAT buffer at once.
|
|
3
|
+
* Vendored from `pngjs` 7.0.0 (MIT). Only require()/module.exports converted to import/export. See ./LICENSE.
|
|
4
|
+
* Algorithm body unchanged vs. upstream md5 6b0d6b55fd9979ecf821a282c4310756.
|
|
5
|
+
* https://github.com/pngjs/pngjs
|
|
6
|
+
*/
|
|
7
|
+
"use strict";
|
|
8
|
+
|
|
9
|
+
import SyncReader from './sync-reader.js'
|
|
10
|
+
import Filter from './filter-parse.js'
|
|
11
|
+
|
|
12
|
+
export const process = function (inBuffer, bitmapInfo) {
|
|
13
|
+
let outBuffers = [];
|
|
14
|
+
let reader = new SyncReader(inBuffer);
|
|
15
|
+
let filter = new Filter(bitmapInfo, {
|
|
16
|
+
read: reader.read.bind(reader),
|
|
17
|
+
write: function (bufferPart) {
|
|
18
|
+
outBuffers.push(bufferPart);
|
|
19
|
+
},
|
|
20
|
+
complete: function () {},
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
filter.start();
|
|
24
|
+
reader.process();
|
|
25
|
+
|
|
26
|
+
return Buffer.concat(outBuffers);
|
|
27
|
+
};
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reverses PNG scanline filters (None/Sub/Up/Average/Paeth) during decode.
|
|
3
|
+
* Vendored from `pngjs` 7.0.0 (MIT). Only require()/module.exports converted to import/export. See ./LICENSE.
|
|
4
|
+
* Algorithm body unchanged vs. upstream md5 895c8fa775fd9cab2d41a4e57f1d3948.
|
|
5
|
+
* https://github.com/pngjs/pngjs
|
|
6
|
+
*/
|
|
7
|
+
"use strict";
|
|
8
|
+
|
|
9
|
+
import * as interlaceUtils from './interlace.js'
|
|
10
|
+
import paethPredictor from './paeth-predictor.js'
|
|
11
|
+
|
|
12
|
+
function getByteWidth(width, bpp, depth) {
|
|
13
|
+
let byteWidth = width * bpp;
|
|
14
|
+
if (depth !== 8) {
|
|
15
|
+
byteWidth = Math.ceil(byteWidth / (8 / depth));
|
|
16
|
+
}
|
|
17
|
+
return byteWidth;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let Filter = function (bitmapInfo, dependencies) {
|
|
21
|
+
let width = bitmapInfo.width;
|
|
22
|
+
let height = bitmapInfo.height;
|
|
23
|
+
let interlace = bitmapInfo.interlace;
|
|
24
|
+
let bpp = bitmapInfo.bpp;
|
|
25
|
+
let depth = bitmapInfo.depth;
|
|
26
|
+
|
|
27
|
+
this.read = dependencies.read;
|
|
28
|
+
this.write = dependencies.write;
|
|
29
|
+
this.complete = dependencies.complete;
|
|
30
|
+
|
|
31
|
+
this._imageIndex = 0;
|
|
32
|
+
this._images = [];
|
|
33
|
+
if (interlace) {
|
|
34
|
+
let passes = interlaceUtils.getImagePasses(width, height);
|
|
35
|
+
for (let i = 0; i < passes.length; i++) {
|
|
36
|
+
this._images.push({
|
|
37
|
+
byteWidth: getByteWidth(passes[i].width, bpp, depth),
|
|
38
|
+
height: passes[i].height,
|
|
39
|
+
lineIndex: 0,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
this._images.push({
|
|
44
|
+
byteWidth: getByteWidth(width, bpp, depth),
|
|
45
|
+
height: height,
|
|
46
|
+
lineIndex: 0,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// when filtering the line we look at the pixel to the left
|
|
51
|
+
// the spec also says it is done on a byte level regardless of the number of pixels
|
|
52
|
+
// so if the depth is byte compatible (8 or 16) we subtract the bpp in order to compare back
|
|
53
|
+
// a pixel rather than just a different byte part. However if we are sub byte, we ignore.
|
|
54
|
+
if (depth === 8) {
|
|
55
|
+
this._xComparison = bpp;
|
|
56
|
+
} else if (depth === 16) {
|
|
57
|
+
this._xComparison = bpp * 2;
|
|
58
|
+
} else {
|
|
59
|
+
this._xComparison = 1;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
Filter.prototype.start = function () {
|
|
64
|
+
this.read(
|
|
65
|
+
this._images[this._imageIndex].byteWidth + 1,
|
|
66
|
+
this._reverseFilterLine.bind(this)
|
|
67
|
+
);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
Filter.prototype._unFilterType1 = function (
|
|
71
|
+
rawData,
|
|
72
|
+
unfilteredLine,
|
|
73
|
+
byteWidth
|
|
74
|
+
) {
|
|
75
|
+
let xComparison = this._xComparison;
|
|
76
|
+
let xBiggerThan = xComparison - 1;
|
|
77
|
+
|
|
78
|
+
for (let x = 0; x < byteWidth; x++) {
|
|
79
|
+
let rawByte = rawData[1 + x];
|
|
80
|
+
let f1Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0;
|
|
81
|
+
unfilteredLine[x] = rawByte + f1Left;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
Filter.prototype._unFilterType2 = function (
|
|
86
|
+
rawData,
|
|
87
|
+
unfilteredLine,
|
|
88
|
+
byteWidth
|
|
89
|
+
) {
|
|
90
|
+
let lastLine = this._lastLine;
|
|
91
|
+
|
|
92
|
+
for (let x = 0; x < byteWidth; x++) {
|
|
93
|
+
let rawByte = rawData[1 + x];
|
|
94
|
+
let f2Up = lastLine ? lastLine[x] : 0;
|
|
95
|
+
unfilteredLine[x] = rawByte + f2Up;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
Filter.prototype._unFilterType3 = function (
|
|
100
|
+
rawData,
|
|
101
|
+
unfilteredLine,
|
|
102
|
+
byteWidth
|
|
103
|
+
) {
|
|
104
|
+
let xComparison = this._xComparison;
|
|
105
|
+
let xBiggerThan = xComparison - 1;
|
|
106
|
+
let lastLine = this._lastLine;
|
|
107
|
+
|
|
108
|
+
for (let x = 0; x < byteWidth; x++) {
|
|
109
|
+
let rawByte = rawData[1 + x];
|
|
110
|
+
let f3Up = lastLine ? lastLine[x] : 0;
|
|
111
|
+
let f3Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0;
|
|
112
|
+
let f3Add = Math.floor((f3Left + f3Up) / 2);
|
|
113
|
+
unfilteredLine[x] = rawByte + f3Add;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
Filter.prototype._unFilterType4 = function (
|
|
118
|
+
rawData,
|
|
119
|
+
unfilteredLine,
|
|
120
|
+
byteWidth
|
|
121
|
+
) {
|
|
122
|
+
let xComparison = this._xComparison;
|
|
123
|
+
let xBiggerThan = xComparison - 1;
|
|
124
|
+
let lastLine = this._lastLine;
|
|
125
|
+
|
|
126
|
+
for (let x = 0; x < byteWidth; x++) {
|
|
127
|
+
let rawByte = rawData[1 + x];
|
|
128
|
+
let f4Up = lastLine ? lastLine[x] : 0;
|
|
129
|
+
let f4Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0;
|
|
130
|
+
let f4UpLeft = x > xBiggerThan && lastLine ? lastLine[x - xComparison] : 0;
|
|
131
|
+
let f4Add = paethPredictor(f4Left, f4Up, f4UpLeft);
|
|
132
|
+
unfilteredLine[x] = rawByte + f4Add;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
Filter.prototype._reverseFilterLine = function (rawData) {
|
|
137
|
+
let filter = rawData[0];
|
|
138
|
+
let unfilteredLine;
|
|
139
|
+
let currentImage = this._images[this._imageIndex];
|
|
140
|
+
let byteWidth = currentImage.byteWidth;
|
|
141
|
+
|
|
142
|
+
if (filter === 0) {
|
|
143
|
+
unfilteredLine = rawData.slice(1, byteWidth + 1);
|
|
144
|
+
} else {
|
|
145
|
+
unfilteredLine = Buffer.alloc(byteWidth);
|
|
146
|
+
|
|
147
|
+
switch (filter) {
|
|
148
|
+
case 1:
|
|
149
|
+
this._unFilterType1(rawData, unfilteredLine, byteWidth);
|
|
150
|
+
break;
|
|
151
|
+
case 2:
|
|
152
|
+
this._unFilterType2(rawData, unfilteredLine, byteWidth);
|
|
153
|
+
break;
|
|
154
|
+
case 3:
|
|
155
|
+
this._unFilterType3(rawData, unfilteredLine, byteWidth);
|
|
156
|
+
break;
|
|
157
|
+
case 4:
|
|
158
|
+
this._unFilterType4(rawData, unfilteredLine, byteWidth);
|
|
159
|
+
break;
|
|
160
|
+
default:
|
|
161
|
+
throw new Error("Unrecognised filter type - " + filter);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
this.write(unfilteredLine);
|
|
166
|
+
|
|
167
|
+
currentImage.lineIndex++;
|
|
168
|
+
if (currentImage.lineIndex >= currentImage.height) {
|
|
169
|
+
this._lastLine = null;
|
|
170
|
+
this._imageIndex++;
|
|
171
|
+
currentImage = this._images[this._imageIndex];
|
|
172
|
+
} else {
|
|
173
|
+
this._lastLine = unfilteredLine;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (currentImage) {
|
|
177
|
+
// read, using the byte width that may be from the new current image
|
|
178
|
+
this.read(currentImage.byteWidth + 1, this._reverseFilterLine.bind(this));
|
|
179
|
+
} else {
|
|
180
|
+
this._lastLine = null;
|
|
181
|
+
this.complete();
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
export default Filter;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* De-palettizes, applies tRNS transparency, and rescales <8-bit / 16-bit samples to 8-bit RGBA.
|
|
3
|
+
* Vendored from `pngjs` 7.0.0 (MIT). Only require()/module.exports converted to import/export. See ./LICENSE.
|
|
4
|
+
* Algorithm body unchanged vs. upstream md5 99537bc853caf0ed8677477ac8781e6b.
|
|
5
|
+
* https://github.com/pngjs/pngjs
|
|
6
|
+
*/
|
|
7
|
+
"use strict";
|
|
8
|
+
|
|
9
|
+
function dePalette(indata, outdata, width, height, palette) {
|
|
10
|
+
let pxPos = 0;
|
|
11
|
+
// use values from palette
|
|
12
|
+
for (let y = 0; y < height; y++) {
|
|
13
|
+
for (let x = 0; x < width; x++) {
|
|
14
|
+
let color = palette[indata[pxPos]];
|
|
15
|
+
|
|
16
|
+
if (!color) {
|
|
17
|
+
throw new Error("index " + indata[pxPos] + " not in palette");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
for (let i = 0; i < 4; i++) {
|
|
21
|
+
outdata[pxPos + i] = color[i];
|
|
22
|
+
}
|
|
23
|
+
pxPos += 4;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function replaceTransparentColor(indata, outdata, width, height, transColor) {
|
|
29
|
+
let pxPos = 0;
|
|
30
|
+
for (let y = 0; y < height; y++) {
|
|
31
|
+
for (let x = 0; x < width; x++) {
|
|
32
|
+
let makeTrans = false;
|
|
33
|
+
|
|
34
|
+
if (transColor.length === 1) {
|
|
35
|
+
if (transColor[0] === indata[pxPos]) {
|
|
36
|
+
makeTrans = true;
|
|
37
|
+
}
|
|
38
|
+
} else if (
|
|
39
|
+
transColor[0] === indata[pxPos] &&
|
|
40
|
+
transColor[1] === indata[pxPos + 1] &&
|
|
41
|
+
transColor[2] === indata[pxPos + 2]
|
|
42
|
+
) {
|
|
43
|
+
makeTrans = true;
|
|
44
|
+
}
|
|
45
|
+
if (makeTrans) {
|
|
46
|
+
for (let i = 0; i < 4; i++) {
|
|
47
|
+
outdata[pxPos + i] = 0;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
pxPos += 4;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function scaleDepth(indata, outdata, width, height, depth) {
|
|
56
|
+
let maxOutSample = 255;
|
|
57
|
+
let maxInSample = Math.pow(2, depth) - 1;
|
|
58
|
+
let pxPos = 0;
|
|
59
|
+
|
|
60
|
+
for (let y = 0; y < height; y++) {
|
|
61
|
+
for (let x = 0; x < width; x++) {
|
|
62
|
+
for (let i = 0; i < 4; i++) {
|
|
63
|
+
outdata[pxPos + i] = Math.floor(
|
|
64
|
+
(indata[pxPos + i] * maxOutSample) / maxInSample + 0.5
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
pxPos += 4;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const formatNormaliser = function (indata, imageData, skipRescale = false) {
|
|
73
|
+
let depth = imageData.depth;
|
|
74
|
+
let width = imageData.width;
|
|
75
|
+
let height = imageData.height;
|
|
76
|
+
let colorType = imageData.colorType;
|
|
77
|
+
let transColor = imageData.transColor;
|
|
78
|
+
let palette = imageData.palette;
|
|
79
|
+
|
|
80
|
+
let outdata = indata; // only different for 16 bits
|
|
81
|
+
|
|
82
|
+
if (colorType === 3) {
|
|
83
|
+
// paletted
|
|
84
|
+
dePalette(indata, outdata, width, height, palette);
|
|
85
|
+
} else {
|
|
86
|
+
if (transColor) {
|
|
87
|
+
replaceTransparentColor(indata, outdata, width, height, transColor);
|
|
88
|
+
}
|
|
89
|
+
// if it needs scaling
|
|
90
|
+
if (depth !== 8 && !skipRescale) {
|
|
91
|
+
// if we need to change the buffer size
|
|
92
|
+
if (depth === 16) {
|
|
93
|
+
outdata = Buffer.alloc(width * height * 4);
|
|
94
|
+
}
|
|
95
|
+
scaleDepth(indata, outdata, width, height, depth);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return outdata;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export default formatNormaliser;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adam7 interlacing: pass geometry + per-pixel position iterator.
|
|
3
|
+
* Vendored from `pngjs` 7.0.0 (MIT). Only require()/module.exports converted to import/export. See ./LICENSE.
|
|
4
|
+
* Algorithm body unchanged vs. upstream md5 5b88af15956679350faec79a91ec6c17.
|
|
5
|
+
* https://github.com/pngjs/pngjs
|
|
6
|
+
*/
|
|
7
|
+
"use strict";
|
|
8
|
+
|
|
9
|
+
// Adam 7
|
|
10
|
+
// 0 1 2 3 4 5 6 7
|
|
11
|
+
// 0 x 6 4 6 x 6 4 6
|
|
12
|
+
// 1 7 7 7 7 7 7 7 7
|
|
13
|
+
// 2 5 6 5 6 5 6 5 6
|
|
14
|
+
// 3 7 7 7 7 7 7 7 7
|
|
15
|
+
// 4 3 6 4 6 3 6 4 6
|
|
16
|
+
// 5 7 7 7 7 7 7 7 7
|
|
17
|
+
// 6 5 6 5 6 5 6 5 6
|
|
18
|
+
// 7 7 7 7 7 7 7 7 7
|
|
19
|
+
|
|
20
|
+
let imagePasses = [
|
|
21
|
+
{
|
|
22
|
+
// pass 1 - 1px
|
|
23
|
+
x: [0],
|
|
24
|
+
y: [0],
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
// pass 2 - 1px
|
|
28
|
+
x: [4],
|
|
29
|
+
y: [0],
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
// pass 3 - 2px
|
|
33
|
+
x: [0, 4],
|
|
34
|
+
y: [4],
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
// pass 4 - 4px
|
|
38
|
+
x: [2, 6],
|
|
39
|
+
y: [0, 4],
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
// pass 5 - 8px
|
|
43
|
+
x: [0, 2, 4, 6],
|
|
44
|
+
y: [2, 6],
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
// pass 6 - 16px
|
|
48
|
+
x: [1, 3, 5, 7],
|
|
49
|
+
y: [0, 2, 4, 6],
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
// pass 7 - 32px
|
|
53
|
+
x: [0, 1, 2, 3, 4, 5, 6, 7],
|
|
54
|
+
y: [1, 3, 5, 7],
|
|
55
|
+
},
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
export const getImagePasses = function (width, height) {
|
|
59
|
+
let images = [];
|
|
60
|
+
let xLeftOver = width % 8;
|
|
61
|
+
let yLeftOver = height % 8;
|
|
62
|
+
let xRepeats = (width - xLeftOver) / 8;
|
|
63
|
+
let yRepeats = (height - yLeftOver) / 8;
|
|
64
|
+
for (let i = 0; i < imagePasses.length; i++) {
|
|
65
|
+
let pass = imagePasses[i];
|
|
66
|
+
let passWidth = xRepeats * pass.x.length;
|
|
67
|
+
let passHeight = yRepeats * pass.y.length;
|
|
68
|
+
for (let j = 0; j < pass.x.length; j++) {
|
|
69
|
+
if (pass.x[j] < xLeftOver) {
|
|
70
|
+
passWidth++;
|
|
71
|
+
} else {
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
for (let j = 0; j < pass.y.length; j++) {
|
|
76
|
+
if (pass.y[j] < yLeftOver) {
|
|
77
|
+
passHeight++;
|
|
78
|
+
} else {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (passWidth > 0 && passHeight > 0) {
|
|
83
|
+
images.push({ width: passWidth, height: passHeight, index: i });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return images;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const getInterlaceIterator = function (width) {
|
|
90
|
+
return function (x, y, pass) {
|
|
91
|
+
let outerXLeftOver = x % imagePasses[pass].x.length;
|
|
92
|
+
let outerX =
|
|
93
|
+
((x - outerXLeftOver) / imagePasses[pass].x.length) * 8 +
|
|
94
|
+
imagePasses[pass].x[outerXLeftOver];
|
|
95
|
+
let outerYLeftOver = y % imagePasses[pass].y.length;
|
|
96
|
+
let outerY =
|
|
97
|
+
((y - outerYLeftOver) / imagePasses[pass].y.length) * 8 +
|
|
98
|
+
imagePasses[pass].y[outerYLeftOver];
|
|
99
|
+
return outerX * 4 + outerY * width * 4;
|
|
100
|
+
};
|
|
101
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synchronous whole-buffer PNG encode entry point: signature + IHDR + gAMA + deflated IDAT + IEND.
|
|
3
|
+
* Vendored from `pngjs` 7.0.0 (MIT). Only require()/module.exports converted to import/export
|
|
4
|
+
* (zlib/util/assert/buffer -> node: specifiers). See ./LICENSE.
|
|
5
|
+
* Algorithm body unchanged vs. upstream md5 51da0119926dab0998788987873fc853.
|
|
6
|
+
* https://github.com/pngjs/pngjs
|
|
7
|
+
*/
|
|
8
|
+
"use strict";
|
|
9
|
+
|
|
10
|
+
let hasSyncZlib = true;
|
|
11
|
+
import zlib from 'node:zlib'
|
|
12
|
+
if (!zlib.deflateSync) {
|
|
13
|
+
hasSyncZlib = false;
|
|
14
|
+
}
|
|
15
|
+
import constants from './constants.js'
|
|
16
|
+
import Packer from './packer.js'
|
|
17
|
+
|
|
18
|
+
const packSync = function (metaData, opt) {
|
|
19
|
+
if (!hasSyncZlib) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
"To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0"
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let options = opt || {};
|
|
26
|
+
|
|
27
|
+
let packer = new Packer(options);
|
|
28
|
+
|
|
29
|
+
let chunks = [];
|
|
30
|
+
|
|
31
|
+
// Signature
|
|
32
|
+
chunks.push(Buffer.from(constants.PNG_SIGNATURE));
|
|
33
|
+
|
|
34
|
+
// Header
|
|
35
|
+
chunks.push(packer.packIHDR(metaData.width, metaData.height));
|
|
36
|
+
|
|
37
|
+
if (metaData.gamma) {
|
|
38
|
+
chunks.push(packer.packGAMA(metaData.gamma));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let filteredData = packer.filterData(
|
|
42
|
+
metaData.data,
|
|
43
|
+
metaData.width,
|
|
44
|
+
metaData.height
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
// compress it
|
|
48
|
+
let compressedData = zlib.deflateSync(
|
|
49
|
+
filteredData,
|
|
50
|
+
packer.getDeflateOptions()
|
|
51
|
+
);
|
|
52
|
+
filteredData = null;
|
|
53
|
+
|
|
54
|
+
if (!compressedData || !compressedData.length) {
|
|
55
|
+
throw new Error("bad png - invalid compressed data response");
|
|
56
|
+
}
|
|
57
|
+
chunks.push(packer.packIDAT(compressedData));
|
|
58
|
+
|
|
59
|
+
// End
|
|
60
|
+
chunks.push(packer.packIEND());
|
|
61
|
+
|
|
62
|
+
return Buffer.concat(chunks);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export default packSync;
|