@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,321 @@
1
+ /**
2
+ * Node‑RED logic for *rosepetal‑add‑masks* (C++ backend).
3
+ * Applies multiple polygon-based masks to a base image using weighted blending with
4
+ * configurable mask strength and class-based color mapping.
5
+ *
6
+ * Timing fields returned by C++:
7
+ * timing.convertMs · timing.taskMs · timing.encodeMs
8
+ * …and the node status shows: OK … (conv X | task Y ms)
9
+ */
10
+ const { performance } = require('perf_hooks');
11
+ const Cpp = require('../../lib/cpp-bridge.js');
12
+
13
+ module.exports = function (RED) {
14
+ const NodeUtils = require('../../lib/node-utils.js')(RED);
15
+
16
+ function AddMasksNode (config) {
17
+ RED.nodes.createNode(this, config);
18
+ const node = this;
19
+
20
+ node.on('input', async (msg, send, done) => {
21
+ // Capture original payload for error passthrough
22
+ const outputPath = config.outputPath || 'payload';
23
+ const image = RED.util.getMessageProperty(msg, config.imagePath || 'payload.image');
24
+ const originalPayload = image;
25
+
26
+ try {
27
+ const t0 = performance.now();
28
+ node.status({}); // clear status
29
+
30
+ /* ▸ Read image and masks array from message ------------------------ */
31
+ // Get base image and masks array from specified paths
32
+ const masksArray = RED.util.getMessageProperty(msg, config.masksPath || 'payload');
33
+
34
+ // Validate base image
35
+ const baseImg = NodeUtils.validateImageStructure(image, node);
36
+ if (!baseImg) {
37
+ return NodeUtils.handleValidationErrorWithPassthrough(
38
+ node, 'Base image is invalid or missing', msg, send, done,
39
+ { originalPayload, outputPath, outputType: 'single' }
40
+ );
41
+ }
42
+
43
+ // Validate masks array - expect direct array format
44
+ if (!Array.isArray(masksArray)) {
45
+ return NodeUtils.handleValidationErrorWithPassthrough(
46
+ node, 'Masks input must be an array', msg, send, done,
47
+ { originalPayload, outputPath, outputType: 'single' }
48
+ );
49
+ }
50
+
51
+ if (masksArray.length === 0) {
52
+ const total = performance.now() - t0;
53
+ RED.util.setMessageProperty(msg, config.outputPath || 'payload', baseImg);
54
+ NodeUtils.setSuccessStatus(node, 0, total, { convertMs: 0, taskMs: 0, encodeMs: 0 });
55
+ NodeUtils.recordPerformanceMetrics(node, msg, {
56
+ convertMs: 0,
57
+ encodeMs: 0,
58
+ taskMs: 0
59
+ }, total);
60
+ send(msg);
61
+ done && done();
62
+ return;
63
+ }
64
+
65
+ const getClassName = (maskObj) => {
66
+ const fields = ['tag', 'class_name', 'className', 'label', 'class'];
67
+ for (const field of fields) {
68
+ if (typeof maskObj[field] === 'string' && maskObj[field].trim() !== '') {
69
+ return maskObj[field].trim();
70
+ }
71
+ }
72
+ return null;
73
+ };
74
+
75
+ const isFiniteNumber = (v) => typeof v === 'number' && Number.isFinite(v);
76
+
77
+ const validate2DMask = (matrix) => {
78
+ if (!Array.isArray(matrix) || matrix.length === 0) return false;
79
+ for (let y = 0; y < matrix.length; y++) {
80
+ const row = matrix[y];
81
+ if (!Array.isArray(row) || row.length === 0) return false;
82
+ for (let x = 0; x < row.length; x++) {
83
+ if (!isFiniteNumber(row[x])) {
84
+ return false;
85
+ }
86
+ }
87
+ }
88
+ return true;
89
+ };
90
+
91
+ const countMasksInValue = (maskVal) => {
92
+ if (!maskVal) return 0;
93
+
94
+ // Raw image mask from inferencer (rgba buffer with width/height)
95
+ if (maskVal.data && maskVal.width && maskVal.height) {
96
+ return NodeUtils.validateImageStructure(maskVal, node) ? 1 : 0;
97
+ }
98
+
99
+ // 2D matrix or array of 2D matrices
100
+ if (Array.isArray(maskVal) && maskVal.length > 0) {
101
+ const first = maskVal[0];
102
+ if (Array.isArray(first) && first.length > 0 && Array.isArray(first[0])) {
103
+ // Possibly array of masks
104
+ let validCount = 0;
105
+ for (const candidate of maskVal) {
106
+ if (validate2DMask(candidate)) validCount++;
107
+ }
108
+ return validCount;
109
+ }
110
+ return validate2DMask(maskVal) ? 1 : 0;
111
+ }
112
+
113
+ return 0;
114
+ };
115
+
116
+ // Validate masks array structure in detail
117
+ let totalMaskCount = 0;
118
+ for (let maskIndex = 0; maskIndex < masksArray.length; maskIndex++) {
119
+ const maskObj = masksArray[maskIndex];
120
+
121
+ if (!maskObj || typeof maskObj !== 'object') {
122
+ return NodeUtils.handleValidationErrorWithPassthrough(
123
+ node, `Invalid mask object at index ${maskIndex}: expected object`, msg, send, done,
124
+ { originalPayload, outputPath, outputType: 'single' }
125
+ );
126
+ }
127
+
128
+ const className = getClassName(maskObj);
129
+ if (!className) {
130
+ return NodeUtils.handleValidationErrorWithPassthrough(
131
+ node, `Mask at index ${maskIndex} is missing a valid class/tag field`, msg, send, done,
132
+ { originalPayload, outputPath, outputType: 'single' }
133
+ );
134
+ }
135
+
136
+ const hasPolygons = Array.isArray(maskObj.polygons);
137
+ const hasMaskField = Object.prototype.hasOwnProperty.call(maskObj, 'mask');
138
+
139
+ if (!hasPolygons && !hasMaskField) {
140
+ return NodeUtils.handleValidationErrorWithPassthrough(
141
+ node, `Mask at index ${maskIndex} must include either 'polygons' or 'mask'`, msg, send, done,
142
+ { originalPayload, outputPath, outputType: 'single' }
143
+ );
144
+ }
145
+
146
+ let entryValid = false;
147
+
148
+ if (hasPolygons) {
149
+ // Validate coordinate format for all polygons in this mask
150
+ for (let polyIdx = 0; polyIdx < maskObj.polygons.length; polyIdx++) {
151
+ const coordinates = maskObj.polygons[polyIdx];
152
+ if (!Array.isArray(coordinates)) {
153
+ return NodeUtils.handleValidationErrorWithPassthrough(
154
+ node, `Invalid polygon at index ${maskIndex}.polygons[${polyIdx}]: expected an array of coordinate pairs`, msg, send, done,
155
+ { originalPayload, outputPath, outputType: 'single' }
156
+ );
157
+ }
158
+
159
+ if (coordinates.length === 0) {
160
+ return NodeUtils.handleValidationErrorWithPassthrough(
161
+ node, `Empty polygon at index ${maskIndex}.polygons[${polyIdx}]: at least one coordinate is required`, msg, send, done,
162
+ { originalPayload, outputPath, outputType: 'single' }
163
+ );
164
+ }
165
+
166
+ for (let i = 0; i < coordinates.length; i++) {
167
+ const point = coordinates[i];
168
+ if (!Array.isArray(point) || point.length !== 2) {
169
+ return NodeUtils.handleValidationErrorWithPassthrough(
170
+ node, `Invalid coordinate at index ${maskIndex}.polygons[${polyIdx}][${i}]: expected [x, y] pair`, msg, send, done,
171
+ { originalPayload, outputPath, outputType: 'single' }
172
+ );
173
+ }
174
+
175
+ const [x, y] = point;
176
+ if (!isFiniteNumber(x) || !isFiniteNumber(y)) {
177
+ return NodeUtils.handleValidationErrorWithPassthrough(
178
+ node, `Invalid coordinate at index ${maskIndex}.polygons[${polyIdx}][${i}]: coordinates must be numbers`, msg, send, done,
179
+ { originalPayload, outputPath, outputType: 'single' }
180
+ );
181
+ }
182
+
183
+ if (x < 0 || x > 1 || y < 0 || y > 1) {
184
+ return NodeUtils.handleValidationErrorWithPassthrough(
185
+ node, `Invalid coordinate at index ${maskIndex}.polygons[${polyIdx}][${i}]: coordinates must be in range [0, 1]`, msg, send, done,
186
+ { originalPayload, outputPath, outputType: 'single' }
187
+ );
188
+ }
189
+ }
190
+
191
+ entryValid = true;
192
+ totalMaskCount++;
193
+ }
194
+ }
195
+
196
+ if (!entryValid && hasMaskField) {
197
+ const maskCount = countMasksInValue(maskObj.mask);
198
+ if (maskCount === 0) {
199
+ return NodeUtils.handleValidationErrorWithPassthrough(
200
+ node, `Invalid mask data at index ${maskIndex}: expected raw mask image or 2D matrix`, msg, send, done,
201
+ { originalPayload, outputPath, outputType: 'single' }
202
+ );
203
+ }
204
+ totalMaskCount += maskCount;
205
+ entryValid = true;
206
+ }
207
+
208
+ if (!entryValid) {
209
+ return NodeUtils.handleValidationErrorWithPassthrough(
210
+ node, `No valid polygons or mask found at index ${maskIndex}`, msg, send, done,
211
+ { originalPayload, outputPath, outputType: 'single' }
212
+ );
213
+ }
214
+ }
215
+
216
+ if (totalMaskCount === 0) {
217
+ const total = performance.now() - t0;
218
+ RED.util.setMessageProperty(msg, config.outputPath || 'payload', baseImg);
219
+ NodeUtils.setSuccessStatus(node, 0, total, { convertMs: 0, taskMs: 0, encodeMs: 0 });
220
+ NodeUtils.recordPerformanceMetrics(node, msg, {
221
+ convertMs: 0,
222
+ encodeMs: 0,
223
+ taskMs: 0
224
+ }, total);
225
+ send(msg);
226
+ done && done();
227
+ return;
228
+ }
229
+
230
+ /* ▸ Options from the editor -------------------------------------- */
231
+ const maskStrength = Math.max(0, Math.min(100,
232
+ config.globalMaskStrength !== undefined ? parseInt(config.globalMaskStrength) : 50)) / 100.0;
233
+ const outputFormat = config.outputFormat || 'raw';
234
+ const outputQuality = parseInt(config.outputQuality) || 90;
235
+ const pngOptimize = config.pngOptimize || false;
236
+
237
+ // Build class color map and priority order from configuration
238
+ const classColorMap = {};
239
+ const classPriorityOrder = []; // Preserves order for priority-based painting
240
+ const classColorMappings = config.classColorMappings || [];
241
+
242
+ for (const mapping of classColorMappings) {
243
+ if (mapping.className && mapping.className.trim() !== '' && mapping.color) {
244
+ const cls = mapping.className.trim();
245
+ classColorMap[cls] = mapping.color;
246
+ classPriorityOrder.push(cls); // First in list = highest priority
247
+ }
248
+ }
249
+
250
+ /* ▸ Single call to the C++ addon --------------------------------- */
251
+ const { image: result, timing = {} } =
252
+ await Cpp.addMasks(
253
+ baseImg,
254
+ masksArray,
255
+ classColorMap,
256
+ classPriorityOrder, // Priority order for overlap resolution
257
+ maskStrength,
258
+ true, // Always auto-generate colors for undefined classes
259
+ outputFormat,
260
+ outputQuality,
261
+ pngOptimize
262
+ );
263
+
264
+ /* ▸ Write the result back to msg ---------------------------------- */
265
+ RED.util.setMessageProperty(msg, config.outputPath || 'payload', result);
266
+
267
+ /* ▸ Status: standardized success formatting ----------------------- */
268
+ const total = performance.now() - t0;
269
+
270
+ // Debug image display
271
+ let debugFormat = null;
272
+ if (config.debugEnabled) {
273
+ try {
274
+ // Resolve and validate debug width
275
+ let debugWidth = NodeUtils.resolveDimension(
276
+ node,
277
+ config.debugWidthType,
278
+ config.debugWidth,
279
+ msg
280
+ );
281
+ debugWidth = Math.max(1, parseInt(debugWidth) || 200); // Ensure positive, default 200
282
+
283
+ const debugResult = await NodeUtils.debugImageDisplay(
284
+ result,
285
+ outputFormat,
286
+ outputQuality,
287
+ node,
288
+ true,
289
+ debugWidth
290
+ );
291
+
292
+ if (debugResult) {
293
+ debugFormat = debugResult.formatMessage;
294
+ // Update node status with debug info and mask count
295
+ NodeUtils.setSuccessStatusWithDebug(node, totalMaskCount, total, timing, debugFormat);
296
+ }
297
+ } catch (debugError) {
298
+ node.warn(`Debug display error: ${debugError.message}`);
299
+ }
300
+ }
301
+
302
+ // Set regular status if debug not enabled or failed
303
+ if (!debugFormat) {
304
+ NodeUtils.setSuccessStatus(node, totalMaskCount, total, timing);
305
+ }
306
+
307
+ NodeUtils.recordPerformanceMetrics(node, msg, timing || {}, total);
308
+
309
+ send(msg);
310
+ done && done();
311
+ } catch (err) {
312
+ NodeUtils.handleNodeErrorWithPassthrough(
313
+ node, err, msg, send, done, 'add-masks processing',
314
+ { originalPayload, outputPath, outputType: 'single' }
315
+ );
316
+ }
317
+ });
318
+ }
319
+
320
+ RED.nodes.registerType('add-masks', AddMasksNode);
321
+ };