switch from eclipse to android studio
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / gamma / gammawindow.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 "gammawindow.h"
24 #include "language.h"
25
26
27
28
29
30
31
32
33
34
35 GammaWindow::GammaWindow(GammaMain *client)
36  : PluginClientWindow(client,
37         400,
38         380,
39         400,
40         380,
41         0)
42 {
43         this->client = client;
44 }
45
46 void GammaWindow::create_objects()
47 {
48         int x = 10, y = 10;
49         add_subwindow(histogram = new BC_SubWindow(x,
50                 y,
51                 get_w() - x * 2,
52                 get_h() - 180,
53                 WHITE));
54         y += histogram->get_h() + 10;
55
56         BC_Title *title;
57         add_tool(title = new BC_Title(x, y, _("Maximum:")));
58         x += title->get_w() + 10;
59         add_tool(max_slider = new MaxSlider(client,
60                 this,
61                 x,
62                 y,
63                 190));
64         x += max_slider->get_w() + 10;
65         add_tool(max_text = new MaxText(client,
66                 this,
67                 x,
68                 y,
69                 100));
70         y += max_text->get_h() + 10;
71         x = 10;
72         add_tool(automatic = new GammaAuto(client, x, y));
73
74         y += automatic->get_h() + 10;
75         add_tool(title = new BC_Title(x, y, _("Gamma:")));
76         x += title->get_w() + 10;
77         add_tool(gamma_slider = new GammaSlider(client,
78                 this,
79                 x,
80                 y,
81                 190));
82         x += gamma_slider->get_w() + 10;
83         add_tool(gamma_text = new GammaText(client,
84                 this,
85                 x,
86                 y,
87                 100));
88         y += gamma_text->get_h() + 10;
89         x = 10;
90
91         add_tool(plot = new GammaPlot(client, x, y));
92         y += plot->get_h() + 10;
93
94         add_tool(new GammaColorPicker(client, this, x, y));
95
96         show_window();
97         flush();
98 }
99
100 void GammaWindow::update()
101 {
102         max_slider->update(client->config.max);
103         max_text->update(client->config.max);
104         gamma_slider->update(client->config.gamma);
105         gamma_text->update(client->config.gamma);
106         automatic->update(client->config.automatic);
107         plot->update(client->config.plot);
108         update_histogram();
109 }
110
111 void GammaWindow::update_histogram()
112 {
113         histogram->clear_box(0, 0, histogram->get_w(), histogram->get_h());
114         if(client->engine)
115         {
116                 int max = 0;
117                 histogram->set_color(MEGREY);
118                 for(int i = 0; i < histogram->get_w(); i++)
119                 {
120                         int x1 = (int64_t)i * HISTOGRAM_SIZE / histogram->get_w();
121                         int x2 = (int64_t)(i + 1) * HISTOGRAM_SIZE / histogram->get_w();
122                         if(x2 == x1) x2++;
123                         int accum = 0;
124                         for(int x = x1; x < x2; x++)
125                         {
126                                 accum += client->engine->accum[x];
127                         }
128                         if(accum > max) max = accum;
129                 }
130                 for(int i = 0; i < histogram->get_w(); i++)
131                 {
132                         int x1 = (int64_t)i * HISTOGRAM_SIZE / histogram->get_w();
133                         int x2 = (int64_t)(i + 1) * HISTOGRAM_SIZE / histogram->get_w();
134                         if(x2 == x1) x2++;
135                         int accum = 0;
136                         for(int x = x1; x < x2; x++)
137                         {
138                                 accum += client->engine->accum[x];
139                         }
140
141                         int h = (int)(log(accum) / log(max) * histogram->get_h());
142                         histogram->draw_line(i,
143                                 histogram->get_h(),
144                                 i,
145                                 histogram->get_h() - h);
146                 }
147         }
148
149         histogram->set_color(GREEN);
150         int y1 = histogram->get_h();
151         float max = client->config.max * client->config.gamma;
152         float scale = 1.0 / max;
153         float gamma = client->config.gamma - 1.0;
154         for(int i = 1; i < histogram->get_w(); i++)
155         {
156                 float in = (float)i / histogram->get_w();
157                 float out = in * (scale * pow(in * 2 / max, gamma));
158                 int y2 = (int)(histogram->get_h() - out * histogram->get_h());
159                 histogram->draw_line(i - 1, y1, i, y2);
160                 y1 = y2;
161         }
162         histogram->flash();
163 }
164
165
166
167
168 MaxSlider::MaxSlider(GammaMain *client,
169         GammaWindow *gui,
170         int x,
171         int y,
172         int w)
173  : BC_FSlider(x,
174         y,
175         0,
176         w,
177         w,
178         0.0,
179         1.0,
180         client->config.max)
181 {
182         this->client = client;
183         this->gui = gui;
184         set_precision(0.01);
185 }
186
187 int MaxSlider::handle_event()
188 {
189         client->config.max = get_value();
190         gui->max_text->update(client->config.max);
191         gui->update_histogram();
192         client->send_configure_change();
193         return 1;
194 }
195
196 MaxText::MaxText(GammaMain *client,
197         GammaWindow *gui,
198         int x,
199         int y,
200         int w)
201  : BC_TextBox(x, y, w, 1, client->config.max)
202 {
203         this->client = client;
204         this->gui = gui;
205 }
206
207 int MaxText::handle_event()
208 {
209         client->config.max = atof(get_text());
210         gui->max_slider->update(client->config.max);
211         client->send_configure_change();
212         return 1;
213 }
214
215 GammaSlider::GammaSlider(GammaMain *client,
216         GammaWindow *gui,
217         int x,
218         int y,
219         int w)
220  : BC_FSlider(x,
221         y,
222         0,
223         w,
224         w,
225         0.0,
226         1.0,
227         client->config.gamma)
228 {
229         this->client = client;
230         this->gui = gui;
231         set_precision(0.01);
232 }
233
234 int GammaSlider::handle_event()
235 {
236         client->config.gamma = get_value();
237         gui->gamma_text->update(client->config.gamma);
238         gui->update_histogram();
239         client->send_configure_change();
240         return 1;
241 }
242
243 GammaText::GammaText(GammaMain *client,
244         GammaWindow *gui,
245         int x,
246         int y,
247         int w)
248  : BC_TextBox(x, y, w, 1, client->config.gamma)
249 {
250         this->client = client;
251         this->gui = gui;
252 }
253
254 int GammaText::handle_event()
255 {
256         client->config.gamma = atof(get_text());
257         gui->gamma_slider->update(client->config.gamma);
258         client->send_configure_change();
259         return 1;
260 }
261
262 GammaAuto::GammaAuto(GammaMain *client, int x, int y)
263  : BC_CheckBox(x,
264         y,
265         client->config.automatic,
266         _("Automatic"))
267 {
268         this->plugin = client;
269 }
270
271 int GammaAuto::handle_event()
272 {
273         plugin->config.automatic = get_value();
274         plugin->send_configure_change();
275         return 1;
276 }
277
278
279 GammaPlot::GammaPlot(GammaMain *plugin, int x, int y)
280  : BC_CheckBox(x, y, plugin->config.plot, _("Plot histogram"))
281 {
282         this->plugin = plugin;
283 }
284 int GammaPlot::handle_event()
285 {
286         plugin->config.plot = get_value();
287         plugin->send_configure_change();
288         return 1;
289 }
290
291
292 GammaColorPicker::GammaColorPicker(GammaMain *plugin,
293         GammaWindow *gui,
294         int x,
295         int y)
296  : BC_GenericButton(x, y, _("Use Color Picker"))
297 {
298         this->plugin = plugin;
299         this->gui = gui;
300 }
301
302 int GammaColorPicker::handle_event()
303 {
304 // Get colorpicker value
305         float red = plugin->get_red();
306         float green = plugin->get_green();
307         float blue = plugin->get_blue();
308 // Get maximum value
309         plugin->config.max = MAX(red, green);
310         plugin->config.max = MAX(plugin->config.max, blue);
311         gui->max_text->update(plugin->config.max);
312         gui->max_slider->update(plugin->config.max);
313         plugin->send_configure_change();
314         return 1;
315 }
316
317