• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "image_common.h"
17 #include "image_log.h"
18 #include "image_taihe_utils.h"
19 #include "picture_taihe.h"
20 #include "pixel_map_taihe.h"
21 #include "media_errors.h"
22 #include "message_parcel.h"
23 
24 using namespace ANI::Image;
25 
26 namespace ANI::Image {
27 
PictureImpl()28 PictureImpl::PictureImpl() : nativePicture_(nullptr), isRelease(false) {}
29 
PictureImpl(std::shared_ptr<OHOS::Media::Picture> picture)30 PictureImpl::PictureImpl(std::shared_ptr<OHOS::Media::Picture> picture)
31 {
32     nativePicture_ = picture;
33 }
34 
~PictureImpl()35 PictureImpl::~PictureImpl()
36 {
37     Release();
38 }
39 
GetImplPtr()40 int64_t PictureImpl::GetImplPtr()
41 {
42     return reinterpret_cast<uintptr_t>(this);
43 }
44 
GetNativePtr()45 std::shared_ptr<OHOS::Media::Picture> PictureImpl::GetNativePtr()
46 {
47     return nativePicture_;
48 }
49 
GetMainPixelmap()50 PixelMap PictureImpl::GetMainPixelmap()
51 {
52     if (nativePicture_ == nullptr) {
53         ImageTaiheUtils::ThrowExceptionError("Native picture is nullptr!");
54         return make_holder<PixelMapImpl, PixelMap>();
55     }
56     auto pixelmap = nativePicture_->GetMainPixel();
57     if (pixelmap == nullptr) {
58         ImageTaiheUtils::ThrowExceptionError("Get main pixelmap failed, pixelmap is nullptr!");
59         return make_holder<PixelMapImpl, PixelMap>();
60     }
61     return PixelMapImpl::CreatePixelMap(pixelmap);
62 }
63 
Marshalling(uintptr_t sequence)64 void PictureImpl::Marshalling(uintptr_t sequence)
65 {
66 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
67     ani_env *env = ::taihe::get_env();
68     if (env == nullptr) {
69         ImageTaiheUtils::ThrowExceptionError(IMAGE_BAD_PARAMETER, "Ani unwarp messageParcel failed.");
70         return;
71     }
72     ani_object sequenceObj = reinterpret_cast<ani_object>(sequence);
73     ani_long nativePtr;
74     if (ANI_OK != env->Object_GetFieldByName_Long(sequenceObj, "nativePtr", &nativePtr)) {
75         ImageTaiheUtils::ThrowExceptionError(IMAGE_BAD_PARAMETER, "Marshalling picture unwrap failed.");
76         return;
77     }
78     OHOS::MessageParcel* messageParcel = reinterpret_cast<OHOS::MessageParcel*>(nativePtr);
79     if (messageParcel == nullptr) {
80         ImageTaiheUtils::ThrowExceptionError(OHOS::Media::ERR_IPC, "Marshalling picture to parcel failed.");
81         return;
82     }
83     bool st = nativePicture_->Marshalling(*messageParcel);
84     if (!st) {
85         ImageTaiheUtils::ThrowExceptionError(OHOS::Media::ERR_IPC, "Marshalling picture to parcel failed.");
86     }
87 #endif
88 }
89 
Release()90 void PictureImpl::Release()
91 {
92     if (!isRelease) {
93         if (nativePicture_ != nullptr) {
94             nativePicture_ = nullptr;
95         }
96         isRelease = true;
97     }
98 }
99 
CreatePictureByPixelMap(weak::PixelMap mainPixelmap)100 Picture CreatePictureByPixelMap(weak::PixelMap mainPixelmap)
101 {
102     IMAGE_LOGI("CreatePicture IN");
103     PixelMapImpl* pixelMapImpl = reinterpret_cast<PixelMapImpl*>(mainPixelmap->GetImplPtr());
104     if (pixelMapImpl == nullptr) {
105         ImageTaiheUtils::ThrowExceptionError(IMAGE_BAD_PARAMETER, "Pixelmap instance is nullptr!");
106         return make_holder<PictureImpl, Picture>();
107     }
108     auto nativePixelMap = pixelMapImpl->GetNativePtr();
109     if (nativePixelMap == nullptr) {
110         ImageTaiheUtils::ThrowExceptionError(IMAGE_BAD_PARAMETER, "Get native pixelmap failed!");
111         return make_holder<PictureImpl, Picture>();
112     }
113     auto picture = OHOS::Media::Picture::Create(nativePixelMap);
114     if (picture == nullptr) {
115         ImageTaiheUtils::ThrowExceptionError(OHOS::Media::ERROR, "Create picture failed!");
116         return make_holder<PictureImpl, Picture>();
117     }
118     IMAGE_LOGI("CreatePicture OUT");
119     return make_holder<PictureImpl, Picture>(std::move(picture));
120 }
121 } // namespace ANI::Image
122 
123 TH_EXPORT_CPP_API_CreatePictureByPixelMap(CreatePictureByPixelMap);