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 _("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->get_data(), MESSAGESIZE);
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->get_data(), strlen(keyframe->get_data()));
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])
386 if(!temp_frame) temp_frame = new VFrame(0,
390 input->get_color_model(),
392 temp_frame->copy_from(input);
393 this->input = temp_frame;
399 engine = new OilServer(this, (PluginClient::smp + 1));
402 engine->process_packages();
415 OilPackage::OilPackage()
425 OilUnit::OilUnit(OilEffect *plugin, OilServer *server)
428 this->plugin = plugin;
432 #define INTENSITY(p) ((unsigned int)(((p)[0]) * 77+ \
437 #define OIL_MACRO(type, hist_size, components) \
440 type val[components]; \
441 int count[components], count2; \
442 int *hist[components]; \
444 type **src_rows = (type**)plugin->input->get_rows(); \
446 for(int i = 0; i < components; i++) \
447 hist[i] = new int[hist_size + 1]; \
448 hist2 = new int[hist_size + 1]; \
450 for(int y1 = pkg->row1; y1 < pkg->row2; y1++) \
452 dest = (type*)plugin->output->get_rows()[y1]; \
454 if(!plugin->config.use_intensity) \
456 for(int x1 = 0; x1 < w; x1++) \
458 bzero(count, sizeof(count)); \
459 bzero(val, sizeof(val)); \
460 bzero(hist[0], sizeof(int) * (hist_size + 1)); \
461 bzero(hist[1], sizeof(int) * (hist_size + 1)); \
462 bzero(hist[2], sizeof(int) * (hist_size + 1)); \
463 if (components == 4) bzero(hist[3], sizeof(int) * (hist_size + 1)); \
465 int x3 = CLIP((x1 - n), 0, w - 1); \
466 int y3 = CLIP((y1 - n), 0, h - 1); \
467 int x4 = CLIP((x1 + n + 1), 0, w - 1); \
468 int y4 = CLIP((y1 + n + 1), 0, h - 1); \
470 for(int y2 = y3; y2 < y4; y2++) \
472 src = src_rows[y2]; \
473 for(int x2 = x3; x2 < x4; x2++) \
479 value = src[x2 * components + 0]; \
480 if(sizeof(type) == 4) \
482 subscript = (int)(value * hist_size); \
483 CLAMP(subscript, 0, hist_size); \
486 subscript = (int)value; \
488 if((c = ++hist[0][subscript]) > count[0]) \
494 value = src[x2 * components + 1]; \
495 if(sizeof(type) == 4) \
497 subscript = (int)(value * hist_size); \
498 CLAMP(subscript, 0, hist_size); \
501 subscript = (int)value; \
503 if((c = ++hist[1][subscript]) > count[1]) \
509 value = src[x2 * components + 2]; \
510 if(sizeof(type) == 4) \
512 subscript = (int)(value * hist_size); \
513 CLAMP(subscript, 0, hist_size); \
516 subscript = (int)value; \
518 if((c = ++hist[2][subscript]) > count[2]) \
524 if(components == 4) \
526 value = src[x2 * components + 3]; \
527 if(sizeof(type) == 4) \
529 subscript = (int)(value * hist_size); \
530 CLAMP(subscript, 0, hist_size); \
533 subscript = (int)value; \
535 if((c = ++hist[3][subscript]) > count[3]) \
544 dest[x1 * components + 0] = val[0]; \
545 dest[x1 * components + 1] = val[1]; \
546 dest[x1 * components + 2] = val[2]; \
547 if(components == 4) dest[x1 * components + 3] = val[3]; \
552 for(int x1 = 0; x1 < w; x1++) \
555 bzero(val, sizeof(val)); \
556 bzero(hist2, sizeof(int) * (hist_size + 1)); \
558 int x3 = CLIP((x1 - n), 0, w - 1); \
559 int y3 = CLIP((y1 - n), 0, h - 1); \
560 int x4 = CLIP((x1 + n + 1), 0, w - 1); \
561 int y4 = CLIP((y1 + n + 1), 0, h - 1); \
563 for(int y2 = y3; y2 < y4; y2++) \
565 src = src_rows[y2]; \
566 for(int x2 = x3; x2 < x4; x2++) \
569 if((c = ++hist2[INTENSITY(src + x2 * components)]) > count2) \
571 val[0] = src[x2 * components + 0]; \
572 val[1] = src[x2 * components + 1]; \
573 val[2] = src[x2 * components + 2]; \
574 if(components == 4) val[3] = src[x2 * components + 3]; \
580 dest[x1 * components + 0] = val[0]; \
581 dest[x1 * components + 1] = val[1]; \
582 dest[x1 * components + 2] = val[2]; \
583 if(components == 4) dest[x1 * components + 3] = val[3]; \
588 for(int i = 0; i < components; i++) \
596 void OilUnit::process_package(LoadPackage *package)
598 OilPackage *pkg = (OilPackage*)package;
599 int w = plugin->input->get_w();
600 int h = plugin->input->get_h();
601 int n = (int)(plugin->config.radius / 2);
603 switch(plugin->input->get_color_model())
606 OIL_MACRO(float, 0xffff, 3)
610 OIL_MACRO(unsigned char, 0xff, 3)
614 OIL_MACRO(uint16_t, 0xffff, 3)
617 OIL_MACRO(float, 0xffff, 4)
621 OIL_MACRO(unsigned char, 0xff, 4)
623 case BC_RGBA16161616:
624 case BC_YUVA16161616:
625 OIL_MACRO(uint16_t, 0xffff, 4)
639 OilServer::OilServer(OilEffect *plugin, int cpus)
640 : LoadServer(cpus, cpus)
642 this->plugin = plugin;
645 void OilServer::init_packages()
647 for(int i = 0; i < LoadServer::get_total_packages(); i++)
649 OilPackage *pkg = (OilPackage*)get_package(i);
650 pkg->row1 = plugin->input->get_h() * i / LoadServer::get_total_packages();
651 pkg->row2 = plugin->input->get_h() * (i + 1) / LoadServer::get_total_packages();
656 LoadClient* OilServer::new_client()
658 return new OilUnit(plugin, this);
661 LoadPackage* OilServer::new_package()
663 return new OilPackage;