Attempt to simplify and fix histrogram code.
[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 avg[HISTMAP_SIZE];
33         gulong max[HISTMAP_SIZE];
34 };
35
36 struct _Histogram {
37         gint channel_mode; /* drawing mode for histogram */
38         gint log_mode;     /* logarithmical or not */
39         guint vgrid; /* number of vertical divisions, 0 for none */
40         guint hgrid; /* number of horizontal divisions, 0 for none */
41         struct {
42                 int R; /* red */
43                 int G; /* green */
44                 int B; /* blue */
45                 int A; /* alpha */
46         } grid_color;  /* grid color */
47
48 };
49
50 Histogram *histogram_new(void)
51 {
52         Histogram *histogram;
53
54         histogram = g_new0(Histogram, 1);
55         histogram->channel_mode = options->histogram.last_channel_mode;
56         histogram->log_mode = options->histogram.last_log_mode;
57
58         /* grid */
59         histogram->vgrid = 5;
60         histogram->hgrid = 3;
61         histogram->grid_color.R = 160;
62         histogram->grid_color.G = 160;
63         histogram->grid_color.B = 160;
64         histogram->grid_color.A = 250;
65
66         return histogram;
67 }
68
69 void histogram_free(Histogram *histogram)
70 {
71         g_free(histogram);
72 }
73
74
75 gint histogram_set_channel(Histogram *histogram, gint chan)
76 {
77         if (!histogram) return 0;
78         options->histogram.last_channel_mode = histogram->channel_mode = chan;
79         return chan;
80 }
81
82 gint histogram_get_channel(Histogram *histogram)
83 {
84         if (!histogram) return 0;
85         return histogram->channel_mode;
86 }
87
88 gint histogram_set_mode(Histogram *histogram, gint mode)
89 {
90         if (!histogram) return 0;
91         options->histogram.last_log_mode = histogram->log_mode = mode;
92         return mode;
93 }
94
95 gint histogram_get_mode(Histogram *histogram)
96 {
97         if (!histogram) return 0;
98         return histogram->log_mode;
99 }
100
101 const gchar *histogram_label(Histogram *histogram)
102 {
103         const gchar *t1 = "";
104         
105         if (!histogram) return NULL;
106
107         if (histogram->log_mode)
108                 switch (histogram->channel_mode)
109                         {
110                         case HCHAN_R:   t1 = _("logarithmical histogram on red"); break;
111                         case HCHAN_G:   t1 = _("logarithmical histogram on green"); break;
112                         case HCHAN_B:   t1 = _("logarithmical histogram on blue"); break;
113                         case HCHAN_VAL: t1 = _("logarithmical histogram on value"); break;
114                         case HCHAN_RGB: t1 = _("logarithmical histogram on RGB"); break;
115                         case HCHAN_MAX: t1 = _("logarithmical histogram on max value"); break;
116                         }
117         else
118                 switch (histogram->channel_mode)
119                         {
120                         case HCHAN_R:   t1 = _("linear histogram on red"); break;
121                         case HCHAN_G:   t1 = _("linear histogram on green"); break;
122                         case HCHAN_B:   t1 = _("linear histogram on blue"); break;
123                         case HCHAN_VAL: t1 = _("linear histogram on value"); break;
124                         case HCHAN_RGB: t1 = _("linear histogram on RGB"); break;
125                         case HCHAN_MAX: t1 = _("linear histogram on max value"); break;
126                         }
127         return t1;
128 }
129
130 static HistMap *histmap_read(GdkPixbuf *imgpixbuf)
131 {
132         gint w, h, i, j, srs, has_alpha, step;
133         guchar *s_pix;
134
135         HistMap *histmap;
136         
137         w = gdk_pixbuf_get_width(imgpixbuf);
138         h = gdk_pixbuf_get_height(imgpixbuf);
139         srs = gdk_pixbuf_get_rowstride(imgpixbuf);
140         s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
141         has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
142
143         histmap = g_new0(HistMap, 1);
144
145         step = 3 + !!(has_alpha);
146         for (i = 0; i < h; i++)
147                 {
148                 guchar *sp = s_pix + (i * srs); /* 8bit */
149                 for (j = 0; j < w; j++)
150                         {
151                         guint avg = (sp[0] + sp[1] + sp[2]) / 3;
152                         guint max = sp[0];
153                         if (sp[1] > max) max = sp[1];
154                         if (sp[2] > max) max = sp[2];
155         
156                         histmap->r[sp[0]]++;
157                         histmap->g[sp[1]]++;
158                         histmap->b[sp[2]]++;
159                         histmap->avg[avg]++;
160                         histmap->max[max]++;
161                         
162                         sp += step;
163                         }
164                 }
165         
166         return histmap;
167 }
168
169 const HistMap *histmap_get(FileData *fd)
170 {
171         if (fd->histmap) return fd->histmap;
172         
173         if (fd->pixbuf) 
174                 {
175                 fd->histmap = histmap_read(fd->pixbuf);
176                 return fd->histmap;
177                 }
178         return NULL;
179 }
180
181 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
182 {
183         guint i;
184         float add;
185         
186         if (histogram->vgrid == 0) return;
187
188         add = width / (float)histogram->vgrid;
189
190         for (i = 1; i < histogram->vgrid; i++)
191                 {
192                 gint xpos = x + (int)(i * add + 0.5);
193
194                 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
195                                  histogram->grid_color.R,
196                                  histogram->grid_color.G,
197                                  histogram->grid_color.B,
198                                  histogram->grid_color.A);
199                 }
200 }
201
202 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
203 {
204         guint i;
205         float add;
206         
207         if (histogram->hgrid == 0) return;
208
209         add = height / (float)histogram->hgrid;
210
211         for (i = 1; i < histogram->hgrid; i++)
212                 {
213                 gint ypos = y + (int)(i * add + 0.5);
214         
215                 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
216                                  histogram->grid_color.R,
217                                  histogram->grid_color.G,
218                                  histogram->grid_color.B,
219                                  histogram->grid_color.A);
220                 }
221 }
222
223 gint histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
224 {
225         /* FIXME: use the coordinates correctly */
226         gint i;
227         gulong max = 0;
228         gdouble logmax;
229         gint combine = (HISTMAP_SIZE - 1) / width + 1;
230         gint ypos = y + height;
231
232         if (!histogram || !histmap) return 0;
233         
234         /* Draw the grid */
235         histogram_vgrid(histogram, pixbuf, x, y, width, height);
236         histogram_hgrid(histogram, pixbuf, x, y, width, height);
237
238         switch (histogram->channel_mode)
239                 {
240                 case HCHAN_VAL:
241                 case HCHAN_MAX:
242                 case HCHAN_RGB:
243                         for (i = 0; i < HISTMAP_SIZE; i++)
244                                 {
245                                 if (histmap->r[i] > max) max = histmap->r[i];
246                                 if (histmap->g[i] > max) max = histmap->g[i];
247                                 if (histmap->b[i] > max) max = histmap->b[i];
248                                 }
249                         break;
250                 case HCHAN_R: for (i = 0; i < HISTMAP_SIZE; i++) if (histmap->r[i] > max) max = histmap->r[i]; break;
251                 case HCHAN_G: for (i = 0; i < HISTMAP_SIZE; i++) if (histmap->g[i] > max) max = histmap->g[i]; break;
252                 case HCHAN_B: for (i = 0; i < HISTMAP_SIZE; i++) if (histmap->b[i] > max) max = histmap->b[i]; break;
253                 }
254
255         logmax = log(max);
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                         if (histogram->channel_mode == HCHAN_VAL)
273                                 {
274                                 v[3] += histmap->avg[p];
275                                 }
276                         else
277                                 {
278                                 v[3] += histmap->max[p];
279                                 }
280                         }
281
282                 for (j = 0; j < 4; j++)
283                         {
284                         gint k;
285                         gint chanmax = 0;
286                 
287                         for (k = 1; k < 4; k++)
288                                 if (v[k] > v[chanmax]) chanmax = k;
289                                 
290                         if (histogram->channel_mode >= HCHAN_RGB
291                             || chanmax == histogram->channel_mode)
292                                 {
293                                 gulong pt;
294                                 gint r = rplus;
295                                 gint g = gplus;
296                                 gint b = bplus;
297
298                                 switch (chanmax)
299                                         {
300                                         case 0: rplus = r = 255; break;
301                                         case 1: gplus = g = 255; break;
302                                         case 2: bplus = b = 255; break;
303                                         }
304
305                                 switch (histogram->channel_mode)
306                                         {
307                                         case HCHAN_RGB:
308                                                 if (r == 255 && g == 255 && b == 255)
309                                                         {
310                                                         r = 0; b = 0; g = 0;
311                                                         }
312                                                 break;
313                                         case HCHAN_R:          b = 0; g = 0; break;
314                                         case HCHAN_G:   r = 0; b = 0;        break;
315                                         case HCHAN_B:   r = 0;        g = 0; break;
316                                         case HCHAN_MAX:
317                                         case HCHAN_VAL: r = 0; b = 0; g = 0; break;
318                                         }
319                                 
320                                 if (v[chanmax] == 0)
321                                         pt = 0;
322                                 else if (histogram->log_mode)
323                                         pt = ((gdouble)log(v[chanmax])) / logmax * (height - 1);
324                                 else
325                                         pt = ((gdouble)v[chanmax]) / max * (height - 1);
326
327                                 pixbuf_draw_line(pixbuf,
328                                         x, y, width, height,
329                                         xpos, ypos, xpos, ypos - pt,
330                                         r, g, b, 255);
331                                 }
332
333                         v[chanmax] = -1;
334                         }
335                 }
336
337         return TRUE;
338 }
339
340 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
341 {
342         if (type != NOTIFY_TYPE_INTERNAL && fd->histmap)
343                 {
344                 g_free(fd->histmap);
345                 fd->histmap = NULL;
346                 }
347 }
348
349 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */