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