xref: /OK3568_Linux_fs/external/linux-rga/samples/alpha_demo/src/rga_alpha_3channel_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_3channel_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     rga_buffer_t fg_img, bg_img, output_img;
49     rga_buffer_handle_t fg_handle, bg_handle, output_handle;
50 
51     memset(&fg_img, 0, sizeof(fg_img));
52     memset(&bg_img, 0, sizeof(bg_img));
53     memset(&output_img, 0, sizeof(output_img));
54 
55     fg_width = 1280;
56     fg_height = 720;
57     fg_format = RK_FORMAT_RGBA_8888;
58 
59     bg_width = 1280;
60     bg_height = 720;
61     bg_format = RK_FORMAT_RGBA_8888;
62 
63     output_width = 1280;
64     output_height = 720;
65     output_format = RK_FORMAT_RGBA_8888;
66 
67     fg_buf_size = fg_width * fg_height * get_bpp_from_format(fg_format);
68     bg_buf_size = bg_width * bg_height * get_bpp_from_format(bg_format);
69     output_buf_size = output_width * output_height * get_bpp_from_format(output_format);
70 
71     fg_buf = (char *)malloc(fg_buf_size);
72     bg_buf = (char *)malloc(bg_buf_size);
73     output_buf = (char *)malloc(output_buf_size);
74 
75     /* fill image data */
76     if (0 != read_image_from_file(fg_buf, LOCAL_FILE_PATH, fg_width, fg_height, fg_format, 0)) {
77         printf("foreground image read err\n");
78         memset(fg_buf, 0xaa, fg_buf_size);
79     }
80     if (0 != read_image_from_file(fg_buf, LOCAL_FILE_PATH, bg_width, bg_height, bg_format, 1)) {
81         printf("background image read err\n");
82         memset(bg_buf, 0x66, bg_buf_size);
83     }
84     memset(output_buf, 0x80, output_buf_size);
85 
86     fg_handle = importbuffer_virtualaddr(fg_buf, fg_buf_size);
87     bg_handle = importbuffer_virtualaddr(bg_buf, bg_buf_size);
88     output_handle = importbuffer_virtualaddr(output_buf, output_buf_size);
89     if (fg_handle == 0 || bg_handle == 0 || output_handle == 0) {
90         printf("importbuffer failed!\n");
91         goto release_buffer;
92     }
93 
94     fg_img = wrapbuffer_handle(fg_handle, fg_width, fg_height, fg_format);
95     bg_img = wrapbuffer_handle(bg_handle, bg_width, bg_height, bg_format);
96     output_img = wrapbuffer_handle(output_handle, output_width, output_height, output_format);
97 
98     /*
99      * Here are two RGBA8888 images of the same size for src_over overlay and
100      * output them to another buffer.
101         --------------        --------------      --------------
102         |            |        |            |      | output_img |
103         |   fg_img   |    +   |   bg_img   |  =>  | fg over bg |
104         |            |        |            |      |            |
105         --------------        --------------      --------------
106      */
107 
108     ret = imcheck_composite(fg_img, output_img, bg_img, {}, {}, {});
109     if (IM_STATUS_NOERROR != ret) {
110         printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
111         return -1;
112     }
113 
114     ret = imcomposite(fg_img, bg_img, output_img, IM_ALPHA_BLEND_SRC_OVER | IM_ALPHA_BLEND_PRE_MUL);
115     if (ret == IM_STATUS_SUCCESS) {
116         printf("%s running success!\n", LOG_TAG);
117     } else {
118         printf("%s running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
119         goto release_buffer;
120     }
121 
122     write_image_to_file(output_buf, LOCAL_FILE_PATH, output_width, output_height, output_format, 0);
123 
124 release_buffer:
125     if (fg_handle)
126         releasebuffer_handle(fg_handle);
127     if (bg_handle)
128         releasebuffer_handle(bg_handle);
129     if (output_handle)
130         releasebuffer_handle(output_handle);
131 
132     if (fg_buf)
133         free(fg_buf);
134     if (bg_buf)
135         free(bg_buf);
136     if (output_buf)
137         free(output_buf);
138 
139     return ret;
140 }
141