Skip to content

Commit c595143

Browse files
Merge pull request #67 from lukaszkurantdev/feat/bufferToMat
feat: buffer to mat support
2 parents 01a7e2b + db4ea32 commit c595143

7 files changed

+111
-8
lines changed

cpp/react-native-fast-opencv.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,70 @@ jsi::Value OpenCVPlugin::get(jsi::Runtime& runtime, const jsi::PropNameID& propN
7171
memcpy(mat.data, vec.data(), (int)rows * (int)cols * (int)channels);
7272
auto id = FOCV_Storage::save(mat);
7373

74+
return FOCV_JsiObject::wrap(runtime, "mat", id);
75+
});
76+
} else if (propName == "bufferToMat") {
77+
return jsi::Function::createFromHostFunction(
78+
runtime, jsi::PropNameID::forAscii(runtime, "bufferToMat"), 4,
79+
[=](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
80+
size_t count) -> jsi::Object {
81+
auto type = arguments[0].asString(runtime).utf8(runtime);
82+
auto rows = arguments[1].asNumber();
83+
auto cols = arguments[2].asNumber();
84+
auto channels = arguments[3].asNumber();
85+
auto input = arguments[4].asObject(runtime);
86+
87+
auto modeType = -1;
88+
auto typeSize = 1;
89+
90+
if(type == "uint8") {
91+
typeSize = 1;
92+
if (channels == 1) modeType = CV_8U;
93+
if (channels == 3) modeType = CV_8UC3;
94+
if (channels == 4) modeType = CV_8UC4;
95+
} else if(type == "uint16") {
96+
typeSize = 2;
97+
if (channels == 1) modeType = CV_16U;
98+
if (channels == 3) modeType = CV_16UC3;
99+
if (channels == 4) modeType = CV_16UC4;
100+
} else if(type == "int8") {
101+
typeSize = 1;
102+
if (channels == 1) modeType = CV_8S;
103+
if (channels == 3) modeType = CV_8SC3;
104+
if (channels == 4) modeType = CV_8SC4;
105+
} else if(type == "int16") {
106+
typeSize = 2;
107+
if (channels == 1) modeType = CV_16S;
108+
if (channels == 3) modeType = CV_16SC3;
109+
if (channels == 4) modeType = CV_16SC4;
110+
} else if(type == "int32") {
111+
typeSize = 4;
112+
if (channels == 1) modeType = CV_32S;
113+
if (channels == 3) modeType = CV_32SC3;
114+
if (channels == 4) modeType = CV_32SC4;
115+
} else if(type == "float32") {
116+
typeSize = 4;
117+
if (channels == 1) modeType = CV_32F;
118+
if (channels == 3) modeType = CV_32FC3;
119+
if (channels == 4) modeType = CV_32FC4;
120+
} else if(type == "float64") {
121+
typeSize = 8;
122+
if (channels == 1) modeType = CV_64F;
123+
if (channels == 3) modeType = CV_64FC3;
124+
if (channels == 4) modeType = CV_64FC4;
125+
}
126+
127+
if (channels == -1) {
128+
throw std::runtime_error("Fast OpenCV Error: Invalid channel count passed to frameBufferToMat!");
129+
}
130+
131+
auto inputBuffer = getTypedArray(runtime, std::move(input));
132+
auto vec = inputBuffer.toVector(runtime);
133+
134+
cv::Mat mat(rows, cols, modeType);
135+
memcpy(mat.data, vec.data(), (int)rows * (int)cols * (int)channels * typeSize);
136+
auto id = FOCV_Storage::save(mat);
137+
74138
return FOCV_JsiObject::wrap(runtime, "mat", id);
75139
});
76140
}

docs/pages/apidetails.md

+23-3
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,32 @@ Clears stored objects from memory.
130130
clearBuffers(idsToKeep?: string[]): void;
131131
```
132132

133-
### Frame Buffer to Mat
133+
### Buffer to Mat
134134

135-
Creates an object of type Mat based on an array of Uint8Array.
135+
Creates an object of type Mat based on an array of Buffer Array.
136136

137137
```js
138-
frameBufferToMat(rows: number, cols: number, channels: number, input: Uint8Array): Mat;
138+
bufferToMat<T extends keyof ImportBufferType>(
139+
type: T,
140+
rows: number,
141+
cols: number,
142+
channels: 1 | 3 | 4,
143+
input: ImportBufferType[T]
144+
): Mat;
145+
```
146+
147+
where `ImportBufferType` is:
148+
149+
```ts
150+
type ImportBufferType = {
151+
uint8: Uint8Array;
152+
uint16: Uint16Array;
153+
int8: Int8Array;
154+
int16: Int16Array;
155+
int32: Int32Array;
156+
float32: Float32Array;
157+
float64: Float64Array;
158+
};
139159
```
140160

141161
### Base64 to Mat

example/ios/Podfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ PODS:
936936
- React-Mapbuffer (0.74.4):
937937
- glog
938938
- React-debug
939-
- react-native-fast-opencv (0.4.0):
939+
- react-native-fast-opencv (0.4.1):
940940
- DoubleConversion
941941
- FastOpenCV-iOS (= 1.0.4)
942942
- glog
@@ -1633,7 +1633,7 @@ SPEC CHECKSUMS:
16331633
React-jsitracing: 7246bbdc12aa5ac7f25f5d14eb0e080038d75b00
16341634
React-logger: b4440a25b9c41b28042d289998d90b18c79ce2b0
16351635
React-Mapbuffer: fe1b4b0aa9c3fb49768dee7e322ee33d2dd6929e
1636-
react-native-fast-opencv: ec12ddebad1905298f63036fb9dfbfb5a1cfa502
1636+
react-native-fast-opencv: 933f133ed6a319c8b18e105279f9f93efd39f92f
16371637
react-native-image-picker: 6f7695f6e5aa43dc6275cbb3198cfa066a2f5be4
16381638
react-native-safe-area-context: b13be9714d9771fbde0120bc519c963484de3a71
16391639
react-native-skia: d0f075344d4e9333110c4f82dcf86d61105e90f9

example/src/examples/CameraPassthrough.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export function CameraPassthrough() {
130130
rotation: '90deg',
131131
});
132132

133-
const frameMat = OpenCV.frameBufferToMat(HEIGHT, WIDTH, 4, resized);
133+
const frameMat = OpenCV.bufferToMat('uint8', HEIGHT, WIDTH, 4, resized);
134134
const output = OpenCV.matToBuffer(frameMat, 'uint8');
135135
const data = Skia.Data.fromBytes(output.buffer);
136136

example/src/examples/CameraRealtimeDetection.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function CameraRealtimeDetection() {
4747
dataType: 'uint8',
4848
});
4949

50-
const src = OpenCV.frameBufferToMat(height, width, 3, resized);
50+
const src = OpenCV.bufferToMat('uint8', height, width, 3, resized);
5151
const dst = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
5252

5353
const lowerBound = OpenCV.createObject(ObjectType.Scalar, 30, 60, 60);

example/src/examples/DocumentDetection.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function DocumentDetection() {
5757
},
5858
});
5959

60-
const source = OpenCV.frameBufferToMat(height, width, 3, resized);
60+
const source = OpenCV.bufferToMat('uint8', height, width, 3, resized);
6161

6262
OpenCV.invoke(
6363
'cvtColor',

src/utils/UtilsFunctions.ts

+19
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ type BufferType = {
1111
float64: Float64Array;
1212
};
1313

14+
type ImportBufferType = Omit<BufferType, 'uint32'>;
15+
1416
export type UtilsFunctions = {
1517
/**
1618
* Clears any buffers that were allocate to back Mats on the native side.
1719
*/
1820
clearBuffers(idsToKeep?: string[]): void;
1921
/**
2022
* Converts a byte array to a Mat.
23+
*
24+
* @deprecated Use the new `bufferToMat` function instead.
25+
*
2126
* @param rows - the number of rows in the Mat
2227
* @param cols - the number of columns in the Mat
2328
* @param channels - the number of channels in the Mat
@@ -29,6 +34,20 @@ export type UtilsFunctions = {
2934
channels: number,
3035
input: Uint8Array
3136
): Mat;
37+
/**
38+
* Converts a byte array to a Mat.
39+
* @param rows - the number of rows in the Mat
40+
* @param cols - the number of columns in the Mat
41+
* @param channels - the number of channels in the Mat
42+
* @param input - the byte array to convert
43+
*/
44+
bufferToMat<T extends keyof ImportBufferType>(
45+
type: T,
46+
rows: number,
47+
cols: number,
48+
channels: 1 | 3 | 4,
49+
input: ImportBufferType[T]
50+
): Mat;
3251
/**
3352
* Converts a base64 string to a Mat.
3453
* @param data - the base64 string to convert

0 commit comments

Comments
 (0)