clang-tidy: modernize-macro-to-enum
[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, h, i, j, srs, has_alpha, step, end_line;
158         guchar *s_pix;
159         GdkPixbuf *imgpixbuf = histmap->pixbuf;
160
161         w = gdk_pixbuf_get_width(imgpixbuf);
162         h = gdk_pixbuf_get_height(imgpixbuf);
163         srs = gdk_pixbuf_get_rowstride(imgpixbuf);
164         s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
165         has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
166
167         if (whole)
168                 {
169                 end_line = h;
170                 }
171         else
172                 {
173                 gint lines = 1 + 16384 / w;
174                 end_line = histmap->y + lines;
175                 if (end_line > h) end_line = h;
176                 }
177
178         step = 3 + !!(has_alpha);
179         for (i = histmap->y; i < end_line; i++)
180                 {
181                 guchar *sp = s_pix + (i * srs); /* 8bit */
182                 for (j = 0; j < w; j++)
183                         {
184                         guint max = sp[0];
185                         if (sp[1] > max) max = sp[1];
186                         if (sp[2] > max) max = sp[2];
187
188                         histmap->r[sp[0]]++;
189                         histmap->g[sp[1]]++;
190                         histmap->b[sp[2]]++;
191                         histmap->max[max]++;
192
193                         sp += step;
194                         }
195                 }
196         histmap->y = end_line;
197         return end_line >= h;
198 }
199
200 const HistMap *histmap_get(FileData *fd)
201 {
202         if (fd->histmap && !fd->histmap->idle_id) return fd->histmap; /* histmap exists and is finished */
203
204         return nullptr;
205 }
206
207 static gboolean histmap_idle_cb(gpointer data)
208 {
209         auto fd = static_cast<FileData *>(data);
210         if (histmap_read(fd->histmap, FALSE))
211                 {
212                 /* finished */
213                 g_object_unref(fd->histmap->pixbuf); /*pixbuf is no longer needed */
214                 fd->histmap->pixbuf = nullptr;
215                 fd->histmap->idle_id = 0;
216                 file_data_send_notification(fd, NOTIFY_HISTMAP);
217                 return G_SOURCE_REMOVE;
218                 }
219         return G_SOURCE_CONTINUE;
220 }
221
222 gboolean histmap_start_idle(FileData *fd)
223 {
224         if (fd->histmap || !fd->pixbuf) return FALSE;
225
226         fd->histmap = histmap_new();
227         fd->histmap->pixbuf = fd->pixbuf;
228         g_object_ref(fd->histmap->pixbuf);
229
230         fd->histmap->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, histmap_idle_cb, fd, nullptr);
231         return TRUE;
232 }
233
234
235 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
236 {
237         guint i;
238         float add;
239
240         if (histogram->vgrid == 0) return;
241
242         add = width / static_cast<float>(histogram->vgrid);
243
244         for (i = 1; i < histogram->vgrid; i++)
245                 {
246                 gint xpos = x + static_cast<int>(i * add + 0.5);
247
248                 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
249                                  histogram->grid_color.R,
250                                  histogram->grid_color.G,
251                                  histogram->grid_color.B,
252                                  histogram->grid_color.A);
253                 }
254 }
255
256 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
257 {
258         guint i;
259         float add;
260
261         if (histogram->hgrid == 0) return;
262
263         add = height / static_cast<float>(histogram->hgrid);
264
265         for (i = 1; i < histogram->hgrid; i++)
266                 {
267                 gint ypos = y + static_cast<int>(i * add + 0.5);
268
269                 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
270                                  histogram->grid_color.R,
271                                  histogram->grid_color.G,
272                                  histogram->grid_color.B,
273                                  histogram->grid_color.A);
274                 }
275 }
276
277 gboolean histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
278 {
279         /** @FIXME use the coordinates correctly */
280         gint i;
281         gulong max = 0;
282         gdouble logmax;
283         gint combine = (HISTMAP_SIZE - 1) / width + 1;
284         gint ypos = y + height;
285
286         if (!histogram || !histmap) return FALSE;
287
288         /* Draw the grid */
289         histogram_vgrid(histogram, pixbuf, x, y, width, height);
290         histogram_hgrid(histogram, pixbuf, x, y, width, height);
291
292         /* exclude overexposed and underexposed */
293         for (i = 1; i < HISTMAP_SIZE - 1; i++)
294                 {
295                 if (histmap->r[i] > max) max = histmap->r[i];
296                 if (histmap->g[i] > max) max = histmap->g[i];
297                 if (histmap->b[i] > max) max = histmap->b[i];
298                 if (histmap->max[i] > max) max = histmap->max[i];
299                 }
300
301         if (max > 0)
302                 logmax = log(max);
303         else
304                 logmax = 1.0;
305
306         for (i = 0; i < width; i++)
307                 {
308                 gint j;
309                 glong v[4] = {0, 0, 0, 0};
310                 gint rplus = 0;
311                 gint gplus = 0;
312                 gint bplus = 0;
313                 gint ii = i * HISTMAP_SIZE / width;
314                 gint xpos = x + i;
315                 gint num_chan;
316
317                 for (j = 0; j < combine; j++)
318                         {
319                         guint p = ii + j;
320                         v[0] += histmap->r[p];
321                         v[1] += histmap->g[p];
322                         v[2] += histmap->b[p];
323                         v[3] += histmap->max[p];
324                         }
325
326                 for (j = 0; combine > 1 && j < 4; j++)
327                         v[j] /= combine;
328
329                 num_chan = (histogram->histogram_channel == HCHAN_RGB) ? 3 : 1;
330                 for (j = 0; j < num_chan; j++)
331                         {
332                         gint chanmax;
333                         if (histogram->histogram_channel == HCHAN_RGB)
334                                 {
335                                 chanmax = HCHAN_R;
336                                 if (v[HCHAN_G] > v[HCHAN_R]) chanmax = HCHAN_G;
337                                 if (v[HCHAN_B] > v[chanmax]) chanmax = HCHAN_B;
338                                 }
339                         else
340                                 {
341                                 chanmax = histogram->histogram_channel;
342                                 }
343
344                                 {
345                                 gulong pt;
346                                 gint r = rplus;
347                                 gint g = gplus;
348                                 gint b = bplus;
349
350                                 switch (chanmax)
351                                         {
352                                         case HCHAN_R: rplus = r = 255; break;
353                                         case HCHAN_G: gplus = g = 255; break;
354                                         case HCHAN_B: bplus = b = 255; break;
355                                         }
356
357                                 switch (histogram->histogram_channel)
358                                         {
359                                         case HCHAN_RGB:
360                                                 if (r == 255 && g == 255 && b == 255)
361                                                         {
362                                                         r = 0;  b = 0;  g = 0;
363                                                         }
364                                                 break;
365                                         case HCHAN_R:           b = 0;  g = 0;  break;
366                                         case HCHAN_G:   r = 0;  b = 0;          break;
367                                         case HCHAN_B:   r = 0;          g = 0;  break;
368                                         case HCHAN_MAX: r = 0;  b = 0;  g = 0;  break;
369                                         }
370
371                                 if (v[chanmax] == 0)
372                                         pt = 0;
373                                 else if (histogram->histogram_mode)
374                                         pt = (static_cast<gdouble>(log(v[chanmax]))) / logmax * (height - 1);
375                                 else
376                                         pt = (static_cast<gdouble>(v[chanmax])) / max * (height - 1);
377
378                                 pixbuf_draw_line(pixbuf,
379                                         x, y, width, height,
380                                         xpos, ypos, xpos, ypos - pt,
381                                         r, g, b, 255);
382                                 }
383
384                         v[chanmax] = -1;
385                         }
386                 }
387
388         return TRUE;
389 }
390
391 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer)
392 {
393         if ((type & NOTIFY_REREAD) && fd->histmap)
394                 {
395                 DEBUG_1("Notify histogram: %s %04x", fd->path, type);
396                 histmap_free(fd->histmap);
397                 fd->histmap = nullptr;
398                 }
399 }
400
401 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */