@rosepetal/node-red-contrib-image-tools 1.1.3 → 1.2.0

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 (56) hide show
  1. package/README.md +1 -13
  2. package/node-red-contrib-image-tools/lib/node-utils.js +218 -17
  3. package/node-red-contrib-image-tools/nodes/blend/add-bbs.js +64 -9
  4. package/node-red-contrib-image-tools/nodes/blend/add-mask.js +81 -9
  5. package/node-red-contrib-image-tools/nodes/blend/add-masks.js +64 -8
  6. package/node-red-contrib-image-tools/nodes/blend/blend.js +64 -10
  7. package/node-red-contrib-image-tools/nodes/io/folder-in.js +12 -1
  8. package/node-red-contrib-image-tools/nodes/io/image-in.js +10 -2
  9. package/node-red-contrib-image-tools/nodes/io/image-out.js +25 -3
  10. package/node-red-contrib-image-tools/nodes/mix/advanced-mosaic.js +52 -26
  11. package/node-red-contrib-image-tools/nodes/mix/concat.js +46 -9
  12. package/node-red-contrib-image-tools/nodes/mix/mosaic.js +52 -15
  13. package/node-red-contrib-image-tools/nodes/specialized/cropBB.js +53 -5
  14. package/node-red-contrib-image-tools/nodes/specialized/image-align.js +54 -7
  15. package/node-red-contrib-image-tools/nodes/transform/crop.js +38 -17
  16. package/node-red-contrib-image-tools/nodes/transform/draw.js +28 -7
  17. package/node-red-contrib-image-tools/nodes/transform/filter.js +34 -15
  18. package/node-red-contrib-image-tools/nodes/transform/padding.js +30 -9
  19. package/node-red-contrib-image-tools/nodes/transform/resize.js +58 -33
  20. package/node-red-contrib-image-tools/nodes/transform/rotate.js +40 -20
  21. package/node-red-contrib-image-tools/package.json +1 -8
  22. package/package.json +1 -1
  23. package/rosepetal-image-engine/binding.gyp +5 -23
  24. package/rosepetal-image-engine/src/add-mask.cpp +2 -2
  25. package/rosepetal-image-engine/src/add-masks.cpp +2 -2
  26. package/rosepetal-image-engine/src/advanced-mosaic.cpp +3 -4
  27. package/rosepetal-image-engine/src/blend.cpp +2 -2
  28. package/rosepetal-image-engine/src/concat.cpp +2 -2
  29. package/rosepetal-image-engine/src/crop.cpp +3 -3
  30. package/rosepetal-image-engine/src/draw.cpp +1 -1
  31. package/rosepetal-image-engine/src/filter.cpp +3 -3
  32. package/rosepetal-image-engine/src/mosaic.cpp +4 -5
  33. package/rosepetal-image-engine/src/padding.cpp +4 -3
  34. package/rosepetal-image-engine/src/resize.cpp +101 -23
  35. package/rosepetal-image-engine/src/rotate.cpp +3 -6
  36. package/rosepetal-image-engine/src/utils.h +46 -8
  37. package/docs/nodes/io/array-in.md +0 -119
  38. package/docs/nodes/io/array-out.md +0 -150
  39. package/docs/nodes/io/array-select.md +0 -157
  40. package/docs/nodes/io/queue.md +0 -57
  41. package/docs/nodes/io/save-file.md +0 -30
  42. package/docs/nodes/util/block-detect.md +0 -46
  43. package/node-red-contrib-image-tools/nodes/io/array-in.html +0 -110
  44. package/node-red-contrib-image-tools/nodes/io/array-in.js +0 -113
  45. package/node-red-contrib-image-tools/nodes/io/array-out.html +0 -125
  46. package/node-red-contrib-image-tools/nodes/io/array-out.js +0 -213
  47. package/node-red-contrib-image-tools/nodes/io/array-select.html +0 -156
  48. package/node-red-contrib-image-tools/nodes/io/array-select.js +0 -187
  49. package/node-red-contrib-image-tools/nodes/io/queue.html +0 -119
  50. package/node-red-contrib-image-tools/nodes/io/queue.js +0 -191
  51. package/node-red-contrib-image-tools/nodes/io/save-file.html +0 -243
  52. package/node-red-contrib-image-tools/nodes/io/save-file.js +0 -440
  53. package/node-red-contrib-image-tools/nodes/util/block-detect.html +0 -85
  54. package/node-red-contrib-image-tools/nodes/util/block-detect.js +0 -136
  55. package/node-red-contrib-image-tools/nodes/util/clean-debug.html +0 -201
  56. package/node-red-contrib-image-tools/nodes/util/clean-debug.js +0 -393
@@ -15,10 +15,30 @@ module.exports = function (RED) {
15
15
  node.on('input', async (msg, send, done) => {
16
16
  /* paths */
17
17
  const inPath = cfg.inputPath || 'payload';
18
+ const inputPathType = cfg.inputPathType || 'msg';
18
19
  const outPath = cfg.outputPath || 'payload';
20
+ const outputPathType = cfg.outputPathType || 'msg';
19
21
 
20
22
  /* image / array - capture original for error passthrough */
21
- const originalPayload = RED.util.getMessageProperty(msg, inPath);
23
+ const { value: originalPayload, error: inputErr } =
24
+ NodeUtils.getInputValue(node, msg, inPath, inputPathType);
25
+ if (inputErr) {
26
+ const hint = inputPathType === 'msg'
27
+ ? `Set inputPath to an existing msg property (e.g. "payload"), or ensure "${inPath}" exists before this node.`
28
+ : `Set inputPath to an existing ${inputPathType} context key, or ensure "${inPath}" exists before this node.`;
29
+ return NodeUtils.handleValidationErrorWithPassthrough(
30
+ node,
31
+ {
32
+ message: `Invalid inputPath "${inPath}" (${inputPathType}): ${inputErr.message}`,
33
+ hint,
34
+ details: { inputPath: inPath, inputPathType, outputPath: outPath, outputPathType }
35
+ },
36
+ msg,
37
+ send,
38
+ done,
39
+ { originalPayload: undefined, outputPath: null, outputPathType, outputType: 'preserve' }
40
+ );
41
+ }
22
42
 
23
43
  try {
24
44
  const t0 = performance.now();
@@ -29,14 +49,14 @@ module.exports = function (RED) {
29
49
  if (!NodeUtils.validateListImage(originalPayload, node)) {
30
50
  return NodeUtils.handleValidationErrorWithPassthrough(
31
51
  node, 'Invalid image list structure', msg, send, done,
32
- { originalPayload, outputPath: outPath, outputType: 'preserve' }
52
+ { originalPayload, outputPath: outPath, outputPathType, outputType: 'preserve' }
33
53
  );
34
54
  }
35
55
  } else {
36
56
  if (!NodeUtils.validateSingleImage(originalPayload, node)) {
37
57
  return NodeUtils.handleValidationErrorWithPassthrough(
38
58
  node, 'Invalid image structure', msg, send, done,
39
- { originalPayload, outputPath: outPath, outputType: 'preserve' }
59
+ { originalPayload, outputPath: outPath, outputPathType, outputType: 'preserve' }
40
60
  );
41
61
  }
42
62
  }
@@ -70,30 +90,31 @@ module.exports = function (RED) {
70
90
  return r.image;
71
91
  });
72
92
 
73
- RED.util.setMessageProperty(msg, outPath, Array.isArray(originalPayload) ? outImgs : outImgs[0]);
93
+ NodeUtils.setOutputValue(node, msg, outPath, outputPathType, Array.isArray(originalPayload) ? outImgs : outImgs[0]);
74
94
 
75
95
  // Debug image display
76
96
  const elapsedTime = performance.now() - t0;
77
97
  let debugFormat = null;
78
- if (cfg.debugEnabled) {
98
+ const debugEnabled = cfg.debugEnabled === true || cfg.debugEnabled === 'true';
99
+ if (debugEnabled) {
79
100
  try {
80
101
  // Resolve and validate debug width
81
102
  let debugWidth = NodeUtils.resolveDimension(
82
103
  node,
83
- cfg.debugWidthType,
104
+ cfg.debugWidthType || 'num',
84
105
  cfg.debugWidth,
85
106
  msg
86
107
  );
87
108
  debugWidth = Math.max(1, parseInt(debugWidth) || 200); // Ensure positive, default 200
88
109
 
89
110
  // For arrays, show the first image as representative
90
- const debugImage = Array.isArray(originalPayload) ? outImgs[0] : outImgs[0];
111
+ const debugImage = outImgs[0];
91
112
  const debugResult = await NodeUtils.debugImageDisplay(
92
113
  debugImage,
93
114
  outputFormat,
94
115
  outputQuality,
95
116
  node,
96
- true,
117
+ debugEnabled,
97
118
  debugWidth
98
119
  );
99
120
 
@@ -132,7 +153,7 @@ module.exports = function (RED) {
132
153
  } catch (err) {
133
154
  NodeUtils.handleNodeErrorWithPassthrough(
134
155
  node, err, msg, send, done, 'padding processing',
135
- { originalPayload, outputPath: outPath, outputType: 'preserve' }
156
+ { originalPayload, outputPath: outPath, outputPathType, outputType: 'preserve' }
136
157
  );
137
158
  }
138
159
  });
@@ -14,16 +14,36 @@ module.exports = function (RED) {
14
14
  const node = this;
15
15
 
16
16
  node.on('input', async (msg, send, done) => {
17
+ const inputPath = config.inputPath || 'payload';
18
+ const inputPathType = config.inputPathType || 'msg';
19
+ const outputPath = config.outputPath || 'payload';
20
+ const outputPathType = config.outputPathType || 'msg';
21
+ const { value: originalPayload, error: inputErr } =
22
+ NodeUtils.getInputValue(node, msg, inputPath, inputPathType);
23
+ if (inputErr) {
24
+ const hint = inputPathType === 'msg'
25
+ ? `Set inputPath to an existing msg property (e.g. "payload"), or ensure "${inputPath}" exists before this node.`
26
+ : `Set inputPath to an existing ${inputPathType} context key, or ensure "${inputPath}" exists before this node.`;
27
+ return NodeUtils.handleValidationErrorWithPassthrough(
28
+ node,
29
+ {
30
+ message: `Invalid inputPath "${inputPath}" (${inputPathType}): ${inputErr.message}`,
31
+ hint,
32
+ details: { inputPath, inputPathType, outputPath, outputPathType }
33
+ },
34
+ msg,
35
+ send,
36
+ done,
37
+ { originalPayload: undefined, outputPath: null, outputPathType, outputType: 'preserve' }
38
+ );
39
+ }
40
+
17
41
  try {
18
42
  const startTime = performance.now();
19
43
  node.status({});
20
- const inputPath = config.inputPath || 'payload';
21
- const outputPath = config.outputPath || 'payload';
22
44
  const outputFormat = config.outputFormat || 'raw';
23
45
  const outputQuality = parseInt(config.outputQuality) || 90;
24
46
  const pngOptimize = config.pngOptimize || false;
25
-
26
- const originalPayload = RED.util.getMessageProperty(msg, inputPath);
27
47
  const inputList = Array.isArray(originalPayload)
28
48
  ? originalPayload
29
49
  : [originalPayload];
@@ -33,47 +53,51 @@ module.exports = function (RED) {
33
53
  if (!NodeUtils.validateListImage(originalPayload, node)) {
34
54
  return NodeUtils.handleValidationErrorWithPassthrough(
35
55
  node, 'Invalid image list structure', msg, send, done,
36
- { originalPayload, outputPath, outputType: 'preserve' }
56
+ { originalPayload, outputPath, outputPathType, outputType: 'preserve' }
37
57
  );
38
58
  }
39
59
  } else {
40
60
  if (!NodeUtils.validateSingleImage(originalPayload, node)) {
41
61
  return NodeUtils.handleValidationErrorWithPassthrough(
42
62
  node, 'Invalid image structure', msg, send, done,
43
- { originalPayload, outputPath, outputType: 'preserve' }
63
+ { originalPayload, outputPath, outputPathType, outputType: 'preserve' }
44
64
  );
45
65
  }
46
66
  }
47
67
 
48
- const promises = inputList.map((inputImage) => {
49
- // Resolve dimension values (can come from msg/flow/global)
50
- let wVal = NodeUtils.resolveDimension(
51
- node,
52
- config.widthType,
53
- config.widthValue,
54
- msg
55
- );
56
- let hVal = NodeUtils.resolveDimension(
57
- node,
58
- config.heightType,
59
- config.heightValue,
60
- msg
61
- );
68
+ // Resolve dimension values once per message (can come from msg/flow/global)
69
+ let wVal = NodeUtils.resolveDimension(
70
+ node,
71
+ config.widthType,
72
+ config.widthValue,
73
+ msg
74
+ );
75
+ let hVal = NodeUtils.resolveDimension(
76
+ node,
77
+ config.heightType,
78
+ config.heightValue,
79
+ msg
80
+ );
81
+
82
+ // Convert to Number; use 0 as "Auto" sentinel for the native addon.
83
+ // (NaN is unsafe with -ffast-math builds and can lead to huge allocations.)
84
+ wVal = wVal === null || wVal === '' ? 0 : Number(wVal);
85
+ hVal = hVal === null || hVal === '' ? 0 : Number(hVal);
62
86
 
63
- // Convert to Number or NaN (C++ interprets NaN as "Auto")
64
- wVal = wVal === null || wVal === '' ? NaN : Number(wVal);
65
- hVal = hVal === null || hVal === '' ? NaN : Number(hVal);
87
+ if (!Number.isFinite(wVal) || !Number.isFinite(hVal)) {
88
+ throw new Error('Resize width/height must be finite numbers');
89
+ }
66
90
 
67
- // Direct call to addon: (image, wMode, wVal, hMode, hVal, outputFormat, quality, pngOptimize)
68
- return CppProcessor.resize(
91
+ const promises = inputList.map((inputImage) =>
92
+ CppProcessor.resize(
69
93
  inputImage,
70
94
  config.widthMode, wVal,
71
95
  config.heightMode, hVal,
72
96
  outputFormat,
73
97
  outputQuality,
74
98
  pngOptimize
75
- );
76
- });
99
+ )
100
+ );
77
101
  const results = await Promise.all(promises);
78
102
 
79
103
  // Aggregate timings and prepare output
@@ -94,25 +118,26 @@ module.exports = function (RED) {
94
118
 
95
119
  // Debug image display
96
120
  let debugFormat = null;
97
- if (config.debugEnabled) {
121
+ const debugEnabled = config.debugEnabled === true || config.debugEnabled === 'true';
122
+ if (debugEnabled) {
98
123
  try {
99
124
  // Resolve and validate debug width
100
125
  let debugWidth = NodeUtils.resolveDimension(
101
126
  node,
102
- config.debugWidthType,
127
+ config.debugWidthType || 'num',
103
128
  config.debugWidth,
104
129
  msg
105
130
  );
106
131
  debugWidth = Math.max(1, parseInt(debugWidth) || 200); // Ensure positive, default 200
107
132
 
108
133
  // For arrays, show the first image as representative
109
- const debugImage = Array.isArray(originalPayload) ? images[0] : images[0];
134
+ const debugImage = images[0];
110
135
  const debugResult = await NodeUtils.debugImageDisplay(
111
136
  debugImage,
112
137
  outputFormat,
113
138
  outputQuality,
114
139
  node,
115
- true,
140
+ debugEnabled,
116
141
  debugWidth
117
142
  );
118
143
 
@@ -145,14 +170,14 @@ module.exports = function (RED) {
145
170
  taskMs: totalTaskMs
146
171
  }, elapsedTime);
147
172
 
148
- RED.util.setMessageProperty(msg, outputPath, out);
173
+ NodeUtils.setOutputValue(node, msg, outputPath, outputPathType, out);
149
174
 
150
175
  send(msg);
151
176
  done && done();
152
177
  } catch (err) {
153
178
  NodeUtils.handleNodeErrorWithPassthrough(
154
179
  node, err, msg, send, done, 'resize processing',
155
- { originalPayload, outputPath, outputType: 'preserve' }
180
+ { originalPayload, outputPath, outputPathType, outputType: 'preserve' }
156
181
  );
157
182
  }
158
183
  });
@@ -12,46 +12,65 @@ module.exports = function (RED) {
12
12
  const node = this;
13
13
 
14
14
  node.on('input', async (msg, send, done) => {
15
+ const inputPath = config.inputPath || 'payload';
16
+ const inputPathType = config.inputPathType || 'msg';
17
+ const outputPath = config.outputPath || 'payload';
18
+ const outputPathType = config.outputPathType || 'msg';
19
+ const { value: originalPayload, error: inputErr } =
20
+ NodeUtils.getInputValue(node, msg, inputPath, inputPathType);
21
+ if (inputErr) {
22
+ const hint = inputPathType === 'msg'
23
+ ? `Set inputPath to an existing msg property (e.g. "payload"), or ensure "${inputPath}" exists before this node.`
24
+ : `Set inputPath to an existing ${inputPathType} context key, or ensure "${inputPath}" exists before this node.`;
25
+ return NodeUtils.handleValidationErrorWithPassthrough(
26
+ node,
27
+ {
28
+ message: `Invalid inputPath "${inputPath}" (${inputPathType}): ${inputErr.message}`,
29
+ hint,
30
+ details: { inputPath, inputPathType, outputPath, outputPathType }
31
+ },
32
+ msg,
33
+ send,
34
+ done,
35
+ { originalPayload: undefined, outputPath: null, outputPathType, outputType: 'preserve' }
36
+ );
37
+ }
38
+
15
39
  try {
16
40
  const t0 = performance.now();
17
41
  node.status({}); // clear
18
-
19
- const inputPath = config.inputPath || 'payload';
20
- const outputPath = config.outputPath || 'payload';
21
42
  const outputFormat = config.outputFormat || 'raw';
22
43
  const outputQuality = parseInt(config.outputQuality) || 90;
23
44
  const pngOptimize = config.pngOptimize || false;
24
45
  const padColorHex = config.padColor || '#000000';
25
46
 
26
- const originalPayload = RED.util.getMessageProperty(msg, inputPath);
27
-
28
47
  // Validate input images with error passthrough
29
48
  if (Array.isArray(originalPayload)) {
30
49
  if (!NodeUtils.validateListImage(originalPayload, node)) {
31
50
  return NodeUtils.handleValidationErrorWithPassthrough(
32
51
  node, 'Invalid image list structure', msg, send, done,
33
- { originalPayload, outputPath, outputType: 'preserve' }
52
+ { originalPayload, outputPath, outputPathType, outputType: 'preserve' }
34
53
  );
35
54
  }
36
55
  } else {
37
56
  if (!NodeUtils.validateSingleImage(originalPayload, node)) {
38
57
  return NodeUtils.handleValidationErrorWithPassthrough(
39
58
  node, 'Invalid image structure', msg, send, done,
40
- { originalPayload, outputPath, outputType: 'preserve' }
59
+ { originalPayload, outputPath, outputPathType, outputType: 'preserve' }
41
60
  );
42
61
  }
43
62
  }
44
63
 
45
64
  const imgs = Array.isArray(originalPayload) ? originalPayload : [originalPayload];
46
65
 
47
- /* ——— lanzar rotaciones en paralelo ——— */
48
- const promises = imgs.map(img => {
49
- let angle = NodeUtils.resolveDimension(
50
- node, config.angleType, config.angleValue, msg);
51
- angle = angle === null || angle === '' ? 0 : Number(angle);
66
+ let angle = NodeUtils.resolveDimension(
67
+ node, config.angleType, config.angleValue, msg);
68
+ angle = angle === null || angle === '' ? 0 : Number(angle);
52
69
 
53
- return CppProcessor.rotate(img, angle, padColorHex, outputFormat, outputQuality, pngOptimize);
54
- });
70
+ /* ——— lanzar rotaciones en paralelo ——— */
71
+ const promises = imgs.map(img =>
72
+ CppProcessor.rotate(img, angle, padColorHex, outputFormat, outputQuality, pngOptimize)
73
+ );
55
74
 
56
75
  const results = await Promise.all(promises);
57
76
 
@@ -68,29 +87,30 @@ module.exports = function (RED) {
68
87
  const out = Array.isArray(originalPayload) ? images : images[0];
69
88
  const durMs = performance.now() - t0;
70
89
 
71
- RED.util.setMessageProperty(msg, outputPath, out);
90
+ NodeUtils.setOutputValue(node, msg, outputPath, outputPathType, out);
72
91
 
73
92
  // Debug image display
74
93
  let debugFormat = null;
75
- if (config.debugEnabled) {
94
+ const debugEnabled = config.debugEnabled === true || config.debugEnabled === 'true';
95
+ if (debugEnabled) {
76
96
  try {
77
97
  // Resolve and validate debug width
78
98
  let debugWidth = NodeUtils.resolveDimension(
79
99
  node,
80
- config.debugWidthType,
100
+ config.debugWidthType || 'num',
81
101
  config.debugWidth,
82
102
  msg
83
103
  );
84
104
  debugWidth = Math.max(1, parseInt(debugWidth) || 200); // Ensure positive, default 200
85
105
 
86
106
  // For arrays, show the first image as representative
87
- const debugImage = Array.isArray(originalPayload) ? images[0] : images[0];
107
+ const debugImage = images[0];
88
108
  const debugResult = await NodeUtils.debugImageDisplay(
89
109
  debugImage,
90
110
  outputFormat,
91
111
  outputQuality,
92
112
  node,
93
- true,
113
+ debugEnabled,
94
114
  debugWidth
95
115
  );
96
116
 
@@ -131,7 +151,7 @@ module.exports = function (RED) {
131
151
  } catch (err) {
132
152
  NodeUtils.handleNodeErrorWithPassthrough(
133
153
  node, err, msg, send, done, 'rotate processing',
134
- { originalPayload, outputPath, outputType: 'preserve' }
154
+ { originalPayload, outputPath, outputPathType, outputType: 'preserve' }
135
155
  );
136
156
  }
137
157
  });
@@ -33,11 +33,6 @@
33
33
  "image-in": "nodes/io/image-in.js",
34
34
  "folder-in": "nodes/io/folder-in.js",
35
35
  "image-out": "nodes/io/image-out.js",
36
- "array-in": "nodes/io/array-in.js",
37
- "array-out": "nodes/io/array-out.js",
38
- "array-select": "nodes/io/array-select.js",
39
- "queue": "nodes/io/queue.js",
40
- "save-file": "nodes/io/save-file.js",
41
36
  "cropBB": "nodes/specialized/cropBB.js",
42
37
  "image-align": "nodes/specialized/image-align.js",
43
38
  "resize": "nodes/transform/resize.js",
@@ -52,9 +47,7 @@
52
47
  "blend": "nodes/blend/blend.js",
53
48
  "add-mask": "nodes/blend/add-mask.js",
54
49
  "add-masks": "nodes/blend/add-masks.js",
55
- "add-bbs": "nodes/blend/add-bbs.js",
56
- "clean-debug": "nodes/util/clean-debug.js",
57
- "block-detect": "nodes/util/block-detect.js"
50
+ "add-bbs": "nodes/blend/add-bbs.js"
58
51
  }
59
52
  },
60
53
  "dependencies": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rosepetal/node-red-contrib-image-tools",
3
- "version": "1.1.3",
3
+ "version": "1.2.0",
4
4
  "description": "Node-RED Image Processing Toolkit",
5
5
  "main": "install.sh",
6
6
  "directories": {
@@ -101,39 +101,23 @@
101
101
  ]
102
102
  }, {
103
103
  "include_dirs": [
104
- "/usr/include/opencv4",
104
+ "<!@(pkg-config --cflags-only-I opencv4 | sed s/-I//g)",
105
105
  "<!@(node -p \"require('node-addon-api').include\")"
106
106
  ],
107
107
  "libraries": [
108
- "<!@(pkg-config --libs --cflags opencv4)"
108
+ "<!@(pkg-config --libs opencv4)"
109
109
  ],
110
110
  "cflags_cc": [
111
111
  "-std=c++17",
112
112
  "-O3",
113
- "-ffast-math",
114
- "-march=native",
115
113
  "-fexceptions",
116
114
  "-frtti",
117
115
  "-fno-omit-frame-pointer",
118
- "-funroll-loops",
119
- "-fstrict-aliasing",
120
- "-fopenmp",
121
- "-mavx2",
122
- "-ftree-vectorize",
123
- "-fprefetch-loop-arrays",
124
- "-finline-functions",
125
- "-flto",
126
- "-fipa-pta",
127
- "-fvect-cost-model=cheap",
128
- "-minline-all-stringops"
116
+ "-fopenmp"
129
117
  ],
130
118
  "ldflags": [
131
119
  "-O3",
132
- "-march=native",
133
- "-fopenmp",
134
- "-lgomp",
135
- "-flto",
136
- "-fuse-linker-plugin"
120
+ "-fopenmp"
137
121
  ],
138
122
  "xcode_settings": {
139
123
  "GCC_ENABLE_CPP_EXCEPTIONS": "YES",
@@ -141,9 +125,7 @@
141
125
  "OTHER_CPLUSPLUSFLAGS": [
142
126
  "-std=c++17",
143
127
  "-O3",
144
- "-ffast-math",
145
- "-march=native",
146
- "-funroll-loops"
128
+ "-march=native"
147
129
  ]
148
130
  }
149
131
  }]
@@ -153,7 +153,7 @@ protected:
153
153
 
154
154
  // Multi-format encoding if needed
155
155
  if (outputFormat != "raw") {
156
- cv::Mat tmp = ToBgrForJpg(result, outputChannel);
156
+ cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
157
157
  encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
158
158
  }
159
159
  }
@@ -237,4 +237,4 @@ Napi::Value AddMask(const Napi::CallbackInfo& info) {
237
237
  ))->Queue();
238
238
 
239
239
  return env.Undefined();
240
- }
240
+ }
@@ -472,7 +472,7 @@ protected:
472
472
  taskMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
473
473
 
474
474
  if (outputFormat != "raw") {
475
- cv::Mat tmp = ToBgrForJpg(result, outputChannel);
475
+ cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
476
476
  encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
477
477
  }
478
478
  return;
@@ -560,7 +560,7 @@ protected:
560
560
 
561
561
  // Multi-format encoding if needed
562
562
  if (outputFormat != "raw") {
563
- cv::Mat tmp = ToBgrForJpg(result, outputChannel);
563
+ cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
564
564
  encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
565
565
  }
566
566
  }
@@ -303,9 +303,8 @@ protected:
303
303
 
304
304
  /* ─ SUPER FAST multi-format encoding (optional) ─ */
305
305
  if (outputFormat_ != "raw") {
306
- // Convert to BGR format for encoding if needed
307
- const cv::Mat& srcForEncoding = (canvasChannel_ == "BGR") ? canvas_
308
- : ToBgrForJpg(canvas_, canvasChannel_);
306
+ const cv::Mat srcForEncoding =
307
+ PrepareForEncoding(canvas_, canvasChannel_, outputFormat_);
309
308
  encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat_, quality_, pngOptimize_);
310
309
  }
311
310
  }
@@ -316,7 +315,7 @@ protected:
316
315
  // Zero-copy output creation with correct channel format
317
316
  Napi::Value jsImg = (outputFormat_ != "raw")
318
317
  ? VectorToBuffer(env, std::move(encodedBuf_)) // Zero-copy encoded
319
- : MatToRawJS(env, canvas_.clone(), canvasChannel_); // Contiguous raw with correct channel format
318
+ : MatToRawJS(env, canvas_, canvasChannel_);
320
319
 
321
320
  Napi::Object result = Napi::Object::New(env);
322
321
  result.Set("image", jsImg);
@@ -96,7 +96,7 @@ protected:
96
96
 
97
97
  // Multi-format encoding if needed
98
98
  if (outputFormat != "raw") {
99
- cv::Mat tmp = ToBgrForJpg(result, outputChannel);
99
+ cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
100
100
  encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
101
101
  }
102
102
  }
@@ -197,4 +197,4 @@ Napi::Value Blend(const Napi::CallbackInfo& info) {
197
197
  (new BlendWorker(info[cbIdx].As<Napi::Function>(), jsImg1, jsImg2, opacity, outputFormat, quality, pngOptimize,
198
198
  alphaCompositing, removeBackground, backgroundColor, colorTolerance))->Queue();
199
199
  return env.Undefined();
200
- }
200
+ }
@@ -243,7 +243,7 @@ protected:
243
243
 
244
244
  // Multi-format encoding if needed
245
245
  if (outputFormat != "raw") {
246
- cv::Mat tmp = ToBgrForJpg(result, outputChannel);
246
+ cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
247
247
  encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
248
248
  }
249
249
  }
@@ -326,4 +326,4 @@ Napi::Value Concat(const Napi::CallbackInfo& info) {
326
326
  // Create and queue worker
327
327
  (new ConcatWorker(info[cbIdx].As<Napi::Function>(), raw, dir, st, pad, outputFormat, quality, pngOptimize))->Queue();
328
328
  return env.Undefined();
329
- }
329
+ }
@@ -64,8 +64,8 @@ protected:
64
64
 
65
65
  /* ─ Multi-format encoding (encodeMs) ─ */
66
66
  if(outputFormat_ != "raw"){
67
- const cv::Mat& srcForEncoding = (channel_=="BGR")? result_
68
- : ToBgrForJpg(result_,channel_);
67
+ const cv::Mat srcForEncoding =
68
+ PrepareForEncoding(result_, channel_, outputFormat_);
69
69
  encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat_, quality_, pngOptimize_);
70
70
  }
71
71
  }
@@ -74,7 +74,7 @@ protected:
74
74
  Napi::Env env = Env();
75
75
  Napi::Value jsImg = (outputFormat_ != "raw")
76
76
  ? VectorToBuffer(env,std::move(encodedBuf_)) // 0-copy
77
- : MatToRawJS(env,result_.clone(),channel_); // contiguo
77
+ : MatToRawJS(env,result_,channel_);
78
78
 
79
79
  Napi::Object out = Napi::Object::New(env);
80
80
  out.Set("image", jsImg);
@@ -202,7 +202,7 @@ protected:
202
202
  }
203
203
 
204
204
  if (outputFormat != "raw") {
205
- cv::Mat encodeSrc = ToBgrForJpg(resultImage, imageChannelOrder);
205
+ cv::Mat encodeSrc = PrepareForEncoding(resultImage, imageChannelOrder, outputFormat);
206
206
  encodeMs = EncodeToFormat(encodeSrc, encodedBuffer, outputFormat, quality, pngOptimize);
207
207
  }
208
208
 
@@ -76,8 +76,8 @@ protected:
76
76
 
77
77
  // Multi-format encoding
78
78
  if (outputFormat_ != "raw") {
79
- const cv::Mat& srcForEncoding = (channel_ == "BGR") ? result_
80
- : ToBgrForJpg(result_, channel_);
79
+ const cv::Mat srcForEncoding =
80
+ PrepareForEncoding(result_, channel_, outputFormat_);
81
81
  encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat_, quality_, pngOptimize_);
82
82
  }
83
83
  } catch (const std::exception& e) {
@@ -227,4 +227,4 @@ Napi::Value Filter(const Napi::CallbackInfo& info) {
227
227
  // Create and queue worker
228
228
  (new FilterWorker(cb, img, filterType, kernelSize, intensity, outputFormat, quality, pngOptimize))->Queue();
229
229
  return env.Undefined();
230
- }
230
+ }
@@ -145,9 +145,8 @@ protected:
145
145
 
146
146
  /* ─ SUPER FAST multi-format encoding (optional) ─ */
147
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_);
148
+ const cv::Mat srcForEncoding =
149
+ PrepareForEncoding(canvas_, canvasChannel_, outputFormat_);
151
150
  encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat_, quality_, pngOptimize_);
152
151
  }
153
152
  }
@@ -158,7 +157,7 @@ protected:
158
157
  // Zero-copy output creation with correct channel format
159
158
  Napi::Value jsImg = (outputFormat_ != "raw")
160
159
  ? VectorToBuffer(env, std::move(encodedBuf_)) // Zero-copy encoded
161
- : MatToRawJS(env, canvas_.clone(), canvasChannel_); // Contiguous raw with correct channel format
160
+ : MatToRawJS(env, canvas_, canvasChannel_);
162
161
 
163
162
  Napi::Object result = Napi::Object::New(env);
164
163
  result.Set("image", jsImg);
@@ -370,4 +369,4 @@ Napi::Value Mosaic(const Napi::CallbackInfo& info) {
370
369
  backgroundColor, positions, normalized, outputFormat, quality, pngOptimize))->Queue();
371
370
 
372
371
  return env.Undefined();
373
- }
372
+ }
@@ -55,9 +55,10 @@ protected:
55
55
  cv::copyMakeBorder(src_,dst_,t_,b_,l_,r_,cv::BORDER_CONSTANT,padClrImg);
56
56
  taskMs_=(cv::getTickCount()-t0)/cv::getTickFrequency()*1e3;
57
57
 
58
- if(outputFormat != "raw"){
59
- const cv::Mat& srcForEncoding=(channel_=="BGR")?dst_:ToBgrForJpg(dst_,channel_);
60
- encodeMs_=EncodeToFormat(srcForEncoding,encodedBuf_,outputFormat,quality,pngOptimize);
58
+ if (outputFormat != "raw") {
59
+ const cv::Mat srcForEncoding =
60
+ PrepareForEncoding(dst_, channel_, outputFormat);
61
+ encodeMs_ = EncodeToFormat(srcForEncoding, encodedBuf_, outputFormat, quality, pngOptimize);
61
62
  }
62
63
  }
63
64