Fix and simplify histogram code, drop histogram based on mean value.
[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 const gchar *histogram_label(Histogram *histogram)
101 {
102         const gchar *t1 = "";
103         
104         if (!histogram) return NULL;
105
106         if (histogram->log_mode)
107                 switch (histogram->channel_mode)
108                         {
109                         case HCHAN_R:   t1 = _("logarithmical histogram on red"); break;
110                         case HCHAN_G:   t1 = _("logarithmical histogram on green"); break;
111                         case HCHAN_B:   t1 = _("logarithmical histogram on blue"); break;
112                         case HCHAN_RGB: t1 = _("logarithmical histogram on RGB"); break;
113                         case HCHAN_MAX: t1 = _("logarithmical histogram on max value"); break;
114                         }
115         else
116                 switch (histogram->channel_mode)
117                         {
118                         case HCHAN_R:   t1 = _("linear histogram on red"); break;
119                         case HCHAN_G:   t1 = _("linear histogram on green"); break;
120                         case HCHAN_B:   t1 = _("linear histogram on blue"); break;
121                         case HCHAN_RGB: t1 = _("linear histogram on RGB"); break;
122                         case HCHAN_MAX: t1 = _("linear histogram on max value"); break;
123                         }
124         return t1;
125 }
126
127 static HistMap *histmap_read(GdkPixbuf *imgpixbuf)
128 {
129         gint w, h, i, j, srs, has_alpha, step;
130         guchar *s_pix;
131         HistMap *histmap;
132         
133         w = gdk_pixbuf_get_width(imgpixbuf);
134         h = gdk_pixbuf_get_height(imgpixbuf);
135         srs = gdk_pixbuf_get_rowstride(imgpixbuf);
136         s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
137         has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
138
139         histmap = g_new0(HistMap, 1);
140
141         step = 3 + !!(has_alpha);
142         for (i = 0; i < h; i++)
143                 {
144                 guchar *sp = s_pix + (i * srs); /* 8bit */
145                 for (j = 0; j < w; j++)
146                         {
147                         guint max = sp[0];
148                         if (sp[1] > max) max = sp[1];
149                         if (sp[2] > max) max = sp[2];
150                 
151                         histmap->r[sp[0]]++;
152                         histmap->g[sp[1]]++;
153                         histmap->b[sp[2]]++;
154                         histmap->max[max]++;
155
156                         sp += step;
157                         }
158                 }
159         
160         return histmap;
161 }
162
163 const HistMap *histmap_get(FileData *fd)
164 {
165         if (fd->histmap) return fd->histmap;
166         
167         if (fd->pixbuf)
168                 {
169                 fd->histmap = histmap_read(fd->pixbuf);
170                 return fd->histmap;
171                 }
172         return NULL;
173 }
174
175 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
176 {
177         guint i;
178         float add;
179         
180         if (histogram->vgrid == 0) return;
181
182         add = width / (float)histogram->vgrid;
183
184         for (i = 1; i < histogram->vgrid; i++)
185                 {
186                 gint xpos = x + (int)(i * add + 0.5);
187
188                 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
189                                  histogram->grid_color.R,
190                                  histogram->grid_color.G,
191                                  histogram->grid_color.B,
192                                  histogram->grid_color.A);
193                 }
194 }
195
196 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
197 {
198         guint i;
199         float add;
200         
201         if (histogram->hgrid == 0) return;
202
203         add = height / (float)histogram->hgrid;
204
205         for (i = 1; i < histogram->hgrid; i++)
206                 {
207                 gint ypos = y + (int)(i * add + 0.5);
208         
209                 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
210                                  histogram->grid_color.R,
211                                  histogram->grid_color.G,
212                                  histogram->grid_color.B,
213                                  histogram->grid_color.A);
214                 }
215 }
216
217 gint histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
218 {
219         /* FIXME: use the coordinates correctly */
220         gint i;
221         gulong max = 0;
222         gdouble logmax;
223         gint combine = (HISTMAP_SIZE - 1) / width + 1;
224         gint ypos = y + height;
225         
226         if (!histogram || !histmap) return 0;
227         
228         /* Draw the grid */
229         histogram_vgrid(histogram, pixbuf, x, y, width, height);
230         histogram_hgrid(histogram, pixbuf, x, y, width, height);
231
232         for (i = 0; i < HISTMAP_SIZE; i++)
233                 {
234                 if (histmap->r[i] > max) max = histmap->r[i];
235                 if (histmap->g[i] > max) max = histmap->g[i];
236                 if (histmap->b[i] > max) max = histmap->b[i];
237                 }
238
239         if (max > 0)
240                 logmax = log(max);
241         else
242                 logmax = 1.0;
243
244         for (i = 0; i < width; i++)
245                 {
246                 gint j;
247                 glong v[4] = {0, 0, 0, 0};
248                 gint rplus = 0;
249                 gint gplus = 0;
250                 gint bplus = 0;
251                 gint ii = i * HISTMAP_SIZE / width;
252                 gint xpos = x + i;
253
254                 for (j = 0; j < combine; j++)
255                         {
256                         guint p = ii + j;
257                         v[0] += histmap->r[p];
258                         v[1] += histmap->g[p];
259                         v[2] += histmap->b[p];
260                         v[3] += histmap->max[p];
261                         }
262         
263                 for (j = 0; combine > 1 && j < 4; j++)
264                         v[j] /= combine;
265                 
266                 for (j = 0; j < 4; j++)
267                         {
268                         gint k;
269                         gint chanmax = 0;
270                 
271                         for (k = 1; k < 3; k++)
272                                 if (v[k] > v[chanmax])
273                                         chanmax = k;
274                                 
275                         if (histogram->channel_mode >= HCHAN_RGB
276                             || chanmax == histogram->channel_mode)
277                                 {
278                                 gulong pt;
279                                 gint r = rplus;
280                                 gint g = gplus;
281                                 gint b = bplus;
282
283                                 switch (chanmax)
284                                         {
285                                         case 0: rplus = r = 255; break;
286                                         case 1: gplus = g = 255; break;
287                                         case 2: bplus = b = 255; break;
288                                         }
289
290                                 switch (histogram->channel_mode)
291                                         {
292                                         case HCHAN_RGB:
293                                                 if (r == 255 && g == 255 && b == 255)
294                                                         {
295                                                         r = 0; b = 0; g = 0;
296                                                         }
297                                                 break;
298                                         case HCHAN_R:     b = 0; g = 0; break;
299                                         case HCHAN_G:   r = 0; b = 0;   break;
300                                         case HCHAN_B:   r = 0;  g = 0; break;
301                                         case HCHAN_MAX: r = 0; b = 0; g = 0; break;
302                                         }
303                                 
304                                 if (v[chanmax] == 0)
305                                         pt = 0;
306                                 else if (histogram->log_mode)
307                                         pt = ((gdouble)log(v[chanmax])) / logmax * (height - 1);
308                                 else
309                                         pt = ((gdouble)v[chanmax]) / max * (height - 1);
310
311                                 pixbuf_draw_line(pixbuf,
312                                         x, y, width, height,
313                                         xpos, ypos, xpos, ypos - pt,
314                                         r, g, b, 255);
315                                 }
316
317                         v[chanmax] = -1;
318                         }
319                 }
320
321         return TRUE;
322 }
323
324 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
325 {
326         if (type != NOTIFY_TYPE_INTERNAL && fd->histmap)
327                 {
328                 g_free(fd->histmap);
329                 fd->histmap = NULL;
330                 }
331 }
332
333 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */