From 8817c3437da177d008a9121a52f9b72029117ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Kurant?= Date: Sun, 20 Oct 2024 09:19:12 +0200 Subject: [PATCH] feat: multiple buffer types in matToBuffer function --- cpp/react-native-fast-opencv.cpp | 70 +++++++++++++++++++++----------- src/utils/UtilsFunctions.ts | 15 ++++++- 2 files changed, 60 insertions(+), 25 deletions(-) diff --git a/cpp/react-native-fast-opencv.cpp b/cpp/react-native-fast-opencv.cpp index 562d426..7be7618 100644 --- a/cpp/react-native-fast-opencv.cpp +++ b/cpp/react-native-fast-opencv.cpp @@ -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(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(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(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(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(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(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(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(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(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(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(runtime, size); + arr.updateUnsafe(runtime, (float*)mat.data, size * sizeof(float)); + value.setProperty(runtime, "buffer", arr); + } else if(type == "float64") { + auto arr = TypedArray(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( diff --git a/src/utils/UtilsFunctions.ts b/src/utils/UtilsFunctions.ts index d2fac63..cec044d 100644 --- a/src/utils/UtilsFunctions.ts +++ b/src/utils/UtilsFunctions.ts @@ -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. @@ -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( + matToBuffer( mat: Mat, type: T ): { cols: number; rows: number; channels: number; - buffer: T extends 'uint8' ? Uint8Array : Float32Array; + buffer: BufferType[T]; }; };