@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.
- 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/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.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.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 +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
|
@@ -44,29 +44,8 @@ inline cv::Vec3f GenerateMaximallyDifferentColor(
|
|
|
44
44
|
return firstColor;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
//
|
|
48
|
-
std::vector<cv::Vec3f
|
|
49
|
-
const int numHues = 24; // Every 15 degrees
|
|
50
|
-
const int numSaturations = 3; // 60%, 80%, 100%
|
|
51
|
-
const int numValues = 3; // 60%, 80%, 100%
|
|
52
|
-
|
|
53
|
-
for (int h = 0; h < numHues; h++) {
|
|
54
|
-
for (int s = 0; s < numSaturations; s++) {
|
|
55
|
-
for (int v = 0; v < numValues; v++) {
|
|
56
|
-
float hue = (h * 360.0f / numHues);
|
|
57
|
-
float saturation = 0.6f + s * 0.2f;
|
|
58
|
-
float value = 0.6f + v * 0.2f;
|
|
59
|
-
|
|
60
|
-
// Convert HSV to BGR
|
|
61
|
-
cv::Mat hsv(1, 1, CV_32FC3, cv::Scalar(hue / 360.0f, saturation, value));
|
|
62
|
-
cv::Mat bgr;
|
|
63
|
-
cv::cvtColor(hsv, bgr, cv::COLOR_HSV2BGR);
|
|
64
|
-
|
|
65
|
-
cv::Vec3f color = bgr.at<cv::Vec3f>(0, 0);
|
|
66
|
-
candidates.push_back(color);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
47
|
+
// Candidate colors evenly distributed in HSV space (precomputed once)
|
|
48
|
+
const std::vector<cv::Vec3f>& candidates = CandidateColorPalette();
|
|
70
49
|
|
|
71
50
|
// Find candidate with maximum minimum distance to existing colors
|
|
72
51
|
float maxMinDistance = 0;
|
|
@@ -95,6 +74,14 @@ inline cv::Vec3f GenerateMaximallyDifferentColor(
|
|
|
95
74
|
}
|
|
96
75
|
|
|
97
76
|
/*------------------------------------------------------------------------*/
|
|
77
|
+
// Box as parsed from JS (normalized coordinates). Geometry that depends on
|
|
78
|
+
// the image size is resolved in Execute(), after the image is decoded.
|
|
79
|
+
struct ParsedBox {
|
|
80
|
+
std::vector<cv::Point2f> corners; // 4 normalized corners (TL, TR, BR, BL)
|
|
81
|
+
std::string className;
|
|
82
|
+
float confidence;
|
|
83
|
+
};
|
|
84
|
+
|
|
98
85
|
class AddBBsWorker final : public Napi::AsyncWorker {
|
|
99
86
|
public:
|
|
100
87
|
AddBBsWorker(Napi::Function cb,
|
|
@@ -123,13 +110,16 @@ public:
|
|
|
123
110
|
quality(quality),
|
|
124
111
|
pngOptimize(pngOptimize)
|
|
125
112
|
{
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
113
|
+
// JS thread: metadata + persistent reference only (no decode, no pixels)
|
|
114
|
+
try {
|
|
115
|
+
src_ = CaptureImage(jsImg);
|
|
116
|
+
} catch (const Napi::Error& e) {
|
|
117
|
+
captureFailed_ = true; SetError(e.Message());
|
|
118
|
+
} catch (const std::exception& e) {
|
|
119
|
+
captureFailed_ = true; SetError(e.what());
|
|
120
|
+
}
|
|
130
121
|
|
|
131
|
-
// Parse class color map
|
|
132
|
-
std::unordered_map<std::string, cv::Vec3f> userColorMap;
|
|
122
|
+
// Parse class color map (JS → plain map)
|
|
133
123
|
Napi::Array colorMapKeys = classColorMap.GetPropertyNames();
|
|
134
124
|
for (size_t i = 0; i < colorMapKeys.Length(); i++) {
|
|
135
125
|
std::string className = colorMapKeys.Get(i).As<Napi::String>().Utf8Value();
|
|
@@ -140,15 +130,11 @@ public:
|
|
|
140
130
|
float g = std::stoi(hexColor.substr(3, 2), nullptr, 16) / 255.0f;
|
|
141
131
|
float b = std::stoi(hexColor.substr(5, 2), nullptr, 16) / 255.0f;
|
|
142
132
|
userColorMap[className] = cv::Vec3f(b, g, r); // BGR for OpenCV
|
|
143
|
-
// Debug: Print parsed color
|
|
144
|
-
// printf("Class %s: hex=%s -> BGR=(%.2f,%.2f,%.2f)\n", className.c_str(), hexColor.c_str(), b, g, r);
|
|
145
133
|
}
|
|
146
134
|
}
|
|
147
135
|
|
|
148
|
-
//
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
// Parse boxes array structure - direct array format
|
|
136
|
+
// Parse boxes array structure (JS → plain structs, normalized coords)
|
|
137
|
+
parsedBoxes.reserve(boxesArray.Length());
|
|
152
138
|
for (size_t boxIndex = 0; boxIndex < boxesArray.Length(); boxIndex++) {
|
|
153
139
|
Napi::Object boxObj = boxesArray.Get(boxIndex).As<Napi::Object>();
|
|
154
140
|
|
|
@@ -158,108 +144,113 @@ public:
|
|
|
158
144
|
if (box.Length() != 4) continue;
|
|
159
145
|
|
|
160
146
|
// Parse 4 corners: [[x1,y1], [x2,y1], [x2,y2], [x1,y2]]
|
|
161
|
-
|
|
147
|
+
ParsedBox pb;
|
|
148
|
+
pb.corners.reserve(4);
|
|
162
149
|
for (size_t i = 0; i < 4; i++) {
|
|
163
150
|
Napi::Array corner = box.Get(i).As<Napi::Array>();
|
|
164
151
|
if (corner.Length() >= 2) {
|
|
165
152
|
float x = corner.Get(0u).As<Napi::Number>().FloatValue();
|
|
166
153
|
float y = corner.Get(1u).As<Napi::Number>().FloatValue();
|
|
167
|
-
corners.emplace_back(x, y);
|
|
154
|
+
pb.corners.emplace_back(x, y);
|
|
168
155
|
}
|
|
169
156
|
}
|
|
170
|
-
|
|
171
|
-
if (corners.size() != 4) continue;
|
|
157
|
+
if (pb.corners.size() != 4) continue;
|
|
172
158
|
|
|
173
159
|
// Extract tag (class name) and confidence
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
// Skip unmapped classes if onlyMapped is true
|
|
179
|
-
if (onlyMapped) {
|
|
180
|
-
if (userColorMap.find(className) == userColorMap.end()) {
|
|
181
|
-
continue; // Skip this box
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// Get color for this class and pre-compute BGR scalar
|
|
186
|
-
cv::Vec3f color;
|
|
187
|
-
auto colorIt = userColorMap.find(className);
|
|
188
|
-
if (colorIt != userColorMap.end()) {
|
|
189
|
-
color = colorIt->second;
|
|
190
|
-
} else {
|
|
191
|
-
color = GenerateMaximallyDifferentColor(className, userColorMap, colorCache);
|
|
192
|
-
}
|
|
160
|
+
pb.className = boxObj.Get("tag").As<Napi::String>().Utf8Value();
|
|
161
|
+
pb.confidence = boxObj.Has("confidence") ?
|
|
162
|
+
boxObj.Get("confidence").As<Napi::Number>().FloatValue() : 1.0f;
|
|
193
163
|
|
|
194
|
-
|
|
195
|
-
cv::Scalar bgrColor(
|
|
196
|
-
color[0] * 255.0f, // B
|
|
197
|
-
color[1] * 255.0f, // G
|
|
198
|
-
color[2] * 255.0f // R
|
|
199
|
-
);
|
|
200
|
-
|
|
201
|
-
// Convert normalized corners to pixel coordinates
|
|
202
|
-
std::vector<cv::Point> pixelCorners;
|
|
203
|
-
pixelCorners.reserve(4);
|
|
204
|
-
float minX = corners[0].x, maxX = corners[0].x;
|
|
205
|
-
float minY = corners[0].y, maxY = corners[0].y;
|
|
206
|
-
for (int i = 0; i < 4; i++) {
|
|
207
|
-
int px = static_cast<int>(corners[i].x * imageMat.cols);
|
|
208
|
-
int py = static_cast<int>(corners[i].y * imageMat.rows);
|
|
209
|
-
px = std::max(0, std::min(imageMat.cols - 1, px));
|
|
210
|
-
py = std::max(0, std::min(imageMat.rows - 1, py));
|
|
211
|
-
pixelCorners.emplace_back(px, py);
|
|
212
|
-
minX = std::min(minX, corners[i].x);
|
|
213
|
-
maxX = std::max(maxX, corners[i].x);
|
|
214
|
-
minY = std::min(minY, corners[i].y);
|
|
215
|
-
maxY = std::max(maxY, corners[i].y);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// Axis-aligned bounding rect (for label positioning)
|
|
219
|
-
cv::Rect bbox;
|
|
220
|
-
bbox.x = static_cast<int>(minX * imageMat.cols);
|
|
221
|
-
bbox.y = static_cast<int>(minY * imageMat.rows);
|
|
222
|
-
bbox.width = static_cast<int>((maxX - minX) * imageMat.cols);
|
|
223
|
-
bbox.height = static_cast<int>((maxY - minY) * imageMat.rows);
|
|
224
|
-
bbox.x = std::max(0, std::min(imageMat.cols - 1, bbox.x));
|
|
225
|
-
bbox.y = std::max(0, std::min(imageMat.rows - 1, bbox.y));
|
|
226
|
-
bbox.width = std::min(bbox.width, imageMat.cols - bbox.x);
|
|
227
|
-
bbox.height = std::min(bbox.height, imageMat.rows - bbox.y);
|
|
228
|
-
|
|
229
|
-
// Pre-generate label text with optimized string handling
|
|
230
|
-
char labelBuffer[256];
|
|
231
|
-
if (showClassName && showConfidence) {
|
|
232
|
-
snprintf(labelBuffer, sizeof(labelBuffer), "%s (%.1f%%)", className.c_str(), confidence * 100.0f);
|
|
233
|
-
} else if (showClassName) {
|
|
234
|
-
snprintf(labelBuffer, sizeof(labelBuffer), "%s", className.c_str());
|
|
235
|
-
} else if (showConfidence) {
|
|
236
|
-
snprintf(labelBuffer, sizeof(labelBuffer), "%.1f%%", confidence * 100.0f);
|
|
237
|
-
} else {
|
|
238
|
-
labelBuffer[0] = '\0';
|
|
239
|
-
}
|
|
240
|
-
std::string labelText(labelBuffer);
|
|
241
|
-
|
|
242
|
-
// Store bbox info with pre-computed values
|
|
243
|
-
BBoxInfo info;
|
|
244
|
-
info.boundingRect = bbox;
|
|
245
|
-
info.cornersPx = pixelCorners;
|
|
246
|
-
info.className = className;
|
|
247
|
-
info.confidence = confidence;
|
|
248
|
-
info.bgrColor = bgrColor;
|
|
249
|
-
info.labelText = labelText;
|
|
250
|
-
bboxInfos.push_back(info);
|
|
164
|
+
parsedBoxes.push_back(std::move(pb));
|
|
251
165
|
}
|
|
252
166
|
}
|
|
253
|
-
|
|
254
|
-
imageFormat = DetectChannelFormatShared(jsImg, imageMat);
|
|
255
|
-
outputChannel = imageFormat;
|
|
256
|
-
|
|
257
|
-
convertMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
258
167
|
}
|
|
259
168
|
|
|
260
169
|
protected:
|
|
261
170
|
void Execute() override {
|
|
262
|
-
|
|
171
|
+
if (captureFailed_) return;
|
|
172
|
+
|
|
173
|
+
// Decode (encoded inputs only) on the worker thread
|
|
174
|
+
int64 t0 = cv::getTickCount();
|
|
175
|
+
src_.Materialize();
|
|
176
|
+
const cv::Mat& imageMat = src_.mat;
|
|
177
|
+
imageFormat = src_.colorSpace;
|
|
178
|
+
outputChannel = imageFormat;
|
|
179
|
+
|
|
180
|
+
// Resolve colors, labels and pixel geometry now that the image size is known
|
|
181
|
+
bboxInfos.clear();
|
|
182
|
+
bboxInfos.reserve(parsedBoxes.size());
|
|
183
|
+
for (const auto& pb : parsedBoxes) {
|
|
184
|
+
// Skip unmapped classes if onlyMapped is true
|
|
185
|
+
if (onlyMapped && userColorMap.find(pb.className) == userColorMap.end()) {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Get color for this class and pre-compute BGR scalar
|
|
190
|
+
cv::Vec3f color;
|
|
191
|
+
auto colorIt = userColorMap.find(pb.className);
|
|
192
|
+
if (colorIt != userColorMap.end()) {
|
|
193
|
+
color = colorIt->second;
|
|
194
|
+
} else {
|
|
195
|
+
auto cachedIt = colorCache.find(pb.className);
|
|
196
|
+
color = (cachedIt != colorCache.end())
|
|
197
|
+
? cachedIt->second
|
|
198
|
+
: GenerateMaximallyDifferentColor(pb.className, userColorMap, colorCache);
|
|
199
|
+
}
|
|
200
|
+
cv::Scalar bgrColor(color[0] * 255.0f, color[1] * 255.0f, color[2] * 255.0f);
|
|
201
|
+
|
|
202
|
+
// Convert normalized corners to pixel coordinates
|
|
203
|
+
std::vector<cv::Point> pixelCorners;
|
|
204
|
+
pixelCorners.reserve(4);
|
|
205
|
+
float minX = pb.corners[0].x, maxX = pb.corners[0].x;
|
|
206
|
+
float minY = pb.corners[0].y, maxY = pb.corners[0].y;
|
|
207
|
+
for (int i = 0; i < 4; i++) {
|
|
208
|
+
int px = static_cast<int>(pb.corners[i].x * imageMat.cols);
|
|
209
|
+
int py = static_cast<int>(pb.corners[i].y * imageMat.rows);
|
|
210
|
+
px = std::max(0, std::min(imageMat.cols - 1, px));
|
|
211
|
+
py = std::max(0, std::min(imageMat.rows - 1, py));
|
|
212
|
+
pixelCorners.emplace_back(px, py);
|
|
213
|
+
minX = std::min(minX, pb.corners[i].x);
|
|
214
|
+
maxX = std::max(maxX, pb.corners[i].x);
|
|
215
|
+
minY = std::min(minY, pb.corners[i].y);
|
|
216
|
+
maxY = std::max(maxY, pb.corners[i].y);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Axis-aligned bounding rect (for label positioning)
|
|
220
|
+
cv::Rect bbox;
|
|
221
|
+
bbox.x = static_cast<int>(minX * imageMat.cols);
|
|
222
|
+
bbox.y = static_cast<int>(minY * imageMat.rows);
|
|
223
|
+
bbox.width = static_cast<int>((maxX - minX) * imageMat.cols);
|
|
224
|
+
bbox.height = static_cast<int>((maxY - minY) * imageMat.rows);
|
|
225
|
+
bbox.x = std::max(0, std::min(imageMat.cols - 1, bbox.x));
|
|
226
|
+
bbox.y = std::max(0, std::min(imageMat.rows - 1, bbox.y));
|
|
227
|
+
bbox.width = std::min(bbox.width, imageMat.cols - bbox.x);
|
|
228
|
+
bbox.height = std::min(bbox.height, imageMat.rows - bbox.y);
|
|
229
|
+
|
|
230
|
+
// Pre-generate label text with optimized string handling
|
|
231
|
+
char labelBuffer[256];
|
|
232
|
+
if (showClassName && showConfidence) {
|
|
233
|
+
snprintf(labelBuffer, sizeof(labelBuffer), "%s (%.1f%%)", pb.className.c_str(), pb.confidence * 100.0f);
|
|
234
|
+
} else if (showClassName) {
|
|
235
|
+
snprintf(labelBuffer, sizeof(labelBuffer), "%s", pb.className.c_str());
|
|
236
|
+
} else if (showConfidence) {
|
|
237
|
+
snprintf(labelBuffer, sizeof(labelBuffer), "%.1f%%", pb.confidence * 100.0f);
|
|
238
|
+
} else {
|
|
239
|
+
labelBuffer[0] = '\0';
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
BBoxInfo info;
|
|
243
|
+
info.boundingRect = bbox;
|
|
244
|
+
info.cornersPx = std::move(pixelCorners);
|
|
245
|
+
info.className = pb.className;
|
|
246
|
+
info.confidence = pb.confidence;
|
|
247
|
+
info.bgrColor = bgrColor;
|
|
248
|
+
info.labelText = labelBuffer;
|
|
249
|
+
bboxInfos.push_back(std::move(info));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
convertMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
253
|
+
t0 = cv::getTickCount();
|
|
263
254
|
|
|
264
255
|
// Work directly on the image if possible, avoid clone
|
|
265
256
|
bool needsConversion = (imageFormat == "RGB" || imageFormat == "RGBA" || imageFormat == "GRAY");
|
|
@@ -274,7 +265,7 @@ protected:
|
|
|
274
265
|
cv::cvtColor(imageMat, result, cv::COLOR_GRAY2BGR);
|
|
275
266
|
}
|
|
276
267
|
} else {
|
|
277
|
-
// Already BGR/BGRA - clone
|
|
268
|
+
// Already BGR/BGRA - clone (never draw into JS-owned memory)
|
|
278
269
|
result = imageMat.clone();
|
|
279
270
|
}
|
|
280
271
|
|
|
@@ -368,6 +359,7 @@ protected:
|
|
|
368
359
|
cv::cvtColor(result, result, cv::COLOR_BGR2GRAY);
|
|
369
360
|
}
|
|
370
361
|
// If already BGR/BGRA, no conversion needed
|
|
362
|
+
FinalizeForOutput(result);
|
|
371
363
|
} else {
|
|
372
364
|
// For encoded formats, keep as BGR
|
|
373
365
|
cv::Mat tmp = result;
|
|
@@ -387,10 +379,18 @@ protected:
|
|
|
387
379
|
Callback().Call({env.Null(), out});
|
|
388
380
|
}
|
|
389
381
|
|
|
382
|
+
void OnError(const Napi::Error& e) override {
|
|
383
|
+
Callback().Call({ e.Value(), Env().Null() });
|
|
384
|
+
}
|
|
385
|
+
|
|
390
386
|
private:
|
|
391
|
-
|
|
387
|
+
ImageSource src_;
|
|
388
|
+
bool captureFailed_ = false;
|
|
389
|
+
cv::Mat result;
|
|
392
390
|
std::string imageFormat, outputChannel;
|
|
391
|
+
std::vector<ParsedBox> parsedBoxes;
|
|
393
392
|
std::vector<BBoxInfo> bboxInfos;
|
|
393
|
+
std::unordered_map<std::string, cv::Vec3f> userColorMap;
|
|
394
394
|
bool showClassName, showConfidence, onlyMapped, labelBackground;
|
|
395
395
|
int boxThickness;
|
|
396
396
|
double fontSize;
|
|
@@ -48,12 +48,15 @@ public:
|
|
|
48
48
|
outputFormat(std::move(outputFormat)),
|
|
49
49
|
quality(quality), pngOptimize(pngOptimize)
|
|
50
50
|
{
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
// JS thread: metadata + persistent reference only (no decode, no pixels)
|
|
52
|
+
try {
|
|
53
|
+
src_ = CaptureImage(jsImg);
|
|
54
|
+
} catch (const Napi::Error& e) {
|
|
55
|
+
captureFailed_ = true; SetError(e.Message());
|
|
56
|
+
} catch (const std::exception& e) {
|
|
57
|
+
captureFailed_ = true; SetError(e.what());
|
|
58
|
+
}
|
|
59
|
+
|
|
57
60
|
// Parse polygon coordinates from JavaScript array
|
|
58
61
|
polygon.reserve(polygonArray.Length());
|
|
59
62
|
for (size_t i = 0; i < polygonArray.Length(); i++) {
|
|
@@ -65,18 +68,22 @@ public:
|
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
70
|
|
|
68
|
-
// Detect image channel format
|
|
69
|
-
imageFormat = DetectChannelFormatShared(jsImg, imageMat);
|
|
70
|
-
|
|
71
|
-
// For polygon-based masking, output format matches image format
|
|
72
|
-
outputChannel = imageFormat;
|
|
73
|
-
|
|
74
|
-
convertMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
75
71
|
}
|
|
76
72
|
|
|
77
73
|
protected:
|
|
78
74
|
void Execute() override {
|
|
79
|
-
|
|
75
|
+
if (captureFailed_) return;
|
|
76
|
+
|
|
77
|
+
// Decode (encoded inputs only) on the worker thread
|
|
78
|
+
int64 t0 = cv::getTickCount();
|
|
79
|
+
src_.Materialize();
|
|
80
|
+
const cv::Mat& imageMat = src_.mat;
|
|
81
|
+
imageFormat = src_.colorSpace;
|
|
82
|
+
// For polygon-based masking, output format matches image format
|
|
83
|
+
outputChannel = imageFormat;
|
|
84
|
+
convertMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
85
|
+
|
|
86
|
+
t0 = cv::getTickCount();
|
|
80
87
|
|
|
81
88
|
// Convert image to target format
|
|
82
89
|
cv::Mat img = ConvertToTargetFormatShared(imageMat, imageFormat, outputChannel);
|
|
@@ -155,6 +162,8 @@ protected:
|
|
|
155
162
|
if (outputFormat != "raw") {
|
|
156
163
|
cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
|
|
157
164
|
encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
|
|
165
|
+
} else {
|
|
166
|
+
FinalizeForOutput(result);
|
|
158
167
|
}
|
|
159
168
|
}
|
|
160
169
|
|
|
@@ -169,8 +178,14 @@ protected:
|
|
|
169
178
|
Callback().Call({env.Null(), out});
|
|
170
179
|
}
|
|
171
180
|
|
|
181
|
+
void OnError(const Napi::Error& e) override {
|
|
182
|
+
Callback().Call({ e.Value(), Env().Null() });
|
|
183
|
+
}
|
|
184
|
+
|
|
172
185
|
private:
|
|
173
|
-
|
|
186
|
+
ImageSource src_;
|
|
187
|
+
bool captureFailed_ = false;
|
|
188
|
+
cv::Mat result;
|
|
174
189
|
std::string imageFormat, outputChannel;
|
|
175
190
|
std::vector<cv::Point2f> polygon;
|
|
176
191
|
double maskStrength;
|