@rosepetal/node-red-contrib-image-tools 1.6.4 → 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.
Files changed (46) hide show
  1. package/.claude/rules/node-implementation-template.md +1 -1
  2. package/README.md +2 -0
  3. package/node-red-contrib-image-tools/lib/bmp-decode.js +18 -68
  4. package/node-red-contrib-image-tools/lib/cpp-bridge.js +202 -104
  5. package/node-red-contrib-image-tools/lib/node-utils.js +78 -78
  6. package/node-red-contrib-image-tools/nodes/io/folder-in.js +98 -46
  7. package/node-red-contrib-image-tools/nodes/io/image-in.js +3 -4
  8. package/node-red-contrib-image-tools/nodes/io/image-out.js +93 -146
  9. package/node-red-contrib-image-tools/nodes/specialized/cropBB.js +3 -3
  10. package/node-red-contrib-image-tools/nodes/specialized/heat-diff.js +3 -3
  11. package/node-red-contrib-image-tools/nodes/specialized/image-align.js +3 -3
  12. package/node-red-contrib-image-tools/nodes/transform/color-convert.js +3 -3
  13. package/node-red-contrib-image-tools/nodes/transform/crop.js +3 -3
  14. package/node-red-contrib-image-tools/nodes/transform/filter.js +3 -3
  15. package/node-red-contrib-image-tools/nodes/transform/padding.js +3 -3
  16. package/node-red-contrib-image-tools/nodes/transform/resize.js +3 -3
  17. package/node-red-contrib-image-tools/nodes/transform/rotate.js +3 -3
  18. package/npm/darwin-arm64/package.json +1 -1
  19. package/npm/darwin-x64/package.json +1 -1
  20. package/npm/linux-arm64/package.json +1 -1
  21. package/npm/linux-x64/package.json +1 -1
  22. package/npm/linuxmusl-x64/package.json +1 -1
  23. package/npm/win32-x64/package.json +1 -1
  24. package/package.json +6 -6
  25. package/rosepetal-image-engine/binding.gyp +2 -1
  26. package/rosepetal-image-engine/src/add-bbs.cpp +123 -123
  27. package/rosepetal-image-engine/src/add-mask.cpp +30 -15
  28. package/rosepetal-image-engine/src/add-masks.cpp +145 -102
  29. package/rosepetal-image-engine/src/advanced-mosaic.cpp +200 -129
  30. package/rosepetal-image-engine/src/blend.cpp +34 -20
  31. package/rosepetal-image-engine/src/codec.cpp +157 -0
  32. package/rosepetal-image-engine/src/color-convert.cpp +22 -19
  33. package/rosepetal-image-engine/src/concat.cpp +38 -43
  34. package/rosepetal-image-engine/src/crop.cpp +23 -26
  35. package/rosepetal-image-engine/src/draw.cpp +33 -10
  36. package/rosepetal-image-engine/src/filter.cpp +22 -26
  37. package/rosepetal-image-engine/src/heat-diff.cpp +31 -8
  38. package/rosepetal-image-engine/src/image-align.cpp +36 -32
  39. package/rosepetal-image-engine/src/main.cpp +4 -0
  40. package/rosepetal-image-engine/src/mosaic.cpp +31 -40
  41. package/rosepetal-image-engine/src/padding.cpp +30 -29
  42. package/rosepetal-image-engine/src/resize.cpp +20 -25
  43. package/rosepetal-image-engine/src/rotate.cpp +22 -23
  44. package/rosepetal-image-engine/src/utils.h +243 -144
  45. package/scripts/dual-register.js +32 -25
  46. package/scripts/sync-version.js +58 -41
@@ -252,9 +252,10 @@ public:
252
252
  // And the feature detector (orb / sift).
253
253
  detectorKind = ParseDetector(detectorName);
254
254
 
255
- // Convert input images to OpenCV Mat
256
- referenceMat = ConvertToMat(referenceImage);
257
- targetMat = ConvertToMat(targetImage);
255
+ // Capture input images (metadata + persistent reference only;
256
+ // decoding happens in Execute() on the worker thread)
257
+ refSrc_ = CaptureImage(referenceImage);
258
+ tgtSrc_ = CaptureImage(targetImage);
258
259
 
259
260
  // Parse polygon(s) if provided
260
261
  if (!polygonValue.IsNull() && !polygonValue.IsUndefined() && polygonValue.IsArray()) {
@@ -322,24 +323,34 @@ public:
322
323
  }
323
324
  }
324
325
 
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
326
  auto t1 = std::chrono::steady_clock::now();
333
327
  convertMs = std::chrono::duration<double, std::milli>(t1 - t0).count();
334
328
 
335
329
  } catch (const Napi::Error& e) {
330
+ captureFailed_ = true;
336
331
  SetError(e.Message());
332
+ } catch (const std::exception& e) {
333
+ captureFailed_ = true;
334
+ SetError(e.what());
337
335
  }
338
336
  }
339
337
 
340
338
  protected:
341
339
  void Execute() override {
340
+ if (captureFailed_) return;
342
341
  try {
342
+ // Decode (encoded inputs only) on the worker thread
343
+ auto decodeStart = std::chrono::steady_clock::now();
344
+ refSrc_.Materialize();
345
+ tgtSrc_.Materialize();
346
+ referenceMat = refSrc_.mat;
347
+ targetMat = tgtSrc_.mat;
348
+ referenceChannelOrder = refSrc_.colorSpace;
349
+ targetChannelOrder = tgtSrc_.colorSpace;
350
+ outputChannelOrder = referenceChannelOrder; // reference format drives the output
351
+ convertMs += std::chrono::duration<double, std::milli>(
352
+ std::chrono::steady_clock::now() - decodeStart).count();
353
+
343
354
  auto taskStart = std::chrono::steady_clock::now();
344
355
 
345
356
  // Get reference dimensions
@@ -418,6 +429,16 @@ protected:
418
429
 
419
430
  auto taskEnd = std::chrono::steady_clock::now();
420
431
  taskMs = std::chrono::duration<double, std::milli>(taskEnd - taskStart).count();
432
+
433
+ // Encode on the worker thread (never on the JS thread)
434
+ auto encodeStart = std::chrono::steady_clock::now();
435
+ if (outputFormat == "raw") {
436
+ FinalizeForOutput(alignedImage);
437
+ } else {
438
+ EncodeToFormat(alignedImage, encodedBuf_, outputFormat, quality, pngOptimize);
439
+ }
440
+ encodeMs = std::chrono::duration<double, std::milli>(
441
+ std::chrono::steady_clock::now() - encodeStart).count();
421
442
 
422
443
  } catch (const std::exception& e) {
423
444
  SetError(std::string("Image alignment failed: ") + e.what());
@@ -428,22 +449,15 @@ protected:
428
449
  Napi::Env env = Env();
429
450
 
430
451
  try {
431
- auto encodeStart = std::chrono::steady_clock::now();
432
452
  Napi::Value result;
433
453
 
434
454
  if (outputFormat == "raw") {
435
- // Return raw image object
455
+ // Return raw image object (zero-copy: memory is already owned)
436
456
  result = MatToRawJS(env, alignedImage, outputChannelOrder);
437
457
  } else {
438
- // Encode to specified format
439
- std::vector<uchar> encoded;
440
- EncodeToFormat(alignedImage, encoded, outputFormat, quality, pngOptimize);
441
- result = VectorToBuffer(env, std::move(encoded));
458
+ result = VectorToBuffer(env, std::move(encodedBuf_));
442
459
  }
443
460
 
444
- auto encodeEnd = std::chrono::steady_clock::now();
445
- encodeMs = std::chrono::duration<double, std::milli>(encodeEnd - encodeStart).count();
446
-
447
461
  // Create response object
448
462
  Napi::Object response = Napi::Object::New(env);
449
463
  response.Set("image", result);
@@ -558,6 +572,9 @@ private:
558
572
  bool returnMatrix;
559
573
 
560
574
  // Image data
575
+ ImageSource refSrc_, tgtSrc_;
576
+ bool captureFailed_ = false;
577
+ std::vector<uchar> encodedBuf_;
561
578
  cv::Mat referenceMat, targetMat, alignedImage;
562
579
  cv::Mat transformationMatrix;
563
580
  std::string referenceChannelOrder, targetChannelOrder, outputChannelOrder;
@@ -594,19 +611,6 @@ private:
594
611
  double taskMs = 0.0;
595
612
  double encodeMs = 0.0;
596
613
 
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
614
  // Convert image to grayscale
611
615
  void ConvertToGray(const cv::Mat& src, cv::Mat& gray) {
612
616
  if (src.channels() == 1) {
@@ -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
- /* ─ SUPER FAST image conversion with timing ─ */
50
- const int64 t0 = cv::getTickCount();
51
-
52
- // Pre-allocate vectors for maximum performance
53
- images_.reserve(imagesArray.Length());
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
- imageChannels_.emplace_back(channel);
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
- const int64 t0 = cv::getTickCount();
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
- /* convertMs — zero‑copy → cv::Mat */
24
- const int64 t0=cv::getTickCount();
25
- src_=ConvertToMat(imgVal);
26
- convertMs_=(cv::getTickCount()-t0)/cv::getTickFrequency()*1e3;
27
-
28
- if (imgVal.IsObject() && !imgVal.IsBuffer()) {
29
- Napi::Object obj = imgVal.As<Napi::Object>();
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
- const int64 t0=cv::getTickCount();
55
- cv::copyMakeBorder(src_,dst_,t_,b_,l_,r_,cv::BORDER_CONSTANT,padClrImg);
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
- cv::Mat src_,dst_;
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
- auto t0 = std::chrono::steady_clock::now();
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;
@@ -22,34 +22,28 @@ public:
22
22
  quality(quality),
23
23
  pngOptimize(pngOptimize)
24
24
  {
25
+ // JS thread: metadata + persistent reference only (no decode, no pixels)
25
26
  try {
26
- inputMat = ConvertToMat(imgVal); // zero-copy RAW
27
-
28
- if (imgVal.IsObject() && !imgVal.IsBuffer()) {
29
- Napi::Object obj = imgVal.As<Napi::Object>();
30
-
31
- // Check for new colorSpace field first
32
- if (obj.Has("colorSpace")) {
33
- channelOrder = obj.Get("colorSpace").As<Napi::String>().Utf8Value();
34
- }
35
- // Default based on channel count
36
- else {
37
- channelOrder = (inputMat.channels() == 4) ? "RGBA"
38
- : (inputMat.channels() == 3) ? "RGB"
39
- : "GRAY";
40
- }
41
- } else {
42
- // Buffer input - determine from OpenCV Mat
43
- channelOrder = (inputMat.channels() == 4) ? "RGBA"
44
- : (inputMat.channels() == 3) ? "RGB"
45
- : "GRAY";
46
- }
47
- } catch (const Napi::Error& e) { SetError(e.Message()); }
27
+ src_ = CaptureImage(imgVal);
28
+ } catch (const Napi::Error& e) {
29
+ captureFailed_ = true; SetError(e.Message());
30
+ } catch (const std::exception& e) {
31
+ captureFailed_ = true; SetError(e.what());
32
+ }
48
33
  }
49
34
 
50
35
  protected:
51
36
  void Execute() override {
37
+ if (captureFailed_) return;
52
38
  try {
39
+ // Decode (encoded inputs only) on the worker thread
40
+ auto tc = std::chrono::steady_clock::now();
41
+ src_.Materialize();
42
+ inputMat = src_.mat;
43
+ channelOrder = src_.colorSpace;
44
+ convertMs = std::chrono::duration<double,std::milli>(
45
+ std::chrono::steady_clock::now() - tc).count();
46
+
53
47
  auto t0 = std::chrono::steady_clock::now();
54
48
 
55
49
  // Ajustar color al orden real de la imagen
@@ -91,6 +85,8 @@ protected:
91
85
  const cv::Mat srcForEncoding =
92
86
  PrepareForEncoding(resultMat, channelOrder, outputFormat);
93
87
  encodeMs = EncodeToFormat(srcForEncoding, encodedBuf, outputFormat, quality, pngOptimize);
88
+ } else {
89
+ FinalizeForOutput(resultMat); // alias of JS memory → owned copy (worker thread)
94
90
  }
95
91
  } catch (const std::exception& e) { SetError(e.what()); }
96
92
  }
@@ -103,7 +99,7 @@ protected:
103
99
 
104
100
  Napi::Object res = Napi::Object::New(env);
105
101
  res.Set("image", jsImg);
106
- res.Set("timing", MakeTimingJS(env, 0.0, taskMs, encodeMs)); // convertMs=0
102
+ res.Set("timing", MakeTimingJS(env, convertMs, taskMs, encodeMs));
107
103
  Callback().Call({ env.Null(), res });
108
104
  }
109
105
 
@@ -112,14 +108,17 @@ protected:
112
108
  }
113
109
 
114
110
  private:
111
+ ImageSource src_;
115
112
  cv::Mat inputMat, resultMat;
116
113
  double angleDeg;
117
114
  cv::Scalar padColorRGB, padClrImg;
118
115
  std::string outputFormat;
119
116
  int quality;
120
117
  bool pngOptimize;
118
+ bool captureFailed_ = false;
121
119
 
122
120
  std::string channelOrder;
121
+ double convertMs = 0.0;
123
122
  double taskMs = 0.0;
124
123
  double encodeMs = 0.0;
125
124
  std::vector<uchar> encodedBuf;