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 "loadbalance.h"
30 #include "pluginvclient.h"
40 // Algorithm by Torsten Martinsen
41 // Ported to Cinelerra by Heroine Virtual Ltd.
57 void copy_from(OilConfig &src);
58 int equivalent(OilConfig &src);
59 void interpolate(OilConfig &prev,
68 class OilRadius : public BC_FSlider
71 OilRadius(OilEffect *plugin, int x, int y);
77 class OilIntensity : public BC_CheckBox
80 OilIntensity(OilEffect *plugin, int x, int y);
85 class OilReset : public BC_GenericButton
88 OilReset(OilEffect *plugin, OilWindow *window, int x, int y);
96 class OilWindow : public PluginClientWindow
99 OilWindow(OilEffect *plugin);
101 void create_objects();
105 OilIntensity *intensity;
115 class OilServer : public LoadServer
118 OilServer(OilEffect *plugin, int cpus);
119 void init_packages();
120 LoadClient* new_client();
121 LoadPackage* new_package();
125 class OilPackage : public LoadPackage
132 class OilUnit : public LoadClient
135 OilUnit(OilEffect *plugin, OilServer *server);
136 void process_package(LoadPackage *package);
148 class OilEffect : public PluginVClient
151 OilEffect(PluginServer *server);
154 PLUGIN_CLASS_MEMBERS(OilConfig);
155 int process_realtime(VFrame *input, VFrame *output);
157 void save_data(KeyFrame *keyframe);
158 void read_data(KeyFrame *keyframe);
162 VFrame *input, *output;
164 int need_reconfigure;
177 OilConfig::OilConfig()
182 void OilConfig::reset()
188 void OilConfig::copy_from(OilConfig &src)
190 this->radius = src.radius;
191 this->use_intensity = src.use_intensity;
194 int OilConfig::equivalent(OilConfig &src)
196 return (EQUIV(this->radius, src.radius) &&
197 this->use_intensity == src.use_intensity);
200 void OilConfig::interpolate(OilConfig &prev,
206 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
207 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
208 this->radius = prev.radius * prev_scale + next.radius * next_scale;
209 this->use_intensity = prev.use_intensity;
210 // printf("OilConfig::interpolate prev_frame=%ld current_frame=%ld next_frame=%ld prev.radius=%f this->radius=%f next.radius=%f\n",
211 // prev_frame, current_frame, next_frame, prev.radius, this->radius, next.radius);
225 OilRadius::OilRadius(OilEffect *plugin, int x, int y)
233 plugin->config.radius)
235 this->plugin = plugin;
237 int OilRadius::handle_event()
239 plugin->config.radius = get_value();
240 plugin->send_configure_change();
250 OilIntensity::OilIntensity(OilEffect *plugin, int x, int y)
251 : BC_CheckBox(x, y, plugin->config.use_intensity, _("Use intensity"))
253 this->plugin = plugin;
255 int OilIntensity::handle_event()
257 plugin->config.use_intensity = get_value();
258 plugin->send_configure_change();
263 OilReset::OilReset(OilEffect *plugin, OilWindow *window, int x, int y)
264 : BC_GenericButton(x, y, _("Reset"))
266 this->plugin = plugin;
267 this->window = window;
269 OilReset::~OilReset()
272 int OilReset::handle_event()
274 plugin->config.reset();
276 plugin->send_configure_change();
284 OilWindow::OilWindow(OilEffect *plugin)
285 : PluginClientWindow(plugin,
292 this->plugin = plugin;
295 OilWindow::~OilWindow()
299 void OilWindow::create_objects()
302 add_subwindow(new BC_Title(x, y, _("Radius:")));
303 add_subwindow(radius = new OilRadius(plugin, x + 70, y));
305 add_subwindow(intensity = new OilIntensity(plugin, x, y));
307 add_subwindow(reset = new OilReset(plugin, this, x, y));
315 void OilWindow::update()
317 radius->update(plugin->config.radius);
318 intensity->update(plugin->config.use_intensity);
327 REGISTER_PLUGIN(OilEffect)
332 OilEffect::OilEffect(PluginServer *server)
333 : PluginVClient(server)
336 need_reconfigure = 1;
341 OilEffect::~OilEffect()
345 if(temp_frame) delete temp_frame;
346 if(engine) delete engine;
350 const char* OilEffect::plugin_title() { return N_("Oil painting"); }
351 int OilEffect::is_realtime() { return 1; }
355 NEW_WINDOW_MACRO(OilEffect, OilWindow)
357 void OilEffect::update_gui()
361 thread->window->lock_window();
362 load_configuration();
363 //printf("OilEffect::update_gui 1 %ld %f\n", get_source_position(), config.radius);
365 ((OilWindow*)thread->window)->radius->update(config.radius);
366 ((OilWindow*)thread->window)->intensity->update(config.use_intensity);
367 thread->window->unlock_window();
372 LOAD_CONFIGURATION_MACRO(OilEffect, OilConfig)
375 void OilEffect::save_data(KeyFrame *keyframe)
379 // cause data to be stored directly in text
380 output.set_shared_output(keyframe->xbuf);
381 output.tag.set_title("OIL_PAINTING");
382 output.tag.set_property("RADIUS", config.radius);
383 output.tag.set_property("USE_INTENSITY", config.use_intensity);
385 output.tag.set_title("/OIL_PAINTING");
387 output.append_newline();
388 output.terminate_string();
391 void OilEffect::read_data(KeyFrame *keyframe)
395 input.set_shared_input(keyframe->xbuf);
397 while(!input.read_tag())
399 if(input.tag.title_is("OIL_PAINTING"))
401 config.radius = input.tag.get_property("RADIUS", config.radius);
402 config.use_intensity = input.tag.get_property("USE_INTENSITY", config.use_intensity);
408 int OilEffect::process_realtime(VFrame *input, VFrame *output)
410 need_reconfigure |= load_configuration();
414 //printf("OilEffect::process_realtime %f %d\n", config.radius, config.use_intensity);
416 this->output = output;
418 if(EQUIV(config.radius, 0))
420 if(input->get_rows()[0] != output->get_rows()[0])
421 output->copy_from(input);
425 if(input->get_rows()[0] == output->get_rows()[0])
428 temp_frame = new VFrame(input->get_w(), input->get_h(),
429 input->get_color_model(), 0);
430 temp_frame->copy_from(input);
431 this->input = temp_frame;
437 engine = new OilServer(this, (PluginClient::smp + 1));
440 engine->process_packages();
453 OilPackage::OilPackage()
463 OilUnit::OilUnit(OilEffect *plugin, OilServer *server)
466 this->plugin = plugin;
470 #define INTENSITY(p) ((unsigned int)(((p)[0]) * 77+ \
475 #define OIL_MACRO(type, hist_size, components) \
478 type val[components]; \
479 int count[components], count2; \
480 int *hist[components]; \
482 type **src_rows = (type**)plugin->input->get_rows(); \
484 for(int i = 0; i < components; i++) \
485 hist[i] = new int[hist_size + 1]; \
486 hist2 = new int[hist_size + 1]; \
488 for(int y1 = pkg->row1; y1 < pkg->row2; y1++) \
490 dest = (type*)plugin->output->get_rows()[y1]; \
492 if(!plugin->config.use_intensity) \
494 for(int x1 = 0; x1 < w; x1++) \
496 bzero(count, sizeof(count)); \
497 bzero(val, sizeof(val)); \
498 bzero(hist[0], sizeof(int) * (hist_size + 1)); \
499 bzero(hist[1], sizeof(int) * (hist_size + 1)); \
500 bzero(hist[2], sizeof(int) * (hist_size + 1)); \
501 if (components == 4) bzero(hist[3], sizeof(int) * (hist_size + 1)); \
503 int x3 = CLIP((x1 - n), 0, w - 1); \
504 int y3 = CLIP((y1 - n), 0, h - 1); \
505 int x4 = CLIP((x1 + n + 1), 0, w - 1); \
506 int y4 = CLIP((y1 + n + 1), 0, h - 1); \
508 for(int y2 = y3; y2 < y4; y2++) \
510 src = src_rows[y2]; \
511 for(int x2 = x3; x2 < x4; x2++) \
517 value = src[x2 * components + 0]; \
518 if(sizeof(type) == 4) \
520 subscript = (int)(value * hist_size); \
521 CLAMP(subscript, 0, hist_size); \
524 subscript = (int)value; \
526 if((c = ++hist[0][subscript]) > count[0]) \
532 value = src[x2 * components + 1]; \
533 if(sizeof(type) == 4) \
535 subscript = (int)(value * hist_size); \
536 CLAMP(subscript, 0, hist_size); \
539 subscript = (int)value; \
541 if((c = ++hist[1][subscript]) > count[1]) \
547 value = src[x2 * components + 2]; \
548 if(sizeof(type) == 4) \
550 subscript = (int)(value * hist_size); \
551 CLAMP(subscript, 0, hist_size); \
554 subscript = (int)value; \
556 if((c = ++hist[2][subscript]) > count[2]) \
562 if(components == 4) \
564 value = src[x2 * components + 3]; \
565 if(sizeof(type) == 4) \
567 subscript = (int)(value * hist_size); \
568 CLAMP(subscript, 0, hist_size); \
571 subscript = (int)value; \
573 if((c = ++hist[3][subscript]) > count[3]) \
582 dest[x1 * components + 0] = val[0]; \
583 dest[x1 * components + 1] = val[1]; \
584 dest[x1 * components + 2] = val[2]; \
585 if(components == 4) dest[x1 * components + 3] = val[3]; \
590 for(int x1 = 0; x1 < w; x1++) \
593 bzero(val, sizeof(val)); \
594 bzero(hist2, sizeof(int) * (hist_size + 1)); \
596 int x3 = CLIP((x1 - n), 0, w - 1); \
597 int y3 = CLIP((y1 - n), 0, h - 1); \
598 int x4 = CLIP((x1 + n + 1), 0, w - 1); \
599 int y4 = CLIP((y1 + n + 1), 0, h - 1); \
601 for(int y2 = y3; y2 < y4; y2++) \
603 src = src_rows[y2]; \
604 for(int x2 = x3; x2 < x4; x2++) \
607 if((c = ++hist2[INTENSITY(src + x2 * components)]) > count2) \
609 val[0] = src[x2 * components + 0]; \
610 val[1] = src[x2 * components + 1]; \
611 val[2] = src[x2 * components + 2]; \
612 if(components == 4) val[3] = src[x2 * components + 3]; \
618 dest[x1 * components + 0] = val[0]; \
619 dest[x1 * components + 1] = val[1]; \
620 dest[x1 * components + 2] = val[2]; \
621 if(components == 4) dest[x1 * components + 3] = val[3]; \
626 for(int i = 0; i < components; i++) \
634 void OilUnit::process_package(LoadPackage *package)
636 OilPackage *pkg = (OilPackage*)package;
637 int w = plugin->input->get_w();
638 int h = plugin->input->get_h();
639 int n = (int)(plugin->config.radius / 2);
641 switch(plugin->input->get_color_model())
644 OIL_MACRO(float, 0xffff, 3)
648 OIL_MACRO(unsigned char, 0xff, 3)
652 OIL_MACRO(uint16_t, 0xffff, 3)
655 OIL_MACRO(float, 0xffff, 4)
659 OIL_MACRO(unsigned char, 0xff, 4)
661 case BC_RGBA16161616:
662 case BC_YUVA16161616:
663 OIL_MACRO(uint16_t, 0xffff, 4)
677 OilServer::OilServer(OilEffect *plugin, int cpus)
678 : LoadServer(cpus, cpus)
680 this->plugin = plugin;
683 void OilServer::init_packages()
685 for(int i = 0; i < LoadServer::get_total_packages(); i++)
687 OilPackage *pkg = (OilPackage*)get_package(i);
688 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
689 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
694 LoadClient* OilServer::new_client()
696 return new OilUnit(plugin, this);
699 LoadPackage* OilServer::new_package()
701 return new OilPackage;