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