Skip to content

Commit db7a45c

Browse files
refactor: code improvements
1 parent a2fcc34 commit db7a45c

16 files changed

+102
-53
lines changed

.yarnrc.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
nodeLinker: node-modules
22
nmHoistingLimits: workspaces
3+
checksumBehavior: "update"
34

45
plugins:
56
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

README.md

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
1-
# react-native-fast-opencv
1+
![React Native Fast OpenCV](./docs/images/title-banner.svg)
22

3-
OpenCV port to React Native
3+
A powerful port of **OpenCV** for React Native.
44

5-
## Installation
5+
> The library is in the early stages of development and not all functions or objects available in OpenCV are supported. Add an issue if you have any problems or questions.
66
7-
```sh
8-
npm install react-native-fast-opencv
9-
```
7+
- 🔥 Powered by JSI
8+
- 🏎️ Uses OpenCV C++ API
9+
- 🏛️ New architecture ready
10+
- 🪽 Easy usage, without manual configuration
11+
- 📸 Easy [VisionCamera](https://github.com/mrousavy/react-native-vision-camera) integration
12+
- 🧵 Easy [WorkletsCore](https://github.com/margelo/react-native-worklets-core) integration
13+
- ⛓️‍💥 It can also be used on its own without integration
1014

11-
## Usage
15+
### Motivation
1216

17+
OpenCV is a popular library for image processing, but quite tricky when it comes to its integration and use in React Native applications. My main goal is to enable simple operations directly from JavaScript code.
1318

14-
```js
15-
import { multiply } from 'react-native-fast-opencv';
19+
Currently, there is a port of the OpenCV library but due to the change in standards when developing React Native applications, I decided to create a new library using an API in C++ that will allow for simpler integration, better performance and readability when used in code.
1620

17-
// ...
21+
### Documentation
1822

19-
const result = await multiply(3, 7);
20-
```
23+
Docs are available [here](./docs/pages/index.md).
2124

25+
### State of development
2226

23-
## Contributing
27+
Due to the size of the OpenCV library, this port currently only supports selected objects and functions. However, development is simple enough to only require the addition of specific code in C++ using the functionality.
2428

25-
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
29+
List of available functions are available [here](./docs/pages/availablefunctions.md).
2630

27-
## License
31+
---
2832

29-
MIT
33+
### Credits
34+
Special thanks to:
35+
- The creators of the [Vision Camera](https://github.com/mrousavy/react-native-vision-camera), [WorkletsCore](https://github.com/margelo/react-native-worklets-core) and [FastTFLite](https://github.com/mrousavy/react-native-fast-tflite) libraries, especially Marc Rousavy - thanks for the amazing libraries.
36+
- Creator of the [react-native-opencv3](https://github.com/adamgf/react-native-opencv3) library (Adam G. Freeman) - your library was an important starting point for creating my port.
37+
- Creator of the [TS definition for OpenCV](https://github.com/peteruhnak/opencv-ts) (Peter Uhnak) - the library was an important support for this port.
3038

31-
---
3239

33-
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
40+
41+
### License
42+
MIT © 2024 Lukasz Kurant
43+

cpp/ConvertImage.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//
55
// Created by Łukasz Kurant on 13/08/2024.
66
//
7+
// This methods are from https://stackoverflow.com/a/32270463
8+
// Credits to Miki
79

810
#include "ConvertImage.hpp"
911
#include <opencv2/opencv.hpp>

cpp/FOCV_Function.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <opencv2/opencv.hpp>
1313
#include "FOCV_FunctionArguments.hpp"
1414

15+
// General idea and this function for hashing is from
16+
// https://mrousavy.com/blog/Hashing-String-Ifs
1517
constexpr uint64_t hashString(const char* str, size_t length) {
1618
uint64_t hash = 14695981039346656037ull;
1719
const uint64_t fnv_prime = 1099511628211ull;
@@ -24,6 +26,8 @@ constexpr uint64_t hashString(const char* str, size_t length) {
2426
return hash;
2527
}
2628

29+
// General idea of invocation switch is from react-native-opencv3 library,
30+
// but it was adapted and optimized.
2731
jsi::Object FOCV_Function::invoke(jsi::Runtime& runtime, const jsi::Value* arguments) {
2832
jsi::Object value(runtime);
2933

cpp/FOCV_Object.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <opencv2/opencv.hpp>
1313
#include "ConvertImage.hpp"
1414

15+
// General idea and this function for hashing is from
16+
// https://mrousavy.com/blog/Hashing-String-Ifs
1517
constexpr uint64_t hashString(const char* str, size_t length) {
1618
uint64_t hash = 14695981039346656037ull;
1719
const uint64_t fnv_prime = 1099511628211ull;

cpp/FOCV_Storage.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ std::string FOCV_Storage::save(std::string key, T &item) {
5353
template <typename T>
5454
std::shared_ptr<T> FOCV_Storage::get(std::string key) {
5555
if(!items.contains(key)) {
56-
// Error here!
56+
// TODO: Error here!
5757
}
5858

5959
return std::any_cast<std::shared_ptr<T>>(items.at(key));

cpp/jsi/Promise.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copied from https://github.com/mrousavy/react-native-fast-tflite/blob/main/cpp/jsi/Promise.cpp
2+
// Credits to Marc Rousavy
3+
14
#include "Promise.h"
25
#include <future>
36
#include <jsi/jsi.h>

cpp/jsi/TypedArray.cpp

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
//
2-
// TypedArray.cpp
3-
// VisionCamera
4-
//
5-
// Created by Marc Rousavy on 21.02.23.
6-
// Copyright © 2023 mrousavy. All rights reserved.
7-
//
8-
9-
// Copied & Adapted from
10-
// https://github.com/expo/expo/blob/main/packages/expo-gl/common/EXTypedArrayApi.cpp Credits to
11-
// Expo
1+
// Copied from https://github.com/mrousavy/react-native-fast-tflite/blob/main/cpp/jsi/TypedArray.cpp
2+
// Credits to Marc Rousavy and Expo
123

134
#include "TypedArray.h"
145

cpp/react-native-fast-opencv.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ void OpenCVPlugin::installOpenCV(jsi::Runtime& runtime, std::shared_ptr<react::C
2626
};
2727

2828
auto jsiFunc = jsi::Function::createFromHostFunction(runtime,
29-
jsi::PropNameID::forUtf8(runtime, "__loadOpenCV"),
30-
1,
31-
func);
29+
jsi::PropNameID::forUtf8(runtime, "__loadOpenCV"),
30+
1,
31+
func);
3232

3333
runtime.global().setProperty(runtime, "__loadOpenCV", jsiFunc);
3434

cpp/react-native-fast-opencv.h

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class OpenCVPlugin : public jsi::HostObject {
3636

3737
jsi::Value get(jsi::Runtime& runtime, const jsi::PropNameID& name) override;
3838
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime& runtime) override;
39-
4039
};
4140

4241
#endif /* FASTOPENCV_H */

docs/pages/index.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,12 @@ List of available functions are available [here](./availablefunctions.md).
3232

3333
### Credits
3434
Special thanks to:
35-
- The creators of the [Vision Camera](https://github.com/mrousavy/react-native-vision-camera) and [WorkletsCore](https://github.com/margelo/react-native-worklets-core) libraries, especially Marc Rousavy - thanks for the amazing libraries.
35+
- The creators of the [Vision Camera](https://github.com/mrousavy/react-native-vision-camera), [WorkletsCore](https://github.com/margelo/react-native-worklets-core) and [FastTFLite](https://github.com/mrousavy/react-native-fast-tflite) libraries, especially Marc Rousavy - thanks for the amazing libraries.
3636
- Creator of the [react-native-opencv3](https://github.com/adamgf/react-native-opencv3) library (Adam G. Freeman) - your library was an important starting point for creating my port.
3737
- Creator of the [TS definition for OpenCV](https://github.com/peteruhnak/opencv-ts) (Peter Uhnak) - the library was an important support for this port.
3838

3939

4040

4141
### License
42-
MIT
42+
MIT © 2024 Lukasz Kurant
4343

44-
---
45-
46-
2024 Lukasz Kurant

docs/public/favicon.png

3.54 KB
Loading

docs/public/favicon.svg

+7
Loading

docs/theme.config.jsx

+43-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useRouter } from 'next/router';
2+
13
const logo = () => {
24
return (
35
<span style={{ display: 'flex', alignItems: 'center', columnGap: 10 }}>
@@ -13,21 +15,21 @@ const logo = () => {
1315
data-name="Path 16"
1416
d="M-7.119,4.2A8.289,8.289,0,0,1-10.981-5.12a8.289,8.289,0,0,1,8.006-6.143A8.289,8.289,0,0,1,5.032-5.12,8.289,8.289,0,0,1,1.17,4.2l-2.5-4.323A3.3,3.3,0,0,0,.211-3.828,3.3,3.3,0,0,0-2.975-6.272,3.3,3.3,0,0,0-6.16-3.828,3.3,3.3,0,0,0-4.623-.119Z"
1517
transform="translate(23.58 11.263)"
16-
fill="#409ded"
18+
fill="#409ded"
1719
/>
1820
<path
1921
id="Path_17"
2022
data-name="Path 17"
2123
d="M4.145,15.467a8.289,8.289,0,1,1,8.289,0l-2.5-4.323a3.3,3.3,0,1,0-3.3,0Z"
2224
transform="translate(38.36 32.945) rotate(180)"
23-
fill="#409ded"
25+
fill="#409ded"
2426
/>
2527
<path
2628
id="Path_18"
2729
data-name="Path 18"
2830
d="M4.145,15.468a8.289,8.289,0,1,1,8.289,0l-2.5-4.323a3.3,3.3,0,1,0-3.3,0Z"
2931
transform="translate(8.29 35.979) rotate(-119.998)"
30-
fill="#409ded"
32+
fill="#409ded"
3133
/>
3234
</g>
3335
</svg>
@@ -39,7 +41,43 @@ const logo = () => {
3941
export default {
4042
logo,
4143
project: {
42-
link: 'https://github.com/shuding/nextra',
44+
link: 'https://github.com/lukaszkurantdev/react-native-fast-opencv',
45+
},
46+
docsRepositoryBase:
47+
'https://github.com/lukaszkurantdev/react-native-fast-opencv',
48+
useNextSeoProps() {
49+
const { asPath } = useRouter();
50+
if (asPath !== '/') {
51+
return {
52+
titleTemplate: '%s – React Native Fast OpenCV',
53+
};
54+
}
55+
56+
return {
57+
titleTemplate: 'React Native Fast OpenCV',
58+
};
59+
},
60+
head: (
61+
<>
62+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
63+
<meta property="og:title" content="React Native Fast OpenCV" />
64+
<meta
65+
property="og:description"
66+
content="A powerful port of OpenCV for React Native."
67+
/>
68+
<link rel="icon" href="/favicon.svg" type="image/svg+xml" />
69+
<link rel="icon" href="/favicon.png" type="image/png" />
70+
</>
71+
),
72+
footer: {
73+
text: (
74+
<span>
75+
MIT {new Date().getFullYear()} ©{' '}
76+
<a href="https://lukaszkurant.com" target="_blank">
77+
Lukasz Kurant
78+
</a>
79+
.
80+
</span>
81+
),
4382
},
44-
// ... other theme options
4583
};

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@
9191
},
9292
"peerDependencies": {
9393
"react": "*",
94-
"react-native": "*",
95-
"react-native-fs": "*"
94+
"react-native": "*"
9695
},
9796
"workspaces": [
9897
"example"

src/NativeFastOpencv.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,18 @@ if (global.__loadOpenCV == null) {
1313
typeof OpenCVInstaller.install !== 'function'
1414
) {
1515
console.error(
16-
'Native Worklets Module cannot be found! Make sure you correctly ' +
16+
'Native Fast OpenCV Module cannot be found! Make sure you correctly ' +
1717
'installed native dependencies and rebuilt your app.'
1818
);
1919
} else {
20-
// Install the module
2120
const result = OpenCVInstaller.install();
21+
2222
if (result !== true) {
2323
console.error(
24-
`Native Worklets Module failed to correctly install JSI Bindings! Result: ${result}`
24+
'Native OpenCV Module failed to correctly install JSI Bindings!'
2525
);
26-
} else {
27-
console.log('Worklets loaded successfully');
2826
}
2927
}
30-
} else {
31-
console.log('react-native-worklets-core installed.');
3228
}
3329

3430
export default OpenCVInstaller;

0 commit comments

Comments
 (0)