3 * Copyright (C) 2008 - 2009 The Geeqie Team
5 * Author: Vladimir Nadvornik
6 * based on a patch by Uwe Ohse
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!
14 #include "histogram.h"
16 #include "pixbuf_util.h"
21 *----------------------------------------------------------------------------
23 *----------------------------------------------------------------------------
26 #define HISTMAP_SIZE 256
28 HISTMAP_CHANNEL_R = 0,
37 gulong histmap[HISTMAP_SIZE * HISTMAP_CHANNELS];
43 gint histogram_logmode;
47 Histogram *histogram_new(void)
51 histogram = g_new0(Histogram, 1);
52 histogram->histogram_chan = options->histogram.last_channel_mode;
53 histogram->histogram_logmode = options->histogram.last_log_mode;
58 void histogram_free(Histogram *histogram)
64 gint histogram_set_channel(Histogram *histogram, gint chan)
66 if (!histogram) return 0;
67 options->histogram.last_channel_mode = histogram->histogram_chan = chan;
71 gint histogram_get_channel(Histogram *histogram)
73 if (!histogram) return 0;
74 return histogram->histogram_chan;
77 gint histogram_set_mode(Histogram *histogram, gint mode)
79 if (!histogram) return 0;
80 options->histogram.last_log_mode = histogram->histogram_logmode = mode;
84 gint histogram_get_mode(Histogram *histogram)
86 if (!histogram) return 0;
87 return histogram->histogram_logmode;
90 const gchar *histogram_label(Histogram *histogram)
94 if (!histogram) return NULL;
96 if (histogram->histogram_logmode)
97 switch (histogram->histogram_chan)
99 case HCHAN_R: t1 = _("logarithmical histogram on red"); break;
100 case HCHAN_G: t1 = _("logarithmical histogram on green"); break;
101 case HCHAN_B: t1 = _("logarithmical histogram on blue"); break;
102 case HCHAN_VAL: t1 = _("logarithmical histogram on value"); break;
103 case HCHAN_RGB: t1 = _("logarithmical histogram on RGB"); break;
104 case HCHAN_MAX: t1 = _("logarithmical histogram on max value"); break;
107 switch (histogram->histogram_chan)
109 case HCHAN_R: t1 = _("linear histogram on red"); break;
110 case HCHAN_G: t1 = _("linear histogram on green"); break;
111 case HCHAN_B: t1 = _("linear histogram on blue"); break;
112 case HCHAN_VAL: t1 = _("linear histogram on value"); break;
113 case HCHAN_RGB: t1 = _("linear histogram on RGB"); break;
114 case HCHAN_MAX: t1 = _("linear histogram on max value"); break;
119 static HistMap *histmap_read(GdkPixbuf *imgpixbuf)
121 gint w, h, i, j, srs, has_alpha, step;
126 w = gdk_pixbuf_get_width(imgpixbuf);
127 h = gdk_pixbuf_get_height(imgpixbuf);
128 srs = gdk_pixbuf_get_rowstride(imgpixbuf);
129 s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
130 has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
132 histmap = g_new0(HistMap, 1);
134 step = 3 + !!(has_alpha);
135 for (i = 0; i < h; i++)
137 guchar *sp = s_pix + (i * srs); /* 8bit */
138 for (j = 0; j < w; j++)
140 guint avg = (sp[0] + sp[1] + sp[2]) / 3;
142 if (sp[1] > max) max = sp[1];
143 if (sp[2] > max) max = sp[2];
145 histmap->histmap[sp[0] * HISTMAP_CHANNELS + HISTMAP_CHANNEL_R]++;
146 histmap->histmap[sp[1] * HISTMAP_CHANNELS + HISTMAP_CHANNEL_G]++;
147 histmap->histmap[sp[2] * HISTMAP_CHANNELS + HISTMAP_CHANNEL_B]++;
148 histmap->histmap[avg * HISTMAP_CHANNELS + HISTMAP_CHANNEL_AVG]++;
149 histmap->histmap[max * HISTMAP_CHANNELS + HISTMAP_CHANNEL_MAX]++;
153 histmap->area = w * h;
157 const HistMap *histmap_get(FileData *fd)
159 if (fd->histmap) return fd->histmap;
163 fd->histmap = histmap_read(fd->pixbuf);
169 gint histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
171 /* FIXME: use the coordinates correctly */
176 if (!histogram || !histmap) return 0;
178 for (i = 0; i < 1024; i++) {
180 /* this is probably broken for MAX or VAL mode */
183 switch (histogram->histogram_chan)
185 case HCHAN_RGB: if ((i % HISTMAP_CHANNELS) < 3) flag = 1; break;
186 case HCHAN_R: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_R) flag = 1; break;
187 case HCHAN_G: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_G) flag = 1; break;
188 case HCHAN_B: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_B) flag = 1; break;
189 case HCHAN_VAL: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_AVG) flag = 1; break;
190 case HCHAN_MAX: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_MAX) flag = 1; break;
192 if (flag && histmap->histmap[i] > max) max = histmap->histmap[i];
194 if (histmap->histmap[i] > max) max = histmap->histmap[i];
199 for (i = 0; i < width; i++)
202 glong v[4] = {0, 0, 0, 0};
206 gint ii = i * HISTMAP_SIZE / width;
207 gint combine = (HISTMAP_SIZE - 1) / width + 1;
209 for (j = 0; j < combine; j++)
211 v[0] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + HISTMAP_CHANNEL_R]; // r
212 v[1] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + HISTMAP_CHANNEL_G]; // g
213 v[2] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + HISTMAP_CHANNEL_B]; // b
214 v[3] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS +
215 ((histogram->histogram_chan == HCHAN_VAL) ? HISTMAP_CHANNEL_AVG : HISTMAP_CHANNEL_MAX)]; // value, max
218 for (j = 0; j < 4; j++)
223 for (j = 0; j < 4; j++)
228 for (k = 1; k < 4; k++)
229 if (v[k] > v[max2]) max2 = k;
231 if (histogram->histogram_chan >= HCHAN_RGB
232 || max2 == histogram->histogram_chan)
241 case HCHAN_R: rplus = r = 255; break;
242 case HCHAN_G: gplus = g = 255; break;
243 case HCHAN_B: bplus = b = 255; break;
246 switch (histogram->histogram_chan)
249 if (r == 255 && g == 255 && b == 255)
254 case HCHAN_R: b = 0; g = 0; break;
255 case HCHAN_G: r = 0; b = 0; break;
256 case HCHAN_B: r = 0; g = 0; break;
258 case HCHAN_VAL: r = 0; b = 0; g = 0; break;
263 else if (histogram->histogram_logmode)
264 pt = ((float)log(v[max2])) / logmax * (height - 1);
266 pt = ((float)v[max2])/ max * (height - 1);
268 pixbuf_draw_line(pixbuf,
270 x + i, y + height, x + i, y + height - pt,
280 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
282 if (type != NOTIFY_TYPE_INTERNAL && fd->histmap)
289 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */