Skip to content

feat: multiple buffer types in matToBuffer function #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 47 additions & 23 deletions cpp/react-native-fast-opencv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,53 @@ jsi::Value OpenCVPlugin::get(jsi::Runtime& runtime, const jsi::PropNameID& propN
[=](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
size_t count) -> jsi::Object {

std::string id = FOCV_JsiObject::id_from_wrap(runtime, arguments[0]);
auto mat = *FOCV_Storage::get<cv::Mat>(id);

jsi::Object value(runtime);

value.setProperty(runtime, "cols", jsi::Value(mat.cols));
value.setProperty(runtime, "rows", jsi::Value(mat.rows));
value.setProperty(runtime, "channels", jsi::Value(mat.channels()));

auto type = arguments[1].asString(runtime).utf8(runtime);
int size = mat.cols * mat.rows * mat.channels();

if(type == "uint8") {
auto arr = TypedArray<TypedArrayKind::Uint8Array>(runtime, size);
arr.updateUnsafe(runtime, (uint8_t*)mat.data, size * sizeof(uint8_t));
value.setProperty(runtime, "buffer", arr);
} else if(type == "float32") {
auto arr = TypedArray<TypedArrayKind::Float32Array>(runtime, size);
arr.updateUnsafe(runtime, (float*)mat.data, size * sizeof(float));
value.setProperty(runtime, "buffer", arr);
}

return value;
std::string id = FOCV_JsiObject::id_from_wrap(runtime, arguments[0]);
auto mat = *FOCV_Storage::get<cv::Mat>(id);

jsi::Object value(runtime);

value.setProperty(runtime, "cols", jsi::Value(mat.cols));
value.setProperty(runtime, "rows", jsi::Value(mat.rows));
value.setProperty(runtime, "channels", jsi::Value(mat.channels()));

auto type = arguments[1].asString(runtime).utf8(runtime);
int size = mat.cols * mat.rows * mat.channels();

if(type == "uint8") {
auto arr = TypedArray<TypedArrayKind::Uint8Array>(runtime, size);
arr.updateUnsafe(runtime, (uint8_t*)mat.data, size * sizeof(uint8_t));
value.setProperty(runtime, "buffer", arr);
} else if(type == "uint16") {
auto arr = TypedArray<TypedArrayKind::Uint16Array>(runtime, size);
arr.updateUnsafe(runtime, (uint16_t*)mat.data, size * sizeof(uint16_t));
value.setProperty(runtime, "buffer", arr);
} else if(type == "uint32") {
auto arr = TypedArray<TypedArrayKind::Uint32Array>(runtime, size);
arr.updateUnsafe(runtime, (uint32_t*)mat.data, size * sizeof(uint32_t));
value.setProperty(runtime, "buffer", arr);
} else if(type == "int8") {
auto arr = TypedArray<TypedArrayKind::Int8Array>(runtime, size);
arr.updateUnsafe(runtime, (int8_t*)mat.data, size * sizeof(int8_t));
value.setProperty(runtime, "buffer", arr);
} else if(type == "int16") {
auto arr = TypedArray<TypedArrayKind::Int16Array>(runtime, size);
arr.updateUnsafe(runtime, (int16_t*)mat.data, size * sizeof(int16_t));
value.setProperty(runtime, "buffer", arr);
} else if(type == "int32") {
auto arr = TypedArray<TypedArrayKind::Int32Array>(runtime, size);
arr.updateUnsafe(runtime, (int32_t*)mat.data, size * sizeof(int32_t));
value.setProperty(runtime, "buffer", arr);
} else if(type == "float32") {
auto arr = TypedArray<TypedArrayKind::Float32Array>(runtime, size);
arr.updateUnsafe(runtime, (float*)mat.data, size * sizeof(float));
value.setProperty(runtime, "buffer", arr);
} else if(type == "float64") {
auto arr = TypedArray<TypedArrayKind::Float64Array>(runtime, size);
arr.updateUnsafe(runtime, (double*)mat.data, size * sizeof(double));
value.setProperty(runtime, "buffer", arr);
}

return value;
});
} else if (propName == "createObject") {
return jsi::Function::createFromHostFunction(
Expand Down
15 changes: 13 additions & 2 deletions src/utils/UtilsFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import type { Mat } from '../objects/Objects';

type BufferType = {
uint8: Uint8Array;
uint16: Uint16Array;
uint32: Uint32Array;
int8: Int8Array;
int16: Int16Array;
int32: Int32Array;
float32: Float32Array;
float64: Float64Array;
};

export type UtilsFunctions = {
/**
* Clears any buffers that were allocate to back Mats on the native side.
Expand Down Expand Up @@ -28,13 +39,13 @@ export type UtilsFunctions = {
* @param mat - the Mat to convert
* @param type - the type of the buffer to return ('uint8' or 'float32')
*/
matToBuffer<T extends 'uint8' | 'float32'>(
matToBuffer<T extends keyof BufferType>(
mat: Mat,
type: T
): {
cols: number;
rows: number;
channels: number;
buffer: T extends 'uint8' ? Uint8Array : Float32Array;
buffer: BufferType[T];
};
};
Loading