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.
54 void copy_from(OilConfig &src);
55 int equivalent(OilConfig &src);
56 void interpolate(OilConfig &prev,
65 class OilRadius : public BC_FSlider
68 OilRadius(OilEffect *plugin, int x, int y);
74 class OilIntensity : public BC_CheckBox
77 OilIntensity(OilEffect *plugin, int x, int y);
82 class OilWindow : public PluginClientWindow
85 OilWindow(OilEffect *plugin);
87 void create_objects();
91 OilIntensity *intensity;
100 class OilServer : public LoadServer
103 OilServer(OilEffect *plugin, int cpus);
104 void init_packages();
105 LoadClient* new_client();
106 LoadPackage* new_package();
110 class OilPackage : public LoadPackage
117 class OilUnit : public LoadClient
120 OilUnit(OilEffect *plugin, OilServer *server);
121 void process_package(LoadPackage *package);
133 class OilEffect : public PluginVClient
136 OilEffect(PluginServer *server);
139 PLUGIN_CLASS_MEMBERS(OilConfig);
140 int process_realtime(VFrame *input, VFrame *output);
142 void save_data(KeyFrame *keyframe);
143 void read_data(KeyFrame *keyframe);
147 VFrame *input, *output;
149 int need_reconfigure;
164 OilConfig::OilConfig()
170 void OilConfig::copy_from(OilConfig &src)
172 this->radius = src.radius;
173 this->use_intensity = src.use_intensity;
176 int OilConfig::equivalent(OilConfig &src)
178 return (EQUIV(this->radius, src.radius) &&
179 this->use_intensity == src.use_intensity);
182 void OilConfig::interpolate(OilConfig &prev,
188 double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
189 double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
190 this->radius = prev.radius * prev_scale + next.radius * next_scale;
191 this->use_intensity = prev.use_intensity;
192 // printf("OilConfig::interpolate prev_frame=%ld current_frame=%ld next_frame=%ld prev.radius=%f this->radius=%f next.radius=%f\n",
193 // prev_frame, current_frame, next_frame, prev.radius, this->radius, next.radius);
207 OilRadius::OilRadius(OilEffect *plugin, int x, int y)
215 plugin->config.radius)
217 this->plugin = plugin;
219 int OilRadius::handle_event()
221 plugin->config.radius = get_value();
222 plugin->send_configure_change();
232 OilIntensity::OilIntensity(OilEffect *plugin, int x, int y)
233 : BC_CheckBox(x, y, plugin->config.use_intensity, _("Use intensity"))
235 this->plugin = plugin;
237 int OilIntensity::handle_event()
239 plugin->config.use_intensity = get_value();
240 plugin->send_configure_change();
250 OilWindow::OilWindow(OilEffect *plugin)
251 : PluginClientWindow(plugin,
258 this->plugin = plugin;
261 OilWindow::~OilWindow()
265 void OilWindow::create_objects()
268 add_subwindow(new BC_Title(x, y, _("Radius:")));
269 add_subwindow(radius = new OilRadius(plugin, x + 70, y));
271 add_subwindow(intensity = new OilIntensity(plugin, x, y));
286 REGISTER_PLUGIN(OilEffect)
291 OilEffect::OilEffect(PluginServer *server)
292 : PluginVClient(server)
295 need_reconfigure = 1;
300 OilEffect::~OilEffect()
304 if(temp_frame) delete temp_frame;
305 if(engine) delete engine;
309 const char* OilEffect::plugin_title() { return N_("Oil painting"); }
310 int OilEffect::is_realtime() { return 1; }
314 NEW_WINDOW_MACRO(OilEffect, OilWindow)
316 void OilEffect::update_gui()
320 thread->window->lock_window();
321 load_configuration();
322 //printf("OilEffect::update_gui 1 %ld %f\n", get_source_position(), config.radius);
324 ((OilWindow*)thread->window)->radius->update(config.radius);
325 ((OilWindow*)thread->window)->intensity->update(config.use_intensity);
326 thread->window->unlock_window();
331 LOAD_CONFIGURATION_MACRO(OilEffect, OilConfig)
334 void OilEffect::save_data(KeyFrame *keyframe)
338 // cause data to be stored directly in text
339 output.set_shared_output(keyframe->xbuf);
340 output.tag.set_title("OIL_PAINTING");
341 output.tag.set_property("RADIUS", config.radius);
342 output.tag.set_property("USE_INTENSITY", config.use_intensity);
344 output.tag.set_title("/OIL_PAINTING");
346 output.append_newline();
347 output.terminate_string();
350 void OilEffect::read_data(KeyFrame *keyframe)
354 input.set_shared_input(keyframe->xbuf);
356 while(!input.read_tag())
358 if(input.tag.title_is("OIL_PAINTING"))
360 config.radius = input.tag.get_property("RADIUS", config.radius);
361 config.use_intensity = input.tag.get_property("USE_INTENSITY", config.use_intensity);
367 int OilEffect::process_realtime(VFrame *input, VFrame *output)
369 need_reconfigure |= load_configuration();
373 //printf("OilEffect::process_realtime %f %d\n", config.radius, config.use_intensity);
375 this->output = output;
377 if(EQUIV(config.radius, 0))
379 if(input->get_rows()[0] != output->get_rows()[0])
380 output->copy_from(input);
384 if(input->get_rows()[0] == output->get_rows()[0])
387 temp_frame = new VFrame(input->get_w(), input->get_h(),
388 input->get_color_model(), 0);
389 temp_frame->copy_from(input);
390 this->input = temp_frame;
396 engine = new OilServer(this, (PluginClient::smp + 1));
399 engine->process_packages();
412 OilPackage::OilPackage()
422 OilUnit::OilUnit(OilEffect *plugin, OilServer *server)
425 this->plugin = plugin;
429 #define INTENSITY(p) ((unsigned int)(((p)[0]) * 77+ \
434 #define OIL_MACRO(type, hist_size, components) \
437 type val[components]; \
438 int count[components], count2; \
439 int *hist[components]; \
441 type **src_rows = (type**)plugin->input->get_rows(); \
443 for(int i = 0; i < components; i++) \
444 hist[i] = new int[hist_size + 1]; \
445 hist2 = new int[hist_size + 1]; \
447 for(int y1 = pkg->row1; y1 < pkg->row2; y1++) \
449 dest = (type*)plugin->output->get_rows()[y1]; \
451 if(!plugin->config.use_intensity) \
453 for(int x1 = 0; x1 < w; x1++) \
455 bzero(count, sizeof(count)); \
456 bzero(val, sizeof(val)); \
457 bzero(hist[0], sizeof(int) * (hist_size + 1)); \
458 bzero(hist[1], sizeof(int) * (hist_size + 1)); \
459 bzero(hist[2], sizeof(int) * (hist_size + 1)); \
460 if (components == 4) bzero(hist[3], sizeof(int) * (hist_size + 1)); \
462 int x3 = CLIP((x1 - n), 0, w - 1); \
463 int y3 = CLIP((y1 - n), 0, h - 1); \
464 int x4 = CLIP((x1 + n + 1), 0, w - 1); \
465 int y4 = CLIP((y1 + n + 1), 0, h - 1); \
467 for(int y2 = y3; y2 < y4; y2++) \
469 src = src_rows[y2]; \
470 for(int x2 = x3; x2 < x4; x2++) \
476 value = src[x2 * components + 0]; \
477 if(sizeof(type) == 4) \
479 subscript = (int)(value * hist_size); \
480 CLAMP(subscript, 0, hist_size); \
483 subscript = (int)value; \
485 if((c = ++hist[0][subscript]) > count[0]) \
491 value = src[x2 * components + 1]; \
492 if(sizeof(type) == 4) \
494 subscript = (int)(value * hist_size); \
495 CLAMP(subscript, 0, hist_size); \
498 subscript = (int)value; \
500 if((c = ++hist[1][subscript]) > count[1]) \
506 value = src[x2 * components + 2]; \
507 if(sizeof(type) == 4) \
509 subscript = (int)(value * hist_size); \
510 CLAMP(subscript, 0, hist_size); \
513 subscript = (int)value; \
515 if((c = ++hist[2][subscript]) > count[2]) \
521 if(components == 4) \
523 value = src[x2 * components + 3]; \
524 if(sizeof(type) == 4) \
526 subscript = (int)(value * hist_size); \
527 CLAMP(subscript, 0, hist_size); \
530 subscript = (int)value; \
532 if((c = ++hist[3][subscript]) > count[3]) \
541 dest[x1 * components + 0] = val[0]; \
542 dest[x1 * components + 1] = val[1]; \
543 dest[x1 * components + 2] = val[2]; \
544 if(components == 4) dest[x1 * components + 3] = val[3]; \
549 for(int x1 = 0; x1 < w; x1++) \
552 bzero(val, sizeof(val)); \
553 bzero(hist2, sizeof(int) * (hist_size + 1)); \
555 int x3 = CLIP((x1 - n), 0, w - 1); \
556 int y3 = CLIP((y1 - n), 0, h - 1); \
557 int x4 = CLIP((x1 + n + 1), 0, w - 1); \
558 int y4 = CLIP((y1 + n + 1), 0, h - 1); \
560 for(int y2 = y3; y2 < y4; y2++) \
562 src = src_rows[y2]; \
563 for(int x2 = x3; x2 < x4; x2++) \
566 if((c = ++hist2[INTENSITY(src + x2 * components)]) > count2) \
568 val[0] = src[x2 * components + 0]; \
569 val[1] = src[x2 * components + 1]; \
570 val[2] = src[x2 * components + 2]; \
571 if(components == 4) val[3] = src[x2 * components + 3]; \
577 dest[x1 * components + 0] = val[0]; \
578 dest[x1 * components + 1] = val[1]; \
579 dest[x1 * components + 2] = val[2]; \
580 if(components == 4) dest[x1 * components + 3] = val[3]; \
585 for(int i = 0; i < components; i++) \
593 void OilUnit::process_package(LoadPackage *package)
595 OilPackage *pkg = (OilPackage*)package;
596 int w = plugin->input->get_w();
597 int h = plugin->input->get_h();
598 int n = (int)(plugin->config.radius / 2);
600 switch(plugin->input->get_color_model())
603 OIL_MACRO(float, 0xffff, 3)
607 OIL_MACRO(unsigned char, 0xff, 3)
611 OIL_MACRO(uint16_t, 0xffff, 3)
614 OIL_MACRO(float, 0xffff, 4)
618 OIL_MACRO(unsigned char, 0xff, 4)
620 case BC_RGBA16161616:
621 case BC_YUVA16161616:
622 OIL_MACRO(uint16_t, 0xffff, 4)
636 OilServer::OilServer(OilEffect *plugin, int cpus)
637 : LoadServer(cpus, cpus)
639 this->plugin = plugin;
642 void OilServer::init_packages()
644 for(int i = 0; i < LoadServer::get_total_packages(); i++)
646 OilPackage *pkg = (OilPackage*)get_package(i);
647 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
648 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
653 LoadClient* OilServer::new_client()
655 return new OilUnit(plugin, this);
658 LoadPackage* OilServer::new_package()
660 return new OilPackage;