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 
GetTimestamp()107 int64_t ImageImpl::GetTimestamp()
108 {
109     if (native_ == nullptr) {
110         IMAGE_LOGE("Image buffer cannot be nullptr");
111         return 0;
112     }
113     int64_t timestamp = 0;
114     if (native_->GetTimestamp(timestamp) != SUCCESS) {
115         IMAGE_LOGE("Image native get timestamp failed");
116     }
117     return timestamp;
118 }
119 
GetComponent(int32_t componentType, CRetComponent *ret)120 uint32_t ImageImpl::GetComponent(int32_t componentType, CRetComponent *ret)
121 {
122     if (native_ == nullptr) {
123         IMAGE_LOGE("Image buffer cannot be nullptr");
124         return ERR_IMAGE_INIT_ABNORMAL;
125     }
126     auto nativePtr = native_->GetComponent(componentType);
127     if (nativePtr == nullptr) {
128         IMAGE_LOGE("Image component is nullptr");
129         return ERR_IMAGE_INIT_ABNORMAL;
130     }
131     ret->componentType = componentType;
132     ret->rowStride = nativePtr->rowStride;
133     ret->pixelStride = nativePtr->pixelStride;
134     int64_t len = static_cast<int64_t>(nativePtr->raw.size());
135     ret->byteBuffer = static_cast<uint8_t*>(malloc(len + 1));
136     if (ret->byteBuffer == nullptr) {
137         IMAGE_LOGE("[ImageImpl] GetComponent failed to malloc.");
138         return ERR_IMAGE_INIT_ABNORMAL;
139     }
140     for (int i = 0; i < len; i++) {
141         ret->byteBuffer[i] = nativePtr->raw[i];
142     }
143     ret->byteBuffer[len] = '\0';
144     ret->bufSize = len + 1;
145     return SUCCESS;
146 }
147 
Release()148 void ImageImpl::Release() {}
149 }  // namespace Media
150 }  // namespace OHOS