1719fee85621db12aa43f6e896878e2c07b2f1da
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / colorbalance / colorbalancewindow.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 "colorbalancewindow.h"
24 #include "language.h"
25
26
27
28
29
30
31
32
33
34
35
36 ColorBalanceWindow::ColorBalanceWindow(ColorBalanceMain *client)
37  : PluginClientWindow(client, 400, 210, 400, 210, 0)
38 {
39         this->client = client;
40 }
41
42 ColorBalanceWindow::~ColorBalanceWindow()
43 {
44 }
45
46 void ColorBalanceWindow::create_objects()
47 {
48         int x = 10, y = 10;
49         int clrBtn_w = 50;
50         int x1 = 400 - clrBtn_w - 10; // (window_width - clrBtn_width - margin_rx)
51
52         add_tool(new BC_Title(x, y, _("Color Balance")));
53         y += 25;
54         add_tool(new BC_Title(x, y, _("Cyan")));
55         add_tool(cyan = new ColorBalanceSlider(client, &(client->config.cyan), x + 70, y));
56         add_tool(new BC_Title(x + 280, y, _("Red")));
57         add_subwindow(cyanClr = new ColorBalanceSliderClr(client, this, x1, y, clrBtn_w, 1));
58
59         y += 25;
60         add_tool(new BC_Title(x, y, _("Magenta")));
61         add_tool(magenta = new ColorBalanceSlider(client, &(client->config.magenta), x + 70, y));
62         add_tool(new BC_Title(x + 280, y, _("Green")));
63         add_subwindow(magentaClr = new ColorBalanceSliderClr(client, this, x1, y, clrBtn_w, 2));
64
65         y += 25;
66         add_tool(new BC_Title(x, y, _("Yellow")));
67         add_tool(yellow = new ColorBalanceSlider(client, &(client->config.yellow), x + 70, y));
68         add_tool(new BC_Title(x + 280, y, _("Blue")));
69         add_subwindow(yellowClr = new ColorBalanceSliderClr(client, this, x1, y, clrBtn_w, 3));
70
71         y += 25;
72         add_tool(preserve = new ColorBalancePreserve(client, x, y));
73         y += preserve->get_h() + 10;
74         add_tool(lock_params = new ColorBalanceLock(client, x, y));
75
76         y += lock_params->get_h() + 15;
77         add_tool(new ColorBalanceReset(client, this, x, y));
78         add_tool(new ColorBalanceWhite(client, this, int(400 / 2), y));
79
80         show_window();
81         flush();
82 }
83
84 void ColorBalanceWindow::update_gui(int clear)
85 {
86         switch(clear) {
87                 case RESET_CYAN : cyan->update((int64_t)client->config.cyan);
88                         break;
89                 case RESET_MAGENTA : magenta->update((int64_t)client->config.magenta);
90                         break;
91                 case RESET_YELLOW : yellow->update((int64_t)client->config.yellow);
92                         break;
93                 case RESET_ALL :
94                 case RESET_DEFAULT_SETTINGS :
95                 default:
96                         cyan->update((int64_t)client->config.cyan);
97                         magenta->update((int64_t)client->config.magenta);
98                         yellow->update((int64_t)client->config.yellow);
99                         preserve->update(client->config.preserve);
100                         lock_params->update(client->config.lock_params);
101                         break;
102         }
103 }
104
105
106 ColorBalanceSlider::ColorBalanceSlider(ColorBalanceMain *client,
107         float *output, int x, int y)
108  : BC_ISlider(x, y, 0, 200, 200, -1000, 1000, (int)*output)
109 {
110         this->client = client;
111         this->output = output;
112         old_value = *output;
113 }
114
115 ColorBalanceSlider::~ColorBalanceSlider()
116 {
117 }
118
119 int ColorBalanceSlider::handle_event()
120 {
121         float difference = get_value() - *output;
122         *output = get_value();
123         client->synchronize_params(this, difference);
124         client->send_configure_change();
125         return 1;
126 }
127
128 char* ColorBalanceSlider::get_caption()
129 {
130         float fraction = client->calculate_transfer(*output);
131         sprintf(string, "%0.4f", fraction);
132         return string;
133 }
134
135
136
137
138 ColorBalancePreserve::ColorBalancePreserve(ColorBalanceMain *client, int x, int y)
139  : BC_CheckBox(x,
140         y,
141         client->config.preserve,
142         _("Preserve luminosity"))
143 {
144         this->client = client;
145 }
146 ColorBalancePreserve::~ColorBalancePreserve()
147 {
148 }
149
150 int ColorBalancePreserve::handle_event()
151 {
152         client->config.preserve = get_value();
153         client->send_configure_change();
154         return 1;
155 }
156
157 ColorBalanceLock::ColorBalanceLock(ColorBalanceMain *client, int x, int y)
158  : BC_CheckBox(x,
159         y,
160         client->config.lock_params,
161         _("Lock parameters"))
162 {
163         this->client = client;
164 }
165 ColorBalanceLock::~ColorBalanceLock()
166 {
167 }
168
169 int ColorBalanceLock::handle_event()
170 {
171         client->config.lock_params = get_value();
172         client->send_configure_change();
173         return 1;
174 }
175
176
177 ColorBalanceWhite::ColorBalanceWhite(ColorBalanceMain *plugin,
178         ColorBalanceWindow *gui,
179         int x,
180         int y)
181  : BC_GenericButton(x, y, _("White balance"))
182 {
183         this->plugin = plugin;
184         this->gui = gui;
185 }
186
187 int ColorBalanceWhite::handle_event()
188 {
189 // Get colorpicker value
190         float red = plugin->get_red();
191         float green = plugin->get_green();
192         float blue = plugin->get_blue();
193 // // Get maximum value
194 //      float max = MAX(red, green);
195 //      max  = MAX(max, blue);
196 // // Get factors required to normalize other values to maximum
197 //      float r_factor = max / red;
198 //      float g_factor = max / green;
199 //      float b_factor = max / blue;
200 // Get minimum value.  Can't use maximum because the sliders run out of room.
201         float min = MIN(red, green);
202         min = MIN(min, blue);
203 // Get factors required to normalize other values to minimum
204         float r_factor = min / red;
205         float g_factor = min / green;
206         float b_factor = min / blue;
207
208 // Try normalizing to green like others do it
209         r_factor = green / red;
210         g_factor = 1.0;
211         b_factor = green / blue;
212 // Convert factors to slider values
213         plugin->config.cyan = plugin->calculate_slider(r_factor);
214         plugin->config.magenta = plugin->calculate_slider(g_factor);
215         plugin->config.yellow = plugin->calculate_slider(b_factor);
216         gui->update_gui(RESET_DEFAULT_SETTINGS);
217
218         plugin->send_configure_change();
219         return 1;
220 }
221
222
223 ColorBalanceReset::ColorBalanceReset(ColorBalanceMain *plugin,
224         ColorBalanceWindow *gui,
225         int x,
226         int y)
227  : BC_GenericButton(x, y, _("Reset"))
228 {
229         this->plugin = plugin;
230         this->gui = gui;
231 }
232
233 int ColorBalanceReset::handle_event()
234 {
235         plugin->config.reset(RESET_ALL);
236         gui->update_gui(RESET_ALL);
237         plugin->send_configure_change();
238         return 1;
239 }
240
241
242 ColorBalanceSliderClr::ColorBalanceSliderClr(ColorBalanceMain *plugin,
243         ColorBalanceWindow *gui, int x, int y, int w, int clear)
244  : BC_GenericButton(x, y, w, _("⌂"))
245 {
246         this->plugin = plugin;
247         this->gui = gui;
248         this->clear = clear;
249 }
250 ColorBalanceSliderClr::~ColorBalanceSliderClr()
251 {
252 }
253 int ColorBalanceSliderClr::handle_event()
254 {
255         plugin->config.reset(clear);
256         gui->update_gui(clear);
257         plugin->send_configure_change();
258         return 1;
259 }
260