4 * Copyright (C) 2008 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
22 #include "bcdisplayinfo.h"
29 #include "pluginvclient.h"
37 class InvertVideoEffect;
40 class InvertVideoConfig
45 void copy_from(InvertVideoConfig &src);
46 int equivalent(InvertVideoConfig &src);
47 void interpolate(InvertVideoConfig &prev,
48 InvertVideoConfig &next,
56 class InvertVideoEnable : public BC_CheckBox
59 InvertVideoEnable(InvertVideoEffect *plugin, int *output, int x, int y, char *text);
61 InvertVideoEffect *plugin;
65 class InvertVideoWindow : public PluginClientWindow
68 InvertVideoWindow(InvertVideoEffect *plugin);
69 void create_objects();
70 InvertVideoEnable *r, *g, *b, *a;
71 InvertVideoEffect *plugin;
75 class InvertVideoEffect : public PluginVClient
78 InvertVideoEffect(PluginServer *server);
80 PLUGIN_CLASS_MEMBERS(InvertVideoConfig)
81 int process_buffer(VFrame *frame,
82 int64_t start_position,
85 void save_data(KeyFrame *keyframe);
86 void read_data(KeyFrame *keyframe);
95 REGISTER_PLUGIN(InvertVideoEffect)
103 InvertVideoConfig::InvertVideoConfig()
111 void InvertVideoConfig::copy_from(InvertVideoConfig &src)
119 int InvertVideoConfig::equivalent(InvertVideoConfig &src)
127 void InvertVideoConfig::interpolate(InvertVideoConfig &prev,
128 InvertVideoConfig &next,
142 InvertVideoEnable::InvertVideoEnable(InvertVideoEffect *plugin, int *output, int x, int y, char *text)
143 : BC_CheckBox(x, y, *output, text)
145 this->plugin = plugin;
146 this->output = output;
148 int InvertVideoEnable::handle_event()
150 *output = get_value();
151 plugin->send_configure_change();
159 InvertVideoWindow::InvertVideoWindow(InvertVideoEffect *plugin)
160 : PluginClientWindow(plugin,
167 this->plugin = plugin;
170 void InvertVideoWindow::create_objects()
173 add_subwindow(r = new InvertVideoEnable(plugin, &plugin->config.r, x, y, _("Invert R")));
175 add_subwindow(g = new InvertVideoEnable(plugin, &plugin->config.g, x, y, _("Invert G")));
177 add_subwindow(b = new InvertVideoEnable(plugin, &plugin->config.b, x, y, _("Invert B")));
179 add_subwindow(a = new InvertVideoEnable(plugin, &plugin->config.a, x, y, _("Invert A")));
194 InvertVideoEffect::InvertVideoEffect(PluginServer *server)
195 : PluginVClient(server)
199 InvertVideoEffect::~InvertVideoEffect()
204 const char* InvertVideoEffect::plugin_title() { return N_("Invert Video"); }
205 int InvertVideoEffect::is_realtime() { return 1; }
207 NEW_WINDOW_MACRO(InvertVideoEffect, InvertVideoWindow)
208 LOAD_CONFIGURATION_MACRO(InvertVideoEffect, InvertVideoConfig)
210 void InvertVideoEffect::update_gui()
214 thread->window->lock_window();
215 load_configuration();
216 ((InvertVideoWindow*)thread->window)->r->update(config.r);
217 ((InvertVideoWindow*)thread->window)->g->update(config.g);
218 ((InvertVideoWindow*)thread->window)->b->update(config.b);
219 ((InvertVideoWindow*)thread->window)->a->update(config.a);
220 thread->window->unlock_window();
225 void InvertVideoEffect::save_data(KeyFrame *keyframe)
228 output.set_shared_output(keyframe->xbuf);
229 output.tag.set_title("INVERTVIDEO");
230 output.tag.set_property("R", config.r);
231 output.tag.set_property("G", config.g);
232 output.tag.set_property("B", config.b);
233 output.tag.set_property("A", config.a);
235 output.tag.set_title("/INVERTVIDEO");
237 output.append_newline();
238 output.terminate_string();
241 void InvertVideoEffect::read_data(KeyFrame *keyframe)
244 input.set_shared_input(keyframe->xbuf);
245 while(!input.read_tag())
247 if(input.tag.title_is("INVERTVIDEO"))
249 config.r = input.tag.get_property("R", config.r);
250 config.g = input.tag.get_property("G", config.g);
251 config.b = input.tag.get_property("B", config.b);
252 config.a = input.tag.get_property("A", config.a);
258 #define INVERT_MACRO(type, components, max) \
260 for(int i = 0; i < frame->get_h(); i++) \
262 type *in_row = (type*)frame->get_rows()[i]; \
263 type *out_row = (type*)frame->get_rows()[i]; \
265 for(int j = 0; j < w; j++) \
267 if(config.r) out_row[0] = max - in_row[0]; \
268 if(config.g) out_row[1] = max - in_row[1]; \
269 if(config.b) out_row[2] = max - in_row[2]; \
270 if(components == 4) \
271 if(config.a) out_row[3] = max - in_row[3]; \
273 in_row += components; \
274 out_row += components; \
279 int InvertVideoEffect::process_buffer(VFrame *frame,
280 int64_t start_position,
283 load_configuration();
292 if(config.r || config.g || config.b || config.a)
299 int w = frame->get_w();
301 switch(frame->get_color_model())
304 INVERT_MACRO(float, 3, 1.0)
308 INVERT_MACRO(unsigned char, 3, 0xff)
311 INVERT_MACRO(float, 4, 1.0)
315 INVERT_MACRO(unsigned char, 4, 0xff)
319 INVERT_MACRO(uint16_t, 3, 0xffff)
321 case BC_RGBA16161616:
322 case BC_YUVA16161616:
323 INVERT_MACRO(uint16_t, 4, 0xffff)
331 int InvertVideoEffect::handle_opengl()
334 static const char *invert_frag =
335 "uniform sampler2D tex;\n"
336 "uniform bool do_r;\n"
337 "uniform bool do_g;\n"
338 "uniform bool do_b;\n"
339 "uniform bool do_a;\n"
342 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
343 " if(do_r) gl_FragColor.r = 1.0 - gl_FragColor.r;\n"
344 " if(do_g) gl_FragColor.g = 1.0 - gl_FragColor.g;\n"
345 " if(do_b) gl_FragColor.b = 1.0 - gl_FragColor.b;\n"
346 " if(do_a) gl_FragColor.a = 1.0 - gl_FragColor.a;\n"
349 get_output()->to_texture();
350 get_output()->enable_opengl();
352 unsigned int frag_shader = VFrame::make_shader(0, invert_frag, 0);
353 glUseProgram(frag_shader);
354 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
355 glUniform1i(glGetUniformLocation(frag_shader, "do_r"), config.r);
356 glUniform1i(glGetUniformLocation(frag_shader, "do_g"), config.g);
357 glUniform1i(glGetUniformLocation(frag_shader, "do_b"), config.b);
358 glUniform1i(glGetUniformLocation(frag_shader, "do_a"), config.a);
361 VFrame::init_screen(get_output()->get_w(), get_output()->get_h());
362 get_output()->bind_texture(0);
363 get_output()->draw_texture();
365 get_output()->set_opengl_state(VFrame::SCREEN);