0b782ab5b02ac7c43231d71956b62f9bb9b0814c
[goodguy/history.git] / cinelerra-5.1 / plugins / oilpainting / oil.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "clip.h"
24 #include "bchash.h"
25 #include "filexml.h"
26 #include "guicast.h"
27 #include "keyframe.h"
28 #include "language.h"
29 #include "loadbalance.h"
30 #include "pluginvclient.h"
31 #include "vframe.h"
32
33 #include <math.h>
34 #include <stdint.h>
35 #include <string.h>
36
37
38
39
40 // Algorithm by Torsten Martinsen
41 // Ported to Cinelerra by Heroine Virtual Ltd.
42
43
44
45
46 class OilEffect;
47
48
49
50 class OilConfig
51 {
52 public:
53         OilConfig();
54         void copy_from(OilConfig &src);
55         int equivalent(OilConfig &src);
56         void interpolate(OilConfig &prev,
57                 OilConfig &next,
58                 long prev_frame,
59                 long next_frame,
60                 long current_frame);
61         float radius;
62         int use_intensity;
63 };
64
65 class OilRadius : public BC_FSlider
66 {
67 public:
68         OilRadius(OilEffect *plugin, int x, int y);
69         int handle_event();
70         OilEffect *plugin;
71 };
72
73
74 class OilIntensity : public BC_CheckBox
75 {
76 public:
77         OilIntensity(OilEffect *plugin, int x, int y);
78         int handle_event();
79         OilEffect *plugin;
80 };
81
82 class OilWindow : public PluginClientWindow
83 {
84 public:
85         OilWindow(OilEffect *plugin);
86         ~OilWindow();
87         void create_objects();
88
89         OilEffect *plugin;
90         OilRadius *radius;
91         OilIntensity *intensity;
92 };
93
94
95
96
97
98
99
100 class OilServer : public LoadServer
101 {
102 public:
103         OilServer(OilEffect *plugin, int cpus);
104         void init_packages();
105         LoadClient* new_client();
106         LoadPackage* new_package();
107         OilEffect *plugin;
108 };
109
110 class OilPackage : public LoadPackage
111 {
112 public:
113         OilPackage();
114         int row1, row2;
115 };
116
117 class OilUnit : public LoadClient
118 {
119 public:
120         OilUnit(OilEffect *plugin, OilServer *server);
121         void process_package(LoadPackage *package);
122         OilEffect *plugin;
123 };
124
125
126
127
128
129
130
131
132
133 class OilEffect : public PluginVClient
134 {
135 public:
136         OilEffect(PluginServer *server);
137         ~OilEffect();
138
139         PLUGIN_CLASS_MEMBERS(OilConfig);
140         int process_realtime(VFrame *input, VFrame *output);
141         int is_realtime();
142         void save_data(KeyFrame *keyframe);
143         void read_data(KeyFrame *keyframe);
144         void update_gui();
145
146         VFrame *temp_frame;
147         VFrame *input, *output;
148         OilServer *engine;
149         int need_reconfigure;
150 };
151
152
153
154
155
156
157
158
159
160
161
162
163
164 OilConfig::OilConfig()
165 {
166         radius = 5;
167         use_intensity = 0;
168 }
169
170 void OilConfig::copy_from(OilConfig &src)
171 {
172         this->radius = src.radius;
173         this->use_intensity = src.use_intensity;
174 }
175
176 int OilConfig::equivalent(OilConfig &src)
177 {
178         return (EQUIV(this->radius, src.radius) &&
179                 this->use_intensity == src.use_intensity);
180 }
181
182 void OilConfig::interpolate(OilConfig &prev,
183                 OilConfig &next,
184                 long prev_frame,
185                 long next_frame,
186                 long current_frame)
187 {
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);
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207 OilRadius::OilRadius(OilEffect *plugin, int x, int y)
208  : BC_FSlider(x,
209                 y,
210                 0,
211                 200,
212                 200,
213                 (float)0,
214                 (float)30,
215                 plugin->config.radius)
216 {
217         this->plugin = plugin;
218 }
219 int OilRadius::handle_event()
220 {
221         plugin->config.radius = get_value();
222         plugin->send_configure_change();
223         return 1;
224 }
225
226
227
228
229
230
231
232 OilIntensity::OilIntensity(OilEffect *plugin, int x, int y)
233  : BC_CheckBox(x, y, plugin->config.use_intensity, _("Use intensity"))
234 {
235         this->plugin = plugin;
236 }
237 int OilIntensity::handle_event()
238 {
239         plugin->config.use_intensity = get_value();
240         plugin->send_configure_change();
241         return 1;
242 }
243
244
245
246
247
248
249
250 OilWindow::OilWindow(OilEffect *plugin)
251  : PluginClientWindow(plugin,
252         300,
253         160,
254         300,
255         160,
256         0)
257 {
258         this->plugin = plugin;
259 }
260
261 OilWindow::~OilWindow()
262 {
263 }
264
265 void OilWindow::create_objects()
266 {
267         int x = 10, y = 10;
268         add_subwindow(new BC_Title(x, y, _("Radius:")));
269         add_subwindow(radius = new OilRadius(plugin, x + 70, y));
270         y += 40;
271         add_subwindow(intensity = new OilIntensity(plugin, x, y));
272
273         show_window();
274         flush();
275 }
276
277
278
279
280
281
282
283
284
285
286 REGISTER_PLUGIN(OilEffect)
287
288
289
290
291 OilEffect::OilEffect(PluginServer *server)
292  : PluginVClient(server)
293 {
294         temp_frame = 0;
295         need_reconfigure = 1;
296         engine = 0;
297
298 }
299
300 OilEffect::~OilEffect()
301 {
302
303
304         if(temp_frame) delete temp_frame;
305         if(engine) delete engine;
306 }
307
308
309 const char* OilEffect::plugin_title() { return _("Oil painting"); }
310 int OilEffect::is_realtime() { return 1; }
311
312
313
314 NEW_WINDOW_MACRO(OilEffect, OilWindow)
315
316 void OilEffect::update_gui()
317 {
318         if(thread)
319         {
320                 thread->window->lock_window();
321                 load_configuration();
322 //printf("OilEffect::update_gui 1 %ld %f\n", get_source_position(), config.radius);
323
324                 ((OilWindow*)thread->window)->radius->update(config.radius);
325                 ((OilWindow*)thread->window)->intensity->update(config.use_intensity);
326                 thread->window->unlock_window();
327         }
328 }
329
330
331 LOAD_CONFIGURATION_MACRO(OilEffect, OilConfig)
332
333
334 void OilEffect::save_data(KeyFrame *keyframe)
335 {
336         FileXML output;
337
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);
343         output.append_tag();
344         output.tag.set_title("/OIL_PAINTING");
345         output.append_tag();
346         output.append_newline();
347         output.terminate_string();
348 }
349
350 void OilEffect::read_data(KeyFrame *keyframe)
351 {
352         FileXML input;
353
354         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
355
356         while(!input.read_tag())
357         {
358                 if(input.tag.title_is("OIL_PAINTING"))
359                 {
360                         config.radius = input.tag.get_property("RADIUS", config.radius);
361                         config.use_intensity = input.tag.get_property("USE_INTENSITY", config.use_intensity);
362                 }
363         }
364 }
365
366
367 int OilEffect::process_realtime(VFrame *input, VFrame *output)
368 {
369         need_reconfigure |= load_configuration();
370
371
372
373 //printf("OilEffect::process_realtime %f %d\n", config.radius, config.use_intensity);
374         this->input = input;
375         this->output = output;
376
377         if(EQUIV(config.radius, 0))
378         {
379                 if(input->get_rows()[0] != output->get_rows()[0])
380                         output->copy_from(input);
381         }
382         else
383         {
384                 if(input->get_rows()[0] == output->get_rows()[0])
385                 {
386                         if(!temp_frame)
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;
391                 }
392
393
394                 if(!engine)
395                 {
396                         engine = new OilServer(this, (PluginClient::smp + 1));
397                 }
398
399                 engine->process_packages();
400         }
401
402
403
404         return 0;
405 }
406
407
408
409
410
411
412 OilPackage::OilPackage()
413  : LoadPackage()
414 {
415 }
416
417
418
419
420
421
422 OilUnit::OilUnit(OilEffect *plugin, OilServer *server)
423  : LoadClient(server)
424 {
425         this->plugin = plugin;
426 }
427
428
429 #define INTENSITY(p) ((unsigned int)(((p)[0]) * 77+ \
430                                                                         ((p)[1] * 150) + \
431                                                                         ((p)[2] * 29)) >> 8)
432
433
434 #define OIL_MACRO(type, hist_size, components) \
435 { \
436         type *src, *dest; \
437         type val[components]; \
438         int count[components], count2; \
439         int *hist[components]; \
440         int *hist2; \
441         type **src_rows = (type**)plugin->input->get_rows(); \
442  \
443         for(int i = 0; i < components; i++) \
444                 hist[i] = new int[hist_size + 1]; \
445         hist2 = new int[hist_size + 1]; \
446  \
447         for(int y1 = pkg->row1; y1 < pkg->row2; y1++) \
448         { \
449                 dest = (type*)plugin->output->get_rows()[y1]; \
450  \
451                 if(!plugin->config.use_intensity) \
452                 { \
453                         for(int x1 = 0; x1 < w; x1++) \
454                         { \
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)); \
461  \
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); \
466  \
467                                 for(int y2 = y3; y2 < y4; y2++) \
468                                 { \
469                                         src = src_rows[y2]; \
470                                         for(int x2 = x3; x2 < x4; x2++) \
471                                         { \
472                                                 int c; \
473                                                 int subscript; \
474                                                 type value; \
475  \
476                         value = src[x2 * components + 0]; \
477                                                 if(sizeof(type) == 4) \
478                                                 { \
479                                                         subscript = (int)(value * hist_size); \
480                                                         CLAMP(subscript, 0, hist_size); \
481                                                 } \
482                                                 else \
483                                                         subscript = (int)value; \
484  \
485                                                 if((c = ++hist[0][subscript]) > count[0]) \
486                                                 { \
487                                                         val[0] = value; \
488                                                         count[0] = c; \
489                                                 } \
490  \
491                         value = src[x2 * components + 1]; \
492                                                 if(sizeof(type) == 4) \
493                                                 { \
494                                                         subscript = (int)(value * hist_size); \
495                                                         CLAMP(subscript, 0, hist_size); \
496                                                 } \
497                                                 else \
498                                                         subscript = (int)value; \
499  \
500                                                 if((c = ++hist[1][subscript]) > count[1]) \
501                                                 { \
502                                                         val[1] = value; \
503                                                         count[1] = c; \
504                                                 } \
505  \
506                         value = src[x2 * components + 2]; \
507                                                 if(sizeof(type) == 4) \
508                                                 { \
509                                                         subscript = (int)(value * hist_size); \
510                                                         CLAMP(subscript, 0, hist_size); \
511                                                 } \
512                                                 else \
513                                                         subscript = (int)value; \
514  \
515                                                 if((c = ++hist[2][subscript]) > count[2]) \
516                                                 { \
517                                                         val[2] = value; \
518                                                         count[2] = c; \
519                                                 } \
520  \
521                                                 if(components == 4) \
522                                                 { \
523                                 value = src[x2 * components + 3]; \
524                                                         if(sizeof(type) == 4) \
525                                                         { \
526                                                                 subscript = (int)(value * hist_size); \
527                                                                 CLAMP(subscript, 0, hist_size); \
528                                                         } \
529                                                         else \
530                                                                 subscript = (int)value; \
531  \
532                                                         if((c = ++hist[3][subscript]) > count[3]) \
533                                                         { \
534                                                                 val[3] = value; \
535                                                                 count[3] = c; \
536                                                         } \
537                                                 } \
538                                         } \
539                                 } \
540  \
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]; \
545                         } \
546                 } \
547                 else \
548                 { \
549                         for(int x1 = 0; x1 < w; x1++) \
550                         { \
551                                 count2 = 0; \
552                                 bzero(val, sizeof(val)); \
553                                 bzero(hist2, sizeof(int) * (hist_size + 1)); \
554  \
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); \
559  \
560                                 for(int y2 = y3; y2 < y4; y2++) \
561                                 { \
562                                         src = src_rows[y2]; \
563                                         for(int x2 = x3; x2 < x4; x2++) \
564                                         { \
565                                                 int c; \
566                                                 if((c = ++hist2[INTENSITY(src + x2 * components)]) > count2) \
567                                                 { \
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]; \
572                                                         count2 = c; \
573                                                 } \
574                                         } \
575                                 } \
576  \
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]; \
581                         } \
582                 } \
583         } \
584  \
585         for(int i = 0; i < components; i++) \
586                 delete [] hist[i]; \
587         delete [] hist2; \
588 }
589
590
591
592
593 void OilUnit::process_package(LoadPackage *package)
594 {
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);
599
600         switch(plugin->input->get_color_model())
601         {
602                 case BC_RGB_FLOAT:
603                         OIL_MACRO(float, 0xffff, 3)
604                         break;
605                 case BC_RGB888:
606                 case BC_YUV888:
607                         OIL_MACRO(unsigned char, 0xff, 3)
608                         break;
609                 case BC_RGB161616:
610                 case BC_YUV161616:
611                         OIL_MACRO(uint16_t, 0xffff, 3)
612                         break;
613                 case BC_RGBA_FLOAT:
614                         OIL_MACRO(float, 0xffff, 4)
615                         break;
616                 case BC_RGBA8888:
617                 case BC_YUVA8888:
618                         OIL_MACRO(unsigned char, 0xff, 4)
619                         break;
620                 case BC_RGBA16161616:
621                 case BC_YUVA16161616:
622                         OIL_MACRO(uint16_t, 0xffff, 4)
623                         break;
624         }
625
626
627
628
629 }
630
631
632
633
634
635
636 OilServer::OilServer(OilEffect *plugin, int cpus)
637  : LoadServer(cpus, cpus)
638 {
639         this->plugin = plugin;
640 }
641
642 void OilServer::init_packages()
643 {
644         for(int i = 0; i < LoadServer::get_total_packages(); i++)
645         {
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();
649         }
650 }
651
652
653 LoadClient* OilServer::new_client()
654 {
655         return new OilUnit(plugin, this);
656 }
657
658 LoadPackage* OilServer::new_package()
659 {
660         return new OilPackage;
661 }
662