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