@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,112 @@
1
+ /**
2
+ * Node‑RED logic for *rosepetal‑concat* (C++ backend).
3
+ * Concatenates an array of images into ONE image according to the chosen
4
+ * direction and padding / resizing strategy.
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 ConcatNode (config) {
17
+ RED.nodes.createNode(this, config);
18
+ const node = this;
19
+
20
+ node.on('input', async (msg, send, done) => {
21
+ const outputPath = config.outputPath || 'payload';
22
+ let firstImage = null;
23
+
24
+ try {
25
+ const t0 = performance.now();
26
+ node.status({}); // clear status
27
+
28
+ /* ▸ Read images --------------------------------------------------- */
29
+ const list = RED.util.getMessageProperty(msg, config.inputPath || 'payload');
30
+ const imgs = Array.isArray(list) ? list : [ list ]; // always an array
31
+
32
+ // Capture first image for error passthrough
33
+ firstImage = imgs[0] || null;
34
+
35
+ // Validate input images - concat expects array input
36
+ if (!NodeUtils.validateListImage(imgs, node)) {
37
+ return NodeUtils.handleValidationErrorWithPassthrough(
38
+ node, 'Invalid image list', msg, send, done,
39
+ { originalPayload: firstImage, outputPath, outputType: 'single' }
40
+ );
41
+ }
42
+
43
+ /* ▸ Options from the editor -------------------------------------- */
44
+ const direction = config.direction; // 'right' | 'left' | 'down' | 'up'
45
+ const strategy = config.strategy; // 'pad-start' | 'pad-end' | 'pad-both' | 'resize'
46
+ const outputFormat = config.outputFormat || 'raw';
47
+ const outputQuality = parseInt(config.outputQuality) || 90;
48
+ const pngOptimize = config.pngOptimize || false;
49
+ const padColorHex = config.padColor || '#000000';
50
+
51
+ /* ▸ Single call to the C++ addon --------------------------------- */
52
+ const { image, timing = {} } =
53
+ await Cpp.concat(imgs, direction, strategy, padColorHex, outputFormat, outputQuality, pngOptimize);
54
+
55
+ /* ▸ Write the single result back to msg -------------------------- */
56
+ RED.util.setMessageProperty(msg, config.outputPath || 'payload', image);
57
+
58
+ /* ▸ Status: standardized success formatting ----------------------- */
59
+ const total = performance.now() - t0;
60
+
61
+ // Debug image display
62
+ let debugFormat = null;
63
+ if (config.debugEnabled) {
64
+ try {
65
+ // Resolve and validate debug width
66
+ let debugWidth = NodeUtils.resolveDimension(
67
+ node,
68
+ config.debugWidthType,
69
+ config.debugWidth,
70
+ msg
71
+ );
72
+ debugWidth = Math.max(1, parseInt(debugWidth) || 200); // Ensure positive, default 200
73
+
74
+ const debugResult = await NodeUtils.debugImageDisplay(
75
+ image,
76
+ outputFormat,
77
+ outputQuality,
78
+ node,
79
+ true,
80
+ debugWidth
81
+ );
82
+
83
+ if (debugResult) {
84
+ debugFormat = debugResult.formatMessage;
85
+ // Update node status with debug info
86
+ NodeUtils.setSuccessStatusWithDebug(node, 1, total, timing, debugFormat);
87
+ }
88
+ } catch (debugError) {
89
+ node.warn(`Debug display error: ${debugError.message}`);
90
+ }
91
+ }
92
+
93
+ // Set regular status if debug not enabled or failed
94
+ if (!debugFormat) {
95
+ NodeUtils.setSuccessStatus(node, 1, total, timing);
96
+ }
97
+
98
+ NodeUtils.recordPerformanceMetrics(node, msg, timing || {}, total);
99
+
100
+ send(msg);
101
+ done && done();
102
+ } catch (err) {
103
+ NodeUtils.handleNodeErrorWithPassthrough(
104
+ node, err, msg, send, done, 'concat processing',
105
+ { originalPayload: firstImage, outputPath, outputType: 'single' }
106
+ );
107
+ }
108
+ });
109
+ }
110
+
111
+ RED.nodes.registerType('concat', ConcatNode);
112
+ };
@@ -0,0 +1,550 @@
1
+ <!-- ───────── nodes/mix/mosaic.html ────────────── -->
2
+
3
+ <!-- 1 ▸ Runtime registration -->
4
+ <script type="text/javascript">
5
+ RED.nodes.registerType('mosaic',{
6
+
7
+ category:'RP Image',
8
+ color :'#DDA0DD',
9
+ icon :'font-awesome/fa-th',
10
+
11
+ defaults:{
12
+ name:{value:""},
13
+ inputPath:{value:"payload"}, inputPathType:{value:"msg"},
14
+ outputPath:{value:"payload"}, outputPathType:{value:"msg"},
15
+ outputFormat:{value:"raw"},
16
+ outputQuality:{value:90},
17
+ pngOptimize:{value:false},
18
+ // Debug configuration
19
+ debugEnabled:{value:false},
20
+ debugWidth:{value:200},
21
+ debugWidthType:{value:"num"},
22
+
23
+ canvasWidth:{value:"800"}, canvasWidthType:{value:"num"},
24
+ canvasHeight:{value:"600"}, canvasHeightType:{value:"num"},
25
+ backgroundColor:{value:"#000000"},
26
+ coordNorm:{value:false},
27
+
28
+ positions:{value:[]} // Array of {arrayIndex, x, y} objects
29
+ },
30
+
31
+ inputs:1, outputs:1,
32
+ label:function(){ return this.name || "mosaic"; },
33
+
34
+ /* ---------------- oneditprepare ---------------- */
35
+ oneditprepare:function(){
36
+ const node = this;
37
+
38
+ /* Standard I/O typedInputs */
39
+ $("#node-input-inputPath").typedInput({
40
+ default:'msg',types:['msg','flow','global'],
41
+ typeField:"#node-input-inputPathType"
42
+ });
43
+ $("#node-input-outputPath").typedInput({
44
+ default:'msg',types:['msg','flow','global'],
45
+ typeField:"#node-input-outputPathType"
46
+ });
47
+
48
+ /* Canvas dimension typedInputs */
49
+ $("#node-input-canvasWidth").typedInput({
50
+ default:'num',
51
+ types:['num','msg','flow','global'],
52
+ typeField:"#node-input-canvasWidthType"
53
+ });
54
+ $("#node-input-canvasHeight").typedInput({
55
+ default:'num',
56
+ types:['num','msg','flow','global'],
57
+ typeField:"#node-input-canvasHeightType"
58
+ });
59
+
60
+ /* Coordinate normalization toggle */
61
+ function updateCoordTips(){
62
+ const on = $("#node-input-coordNorm").prop("checked");
63
+ $("#norm-enabled").toggle( on );
64
+ $("#norm-disabled").toggle(!on );
65
+ }
66
+ $("#node-input-coordNorm").on("change", updateCoordTips);
67
+ updateCoordTips();
68
+
69
+ /* Dynamic position mapping table */
70
+ function renderPositionTable() {
71
+ const container = $("#position-container");
72
+ container.empty();
73
+
74
+ const table = $('<table id="position-table" style="width:100%; border-collapse: collapse; margin: 10px 0;">');
75
+ const header = $('<tr style="background-color: #f0f0f0; font-weight: bold;">');
76
+ header.append('<td style="padding: 8px; border: 1px solid #ddd;">Array Index</td>');
77
+ header.append('<td style="padding: 8px; border: 1px solid #ddd;">X Position</td>');
78
+ header.append('<td style="padding: 8px; border: 1px solid #ddd;">Y Position</td>');
79
+ header.append('<td style="padding: 8px; border: 1px solid #ddd;">Actions</td>');
80
+ table.append(header);
81
+
82
+ node.positions.forEach((pos, index) => {
83
+ const row = $('<tr>');
84
+ row.append(`<td style="padding: 8px; border: 1px solid #ddd;"><input type="number" value="${pos.arrayIndex}" min="0" class="array-index" style="width: 100%;" /></td>`);
85
+ row.append(`<td style="padding: 8px; border: 1px solid #ddd;"><input type="number" value="${pos.x}" class="pos-x" style="width: 100%;" step="any" /></td>`);
86
+ row.append(`<td style="padding: 8px; border: 1px solid #ddd;"><input type="number" value="${pos.y}" class="pos-y" style="width: 100%;" step="any" /></td>`);
87
+ row.append(`<td style="padding: 8px; border: 1px solid #ddd;"><button type="button" class="remove-position" data-index="${index}" style="background: #ff4444; color: white; border: none; padding: 4px 8px; cursor: pointer;">Remove</button></td>`);
88
+ table.append(row);
89
+ });
90
+
91
+ container.append(table);
92
+
93
+ // Add new position button
94
+ const addButton = $('<button type="button" id="add-position" style="background: #4CAF50; color: white; border: none; padding: 8px 16px; margin-top: 10px; cursor: pointer;">Add Position</button>');
95
+ container.append(addButton);
96
+ }
97
+
98
+ // Initialize positions if empty
99
+ if (!node.positions || node.positions.length === 0) {
100
+ node.positions = [{arrayIndex: 0, x: 0, y: 0}];
101
+ }
102
+
103
+ renderPositionTable();
104
+
105
+ // Add position handler
106
+ $("#position-container").on("click", "#add-position", function() {
107
+ node.positions.push({arrayIndex: 0, x: 0, y: 0});
108
+ renderPositionTable();
109
+ RED.nodes.dirty(true); // Mark node as dirty
110
+ });
111
+
112
+ // Remove position handler
113
+ $("#position-container").on("click", ".remove-position", function() {
114
+ const index = $(this).data("index");
115
+ node.positions.splice(index, 1);
116
+ renderPositionTable();
117
+ RED.nodes.dirty(true); // Mark node as dirty
118
+ });
119
+
120
+ // Update positions when values change and mark node as dirty
121
+ $("#position-container").on("input", "input", function() {
122
+ const row = $(this).closest("tr");
123
+ const index = row.index() - 1; // Subtract 1 for header row
124
+
125
+ if (index >= 0 && index < node.positions.length) {
126
+ node.positions[index].arrayIndex = parseInt(row.find(".array-index").val()) || 0;
127
+ node.positions[index].x = parseFloat(row.find(".pos-x").val()) || 0;
128
+ node.positions[index].y = parseFloat(row.find(".pos-y").val()) || 0;
129
+
130
+ // Mark the node as dirty so Node-RED enables deploy
131
+ RED.nodes.dirty(true);
132
+ }
133
+ });
134
+
135
+ function updateQualityVisibility(){
136
+ const format = $("#node-input-outputFormat").val();
137
+ $("#quality-row").toggle(format === "jpg" || format === "webp");
138
+ $("#png-optimize-row").toggle(format === "png");
139
+ }
140
+ $("#node-input-outputFormat").on("change", updateQualityVisibility);
141
+ updateQualityVisibility(); // initial state
142
+
143
+ // === Debug Display Logic ===
144
+ const debugCheckbox = $('#node-input-debugEnabled');
145
+ const debugWidthRow = $('#debug-width-row');
146
+
147
+ // Initialize debug width TypedInput
148
+ $("#node-input-debugWidth").typedInput({
149
+ default: 'num',
150
+ types: ['num', 'msg', 'flow', 'global'],
151
+ typeField: "#node-input-debugWidthType"
152
+ });
153
+
154
+ // Function to toggle debug width visibility
155
+ function updateDebugWidthVisibility() {
156
+ if (debugCheckbox.is(':checked')) {
157
+ debugWidthRow.show();
158
+ } else {
159
+ debugWidthRow.hide();
160
+ }
161
+ }
162
+
163
+ // Initialize debug state on node open
164
+ updateDebugWidthVisibility();
165
+
166
+ // Handle debug checkbox changes
167
+ debugCheckbox.on('change', function() {
168
+ const enabled = this.checked;
169
+ updateDebugWidthVisibility();
170
+ // Store debug state for runtime access
171
+ $('#node-input-debugEnabled').data('debug-enabled', enabled);
172
+ });
173
+ }
174
+ });
175
+
176
+ // Debug image display renderer - shared across all image processing nodes
177
+ if (!window.debugImageRendererInitialized) {
178
+ window.debugImageRendererInitialized = true;
179
+
180
+ (function() {
181
+ var isSubscribed = false;
182
+
183
+ function subscribeToDebugImages() {
184
+ if (isSubscribed || typeof RED === 'undefined' || !RED.comms) return;
185
+ isSubscribed = true;
186
+
187
+ RED.comms.subscribe('debug-image', function(event, data) {
188
+ if (!data || !data.id) return;
189
+
190
+ try {
191
+ renderDebugImage(data);
192
+ } catch (err) {
193
+ console.warn('Debug image render error:', err);
194
+ }
195
+ });
196
+ }
197
+
198
+ function renderDebugImage(data) {
199
+ const nodeId = data.id;
200
+ const base64Data = data.data;
201
+ const format = data.format || 'unknown';
202
+ const mimeType = data.mimeType || 'jpeg';
203
+
204
+ if (!base64Data) {
205
+ removeDebugImage(nodeId);
206
+ return;
207
+ }
208
+
209
+ const nodeElement = document.getElementById(nodeId);
210
+ if (!nodeElement) return;
211
+
212
+ let debugContainer = document.getElementById('debug-img-container-' + nodeId);
213
+
214
+ if (!debugContainer) {
215
+ // Create SVG foreignObject to embed HTML content in SVG
216
+ debugContainer = document.createElementNS('http://www.w3.org/2000/svg', 'foreignObject');
217
+ debugContainer.id = 'debug-img-container-' + nodeId;
218
+ debugContainer.setAttribute('x', '70');
219
+ debugContainer.setAttribute('y', '5');
220
+ debugContainer.setAttribute('width', '120');
221
+ debugContainer.setAttribute('height', '120');
222
+ debugContainer.style.overflow = 'visible';
223
+
224
+ // Create simple image directly inside foreignObject
225
+ const img = document.createElement('img');
226
+ img.id = 'debug-img-' + nodeId;
227
+ img.style.width = '100%';
228
+ img.style.height = 'auto';
229
+ img.style.display = 'block';
230
+ img.style.cursor = 'pointer';
231
+ img.style.maxHeight = '100px';
232
+ img.style.maxWidth = '100px';
233
+
234
+ img.onclick = function() {
235
+ removeDebugImage(nodeId);
236
+ };
237
+
238
+ debugContainer.appendChild(img);
239
+ nodeElement.appendChild(debugContainer);
240
+ }
241
+
242
+ const img = document.getElementById('debug-img-' + nodeId);
243
+
244
+ if (img) {
245
+ img.src = `data:image/${mimeType};base64,${base64Data}`;
246
+ img.title = 'Debug image (click to hide)';
247
+ }
248
+ }
249
+
250
+ function removeDebugImage(nodeId) {
251
+ const debugContainer = document.getElementById('debug-img-container-' + nodeId);
252
+ if (debugContainer) {
253
+ debugContainer.remove();
254
+ }
255
+ }
256
+
257
+ function initializeWhenReady() {
258
+ if (typeof RED !== 'undefined' && RED.comms) {
259
+ subscribeToDebugImages();
260
+
261
+ // Clean up orphaned debug images periodically
262
+ setInterval(function() {
263
+ const debugContainers = document.querySelectorAll('[id^="debug-img-container-"]');
264
+ debugContainers.forEach(container => {
265
+ const nodeId = container.id.replace('debug-img-container-', '');
266
+ const nodeElement = document.getElementById(nodeId);
267
+ if (!nodeElement) {
268
+ container.remove();
269
+ }
270
+ });
271
+ }, 5000);
272
+ } else {
273
+ setTimeout(initializeWhenReady, 100);
274
+ }
275
+ }
276
+
277
+ if (document.readyState === 'loading') {
278
+ document.addEventListener('DOMContentLoaded', initializeWhenReady);
279
+ } else {
280
+ initializeWhenReady();
281
+ }
282
+ })();
283
+ }
284
+ </script>
285
+
286
+ <!-- 2 ▸ Edit‑dialog template -->
287
+ <script type="text/x-red" data-template-name="mosaic">
288
+
289
+ <!-- Name -->
290
+ <div class="form-row">
291
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
292
+ <input type="text" id="node-input-name" placeholder="Name">
293
+ </div>
294
+
295
+ <hr>
296
+
297
+ <!-- STANDARD I/O SECTION (copied from crop.html) -->
298
+ <div class="form-row">
299
+ <label for="node-input-inputPath"><i class="fa fa-sign-in"></i> Input from</label>
300
+ <input type="text" id="node-input-inputPath" style="width: 70%;">
301
+ <input type="hidden" id="node-input-inputPathType">
302
+ </div>
303
+ <div class="form-row">
304
+ <label for="node-input-outputPath"><i class="fa fa-sign-out"></i> Output to</label>
305
+ <input type="text" id="node-input-outputPath" style="width: 70%;">
306
+ <input type="hidden" id="node-input-outputPathType">
307
+ </div>
308
+ <div class="form-row">
309
+ <label for="node-input-outputFormat"><i class="fa fa-file-image-o"></i> Output Format</label>
310
+ <select id="node-input-outputFormat" style="width: 200px;">
311
+ <option value="raw">Raw (fastest)</option>
312
+ <option value="jpg">JPEG</option>
313
+ <option value="png">PNG</option>
314
+ <option value="webp">WebP</option>
315
+ </select>
316
+ </div>
317
+ <div class="form-row" id="quality-row">
318
+ <label for="node-input-outputQuality"><i class="fa fa-sliders"></i> Quality</label>
319
+ <input type="number" id="node-input-outputQuality" min="1" max="100" value="90" style="width: 70px;">
320
+ <span style="margin-left: 10px; color: #666;">1-100 (JPG/WebP only)</span>
321
+ </div>
322
+ <div class="form-row" id="png-optimize-row" style="display: none;">
323
+ <label for="node-input-pngOptimize"><i class="fa fa-compress"></i> PNG Optimize</label>
324
+ <input type="checkbox" id="node-input-pngOptimize" style="display:inline-block; width:auto; vertical-align:baseline;">
325
+ <label for="node-input-pngOptimize" style="width:auto; margin-left:5px;">Compress PNG (slower, smaller file)</label>
326
+ </div>
327
+
328
+ <!-- Debug Configuration -->
329
+ <div class="form-row">
330
+ <label for="node-input-debugEnabled"><i class="fa fa-bug"></i> Debug</label>
331
+ <input type="checkbox" id="node-input-debugEnabled" style="display:inline-block; width:auto; vertical-align:baseline;">
332
+ <label for="node-input-debugEnabled" style="width:auto; margin-left:5px;">Show processed image</label>
333
+ </div>
334
+ <div class="form-row" id="debug-width-row" style="display: none;">
335
+ <label for="node-input-debugWidth"><i class="fa fa-arrows-h"></i> Debug Width</label>
336
+ <input type="text" id="node-input-debugWidth" style="width: 120px;">
337
+ <input type="hidden" id="node-input-debugWidthType">
338
+ <span style="margin-left: 10px; color: #666;">pixels</span>
339
+ </div>
340
+
341
+ <hr>
342
+
343
+ <!-- CANVAS CONFIGURATION -->
344
+ <div class="form-row">
345
+ <label for="node-input-canvasWidth"><i class="fa fa-arrows-h"></i> Canvas Width</label>
346
+ <input type="text" id="node-input-canvasWidth" style="width: 70%;" placeholder="800">
347
+ <input type="hidden" id="node-input-canvasWidthType">
348
+ </div>
349
+ <div class="form-row">
350
+ <label for="node-input-canvasHeight"><i class="fa fa-arrows-v"></i> Canvas Height</label>
351
+ <input type="text" id="node-input-canvasHeight" style="width: 70%;" placeholder="600">
352
+ <input type="hidden" id="node-input-canvasHeightType">
353
+ </div>
354
+ <div class="form-row">
355
+ <label for="node-input-backgroundColor"><i class="fa fa-paint-brush"></i> Background Color</label>
356
+ <input type="color" id="node-input-backgroundColor" style="width: 70px; height: 30px;">
357
+ </div>
358
+
359
+ <div class="form-row">
360
+ <label for="node-input-coordNorm"><i class="fa fa-percent"></i> Normalized Coords</label>
361
+ <input type="checkbox" id="node-input-coordNorm" style="width: 20px; height: 20px;">
362
+ </div>
363
+
364
+ <!-- Tips that toggle -->
365
+ <div id="norm-enabled" class="form-tips" style="font-size: 0.85em; margin: 4px 0; color: #666;">
366
+ <b>Normalized ON:</b> position values must be between <code>0.0</code> and <code>1.0</code> (fractions of canvas dimensions).
367
+ </div>
368
+ <div id="norm-disabled" class="form-tips" style="font-size: 0.85em; margin: 4px 0; color: #666;">
369
+ <b>Normalized OFF:</b> position values are absolute pixels from top-left corner.
370
+ </div>
371
+
372
+ <hr>
373
+
374
+ <!-- POSITION MAPPING TABLE -->
375
+ <div class="form-row">
376
+ <label style="width: 100%; margin-bottom: 10px;"><i class="fa fa-th"></i> Image Positions</label>
377
+ <div id="position-container" style="width: 100%;"></div>
378
+ </div>
379
+
380
+ <div class="form-tips">
381
+ <b>Instructions:</b>
382
+ <ul>
383
+ <li><b>Array Index:</b> Which image from the input array to place (0 = first image, 1 = second, etc.)</li>
384
+ <li><b>X, Y Position:</b> Top-left corner coordinates on the canvas</li>
385
+ <li><b>Multiple Positions:</b> Same image can be placed multiple times at different positions</li>
386
+ <li><b>Dynamic Configuration:</b> Use Add/Remove buttons to manage position mappings</li>
387
+ </ul>
388
+ </div>
389
+ </script>
390
+
391
+ <!-- 3 ▸ Help panel -->
392
+ <script type="text/x-red" data-help-name="mosaic">
393
+ <p><b>rosepetal-mosaic</b> creates a composite image by placing multiple images from an input array onto a canvas at specified positions. This node enables precise control over image positioning, supports normalized coordinates, and offers flexible canvas configuration for creating complex image layouts.</p>
394
+
395
+ <h3>Details</h3>
396
+ <p>This node processes arrays of images and places them onto a customizable canvas using high-performance C++ operations. It supports both absolute pixel coordinates and normalized coordinates (0.0-1.0), dynamic canvas sizing, and flexible positioning controls. The node handles both raw pixel data and encoded image files transparently, making it ideal for creating collages, dashboards, and composite layouts.</p>
397
+
398
+ <h3>Properties</h3>
399
+ <dl class="message-properties">
400
+ <dt>Input from <span class="property-type">string</span></dt>
401
+ <dd>Message property path containing the input image array. Supports <code>msg</code>, <code>flow</code>, or <code>global</code> context via TypedInput.</dd>
402
+
403
+ <dt>Output to <span class="property-type">string</span></dt>
404
+ <dd>Message property path where the mosaic image will be stored. Supports <code>msg</code>, <code>flow</code>, or <code>global</code> context via TypedInput.</dd>
405
+
406
+ <dt>Output Format <span class="property-type">string</span></dt>
407
+ <dd>Format for the output image: <code>raw</code> (fastest), <code>jpg</code>, <code>png</code>, or <code>webp</code>.</dd>
408
+
409
+ <dt>Quality <span class="property-type">number</span></dt>
410
+ <dd>Compression quality (1-100) for JPEG and WebP formats. Only visible when JPG or WebP is selected.</dd>
411
+
412
+ <dt>Canvas Width <span class="property-type">number|string</span></dt>
413
+ <dd>Width of the output canvas in pixels. Supports dynamic values from <code>msg</code>, <code>flow</code>, or <code>global</code> context via TypedInput.</dd>
414
+
415
+ <dt>Canvas Height <span class="property-type">number|string</span></dt>
416
+ <dd>Height of the output canvas in pixels. Supports dynamic values from <code>msg</code>, <code>flow</code>, or <code>global</code> context via TypedInput.</dd>
417
+
418
+ <dt>Background Color <span class="property-type">color</span></dt>
419
+ <dd>Canvas background color in hex format (e.g., #000000). Images are placed on top of this background.</dd>
420
+
421
+ <dt>Normalized Coords <span class="property-type">boolean</span></dt>
422
+ <dd>When enabled, position coordinates use normalized values (0.0-1.0) as fractions of canvas dimensions. When disabled, coordinates are absolute pixels.</dd>
423
+
424
+ <dt>Image Positions <span class="property-type">array</span></dt>
425
+ <dd>Dynamic table of position mappings. Each row specifies: Array Index (which input image), X Position, Y Position. Same image can be placed multiple times.</dd>
426
+ </dl>
427
+
428
+ <h3>Inputs</h3>
429
+ <p>The input should be an array of image objects in the standard format:</p>
430
+ <pre>{
431
+ data: Buffer, // Raw pixel data or encoded file buffer
432
+ width: number, // Image width in pixels
433
+ height: number, // Image height in pixels
434
+ channels: number, // Channel count (1=grayscale, 3=RGB, 4=RGBA)
435
+ colorSpace: string, // "GRAY", "RGB", "RGBA", "BGR", "BGRA"
436
+ dtype: string // "uint8", "uint16", "float32"
437
+ }</pre>
438
+ <p>File buffers (JPEG, PNG, WebP, BMP) are automatically detected and decoded.</p>
439
+
440
+ <h3>Outputs</h3>
441
+ <p>Produces a single composite mosaic image in the specified format:</p>
442
+ <ul>
443
+ <li><b>Raw format:</b> Complete image object with pixel data and metadata</li>
444
+ <li><b>Encoded formats (JPG/PNG/WebP):</b> File buffer ready for storage or transmission</li>
445
+ </ul>
446
+ <p>Output dimensions match the configured canvas size. Images are placed at their specified positions, potentially overlapping if coordinates overlap.</p>
447
+
448
+ <h3>Position Mapping System</h3>
449
+ <h4>Array Index</h4>
450
+ <ul>
451
+ <li><b>Zero-based indexing:</b> 0 = first image, 1 = second image, etc.</li>
452
+ <li><b>Multiple placement:</b> Same image can be referenced multiple times for duplication</li>
453
+ <li><b>Out-of-bounds handling:</b> Invalid indices are gracefully ignored with warnings</li>
454
+ </ul>
455
+
456
+ <h4>Coordinate Systems</h4>
457
+ <ul>
458
+ <li><b>Absolute pixels (Normalized OFF):</b> Direct pixel coordinates from top-left corner (0,0)</li>
459
+ <li><b>Normalized coordinates (Normalized ON):</b> Values 0.0-1.0 as fractions of canvas dimensions</li>
460
+ <li><b>Position reference:</b> All coordinates specify the top-left corner of placed images</li>
461
+ </ul>
462
+
463
+ <h4>Dynamic Configuration</h4>
464
+ <ul>
465
+ <li><b>Add/Remove buttons:</b> Dynamically manage position mappings in the editor</li>
466
+ <li><b>Real-time validation:</b> Invalid values are automatically corrected</li>
467
+ <li><b>Persistent configuration:</b> Position mappings are saved with the flow</li>
468
+ </ul>
469
+
470
+ <h3>Examples</h3>
471
+ <h4>Photo Collage</h4>
472
+ <p>Create a photo collage with normalized positioning:</p>
473
+ <ul>
474
+ <li>Canvas: 1200x800 pixels</li>
475
+ <li>Normalized Coords: ON</li>
476
+ <li>Positions: (0, 0.1, 0.1), (1, 0.6, 0.1), (2, 0.1, 0.6), (3, 0.6, 0.6)</li>
477
+ <li>Background: White (#FFFFFF)</li>
478
+ </ul>
479
+
480
+ <h4>Dashboard Layout</h4>
481
+ <p>Create a dashboard with absolute positioning:</p>
482
+ <ul>
483
+ <li>Canvas: 1920x1080 pixels</li>
484
+ <li>Normalized Coords: OFF</li>
485
+ <li>Header image at (0, 0), sidebar at (0, 100), main content at (300, 100)</li>
486
+ <li>Background: Dark gray (#333333)</li>
487
+ </ul>
488
+
489
+ <h4>Dynamic Canvas from Context</h4>
490
+ <p>Use flow context for dynamic sizing:</p>
491
+ <ul>
492
+ <li>Canvas Width: flow.screenWidth</li>
493
+ <li>Canvas Height: flow.screenHeight</li>
494
+ <li>Input from: flow.thumbnails</li>
495
+ <li>Responsive positioning based on screen dimensions</li>
496
+ </ul>
497
+
498
+ <h3>Mosaic Patterns</h3>
499
+ <h4>Grid Layouts</h4>
500
+ <ul>
501
+ <li><b>Regular Grid:</b> Use normalized coordinates with calculated spacing (e.g., 0.25, 0.5, 0.75)</li>
502
+ <li><b>Asymmetric Grid:</b> Mix different image sizes with custom positioning</li>
503
+ <li><b>Overlapping Grid:</b> Create layered effects with overlapping positions</li>
504
+ </ul>
505
+
506
+ <h4>Artistic Arrangements</h4>
507
+ <ul>
508
+ <li><b>Scattered Layout:</b> Random positioning for organic arrangements</li>
509
+ <li><b>Circular Pattern:</b> Calculate positions around a center point</li>
510
+ <li><b>Layered Composition:</b> Z-order determined by position mapping sequence</li>
511
+ </ul>
512
+
513
+ <h3>Node Interactions</h3>
514
+ <ul>
515
+ <li><b>Upstream:</b> Works with <code>array-in</code>, <code>image-in</code>, or any nodes producing image arrays</li>
516
+ <li><b>Downstream:</b> Connect to <code>array-out</code>, file writers, or display nodes</li>
517
+ <li><b>Chaining:</b> Can be combined with <code>concat</code> and other processing nodes</li>
518
+ <li><b>Context Integration:</b> Supports dynamic canvas sizing and positioning via context</li>
519
+ <li><b>Flow Coordination:</b> Use with inject nodes for automated mosaic generation</li>
520
+ </ul>
521
+
522
+ <h3>Output Formats</h3>
523
+ <ul>
524
+ <li><b>Raw (fastest):</b> Uncompressed pixel data, ideal for further processing or chaining</li>
525
+ <li><b>JPEG:</b> Lossy compression with quality control, smaller files for web display</li>
526
+ <li><b>PNG:</b> Lossless compression, preserves transparency, larger files</li>
527
+ <li><b>WebP:</b> Modern format with superior compression and quality control</li>
528
+ </ul>
529
+
530
+ <h3>Error Handling</h3>
531
+ <ul>
532
+ <li><b>Input Validation:</b> Comprehensive validation of image array structure and position mappings</li>
533
+ <li><b>Bounds Checking:</b> Automatic handling of out-of-bounds coordinates and array indices</li>
534
+ <li><b>Canvas Validation:</b> Ensures valid canvas dimensions with minimum size constraints</li>
535
+ <li><b>Memory Management:</b> Efficient memory usage with automatic cleanup and garbage collection</li>
536
+ <li><b>Position Validation:</b> Validates coordinate ranges for both absolute and normalized modes</li>
537
+ <li><b>Warning System:</b> Uses <code>node.warn()</code> for non-fatal issues without stopping flow execution</li>
538
+ </ul>
539
+
540
+ <h3>Performance</h3>
541
+ <ul>
542
+ <li><b>C++ Backend:</b> High-performance OpenCV-based processing with optimized build flags</li>
543
+ <li><b>Zero-Copy Operations:</b> Minimal memory allocation and copying for maximum efficiency</li>
544
+ <li><b>Parallel Processing:</b> Image placement operations processed in parallel using Promise.all()</li>
545
+ <li><b>Canvas Optimization:</b> Efficient canvas initialization and background rendering</li>
546
+ <li><b>Memory Pooling:</b> Reuses memory buffers across operations to reduce allocation overhead</li>
547
+ <li><b>Build Optimization:</b> Compiled with <code>-O3</code>, <code>-march=native</code>, <code>-ffast-math</code> flags</li>
548
+ <li><b>Position Caching:</b> Optimized coordinate calculations for repeated positioning operations</li>
549
+ </ul>
550
+ </script>