4 * Author: Vladimir Nadvornik
5 * based on a patch by Uwe Ohse
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!
13 #include "histogram.h"
15 #include "pixbuf_util.h"
20 *----------------------------------------------------------------------------
22 *----------------------------------------------------------------------------
25 #define HISTOGRAM_SIZE 256
28 gulong histmap[HISTOGRAM_SIZE*4];
30 gint histogram_logmode;
34 Histogram *histogram_new()
38 histogram = g_new0(Histogram, 1);
39 histogram->histogram_chan = HCHAN_RGB;
40 histogram->histogram_logmode = 1;
45 void histogram_free(Histogram *histogram)
51 gint histogram_set_channel(Histogram *histogram, gint chan)
53 if (!histogram) return 0;
54 histogram->histogram_chan = chan;
58 gint histogram_get_channel(Histogram *histogram)
60 if (!histogram) return 0;
61 return histogram->histogram_chan;
64 gint histogram_set_mode(Histogram *histogram, gint mode)
66 if (!histogram) return 0;
67 histogram->histogram_logmode = mode;
71 gint histogram_get_mode(Histogram *histogram)
73 if (!histogram) return 0;
74 return histogram->histogram_logmode;
77 const gchar *histogram_label(Histogram *histogram)
80 if (!histogram) return NULL;
82 if (histogram->histogram_logmode)
83 switch (histogram->histogram_chan) {
84 case HCHAN_R: t1 = _("logarithmical histogram on red"); break;
85 case HCHAN_G: t1 = _("logarithmical histogram on green"); break;
86 case HCHAN_B: t1 = _("logarithmical histogram on blue"); break;
87 case HCHAN_VAL: t1 = _("logarithmical histogram on value"); break;
88 case HCHAN_RGB: t1 = _("logarithmical histogram on RGB"); break;
89 case HCHAN_MAX: t1 = _("logarithmical histogram on max value"); break;
92 switch (histogram->histogram_chan) {
93 case HCHAN_R: t1 = _("linear histogram on red"); break;
94 case HCHAN_G: t1 = _("linear histogram on green"); break;
95 case HCHAN_B: t1 = _("linear histogram on blue"); break;
96 case HCHAN_VAL: t1 = _("linear histogram on value"); break;
97 case HCHAN_RGB: t1 = _("linear histogram on RGB"); break;
98 case HCHAN_MAX: t1 = _("linear histogram on max value"); break;
103 gulong histogram_read(Histogram *histogram, GdkPixbuf *imgpixbuf)
105 gint w, h, i, j, srs, has_alpha;
108 if (!histogram) return 0;
110 w = gdk_pixbuf_get_width(imgpixbuf);
111 h = gdk_pixbuf_get_height(imgpixbuf);
112 srs = gdk_pixbuf_get_rowstride(imgpixbuf);
113 s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
114 has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
116 for (i = 0; i < HISTOGRAM_SIZE*4; i++) histogram->histmap[i] = 0;
118 for (i = 0; i < h; i++)
120 guchar *sp = s_pix + (i * srs); /* 8bit */
121 for (j = 0; j < w; j++)
123 histogram->histmap[sp[0] + 0 * HISTOGRAM_SIZE]++;
124 histogram->histmap[sp[1] + 1 * HISTOGRAM_SIZE]++;
125 histogram->histmap[sp[2] + 2 * HISTOGRAM_SIZE]++;
126 if (histogram->histogram_chan == HCHAN_MAX)
129 if (sp[1]>t) t = sp[1];
130 if (sp[2]>t) t = sp[2];
131 histogram->histmap[t + 3 * HISTOGRAM_SIZE]++;
134 histogram->histmap[3 * HISTOGRAM_SIZE + (sp[0]+sp[1]+sp[2])/3]++;
144 gint histogram_draw(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
146 /* FIXME: use the coordinates correctly */
151 if (!histogram) return 0;
153 for (i=0; i<1024; i++) {
156 switch (histogram->histogram_chan)
158 case HCHAN_RGB: if ((i%4) != 3 ) flag = 1; break;
159 case HCHAN_R: if ((i%4) == 0) flag = 1; break;
160 case HCHAN_G: if ((i%4) == 1) flag = 1; break;
161 case HCHAN_B: if ((i%4) == 2) flag = 1; break;
162 case HCHAN_VAL: if ((i%4) == 3) flag = 1; break;
163 case HCHAN_MAX: if ((i%4) == 3) flag = 1; break;
165 if (flag && histogram->histmap[i] > max) max = histogram->histmap[i];
169 for (i=0; i<width; i++)
172 glong v[4] = {0, 0, 0, 0};
176 gint ii = i * HISTOGRAM_SIZE / width;
177 gint combine = (HISTOGRAM_SIZE - 1) / width + 1;
179 for (j = 0; j < combine; j++)
181 v[0] += histogram->histmap[ii + j + 0*HISTOGRAM_SIZE]; // r
182 v[1] += histogram->histmap[ii + j + 1*HISTOGRAM_SIZE]; // g
183 v[2] += histogram->histmap[ii + j + 2*HISTOGRAM_SIZE]; // b
184 v[3] += histogram->histmap[ii + j + 3*HISTOGRAM_SIZE]; // value, max
197 if (v[k] > v[max2]) max2 = k;
201 case HCHAN_R: rplus = r = 255; break;
202 case HCHAN_G: gplus = g = 255; break;
203 case HCHAN_B: bplus = b = 255; break;
206 switch(histogram->histogram_chan)
208 case HCHAN_MAX: r = 0; b = 0; g = 0; break;
209 case HCHAN_VAL: r = 0; b = 0; g = 0; break;
210 case HCHAN_R: g = 0; b = 0; break;
211 case HCHAN_G: r = 0; b = 0; break;
212 case HCHAN_B: r = 0; g = 0; break;
214 if (r == 255 && g == 255 && b == 255) {
224 else if (histogram->histogram_logmode)
225 pt = ((float)log(v[max2])) / logmax * (height - 1);
227 pt = ((float)v[max2])/ max * (height - 1);
228 if (histogram->histogram_chan >= HCHAN_RGB
229 || max2 == histogram->histogram_chan)
230 pixbuf_draw_line(pixbuf,
232 x + i, y + height, x + i, y + height-pt,