87f141e913de71470557e38bff5b187150184402
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / alpha / alpha.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008-2019 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 #include <math.h>
22 #include <stdint.h>
23 #include <string.h>
24
25 #include "alpha.h"
26 #include "filexml.h"
27 #include "keyframe.h"
28 #include "language.h"
29 #include "vframe.h"
30
31 REGISTER_PLUGIN(AlphaMain)
32
33
34 AlphaConfig::AlphaConfig()
35 {
36         a = 1.;
37 }
38
39 int AlphaConfig::equivalent(AlphaConfig &that)
40 {
41         return a == that.a;
42 }
43
44 void AlphaConfig::copy_from(AlphaConfig &that)
45 {
46         a = that.a;
47 }
48
49 void AlphaConfig::interpolate(AlphaConfig &prev, AlphaConfig &next,
50                 long prev_frame, long next_frame, long current_frame)
51 {
52         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
53         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
54
55         a = (prev.a * prev_scale + next.a * next_scale);
56 }
57
58
59 AlphaSlider::AlphaSlider(AlphaWindow *window, AlphaMain *plugin,
60                 int x, int y, int w)
61  : BC_FSlider(x, y, 0, w, w, 0.f, 1.f, plugin->config.a)
62 {
63         this->window = window;
64         this->plugin = plugin;
65         set_precision(0.001);
66 }
67 AlphaSlider::~AlphaSlider()
68 {
69 }
70
71 int AlphaSlider::handle_event()
72 {
73         plugin->config.a = get_value();
74         plugin->send_configure_change();
75         return 1;
76 }
77
78 #define ALPHA_W xS(400)
79 #define ALPHA_H yS(60)
80
81 AlphaWindow::AlphaWindow(AlphaMain *plugin)
82  : PluginClientWindow(plugin, ALPHA_W, ALPHA_H, ALPHA_W, ALPHA_H, 0)
83 {
84         this->plugin = plugin;
85 }
86
87 AlphaWindow::~AlphaWindow()
88 {
89 }
90
91 void AlphaWindow::create_objects()
92 {
93         int x = xS(10), y = yS(10);
94         BC_Title *title;
95         add_subwindow(title = new BC_Title(x, y, _("Alpha:")));
96         y += title->get_h() + yS(5);
97         add_subwindow(alpha_slider = new AlphaSlider(this, plugin, x, y, xS(380)));
98         show_window();
99 }
100
101 void AlphaWindow::update()
102 {
103         float alpha = plugin->config.a;
104         alpha_slider->update(alpha);
105 }
106
107
108 AlphaMain::AlphaMain(PluginServer *server)
109  : PluginVClient(server)
110 {
111 }
112
113 AlphaMain::~AlphaMain()
114 {
115 }
116
117 const char* AlphaMain::plugin_title() { return N_("Alpha"); }
118 int AlphaMain::is_realtime() { return 1; }
119
120 NEW_WINDOW_MACRO(AlphaMain, AlphaWindow)
121
122 LOAD_CONFIGURATION_MACRO(AlphaMain, AlphaConfig)
123
124 int AlphaMain::is_synthesis()
125 {
126         return 1;
127 }
128
129
130 int AlphaMain::process_buffer(VFrame *frame,
131         int64_t start_position,
132         double frame_rate)
133 {
134         load_configuration();
135
136         int color_model = frame->get_color_model();
137         int need_alpha = BC_CModels::has_alpha(color_model);
138         if( need_alpha ) {
139                 read_frame(frame, 0, start_position,
140                         frame_rate, get_use_opengl());
141         }
142         int w = frame->get_w();
143         int h = frame->get_h();
144
145
146 #define MAIN_LOOP(type, components, is_yuv, max) do { \
147         if( components == 4 ) { \
148                 for( int y=0; y<h; ++y ) { \
149                         type *row = (type*)frame->get_rows()[y]; \
150                         for( int x=0; x<w; ++x ) { \
151                                 row[3] = a * max; \
152                                 row += components; \
153                         } \
154                 } \
155         } \
156         else if( is_yuv ) { \
157                 type ofs = (max+1)/2; \
158                 for( int y=0; y<h; ++y ) { \
159                         type *row = (type*)frame->get_rows()[y]; \
160                         for( int x=0; x<w; ++x ) { \
161                                 row[0] = row[0] * a; \
162                                 row[1] = (row[1]-ofs) * a + ofs; \
163                                 row[2] = (row[2]-ofs) * a + ofs; \
164                                 row += components; \
165                         } \
166                 } \
167         } \
168         else {\
169                 for( int y=0; y<h; ++y ) { \
170                         type *row = (type*)frame->get_rows()[y]; \
171                         for( int x=0; x<w; ++x ) { \
172                                 row[0] *= a; \
173                                 row[1] *= a; \
174                                 row[2] *= a; \
175                                 row += components; \
176                         } \
177                 } \
178         } \
179 } while(0)
180
181         float a = config.a;
182         switch( frame->get_color_model() ) {
183         case BC_RGB888:     MAIN_LOOP(uint8_t, 3, 0, 0xff);  break;
184         case BC_RGB_FLOAT:  MAIN_LOOP(float,   3, 0, 1.0 );  break;
185         case BC_YUV888:     MAIN_LOOP(uint8_t, 3, 1, 0xff);  break;
186         case BC_RGBA8888:   MAIN_LOOP(uint8_t, 4, 0, 0xff);  break;
187         case BC_RGBA_FLOAT: MAIN_LOOP(float,   4, 0, 1.0 );  break;
188         case BC_YUVA8888:   MAIN_LOOP(uint8_t, 4, 1, 0xff);  break;
189         }
190
191         return 0;
192 }
193
194
195 void AlphaMain::update_gui()
196 {
197         if( !thread ) return;
198         AlphaWindow *window = (AlphaWindow*)thread->window;
199         if( !window ) return;
200         if( !load_configuration() ) return;
201         window->lock_window("AlphaMain::update_gui");
202         window->update();
203         window->unlock_window();
204 }
205
206
207 void AlphaMain::save_data(KeyFrame *keyframe)
208 {
209         FileXML output;
210         output.set_shared_output(keyframe->xbuf);
211         output.tag.set_title("ALPHA");
212         output.tag.set_property("A", config.a);
213         output.append_tag();
214         output.terminate_string();
215 }
216
217 void AlphaMain::read_data(KeyFrame *keyframe)
218 {
219         FileXML input;
220         input.set_shared_input(keyframe->xbuf);
221
222         int result = 0;
223         while( !(result = input.read_tag()) ) {
224                 if( input.tag.title_is("ALPHA") ) {
225                         config.a = input.tag.get_property("A", config.a);
226                 }
227         }
228 }
229