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_bitmap_operations]
17 #include <linux/kd.h>
18 #include <string>
19
20 #include <hilog/log.h>
21 #include <multimedia/image_framework/image/pixelmap_native.h>
22
23 #undef LOG_DOMAIN
24 #undef LOG_TAG
25 #define LOG_DOMAIN 0x3200
26 #define LOG_TAG "MY_TAG"
27
28 const uint8_t DATA_SIZE = 96;
29 const uint8_t OPTS_WIDTH = 6;
30 const uint8_t OPTS_HEIGHT = 4;
31 const float OPACITY_VALUE = 0.5;
32 const float SCALE_X = 2.0;
33 const float SCALE_Y = 1.0;
34 const float TRANSLATE_X = 50.0;
35 const float TRANSLATE_Y = 10.0;
36 const float ROTATE_ANGLE = 90.0;
37 const uint8_t REGION_X = 100;
38 const uint8_t REGION_Y = 100;
39 const uint8_t REGION_WIDTH = 6;
40 const uint8_t REGION_HEIGHT = 4;
41
CreatePixelMap(OH_PixelmapNative ** pixelmap)42 static Image_ErrorCode CreatePixelMap(OH_PixelmapNative **pixelmap)
43 {
44 uint8_t data[DATA_SIZE];
45 for (int i = 0; i < DATA_SIZE; i++) {
46 data[i] = i + 1;
47 }
48
49 // 创建参数结构体实例,并设置参数
50 OH_Pixelmap_InitializationOptions *createOpts;
51 OH_PixelmapInitializationOptions_Create(&createOpts);
52 OH_PixelmapInitializationOptions_SetWidth(createOpts, OPTS_WIDTH);
53 OH_PixelmapInitializationOptions_SetHeight(createOpts, OPTS_HEIGHT);
54 OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, PIXEL_FORMAT_RGBA_8888);
55 OH_PixelmapInitializationOptions_SetAlphaType(createOpts, PIXELMAP_ALPHA_TYPE_UNKNOWN);
56
57 Image_ErrorCode errCode = OH_PixelmapNative_CreatePixelmap(data, DATA_SIZE, createOpts, pixelmap);
58
59 // 读取图像像素数据,结果写入数组里
60 uint8_t destination[DATA_SIZE];
61 size_t destinationSize = DATA_SIZE;
62 errCode = OH_PixelmapNative_ReadPixels(*pixelmap, destination, &destinationSize);
63 if (errCode != IMAGE_SUCCESS) {
64 OH_LOG_ERROR(LOG_APP, "pixelmapTest OH_PixelmapNative_ReadPixels failed, errCode: %{public}d.", errCode);
65 return errCode;
66 }
67
68 // 读取缓冲区中的图片数据,结果写入Pixelmap中
69 uint8_t source[DATA_SIZE];
70 size_t sourceSize = DATA_SIZE;
71 for (int i = 0; i < sourceSize; i++) {
72 source[i] = i + 1;
73 }
74 errCode = OH_PixelmapNative_WritePixels(*pixelmap, source, sourceSize);
75 if (errCode != IMAGE_SUCCESS) {
76 OH_LOG_ERROR(LOG_APP, "pixelmapTest OH_PixelmapNative_WritePixels failed, errCode:%{public}d.", errCode);
77 return errCode;
78 }
79
80 // 创建图片信息实例,并获取图像像素信息
81 OH_Pixelmap_ImageInfo *imageInfo;
82 OH_PixelmapImageInfo_Create(&imageInfo);
83 errCode = OH_PixelmapNative_GetImageInfo(*pixelmap, imageInfo);
84 if (errCode != IMAGE_SUCCESS) {
85 OH_LOG_ERROR(LOG_APP, "pixelmapTest OH_PixelmapNative_GetImageInfo failed, errCode: %{public}d.", errCode);
86 return errCode;
87 }
88
89 // 获取图片的宽,高,pixel格式,透明度等信息
90 uint32_t width, height, rowStride;
91 int32_t pixelFormat, alphaType;
92 OH_PixelmapImageInfo_GetWidth(imageInfo, &width);
93 OH_PixelmapImageInfo_GetHeight(imageInfo, &height);
94 OH_PixelmapImageInfo_GetRowStride(imageInfo, &rowStride);
95 OH_PixelmapImageInfo_GetPixelFormat(imageInfo, &pixelFormat);
96 OH_PixelmapImageInfo_GetAlphaType(imageInfo, &alphaType);
97 OH_PixelmapImageInfo_Release(imageInfo);
98 OH_LOG_INFO(LOG_APP, "pixelmapTest GetImageInfo success, width:%{public}d, height:%{public}d, rowStride:"
99 "%{public}d, pixelFormat:%{public}d, alphaType:%{public}d.", width, height, rowStride, pixelFormat, alphaType);
100
101 OH_PixelmapInitializationOptions_Release(createOpts);
102 return IMAGE_SUCCESS;
103 }
104
PixelmapTest()105 static Image_ErrorCode PixelmapTest()
106 {
107 // 创建Pixelmap实例
108 OH_PixelmapNative *pixelmap = nullptr;
109
110 Image_ErrorCode errCode = CreatePixelMap(&pixelmap);
111 if (errCode != IMAGE_SUCCESS) {
112 return errCode;
113 }
114
115 // 设置透明比率来让Pixelap达到对应的透明效果
116 errCode = OH_PixelmapNative_Opacity(pixelmap, OPACITY_VALUE);
117 if (errCode != IMAGE_SUCCESS) {
118 OH_LOG_ERROR(LOG_APP, "pixelmapTest OH_PixelmapNative_Opacity failed, errCode: %{public}d.", errCode);
119 return errCode;
120 }
121
122 // 对图片进行缩放
123 errCode = OH_PixelmapNative_Scale(pixelmap, SCALE_X, SCALE_Y);
124 if (errCode != IMAGE_SUCCESS) {
125 OH_LOG_ERROR(LOG_APP, "pixelmapTest OH_PixelmapNative_Scale failed, errCode: %{public}d.", errCode);
126 return errCode;
127 }
128
129 // 对图片进行位置变换
130 errCode = OH_PixelmapNative_Translate(pixelmap, TRANSLATE_X, TRANSLATE_Y);
131 if (errCode != IMAGE_SUCCESS) {
132 OH_LOG_ERROR(LOG_APP, "pixelmapTest OH_PixelmapNative_Translate failed, errCode: %{public}d.", errCode);
133 return errCode;
134 }
135
136 // 对图片进行旋转
137 errCode = OH_PixelmapNative_Rotate(pixelmap, ROTATE_ANGLE);
138 if (errCode != IMAGE_SUCCESS) {
139 OH_LOG_ERROR(LOG_APP, "pixelmapTest OH_PixelmapNative_Rotate failed, errCode: %{public}d.", errCode);
140 return errCode;
141 }
142
143 // 对图片进行翻转
144 errCode = OH_PixelmapNative_Flip(pixelmap, true, true);
145 if (errCode != IMAGE_SUCCESS) {
146 OH_LOG_ERROR(LOG_APP, "pixelmapTest OH_PixelmapNative_Flip failed, errCode: %{public}d.", errCode);
147 return errCode;
148 }
149
150 // 对图片进行裁剪
151 Image_Region region;
152 region.x = REGION_X;
153 region.y = REGION_Y;
154 region.width = REGION_WIDTH;
155 region.height = REGION_HEIGHT;
156 errCode = OH_PixelmapNative_Crop(pixelmap, ®ion);
157 if (errCode != IMAGE_SUCCESS) {
158 OH_LOG_ERROR(LOG_APP, "pixelmapTest OH_PixelmapNative_Crop failed, errCode: %{public}d.", errCode);
159 return errCode;
160 }
161
162 // 释放Pixelmap, InitializationOptions实例
163 OH_PixelmapNative_Release(pixelmap);
164 return IMAGE_SUCCESS;
165 }
166 // [End ndk_pixelmap_bitmap_operations]