Skip to content

Commit 18419c5

Browse files
Merge pull request #4 from lukaszkurantdev/feat/mat-to-buffer
feat: mat to buffer (#3)
2 parents bfd6ce4 + d779eab commit 18419c5

File tree

6 files changed

+87
-4
lines changed

6 files changed

+87
-4
lines changed

cpp/FOCV_Function.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,13 @@ jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* argum
12661266
auto id = FOCV_Storage::save(rect);
12671267
return FOCV_JsiObject::wrap(runtime, "rotated_rect", id);
12681268
} break;
1269+
case hashString("convertTo", 9): {
1270+
auto src = args.asMatPtr(1);
1271+
auto dst = args.asMatPtr(2);
1272+
auto rtype = args.asNumber(3);
1273+
1274+
(*src).convertTo(*dst, rtype);
1275+
} break;
12691276
}
12701277

12711278
return value;

cpp/react-native-fast-opencv.cpp

+33-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jsi::Value OpenCVPlugin::get(jsi::Runtime& runtime, const jsi::PropNameID& propN
5555
return FOCV_JsiObject::wrap(runtime, "mat", id);
5656
});
5757
}
58-
if (propName == "base64ToMat") {
58+
else if (propName == "base64ToMat") {
5959
return jsi::Function::createFromHostFunction(
6060
runtime, jsi::PropNameID::forAscii(runtime, "frameBufferToMat"), 1,
6161
[=](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
@@ -69,7 +69,37 @@ jsi::Value OpenCVPlugin::get(jsi::Runtime& runtime, const jsi::PropNameID& propN
6969
return FOCV_JsiObject::wrap(runtime, "mat", id);
7070
});
7171
}
72-
else if (propName == "createObject") {
72+
else if (propName == "matToBuffer") {
73+
return jsi::Function::createFromHostFunction(
74+
runtime, jsi::PropNameID::forAscii(runtime, "matToBuffer"), 1,
75+
[=](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
76+
size_t count) -> jsi::Object {
77+
78+
std::string id = FOCV_JsiObject::id_from_wrap(runtime, arguments[0]);
79+
auto mat = *FOCV_Storage::get<cv::Mat>(id);
80+
81+
jsi::Object value(runtime);
82+
83+
value.setProperty(runtime, "cols", jsi::Value(mat.cols));
84+
value.setProperty(runtime, "rows", jsi::Value(mat.rows));
85+
value.setProperty(runtime, "channels", jsi::Value(mat.channels()));
86+
87+
auto type = arguments[1].asString(runtime).utf8(runtime);
88+
int size = mat.cols * mat.rows * mat.channels();
89+
90+
if(type == "uint8") {
91+
auto arr = TypedArray<TypedArrayKind::Uint8Array>(runtime, size);
92+
arr.updateUnsafe(runtime, (uint8_t*)mat.data, size * sizeof(uint8_t));
93+
value.setProperty(runtime, "buffer", arr);
94+
} else if(type == "float32") {
95+
auto arr = TypedArray<TypedArrayKind::Float32Array>(runtime, size);
96+
arr.updateUnsafe(runtime, (float32_t*)mat.data, size * sizeof(float32_t));
97+
value.setProperty(runtime, "buffer", arr);
98+
}
99+
100+
return value;
101+
});
102+
} else if (propName == "createObject") {
73103
return jsi::Function::createFromHostFunction(
74104
runtime, jsi::PropNameID::forAscii(runtime, "createObject"), 1,
75105
[=](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
@@ -123,6 +153,7 @@ std::vector<jsi::PropNameID> OpenCVPlugin::getPropertyNames(jsi::Runtime& runtim
123153

124154
result.push_back(jsi::PropNameID::forAscii(runtime, "frameBufferToMat"));
125155
result.push_back(jsi::PropNameID::forAscii(runtime, "base64ToMat"));
156+
result.push_back(jsi::PropNameID::forAscii(runtime, "matToBuffer"));
126157
result.push_back(jsi::PropNameID::forAscii(runtime, "createObject"));
127158
result.push_back(jsi::PropNameID::forAscii(runtime, "toJSValue"));
128159
result.push_back(jsi::PropNameID::forAscii(runtime, "copyObjectFromVector"));

docs/pages/apidetails.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ createObject(
1010
type: ObjectType.Mat,
1111
rows: number,
1212
cols: number,
13-
dataType: DataTypes
13+
dataType: DataTypes,
14+
data?: number[]
1415
): Mat;
1516
createObject(type: ObjectType.MatVector): MatVector;
1617
createObject(type: ObjectType.Point, x: number, y: number): Point;
@@ -125,6 +126,21 @@ Creates an object of type Mat based on image in Base64.
125126
base64ToMat(data: string): Mat;
126127
```
127128

129+
### Mat to Buffer
130+
Convert Mat object to Uint8Array or Float32Array based on value of parameter and returns with number of cols, rows and channels.
131+
132+
```js
133+
matToBuffer(
134+
mat: Mat,
135+
type: 'uint8'
136+
): { cols: number; rows: number; channels: number; buffer: Uint8Array };
137+
138+
matToBuffer(
139+
mat: Mat,
140+
type: 'float32'
141+
): { cols: number; rows: number; channels: number; buffer: Float32Array };
142+
```
143+
128144
## Functions
129145

130146
### Invoke function

docs/pages/availablefunctions.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ invoke(name: 'completeSymm', m: MatVector | Mat, lowerToUpper: boolean): void;
249249

250250
### convertFp16
251251

252-
Converts an array to half precision floating number.\
252+
Converts an array to half precision floating number.
253253

254254
- src input array
255255
- dst output array
@@ -285,6 +285,18 @@ invoke(
285285
): void;
286286
```
287287

288+
### convertTo
289+
290+
Converts an array to another data type with optional scaling.
291+
292+
- src input array
293+
- dst output array of the same type as src
294+
- rtype desired output matrix type or, rather, the depth since the number of channels are the same as the input has; if rtype is negative, the output matrix will have the same type as the input.
295+
296+
```js
297+
invoke(name: 'convertTo', src: Mat, dst: Mat, rtype: DataTypes): void;
298+
```
299+
288300
### copyMakeBorder
289301

290302
Forms a border around an image.

src/functions/Core.ts

+9
Original file line numberDiff line numberDiff line change
@@ -903,4 +903,13 @@ export type Core = {
903903
* @param dst output array. It has the same number of cols and depth as the src, and the sum of rows of the src. same depth
904904
*/
905905
invoke(name: 'vconcat', src: MatVector, dst: Mat): void;
906+
907+
/**
908+
* Converts an array to another data type with optional scaling.
909+
* @param name Function name.
910+
* @param src input array.
911+
* @param dst output array of the same type as src
912+
* @param rtype desired output matrix type or, rather, the depth since the number of channels are the same as the input has; if rtype is negative, the output matrix will have the same type as the input.
913+
*/
914+
invoke(name: 'convertTo', src: Mat, dst: Mat, rtype: DataTypes): void;
906915
};

src/utils/UtilsFunctions.ts

+8
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,12 @@ export type UtilsFunctions = {
44
clearBuffers(): void;
55
frameBufferToMat(rows: number, cols: number, input: Uint8Array): Mat;
66
base64ToMat(data: string): Mat;
7+
matToBuffer(
8+
mat: Mat,
9+
type: 'uint8'
10+
): { cols: number; rows: number; channels: number; buffer: Uint8Array };
11+
matToBuffer(
12+
mat: Mat,
13+
type: 'float32'
14+
): { cols: number; rows: number; channels: number; buffer: Float32Array };
715
};

0 commit comments

Comments
 (0)