1# Using Image_NativeModule for PixelMap Operations
2
3You can use the **Pixelmap** class to create, operation and release a PixelMap, and obtain its width, height, pixel format, alpha type, and row stride.
4
5## How to Develop
6
7### Adding a Link Library
8
9Open the **src/main/cpp/CMakeLists.txt** file of the native project, add **libpixelmap.so** and **libhilog_ndk.z.so** (on which the log APIs depend) to the **target_link_libraries** dependency.
10
11```txt
12target_link_libraries(entry PUBLIC libhilog_ndk.z.so libpixelmap.so)
13```
14
15### Calling the Native APIs
16
17For details about the APIs, see [Image_NativeModule](../../reference/apis-image-kit/_image___native_module.md).
18
19Implement the C APIs in **hello.cpp**. Refer to the sample code below.
20
21**Example of Using the Pixelmap APIs**
22
23Create a **Pixelmap** instance after parameter initialization, read and write pixel data, and perform operations such as scaling, translating, flipping, rotating, and cropping on the image.
24
25   ```c++
26
27      #include <linux/kd.h>
28      #include <string>
29
30      #include <hilog/log.h>
31      #include <multimedia/image_framework/image/pixelmap_native.h>
32
33      #undef LOG_DOMAIN
34      #undef LOG_TAG
35      #define LOG_DOMAIN 0x3200
36      #define LOG_TAG "MY_TAG"
37
38      Image_ErrorCode PixelmapTest()
39      {
40          uint8_t data[96];
41          size_t dataSize = 96;
42          for (int i = 0; i < dataSize; i++) {
43              data[i] = i + 1;
44          }
45
46          // Create a parameter structure instance and set parameters.
47          OH_Pixelmap_InitializationOptions *createOpts;
48          OH_PixelmapInitializationOptions_Create(&createOpts);
49          OH_PixelmapInitializationOptions_SetWidth(createOpts, 6);
50          OH_PixelmapInitializationOptions_SetHeight(createOpts, 4);
51          OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, PIXEL_FORMAT_RGBA_8888);
52          OH_PixelmapInitializationOptions_SetAlphaType(createOpts, PIXELMAP_ALPHA_TYPE_UNKNOWN);
53
54          // Create a Pixelmap instance.
55          OH_PixelmapNative *pixelmap = nullptr;
56          Image_ErrorCode errCode = OH_PixelmapNative_CreatePixelmap(data, dataSize, createOpts, &pixelmap);
57
58          // Read the image pixel data and write the data to the array.
59          uint8_t destination[96];
60          size_t destinationSize = 96;
61          errCode = OH_PixelmapNative_ReadPixels(pixelmap, destination, &destinationSize);
62          if (errCode != IMAGE_SUCCESS) {
63              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_ReadPixels failed, errCode: %{public}d.", errCode);
64              return errCode;
65          }
66
67          // Read the image data in the buffer and write the data to the Pixelmap instance.
68          uint8_t source[96];
69          size_t sourceSize = 96;
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, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_WritePixels failed, errCode: %{public}d.", errCode);
76              return errCode;
77          }
78
79          // Create an image information instance and obtain the pixel information.
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, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_GetImageInfo failed, errCode: %{public}d.", errCode);
85              return errCode;
86          }
87
88          // Obtain the width, height, pixel format, and alpha type of the image.
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, "ImagePixelmapNativeCTest pixelmapTest GetImageInfo success, width: %{public}d, height: %{public}d, rowStride: %{public}d, pixelFormat: %{public}d, alphaType: %{public}d.", width, height, rowStride, pixelFormat, alphaType);
98
99          // Set the opacity rate to enable the image to achieve the corresponding opacity effect.
100          errCode = OH_PixelmapNative_Opacity(pixelmap, 0.5);
101          if (errCode != IMAGE_SUCCESS) {
102              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_Opacity failed, errCode: %{public}d.", errCode);
103              return errCode;
104          }
105
106          // Scale the image.
107          errCode = OH_PixelmapNative_Scale(pixelmap, 2.0, 1.0);
108          if (errCode != IMAGE_SUCCESS) {
109              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_Scale failed, errCode: %{public}d.", errCode);
110              return errCode;
111          }
112
113          // Translate the image.
114          errCode = OH_PixelmapNative_Translate(pixelmap, 50.0, 10.0);
115          if (errCode != IMAGE_SUCCESS) {
116              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_Translate failed, errCode: %{public}d.", errCode);
117              return errCode;
118          }
119
120          // Rotate the image.
121          errCode = OH_PixelmapNative_Rotate(pixelmap, 90.0);
122          if (errCode != IMAGE_SUCCESS) {
123              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_Rotate failed, errCode: %{public}d.", errCode);
124              return errCode;
125          }
126
127          // Flip the image.
128          errCode = OH_PixelmapNative_Flip(pixelmap, true, true);
129          if (errCode != IMAGE_SUCCESS) {
130              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_Flip failed, errCode: %{public}d.", errCode);
131              return errCode;
132          }
133
134          // Crop the image.
135          Image_Region region;
136          region.x = 100;
137          region.y = 100;
138          region.width = 6;
139          region.height = 4;
140          errCode = OH_PixelmapNative_Crop(pixelmap, &region);
141          if (errCode != IMAGE_SUCCESS) {
142              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_Crop failed, errCode: %{public}d.", errCode);
143              return errCode;
144          }
145
146          // Release the Pixelmap and InitializationOptions instances.
147          OH_PixelmapNative_Release(pixelmap);
148          OH_PixelmapInitializationOptions_Release(createOpts);
149          return IMAGE_SUCCESS;
150      }
151   ```
152