added histogram pane
[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 typedef enum {
28         HISTMAP_CHANNEL_R = 0,
29         HISTMAP_CHANNEL_G,
30         HISTMAP_CHANNEL_B,
31         HISTMAP_CHANNEL_AVG,
32         HISTMAP_CHANNEL_MAX,
33         HISTMAP_CHANNELS
34 } HistMapChannels;
35
36 struct _HistMap {
37         gulong histmap[HISTMAP_SIZE * HISTMAP_CHANNELS];
38         gulong area;
39 };
40
41 struct _Histogram {
42         gint histogram_chan;
43         gint histogram_logmode;
44 };
45
46
47 Histogram *histogram_new(void)
48 {
49         Histogram *histogram;
50
51         histogram = g_new0(Histogram, 1);
52         histogram->histogram_chan = options->histogram.last_channel_mode;
53         histogram->histogram_logmode = options->histogram.last_log_mode;
54
55         return histogram;
56 }
57
58 void histogram_free(Histogram *histogram)
59 {
60         g_free(histogram);
61 }
62
63
64 gint histogram_set_channel(Histogram *histogram, gint chan)
65 {
66         if (!histogram) return 0;
67         options->histogram.last_channel_mode = histogram->histogram_chan = chan;
68         return chan;
69 }
70
71 gint histogram_get_channel(Histogram *histogram)
72 {
73         if (!histogram) return 0;
74         return histogram->histogram_chan;
75 }
76
77 gint histogram_set_mode(Histogram *histogram, gint mode)
78 {
79         if (!histogram) return 0;
80         options->histogram.last_log_mode = histogram->histogram_logmode = mode;
81         return mode;
82 }
83
84 gint histogram_get_mode(Histogram *histogram)
85 {
86         if (!histogram) return 0;
87         return histogram->histogram_logmode;
88 }
89
90 const gchar *histogram_label(Histogram *histogram)
91 {
92         const gchar *t1 = "";
93         
94         if (!histogram) return NULL;
95
96         if (histogram->histogram_logmode)
97                 switch (histogram->histogram_chan)
98                         {
99                         case HCHAN_R:   t1 = _("logarithmical histogram on red"); break;
100                         case HCHAN_G:   t1 = _("logarithmical histogram on green"); break;
101                         case HCHAN_B:   t1 = _("logarithmical histogram on blue"); break;
102                         case HCHAN_VAL: t1 = _("logarithmical histogram on value"); break;
103                         case HCHAN_RGB: t1 = _("logarithmical histogram on RGB"); break;
104                         case HCHAN_MAX: t1 = _("logarithmical histogram on max value"); break;
105                         }
106         else
107                 switch (histogram->histogram_chan)
108                         {
109                         case HCHAN_R:   t1 = _("linear histogram on red"); break;
110                         case HCHAN_G:   t1 = _("linear histogram on green"); break;
111                         case HCHAN_B:   t1 = _("linear histogram on blue"); break;
112                         case HCHAN_VAL: t1 = _("linear histogram on value"); break;
113                         case HCHAN_RGB: t1 = _("linear histogram on RGB"); break;
114                         case HCHAN_MAX: t1 = _("linear histogram on max value"); break;
115                         }
116         return t1;
117 }
118
119 static HistMap *histmap_read(GdkPixbuf *imgpixbuf)
120 {
121         gint w, h, i, j, srs, has_alpha, step;
122         guchar *s_pix;
123
124         HistMap *histmap;
125         
126         w = gdk_pixbuf_get_width(imgpixbuf);
127         h = gdk_pixbuf_get_height(imgpixbuf);
128         srs = gdk_pixbuf_get_rowstride(imgpixbuf);
129         s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
130         has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
131
132         histmap = g_new0(HistMap, 1);
133
134         step = 3 + !!(has_alpha);
135         for (i = 0; i < h; i++)
136                 {
137                 guchar *sp = s_pix + (i * srs); /* 8bit */
138                 for (j = 0; j < w; j++)
139                         {
140                         guint avg = (sp[0] + sp[1] + sp[2]) / 3;
141                         guint max = sp[0];
142                         if (sp[1] > max) max = sp[1];
143                         if (sp[2] > max) max = sp[2];
144
145                         histmap->histmap[sp[0] * HISTMAP_CHANNELS + HISTMAP_CHANNEL_R]++;
146                         histmap->histmap[sp[1] * HISTMAP_CHANNELS + HISTMAP_CHANNEL_G]++;
147                         histmap->histmap[sp[2] * HISTMAP_CHANNELS + HISTMAP_CHANNEL_B]++;
148                         histmap->histmap[avg   * HISTMAP_CHANNELS + HISTMAP_CHANNEL_AVG]++;
149                         histmap->histmap[max   * HISTMAP_CHANNELS + HISTMAP_CHANNEL_MAX]++;
150                         sp += step;
151                         }
152                 }
153         histmap->area = w * h;
154         return histmap;
155 }
156
157 const HistMap *histmap_get(FileData *fd)
158 {
159         if (fd->histmap) return fd->histmap;
160         
161         if (fd->pixbuf) 
162                 {
163                 fd->histmap = histmap_read(fd->pixbuf);
164                 return fd->histmap;
165                 }
166         return NULL;
167 }
168
169 gint histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
170 {
171         /* FIXME: use the coordinates correctly */
172         gint i;
173         gulong max = 0;
174         gdouble logmax;
175
176         if (!histogram || !histmap) return 0;
177
178         for (i = 0; i < 1024; i++) {
179 #if 0
180                 /* this is probably broken for MAX or VAL mode */
181                 gint flag = 0;
182
183                 switch (histogram->histogram_chan)
184                         {
185                         case HCHAN_RGB: if ((i % HISTMAP_CHANNELS) < 3) flag = 1; break;
186                         case HCHAN_R:   if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_R) flag = 1; break;
187                         case HCHAN_G:   if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_G) flag = 1; break;
188                         case HCHAN_B:   if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_B) flag = 1; break;
189                         case HCHAN_VAL: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_AVG) flag = 1; break;
190                         case HCHAN_MAX: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_MAX) flag = 1; break;
191                         }
192                 if (flag && histmap->histmap[i] > max) max = histmap->histmap[i];
193 #else
194                 if (histmap->histmap[i] > max) max = histmap->histmap[i];
195 #endif
196         }
197
198         logmax = log(max);
199         for (i = 0; i < width; i++)
200                 {
201                 gint j;
202                 glong v[4] = {0, 0, 0, 0};
203                 gint rplus = 0;
204                 gint gplus = 0;
205                 gint bplus = 0;
206                 gint ii = i * HISTMAP_SIZE / width;
207                 gint combine  = (HISTMAP_SIZE - 1) / width + 1;
208
209                 for (j = 0; j < combine; j++)
210                         {
211                         v[0] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + HISTMAP_CHANNEL_R]; // r
212                         v[1] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + HISTMAP_CHANNEL_G]; // g
213                         v[2] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + HISTMAP_CHANNEL_B]; // b
214                         v[3] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + 
215                                 ((histogram->histogram_chan == HCHAN_VAL) ? HISTMAP_CHANNEL_AVG : HISTMAP_CHANNEL_MAX)]; // value, max
216                         }
217                         
218                 for (j = 0; j < 4; j++)
219                         {
220                         v[j] /= combine;
221                         }
222
223                 for (j = 0; j < 4; j++)
224                         {
225                         gint max2 = 0;
226                         gint k;
227                 
228                         for (k = 1; k < 4; k++)
229                                 if (v[k] > v[max2]) max2 = k;
230                         
231                         if (histogram->histogram_chan >= HCHAN_RGB
232                             || max2 == histogram->histogram_chan)
233                                 {
234                                 gulong pt;
235                                 gint r = rplus;
236                                 gint g = gplus;
237                                 gint b = bplus;
238
239                                 switch (max2)
240                                         {
241                                         case HCHAN_R: rplus = r = 255; break;
242                                         case HCHAN_G: gplus = g = 255; break;
243                                         case HCHAN_B: bplus = b = 255; break;
244                                         }
245
246                                 switch (histogram->histogram_chan)
247                                         {
248                                         case HCHAN_RGB:
249                                                 if (r == 255 && g == 255 && b == 255)
250                                                         {
251                                                         r = 0; b = 0; g = 0;
252                                                         }
253                                                 break;
254                                         case HCHAN_R:          b = 0; g = 0; break;
255                                         case HCHAN_G:   r = 0; b = 0;        break;
256                                         case HCHAN_B:   r = 0;        g = 0; break;
257                                         case HCHAN_MAX:
258                                         case HCHAN_VAL: r = 0; b = 0; g = 0; break;
259                                         }
260                                 
261                                 if (v[max2] == 0)
262                                         pt = 0;
263                                 else if (histogram->histogram_logmode)
264                                         pt = ((float)log(v[max2])) / logmax * (height - 1);
265                                 else
266                                         pt = ((float)v[max2])/ max * (height - 1);
267
268                                 pixbuf_draw_line(pixbuf,
269                                         x, y, width, height,
270                                         x + i, y + height, x + i, y + height - pt,
271                                         r, g, b, 255);
272                                 }
273                         v[max2] = -1;
274                         }
275                 }
276
277         return TRUE;
278 }
279
280 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
281 {
282         if (type != NOTIFY_TYPE_INTERNAL && fd->histmap)
283                 {
284                 g_free(fd->histmap);
285                 fd->histmap = NULL;
286                 }
287 }
288
289 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */