@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,240 @@
|
|
|
1
|
+
#include <napi.h>
|
|
2
|
+
#include <opencv2/opencv.hpp>
|
|
3
|
+
#include "utils.h"
|
|
4
|
+
|
|
5
|
+
// Helper function to create binary mask from polygon coordinates
|
|
6
|
+
cv::Mat CreatePolygonMask(const std::vector<cv::Point2f>& polygon, cv::Size imageSize) {
|
|
7
|
+
// Create empty binary mask (grayscale, black background)
|
|
8
|
+
cv::Mat mask = cv::Mat::zeros(imageSize, CV_8UC1);
|
|
9
|
+
|
|
10
|
+
// Convert normalized coordinates to pixel coordinates
|
|
11
|
+
std::vector<cv::Point> pixelPolygon;
|
|
12
|
+
pixelPolygon.reserve(polygon.size());
|
|
13
|
+
|
|
14
|
+
for (const auto& point : polygon) {
|
|
15
|
+
int x = static_cast<int>(point.x * imageSize.width);
|
|
16
|
+
int y = static_cast<int>(point.y * imageSize.height);
|
|
17
|
+
|
|
18
|
+
// Clamp to image bounds
|
|
19
|
+
x = std::max(0, std::min(imageSize.width - 1, x));
|
|
20
|
+
y = std::max(0, std::min(imageSize.height - 1, y));
|
|
21
|
+
|
|
22
|
+
pixelPolygon.emplace_back(x, y);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Fill polygon with white (255) on binary mask
|
|
26
|
+
const cv::Point* pts = pixelPolygon.data();
|
|
27
|
+
int npts = static_cast<int>(pixelPolygon.size());
|
|
28
|
+
cv::fillPoly(mask, &pts, &npts, 1, cv::Scalar(255)); // White fill for binary mask
|
|
29
|
+
|
|
30
|
+
return mask;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
/*------------------------------------------------------------------------*/
|
|
35
|
+
class AddMaskWorker final : public Napi::AsyncWorker {
|
|
36
|
+
public:
|
|
37
|
+
AddMaskWorker(Napi::Function cb,
|
|
38
|
+
const Napi::Value& jsImg,
|
|
39
|
+
const Napi::Array& polygonArray,
|
|
40
|
+
double maskStrength,
|
|
41
|
+
int fillR, int fillG, int fillB,
|
|
42
|
+
std::string outputFormat,
|
|
43
|
+
int quality = 90,
|
|
44
|
+
bool pngOptimize = false)
|
|
45
|
+
: Napi::AsyncWorker(cb),
|
|
46
|
+
maskStrength(maskStrength),
|
|
47
|
+
fillR(fillR), fillG(fillG), fillB(fillB),
|
|
48
|
+
outputFormat(std::move(outputFormat)),
|
|
49
|
+
quality(quality), pngOptimize(pngOptimize)
|
|
50
|
+
{
|
|
51
|
+
// Timing and conversion
|
|
52
|
+
const int64 t0 = cv::getTickCount();
|
|
53
|
+
|
|
54
|
+
// Convert JavaScript image to OpenCV Mat
|
|
55
|
+
imageMat = ConvertToMat(jsImg);
|
|
56
|
+
|
|
57
|
+
// Parse polygon coordinates from JavaScript array
|
|
58
|
+
polygon.reserve(polygonArray.Length());
|
|
59
|
+
for (size_t i = 0; i < polygonArray.Length(); i++) {
|
|
60
|
+
Napi::Array point = polygonArray.Get(i).As<Napi::Array>();
|
|
61
|
+
if (point.Length() >= 2) {
|
|
62
|
+
float x = point.Get(0u).As<Napi::Number>().FloatValue();
|
|
63
|
+
float y = point.Get(1u).As<Napi::Number>().FloatValue();
|
|
64
|
+
polygon.emplace_back(x, y);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
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
|
+
}
|
|
76
|
+
|
|
77
|
+
protected:
|
|
78
|
+
void Execute() override {
|
|
79
|
+
const int64 t0 = cv::getTickCount();
|
|
80
|
+
|
|
81
|
+
// Convert image to target format
|
|
82
|
+
cv::Mat img = ConvertToTargetFormatShared(imageMat, imageFormat, outputChannel);
|
|
83
|
+
|
|
84
|
+
// Generate binary mask from polygon coordinates
|
|
85
|
+
cv::Mat binaryMask = CreatePolygonMask(polygon, img.size());
|
|
86
|
+
|
|
87
|
+
// Convert image and mask to float for precise blending
|
|
88
|
+
cv::Mat imgFloat;
|
|
89
|
+
img.convertTo(imgFloat, CV_32F, 1.0/255.0);
|
|
90
|
+
|
|
91
|
+
// Convert binary mask to float weights (0.0 to 1.0)
|
|
92
|
+
cv::Mat maskWeights;
|
|
93
|
+
binaryMask.convertTo(maskWeights, CV_32F, 1.0/255.0);
|
|
94
|
+
|
|
95
|
+
// Apply mask strength to weights
|
|
96
|
+
maskWeights *= maskStrength;
|
|
97
|
+
|
|
98
|
+
// Create solid color image with fill color
|
|
99
|
+
cv::Mat fillColorImg;
|
|
100
|
+
if (imgFloat.channels() == 1) {
|
|
101
|
+
// Grayscale: use average of RGB values
|
|
102
|
+
float grayValue = (fillR + fillG + fillB) / (3.0f * 255.0f);
|
|
103
|
+
fillColorImg = cv::Mat::ones(img.size(), CV_32F) * grayValue;
|
|
104
|
+
} else if (imgFloat.channels() == 3) {
|
|
105
|
+
// Color image - respect the color space format
|
|
106
|
+
fillColorImg = cv::Mat::ones(img.size(), CV_32FC3);
|
|
107
|
+
if (outputChannel == "RGB" || outputChannel == "RGBA") {
|
|
108
|
+
// RGB format: R, G, B order
|
|
109
|
+
fillColorImg.setTo(cv::Scalar(fillR/255.0f, fillG/255.0f, fillB/255.0f));
|
|
110
|
+
} else {
|
|
111
|
+
// BGR format: B, G, R order (OpenCV default)
|
|
112
|
+
fillColorImg.setTo(cv::Scalar(fillB/255.0f, fillG/255.0f, fillR/255.0f));
|
|
113
|
+
}
|
|
114
|
+
} else if (imgFloat.channels() == 4) {
|
|
115
|
+
// Color image with alpha
|
|
116
|
+
fillColorImg = cv::Mat::ones(img.size(), CV_32FC4);
|
|
117
|
+
if (outputChannel == "RGBA") {
|
|
118
|
+
// RGBA format: R, G, B, A order
|
|
119
|
+
fillColorImg.setTo(cv::Scalar(fillR/255.0f, fillG/255.0f, fillB/255.0f, 1.0f));
|
|
120
|
+
} else {
|
|
121
|
+
// BGRA format: B, G, R, A order (OpenCV default)
|
|
122
|
+
fillColorImg.setTo(cv::Scalar(fillB/255.0f, fillG/255.0f, fillR/255.0f, 1.0f));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Replicate single channel weights to match image channels
|
|
127
|
+
cv::Mat blendWeights;
|
|
128
|
+
if (imgFloat.channels() > 1) {
|
|
129
|
+
std::vector<cv::Mat> channels(imgFloat.channels());
|
|
130
|
+
for (int i = 0; i < imgFloat.channels(); i++) {
|
|
131
|
+
channels[i] = maskWeights;
|
|
132
|
+
}
|
|
133
|
+
cv::merge(channels, blendWeights);
|
|
134
|
+
} else {
|
|
135
|
+
blendWeights = maskWeights;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Weighted blending: result = image * (1 - blend_weights) + fill_color * blend_weights
|
|
139
|
+
cv::Mat resultFloat;
|
|
140
|
+
cv::Mat inverseWeights;
|
|
141
|
+
cv::subtract(cv::Scalar::all(1.0), blendWeights, inverseWeights);
|
|
142
|
+
|
|
143
|
+
// Element-wise multiplication and addition
|
|
144
|
+
cv::Mat term1, term2;
|
|
145
|
+
cv::multiply(imgFloat, inverseWeights, term1);
|
|
146
|
+
cv::multiply(fillColorImg, blendWeights, term2);
|
|
147
|
+
cv::add(term1, term2, resultFloat);
|
|
148
|
+
|
|
149
|
+
// Convert back to 8-bit
|
|
150
|
+
resultFloat.convertTo(result, CV_8U, 255.0);
|
|
151
|
+
|
|
152
|
+
taskMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
153
|
+
|
|
154
|
+
// Multi-format encoding if needed
|
|
155
|
+
if (outputFormat != "raw") {
|
|
156
|
+
cv::Mat tmp = ToBgrForJpg(result, outputChannel);
|
|
157
|
+
encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
void OnOK() override {
|
|
162
|
+
Napi::Env env = Env();
|
|
163
|
+
Napi::Value jsImg = (outputFormat != "raw") ? VectorToBuffer(env, std::move(encodedBuf))
|
|
164
|
+
: MatToRawJS(env, result, outputChannel);
|
|
165
|
+
|
|
166
|
+
Napi::Object out = Napi::Object::New(env);
|
|
167
|
+
out.Set("image", jsImg);
|
|
168
|
+
out.Set("timing", MakeTimingJS(env, convertMs, taskMs, encodeMs));
|
|
169
|
+
Callback().Call({env.Null(), out});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private:
|
|
173
|
+
cv::Mat imageMat, result;
|
|
174
|
+
std::string imageFormat, outputChannel;
|
|
175
|
+
std::vector<cv::Point2f> polygon;
|
|
176
|
+
double maskStrength;
|
|
177
|
+
int fillR, fillG, fillB;
|
|
178
|
+
std::string outputFormat;
|
|
179
|
+
int quality;
|
|
180
|
+
bool pngOptimize;
|
|
181
|
+
double convertMs = 0, taskMs = 0, encodeMs = 0;
|
|
182
|
+
std::vector<uchar> encodedBuf;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/*------------------------------------------------------------------------*/
|
|
186
|
+
Napi::Value AddMask(const Napi::CallbackInfo& info) {
|
|
187
|
+
Napi::Env env = info.Env();
|
|
188
|
+
|
|
189
|
+
// Fast parameter validation: image, polygon, maskStrength, fillR, fillG, fillB, [outputFormat], [quality], [pngOptimize], callback
|
|
190
|
+
if (info.Length() < 7 || info.Length() > 10 || !info[info.Length() - 1].IsFunction()) {
|
|
191
|
+
Napi::TypeError::New(env, "addMask(image, polygon, maskStrength, fillR, fillG, fillB, [outputFormat], [quality], [pngOptimize], callback)")
|
|
192
|
+
.ThrowAsJavaScriptException();
|
|
193
|
+
return env.Null();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Extract required parameters
|
|
197
|
+
Napi::Value jsImg = info[0];
|
|
198
|
+
Napi::Array polygonArray = info[1].As<Napi::Array>();
|
|
199
|
+
double maskStrength = info[2].As<Napi::Number>().DoubleValue();
|
|
200
|
+
int fillR = info[3].As<Napi::Number>().Int32Value();
|
|
201
|
+
int fillG = info[4].As<Napi::Number>().Int32Value();
|
|
202
|
+
int fillB = info[5].As<Napi::Number>().Int32Value();
|
|
203
|
+
|
|
204
|
+
// Clamp values to valid ranges
|
|
205
|
+
maskStrength = std::max(0.0, std::min(1.0, maskStrength));
|
|
206
|
+
fillR = std::max(0, std::min(255, fillR));
|
|
207
|
+
fillG = std::max(0, std::min(255, fillG));
|
|
208
|
+
fillB = std::max(0, std::min(255, fillB));
|
|
209
|
+
|
|
210
|
+
// Handle optional parameters
|
|
211
|
+
std::string outputFormat = "raw";
|
|
212
|
+
int quality = 90;
|
|
213
|
+
bool pngOptimize = false;
|
|
214
|
+
size_t cbIdx = 6;
|
|
215
|
+
|
|
216
|
+
if (info.Length() >= 8) {
|
|
217
|
+
outputFormat = info[6].As<Napi::String>().Utf8Value();
|
|
218
|
+
cbIdx = 7;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (info.Length() >= 9) {
|
|
222
|
+
quality = info[7].As<Napi::Number>().Int32Value();
|
|
223
|
+
cbIdx = 8;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (info.Length() == 10) {
|
|
227
|
+
pngOptimize = info[8].As<Napi::Boolean>().Value();
|
|
228
|
+
cbIdx = 9;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Create and queue worker
|
|
232
|
+
(new AddMaskWorker(
|
|
233
|
+
info[cbIdx].As<Napi::Function>(),
|
|
234
|
+
jsImg, polygonArray, maskStrength,
|
|
235
|
+
fillR, fillG, fillB,
|
|
236
|
+
outputFormat, quality, pngOptimize
|
|
237
|
+
))->Queue();
|
|
238
|
+
|
|
239
|
+
return env.Undefined();
|
|
240
|
+
}
|