Skip to content

feat: mat to buffer (#3) #4

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
Sep 3, 2024
Merged
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
7 changes: 7 additions & 0 deletions cpp/FOCV_Function.cpp
Original file line number Diff line number Diff line change
@@ -1266,6 +1266,13 @@ jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* argum
auto id = FOCV_Storage::save(rect);
return FOCV_JsiObject::wrap(runtime, "rotated_rect", id);
} break;
case hashString("convertTo", 9): {
auto src = args.asMatPtr(1);
auto dst = args.asMatPtr(2);
auto rtype = args.asNumber(3);

(*src).convertTo(*dst, rtype);
} break;
}

return value;
35 changes: 33 additions & 2 deletions cpp/react-native-fast-opencv.cpp
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ jsi::Value OpenCVPlugin::get(jsi::Runtime& runtime, const jsi::PropNameID& propN
return FOCV_JsiObject::wrap(runtime, "mat", id);
});
}
if (propName == "base64ToMat") {
else if (propName == "base64ToMat") {
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, "frameBufferToMat"), 1,
[=](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
return FOCV_JsiObject::wrap(runtime, "mat", id);
});
}
else if (propName == "createObject") {
else if (propName == "matToBuffer") {
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, "matToBuffer"), 1,
[=](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, (float32_t*)mat.data, size * sizeof(float32_t));
value.setProperty(runtime, "buffer", arr);
}

return value;
});
} else if (propName == "createObject") {
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, "createObject"), 1,
[=](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments,
@@ -123,6 +153,7 @@ std::vector<jsi::PropNameID> OpenCVPlugin::getPropertyNames(jsi::Runtime& runtim

result.push_back(jsi::PropNameID::forAscii(runtime, "frameBufferToMat"));
result.push_back(jsi::PropNameID::forAscii(runtime, "base64ToMat"));
result.push_back(jsi::PropNameID::forAscii(runtime, "matToBuffer"));
result.push_back(jsi::PropNameID::forAscii(runtime, "createObject"));
result.push_back(jsi::PropNameID::forAscii(runtime, "toJSValue"));
result.push_back(jsi::PropNameID::forAscii(runtime, "copyObjectFromVector"));
18 changes: 17 additions & 1 deletion docs/pages/apidetails.md
Original file line number Diff line number Diff line change
@@ -10,7 +10,8 @@ createObject(
type: ObjectType.Mat,
rows: number,
cols: number,
dataType: DataTypes
dataType: DataTypes,
data?: number[]
): Mat;
createObject(type: ObjectType.MatVector): MatVector;
createObject(type: ObjectType.Point, x: number, y: number): Point;
@@ -125,6 +126,21 @@ Creates an object of type Mat based on image in Base64.
base64ToMat(data: string): Mat;
```

### Mat to Buffer
Convert Mat object to Uint8Array or Float32Array based on value of parameter and returns with number of cols, rows and channels.

```js
matToBuffer(
mat: Mat,
type: 'uint8'
): { cols: number; rows: number; channels: number; buffer: Uint8Array };

matToBuffer(
mat: Mat,
type: 'float32'
): { cols: number; rows: number; channels: number; buffer: Float32Array };
```

## Functions

### Invoke function
14 changes: 13 additions & 1 deletion docs/pages/availablefunctions.md
Original file line number Diff line number Diff line change
@@ -249,7 +249,7 @@ invoke(name: 'completeSymm', m: MatVector | Mat, lowerToUpper: boolean): void;

### convertFp16

Converts an array to half precision floating number.\
Converts an array to half precision floating number.

- src input array
- dst output array
@@ -285,6 +285,18 @@ invoke(
): void;
```

### convertTo

Converts an array to another data type with optional scaling.

- src input array
- dst output array of the same type as src
- 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.

```js
invoke(name: 'convertTo', src: Mat, dst: Mat, rtype: DataTypes): void;
```

### copyMakeBorder

Forms a border around an image.
9 changes: 9 additions & 0 deletions src/functions/Core.ts
Original file line number Diff line number Diff line change
@@ -903,4 +903,13 @@ export type Core = {
* @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
*/
invoke(name: 'vconcat', src: MatVector, dst: Mat): void;

/**
* Converts an array to another data type with optional scaling.
* @param name Function name.
* @param src input array.
* @param dst output array of the same type as src
* @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.
*/
invoke(name: 'convertTo', src: Mat, dst: Mat, rtype: DataTypes): void;
};
8 changes: 8 additions & 0 deletions src/utils/UtilsFunctions.ts
Original file line number Diff line number Diff line change
@@ -4,4 +4,12 @@ export type UtilsFunctions = {
clearBuffers(): void;
frameBufferToMat(rows: number, cols: number, input: Uint8Array): Mat;
base64ToMat(data: string): Mat;
matToBuffer(
mat: Mat,
type: 'uint8'
): { cols: number; rows: number; channels: number; buffer: Uint8Array };
matToBuffer(
mat: Mat,
type: 'float32'
): { cols: number; rows: number; channels: number; buffer: Float32Array };
};

Unchanged files with check annotations Beta

const [result, setResult] = useState<string>('');
const getImageFromGallery = async () => {
const result = await launchImageLibrary({

Check warning on line 13 in example/src/ImageExample.tsx

GitHub Actions / lint

'result' is already declared in the upper scope on line 10 column 10
mediaType: 'photo',
includeBase64: true,
});
});
return (
<SafeAreaView style={{ backgroundColor: 'white', flex: 1 }}>

Check warning on line 49 in example/src/ImageExample.tsx

GitHub Actions / lint

Inline style: { backgroundColor: 'white', flex: 1 }
<Button title="Select photo" onPress={getImageFromGallery} />
<Button title="Process" onPress={() => worklet()} />