f733033e7d3413e737139d01a5afd26735c77ab9
[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         read_frame(frame, 0, start_position, frame_rate, get_use_opengl());
137         int w = frame->get_w(), h = frame->get_h();
138
139 #define MAIN_LOOP(type, components, is_yuv, max) do { \
140         if( components == 4 ) { \
141                 for( int y=0; y<h; ++y ) { \
142                         type *row = (type*)frame->get_rows()[y]; \
143                         for( int x=0; x<w; ++x ) { \
144                                 row[3] = a * max; \
145                                 row += components; \
146                         } \
147                 } \
148         } \
149         else if( is_yuv ) { \
150                 type ofs = (max+1)/2; \
151                 for( int y=0; y<h; ++y ) { \
152                         type *row = (type*)frame->get_rows()[y]; \
153                         for( int x=0; x<w; ++x ) { \
154                                 row[0] = row[0] * a; \
155                                 row[1] = (row[1]-ofs) * a + ofs; \
156                                 row[2] = (row[2]-ofs) * a + ofs; \
157                                 row += components; \
158                         } \
159                 } \
160         } \
161         else {\
162                 for( int y=0; y<h; ++y ) { \
163                         type *row = (type*)frame->get_rows()[y]; \
164                         for( int x=0; x<w; ++x ) { \
165                                 row[0] *= a; \
166                                 row[1] *= a; \
167                                 row[2] *= a; \
168                                 row += components; \
169                         } \
170                 } \
171         } \
172 } while(0)
173
174         float a = config.a;
175         switch( frame->get_color_model() ) {
176         case BC_RGB888:     MAIN_LOOP(uint8_t, 3, 0, 0xff);  break;
177         case BC_RGB_FLOAT:  MAIN_LOOP(float,   3, 0, 1.0 );  break;
178         case BC_YUV888:     MAIN_LOOP(uint8_t, 3, 1, 0xff);  break;
179         case BC_RGBA8888:   MAIN_LOOP(uint8_t, 4, 0, 0xff);  break;
180         case BC_RGBA_FLOAT: MAIN_LOOP(float,   4, 0, 1.0 );  break;
181         case BC_YUVA8888:   MAIN_LOOP(uint8_t, 4, 1, 0xff);  break;
182         }
183
184         return 0;
185 }
186
187
188 void AlphaMain::update_gui()
189 {
190         if( !thread ) return;
191         AlphaWindow *window = (AlphaWindow*)thread->window;
192         if( !window ) return;
193         if( !load_configuration() ) return;
194         window->lock_window("AlphaMain::update_gui");
195         window->update();
196         window->unlock_window();
197 }
198
199
200 void AlphaMain::save_data(KeyFrame *keyframe)
201 {
202         FileXML output;
203         output.set_shared_output(keyframe->xbuf);
204         output.tag.set_title("ALPHA");
205         output.tag.set_property("A", config.a);
206         output.append_tag();
207         output.terminate_string();
208 }
209
210 void AlphaMain::read_data(KeyFrame *keyframe)
211 {
212         FileXML input;
213         input.set_shared_input(keyframe->xbuf);
214
215         int result = 0;
216         while( !(result = input.read_tag()) ) {
217                 if( input.tag.title_is("ALPHA") ) {
218                         config.a = input.tag.get_property("A", config.a);
219                 }
220         }
221 }
222