1 /*
2  * Copyright (c) 2024 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 #include "image_impl.h"
16 #include "image_log.h"
17 #include "media_errors.h"
18 
19 namespace OHOS {
20 namespace Media {
21 
22 const int32_t DEFAULT_FORMAT = 12;
23 const int32_t DEFAULT_HEIGHT = 8;
24 const int32_t DEFAULT_WIDTH = 8192;
25 
26 ImageHolderManager<NativeImage> ImageImpl::sNativeImageHolder_;
27 
ImageImpl(std::shared_ptr<NativeImage> nativeImage)28 ImageImpl::ImageImpl(std::shared_ptr<NativeImage> nativeImage)
29 {
30     ImageImpl::Create(this, nativeImage);
31 }
32 
GetNativeImage()33 std::shared_ptr<NativeImage> ImageImpl::GetNativeImage()
34 {
35     return native_;
36 }
37 
Create(ImageImpl * image,std::shared_ptr<NativeImage> nativeImage)38 int64_t ImageImpl::Create(ImageImpl *image, std::shared_ptr<NativeImage> nativeImage)
39 {
40     auto id = sNativeImageHolder_.save(nativeImage);
41     image->native_ = sNativeImageHolder_.get(id);
42     image->isTestImage_ = false;
43     if (image->native_ == nullptr) {
44         IMAGE_LOGE("[ImageImpl] Create : Failed to get native image");
45         return INIT_FAILED;
46     }
47     return SUCCESS;
48 }
49 
GetClipRect(CRegion * ret)50 uint32_t ImageImpl::GetClipRect(CRegion *ret)
51 {
52     if (isTestImage_ == true) {
53         ret->size.width = DEFAULT_WIDTH;
54         ret->size.height = DEFAULT_HEIGHT;
55         ret->x = 0;
56         ret->y = 0;
57         return SUCCESS;
58     }
59     if (native_ == nullptr) {
60         IMAGE_LOGE("Image buffer cannot be nullptr");
61         return ERR_IMAGE_INIT_ABNORMAL;
62     }
63     uint32_t retCode = native_->GetSize(ret->size.width, ret->size.height);
64     ret->x = 0;
65     ret->y = 0;
66     if (retCode != SUCCESS) {
67         IMAGE_LOGE("[ImageImpl] GetSize : Image native get size failed.");
68     }
69     return retCode;
70 }
71 
GetSize(CSize * ret)72 uint32_t ImageImpl::GetSize(CSize *ret)
73 {
74     if (isTestImage_ == true) {
75         ret->width = DEFAULT_WIDTH;
76         ret->height = DEFAULT_HEIGHT;
77         return SUCCESS;
78     }
79     if (native_ == nullptr) {
80         IMAGE_LOGE("Image buffer cannot be nullptr");
81         return ERR_IMAGE_INIT_ABNORMAL;
82     }
83     uint32_t retCode = native_->GetSize(ret->width, ret->height);
84     if (retCode != SUCCESS) {
85         IMAGE_LOGE("[ImageImpl] GetSize : Image native get size failed.");
86     }
87     return retCode;
88 }
89 
GetFormat(int32_t * ret)90 uint32_t ImageImpl::GetFormat(int32_t *ret)
91 {
92     if (isTestImage_ == true) {
93         *ret = DEFAULT_FORMAT;
94         return SUCCESS;
95     }
96     if (native_ == nullptr) {
97         IMAGE_LOGE("Image buffer cannot be nullptr");
98         return ERR_IMAGE_INIT_ABNORMAL;
99     }
100     uint32_t retCode = native_->GetFormat(*ret);
101     if (retCode != SUCCESS) {
102         IMAGE_LOGE("[ImageImpl] GetFormat : Image native get format failed.");
103     }
104     return retCode;
105 }
106 
GetComponent(int32_t componentType,CRetComponent * ret)107 uint32_t ImageImpl::GetComponent(int32_t componentType, CRetComponent *ret)
108 {
109     if (native_ == nullptr) {
110         IMAGE_LOGE("Image buffer cannot be nullptr");
111         return ERR_IMAGE_INIT_ABNORMAL;
112     }
113     auto nativePtr = native_->GetComponent(componentType);
114     if (nativePtr == nullptr) {
115         IMAGE_LOGE("Image component is nullptr");
116         return ERR_IMAGE_INIT_ABNORMAL;
117     }
118     ret->componentType = componentType;
119     ret->rowStride = nativePtr->rowStride;
120     ret->pixelStride = nativePtr->pixelStride;
121     int64_t len = static_cast<int64_t>(nativePtr->raw.size());
122     ret->byteBuffer = static_cast<uint8_t*>(malloc(len + 1));
123     if (ret->byteBuffer == nullptr) {
124         IMAGE_LOGE("[ImageImpl] GetComponent failed to malloc.");
125         return ERR_IMAGE_INIT_ABNORMAL;
126     }
127     for (int i = 0; i < len; i++) {
128         ret->byteBuffer[i] = nativePtr->raw[i];
129     }
130     ret->byteBuffer[len] = '\0';
131     ret->bufSize = len + 1;
132     return SUCCESS;
133 }
134 
Release()135 void ImageImpl::Release() {}
136 }  // namespace Media
137 }  // namespace OHOS