histogram_read(): use memset() to initialize histogram data instead of for() loop.
[geeqie.git] / src / histogram.c
1 /*
2  * Geeqie
3  *
4  * Author: Vladimir Nadvornik
5  * based on a patch by Uwe Ohse
6  *
7  * This software is released under the GNU General Public License (GNU GPL).
8  * Please read the included file COPYING for more information.
9  * This software comes with no warranty of any kind, use at your own risk!
10  */
11
12 #include "main.h"
13 #include "histogram.h"
14
15 #include "pixbuf_util.h"
16
17 #include <math.h>
18
19 /*
20  *----------------------------------------------------------------------------
21  * image histogram
22  *----------------------------------------------------------------------------
23  */
24
25 #define HISTOGRAM_SIZE 256
26
27 struct _Histogram {
28         gulong histmap[HISTOGRAM_SIZE*4];
29         gint histogram_chan;
30         gint histogram_logmode;
31 };
32
33
34 Histogram *histogram_new()
35 {
36         Histogram *histogram;
37
38         histogram = g_new0(Histogram, 1);
39         histogram->histogram_chan = HCHAN_RGB;
40         histogram->histogram_logmode = 1;
41
42         return histogram;
43 }
44
45 void histogram_free(Histogram *histogram)
46 {
47         g_free(histogram);
48 }
49
50
51 gint histogram_set_channel(Histogram *histogram, gint chan)
52 {
53         if (!histogram) return 0;
54         histogram->histogram_chan = chan;
55         return chan;
56 }
57
58 gint histogram_get_channel(Histogram *histogram)
59 {
60         if (!histogram) return 0;
61         return histogram->histogram_chan;
62 }
63
64 gint histogram_set_mode(Histogram *histogram, gint mode)
65 {
66         if (!histogram) return 0;
67         histogram->histogram_logmode = mode;
68         return mode;
69 }
70
71 gint histogram_get_mode(Histogram *histogram)
72 {
73         if (!histogram) return 0;
74         return histogram->histogram_logmode;
75 }
76
77 const gchar *histogram_label(Histogram *histogram)
78 {
79         const gchar *t1 = "";
80         if (!histogram) return NULL;
81
82         if (histogram->histogram_logmode)
83                         switch (histogram->histogram_chan)
84                         {
85                         case HCHAN_R:   t1 = _("logarithmical histogram on red"); break;
86                         case HCHAN_G:   t1 = _("logarithmical histogram on green"); break;
87                         case HCHAN_B:   t1 = _("logarithmical histogram on blue"); break;
88                         case HCHAN_VAL: t1 = _("logarithmical histogram on value"); break;
89                         case HCHAN_RGB: t1 = _("logarithmical histogram on RGB"); break;
90                         case HCHAN_MAX: t1 = _("logarithmical histogram on max value"); break;
91                         }
92                 else
93                         switch (histogram->histogram_chan)
94                         {
95                         case HCHAN_R:   t1 = _("linear histogram on red"); break;
96                         case HCHAN_G:   t1 = _("linear histogram on green"); break;
97                         case HCHAN_B:   t1 = _("linear histogram on blue"); break;
98                         case HCHAN_VAL: t1 = _("linear histogram on value"); break;
99                         case HCHAN_RGB: t1 = _("linear histogram on RGB"); break;
100                         case HCHAN_MAX: t1 = _("linear histogram on max value"); break;
101                         }
102         return t1;
103 }
104
105 gulong histogram_read(Histogram *histogram, GdkPixbuf *imgpixbuf)
106 {
107         gint w, h, i, j, srs, has_alpha;
108         guchar *s_pix;
109
110         if (!histogram) return 0;
111
112         w = gdk_pixbuf_get_width(imgpixbuf);
113         h = gdk_pixbuf_get_height(imgpixbuf);
114         srs = gdk_pixbuf_get_rowstride(imgpixbuf);
115         s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
116         has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
117
118         memset(histogram->histmap, 0, sizeof(histogram->histmap));
119
120         for (i = 0; i < h; i++)
121                 {
122                 guchar *sp = s_pix + (i * srs); /* 8bit */
123                 for (j = 0; j < w; j++)
124                         {
125                         histogram->histmap[sp[0] + 0 * HISTOGRAM_SIZE]++;
126                         histogram->histmap[sp[1] + 1 * HISTOGRAM_SIZE]++;
127                         histogram->histmap[sp[2] + 2 * HISTOGRAM_SIZE]++;
128                         if (histogram->histogram_chan == HCHAN_MAX)
129                                 {
130                                 guchar t = sp[0];
131                                 if (sp[1]>t) t = sp[1];
132                                 if (sp[2]>t) t = sp[2];
133                                 histogram->histmap[t + 3 * HISTOGRAM_SIZE]++;
134                                 }
135                         else
136                                 histogram->histmap[3 * HISTOGRAM_SIZE + (sp[0]+sp[1]+sp[2])/3]++;
137                         sp += 3;
138                         if (has_alpha) sp++;
139                         }
140                 }
141
142         return w*h;
143 }
144
145
146 gint histogram_draw(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
147 {
148         /* FIXME: use the coordinates correctly */
149         gint i;
150         gulong max = 0;
151         double logmax;
152
153         if (!histogram) return 0;
154
155         for (i=0; i<1024; i++) {
156                 gint flag = 0;
157
158                 switch (histogram->histogram_chan)
159                 {
160                 case HCHAN_RGB: if ((i%4) != 3 ) flag = 1; break;
161                 case HCHAN_R: if ((i%4) == 0) flag = 1; break;
162                 case HCHAN_G: if ((i%4) == 1) flag = 1; break;
163                 case HCHAN_B: if ((i%4) == 2) flag = 1; break;
164                 case HCHAN_VAL: if ((i%4) == 3) flag = 1; break;
165                 case HCHAN_MAX: if ((i%4) == 3) flag = 1; break;
166                 }
167                 if (flag && histogram->histmap[i] > max) max = histogram->histmap[i];
168         }
169
170         logmax = log(max);
171         for (i=0; i<width; i++)
172                 {
173                 gint j;
174                 glong v[4] = {0, 0, 0, 0};
175                 gint rplus = 0;
176                 gint gplus = 0;
177                 gint bplus = 0;
178                 gint ii = i * HISTOGRAM_SIZE / width;
179                 gint combine  = (HISTOGRAM_SIZE - 1) / width + 1;
180
181                 for (j = 0; j < combine; j++)
182                         {
183                         v[0] += histogram->histmap[ii + j + 0*HISTOGRAM_SIZE]; // r
184                         v[1] += histogram->histmap[ii + j + 1*HISTOGRAM_SIZE]; // g
185                         v[2] += histogram->histmap[ii + j + 2*HISTOGRAM_SIZE]; // b
186                         v[3] += histogram->histmap[ii + j + 3*HISTOGRAM_SIZE]; // value, max
187                         }
188
189                 for (j=0; j<4; j++)
190                         {
191                         gint r = rplus;
192                         gint g = gplus;
193                         gint b = bplus;
194                         gint max2 = 0;
195                         gint k;
196                         gulong pt;
197
198                         for (k=1; k<4; k++)
199                                 if (v[k] > v[max2]) max2 = k;
200
201                         switch (max2)
202                         {
203                         case HCHAN_R: rplus = r = 255; break;
204                         case HCHAN_G: gplus = g = 255; break;
205                         case HCHAN_B: bplus = b = 255; break;
206                         }
207
208                         switch(histogram->histogram_chan)
209                         {
210                         case HCHAN_MAX: r = 0; b = 0; g = 0; break;
211                         case HCHAN_VAL: r = 0; b = 0; g = 0; break;
212                         case HCHAN_R: g = 0; b = 0; break;
213                         case HCHAN_G: r = 0; b = 0; break;
214                         case HCHAN_B: r = 0; g = 0; break;
215                         case HCHAN_RGB:
216                                 if (r == 255 && g == 255 && b == 255) {
217                                         r = 0;
218                                         g = 0;
219                                         b = 0;
220                                 }
221                                 break;
222                         }
223
224                         if (v[max2] == 0)
225                                 pt = 0;
226                         else if (histogram->histogram_logmode)
227                                 pt = ((float)log(v[max2])) / logmax * (height - 1);
228                         else
229                                 pt = ((float)v[max2])/ max * (height - 1);
230                         if (histogram->histogram_chan >= HCHAN_RGB
231                             || max2 == histogram->histogram_chan)
232                                 pixbuf_draw_line(pixbuf,
233                                         x, y, width, height,
234                                         x + i, y + height, x + i, y + height-pt,
235                                         r, g, b, 255);
236                         v[max2] = -1;
237                         }
238                 }
239         return TRUE;
240 }