xref: /OK3568_Linux_fs/external/linux-rga/samples/resize_demo/src/rga_resize_rect_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_resize_rect_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 #include "utils.h"
41 
42 #define LOCAL_FILE_PATH "/data"
43 
main()44 int main() {
45     int ret = 0;
46     int src_width, src_height, src_format;
47     int dst_width, dst_height, dst_format;
48     char *src_buf, *dst_buf;
49     int src_buf_size, dst_buf_size;
50 
51     rga_buffer_t src_img, dst_img;
52     im_rect dst_rect[4];
53     rga_buffer_handle_t src_handle, dst_handle;
54 
55     memset(&src_img, 0, sizeof(src_img));
56     memset(&dst_img, 0, sizeof(dst_img));
57     memset(&dst_rect, 0, sizeof(dst_rect));
58 
59     src_width = 1280;
60     src_height = 720;
61     src_format = RK_FORMAT_RGBA_8888;
62 
63     dst_width = 1920;
64     dst_height = 1080;
65     dst_format = RK_FORMAT_RGBA_8888;
66 
67     src_buf_size = src_width * src_height * get_bpp_from_format(src_format);
68     dst_buf_size = dst_width * dst_height * get_bpp_from_format(dst_format);
69 
70     src_buf = (char *)malloc(src_buf_size);
71     dst_buf = (char *)malloc(dst_buf_size);
72 
73     /* fill image data */
74     if (0 != read_image_from_file(src_buf, LOCAL_FILE_PATH, src_width, src_height, src_format, 0)) {
75         printf("src image read err\n");
76         draw_rgba(src_buf, src_width, src_height);
77     }
78     memset(dst_buf, 0x80, dst_buf_size);
79 
80     src_handle = importbuffer_virtualaddr(src_buf, src_buf_size);
81     dst_handle = importbuffer_virtualaddr(dst_buf, dst_buf_size);
82     if (src_handle == 0 || dst_handle == 0) {
83         printf("importbuffer failed!\n");
84         goto release_buffer;
85     }
86 
87     src_img = wrapbuffer_handle(src_handle, src_width, src_height, src_format);
88     dst_img = wrapbuffer_handle(dst_handle, dst_width, dst_height, dst_format);
89 
90     /*
91      * Scaling and splicing the src image into 4 grids
92         --------------    ---------------------
93         |            |    |         |         |
94         |  src_img   |    |         |         |
95         |            |    |         |         |
96         -------------- => ---------------------
97                           |         |         |
98                           |         |         |
99                           |         |         |
100                           ---------------------
101      */
102 
103     dst_rect[0].x = 0;
104     dst_rect[0].y = 0;
105     dst_rect[0].width = dst_width / 2;
106     dst_rect[0].height = dst_height / 2;
107 
108     dst_rect[1].x = dst_width / 2;
109     dst_rect[1].y = 0;
110     dst_rect[1].width = dst_width / 2;
111     dst_rect[1].height = dst_height / 2;
112 
113     dst_rect[2].x = 0;
114     dst_rect[2].y = dst_height / 2;
115     dst_rect[2].width = dst_width / 2;
116     dst_rect[2].height = dst_height / 2;
117 
118     dst_rect[3].x = dst_width / 2;
119     dst_rect[3].y = dst_height / 2;
120     dst_rect[3].width = dst_width / 2;
121     dst_rect[3].height = dst_height / 2;
122 
123     for (int i = 0; i < 4; i++) {
124         ret = imcheck(src_img, dst_img, {}, dst_rect[i]);
125         if (IM_STATUS_NOERROR != ret) {
126             printf("%d, [%d] check error! %s", __LINE__, i, imStrError((IM_STATUS)ret));
127             return -1;
128         }
129 
130         ret = improcess(src_img, dst_img, {}, {}, dst_rect[i], {}, IM_SYNC);
131         if (ret == IM_STATUS_SUCCESS) {
132             printf("%s [%d] running success!\n", LOG_TAG, i);
133         } else {
134             printf("%s [%d] running failed, %s\n", LOG_TAG, i, imStrError((IM_STATUS)ret));
135             goto release_buffer;
136         }
137     }
138 
139     write_image_to_file(dst_buf, LOCAL_FILE_PATH, dst_width, dst_height, dst_format, 0);
140 
141 release_buffer:
142     if (src_handle)
143         releasebuffer_handle(src_handle);
144     if (dst_handle)
145         releasebuffer_handle(dst_handle);
146 
147     if (src_buf)
148         free(src_buf);
149     if (dst_buf)
150         free(dst_buf);
151 
152     return ret;
153 }
154