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