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