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