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_packer_native.h>
21 #include <multimedia/image_framework/image/pixelmap_native.h>
22 #include <multimedia/image_framework/image/image_source_native.h>
23 #undef LOG_DOMAIN
24 #undef LOG_TAG
25 #define LOG_DOMAIN 0x3200
26 #define LOG_TAG "MY_TAG"
27 #include <cstdlib>
28 #include <fcntl.h>
29
30 const uint8_t OPTIONS_WIDTH = 6;
31 const uint8_t OPTIONS_HEIGHT = 4;
32 const uint8_t OPTIONS_PIXELFORMAT = 3;
33 const uint8_t OPTIONS_ALPHATYPE = 0;
34
packToFileFromImageSourceTest(int fd)35 static Image_ErrorCode packToFileFromImageSourceTest(int fd)
36 {
37 // 创建ImagePacker实例
38 OH_ImagePackerNative *testPacker = nullptr;
39 Image_ErrorCode errCode = OH_ImagePackerNative_Create(&testPacker);
40 if (errCode != IMAGE_SUCCESS) {
41 OH_LOG_ERROR(LOG_APP,
42 "ImagePackerNativeCTest CreatePacker OH_ImagePackerNative_Create failed, errCode: %{public}d.",
43 errCode);
44 return errCode;
45 }
46
47 // 创建ImageSource实例
48 OH_ImageSourceNative *imageSource = nullptr;
49 errCode = OH_ImageSourceNative_CreateFromFd(fd, &imageSource);
50 if (errCode != IMAGE_SUCCESS) {
51 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImageSourceNative_CreateFromFd failed, errCode: %{public}d.",
52 errCode);
53 return errCode;
54 }
55
56 // 指定打包参数,将ImageSource图片源编码后直接打包进文件
57 OH_PackingOptions *option = nullptr;
58 OH_PackingOptions_Create(&option);
59 char type[] = "image/jpeg";
60 Image_MimeType image_MimeType = {type, strlen(type)};
61 OH_PackingOptions_SetMimeType(option, &image_MimeType);
62 // 编码为hdr内容(需要资源本身为hdr,支持jpeg格式)
63 OH_PackingOptions_SetDesiredDynamicRange(option, IMAGE_PACKER_DYNAMIC_RANGE_AUTO);
64 OH_PackingOptions_SetNeedsPackProperties(option, true);
65 errCode = OH_ImagePackerNative_PackToFileFromImageSource(testPacker, option, imageSource, fd);
66 if (errCode != IMAGE_SUCCESS) {
67 OH_LOG_ERROR(
68 LOG_APP,
69 "ImagePackerNativeCTest OH_ImagePackerNative_PackToFileFromImageSource failed, errCode: %{public}d.",
70 errCode);
71 return errCode;
72 }
73
74 // 释放ImagePacker实例
75 errCode = OH_ImagePackerNative_Release(testPacker);
76 if (errCode != IMAGE_SUCCESS) {
77 OH_LOG_ERROR(LOG_APP,
78 "ImagePackerNativeCTest ReleasePacker OH_ImagePackerNative_Release failed, errCode: %{public}d.",
79 errCode);
80 return errCode;
81 }
82 // 释放ImageSource实例
83 errCode = OH_ImageSourceNative_Release(imageSource);
84 if (errCode != IMAGE_SUCCESS) {
85 OH_LOG_ERROR(LOG_APP,
86 "ImagePackerNativeCTest ReleasePacker OH_ImageSourceNative_Release failed, errCode: %{public}d.",
87 errCode);
88 return errCode;
89 }
90
91 return IMAGE_SUCCESS;
92 }
93
packToFileFromPixelmapTest(uint8_t * buffer,size_t buffSize,int fd)94 static Image_ErrorCode packToFileFromPixelmapTest(uint8_t *buffer, size_t buffSize, int fd)
95 {
96 // 创建ImagePacker实例
97 OH_ImagePackerNative *testPacker = nullptr;
98 Image_ErrorCode errCode = OH_ImagePackerNative_Create(&testPacker);
99 if (errCode != IMAGE_SUCCESS) {
100 OH_LOG_ERROR(LOG_APP, "CreatePacker OH_ImagePackerNative_Create failed, errCode: %{public}d.", errCode);
101 return errCode;
102 }
103
104 // 创建Pixelmap实例
105 OH_Pixelmap_InitializationOptions *createOpts;
106 OH_PixelmapInitializationOptions_Create(&createOpts);
107 OH_PixelmapInitializationOptions_SetWidth(createOpts, OPTIONS_WIDTH);
108 OH_PixelmapInitializationOptions_SetHeight(createOpts, OPTIONS_HEIGHT);
109 OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, OPTIONS_PIXELFORMAT);
110 OH_PixelmapInitializationOptions_SetAlphaType(createOpts, OPTIONS_ALPHATYPE);
111 OH_PixelmapNative *pixelmap = nullptr;
112 errCode = OH_PixelmapNative_CreatePixelmap(buffer, buffSize, createOpts, &pixelmap);
113 if (errCode != IMAGE_SUCCESS) {
114 OH_LOG_ERROR(LOG_APP, "OH_PixelmapNative_CreatePixelmap failed, errCode: %{public}d.", errCode);
115 return errCode;
116 }
117
118 // 指定打包参数,将PixelMap图片源编码后直接打包进文件
119 OH_PackingOptions *option = nullptr;
120 OH_PackingOptions_Create(&option);
121 char type[] = "image/jpeg";
122 Image_MimeType image_MimeType = {type, strlen(type)};
123 OH_PackingOptions_SetMimeType(option, &image_MimeType);
124 OH_PackingOptions_SetNeedsPackProperties(option, true);
125 errCode = OH_ImagePackerNative_PackToFileFromPixelmap(testPacker, option, pixelmap, fd);
126 if (errCode != IMAGE_SUCCESS) {
127 OH_LOG_ERROR(LOG_APP, "OH_ImagePackerNative_PackToFileFromPixelmap failed, errCode: %{public}d.", errCode);
128 return errCode;
129 }
130
131 // 释放ImagePacker实例
132 errCode = OH_ImagePackerNative_Release(testPacker);
133 if (errCode != IMAGE_SUCCESS) {
134 OH_LOG_ERROR(LOG_APP, "ReleasePacker OH_ImagePackerNative_Release failed, errCode: %{public}d.", errCode);
135 return errCode;
136 }
137
138 // 释放Pixelmap实例
139 errCode = OH_PixelmapNative_Release(pixelmap);
140 if (errCode != IMAGE_SUCCESS) {
141 OH_LOG_ERROR(LOG_APP, "ReleasePacker OH_PixelmapNative_Release failed, errCode: %{public}d.", errCode);
142 return errCode;
143 }
144
145 return IMAGE_SUCCESS;
146 }