Skip to content

Commit eba8718

Browse files
refactor: architecture update
1 parent 33f03b4 commit eba8718

25 files changed

+643
-641
lines changed

cpp/FOCV_Function.cpp

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "FOCV_Function.hpp"
99
#include "FOCV_Storage.hpp"
10+
#include "FOCV_Ids.hpp"
11+
#include <FOCV_JsiObject.hpp>
1012
#include <opencv2/opencv.hpp>
1113

1214
constexpr uint64_t hashString(const char* str, size_t length) {
@@ -21,47 +23,43 @@ constexpr uint64_t hashString(const char* str, size_t length) {
2123
return hash;
2224
}
2325

24-
std::string getId(jsi::Object argument) {
25-
return argument.asString(runtime).utf8(runtime)
26+
std::string getId(jsi::Runtime& runtime, const jsi::Value& argument) {
27+
return argument.asString(runtime).utf8(runtime);
2628
}
2729

2830
jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* arguments) {
2931
jsi::Object value(runtime);
30-
std::string functionName = getId(arguments[0]);
3132

32-
switch(hashString(objectType.c_str(), objectType.size())) {
33+
std::string functionName = getId(runtime, arguments[0]);
34+
35+
switch (hashString(functionName.c_str(), functionName.size())) {
3336
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]));
37+
cv::Mat src = FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1]));
38+
cv::Mat dst = FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[2]));
3639
cv::cvtColor(src, dst, arguments[3].getNumber());
37-
FOCV_Storage::save(getId(arguments[2]), dst));
40+
FOCV_Storage::save(FOCV_JsiObject::id_from_wrap(runtime, arguments[2]), dst);
3841
} break;
3942
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]));
43+
cv::Mat src = FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1]));
44+
std::string s = FOCV_JsiObject::id_from_wrap(runtime, arguments[2]);
45+
std::string id = FOCV_JsiObject::type_from_wrap(runtime, arguments[2]);
46+
cv::Vec3b lowerBound = FOCV_Storage::get<cv::Vec3b>(FOCV_JsiObject::id_from_wrap(runtime, arguments[2]));
47+
cv::Vec3b upperBound = FOCV_Storage::get<cv::Vec3b>(FOCV_JsiObject::id_from_wrap(runtime, arguments[3]));
48+
cv::Mat dst = FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[4]));
4449
cv::inRange(src, lowerBound, upperBound, dst);
45-
FOCV_Storage::save(getId(arguments[3]), dst);
50+
FOCV_Storage::save(FOCV_JsiObject::id_from_wrap(runtime, arguments[4]), dst);
4651
} break;
4752
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]));
53+
cv::Mat src = FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1]));
54+
std::vector<cv::Mat> dst = FOCV_Storage::get<std::vector<cv::Mat>>(FOCV_JsiObject::id_from_wrap(runtime, arguments[2]));
5055
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));
56+
FOCV_Storage::save(FOCV_JsiObject::id_from_wrap(runtime, arguments[2]), dst);
5957
} break;
6058
case hashString("findContours", 12): {
61-
cv::Mat src = FOCV_Storage::get<cv::Mat>(getId(arguments[0]));
59+
cv::Mat src = FOCV_Storage::get<cv::Mat>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1]));
6260
std::vector<std::vector<cv::Point>> contours;
6361

64-
cv::findContours(src, contours, arguments[1].getNumber(), arguments[2].getNumber());
62+
cv::findContours(src, contours, arguments[2].getNumber(), arguments[3].getNumber());
6563

6664
auto ids = FOCV_Ids();
6765

@@ -70,18 +68,22 @@ jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* argum
7068
ids.push(id);
7169
}
7270

73-
value.setProperty(runtime, "array", ids.toJsiArray(runtime));
71+
return ids.toJsiArray(runtime, "point_vector");
7472
} break;
7573
case hashString("contourArea", 11): {
76-
std::vector<cv::Point> src = FOCV_Storage::get<std::vector<cv::Point>>(getId(arguments[0]));
74+
std::vector<cv::Point> src = FOCV_Storage::get<std::vector<cv::Point>>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1]));
7775

78-
value.setProperty(runtime, "area", contourArea(src, arguments[1].getBool()));
76+
value.setProperty(runtime, "area", contourArea(src, arguments[2].getBool()));
77+
} break;
78+
case hashString("boundingRect", 12): {
79+
std::vector<cv::Point> src = FOCV_Storage::get<std::vector<cv::Point>>(FOCV_JsiObject::id_from_wrap(runtime, arguments[1]));
80+
cv::Rect rect = cv::boundingRect(src);
81+
82+
std::string id = FOCV_Storage::save(rect);
83+
84+
return FOCV_JsiObject::wrap(runtime, "rect", id);
7985
} break;
80-
8186
}
8287

83-
jsi::Object object(runtime);
84-
object.setProperty(runtime, "value", value);
85-
86-
return object;
88+
return value;
8789
}

cpp/FOCV_Ids.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//
66

77
#include "FOCV_Ids.hpp"
8+
#include "FOCV_JsiObject.hpp"
89
#include <jsi/jsilib.h>
910
#include <jsi/jsi.h>
1011

@@ -14,11 +15,11 @@ void FOCV_Ids::push(std::string id) {
1415
ids.push_back(id);
1516
}
1617

17-
jsi::Array FOCV_Ids::toJsiArray(jsi::Runtime& runtime) {
18+
jsi::Array FOCV_Ids::toJsiArray(jsi::Runtime& runtime, std::string type) {
1819
auto result = jsi::Array(runtime, ids.size());
1920

2021
for (int i = 0; i < ids.size(); i++) {
21-
result.setValueAtIndex(runtime, i, ids[i]);
22+
result.setValueAtIndex(runtime, i, FOCV_JsiObject::wrap(runtime, type, ids[i]));
2223
}
2324

2425
return result;

cpp/FOCV_Ids.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class FOCV_Ids {
1717

1818
public:
1919
void push(std::string id);
20-
facebook::jsi::Array toJsiArray(facebook::jsi::Runtime& runtime);
20+
facebook::jsi::Array toJsiArray(facebook::jsi::Runtime& runtime, std::string type);
2121
};
2222

2323
#endif /* FOCV_Ids_hpp */

cpp/FOCV_JsiObject.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// FOCV_JsiObject.cpp
3+
// react-native-fast-opencv
4+
//
5+
// Created by Łukasz Kurant on 10/08/2024.
6+
//
7+
8+
#include "FOCV_JsiObject.hpp"
9+
10+
jsi::Object FOCV_JsiObject::wrap(jsi::Runtime& runtime, std::string type, std::string id) {
11+
jsi::Object wrapped(runtime);
12+
wrapped.setProperty(runtime, "type", jsi::String::createFromUtf8(runtime, type));
13+
wrapped.setProperty(runtime, "id", jsi::String::createFromUtf8(runtime, id));
14+
return wrapped;
15+
}
16+
17+
std::string FOCV_JsiObject::id_from_wrap(jsi::Runtime& runtime, const jsi::Value& wrap) {
18+
return wrap.asObject(runtime).getProperty(runtime, "id").asString(runtime).utf8(runtime);
19+
}
20+
21+
std::string FOCV_JsiObject::type_from_wrap(jsi::Runtime& runtime, const jsi::Value& wrap) {
22+
return wrap.asObject(runtime).getProperty(runtime, "type").asString(runtime).utf8(runtime);
23+
}

cpp/FOCV_JsiObject.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// FOCV_JsiObject.hpp
3+
// react-native-fast-opencv
4+
//
5+
// Created by Łukasz Kurant on 10/08/2024.
6+
//
7+
8+
#ifndef FOCV_JsiObject_hpp
9+
#define FOCV_JsiObject_hpp
10+
11+
#include <stdio.h>
12+
#include <jsi/jsilib.h>
13+
#include <jsi/jsi.h>
14+
15+
using namespace facebook;
16+
17+
class FOCV_JsiObject {
18+
public:
19+
static jsi::Object wrap(jsi::Runtime& runtime, std::string type, std::string id);
20+
static std::string id_from_wrap(jsi::Runtime& runtime, const jsi::Value& wrap);
21+
static std::string type_from_wrap(jsi::Runtime& runtime, const jsi::Value& wrap);
22+
};
23+
24+
#endif /* FOCV_JsiObject_hpp */

cpp/FOCV_Mat.cpp

Lines changed: 0 additions & 52 deletions
This file was deleted.

cpp/FOCV_Mat.hpp

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)