1 /*
2 * Copyright (C) 2025 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 <ani.h>
17 #include <iostream>
18
19 #include "image_ani_utils.h"
20 #include "image_log.h"
21 #include "log_tags.h"
22 #include "media_errors.h"
23 #include "picture_ani.h"
24 #include "pixel_map_ani.h"
25
26 #undef LOG_DOMAIN
27 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
28
29 #undef LOG_TAG
30 #define LOG_TAG "PictureAni"
31
32 namespace OHOS {
33 namespace Media {
34 using namespace std;
35
CreatePictureAni(ani_env * env,ani_object obj)36 ani_object PictureAni::CreatePictureAni([[maybe_unused]] ani_env* env, ani_object obj)
37 {
38 unique_ptr<PictureAni> pPictureAni = std::make_unique<PictureAni>();
39 auto pixelMap = ImageAniUtils::GetPixelMapFromEnvSp(env, obj);
40 if (pixelMap == nullptr) {
41 IMAGE_LOGE("[GetPixelMapFromEnvSp] pixelMap nullptr");
42 return nullptr;
43 }
44
45 pPictureAni->nativePicture_ = Picture::Create(pixelMap);
46 return ImageAniUtils::CreateAniPicture(env, pPictureAni);
47 }
48
49
GetMainPixelmap(ani_env * env,ani_object obj)50 static ani_object GetMainPixelmap([[maybe_unused]] ani_env* env, [[maybe_unused]] ani_object obj)
51 {
52 auto picture = ImageAniUtils::GetPictureFromEnv(env, obj);
53 if (picture == nullptr) {
54 IMAGE_LOGE("[GetPictureFromEnv] picture nullptr");
55 return nullptr;
56 }
57
58 return PixelMapAni::CreatePixelMap(env, picture->GetMainPixel());
59 }
60
Init(ani_env * env)61 ani_status PictureAni::Init(ani_env* env)
62 {
63 static const char* className = "L@ohos/multimedia/image/image/PictureInner;";
64 ani_class cls;
65 if (ANI_OK != env->FindClass(className, &cls)) {
66 IMAGE_LOGE("Not found L@ohos/multimedia/image/image/PictureInner;");
67 return ANI_ERROR;
68 }
69 std::array methods = {
70 ani_native_function {"getMainPixelmap", ":L@ohos/multimedia/image/image/PixelMap;",
71 reinterpret_cast<void*>(OHOS::Media::GetMainPixelmap)},
72 };
73 ani_status ret = env->Class_BindNativeMethods(cls, methods.data(), methods.size());
74 if (ANI_OK != ret) {
75 IMAGE_LOGE("[Init] Class_BindNativeMethods failed: %{public}d", ret);
76 return ANI_ERROR;
77 };
78 return ANI_OK;
79 }
80 } // Media
81 } // OHOS