@react-native-ohos/react-native-blurhash 2.0.4-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/LICENSE +21 -0
  2. package/README.OpenSource +11 -0
  3. package/README.md +13 -0
  4. package/harmony/blurhash/BuildProfile.ets +17 -0
  5. package/harmony/blurhash/build-profile.json5 +9 -0
  6. package/harmony/blurhash/hvigorfile.ts +2 -0
  7. package/harmony/blurhash/index.ets +2 -0
  8. package/harmony/blurhash/oh-package.json5 +13 -0
  9. package/harmony/blurhash/src/main/cpp/Blurhash.cpp +314 -0
  10. package/harmony/blurhash/src/main/cpp/Blurhash.hpp +27 -0
  11. package/harmony/blurhash/src/main/cpp/BlurhashNode.cpp +87 -0
  12. package/harmony/blurhash/src/main/cpp/BlurhashNode.h +35 -0
  13. package/harmony/blurhash/src/main/cpp/BlurhashPackage.h +53 -0
  14. package/harmony/blurhash/src/main/cpp/BlurhashViewComponentInstance.cpp +137 -0
  15. package/harmony/blurhash/src/main/cpp/BlurhashViewComponentInstance.h +34 -0
  16. package/harmony/blurhash/src/main/cpp/BlurhashViewJSIBinder.h +34 -0
  17. package/harmony/blurhash/src/main/cpp/CMakeLists.txt +13 -0
  18. package/harmony/blurhash/src/main/cpp/ComponentDescriptors.h +18 -0
  19. package/harmony/blurhash/src/main/cpp/EventEmitters.cpp +36 -0
  20. package/harmony/blurhash/src/main/cpp/EventEmitters.h +40 -0
  21. package/harmony/blurhash/src/main/cpp/Props.cpp +25 -0
  22. package/harmony/blurhash/src/main/cpp/Props.h +73 -0
  23. package/harmony/blurhash/src/main/cpp/RNBlurhashTurboModule.cpp +42 -0
  24. package/harmony/blurhash/src/main/cpp/RNBlurhashTurboModule.h +19 -0
  25. package/harmony/blurhash/src/main/cpp/ShadowNodes.cpp +15 -0
  26. package/harmony/blurhash/src/main/cpp/ShadowNodes.h +27 -0
  27. package/harmony/blurhash/src/main/cpp/SparseArray.cpp +28 -0
  28. package/harmony/blurhash/src/main/cpp/SparseArray.h +26 -0
  29. package/harmony/blurhash/src/main/cpp/napi_init.cpp +68 -0
  30. package/harmony/blurhash/src/main/cpp/stb_image.h +7656 -0
  31. package/harmony/blurhash/src/main/cpp/stb_image_write.h +1724 -0
  32. package/harmony/blurhash/src/main/cpp/types/libblurhash/Index.d.ts +1 -0
  33. package/harmony/blurhash/src/main/cpp/types/libblurhash/oh-package.json5 +6 -0
  34. package/harmony/blurhash/src/main/ets/BlurhashPackage.ts +58 -0
  35. package/harmony/blurhash/src/main/ets/BlurhashTurboModule.ts +49 -0
  36. package/harmony/blurhash/src/main/ets/BlurhashView.ets +107 -0
  37. package/harmony/blurhash/src/main/ets/Logger.ts +64 -0
  38. package/harmony/blurhash/src/main/ets/RNCBlurhashView.ts +157 -0
  39. package/harmony/blurhash/src/main/ets/RNCSpecs.ts +163 -0
  40. package/harmony/blurhash/src/main/ets/TMSpecs.ts +33 -0
  41. package/harmony/blurhash/src/main/module.json5 +7 -0
  42. package/harmony/blurhash/src/main/resources/base/element/string.json +8 -0
  43. package/harmony/blurhash/src/main/resources/en_US/element/string.json +8 -0
  44. package/harmony/blurhash/src/main/resources/zh_CN/element/string.json +8 -0
  45. package/harmony/blurhash/ts.ts +26 -0
  46. package/harmony/blurhash.har +0 -0
  47. package/lib/commonjs/index.js +95 -0
  48. package/lib/commonjs/index.js.map +1 -0
  49. package/lib/commonjs/specs/NativeBlurhashModule.js +9 -0
  50. package/lib/commonjs/specs/NativeBlurhashModule.js.map +1 -0
  51. package/lib/commonjs/specs/NativeBlurhashView.js +10 -0
  52. package/lib/commonjs/specs/NativeBlurhashView.js.map +1 -0
  53. package/lib/commonjs/utils.js +56 -0
  54. package/lib/commonjs/utils.js.map +1 -0
  55. package/lib/module/index.js +85 -0
  56. package/lib/module/index.js.map +1 -0
  57. package/lib/module/specs/NativeBlurhashModule.js +3 -0
  58. package/lib/module/specs/NativeBlurhashModule.js.map +1 -0
  59. package/lib/module/specs/NativeBlurhashView.js +3 -0
  60. package/lib/module/specs/NativeBlurhashView.js.map +1 -0
  61. package/lib/module/utils.js +48 -0
  62. package/lib/module/utils.js.map +1 -0
  63. package/lib/typescript/index.d.ts +90 -0
  64. package/lib/typescript/specs/NativeBlurhashModule.d.ts +9 -0
  65. package/lib/typescript/specs/NativeBlurhashView.d.ts +19 -0
  66. package/lib/typescript/utils.d.ts +22 -0
  67. package/package.json +113 -0
  68. package/react-native-blurhash.podspec +34 -0
  69. package/src/index.tsx +131 -0
  70. package/src/specs/NativeBlurhashModule.ts +9 -0
  71. package/src/specs/NativeBlurhashView.ts +19 -0
  72. package/src/utils.ts +147 -0
@@ -0,0 +1,137 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #include "BlurhashViewComponentInstance.h"
8
+ #include "Blurhash.hpp"
9
+ #include "Props.h"
10
+ #include <filemanagement/file_uri/oh_file_uri.h>
11
+ #include "RNBlurhashTurboModule.h"
12
+ #include <filemanagement/environment/error_code.h>
13
+ #include <iomanip>
14
+ #include <react/renderer/core/ConcreteState.h>
15
+ #include <sstream>
16
+ #include <cstdio>
17
+
18
+ namespace rnoh {
19
+
20
+ BlurhashViewComponentInstance::BlurhashViewComponentInstance(Context context) : CppComponentInstance(std::move(context)) {
21
+ this->getLocalRootArkUINode().setNodeDelegate(this);
22
+ }
23
+
24
+ void BlurhashViewComponentInstance::onPropsChanged(SharedConcreteProps const &props) {
25
+
26
+ CppComponentInstance::onPropsChanged(props);
27
+
28
+ if (!m_props || m_props->resizeMode != props->resizeMode) {
29
+ this->getLocalRootArkUINode().setResizeMode(convertToImageResizeMode(props->resizeMode));
30
+ }
31
+ // Validate inputs and early-return to avoid passing null/invalid data into ArkUI
32
+ if (props->blurhash == "") {
33
+ if (m_eventEmitter) m_eventEmitter->onLoadError({.message = "The provided Blurhash string must not be empty!"});
34
+ return;
35
+ }
36
+ if (props->decodeWidth <= 0) {
37
+ if (m_eventEmitter) m_eventEmitter->onLoadError({.message = "decodeWidth must be greater than 0!"});
38
+ return;
39
+ }
40
+ if (props->decodeHeight <= 0) {
41
+ if (m_eventEmitter) m_eventEmitter->onLoadError({.message = "decodeHeight must be greater than 0!"});
42
+ return;
43
+ }
44
+ if (props->decodePunch <= 0) {
45
+ if (m_eventEmitter) m_eventEmitter->onLoadError({.message = "decodePunch must be greater than 0!"});
46
+ return;
47
+ }
48
+
49
+ std::string filePath = decodeImageByBlurhash(props->blurhash, props->decodeWidth, props->decodeHeight, props->decodePunch);
50
+ if (filePath.empty()) {
51
+ if (m_eventEmitter) m_eventEmitter->onLoadError({.message = "Failed to decode blurhash to image."});
52
+ return;
53
+ }
54
+
55
+
56
+ // If filePath is already a URI, use it directly
57
+ if (filePath.find("://") != std::string::npos) {
58
+ this->getLocalRootArkUINode().setSource(filePath);
59
+ return;
60
+ }
61
+ // If filePath is an absolute path, use it as-is; otherwise prefix cache dir
62
+ std::string absPath = filePath;
63
+ if (!(absPath.rfind("/", 0) == 0)) {
64
+ absPath = "/data/storage/el2/base/haps/entry/cache/" + absPath;
65
+ }
66
+
67
+ const char *path = absPath.c_str();
68
+ unsigned int length = static_cast<unsigned int>(absPath.size());
69
+ char *result = nullptr;
70
+ FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(path, length, &result);
71
+ if (ret != FileManagement_ErrCode::ERR_OK || result == nullptr) {
72
+ if (m_eventEmitter) m_eventEmitter->onLoadError({.message = "Failed to convert path to file URI."});
73
+ return;
74
+ }
75
+ std::string uriStr(result);
76
+ this->getLocalRootArkUINode().setSource(uriStr);
77
+ free(result);
78
+ }
79
+
80
+ std::string BlurhashViewComponentInstance::decodeImageByBlurhash(const std::string &blurhash, const int &width, const int &height, const float &punch) {
81
+ std::string expectedName = blurhash + ".bmp";
82
+ blurhash::decode(blurhash, width, height, punch);
83
+ auto rnInstance = m_deps->rnInstance.lock();
84
+ auto turboModule = rnInstance->getTurboModule("ImageLoader");
85
+ auto arkTsTurboModule = std::dynamic_pointer_cast<rnoh::ArkTSTurboModule>(turboModule);
86
+ if (!arkTsTurboModule) {
87
+ return expectedName; // fallback filename
88
+ }
89
+ auto cache = arkTsTurboModule->callSync("getPrefetchResult", {expectedName});
90
+ std::string result;
91
+ if (cache.isString()) {
92
+ result = cache.asString();
93
+ }
94
+ if (result.empty()) {
95
+ // in case non-string/unsupported shape returned, fallback
96
+ result = expectedName;
97
+ }
98
+ return result;
99
+ }
100
+
101
+ BlurhashNode &BlurhashViewComponentInstance::getLocalRootArkUINode() { return m_imageNode; }
102
+
103
+ void BlurhashViewComponentInstance::onComplete() {
104
+ if (m_eventEmitter) {
105
+ m_eventEmitter->onLoadStart({});
106
+ m_eventEmitter->onLoadEnd({});
107
+ }
108
+ }
109
+
110
+ void BlurhashViewComponentInstance::onError(int32_t errorCode) {
111
+ if (m_eventEmitter) {
112
+ m_eventEmitter->onLoadError({.message = ""});
113
+ }
114
+ }
115
+
116
+ void BlurhashViewComponentInstance::onLoadStart(SharedConcreteProps const &props) {
117
+ if (m_eventEmitter) {
118
+ // RN 0.77: DirectEventHandler<null>
119
+ m_eventEmitter->onLoadStart({});
120
+ }
121
+ }
122
+
123
+ facebook::react::ImageResizeMode BlurhashViewComponentInstance::convertToImageResizeMode(facebook::react::BlurhashViewResizeMode mode) {
124
+ switch (mode) {
125
+ case facebook::react::BlurhashViewResizeMode::Contain:
126
+ return facebook::react::ImageResizeMode::Contain;
127
+ case facebook::react::BlurhashViewResizeMode::Cover:
128
+ return facebook::react::ImageResizeMode::Cover;
129
+ case facebook::react::BlurhashViewResizeMode::Stretch:
130
+ return facebook::react::ImageResizeMode::Stretch;
131
+ case facebook::react::BlurhashViewResizeMode::Center:
132
+ return facebook::react::ImageResizeMode::Center;
133
+ }
134
+ // Fallback to avoid undefined behavior
135
+ return facebook::react::ImageResizeMode::Cover;
136
+ }
137
+ } // namespace rnoh
@@ -0,0 +1,34 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #pragma once
8
+
9
+ #include "EventEmitters.h"
10
+ #include "BlurhashNode.h"
11
+ #include "ShadowNodes.h"
12
+ #include "RNOH/CppComponentInstance.h"
13
+ #include "RNOH/arkui/ImageNode.h"
14
+
15
+ namespace rnoh {
16
+ class BlurhashViewComponentInstance : public CppComponentInstance<facebook::react::BlurhashViewShadowNode>, public BlurhashNodeDelegate {
17
+ private:
18
+ BlurhashNode m_imageNode;
19
+ std::string m_uri;
20
+
21
+ std::string decodeImageByBlurhash(std::string const &blurhash, int const &width, int const &height, float const &punch);
22
+
23
+ public:
24
+ BlurhashViewComponentInstance(Context context);
25
+ void onPropsChanged(SharedConcreteProps const &props) override;
26
+ void onComplete() override;
27
+ void onLoadStart(SharedConcreteProps const &props);
28
+ void onError(int32_t errorCode) override;
29
+
30
+ facebook::react::ImageResizeMode convertToImageResizeMode(facebook::react::BlurhashViewResizeMode mode);
31
+
32
+ BlurhashNode &getLocalRootArkUINode() override;
33
+ };
34
+ } // namespace rnoh
@@ -0,0 +1,34 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #pragma once
8
+
9
+ #include "RNOHCorePackage/ComponentBinders/ViewComponentJSIBinder.h"
10
+
11
+ namespace rnoh {
12
+ class BlurhashViewJSIBinder : public ViewComponentJSIBinder {
13
+ protected:
14
+ facebook::jsi::Object createNativeProps(facebook::jsi::Runtime &rt) override {
15
+ auto object = ViewComponentJSIBinder::createNativeProps(rt);
16
+ object.setProperty(rt, "imgUri", true);
17
+ object.setProperty(rt, "blurhash", true);
18
+ object.setProperty(rt, "decodeWidth", true);
19
+ object.setProperty(rt, "decodeHeight", true);
20
+ object.setProperty(rt, "decodePunch", true);
21
+ object.setProperty(rt, "decodeAsync", true);
22
+ object.setProperty(rt, "resizeMode", true);
23
+ return object;
24
+ }
25
+
26
+ facebook::jsi::Object createDirectEventTypes(facebook::jsi::Runtime &rt) override {
27
+ facebook::jsi::Object events(rt);
28
+ events.setProperty(rt, "topLoadStart", createDirectEvent(rt, "onLoadStart"));
29
+ events.setProperty(rt, "topLoadEnd", createDirectEvent(rt, "onLoadEnd"));
30
+ events.setProperty(rt, "topLoadError", createDirectEvent(rt, "onLoadError"));
31
+ return events;
32
+ }
33
+ };
34
+ } // namespace rnoh
@@ -0,0 +1,13 @@
1
+ cmake_minimum_required(VERSION 3.13)
2
+ set(CMAKE_VERBOSE_MAKEFILE on)
3
+
4
+ file(GLOB rnoh_blurhash_SRC CONFIGURE_DEPENDS
5
+ *.cpp
6
+ )
7
+ add_library(rnoh_blurhash SHARED ${rnoh_blurhash_SRC})
8
+ target_include_directories(rnoh_blurhash PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
9
+ #target_include_directories(rnoh_blurhash PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/openssl/include)
10
+
11
+ target_link_libraries(rnoh_blurhash PUBLIC rnoh)
12
+ target_link_libraries(rnoh_blurhash PUBLIC libohfileuri.so)
13
+ #target_link_libraries(rnoh_blurhash PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/openssl/lib/libcrypto.a)
@@ -0,0 +1,18 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #pragma once
8
+
9
+ #include "ShadowNodes.h"
10
+ #include <react/renderer/core/ConcreteComponentDescriptor.h>
11
+
12
+ namespace facebook {
13
+ namespace react {
14
+
15
+ using BlurhashViewComponentDescriptor = ConcreteComponentDescriptor<BlurhashViewShadowNode>;
16
+
17
+ } // namespace react
18
+ } // namespace facebook
@@ -0,0 +1,36 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #include "EventEmitters.h"
8
+
9
+
10
+ namespace facebook {
11
+ namespace react {
12
+
13
+ void BlurhashViewEventEmitter::onLoadError(OnLoadError event) const {
14
+ dispatchEvent("LoadError", [event = std::move(event)](jsi::Runtime &runtime) {
15
+ auto payload = jsi::Object(runtime);
16
+ payload.setProperty(runtime, "message", event.message);
17
+ return payload;
18
+ });
19
+ }
20
+
21
+ void BlurhashViewEventEmitter::onLoadEnd(OnLoadEnd event) const {
22
+ dispatchEvent("LoadEnd", [](jsi::Runtime &runtime) {
23
+ auto payload = jsi::Object(runtime);
24
+ return payload;
25
+ });
26
+ }
27
+ void BlurhashViewEventEmitter::onLoadStart(OnLoadStart event) const {
28
+ dispatchEvent("LoadStart", [](jsi::Runtime &runtime) {
29
+ auto payload = jsi::Object(runtime);
30
+ return payload;
31
+ });
32
+ }
33
+
34
+
35
+ } // namespace react
36
+ } // namespace facebook
@@ -0,0 +1,40 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #pragma once
8
+
9
+ #include <react/renderer/components/view/ViewEventEmitter.h>
10
+ #include <jsi/jsi.h>
11
+
12
+ namespace facebook {
13
+ namespace react {
14
+
15
+ class JSI_EXPORT BlurhashViewEventEmitter : public ViewEventEmitter {
16
+ public:
17
+ using ViewEventEmitter::ViewEventEmitter;
18
+
19
+ struct OnLoadStart {
20
+ std::string blurhash;
21
+ int decodeWidth;
22
+ int decodeHeight;
23
+ double decodePunch;
24
+ };
25
+
26
+ struct OnLoadEnd {};
27
+
28
+ struct OnLoadError {
29
+ std::string message;
30
+ };
31
+
32
+ void onLoadError(OnLoadError value) const;
33
+
34
+ void onLoadEnd(OnLoadEnd value) const;
35
+
36
+ void onLoadStart(OnLoadStart value) const;
37
+ };
38
+
39
+ } // namespace react
40
+ } // namespace facebook
@@ -0,0 +1,25 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #include "Props.h"
8
+ #include <react/renderer/core/PropsParserContext.h>
9
+ #include <react/renderer/core/propsConversions.h>
10
+
11
+ namespace facebook {
12
+ namespace react {
13
+
14
+ BlurhashViewProps::BlurhashViewProps(const PropsParserContext &context, const BlurhashViewProps &sourceProps, const RawProps &rawProps)
15
+ : ViewProps(context, sourceProps, rawProps),
16
+ imgUri(convertRawProp(context, rawProps, "imgUri", sourceProps.imgUri, {})),
17
+ blurhash(convertRawProp(context, rawProps, "blurhash", sourceProps.blurhash, {})),
18
+ decodeWidth(convertRawProp(context, rawProps, "decodeWidth", sourceProps.decodeWidth, {})),
19
+ decodeHeight(convertRawProp(context, rawProps, "decodeHeight", sourceProps.decodeHeight, {})),
20
+ decodePunch(convertRawProp(context, rawProps, "decodePunch", sourceProps.decodePunch, {})),
21
+ decodeAsync(convertRawProp(context, rawProps, "decodeAsync", sourceProps.decodeAsync, {})),
22
+ resizeMode(convertRawProp(context, rawProps, "resizeMode", sourceProps.resizeMode, {facebook::react::BlurhashViewResizeMode::Cover})) {}
23
+
24
+ } // namespace react
25
+ } // namespace facebook
@@ -0,0 +1,73 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #pragma once
8
+
9
+ #include <jsi/jsi.h>
10
+ #include <react/renderer/components/view/ViewProps.h>
11
+ #include <react/renderer/core/PropsParserContext.h>
12
+ #include <react/renderer/core/propsConversions.h>
13
+ #include <vector>
14
+ #include <react/renderer/imagemanager/primitives.h>
15
+
16
+ namespace facebook {
17
+ namespace react {
18
+
19
+ enum class BlurhashViewResizeMode { Contain, Cover, Stretch, Center };
20
+
21
+ static inline void fromRawValue(const PropsParserContext &context, const RawValue &value, BlurhashViewResizeMode &result) {
22
+ auto string = (std::string)value;
23
+ if (string == "contain") {
24
+ result = BlurhashViewResizeMode::Contain;
25
+ return;
26
+ }
27
+ if (string == "cover") {
28
+ result = BlurhashViewResizeMode::Cover;
29
+ return;
30
+ }
31
+ if (string == "stretch") {
32
+ result = BlurhashViewResizeMode::Stretch;
33
+ return;
34
+ }
35
+ if (string == "center") {
36
+ result = BlurhashViewResizeMode::Center;
37
+ return;
38
+ }
39
+ abort();
40
+ }
41
+
42
+ static inline std::string toString(const BlurhashViewResizeMode &value) {
43
+ switch (value) {
44
+ case BlurhashViewResizeMode::Contain:
45
+ return "contain";
46
+ case BlurhashViewResizeMode::Cover:
47
+ return "cover";
48
+ case BlurhashViewResizeMode::Stretch:
49
+ return "stretch";
50
+ case BlurhashViewResizeMode::Center:
51
+ return "center";
52
+ }
53
+ }
54
+
55
+ enum class decodeWeight { defaltWeight };
56
+
57
+ class JSI_EXPORT BlurhashViewProps final : public ViewProps {
58
+ public:
59
+ BlurhashViewProps() = default;
60
+ BlurhashViewProps(const PropsParserContext &context, const BlurhashViewProps &sourceProps, const RawProps &rawProps);
61
+
62
+ #pragma mark - Props
63
+ std::string imgUri{};
64
+ std::string blurhash{};
65
+ int decodeWidth{};
66
+ int decodeHeight{};
67
+ double decodePunch{};
68
+ bool decodeAsync{};
69
+ BlurhashViewResizeMode resizeMode{BlurhashViewResizeMode::Cover};
70
+ };
71
+
72
+ } // namespace react
73
+ } // namespace facebook
@@ -0,0 +1,42 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #include "RNBlurhashTurboModule.h"
8
+ #include "Blurhash.hpp"
9
+ #include <sstream>
10
+ #include <jsi/jsi.h>
11
+ #include <react/renderer/components/view/ViewProps.h>
12
+ #include <react/renderer/core/propsConversions.h>
13
+ #include <react/renderer/imagemanager/primitives.h>
14
+ #include <string>
15
+
16
+ using namespace rnoh;
17
+ using namespace facebook;
18
+
19
+ using Object = std::optional<jsi::Object>;
20
+
21
+ static jsi::Value __hostFunction_RNBlurhashInstallModule_createBlurhashFromImage(jsi::Runtime &rt, react::TurboModule &turboModule, const jsi::Value *args,
22
+ size_t count) {
23
+
24
+ return static_cast<ArkTSTurboModule &>(turboModule).callAsync(rt, "createBlurhashFromImage", args, count);
25
+ }
26
+
27
+
28
+ void RNBlurhashTurboModule::clearCosineCache() {
29
+ ::blurhash::clearCache();
30
+ return;
31
+ }
32
+
33
+ static jsi::Value __hostFunction_RNBlurhashInstallModule_clearCosineCache(jsi::Runtime &rt, react::TurboModule &turboModule, const jsi::Value *args, size_t count) {
34
+ static_cast<RNBlurhashTurboModule *>(&turboModule)->clearCosineCache();
35
+ return jsi::Value::undefined();
36
+ };
37
+
38
+
39
+ RNBlurhashTurboModule::RNBlurhashTurboModule(const ArkTSTurboModule::Context ctx, const std::string name) : ArkTSTurboModule(ctx, name) {
40
+ methodMap_["clearCosineCache"] = MethodMetadata{0, __hostFunction_RNBlurhashInstallModule_clearCosineCache};
41
+ methodMap_["createBlurhashFromImage"] = MethodMetadata{4, __hostFunction_RNBlurhashInstallModule_createBlurhashFromImage};
42
+ }
@@ -0,0 +1,19 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+ #pragma once
7
+
8
+ #include <ReactCommon/TurboModule.h>
9
+ #include "RNOH/ArkTSTurboModule.h"
10
+ namespace rnoh {
11
+
12
+ class JSI_EXPORT RNBlurhashTurboModule : public ArkTSTurboModule {
13
+ public:
14
+ RNBlurhashTurboModule(const ArkTSTurboModule::Context ctx, const std::string name);
15
+
16
+ void clearCosineCache();
17
+
18
+ };
19
+ } // namespace rnoh
@@ -0,0 +1,15 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #include "ShadowNodes.h"
8
+
9
+ namespace facebook {
10
+ namespace react {
11
+
12
+ extern const char BlurhashViewComponentName[] = "BlurhashView";
13
+
14
+ } // namespace react
15
+ } // namespace facebook
@@ -0,0 +1,27 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #pragma once
8
+ #include "Props.h"
9
+ #include "EventEmitters.h"
10
+ #include <react/renderer/components/view/ConcreteViewShadowNode.h>
11
+ #include <jsi/jsi.h>
12
+
13
+ namespace facebook {
14
+ namespace react {
15
+
16
+ JSI_EXPORT extern const char BlurhashViewComponentName[];
17
+
18
+ /*
19
+ * `ShadowNode` for <BlurhashView> component.
20
+ */
21
+ using BlurhashViewShadowNode = ConcreteViewShadowNode<
22
+ BlurhashViewComponentName,
23
+ BlurhashViewProps,
24
+ BlurhashViewEventEmitter>;
25
+
26
+ } // namespace react
27
+ } // namespace facebook
@@ -0,0 +1,28 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #include "SparseArray.h"
8
+ #include <stdexcept>
9
+
10
+ template <typename T> void SparseArray<T>::set(int key, const T &value) { data[key] = value; }
11
+
12
+ template <typename T> T SparseArray<T>::get(int key) const {
13
+ auto it = data.find(key);
14
+ if (it != data.end()) {
15
+ return it->second;
16
+ }
17
+ throw std::runtime_error("Key not found");
18
+ }
19
+
20
+ template <typename T> void SparseArray<T>::remove(int key) { data.erase(key); }
21
+
22
+ template <typename T> void SparseArray<T>::clear() { data.clear(); }
23
+
24
+ template <typename T> bool SparseArray<T>::containsKey(int key) const { return data.find(key) != data.end(); }
25
+
26
+ // 显式实例化模板类
27
+ template class SparseArray<std::vector<double>>;
28
+ template class SparseArray<std::vector<int>>;
@@ -0,0 +1,26 @@
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #ifndef SPARSEARRAY_H
8
+ #define SPARSEARRAY_H
9
+
10
+ #include <unordered_map>
11
+ #include <vector>
12
+
13
+ template <typename T>
14
+ class SparseArray {
15
+ private:
16
+ std::unordered_map<int, T> data;
17
+
18
+ public:
19
+ void set(int key, const T &value);
20
+ T get(int key) const;
21
+ void remove(int key);
22
+ void clear();
23
+ bool containsKey(int key) const;
24
+ };
25
+
26
+ #endif // SPARSEARRAY_H
@@ -0,0 +1,68 @@
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (C) 2024 Huawei Device Co., Ltd.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ #include "RNOH/ArkJS.h"
26
+ #include "Blurhash.hpp"
27
+ #include "napi/native_api.h"
28
+
29
+
30
+ static napi_value getEncode(napi_env env, napi_callback_info info){
31
+ ArkJS arkJs(env);
32
+ auto args = arkJs.getCallbackArgs(info, 3);
33
+ std::string filename = arkJs.getString(args[0]);
34
+ int components_x = arkJs.getInteger(args[1]);
35
+ int components_y = arkJs.getInteger(args[2]);
36
+
37
+ std::string blurhashcode = blurhash::encode(filename.c_str(),components_x,components_y);
38
+
39
+ napi_value val;
40
+ napi_create_string_utf8(env, blurhashcode.c_str(), blurhashcode.size(), &val);
41
+ return val;
42
+ }
43
+
44
+ EXTERN_C_START
45
+ static napi_value Init(napi_env env, napi_value exports)
46
+ {
47
+ napi_property_descriptor desc[] = {
48
+ { "getEncode", nullptr, getEncode, nullptr, nullptr, nullptr, napi_default, nullptr }
49
+ };
50
+ napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
51
+ return exports;
52
+ }
53
+ EXTERN_C_END
54
+
55
+ static napi_module blurhashModule = {
56
+ .nm_version = 1,
57
+ .nm_flags = 0,
58
+ .nm_filename = nullptr,
59
+ .nm_register_func = Init,
60
+ .nm_modname = "rnoh_blurhash",
61
+ .nm_priv = ((void*)0),
62
+ .reserved = { 0 },
63
+ };
64
+
65
+ extern "C" __attribute__((constructor)) void RegisterNdktesterModule(void)
66
+ {
67
+ napi_module_register(&blurhashModule);
68
+ }