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_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 char *fg_buf, *bg_buf;
45 int fg_buf_size, bg_buf_size;
46
47 rga_buffer_t fg_img, bg_img;
48 rga_buffer_handle_t fg_handle, bg_handle;
49
50 memset(&fg_img, 0, sizeof(fg_img));
51 memset(&bg_img, 0, sizeof(bg_img));
52
53 fg_width = 1280;
54 fg_height = 720;
55 fg_format = RK_FORMAT_RGBA_8888;
56
57 bg_width = 1280;
58 bg_height = 720;
59 bg_format = RK_FORMAT_RGBA_8888;
60
61 fg_buf_size = fg_width * fg_height * get_bpp_from_format(fg_format);
62 bg_buf_size = bg_width * bg_height * get_bpp_from_format(bg_format);
63
64 fg_buf = (char *)malloc(fg_buf_size);
65 bg_buf = (char *)malloc(bg_buf_size);
66
67 /* fill image data */
68 if (0 != read_image_from_file(fg_buf, LOCAL_FILE_PATH, fg_width, fg_height, fg_format, 0)) {
69 printf("foreground image read err\n");
70 memset(fg_buf, 0xaa, fg_buf_size);
71 }
72 if (0 != read_image_from_file(fg_buf, LOCAL_FILE_PATH, bg_width, bg_height, bg_format, 1)) {
73 printf("background image read err\n");
74 memset(bg_buf, 0x66, bg_buf_size);
75 }
76
77 fg_handle = importbuffer_virtualaddr(fg_buf, fg_buf_size);
78 bg_handle = importbuffer_virtualaddr(bg_buf, bg_buf_size);
79 if (fg_handle == 0 || bg_handle == 0) {
80 printf("importbuffer failed!\n");
81 goto release_buffer;
82 }
83
84 fg_img = wrapbuffer_handle(fg_handle, fg_width, fg_height, fg_format);
85 bg_img = wrapbuffer_handle(bg_handle, bg_width, bg_height, bg_format);
86
87 /*
88 * Here are two RGBA8888 images of the same size for src_over overlay.
89 -------------- -------------- --------------
90 | | | | | |
91 | fg_img | + | bg_img | => | fg over bg |
92 | | | | | |
93 -------------- -------------- --------------
94 */
95
96 ret = imcheck(fg_img, bg_img, {}, {});
97 if (IM_STATUS_NOERROR != ret) {
98 printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
99 return -1;
100 }
101
102 ret = imblend(fg_img, bg_img, IM_ALPHA_BLEND_SRC_OVER | IM_ALPHA_BLEND_PRE_MUL);
103 if (ret == IM_STATUS_SUCCESS) {
104 printf("%s running success!\n", LOG_TAG);
105 } else {
106 printf("%s running failed, %s\n", LOG_TAG, imStrError((IM_STATUS)ret));
107 goto release_buffer;
108 }
109
110 write_image_to_file(bg_buf, LOCAL_FILE_PATH, bg_width, bg_height, bg_format, 0);
111
112 release_buffer:
113 if (fg_handle)
114 releasebuffer_handle(fg_handle);
115 if (bg_handle)
116 releasebuffer_handle(bg_handle);
117
118 if (fg_buf)
119 free(fg_buf);
120 if (bg_buf)
121 free(bg_buf);
122
123 return ret;
124 }
125