xref: /OK3568_Linux_fs/external/linux-rga/samples/alpha_demo/src/rga_alpha_yuv_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_alpha_rgba_yuv_demo"
22 
23 #include <iostream>
24 #include <fstream>
25 #include <sstream>
26 #include <cstddef>
27 #include <cmath>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/time.h>
31 #include <unistd.h>
32 
33 #include "RgaUtils.h"
34 #include "im2d.hpp"
35 
36 #include "utils.h"
37 
38 #define LOCAL_FILE_PATH "/data"
39 
main()40 int main() {
41     int ret = 0;
42     int fg_width, fg_height, fg_format;
43     int bg_width, bg_height, bg_format;
44     int output_width, output_height, output_format;
45     char *fg_buf, *bg_buf, *output_buf;
46     int fg_buf_size, bg_buf_size, output_buf_size;
47 
48     int usage = 0;
49     rga_buffer_t fg_img, bg_img, output_img;
50     im_rect fg_rect, bg_rect, output_rect;
51     rga_buffer_handle_t fg_handle, bg_handle, output_handle;
52 
53     memset(&fg_img, 0, sizeof(fg_img));
54     memset(&bg_img, 0, sizeof(bg_img));
55     memset(&output_img, 0, sizeof(output_img));
56     memset(&fg_rect, 0, sizeof(fg_rect));
57     memset(&bg_rect, 0, sizeof(bg_rect));
58     memset(&output_rect, 0, sizeof(output_rect));
59 
60     fg_width = 1920;
61     fg_height = 1080;
62     fg_format = RK_FORMAT_YCbCr_420_SP;
63 
64     bg_width = 1280;
65     bg_height = 720;
66     bg_format = RK_FORMAT_RGBA_8888;
67 
68     output_width = 1920;
69     output_height = 1080;
70     output_format = RK_FORMAT_YCbCr_420_SP;
71 
72     fg_buf_size = fg_width * fg_height * get_bpp_from_format(fg_format);
73     bg_buf_size = bg_width * bg_height * get_bpp_from_format(bg_format);
74     output_buf_size = output_width * output_height * get_bpp_from_format(output_format);
75 
76     fg_buf = (char *)malloc(fg_buf_size);
77     bg_buf = (char *)malloc(bg_buf_size);
78     output_buf = (char *)malloc(output_buf_size);
79 
80     /* fill image data */
81     if (0 != read_image_from_file(fg_buf, LOCAL_FILE_PATH, fg_width, fg_height, fg_format, 0)) {
82         printf("foreground image read err\n");
83         memset(fg_buf, 0xaa, fg_buf_size);
84     }
85     if (0 != read_image_from_file(fg_buf, LOCAL_FILE_PATH, bg_width, bg_height, bg_format, 0)) {
86         printf("background image read err\n");
87         memset(bg_buf, 0x66, bg_buf_size);
88     }
89     memset(output_buf, 0x80, output_buf_size);
90 
91     fg_handle = importbuffer_virtualaddr(fg_buf, fg_buf_size);
92     bg_handle = importbuffer_virtualaddr(bg_buf, bg_buf_size);
93     output_handle = importbuffer_virtualaddr(output_buf, output_buf_size);
94     if (fg_handle == 0 || bg_handle == 0 || output_handle == 0) {
95         printf("importbuffer failed!\n");
96         goto release_buffer;
97     }
98 
99     fg_img = wrapbuffer_handle(fg_handle, fg_width, fg_height, fg_format);
100     bg_img = wrapbuffer_handle(bg_handle, bg_width, bg_height, bg_format);
101     output_img = wrapbuffer_handle(output_handle, output_width, output_height, output_format);
102 
103     /*
104      * Configure the blended rectangular area here.
105      *      Here is intercepted from the foreground image (100, 200) as the starting point,
106      *  and a rectangle with the same resolution as the background image is blended with
107      *  the background image, and finally output to the output layer where (100, 200) is
108      *  the starting point.
109      *      fg_img => src_channel
110      *      bg_img => src1_channel
111      *      output_img => dst_channel
112         ---------------------------        --------------      ---------------------------
113         |         fg_img          |        |   bg_img/  |      |       output_img        |
114         |     --------------      |        |   bg_rect  |      |     --------------      |
115         |     |            |      |        |            |      |     |            |      |
116         |     |   fg_rect  |      |    +   --------------  =>  |     | output_rect|      |
117         |     |            |      |                            |     |(bg over fg)|      |
118         |     --------------      |                            |     --------------      |
119         |                         |                            |                         |
120         ---------------------------                            ---------------------------
121      */
122 
123     fg_rect.x = 100;
124     fg_rect.y = 200;
125     fg_rect.width = bg_width;
126     fg_rect.height = bg_height;
127 
128     bg_rect.x = 0;
129     bg_rect.y = 0;
130     bg_rect.width = fg_rect.width;
131     bg_rect.height = fg_rect.height;
132 
133     output_rect.x = fg_rect.x;
134     output_rect.y = fg_rect.y;
135     output_rect.width = fg_rect.width;
136     output_rect.height = fg_rect.height;
137 
138     ret = imcheck_composite(fg_img, output_img, bg_img, fg_rect, output_rect, bg_rect);
139     if (IM_STATUS_NOERROR != ret) {
140         printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
141         return -1;
142     }
143 
144     usage = IM_SYNC | IM_ALPHA_BLEND_DST_OVER | IM_ALPHA_BLEND_PRE_MUL;
145 
146     ret = improcess(fg_img, output_img, bg_img, fg_rect, output_rect, bg_rect, -1, NULL, NULL, usage);
147     if (ret == IM_STATUS_SUCCESS) {
148         printf("%s running success!\n", LOG_TAG);
149     } else {
150         printf("%s running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
151         goto release_buffer;
152     }
153 
154     write_image_to_file(output_buf, LOCAL_FILE_PATH, output_width, output_height, output_format, 0);
155 
156 release_buffer:
157     if (fg_handle)
158         releasebuffer_handle(fg_handle);
159     if (bg_handle)
160         releasebuffer_handle(bg_handle);
161     if (output_handle)
162         releasebuffer_handle(output_handle);
163 
164     if (fg_buf)
165         free(fg_buf);
166     if (bg_buf)
167         free(bg_buf);
168     if (output_buf)
169         free(output_buf);
170 
171     return ret;
172 }
173