@rosepetal/node-red-contrib-image-tools 1.2.0 → 1.2.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 (48) hide show
  1. package/node-red-contrib-image-tools/lib/node-utils.js +64 -0
  2. package/node-red-contrib-image-tools/nodes/blend/add-bbs.html +23 -0
  3. package/node-red-contrib-image-tools/nodes/blend/add-bbs.js +8 -2
  4. package/node-red-contrib-image-tools/nodes/blend/add-mask.html +24 -1
  5. package/node-red-contrib-image-tools/nodes/blend/add-mask.js +13 -7
  6. package/node-red-contrib-image-tools/nodes/blend/add-masks.html +23 -0
  7. package/node-red-contrib-image-tools/nodes/blend/add-masks.js +8 -2
  8. package/node-red-contrib-image-tools/nodes/blend/blend.html +24 -1
  9. package/node-red-contrib-image-tools/nodes/blend/blend.js +8 -2
  10. package/node-red-contrib-image-tools/nodes/mix/advanced-mosaic.html +23 -0
  11. package/node-red-contrib-image-tools/nodes/mix/advanced-mosaic.js +9 -3
  12. package/node-red-contrib-image-tools/nodes/mix/concat.html +24 -1
  13. package/node-red-contrib-image-tools/nodes/mix/concat.js +8 -2
  14. package/node-red-contrib-image-tools/nodes/mix/mosaic.html +23 -0
  15. package/node-red-contrib-image-tools/nodes/mix/mosaic.js +8 -2
  16. package/node-red-contrib-image-tools/nodes/specialized/cropBB.html +24 -1
  17. package/node-red-contrib-image-tools/nodes/specialized/cropBB.js +9 -1
  18. package/node-red-contrib-image-tools/nodes/specialized/image-align.html +25 -2
  19. package/node-red-contrib-image-tools/nodes/specialized/image-align.js +7 -1
  20. package/node-red-contrib-image-tools/nodes/transform/color-convert.html +213 -0
  21. package/node-red-contrib-image-tools/nodes/transform/color-convert.js +161 -0
  22. package/node-red-contrib-image-tools/nodes/transform/crop.html +25 -1
  23. package/node-red-contrib-image-tools/nodes/transform/crop.js +9 -1
  24. package/node-red-contrib-image-tools/nodes/transform/draw.html +24 -0
  25. package/node-red-contrib-image-tools/nodes/transform/draw.js +8 -2
  26. package/node-red-contrib-image-tools/nodes/transform/filter.html +25 -1
  27. package/node-red-contrib-image-tools/nodes/transform/filter.js +9 -1
  28. package/node-red-contrib-image-tools/nodes/transform/padding.html +25 -1
  29. package/node-red-contrib-image-tools/nodes/transform/padding.js +9 -1
  30. package/node-red-contrib-image-tools/nodes/transform/resize.html +26 -2
  31. package/node-red-contrib-image-tools/nodes/transform/resize.js +9 -1
  32. package/node-red-contrib-image-tools/nodes/transform/rotate.html +30 -6
  33. package/node-red-contrib-image-tools/nodes/transform/rotate.js +9 -1
  34. package/node-red-contrib-image-tools/package.json +1 -0
  35. package/package.json +2 -1
  36. package/rosepetal-image-engine/binding.gyp +2 -1
  37. package/rosepetal-image-engine/src/advanced-mosaic.cpp +4 -4
  38. package/rosepetal-image-engine/src/color-convert.cpp +184 -0
  39. package/rosepetal-image-engine/src/concat.cpp +2 -2
  40. package/rosepetal-image-engine/src/crop.cpp +4 -4
  41. package/rosepetal-image-engine/src/filter.cpp +4 -4
  42. package/rosepetal-image-engine/src/image-align.cpp +1 -1
  43. package/rosepetal-image-engine/src/main.cpp +2 -0
  44. package/rosepetal-image-engine/src/mosaic.cpp +4 -4
  45. package/rosepetal-image-engine/src/padding.cpp +4 -4
  46. package/rosepetal-image-engine/src/resize.cpp +4 -4
  47. package/rosepetal-image-engine/src/rotate.cpp +6 -6
  48. package/rosepetal-image-engine/src/utils.h +2 -2
@@ -0,0 +1,184 @@
1
+ // ───────── src/color-convert.cpp ───────────────────────────────────────────
2
+ #include <napi.h>
3
+ #include <opencv2/opencv.hpp>
4
+ #include <chrono>
5
+ #include <string>
6
+ #include <unordered_map>
7
+ #include "utils.h"
8
+
9
+ // ──────────────────────────────── Lookup key helper
10
+ static inline std::string MakeKey(const std::string& src, const std::string& dst) {
11
+ return src + ">" + dst;
12
+ }
13
+
14
+ // ──────────────────────────────── Complete conversion table (20 paths)
15
+ static const std::unordered_map<std::string, int>& ConversionTable() {
16
+ static const std::unordered_map<std::string, int> table = {
17
+ // GRAY →
18
+ { MakeKey("GRAY","BGR"), cv::COLOR_GRAY2BGR },
19
+ { MakeKey("GRAY","RGB"), cv::COLOR_GRAY2RGB },
20
+ { MakeKey("GRAY","BGRA"), cv::COLOR_GRAY2BGRA },
21
+ { MakeKey("GRAY","RGBA"), cv::COLOR_GRAY2RGBA },
22
+ // BGR →
23
+ { MakeKey("BGR","GRAY"), cv::COLOR_BGR2GRAY },
24
+ { MakeKey("BGR","RGB"), cv::COLOR_BGR2RGB },
25
+ { MakeKey("BGR","BGRA"), cv::COLOR_BGR2BGRA },
26
+ { MakeKey("BGR","RGBA"), cv::COLOR_BGR2RGBA },
27
+ // RGB →
28
+ { MakeKey("RGB","GRAY"), cv::COLOR_RGB2GRAY },
29
+ { MakeKey("RGB","BGR"), cv::COLOR_RGB2BGR },
30
+ { MakeKey("RGB","BGRA"), cv::COLOR_RGB2BGRA },
31
+ { MakeKey("RGB","RGBA"), cv::COLOR_RGB2RGBA },
32
+ // BGRA →
33
+ { MakeKey("BGRA","GRAY"), cv::COLOR_BGRA2GRAY },
34
+ { MakeKey("BGRA","BGR"), cv::COLOR_BGRA2BGR },
35
+ { MakeKey("BGRA","RGB"), cv::COLOR_BGRA2RGB },
36
+ { MakeKey("BGRA","RGBA"), cv::COLOR_BGRA2RGBA },
37
+ // RGBA →
38
+ { MakeKey("RGBA","GRAY"), cv::COLOR_RGBA2GRAY },
39
+ { MakeKey("RGBA","BGR"), cv::COLOR_RGBA2BGR },
40
+ { MakeKey("RGBA","RGB"), cv::COLOR_RGBA2RGB },
41
+ { MakeKey("RGBA","BGRA"), cv::COLOR_RGBA2BGRA },
42
+ };
43
+ return table;
44
+ }
45
+
46
+ // ──────────────────────────────── Worker
47
+ class ColorConvertWorker : public Napi::AsyncWorker {
48
+ public:
49
+ ColorConvertWorker(Napi::Function& cb,
50
+ const Napi::Value& imgVal,
51
+ std::string targetColorSpace,
52
+ std::string outputFormat,
53
+ int quality = 90,
54
+ bool pngOptimize = false)
55
+ : Napi::AsyncWorker(cb),
56
+ targetCS(std::move(targetColorSpace)),
57
+ outputFormat(std::move(outputFormat)),
58
+ quality(quality),
59
+ pngOptimize(pngOptimize)
60
+ {
61
+ try {
62
+ inputMat = ConvertToMat(imgVal);
63
+
64
+ // Detect source color space
65
+ if (imgVal.IsObject() && !imgVal.IsBuffer()) {
66
+ Napi::Object obj = imgVal.As<Napi::Object>();
67
+ if (obj.Has("colorSpace")) {
68
+ sourceCS = obj.Get("colorSpace").As<Napi::String>().Utf8Value();
69
+ } else {
70
+ sourceCS = (inputMat.channels() == 4) ? "RGBA"
71
+ : (inputMat.channels() == 3) ? "RGB"
72
+ : "GRAY";
73
+ }
74
+ } else {
75
+ sourceCS = (inputMat.channels() == 4) ? "RGBA"
76
+ : (inputMat.channels() == 3) ? "RGB"
77
+ : "GRAY";
78
+ }
79
+ } catch (const Napi::Error& e) { SetError(e.Message()); }
80
+ }
81
+
82
+ protected:
83
+ void Execute() override {
84
+ try {
85
+ auto t0 = std::chrono::steady_clock::now();
86
+
87
+ // Identity fast-path: same source and target
88
+ if (sourceCS == targetCS) {
89
+ resultMat = inputMat;
90
+ } else {
91
+ const std::string key = MakeKey(sourceCS, targetCS);
92
+ const auto& table = ConversionTable();
93
+ auto it = table.find(key);
94
+ if (it == table.end()) {
95
+ SetError("Unsupported color space conversion: " + sourceCS + " -> " + targetCS);
96
+ return;
97
+ }
98
+ cv::cvtColor(inputMat, resultMat, it->second);
99
+ }
100
+
101
+ taskMs = std::chrono::duration<double, std::milli>(
102
+ std::chrono::steady_clock::now() - t0).count();
103
+
104
+ // Multi-format encoding
105
+ if (outputFormat != "raw") {
106
+ const cv::Mat srcForEncoding =
107
+ PrepareForEncoding(resultMat, targetCS, outputFormat);
108
+ encodeMs = EncodeToFormat(srcForEncoding, encodedBuf, outputFormat, quality, pngOptimize);
109
+ }
110
+ } catch (const std::exception& e) { SetError(e.what()); }
111
+ }
112
+
113
+ void OnOK() override {
114
+ Napi::Env env = Env();
115
+ Napi::Value jsImg = (outputFormat != "raw")
116
+ ? VectorToBuffer(env, std::move(encodedBuf))
117
+ : MatToRawJS(env, resultMat, targetCS);
118
+
119
+ Napi::Object res = Napi::Object::New(env);
120
+ res.Set("image", jsImg);
121
+ res.Set("timing", MakeTimingJS(env, 0.0, taskMs, encodeMs));
122
+ Callback().Call({ env.Null(), res });
123
+ }
124
+
125
+ void OnError(const Napi::Error& e) override {
126
+ Callback().Call({ e.Value(), Env().Null() });
127
+ }
128
+
129
+ private:
130
+ cv::Mat inputMat, resultMat;
131
+ std::string sourceCS;
132
+ std::string targetCS;
133
+ std::string outputFormat;
134
+ int quality;
135
+ bool pngOptimize;
136
+
137
+ double taskMs = 0.0;
138
+ double encodeMs = 0.0;
139
+ std::vector<uchar> encodedBuf;
140
+ };
141
+
142
+ // ───────── Binding JS → C++ ────────────────────────────────────────────
143
+ // colorConvert(image, targetColorSpace, outputFormat, quality, pngOptimize, callback)
144
+ Napi::Value ColorConvert(const Napi::CallbackInfo& info)
145
+ {
146
+ Napi::Env env = info.Env();
147
+ if (info.Length() < 3 || info.Length() > 6 ||
148
+ !info[info.Length()-1].IsFunction())
149
+ return Napi::TypeError::New(env,
150
+ "colorConvert(image, targetColorSpace, [outputFormat], [quality], [pngOptimize], callback)").Value();
151
+
152
+ int i = 0;
153
+ Napi::Value img = info[i++];
154
+ std::string targetColorSpace = info[i++].As<Napi::String>().Utf8Value();
155
+
156
+ // Handle optional parameters
157
+ std::string outputFormat = "raw";
158
+ int quality = 90;
159
+ bool pngOptimize = false;
160
+
161
+ if (info.Length() - i >= 2) {
162
+ outputFormat = info[i++].As<Napi::String>().Utf8Value();
163
+ }
164
+
165
+ if (info.Length() - i >= 2) {
166
+ quality = info[i++].As<Napi::Number>().Int32Value();
167
+ }
168
+
169
+ if (info.Length() - i >= 2) {
170
+ pngOptimize = info[i++].As<Napi::Boolean>().Value();
171
+ }
172
+
173
+ Napi::Function cb = info[i].As<Napi::Function>();
174
+
175
+ auto* worker = new ColorConvertWorker(
176
+ cb,
177
+ img,
178
+ targetColorSpace,
179
+ outputFormat,
180
+ quality,
181
+ pngOptimize);
182
+ worker->Queue();
183
+ return env.Undefined();
184
+ }
@@ -19,12 +19,12 @@ std::string DetectChannelFormat(const Napi::Value& jsImg, const cv::Mat& mat) {
19
19
  // Default based on channel count
20
20
  else {
21
21
  const int channels = mat.channels();
22
- return (channels == 4) ? "BGRA" : (channels == 3) ? "BGR" : "GRAY";
22
+ return (channels == 4) ? "RGBA" : (channels == 3) ? "RGB" : "GRAY";
23
23
  }
24
24
  } else {
25
25
  // Buffer input - determine from OpenCV Mat
26
26
  const int channels = mat.channels();
27
- return (channels == 4) ? "BGRA" : (channels == 3) ? "BGR" : "GRAY";
27
+ return (channels == 4) ? "RGBA" : (channels == 3) ? "RGB" : "GRAY";
28
28
  }
29
29
  }
30
30
 
@@ -29,14 +29,14 @@ public:
29
29
  }
30
30
  // Default based on channel count
31
31
  else {
32
- channel_ = (input_.channels() == 4) ? "BGRA"
33
- : (input_.channels() == 3) ? "BGR"
32
+ channel_ = (input_.channels() == 4) ? "RGBA"
33
+ : (input_.channels() == 3) ? "RGB"
34
34
  : "GRAY";
35
35
  }
36
36
  } else {
37
37
  // Buffer input - determine from OpenCV Mat
38
- channel_ = (input_.channels() == 4) ? "BGRA"
39
- : (input_.channels() == 3) ? "BGR"
38
+ channel_ = (input_.channels() == 4) ? "RGBA"
39
+ : (input_.channels() == 3) ? "RGB"
40
40
  : "GRAY";
41
41
  }
42
42
  }
@@ -40,14 +40,14 @@ public:
40
40
  }
41
41
  // Default based on channel count
42
42
  else {
43
- channel_ = (input_.channels() == 4) ? "BGRA"
44
- : (input_.channels() == 3) ? "BGR"
43
+ channel_ = (input_.channels() == 4) ? "RGBA"
44
+ : (input_.channels() == 3) ? "RGB"
45
45
  : "GRAY";
46
46
  }
47
47
  } else {
48
48
  // Buffer input - determine from OpenCV Mat
49
- channel_ = (input_.channels() == 4) ? "BGRA"
50
- : (input_.channels() == 3) ? "BGR"
49
+ channel_ = (input_.channels() == 4) ? "RGBA"
50
+ : (input_.channels() == 3) ? "RGB"
51
51
  : "GRAY";
52
52
  }
53
53
  }
@@ -322,7 +322,7 @@ private:
322
322
  }
323
323
  // Default based on channel count
324
324
  const int channels = mat.channels();
325
- return (channels == 4) ? "BGRA" : (channels == 3) ? "BGR" : "GRAY";
325
+ return (channels == 4) ? "RGBA" : (channels == 3) ? "RGB" : "GRAY";
326
326
  }
327
327
 
328
328
  // Convert image to grayscale
@@ -14,6 +14,7 @@ Napi::Value AddMasks(const Napi::CallbackInfo& info);
14
14
  Napi::Value AddBBs(const Napi::CallbackInfo& info);
15
15
  Napi::Value ImageAlign(const Napi::CallbackInfo& info);
16
16
  Napi::Value Draw(const Napi::CallbackInfo& info);
17
+ Napi::Value ColorConvert(const Napi::CallbackInfo& info);
17
18
 
18
19
  Napi::Object Init(Napi::Env env, Napi::Object exports) {
19
20
  exports.Set(Napi::String::New(env, "resize"), Napi::Function::New(env, Resize));
@@ -30,6 +31,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
30
31
  exports.Set(Napi::String::New(env, "addBBs"), Napi::Function::New(env, AddBBs));
31
32
  exports.Set(Napi::String::New(env, "imageAlign"), Napi::Function::New(env, ImageAlign));
32
33
  exports.Set(Napi::String::New(env, "draw"), Napi::Function::New(env, Draw));
34
+ exports.Set(Napi::String::New(env, "colorConvert"), Napi::Function::New(env, ColorConvert));
33
35
  return exports;
34
36
  }
35
37
 
@@ -69,14 +69,14 @@ public:
69
69
  }
70
70
  // Default based on channel count
71
71
  else {
72
- channel = (images_[i].channels() == 4) ? "BGRA"
73
- : (images_[i].channels() == 3) ? "BGR"
72
+ channel = (images_[i].channels() == 4) ? "RGBA"
73
+ : (images_[i].channels() == 3) ? "RGB"
74
74
  : "GRAY";
75
75
  }
76
76
  } else {
77
77
  // Buffer input - determine from OpenCV Mat
78
- channel = (images_[i].channels() == 4) ? "BGRA"
79
- : (images_[i].channels() == 3) ? "BGR"
78
+ channel = (images_[i].channels() == 4) ? "RGBA"
79
+ : (images_[i].channels() == 3) ? "RGB"
80
80
  : "GRAY";
81
81
  }
82
82
 
@@ -34,14 +34,14 @@ public:
34
34
  }
35
35
  // Default based on channel count
36
36
  else {
37
- channel_ = (src_.channels() == 4) ? "BGRA"
38
- : (src_.channels() == 3) ? "BGR"
37
+ channel_ = (src_.channels() == 4) ? "RGBA"
38
+ : (src_.channels() == 3) ? "RGB"
39
39
  : "GRAY";
40
40
  }
41
41
  } else {
42
42
  // Buffer input - determine from OpenCV Mat
43
- channel_ = (src_.channels() == 4) ? "BGRA"
44
- : (src_.channels() == 3) ? "BGR"
43
+ channel_ = (src_.channels() == 4) ? "RGBA"
44
+ : (src_.channels() == 3) ? "RGB"
45
45
  : "GRAY";
46
46
  }
47
47
 
@@ -41,14 +41,14 @@ ResizeWorker(Napi::Function& callback,
41
41
  }
42
42
  // Default based on channel count
43
43
  else {
44
- channelOrder = (inputMat.channels() == 4) ? "BGRA"
45
- : (inputMat.channels() == 3) ? "BGR"
44
+ channelOrder = (inputMat.channels() == 4) ? "RGBA"
45
+ : (inputMat.channels() == 3) ? "RGB"
46
46
  : "GRAY";
47
47
  }
48
48
  } else {
49
49
  // Buffer input - determine from OpenCV Mat
50
- channelOrder = (inputMat.channels() == 4) ? "BGRA"
51
- : (inputMat.channels() == 3) ? "BGR"
50
+ channelOrder = (inputMat.channels() == 4) ? "RGBA"
51
+ : (inputMat.channels() == 3) ? "RGB"
52
52
  : "GRAY";
53
53
  }
54
54
  } catch (const Napi::Error& e) {
@@ -34,14 +34,14 @@ public:
34
34
  }
35
35
  // Default based on channel count
36
36
  else {
37
- channelOrder = (inputMat.channels() == 4) ? "BGRA"
38
- : (inputMat.channels() == 3) ? "BGR"
37
+ channelOrder = (inputMat.channels() == 4) ? "RGBA"
38
+ : (inputMat.channels() == 3) ? "RGB"
39
39
  : "GRAY";
40
40
  }
41
41
  } else {
42
42
  // Buffer input - determine from OpenCV Mat
43
- channelOrder = (inputMat.channels() == 4) ? "BGRA"
44
- : (inputMat.channels() == 3) ? "BGR"
43
+ channelOrder = (inputMat.channels() == 4) ? "RGBA"
44
+ : (inputMat.channels() == 3) ? "RGB"
45
45
  : "GRAY";
46
46
  }
47
47
  } catch (const Napi::Error& e) { SetError(e.Message()); }
@@ -63,9 +63,9 @@ protected:
63
63
 
64
64
  if (near(0) || near(90) || near(180) || near(270)) {
65
65
  if (near(0)) resultMat = inputMat; // alias
66
- else if (near(90)) cv::rotate(inputMat,resultMat,cv::ROTATE_90_CLOCKWISE);
66
+ else if (near(90)) cv::rotate(inputMat,resultMat,cv::ROTATE_90_COUNTERCLOCKWISE);
67
67
  else if (near(180)) cv::rotate(inputMat,resultMat,cv::ROTATE_180);
68
- else cv::rotate(inputMat,resultMat,cv::ROTATE_90_COUNTERCLOCKWISE);
68
+ else cv::rotate(inputMat,resultMat,cv::ROTATE_90_CLOCKWISE);
69
69
  } else {
70
70
  // Ángulos arbitrarios (sempre PAD)
71
71
  int w=inputMat.cols, h=inputMat.rows;
@@ -341,12 +341,12 @@ inline std::string DetectChannelFormatShared(const Napi::Value& jsImg, const cv:
341
341
  // Default based on channel count
342
342
  else {
343
343
  const int channels = mat.channels();
344
- return (channels == 4) ? "BGRA" : (channels == 3) ? "BGR" : "GRAY";
344
+ return (channels == 4) ? "RGBA" : (channels == 3) ? "RGB" : "GRAY";
345
345
  }
346
346
  } else {
347
347
  // Buffer input - determine from OpenCV Mat
348
348
  const int channels = mat.channels();
349
- return (channels == 4) ? "BGRA" : (channels == 3) ? "BGR" : "GRAY";
349
+ return (channels == 4) ? "RGBA" : (channels == 3) ? "RGB" : "GRAY";
350
350
  }
351
351
  }
352
352