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