@rosepetal/node-red-contrib-image-tools 1.1.3 → 1.2.2
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 +1 -13
- package/node-red-contrib-image-tools/lib/node-utils.js +218 -17
- package/node-red-contrib-image-tools/nodes/blend/add-bbs.js +64 -9
- package/node-red-contrib-image-tools/nodes/blend/add-mask.js +81 -9
- package/node-red-contrib-image-tools/nodes/blend/add-masks.js +64 -8
- package/node-red-contrib-image-tools/nodes/blend/blend.js +64 -10
- package/node-red-contrib-image-tools/nodes/io/folder-in.js +12 -1
- package/node-red-contrib-image-tools/nodes/io/image-in.js +10 -2
- package/node-red-contrib-image-tools/nodes/io/image-out.js +25 -3
- package/node-red-contrib-image-tools/nodes/mix/advanced-mosaic.js +52 -26
- package/node-red-contrib-image-tools/nodes/mix/concat.js +46 -9
- package/node-red-contrib-image-tools/nodes/mix/mosaic.js +52 -15
- package/node-red-contrib-image-tools/nodes/specialized/cropBB.js +53 -5
- package/node-red-contrib-image-tools/nodes/specialized/image-align.js +54 -7
- package/node-red-contrib-image-tools/nodes/transform/color-convert.html +189 -0
- package/node-red-contrib-image-tools/nodes/transform/color-convert.js +153 -0
- package/node-red-contrib-image-tools/nodes/transform/crop.js +38 -17
- package/node-red-contrib-image-tools/nodes/transform/draw.js +28 -7
- package/node-red-contrib-image-tools/nodes/transform/filter.js +34 -15
- package/node-red-contrib-image-tools/nodes/transform/padding.js +30 -9
- package/node-red-contrib-image-tools/nodes/transform/resize.js +58 -33
- package/node-red-contrib-image-tools/nodes/transform/rotate.html +5 -5
- package/node-red-contrib-image-tools/nodes/transform/rotate.js +40 -20
- package/node-red-contrib-image-tools/package.json +2 -8
- package/package.json +2 -1
- package/rosepetal-image-engine/binding.gyp +7 -24
- package/rosepetal-image-engine/src/add-mask.cpp +2 -2
- package/rosepetal-image-engine/src/add-masks.cpp +2 -2
- package/rosepetal-image-engine/src/advanced-mosaic.cpp +7 -8
- package/rosepetal-image-engine/src/blend.cpp +2 -2
- package/rosepetal-image-engine/src/color-convert.cpp +184 -0
- package/rosepetal-image-engine/src/concat.cpp +4 -4
- package/rosepetal-image-engine/src/crop.cpp +7 -7
- package/rosepetal-image-engine/src/draw.cpp +1 -1
- package/rosepetal-image-engine/src/filter.cpp +7 -7
- package/rosepetal-image-engine/src/image-align.cpp +1 -1
- package/rosepetal-image-engine/src/main.cpp +2 -0
- package/rosepetal-image-engine/src/mosaic.cpp +8 -9
- package/rosepetal-image-engine/src/padding.cpp +8 -7
- package/rosepetal-image-engine/src/resize.cpp +105 -27
- package/rosepetal-image-engine/src/rotate.cpp +9 -12
- package/rosepetal-image-engine/src/utils.h +48 -10
- package/docs/nodes/io/array-in.md +0 -119
- package/docs/nodes/io/array-out.md +0 -150
- package/docs/nodes/io/array-select.md +0 -157
- package/docs/nodes/io/queue.md +0 -57
- package/docs/nodes/io/save-file.md +0 -30
- package/docs/nodes/util/block-detect.md +0 -46
- package/node-red-contrib-image-tools/nodes/io/array-in.html +0 -110
- package/node-red-contrib-image-tools/nodes/io/array-in.js +0 -113
- package/node-red-contrib-image-tools/nodes/io/array-out.html +0 -125
- package/node-red-contrib-image-tools/nodes/io/array-out.js +0 -213
- package/node-red-contrib-image-tools/nodes/io/array-select.html +0 -156
- package/node-red-contrib-image-tools/nodes/io/array-select.js +0 -187
- package/node-red-contrib-image-tools/nodes/io/queue.html +0 -119
- package/node-red-contrib-image-tools/nodes/io/queue.js +0 -191
- package/node-red-contrib-image-tools/nodes/io/save-file.html +0 -243
- package/node-red-contrib-image-tools/nodes/io/save-file.js +0 -440
- package/node-red-contrib-image-tools/nodes/util/block-detect.html +0 -85
- package/node-red-contrib-image-tools/nodes/util/block-detect.js +0 -136
- package/node-red-contrib-image-tools/nodes/util/clean-debug.html +0 -201
- package/node-red-contrib-image-tools/nodes/util/clean-debug.js +0 -393
|
@@ -12,46 +12,65 @@ module.exports = function (RED) {
|
|
|
12
12
|
const node = this;
|
|
13
13
|
|
|
14
14
|
node.on('input', async (msg, send, done) => {
|
|
15
|
+
const inputPath = config.inputPath || 'payload';
|
|
16
|
+
const inputPathType = config.inputPathType || 'msg';
|
|
17
|
+
const outputPath = config.outputPath || 'payload';
|
|
18
|
+
const outputPathType = config.outputPathType || 'msg';
|
|
19
|
+
const { value: originalPayload, error: inputErr } =
|
|
20
|
+
NodeUtils.getInputValue(node, msg, inputPath, inputPathType);
|
|
21
|
+
if (inputErr) {
|
|
22
|
+
const hint = inputPathType === 'msg'
|
|
23
|
+
? `Set inputPath to an existing msg property (e.g. "payload"), or ensure "${inputPath}" exists before this node.`
|
|
24
|
+
: `Set inputPath to an existing ${inputPathType} context key, or ensure "${inputPath}" exists before this node.`;
|
|
25
|
+
return NodeUtils.handleValidationErrorWithPassthrough(
|
|
26
|
+
node,
|
|
27
|
+
{
|
|
28
|
+
message: `Invalid inputPath "${inputPath}" (${inputPathType}): ${inputErr.message}`,
|
|
29
|
+
hint,
|
|
30
|
+
details: { inputPath, inputPathType, outputPath, outputPathType }
|
|
31
|
+
},
|
|
32
|
+
msg,
|
|
33
|
+
send,
|
|
34
|
+
done,
|
|
35
|
+
{ originalPayload: undefined, outputPath: null, outputPathType, outputType: 'preserve' }
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
15
39
|
try {
|
|
16
40
|
const t0 = performance.now();
|
|
17
41
|
node.status({}); // clear
|
|
18
|
-
|
|
19
|
-
const inputPath = config.inputPath || 'payload';
|
|
20
|
-
const outputPath = config.outputPath || 'payload';
|
|
21
42
|
const outputFormat = config.outputFormat || 'raw';
|
|
22
43
|
const outputQuality = parseInt(config.outputQuality) || 90;
|
|
23
44
|
const pngOptimize = config.pngOptimize || false;
|
|
24
45
|
const padColorHex = config.padColor || '#000000';
|
|
25
46
|
|
|
26
|
-
const originalPayload = RED.util.getMessageProperty(msg, inputPath);
|
|
27
|
-
|
|
28
47
|
// Validate input images with error passthrough
|
|
29
48
|
if (Array.isArray(originalPayload)) {
|
|
30
49
|
if (!NodeUtils.validateListImage(originalPayload, node)) {
|
|
31
50
|
return NodeUtils.handleValidationErrorWithPassthrough(
|
|
32
51
|
node, 'Invalid image list structure', msg, send, done,
|
|
33
|
-
{ originalPayload, outputPath, outputType: 'preserve' }
|
|
52
|
+
{ originalPayload, outputPath, outputPathType, outputType: 'preserve' }
|
|
34
53
|
);
|
|
35
54
|
}
|
|
36
55
|
} else {
|
|
37
56
|
if (!NodeUtils.validateSingleImage(originalPayload, node)) {
|
|
38
57
|
return NodeUtils.handleValidationErrorWithPassthrough(
|
|
39
58
|
node, 'Invalid image structure', msg, send, done,
|
|
40
|
-
{ originalPayload, outputPath, outputType: 'preserve' }
|
|
59
|
+
{ originalPayload, outputPath, outputPathType, outputType: 'preserve' }
|
|
41
60
|
);
|
|
42
61
|
}
|
|
43
62
|
}
|
|
44
63
|
|
|
45
64
|
const imgs = Array.isArray(originalPayload) ? originalPayload : [originalPayload];
|
|
46
65
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
node, config.angleType, config.angleValue, msg);
|
|
51
|
-
angle = angle === null || angle === '' ? 0 : Number(angle);
|
|
66
|
+
let angle = NodeUtils.resolveDimension(
|
|
67
|
+
node, config.angleType, config.angleValue, msg);
|
|
68
|
+
angle = angle === null || angle === '' ? 0 : Number(angle);
|
|
52
69
|
|
|
53
|
-
|
|
54
|
-
|
|
70
|
+
/* ——— lanzar rotaciones en paralelo ——— */
|
|
71
|
+
const promises = imgs.map(img =>
|
|
72
|
+
CppProcessor.rotate(img, angle, padColorHex, outputFormat, outputQuality, pngOptimize)
|
|
73
|
+
);
|
|
55
74
|
|
|
56
75
|
const results = await Promise.all(promises);
|
|
57
76
|
|
|
@@ -68,29 +87,30 @@ module.exports = function (RED) {
|
|
|
68
87
|
const out = Array.isArray(originalPayload) ? images : images[0];
|
|
69
88
|
const durMs = performance.now() - t0;
|
|
70
89
|
|
|
71
|
-
|
|
90
|
+
NodeUtils.setOutputValue(node, msg, outputPath, outputPathType, out);
|
|
72
91
|
|
|
73
92
|
// Debug image display
|
|
74
93
|
let debugFormat = null;
|
|
75
|
-
|
|
94
|
+
const debugEnabled = config.debugEnabled === true || config.debugEnabled === 'true';
|
|
95
|
+
if (debugEnabled) {
|
|
76
96
|
try {
|
|
77
97
|
// Resolve and validate debug width
|
|
78
98
|
let debugWidth = NodeUtils.resolveDimension(
|
|
79
99
|
node,
|
|
80
|
-
config.debugWidthType,
|
|
100
|
+
config.debugWidthType || 'num',
|
|
81
101
|
config.debugWidth,
|
|
82
102
|
msg
|
|
83
103
|
);
|
|
84
104
|
debugWidth = Math.max(1, parseInt(debugWidth) || 200); // Ensure positive, default 200
|
|
85
105
|
|
|
86
106
|
// For arrays, show the first image as representative
|
|
87
|
-
const debugImage =
|
|
107
|
+
const debugImage = images[0];
|
|
88
108
|
const debugResult = await NodeUtils.debugImageDisplay(
|
|
89
109
|
debugImage,
|
|
90
110
|
outputFormat,
|
|
91
111
|
outputQuality,
|
|
92
112
|
node,
|
|
93
|
-
|
|
113
|
+
debugEnabled,
|
|
94
114
|
debugWidth
|
|
95
115
|
);
|
|
96
116
|
|
|
@@ -131,7 +151,7 @@ module.exports = function (RED) {
|
|
|
131
151
|
} catch (err) {
|
|
132
152
|
NodeUtils.handleNodeErrorWithPassthrough(
|
|
133
153
|
node, err, msg, send, done, 'rotate processing',
|
|
134
|
-
{ originalPayload, outputPath, outputType: 'preserve' }
|
|
154
|
+
{ originalPayload, outputPath, outputPathType, outputType: 'preserve' }
|
|
135
155
|
);
|
|
136
156
|
}
|
|
137
157
|
});
|
|
@@ -33,11 +33,6 @@
|
|
|
33
33
|
"image-in": "nodes/io/image-in.js",
|
|
34
34
|
"folder-in": "nodes/io/folder-in.js",
|
|
35
35
|
"image-out": "nodes/io/image-out.js",
|
|
36
|
-
"array-in": "nodes/io/array-in.js",
|
|
37
|
-
"array-out": "nodes/io/array-out.js",
|
|
38
|
-
"array-select": "nodes/io/array-select.js",
|
|
39
|
-
"queue": "nodes/io/queue.js",
|
|
40
|
-
"save-file": "nodes/io/save-file.js",
|
|
41
36
|
"cropBB": "nodes/specialized/cropBB.js",
|
|
42
37
|
"image-align": "nodes/specialized/image-align.js",
|
|
43
38
|
"resize": "nodes/transform/resize.js",
|
|
@@ -47,14 +42,13 @@
|
|
|
47
42
|
"padding": "nodes/transform/padding.js",
|
|
48
43
|
"filter": "nodes/transform/filter.js",
|
|
49
44
|
"draw": "nodes/transform/draw.js",
|
|
45
|
+
"color-convert": "nodes/transform/color-convert.js",
|
|
50
46
|
"mosaic": "nodes/mix/mosaic.js",
|
|
51
47
|
"advanced-mosaic": "nodes/mix/advanced-mosaic.js",
|
|
52
48
|
"blend": "nodes/blend/blend.js",
|
|
53
49
|
"add-mask": "nodes/blend/add-mask.js",
|
|
54
50
|
"add-masks": "nodes/blend/add-masks.js",
|
|
55
|
-
"add-bbs": "nodes/blend/add-bbs.js"
|
|
56
|
-
"clean-debug": "nodes/util/clean-debug.js",
|
|
57
|
-
"block-detect": "nodes/util/block-detect.js"
|
|
51
|
+
"add-bbs": "nodes/blend/add-bbs.js"
|
|
58
52
|
}
|
|
59
53
|
},
|
|
60
54
|
"dependencies": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rosepetal/node-red-contrib-image-tools",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "Node-RED Image Processing Toolkit",
|
|
5
5
|
"main": "install.sh",
|
|
6
6
|
"directories": {
|
|
@@ -59,6 +59,7 @@
|
|
|
59
59
|
"padding": "node-red-contrib-image-tools/nodes/transform/padding.js",
|
|
60
60
|
"filter": "node-red-contrib-image-tools/nodes/transform/filter.js",
|
|
61
61
|
"draw": "node-red-contrib-image-tools/nodes/transform/draw.js",
|
|
62
|
+
"color-convert": "node-red-contrib-image-tools/nodes/transform/color-convert.js",
|
|
62
63
|
"mosaic": "node-red-contrib-image-tools/nodes/mix/mosaic.js",
|
|
63
64
|
"advanced-mosaic": "node-red-contrib-image-tools/nodes/mix/advanced-mosaic.js",
|
|
64
65
|
"blend": "node-red-contrib-image-tools/nodes/blend/blend.js",
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"src/add-masks.cpp",
|
|
22
22
|
"src/add-bbs.cpp",
|
|
23
23
|
"src/image-align.cpp",
|
|
24
|
-
"src/draw.cpp"
|
|
24
|
+
"src/draw.cpp",
|
|
25
|
+
"src/color-convert.cpp"
|
|
25
26
|
],
|
|
26
27
|
"defines": [
|
|
27
28
|
"NAPI_CPP_EXCEPTIONS"
|
|
@@ -101,39 +102,23 @@
|
|
|
101
102
|
]
|
|
102
103
|
}, {
|
|
103
104
|
"include_dirs": [
|
|
104
|
-
"
|
|
105
|
+
"<!@(pkg-config --cflags-only-I opencv4 | sed s/-I//g)",
|
|
105
106
|
"<!@(node -p \"require('node-addon-api').include\")"
|
|
106
107
|
],
|
|
107
108
|
"libraries": [
|
|
108
|
-
"<!@(pkg-config --libs
|
|
109
|
+
"<!@(pkg-config --libs opencv4)"
|
|
109
110
|
],
|
|
110
111
|
"cflags_cc": [
|
|
111
112
|
"-std=c++17",
|
|
112
113
|
"-O3",
|
|
113
|
-
"-ffast-math",
|
|
114
|
-
"-march=native",
|
|
115
114
|
"-fexceptions",
|
|
116
115
|
"-frtti",
|
|
117
116
|
"-fno-omit-frame-pointer",
|
|
118
|
-
"-
|
|
119
|
-
"-fstrict-aliasing",
|
|
120
|
-
"-fopenmp",
|
|
121
|
-
"-mavx2",
|
|
122
|
-
"-ftree-vectorize",
|
|
123
|
-
"-fprefetch-loop-arrays",
|
|
124
|
-
"-finline-functions",
|
|
125
|
-
"-flto",
|
|
126
|
-
"-fipa-pta",
|
|
127
|
-
"-fvect-cost-model=cheap",
|
|
128
|
-
"-minline-all-stringops"
|
|
117
|
+
"-fopenmp"
|
|
129
118
|
],
|
|
130
119
|
"ldflags": [
|
|
131
120
|
"-O3",
|
|
132
|
-
"-
|
|
133
|
-
"-fopenmp",
|
|
134
|
-
"-lgomp",
|
|
135
|
-
"-flto",
|
|
136
|
-
"-fuse-linker-plugin"
|
|
121
|
+
"-fopenmp"
|
|
137
122
|
],
|
|
138
123
|
"xcode_settings": {
|
|
139
124
|
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
|
|
@@ -141,9 +126,7 @@
|
|
|
141
126
|
"OTHER_CPLUSPLUSFLAGS": [
|
|
142
127
|
"-std=c++17",
|
|
143
128
|
"-O3",
|
|
144
|
-
"-
|
|
145
|
-
"-march=native",
|
|
146
|
-
"-funroll-loops"
|
|
129
|
+
"-march=native"
|
|
147
130
|
]
|
|
148
131
|
}
|
|
149
132
|
}]
|
|
@@ -153,7 +153,7 @@ protected:
|
|
|
153
153
|
|
|
154
154
|
// Multi-format encoding if needed
|
|
155
155
|
if (outputFormat != "raw") {
|
|
156
|
-
cv::Mat tmp =
|
|
156
|
+
cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
|
|
157
157
|
encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
|
|
158
158
|
}
|
|
159
159
|
}
|
|
@@ -237,4 +237,4 @@ Napi::Value AddMask(const Napi::CallbackInfo& info) {
|
|
|
237
237
|
))->Queue();
|
|
238
238
|
|
|
239
239
|
return env.Undefined();
|
|
240
|
-
}
|
|
240
|
+
}
|
|
@@ -472,7 +472,7 @@ protected:
|
|
|
472
472
|
taskMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
473
473
|
|
|
474
474
|
if (outputFormat != "raw") {
|
|
475
|
-
cv::Mat tmp =
|
|
475
|
+
cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
|
|
476
476
|
encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
|
|
477
477
|
}
|
|
478
478
|
return;
|
|
@@ -560,7 +560,7 @@ protected:
|
|
|
560
560
|
|
|
561
561
|
// Multi-format encoding if needed
|
|
562
562
|
if (outputFormat != "raw") {
|
|
563
|
-
cv::Mat tmp =
|
|
563
|
+
cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
|
|
564
564
|
encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
|
|
565
565
|
}
|
|
566
566
|
}
|
|
@@ -152,14 +152,14 @@ public:
|
|
|
152
152
|
}
|
|
153
153
|
// Default based on channel count
|
|
154
154
|
else {
|
|
155
|
-
channel = (images_[i].channels() == 4) ? "
|
|
156
|
-
: (images_[i].channels() == 3) ? "
|
|
155
|
+
channel = (images_[i].channels() == 4) ? "RGBA"
|
|
156
|
+
: (images_[i].channels() == 3) ? "RGB"
|
|
157
157
|
: "GRAY";
|
|
158
158
|
}
|
|
159
159
|
} else {
|
|
160
160
|
// Buffer input - determine from OpenCV Mat
|
|
161
|
-
channel = (images_[i].channels() == 4) ? "
|
|
162
|
-
: (images_[i].channels() == 3) ? "
|
|
161
|
+
channel = (images_[i].channels() == 4) ? "RGBA"
|
|
162
|
+
: (images_[i].channels() == 3) ? "RGB"
|
|
163
163
|
: "GRAY";
|
|
164
164
|
}
|
|
165
165
|
|
|
@@ -303,9 +303,8 @@ protected:
|
|
|
303
303
|
|
|
304
304
|
/* ─ SUPER FAST multi-format encoding (optional) ─ */
|
|
305
305
|
if (outputFormat_ != "raw") {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
: ToBgrForJpg(canvas_, canvasChannel_);
|
|
306
|
+
const cv::Mat srcForEncoding =
|
|
307
|
+
PrepareForEncoding(canvas_, canvasChannel_, outputFormat_);
|
|
309
308
|
encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat_, quality_, pngOptimize_);
|
|
310
309
|
}
|
|
311
310
|
}
|
|
@@ -316,7 +315,7 @@ protected:
|
|
|
316
315
|
// Zero-copy output creation with correct channel format
|
|
317
316
|
Napi::Value jsImg = (outputFormat_ != "raw")
|
|
318
317
|
? VectorToBuffer(env, std::move(encodedBuf_)) // Zero-copy encoded
|
|
319
|
-
: MatToRawJS(env, canvas_
|
|
318
|
+
: MatToRawJS(env, canvas_, canvasChannel_);
|
|
320
319
|
|
|
321
320
|
Napi::Object result = Napi::Object::New(env);
|
|
322
321
|
result.Set("image", jsImg);
|
|
@@ -96,7 +96,7 @@ protected:
|
|
|
96
96
|
|
|
97
97
|
// Multi-format encoding if needed
|
|
98
98
|
if (outputFormat != "raw") {
|
|
99
|
-
cv::Mat tmp =
|
|
99
|
+
cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
|
|
100
100
|
encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
|
|
101
101
|
}
|
|
102
102
|
}
|
|
@@ -197,4 +197,4 @@ Napi::Value Blend(const Napi::CallbackInfo& info) {
|
|
|
197
197
|
(new BlendWorker(info[cbIdx].As<Napi::Function>(), jsImg1, jsImg2, opacity, outputFormat, quality, pngOptimize,
|
|
198
198
|
alphaCompositing, removeBackground, backgroundColor, colorTolerance))->Queue();
|
|
199
199
|
return env.Undefined();
|
|
200
|
-
}
|
|
200
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// ───────── src/color-convert.cpp ───────────────────────────────────────────
|
|
2
|
+
#include <napi.h>
|
|
3
|
+
#include <opencv2/opencv.hpp>
|
|
4
|
+
#include <chrono>
|
|
5
|
+
#include <string>
|
|
6
|
+
#include <unordered_map>
|
|
7
|
+
#include "utils.h"
|
|
8
|
+
|
|
9
|
+
// ──────────────────────────────── Lookup key helper
|
|
10
|
+
static inline std::string MakeKey(const std::string& src, const std::string& dst) {
|
|
11
|
+
return src + ">" + dst;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// ──────────────────────────────── Complete conversion table (20 paths)
|
|
15
|
+
static const std::unordered_map<std::string, int>& ConversionTable() {
|
|
16
|
+
static const std::unordered_map<std::string, int> table = {
|
|
17
|
+
// GRAY →
|
|
18
|
+
{ MakeKey("GRAY","BGR"), cv::COLOR_GRAY2BGR },
|
|
19
|
+
{ MakeKey("GRAY","RGB"), cv::COLOR_GRAY2RGB },
|
|
20
|
+
{ MakeKey("GRAY","BGRA"), cv::COLOR_GRAY2BGRA },
|
|
21
|
+
{ MakeKey("GRAY","RGBA"), cv::COLOR_GRAY2RGBA },
|
|
22
|
+
// BGR →
|
|
23
|
+
{ MakeKey("BGR","GRAY"), cv::COLOR_BGR2GRAY },
|
|
24
|
+
{ MakeKey("BGR","RGB"), cv::COLOR_BGR2RGB },
|
|
25
|
+
{ MakeKey("BGR","BGRA"), cv::COLOR_BGR2BGRA },
|
|
26
|
+
{ MakeKey("BGR","RGBA"), cv::COLOR_BGR2RGBA },
|
|
27
|
+
// RGB →
|
|
28
|
+
{ MakeKey("RGB","GRAY"), cv::COLOR_RGB2GRAY },
|
|
29
|
+
{ MakeKey("RGB","BGR"), cv::COLOR_RGB2BGR },
|
|
30
|
+
{ MakeKey("RGB","BGRA"), cv::COLOR_RGB2BGRA },
|
|
31
|
+
{ MakeKey("RGB","RGBA"), cv::COLOR_RGB2RGBA },
|
|
32
|
+
// BGRA →
|
|
33
|
+
{ MakeKey("BGRA","GRAY"), cv::COLOR_BGRA2GRAY },
|
|
34
|
+
{ MakeKey("BGRA","BGR"), cv::COLOR_BGRA2BGR },
|
|
35
|
+
{ MakeKey("BGRA","RGB"), cv::COLOR_BGRA2RGB },
|
|
36
|
+
{ MakeKey("BGRA","RGBA"), cv::COLOR_BGRA2RGBA },
|
|
37
|
+
// RGBA →
|
|
38
|
+
{ MakeKey("RGBA","GRAY"), cv::COLOR_RGBA2GRAY },
|
|
39
|
+
{ MakeKey("RGBA","BGR"), cv::COLOR_RGBA2BGR },
|
|
40
|
+
{ MakeKey("RGBA","RGB"), cv::COLOR_RGBA2RGB },
|
|
41
|
+
{ MakeKey("RGBA","BGRA"), cv::COLOR_RGBA2BGRA },
|
|
42
|
+
};
|
|
43
|
+
return table;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ──────────────────────────────── Worker
|
|
47
|
+
class ColorConvertWorker : public Napi::AsyncWorker {
|
|
48
|
+
public:
|
|
49
|
+
ColorConvertWorker(Napi::Function& cb,
|
|
50
|
+
const Napi::Value& imgVal,
|
|
51
|
+
std::string targetColorSpace,
|
|
52
|
+
std::string outputFormat,
|
|
53
|
+
int quality = 90,
|
|
54
|
+
bool pngOptimize = false)
|
|
55
|
+
: Napi::AsyncWorker(cb),
|
|
56
|
+
targetCS(std::move(targetColorSpace)),
|
|
57
|
+
outputFormat(std::move(outputFormat)),
|
|
58
|
+
quality(quality),
|
|
59
|
+
pngOptimize(pngOptimize)
|
|
60
|
+
{
|
|
61
|
+
try {
|
|
62
|
+
inputMat = ConvertToMat(imgVal);
|
|
63
|
+
|
|
64
|
+
// Detect source color space
|
|
65
|
+
if (imgVal.IsObject() && !imgVal.IsBuffer()) {
|
|
66
|
+
Napi::Object obj = imgVal.As<Napi::Object>();
|
|
67
|
+
if (obj.Has("colorSpace")) {
|
|
68
|
+
sourceCS = obj.Get("colorSpace").As<Napi::String>().Utf8Value();
|
|
69
|
+
} else {
|
|
70
|
+
sourceCS = (inputMat.channels() == 4) ? "RGBA"
|
|
71
|
+
: (inputMat.channels() == 3) ? "RGB"
|
|
72
|
+
: "GRAY";
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
sourceCS = (inputMat.channels() == 4) ? "RGBA"
|
|
76
|
+
: (inputMat.channels() == 3) ? "RGB"
|
|
77
|
+
: "GRAY";
|
|
78
|
+
}
|
|
79
|
+
} catch (const Napi::Error& e) { SetError(e.Message()); }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
protected:
|
|
83
|
+
void Execute() override {
|
|
84
|
+
try {
|
|
85
|
+
auto t0 = std::chrono::steady_clock::now();
|
|
86
|
+
|
|
87
|
+
// Identity fast-path: same source and target
|
|
88
|
+
if (sourceCS == targetCS) {
|
|
89
|
+
resultMat = inputMat;
|
|
90
|
+
} else {
|
|
91
|
+
const std::string key = MakeKey(sourceCS, targetCS);
|
|
92
|
+
const auto& table = ConversionTable();
|
|
93
|
+
auto it = table.find(key);
|
|
94
|
+
if (it == table.end()) {
|
|
95
|
+
SetError("Unsupported color space conversion: " + sourceCS + " -> " + targetCS);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
cv::cvtColor(inputMat, resultMat, it->second);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
taskMs = std::chrono::duration<double, std::milli>(
|
|
102
|
+
std::chrono::steady_clock::now() - t0).count();
|
|
103
|
+
|
|
104
|
+
// Multi-format encoding
|
|
105
|
+
if (outputFormat != "raw") {
|
|
106
|
+
const cv::Mat srcForEncoding =
|
|
107
|
+
PrepareForEncoding(resultMat, targetCS, outputFormat);
|
|
108
|
+
encodeMs = EncodeToFormat(srcForEncoding, encodedBuf, outputFormat, quality, pngOptimize);
|
|
109
|
+
}
|
|
110
|
+
} catch (const std::exception& e) { SetError(e.what()); }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
void OnOK() override {
|
|
114
|
+
Napi::Env env = Env();
|
|
115
|
+
Napi::Value jsImg = (outputFormat != "raw")
|
|
116
|
+
? VectorToBuffer(env, std::move(encodedBuf))
|
|
117
|
+
: MatToRawJS(env, resultMat, targetCS);
|
|
118
|
+
|
|
119
|
+
Napi::Object res = Napi::Object::New(env);
|
|
120
|
+
res.Set("image", jsImg);
|
|
121
|
+
res.Set("timing", MakeTimingJS(env, 0.0, taskMs, encodeMs));
|
|
122
|
+
Callback().Call({ env.Null(), res });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
void OnError(const Napi::Error& e) override {
|
|
126
|
+
Callback().Call({ e.Value(), Env().Null() });
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private:
|
|
130
|
+
cv::Mat inputMat, resultMat;
|
|
131
|
+
std::string sourceCS;
|
|
132
|
+
std::string targetCS;
|
|
133
|
+
std::string outputFormat;
|
|
134
|
+
int quality;
|
|
135
|
+
bool pngOptimize;
|
|
136
|
+
|
|
137
|
+
double taskMs = 0.0;
|
|
138
|
+
double encodeMs = 0.0;
|
|
139
|
+
std::vector<uchar> encodedBuf;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// ───────── Binding JS → C++ ────────────────────────────────────────────
|
|
143
|
+
// colorConvert(image, targetColorSpace, outputFormat, quality, pngOptimize, callback)
|
|
144
|
+
Napi::Value ColorConvert(const Napi::CallbackInfo& info)
|
|
145
|
+
{
|
|
146
|
+
Napi::Env env = info.Env();
|
|
147
|
+
if (info.Length() < 3 || info.Length() > 6 ||
|
|
148
|
+
!info[info.Length()-1].IsFunction())
|
|
149
|
+
return Napi::TypeError::New(env,
|
|
150
|
+
"colorConvert(image, targetColorSpace, [outputFormat], [quality], [pngOptimize], callback)").Value();
|
|
151
|
+
|
|
152
|
+
int i = 0;
|
|
153
|
+
Napi::Value img = info[i++];
|
|
154
|
+
std::string targetColorSpace = info[i++].As<Napi::String>().Utf8Value();
|
|
155
|
+
|
|
156
|
+
// Handle optional parameters
|
|
157
|
+
std::string outputFormat = "raw";
|
|
158
|
+
int quality = 90;
|
|
159
|
+
bool pngOptimize = false;
|
|
160
|
+
|
|
161
|
+
if (info.Length() - i >= 2) {
|
|
162
|
+
outputFormat = info[i++].As<Napi::String>().Utf8Value();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (info.Length() - i >= 2) {
|
|
166
|
+
quality = info[i++].As<Napi::Number>().Int32Value();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (info.Length() - i >= 2) {
|
|
170
|
+
pngOptimize = info[i++].As<Napi::Boolean>().Value();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
Napi::Function cb = info[i].As<Napi::Function>();
|
|
174
|
+
|
|
175
|
+
auto* worker = new ColorConvertWorker(
|
|
176
|
+
cb,
|
|
177
|
+
img,
|
|
178
|
+
targetColorSpace,
|
|
179
|
+
outputFormat,
|
|
180
|
+
quality,
|
|
181
|
+
pngOptimize);
|
|
182
|
+
worker->Queue();
|
|
183
|
+
return env.Undefined();
|
|
184
|
+
}
|
|
@@ -19,12 +19,12 @@ std::string DetectChannelFormat(const Napi::Value& jsImg, const cv::Mat& mat) {
|
|
|
19
19
|
// Default based on channel count
|
|
20
20
|
else {
|
|
21
21
|
const int channels = mat.channels();
|
|
22
|
-
return (channels == 4) ? "
|
|
22
|
+
return (channels == 4) ? "RGBA" : (channels == 3) ? "RGB" : "GRAY";
|
|
23
23
|
}
|
|
24
24
|
} else {
|
|
25
25
|
// Buffer input - determine from OpenCV Mat
|
|
26
26
|
const int channels = mat.channels();
|
|
27
|
-
return (channels == 4) ? "
|
|
27
|
+
return (channels == 4) ? "RGBA" : (channels == 3) ? "RGB" : "GRAY";
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
@@ -243,7 +243,7 @@ protected:
|
|
|
243
243
|
|
|
244
244
|
// Multi-format encoding if needed
|
|
245
245
|
if (outputFormat != "raw") {
|
|
246
|
-
cv::Mat tmp =
|
|
246
|
+
cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
|
|
247
247
|
encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
|
|
248
248
|
}
|
|
249
249
|
}
|
|
@@ -326,4 +326,4 @@ Napi::Value Concat(const Napi::CallbackInfo& info) {
|
|
|
326
326
|
// Create and queue worker
|
|
327
327
|
(new ConcatWorker(info[cbIdx].As<Napi::Function>(), raw, dir, st, pad, outputFormat, quality, pngOptimize))->Queue();
|
|
328
328
|
return env.Undefined();
|
|
329
|
-
}
|
|
329
|
+
}
|
|
@@ -29,14 +29,14 @@ public:
|
|
|
29
29
|
}
|
|
30
30
|
// Default based on channel count
|
|
31
31
|
else {
|
|
32
|
-
channel_ = (input_.channels() == 4) ? "
|
|
33
|
-
: (input_.channels() == 3) ? "
|
|
32
|
+
channel_ = (input_.channels() == 4) ? "RGBA"
|
|
33
|
+
: (input_.channels() == 3) ? "RGB"
|
|
34
34
|
: "GRAY";
|
|
35
35
|
}
|
|
36
36
|
} else {
|
|
37
37
|
// Buffer input - determine from OpenCV Mat
|
|
38
|
-
channel_ = (input_.channels() == 4) ? "
|
|
39
|
-
: (input_.channels() == 3) ? "
|
|
38
|
+
channel_ = (input_.channels() == 4) ? "RGBA"
|
|
39
|
+
: (input_.channels() == 3) ? "RGB"
|
|
40
40
|
: "GRAY";
|
|
41
41
|
}
|
|
42
42
|
}
|
|
@@ -64,8 +64,8 @@ protected:
|
|
|
64
64
|
|
|
65
65
|
/* ─ Multi-format encoding (encodeMs) ─ */
|
|
66
66
|
if(outputFormat_ != "raw"){
|
|
67
|
-
const cv::Mat
|
|
68
|
-
|
|
67
|
+
const cv::Mat srcForEncoding =
|
|
68
|
+
PrepareForEncoding(result_, channel_, outputFormat_);
|
|
69
69
|
encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat_, quality_, pngOptimize_);
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -74,7 +74,7 @@ protected:
|
|
|
74
74
|
Napi::Env env = Env();
|
|
75
75
|
Napi::Value jsImg = (outputFormat_ != "raw")
|
|
76
76
|
? VectorToBuffer(env,std::move(encodedBuf_)) // 0-copy
|
|
77
|
-
: MatToRawJS(env,result_
|
|
77
|
+
: MatToRawJS(env,result_,channel_);
|
|
78
78
|
|
|
79
79
|
Napi::Object out = Napi::Object::New(env);
|
|
80
80
|
out.Set("image", jsImg);
|
|
@@ -202,7 +202,7 @@ protected:
|
|
|
202
202
|
}
|
|
203
203
|
|
|
204
204
|
if (outputFormat != "raw") {
|
|
205
|
-
cv::Mat encodeSrc =
|
|
205
|
+
cv::Mat encodeSrc = PrepareForEncoding(resultImage, imageChannelOrder, outputFormat);
|
|
206
206
|
encodeMs = EncodeToFormat(encodeSrc, encodedBuffer, outputFormat, quality, pngOptimize);
|
|
207
207
|
}
|
|
208
208
|
|
|
@@ -40,14 +40,14 @@ public:
|
|
|
40
40
|
}
|
|
41
41
|
// Default based on channel count
|
|
42
42
|
else {
|
|
43
|
-
channel_ = (input_.channels() == 4) ? "
|
|
44
|
-
: (input_.channels() == 3) ? "
|
|
43
|
+
channel_ = (input_.channels() == 4) ? "RGBA"
|
|
44
|
+
: (input_.channels() == 3) ? "RGB"
|
|
45
45
|
: "GRAY";
|
|
46
46
|
}
|
|
47
47
|
} else {
|
|
48
48
|
// Buffer input - determine from OpenCV Mat
|
|
49
|
-
channel_ = (input_.channels() == 4) ? "
|
|
50
|
-
: (input_.channels() == 3) ? "
|
|
49
|
+
channel_ = (input_.channels() == 4) ? "RGBA"
|
|
50
|
+
: (input_.channels() == 3) ? "RGB"
|
|
51
51
|
: "GRAY";
|
|
52
52
|
}
|
|
53
53
|
}
|
|
@@ -76,8 +76,8 @@ protected:
|
|
|
76
76
|
|
|
77
77
|
// Multi-format encoding
|
|
78
78
|
if (outputFormat_ != "raw") {
|
|
79
|
-
const cv::Mat
|
|
80
|
-
|
|
79
|
+
const cv::Mat srcForEncoding =
|
|
80
|
+
PrepareForEncoding(result_, channel_, outputFormat_);
|
|
81
81
|
encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat_, quality_, pngOptimize_);
|
|
82
82
|
}
|
|
83
83
|
} catch (const std::exception& e) {
|
|
@@ -227,4 +227,4 @@ Napi::Value Filter(const Napi::CallbackInfo& info) {
|
|
|
227
227
|
// Create and queue worker
|
|
228
228
|
(new FilterWorker(cb, img, filterType, kernelSize, intensity, outputFormat, quality, pngOptimize))->Queue();
|
|
229
229
|
return env.Undefined();
|
|
230
|
-
}
|
|
230
|
+
}
|
|
@@ -322,7 +322,7 @@ private:
|
|
|
322
322
|
}
|
|
323
323
|
// Default based on channel count
|
|
324
324
|
const int channels = mat.channels();
|
|
325
|
-
return (channels == 4) ? "
|
|
325
|
+
return (channels == 4) ? "RGBA" : (channels == 3) ? "RGB" : "GRAY";
|
|
326
326
|
}
|
|
327
327
|
|
|
328
328
|
// Convert image to grayscale
|
|
@@ -14,6 +14,7 @@ Napi::Value AddMasks(const Napi::CallbackInfo& info);
|
|
|
14
14
|
Napi::Value AddBBs(const Napi::CallbackInfo& info);
|
|
15
15
|
Napi::Value ImageAlign(const Napi::CallbackInfo& info);
|
|
16
16
|
Napi::Value Draw(const Napi::CallbackInfo& info);
|
|
17
|
+
Napi::Value ColorConvert(const Napi::CallbackInfo& info);
|
|
17
18
|
|
|
18
19
|
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
19
20
|
exports.Set(Napi::String::New(env, "resize"), Napi::Function::New(env, Resize));
|
|
@@ -30,6 +31,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
|
30
31
|
exports.Set(Napi::String::New(env, "addBBs"), Napi::Function::New(env, AddBBs));
|
|
31
32
|
exports.Set(Napi::String::New(env, "imageAlign"), Napi::Function::New(env, ImageAlign));
|
|
32
33
|
exports.Set(Napi::String::New(env, "draw"), Napi::Function::New(env, Draw));
|
|
34
|
+
exports.Set(Napi::String::New(env, "colorConvert"), Napi::Function::New(env, ColorConvert));
|
|
33
35
|
return exports;
|
|
34
36
|
}
|
|
35
37
|
|