Remove ToolbarButtonData
[geeqie.git] / src / histogram.cc
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Author: Vladimir Nadvornik
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 along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "histogram.h"
22
23 #include <algorithm>
24 #include <cmath>
25
26 #include <glib-object.h>
27
28 #include "debug.h"
29 #include "filedata.h"
30 #include "intl.h"
31 #include "pixbuf-util.h"
32
33 /*
34  *----------------------------------------------------------------------------
35  * image histogram
36  *----------------------------------------------------------------------------
37  */
38
39 namespace
40 {
41
42 constexpr gint HISTMAP_SIZE = 256;
43
44 } // namespace
45
46 struct HistMap {
47         gulong r[HISTMAP_SIZE];
48         gulong g[HISTMAP_SIZE];
49         gulong b[HISTMAP_SIZE];
50         gulong max[HISTMAP_SIZE];
51
52         guint idle_id; /* event source id */
53         GdkPixbuf *pixbuf;
54         gint y;
55 };
56
57
58 Histogram *histogram_new()
59 {
60         Histogram *histogram;
61
62         histogram = g_new0(Histogram, 1);
63         histogram->histogram_channel = HCHAN_DEFAULT;
64         histogram->histogram_mode = 0;
65
66         /* grid */
67         histogram->vgrid = 5;
68         histogram->hgrid = 3;
69         histogram->grid_color.R = 160;
70         histogram->grid_color.G = 160;
71         histogram->grid_color.B = 160;
72         histogram->grid_color.A = 250;
73
74         return histogram;
75 }
76
77 void histogram_free(Histogram *histogram)
78 {
79         g_free(histogram);
80 }
81
82
83 gint histogram_set_channel(Histogram *histogram, gint chan)
84 {
85         if (!histogram) return 0;
86         histogram->histogram_channel = chan;
87         return chan;
88 }
89
90 gint histogram_get_channel(Histogram *histogram)
91 {
92         if (!histogram) return 0;
93         return histogram->histogram_channel;
94 }
95
96 gint histogram_set_mode(Histogram *histogram, gint mode)
97 {
98         if (!histogram) return 0;
99         histogram->histogram_mode = mode;
100         return mode;
101 }
102
103 gint histogram_get_mode(Histogram *histogram)
104 {
105         if (!histogram) return 0;
106         return histogram->histogram_mode;
107 }
108
109 gint histogram_toggle_channel(Histogram *histogram)
110 {
111         if (!histogram) return 0;
112         return histogram_set_channel(histogram, (histogram_get_channel(histogram)+1)%HCHAN_COUNT);
113 }
114
115 gint histogram_toggle_mode(Histogram *histogram)
116 {
117         if (!histogram) return 0;
118         return histogram_set_mode(histogram, !histogram_get_mode(histogram));
119 }
120
121 const gchar *histogram_label(Histogram *histogram)
122 {
123         const gchar *t1 = "";
124
125         if (!histogram) return nullptr;
126
127         if (histogram->histogram_mode)
128                 switch (histogram->histogram_channel)
129                         {
130                         case HCHAN_R:   t1 = _("Log Histogram on Red"); break;
131                         case HCHAN_G:   t1 = _("Log Histogram on Green"); break;
132                         case HCHAN_B:   t1 = _("Log Histogram on Blue"); break;
133                         case HCHAN_RGB: t1 = _("Log Histogram on RGB"); break;
134                         case HCHAN_MAX: t1 = _("Log Histogram on value"); break;
135                         }
136         else
137                 switch (histogram->histogram_channel)
138                         {
139                         case HCHAN_R:   t1 = _("Linear Histogram on Red"); break;
140                         case HCHAN_G:   t1 = _("Linear Histogram on Green"); break;
141                         case HCHAN_B:   t1 = _("Linear Histogram on Blue"); break;
142                         case HCHAN_RGB: t1 = _("Linear Histogram on RGB"); break;
143                         case HCHAN_MAX: t1 = _("Linear Histogram on value"); break;
144                         }
145         return t1;
146 }
147
148 static HistMap *histmap_new()
149 {
150         auto histmap = g_new0(HistMap, 1);
151         return histmap;
152 }
153
154 void histmap_free(HistMap *histmap)
155 {
156         if (!histmap) return;
157         if (histmap->idle_id) g_source_remove(histmap->idle_id);
158         if (histmap->pixbuf) g_object_unref(histmap->pixbuf);
159         g_free(histmap);
160 }
161
162 static gboolean histmap_read(HistMap *histmap, gboolean whole)
163 {
164         gint w;
165         gint h;
166         gint i;
167         gint j;
168         gint srs;
169         gint has_alpha;
170         gint step;
171         gint end_line;
172         guchar *s_pix;
173         GdkPixbuf *imgpixbuf = histmap->pixbuf;
174
175         w = gdk_pixbuf_get_width(imgpixbuf);
176         h = gdk_pixbuf_get_height(imgpixbuf);
177         srs = gdk_pixbuf_get_rowstride(imgpixbuf);
178         s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
179         has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
180
181         if (whole)
182                 {
183                 end_line = h;
184                 }
185         else
186                 {
187                 gint lines = 1 + 16384 / w;
188                 end_line = histmap->y + lines;
189                 if (end_line > h) end_line = h;
190                 }
191
192         step = 3 + !!(has_alpha);
193         for (i = histmap->y; i < end_line; i++)
194                 {
195                 guchar *sp = s_pix + (i * srs); /* 8bit */
196                 for (j = 0; j < w; j++)
197                         {
198                         guint max = std::max({sp[0], sp[1], sp[2]});
199
200                         histmap->r[sp[0]]++;
201                         histmap->g[sp[1]]++;
202                         histmap->b[sp[2]]++;
203                         histmap->max[max]++;
204
205                         sp += step;
206                         }
207                 }
208         histmap->y = end_line;
209         return end_line >= h;
210 }
211
212 const HistMap *histmap_get(FileData *fd)
213 {
214         if (fd->histmap && !fd->histmap->idle_id) return fd->histmap; /* histmap exists and is finished */
215
216         return nullptr;
217 }
218
219 static gboolean histmap_idle_cb(gpointer data)
220 {
221         auto fd = static_cast<FileData *>(data);
222         if (histmap_read(fd->histmap, FALSE))
223                 {
224                 /* finished */
225                 g_object_unref(fd->histmap->pixbuf); /*pixbuf is no longer needed */
226                 fd->histmap->pixbuf = nullptr;
227                 fd->histmap->idle_id = 0;
228                 file_data_send_notification(fd, NOTIFY_HISTMAP);
229                 return G_SOURCE_REMOVE;
230                 }
231         return G_SOURCE_CONTINUE;
232 }
233
234 gboolean histmap_start_idle(FileData *fd)
235 {
236         if (fd->histmap || !fd->pixbuf) return FALSE;
237
238         fd->histmap = histmap_new();
239         fd->histmap->pixbuf = fd->pixbuf;
240         g_object_ref(fd->histmap->pixbuf);
241
242         fd->histmap->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, histmap_idle_cb, fd, nullptr);
243         return TRUE;
244 }
245
246
247 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
248 {
249         guint i;
250         float add;
251
252         if (histogram->vgrid == 0) return;
253
254         add = width / static_cast<float>(histogram->vgrid);
255
256         for (i = 1; i < histogram->vgrid; i++)
257                 {
258                 gint xpos = x + static_cast<int>(i * add + 0.5);
259
260                 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
261                                  histogram->grid_color.R,
262                                  histogram->grid_color.G,
263                                  histogram->grid_color.B,
264                                  histogram->grid_color.A);
265                 }
266 }
267
268 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
269 {
270         guint i;
271         float add;
272
273         if (histogram->hgrid == 0) return;
274
275         add = height / static_cast<float>(histogram->hgrid);
276
277         for (i = 1; i < histogram->hgrid; i++)
278                 {
279                 gint ypos = y + static_cast<int>(i * add + 0.5);
280
281                 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
282                                  histogram->grid_color.R,
283                                  histogram->grid_color.G,
284                                  histogram->grid_color.B,
285                                  histogram->grid_color.A);
286                 }
287 }
288
289 gboolean histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
290 {
291         /** @FIXME use the coordinates correctly */
292         gint i;
293         gulong max = 0;
294         gdouble logmax;
295         gint combine = (HISTMAP_SIZE - 1) / width + 1;
296         gint ypos = y + height;
297
298         if (!histogram || !histmap) return FALSE;
299
300         /* Draw the grid */
301         histogram_vgrid(histogram, pixbuf, x, y, width, height);
302         histogram_hgrid(histogram, pixbuf, x, y, width, height);
303
304         /* exclude overexposed and underexposed */
305         for (i = 1; i < HISTMAP_SIZE - 1; i++)
306                 {
307                 max = std::max({histmap->r[i], histmap->g[i], histmap->b[i], histmap->max[i], max});
308                 }
309
310         if (max > 0)
311                 logmax = log(max);
312         else
313                 logmax = 1.0;
314
315         for (i = 0; i < width; i++)
316                 {
317                 gint j;
318                 glong v[4] = {0, 0, 0, 0};
319                 gint rplus = 0;
320                 gint gplus = 0;
321                 gint bplus = 0;
322                 gint ii = i * HISTMAP_SIZE / width;
323                 gint xpos = x + i;
324                 gint num_chan;
325
326                 for (j = 0; j < combine; j++)
327                         {
328                         guint p = ii + j;
329                         v[0] += histmap->r[p];
330                         v[1] += histmap->g[p];
331                         v[2] += histmap->b[p];
332                         v[3] += histmap->max[p];
333                         }
334
335                 for (j = 0; combine > 1 && j < 4; j++)
336                         v[j] /= combine;
337
338                 num_chan = (histogram->histogram_channel == HCHAN_RGB) ? 3 : 1;
339                 for (j = 0; j < num_chan; j++)
340                         {
341                         gint chanmax;
342                         if (histogram->histogram_channel == HCHAN_RGB)
343                                 {
344                                 chanmax = HCHAN_R;
345                                 if (v[HCHAN_G] > v[HCHAN_R]) chanmax = HCHAN_G;
346                                 if (v[HCHAN_B] > v[chanmax]) chanmax = HCHAN_B;
347                                 }
348                         else
349                                 {
350                                 chanmax = histogram->histogram_channel;
351                                 }
352
353                                 {
354                                 gulong pt;
355                                 gint r = rplus;
356                                 gint g = gplus;
357                                 gint b = bplus;
358
359                                 switch (chanmax)
360                                         {
361                                         case HCHAN_R: rplus = r = 255; break;
362                                         case HCHAN_G: gplus = g = 255; break;
363                                         case HCHAN_B: bplus = b = 255; break;
364                                         }
365
366                                 switch (histogram->histogram_channel)
367                                         {
368                                         case HCHAN_RGB:
369                                                 if (r == 255 && g == 255 && b == 255)
370                                                         {
371                                                         r = 0;  b = 0;  g = 0;
372                                                         }
373                                                 break;
374                                         case HCHAN_R:           b = 0;  g = 0;  break;
375                                         case HCHAN_G:   r = 0;  b = 0;          break;
376                                         case HCHAN_B:   r = 0;          g = 0;  break;
377                                         case HCHAN_MAX: r = 0;  b = 0;  g = 0;  break;
378                                         }
379
380                                 if (v[chanmax] == 0)
381                                         pt = 0;
382                                 else if (histogram->histogram_mode)
383                                         pt = (static_cast<gdouble>(log(v[chanmax]))) / logmax * (height - 1);
384                                 else
385                                         pt = (static_cast<gdouble>(v[chanmax])) / max * (height - 1);
386
387                                 pixbuf_draw_line(pixbuf,
388                                         x, y, width, height,
389                                         xpos, ypos, xpos, ypos - pt,
390                                         r, g, b, 255);
391                                 }
392
393                         v[chanmax] = -1;
394                         }
395                 }
396
397         return TRUE;
398 }
399
400 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer)
401 {
402         if ((type & NOTIFY_REREAD) && fd->histmap)
403                 {
404                 DEBUG_1("Notify histogram: %s %04x", fd->path, type);
405                 histmap_free(fd->histmap);
406                 fd->histmap = nullptr;
407                 }
408 }
409
410 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */