1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "image_packer_mdk.h"
17 
18 #include "common_utils.h"
19 #include "image_packer_mdk_kits.h"
20 
21 using namespace OHOS::Media;
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 struct ImagePacker_Native_ {
26     ImagePackerNapi* napi = nullptr;
27     napi_env env = nullptr;
28 };
29 
30 MIDK_EXPORT
OH_ImagePacker_Create(napi_env env,napi_value * res)31 int32_t OH_ImagePacker_Create(napi_env env, napi_value *res)
32 {
33     ImagePackerArgs args;
34     args.inEnv = env;
35     args.outVal = res;
36     return ImagePackerNativeCall(ENV_FUNC_IMAGEPACKER_CREATE, &args);
37 }
38 
39 MIDK_EXPORT
OH_ImagePacker_InitNative(napi_env env,napi_value packer)40 ImagePacker_Native* OH_ImagePacker_InitNative(napi_env env, napi_value packer)
41 {
42     std::unique_ptr<ImagePacker_Native> result = std::make_unique<ImagePacker_Native>();
43     result->napi = ImagePackerNapi_Unwrap(env, packer);
44     if (result->napi == nullptr) {
45         return nullptr;
46     }
47     result->env = env;
48     return result.release();
49 }
50 
51 MIDK_EXPORT
OH_ImagePacker_PackToData(ImagePacker_Native * native,napi_value source,ImagePacker_Opts * opts,uint8_t * outData,size_t * size)52 int32_t OH_ImagePacker_PackToData(ImagePacker_Native* native, napi_value source,
53     ImagePacker_Opts* opts, uint8_t* outData, size_t* size)
54 {
55     if (native == nullptr || native->napi == nullptr || native->env == nullptr) {
56         return IMAGE_RESULT_BAD_PARAMETER;
57     }
58     ImagePackerArgs args;
59     args.inEnv = native->env;
60     args.inNapi = native->napi;
61     args.inVal = source;
62     args.inOpts = opts;
63     args.outData = outData;
64     args.dataSize = size;
65     return ImagePackerNativeCall(CTX_FUNC_IMAGEPACKER_PACKTODATA, &args);
66 }
67 
68 MIDK_EXPORT
OH_ImagePacker_PackToFile(ImagePacker_Native * native,napi_value source,ImagePacker_Opts * opts,int fd)69 int32_t OH_ImagePacker_PackToFile(ImagePacker_Native* native, napi_value source,
70     ImagePacker_Opts* opts, int fd)
71 {
72     if (native == nullptr || native->napi == nullptr || native->env == nullptr) {
73         return IMAGE_RESULT_BAD_PARAMETER;
74     }
75     ImagePackerArgs args;
76     args.inEnv = native->env;
77     args.inNapi = native->napi;
78     args.inVal = source;
79     args.inOpts = opts;
80     args.inNum0 = fd;
81     return ImagePackerNativeCall(CTX_FUNC_IMAGEPACKER_PACKTOFILE, &args);
82 }
83 
84 MIDK_EXPORT
OH_ImagePacker_Release(ImagePacker_Native * native)85 int32_t OH_ImagePacker_Release(ImagePacker_Native* native)
86 {
87     if (native != nullptr) {
88         delete native;
89     }
90     return IMAGE_RESULT_SUCCESS;
91 }
92 
93 #ifdef __cplusplus
94 };
95 #endif
96