@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.
Files changed (131) hide show
  1. package/.gitattributes +6 -0
  2. package/.github/workflows/build-prebuild.yml +326 -0
  3. package/LICENSE +13 -0
  4. package/README.md +161 -0
  5. package/artifacts/darwin-arm64/package.json +25 -0
  6. package/artifacts/linux-arm64/package.json +26 -0
  7. package/artifacts/linux-x64/package.json +26 -0
  8. package/artifacts/linuxmusl-x64/package.json +26 -0
  9. package/assets/flow_time_example.gif +0 -0
  10. package/assets/nodes/io/array-in-demo.gif +0 -0
  11. package/assets/nodes/io/array-out-demo.gif +0 -0
  12. package/assets/nodes/io/array-select-demo.gif +0 -0
  13. package/assets/nodes/io/image-in-demo.gif +0 -0
  14. package/assets/nodes/mix/concat-demo.gif +0 -0
  15. package/assets/nodes/mix/mosaic-demo.gif +0 -0
  16. package/assets/nodes/transform/crop-demo.gif +0 -0
  17. package/assets/nodes/transform/filter-demo.gif +0 -0
  18. package/assets/nodes/transform/padding-demo.gif +0 -0
  19. package/assets/nodes/transform/resize-demo.gif +0 -0
  20. package/assets/nodes/transform/rotate-demo.gif +0 -0
  21. package/docs/nodes/blend/add-mask.md +275 -0
  22. package/docs/nodes/blend/add-masks.md +284 -0
  23. package/docs/nodes/blend/blend.md +248 -0
  24. package/docs/nodes/io/array-in.md +119 -0
  25. package/docs/nodes/io/array-out.md +150 -0
  26. package/docs/nodes/io/array-select.md +157 -0
  27. package/docs/nodes/io/image-in.md +131 -0
  28. package/docs/nodes/io/image-out.md +179 -0
  29. package/docs/nodes/io/queue.md +57 -0
  30. package/docs/nodes/io/save-file.md +30 -0
  31. package/docs/nodes/mix/advanced-mosaic.md +108 -0
  32. package/docs/nodes/mix/concat.md +127 -0
  33. package/docs/nodes/mix/mosaic.md +146 -0
  34. package/docs/nodes/specialized/cropBB.md +271 -0
  35. package/docs/nodes/specialized/image-align.md +426 -0
  36. package/docs/nodes/transform/crop.md +132 -0
  37. package/docs/nodes/transform/draw.md +77 -0
  38. package/docs/nodes/transform/filter.md +84 -0
  39. package/docs/nodes/transform/padding.md +133 -0
  40. package/docs/nodes/transform/resize.md +158 -0
  41. package/docs/nodes/transform/rotate.md +121 -0
  42. package/docs/nodes/util/block-detect.md +46 -0
  43. package/install.sh +144 -0
  44. package/node-red-contrib-image-tools/lib/cpp-bridge.js +224 -0
  45. package/node-red-contrib-image-tools/lib/node-utils.js +529 -0
  46. package/node-red-contrib-image-tools/nodes/blend/add-bbs.html +590 -0
  47. package/node-red-contrib-image-tools/nodes/blend/add-bbs.js +241 -0
  48. package/node-red-contrib-image-tools/nodes/blend/add-mask.html +449 -0
  49. package/node-red-contrib-image-tools/nodes/blend/add-mask.js +170 -0
  50. package/node-red-contrib-image-tools/nodes/blend/add-masks.html +703 -0
  51. package/node-red-contrib-image-tools/nodes/blend/add-masks.js +321 -0
  52. package/node-red-contrib-image-tools/nodes/blend/blend.html +552 -0
  53. package/node-red-contrib-image-tools/nodes/blend/blend.js +124 -0
  54. package/node-red-contrib-image-tools/nodes/io/array-in.html +110 -0
  55. package/node-red-contrib-image-tools/nodes/io/array-in.js +113 -0
  56. package/node-red-contrib-image-tools/nodes/io/array-out.html +125 -0
  57. package/node-red-contrib-image-tools/nodes/io/array-out.js +213 -0
  58. package/node-red-contrib-image-tools/nodes/io/array-select.html +156 -0
  59. package/node-red-contrib-image-tools/nodes/io/array-select.js +187 -0
  60. package/node-red-contrib-image-tools/nodes/io/folder-in.html +248 -0
  61. package/node-red-contrib-image-tools/nodes/io/folder-in.js +335 -0
  62. package/node-red-contrib-image-tools/nodes/io/image-in.html +146 -0
  63. package/node-red-contrib-image-tools/nodes/io/image-in.js +143 -0
  64. package/node-red-contrib-image-tools/nodes/io/image-out.html +506 -0
  65. package/node-red-contrib-image-tools/nodes/io/image-out.js +545 -0
  66. package/node-red-contrib-image-tools/nodes/io/queue.html +119 -0
  67. package/node-red-contrib-image-tools/nodes/io/queue.js +191 -0
  68. package/node-red-contrib-image-tools/nodes/io/save-file.html +243 -0
  69. package/node-red-contrib-image-tools/nodes/io/save-file.js +440 -0
  70. package/node-red-contrib-image-tools/nodes/mix/advanced-mosaic.html +2149 -0
  71. package/node-red-contrib-image-tools/nodes/mix/advanced-mosaic.js +195 -0
  72. package/node-red-contrib-image-tools/nodes/mix/concat.html +390 -0
  73. package/node-red-contrib-image-tools/nodes/mix/concat.js +112 -0
  74. package/node-red-contrib-image-tools/nodes/mix/mosaic.html +550 -0
  75. package/node-red-contrib-image-tools/nodes/mix/mosaic.js +151 -0
  76. package/node-red-contrib-image-tools/nodes/specialized/cropBB.html +268 -0
  77. package/node-red-contrib-image-tools/nodes/specialized/cropBB.js +277 -0
  78. package/node-red-contrib-image-tools/nodes/specialized/image-align.html +528 -0
  79. package/node-red-contrib-image-tools/nodes/specialized/image-align.js +306 -0
  80. package/node-red-contrib-image-tools/nodes/transform/crop.html +410 -0
  81. package/node-red-contrib-image-tools/nodes/transform/crop.js +149 -0
  82. package/node-red-contrib-image-tools/nodes/transform/draw.html +719 -0
  83. package/node-red-contrib-image-tools/nodes/transform/draw.js +509 -0
  84. package/node-red-contrib-image-tools/nodes/transform/filter.html +578 -0
  85. package/node-red-contrib-image-tools/nodes/transform/filter.js +177 -0
  86. package/node-red-contrib-image-tools/nodes/transform/padding.html +475 -0
  87. package/node-red-contrib-image-tools/nodes/transform/padding.js +142 -0
  88. package/node-red-contrib-image-tools/nodes/transform/resize.html +429 -0
  89. package/node-red-contrib-image-tools/nodes/transform/resize.js +162 -0
  90. package/node-red-contrib-image-tools/nodes/transform/rotate.html +299 -0
  91. package/node-red-contrib-image-tools/nodes/transform/rotate.js +141 -0
  92. package/node-red-contrib-image-tools/nodes/util/block-detect.html +85 -0
  93. package/node-red-contrib-image-tools/nodes/util/block-detect.js +136 -0
  94. package/node-red-contrib-image-tools/nodes/util/clean-debug.html +201 -0
  95. package/node-red-contrib-image-tools/nodes/util/clean-debug.js +393 -0
  96. package/node-red-contrib-image-tools/package.json +64 -0
  97. package/npm/darwin-arm64/lib/.gitkeep +1 -0
  98. package/npm/darwin-arm64/package.json +25 -0
  99. package/npm/darwin-x64/lib/.gitkeep +1 -0
  100. package/npm/darwin-x64/package.json +25 -0
  101. package/npm/linux-arm64/lib/.gitkeep +1 -0
  102. package/npm/linux-arm64/package.json +26 -0
  103. package/npm/linux-x64/lib/.gitkeep +1 -0
  104. package/npm/linux-x64/package.json +26 -0
  105. package/npm/linuxmusl-x64/lib/.gitkeep +1 -0
  106. package/npm/linuxmusl-x64/package.json +26 -0
  107. package/npm/win32-x64/lib/.gitkeep +1 -0
  108. package/npm/win32-x64/package.json +25 -0
  109. package/package.json +74 -0
  110. package/rosepetal-image-engine/binding.gyp +153 -0
  111. package/rosepetal-image-engine/package.json +19 -0
  112. package/rosepetal-image-engine/src/add-bbs.cpp +433 -0
  113. package/rosepetal-image-engine/src/add-mask.cpp +240 -0
  114. package/rosepetal-image-engine/src/add-masks.cpp +638 -0
  115. package/rosepetal-image-engine/src/advanced-mosaic.cpp +740 -0
  116. package/rosepetal-image-engine/src/blend.cpp +200 -0
  117. package/rosepetal-image-engine/src/concat.cpp +329 -0
  118. package/rosepetal-image-engine/src/crop.cpp +139 -0
  119. package/rosepetal-image-engine/src/draw.cpp +414 -0
  120. package/rosepetal-image-engine/src/filter.cpp +230 -0
  121. package/rosepetal-image-engine/src/filters/kernels.h +171 -0
  122. package/rosepetal-image-engine/src/image-align.cpp +551 -0
  123. package/rosepetal-image-engine/src/main.cpp +36 -0
  124. package/rosepetal-image-engine/src/mosaic.cpp +373 -0
  125. package/rosepetal-image-engine/src/padding.cpp +124 -0
  126. package/rosepetal-image-engine/src/resize.cpp +210 -0
  127. package/rosepetal-image-engine/src/rotate.cpp +177 -0
  128. package/rosepetal-image-engine/src/utils.h +563 -0
  129. package/rosepetal-image-engine/test-formats.js +119 -0
  130. package/scripts/build-opencv-static.ps1 +166 -0
  131. package/scripts/build-opencv-static.sh +147 -0
@@ -0,0 +1,373 @@
1
+ // ───────── src/mosaic.cpp ───────────────────────────────────────────────
2
+ // SUPER FAST mosaic node - Ultra-optimized image compositing
3
+ // Zero-copy operations, parallel processing, SIMD acceleration
4
+ #include <napi.h>
5
+ #include <opencv2/opencv.hpp>
6
+ #include <vector>
7
+ #include <algorithm>
8
+ #include <cmath>
9
+ #include "utils.h"
10
+
11
+ // Helper function to determine the best canvas format from multiple input formats
12
+ std::string DetermineBestCanvasFormat(const std::vector<std::string>& channels) {
13
+ if (channels.empty()) return "BGR";
14
+
15
+ bool hasRGBA = false, hasBGRA = false, hasRGB = false, hasBGR = false;
16
+
17
+ for (const auto& ch : channels) {
18
+ if (ch == "RGBA") hasRGBA = true;
19
+ else if (ch == "BGRA") hasBGRA = true;
20
+ else if (ch == "RGB") hasRGB = true;
21
+ else if (ch == "BGR") hasBGR = true;
22
+ }
23
+
24
+ // Priority: RGBA > BGRA > RGB > BGR > GRAY
25
+ if (hasRGBA) return "RGBA";
26
+ if (hasBGRA) return "BGRA";
27
+ if (hasRGB) return "RGB";
28
+ if (hasBGR) return "BGR";
29
+ return "GRAY";
30
+ }
31
+
32
+ /*────────────────────────── ULTRA-FAST MosaicWorker ───────────────────────────────────*/
33
+ class MosaicWorker : public Napi::AsyncWorker {
34
+ public:
35
+ MosaicWorker(Napi::Function cb,
36
+ const Napi::Array& imagesArray,
37
+ int canvasWidth, int canvasHeight,
38
+ const std::string& backgroundColor,
39
+ const Napi::Array& positionsArray,
40
+ bool normalized, std::string outputFormat,
41
+ int quality = 90,
42
+ bool pngOptimize = false)
43
+ : Napi::AsyncWorker(cb),
44
+ canvasWidth_(canvasWidth), canvasHeight_(canvasHeight),
45
+ backgroundColor_(backgroundColor),
46
+ normalized_(normalized), outputFormat_(std::move(outputFormat)),
47
+ quality_(quality), pngOptimize_(pngOptimize)
48
+ {
49
+ /* ─ SUPER FAST image conversion with timing ─ */
50
+ const int64 t0 = cv::getTickCount();
51
+
52
+ // Pre-allocate vectors for maximum performance
53
+ images_.reserve(imagesArray.Length());
54
+ imageChannels_.reserve(imagesArray.Length());
55
+ positions_.reserve(positionsArray.Length());
56
+
57
+ // Convert images with zero-copy operations and extract channel formats
58
+ for (uint32_t i = 0; i < imagesArray.Length(); i++) {
59
+ images_.emplace_back(ConvertToMat(imagesArray[i]));
60
+
61
+ // Extract and store channel format for each image (like other nodes do)
62
+ std::string channel;
63
+ if (imagesArray[i].IsObject() && !imagesArray[i].IsBuffer()) {
64
+ Napi::Object obj = imagesArray[i].As<Napi::Object>();
65
+
66
+ // Check for new colorSpace field first
67
+ if (obj.Has("colorSpace")) {
68
+ channel = obj.Get("colorSpace").As<Napi::String>().Utf8Value();
69
+ }
70
+ // Default based on channel count
71
+ else {
72
+ channel = (images_[i].channels() == 4) ? "BGRA"
73
+ : (images_[i].channels() == 3) ? "BGR"
74
+ : "GRAY";
75
+ }
76
+ } else {
77
+ // Buffer input - determine from OpenCV Mat
78
+ channel = (images_[i].channels() == 4) ? "BGRA"
79
+ : (images_[i].channels() == 3) ? "BGR"
80
+ : "GRAY";
81
+ }
82
+
83
+ imageChannels_.emplace_back(channel);
84
+ }
85
+
86
+ // Parse positions - ultra-fast integer operations
87
+ for (uint32_t i = 0; i < positionsArray.Length(); i++) {
88
+ Napi::Object pos = positionsArray[i].As<Napi::Object>();
89
+ Position p;
90
+ p.arrayIndex = pos.Get("arrayIndex").As<Napi::Number>().Int32Value();
91
+ p.x = pos.Get("x").As<Napi::Number>().DoubleValue();
92
+ p.y = pos.Get("y").As<Napi::Number>().DoubleValue();
93
+ positions_.emplace_back(p);
94
+ }
95
+
96
+ // Determine the best canvas format from all input images (priority: RGBA > BGRA > RGB > BGR > GRAY)
97
+ canvasChannel_ = DetermineBestCanvasFormat(imageChannels_);
98
+
99
+ convertMs_ = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
100
+ }
101
+
102
+ protected:
103
+ void Execute() override {
104
+ /* ─ SUPER FAST mosaic composition ─ */
105
+ const int64 t0 = cv::getTickCount();
106
+
107
+ // Parse background color - optimized hex parsing (returns BGR format)
108
+ cv::Scalar bgColor = ParseColor(backgroundColor_, cv::Scalar(0, 0, 0));
109
+
110
+ // Fix color inversion: ParseColor returns BGR, but we need to handle different canvas formats
111
+ if (canvasChannel_ == "RGB" || canvasChannel_ == "RGBA") {
112
+ // For RGB format canvas, swap B and R channels from BGR to RGB
113
+ std::swap(bgColor[0], bgColor[2]); // Swap Blue and Red
114
+ }
115
+
116
+ // Create canvas with correct format based on output channel format
117
+ int canvasType = CV_8UC3; // Default to 3 channels (BGR)
118
+ if (canvasChannel_ == "BGRA" || canvasChannel_ == "RGBA") {
119
+ canvasType = CV_8UC4;
120
+ bgColor = cv::Scalar(bgColor[0], bgColor[1], bgColor[2], 255); // Add alpha channel
121
+ } else if (canvasChannel_ == "GRAY") {
122
+ canvasType = CV_8UC1;
123
+ bgColor = cv::Scalar((bgColor[0] + bgColor[1] + bgColor[2]) / 3.0); // Convert to grayscale
124
+ }
125
+
126
+ canvas_ = cv::Mat(canvasHeight_, canvasWidth_, canvasType, bgColor);
127
+
128
+ // Process all positions - PARALLEL when beneficial
129
+ const size_t numPositions = positions_.size();
130
+
131
+ if (numPositions > 4) {
132
+ // Parallel processing for multiple positions
133
+ #pragma omp parallel for
134
+ for (int i = 0; i < static_cast<int>(numPositions); i++) {
135
+ PlaceImageFast(positions_[i]);
136
+ }
137
+ } else {
138
+ // Sequential processing for small batches (avoid threading overhead)
139
+ for (const auto& pos : positions_) {
140
+ PlaceImageFast(pos);
141
+ }
142
+ }
143
+
144
+ taskMs_ = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
145
+
146
+ /* ─ SUPER FAST multi-format encoding (optional) ─ */
147
+ if (outputFormat_ != "raw") {
148
+ // Convert to BGR format for encoding if needed (like other nodes do)
149
+ const cv::Mat& srcForEncoding = (canvasChannel_ == "BGR") ? canvas_
150
+ : ToBgrForJpg(canvas_, canvasChannel_);
151
+ encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat_, quality_, pngOptimize_);
152
+ }
153
+ }
154
+
155
+ void OnOK() override {
156
+ Napi::Env env = Env();
157
+
158
+ // Zero-copy output creation with correct channel format
159
+ Napi::Value jsImg = (outputFormat_ != "raw")
160
+ ? VectorToBuffer(env, std::move(encodedBuf_)) // Zero-copy encoded
161
+ : MatToRawJS(env, canvas_.clone(), canvasChannel_); // Contiguous raw with correct channel format
162
+
163
+ Napi::Object result = Napi::Object::New(env);
164
+ result.Set("image", jsImg);
165
+ result.Set("timing", MakeTimingJS(env, convertMs_, taskMs_, encodeMs_));
166
+
167
+ Callback().Call({ env.Null(), result });
168
+ }
169
+
170
+ void OnError(const Napi::Error& e) override {
171
+ Callback().Call({ e.Value(), Env().Null() });
172
+ }
173
+
174
+ private:
175
+ struct Position {
176
+ int arrayIndex;
177
+ double x, y;
178
+ };
179
+
180
+ // ULTRA-FAST image placement with bounds checking
181
+ void PlaceImageFast(const Position& pos) {
182
+ if (pos.arrayIndex < 0 || pos.arrayIndex >= static_cast<int>(images_.size())) {
183
+ return; // Skip invalid indices
184
+ }
185
+
186
+ const cv::Mat& img = images_[pos.arrayIndex];
187
+ if (img.empty()) return;
188
+
189
+ // Calculate position - FAST integer operations
190
+ int x = normalized_ ? static_cast<int>(std::round(pos.x * canvasWidth_))
191
+ : static_cast<int>(std::lround(pos.x));
192
+ int y = normalized_ ? static_cast<int>(std::round(pos.y * canvasHeight_))
193
+ : static_cast<int>(std::lround(pos.y));
194
+
195
+ // FAST bounds checking with early exit
196
+ if (x >= canvasWidth_ || y >= canvasHeight_) return;
197
+ if (x + img.cols <= 0 || y + img.rows <= 0) return;
198
+
199
+ // Calculate intersection rectangle - OPTIMIZED
200
+ int srcX = std::max(0, -x);
201
+ int srcY = std::max(0, -y);
202
+ int dstX = std::max(0, x);
203
+ int dstY = std::max(0, y);
204
+
205
+ int width = std::min(img.cols - srcX, canvasWidth_ - dstX);
206
+ int height = std::min(img.rows - srcY, canvasHeight_ - dstY);
207
+
208
+ if (width <= 0 || height <= 0) return;
209
+
210
+ // ZERO-COPY image placement using OpenCV's optimized copyTo
211
+ cv::Rect srcROI(srcX, srcY, width, height);
212
+ cv::Rect dstROI(dstX, dstY, width, height);
213
+
214
+ // Get the channel format for this specific image
215
+ const std::string& imgChannel = imageChannels_[pos.arrayIndex];
216
+
217
+ // Convert to canvas format based on actual input format (fixes blue color issue)
218
+ cv::Mat imgToPlace;
219
+ cv::Mat srcRegion = img(srcROI);
220
+
221
+ // Convert input image to match canvas format
222
+ if (canvasChannel_ == "GRAY") {
223
+ // Convert to grayscale canvas
224
+ if (imgChannel == "GRAY") {
225
+ imgToPlace = srcRegion;
226
+ } else if (imgChannel == "RGB") {
227
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_RGB2GRAY);
228
+ } else if (imgChannel == "RGBA") {
229
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_RGBA2GRAY);
230
+ } else if (imgChannel == "BGRA") {
231
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_BGRA2GRAY);
232
+ } else {
233
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_BGR2GRAY);
234
+ }
235
+ } else if (canvasChannel_ == "BGR") {
236
+ // Convert to BGR canvas
237
+ if (imgChannel == "GRAY") {
238
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_GRAY2BGR);
239
+ } else if (imgChannel == "RGB") {
240
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_RGB2BGR);
241
+ } else if (imgChannel == "RGBA") {
242
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_RGBA2BGR);
243
+ } else if (imgChannel == "BGRA") {
244
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_BGRA2BGR);
245
+ } else {
246
+ imgToPlace = srcRegion; // Already BGR
247
+ }
248
+ } else if (canvasChannel_ == "RGB") {
249
+ // Convert to RGB canvas
250
+ if (imgChannel == "GRAY") {
251
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_GRAY2RGB);
252
+ } else if (imgChannel == "RGB") {
253
+ imgToPlace = srcRegion; // Already RGB
254
+ } else if (imgChannel == "RGBA") {
255
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_RGBA2RGB);
256
+ } else if (imgChannel == "BGRA") {
257
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_BGRA2RGB);
258
+ } else {
259
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_BGR2RGB);
260
+ }
261
+ } else if (canvasChannel_ == "BGRA") {
262
+ // Convert to BGRA canvas
263
+ if (imgChannel == "GRAY") {
264
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_GRAY2BGRA);
265
+ } else if (imgChannel == "RGB") {
266
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_RGB2BGRA);
267
+ } else if (imgChannel == "RGBA") {
268
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_RGBA2BGRA);
269
+ } else if (imgChannel == "BGRA") {
270
+ imgToPlace = srcRegion; // Already BGRA
271
+ } else {
272
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_BGR2BGRA);
273
+ }
274
+ } else if (canvasChannel_ == "RGBA") {
275
+ // Convert to RGBA canvas
276
+ if (imgChannel == "GRAY") {
277
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_GRAY2RGBA);
278
+ } else if (imgChannel == "RGB") {
279
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_RGB2RGBA);
280
+ } else if (imgChannel == "RGBA") {
281
+ imgToPlace = srcRegion; // Already RGBA
282
+ } else if (imgChannel == "BGRA") {
283
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_BGRA2RGBA);
284
+ } else {
285
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_BGR2RGBA);
286
+ }
287
+ } else {
288
+ // Default to BGR conversion
289
+ if (imgChannel == "GRAY") {
290
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_GRAY2BGR);
291
+ } else if (imgChannel == "RGB") {
292
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_RGB2BGR);
293
+ } else if (imgChannel == "RGBA") {
294
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_RGBA2BGR);
295
+ } else if (imgChannel == "BGRA") {
296
+ cv::cvtColor(srcRegion, imgToPlace, cv::COLOR_BGRA2BGR);
297
+ } else {
298
+ imgToPlace = srcRegion;
299
+ }
300
+ }
301
+
302
+ // FASTEST copy operation - zero-copy when possible
303
+ imgToPlace.copyTo(canvas_(dstROI));
304
+ }
305
+
306
+ // Member variables
307
+ std::vector<cv::Mat> images_;
308
+ std::vector<std::string> imageChannels_; // Channel format for each image
309
+ std::vector<Position> positions_;
310
+ cv::Mat canvas_;
311
+ std::string canvasChannel_; // Output channel format
312
+
313
+ int canvasWidth_, canvasHeight_;
314
+ std::string backgroundColor_;
315
+ bool normalized_;
316
+ std::string outputFormat_;
317
+ int quality_;
318
+ bool pngOptimize_;
319
+
320
+ double convertMs_{0.0}, taskMs_{0.0}, encodeMs_{0.0};
321
+ std::vector<uchar> encodedBuf_;
322
+ };
323
+
324
+ /*──────── BINDING: mosaic(imagesArray, width, height, bgColor, positions, normalized, [outputFormat], [quality], [pngOptimize], cb) ─*/
325
+ Napi::Value Mosaic(const Napi::CallbackInfo& info) {
326
+ Napi::Env env = info.Env();
327
+
328
+ // Ultra-fast parameter validation
329
+ if (info.Length() < 7 || info.Length() > 10 || !info[info.Length()-1].IsFunction()) {
330
+ return Napi::TypeError::New(env,
331
+ "mosaic(imagesArray, width, height, bgColor, positions, normalized, [outputFormat], [quality], [pngOptimize], callback)")
332
+ .Value();
333
+ }
334
+
335
+ // Parse parameters with minimal overhead
336
+ int i = 0;
337
+ Napi::Array imagesArray = info[i++].As<Napi::Array>();
338
+ int canvasWidth = info[i++].As<Napi::Number>().Int32Value();
339
+ int canvasHeight = info[i++].As<Napi::Number>().Int32Value();
340
+ std::string backgroundColor = info[i++].As<Napi::String>().Utf8Value();
341
+ Napi::Array positions = info[i++].As<Napi::Array>();
342
+ bool normalized = info[i++].As<Napi::Boolean>().Value();
343
+
344
+ // Handle parameters
345
+ std::string outputFormat = "raw";
346
+ int quality = 90;
347
+ bool pngOptimize = false;
348
+
349
+ if (info.Length() - i >= 2) {
350
+ outputFormat = info[i++].As<Napi::String>().Utf8Value();
351
+ }
352
+
353
+ if (info.Length() - i >= 2) {
354
+ quality = info[i++].As<Napi::Number>().Int32Value();
355
+ }
356
+
357
+ if (info.Length() - i >= 2) {
358
+ pngOptimize = info[i++].As<Napi::Boolean>().Value();
359
+ }
360
+
361
+ Napi::Function callback = info[i].As<Napi::Function>();
362
+
363
+ // Validate canvas dimensions
364
+ if (canvasWidth <= 0 || canvasHeight <= 0) {
365
+ return Napi::TypeError::New(env, "Canvas dimensions must be positive").Value();
366
+ }
367
+
368
+ // Launch ULTRA-FAST worker
369
+ (new MosaicWorker(callback, imagesArray, canvasWidth, canvasHeight,
370
+ backgroundColor, positions, normalized, outputFormat, quality, pngOptimize))->Queue();
371
+
372
+ return env.Undefined();
373
+ }
@@ -0,0 +1,124 @@
1
+ // ───────── src/padding.cpp (ultra‑optimised + pad colour) ─────────
2
+ #include <napi.h>
3
+ #include <opencv2/opencv.hpp>
4
+ #include "utils.h"
5
+
6
+ /*------------------------------------------------------------------------*/
7
+ class PaddingWorker final : public Napi::AsyncWorker {
8
+ public:
9
+ PaddingWorker(Napi::Function cb,
10
+ const Napi::Value& imgVal,
11
+ int top,int bottom,int left,int right,
12
+ cv::Scalar padRGB,
13
+ std::string outputFormat,
14
+ int quality = 90,
15
+ bool pngOptimize = false)
16
+ : Napi::AsyncWorker(cb),
17
+ t_(top),b_(bottom),l_(left),r_(right),
18
+ padColorRGB(padRGB),
19
+ outputFormat(std::move(outputFormat)),
20
+ quality(quality),
21
+ pngOptimize(pngOptimize)
22
+ {
23
+ /* convertMs — zero‑copy → cv::Mat */
24
+ const int64 t0=cv::getTickCount();
25
+ src_=ConvertToMat(imgVal);
26
+ convertMs_=(cv::getTickCount()-t0)/cv::getTickFrequency()*1e3;
27
+
28
+ if (imgVal.IsObject() && !imgVal.IsBuffer()) {
29
+ Napi::Object obj = imgVal.As<Napi::Object>();
30
+
31
+ // Check for new colorSpace field first
32
+ if (obj.Has("colorSpace")) {
33
+ channel_ = obj.Get("colorSpace").As<Napi::String>().Utf8Value();
34
+ }
35
+ // Default based on channel count
36
+ else {
37
+ channel_ = (src_.channels() == 4) ? "BGRA"
38
+ : (src_.channels() == 3) ? "BGR"
39
+ : "GRAY";
40
+ }
41
+ } else {
42
+ // Buffer input - determine from OpenCV Mat
43
+ channel_ = (src_.channels() == 4) ? "BGRA"
44
+ : (src_.channels() == 3) ? "BGR"
45
+ : "GRAY";
46
+ }
47
+
48
+ padClrImg=padColorRGB;
49
+ if(channel_=="RGB"||channel_=="RGBA") std::swap(padClrImg[0],padClrImg[2]);
50
+ }
51
+
52
+ protected:
53
+ void Execute() override {
54
+ const int64 t0=cv::getTickCount();
55
+ cv::copyMakeBorder(src_,dst_,t_,b_,l_,r_,cv::BORDER_CONSTANT,padClrImg);
56
+ taskMs_=(cv::getTickCount()-t0)/cv::getTickFrequency()*1e3;
57
+
58
+ if(outputFormat != "raw"){
59
+ const cv::Mat& srcForEncoding=(channel_=="BGR")?dst_:ToBgrForJpg(dst_,channel_);
60
+ encodeMs_=EncodeToFormat(srcForEncoding,encodedBuf_,outputFormat,quality,pngOptimize);
61
+ }
62
+ }
63
+
64
+ void OnOK() override {
65
+ Napi::Env env=Env();
66
+ Napi::Value jsImg=(outputFormat != "raw")?VectorToBuffer(env,std::move(encodedBuf_))
67
+ :MatToRawJS(env,dst_,channel_);
68
+
69
+ Napi::Object res=Napi::Object::New(env);
70
+ res.Set("image",jsImg);
71
+ res.Set("timing",MakeTimingJS(env,convertMs_,taskMs_,encodeMs_));
72
+ Callback().Call({env.Null(),res});
73
+ }
74
+
75
+ private:
76
+ cv::Mat src_,dst_;
77
+ int t_,b_,l_,r_;
78
+ cv::Scalar padColorRGB,padClrImg;
79
+ std::string channel_;
80
+ std::string outputFormat;
81
+ int quality;
82
+ bool pngOptimize;
83
+
84
+ double convertMs_{0},taskMs_{0},encodeMs_{0};
85
+ std::vector<uchar> encodedBuf_;
86
+ };
87
+
88
+ /*──────── binding: padding(img,top,bottom,left,right,padHex,[outputFormat],[quality],cb) ─*/
89
+ Napi::Value Padding(const Napi::CallbackInfo& info){
90
+ Napi::Env env=info.Env();
91
+ if(info.Length()<7||info.Length()>10||!info[info.Length()-1].IsFunction())
92
+ return Napi::TypeError::New(env,
93
+ "padding(image,top,bottom,left,right,padHex,[outputFormat],[quality],[pngOptimize],callback)").Value();
94
+
95
+ int i=0;
96
+ Napi::Value img=info[i++];
97
+ int top =info[i++].As<Napi::Number>();
98
+ int bottom=info[i++].As<Napi::Number>();
99
+ int left =info[i++].As<Napi::Number>();
100
+ int right =info[i++].As<Napi::Number>();
101
+ cv::Scalar pad=ParseColor(info[i++].As<Napi::String>());
102
+
103
+ // Handle parameters
104
+ std::string outputFormat = "raw";
105
+ int quality = 90;
106
+ bool pngOptimize = false;
107
+
108
+ if (info.Length() - i >= 2) {
109
+ outputFormat = info[i++].As<Napi::String>().Utf8Value();
110
+ }
111
+
112
+ if (info.Length() - i >= 2) {
113
+ quality = info[i++].As<Napi::Number>().Int32Value();
114
+ }
115
+
116
+ if (info.Length() - i >= 2) {
117
+ pngOptimize = info[i++].As<Napi::Boolean>().Value();
118
+ }
119
+
120
+ Napi::Function cb=info[i].As<Napi::Function>();
121
+
122
+ (new PaddingWorker(cb,img,top,bottom,left,right,pad,outputFormat,quality,pngOptimize))->Queue();
123
+ return env.Undefined();
124
+ }