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 <linux/kd.h>
17 #include <string>
18
19 #include <hilog/log.h>
20 #include <multimedia/image_framework/image/image_source_native.h>
21 #undef LOG_DOMAIN
22 #undef LOG_TAG
23 #define LOG_DOMAIN 0x3200
24 #define LOG_TAG "MY_TAG"
25
26 #define NUM_0 0
27 #define NUM_1 1
28
29 const uint32_t MAX_FRAME_CNT = 1024;
30 const uint32_t NAME_SIZE = 1024;
31
getJsResult(napi_env env,Image_ErrorCode errCode)32 static napi_value getJsResult(napi_env env, Image_ErrorCode errCode)
33 {
34 napi_value result;
35 napi_create_int32(env, errCode, &result);
36 return result;
37 }
38
CreatePixelMap(OH_ImageSourceNative * source,OH_PixelmapNative * resPixMap)39 static Image_ErrorCode CreatePixelMap(OH_ImageSourceNative *source, OH_PixelmapNative *resPixMap)
40 {
41 // 创建定义图片信息的结构体对象,并获取图片信息
42 OH_ImageSource_Info *imageInfo;
43 OH_ImageSourceInfo_Create(&imageInfo);
44 Image_ErrorCode errCode = OH_ImageSourceNative_GetImageInfo(source, 0, imageInfo);
45 if (errCode != IMAGE_SUCCESS) {
46 OH_LOG_ERROR(
47 LOG_APP,
48 "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageInfo failed, errCode:%{public}d.",
49 errCode);
50 return errCode;
51 }
52
53 // 获取指定属性键的值
54 uint32_t width, height;
55 OH_ImageSourceInfo_GetWidth(imageInfo, &width);
56 OH_ImageSourceInfo_GetHeight(imageInfo, &height);
57 OH_ImageSourceInfo_Release(imageInfo);
58 OH_LOG_INFO(LOG_APP,
59 "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageInfo success, width: %{public}d, "
60 "height: %{public}d.",
61 width, height);
62
63 // 通过图片解码参数创建PixelMap对象
64 OH_DecodingOptions *ops = nullptr;
65 OH_DecodingOptions_Create(&ops);
66 // 设置为AUTO会根据图片资源格式解码,如果图片资源为HDR资源则会解码为HDR的pixelmap。
67 OH_DecodingOptions_SetDesiredDynamicRange(ops, IMAGE_DYNAMIC_RANGE_AUTO);
68
69 // ops参数支持传入nullptr, 当不需要设置解码参数时,不用创建
70 errCode = OH_ImageSourceNative_CreatePixelmap(source, ops, &resPixMap);
71 OH_DecodingOptions_Release(ops);
72 if (errCode != IMAGE_SUCCESS) {
73 OH_LOG_ERROR(
74 LOG_APP,
75 "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreatePixelmap failed, errCode: %{public}d.",
76 errCode);
77 return errCode;
78 }
79 return IMAGE_SUCCESS;
80 }
81
GetPixelMapInfo(OH_ImageSourceNative * source,OH_PixelmapNative * resPixMap)82 static Image_ErrorCode GetPixelMapInfo(OH_ImageSourceNative *source, OH_PixelmapNative *resPixMap)
83 {
84 // 判断pixelmap是否为hdr内容
85 OH_Pixelmap_ImageInfo *pixelmapImageInfo = nullptr;
86 OH_PixelmapImageInfo_Create(&pixelmapImageInfo);
87 OH_PixelmapNative_GetImageInfo(resPixMap, pixelmapImageInfo);
88 bool pixelmapIsHdr;
89 OH_PixelmapImageInfo_GetDynamicRange(pixelmapImageInfo, &pixelmapIsHdr);
90 OH_PixelmapImageInfo_Release(pixelmapImageInfo);
91
92 // 获取图像帧数
93 uint32_t frameCnt = 0;
94 Image_ErrorCode errCode = OH_ImageSourceNative_GetFrameCount(source, &frameCnt);
95 if (errCode != IMAGE_SUCCESS) {
96 OH_LOG_ERROR(
97 LOG_APP,
98 "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetFrameCount failed, errCode: %{public}d.",
99 errCode);
100 return errCode;
101 }
102
103 // 通过图片解码参数创建Pixelmap列表
104 OH_DecodingOptions *opts = nullptr;
105 OH_DecodingOptions_Create(&opts);
106 OH_PixelmapNative **resVecPixMap = nullptr;
107 if (frameCnt > 0 && frameCnt <= MAX_FRAME_CNT) {
108 resVecPixMap = new OH_PixelmapNative *[frameCnt];
109 }
110 size_t outSize = frameCnt;
111 errCode = OH_ImageSourceNative_CreatePixelmapList(source, opts, resVecPixMap, outSize);
112 OH_DecodingOptions_Release(opts);
113 delete[] resVecPixMap;
114 if (errCode != IMAGE_SUCCESS) {
115 OH_LOG_ERROR(
116 LOG_APP,
117 "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreatePixelmapList failed, errCode: %{public}d.",
118 errCode);
119 return errCode;
120 }
121
122 int32_t *delayTimeList = nullptr;
123 if (frameCnt > 0 && frameCnt <= MAX_FRAME_CNT) {
124 // 获取图像延迟时间列表
125 delayTimeList = new int32_t[frameCnt];
126 }
127
128 size_t size = frameCnt;
129 errCode = OH_ImageSourceNative_GetDelayTimeList(source, delayTimeList, size);
130 delete[] delayTimeList;
131 if (errCode != IMAGE_SUCCESS) {
132 OH_LOG_ERROR(
133 LOG_APP,
134 "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetDelayTimeList failed, errCode: %{public}d.",
135 errCode);
136 return errCode;
137 }
138 return IMAGE_SUCCESS;
139 }
140
sourceTest(napi_env env,napi_callback_info info)141 static napi_value sourceTest(napi_env env, napi_callback_info info)
142 {
143 napi_value argValue[NUM_1] = {0};
144 size_t argCount = NUM_1;
145 if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1 ||
146 argValue[NUM_0] == nullptr) {
147 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest napi_get_cb_info failed, argCount: %{public}d.",
148 argCount);
149 return getJsResult(env, IMAGE_BAD_PARAMETER);
150 }
151 char name[NAME_SIZE];
152 size_t nameSize = NAME_SIZE;
153 napi_get_value_string_utf8(env, argValue[NUM_0], name, NAME_SIZE, &nameSize);
154
155 // 创建ImageSource实例
156 OH_ImageSourceNative *source = nullptr;
157 Image_ErrorCode errCode = OH_ImageSourceNative_CreateFromUri(name, nameSize, &source);
158 if (errCode != IMAGE_SUCCESS) {
159 OH_LOG_ERROR(
160 LOG_APP,
161 "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreateFromUri failed, errCode: %{public}d.",
162 errCode);
163 return getJsResult(env, errCode);
164 }
165
166 OH_PixelmapNative *resPixMap = nullptr;
167 errCode = CreatePixelMap(source, resPixMap);
168 if (errCode != IMAGE_SUCCESS) {
169 return getJsResult(env, errCode);
170 }
171
172 errCode = GetPixelMapInfo(source, resPixMap);
173 if (errCode != IMAGE_SUCCESS) {
174 return getJsResult(env, errCode);
175 }
176
177 // 释放ImageSource实例
178 OH_ImageSourceNative_Release(source);
179 OH_LOG_INFO(LOG_APP, "ImageSourceNativeCTest sourceTest success.");
180 return getJsResult(env, IMAGE_SUCCESS);
181 }