no longer need ffmpeg patch0 which was for Termux
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / foreground / foreground.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008-2019 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <math.h>
22 #include <stdint.h>
23 #include <string.h>
24
25 #include "foreground.h"
26 #include "filexml.h"
27 #include "keyframe.h"
28 #include "language.h"
29 #include "vframe.h"
30
31
32 REGISTER_PLUGIN(ForegroundMain)
33
34
35 ForegroundConfig::ForegroundConfig()
36 {
37         r = 0xff;
38         g = 0xff;
39         b = 0xff;
40         a = 0xff;
41 }
42
43 int ForegroundConfig::equivalent(ForegroundConfig &that)
44 {
45         return (r == that.r && g == that.g && b == that.b &&
46                 a == that.a);
47 }
48
49 void ForegroundConfig::copy_from(ForegroundConfig &that)
50 {
51         r = that.r;
52         g = that.g;
53         b = that.b;
54         a = that.a;
55 }
56
57 void ForegroundConfig::interpolate(ForegroundConfig &prev, ForegroundConfig &next,
58                 long prev_frame, long next_frame, long current_frame)
59 {
60         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
61         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
62
63
64         r = (int)(prev.r * prev_scale + next.r * next_scale);
65         g = (int)(prev.g * prev_scale + next.g * next_scale);
66         b = (int)(prev.b * prev_scale + next.b * next_scale);
67         a = (int)(prev.a * prev_scale + next.a * next_scale);
68 }
69
70 int ForegroundConfig::get_color()
71 {
72         int result = (r << 16) | (g << 8) | (b);
73         return result;
74 }
75
76
77 ForegroundColors::ForegroundColors(ForegroundWindow *window, ForegroundMain *plugin)
78  : ColorGUI(window)
79 {
80         this->window = window;
81         this->plugin = plugin;
82 }
83 ForegroundColors::~ForegroundColors()
84 {
85 }
86
87 int ForegroundColors::handle_new_color(int output, int alpha)
88 {
89         plugin->config.r = (output>>16) & 0xff;
90         plugin->config.g = (output>>8) & 0xff;
91         plugin->config.b = (output) & 0xff;
92         plugin->config.a = alpha;
93         plugin->send_configure_change();
94         return 1;
95 }
96 void ForegroundColors::update_gui(int output, int alpha)
97 {
98 }
99
100 ForegroundWindow::ForegroundWindow(ForegroundMain *plugin)
101  : PluginClientWindow(plugin,
102         ColorWindow::calculate_w(), ColorWindow::calculate_h() + BC_OKButton::calculate_h(),
103         ColorWindow::calculate_w(), ColorWindow::calculate_h() + BC_OKButton::calculate_h(),
104         0)
105 {
106         this->plugin = plugin;
107         colors = new ForegroundColors(this, plugin);
108 }
109
110 ForegroundWindow::~ForegroundWindow()
111 {
112         delete colors;
113 }
114
115 void ForegroundWindow::create_objects()
116 {
117         colors = new ForegroundColors(this, plugin);
118         int color = plugin->config.get_color();
119         int alpha = plugin->config.a;
120         colors->start_selection(color, alpha, 1);
121         colors->create_objects();
122         show_window();
123 }
124
125 void ForegroundWindow::update()
126 {
127         int color = plugin->config.get_color();
128         int alpha = plugin->config.a;
129         colors->update_gui(color, alpha);
130 }
131
132
133 ForegroundMain::ForegroundMain(PluginServer *server)
134  : PluginVClient(server)
135 {
136 }
137
138 ForegroundMain::~ForegroundMain()
139 {
140 }
141
142 const char* ForegroundMain::plugin_title() { return N_("Foreground"); }
143 int ForegroundMain::is_realtime() { return 1; }
144
145 NEW_WINDOW_MACRO(ForegroundMain, ForegroundWindow)
146
147 LOAD_CONFIGURATION_MACRO(ForegroundMain, ForegroundConfig)
148
149 int ForegroundMain::is_synthesis()
150 {
151         return 1;
152 }
153
154
155 int ForegroundMain::process_buffer(VFrame *frame,
156         int64_t start_position,
157         double frame_rate)
158 {
159         load_configuration();
160
161         int need_alpha = config.a != 0xff;
162         if( need_alpha ) {
163                 read_frame(frame, 0, start_position,
164                         frame_rate, get_use_opengl());
165         }
166
167         int w = frame->get_w();
168         int h = frame->get_h();
169
170
171 #define MAIN_LOOP(type, temp, components, is_yuv) \
172 { \
173         temp c1, c2, c3, a; \
174         temp transparency, max; \
175         if( is_yuv ) { \
176                 yuv.rgb_to_yuv_8(config.r, config.g, config.b, \
177                         c1, c2, c3); \
178                 a = config.a; \
179                 transparency = 0xff - a; \
180                 max = 0xff; \
181 /* multiply alpha */ \
182                 if( components == 3 ) { \
183                         c1 = a * c1 / 0xff; \
184                         c2 = (a * c2 + 0x80 * transparency) / 0xff; \
185                         c3 = (a * c3 + 0x80 * transparency) / 0xff; \
186                 } \
187         } \
188         else { \
189                 c1 = config.r; c2 = config.g; c3 = config.b; a = config.a; \
190                 transparency = 0xff - a; \
191                 max = 0xff; \
192                 if( sizeof(type) == 4 ) { \
193                         c1 /= 0xff; c2 /= 0xff; c3 /= 0xff; a /= 0xff; \
194                         transparency /= 0xff; \
195                         max = 1.0; \
196                 } \
197 /* multiply alpha */ \
198                 if(components == 3) { \
199                         c1 = a * c1 / max; \
200                         c2 = a * c2 / max; \
201                         c3 = a * c3 / max; \
202                 } \
203         } \
204  \
205         for( int i=0; i<h; ++i ) { \
206                 type *row = (type*)frame->get_rows()[i]; \
207                 for( int j=0; j<w; ++j ) { \
208                         if( components == 3 ) { \
209                                 *row++ = c1; \
210                                 *row++ = c2; \
211                                 *row++ = c3; \
212                         } \
213                         else { \
214                                 row[0] = (transparency * row[0] + a * c1) / max; \
215                                 row[1] = (transparency * row[1] + a * c2) / max; \
216                                 row[2] = (transparency * row[2] + a * c3) / max; \
217                                 row[3] = MAX(row[3], a); \
218                                 row += components; \
219                         } \
220                 } \
221         } \
222 }
223         switch( frame->get_color_model() ) {
224         case BC_RGB888:     MAIN_LOOP(uint8_t, int, 3, 0) break;
225         case BC_RGB_FLOAT:  MAIN_LOOP(float, float, 3, 0) break;
226         case BC_YUV888:     MAIN_LOOP(uint8_t, int, 3, 1) break;
227         case BC_RGBA8888:   MAIN_LOOP(uint8_t, int, 4, 0) break;
228         case BC_RGBA_FLOAT: MAIN_LOOP(float, float, 4, 0) break;
229         case BC_YUVA8888:   MAIN_LOOP(uint8_t, int, 4, 1) break;
230         }
231
232         return 0;
233 }
234
235
236 void ForegroundMain::update_gui()
237 {
238         if( !thread ) return;
239         ForegroundWindow *window = (ForegroundWindow*)thread->window;
240         if( !window ) return;
241         if( !load_configuration() ) return;
242         window->lock_window("ForegroundMain::update_gui");
243         window->update();
244         window->unlock_window();
245 }
246
247
248 void ForegroundMain::save_data(KeyFrame *keyframe)
249 {
250         FileXML output;
251         output.set_shared_output(keyframe->xbuf);
252         output.tag.set_title("FOREGROUND");
253         output.tag.set_property("R", config.r);
254         output.tag.set_property("G", config.g);
255         output.tag.set_property("B", config.b);
256         output.tag.set_property("A", config.a);
257         output.append_tag();
258         output.terminate_string();
259 }
260
261 void ForegroundMain::read_data(KeyFrame *keyframe)
262 {
263         FileXML input;
264         input.set_shared_input(keyframe->xbuf);
265
266         int result = 0;
267         while( !(result = input.read_tag()) ) {
268                 if( input.tag.title_is("FOREGROUND") ) {
269                         config.r = input.tag.get_property("R", config.r);
270                         config.g = input.tag.get_property("G", config.g);
271                         config.b = input.tag.get_property("B", config.b);
272                         config.a = input.tag.get_property("A", config.a);
273                 }
274         }
275 }
276