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