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_colorkey_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 int usage = 0;
48 rga_buffer_t fg_img, bg_img;
49 rga_buffer_handle_t fg_handle, bg_handle;
50 im_colorkey_range range;
51
52 memset(&fg_img, 0, sizeof(fg_img));
53 memset(&bg_img, 0, sizeof(bg_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 fg_buf_size = fg_width * fg_height * get_bpp_from_format(fg_format);
64 bg_buf_size = bg_width * bg_height * get_bpp_from_format(bg_format);
65
66 fg_buf = (char *)malloc(fg_buf_size);
67 bg_buf = (char *)malloc(bg_buf_size);
68
69 /* fill image data */
70 if (0 != read_image_from_file(fg_buf, LOCAL_FILE_PATH, fg_width, fg_height, fg_format, 0)) {
71 printf("foreground image read err\n");
72 memset(fg_buf, 0xaa, fg_buf_size);
73 }
74 if (0 != read_image_from_file(fg_buf, LOCAL_FILE_PATH, bg_width, bg_height, bg_format, 1)) {
75 printf("background image read err\n");
76 memset(bg_buf, 0x66, bg_buf_size);
77 }
78
79 /* Fill color on foreground image. */
80 for (int i = 0; i < fg_height; i++) {
81 for (int j = 0; j < fg_width/8; j++) {
82 fg_buf[(i*fg_width*4) + j*4 + 0] = 0x11; //R
83 fg_buf[(i*fg_width*4) + j*4 + 1] = 0x11; //G
84 fg_buf[(i*fg_width*4) + j*4 + 2] = 0x11; //B
85 fg_buf[(i*fg_width*4) + j*4 + 3] = 0xff; //A
86 }
87 for (int j = fg_width/8; j < fg_width/4; j++) {
88 fg_buf[(i*fg_width*4) + j*4 + 0] = 0x66;
89 fg_buf[(i*fg_width*4) + j*4 + 1] = 0x66;
90 fg_buf[(i*fg_width*4) + j*4 + 2] = 0x66;
91 fg_buf[(i*fg_width*4) + j*4 + 3] = 0xff;
92 }
93 for (int j = fg_width/4; j < fg_width/8*3; j++) {
94 fg_buf[(i*fg_width*4) + j*4 + 0] = 0xaa;
95 fg_buf[(i*fg_width*4) + j*4 + 1] = 0xaa;
96 fg_buf[(i*fg_width*4) + j*4 + 2] = 0xaa;
97 fg_buf[(i*fg_width*4) + j*4 + 3] = 0xff;
98 }
99 for (int j = fg_width/8*3; j < fg_width/2; j++) {
100 fg_buf[(i*fg_width*4) + j*4 + 0] = 0xff;
101 fg_buf[(i*fg_width*4) + j*4 + 1] = 0xff;
102 fg_buf[(i*fg_width*4) + j*4 + 2] = 0xff;
103 fg_buf[(i*fg_width*4) + j*4 + 3] = 0xff;
104 }
105 }
106
107 fg_handle = importbuffer_virtualaddr(fg_buf, fg_buf_size);
108 bg_handle = importbuffer_virtualaddr(bg_buf, bg_buf_size);
109 if (fg_handle == 0 || bg_handle == 0) {
110 printf("importbuffer failed!\n");
111 goto release_buffer;
112 }
113
114 fg_img = wrapbuffer_handle(fg_handle, fg_width, fg_height, fg_format);
115 bg_img = wrapbuffer_handle(bg_handle, bg_width, bg_height, bg_format);
116
117 /*
118 * Overlays the background image after removing a specified
119 * range of colors in the foreground image.
120 -------------- -------------- --------------
121 | | | | | |
122 | fg_img | + | bg_img | => | fg over bg |
123 | | | | | |
124 -------------- -------------- --------------
125 */
126
127 ret = imcheck(fg_img, bg_img, {}, {});
128 if (IM_STATUS_NOERROR != ret) {
129 printf("%d, check error! %s", __LINE__, imStrError((IM_STATUS)ret));
130 return -1;
131 }
132
133 range.min = 0xFF666666; //ABGR
134 range.max = 0xFFaaaaaa;
135
136 ret = imcolorkey(fg_img, bg_img, range, IM_ALPHA_COLORKEY_NORMAL);
137 printf("%s .... %s\n", LOG_TAG, imStrError(ret));
138 if (ret != IM_STATUS_SUCCESS)
139 goto release_buffer;
140
141 write_image_to_file(bg_buf, LOCAL_FILE_PATH, bg_width, bg_height, bg_format, 0);
142
143 release_buffer:
144 if (fg_handle)
145 releasebuffer_handle(fg_handle);
146 if (bg_handle)
147 releasebuffer_handle(bg_handle);
148
149 if (fg_buf)
150 free(fg_buf);
151 if (bg_buf)
152 free(bg_buf);
153
154 return ret;
155 }
156