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 
16 #include <inttypes.h>
17 
18 #include "common_utils.h"
19 #include "image_log.h"
20 #include "image_native.h"
21 #include "image_kits.h"
22 #include "media_errors.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 MIDK_EXPORT
OH_ImageNative_GetImageSize(OH_ImageNative * image,Image_Size * size)29 Image_ErrorCode OH_ImageNative_GetImageSize(OH_ImageNative* image, Image_Size* size)
30 {
31     if (nullptr == image || nullptr == image->imgNative || nullptr == size) {
32         IMAGE_LOGE("OH_ImageNative_GetImageSize: Invalid parameter");
33         return IMAGE_BAD_PARAMETER;
34     }
35     int32_t width = 0;
36     int32_t height = 0;
37     Image_ErrorCode err = (Image_ErrorCode)image->imgNative->GetSize(width, height);
38     size->width = static_cast<uint32_t>(width);
39     size->height = static_cast<uint32_t>(height);
40     return err;
41 }
42 
43 MIDK_EXPORT
OH_ImageNative_GetComponentTypes(OH_ImageNative * image,uint32_t ** types,size_t * typeSize)44 Image_ErrorCode OH_ImageNative_GetComponentTypes(OH_ImageNative* image, uint32_t** types, size_t* typeSize)
45 {
46     if (nullptr == image || nullptr == image->imgNative || nullptr == typeSize) {
47         IMAGE_LOGE("OH_ImageNative_GetComponentTypes: Invalid parameter");
48         return IMAGE_BAD_PARAMETER;
49     }
50 
51     image->imgNative->GetComponent(int32_t(OHOS::Media::ComponentType::JPEG));
52 
53     auto& components = image->imgNative->GetComponents();
54     *typeSize = components.size();
55     if (nullptr == types) {
56         return IMAGE_SUCCESS;
57     }
58 
59     uint32_t* p = *types;
60     for (auto itor = components.begin(); itor != components.end(); ++itor) {
61         *p = itor->first;
62         p++;
63     }
64 
65     return IMAGE_SUCCESS;
66 }
67 
68 MIDK_EXPORT
OH_ImageNative_GetByteBuffer(OH_ImageNative * image,uint32_t componentType,OH_NativeBuffer ** nativeBuffer)69 Image_ErrorCode OH_ImageNative_GetByteBuffer(OH_ImageNative* image,
70                                              uint32_t componentType, OH_NativeBuffer** nativeBuffer)
71 {
72     if (nullptr == image || nullptr == image->imgNative || nullptr == nativeBuffer) {
73         IMAGE_LOGE("OH_ImageNative_GetByteBuffer: Invalid parameter");
74         return IMAGE_BAD_PARAMETER;
75     }
76 
77     auto component = image->imgNative->GetComponent(componentType);
78     if (nullptr == component) {
79         return IMAGE_BAD_PARAMETER;
80     }
81 
82     auto buffer = image->imgNative->GetBuffer();
83     if (buffer != nullptr) {
84         *nativeBuffer = buffer->SurfaceBufferToNativeBuffer();
85     }
86     return IMAGE_SUCCESS;
87 }
88 
89 MIDK_EXPORT
OH_ImageNative_GetBufferSize(OH_ImageNative * image,uint32_t componentType,size_t * size)90 Image_ErrorCode OH_ImageNative_GetBufferSize(OH_ImageNative* image, uint32_t componentType, size_t* size)
91 {
92     if (nullptr == image || nullptr == image->imgNative || nullptr == size) {
93         IMAGE_LOGE("OH_ImageNative_GetBufferSize: Invalid parameter");
94         return IMAGE_BAD_PARAMETER;
95     }
96 
97     auto component = image->imgNative->GetComponent(componentType);
98     if (nullptr == component) {
99         return IMAGE_BAD_PARAMETER;
100     }
101 
102     *size = component->size;
103     return IMAGE_SUCCESS;
104 }
105 
106 MIDK_EXPORT
OH_ImageNative_GetRowStride(OH_ImageNative * image,uint32_t componentType,int32_t * rowStride)107 Image_ErrorCode OH_ImageNative_GetRowStride(OH_ImageNative* image, uint32_t componentType, int32_t* rowStride)
108 {
109     if (nullptr == image || nullptr == image->imgNative || nullptr == rowStride) {
110         IMAGE_LOGE("OH_ImageNative_GetRowStride: Invalid parameter");
111         return IMAGE_BAD_PARAMETER;
112     }
113 
114     auto component = image->imgNative->GetComponent(componentType);
115     if (nullptr == component) {
116         return IMAGE_BAD_PARAMETER;
117     }
118 
119     *rowStride = component->rowStride;
120     return IMAGE_SUCCESS;
121 }
122 
123 MIDK_EXPORT
OH_ImageNative_GetPixelStride(OH_ImageNative * image,uint32_t componentType,int32_t * pixelStride)124 Image_ErrorCode OH_ImageNative_GetPixelStride(OH_ImageNative* image, uint32_t componentType, int32_t* pixelStride)
125 {
126     if (nullptr == image || nullptr == image->imgNative || nullptr == pixelStride) {
127         IMAGE_LOGE("OH_ImageNative_GetPixelStride: Invalid parameter");
128         return IMAGE_BAD_PARAMETER;
129     }
130 
131     auto component = image->imgNative->GetComponent(componentType);
132     if (nullptr == component) {
133         return IMAGE_BAD_PARAMETER;
134     }
135 
136     *pixelStride = component->pixelStride;
137     return IMAGE_SUCCESS;
138 }
139 
140 MIDK_EXPORT
OH_ImageNative_GetTimestamp(OH_ImageNative * image,int64_t * timestamp)141 Image_ErrorCode OH_ImageNative_GetTimestamp(OH_ImageNative *image, int64_t *timestamp)
142 {
143     if (nullptr == image || nullptr == image->imgNative || nullptr == timestamp) {
144         IMAGE_LOGE("%{public}s Invalid parameter: Null Pointer Error", __func__);
145         return IMAGE_BAD_PARAMETER;
146     }
147     if (OHOS::Media::SUCCESS == image->imgNative->GetTimestamp(*timestamp)) {
148         return IMAGE_SUCCESS;
149     } else {
150         IMAGE_LOGE("image buffer is unusable");
151         return IMAGE_BAD_PARAMETER;
152     }
153 }
154 
155 MIDK_EXPORT
OH_ImageNative_Release(OH_ImageNative * image)156 Image_ErrorCode OH_ImageNative_Release(OH_ImageNative* image)
157 {
158     if (nullptr == image) {
159         IMAGE_LOGE("OH_ImageNative_Release: Invalid parameter");
160         return IMAGE_BAD_PARAMETER;
161     }
162     if (nullptr != image->imgNative) {
163         image->imgNative->release();
164         delete image->imgNative;
165     }
166     IMAGE_LOGI("OH_ImageNative Release");
167     delete image;
168     return IMAGE_SUCCESS;
169 }
170 
171 #ifdef __cplusplus
172 };
173 #endif
174