Introduce helpers histogram_toggle_channel() and histogram_toggle_mode().
[geeqie.git] / src / histogram.c
1 /*
2  * Geeqie
3  * Copyright (C) 2008 - 2009 The Geeqie Team
4  *
5  * Author: Vladimir Nadvornik
6  * based on a patch by Uwe Ohse
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 #include "main.h"
14 #include "histogram.h"
15
16 #include "pixbuf_util.h"
17
18 #include <math.h>
19
20 /*
21  *----------------------------------------------------------------------------
22  * image histogram
23  *----------------------------------------------------------------------------
24  */
25
26 #define HISTMAP_SIZE 256
27
28 struct _HistMap {
29         gulong r[HISTMAP_SIZE];
30         gulong g[HISTMAP_SIZE];
31         gulong b[HISTMAP_SIZE];
32         gulong max[HISTMAP_SIZE];
33 };
34
35 struct _Histogram {
36         gint channel_mode; /* drawing mode for histogram */
37         gint log_mode;     /* logarithmical or not */
38         guint vgrid; /* number of vertical divisions, 0 for none */
39         guint hgrid; /* number of horizontal divisions, 0 for none */
40         struct {
41                 int R; /* red */
42                 int G; /* green */
43                 int B; /* blue */
44                 int A; /* alpha */
45         } grid_color;  /* grid color */
46
47 };
48
49 Histogram *histogram_new(void)
50 {
51         Histogram *histogram;
52
53         histogram = g_new0(Histogram, 1);
54         histogram->channel_mode = options->histogram.last_channel_mode;
55         histogram->log_mode = options->histogram.last_log_mode;
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         options->histogram.last_channel_mode = histogram->channel_mode = chan;
78         return chan;
79 }
80
81 gint histogram_get_channel(Histogram *histogram)
82 {
83         if (!histogram) return 0;
84         return histogram->channel_mode;
85 }
86
87 gint histogram_set_mode(Histogram *histogram, gint mode)
88 {
89         if (!histogram) return 0;
90         options->histogram.last_log_mode = histogram->log_mode = mode;
91         return mode;
92 }
93
94 gint histogram_get_mode(Histogram *histogram)
95 {
96         if (!histogram) return 0;
97         return histogram->log_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->log_mode)
119                 switch (histogram->channel_mode)
120                         {
121                         case HCHAN_R:   t1 = _("logarithmical histogram on red"); break;
122                         case HCHAN_G:   t1 = _("logarithmical histogram on green"); break;
123                         case HCHAN_B:   t1 = _("logarithmical histogram on blue"); break;
124                         case HCHAN_RGB: t1 = _("logarithmical histogram on RGB"); break;
125                         case HCHAN_MAX: t1 = _("logarithmical histogram on max value"); break;
126                         }
127         else
128                 switch (histogram->channel_mode)
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 max value"); break;
135                         }
136         return t1;
137 }
138
139 static HistMap *histmap_read(GdkPixbuf *imgpixbuf)
140 {
141         gint w, h, i, j, srs, has_alpha, step;
142         guchar *s_pix;
143         HistMap *histmap;
144         
145         w = gdk_pixbuf_get_width(imgpixbuf);
146         h = gdk_pixbuf_get_height(imgpixbuf);
147         srs = gdk_pixbuf_get_rowstride(imgpixbuf);
148         s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
149         has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
150
151         histmap = g_new0(HistMap, 1);
152
153         step = 3 + !!(has_alpha);
154         for (i = 0; i < h; i++)
155                 {
156                 guchar *sp = s_pix + (i * srs); /* 8bit */
157                 for (j = 0; j < w; j++)
158                         {
159                         guint max = sp[0];
160                         if (sp[1] > max) max = sp[1];
161                         if (sp[2] > max) max = sp[2];
162                 
163                         histmap->r[sp[0]]++;
164                         histmap->g[sp[1]]++;
165                         histmap->b[sp[2]]++;
166                         histmap->max[max]++;
167
168                         sp += step;
169                         }
170                 }
171         
172         return histmap;
173 }
174
175 const HistMap *histmap_get(FileData *fd)
176 {
177         if (fd->histmap) return fd->histmap;
178         
179         if (fd->pixbuf)
180                 {
181                 fd->histmap = histmap_read(fd->pixbuf);
182                 return fd->histmap;
183                 }
184         return NULL;
185 }
186
187 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
188 {
189         guint i;
190         float add;
191         
192         if (histogram->vgrid == 0) return;
193
194         add = width / (float)histogram->vgrid;
195
196         for (i = 1; i < histogram->vgrid; i++)
197                 {
198                 gint xpos = x + (int)(i * add + 0.5);
199
200                 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
201                                  histogram->grid_color.R,
202                                  histogram->grid_color.G,
203                                  histogram->grid_color.B,
204                                  histogram->grid_color.A);
205                 }
206 }
207
208 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
209 {
210         guint i;
211         float add;
212         
213         if (histogram->hgrid == 0) return;
214
215         add = height / (float)histogram->hgrid;
216
217         for (i = 1; i < histogram->hgrid; i++)
218                 {
219                 gint ypos = y + (int)(i * add + 0.5);
220         
221                 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
222                                  histogram->grid_color.R,
223                                  histogram->grid_color.G,
224                                  histogram->grid_color.B,
225                                  histogram->grid_color.A);
226                 }
227 }
228
229 gint histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
230 {
231         /* FIXME: use the coordinates correctly */
232         gint i;
233         gulong max = 0;
234         gdouble logmax;
235         gint combine = (HISTMAP_SIZE - 1) / width + 1;
236         gint ypos = y + height;
237         
238         if (!histogram || !histmap) return 0;
239         
240         /* Draw the grid */
241         histogram_vgrid(histogram, pixbuf, x, y, width, height);
242         histogram_hgrid(histogram, pixbuf, x, y, width, height);
243
244         for (i = 0; i < HISTMAP_SIZE; i++)
245                 {
246                 if (histmap->r[i] > max) max = histmap->r[i];
247                 if (histmap->g[i] > max) max = histmap->g[i];
248                 if (histmap->b[i] > max) max = histmap->b[i];
249                 }
250
251         if (max > 0)
252                 logmax = log(max);
253         else
254                 logmax = 1.0;
255
256         for (i = 0; i < width; i++)
257                 {
258                 gint j;
259                 glong v[4] = {0, 0, 0, 0};
260                 gint rplus = 0;
261                 gint gplus = 0;
262                 gint bplus = 0;
263                 gint ii = i * HISTMAP_SIZE / width;
264                 gint xpos = x + i;
265
266                 for (j = 0; j < combine; j++)
267                         {
268                         guint p = ii + j;
269                         v[0] += histmap->r[p];
270                         v[1] += histmap->g[p];
271                         v[2] += histmap->b[p];
272                         v[3] += histmap->max[p];
273                         }
274         
275                 for (j = 0; combine > 1 && j < 4; j++)
276                         v[j] /= combine;
277                 
278                 for (j = 0; j < 4; j++)
279                         {
280                         gint k;
281                         gint chanmax = 0;
282                 
283                         for (k = 1; k < 3; k++)
284                                 if (v[k] > v[chanmax])
285                                         chanmax = k;
286                                 
287                         if (histogram->channel_mode >= HCHAN_RGB
288                             || chanmax == histogram->channel_mode)
289                                 {
290                                 gulong pt;
291                                 gint r = rplus;
292                                 gint g = gplus;
293                                 gint b = bplus;
294
295                                 switch (chanmax)
296                                         {
297                                         case 0: rplus = r = 255; break;
298                                         case 1: gplus = g = 255; break;
299                                         case 2: bplus = b = 255; break;
300                                         }
301
302                                 switch (histogram->channel_mode)
303                                         {
304                                         case HCHAN_RGB:
305                                                 if (r == 255 && g == 255 && b == 255)
306                                                         {
307                                                         r = 0; b = 0; g = 0;
308                                                         }
309                                                 break;
310                                         case HCHAN_R:     b = 0; g = 0; break;
311                                         case HCHAN_G:   r = 0; b = 0;   break;
312                                         case HCHAN_B:   r = 0;  g = 0; break;
313                                         case HCHAN_MAX: r = 0; b = 0; g = 0; break;
314                                         }
315                                 
316                                 if (v[chanmax] == 0)
317                                         pt = 0;
318                                 else if (histogram->log_mode)
319                                         pt = ((gdouble)log(v[chanmax])) / logmax * (height - 1);
320                                 else
321                                         pt = ((gdouble)v[chanmax]) / max * (height - 1);
322
323                                 pixbuf_draw_line(pixbuf,
324                                         x, y, width, height,
325                                         xpos, ypos, xpos, ypos - pt,
326                                         r, g, b, 255);
327                                 }
328
329                         v[chanmax] = -1;
330                         }
331                 }
332
333         return TRUE;
334 }
335
336 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
337 {
338         if (type != NOTIFY_TYPE_INTERNAL && fd->histmap)
339                 {
340                 g_free(fd->histmap);
341                 fd->histmap = NULL;
342                 }
343 }
344
345 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */