@rosepetal/node-red-contrib-image-tools 1.6.3 → 1.7.0
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/.claude/rules/node-implementation-template.md +1 -1
- package/README.md +2 -0
- package/node-red-contrib-image-tools/lib/bmp-decode.js +18 -68
- package/node-red-contrib-image-tools/lib/cpp-bridge.js +202 -104
- package/node-red-contrib-image-tools/lib/node-utils.js +78 -78
- package/node-red-contrib-image-tools/nodes/blend/add-bbs.html +11 -4
- package/node-red-contrib-image-tools/nodes/blend/add-mask.html +11 -4
- package/node-red-contrib-image-tools/nodes/blend/add-masks.html +11 -4
- package/node-red-contrib-image-tools/nodes/blend/blend.html +11 -4
- package/node-red-contrib-image-tools/nodes/io/folder-in.js +98 -46
- package/node-red-contrib-image-tools/nodes/io/image-in.js +3 -4
- package/node-red-contrib-image-tools/nodes/io/image-out.html +11 -4
- package/node-red-contrib-image-tools/nodes/io/image-out.js +93 -146
- package/node-red-contrib-image-tools/nodes/mix/advanced-mosaic.html +11 -4
- package/node-red-contrib-image-tools/nodes/mix/concat.html +11 -4
- package/node-red-contrib-image-tools/nodes/mix/mosaic.html +11 -4
- package/node-red-contrib-image-tools/nodes/specialized/cropBB.js +3 -3
- package/node-red-contrib-image-tools/nodes/specialized/heat-diff.js +3 -3
- package/node-red-contrib-image-tools/nodes/specialized/image-align.html +3 -2
- package/node-red-contrib-image-tools/nodes/specialized/image-align.js +3 -3
- package/node-red-contrib-image-tools/nodes/transform/color-convert.js +3 -3
- package/node-red-contrib-image-tools/nodes/transform/crop.html +11 -4
- package/node-red-contrib-image-tools/nodes/transform/crop.js +3 -3
- package/node-red-contrib-image-tools/nodes/transform/filter.html +11 -4
- package/node-red-contrib-image-tools/nodes/transform/filter.js +3 -3
- package/node-red-contrib-image-tools/nodes/transform/padding.html +11 -4
- package/node-red-contrib-image-tools/nodes/transform/padding.js +3 -3
- package/node-red-contrib-image-tools/nodes/transform/resize.html +3 -3
- package/node-red-contrib-image-tools/nodes/transform/resize.js +3 -3
- package/node-red-contrib-image-tools/nodes/transform/rotate.html +11 -4
- package/node-red-contrib-image-tools/nodes/transform/rotate.js +3 -3
- 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
- package/rosepetal-image-engine/binding.gyp +2 -1
- package/rosepetal-image-engine/src/add-bbs.cpp +123 -123
- package/rosepetal-image-engine/src/add-mask.cpp +30 -15
- package/rosepetal-image-engine/src/add-masks.cpp +145 -102
- package/rosepetal-image-engine/src/advanced-mosaic.cpp +200 -129
- package/rosepetal-image-engine/src/blend.cpp +34 -20
- package/rosepetal-image-engine/src/codec.cpp +157 -0
- package/rosepetal-image-engine/src/color-convert.cpp +22 -19
- package/rosepetal-image-engine/src/concat.cpp +38 -43
- package/rosepetal-image-engine/src/crop.cpp +23 -26
- package/rosepetal-image-engine/src/draw.cpp +33 -10
- package/rosepetal-image-engine/src/filter.cpp +22 -26
- package/rosepetal-image-engine/src/heat-diff.cpp +31 -8
- package/rosepetal-image-engine/src/image-align.cpp +36 -32
- package/rosepetal-image-engine/src/main.cpp +4 -0
- package/rosepetal-image-engine/src/mosaic.cpp +31 -40
- package/rosepetal-image-engine/src/padding.cpp +30 -29
- package/rosepetal-image-engine/src/resize.cpp +20 -25
- package/rosepetal-image-engine/src/rotate.cpp +22 -23
- package/rosepetal-image-engine/src/utils.h +243 -144
- package/scripts/dual-register.js +32 -25
- package/scripts/sync-version.js +58 -41
|
@@ -52,32 +52,46 @@ static inline cv::Mat BuildMaskFrom2DArray(const Napi::Array& rows) {
|
|
|
52
52
|
return mask;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
55
|
+
// JS thread: parse normalized polygons (no rasterisation)
|
|
56
|
+
static inline std::vector<std::vector<cv::Point2d>> ParsePolygonsAdvanced(const Napi::Array& polygons) {
|
|
57
|
+
std::vector<std::vector<cv::Point2d>> out;
|
|
58
|
+
out.reserve(polygons.Length());
|
|
59
59
|
for (uint32_t idx = 0; idx < polygons.Length(); idx++) {
|
|
60
60
|
if (!polygons.Get(idx).IsArray()) continue;
|
|
61
61
|
Napi::Array coords = polygons.Get(idx).As<Napi::Array>();
|
|
62
62
|
if (coords.Length() == 0) continue;
|
|
63
63
|
|
|
64
|
-
std::vector<cv::
|
|
64
|
+
std::vector<cv::Point2d> points;
|
|
65
65
|
points.reserve(coords.Length());
|
|
66
|
-
|
|
67
66
|
for (uint32_t i = 0; i < coords.Length(); i++) {
|
|
68
67
|
if (!coords.Get(i).IsArray()) continue;
|
|
69
68
|
Napi::Array point = coords.Get(i).As<Napi::Array>();
|
|
70
69
|
if (point.Length() >= 2 && point.Get(0u).IsNumber() && point.Get(1u).IsNumber()) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
int px = static_cast<int>(std::round(x * size.width));
|
|
74
|
-
int py = static_cast<int>(std::round(y * size.height));
|
|
75
|
-
px = std::max(0, std::min(size.width - 1, px));
|
|
76
|
-
py = std::max(0, std::min(size.height - 1, py));
|
|
77
|
-
points.emplace_back(px, py);
|
|
70
|
+
points.emplace_back(point.Get(0u).As<Napi::Number>().DoubleValue(),
|
|
71
|
+
point.Get(1u).As<Napi::Number>().DoubleValue());
|
|
78
72
|
}
|
|
79
73
|
}
|
|
74
|
+
if (!points.empty()) out.push_back(std::move(points));
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Worker thread: rasterise normalized polygons into a binary mask
|
|
80
|
+
static inline cv::Mat RasterizePolygonsAdvanced(const std::vector<std::vector<cv::Point2d>>& polygons,
|
|
81
|
+
const cv::Size& size) {
|
|
82
|
+
if (polygons.empty() || size.width <= 0 || size.height <= 0) return cv::Mat();
|
|
83
|
+
cv::Mat mask = cv::Mat::zeros(size, CV_8UC1);
|
|
80
84
|
|
|
85
|
+
for (const auto& poly : polygons) {
|
|
86
|
+
std::vector<cv::Point> points;
|
|
87
|
+
points.reserve(poly.size());
|
|
88
|
+
for (const auto& p : poly) {
|
|
89
|
+
int px = static_cast<int>(std::round(p.x * size.width));
|
|
90
|
+
int py = static_cast<int>(std::round(p.y * size.height));
|
|
91
|
+
px = std::max(0, std::min(size.width - 1, px));
|
|
92
|
+
py = std::max(0, std::min(size.height - 1, py));
|
|
93
|
+
points.emplace_back(px, py);
|
|
94
|
+
}
|
|
81
95
|
if (points.size() >= 3) {
|
|
82
96
|
const cv::Point* pts = points.data();
|
|
83
97
|
int npts = static_cast<int>(points.size());
|
|
@@ -129,92 +143,57 @@ public:
|
|
|
129
143
|
quality_(quality), pngOptimize_(pngOptimize),
|
|
130
144
|
hasMasks_(hasMasks)
|
|
131
145
|
{
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
imageChannels_.reserve(imagesArray.Length());
|
|
138
|
-
imageConfigs_.reserve(imageConfigsArray.Length());
|
|
139
|
-
|
|
140
|
-
// Convert images with zero-copy operations and extract channel formats
|
|
141
|
-
for (uint32_t i = 0; i < imagesArray.Length(); i++) {
|
|
142
|
-
images_.emplace_back(ConvertToMat(imagesArray[i]));
|
|
143
|
-
|
|
144
|
-
// Extract and store channel format for each image
|
|
145
|
-
std::string channel;
|
|
146
|
-
if (imagesArray[i].IsObject() && !imagesArray[i].IsBuffer()) {
|
|
147
|
-
Napi::Object obj = imagesArray[i].As<Napi::Object>();
|
|
148
|
-
|
|
149
|
-
// Check for new colorSpace field first
|
|
150
|
-
if (obj.Has("colorSpace")) {
|
|
151
|
-
channel = obj.Get("colorSpace").As<Napi::String>().Utf8Value();
|
|
152
|
-
}
|
|
153
|
-
// Default based on channel count
|
|
154
|
-
else {
|
|
155
|
-
channel = (images_[i].channels() == 4) ? "RGBA"
|
|
156
|
-
: (images_[i].channels() == 3) ? "RGB"
|
|
157
|
-
: "GRAY";
|
|
158
|
-
}
|
|
159
|
-
} else {
|
|
160
|
-
// Buffer input - determine from OpenCV Mat
|
|
161
|
-
channel = (images_[i].channels() == 4) ? "RGBA"
|
|
162
|
-
: (images_[i].channels() == 3) ? "RGB"
|
|
163
|
-
: "GRAY";
|
|
146
|
+
// JS thread: metadata + persistent references only (no decode, no pixels)
|
|
147
|
+
try {
|
|
148
|
+
sources_.reserve(imagesArray.Length());
|
|
149
|
+
for (uint32_t i = 0; i < imagesArray.Length(); i++) {
|
|
150
|
+
sources_.emplace_back(CaptureImage(imagesArray[i]));
|
|
164
151
|
}
|
|
165
|
-
|
|
166
|
-
imageChannels_.emplace_back(channel);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// Optional masks aligned with input images
|
|
170
|
-
if (hasMasks_) {
|
|
171
|
-
masks_.resize(images_.size());
|
|
172
|
-
maskTags_.resize(images_.size());
|
|
173
|
-
maskOutputs_.resize(images_.size());
|
|
174
152
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
153
|
+
// Optional masks aligned with input images (parsed, not rasterised)
|
|
154
|
+
if (hasMasks_) {
|
|
155
|
+
pendingMasks_.resize(sources_.size());
|
|
156
|
+
|
|
157
|
+
for (uint32_t i = 0; i < masksArray.Length() && i < sources_.size(); i++) {
|
|
158
|
+
Napi::Value mv = masksArray.Get(i);
|
|
159
|
+
PendingMask& pm = pendingMasks_[i];
|
|
160
|
+
|
|
161
|
+
if (mv.IsObject() && !mv.IsArray() && !mv.IsBuffer()) {
|
|
162
|
+
Napi::Object mObj = mv.As<Napi::Object>();
|
|
163
|
+
pm.tag = ExtractClassNameAdvanced(mObj);
|
|
164
|
+
|
|
165
|
+
if (mObj.Has("mask")) {
|
|
166
|
+
Napi::Value inner = mObj.Get("mask");
|
|
167
|
+
if (inner.IsArray()) {
|
|
168
|
+
pm.arrayMask = BuildMaskFrom2DArray(inner.As<Napi::Array>());
|
|
169
|
+
pm.kind = PendingMask::ARRAY;
|
|
170
|
+
} else {
|
|
171
|
+
pm.src = CaptureImage(inner);
|
|
172
|
+
pm.kind = PendingMask::IMAGE;
|
|
173
|
+
}
|
|
174
|
+
} else if (mObj.Has("polygons") && mObj.Get("polygons").IsArray()) {
|
|
175
|
+
pm.polygons = ParsePolygonsAdvanced(mObj.Get("polygons").As<Napi::Array>());
|
|
176
|
+
pm.kind = PendingMask::POLYGONS;
|
|
177
|
+
} else if (mObj.Has("data") && mObj.Has("width") && mObj.Has("height")) {
|
|
178
|
+
pm.src = CaptureImage(mv);
|
|
179
|
+
pm.kind = PendingMask::IMAGE;
|
|
190
180
|
}
|
|
191
|
-
} else if (
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
maskMat = BuildMaskFrom2DArray(mv.As<Napi::Array>());
|
|
198
|
-
} else if (mv.IsBuffer()) {
|
|
199
|
-
maskMat = ConvertToMat(mv);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
if (!maskMat.empty()) {
|
|
203
|
-
if (maskMat.channels() > 1) {
|
|
204
|
-
cv::cvtColor(maskMat, maskMat, cv::COLOR_BGR2GRAY);
|
|
181
|
+
} else if (mv.IsArray()) {
|
|
182
|
+
pm.arrayMask = BuildMaskFrom2DArray(mv.As<Napi::Array>());
|
|
183
|
+
pm.kind = PendingMask::ARRAY;
|
|
184
|
+
} else if (mv.IsBuffer()) {
|
|
185
|
+
pm.src = CaptureImage(mv);
|
|
186
|
+
pm.kind = PendingMask::IMAGE;
|
|
205
187
|
}
|
|
206
|
-
if (maskMat.depth() != CV_8U) {
|
|
207
|
-
cv::Mat tmp;
|
|
208
|
-
maskMat.convertTo(tmp, CV_8U, 255.0);
|
|
209
|
-
maskMat = tmp;
|
|
210
|
-
}
|
|
211
|
-
cv::threshold(maskMat, maskMat, 0, 255, cv::THRESH_BINARY);
|
|
212
|
-
masks_[i] = maskMat;
|
|
213
|
-
maskTags_[i] = tag;
|
|
214
|
-
maskOutputs_[i] = cv::Mat::zeros(canvasHeight_, canvasWidth_, CV_8UC1);
|
|
215
188
|
}
|
|
216
189
|
}
|
|
190
|
+
} catch (const Napi::Error& e) {
|
|
191
|
+
captureFailed_ = true; SetError(e.Message());
|
|
192
|
+
} catch (const std::exception& e) {
|
|
193
|
+
captureFailed_ = true; SetError(e.what());
|
|
217
194
|
}
|
|
195
|
+
|
|
196
|
+
imageConfigs_.reserve(imageConfigsArray.Length());
|
|
218
197
|
|
|
219
198
|
// Parse image configurations - ultra-fast operations
|
|
220
199
|
for (uint32_t i = 0; i < imageConfigsArray.Length(); i++) {
|
|
@@ -258,20 +237,75 @@ public:
|
|
|
258
237
|
}
|
|
259
238
|
}
|
|
260
239
|
|
|
240
|
+
hasRotation_ = hasRotation;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
protected:
|
|
244
|
+
void Execute() override {
|
|
245
|
+
if (captureFailed_) return;
|
|
246
|
+
|
|
247
|
+
/* ─ Decode (encoded inputs only) + channel detection, worker thread ─ */
|
|
248
|
+
int64 t0 = cv::getTickCount();
|
|
249
|
+
images_.clear(); imageChannels_.clear();
|
|
250
|
+
images_.reserve(sources_.size());
|
|
251
|
+
imageChannels_.reserve(sources_.size());
|
|
252
|
+
for (auto& s : sources_) {
|
|
253
|
+
s.Materialize();
|
|
254
|
+
images_.push_back(s.mat);
|
|
255
|
+
imageChannels_.push_back(s.colorSpace);
|
|
256
|
+
}
|
|
257
|
+
|
|
261
258
|
// Determine the best canvas format, preferring RGBA if rotation is detected
|
|
262
|
-
if (
|
|
259
|
+
if (hasRotation_) {
|
|
263
260
|
canvasChannel_ = "RGBA"; // Force RGBA for transparent rotation padding
|
|
264
261
|
} else {
|
|
265
262
|
canvasChannel_ = DetermineBestCanvasFormatAdvanced(imageChannels_);
|
|
266
263
|
}
|
|
267
|
-
|
|
264
|
+
|
|
265
|
+
// Optional masks: decode / rasterise / binarise (worker thread)
|
|
266
|
+
if (hasMasks_) {
|
|
267
|
+
masks_.assign(images_.size(), cv::Mat());
|
|
268
|
+
maskTags_.assign(images_.size(), std::string());
|
|
269
|
+
maskOutputs_.assign(images_.size(), cv::Mat());
|
|
270
|
+
|
|
271
|
+
for (size_t i = 0; i < pendingMasks_.size() && i < images_.size(); i++) {
|
|
272
|
+
PendingMask& pm = pendingMasks_[i];
|
|
273
|
+
cv::Mat maskMat;
|
|
274
|
+
switch (pm.kind) {
|
|
275
|
+
case PendingMask::ARRAY:
|
|
276
|
+
maskMat = pm.arrayMask;
|
|
277
|
+
break;
|
|
278
|
+
case PendingMask::POLYGONS:
|
|
279
|
+
maskMat = RasterizePolygonsAdvanced(pm.polygons, images_[i].size());
|
|
280
|
+
break;
|
|
281
|
+
case PendingMask::IMAGE:
|
|
282
|
+
pm.src.Materialize();
|
|
283
|
+
maskMat = pm.src.mat;
|
|
284
|
+
break;
|
|
285
|
+
default:
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (!maskMat.empty()) {
|
|
290
|
+
if (maskMat.channels() > 1) {
|
|
291
|
+
cv::cvtColor(maskMat, maskMat, cv::COLOR_BGR2GRAY);
|
|
292
|
+
}
|
|
293
|
+
if (maskMat.depth() != CV_8U) {
|
|
294
|
+
cv::Mat tmp;
|
|
295
|
+
maskMat.convertTo(tmp, CV_8U, 255.0);
|
|
296
|
+
maskMat = tmp;
|
|
297
|
+
}
|
|
298
|
+
cv::threshold(maskMat, maskMat, 0, 255, cv::THRESH_BINARY);
|
|
299
|
+
masks_[i] = maskMat;
|
|
300
|
+
maskTags_[i] = pm.tag;
|
|
301
|
+
maskOutputs_[i] = cv::Mat::zeros(canvasHeight_, canvasWidth_, CV_8UC1);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
268
305
|
convertMs_ = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
269
|
-
}
|
|
270
306
|
|
|
271
|
-
protected:
|
|
272
|
-
void Execute() override {
|
|
273
307
|
/* ─ SUPER FAST advanced mosaic composition ─ */
|
|
274
|
-
|
|
308
|
+
t0 = cv::getTickCount();
|
|
275
309
|
|
|
276
310
|
// Parse background color - optimized hex parsing (returns BGR format)
|
|
277
311
|
cv::Scalar bgColor = ParseColor(backgroundColor_, cv::Scalar(0, 0, 0));
|
|
@@ -306,7 +340,10 @@ protected:
|
|
|
306
340
|
const cv::Mat srcForEncoding =
|
|
307
341
|
PrepareForEncoding(canvas_, canvasChannel_, outputFormat_);
|
|
308
342
|
encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat_, quality_, pngOptimize_);
|
|
343
|
+
} else {
|
|
344
|
+
FinalizeForOutput(canvas_);
|
|
309
345
|
}
|
|
346
|
+
for (auto& m : maskOutputs_) FinalizeForOutput(m);
|
|
310
347
|
}
|
|
311
348
|
|
|
312
349
|
void OnOK() override {
|
|
@@ -351,6 +388,16 @@ private:
|
|
|
351
388
|
int width, height; // -1 means keep original
|
|
352
389
|
int zIndex;
|
|
353
390
|
};
|
|
391
|
+
|
|
392
|
+
// Mask input as parsed on the JS thread; materialised in Execute()
|
|
393
|
+
struct PendingMask {
|
|
394
|
+
enum Kind { NONE, ARRAY, POLYGONS, IMAGE };
|
|
395
|
+
Kind kind = NONE;
|
|
396
|
+
std::string tag;
|
|
397
|
+
cv::Mat arrayMask; // from 2D JS array
|
|
398
|
+
std::vector<std::vector<cv::Point2d>> polygons; // normalized
|
|
399
|
+
ImageSource src; // raw / encoded image
|
|
400
|
+
};
|
|
354
401
|
|
|
355
402
|
// ULTRA-FAST image processing with transformations
|
|
356
403
|
void ProcessImageFast(const ImageConfig& config) {
|
|
@@ -362,8 +409,10 @@ private:
|
|
|
362
409
|
config.arrayIndex < static_cast<int>(masks_.size()) &&
|
|
363
410
|
!masks_[config.arrayIndex].empty();
|
|
364
411
|
|
|
365
|
-
|
|
366
|
-
|
|
412
|
+
// Headers only: the source pixels are never written to. Every transform
|
|
413
|
+
// below writes into a fresh Mat, so no up-front full-image clone is needed.
|
|
414
|
+
cv::Mat img = images_[config.arrayIndex];
|
|
415
|
+
cv::Mat mask = hasMaskForImage ? masks_[config.arrayIndex] : cv::Mat();
|
|
367
416
|
if (img.empty()) return;
|
|
368
417
|
|
|
369
418
|
const std::string& imgChannel = imageChannels_[config.arrayIndex];
|
|
@@ -380,9 +429,13 @@ private:
|
|
|
380
429
|
targetWidth = static_cast<int>(std::round(targetHeight * static_cast<double>(img.cols) / img.rows));
|
|
381
430
|
}
|
|
382
431
|
|
|
383
|
-
cv::
|
|
432
|
+
cv::Mat resized;
|
|
433
|
+
cv::resize(img, resized, cv::Size(targetWidth, targetHeight), 0, 0, cv::INTER_LINEAR);
|
|
434
|
+
img = resized;
|
|
384
435
|
if (hasMaskForImage && !mask.empty()) {
|
|
385
|
-
cv::
|
|
436
|
+
cv::Mat resizedMask;
|
|
437
|
+
cv::resize(mask, resizedMask, cv::Size(targetWidth, targetHeight), 0, 0, cv::INTER_NEAREST);
|
|
438
|
+
mask = resizedMask;
|
|
386
439
|
}
|
|
387
440
|
}
|
|
388
441
|
|
|
@@ -396,12 +449,12 @@ private:
|
|
|
396
449
|
// 0 degrees - no rotation needed
|
|
397
450
|
} else if (std::abs(normalizedAngle - 90.0) < eps) {
|
|
398
451
|
// 90° counterclockwise (mathematical standard)
|
|
399
|
-
cv::rotate(img,
|
|
452
|
+
cv::Mat rotated; cv::rotate(img, rotated, cv::ROTATE_90_COUNTERCLOCKWISE); img = rotated;
|
|
400
453
|
} else if (std::abs(normalizedAngle - 180.0) < eps) {
|
|
401
|
-
cv::rotate(img,
|
|
454
|
+
cv::Mat rotated; cv::rotate(img, rotated, cv::ROTATE_180); img = rotated;
|
|
402
455
|
} else if (std::abs(normalizedAngle - 270.0) < eps) {
|
|
403
456
|
// 270° counterclockwise = 90° clockwise
|
|
404
|
-
cv::rotate(img,
|
|
457
|
+
cv::Mat rotated; cv::rotate(img, rotated, cv::ROTATE_90_CLOCKWISE); img = rotated;
|
|
405
458
|
} else {
|
|
406
459
|
// Arbitrary angles - use affine transformation
|
|
407
460
|
// Negate rotation to make positive angles counterclockwise (mathematical standard)
|
|
@@ -427,21 +480,29 @@ private:
|
|
|
427
480
|
// Ensure the image has an alpha channel for transparent padding
|
|
428
481
|
// Respect original colorSpace format to prevent channel inversion
|
|
429
482
|
if (img.channels() == 3) {
|
|
483
|
+
cv::Mat withAlpha;
|
|
430
484
|
if (imgChannel == "RGB") {
|
|
431
|
-
cv::cvtColor(img,
|
|
485
|
+
cv::cvtColor(img, withAlpha, cv::COLOR_RGB2RGBA);
|
|
432
486
|
} else { // BGR format
|
|
433
|
-
cv::cvtColor(img,
|
|
487
|
+
cv::cvtColor(img, withAlpha, cv::COLOR_BGR2BGRA);
|
|
434
488
|
}
|
|
489
|
+
img = withAlpha;
|
|
435
490
|
} else if (img.channels() == 1) {
|
|
436
|
-
cv::
|
|
491
|
+
cv::Mat withAlpha;
|
|
492
|
+
cv::cvtColor(img, withAlpha, cv::COLOR_GRAY2BGRA);
|
|
493
|
+
img = withAlpha;
|
|
437
494
|
}
|
|
438
495
|
// 4-channel images (RGBA/BGRA) already have alpha - no conversion needed
|
|
439
496
|
|
|
440
|
-
cv::
|
|
497
|
+
cv::Mat warped;
|
|
498
|
+
cv::warpAffine(img, warped, rotationMatrix, newSize,
|
|
441
499
|
cv::INTER_LINEAR, cv::BORDER_CONSTANT, padColor);
|
|
500
|
+
img = warped;
|
|
442
501
|
if (hasMaskForImage && !mask.empty()) {
|
|
443
|
-
cv::
|
|
502
|
+
cv::Mat warpedMask;
|
|
503
|
+
cv::warpAffine(mask, warpedMask, rotationMatrix, newSize,
|
|
444
504
|
cv::INTER_NEAREST, cv::BORDER_CONSTANT, cv::Scalar(0));
|
|
505
|
+
mask = warpedMask;
|
|
445
506
|
}
|
|
446
507
|
}
|
|
447
508
|
}
|
|
@@ -589,28 +650,34 @@ private:
|
|
|
589
650
|
// Both source and destination have alpha - use proper alpha blending
|
|
590
651
|
cv::Mat canvasROI = canvas_(dstROI);
|
|
591
652
|
|
|
592
|
-
//
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
dstPixel[0] = static_cast<uchar>((srcPixel[0] * srcAlpha + dstPixel[0] * dstAlpha * (1.0f - srcAlpha)) / outAlpha);
|
|
607
|
-
dstPixel[1] = static_cast<uchar>((srcPixel[1] * srcAlpha + dstPixel[1] * dstAlpha * (1.0f - srcAlpha)) / outAlpha);
|
|
608
|
-
dstPixel[2] = static_cast<uchar>((srcPixel[2] * srcAlpha + dstPixel[2] * dstAlpha * (1.0f - srcAlpha)) / outAlpha);
|
|
609
|
-
dstPixel[3] = static_cast<uchar>(outAlpha * 255.0f);
|
|
653
|
+
// Row-pointer alpha blending ("over" operator). Same arithmetic as the
|
|
654
|
+
// previous per-pixel .at<>() loop, but without per-element bounds/step
|
|
655
|
+
// computations; fully opaque source pixels are a plain copy.
|
|
656
|
+
const int rows = imgToPlace.rows, cols = imgToPlace.cols;
|
|
657
|
+
cv::parallel_for_(cv::Range(0, rows), [&](const cv::Range& range) {
|
|
658
|
+
for (int y = range.start; y < range.end; ++y) {
|
|
659
|
+
const uchar* s = imgToPlace.ptr<uchar>(y);
|
|
660
|
+
uchar* d = canvasROI.ptr<uchar>(y);
|
|
661
|
+
for (int x = 0; x < cols; ++x, s += 4, d += 4) {
|
|
662
|
+
const uchar sa = s[3];
|
|
663
|
+
if (sa == 0) continue; // fully transparent: keep canvas
|
|
664
|
+
if (sa == 255) { // fully opaque: overwrite
|
|
665
|
+
d[0] = s[0]; d[1] = s[1]; d[2] = s[2]; d[3] = 255;
|
|
666
|
+
continue;
|
|
610
667
|
}
|
|
668
|
+
const float srcAlpha = sa / 255.0f;
|
|
669
|
+
const float dstAlpha = d[3] / 255.0f;
|
|
670
|
+
// dst = src * srcAlpha + dst * (1 - srcAlpha) * dstAlpha
|
|
671
|
+
const float outAlpha = srcAlpha + dstAlpha * (1.0f - srcAlpha);
|
|
672
|
+
if (outAlpha <= 0.0f) continue;
|
|
673
|
+
const float dstW = dstAlpha * (1.0f - srcAlpha);
|
|
674
|
+
d[0] = static_cast<uchar>((s[0] * srcAlpha + d[0] * dstW) / outAlpha);
|
|
675
|
+
d[1] = static_cast<uchar>((s[1] * srcAlpha + d[1] * dstW) / outAlpha);
|
|
676
|
+
d[2] = static_cast<uchar>((s[2] * srcAlpha + d[2] * dstW) / outAlpha);
|
|
677
|
+
d[3] = static_cast<uchar>(outAlpha * 255.0f);
|
|
611
678
|
}
|
|
612
679
|
}
|
|
613
|
-
}
|
|
680
|
+
});
|
|
614
681
|
} else {
|
|
615
682
|
// Standard copy operation for non-alpha images
|
|
616
683
|
imgToPlace.copyTo(canvas_(dstROI));
|
|
@@ -653,6 +720,10 @@ private:
|
|
|
653
720
|
}
|
|
654
721
|
|
|
655
722
|
// Member variables
|
|
723
|
+
std::vector<ImageSource> sources_; // JS inputs (decoded in Execute)
|
|
724
|
+
std::vector<PendingMask> pendingMasks_; // JS mask inputs (materialised in Execute)
|
|
725
|
+
bool captureFailed_{false};
|
|
726
|
+
bool hasRotation_{false};
|
|
656
727
|
std::vector<cv::Mat> images_;
|
|
657
728
|
std::vector<std::string> imageChannels_;
|
|
658
729
|
std::vector<ImageConfig> imageConfigs_;
|
|
@@ -2,9 +2,6 @@
|
|
|
2
2
|
#include <opencv2/opencv.hpp>
|
|
3
3
|
#include "utils.h"
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
5
|
/*------------------------------------------------------------------------*/
|
|
9
6
|
class BlendWorker final : public Napi::AsyncWorker {
|
|
10
7
|
public:
|
|
@@ -28,30 +25,39 @@ public:
|
|
|
28
25
|
backgroundColor(std::move(backgroundColor)),
|
|
29
26
|
colorTolerance(colorTolerance)
|
|
30
27
|
{
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
28
|
+
// JS thread: metadata + persistent references only (no decode, no pixels)
|
|
29
|
+
try {
|
|
30
|
+
src1_ = CaptureImage(jsImg1);
|
|
31
|
+
src2_ = CaptureImage(jsImg2);
|
|
32
|
+
} catch (const Napi::Error& e) {
|
|
33
|
+
captureFailed_ = true; SetError(e.Message());
|
|
34
|
+
} catch (const std::exception& e) {
|
|
35
|
+
captureFailed_ = true; SetError(e.what());
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
protected:
|
|
40
|
+
void Execute() override {
|
|
41
|
+
if (captureFailed_) return;
|
|
42
|
+
|
|
43
|
+
// Decode (encoded inputs only) on the worker thread
|
|
44
|
+
int64 t0 = cv::getTickCount();
|
|
45
|
+
src1_.Materialize();
|
|
46
|
+
src2_.Materialize();
|
|
47
|
+
const cv::Mat& mat1 = src1_.mat;
|
|
48
|
+
const cv::Mat& mat2 = src2_.mat;
|
|
49
|
+
format1 = src1_.colorSpace;
|
|
50
|
+
format2 = src2_.colorSpace;
|
|
51
|
+
|
|
42
52
|
// Determine output channel format - force BGRA for alpha compositing
|
|
43
53
|
if (alphaCompositing) {
|
|
44
54
|
outputChannel = "BGRA";
|
|
45
55
|
} else {
|
|
46
56
|
outputChannel = DetermineOutputFormat(format1, format2);
|
|
47
57
|
}
|
|
48
|
-
|
|
49
58
|
convertMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
50
|
-
}
|
|
51
59
|
|
|
52
|
-
|
|
53
|
-
void Execute() override {
|
|
54
|
-
const int64 t0 = cv::getTickCount();
|
|
60
|
+
t0 = cv::getTickCount();
|
|
55
61
|
|
|
56
62
|
// Convert both images to the same target format
|
|
57
63
|
cv::Mat img1 = ConvertToTargetFormatShared(mat1, format1, outputChannel);
|
|
@@ -98,6 +104,8 @@ protected:
|
|
|
98
104
|
if (outputFormat != "raw") {
|
|
99
105
|
cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
|
|
100
106
|
encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
|
|
107
|
+
} else {
|
|
108
|
+
FinalizeForOutput(result);
|
|
101
109
|
}
|
|
102
110
|
}
|
|
103
111
|
|
|
@@ -112,8 +120,13 @@ protected:
|
|
|
112
120
|
Callback().Call({env.Null(), out});
|
|
113
121
|
}
|
|
114
122
|
|
|
123
|
+
void OnError(const Napi::Error& e) override {
|
|
124
|
+
Callback().Call({ e.Value(), Env().Null() });
|
|
125
|
+
}
|
|
126
|
+
|
|
115
127
|
private:
|
|
116
|
-
|
|
128
|
+
ImageSource src1_, src2_;
|
|
129
|
+
cv::Mat result;
|
|
117
130
|
std::string format1, format2, outputChannel;
|
|
118
131
|
double opacity;
|
|
119
132
|
std::string outputFormat;
|
|
@@ -123,6 +136,7 @@ private:
|
|
|
123
136
|
bool removeBackground;
|
|
124
137
|
std::string backgroundColor;
|
|
125
138
|
double colorTolerance;
|
|
139
|
+
bool captureFailed_ = false;
|
|
126
140
|
double convertMs = 0, taskMs = 0, encodeMs = 0;
|
|
127
141
|
std::vector<uchar> encodedBuf;
|
|
128
142
|
};
|