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