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
31 WaveConfig::WaveConfig()
37 void WaveConfig::reset(int clear)
47 case RESET_AMPLITUDE : amplitude = 0;
49 case RESET_PHASE : phase = 0;
51 case RESET_WAVELENGTH : wavelength = 0;
53 case RESET_DEFAULT_SETTINGS :
64 void WaveConfig::copy_from(WaveConfig &src)
66 this->mode = src.mode;
67 this->reflective = src.reflective;
68 this->amplitude = src.amplitude;
69 this->phase = src.phase;
70 this->wavelength = src.wavelength;
73 int WaveConfig::equivalent(WaveConfig &src)
76 (this->mode == src.mode) &&
77 EQUIV(this->reflective, src.reflective) &&
78 EQUIV(this->amplitude, src.amplitude) &&
79 EQUIV(this->phase, src.phase) &&
80 EQUIV(this->wavelength, src.wavelength);
83 void WaveConfig::interpolate(WaveConfig &prev,
89 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
90 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
92 this->amplitude = prev.amplitude * prev_scale + next.amplitude * next_scale;
93 this->phase = prev.phase * prev_scale + next.phase * next_scale;
94 this->wavelength = prev.wavelength * prev_scale + next.wavelength * next_scale;
95 this->mode = prev.mode;
96 this->reflective = prev.reflective;
106 WaveSmear::WaveSmear(WaveEffect *plugin, WaveWindow *window, int x, int y)
107 : BC_Radial(x, y, plugin->config.mode == SMEAR, _("Smear"))
109 this->plugin = plugin;
110 this->window = window;
112 int WaveSmear::handle_event()
114 plugin->config.mode = SMEAR;
115 window->update_mode();
116 plugin->send_configure_change();
123 WaveBlacken::WaveBlacken(WaveEffect *plugin, WaveWindow *window, int x, int y)
124 : BC_Radial(x, y, plugin->config.mode == BLACKEN, _("Blacken"))
126 this->plugin = plugin;
127 this->window = window;
129 int WaveBlacken::handle_event()
131 plugin->config.mode = BLACKEN;
132 window->update_mode();
133 plugin->send_configure_change();
142 WaveReflective::WaveReflective(WaveEffect *plugin, int x, int y)
143 : BC_CheckBox(x, y, plugin->config.reflective, _("Reflective"))
145 this->plugin = plugin;
147 int WaveReflective::handle_event()
149 plugin->config.reflective = get_value();
150 plugin->send_configure_change();
155 WaveAmplitude::WaveAmplitude(WaveEffect *plugin, int x, int y)
163 plugin->config.amplitude)
165 this->plugin = plugin;
167 int WaveAmplitude::handle_event()
169 plugin->config.amplitude = get_value();
170 plugin->send_configure_change();
176 WavePhase::WavePhase(WaveEffect *plugin, int x, int y)
184 plugin->config.phase)
186 this->plugin = plugin;
188 int WavePhase::handle_event()
190 plugin->config.phase = get_value();
191 plugin->send_configure_change();
195 WaveLength::WaveLength(WaveEffect *plugin, int x, int y)
203 plugin->config.wavelength)
205 this->plugin = plugin;
207 int WaveLength::handle_event()
209 plugin->config.wavelength = get_value();
210 plugin->send_configure_change();
214 WaveReset::WaveReset(WaveEffect *plugin, WaveWindow *gui, int x, int y)
215 : BC_GenericButton(x, y, _("Reset"))
217 this->plugin = plugin;
220 WaveReset::~WaveReset()
223 int WaveReset::handle_event()
225 plugin->config.reset(RESET_ALL);
226 gui->update_gui(RESET_ALL);
227 plugin->send_configure_change();
231 WaveDefaultSettings::WaveDefaultSettings(WaveEffect *plugin, WaveWindow *gui, int x, int y, int w)
232 : BC_GenericButton(x, y, w, _("Default"))
234 this->plugin = plugin;
237 WaveDefaultSettings::~WaveDefaultSettings()
240 int WaveDefaultSettings::handle_event()
242 plugin->config.reset(RESET_DEFAULT_SETTINGS);
243 gui->update_gui(RESET_DEFAULT_SETTINGS);
244 plugin->send_configure_change();
248 WaveSliderClr::WaveSliderClr(WaveEffect *plugin, WaveWindow *gui, int x, int y, int w, int clear)
249 : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
251 this->plugin = plugin;
255 WaveSliderClr::~WaveSliderClr()
258 int WaveSliderClr::handle_event()
260 // clear==1 ==> Amplitude slider
261 // clear==2 ==> Phase slider
262 // clear==3 ==> Steps slider
263 plugin->config.reset(clear);
264 gui->update_gui(clear);
265 plugin->send_configure_change();
272 WaveWindow::WaveWindow(WaveEffect *plugin)
273 : PluginClientWindow(plugin, xS(385), yS(140), xS(385), yS(140), 0)
275 this->plugin = plugin;
278 WaveWindow::~WaveWindow()
282 void WaveWindow::create_objects()
284 int xs10 = xS(10), xs50 = xS(50), xs100 = xS(100), xs115 = xS(115);
285 int ys10 = yS(10), ys30 = yS(30), ys40 = yS(40);
286 int x = xs10, y = ys10, x1 = xs115;
287 int x2 = 0; int clrBtn_w = xs50;
288 int defaultBtn_w = xs100;
290 // add_subwindow(new BC_Title(x, y, _("Mode:")));
291 // add_subwindow(smear = new WaveSmear(plugin, this, x1, y));
293 // add_subwindow(blacken = new WaveBlacken(plugin, this, x1, y));
295 // add_subwindow(reflective = new WaveReflective(plugin, x1, y));
297 add_subwindow(new BC_Title(x, y, _("Amplitude:")));
298 add_subwindow(amplitude = new WaveAmplitude(plugin, x1, y));
299 x2 = x1 + amplitude->get_w() + xs10;
300 add_subwindow(amplitudeClr = new WaveSliderClr(plugin, this, x2, y, clrBtn_w, RESET_AMPLITUDE));
303 add_subwindow(new BC_Title(x, y, _("Phase:")));
304 add_subwindow(phase = new WavePhase(plugin, x1, y));
305 add_subwindow(phaseClr = new WaveSliderClr(plugin, this, x2, y, clrBtn_w, RESET_PHASE));
308 add_subwindow(new BC_Title(x, y, _("Wavelength:")));
309 add_subwindow(wavelength = new WaveLength(plugin, x1, y));
310 add_subwindow(wavelengthClr = new WaveSliderClr(plugin, this, x2, y, clrBtn_w, RESET_WAVELENGTH));
313 add_subwindow(reset = new WaveReset(plugin, this, x, y));
314 add_subwindow(default_settings = new WaveDefaultSettings(plugin, this,
315 (xS(385) - xs10 - defaultBtn_w), y, defaultBtn_w));
321 void WaveWindow::update_mode()
323 // smear->update(plugin->config.mode == SMEAR);
324 // blacken->update(plugin->config.mode == BLACKEN);
328 void WaveWindow::update_gui(int clear)
331 case RESET_AMPLITUDE : amplitude->update(plugin->config.amplitude);
333 case RESET_PHASE : phase->update(plugin->config.phase);
335 case RESET_WAVELENGTH : wavelength->update(plugin->config.wavelength);
338 case RESET_DEFAULT_SETTINGS :
340 amplitude->update(plugin->config.amplitude);
341 phase->update(plugin->config.phase);
342 wavelength->update(plugin->config.wavelength);
355 REGISTER_PLUGIN(WaveEffect)
360 WaveEffect::WaveEffect(PluginServer *server)
361 : PluginVClient(server)
368 WaveEffect::~WaveEffect()
372 if(temp_frame) delete temp_frame;
373 if(engine) delete engine;
377 const char* WaveEffect::plugin_title() { return N_("Wave"); }
378 int WaveEffect::is_realtime() { return 1; }
381 NEW_WINDOW_MACRO(WaveEffect, WaveWindow)
383 void WaveEffect::update_gui()
387 thread->window->lock_window();
388 load_configuration();
389 ((WaveWindow*)thread->window)->update_mode();
390 // thread->window->reflective->update(config.reflective);
391 ((WaveWindow*)thread->window)->amplitude->update(config.amplitude);
392 ((WaveWindow*)thread->window)->phase->update(config.phase);
393 ((WaveWindow*)thread->window)->wavelength->update(config.wavelength);
394 thread->window->unlock_window();
399 LOAD_CONFIGURATION_MACRO(WaveEffect, WaveConfig)
402 void WaveEffect::save_data(KeyFrame *keyframe)
406 // cause data to be stored directly in text
407 output.set_shared_output(keyframe->xbuf);
408 output.tag.set_title("WAVE");
409 output.tag.set_property("MODE", config.mode);
410 output.tag.set_property("REFLECTIVE", config.reflective);
411 output.tag.set_property("AMPLITUDE", config.amplitude);
412 output.tag.set_property("PHASE", config.phase);
413 output.tag.set_property("WAVELENGTH", config.wavelength);
415 output.tag.set_title("/WAVE");
417 output.append_newline();
418 output.terminate_string();
421 void WaveEffect::read_data(KeyFrame *keyframe)
425 input.set_shared_input(keyframe->xbuf);
427 while(!input.read_tag())
429 if(input.tag.title_is("WAVE"))
431 config.mode = input.tag.get_property("MODE", config.mode);
432 config.reflective = input.tag.get_property("REFLECTIVE", config.reflective);
433 config.amplitude = input.tag.get_property("AMPLITUDE", config.amplitude);
434 config.phase = input.tag.get_property("PHASE", config.phase);
435 config.wavelength = input.tag.get_property("WAVELENGTH", config.wavelength);
441 int WaveEffect::process_realtime(VFrame *input, VFrame *output)
443 load_configuration();
447 //printf("WaveEffect::process_realtime %f %d\n", config.radius, config.use_intensity);
449 this->output = output;
451 if(EQUIV(config.amplitude, 0) || EQUIV(config.wavelength, 0))
453 if(input->get_rows()[0] != output->get_rows()[0])
454 output->copy_from(input);
458 if(input->get_rows()[0] == output->get_rows()[0])
460 if(!temp_frame) temp_frame = new VFrame(input->get_w(), input->get_h(),
461 input->get_color_model(), 0);
462 temp_frame->copy_from(input);
463 this->input = temp_frame;
469 engine = new WaveServer(this, (PluginClient::smp + 1));
472 engine->process_packages();
485 WavePackage::WavePackage()
495 WaveUnit::WaveUnit(WaveEffect *plugin, WaveServer *server)
498 this->plugin = plugin;
503 #define WITHIN(a, b, c) ((((a) <= (b)) && ((b) <= (c))) ? 1 : 0)
505 static float bilinear(double x,
518 m0 = (1.0 - x) * v[0] + x * v[1];
519 m1 = (1.0 - x) * v[2] + x * v[3];
521 return((1.0 - y) * m0 + y * m1);
524 void WaveUnit::process_package(LoadPackage *package)
526 WavePackage *pkg = (WavePackage*)package;
527 int w = plugin->input->get_w();
528 int h = plugin->input->get_h();
529 double cen_x, cen_y; /* Center of wave */
530 double xhsiz, yhsiz; /* Half size of selection */
531 double radius; /* Radius */
535 double xscale, yscale;
541 int x1_in, y1_in, x2_in, y2_in;
542 double phase = plugin->config.phase * M_PI / 180;
548 cen_x = (double) (x2 - 1 + x1) / 2.0;
549 cen_y = (double) (y2 - 1 + y1) / 2.0;
550 xhsiz = (double) (x2 - x1) / 2.0;
551 yhsiz = (double) (y2 - y1) / 2.0;
555 xscale = yhsiz / xhsiz;
558 else if (xhsiz > yhsiz)
561 yscale = xhsiz / yhsiz;
569 radius = MAX(xhsiz, yhsiz);
572 wavelength = plugin->config.wavelength / 100 * radius;
577 #define WAVE(type, components, chroma_offset) \
579 int row_size = w * components; \
580 type **in_rows = (type**)plugin->input->get_rows(); \
581 for(int y = pkg->row1; y < pkg->row2; y++) \
583 type *dest = (type*)plugin->output->get_rows()[y]; \
585 for(int x = x1; x < x2; x++) \
587 dx = (x - cen_x) * xscale; \
588 dy = (y - cen_y) * yscale; \
589 d = sqrt(dx * dx + dy * dy); \
591 if(plugin->config.reflective) \
593 amnt = plugin->config.amplitude * \
594 fabs(sin(((d / wavelength) * \
598 needx = (amnt * dx) / xscale + cen_x; \
599 needy = (amnt * dy) / yscale + cen_y; \
603 amnt = plugin->config.amplitude * \
604 sin(((d / wavelength) * \
608 needx = (amnt + dx) / xscale + cen_x; \
609 needy = (amnt + dy) / yscale + cen_y; \
615 if(plugin->config.mode == SMEAR) \
638 type *p = in_rows[CLIP(yi, 0, h - 1)] + \
639 CLIP(xi, 0, w - 1) * components; \
640 x1_in = WITHIN(0, xi, w - 1); \
641 y1_in = WITHIN(0, yi, h - 1); \
642 x2_in = WITHIN(0, xi + 1, w - 1); \
643 y2_in = WITHIN(0, yi + 1, h - 1); \
646 for(int k = 0; k < components; k++) \
648 if (x1_in && y1_in) \
649 values[0] = *(p + k); \
651 values[0] = ((k == 1 || k == 2) ? 0 : chroma_offset); \
653 if (x2_in && y1_in) \
654 values[1] = *(p + components + k); \
656 values[1] = ((k == 1 || k == 2) ? 0 : chroma_offset); \
658 if (x1_in && y2_in) \
659 values[2] = *(p + row_size + k); \
661 values[2] = ((k == 1 || k == 2) ? 0 : chroma_offset); \
666 values[3] = *(p + row_size + components + k); \
668 values[3] = ((k == 1 || k == 2) ? 0 : chroma_offset); \
671 values[3] = ((k == 1 || k == 2) ? 0 : chroma_offset); \
673 val = bilinear(needx, needy, values); \
675 *dest++ = (type)val; \
684 switch(plugin->input->get_color_model())
687 WAVE(unsigned char, 3, 0x0);
693 WAVE(unsigned char, 3, 0x80);
696 WAVE(uint16_t, 3, 0x0);
699 WAVE(uint16_t, 3, 0x8000);
702 WAVE(unsigned char, 4, 0x0);
705 WAVE(unsigned char, 4, 0x0);
708 WAVE(unsigned char, 4, 0x8000);
710 case BC_RGBA16161616:
711 WAVE(uint16_t, 4, 0x0);
713 case BC_YUVA16161616:
714 WAVE(uint16_t, 4, 0x8000);
728 WaveServer::WaveServer(WaveEffect *plugin, int cpus)
729 : LoadServer(cpus, cpus)
731 this->plugin = plugin;
734 void WaveServer::init_packages()
736 for(int i = 0; i < LoadServer::get_total_packages(); i++)
738 WavePackage *pkg = (WavePackage*)get_package(i);
739 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
740 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
745 LoadClient* WaveServer::new_client()
747 return new WaveUnit(plugin, this);
750 LoadPackage* WaveServer::new_package()
752 return new WavePackage;