4 * Copyright (C) 2008-2019 Adam Williams <broadcast at earthling dot net>
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.
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.
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
25 #include "foreground.h"
32 REGISTER_PLUGIN(ForegroundMain)
35 ForegroundConfig::ForegroundConfig()
43 int ForegroundConfig::equivalent(ForegroundConfig &that)
45 return (r == that.r && g == that.g && b == that.b &&
49 void ForegroundConfig::copy_from(ForegroundConfig &that)
57 void ForegroundConfig::interpolate(ForegroundConfig &prev, ForegroundConfig &next,
58 long prev_frame, long next_frame, long current_frame)
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);
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);
70 int ForegroundConfig::get_color()
72 int result = (r << 16) | (g << 8) | (b);
77 ForegroundColors::ForegroundColors(ForegroundWindow *window, ForegroundMain *plugin)
80 this->window = window;
81 this->plugin = plugin;
83 ForegroundColors::~ForegroundColors()
87 int ForegroundColors::handle_new_color(int output, int alpha)
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();
96 void ForegroundColors::update_gui(int output, int alpha)
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(),
106 this->plugin = plugin;
107 colors = new ForegroundColors(this, plugin);
110 ForegroundWindow::~ForegroundWindow()
115 void ForegroundWindow::create_objects()
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();
125 void ForegroundWindow::update()
127 int color = plugin->config.get_color();
128 int alpha = plugin->config.a;
129 colors->update_gui(color, alpha);
133 ForegroundMain::ForegroundMain(PluginServer *server)
134 : PluginVClient(server)
138 ForegroundMain::~ForegroundMain()
142 const char* ForegroundMain::plugin_title() { return N_("Foreground"); }
143 int ForegroundMain::is_realtime() { return 1; }
145 NEW_WINDOW_MACRO(ForegroundMain, ForegroundWindow)
147 LOAD_CONFIGURATION_MACRO(ForegroundMain, ForegroundConfig)
149 int ForegroundMain::is_synthesis()
155 int ForegroundMain::process_buffer(VFrame *frame,
156 int64_t start_position,
159 load_configuration();
161 int need_alpha = config.a != 0xff;
163 read_frame(frame, 0, start_position,
164 frame_rate, get_use_opengl());
167 int w = frame->get_w();
168 int h = frame->get_h();
171 #define MAIN_LOOP(type, temp, components, is_yuv) \
173 temp c1, c2, c3, a; \
174 temp transparency, max; \
176 yuv.rgb_to_yuv_8(config.r, config.g, config.b, \
179 transparency = 0xff - a; \
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; \
189 c1 = config.r; c2 = config.g; c3 = config.b; a = config.a; \
190 transparency = 0xff - a; \
192 if( sizeof(type) == 4 ) { \
193 c1 /= 0xff; c2 /= 0xff; c3 /= 0xff; a /= 0xff; \
194 transparency /= 0xff; \
197 /* multiply alpha */ \
198 if(components == 3) { \
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 ) { \
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); \
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;
236 void ForegroundMain::update_gui()
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");
244 window->unlock_window();
248 void ForegroundMain::save_data(KeyFrame *keyframe)
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);
258 output.terminate_string();
261 void ForegroundMain::read_data(KeyFrame *keyframe)
264 input.set_shared_input(keyframe->xbuf);
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);