@rosepetal/node-red-contrib-image-tools 1.6.4 → 1.8.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/.claude/rules/node-implementation-template.md +1 -1
- package/.github/workflows/build-prebuild.yml +1 -1
- package/README.md +41 -21
- package/docs/index.md +156 -0
- package/docs/nodes/blend/add-bbs.md +116 -0
- package/docs/nodes/blend/add-mask.md +67 -224
- package/docs/nodes/blend/add-masks.md +74 -196
- package/docs/nodes/blend/blend.md +84 -200
- package/docs/nodes/io/folder-in.md +92 -0
- package/docs/nodes/io/image-in.md +61 -52
- package/docs/nodes/io/image-out.md +114 -93
- package/docs/nodes/mix/advanced-mosaic.md +124 -78
- package/docs/nodes/mix/concat.md +71 -56
- package/docs/nodes/mix/mosaic.md +81 -91
- package/docs/nodes/specialized/cropBB.md +76 -214
- package/docs/nodes/specialized/heat-diff.md +111 -0
- package/docs/nodes/specialized/image-align.md +125 -387
- package/docs/nodes/transform/color-convert.md +101 -0
- package/docs/nodes/transform/crop.md +56 -58
- package/docs/nodes/transform/draw.md +46 -26
- package/docs/nodes/transform/filter.md +110 -46
- package/docs/nodes/transform/padding.md +55 -55
- package/docs/nodes/transform/resize.md +81 -56
- package/docs/nodes/transform/rotate.md +67 -50
- 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/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.js +93 -146
- 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 +11 -4
- package/node-red-contrib-image-tools/nodes/specialized/image-align.js +5 -4
- package/node-red-contrib-image-tools/nodes/transform/color-convert.js +3 -3
- package/node-red-contrib-image-tools/nodes/transform/crop.js +3 -3
- package/node-red-contrib-image-tools/nodes/transform/filter.js +3 -3
- package/node-red-contrib-image-tools/nodes/transform/padding.js +3 -3
- package/node-red-contrib-image-tools/nodes/transform/resize.js +3 -3
- 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 +35 -8
- package/rosepetal-image-engine/src/image-align.cpp +107 -35
- 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/build-opencv-static.sh +6 -0
- package/scripts/dual-register.js +32 -25
- package/scripts/sync-version.js +58 -41
|
@@ -51,17 +51,32 @@ public:
|
|
|
51
51
|
emitImage(emitImage),
|
|
52
52
|
computeSsim(computeSsim)
|
|
53
53
|
{
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
54
|
+
// JS thread: metadata + persistent references only (no decode, no pixels)
|
|
55
|
+
try {
|
|
56
|
+
src1_ = CaptureImage(jsImg1);
|
|
57
|
+
src2_ = CaptureImage(jsImg2);
|
|
58
|
+
} catch (const Napi::Error& e) {
|
|
59
|
+
captureFailed_ = true; SetError(e.Message());
|
|
60
|
+
} catch (const std::exception& e) {
|
|
61
|
+
captureFailed_ = true; SetError(e.what());
|
|
62
|
+
}
|
|
60
63
|
}
|
|
61
64
|
|
|
62
65
|
protected:
|
|
63
66
|
void Execute() override {
|
|
64
|
-
|
|
67
|
+
if (captureFailed_) return;
|
|
68
|
+
|
|
69
|
+
// Decode (encoded inputs only) on the worker thread
|
|
70
|
+
int64 t0 = cv::getTickCount();
|
|
71
|
+
src1_.Materialize();
|
|
72
|
+
src2_.Materialize();
|
|
73
|
+
const cv::Mat& mat1 = src1_.mat;
|
|
74
|
+
const cv::Mat& mat2 = src2_.mat;
|
|
75
|
+
format1 = src1_.colorSpace;
|
|
76
|
+
format2 = src2_.colorSpace;
|
|
77
|
+
convertMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
78
|
+
|
|
79
|
+
t0 = cv::getTickCount();
|
|
65
80
|
|
|
66
81
|
// Convert both to grayscale for diff computation
|
|
67
82
|
cv::Mat gray1, gray2;
|
|
@@ -94,6 +109,10 @@ protected:
|
|
|
94
109
|
cv::Mat diff;
|
|
95
110
|
cv::absdiff(gray1, gray2, diff);
|
|
96
111
|
|
|
112
|
+
// Pure black in either input is alignment border / padding (the stats below ignore it).
|
|
113
|
+
// Zero it BEFORE the blur too, or its huge diff bleeds ~ksize/2 px into the valid area.
|
|
114
|
+
diff.setTo(0, (gray1 == 0) | (gray2 == 0));
|
|
115
|
+
|
|
97
116
|
// Optional Gaussian blur to smooth noise
|
|
98
117
|
if (blurSize > 0) {
|
|
99
118
|
int ksize = blurSize | 1; // ensure odd
|
|
@@ -119,6 +138,8 @@ protected:
|
|
|
119
138
|
if (emitImage && outputFormat != "raw") {
|
|
120
139
|
cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
|
|
121
140
|
encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
|
|
141
|
+
} else if (emitImage) {
|
|
142
|
+
FinalizeForOutput(result);
|
|
122
143
|
}
|
|
123
144
|
}
|
|
124
145
|
|
|
@@ -167,6 +188,10 @@ protected:
|
|
|
167
188
|
Callback().Call({env.Null(), out});
|
|
168
189
|
}
|
|
169
190
|
|
|
191
|
+
void OnError(const Napi::Error& e) override {
|
|
192
|
+
Callback().Call({ e.Value(), Env().Null() });
|
|
193
|
+
}
|
|
194
|
+
|
|
170
195
|
private:
|
|
171
196
|
struct BlobStat { int x, y, w, h, area; double cx, cy; };
|
|
172
197
|
static constexpr size_t kMaxBlobs = 50;
|
|
@@ -237,7 +262,9 @@ private:
|
|
|
237
262
|
statBlobs = std::move(blobs);
|
|
238
263
|
}
|
|
239
264
|
|
|
240
|
-
|
|
265
|
+
ImageSource src1_, src2_;
|
|
266
|
+
bool captureFailed_ = false;
|
|
267
|
+
cv::Mat result;
|
|
241
268
|
std::string format1, format2, outputChannel;
|
|
242
269
|
int colormapType;
|
|
243
270
|
int blurSize;
|
|
@@ -158,14 +158,43 @@ static int ParseMotionModel(const std::string& name) {
|
|
|
158
158
|
// ECC - intensity-based ECC only, identity init (legacy default)
|
|
159
159
|
// FEATURES - ORB+RANSAC only, no ECC refinement
|
|
160
160
|
// FEATURES_ECC - ORB seed -> ECC refinement (best accuracy, recommended for new nodes)
|
|
161
|
-
|
|
161
|
+
// SHIFT - exhaustive translation search (template matching); always a pure
|
|
162
|
+
// translation, for low-texture or line-scan images where features fail
|
|
163
|
+
enum class AlignPipeline { ECC, FEATURES, FEATURES_ECC, SHIFT };
|
|
162
164
|
|
|
163
165
|
static AlignPipeline ParsePipeline(const std::string& name) {
|
|
164
166
|
if (name == "ecc") return AlignPipeline::ECC;
|
|
165
167
|
if (name == "features") return AlignPipeline::FEATURES;
|
|
166
168
|
if (name == "features+ecc") return AlignPipeline::FEATURES_ECC;
|
|
169
|
+
if (name == "shift") return AlignPipeline::SHIFT;
|
|
167
170
|
throw std::runtime_error("Unknown pipeline: " + name +
|
|
168
|
-
" (expected: ecc, features,
|
|
171
|
+
" (expected: ecc, features, features+ecc, or shift)");
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Shift search tuning. The window is a fraction of the image (or a floor in full-res px,
|
|
175
|
+
// whichever is larger), capped at a quarter so the template keeps half of each axis.
|
|
176
|
+
constexpr double kShiftRangeY = 0.10, kShiftRangeX = 0.04;
|
|
177
|
+
constexpr double kShiftFloorY = 150.0, kShiftFloorX = 60.0;
|
|
178
|
+
constexpr double kShiftMinScore = 0.15; // normalized correlation below this = no match
|
|
179
|
+
|
|
180
|
+
// Zero-mean, high-passed copy for the shift search. Removes the per-column gain (line-scan
|
|
181
|
+
// cameras: identical in both images, so it pulls any correlation toward zero shift), the
|
|
182
|
+
// per-line brightness (lamp flicker, different in every capture) and slow shading.
|
|
183
|
+
static cv::Mat FlattenForShift(const cv::Mat& gray, double sigma) {
|
|
184
|
+
cv::Mat f, colMean, rowMean, low;
|
|
185
|
+
gray.convertTo(f, CV_32F);
|
|
186
|
+
cv::reduce(f, colMean, 0, cv::REDUCE_AVG);
|
|
187
|
+
f -= cv::repeat(colMean, f.rows, 1);
|
|
188
|
+
cv::reduce(f, rowMean, 1, cv::REDUCE_AVG);
|
|
189
|
+
f -= cv::repeat(rowMean, 1, f.cols);
|
|
190
|
+
cv::GaussianBlur(f, low, cv::Size(0, 0), sigma);
|
|
191
|
+
return f - low;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Sub-pixel offset of a peak from its two neighbours (parabola through three samples).
|
|
195
|
+
static double ParabolicPeak(float left, float centre, float right) {
|
|
196
|
+
const float d = left - 2.0f * centre + right;
|
|
197
|
+
return std::abs(d) > 1e-12f ? 0.5 * (left - right) / d : 0.0;
|
|
169
198
|
}
|
|
170
199
|
|
|
171
200
|
// ECC refinement policy (only meaningful when pipeline == FEATURES_ECC):
|
|
@@ -243,8 +272,13 @@ public:
|
|
|
243
272
|
motionFlag = ParseMotionModel(motionModelName);
|
|
244
273
|
useHomography = (motionFlag == cv::MOTION_HOMOGRAPHY);
|
|
245
274
|
|
|
246
|
-
// Same for pipeline mode (ecc / features / features+ecc).
|
|
275
|
+
// Same for pipeline mode (ecc / features / features+ecc / shift).
|
|
247
276
|
pipelineMode = ParsePipeline(pipelineName);
|
|
277
|
+
// The shift search only ever returns a translation, whatever the motion model says
|
|
278
|
+
if (pipelineMode == AlignPipeline::SHIFT) {
|
|
279
|
+
motionFlag = cv::MOTION_TRANSLATION;
|
|
280
|
+
useHomography = false;
|
|
281
|
+
}
|
|
248
282
|
|
|
249
283
|
// And the ECC refinement policy (always / auto / never).
|
|
250
284
|
eccRefineMode = ParseEccRefine(eccRefineName);
|
|
@@ -252,9 +286,10 @@ public:
|
|
|
252
286
|
// And the feature detector (orb / sift).
|
|
253
287
|
detectorKind = ParseDetector(detectorName);
|
|
254
288
|
|
|
255
|
-
//
|
|
256
|
-
|
|
257
|
-
|
|
289
|
+
// Capture input images (metadata + persistent reference only;
|
|
290
|
+
// decoding happens in Execute() on the worker thread)
|
|
291
|
+
refSrc_ = CaptureImage(referenceImage);
|
|
292
|
+
tgtSrc_ = CaptureImage(targetImage);
|
|
258
293
|
|
|
259
294
|
// Parse polygon(s) if provided
|
|
260
295
|
if (!polygonValue.IsNull() && !polygonValue.IsUndefined() && polygonValue.IsArray()) {
|
|
@@ -322,24 +357,34 @@ public:
|
|
|
322
357
|
}
|
|
323
358
|
}
|
|
324
359
|
|
|
325
|
-
// Detect channel format for output
|
|
326
|
-
referenceChannelOrder = DetectChannelFormat(referenceImage, referenceMat);
|
|
327
|
-
targetChannelOrder = DetectChannelFormat(targetImage, targetMat);
|
|
328
|
-
|
|
329
|
-
// Use reference image format for output
|
|
330
|
-
outputChannelOrder = referenceChannelOrder;
|
|
331
|
-
|
|
332
360
|
auto t1 = std::chrono::steady_clock::now();
|
|
333
361
|
convertMs = std::chrono::duration<double, std::milli>(t1 - t0).count();
|
|
334
362
|
|
|
335
363
|
} catch (const Napi::Error& e) {
|
|
364
|
+
captureFailed_ = true;
|
|
336
365
|
SetError(e.Message());
|
|
366
|
+
} catch (const std::exception& e) {
|
|
367
|
+
captureFailed_ = true;
|
|
368
|
+
SetError(e.what());
|
|
337
369
|
}
|
|
338
370
|
}
|
|
339
371
|
|
|
340
372
|
protected:
|
|
341
373
|
void Execute() override {
|
|
374
|
+
if (captureFailed_) return;
|
|
342
375
|
try {
|
|
376
|
+
// Decode (encoded inputs only) on the worker thread
|
|
377
|
+
auto decodeStart = std::chrono::steady_clock::now();
|
|
378
|
+
refSrc_.Materialize();
|
|
379
|
+
tgtSrc_.Materialize();
|
|
380
|
+
referenceMat = refSrc_.mat;
|
|
381
|
+
targetMat = tgtSrc_.mat;
|
|
382
|
+
referenceChannelOrder = refSrc_.colorSpace;
|
|
383
|
+
targetChannelOrder = tgtSrc_.colorSpace;
|
|
384
|
+
outputChannelOrder = referenceChannelOrder; // reference format drives the output
|
|
385
|
+
convertMs += std::chrono::duration<double, std::milli>(
|
|
386
|
+
std::chrono::steady_clock::now() - decodeStart).count();
|
|
387
|
+
|
|
343
388
|
auto taskStart = std::chrono::steady_clock::now();
|
|
344
389
|
|
|
345
390
|
// Get reference dimensions
|
|
@@ -418,6 +463,16 @@ protected:
|
|
|
418
463
|
|
|
419
464
|
auto taskEnd = std::chrono::steady_clock::now();
|
|
420
465
|
taskMs = std::chrono::duration<double, std::milli>(taskEnd - taskStart).count();
|
|
466
|
+
|
|
467
|
+
// Encode on the worker thread (never on the JS thread)
|
|
468
|
+
auto encodeStart = std::chrono::steady_clock::now();
|
|
469
|
+
if (outputFormat == "raw") {
|
|
470
|
+
FinalizeForOutput(alignedImage);
|
|
471
|
+
} else {
|
|
472
|
+
EncodeToFormat(alignedImage, encodedBuf_, outputFormat, quality, pngOptimize);
|
|
473
|
+
}
|
|
474
|
+
encodeMs = std::chrono::duration<double, std::milli>(
|
|
475
|
+
std::chrono::steady_clock::now() - encodeStart).count();
|
|
421
476
|
|
|
422
477
|
} catch (const std::exception& e) {
|
|
423
478
|
SetError(std::string("Image alignment failed: ") + e.what());
|
|
@@ -428,22 +483,15 @@ protected:
|
|
|
428
483
|
Napi::Env env = Env();
|
|
429
484
|
|
|
430
485
|
try {
|
|
431
|
-
auto encodeStart = std::chrono::steady_clock::now();
|
|
432
486
|
Napi::Value result;
|
|
433
487
|
|
|
434
488
|
if (outputFormat == "raw") {
|
|
435
|
-
// Return raw image object
|
|
489
|
+
// Return raw image object (zero-copy: memory is already owned)
|
|
436
490
|
result = MatToRawJS(env, alignedImage, outputChannelOrder);
|
|
437
491
|
} else {
|
|
438
|
-
|
|
439
|
-
std::vector<uchar> encoded;
|
|
440
|
-
EncodeToFormat(alignedImage, encoded, outputFormat, quality, pngOptimize);
|
|
441
|
-
result = VectorToBuffer(env, std::move(encoded));
|
|
492
|
+
result = VectorToBuffer(env, std::move(encodedBuf_));
|
|
442
493
|
}
|
|
443
494
|
|
|
444
|
-
auto encodeEnd = std::chrono::steady_clock::now();
|
|
445
|
-
encodeMs = std::chrono::duration<double, std::milli>(encodeEnd - encodeStart).count();
|
|
446
|
-
|
|
447
495
|
// Create response object
|
|
448
496
|
Napi::Object response = Napi::Object::New(env);
|
|
449
497
|
response.Set("image", result);
|
|
@@ -558,6 +606,9 @@ private:
|
|
|
558
606
|
bool returnMatrix;
|
|
559
607
|
|
|
560
608
|
// Image data
|
|
609
|
+
ImageSource refSrc_, tgtSrc_;
|
|
610
|
+
bool captureFailed_ = false;
|
|
611
|
+
std::vector<uchar> encodedBuf_;
|
|
561
612
|
cv::Mat referenceMat, targetMat, alignedImage;
|
|
562
613
|
cv::Mat transformationMatrix;
|
|
563
614
|
std::string referenceChannelOrder, targetChannelOrder, outputChannelOrder;
|
|
@@ -594,19 +645,6 @@ private:
|
|
|
594
645
|
double taskMs = 0.0;
|
|
595
646
|
double encodeMs = 0.0;
|
|
596
647
|
|
|
597
|
-
// Helper to detect channel format
|
|
598
|
-
std::string DetectChannelFormat(const Napi::Value& jsImg, const cv::Mat& mat) {
|
|
599
|
-
if (jsImg.IsObject() && !jsImg.IsBuffer()) {
|
|
600
|
-
Napi::Object obj = jsImg.As<Napi::Object>();
|
|
601
|
-
if (obj.Has("colorSpace")) {
|
|
602
|
-
return obj.Get("colorSpace").As<Napi::String>().Utf8Value();
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
// Default based on channel count
|
|
606
|
-
const int channels = mat.channels();
|
|
607
|
-
return (channels == 4) ? "RGBA" : (channels == 3) ? "RGB" : "GRAY";
|
|
608
|
-
}
|
|
609
|
-
|
|
610
648
|
// Convert image to grayscale
|
|
611
649
|
void ConvertToGray(const cv::Mat& src, cv::Mat& gray) {
|
|
612
650
|
if (src.channels() == 1) {
|
|
@@ -845,7 +883,38 @@ private:
|
|
|
845
883
|
}
|
|
846
884
|
}
|
|
847
885
|
|
|
886
|
+
// Exhaustive translation search on the small frames: the centre of the target slides
|
|
887
|
+
// over the reference within the shift window (matchTemplate, normalized correlation),
|
|
888
|
+
// then a parabolic sub-pixel step. Fails (no matrix) on a weak peak or a peak on the
|
|
889
|
+
// window edge, where the true shift may lie outside: invalid beats a wrong warp.
|
|
890
|
+
bool FindShift(const cv::Mat& refSmall, const cv::Mat& targetSmall, cv::Mat& transformMatrix) const {
|
|
891
|
+
const cv::Mat ref = FlattenForShift(refSmall, std::max(2.0, 16.0 * scale));
|
|
892
|
+
const cv::Mat tgt = FlattenForShift(targetSmall, std::max(2.0, 16.0 * scale));
|
|
893
|
+
const int W = std::min(ref.cols, tgt.cols), H = std::min(ref.rows, tgt.rows);
|
|
894
|
+
const int sx = std::min(W / 4, static_cast<int>(std::ceil(std::max(kShiftRangeX * W, kShiftFloorX * scale))));
|
|
895
|
+
const int sy = std::min(H / 4, static_cast<int>(std::ceil(std::max(kShiftRangeY * H, kShiftFloorY * scale))));
|
|
896
|
+
if (W - 2 * sx < 16 || H - 2 * sy < 16) return false;
|
|
897
|
+
|
|
898
|
+
cv::Mat res;
|
|
899
|
+
cv::matchTemplate(ref(cv::Rect(0, 0, W, H)), tgt(cv::Rect(sx, sy, W - 2 * sx, H - 2 * sy)),
|
|
900
|
+
res, cv::TM_CCOEFF_NORMED);
|
|
901
|
+
double best = 0.0;
|
|
902
|
+
cv::Point p;
|
|
903
|
+
cv::minMaxLoc(res, nullptr, &best, nullptr, &p);
|
|
904
|
+
if (!std::isfinite(best) || best < kShiftMinScore) return false;
|
|
905
|
+
if (p.x == 0 || p.y == 0 || p.x == res.cols - 1 || p.y == res.rows - 1) return false;
|
|
906
|
+
|
|
907
|
+
const double fx = p.x + ParabolicPeak(res.at<float>(p.y, p.x - 1), res.at<float>(p.y, p.x), res.at<float>(p.y, p.x + 1));
|
|
908
|
+
const double fy = p.y + ParabolicPeak(res.at<float>(p.y - 1, p.x), res.at<float>(p.y, p.x), res.at<float>(p.y + 1, p.x));
|
|
909
|
+
// target (x, y) lies on reference (x + dx, y + dy); the warp matrix maps reference -> target
|
|
910
|
+
const double dx = (fx - sx) / scale, dy = (fy - sy) / scale;
|
|
911
|
+
transformMatrix = (cv::Mat_<float>(2, 3) << 1.0f, 0.0f, static_cast<float>(-dx),
|
|
912
|
+
0.0f, 1.0f, static_cast<float>(-dy));
|
|
913
|
+
return true;
|
|
914
|
+
}
|
|
915
|
+
|
|
848
916
|
// Find transformation matrix - dispatches on the configured pipeline:
|
|
917
|
+
// SHIFT -> FindShift (exhaustive translation search, no ECC)
|
|
849
918
|
// ECC -> identity init -> findTransformECC (legacy path)
|
|
850
919
|
// FEATURES -> ORB+RANSAC seed only, no ECC
|
|
851
920
|
// FEATURES_ECC -> ORB seed -> findTransformECC refinement.
|
|
@@ -889,6 +958,9 @@ private:
|
|
|
889
958
|
resizeOrCached(refGray, refSmall);
|
|
890
959
|
resizeOrCached(targetGray, targetSmall);
|
|
891
960
|
|
|
961
|
+
if (pipelineMode == AlignPipeline::SHIFT)
|
|
962
|
+
return FindShift(refSmall, targetSmall, transformMatrix);
|
|
963
|
+
|
|
892
964
|
// -------- Stage 1: optional ORB feature seed -----------------
|
|
893
965
|
cv::Mat seedMatrix; // small-frame coords
|
|
894
966
|
int seedInliers = 0;
|
|
@@ -16,6 +16,8 @@ Napi::Value ImageAlign(const Napi::CallbackInfo& info);
|
|
|
16
16
|
Napi::Value Draw(const Napi::CallbackInfo& info);
|
|
17
17
|
Napi::Value ColorConvert(const Napi::CallbackInfo& info);
|
|
18
18
|
Napi::Value HeatDiff(const Napi::CallbackInfo& info);
|
|
19
|
+
Napi::Value Decode(const Napi::CallbackInfo& info);
|
|
20
|
+
Napi::Value Encode(const Napi::CallbackInfo& info);
|
|
19
21
|
|
|
20
22
|
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
21
23
|
exports.Set(Napi::String::New(env, "resize"), Napi::Function::New(env, Resize));
|
|
@@ -34,6 +36,8 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
|
34
36
|
exports.Set(Napi::String::New(env, "draw"), Napi::Function::New(env, Draw));
|
|
35
37
|
exports.Set(Napi::String::New(env, "colorConvert"), Napi::Function::New(env, ColorConvert));
|
|
36
38
|
exports.Set(Napi::String::New(env, "heatDiff"), Napi::Function::New(env, HeatDiff));
|
|
39
|
+
exports.Set(Napi::String::New(env, "decode"), Napi::Function::New(env, Decode));
|
|
40
|
+
exports.Set(Napi::String::New(env, "encode"), Napi::Function::New(env, Encode));
|
|
37
41
|
return exports;
|
|
38
42
|
}
|
|
39
43
|
|
|
@@ -46,42 +46,18 @@ public:
|
|
|
46
46
|
normalized_(normalized), outputFormat_(std::move(outputFormat)),
|
|
47
47
|
quality_(quality), pngOptimize_(pngOptimize)
|
|
48
48
|
{
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
imageChannels_.reserve(imagesArray.Length());
|
|
55
|
-
positions_.reserve(positionsArray.Length());
|
|
56
|
-
|
|
57
|
-
// Convert images with zero-copy operations and extract channel formats
|
|
58
|
-
for (uint32_t i = 0; i < imagesArray.Length(); i++) {
|
|
59
|
-
images_.emplace_back(ConvertToMat(imagesArray[i]));
|
|
60
|
-
|
|
61
|
-
// Extract and store channel format for each image (like other nodes do)
|
|
62
|
-
std::string channel;
|
|
63
|
-
if (imagesArray[i].IsObject() && !imagesArray[i].IsBuffer()) {
|
|
64
|
-
Napi::Object obj = imagesArray[i].As<Napi::Object>();
|
|
65
|
-
|
|
66
|
-
// Check for new colorSpace field first
|
|
67
|
-
if (obj.Has("colorSpace")) {
|
|
68
|
-
channel = obj.Get("colorSpace").As<Napi::String>().Utf8Value();
|
|
69
|
-
}
|
|
70
|
-
// Default based on channel count
|
|
71
|
-
else {
|
|
72
|
-
channel = (images_[i].channels() == 4) ? "RGBA"
|
|
73
|
-
: (images_[i].channels() == 3) ? "RGB"
|
|
74
|
-
: "GRAY";
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
// Buffer input - determine from OpenCV Mat
|
|
78
|
-
channel = (images_[i].channels() == 4) ? "RGBA"
|
|
79
|
-
: (images_[i].channels() == 3) ? "RGB"
|
|
80
|
-
: "GRAY";
|
|
49
|
+
// JS thread: metadata + persistent references only (no decode, no pixels)
|
|
50
|
+
try {
|
|
51
|
+
sources_.reserve(imagesArray.Length());
|
|
52
|
+
for (uint32_t i = 0; i < imagesArray.Length(); i++) {
|
|
53
|
+
sources_.emplace_back(CaptureImage(imagesArray[i]));
|
|
81
54
|
}
|
|
82
|
-
|
|
83
|
-
|
|
55
|
+
} catch (const Napi::Error& e) {
|
|
56
|
+
captureFailed_ = true; SetError(e.Message());
|
|
57
|
+
} catch (const std::exception& e) {
|
|
58
|
+
captureFailed_ = true; SetError(e.what());
|
|
84
59
|
}
|
|
60
|
+
positions_.reserve(positionsArray.Length());
|
|
85
61
|
|
|
86
62
|
// Parse positions - ultra-fast integer operations
|
|
87
63
|
for (uint32_t i = 0; i < positionsArray.Length(); i++) {
|
|
@@ -92,17 +68,28 @@ public:
|
|
|
92
68
|
p.y = pos.Get("y").As<Napi::Number>().DoubleValue();
|
|
93
69
|
positions_.emplace_back(p);
|
|
94
70
|
}
|
|
95
|
-
|
|
96
|
-
// Determine the best canvas format from all input images (priority: RGBA > BGRA > RGB > BGR > GRAY)
|
|
97
|
-
canvasChannel_ = DetermineBestCanvasFormat(imageChannels_);
|
|
98
|
-
|
|
99
|
-
convertMs_ = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
100
71
|
}
|
|
101
72
|
|
|
102
73
|
protected:
|
|
103
74
|
void Execute() override {
|
|
75
|
+
if (captureFailed_) return;
|
|
76
|
+
|
|
77
|
+
/* ─ Decode (encoded inputs only) + channel detection, worker thread ─ */
|
|
78
|
+
int64 t0 = cv::getTickCount();
|
|
79
|
+
images_.clear(); imageChannels_.clear();
|
|
80
|
+
images_.reserve(sources_.size());
|
|
81
|
+
imageChannels_.reserve(sources_.size());
|
|
82
|
+
for (auto& s : sources_) {
|
|
83
|
+
s.Materialize();
|
|
84
|
+
images_.push_back(s.mat);
|
|
85
|
+
imageChannels_.push_back(s.colorSpace);
|
|
86
|
+
}
|
|
87
|
+
// Determine the best canvas format from all input images (priority: RGBA > BGRA > RGB > BGR > GRAY)
|
|
88
|
+
canvasChannel_ = DetermineBestCanvasFormat(imageChannels_);
|
|
89
|
+
convertMs_ = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
90
|
+
|
|
104
91
|
/* ─ SUPER FAST mosaic composition ─ */
|
|
105
|
-
|
|
92
|
+
t0 = cv::getTickCount();
|
|
106
93
|
|
|
107
94
|
// Parse background color - optimized hex parsing (returns BGR format)
|
|
108
95
|
cv::Scalar bgColor = ParseColor(backgroundColor_, cv::Scalar(0, 0, 0));
|
|
@@ -148,6 +135,8 @@ protected:
|
|
|
148
135
|
const cv::Mat srcForEncoding =
|
|
149
136
|
PrepareForEncoding(canvas_, canvasChannel_, outputFormat_);
|
|
150
137
|
encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat_, quality_, pngOptimize_);
|
|
138
|
+
} else {
|
|
139
|
+
FinalizeForOutput(canvas_);
|
|
151
140
|
}
|
|
152
141
|
}
|
|
153
142
|
|
|
@@ -303,6 +292,8 @@ private:
|
|
|
303
292
|
}
|
|
304
293
|
|
|
305
294
|
// Member variables
|
|
295
|
+
std::vector<ImageSource> sources_; // JS inputs (decoded in Execute)
|
|
296
|
+
bool captureFailed_ = false;
|
|
306
297
|
std::vector<cv::Mat> images_;
|
|
307
298
|
std::vector<std::string> imageChannels_; // Channel format for each image
|
|
308
299
|
std::vector<Position> positions_;
|
|
@@ -20,45 +20,40 @@ public:
|
|
|
20
20
|
quality(quality),
|
|
21
21
|
pngOptimize(pngOptimize)
|
|
22
22
|
{
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
// Check for new colorSpace field first
|
|
32
|
-
if (obj.Has("colorSpace")) {
|
|
33
|
-
channel_ = obj.Get("colorSpace").As<Napi::String>().Utf8Value();
|
|
34
|
-
}
|
|
35
|
-
// Default based on channel count
|
|
36
|
-
else {
|
|
37
|
-
channel_ = (src_.channels() == 4) ? "RGBA"
|
|
38
|
-
: (src_.channels() == 3) ? "RGB"
|
|
39
|
-
: "GRAY";
|
|
40
|
-
}
|
|
41
|
-
} else {
|
|
42
|
-
// Buffer input - determine from OpenCV Mat
|
|
43
|
-
channel_ = (src_.channels() == 4) ? "RGBA"
|
|
44
|
-
: (src_.channels() == 3) ? "RGB"
|
|
45
|
-
: "GRAY";
|
|
23
|
+
// JS thread: metadata + persistent reference only (no decode, no pixels)
|
|
24
|
+
try {
|
|
25
|
+
src_ = CaptureImage(imgVal);
|
|
26
|
+
} catch (const Napi::Error& e) {
|
|
27
|
+
captureFailed_ = true; SetError(e.Message());
|
|
28
|
+
} catch (const std::exception& e) {
|
|
29
|
+
captureFailed_ = true; SetError(e.what());
|
|
46
30
|
}
|
|
47
|
-
|
|
48
|
-
padClrImg=padColorRGB;
|
|
49
|
-
if(channel_=="RGB"||channel_=="RGBA") std::swap(padClrImg[0],padClrImg[2]);
|
|
50
31
|
}
|
|
51
32
|
|
|
52
33
|
protected:
|
|
53
34
|
void Execute() override {
|
|
54
|
-
|
|
55
|
-
|
|
35
|
+
if (captureFailed_) return;
|
|
36
|
+
|
|
37
|
+
/* convertMs — decode on the worker thread (raw input is zero‑copy) */
|
|
38
|
+
int64 t0=cv::getTickCount();
|
|
39
|
+
src_.Materialize();
|
|
40
|
+
const cv::Mat& srcMat = src_.mat;
|
|
41
|
+
channel_ = src_.colorSpace;
|
|
42
|
+
convertMs_=(cv::getTickCount()-t0)/cv::getTickFrequency()*1e3;
|
|
43
|
+
|
|
44
|
+
padClrImg=padColorRGB;
|
|
45
|
+
if(channel_=="RGB"||channel_=="RGBA") std::swap(padClrImg[0],padClrImg[2]);
|
|
46
|
+
|
|
47
|
+
t0=cv::getTickCount();
|
|
48
|
+
cv::copyMakeBorder(srcMat,dst_,t_,b_,l_,r_,cv::BORDER_CONSTANT,padClrImg);
|
|
56
49
|
taskMs_=(cv::getTickCount()-t0)/cv::getTickFrequency()*1e3;
|
|
57
50
|
|
|
58
51
|
if (outputFormat != "raw") {
|
|
59
52
|
const cv::Mat srcForEncoding =
|
|
60
53
|
PrepareForEncoding(dst_, channel_, outputFormat);
|
|
61
54
|
encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat, quality, pngOptimize);
|
|
55
|
+
} else {
|
|
56
|
+
FinalizeForOutput(dst_);
|
|
62
57
|
}
|
|
63
58
|
}
|
|
64
59
|
|
|
@@ -73,14 +68,20 @@ protected:
|
|
|
73
68
|
Callback().Call({env.Null(),res});
|
|
74
69
|
}
|
|
75
70
|
|
|
71
|
+
void OnError(const Napi::Error& e) override {
|
|
72
|
+
Callback().Call({ e.Value(), Env().Null() });
|
|
73
|
+
}
|
|
74
|
+
|
|
76
75
|
private:
|
|
77
|
-
|
|
76
|
+
ImageSource src_;
|
|
77
|
+
cv::Mat dst_;
|
|
78
78
|
int t_,b_,l_,r_;
|
|
79
79
|
cv::Scalar padColorRGB,padClrImg;
|
|
80
80
|
std::string channel_;
|
|
81
81
|
std::string outputFormat;
|
|
82
82
|
int quality;
|
|
83
83
|
bool pngOptimize;
|
|
84
|
+
bool captureFailed_ = false;
|
|
84
85
|
|
|
85
86
|
double convertMs_{0},taskMs_{0},encodeMs_{0};
|
|
86
87
|
std::vector<uchar> encodedBuf_;
|
|
@@ -26,39 +26,30 @@ ResizeWorker(Napi::Function& callback,
|
|
|
26
26
|
heightMode(std::move(heightMode)), heightValue(heightValue),
|
|
27
27
|
outputFormat(std::move(outputFormat)), quality(quality), pngOptimize(pngOptimize){
|
|
28
28
|
|
|
29
|
+
// JS thread: metadata + persistent reference only (no decode, no pixels)
|
|
29
30
|
try {
|
|
30
|
-
|
|
31
|
-
inputMat = ConvertToMat(inputImage);
|
|
32
|
-
auto t1 = std::chrono::steady_clock::now();
|
|
33
|
-
convertMs = std::chrono::duration<double, std::milli>(t1 - t0).count();
|
|
34
|
-
|
|
35
|
-
if (inputImage.IsObject() && !inputImage.IsBuffer()) {
|
|
36
|
-
Napi::Object obj = inputImage.As<Napi::Object>();
|
|
37
|
-
|
|
38
|
-
// Check for colorSpace field
|
|
39
|
-
if (obj.Has("colorSpace")) {
|
|
40
|
-
channelOrder = obj.Get("colorSpace").As<Napi::String>().Utf8Value();
|
|
41
|
-
}
|
|
42
|
-
// Default based on channel count
|
|
43
|
-
else {
|
|
44
|
-
channelOrder = (inputMat.channels() == 4) ? "RGBA"
|
|
45
|
-
: (inputMat.channels() == 3) ? "RGB"
|
|
46
|
-
: "GRAY";
|
|
47
|
-
}
|
|
48
|
-
} else {
|
|
49
|
-
// Buffer input - determine from OpenCV Mat
|
|
50
|
-
channelOrder = (inputMat.channels() == 4) ? "RGBA"
|
|
51
|
-
: (inputMat.channels() == 3) ? "RGB"
|
|
52
|
-
: "GRAY";
|
|
53
|
-
}
|
|
31
|
+
src_ = CaptureImage(inputImage);
|
|
54
32
|
} catch (const Napi::Error& e) {
|
|
55
|
-
SetError(e.Message());
|
|
33
|
+
captureFailed_ = true; SetError(e.Message());
|
|
34
|
+
} catch (const std::exception& e) {
|
|
35
|
+
captureFailed_ = true; SetError(e.what());
|
|
56
36
|
}
|
|
57
37
|
}
|
|
58
38
|
|
|
59
39
|
protected:
|
|
60
40
|
void Execute() override {
|
|
41
|
+
if (captureFailed_) return;
|
|
61
42
|
try {
|
|
43
|
+
// --- 4.0 Decode (encoded inputs only) on the worker thread ---
|
|
44
|
+
{
|
|
45
|
+
auto tc0 = std::chrono::steady_clock::now();
|
|
46
|
+
src_.Materialize();
|
|
47
|
+
inputMat = src_.mat;
|
|
48
|
+
channelOrder = src_.colorSpace;
|
|
49
|
+
auto tc1 = std::chrono::steady_clock::now();
|
|
50
|
+
convertMs = std::chrono::duration<double, std::milli>(tc1 - tc0).count();
|
|
51
|
+
}
|
|
52
|
+
|
|
62
53
|
// --- 4.1 Calcular ancho/alto objetivo --------------------------
|
|
63
54
|
auto safeRoundToInt = [](double value) -> int {
|
|
64
55
|
if (!std::isfinite(value)) {
|
|
@@ -179,6 +170,8 @@ protected:
|
|
|
179
170
|
const cv::Mat srcForEncoding =
|
|
180
171
|
PrepareForEncoding(resultMat, channelOrder, outputFormat);
|
|
181
172
|
encodeMs = EncodeToFormat(srcForEncoding, encodedBuf, outputFormat, quality, pngOptimize);
|
|
173
|
+
} else {
|
|
174
|
+
FinalizeForOutput(resultMat);
|
|
182
175
|
}
|
|
183
176
|
} catch (const std::exception& e) {
|
|
184
177
|
SetError(e.what());
|
|
@@ -219,6 +212,8 @@ protected:
|
|
|
219
212
|
}
|
|
220
213
|
|
|
221
214
|
private:
|
|
215
|
+
ImageSource src_;
|
|
216
|
+
bool captureFailed_ = false;
|
|
222
217
|
cv::Mat inputMat, resultMat;
|
|
223
218
|
int targetWidth = 0;
|
|
224
219
|
int targetHeight = 0;
|