@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
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// ───────── src/codec.cpp ───────────────────────────────────────────────
|
|
2
|
+
// Stand-alone decode / encode operations so the JS side never has to touch
|
|
3
|
+
// pixel data on the event loop:
|
|
4
|
+
//
|
|
5
|
+
// decode(buffer, callback)
|
|
6
|
+
// → { image: {data,width,height,channels,colorSpace,dtype}, timing }
|
|
7
|
+
// encode(image, format, [quality], [pngOptimize], callback)
|
|
8
|
+
// → { image: Buffer, timing } format ∈ jpg | png | webp | bmp
|
|
9
|
+
//
|
|
10
|
+
// Both run entirely inside Napi::AsyncWorker::Execute() (libuv thread pool).
|
|
11
|
+
#include <napi.h>
|
|
12
|
+
#include <opencv2/opencv.hpp>
|
|
13
|
+
#include "utils.h"
|
|
14
|
+
|
|
15
|
+
/*------------------------------------------------------------------------*/
|
|
16
|
+
class DecodeWorker final : public Napi::AsyncWorker {
|
|
17
|
+
public:
|
|
18
|
+
DecodeWorker(Napi::Function cb, const Napi::Value& input)
|
|
19
|
+
: Napi::AsyncWorker(cb)
|
|
20
|
+
{
|
|
21
|
+
try {
|
|
22
|
+
src_ = CaptureImage(input);
|
|
23
|
+
} catch (const Napi::Error& e) {
|
|
24
|
+
captureFailed_ = true; SetError(e.Message());
|
|
25
|
+
} catch (const std::exception& e) {
|
|
26
|
+
captureFailed_ = true; SetError(e.what());
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected:
|
|
31
|
+
void Execute() override {
|
|
32
|
+
if (captureFailed_) return;
|
|
33
|
+
const int64 t0 = cv::getTickCount();
|
|
34
|
+
src_.Materialize(); // cv::imdecode on the worker thread
|
|
35
|
+
result_ = src_.mat;
|
|
36
|
+
colorSpace_ = src_.colorSpace;
|
|
37
|
+
FinalizeForOutput(result_); // own the memory before handing it to JS
|
|
38
|
+
convertMs_ = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
void OnOK() override {
|
|
42
|
+
Napi::Env env = Env();
|
|
43
|
+
Napi::Object out = Napi::Object::New(env);
|
|
44
|
+
out.Set("image", MatToRawJS(env, result_, colorSpace_));
|
|
45
|
+
out.Set("timing", MakeTimingJS(env, convertMs_, 0.0, 0.0));
|
|
46
|
+
Callback().Call({ env.Null(), out });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
void OnError(const Napi::Error& e) override {
|
|
50
|
+
Callback().Call({ e.Value(), Env().Null() });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private:
|
|
54
|
+
ImageSource src_;
|
|
55
|
+
cv::Mat result_;
|
|
56
|
+
std::string colorSpace_;
|
|
57
|
+
bool captureFailed_ = false;
|
|
58
|
+
double convertMs_ = 0.0;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/*------------------------------------------------------------------------*/
|
|
62
|
+
class EncodeWorker final : public Napi::AsyncWorker {
|
|
63
|
+
public:
|
|
64
|
+
EncodeWorker(Napi::Function cb,
|
|
65
|
+
const Napi::Value& input,
|
|
66
|
+
std::string format,
|
|
67
|
+
int quality,
|
|
68
|
+
bool pngOptimize)
|
|
69
|
+
: Napi::AsyncWorker(cb),
|
|
70
|
+
format_(std::move(format)),
|
|
71
|
+
quality_(quality),
|
|
72
|
+
pngOptimize_(pngOptimize)
|
|
73
|
+
{
|
|
74
|
+
try {
|
|
75
|
+
src_ = CaptureImage(input);
|
|
76
|
+
} catch (const Napi::Error& e) {
|
|
77
|
+
captureFailed_ = true; SetError(e.Message());
|
|
78
|
+
} catch (const std::exception& e) {
|
|
79
|
+
captureFailed_ = true; SetError(e.what());
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
protected:
|
|
84
|
+
void Execute() override {
|
|
85
|
+
if (captureFailed_) return;
|
|
86
|
+
|
|
87
|
+
if (ParseImageFormat(format_) == ImageFormat::RAW) {
|
|
88
|
+
throw std::runtime_error("encode(): unsupported format \"" + format_ +
|
|
89
|
+
"\" (expected jpg, png, webp or bmp)");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const int64 t0 = cv::getTickCount();
|
|
93
|
+
src_.Materialize();
|
|
94
|
+
convertMs_ = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
95
|
+
|
|
96
|
+
const cv::Mat prepared = PrepareForEncoding(src_.mat, src_.colorSpace, format_);
|
|
97
|
+
encodeMs_ = EncodeToFormat(prepared, encoded_, format_, quality_, pngOptimize_);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
void OnOK() override {
|
|
101
|
+
Napi::Env env = Env();
|
|
102
|
+
Napi::Object out = Napi::Object::New(env);
|
|
103
|
+
out.Set("image", VectorToBuffer(env, std::move(encoded_)));
|
|
104
|
+
out.Set("timing", MakeTimingJS(env, convertMs_, 0.0, encodeMs_));
|
|
105
|
+
Callback().Call({ env.Null(), out });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
void OnError(const Napi::Error& e) override {
|
|
109
|
+
Callback().Call({ e.Value(), Env().Null() });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private:
|
|
113
|
+
ImageSource src_;
|
|
114
|
+
std::string format_;
|
|
115
|
+
int quality_;
|
|
116
|
+
bool pngOptimize_;
|
|
117
|
+
bool captureFailed_ = false;
|
|
118
|
+
std::vector<uchar> encoded_;
|
|
119
|
+
double convertMs_ = 0.0, encodeMs_ = 0.0;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/*──────── binding: decode(buffer, callback) ─────────────────────────────*/
|
|
123
|
+
Napi::Value Decode(const Napi::CallbackInfo& info) {
|
|
124
|
+
Napi::Env env = info.Env();
|
|
125
|
+
if (info.Length() < 2 || !info[info.Length() - 1].IsFunction()) {
|
|
126
|
+
Napi::TypeError::New(env, "decode(buffer, callback)").ThrowAsJavaScriptException();
|
|
127
|
+
return env.Null();
|
|
128
|
+
}
|
|
129
|
+
(new DecodeWorker(info[info.Length() - 1].As<Napi::Function>(), info[0]))->Queue();
|
|
130
|
+
return env.Undefined();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/*──────── binding: encode(image, format, [quality], [pngOptimize], callback) ─*/
|
|
134
|
+
Napi::Value Encode(const Napi::CallbackInfo& info) {
|
|
135
|
+
Napi::Env env = info.Env();
|
|
136
|
+
if (info.Length() < 3 || info.Length() > 5 ||
|
|
137
|
+
!info[1].IsString() || !info[info.Length() - 1].IsFunction()) {
|
|
138
|
+
Napi::TypeError::New(env, "encode(image, format, [quality], [pngOptimize], callback)")
|
|
139
|
+
.ThrowAsJavaScriptException();
|
|
140
|
+
return env.Null();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
std::string format = info[1].As<Napi::String>().Utf8Value();
|
|
144
|
+
int quality = 90;
|
|
145
|
+
bool pngOptimize = false;
|
|
146
|
+
size_t i = 2;
|
|
147
|
+
if (info.Length() - i >= 2 && info[i].IsNumber()) {
|
|
148
|
+
quality = info[i++].As<Napi::Number>().Int32Value();
|
|
149
|
+
}
|
|
150
|
+
if (info.Length() - i >= 2 && info[i].IsBoolean()) {
|
|
151
|
+
pngOptimize = info[i++].As<Napi::Boolean>().Value();
|
|
152
|
+
}
|
|
153
|
+
Napi::Function cb = info[info.Length() - 1].As<Napi::Function>();
|
|
154
|
+
|
|
155
|
+
(new EncodeWorker(cb, info[0], format, quality, pngOptimize))->Queue();
|
|
156
|
+
return env.Undefined();
|
|
157
|
+
}
|
|
@@ -58,30 +58,28 @@ public:
|
|
|
58
58
|
quality(quality),
|
|
59
59
|
pngOptimize(pngOptimize)
|
|
60
60
|
{
|
|
61
|
+
// JS thread: metadata + persistent reference only (no decode, no pixels)
|
|
61
62
|
try {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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()); }
|
|
63
|
+
src_ = CaptureImage(imgVal);
|
|
64
|
+
} catch (const Napi::Error& e) {
|
|
65
|
+
captureFailed_ = true; SetError(e.Message());
|
|
66
|
+
} catch (const std::exception& e) {
|
|
67
|
+
captureFailed_ = true; SetError(e.what());
|
|
68
|
+
}
|
|
80
69
|
}
|
|
81
70
|
|
|
82
71
|
protected:
|
|
83
72
|
void Execute() override {
|
|
73
|
+
if (captureFailed_) return;
|
|
84
74
|
try {
|
|
75
|
+
// Decode (encoded inputs only) on the worker thread
|
|
76
|
+
auto tc = std::chrono::steady_clock::now();
|
|
77
|
+
src_.Materialize();
|
|
78
|
+
inputMat = src_.mat;
|
|
79
|
+
sourceCS = src_.colorSpace;
|
|
80
|
+
convertMs = std::chrono::duration<double, std::milli>(
|
|
81
|
+
std::chrono::steady_clock::now() - tc).count();
|
|
82
|
+
|
|
85
83
|
auto t0 = std::chrono::steady_clock::now();
|
|
86
84
|
|
|
87
85
|
// Identity fast-path: same source and target
|
|
@@ -106,6 +104,8 @@ protected:
|
|
|
106
104
|
const cv::Mat srcForEncoding =
|
|
107
105
|
PrepareForEncoding(resultMat, targetCS, outputFormat);
|
|
108
106
|
encodeMs = EncodeToFormat(srcForEncoding, encodedBuf, outputFormat, quality, pngOptimize);
|
|
107
|
+
} else {
|
|
108
|
+
FinalizeForOutput(resultMat); // identity alias → owned copy (worker thread)
|
|
109
109
|
}
|
|
110
110
|
} catch (const std::exception& e) { SetError(e.what()); }
|
|
111
111
|
}
|
|
@@ -118,7 +118,7 @@ protected:
|
|
|
118
118
|
|
|
119
119
|
Napi::Object res = Napi::Object::New(env);
|
|
120
120
|
res.Set("image", jsImg);
|
|
121
|
-
res.Set("timing", MakeTimingJS(env,
|
|
121
|
+
res.Set("timing", MakeTimingJS(env, convertMs, taskMs, encodeMs));
|
|
122
122
|
Callback().Call({ env.Null(), res });
|
|
123
123
|
}
|
|
124
124
|
|
|
@@ -127,13 +127,16 @@ protected:
|
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
private:
|
|
130
|
+
ImageSource src_;
|
|
130
131
|
cv::Mat inputMat, resultMat;
|
|
131
132
|
std::string sourceCS;
|
|
132
133
|
std::string targetCS;
|
|
133
134
|
std::string outputFormat;
|
|
134
135
|
int quality;
|
|
135
136
|
bool pngOptimize;
|
|
137
|
+
bool captureFailed_ = false;
|
|
136
138
|
|
|
139
|
+
double convertMs = 0.0;
|
|
137
140
|
double taskMs = 0.0;
|
|
138
141
|
double encodeMs = 0.0;
|
|
139
142
|
std::vector<uchar> encodedBuf;
|
|
@@ -7,27 +7,6 @@
|
|
|
7
7
|
enum class Direction { RIGHT, LEFT, UP, DOWN };
|
|
8
8
|
enum class Strategy { RESIZE, PAD_START, PAD_END, PAD_BOTH };
|
|
9
9
|
|
|
10
|
-
// Helper function to detect channel format for individual images
|
|
11
|
-
std::string DetectChannelFormat(const Napi::Value& jsImg, const cv::Mat& mat) {
|
|
12
|
-
if (jsImg.IsObject() && !jsImg.IsBuffer()) {
|
|
13
|
-
Napi::Object obj = jsImg.As<Napi::Object>();
|
|
14
|
-
|
|
15
|
-
// Check for new colorSpace field first
|
|
16
|
-
if (obj.Has("colorSpace")) {
|
|
17
|
-
return obj.Get("colorSpace").As<Napi::String>().Utf8Value();
|
|
18
|
-
}
|
|
19
|
-
// Default based on channel count
|
|
20
|
-
else {
|
|
21
|
-
const int channels = mat.channels();
|
|
22
|
-
return (channels == 4) ? "RGBA" : (channels == 3) ? "RGB" : "GRAY";
|
|
23
|
-
}
|
|
24
|
-
} else {
|
|
25
|
-
// Buffer input - determine from OpenCV Mat
|
|
26
|
-
const int channels = mat.channels();
|
|
27
|
-
return (channels == 4) ? "RGBA" : (channels == 3) ? "RGB" : "GRAY";
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
10
|
// Helper function to determine the best output channel format from a list
|
|
32
11
|
std::string DetermineOutputFormat(const std::vector<std::string>& channels) {
|
|
33
12
|
bool hasRGBA = false, hasBGRA = false, hasRGB = false, hasBGR = false;
|
|
@@ -129,37 +108,45 @@ public:
|
|
|
129
108
|
else if (strat == "pad-end") strategy = Strategy::PAD_END;
|
|
130
109
|
else strategy = Strategy::PAD_BOTH;
|
|
131
110
|
|
|
132
|
-
//
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
111
|
+
// JS thread: metadata + persistent references only (no decode, no pixels)
|
|
112
|
+
try {
|
|
113
|
+
const size_t imgCount = jsImgs.size();
|
|
114
|
+
sources.reserve(imgCount);
|
|
115
|
+
for (size_t i = 0; i < imgCount; ++i) {
|
|
116
|
+
sources.emplace_back(CaptureImage(jsImgs[i]));
|
|
117
|
+
}
|
|
118
|
+
} catch (const Napi::Error& e) {
|
|
119
|
+
captureFailed_ = true; SetError(e.Message());
|
|
120
|
+
} catch (const std::exception& e) {
|
|
121
|
+
captureFailed_ = true; SetError(e.what());
|
|
142
122
|
}
|
|
143
|
-
|
|
144
|
-
// Determine output channel format
|
|
145
|
-
outputChannel = DetermineOutputFormat(channels);
|
|
146
|
-
|
|
147
|
-
convertMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
148
123
|
|
|
149
124
|
// Store original pad color (channel-specific preparation will be done per image)
|
|
150
125
|
padClrImg = padColorRGB;
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
protected:
|
|
129
|
+
void Execute() override {
|
|
130
|
+
if (captureFailed_) return;
|
|
131
|
+
|
|
132
|
+
// Decode (encoded inputs only) + channel detection on the worker thread
|
|
133
|
+
int64 t0 = cv::getTickCount();
|
|
134
|
+
mats.clear(); channels.clear();
|
|
135
|
+
mats.reserve(sources.size());
|
|
136
|
+
channels.reserve(sources.size());
|
|
137
|
+
for (auto& s : sources) {
|
|
138
|
+
s.Materialize();
|
|
139
|
+
mats.push_back(s.mat);
|
|
140
|
+
channels.push_back(s.colorSpace);
|
|
141
|
+
}
|
|
142
|
+
outputChannel = DetermineOutputFormat(channels);
|
|
154
143
|
for (const auto& m : mats) {
|
|
155
144
|
maxW = std::max(maxW, m.cols);
|
|
156
145
|
maxH = std::max(maxH, m.rows);
|
|
157
146
|
}
|
|
158
|
-
|
|
147
|
+
convertMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
159
148
|
|
|
160
|
-
|
|
161
|
-
void Execute() override {
|
|
162
|
-
const int64 t0 = cv::getTickCount();
|
|
149
|
+
t0 = cv::getTickCount();
|
|
163
150
|
|
|
164
151
|
const bool isHorizontal = (direction == Direction::RIGHT || direction == Direction::LEFT);
|
|
165
152
|
const int baseSize = isHorizontal ? maxH : maxW;
|
|
@@ -245,6 +232,8 @@ protected:
|
|
|
245
232
|
if (outputFormat != "raw") {
|
|
246
233
|
cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
|
|
247
234
|
encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
|
|
235
|
+
} else {
|
|
236
|
+
FinalizeForOutput(result);
|
|
248
237
|
}
|
|
249
238
|
}
|
|
250
239
|
|
|
@@ -259,7 +248,13 @@ protected:
|
|
|
259
248
|
Callback().Call({env.Null(), out});
|
|
260
249
|
}
|
|
261
250
|
|
|
251
|
+
void OnError(const Napi::Error& e) override {
|
|
252
|
+
Callback().Call({ e.Value(), Env().Null() });
|
|
253
|
+
}
|
|
254
|
+
|
|
262
255
|
private:
|
|
256
|
+
std::vector<ImageSource> sources;
|
|
257
|
+
bool captureFailed_ = false;
|
|
263
258
|
std::vector<cv::Mat> mats;
|
|
264
259
|
std::vector<std::string> channels;
|
|
265
260
|
cv::Mat result;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
#include <napi.h>
|
|
3
3
|
#include <opencv2/opencv.hpp>
|
|
4
4
|
#include <cmath>
|
|
5
|
-
#include "utils.h" //
|
|
5
|
+
#include "utils.h" // CaptureImage, PrepareForEncoding, EncodeToFormat…
|
|
6
6
|
|
|
7
7
|
/*────────────────────────── Worker ───────────────────────────────────*/
|
|
8
8
|
class CropWorker : public Napi::AsyncWorker {
|
|
@@ -15,36 +15,29 @@ public:
|
|
|
15
15
|
x_(x),y_(y),width_(width),height_(height),
|
|
16
16
|
normalized_(normalized),outputFormat_(std::move(outputFormat)),quality_(quality),pngOptimize_(pngOptimize)
|
|
17
17
|
{
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// Check for colorSpace field
|
|
27
|
-
if (obj.Has("colorSpace")) {
|
|
28
|
-
channel_ = obj.Get("colorSpace").As<Napi::String>().Utf8Value();
|
|
29
|
-
}
|
|
30
|
-
// Default based on channel count
|
|
31
|
-
else {
|
|
32
|
-
channel_ = (input_.channels() == 4) ? "RGBA"
|
|
33
|
-
: (input_.channels() == 3) ? "RGB"
|
|
34
|
-
: "GRAY";
|
|
35
|
-
}
|
|
36
|
-
} else {
|
|
37
|
-
// Buffer input - determine from OpenCV Mat
|
|
38
|
-
channel_ = (input_.channels() == 4) ? "RGBA"
|
|
39
|
-
: (input_.channels() == 3) ? "RGB"
|
|
40
|
-
: "GRAY";
|
|
18
|
+
// JS thread: metadata + persistent reference only (no decode, no pixels)
|
|
19
|
+
try {
|
|
20
|
+
src_ = CaptureImage(imgVal);
|
|
21
|
+
} catch (const Napi::Error& e) {
|
|
22
|
+
captureFailed_ = true; SetError(e.Message());
|
|
23
|
+
} catch (const std::exception& e) {
|
|
24
|
+
captureFailed_ = true; SetError(e.what());
|
|
41
25
|
}
|
|
42
26
|
}
|
|
43
27
|
|
|
44
28
|
protected:
|
|
45
29
|
void Execute() override {
|
|
46
|
-
|
|
47
|
-
|
|
30
|
+
if (captureFailed_) return;
|
|
31
|
+
|
|
32
|
+
/* ─ convertMs: decode (encoded inputs only), worker thread ─ */
|
|
33
|
+
int64 t0 = cv::getTickCount();
|
|
34
|
+
src_.Materialize();
|
|
35
|
+
input_ = src_.mat;
|
|
36
|
+
channel_ = src_.colorSpace;
|
|
37
|
+
convertMs_ = (cv::getTickCount()-t0)/cv::getTickFrequency()*1e3;
|
|
38
|
+
|
|
39
|
+
/* ─ taskMs (recorte) ─ */
|
|
40
|
+
t0 = cv::getTickCount();
|
|
48
41
|
|
|
49
42
|
const int W=input_.cols, H=input_.rows;
|
|
50
43
|
int x = normalized_? int(std::round(x_*W)) : int(std::lround(x_));
|
|
@@ -67,6 +60,8 @@ protected:
|
|
|
67
60
|
const cv::Mat srcForEncoding =
|
|
68
61
|
PrepareForEncoding(result_, channel_, outputFormat_);
|
|
69
62
|
encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat_, quality_, pngOptimize_);
|
|
63
|
+
} else {
|
|
64
|
+
FinalizeForOutput(result_); // ROI → contiguous owned copy (worker thread)
|
|
70
65
|
}
|
|
71
66
|
}
|
|
72
67
|
|
|
@@ -87,6 +82,7 @@ protected:
|
|
|
87
82
|
}
|
|
88
83
|
|
|
89
84
|
private:
|
|
85
|
+
ImageSource src_;
|
|
90
86
|
cv::Mat input_, result_;
|
|
91
87
|
double x_, y_, width_, height_;
|
|
92
88
|
bool normalized_;
|
|
@@ -94,6 +90,7 @@ private:
|
|
|
94
90
|
int quality_;
|
|
95
91
|
bool pngOptimize_;
|
|
96
92
|
std::string channel_;
|
|
93
|
+
bool captureFailed_ = false;
|
|
97
94
|
|
|
98
95
|
double convertMs_{0.0}, taskMs_{0.0}, encodeMs_{0.0};
|
|
99
96
|
std::vector<uchar> encodedBuf_;
|
|
@@ -119,18 +119,31 @@ public:
|
|
|
119
119
|
quality(quality),
|
|
120
120
|
pngOptimize(pngOptimize) {
|
|
121
121
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
122
|
+
// JS thread: metadata + persistent reference only (no decode, no pixels)
|
|
123
|
+
try {
|
|
124
|
+
src_ = CaptureImage(jsImage);
|
|
125
|
+
} catch (const Napi::Error& e) {
|
|
126
|
+
captureFailed_ = true; SetError(e.Message());
|
|
127
|
+
} catch (const std::exception& e) {
|
|
128
|
+
captureFailed_ = true; SetError(e.what());
|
|
129
|
+
}
|
|
125
130
|
|
|
126
|
-
imageChannelOrder = DetectChannelFormatShared(jsImage, baseImage);
|
|
127
131
|
ParsePoints(jsPoints);
|
|
128
132
|
ParseLines(jsLines);
|
|
129
133
|
}
|
|
130
134
|
|
|
131
135
|
protected:
|
|
132
136
|
void Execute() override {
|
|
133
|
-
|
|
137
|
+
if (captureFailed_) return;
|
|
138
|
+
|
|
139
|
+
// Decode (encoded inputs only) on the worker thread
|
|
140
|
+
int64 t0 = cv::getTickCount();
|
|
141
|
+
src_.Materialize();
|
|
142
|
+
const cv::Mat& baseImage = src_.mat;
|
|
143
|
+
imageChannelOrder = src_.colorSpace;
|
|
144
|
+
convertMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
145
|
+
|
|
146
|
+
t0 = cv::getTickCount();
|
|
134
147
|
|
|
135
148
|
const int width = baseImage.cols;
|
|
136
149
|
const int height = baseImage.rows;
|
|
@@ -188,22 +201,27 @@ protected:
|
|
|
188
201
|
cv::min(canvas, 1.0, canvas);
|
|
189
202
|
cv::max(canvas, 0.0, canvas);
|
|
190
203
|
|
|
191
|
-
// Convert back to uint8
|
|
192
|
-
|
|
204
|
+
// Convert back to uint8 into a fresh Mat: `working` may alias the caller's
|
|
205
|
+
// input buffer when no colour conversion was needed, and we must never
|
|
206
|
+
// write into JS-owned memory.
|
|
207
|
+
cv::Mat out8u;
|
|
208
|
+
canvas.convertTo(out8u,
|
|
193
209
|
(channels == 1) ? CV_8UC1 :
|
|
194
210
|
(channels == 3) ? CV_8UC3 :
|
|
195
211
|
CV_8UC4,
|
|
196
212
|
255.0);
|
|
197
213
|
|
|
198
214
|
if (processingOrder != imageChannelOrder) {
|
|
199
|
-
resultImage = ConvertToTargetFormatShared(
|
|
215
|
+
resultImage = ConvertToTargetFormatShared(out8u, processingOrder, imageChannelOrder);
|
|
200
216
|
} else {
|
|
201
|
-
resultImage =
|
|
217
|
+
resultImage = out8u;
|
|
202
218
|
}
|
|
203
219
|
|
|
204
220
|
if (outputFormat != "raw") {
|
|
205
221
|
cv::Mat encodeSrc = PrepareForEncoding(resultImage, imageChannelOrder, outputFormat);
|
|
206
222
|
encodeMs = EncodeToFormat(encodeSrc, encodedBuffer, outputFormat, quality, pngOptimize);
|
|
223
|
+
} else {
|
|
224
|
+
FinalizeForOutput(resultImage);
|
|
207
225
|
}
|
|
208
226
|
|
|
209
227
|
taskMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
@@ -222,6 +240,10 @@ protected:
|
|
|
222
240
|
Callback().Call({ env.Null(), response });
|
|
223
241
|
}
|
|
224
242
|
|
|
243
|
+
void OnError(const Napi::Error& e) override {
|
|
244
|
+
Callback().Call({ e.Value(), Env().Null() });
|
|
245
|
+
}
|
|
246
|
+
|
|
225
247
|
private:
|
|
226
248
|
void ParsePoints(const Napi::Value& value) {
|
|
227
249
|
if (!value.IsArray()) return;
|
|
@@ -362,7 +384,8 @@ private:
|
|
|
362
384
|
}
|
|
363
385
|
|
|
364
386
|
private:
|
|
365
|
-
|
|
387
|
+
ImageSource src_;
|
|
388
|
+
bool captureFailed_ = false;
|
|
366
389
|
cv::Mat resultImage;
|
|
367
390
|
std::vector<uchar> encodedBuffer;
|
|
368
391
|
|
|
@@ -25,38 +25,30 @@ public:
|
|
|
25
25
|
quality_(quality),
|
|
26
26
|
pngOptimize_(pngOptimize)
|
|
27
27
|
{
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
Napi::Object obj = imgVal.As<Napi::Object>();
|
|
36
|
-
|
|
37
|
-
// Check for colorSpace field
|
|
38
|
-
if (obj.Has("colorSpace")) {
|
|
39
|
-
channel_ = obj.Get("colorSpace").As<Napi::String>().Utf8Value();
|
|
40
|
-
}
|
|
41
|
-
// Default based on channel count
|
|
42
|
-
else {
|
|
43
|
-
channel_ = (input_.channels() == 4) ? "RGBA"
|
|
44
|
-
: (input_.channels() == 3) ? "RGB"
|
|
45
|
-
: "GRAY";
|
|
46
|
-
}
|
|
47
|
-
} else {
|
|
48
|
-
// Buffer input - determine from OpenCV Mat
|
|
49
|
-
channel_ = (input_.channels() == 4) ? "RGBA"
|
|
50
|
-
: (input_.channels() == 3) ? "RGB"
|
|
51
|
-
: "GRAY";
|
|
28
|
+
// JS thread: metadata + persistent reference only (no decode, no pixels)
|
|
29
|
+
try {
|
|
30
|
+
src_ = CaptureImage(imgVal);
|
|
31
|
+
} catch (const Napi::Error& e) {
|
|
32
|
+
captureFailed_ = true; SetError(e.Message());
|
|
33
|
+
} catch (const std::exception& e) {
|
|
34
|
+
captureFailed_ = true; SetError(e.what());
|
|
52
35
|
}
|
|
53
36
|
}
|
|
54
37
|
|
|
55
38
|
protected:
|
|
56
39
|
void Execute() override {
|
|
57
|
-
|
|
58
|
-
|
|
40
|
+
if (captureFailed_) return;
|
|
41
|
+
|
|
59
42
|
try {
|
|
43
|
+
// Decode (encoded inputs only) on the worker thread
|
|
44
|
+
const int64 tc = cv::getTickCount();
|
|
45
|
+
src_.Materialize();
|
|
46
|
+
input_ = src_.mat;
|
|
47
|
+
channel_ = src_.colorSpace;
|
|
48
|
+
convertMs_ = (cv::getTickCount() - tc) / cv::getTickFrequency() * 1e3;
|
|
49
|
+
|
|
50
|
+
const int64 t0 = cv::getTickCount();
|
|
51
|
+
|
|
60
52
|
// Apply filter based on type
|
|
61
53
|
if (filterType_ == "blur") {
|
|
62
54
|
ApplyBlurFilter();
|
|
@@ -85,6 +77,8 @@ protected:
|
|
|
85
77
|
const cv::Mat srcForEncoding =
|
|
86
78
|
PrepareForEncoding(result_, channel_, outputFormat_);
|
|
87
79
|
encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat_, quality_, pngOptimize_);
|
|
80
|
+
} else {
|
|
81
|
+
FinalizeForOutput(result_);
|
|
88
82
|
}
|
|
89
83
|
} catch (const std::exception& e) {
|
|
90
84
|
SetError(e.what());
|
|
@@ -108,6 +102,8 @@ protected:
|
|
|
108
102
|
}
|
|
109
103
|
|
|
110
104
|
private:
|
|
105
|
+
ImageSource src_;
|
|
106
|
+
bool captureFailed_ = false;
|
|
111
107
|
cv::Mat input_, result_;
|
|
112
108
|
std::string filterType_;
|
|
113
109
|
int kernelSize_;
|
|
@@ -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;
|
|
@@ -119,6 +134,8 @@ protected:
|
|
|
119
134
|
if (emitImage && outputFormat != "raw") {
|
|
120
135
|
cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
|
|
121
136
|
encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
|
|
137
|
+
} else if (emitImage) {
|
|
138
|
+
FinalizeForOutput(result);
|
|
122
139
|
}
|
|
123
140
|
}
|
|
124
141
|
|
|
@@ -167,6 +184,10 @@ protected:
|
|
|
167
184
|
Callback().Call({env.Null(), out});
|
|
168
185
|
}
|
|
169
186
|
|
|
187
|
+
void OnError(const Napi::Error& e) override {
|
|
188
|
+
Callback().Call({ e.Value(), Env().Null() });
|
|
189
|
+
}
|
|
190
|
+
|
|
170
191
|
private:
|
|
171
192
|
struct BlobStat { int x, y, w, h, area; double cx, cy; };
|
|
172
193
|
static constexpr size_t kMaxBlobs = 50;
|
|
@@ -237,7 +258,9 @@ private:
|
|
|
237
258
|
statBlobs = std::move(blobs);
|
|
238
259
|
}
|
|
239
260
|
|
|
240
|
-
|
|
261
|
+
ImageSource src1_, src2_;
|
|
262
|
+
bool captureFailed_ = false;
|
|
263
|
+
cv::Mat result;
|
|
241
264
|
std::string format1, format2, outputChannel;
|
|
242
265
|
int colormapType;
|
|
243
266
|
int blurSize;
|