@hiroosy/baileys-lite 0.3.1 → 0.3.3

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.
@@ -1,175 +0,0 @@
1
- "use strict";
2
-
3
- import * as interlaceUtils from './interlace.js'
4
- import paethPredictor from './paeth-predictor.js'
5
-
6
- function getByteWidth(width, bpp, depth) {
7
- let byteWidth = width * bpp;
8
- if (depth !== 8) {
9
- byteWidth = Math.ceil(byteWidth / (8 / depth));
10
- }
11
- return byteWidth;
12
- }
13
-
14
- let Filter = function (bitmapInfo, dependencies) {
15
- let width = bitmapInfo.width;
16
- let height = bitmapInfo.height;
17
- let interlace = bitmapInfo.interlace;
18
- let bpp = bitmapInfo.bpp;
19
- let depth = bitmapInfo.depth;
20
-
21
- this.read = dependencies.read;
22
- this.write = dependencies.write;
23
- this.complete = dependencies.complete;
24
-
25
- this._imageIndex = 0;
26
- this._images = [];
27
- if (interlace) {
28
- let passes = interlaceUtils.getImagePasses(width, height);
29
- for (let i = 0; i < passes.length; i++) {
30
- this._images.push({
31
- byteWidth: getByteWidth(passes[i].width, bpp, depth),
32
- height: passes[i].height,
33
- lineIndex: 0,
34
- });
35
- }
36
- } else {
37
- this._images.push({
38
- byteWidth: getByteWidth(width, bpp, depth),
39
- height: height,
40
- lineIndex: 0,
41
- });
42
- }
43
-
44
- if (depth === 8) {
45
- this._xComparison = bpp;
46
- } else if (depth === 16) {
47
- this._xComparison = bpp * 2;
48
- } else {
49
- this._xComparison = 1;
50
- }
51
- };
52
-
53
- Filter.prototype.start = function () {
54
- this.read(
55
- this._images[this._imageIndex].byteWidth + 1,
56
- this._reverseFilterLine.bind(this)
57
- );
58
- };
59
-
60
- Filter.prototype._unFilterType1 = function (
61
- rawData,
62
- unfilteredLine,
63
- byteWidth
64
- ) {
65
- let xComparison = this._xComparison;
66
- let xBiggerThan = xComparison - 1;
67
-
68
- for (let x = 0; x < byteWidth; x++) {
69
- let rawByte = rawData[1 + x];
70
- let f1Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0;
71
- unfilteredLine[x] = rawByte + f1Left;
72
- }
73
- };
74
-
75
- Filter.prototype._unFilterType2 = function (
76
- rawData,
77
- unfilteredLine,
78
- byteWidth
79
- ) {
80
- let lastLine = this._lastLine;
81
-
82
- for (let x = 0; x < byteWidth; x++) {
83
- let rawByte = rawData[1 + x];
84
- let f2Up = lastLine ? lastLine[x] : 0;
85
- unfilteredLine[x] = rawByte + f2Up;
86
- }
87
- };
88
-
89
- Filter.prototype._unFilterType3 = function (
90
- rawData,
91
- unfilteredLine,
92
- byteWidth
93
- ) {
94
- let xComparison = this._xComparison;
95
- let xBiggerThan = xComparison - 1;
96
- let lastLine = this._lastLine;
97
-
98
- for (let x = 0; x < byteWidth; x++) {
99
- let rawByte = rawData[1 + x];
100
- let f3Up = lastLine ? lastLine[x] : 0;
101
- let f3Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0;
102
- let f3Add = Math.floor((f3Left + f3Up) / 2);
103
- unfilteredLine[x] = rawByte + f3Add;
104
- }
105
- };
106
-
107
- Filter.prototype._unFilterType4 = function (
108
- rawData,
109
- unfilteredLine,
110
- byteWidth
111
- ) {
112
- let xComparison = this._xComparison;
113
- let xBiggerThan = xComparison - 1;
114
- let lastLine = this._lastLine;
115
-
116
- for (let x = 0; x < byteWidth; x++) {
117
- let rawByte = rawData[1 + x];
118
- let f4Up = lastLine ? lastLine[x] : 0;
119
- let f4Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0;
120
- let f4UpLeft = x > xBiggerThan && lastLine ? lastLine[x - xComparison] : 0;
121
- let f4Add = paethPredictor(f4Left, f4Up, f4UpLeft);
122
- unfilteredLine[x] = rawByte + f4Add;
123
- }
124
- };
125
-
126
- Filter.prototype._reverseFilterLine = function (rawData) {
127
- let filter = rawData[0];
128
- let unfilteredLine;
129
- let currentImage = this._images[this._imageIndex];
130
- let byteWidth = currentImage.byteWidth;
131
-
132
- if (filter === 0) {
133
- unfilteredLine = rawData.slice(1, byteWidth + 1);
134
- } else {
135
- unfilteredLine = Buffer.alloc(byteWidth);
136
-
137
- switch (filter) {
138
- case 1:
139
- this._unFilterType1(rawData, unfilteredLine, byteWidth);
140
- break;
141
- case 2:
142
- this._unFilterType2(rawData, unfilteredLine, byteWidth);
143
- break;
144
- case 3:
145
- this._unFilterType3(rawData, unfilteredLine, byteWidth);
146
- break;
147
- case 4:
148
- this._unFilterType4(rawData, unfilteredLine, byteWidth);
149
- break;
150
- default:
151
- throw new Error("Unrecognised filter type - " + filter);
152
- }
153
- }
154
-
155
- this.write(unfilteredLine);
156
-
157
- currentImage.lineIndex++;
158
- if (currentImage.lineIndex >= currentImage.height) {
159
- this._lastLine = null;
160
- this._imageIndex++;
161
- currentImage = this._images[this._imageIndex];
162
- } else {
163
- this._lastLine = unfilteredLine;
164
- }
165
-
166
- if (currentImage) {
167
-
168
- this.read(currentImage.byteWidth + 1, this._reverseFilterLine.bind(this));
169
- } else {
170
- this._lastLine = null;
171
- this.complete();
172
- }
173
- };
174
-
175
- export default Filter;
@@ -1,95 +0,0 @@
1
- "use strict";
2
-
3
- function dePalette(indata, outdata, width, height, palette) {
4
- let pxPos = 0;
5
-
6
- for (let y = 0; y < height; y++) {
7
- for (let x = 0; x < width; x++) {
8
- let color = palette[indata[pxPos]];
9
-
10
- if (!color) {
11
- throw new Error("index " + indata[pxPos] + " not in palette");
12
- }
13
-
14
- for (let i = 0; i < 4; i++) {
15
- outdata[pxPos + i] = color[i];
16
- }
17
- pxPos += 4;
18
- }
19
- }
20
- }
21
-
22
- function replaceTransparentColor(indata, outdata, width, height, transColor) {
23
- let pxPos = 0;
24
- for (let y = 0; y < height; y++) {
25
- for (let x = 0; x < width; x++) {
26
- let makeTrans = false;
27
-
28
- if (transColor.length === 1) {
29
- if (transColor[0] === indata[pxPos]) {
30
- makeTrans = true;
31
- }
32
- } else if (
33
- transColor[0] === indata[pxPos] &&
34
- transColor[1] === indata[pxPos + 1] &&
35
- transColor[2] === indata[pxPos + 2]
36
- ) {
37
- makeTrans = true;
38
- }
39
- if (makeTrans) {
40
- for (let i = 0; i < 4; i++) {
41
- outdata[pxPos + i] = 0;
42
- }
43
- }
44
- pxPos += 4;
45
- }
46
- }
47
- }
48
-
49
- function scaleDepth(indata, outdata, width, height, depth) {
50
- let maxOutSample = 255;
51
- let maxInSample = Math.pow(2, depth) - 1;
52
- let pxPos = 0;
53
-
54
- for (let y = 0; y < height; y++) {
55
- for (let x = 0; x < width; x++) {
56
- for (let i = 0; i < 4; i++) {
57
- outdata[pxPos + i] = Math.floor(
58
- (indata[pxPos + i] * maxOutSample) / maxInSample + 0.5
59
- );
60
- }
61
- pxPos += 4;
62
- }
63
- }
64
- }
65
-
66
- const formatNormaliser = function (indata, imageData, skipRescale = false) {
67
- let depth = imageData.depth;
68
- let width = imageData.width;
69
- let height = imageData.height;
70
- let colorType = imageData.colorType;
71
- let transColor = imageData.transColor;
72
- let palette = imageData.palette;
73
-
74
- let outdata = indata;
75
-
76
- if (colorType === 3) {
77
-
78
- dePalette(indata, outdata, width, height, palette);
79
- } else {
80
- if (transColor) {
81
- replaceTransparentColor(indata, outdata, width, height, transColor);
82
- }
83
-
84
- if (depth !== 8 && !skipRescale) {
85
-
86
- if (depth === 16) {
87
- outdata = Buffer.alloc(width * height * 4);
88
- }
89
- scaleDepth(indata, outdata, width, height, depth);
90
- }
91
- }
92
- return outdata;
93
- };
94
-
95
- export default formatNormaliser;
@@ -1,84 +0,0 @@
1
- "use strict";
2
-
3
- let imagePasses = [
4
- {
5
-
6
- x: [0],
7
- y: [0],
8
- },
9
- {
10
-
11
- x: [4],
12
- y: [0],
13
- },
14
- {
15
-
16
- x: [0, 4],
17
- y: [4],
18
- },
19
- {
20
-
21
- x: [2, 6],
22
- y: [0, 4],
23
- },
24
- {
25
-
26
- x: [0, 2, 4, 6],
27
- y: [2, 6],
28
- },
29
- {
30
-
31
- x: [1, 3, 5, 7],
32
- y: [0, 2, 4, 6],
33
- },
34
- {
35
-
36
- x: [0, 1, 2, 3, 4, 5, 6, 7],
37
- y: [1, 3, 5, 7],
38
- },
39
- ];
40
-
41
- export const getImagePasses = function (width, height) {
42
- let images = [];
43
- let xLeftOver = width % 8;
44
- let yLeftOver = height % 8;
45
- let xRepeats = (width - xLeftOver) / 8;
46
- let yRepeats = (height - yLeftOver) / 8;
47
- for (let i = 0; i < imagePasses.length; i++) {
48
- let pass = imagePasses[i];
49
- let passWidth = xRepeats * pass.x.length;
50
- let passHeight = yRepeats * pass.y.length;
51
- for (let j = 0; j < pass.x.length; j++) {
52
- if (pass.x[j] < xLeftOver) {
53
- passWidth++;
54
- } else {
55
- break;
56
- }
57
- }
58
- for (let j = 0; j < pass.y.length; j++) {
59
- if (pass.y[j] < yLeftOver) {
60
- passHeight++;
61
- } else {
62
- break;
63
- }
64
- }
65
- if (passWidth > 0 && passHeight > 0) {
66
- images.push({ width: passWidth, height: passHeight, index: i });
67
- }
68
- }
69
- return images;
70
- };
71
-
72
- export const getInterlaceIterator = function (width) {
73
- return function (x, y, pass) {
74
- let outerXLeftOver = x % imagePasses[pass].x.length;
75
- let outerX =
76
- ((x - outerXLeftOver) / imagePasses[pass].x.length) * 8 +
77
- imagePasses[pass].x[outerXLeftOver];
78
- let outerYLeftOver = y % imagePasses[pass].y.length;
79
- let outerY =
80
- ((y - outerYLeftOver) / imagePasses[pass].y.length) * 8 +
81
- imagePasses[pass].y[outerYLeftOver];
82
- return outerX * 4 + outerY * width * 4;
83
- };
84
- };
@@ -1,54 +0,0 @@
1
- "use strict";
2
-
3
- let hasSyncZlib = true;
4
- import zlib from 'node:zlib'
5
- if (!zlib.deflateSync) {
6
- hasSyncZlib = false;
7
- }
8
- import constants from './constants.js'
9
- import Packer from './packer.js'
10
-
11
- const packSync = function (metaData, opt) {
12
- if (!hasSyncZlib) {
13
- throw new Error(
14
- "To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0"
15
- );
16
- }
17
-
18
- let options = opt || {};
19
-
20
- let packer = new Packer(options);
21
-
22
- let chunks = [];
23
-
24
- chunks.push(Buffer.from(constants.PNG_SIGNATURE));
25
-
26
- chunks.push(packer.packIHDR(metaData.width, metaData.height));
27
-
28
- if (metaData.gamma) {
29
- chunks.push(packer.packGAMA(metaData.gamma));
30
- }
31
-
32
- let filteredData = packer.filterData(
33
- metaData.data,
34
- metaData.width,
35
- metaData.height
36
- );
37
-
38
- let compressedData = zlib.deflateSync(
39
- filteredData,
40
- packer.getDeflateOptions()
41
- );
42
- filteredData = null;
43
-
44
- if (!compressedData || !compressedData.length) {
45
- throw new Error("bad png - invalid compressed data response");
46
- }
47
- chunks.push(packer.packIDAT(compressedData));
48
-
49
- chunks.push(packer.packIEND());
50
-
51
- return Buffer.concat(chunks);
52
- };
53
-
54
- export default packSync;
@@ -1,130 +0,0 @@
1
- "use strict";
2
-
3
- import constants from './constants.js'
4
- import CrcStream from './crc.js'
5
- import bitPacker from './bitpacker.js'
6
- import filter from './filter-pack.js'
7
- import zlib from 'node:zlib'
8
-
9
- let Packer = function (options) {
10
- this._options = options;
11
-
12
- options.deflateChunkSize = options.deflateChunkSize || 32 * 1024;
13
- options.deflateLevel =
14
- options.deflateLevel != null ? options.deflateLevel : 9;
15
- options.deflateStrategy =
16
- options.deflateStrategy != null ? options.deflateStrategy : 3;
17
- options.inputHasAlpha =
18
- options.inputHasAlpha != null ? options.inputHasAlpha : true;
19
- options.deflateFactory = options.deflateFactory || zlib.createDeflate;
20
- options.bitDepth = options.bitDepth || 8;
21
-
22
- options.colorType =
23
- typeof options.colorType === "number"
24
- ? options.colorType
25
- : constants.COLORTYPE_COLOR_ALPHA;
26
- options.inputColorType =
27
- typeof options.inputColorType === "number"
28
- ? options.inputColorType
29
- : constants.COLORTYPE_COLOR_ALPHA;
30
-
31
- if (
32
- [
33
- constants.COLORTYPE_GRAYSCALE,
34
- constants.COLORTYPE_COLOR,
35
- constants.COLORTYPE_COLOR_ALPHA,
36
- constants.COLORTYPE_ALPHA,
37
- ].indexOf(options.colorType) === -1
38
- ) {
39
- throw new Error(
40
- "option color type:" + options.colorType + " is not supported at present"
41
- );
42
- }
43
- if (
44
- [
45
- constants.COLORTYPE_GRAYSCALE,
46
- constants.COLORTYPE_COLOR,
47
- constants.COLORTYPE_COLOR_ALPHA,
48
- constants.COLORTYPE_ALPHA,
49
- ].indexOf(options.inputColorType) === -1
50
- ) {
51
- throw new Error(
52
- "option input color type:" +
53
- options.inputColorType +
54
- " is not supported at present"
55
- );
56
- }
57
- if (options.bitDepth !== 8 && options.bitDepth !== 16) {
58
- throw new Error(
59
- "option bit depth:" + options.bitDepth + " is not supported at present"
60
- );
61
- }
62
- };
63
-
64
- Packer.prototype.getDeflateOptions = function () {
65
- return {
66
- chunkSize: this._options.deflateChunkSize,
67
- level: this._options.deflateLevel,
68
- strategy: this._options.deflateStrategy,
69
- };
70
- };
71
-
72
- Packer.prototype.createDeflate = function () {
73
- return this._options.deflateFactory(this.getDeflateOptions());
74
- };
75
-
76
- Packer.prototype.filterData = function (data, width, height) {
77
-
78
- let packedData = bitPacker(data, width, height, this._options);
79
-
80
- let bpp = constants.COLORTYPE_TO_BPP_MAP[this._options.colorType];
81
- let filteredData = filter(packedData, width, height, this._options, bpp);
82
- return filteredData;
83
- };
84
-
85
- Packer.prototype._packChunk = function (type, data) {
86
- let len = data ? data.length : 0;
87
- let buf = Buffer.alloc(len + 12);
88
-
89
- buf.writeUInt32BE(len, 0);
90
- buf.writeUInt32BE(type, 4);
91
-
92
- if (data) {
93
- data.copy(buf, 8);
94
- }
95
-
96
- buf.writeInt32BE(
97
- CrcStream.crc32(buf.slice(4, buf.length - 4)),
98
- buf.length - 4
99
- );
100
- return buf;
101
- };
102
-
103
- Packer.prototype.packGAMA = function (gamma) {
104
- let buf = Buffer.alloc(4);
105
- buf.writeUInt32BE(Math.floor(gamma * constants.GAMMA_DIVISION), 0);
106
- return this._packChunk(constants.TYPE_gAMA, buf);
107
- };
108
-
109
- Packer.prototype.packIHDR = function (width, height) {
110
- let buf = Buffer.alloc(13);
111
- buf.writeUInt32BE(width, 0);
112
- buf.writeUInt32BE(height, 4);
113
- buf[8] = this._options.bitDepth;
114
- buf[9] = this._options.colorType;
115
- buf[10] = 0;
116
- buf[11] = 0;
117
- buf[12] = 0;
118
-
119
- return this._packChunk(constants.TYPE_IHDR, buf);
120
- };
121
-
122
- Packer.prototype.packIDAT = function (data) {
123
- return this._packChunk(constants.TYPE_IDAT, data);
124
- };
125
-
126
- Packer.prototype.packIEND = function () {
127
- return this._packChunk(constants.TYPE_IEND, null);
128
- };
129
-
130
- export default Packer;
@@ -1,16 +0,0 @@
1
- "use strict";
2
-
3
- export default function paethPredictor(left, above, upLeft) {
4
- let paeth = left + above - upLeft;
5
- let pLeft = Math.abs(paeth - left);
6
- let pAbove = Math.abs(paeth - above);
7
- let pUpLeft = Math.abs(paeth - upLeft);
8
-
9
- if (pLeft <= pAbove && pLeft <= pUpLeft) {
10
- return left;
11
- }
12
- if (pAbove <= pUpLeft) {
13
- return above;
14
- }
15
- return upLeft;
16
- };
@@ -1,113 +0,0 @@
1
- "use strict";
2
-
3
- let hasSyncZlib = true;
4
- import zlib from 'node:zlib'
5
- import inflateSync from './sync-inflate.js'
6
- if (!zlib.deflateSync) {
7
- hasSyncZlib = false;
8
- }
9
- import SyncReader from './sync-reader.js'
10
- import * as FilterSync from './filter-parse-sync.js'
11
- import Parser from './parser.js'
12
- import * as bitmapper from './bitmapper.js'
13
- import formatNormaliser from './format-normaliser.js'
14
-
15
- const parseSync = 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
- let inflateData = Buffer.concat(inflateDataList);
75
- inflateDataList.length = 0;
76
-
77
- let inflatedData;
78
- if (metaData.interlace) {
79
- inflatedData = zlib.inflateSync(inflateData);
80
- } else {
81
- let rowSize =
82
- ((metaData.width * metaData.bpp * metaData.depth + 7) >> 3) + 1;
83
- let imageSize = rowSize * metaData.height;
84
- inflatedData = inflateSync(inflateData, {
85
- chunkSize: imageSize,
86
- maxLength: imageSize,
87
- });
88
- }
89
- inflateData = null;
90
-
91
- if (!inflatedData || !inflatedData.length) {
92
- throw new Error("bad png - invalid inflate data response");
93
- }
94
-
95
- let unfilteredData = FilterSync.process(inflatedData, metaData);
96
- inflateData = null;
97
-
98
- let bitmapData = bitmapper.dataToBitMap(unfilteredData, metaData);
99
- unfilteredData = null;
100
-
101
- let normalisedBitmapData = formatNormaliser(
102
- bitmapData,
103
- metaData,
104
- options.skipRescale
105
- );
106
-
107
- metaData.data = normalisedBitmapData;
108
- metaData.gamma = gamma || 0;
109
-
110
- return metaData;
111
- };
112
-
113
- export default parseSync;