Skip to content

Commit 33f03b4

Browse files
feat: focv functions and objects preparation
1 parent 5aacca5 commit 33f03b4

5 files changed

+355
-1
lines changed

cpp/FOCV_Function.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//
2+
// FOCV_Function.cpp
3+
// react-native-fast-opencv
4+
//
5+
// Created by Łukasz Kurant on 06/08/2024.
6+
//
7+
8+
#include "FOCV_Function.hpp"
9+
#include "FOCV_Storage.hpp"
10+
#include <opencv2/opencv.hpp>
11+
12+
constexpr uint64_t hashString(const char* str, size_t length) {
13+
uint64_t hash = 14695981039346656037ull;
14+
const uint64_t fnv_prime = 1099511628211ull;
15+
16+
for (size_t i = 0; i < length; ++i) {
17+
hash ^= static_cast<uint64_t>(str[i]);
18+
hash *= fnv_prime;
19+
}
20+
21+
return hash;
22+
}
23+
24+
std::string getId(jsi::Object argument) {
25+
return argument.asString(runtime).utf8(runtime)
26+
}
27+
28+
jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* arguments) {
29+
jsi::Object value(runtime);
30+
std::string functionName = getId(arguments[0]);
31+
32+
switch(hashString(objectType.c_str(), objectType.size())) {
33+
case hashString("cvtColor", 8): {
34+
cv::Mat src = FOCV_Storage::get<cv::Mat>(getId(arguments[1]));
35+
cv::Mat dst = FOCV_Storage::get<cv::Mat>(getId(arguments[2]));
36+
cv::cvtColor(src, dst, arguments[3].getNumber());
37+
FOCV_Storage::save(getId(arguments[2]), dst));
38+
} break;
39+
case hashString("inRange", 7): {
40+
cv::Mat src = FOCV_Storage::get<cv::Mat>(getId(arguments[0]));
41+
cv::Vec3b src = FOCV_Storage::get<cv::Vec3b>(getId(arguments[1]));
42+
cv::Vec3b src = FOCV_Storage::get<cv::Vec3b>(getId(arguments[2]));
43+
cv::Mat dst = FOCV_Storage::get<cv::Mat>(getId(arguments[3]));
44+
cv::inRange(src, lowerBound, upperBound, dst);
45+
FOCV_Storage::save(getId(arguments[3]), dst);
46+
} break;
47+
case hashString("split", 5): {
48+
cv::Mat src = FOCV_Storage::get<cv::Mat>(getId(arguments[0]));
49+
std::vector<cv::Mat> dst = FOCV_Storage::get<std::vector<cv::Mat>>(getId(arguments[1]));
50+
cv::split(src, dst);
51+
52+
auto ids = FOCV_Ids();
53+
54+
for (size_t i = 0; i < dst.size(); i++) {
55+
auto id = FOCV_Mat::saveMat(dst.at(i));
56+
ids.push(id);
57+
}
58+
value.setProperty(runtime, "array", ids.toJsiArray(runtime));
59+
} break;
60+
case hashString("findContours", 12): {
61+
cv::Mat src = FOCV_Storage::get<cv::Mat>(getId(arguments[0]));
62+
std::vector<std::vector<cv::Point>> contours;
63+
64+
cv::findContours(src, contours, arguments[1].getNumber(), arguments[2].getNumber());
65+
66+
auto ids = FOCV_Ids();
67+
68+
for (size_t i = 0; i < contours.size(); i++) {
69+
auto id = FOCV_Storage::save(contours.at(i));
70+
ids.push(id);
71+
}
72+
73+
value.setProperty(runtime, "array", ids.toJsiArray(runtime));
74+
} break;
75+
case hashString("contourArea", 11): {
76+
std::vector<cv::Point> src = FOCV_Storage::get<std::vector<cv::Point>>(getId(arguments[0]));
77+
78+
value.setProperty(runtime, "area", contourArea(src, arguments[1].getBool()));
79+
} break;
80+
81+
}
82+
83+
jsi::Object object(runtime);
84+
object.setProperty(runtime, "value", value);
85+
86+
return object;
87+
}

cpp/FOCV_Function.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// FOCV_Function.hpp
3+
// react-native-fast-opencv
4+
//
5+
// Created by Łukasz Kurant on 06/08/2024.
6+
//
7+
8+
#ifndef FOCV_Function_hpp
9+
#define FOCV_Function_hpp
10+
11+
#include <jsi/jsilib.h>
12+
#include <jsi/jsi.h>
13+
#include <stdio.h>
14+
15+
#ifdef __cplusplus
16+
#undef YES
17+
#undef NO
18+
#include <opencv2/opencv.hpp>
19+
using namespace cv;
20+
#if __has_feature(objc_bool)
21+
#define YES __objc_yes
22+
#define NO __objc_no
23+
#else
24+
#define YES ((BOOL)1)
25+
#define NO ((BOOL)0)
26+
#endif
27+
#endif
28+
29+
using namespace facebook;
30+
31+
class FOCV_Function {
32+
public:
33+
static jsi::Object invoke(jsi::Runtime& runtime, const jsi::Value* arguments);
34+
};
35+
36+
37+
#endif /* FOCV_Function_hpp */

cpp/FOCV_Object.cpp

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
//
2+
// FOCV_ObjectCreation.cpp
3+
// Pods
4+
//
5+
// Created by Łukasz Kurant on 06/08/2024.
6+
//
7+
8+
#include "FOCV_Object.hpp"
9+
#include "FOCV_Storage.hpp"
10+
#include <opencv2/opencv.hpp>
11+
12+
constexpr uint64_t hashString(const char* str, size_t length) {
13+
uint64_t hash = 14695981039346656037ull;
14+
const uint64_t fnv_prime = 1099511628211ull;
15+
16+
for (size_t i = 0; i < length; ++i) {
17+
hash ^= static_cast<uint64_t>(str[i]);
18+
hash *= fnv_prime;
19+
}
20+
21+
return hash;
22+
}
23+
24+
jsi::String FOCV_Object::create(jsi::Runtime& runtime, const jsi::Value* arguments) {
25+
std::any object;
26+
std::string objectType = arguments[0].asString(runtime).utf8(runtime);
27+
28+
switch(hashString(objectType.c_str(), objectType.size())) {
29+
case hashString("mat", 3): {
30+
int rows = arguments[1].asNumber();
31+
int cols = arguments[2].asNumber();
32+
int type = arguments[3].asNumber();
33+
object = cv::Mat(rows, cols, type);
34+
} break;
35+
case hashString("mat_array", 9): {
36+
object = std::vector<cv::Mat>();
37+
} break;
38+
case hashString("rect", 4): {
39+
int x = arguments[1].asNumber();
40+
int y = arguments[2].asNumber();
41+
int width = arguments[3].asNumber();
42+
int height = arguments[4].asNumber();
43+
object = cv::Rect(x, y, width, height);
44+
} break;
45+
case hashString("rect_array", 10): {
46+
object = std::vector<cv::Rect>();
47+
} break;
48+
case hashString("point", 5): {
49+
int x = arguments[1].asNumber();
50+
int y = arguments[2].asNumber();
51+
object = cv::Point(x, y);
52+
} break;
53+
case hashString("point_array", 11): {
54+
object = std::vector<cv::Point>();
55+
} break;
56+
case hashString("size", 4): {
57+
int width = arguments[1].asNumber();
58+
int height = arguments[2].asNumber();
59+
object = cv::Size(width, height);
60+
} break;
61+
case hashString("vec3b", 4): {
62+
int a = arguments[1].asNumber();
63+
int b = arguments[2].asNumber();
64+
int c = arguments[3].asNumber();
65+
object = cv::Vec3b(a, b, c);
66+
} break;
67+
}
68+
69+
std::string id = FOCV_Storage::save(object);
70+
return jsi::String::createFromUtf8(runtime, id);
71+
}
72+
73+
jsi::Object FOCV_Object::convertToJSI(jsi::Runtime& runtime, const jsi::Value* arguments) {
74+
75+
jsi::Object value(runtime);
76+
std::string objectType = arguments[0].asString(runtime).utf8(runtime);
77+
std::string id = arguments[1].asString(runtime).utf8(runtime);
78+
79+
switch(hashString(objectType.c_str(), objectType.size())) {
80+
case hashString("mat", 3): {
81+
cv::Mat mat = FOCV_Storage::get<cv::Mat>(id);
82+
83+
value.setProperty(runtime, "size", jsi::Value(mat.size));
84+
value.setProperty(runtime, "cols", jsi::Value(mat.cols));
85+
value.setProperty(runtime, "rows", jsi::Value(mat.rows));
86+
87+
} break;
88+
case hashString("mat_array", 9): {
89+
std::vector<cv::Mat> mats = FOCV_Storage::get<std::vector<cv::Mat>>(id);
90+
91+
auto array = jsi::Array(runtime, mats.size());
92+
93+
for (int i = 0; i < mats.size(); i++) {
94+
jsi::Object item(runtime);
95+
96+
item.setProperty(runtime, "size", jsi::Value(mats.at(i).size));
97+
item.setProperty(runtime, "cols", jsi::Value(mats.at(i).cols));
98+
item.setProperty(runtime, "rows", jsi::Value(mats.at(i).rows));
99+
array.setValueAtIndex(runtime, i, item);
100+
}
101+
102+
value.setProperty(runtime, "array", array);
103+
} break;
104+
case hashString("rect", 4): {
105+
cv::Rect rect = FOCV_Storage::get<cv::Rect>(id);
106+
107+
value.setProperty(runtime, "x", jsi::Value(rect.x));
108+
value.setProperty(runtime, "y", jsi::Value(rect.y));
109+
value.setProperty(runtime, "width", jsi::Value(rect.width));
110+
value.setProperty(runtime, "height", jsi::Value(rect.height));
111+
} break;
112+
case hashString("rect_array", 10): {
113+
std::vector<cv::Rect> rects = FOCV_Storage::get<std::vector<cv::Rect>>(id);
114+
115+
auto array = jsi::Array(runtime, rects.size());
116+
117+
for (int i = 0; i < rects.size(); i++) {
118+
jsi::Object item(runtime);
119+
120+
item.setProperty(runtime, "x", jsi::Value(rects.at(i).x));
121+
item.setProperty(runtime, "y", jsi::Value(rects.at(i).y));
122+
item.setProperty(runtime, "width", jsi::Value(rects.at(i).width));
123+
item.setProperty(runtime, "height", jsi::Value(rects.at(i).height));
124+
array.setValueAtIndex(runtime, i, item);
125+
}
126+
127+
value.setProperty(runtime, "array", array);
128+
} break;
129+
case hashString("point", 5): {
130+
cv::Point point = FOCV_Storage::get<cv::Point>(id);
131+
132+
value.setProperty(runtime, "x", jsi::Value(point.x));
133+
value.setProperty(runtime, "y", jsi::Value(point.y));
134+
} break;
135+
case hashString("point_array", 11): {
136+
std::vector<cv::Point> points = FOCV_Storage::get<std::vector<cv::Point>>(id);
137+
138+
auto array = jsi::Array(runtime, points.size());
139+
140+
for (int i = 0; i < points.size(); i++) {
141+
jsi::Object item(runtime);
142+
143+
item.setProperty(runtime, "x", jsi::Value(points.at(i).x));
144+
item.setProperty(runtime, "y", jsi::Value(points.at(i).y));
145+
array.setValueAtIndex(runtime, i, item);
146+
}
147+
148+
value.setProperty(runtime, "array", array);
149+
} break;
150+
case hashString("size", 4): {
151+
cv::Size size = FOCV_Storage::get<cv::Size>(id);
152+
153+
value.setProperty(runtime, "width", jsi::Value(size.width));
154+
value.setProperty(runtime, "height", jsi::Value(size.height));
155+
} break;
156+
case hashString("vec3b", 4): {
157+
cv::Vec3b vec = FOCV_Storage::get<cv::Vec3b>(id);
158+
159+
value.setProperty(runtime, "a", jsi::Value(vec.val[0]));
160+
value.setProperty(runtime, "b", jsi::Value(vec.val[1]));
161+
value.setProperty(runtime, "c", jsi::Value(vec.val[2]));
162+
} break;
163+
}
164+
165+
jsi::Object object(runtime);
166+
object.setProperty(runtime, "value", value);
167+
168+
return object;
169+
}

cpp/FOCV_Object.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// FOCV_Object.hpp
3+
// Pods
4+
//
5+
// Created by Łukasz Kurant on 06/08/2024.
6+
//
7+
8+
#ifndef FOCV_Object_hpp
9+
#define FOCV_Object_hpp
10+
11+
#include <jsi/jsilib.h>
12+
#include <jsi/jsi.h>
13+
#include <stdio.h>
14+
15+
#ifdef __cplusplus
16+
#undef YES
17+
#undef NO
18+
#include <opencv2/opencv.hpp>
19+
using namespace cv;
20+
#if __has_feature(objc_bool)
21+
#define YES __objc_yes
22+
#define NO __objc_no
23+
#else
24+
#define YES ((BOOL)1)
25+
#define NO ((BOOL)0)
26+
#endif
27+
#endif
28+
29+
using namespace facebook;
30+
31+
class FOCV_Object {
32+
public:
33+
static jsi::String create(jsi::Runtime& runtime, const jsi::Value* arguments);
34+
static jsi::Object convertToJSI(jsi::Runtime& runtime, const jsi::Value* arguments);
35+
};
36+
37+
#endif /* FOCV_Object_hpp */

cpp/react-native-fast-opencv.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <FOCV_Storage.hpp>
1010
#include <FOCV_PointsGroup.hpp>
1111
#include <FOCV_StoredObject.hpp>
12+
#include "FOCV_Object.hpp"
1213
#include "opencv2/opencv.hpp"
1314

1415
//using namespace facebook;
@@ -64,6 +65,24 @@ jsi::Value OpenCVPlugin::get(jsi::Runtime& runtime, const jsi::PropNameID& propN
6465
arguments[2].asObject(runtime));
6566
});
6667
}
68+
else if (propName == "createObject") {
69+
return jsi::Function::createFromHostFunction(
70+
runtime, jsi::PropNameID::forAscii(runtime, "createObject"), 1,
71+
[=](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
72+
size_t count) -> jsi::String {
73+
74+
return FOCV_Object::create(runtime, arguments);
75+
});
76+
}
77+
else if (propName == "convertObjectToJSI") {
78+
return jsi::Function::createFromHostFunction(
79+
runtime, jsi::PropNameID::forAscii(runtime, "convertObjectToJSI"), 1,
80+
[=](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
81+
size_t count) -> jsi::Object {
82+
83+
return FOCV_Object::convertToJSI(runtime, arguments);
84+
});
85+
}
6786
else if (propName == "cvtColor") {
6887
return jsi::Function::createFromHostFunction(
6988
runtime, jsi::PropNameID::forAscii(runtime, "cvtColor"), 1,
@@ -100,7 +119,7 @@ jsi::Value OpenCVPlugin::get(jsi::Runtime& runtime, const jsi::PropNameID& propN
100119
FOCV_Mat::saveMat(arguments[3].asString(runtime).utf8(runtime), dst);
101120
return true;
102121
});
103-
} else if (propName == "split") {
122+
} else if (propName == "split") {
104123
return jsi::Function::createFromHostFunction(
105124
runtime, jsi::PropNameID::forAscii(runtime, "split"), 1,
106125
[=](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
@@ -201,6 +220,11 @@ std::vector<jsi::PropNameID> OpenCVPlugin::getPropertyNames(jsi::Runtime& runtim
201220
std::vector<jsi::PropNameID> result;
202221
// list of available functions!!!
203222
result.push_back(jsi::PropNameID::forAscii(runtime, "frameBufferToMat"));
223+
result.push_back(jsi::PropNameID::forAscii(runtime, "createObject"));
224+
result.push_back(jsi::PropNameID::forAscii(runtime, "convertObjectToJSI"));
225+
226+
227+
204228
result.push_back(jsi::PropNameID::forAscii(runtime, "cvtColor"));
205229
result.push_back(jsi::PropNameID::forAscii(runtime, "inRange"));
206230
result.push_back(jsi::PropNameID::forAscii(runtime, "split"));

0 commit comments

Comments
 (0)