Skip to content

Commit 3e0d61f

Browse files
Merge pull request #26 from lukaszkurantdev/feat/multiple-buffer-types
feat: multiple buffer types in matToBuffer function
2 parents 197e318 + 8817c34 commit 3e0d61f

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed

cpp/react-native-fast-opencv.cpp

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,29 +94,53 @@ jsi::Value OpenCVPlugin::get(jsi::Runtime& runtime, const jsi::PropNameID& propN
9494
[=](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
9595
size_t count) -> jsi::Object {
9696

97-
std::string id = FOCV_JsiObject::id_from_wrap(runtime, arguments[0]);
98-
auto mat = *FOCV_Storage::get<cv::Mat>(id);
99-
100-
jsi::Object value(runtime);
101-
102-
value.setProperty(runtime, "cols", jsi::Value(mat.cols));
103-
value.setProperty(runtime, "rows", jsi::Value(mat.rows));
104-
value.setProperty(runtime, "channels", jsi::Value(mat.channels()));
105-
106-
auto type = arguments[1].asString(runtime).utf8(runtime);
107-
int size = mat.cols * mat.rows * mat.channels();
108-
109-
if(type == "uint8") {
110-
auto arr = TypedArray<TypedArrayKind::Uint8Array>(runtime, size);
111-
arr.updateUnsafe(runtime, (uint8_t*)mat.data, size * sizeof(uint8_t));
112-
value.setProperty(runtime, "buffer", arr);
113-
} else if(type == "float32") {
114-
auto arr = TypedArray<TypedArrayKind::Float32Array>(runtime, size);
115-
arr.updateUnsafe(runtime, (float*)mat.data, size * sizeof(float));
116-
value.setProperty(runtime, "buffer", arr);
117-
}
118-
119-
return value;
97+
std::string id = FOCV_JsiObject::id_from_wrap(runtime, arguments[0]);
98+
auto mat = *FOCV_Storage::get<cv::Mat>(id);
99+
100+
jsi::Object value(runtime);
101+
102+
value.setProperty(runtime, "cols", jsi::Value(mat.cols));
103+
value.setProperty(runtime, "rows", jsi::Value(mat.rows));
104+
value.setProperty(runtime, "channels", jsi::Value(mat.channels()));
105+
106+
auto type = arguments[1].asString(runtime).utf8(runtime);
107+
int size = mat.cols * mat.rows * mat.channels();
108+
109+
if(type == "uint8") {
110+
auto arr = TypedArray<TypedArrayKind::Uint8Array>(runtime, size);
111+
arr.updateUnsafe(runtime, (uint8_t*)mat.data, size * sizeof(uint8_t));
112+
value.setProperty(runtime, "buffer", arr);
113+
} else if(type == "uint16") {
114+
auto arr = TypedArray<TypedArrayKind::Uint16Array>(runtime, size);
115+
arr.updateUnsafe(runtime, (uint16_t*)mat.data, size * sizeof(uint16_t));
116+
value.setProperty(runtime, "buffer", arr);
117+
} else if(type == "uint32") {
118+
auto arr = TypedArray<TypedArrayKind::Uint32Array>(runtime, size);
119+
arr.updateUnsafe(runtime, (uint32_t*)mat.data, size * sizeof(uint32_t));
120+
value.setProperty(runtime, "buffer", arr);
121+
} else if(type == "int8") {
122+
auto arr = TypedArray<TypedArrayKind::Int8Array>(runtime, size);
123+
arr.updateUnsafe(runtime, (int8_t*)mat.data, size * sizeof(int8_t));
124+
value.setProperty(runtime, "buffer", arr);
125+
} else if(type == "int16") {
126+
auto arr = TypedArray<TypedArrayKind::Int16Array>(runtime, size);
127+
arr.updateUnsafe(runtime, (int16_t*)mat.data, size * sizeof(int16_t));
128+
value.setProperty(runtime, "buffer", arr);
129+
} else if(type == "int32") {
130+
auto arr = TypedArray<TypedArrayKind::Int32Array>(runtime, size);
131+
arr.updateUnsafe(runtime, (int32_t*)mat.data, size * sizeof(int32_t));
132+
value.setProperty(runtime, "buffer", arr);
133+
} else if(type == "float32") {
134+
auto arr = TypedArray<TypedArrayKind::Float32Array>(runtime, size);
135+
arr.updateUnsafe(runtime, (float*)mat.data, size * sizeof(float));
136+
value.setProperty(runtime, "buffer", arr);
137+
} else if(type == "float64") {
138+
auto arr = TypedArray<TypedArrayKind::Float64Array>(runtime, size);
139+
arr.updateUnsafe(runtime, (double*)mat.data, size * sizeof(double));
140+
value.setProperty(runtime, "buffer", arr);
141+
}
142+
143+
return value;
120144
});
121145
} else if (propName == "createObject") {
122146
return jsi::Function::createFromHostFunction(

src/utils/UtilsFunctions.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import type { Mat } from '../objects/Objects';
22

3+
type BufferType = {
4+
uint8: Uint8Array;
5+
uint16: Uint16Array;
6+
uint32: Uint32Array;
7+
int8: Int8Array;
8+
int16: Int16Array;
9+
int32: Int32Array;
10+
float32: Float32Array;
11+
float64: Float64Array;
12+
};
13+
314
export type UtilsFunctions = {
415
/**
516
* Clears any buffers that were allocate to back Mats on the native side.
@@ -28,13 +39,13 @@ export type UtilsFunctions = {
2839
* @param mat - the Mat to convert
2940
* @param type - the type of the buffer to return ('uint8' or 'float32')
3041
*/
31-
matToBuffer<T extends 'uint8' | 'float32'>(
42+
matToBuffer<T extends keyof BufferType>(
3243
mat: Mat,
3344
type: T
3445
): {
3546
cols: number;
3647
rows: number;
3748
channels: number;
38-
buffer: T extends 'uint8' ? Uint8Array : Float32Array;
49+
buffer: BufferType[T];
3950
};
4051
};

0 commit comments

Comments
 (0)