1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci#include <js_native_api.h>
17f6603c60Sopenharmony_ci#include "napi/native_api.h"
18f6603c60Sopenharmony_ci#include "native_effect/effect_filter.h"
19f6603c60Sopenharmony_ci#include "native_effect/effect_types.h"
20f6603c60Sopenharmony_ci#include "hilog/log.h"
21f6603c60Sopenharmony_ci
22f6603c60Sopenharmony_ci#define SUCCESS 0
23f6603c60Sopenharmony_ci#define FAIL (-1)
24f6603c60Sopenharmony_ci
25f6603c60Sopenharmony_cistatic napi_value OHFilterCreateEffect(napi_env env, napi_callback_info info)
26f6603c60Sopenharmony_ci{
27f6603c60Sopenharmony_ci    napi_value result = nullptr;
28f6603c60Sopenharmony_ci    OH_PixelmapNative *pixmap = nullptr;
29f6603c60Sopenharmony_ci    OH_PixelmapNative **pixMap = &pixmap;
30f6603c60Sopenharmony_ci    OH_Filter *filter = nullptr;
31f6603c60Sopenharmony_ci    EffectErrorCode effectErrorCode = OH_Filter_CreateEffect(*pixMap, &filter);
32f6603c60Sopenharmony_ci    if (effectErrorCode != EffectErrorCode::EFFECT_SUCCESS) {
33f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
34f6603c60Sopenharmony_ci    } else {
35f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
36f6603c60Sopenharmony_ci    }
37f6603c60Sopenharmony_ci    return result;
38f6603c60Sopenharmony_ci}
39f6603c60Sopenharmony_ci
40f6603c60Sopenharmony_cistatic napi_value OHFilterInvert(napi_env env, napi_callback_info info)
41f6603c60Sopenharmony_ci{
42f6603c60Sopenharmony_ci    napi_value result = nullptr;
43f6603c60Sopenharmony_ci    OH_Filter *filter = nullptr;
44f6603c60Sopenharmony_ci    EffectErrorCode effectErrorCode = OH_Filter_Invert(filter);
45f6603c60Sopenharmony_ci    if (effectErrorCode != EffectErrorCode::EFFECT_SUCCESS) {
46f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
47f6603c60Sopenharmony_ci    } else {
48f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
49f6603c60Sopenharmony_ci    }
50f6603c60Sopenharmony_ci    return result;
51f6603c60Sopenharmony_ci}
52f6603c60Sopenharmony_ci
53f6603c60Sopenharmony_cistatic napi_value OHFilterBlur(napi_env env, napi_callback_info info)
54f6603c60Sopenharmony_ci{
55f6603c60Sopenharmony_ci    napi_value result = nullptr;
56f6603c60Sopenharmony_ci    OH_Filter *filter = nullptr;
57f6603c60Sopenharmony_ci    EffectErrorCode effectErrorCode = OH_Filter_Blur(filter, 0.5f);
58f6603c60Sopenharmony_ci    if (effectErrorCode != EffectErrorCode::EFFECT_SUCCESS) {
59f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
60f6603c60Sopenharmony_ci    } else {
61f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
62f6603c60Sopenharmony_ci    }
63f6603c60Sopenharmony_ci    return result;
64f6603c60Sopenharmony_ci}
65f6603c60Sopenharmony_ci
66f6603c60Sopenharmony_cistatic napi_value OHFilterGrayScale(napi_env env, napi_callback_info info)
67f6603c60Sopenharmony_ci{
68f6603c60Sopenharmony_ci    napi_value result = nullptr;
69f6603c60Sopenharmony_ci    OH_Filter *filter = nullptr;
70f6603c60Sopenharmony_ci    EffectErrorCode effectErrorCode = OH_Filter_GrayScale(filter);
71f6603c60Sopenharmony_ci    if (effectErrorCode != EffectErrorCode::EFFECT_SUCCESS) {
72f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
73f6603c60Sopenharmony_ci    } else {
74f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
75f6603c60Sopenharmony_ci    }
76f6603c60Sopenharmony_ci    return result;
77f6603c60Sopenharmony_ci}
78f6603c60Sopenharmony_ci
79f6603c60Sopenharmony_cistatic napi_value OHFilterBrighten(napi_env env, napi_callback_info info)
80f6603c60Sopenharmony_ci{
81f6603c60Sopenharmony_ci    napi_value result = nullptr;
82f6603c60Sopenharmony_ci    OH_Filter *filter = nullptr;
83f6603c60Sopenharmony_ci    EffectErrorCode effectErrorCode = OH_Filter_Brighten(filter, 0.5);
84f6603c60Sopenharmony_ci    if (effectErrorCode != EffectErrorCode::EFFECT_SUCCESS) {
85f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
86f6603c60Sopenharmony_ci    } else {
87f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
88f6603c60Sopenharmony_ci    }
89f6603c60Sopenharmony_ci    return result;
90f6603c60Sopenharmony_ci}
91f6603c60Sopenharmony_ci
92f6603c60Sopenharmony_cistatic napi_value OHFilterSetColorMatrix(napi_env env, napi_callback_info info)
93f6603c60Sopenharmony_ci{
94f6603c60Sopenharmony_ci    napi_value result = nullptr;
95f6603c60Sopenharmony_ci    OH_Filter *filter = nullptr;
96f6603c60Sopenharmony_ci    OH_Filter_ColorMatrix matrix {
97f6603c60Sopenharmony_ci        -1.0, 0, 0, 0, 1,
98f6603c60Sopenharmony_ci        0, -1.0, 0, 0, 1,
99f6603c60Sopenharmony_ci        0, 0, -1.0, 0, 1,
100f6603c60Sopenharmony_ci        0, 0, 0, 1, 0
101f6603c60Sopenharmony_ci    };
102f6603c60Sopenharmony_ci    EffectErrorCode effectErrorCode = OH_Filter_SetColorMatrix(filter, &matrix);
103f6603c60Sopenharmony_ci    if (effectErrorCode != EffectErrorCode::EFFECT_SUCCESS) {
104f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
105f6603c60Sopenharmony_ci    } else {
106f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
107f6603c60Sopenharmony_ci    }
108f6603c60Sopenharmony_ci    return result;
109f6603c60Sopenharmony_ci}
110f6603c60Sopenharmony_ci
111f6603c60Sopenharmony_cistatic napi_value OHFilterGetEffectPixelMap(napi_env env, napi_callback_info info)
112f6603c60Sopenharmony_ci{
113f6603c60Sopenharmony_ci    napi_value result = nullptr;
114f6603c60Sopenharmony_ci    OH_PixelmapNative *pixmap = nullptr;
115f6603c60Sopenharmony_ci    OH_Filter *filter = nullptr;
116f6603c60Sopenharmony_ci    EffectErrorCode effectErrorCode = OH_Filter_GetEffectPixelMap(filter, &pixmap);
117f6603c60Sopenharmony_ci    if (effectErrorCode != EffectErrorCode::EFFECT_SUCCESS) {
118f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
119f6603c60Sopenharmony_ci    } else {
120f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
121f6603c60Sopenharmony_ci    }
122f6603c60Sopenharmony_ci    return result;
123f6603c60Sopenharmony_ci}
124f6603c60Sopenharmony_ci
125f6603c60Sopenharmony_cistatic napi_value OHFilterRelease(napi_env env, napi_callback_info info)
126f6603c60Sopenharmony_ci{
127f6603c60Sopenharmony_ci    napi_value result = nullptr;
128f6603c60Sopenharmony_ci    OH_Filter *filter = nullptr;
129f6603c60Sopenharmony_ci    EffectErrorCode effectErrorCode = OH_Filter_Release(filter);
130f6603c60Sopenharmony_ci    if (effectErrorCode != EffectErrorCode::EFFECT_SUCCESS) {
131f6603c60Sopenharmony_ci        napi_create_int32(env, SUCCESS, &result);
132f6603c60Sopenharmony_ci    } else {
133f6603c60Sopenharmony_ci        napi_create_int32(env, FAIL, &result);
134f6603c60Sopenharmony_ci    }
135f6603c60Sopenharmony_ci    return result;
136f6603c60Sopenharmony_ci}
137f6603c60Sopenharmony_ci
138f6603c60Sopenharmony_ciEXTERN_C_START
139f6603c60Sopenharmony_cistatic napi_value Init(napi_env env, napi_value exports)
140f6603c60Sopenharmony_ci{
141f6603c60Sopenharmony_ci    napi_property_descriptor desc[] = {
142f6603c60Sopenharmony_ci        {"oHFilterCreateEffect", nullptr, OHFilterCreateEffect, nullptr, nullptr, nullptr,
143f6603c60Sopenharmony_ci         napi_default, nullptr},
144f6603c60Sopenharmony_ci        {"oHFilterInvert", nullptr, OHFilterInvert, nullptr, nullptr, nullptr,
145f6603c60Sopenharmony_ci         napi_default, nullptr},
146f6603c60Sopenharmony_ci        {"oHFilterBlur", nullptr, OHFilterBlur, nullptr, nullptr, nullptr,
147f6603c60Sopenharmony_ci         napi_default, nullptr},
148f6603c60Sopenharmony_ci        {"oHFilterGrayScale", nullptr, OHFilterGrayScale, nullptr, nullptr, nullptr,
149f6603c60Sopenharmony_ci         napi_default, nullptr},
150f6603c60Sopenharmony_ci        {"oHFilterBrighten", nullptr, OHFilterBrighten, nullptr, nullptr, nullptr,
151f6603c60Sopenharmony_ci         napi_default, nullptr},
152f6603c60Sopenharmony_ci        {"oHFilterSetColorMatrix", nullptr, OHFilterSetColorMatrix, nullptr, nullptr, nullptr,
153f6603c60Sopenharmony_ci         napi_default, nullptr},
154f6603c60Sopenharmony_ci        {"oHFilterGetEffectPixelMap", nullptr, OHFilterGetEffectPixelMap, nullptr, nullptr, nullptr,
155f6603c60Sopenharmony_ci         napi_default, nullptr},
156f6603c60Sopenharmony_ci        {"oHFilterRelease", nullptr, OHFilterRelease, nullptr, nullptr, nullptr,
157f6603c60Sopenharmony_ci         napi_default, nullptr},
158f6603c60Sopenharmony_ci    };
159f6603c60Sopenharmony_ci    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
160f6603c60Sopenharmony_ci    return exports;
161f6603c60Sopenharmony_ci}
162f6603c60Sopenharmony_ci
163f6603c60Sopenharmony_ciEXTERN_C_END
164f6603c60Sopenharmony_ci
165f6603c60Sopenharmony_cistatic napi_module demoModule = {
166f6603c60Sopenharmony_ci    .nm_version = 1,
167f6603c60Sopenharmony_ci    .nm_flags = 0,
168f6603c60Sopenharmony_ci    .nm_filename = nullptr,
169f6603c60Sopenharmony_ci    .nm_register_func = Init,
170f6603c60Sopenharmony_ci    .nm_modname = "nativeEffectNdk",
171f6603c60Sopenharmony_ci    .nm_priv = ((void *)0),
172f6603c60Sopenharmony_ci    .reserved = {0},
173f6603c60Sopenharmony_ci};
174f6603c60Sopenharmony_ci
175f6603c60Sopenharmony_ciextern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }