updated copyright in source files
[geeqie.git] / src / histogram.c
1 /*
2  * Geeqie
3  * Copyright (C) 2008 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 HISTOGRAM_SIZE 256
27
28 struct _Histogram {
29         gulong histmap[HISTOGRAM_SIZE*4];
30         gint histogram_chan;
31         gint histogram_logmode;
32 };
33
34
35 Histogram *histogram_new()
36 {
37         Histogram *histogram;
38
39         histogram = g_new0(Histogram, 1);
40         histogram->histogram_chan = HCHAN_RGB;
41         histogram->histogram_logmode = 1;
42
43         return histogram;
44 }
45
46 void histogram_free(Histogram *histogram)
47 {
48         g_free(histogram);
49 }
50
51
52 gint histogram_set_channel(Histogram *histogram, gint chan)
53 {
54         if (!histogram) return 0;
55         histogram->histogram_chan = chan;
56         return chan;
57 }
58
59 gint histogram_get_channel(Histogram *histogram)
60 {
61         if (!histogram) return 0;
62         return histogram->histogram_chan;
63 }
64
65 gint histogram_set_mode(Histogram *histogram, gint mode)
66 {
67         if (!histogram) return 0;
68         histogram->histogram_logmode = mode;
69         return mode;
70 }
71
72 gint histogram_get_mode(Histogram *histogram)
73 {
74         if (!histogram) return 0;
75         return histogram->histogram_logmode;
76 }
77
78 const gchar *histogram_label(Histogram *histogram)
79 {
80         const gchar *t1 = "";
81         if (!histogram) return NULL;
82
83         if (histogram->histogram_logmode)
84                         switch (histogram->histogram_chan)
85                         {
86                         case HCHAN_R:   t1 = _("logarithmical histogram on red"); break;
87                         case HCHAN_G:   t1 = _("logarithmical histogram on green"); break;
88                         case HCHAN_B:   t1 = _("logarithmical histogram on blue"); break;
89                         case HCHAN_VAL: t1 = _("logarithmical histogram on value"); break;
90                         case HCHAN_RGB: t1 = _("logarithmical histogram on RGB"); break;
91                         case HCHAN_MAX: t1 = _("logarithmical histogram on max value"); break;
92                         }
93                 else
94                         switch (histogram->histogram_chan)
95                         {
96                         case HCHAN_R:   t1 = _("linear histogram on red"); break;
97                         case HCHAN_G:   t1 = _("linear histogram on green"); break;
98                         case HCHAN_B:   t1 = _("linear histogram on blue"); break;
99                         case HCHAN_VAL: t1 = _("linear histogram on value"); break;
100                         case HCHAN_RGB: t1 = _("linear histogram on RGB"); break;
101                         case HCHAN_MAX: t1 = _("linear histogram on max value"); break;
102                         }
103         return t1;
104 }
105
106 gulong histogram_read(Histogram *histogram, GdkPixbuf *imgpixbuf)
107 {
108         gint w, h, i, j, srs, has_alpha, step;
109         guchar *s_pix;
110
111         if (!histogram) return 0;
112
113         w = gdk_pixbuf_get_width(imgpixbuf);
114         h = gdk_pixbuf_get_height(imgpixbuf);
115         srs = gdk_pixbuf_get_rowstride(imgpixbuf);
116         s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
117         has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
118
119         memset(histogram->histmap, 0, sizeof(histogram->histmap));
120
121         /* code duplication is here to speed up the calculation */
122         step = 3 + !!(has_alpha);
123         if (histogram->histogram_chan == HCHAN_MAX)
124                 {
125                 for (i = 0; i < h; i++)
126                         {
127                         guchar *sp = s_pix + (i * srs); /* 8bit */
128                         for (j = 0; j < w; j++)
129                                 {
130                                 guchar t = sp[0];
131                                 if (sp[1]>t) t = sp[1];
132                                 if (sp[2]>t) t = sp[2];
133
134                                 histogram->histmap[sp[0] + 0 * HISTOGRAM_SIZE]++;
135                                 histogram->histmap[sp[1] + 1 * HISTOGRAM_SIZE]++;
136                                 histogram->histmap[sp[2] + 2 * HISTOGRAM_SIZE]++;
137                                 histogram->histmap[t + 3 * HISTOGRAM_SIZE]++;
138                                 sp += step;
139                                 }
140                         }
141                 }
142         else
143                 {
144                 for (i = 0; i < h; i++)
145                         {
146                         guchar *sp = s_pix + (i * srs); /* 8bit */
147                         for (j = 0; j < w; j++)
148                                 {
149                                 histogram->histmap[sp[0] + 0 * HISTOGRAM_SIZE]++;
150                                 histogram->histmap[sp[1] + 1 * HISTOGRAM_SIZE]++;
151                                 histogram->histmap[sp[2] + 2 * HISTOGRAM_SIZE]++;
152                                 histogram->histmap[3 * HISTOGRAM_SIZE + (sp[0]+sp[1]+sp[2])/3]++;
153                                 sp += step;
154                                 }
155                         }
156                 }
157
158         return w*h;
159 }
160
161 gint histogram_draw(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
162 {
163         /* FIXME: use the coordinates correctly */
164         gint i;
165         gulong max = 0;
166         double logmax;
167
168         if (!histogram) return 0;
169
170         for (i = 0; i < 1024; i++) {
171                 gint flag = 0;
172
173                 switch (histogram->histogram_chan)
174                 {
175                 case HCHAN_RGB: if ((i%4) != 3) flag = 1; break;
176                 case HCHAN_R:   if ((i%4) == 0) flag = 1; break;
177                 case HCHAN_G:   if ((i%4) == 1) flag = 1; break;
178                 case HCHAN_B:   if ((i%4) == 2) flag = 1; break;
179                 case HCHAN_VAL: if ((i%4) == 3) flag = 1; break;
180                 case HCHAN_MAX: if ((i%4) == 3) flag = 1; break;
181                 }
182                 if (flag && histogram->histmap[i] > max) max = histogram->histmap[i];
183         }
184
185         logmax = log(max);
186         for (i = 0; i < width; i++)
187                 {
188                 gint j;
189                 glong v[4] = {0, 0, 0, 0};
190                 gint rplus = 0;
191                 gint gplus = 0;
192                 gint bplus = 0;
193                 gint ii = i * HISTOGRAM_SIZE / width;
194                 gint combine  = (HISTOGRAM_SIZE - 1) / width + 1;
195
196                 for (j = 0; j < combine; j++)
197                         {
198                         v[0] += histogram->histmap[ii + j + 0 * HISTOGRAM_SIZE]; // r
199                         v[1] += histogram->histmap[ii + j + 1 * HISTOGRAM_SIZE]; // g
200                         v[2] += histogram->histmap[ii + j + 2 * HISTOGRAM_SIZE]; // b
201                         v[3] += histogram->histmap[ii + j + 3 * HISTOGRAM_SIZE]; // value, max
202                         }
203
204                 for (j = 0; j < 4; j++)
205                         {
206                         gint max2 = 0;
207                         gint k;
208                 
209                         for (k = 1; k < 4; k++)
210                                 if (v[k] > v[max2]) max2 = k;
211                         
212                         if (histogram->histogram_chan >= HCHAN_RGB
213                             || max2 == histogram->histogram_chan)
214                                 {
215                                 gulong pt;
216                                 gint r = rplus;
217                                 gint g = gplus;
218                                 gint b = bplus;
219
220                                 switch (max2)
221                                         {
222                                         case HCHAN_R: rplus = r = 255; break;
223                                         case HCHAN_G: gplus = g = 255; break;
224                                         case HCHAN_B: bplus = b = 255; break;
225                                         }
226
227                                 switch (histogram->histogram_chan)
228                                         {
229                                         case HCHAN_RGB:
230                                                 if (r == 255 && g == 255 && b == 255)
231                                                         {
232                                                         r = 0; b = 0; g = 0;
233                                                         }
234                                                 break;
235                                         case HCHAN_R:          b = 0; g = 0; break;
236                                         case HCHAN_G:   r = 0; b = 0;        break;
237                                         case HCHAN_B:   r = 0;        g = 0; break;
238                                         case HCHAN_MAX:
239                                         case HCHAN_VAL: r = 0; b = 0; g = 0; break;
240                                         }
241                                 
242                                 if (v[max2] == 0)
243                                         pt = 0;
244                                 else if (histogram->histogram_logmode)
245                                         pt = ((float)log(v[max2])) / logmax * (height - 1);
246                                 else
247                                         pt = ((float)v[max2])/ max * (height - 1);
248
249                                 pixbuf_draw_line(pixbuf,
250                                         x, y, width, height,
251                                         x + i, y + height, x + i, y + height - pt,
252                                         r, g, b, 255);
253                                 }
254                         v[max2] = -1;
255                         }
256                 }
257
258         return TRUE;
259 }