@rosepetal/node-red-contrib-image-tools 1.6.0 → 1.6.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/node-red-contrib-image-tools/lib/bmp-decode.js +79 -0
- package/node-red-contrib-image-tools/nodes/io/folder-in.js +10 -6
- package/node-red-contrib-image-tools/nodes/io/image-in.js +11 -4
- package/npm/darwin-arm64/package.json +1 -1
- package/npm/darwin-x64/package.json +1 -1
- package/npm/linux-arm64/package.json +1 -1
- package/npm/linux-x64/package.json +1 -1
- package/npm/linuxmusl-x64/package.json +1 -1
- package/npm/win32-x64/package.json +1 -1
- package/package.json +6 -6
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Pure-JS BMP decoder for uncompressed BI_RGB files (8/24/32-bit).
|
|
3
|
+
* Read-side counterpart of encodeBmpRaw in image-out; Sharp cannot read BMP.
|
|
4
|
+
* @author Rosepetal
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
function isBmp(buf) {
|
|
8
|
+
return Buffer.isBuffer(buf) && buf.length >= 2 && buf[0] === 0x42 && buf[1] === 0x4D;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// Decodes a BMP buffer to the toolkit's raw image object.
|
|
12
|
+
// Supports 8-bit palette, 24-bit and 32-bit uncompressed, top-down and bottom-up.
|
|
13
|
+
function decodeBmp(buf) {
|
|
14
|
+
if (!isBmp(buf) || buf.length < 54) throw new Error('Invalid BMP file');
|
|
15
|
+
|
|
16
|
+
const pixelOffset = buf.readUInt32LE(10);
|
|
17
|
+
const infoSize = buf.readUInt32LE(14);
|
|
18
|
+
if (infoSize < 40) throw new Error(`Unsupported BMP header (size ${infoSize})`);
|
|
19
|
+
|
|
20
|
+
const width = buf.readInt32LE(18);
|
|
21
|
+
const rawHeight = buf.readInt32LE(22);
|
|
22
|
+
const bpp = buf.readUInt16LE(28);
|
|
23
|
+
const compression = buf.readUInt32LE(30);
|
|
24
|
+
|
|
25
|
+
const topDown = rawHeight < 0;
|
|
26
|
+
const height = Math.abs(rawHeight);
|
|
27
|
+
if (width <= 0 || height === 0) throw new Error('Invalid BMP dimensions');
|
|
28
|
+
if (compression !== 0) throw new Error(`Unsupported BMP compression ${compression} (only uncompressed BI_RGB)`);
|
|
29
|
+
if (bpp !== 8 && bpp !== 24 && bpp !== 32) throw new Error(`Unsupported BMP bit depth ${bpp} (only 8/24/32)`);
|
|
30
|
+
|
|
31
|
+
const srcStride = (width * (bpp / 8) + 3) & ~3;
|
|
32
|
+
if (pixelOffset + srcStride * height > buf.length) throw new Error('Corrupt BMP: pixel data truncated');
|
|
33
|
+
|
|
34
|
+
// 8-bit files carry a BGRA palette; an all-gray palette maps to a GRAY image
|
|
35
|
+
let palette = null;
|
|
36
|
+
let grayPalette = false;
|
|
37
|
+
if (bpp === 8) {
|
|
38
|
+
const colors = buf.readUInt32LE(46) || 256;
|
|
39
|
+
const palOffset = 14 + infoSize;
|
|
40
|
+
if (palOffset + colors * 4 > buf.length) throw new Error('Corrupt BMP: palette truncated');
|
|
41
|
+
palette = [];
|
|
42
|
+
grayPalette = true;
|
|
43
|
+
for (let i = 0; i < colors; i++) {
|
|
44
|
+
const b = buf[palOffset + i * 4], g = buf[palOffset + i * 4 + 1], r = buf[palOffset + i * 4 + 2];
|
|
45
|
+
if (r !== g || g !== b) grayPalette = false;
|
|
46
|
+
palette.push([r, g, b]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const channels = bpp === 32 ? 4 : (bpp === 24 || !grayPalette) ? 3 : 1;
|
|
51
|
+
const colorSpace = channels === 4 ? 'RGBA' : channels === 3 ? 'RGB' : 'GRAY';
|
|
52
|
+
const out = Buffer.alloc(width * height * channels);
|
|
53
|
+
|
|
54
|
+
for (let y = 0; y < height; y++) {
|
|
55
|
+
const srcRow = pixelOffset + (topDown ? y : height - 1 - y) * srcStride;
|
|
56
|
+
const dstRow = y * width * channels;
|
|
57
|
+
if (bpp === 8) {
|
|
58
|
+
for (let x = 0; x < width; x++) {
|
|
59
|
+
const entry = palette[buf[srcRow + x]] || [0, 0, 0];
|
|
60
|
+
if (channels === 1) {
|
|
61
|
+
out[dstRow + x] = entry[0];
|
|
62
|
+
} else {
|
|
63
|
+
const d = dstRow + x * 3;
|
|
64
|
+
out[d] = entry[0]; out[d + 1] = entry[1]; out[d + 2] = entry[2];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
for (let x = 0; x < width; x++) {
|
|
69
|
+
const s = srcRow + x * (bpp / 8), d = dstRow + x * channels;
|
|
70
|
+
out[d] = buf[s + 2]; out[d + 1] = buf[s + 1]; out[d + 2] = buf[s]; // BGR → RGB
|
|
71
|
+
if (channels === 4) out[d + 3] = buf[s + 3];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return { data: out, width, height, channels, colorSpace, dtype: 'uint8' };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = { isBmp, decodeBmp };
|
|
@@ -14,6 +14,7 @@ try {
|
|
|
14
14
|
}
|
|
15
15
|
const fs = require('fs').promises;
|
|
16
16
|
const path = require('path');
|
|
17
|
+
const BmpDecode = require('../../lib/bmp-decode.js');
|
|
17
18
|
|
|
18
19
|
module.exports = function(RED) {
|
|
19
20
|
const NodeUtils = require('../../lib/node-utils.js')(RED);
|
|
@@ -103,13 +104,16 @@ module.exports = function(RED) {
|
|
|
103
104
|
// Read encoded file once so we can reuse it for both decoding and debug preview
|
|
104
105
|
const fileBuffer = await fs.readFile(filePath);
|
|
105
106
|
|
|
106
|
-
//
|
|
107
|
-
const sharpInstance = sharp(fileBuffer);
|
|
107
|
+
// Decode: BMP natively (Sharp cannot read it), everything else via Sharp
|
|
108
108
|
const decodeStart = performance.now();
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
.
|
|
112
|
-
|
|
109
|
+
let data, info;
|
|
110
|
+
if (BmpDecode.isBmp(fileBuffer)) {
|
|
111
|
+
const decoded = BmpDecode.decodeBmp(fileBuffer);
|
|
112
|
+
data = decoded.data;
|
|
113
|
+
info = { width: decoded.width, height: decoded.height, channels: decoded.channels };
|
|
114
|
+
} else {
|
|
115
|
+
({ data, info } = await sharp(fileBuffer).raw().toBuffer({ resolveWithObject: true }));
|
|
116
|
+
}
|
|
113
117
|
const decodeMs = performance.now() - decodeStart;
|
|
114
118
|
|
|
115
119
|
// Determine colorSpace from Sharp info
|
|
@@ -11,6 +11,7 @@ try {
|
|
|
11
11
|
sharp = null;
|
|
12
12
|
}
|
|
13
13
|
const fs = require('fs').promises;
|
|
14
|
+
const BmpDecode = require('../../lib/bmp-decode.js');
|
|
14
15
|
|
|
15
16
|
module.exports = function(RED) {
|
|
16
17
|
const NodeUtils = require('../../lib/node-utils.js')(RED);
|
|
@@ -46,10 +47,16 @@ module.exports = function(RED) {
|
|
|
46
47
|
return;
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
//
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
// Decode: BMP natively (Sharp cannot read it), everything else via Sharp
|
|
51
|
+
const fileBuffer = await fs.readFile(filePath);
|
|
52
|
+
let data, info;
|
|
53
|
+
if (BmpDecode.isBmp(fileBuffer)) {
|
|
54
|
+
const decoded = BmpDecode.decodeBmp(fileBuffer);
|
|
55
|
+
data = decoded.data;
|
|
56
|
+
info = { width: decoded.width, height: decoded.height, channels: decoded.channels };
|
|
57
|
+
} else {
|
|
58
|
+
({ data, info } = await sharp(fileBuffer).raw().toBuffer({ resolveWithObject: true }));
|
|
59
|
+
}
|
|
53
60
|
|
|
54
61
|
// Determine colorSpace from Sharp info
|
|
55
62
|
let colorSpace;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rosepetal/node-red-contrib-image-tools",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"description": "Node-RED Image Processing Toolkit",
|
|
5
5
|
"main": "install.sh",
|
|
6
6
|
"directories": {
|
|
@@ -36,11 +36,11 @@
|
|
|
36
36
|
"sharp": "^0.34.2"
|
|
37
37
|
},
|
|
38
38
|
"optionalDependencies": {
|
|
39
|
-
"@rosepetal/node-red-contrib-image-tools-linux-x64": "1.6.
|
|
40
|
-
"@rosepetal/node-red-contrib-image-tools-linux-arm64": "1.6.
|
|
41
|
-
"@rosepetal/node-red-contrib-image-tools-linuxmusl-x64": "1.6.
|
|
42
|
-
"@rosepetal/node-red-contrib-image-tools-darwin-x64": "1.6.
|
|
43
|
-
"@rosepetal/node-red-contrib-image-tools-darwin-arm64": "1.6.
|
|
39
|
+
"@rosepetal/node-red-contrib-image-tools-linux-x64": "1.6.1",
|
|
40
|
+
"@rosepetal/node-red-contrib-image-tools-linux-arm64": "1.6.1",
|
|
41
|
+
"@rosepetal/node-red-contrib-image-tools-linuxmusl-x64": "1.6.1",
|
|
42
|
+
"@rosepetal/node-red-contrib-image-tools-darwin-x64": "1.6.1",
|
|
43
|
+
"@rosepetal/node-red-contrib-image-tools-darwin-arm64": "1.6.1"
|
|
44
44
|
},
|
|
45
45
|
"node-red": {
|
|
46
46
|
"version": ">=1.0.0",
|