4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "bcdisplayinfo.h"
23 #include "gammawindow.h"
28 GammaWindow::GammaWindow(GammaMain *client)
29 : PluginClientWindow(client,
36 this->client = client;
39 void GammaWindow::create_objects()
41 int xs10 = xS(10), xs100 = xS(100), xs110 = xS(110), xs190 = xS(190);
42 int ys10 = yS(10), ys180 = yS(180);
43 int x = xs10, y = ys10, x1 = x;
44 add_subwindow(histogram = new BC_SubWindow(x,
49 y += histogram->get_h() + ys10;
52 add_tool(title = new BC_Title(x, y, _("Maximum:")));
53 x += title->get_w() + xs10;
54 x1 = x; // save x to align the two sliders
55 add_tool(max_slider = new MaxSlider(client,
60 x += max_slider->get_w() + xs10;
61 add_tool(max_text = new MaxText(client,
66 y += max_text->get_h() + ys10;
68 add_tool(automatic = new GammaAuto(client, x, y));
69 y += automatic->get_h() + ys10;
70 add_tool(title = new BC_Title(x, y, _("Gamma:")));
71 x += title->get_w() + xs10;
72 x = x1; // recover x of the "MaxSlider" to align the "GammaSlider"
73 add_tool(gamma_slider = new GammaSlider(client,
78 x += gamma_slider->get_w() + xs10;
79 add_tool(gamma_text = new GammaText(client,
84 y += gamma_text->get_h() + ys10;
87 add_tool(plot = new GammaPlot(client, x, y));
88 y += plot->get_h() + ys10;
90 add_tool(new GammaColorPicker(client, this, x, y));
92 add_tool(reset = new GammaReset(client, this, get_w()-xs110, y));
98 void GammaWindow::update()
100 max_slider->update(client->config.max);
101 max_text->update(client->config.max);
102 gamma_slider->update(client->config.gamma);
103 gamma_text->update(client->config.gamma);
104 automatic->update(client->config.automatic);
105 plot->update(client->config.plot);
109 void GammaWindow::update_histogram()
111 histogram->clear_box(0, 0, histogram->get_w(), histogram->get_h());
115 histogram->set_color(MEGREY);
116 for(int i = 0; i < histogram->get_w(); i++)
118 int x1 = (int64_t)i * HISTOGRAM_SIZE / histogram->get_w();
119 int x2 = (int64_t)(i + 1) * HISTOGRAM_SIZE / histogram->get_w();
122 for(int x = x1; x < x2; x++)
124 accum += client->engine->accum[x];
126 if(accum > max) max = accum;
128 for(int i = 0; i < histogram->get_w(); i++)
130 int x1 = (int64_t)i * HISTOGRAM_SIZE / histogram->get_w();
131 int x2 = (int64_t)(i + 1) * HISTOGRAM_SIZE / histogram->get_w();
134 for(int x = x1; x < x2; x++)
136 accum += client->engine->accum[x];
139 int h = (int)(log(accum) / log(max) * histogram->get_h());
140 histogram->draw_line(i,
143 histogram->get_h() - h);
147 histogram->set_color(GREEN);
148 int y1 = histogram->get_h();
149 float max = client->config.max * client->config.gamma;
150 float scale = 1.0 / max;
151 float gamma = client->config.gamma - 1.0;
152 for(int i = 1; i < histogram->get_w(); i++)
154 float in = (float)i / histogram->get_w();
155 float out = in * (scale * pow(in * 2 / max, gamma));
156 int y2 = (int)(histogram->get_h() - out * histogram->get_h());
157 histogram->draw_line(i - 1, y1, i, y2);
166 MaxSlider::MaxSlider(GammaMain *client,
180 this->client = client;
185 int MaxSlider::handle_event()
187 client->config.max = get_value();
188 gui->max_text->update(client->config.max);
189 gui->update_histogram();
190 client->send_configure_change();
194 MaxText::MaxText(GammaMain *client,
199 : BC_TextBox(x, y, w, 1, client->config.max)
201 this->client = client;
205 int MaxText::handle_event()
207 client->config.max = atof(get_text());
208 gui->max_slider->update(client->config.max);
209 client->send_configure_change();
213 GammaSlider::GammaSlider(GammaMain *client,
225 client->config.gamma)
227 this->client = client;
232 int GammaSlider::handle_event()
234 client->config.gamma = get_value();
235 gui->gamma_text->update(client->config.gamma);
236 gui->update_histogram();
237 client->send_configure_change();
241 GammaText::GammaText(GammaMain *client,
246 : BC_TextBox(x, y, w, 1, client->config.gamma)
248 this->client = client;
252 int GammaText::handle_event()
254 client->config.gamma = atof(get_text());
255 gui->gamma_slider->update(client->config.gamma);
256 client->send_configure_change();
260 GammaAuto::GammaAuto(GammaMain *client, int x, int y)
263 client->config.automatic,
266 this->plugin = client;
269 int GammaAuto::handle_event()
271 plugin->config.automatic = get_value();
272 plugin->send_configure_change();
277 GammaPlot::GammaPlot(GammaMain *plugin, int x, int y)
278 : BC_CheckBox(x, y, plugin->config.plot, _("Plot histogram"))
280 this->plugin = plugin;
282 int GammaPlot::handle_event()
284 plugin->config.plot = get_value();
285 plugin->send_configure_change();
290 GammaColorPicker::GammaColorPicker(GammaMain *plugin,
294 : BC_GenericButton(x, y, _("Use Color Picker"))
296 this->plugin = plugin;
300 int GammaColorPicker::handle_event()
302 // Get colorpicker value
303 float red = plugin->get_red();
304 float green = plugin->get_green();
305 float blue = plugin->get_blue();
307 plugin->config.max = MAX(red, green);
308 plugin->config.max = MAX(plugin->config.max, blue);
309 gui->max_text->update(plugin->config.max);
310 gui->max_slider->update(plugin->config.max);
311 plugin->send_configure_change();
316 GammaReset::GammaReset(GammaMain *plugin, GammaWindow *gui, int x, int y)
317 : BC_GenericButton(x, y, _("Reset"))
319 this->plugin = plugin;
323 int GammaReset::handle_event()
325 plugin->config.reset();
327 plugin->send_configure_change();