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