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