mask xy scale, mask boundary only overlay, fix 8 char mask nm bug, rework maskgui...
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / yuv / yuv.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 "language.h"
28 #include "bccolors.h"
29 #include "pluginvclient.h"
30 #include "theme.h"
31 #include "vframe.h"
32
33 #include <stdint.h>
34 #include <string.h>
35
36 #define RESET_ALL 0
37 #define RESET_Y_SLIDER 1
38 #define RESET_U_SLIDER 2
39 #define RESET_V_SLIDER 3
40
41 class YUVEffect;
42 class YUVWindow;
43 class YUVReset;
44 class YUVSliderClr;
45
46
47 class YUVConfig
48 {
49 public:
50         YUVConfig();
51         void reset(int clear);
52
53         void copy_from(YUVConfig &src);
54         int equivalent(YUVConfig &src);
55         void interpolate(YUVConfig &prev,
56                 YUVConfig &next,
57                 long prev_frame,
58                 long next_frame,
59                 long current_frame);
60
61         float y, u, v;
62 };
63
64 class YUVLevel : public BC_FSlider
65 {
66 public:
67         YUVLevel(YUVEffect *plugin, float *output, int x, int y);
68         int handle_event();
69         YUVEffect *plugin;
70         float *output;
71 };
72
73 class YUVReset : public BC_GenericButton
74 {
75 public:
76         YUVReset(YUVEffect *plugin, YUVWindow *window, int x, int y);
77         ~YUVReset();
78         int handle_event();
79         YUVEffect *plugin;
80         YUVWindow *window;
81 };
82
83 class YUVSliderClr : public BC_Button
84 {
85 public:
86         YUVSliderClr(YUVEffect *plugin, YUVWindow *window, int x, int y, int w, int clear);
87         ~YUVSliderClr();
88         int handle_event();
89         YUVEffect *plugin;
90         YUVWindow *window;
91         int clear;
92 };
93
94 class YUVWindow : public PluginClientWindow
95 {
96 public:
97         YUVWindow(YUVEffect *plugin);
98         void create_objects();
99         void update_gui(int clear);
100         YUVLevel *y, *u, *v;
101         YUVEffect *plugin;
102         YUVReset *reset;
103         YUVSliderClr *yClr, *uClr, *vClr;
104 };
105
106
107
108 class YUVEffect : public PluginVClient
109 {
110 public:
111         YUVEffect(PluginServer *server);
112         ~YUVEffect();
113
114
115         PLUGIN_CLASS_MEMBERS(YUVConfig)
116         int process_realtime(VFrame *input, VFrame *output);
117         int is_realtime();
118         void save_data(KeyFrame *keyframe);
119         void read_data(KeyFrame *keyframe);
120         void update_gui();
121 };
122
123
124
125
126
127 REGISTER_PLUGIN(YUVEffect)
128
129
130
131
132
133
134
135 YUVConfig::YUVConfig()
136 {
137         reset(RESET_ALL);
138 }
139
140 void YUVConfig::reset(int clear)
141 {
142         switch(clear) {
143                 case RESET_Y_SLIDER : y = 0;
144                         break;
145                 case RESET_U_SLIDER : u = 0;
146                         break;
147                 case RESET_V_SLIDER : v = 0;
148                         break;
149                 case RESET_ALL :
150                 default:
151                         y = u = v = 0;
152                         break;
153         }
154 }
155
156 void YUVConfig::copy_from(YUVConfig &src)
157 {
158         y = src.y;
159         u = src.u;
160         v = src.v;
161 }
162
163 int YUVConfig::equivalent(YUVConfig &src)
164 {
165         return EQUIV(y, src.y) && EQUIV(u, src.u) && EQUIV(v, src.v);
166 }
167
168 void YUVConfig::interpolate(YUVConfig &prev,
169         YUVConfig &next,
170         long prev_frame,
171         long next_frame,
172         long current_frame)
173 {
174         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
175         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
176
177         y = prev.y * prev_scale + next.y * next_scale;
178         u = prev.u * prev_scale + next.u * next_scale;
179         v = prev.v * prev_scale + next.v * next_scale;
180 }
181
182
183
184
185
186
187 #define MAXVALUE 100
188
189 YUVLevel::YUVLevel(YUVEffect *plugin, float *output, int x, int y)
190  : BC_FSlider(x,
191                         y,
192                         0,
193                         200,
194                         200,
195                         -MAXVALUE,
196                         MAXVALUE,
197                         *output)
198 {
199         this->plugin = plugin;
200         this->output = output;
201 }
202
203 int YUVLevel::handle_event()
204 {
205         *output = get_value();
206         plugin->send_configure_change();
207         return 1;
208 }
209
210
211 YUVReset::YUVReset(YUVEffect *plugin, YUVWindow *window, int x, int y)
212  : BC_GenericButton(x, y, _("Reset"))
213 {
214         this->plugin = plugin;
215         this->window = window;
216 }
217 YUVReset::~YUVReset()
218 {
219 }
220 int YUVReset::handle_event()
221 {
222         plugin->config.reset(RESET_ALL);
223         window->update_gui(RESET_ALL);
224         plugin->send_configure_change();
225         return 1;
226 }
227
228
229 YUVSliderClr::YUVSliderClr(YUVEffect *plugin, YUVWindow *window, int x, int y, int w, int clear)
230  : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
231 {
232         this->plugin = plugin;
233         this->window = window;
234         this->clear = clear;
235 }
236 YUVSliderClr::~YUVSliderClr()
237 {
238 }
239 int YUVSliderClr::handle_event()
240 {
241         // clear==1 ==> Y slider
242         // clear==2 ==> U slider
243         // clear==3 ==> V slider
244         plugin->config.reset(clear);
245         window->update_gui(clear);
246         plugin->send_configure_change();
247         return 1;
248 }
249
250
251 YUVWindow::YUVWindow(YUVEffect *plugin)
252  : PluginClientWindow(plugin, 310, 135, 310, 135, 0)
253 {
254         this->plugin = plugin;
255 }
256
257 void YUVWindow::create_objects()
258 {
259         int x = 10, y = 10, x1 = 40;
260         int x2 = 0; int clrBtn_w = 50;
261
262         add_subwindow(new BC_Title(x, y, _("Y:")));
263         add_subwindow(this->y = new YUVLevel(plugin, &plugin->config.y, x1, y));
264         x2 = x1 + this->y->get_w() + 10;
265         add_subwindow(yClr = new YUVSliderClr(plugin, this, x2, y, clrBtn_w, RESET_Y_SLIDER));
266
267         y += 30;
268         add_subwindow(new BC_Title(x, y, _("U:")));
269         add_subwindow(u = new YUVLevel(plugin, &plugin->config.u, x1, y));
270         add_subwindow(uClr = new YUVSliderClr(plugin, this, x2, y, clrBtn_w, RESET_U_SLIDER));
271
272         y += 30;
273         add_subwindow(new BC_Title(x, y, _("V:")));
274         add_subwindow(v = new YUVLevel(plugin, &plugin->config.v, x1, y));
275         add_subwindow(vClr = new YUVSliderClr(plugin, this, x2, y, clrBtn_w, RESET_V_SLIDER));
276
277         y += 35;
278         add_subwindow(reset = new YUVReset(plugin, this, x, y));
279
280         show_window();
281         flush();
282 }
283
284
285 // for Reset button
286 void YUVWindow::update_gui(int clear)
287 {
288         switch(clear) {
289                 case RESET_Y_SLIDER : this->y->update(plugin->config.y);
290                         break;
291                 case RESET_U_SLIDER : u->update(plugin->config.u);
292                         break;
293                 case RESET_V_SLIDER : v->update(plugin->config.v);
294                         break;
295                 case RESET_ALL :
296                 default:
297                         this->y->update(plugin->config.y);
298                         u->update(plugin->config.u);
299                         v->update(plugin->config.v);
300                         break;
301         }
302 }
303
304
305
306
307
308
309 YUVEffect::YUVEffect(PluginServer *server)
310  : PluginVClient(server)
311 {
312
313 }
314 YUVEffect::~YUVEffect()
315 {
316
317 }
318
319 const char* YUVEffect::plugin_title() { return N_("YUV"); }
320 int YUVEffect::is_realtime() { return 1; }
321
322
323 NEW_WINDOW_MACRO(YUVEffect, YUVWindow)
324 LOAD_CONFIGURATION_MACRO(YUVEffect, YUVConfig)
325
326 void YUVEffect::update_gui()
327 {
328         if(thread)
329         {
330                 thread->window->lock_window();
331                 load_configuration();
332                 ((YUVWindow*)thread->window)->y->update(config.y);
333                 ((YUVWindow*)thread->window)->u->update(config.u);
334                 ((YUVWindow*)thread->window)->v->update(config.v);
335                 thread->window->unlock_window();
336         }
337 }
338
339 void YUVEffect::save_data(KeyFrame *keyframe)
340 {
341         FileXML output;
342         output.set_shared_output(keyframe->xbuf);
343         output.tag.set_title("YUV");
344         output.tag.set_property("Y", config.y);
345         output.tag.set_property("U", config.u);
346         output.tag.set_property("V", config.v);
347         output.append_tag();
348         output.tag.set_title("/YUV");
349         output.append_tag();
350         output.append_newline();
351         output.terminate_string();
352 }
353
354 void YUVEffect::read_data(KeyFrame *keyframe)
355 {
356         FileXML input;
357         input.set_shared_input(keyframe->xbuf);
358         while(!input.read_tag())
359         {
360                 if(input.tag.title_is("YUV"))
361                 {
362                         config.y = input.tag.get_property("Y", config.y);
363                         config.u = input.tag.get_property("U", config.u);
364                         config.v = input.tag.get_property("V", config.v);
365                 }
366         }
367 }
368
369
370 #define YUV_MACRO(type, temp_type, max, components, use_yuv) \
371 { \
372         for(int i = 0; i < input->get_h(); i++) \
373         { \
374                 type *in_row = (type*)input->get_rows()[i]; \
375                 type *out_row = (type*)output->get_rows()[i]; \
376                 const float round = (sizeof(type) == 4) ? 0.0 : 0.5; \
377  \
378                 for(int j = 0; j < w; j++) \
379                 { \
380                         if(use_yuv) \
381                         { \
382                                 int y = (int)((float)in_row[0] * y_scale + round); \
383                                 int u = (int)((float)(in_row[1] - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
384                                 int v = (int)((float)(in_row[2] - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
385                                 out_row[0] = CLIP(y, 0, max); \
386                                 out_row[1] = CLIP(u, 0, max); \
387                                 out_row[2] = CLIP(v, 0, max); \
388                         } \
389                         else \
390                         { \
391                                 temp_type y, u, v, r, g, b; \
392                                 if(sizeof(type) == 4) \
393                                 { \
394                                         YUV::yuv.rgb_to_yuv_f(in_row[0], in_row[1], in_row[2], y, u, v); \
395                                 } \
396                                 else \
397                                 if(sizeof(type) == 2) \
398                                 { \
399                                         YUV::yuv.rgb_to_yuv_16(in_row[0], in_row[1], in_row[2], y, u, v); \
400                                 } \
401                                 else \
402                                 { \
403                                         YUV::yuv.rgb_to_yuv_8(in_row[0], in_row[1], in_row[2], y, u, v); \
404                                 } \
405  \
406                                 if(sizeof(type) < 4) \
407                                 { \
408                                         CLAMP(y, 0, max); \
409                                         CLAMP(u, 0, max); \
410                                         CLAMP(v, 0, max); \
411  \
412                                         y = temp_type((float)y * y_scale + round); \
413                                         u = temp_type((float)(u - (max / 2 + 1)) * u_scale + round) + (max / 2 + 1); \
414                                         v = temp_type((float)(v - (max / 2 + 1)) * v_scale + round) + (max / 2 + 1); \
415  \
416                                         CLAMP(y, 0, max); \
417                                         CLAMP(u, 0, max); \
418                                         CLAMP(v, 0, max); \
419                                 } \
420                                 else \
421                                 { \
422                                         y = temp_type((float)y * y_scale + round); \
423                                         u = temp_type((float)u * u_scale + round); \
424                                         v = temp_type((float)v * v_scale + round); \
425                                 } \
426  \
427                                 if(sizeof(type) == 4) \
428                                         YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v); \
429                                 else \
430                                 if(sizeof(type) == 2) \
431                                         YUV::yuv.yuv_to_rgb_16(r, g, b, y, u, v); \
432                                 else \
433                                         YUV::yuv.yuv_to_rgb_8(r, g, b, y, u, v); \
434  \
435                                 out_row[0] = r; \
436                                 out_row[1] = g; \
437                                 out_row[2] = b; \
438                         } \
439                  \
440                         in_row += components; \
441                         out_row += components; \
442                 } \
443         } \
444 }
445
446 int YUVEffect::process_realtime(VFrame *input, VFrame *output)
447 {
448         load_configuration();
449
450         if(EQUIV(config.y, 0) && EQUIV(config.u, 0) && EQUIV(config.v, 0))
451         {
452                 if(input->get_rows()[0] != output->get_rows()[0])
453                         output->copy_from(input);
454         }
455         else
456         {
457                 int w = input->get_w();
458
459                 float y_scale = (float)(config.y + MAXVALUE) / MAXVALUE;
460                 float u_scale = (float)(config.u + MAXVALUE) / MAXVALUE;
461                 float v_scale = (float)(config.v + MAXVALUE) / MAXVALUE;
462
463                 if(u_scale > 1) u_scale = 1 + (u_scale - 1) * 4;
464                 if(v_scale > 1) v_scale = 1 + (v_scale - 1) * 4;
465
466                 switch(input->get_color_model())
467                 {
468                         case BC_RGB_FLOAT:
469                                 YUV_MACRO(float, float, 1, 3, 0)
470                                 break;
471
472                         case BC_RGB888:
473                                 YUV_MACRO(unsigned char, int, 0xff, 3, 0)
474                                 break;
475
476                         case BC_YUV888:
477                                 YUV_MACRO(unsigned char, int, 0xff, 3, 1)
478                                 break;
479
480                         case BC_RGB161616:
481                                 YUV_MACRO(uint16_t, int, 0xffff, 3, 0)
482                                 break;
483
484                         case BC_YUV161616:
485                                 YUV_MACRO(uint16_t, int, 0xffff, 3, 1)
486                                 break;
487
488                         case BC_RGBA_FLOAT:
489                                 YUV_MACRO(float, float, 1, 4, 0)
490                                 break;
491
492                         case BC_RGBA8888:
493                                 YUV_MACRO(unsigned char, int, 0xff, 4, 0)
494                                 break;
495
496                         case BC_YUVA8888:
497                                 YUV_MACRO(unsigned char, int, 0xff, 4, 1)
498                                 break;
499
500                         case BC_RGBA16161616:
501                                 YUV_MACRO(uint16_t, int, 0xffff, 4, 0)
502                                 break;
503
504                         case BC_YUVA16161616:
505                                 YUV_MACRO(uint16_t, int, 0xffff, 4, 1)
506                                 break;
507                 }
508
509
510
511         }
512         return 0;
513 }
514
515