@rosepetal/node-red-contrib-image-tools 1.1.3
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/.gitattributes +6 -0
- package/.github/workflows/build-prebuild.yml +326 -0
- package/LICENSE +13 -0
- package/README.md +161 -0
- package/artifacts/darwin-arm64/package.json +25 -0
- package/artifacts/linux-arm64/package.json +26 -0
- package/artifacts/linux-x64/package.json +26 -0
- package/artifacts/linuxmusl-x64/package.json +26 -0
- package/assets/flow_time_example.gif +0 -0
- package/assets/nodes/io/array-in-demo.gif +0 -0
- package/assets/nodes/io/array-out-demo.gif +0 -0
- package/assets/nodes/io/array-select-demo.gif +0 -0
- package/assets/nodes/io/image-in-demo.gif +0 -0
- package/assets/nodes/mix/concat-demo.gif +0 -0
- package/assets/nodes/mix/mosaic-demo.gif +0 -0
- package/assets/nodes/transform/crop-demo.gif +0 -0
- package/assets/nodes/transform/filter-demo.gif +0 -0
- package/assets/nodes/transform/padding-demo.gif +0 -0
- package/assets/nodes/transform/resize-demo.gif +0 -0
- package/assets/nodes/transform/rotate-demo.gif +0 -0
- package/docs/nodes/blend/add-mask.md +275 -0
- package/docs/nodes/blend/add-masks.md +284 -0
- package/docs/nodes/blend/blend.md +248 -0
- package/docs/nodes/io/array-in.md +119 -0
- package/docs/nodes/io/array-out.md +150 -0
- package/docs/nodes/io/array-select.md +157 -0
- package/docs/nodes/io/image-in.md +131 -0
- package/docs/nodes/io/image-out.md +179 -0
- package/docs/nodes/io/queue.md +57 -0
- package/docs/nodes/io/save-file.md +30 -0
- package/docs/nodes/mix/advanced-mosaic.md +108 -0
- package/docs/nodes/mix/concat.md +127 -0
- package/docs/nodes/mix/mosaic.md +146 -0
- package/docs/nodes/specialized/cropBB.md +271 -0
- package/docs/nodes/specialized/image-align.md +426 -0
- package/docs/nodes/transform/crop.md +132 -0
- package/docs/nodes/transform/draw.md +77 -0
- package/docs/nodes/transform/filter.md +84 -0
- package/docs/nodes/transform/padding.md +133 -0
- package/docs/nodes/transform/resize.md +158 -0
- package/docs/nodes/transform/rotate.md +121 -0
- package/docs/nodes/util/block-detect.md +46 -0
- package/install.sh +144 -0
- package/node-red-contrib-image-tools/lib/cpp-bridge.js +224 -0
- package/node-red-contrib-image-tools/lib/node-utils.js +529 -0
- package/node-red-contrib-image-tools/nodes/blend/add-bbs.html +590 -0
- package/node-red-contrib-image-tools/nodes/blend/add-bbs.js +241 -0
- package/node-red-contrib-image-tools/nodes/blend/add-mask.html +449 -0
- package/node-red-contrib-image-tools/nodes/blend/add-mask.js +170 -0
- package/node-red-contrib-image-tools/nodes/blend/add-masks.html +703 -0
- package/node-red-contrib-image-tools/nodes/blend/add-masks.js +321 -0
- package/node-red-contrib-image-tools/nodes/blend/blend.html +552 -0
- package/node-red-contrib-image-tools/nodes/blend/blend.js +124 -0
- package/node-red-contrib-image-tools/nodes/io/array-in.html +110 -0
- package/node-red-contrib-image-tools/nodes/io/array-in.js +113 -0
- package/node-red-contrib-image-tools/nodes/io/array-out.html +125 -0
- package/node-red-contrib-image-tools/nodes/io/array-out.js +213 -0
- package/node-red-contrib-image-tools/nodes/io/array-select.html +156 -0
- package/node-red-contrib-image-tools/nodes/io/array-select.js +187 -0
- package/node-red-contrib-image-tools/nodes/io/folder-in.html +248 -0
- package/node-red-contrib-image-tools/nodes/io/folder-in.js +335 -0
- package/node-red-contrib-image-tools/nodes/io/image-in.html +146 -0
- package/node-red-contrib-image-tools/nodes/io/image-in.js +143 -0
- package/node-red-contrib-image-tools/nodes/io/image-out.html +506 -0
- package/node-red-contrib-image-tools/nodes/io/image-out.js +545 -0
- package/node-red-contrib-image-tools/nodes/io/queue.html +119 -0
- package/node-red-contrib-image-tools/nodes/io/queue.js +191 -0
- package/node-red-contrib-image-tools/nodes/io/save-file.html +243 -0
- package/node-red-contrib-image-tools/nodes/io/save-file.js +440 -0
- package/node-red-contrib-image-tools/nodes/mix/advanced-mosaic.html +2149 -0
- package/node-red-contrib-image-tools/nodes/mix/advanced-mosaic.js +195 -0
- package/node-red-contrib-image-tools/nodes/mix/concat.html +390 -0
- package/node-red-contrib-image-tools/nodes/mix/concat.js +112 -0
- package/node-red-contrib-image-tools/nodes/mix/mosaic.html +550 -0
- package/node-red-contrib-image-tools/nodes/mix/mosaic.js +151 -0
- package/node-red-contrib-image-tools/nodes/specialized/cropBB.html +268 -0
- package/node-red-contrib-image-tools/nodes/specialized/cropBB.js +277 -0
- package/node-red-contrib-image-tools/nodes/specialized/image-align.html +528 -0
- package/node-red-contrib-image-tools/nodes/specialized/image-align.js +306 -0
- package/node-red-contrib-image-tools/nodes/transform/crop.html +410 -0
- package/node-red-contrib-image-tools/nodes/transform/crop.js +149 -0
- package/node-red-contrib-image-tools/nodes/transform/draw.html +719 -0
- package/node-red-contrib-image-tools/nodes/transform/draw.js +509 -0
- package/node-red-contrib-image-tools/nodes/transform/filter.html +578 -0
- package/node-red-contrib-image-tools/nodes/transform/filter.js +177 -0
- package/node-red-contrib-image-tools/nodes/transform/padding.html +475 -0
- package/node-red-contrib-image-tools/nodes/transform/padding.js +142 -0
- package/node-red-contrib-image-tools/nodes/transform/resize.html +429 -0
- package/node-red-contrib-image-tools/nodes/transform/resize.js +162 -0
- package/node-red-contrib-image-tools/nodes/transform/rotate.html +299 -0
- package/node-red-contrib-image-tools/nodes/transform/rotate.js +141 -0
- package/node-red-contrib-image-tools/nodes/util/block-detect.html +85 -0
- package/node-red-contrib-image-tools/nodes/util/block-detect.js +136 -0
- package/node-red-contrib-image-tools/nodes/util/clean-debug.html +201 -0
- package/node-red-contrib-image-tools/nodes/util/clean-debug.js +393 -0
- package/node-red-contrib-image-tools/package.json +64 -0
- package/npm/darwin-arm64/lib/.gitkeep +1 -0
- package/npm/darwin-arm64/package.json +25 -0
- package/npm/darwin-x64/lib/.gitkeep +1 -0
- package/npm/darwin-x64/package.json +25 -0
- package/npm/linux-arm64/lib/.gitkeep +1 -0
- package/npm/linux-arm64/package.json +26 -0
- package/npm/linux-x64/lib/.gitkeep +1 -0
- package/npm/linux-x64/package.json +26 -0
- package/npm/linuxmusl-x64/lib/.gitkeep +1 -0
- package/npm/linuxmusl-x64/package.json +26 -0
- package/npm/win32-x64/lib/.gitkeep +1 -0
- package/npm/win32-x64/package.json +25 -0
- package/package.json +74 -0
- package/rosepetal-image-engine/binding.gyp +153 -0
- package/rosepetal-image-engine/package.json +19 -0
- package/rosepetal-image-engine/src/add-bbs.cpp +433 -0
- package/rosepetal-image-engine/src/add-mask.cpp +240 -0
- package/rosepetal-image-engine/src/add-masks.cpp +638 -0
- package/rosepetal-image-engine/src/advanced-mosaic.cpp +740 -0
- package/rosepetal-image-engine/src/blend.cpp +200 -0
- package/rosepetal-image-engine/src/concat.cpp +329 -0
- package/rosepetal-image-engine/src/crop.cpp +139 -0
- package/rosepetal-image-engine/src/draw.cpp +414 -0
- package/rosepetal-image-engine/src/filter.cpp +230 -0
- package/rosepetal-image-engine/src/filters/kernels.h +171 -0
- package/rosepetal-image-engine/src/image-align.cpp +551 -0
- package/rosepetal-image-engine/src/main.cpp +36 -0
- package/rosepetal-image-engine/src/mosaic.cpp +373 -0
- package/rosepetal-image-engine/src/padding.cpp +124 -0
- package/rosepetal-image-engine/src/resize.cpp +210 -0
- package/rosepetal-image-engine/src/rotate.cpp +177 -0
- package/rosepetal-image-engine/src/utils.h +563 -0
- package/rosepetal-image-engine/test-formats.js +119 -0
- package/scripts/build-opencv-static.ps1 +166 -0
- package/scripts/build-opencv-static.sh +147 -0
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
// draw.cpp - draw points and lines on images using OpenCV (ULTRA-OPTIMIZED)
|
|
2
|
+
#include <napi.h>
|
|
3
|
+
#include <opencv2/opencv.hpp>
|
|
4
|
+
#include <vector>
|
|
5
|
+
#include <array>
|
|
6
|
+
#include <algorithm>
|
|
7
|
+
#include <cmath>
|
|
8
|
+
#include <cstring>
|
|
9
|
+
#include "utils.h"
|
|
10
|
+
|
|
11
|
+
namespace {
|
|
12
|
+
struct DrawPoint {
|
|
13
|
+
float x;
|
|
14
|
+
float y;
|
|
15
|
+
float radius;
|
|
16
|
+
std::array<float, 4> color; // B, G, R, A normalized [0,1]
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
struct DrawLine {
|
|
20
|
+
float x1;
|
|
21
|
+
float y1;
|
|
22
|
+
float x2;
|
|
23
|
+
float y2;
|
|
24
|
+
float thickness;
|
|
25
|
+
std::array<float, 4> color; // B, G, R, A normalized [0,1]
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
float Clamp01(float value) {
|
|
29
|
+
return std::max(0.0f, std::min(1.0f, value));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
int ClampPixel(int value, int maxExclusive) {
|
|
33
|
+
if (maxExclusive <= 0) return 0;
|
|
34
|
+
return std::max(0, std::min(maxExclusive - 1, value));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// ULTRA-FAST: ROI-based blending - only process affected pixels
|
|
38
|
+
inline void UltraFastBlend(cv::Mat& canvas, const cv::Mat& mask,
|
|
39
|
+
const std::array<float, 4>& color,
|
|
40
|
+
const cv::Rect& roi) {
|
|
41
|
+
const int channels = canvas.channels();
|
|
42
|
+
|
|
43
|
+
// Extract ROI for faster processing
|
|
44
|
+
cv::Mat canvasROI = canvas(roi);
|
|
45
|
+
cv::Mat maskROI = mask(roi);
|
|
46
|
+
|
|
47
|
+
if (channels == 1) {
|
|
48
|
+
// Grayscale: vectorized operation
|
|
49
|
+
const float intensity = Clamp01(color[2] * 0.299f + color[1] * 0.587f + color[0] * 0.114f);
|
|
50
|
+
|
|
51
|
+
cv::Mat colorMat;
|
|
52
|
+
cv::multiply(maskROI, cv::Scalar(intensity), colorMat);
|
|
53
|
+
cv::addWeighted(canvasROI, 1.0, colorMat, 1.0, 0.0, canvasROI);
|
|
54
|
+
cv::min(canvasROI, 1.0, canvasROI);
|
|
55
|
+
|
|
56
|
+
} else if (channels == 3) {
|
|
57
|
+
// RGB/BGR: Use optimized vectorized operations
|
|
58
|
+
const cv::Scalar colorScalar(color[0], color[1], color[2]);
|
|
59
|
+
|
|
60
|
+
// Create inverse mask
|
|
61
|
+
cv::Mat invMask;
|
|
62
|
+
cv::subtract(1.0f, maskROI, invMask);
|
|
63
|
+
|
|
64
|
+
// Vectorized blending: canvas = canvas * (1-alpha) + color * alpha
|
|
65
|
+
std::vector<cv::Mat> canvasChannels;
|
|
66
|
+
cv::split(canvasROI, canvasChannels);
|
|
67
|
+
|
|
68
|
+
cv::Mat colorMask0, colorMask1, colorMask2;
|
|
69
|
+
cv::multiply(maskROI, colorScalar[0], colorMask0);
|
|
70
|
+
cv::multiply(maskROI, colorScalar[1], colorMask1);
|
|
71
|
+
cv::multiply(maskROI, colorScalar[2], colorMask2);
|
|
72
|
+
|
|
73
|
+
cv::multiply(canvasChannels[0], invMask, canvasChannels[0]);
|
|
74
|
+
cv::multiply(canvasChannels[1], invMask, canvasChannels[1]);
|
|
75
|
+
cv::multiply(canvasChannels[2], invMask, canvasChannels[2]);
|
|
76
|
+
|
|
77
|
+
canvasChannels[0] += colorMask0;
|
|
78
|
+
canvasChannels[1] += colorMask1;
|
|
79
|
+
canvasChannels[2] += colorMask2;
|
|
80
|
+
|
|
81
|
+
cv::merge(canvasChannels, canvasROI);
|
|
82
|
+
cv::min(canvasROI, 1.0, canvasROI);
|
|
83
|
+
|
|
84
|
+
} else if (channels == 4) {
|
|
85
|
+
// RGBA/BGRA: Include alpha channel
|
|
86
|
+
const cv::Scalar colorScalar(color[0], color[1], color[2], color[3]);
|
|
87
|
+
|
|
88
|
+
cv::Mat invMask;
|
|
89
|
+
cv::subtract(1.0f, maskROI, invMask);
|
|
90
|
+
|
|
91
|
+
std::vector<cv::Mat> canvasChannels;
|
|
92
|
+
cv::split(canvasROI, canvasChannels);
|
|
93
|
+
|
|
94
|
+
cv::Mat colorMask[4];
|
|
95
|
+
for (int i = 0; i < 4; ++i) {
|
|
96
|
+
cv::multiply(maskROI, colorScalar[i], colorMask[i]);
|
|
97
|
+
cv::multiply(canvasChannels[i], invMask, canvasChannels[i]);
|
|
98
|
+
canvasChannels[i] += colorMask[i];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
cv::merge(canvasChannels, canvasROI);
|
|
102
|
+
cv::min(canvasROI, 1.0, canvasROI);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
} // namespace
|
|
107
|
+
|
|
108
|
+
class DrawWorker final : public Napi::AsyncWorker {
|
|
109
|
+
public:
|
|
110
|
+
DrawWorker(Napi::Function& callback,
|
|
111
|
+
const Napi::Value& jsImage,
|
|
112
|
+
const Napi::Value& jsPoints,
|
|
113
|
+
const Napi::Value& jsLines,
|
|
114
|
+
std::string outputFormat,
|
|
115
|
+
int quality,
|
|
116
|
+
bool pngOptimize)
|
|
117
|
+
: Napi::AsyncWorker(callback),
|
|
118
|
+
outputFormat(std::move(outputFormat)),
|
|
119
|
+
quality(quality),
|
|
120
|
+
pngOptimize(pngOptimize) {
|
|
121
|
+
|
|
122
|
+
const int64 t0 = cv::getTickCount();
|
|
123
|
+
baseImage = ConvertToMat(jsImage);
|
|
124
|
+
convertMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
125
|
+
|
|
126
|
+
imageChannelOrder = DetectChannelFormatShared(jsImage, baseImage);
|
|
127
|
+
ParsePoints(jsPoints);
|
|
128
|
+
ParseLines(jsLines);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
protected:
|
|
132
|
+
void Execute() override {
|
|
133
|
+
const int64 t0 = cv::getTickCount();
|
|
134
|
+
|
|
135
|
+
const int width = baseImage.cols;
|
|
136
|
+
const int height = baseImage.rows;
|
|
137
|
+
if (width <= 0 || height <= 0) {
|
|
138
|
+
throw std::runtime_error("Input image has invalid dimensions");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Early exit if nothing to draw
|
|
142
|
+
if (points.empty() && lines.empty()) {
|
|
143
|
+
resultImage = baseImage.clone();
|
|
144
|
+
taskMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Determine processing format (OpenCV expects BGR/BGRA)
|
|
149
|
+
const int channels = baseImage.channels();
|
|
150
|
+
std::string processingOrder;
|
|
151
|
+
if (channels == 1) {
|
|
152
|
+
processingOrder = "GRAY";
|
|
153
|
+
} else if (channels == 3) {
|
|
154
|
+
processingOrder = "BGR";
|
|
155
|
+
} else if (channels == 4) {
|
|
156
|
+
processingOrder = "BGRA";
|
|
157
|
+
} else {
|
|
158
|
+
throw std::runtime_error("Unsupported channel count for draw node");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
cv::Mat working = ConvertToTargetFormatShared(baseImage, imageChannelOrder, processingOrder);
|
|
162
|
+
|
|
163
|
+
// OPTIMIZATION: Convert to float once, operate in-place
|
|
164
|
+
const int targetType =
|
|
165
|
+
(channels == 1) ? CV_32FC1 :
|
|
166
|
+
(channels == 3) ? CV_32FC3 :
|
|
167
|
+
CV_32FC4;
|
|
168
|
+
|
|
169
|
+
cv::Mat canvas;
|
|
170
|
+
working.convertTo(canvas, targetType, 1.0 / 255.0);
|
|
171
|
+
|
|
172
|
+
const float minDimension = static_cast<float>(std::max(1, std::min(width, height)));
|
|
173
|
+
|
|
174
|
+
// ULTRA-OPTIMIZATION: Pre-allocate mask buffer once
|
|
175
|
+
cv::Mat maskBuffer(canvas.rows, canvas.cols, CV_32FC1);
|
|
176
|
+
|
|
177
|
+
// Process all points
|
|
178
|
+
for (const auto& point : points) {
|
|
179
|
+
ApplyPointUltraFast(canvas, maskBuffer, point, width, height, minDimension);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Process all lines
|
|
183
|
+
for (const auto& line : lines) {
|
|
184
|
+
ApplyLineUltraFast(canvas, maskBuffer, line, width, height, minDimension);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// OPTIMIZATION: Single clamping pass
|
|
188
|
+
cv::min(canvas, 1.0, canvas);
|
|
189
|
+
cv::max(canvas, 0.0, canvas);
|
|
190
|
+
|
|
191
|
+
// Convert back to uint8
|
|
192
|
+
canvas.convertTo(working,
|
|
193
|
+
(channels == 1) ? CV_8UC1 :
|
|
194
|
+
(channels == 3) ? CV_8UC3 :
|
|
195
|
+
CV_8UC4,
|
|
196
|
+
255.0);
|
|
197
|
+
|
|
198
|
+
if (processingOrder != imageChannelOrder) {
|
|
199
|
+
resultImage = ConvertToTargetFormatShared(working, processingOrder, imageChannelOrder);
|
|
200
|
+
} else {
|
|
201
|
+
resultImage = working;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (outputFormat != "raw") {
|
|
205
|
+
cv::Mat encodeSrc = ToBgrForJpg(resultImage, imageChannelOrder);
|
|
206
|
+
encodeMs = EncodeToFormat(encodeSrc, encodedBuffer, outputFormat, quality, pngOptimize);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
taskMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
void OnOK() override {
|
|
213
|
+
Napi::Env env = Env();
|
|
214
|
+
Napi::Value outImage =
|
|
215
|
+
(outputFormat != "raw")
|
|
216
|
+
? VectorToBuffer(env, std::move(encodedBuffer))
|
|
217
|
+
: MatToRawJS(env, resultImage, imageChannelOrder);
|
|
218
|
+
|
|
219
|
+
Napi::Object response = Napi::Object::New(env);
|
|
220
|
+
response.Set("image", outImage);
|
|
221
|
+
response.Set("timing", MakeTimingJS(env, convertMs, taskMs, encodeMs));
|
|
222
|
+
Callback().Call({ env.Null(), response });
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private:
|
|
226
|
+
void ParsePoints(const Napi::Value& value) {
|
|
227
|
+
if (!value.IsArray()) return;
|
|
228
|
+
Napi::Array arr = value.As<Napi::Array>();
|
|
229
|
+
points.reserve(arr.Length());
|
|
230
|
+
for (uint32_t i = 0; i < arr.Length(); ++i) {
|
|
231
|
+
Napi::Value entry = arr.Get(i);
|
|
232
|
+
if (!entry.IsObject()) continue;
|
|
233
|
+
Napi::Object obj = entry.As<Napi::Object>();
|
|
234
|
+
|
|
235
|
+
if (!obj.Has("x") || !obj.Has("y") || !obj.Has("radius") ||
|
|
236
|
+
!obj.Has("r") || !obj.Has("g") || !obj.Has("b") || !obj.Has("a")) {
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
DrawPoint pt;
|
|
241
|
+
pt.x = Clamp01(obj.Get("x").As<Napi::Number>().FloatValue());
|
|
242
|
+
pt.y = Clamp01(obj.Get("y").As<Napi::Number>().FloatValue());
|
|
243
|
+
pt.radius = Clamp01(obj.Get("radius").As<Napi::Number>().FloatValue());
|
|
244
|
+
const float r = Clamp01(obj.Get("r").As<Napi::Number>().FloatValue() / 255.0f);
|
|
245
|
+
const float g = Clamp01(obj.Get("g").As<Napi::Number>().FloatValue() / 255.0f);
|
|
246
|
+
const float b = Clamp01(obj.Get("b").As<Napi::Number>().FloatValue() / 255.0f);
|
|
247
|
+
const float a = Clamp01(obj.Get("a").As<Napi::Number>().FloatValue());
|
|
248
|
+
pt.color = { b, g, r, a };
|
|
249
|
+
points.push_back(pt);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
void ParseLines(const Napi::Value& value) {
|
|
254
|
+
if (!value.IsArray()) return;
|
|
255
|
+
Napi::Array arr = value.As<Napi::Array>();
|
|
256
|
+
lines.reserve(arr.Length());
|
|
257
|
+
for (uint32_t i = 0; i < arr.Length(); ++i) {
|
|
258
|
+
Napi::Value entry = arr.Get(i);
|
|
259
|
+
if (!entry.IsObject()) continue;
|
|
260
|
+
Napi::Object obj = entry.As<Napi::Object>();
|
|
261
|
+
|
|
262
|
+
if (!obj.Has("x1") || !obj.Has("y1") || !obj.Has("x2") || !obj.Has("y2") ||
|
|
263
|
+
!obj.Has("thickness") ||
|
|
264
|
+
!obj.Has("r") || !obj.Has("g") || !obj.Has("b") || !obj.Has("a")) {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
DrawLine line;
|
|
269
|
+
line.x1 = Clamp01(obj.Get("x1").As<Napi::Number>().FloatValue());
|
|
270
|
+
line.y1 = Clamp01(obj.Get("y1").As<Napi::Number>().FloatValue());
|
|
271
|
+
line.x2 = Clamp01(obj.Get("x2").As<Napi::Number>().FloatValue());
|
|
272
|
+
line.y2 = Clamp01(obj.Get("y2").As<Napi::Number>().FloatValue());
|
|
273
|
+
line.thickness = Clamp01(obj.Get("thickness").As<Napi::Number>().FloatValue());
|
|
274
|
+
const float r = Clamp01(obj.Get("r").As<Napi::Number>().FloatValue() / 255.0f);
|
|
275
|
+
const float g = Clamp01(obj.Get("g").As<Napi::Number>().FloatValue() / 255.0f);
|
|
276
|
+
const float b = Clamp01(obj.Get("b").As<Napi::Number>().FloatValue() / 255.0f);
|
|
277
|
+
const float a = Clamp01(obj.Get("a").As<Napi::Number>().FloatValue());
|
|
278
|
+
line.color = { b, g, r, a };
|
|
279
|
+
lines.push_back(line);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// ULTRA-FAST: ROI-based rendering with minimal mask operations
|
|
284
|
+
void ApplyPointUltraFast(cv::Mat& canvas,
|
|
285
|
+
cv::Mat& maskBuffer,
|
|
286
|
+
const DrawPoint& point,
|
|
287
|
+
int width,
|
|
288
|
+
int height,
|
|
289
|
+
float minDimension) {
|
|
290
|
+
if (point.radius <= 0.0f || point.color[3] < 1e-6f) {
|
|
291
|
+
return; // Skip invisible points
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const int radiusPx = std::max(1, static_cast<int>(std::round(point.radius * minDimension)));
|
|
295
|
+
const int px = ClampPixel(static_cast<int>(std::round(point.x * (width - 1))), width);
|
|
296
|
+
const int py = ClampPixel(static_cast<int>(std::round(point.y * (height - 1))), height);
|
|
297
|
+
|
|
298
|
+
// Calculate bounding box for ROI optimization
|
|
299
|
+
const int x1 = std::max(0, px - radiusPx);
|
|
300
|
+
const int y1 = std::max(0, py - radiusPx);
|
|
301
|
+
const int x2 = std::min(width, px + radiusPx + 1);
|
|
302
|
+
const int y2 = std::min(height, py + radiusPx + 1);
|
|
303
|
+
|
|
304
|
+
if (x1 >= x2 || y1 >= y2) return; // Empty ROI
|
|
305
|
+
|
|
306
|
+
const cv::Rect roi(x1, y1, x2 - x1, y2 - y1);
|
|
307
|
+
|
|
308
|
+
// ULTRA-OPTIMIZATION: Zero only the ROI instead of full mask
|
|
309
|
+
cv::Mat maskROI = maskBuffer(roi);
|
|
310
|
+
maskROI.setTo(0);
|
|
311
|
+
|
|
312
|
+
// Draw circle on ROI with adjusted center
|
|
313
|
+
cv::circle(maskBuffer, cv::Point(px, py), radiusPx, cv::Scalar(point.color[3]), -1, cv::LINE_AA);
|
|
314
|
+
|
|
315
|
+
// Blend only the affected region
|
|
316
|
+
UltraFastBlend(canvas, maskBuffer, point.color, roi);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// ULTRA-FAST: ROI-based line rendering
|
|
320
|
+
void ApplyLineUltraFast(cv::Mat& canvas,
|
|
321
|
+
cv::Mat& maskBuffer,
|
|
322
|
+
const DrawLine& line,
|
|
323
|
+
int width,
|
|
324
|
+
int height,
|
|
325
|
+
float minDimension) {
|
|
326
|
+
if (line.color[3] < 1e-6f) {
|
|
327
|
+
return; // Skip invisible lines
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if ((line.x1 == line.x2) && (line.y1 == line.y2)) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const int thicknessPx = std::max(1, static_cast<int>(std::round(line.thickness * minDimension)));
|
|
335
|
+
const int p1x = ClampPixel(static_cast<int>(std::round(line.x1 * (width - 1))), width);
|
|
336
|
+
const int p1y = ClampPixel(static_cast<int>(std::round(line.y1 * (height - 1))), height);
|
|
337
|
+
const int p2x = ClampPixel(static_cast<int>(std::round(line.x2 * (width - 1))), width);
|
|
338
|
+
const int p2y = ClampPixel(static_cast<int>(std::round(line.y2 * (height - 1))), height);
|
|
339
|
+
|
|
340
|
+
const cv::Point p1(p1x, p1y);
|
|
341
|
+
const cv::Point p2(p2x, p2y);
|
|
342
|
+
|
|
343
|
+
// Calculate bounding box for line ROI
|
|
344
|
+
const int halfThick = thicknessPx / 2 + 1;
|
|
345
|
+
const int x1 = std::max(0, std::min(p1x, p2x) - halfThick);
|
|
346
|
+
const int y1 = std::max(0, std::min(p1y, p2y) - halfThick);
|
|
347
|
+
const int x2 = std::min(width, std::max(p1x, p2x) + halfThick + 1);
|
|
348
|
+
const int y2 = std::min(height, std::max(p1y, p2y) + halfThick + 1);
|
|
349
|
+
|
|
350
|
+
if (x1 >= x2 || y1 >= y2) return; // Empty ROI
|
|
351
|
+
|
|
352
|
+
const cv::Rect roi(x1, y1, x2 - x1, y2 - y1);
|
|
353
|
+
|
|
354
|
+
// ULTRA-OPTIMIZATION: Zero only the ROI
|
|
355
|
+
cv::Mat maskROI = maskBuffer(roi);
|
|
356
|
+
maskROI.setTo(0);
|
|
357
|
+
|
|
358
|
+
cv::line(maskBuffer, p1, p2, cv::Scalar(line.color[3]), thicknessPx, cv::LINE_AA);
|
|
359
|
+
|
|
360
|
+
// Blend only the affected region
|
|
361
|
+
UltraFastBlend(canvas, maskBuffer, line.color, roi);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private:
|
|
365
|
+
cv::Mat baseImage;
|
|
366
|
+
cv::Mat resultImage;
|
|
367
|
+
std::vector<uchar> encodedBuffer;
|
|
368
|
+
|
|
369
|
+
std::vector<DrawPoint> points;
|
|
370
|
+
std::vector<DrawLine> lines;
|
|
371
|
+
|
|
372
|
+
std::string imageChannelOrder;
|
|
373
|
+
std::string outputFormat;
|
|
374
|
+
int quality;
|
|
375
|
+
bool pngOptimize;
|
|
376
|
+
|
|
377
|
+
double convertMs {0.0};
|
|
378
|
+
double taskMs {0.0};
|
|
379
|
+
double encodeMs {0.0};
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
Napi::Value Draw(const Napi::CallbackInfo& info) {
|
|
383
|
+
Napi::Env env = info.Env();
|
|
384
|
+
const size_t length = info.Length();
|
|
385
|
+
if (length < 4 || length > 7 || !info[length - 1].IsFunction()) {
|
|
386
|
+
Napi::TypeError::New(env,
|
|
387
|
+
"draw(image, points, lines, [outputFormat], [quality], [pngOptimize], callback)")
|
|
388
|
+
.ThrowAsJavaScriptException();
|
|
389
|
+
return env.Null();
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
size_t idx = 0;
|
|
393
|
+
Napi::Value imageVal = info[idx++];
|
|
394
|
+
Napi::Value pointsVal = info[idx++];
|
|
395
|
+
Napi::Value linesVal = info[idx++];
|
|
396
|
+
|
|
397
|
+
std::string outputFormat = "raw";
|
|
398
|
+
int quality = 90;
|
|
399
|
+
bool pngOptimize = false;
|
|
400
|
+
|
|
401
|
+
if (idx < length - 1) {
|
|
402
|
+
outputFormat = info[idx++].As<Napi::String>().Utf8Value();
|
|
403
|
+
}
|
|
404
|
+
if (idx < length - 1) {
|
|
405
|
+
quality = info[idx++].As<Napi::Number>().Int32Value();
|
|
406
|
+
}
|
|
407
|
+
if (idx < length - 1) {
|
|
408
|
+
pngOptimize = info[idx++].As<Napi::Boolean>().Value();
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
Napi::Function cb = info[length - 1].As<Napi::Function>();
|
|
412
|
+
(new DrawWorker(cb, imageVal, pointsVal, linesVal, outputFormat, quality, pngOptimize))->Queue();
|
|
413
|
+
return env.Undefined();
|
|
414
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// ───────── src/filter.cpp (optimized kernel filtering) ─────────
|
|
2
|
+
#include <napi.h>
|
|
3
|
+
#include <opencv2/opencv.hpp>
|
|
4
|
+
#include "utils.h"
|
|
5
|
+
#include "filters/kernels.h"
|
|
6
|
+
#include <unordered_map>
|
|
7
|
+
#include <string>
|
|
8
|
+
|
|
9
|
+
/*------------------------------------------------------------------------*/
|
|
10
|
+
class FilterWorker final : public Napi::AsyncWorker {
|
|
11
|
+
public:
|
|
12
|
+
FilterWorker(Napi::Function cb,
|
|
13
|
+
const Napi::Value& imgVal,
|
|
14
|
+
const std::string& filterType,
|
|
15
|
+
int kernelSize,
|
|
16
|
+
double intensity,
|
|
17
|
+
std::string outputFormat,
|
|
18
|
+
int quality = 90,
|
|
19
|
+
bool pngOptimize = false)
|
|
20
|
+
: Napi::AsyncWorker(cb),
|
|
21
|
+
filterType_(filterType),
|
|
22
|
+
kernelSize_(kernelSize),
|
|
23
|
+
intensity_(intensity),
|
|
24
|
+
outputFormat_(std::move(outputFormat)),
|
|
25
|
+
quality_(quality),
|
|
26
|
+
pngOptimize_(pngOptimize)
|
|
27
|
+
{
|
|
28
|
+
// Convert input image and measure timing
|
|
29
|
+
const int64 t0 = cv::getTickCount();
|
|
30
|
+
input_ = ConvertToMat(imgVal);
|
|
31
|
+
convertMs_ = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
32
|
+
|
|
33
|
+
// Determine channel format
|
|
34
|
+
if (imgVal.IsObject() && !imgVal.IsBuffer()) {
|
|
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) ? "BGRA"
|
|
44
|
+
: (input_.channels() == 3) ? "BGR"
|
|
45
|
+
: "GRAY";
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
// Buffer input - determine from OpenCV Mat
|
|
49
|
+
channel_ = (input_.channels() == 4) ? "BGRA"
|
|
50
|
+
: (input_.channels() == 3) ? "BGR"
|
|
51
|
+
: "GRAY";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
protected:
|
|
56
|
+
void Execute() override {
|
|
57
|
+
const int64 t0 = cv::getTickCount();
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
// Apply filter based on type
|
|
61
|
+
if (filterType_ == "blur") {
|
|
62
|
+
ApplyBlurFilter();
|
|
63
|
+
} else if (filterType_ == "sharpen") {
|
|
64
|
+
ApplySharpenFilter();
|
|
65
|
+
} else if (filterType_ == "edge") {
|
|
66
|
+
ApplyEdgeFilter();
|
|
67
|
+
} else if (filterType_ == "emboss") {
|
|
68
|
+
ApplyEmbossFilter();
|
|
69
|
+
} else if (filterType_ == "gaussian") {
|
|
70
|
+
ApplyGaussianFilter();
|
|
71
|
+
} else {
|
|
72
|
+
throw std::runtime_error("Unknown filter type: " + filterType_);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
taskMs_ = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
76
|
+
|
|
77
|
+
// Multi-format encoding
|
|
78
|
+
if (outputFormat_ != "raw") {
|
|
79
|
+
const cv::Mat& srcForEncoding = (channel_ == "BGR") ? result_
|
|
80
|
+
: ToBgrForJpg(result_, channel_);
|
|
81
|
+
encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat_, quality_, pngOptimize_);
|
|
82
|
+
}
|
|
83
|
+
} catch (const std::exception& e) {
|
|
84
|
+
SetError(e.what());
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
void OnOK() override {
|
|
89
|
+
Napi::Env env = Env();
|
|
90
|
+
Napi::Value jsImg = (outputFormat_ != "raw")
|
|
91
|
+
? VectorToBuffer(env, std::move(encodedBuf_))
|
|
92
|
+
: MatToRawJS(env, result_, channel_);
|
|
93
|
+
|
|
94
|
+
Napi::Object out = Napi::Object::New(env);
|
|
95
|
+
out.Set("image", jsImg);
|
|
96
|
+
out.Set("timing", MakeTimingJS(env, convertMs_, taskMs_, encodeMs_));
|
|
97
|
+
Callback().Call({ env.Null(), out });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
void OnError(const Napi::Error& e) override {
|
|
101
|
+
Callback().Call({ e.Value(), Env().Null() });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private:
|
|
105
|
+
cv::Mat input_, result_;
|
|
106
|
+
std::string filterType_;
|
|
107
|
+
int kernelSize_;
|
|
108
|
+
double intensity_;
|
|
109
|
+
std::string outputFormat_;
|
|
110
|
+
int quality_;
|
|
111
|
+
bool pngOptimize_;
|
|
112
|
+
std::string channel_;
|
|
113
|
+
|
|
114
|
+
double convertMs_{0.0}, taskMs_{0.0}, encodeMs_{0.0};
|
|
115
|
+
std::vector<uchar> encodedBuf_;
|
|
116
|
+
|
|
117
|
+
void ApplyBlurFilter() {
|
|
118
|
+
// Box blur using OpenCV's optimized function
|
|
119
|
+
cv::Size ksize(kernelSize_, kernelSize_);
|
|
120
|
+
cv::boxFilter(input_, result_, -1, ksize);
|
|
121
|
+
|
|
122
|
+
// Apply intensity (blend with original)
|
|
123
|
+
if (intensity_ < 1.0) {
|
|
124
|
+
cv::addWeighted(input_, 1.0 - intensity_, result_, intensity_, 0, result_);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
void ApplySharpenFilter() {
|
|
129
|
+
// Create sharpening kernel
|
|
130
|
+
cv::Mat kernel = CreateSharpenKernel(kernelSize_, intensity_);
|
|
131
|
+
cv::filter2D(input_, result_, -1, kernel);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
void ApplyEdgeFilter() {
|
|
135
|
+
// Sobel edge detection
|
|
136
|
+
cv::Mat grad_x, grad_y;
|
|
137
|
+
cv::Mat gray;
|
|
138
|
+
|
|
139
|
+
// Convert to grayscale for edge detection
|
|
140
|
+
if (input_.channels() > 1) {
|
|
141
|
+
cv::cvtColor(input_, gray, cv::COLOR_BGR2GRAY);
|
|
142
|
+
} else {
|
|
143
|
+
gray = input_;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Apply Sobel
|
|
147
|
+
cv::Sobel(gray, grad_x, CV_16S, 1, 0, kernelSize_);
|
|
148
|
+
cv::Sobel(gray, grad_y, CV_16S, 0, 1, kernelSize_);
|
|
149
|
+
|
|
150
|
+
// Calculate gradient magnitude
|
|
151
|
+
cv::Mat abs_grad_x, abs_grad_y;
|
|
152
|
+
cv::convertScaleAbs(grad_x, abs_grad_x);
|
|
153
|
+
cv::convertScaleAbs(grad_y, abs_grad_y);
|
|
154
|
+
|
|
155
|
+
cv::addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, result_);
|
|
156
|
+
|
|
157
|
+
// Apply intensity
|
|
158
|
+
cv::convertScaleAbs(result_, result_, intensity_);
|
|
159
|
+
|
|
160
|
+
// Convert back to original channels if needed
|
|
161
|
+
if (input_.channels() > 1) {
|
|
162
|
+
cv::cvtColor(result_, result_, cv::COLOR_GRAY2BGR);
|
|
163
|
+
if (input_.channels() == 4) {
|
|
164
|
+
cv::cvtColor(result_, result_, cv::COLOR_BGR2BGRA);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
void ApplyEmbossFilter() {
|
|
170
|
+
// Create emboss kernel
|
|
171
|
+
cv::Mat kernel = CreateEmbossKernel(intensity_);
|
|
172
|
+
cv::filter2D(input_, result_, -1, kernel);
|
|
173
|
+
|
|
174
|
+
// Add 128 to bring to mid-gray range for emboss effect
|
|
175
|
+
result_ += cv::Scalar::all(128);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
void ApplyGaussianFilter() {
|
|
179
|
+
// Gaussian blur with OpenCV's optimized function
|
|
180
|
+
cv::Size ksize(kernelSize_, kernelSize_);
|
|
181
|
+
double sigma = kernelSize_ / 6.0 * intensity_; // Scale sigma with intensity
|
|
182
|
+
cv::GaussianBlur(input_, result_, ksize, sigma);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/*──────── binding: filter(image, filterType, kernelSize, intensity, [outputFormat], [quality], [pngOptimize], callback) ─*/
|
|
187
|
+
Napi::Value Filter(const Napi::CallbackInfo& info) {
|
|
188
|
+
Napi::Env env = info.Env();
|
|
189
|
+
|
|
190
|
+
// Validate arguments
|
|
191
|
+
if (info.Length() < 5 || info.Length() > 8 || !info[info.Length() - 1].IsFunction()) {
|
|
192
|
+
return Napi::TypeError::New(env,
|
|
193
|
+
"filter(image, filterType, kernelSize, intensity, [outputFormat], [quality], [pngOptimize], callback)").Value();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
int i = 0;
|
|
197
|
+
Napi::Value img = info[i++];
|
|
198
|
+
std::string filterType = info[i++].As<Napi::String>();
|
|
199
|
+
int kernelSize = info[i++].As<Napi::Number>().Int32Value();
|
|
200
|
+
double intensity = info[i++].As<Napi::Number>().DoubleValue();
|
|
201
|
+
|
|
202
|
+
// Handle parameters
|
|
203
|
+
std::string outputFormat = "raw";
|
|
204
|
+
int quality = 90;
|
|
205
|
+
bool pngOptimize = false;
|
|
206
|
+
|
|
207
|
+
if (info.Length() - i >= 2) {
|
|
208
|
+
outputFormat = info[i++].As<Napi::String>().Utf8Value();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (info.Length() - i >= 2) {
|
|
212
|
+
quality = info[i++].As<Napi::Number>().Int32Value();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (info.Length() - i >= 2) {
|
|
216
|
+
pngOptimize = info[i++].As<Napi::Boolean>().Value();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
Napi::Function cb = info[i].As<Napi::Function>();
|
|
220
|
+
|
|
221
|
+
// Validate kernel size (must be odd)
|
|
222
|
+
if (kernelSize % 2 == 0) {
|
|
223
|
+
kernelSize++;
|
|
224
|
+
}
|
|
225
|
+
kernelSize = std::max(3, std::min(kernelSize, 15)); // Clamp to reasonable range
|
|
226
|
+
|
|
227
|
+
// Create and queue worker
|
|
228
|
+
(new FilterWorker(cb, img, filterType, kernelSize, intensity, outputFormat, quality, pngOptimize))->Queue();
|
|
229
|
+
return env.Undefined();
|
|
230
|
+
}
|