xref: /OK3568_Linux_fs/external/linux-rga/samples/padding_demo/src/rga_padding_demo.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2022  Rockchip Electronics Co., Ltd.
3  * Authors:
4  *     YuQiaowei <cerf.yu@rock-chips.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #define LOG_NDEBUG 0
20 #undef LOG_TAG
21 #define LOG_TAG "rga_padding_demo"
22 
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <time.h>
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/mman.h>
32 #include <math.h>
33 #include <fcntl.h>
34 #include <signal.h>
35 #include <unistd.h>
36 #include <linux/stddef.h>
37 
38 #include "RgaUtils.h"
39 #include "im2d.hpp"
40 
41 #include "utils.h"
42 
43 #define LOCAL_FILE_PATH "/data"
44 
main()45 int main() {
46     int ret = 0;
47     int src_width, src_height, src_format;
48     int dst_width, dst_height, dst_format;
49     char *src_buf, *dst_buf;
50     int src_buf_size, dst_buf_size;
51     int top, bottom, left, right;
52 
53     rga_buffer_t src_img, dst_img;
54     rga_buffer_handle_t src_handle, dst_handle;
55 
56     memset(&src_img, 0, sizeof(src_img));
57     memset(&dst_img, 0, sizeof(dst_img));
58 
59     top = 64;
60     bottom = top * 2;
61     left = bottom * 2;
62     right = left *2;
63 
64     src_width = 1280;
65     src_height = 720;
66     src_format = RK_FORMAT_RGBA_8888;
67 
68     dst_width = src_width + left + right;
69     dst_height = src_height + top + bottom;
70     dst_format = RK_FORMAT_RGBA_8888;
71 
72     src_buf_size = src_width * src_height * get_bpp_from_format(src_format);
73     dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format);
74 
75     src_buf = (char *)malloc(src_buf_size);
76     dst_buf = (char *)malloc(dst_buf_size);
77 
78     /* fill image data */
79     if (0 != read_image_from_file(src_buf, LOCAL_FILE_PATH, src_width, src_height, src_format, 0)) {
80         printf("src image read err\n");
81         draw_rgba(src_buf, src_width, src_height);
82     }
83     memset(dst_buf, 0x80, dst_buf_size);
84 
85     src_handle = importbuffer_virtualaddr(src_buf, src_buf_size);
86     dst_handle = importbuffer_virtualaddr(dst_buf, dst_buf_size);
87     if (src_handle == 0 || dst_handle == 0) {
88         printf("importbuffer failed!\n");
89         goto release_buffer;
90     }
91 
92     src_img = wrapbuffer_handle(src_handle, src_width, src_height, src_format);
93     dst_img = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
94 
95     /*
96      * Padding an image.
97                                     dst_img
98         --------------      ----------------------------
99         |            |      |       top_border         |
100         |  src_image |  =>  |                          |
101         |            |      |      --------------      |
102         --------------      |left_ |            |right_|
103                             |border|  dst_rect  |border|
104                             |      |            |      |
105                             |      --------------      |
106                             |       bottom_border      |
107                             ----------------------------
108      */
109 
110     ret = imcheck(src_img, dst_img, {}, {});
111     if (IM_STATUS_NOERROR != ret) {
112         printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
113         return -1;
114     }
115 
116     ret = immakeBorder(src_img, dst_img, top, bottom, left, right, IM_BORDER_REFLECT);
117     if (ret == IM_STATUS_SUCCESS) {
118         printf("%s running success!\n", LOG_TAG);
119     } else {
120         printf("%s running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
121         goto release_buffer;
122     }
123 
124 	printf("output [0x%x, 0x%x, 0x%x, 0x%x]\n", dst_buf[0], dst_buf[1], dst_buf[2], dst_buf[3]);
125     write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
126 
127 release_buffer:
128     if (src_handle)
129         releasebuffer_handle(src_handle);
130     if (dst_handle)
131         releasebuffer_handle(dst_handle);
132 
133     if (src_buf)
134         free(src_buf);
135     if (dst_buf)
136         free(dst_buf);
137 
138     return ret;
139 }
140