@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,20 @@
|
|
|
1
|
+
pngjs original work Copyright (c) 2015 Luke Page & Original Contributors
|
|
2
|
+
pngjs derived work Copyright (c) 2012 Kuba Niegowski
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in
|
|
12
|
+
all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Raw scanline bytes -> flat RGBA pixel buffer (all bit depths, palette handled upstream).
|
|
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 682312e25be1cfee14b102e855adbeea.
|
|
5
|
+
* https://github.com/pngjs/pngjs
|
|
6
|
+
*/
|
|
7
|
+
"use strict";
|
|
8
|
+
|
|
9
|
+
import * as interlaceUtils from './interlace.js'
|
|
10
|
+
|
|
11
|
+
let pixelBppMapper = [
|
|
12
|
+
// 0 - dummy entry
|
|
13
|
+
function () {},
|
|
14
|
+
|
|
15
|
+
// 1 - L
|
|
16
|
+
// 0: 0, 1: 0, 2: 0, 3: 0xff
|
|
17
|
+
function (pxData, data, pxPos, rawPos) {
|
|
18
|
+
if (rawPos === data.length) {
|
|
19
|
+
throw new Error("Ran out of data");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let pixel = data[rawPos];
|
|
23
|
+
pxData[pxPos] = pixel;
|
|
24
|
+
pxData[pxPos + 1] = pixel;
|
|
25
|
+
pxData[pxPos + 2] = pixel;
|
|
26
|
+
pxData[pxPos + 3] = 0xff;
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
// 2 - LA
|
|
30
|
+
// 0: 0, 1: 0, 2: 0, 3: 1
|
|
31
|
+
function (pxData, data, pxPos, rawPos) {
|
|
32
|
+
if (rawPos + 1 >= data.length) {
|
|
33
|
+
throw new Error("Ran out of data");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let pixel = data[rawPos];
|
|
37
|
+
pxData[pxPos] = pixel;
|
|
38
|
+
pxData[pxPos + 1] = pixel;
|
|
39
|
+
pxData[pxPos + 2] = pixel;
|
|
40
|
+
pxData[pxPos + 3] = data[rawPos + 1];
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
// 3 - RGB
|
|
44
|
+
// 0: 0, 1: 1, 2: 2, 3: 0xff
|
|
45
|
+
function (pxData, data, pxPos, rawPos) {
|
|
46
|
+
if (rawPos + 2 >= data.length) {
|
|
47
|
+
throw new Error("Ran out of data");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
pxData[pxPos] = data[rawPos];
|
|
51
|
+
pxData[pxPos + 1] = data[rawPos + 1];
|
|
52
|
+
pxData[pxPos + 2] = data[rawPos + 2];
|
|
53
|
+
pxData[pxPos + 3] = 0xff;
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
// 4 - RGBA
|
|
57
|
+
// 0: 0, 1: 1, 2: 2, 3: 3
|
|
58
|
+
function (pxData, data, pxPos, rawPos) {
|
|
59
|
+
if (rawPos + 3 >= data.length) {
|
|
60
|
+
throw new Error("Ran out of data");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
pxData[pxPos] = data[rawPos];
|
|
64
|
+
pxData[pxPos + 1] = data[rawPos + 1];
|
|
65
|
+
pxData[pxPos + 2] = data[rawPos + 2];
|
|
66
|
+
pxData[pxPos + 3] = data[rawPos + 3];
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
let pixelBppCustomMapper = [
|
|
71
|
+
// 0 - dummy entry
|
|
72
|
+
function () {},
|
|
73
|
+
|
|
74
|
+
// 1 - L
|
|
75
|
+
// 0: 0, 1: 0, 2: 0, 3: 0xff
|
|
76
|
+
function (pxData, pixelData, pxPos, maxBit) {
|
|
77
|
+
let pixel = pixelData[0];
|
|
78
|
+
pxData[pxPos] = pixel;
|
|
79
|
+
pxData[pxPos + 1] = pixel;
|
|
80
|
+
pxData[pxPos + 2] = pixel;
|
|
81
|
+
pxData[pxPos + 3] = maxBit;
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
// 2 - LA
|
|
85
|
+
// 0: 0, 1: 0, 2: 0, 3: 1
|
|
86
|
+
function (pxData, pixelData, pxPos) {
|
|
87
|
+
let pixel = pixelData[0];
|
|
88
|
+
pxData[pxPos] = pixel;
|
|
89
|
+
pxData[pxPos + 1] = pixel;
|
|
90
|
+
pxData[pxPos + 2] = pixel;
|
|
91
|
+
pxData[pxPos + 3] = pixelData[1];
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
// 3 - RGB
|
|
95
|
+
// 0: 0, 1: 1, 2: 2, 3: 0xff
|
|
96
|
+
function (pxData, pixelData, pxPos, maxBit) {
|
|
97
|
+
pxData[pxPos] = pixelData[0];
|
|
98
|
+
pxData[pxPos + 1] = pixelData[1];
|
|
99
|
+
pxData[pxPos + 2] = pixelData[2];
|
|
100
|
+
pxData[pxPos + 3] = maxBit;
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
// 4 - RGBA
|
|
104
|
+
// 0: 0, 1: 1, 2: 2, 3: 3
|
|
105
|
+
function (pxData, pixelData, pxPos) {
|
|
106
|
+
pxData[pxPos] = pixelData[0];
|
|
107
|
+
pxData[pxPos + 1] = pixelData[1];
|
|
108
|
+
pxData[pxPos + 2] = pixelData[2];
|
|
109
|
+
pxData[pxPos + 3] = pixelData[3];
|
|
110
|
+
},
|
|
111
|
+
];
|
|
112
|
+
|
|
113
|
+
function bitRetriever(data, depth) {
|
|
114
|
+
let leftOver = [];
|
|
115
|
+
let i = 0;
|
|
116
|
+
|
|
117
|
+
function split() {
|
|
118
|
+
if (i === data.length) {
|
|
119
|
+
throw new Error("Ran out of data");
|
|
120
|
+
}
|
|
121
|
+
let byte = data[i];
|
|
122
|
+
i++;
|
|
123
|
+
let byte8, byte7, byte6, byte5, byte4, byte3, byte2, byte1;
|
|
124
|
+
switch (depth) {
|
|
125
|
+
default:
|
|
126
|
+
throw new Error("unrecognised depth");
|
|
127
|
+
case 16:
|
|
128
|
+
byte2 = data[i];
|
|
129
|
+
i++;
|
|
130
|
+
leftOver.push((byte << 8) + byte2);
|
|
131
|
+
break;
|
|
132
|
+
case 4:
|
|
133
|
+
byte2 = byte & 0x0f;
|
|
134
|
+
byte1 = byte >> 4;
|
|
135
|
+
leftOver.push(byte1, byte2);
|
|
136
|
+
break;
|
|
137
|
+
case 2:
|
|
138
|
+
byte4 = byte & 3;
|
|
139
|
+
byte3 = (byte >> 2) & 3;
|
|
140
|
+
byte2 = (byte >> 4) & 3;
|
|
141
|
+
byte1 = (byte >> 6) & 3;
|
|
142
|
+
leftOver.push(byte1, byte2, byte3, byte4);
|
|
143
|
+
break;
|
|
144
|
+
case 1:
|
|
145
|
+
byte8 = byte & 1;
|
|
146
|
+
byte7 = (byte >> 1) & 1;
|
|
147
|
+
byte6 = (byte >> 2) & 1;
|
|
148
|
+
byte5 = (byte >> 3) & 1;
|
|
149
|
+
byte4 = (byte >> 4) & 1;
|
|
150
|
+
byte3 = (byte >> 5) & 1;
|
|
151
|
+
byte2 = (byte >> 6) & 1;
|
|
152
|
+
byte1 = (byte >> 7) & 1;
|
|
153
|
+
leftOver.push(byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
get: function (count) {
|
|
160
|
+
while (leftOver.length < count) {
|
|
161
|
+
split();
|
|
162
|
+
}
|
|
163
|
+
let returner = leftOver.slice(0, count);
|
|
164
|
+
leftOver = leftOver.slice(count);
|
|
165
|
+
return returner;
|
|
166
|
+
},
|
|
167
|
+
resetAfterLine: function () {
|
|
168
|
+
leftOver.length = 0;
|
|
169
|
+
},
|
|
170
|
+
end: function () {
|
|
171
|
+
if (i !== data.length) {
|
|
172
|
+
throw new Error("extra data found");
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function mapImage8Bit(image, pxData, getPxPos, bpp, data, rawPos) {
|
|
179
|
+
// eslint-disable-line max-params
|
|
180
|
+
let imageWidth = image.width;
|
|
181
|
+
let imageHeight = image.height;
|
|
182
|
+
let imagePass = image.index;
|
|
183
|
+
for (let y = 0; y < imageHeight; y++) {
|
|
184
|
+
for (let x = 0; x < imageWidth; x++) {
|
|
185
|
+
let pxPos = getPxPos(x, y, imagePass);
|
|
186
|
+
pixelBppMapper[bpp](pxData, data, pxPos, rawPos);
|
|
187
|
+
rawPos += bpp; //eslint-disable-line no-param-reassign
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return rawPos;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function mapImageCustomBit(image, pxData, getPxPos, bpp, bits, maxBit) {
|
|
194
|
+
// eslint-disable-line max-params
|
|
195
|
+
let imageWidth = image.width;
|
|
196
|
+
let imageHeight = image.height;
|
|
197
|
+
let imagePass = image.index;
|
|
198
|
+
for (let y = 0; y < imageHeight; y++) {
|
|
199
|
+
for (let x = 0; x < imageWidth; x++) {
|
|
200
|
+
let pixelData = bits.get(bpp);
|
|
201
|
+
let pxPos = getPxPos(x, y, imagePass);
|
|
202
|
+
pixelBppCustomMapper[bpp](pxData, pixelData, pxPos, maxBit);
|
|
203
|
+
}
|
|
204
|
+
bits.resetAfterLine();
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export const dataToBitMap = function (data, bitmapInfo) {
|
|
209
|
+
let width = bitmapInfo.width;
|
|
210
|
+
let height = bitmapInfo.height;
|
|
211
|
+
let depth = bitmapInfo.depth;
|
|
212
|
+
let bpp = bitmapInfo.bpp;
|
|
213
|
+
let interlace = bitmapInfo.interlace;
|
|
214
|
+
let bits;
|
|
215
|
+
|
|
216
|
+
if (depth !== 8) {
|
|
217
|
+
bits = bitRetriever(data, depth);
|
|
218
|
+
}
|
|
219
|
+
let pxData;
|
|
220
|
+
if (depth <= 8) {
|
|
221
|
+
pxData = Buffer.alloc(width * height * 4);
|
|
222
|
+
} else {
|
|
223
|
+
pxData = new Uint16Array(width * height * 4);
|
|
224
|
+
}
|
|
225
|
+
let maxBit = Math.pow(2, depth) - 1;
|
|
226
|
+
let rawPos = 0;
|
|
227
|
+
let images;
|
|
228
|
+
let getPxPos;
|
|
229
|
+
|
|
230
|
+
if (interlace) {
|
|
231
|
+
images = interlaceUtils.getImagePasses(width, height);
|
|
232
|
+
getPxPos = interlaceUtils.getInterlaceIterator(width, height);
|
|
233
|
+
} else {
|
|
234
|
+
let nonInterlacedPxPos = 0;
|
|
235
|
+
getPxPos = function () {
|
|
236
|
+
let returner = nonInterlacedPxPos;
|
|
237
|
+
nonInterlacedPxPos += 4;
|
|
238
|
+
return returner;
|
|
239
|
+
};
|
|
240
|
+
images = [{ width: width, height: height }];
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
for (let imageIndex = 0; imageIndex < images.length; imageIndex++) {
|
|
244
|
+
if (depth === 8) {
|
|
245
|
+
rawPos = mapImage8Bit(
|
|
246
|
+
images[imageIndex],
|
|
247
|
+
pxData,
|
|
248
|
+
getPxPos,
|
|
249
|
+
bpp,
|
|
250
|
+
data,
|
|
251
|
+
rawPos
|
|
252
|
+
);
|
|
253
|
+
} else {
|
|
254
|
+
mapImageCustomBit(
|
|
255
|
+
images[imageIndex],
|
|
256
|
+
pxData,
|
|
257
|
+
getPxPos,
|
|
258
|
+
bpp,
|
|
259
|
+
bits,
|
|
260
|
+
maxBit
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (depth === 8) {
|
|
265
|
+
if (rawPos !== data.length) {
|
|
266
|
+
throw new Error("extra data found");
|
|
267
|
+
}
|
|
268
|
+
} else {
|
|
269
|
+
bits.end();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return pxData;
|
|
273
|
+
};
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a raw RGBA(16) buffer into the exact color-type/bit-depth layout the encoder will filter+deflate.
|
|
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 6983b6f4ffa73ed0a734ebd0fc56c46a.
|
|
6
|
+
* https://github.com/pngjs/pngjs
|
|
7
|
+
*/
|
|
8
|
+
"use strict";
|
|
9
|
+
|
|
10
|
+
import constants from './constants.js'
|
|
11
|
+
|
|
12
|
+
const bitPacker = function (dataIn, width, height, options) {
|
|
13
|
+
let outHasAlpha =
|
|
14
|
+
[constants.COLORTYPE_COLOR_ALPHA, constants.COLORTYPE_ALPHA].indexOf(
|
|
15
|
+
options.colorType
|
|
16
|
+
) !== -1;
|
|
17
|
+
if (options.colorType === options.inputColorType) {
|
|
18
|
+
let bigEndian = (function () {
|
|
19
|
+
let buffer = new ArrayBuffer(2);
|
|
20
|
+
new DataView(buffer).setInt16(0, 256, true /* littleEndian */);
|
|
21
|
+
// Int16Array uses the platform's endianness.
|
|
22
|
+
return new Int16Array(buffer)[0] !== 256;
|
|
23
|
+
})();
|
|
24
|
+
// If no need to convert to grayscale and alpha is present/absent in both, take a fast route
|
|
25
|
+
if (options.bitDepth === 8 || (options.bitDepth === 16 && bigEndian)) {
|
|
26
|
+
return dataIn;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// map to a UInt16 array if data is 16bit, fix endianness below
|
|
31
|
+
let data = options.bitDepth !== 16 ? dataIn : new Uint16Array(dataIn.buffer);
|
|
32
|
+
|
|
33
|
+
let maxValue = 255;
|
|
34
|
+
let inBpp = constants.COLORTYPE_TO_BPP_MAP[options.inputColorType];
|
|
35
|
+
if (inBpp === 4 && !options.inputHasAlpha) {
|
|
36
|
+
inBpp = 3;
|
|
37
|
+
}
|
|
38
|
+
let outBpp = constants.COLORTYPE_TO_BPP_MAP[options.colorType];
|
|
39
|
+
if (options.bitDepth === 16) {
|
|
40
|
+
maxValue = 65535;
|
|
41
|
+
outBpp *= 2;
|
|
42
|
+
}
|
|
43
|
+
let outData = Buffer.alloc(width * height * outBpp);
|
|
44
|
+
|
|
45
|
+
let inIndex = 0;
|
|
46
|
+
let outIndex = 0;
|
|
47
|
+
|
|
48
|
+
let bgColor = options.bgColor || {};
|
|
49
|
+
if (bgColor.red === undefined) {
|
|
50
|
+
bgColor.red = maxValue;
|
|
51
|
+
}
|
|
52
|
+
if (bgColor.green === undefined) {
|
|
53
|
+
bgColor.green = maxValue;
|
|
54
|
+
}
|
|
55
|
+
if (bgColor.blue === undefined) {
|
|
56
|
+
bgColor.blue = maxValue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getRGBA() {
|
|
60
|
+
let red;
|
|
61
|
+
let green;
|
|
62
|
+
let blue;
|
|
63
|
+
let alpha = maxValue;
|
|
64
|
+
switch (options.inputColorType) {
|
|
65
|
+
case constants.COLORTYPE_COLOR_ALPHA:
|
|
66
|
+
alpha = data[inIndex + 3];
|
|
67
|
+
red = data[inIndex];
|
|
68
|
+
green = data[inIndex + 1];
|
|
69
|
+
blue = data[inIndex + 2];
|
|
70
|
+
break;
|
|
71
|
+
case constants.COLORTYPE_COLOR:
|
|
72
|
+
red = data[inIndex];
|
|
73
|
+
green = data[inIndex + 1];
|
|
74
|
+
blue = data[inIndex + 2];
|
|
75
|
+
break;
|
|
76
|
+
case constants.COLORTYPE_ALPHA:
|
|
77
|
+
alpha = data[inIndex + 1];
|
|
78
|
+
red = data[inIndex];
|
|
79
|
+
green = red;
|
|
80
|
+
blue = red;
|
|
81
|
+
break;
|
|
82
|
+
case constants.COLORTYPE_GRAYSCALE:
|
|
83
|
+
red = data[inIndex];
|
|
84
|
+
green = red;
|
|
85
|
+
blue = red;
|
|
86
|
+
break;
|
|
87
|
+
default:
|
|
88
|
+
throw new Error(
|
|
89
|
+
"input color type:" +
|
|
90
|
+
options.inputColorType +
|
|
91
|
+
" is not supported at present"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (options.inputHasAlpha) {
|
|
96
|
+
if (!outHasAlpha) {
|
|
97
|
+
alpha /= maxValue;
|
|
98
|
+
red = Math.min(
|
|
99
|
+
Math.max(Math.round((1 - alpha) * bgColor.red + alpha * red), 0),
|
|
100
|
+
maxValue
|
|
101
|
+
);
|
|
102
|
+
green = Math.min(
|
|
103
|
+
Math.max(Math.round((1 - alpha) * bgColor.green + alpha * green), 0),
|
|
104
|
+
maxValue
|
|
105
|
+
);
|
|
106
|
+
blue = Math.min(
|
|
107
|
+
Math.max(Math.round((1 - alpha) * bgColor.blue + alpha * blue), 0),
|
|
108
|
+
maxValue
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return { red: red, green: green, blue: blue, alpha: alpha };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for (let y = 0; y < height; y++) {
|
|
116
|
+
for (let x = 0; x < width; x++) {
|
|
117
|
+
let rgba = getRGBA(data, inIndex);
|
|
118
|
+
|
|
119
|
+
switch (options.colorType) {
|
|
120
|
+
case constants.COLORTYPE_COLOR_ALPHA:
|
|
121
|
+
case constants.COLORTYPE_COLOR:
|
|
122
|
+
if (options.bitDepth === 8) {
|
|
123
|
+
outData[outIndex] = rgba.red;
|
|
124
|
+
outData[outIndex + 1] = rgba.green;
|
|
125
|
+
outData[outIndex + 2] = rgba.blue;
|
|
126
|
+
if (outHasAlpha) {
|
|
127
|
+
outData[outIndex + 3] = rgba.alpha;
|
|
128
|
+
}
|
|
129
|
+
} else {
|
|
130
|
+
outData.writeUInt16BE(rgba.red, outIndex);
|
|
131
|
+
outData.writeUInt16BE(rgba.green, outIndex + 2);
|
|
132
|
+
outData.writeUInt16BE(rgba.blue, outIndex + 4);
|
|
133
|
+
if (outHasAlpha) {
|
|
134
|
+
outData.writeUInt16BE(rgba.alpha, outIndex + 6);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
break;
|
|
138
|
+
case constants.COLORTYPE_ALPHA:
|
|
139
|
+
case constants.COLORTYPE_GRAYSCALE: {
|
|
140
|
+
// Convert to grayscale and alpha
|
|
141
|
+
let grayscale = (rgba.red + rgba.green + rgba.blue) / 3;
|
|
142
|
+
if (options.bitDepth === 8) {
|
|
143
|
+
outData[outIndex] = grayscale;
|
|
144
|
+
if (outHasAlpha) {
|
|
145
|
+
outData[outIndex + 1] = rgba.alpha;
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
outData.writeUInt16BE(grayscale, outIndex);
|
|
149
|
+
if (outHasAlpha) {
|
|
150
|
+
outData.writeUInt16BE(rgba.alpha, outIndex + 2);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
default:
|
|
156
|
+
throw new Error("unrecognised color Type " + options.colorType);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
inIndex += inBpp;
|
|
160
|
+
outIndex += outBpp;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return outData;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
export default bitPacker;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PNG constants (chunk type codes, color-type/bpp map, signature).
|
|
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 4c253cc05421db7bc712c71848becdaf.
|
|
5
|
+
* https://github.com/pngjs/pngjs
|
|
6
|
+
*/
|
|
7
|
+
"use strict";
|
|
8
|
+
|
|
9
|
+
export default {
|
|
10
|
+
PNG_SIGNATURE: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a],
|
|
11
|
+
|
|
12
|
+
TYPE_IHDR: 0x49484452,
|
|
13
|
+
TYPE_IEND: 0x49454e44,
|
|
14
|
+
TYPE_IDAT: 0x49444154,
|
|
15
|
+
TYPE_PLTE: 0x504c5445,
|
|
16
|
+
TYPE_tRNS: 0x74524e53, // eslint-disable-line camelcase
|
|
17
|
+
TYPE_gAMA: 0x67414d41, // eslint-disable-line camelcase
|
|
18
|
+
|
|
19
|
+
// color-type bits
|
|
20
|
+
COLORTYPE_GRAYSCALE: 0,
|
|
21
|
+
COLORTYPE_PALETTE: 1,
|
|
22
|
+
COLORTYPE_COLOR: 2,
|
|
23
|
+
COLORTYPE_ALPHA: 4, // e.g. grayscale and alpha
|
|
24
|
+
|
|
25
|
+
// color-type combinations
|
|
26
|
+
COLORTYPE_PALETTE_COLOR: 3,
|
|
27
|
+
COLORTYPE_COLOR_ALPHA: 6,
|
|
28
|
+
|
|
29
|
+
COLORTYPE_TO_BPP_MAP: {
|
|
30
|
+
0: 1,
|
|
31
|
+
2: 3,
|
|
32
|
+
3: 1,
|
|
33
|
+
4: 2,
|
|
34
|
+
6: 4,
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
GAMMA_DIVISION: 100000,
|
|
38
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming + static CRC-32 calculator for PNG chunk checksums.
|
|
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 5c5c1edffe5de2d22469f6a49e31dcce.
|
|
5
|
+
* https://github.com/pngjs/pngjs
|
|
6
|
+
*/
|
|
7
|
+
"use strict";
|
|
8
|
+
|
|
9
|
+
let crcTable = [];
|
|
10
|
+
|
|
11
|
+
(function () {
|
|
12
|
+
for (let i = 0; i < 256; i++) {
|
|
13
|
+
let currentCrc = i;
|
|
14
|
+
for (let j = 0; j < 8; j++) {
|
|
15
|
+
if (currentCrc & 1) {
|
|
16
|
+
currentCrc = 0xedb88320 ^ (currentCrc >>> 1);
|
|
17
|
+
} else {
|
|
18
|
+
currentCrc = currentCrc >>> 1;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
crcTable[i] = currentCrc;
|
|
22
|
+
}
|
|
23
|
+
})();
|
|
24
|
+
|
|
25
|
+
let CrcCalculator = function () {
|
|
26
|
+
this._crc = -1;
|
|
27
|
+
};
|
|
28
|
+
export default CrcCalculator;
|
|
29
|
+
|
|
30
|
+
CrcCalculator.prototype.write = function (data) {
|
|
31
|
+
for (let i = 0; i < data.length; i++) {
|
|
32
|
+
this._crc = crcTable[(this._crc ^ data[i]) & 0xff] ^ (this._crc >>> 8);
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
CrcCalculator.prototype.crc32 = function () {
|
|
38
|
+
return this._crc ^ -1;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
CrcCalculator.crc32 = function (buf) {
|
|
42
|
+
let crc = -1;
|
|
43
|
+
for (let i = 0; i < buf.length; i++) {
|
|
44
|
+
crc = crcTable[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8);
|
|
45
|
+
}
|
|
46
|
+
return crc ^ -1;
|
|
47
|
+
};
|