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