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];
44 guint vgrid; /* number of vertical divisions, 0 for none */
45 guint hgrid; /* number of horizontal divisions, 0 for none */
49 Histogram *histogram_new(void)
53 histogram = g_new0(Histogram, 1);
54 histogram->channel_mode = options->histogram.last_channel_mode;
55 histogram->log_mode = options->histogram.last_log_mode;
62 void histogram_free(Histogram *histogram)
68 gint histogram_set_channel(Histogram *histogram, gint chan)
70 if (!histogram) return 0;
71 options->histogram.last_channel_mode = histogram->channel_mode = chan;
75 gint histogram_get_channel(Histogram *histogram)
77 if (!histogram) return 0;
78 return histogram->channel_mode;
81 gint histogram_set_mode(Histogram *histogram, gint mode)
83 if (!histogram) return 0;
84 options->histogram.last_log_mode = histogram->log_mode = mode;
88 gint histogram_get_mode(Histogram *histogram)
90 if (!histogram) return 0;
91 return histogram->log_mode;
94 const gchar *histogram_label(Histogram *histogram)
98 if (!histogram) return NULL;
100 if (histogram->log_mode)
101 switch (histogram->channel_mode)
103 case HCHAN_R: t1 = _("logarithmical histogram on red"); break;
104 case HCHAN_G: t1 = _("logarithmical histogram on green"); break;
105 case HCHAN_B: t1 = _("logarithmical histogram on blue"); break;
106 case HCHAN_VAL: t1 = _("logarithmical histogram on value"); break;
107 case HCHAN_RGB: t1 = _("logarithmical histogram on RGB"); break;
108 case HCHAN_MAX: t1 = _("logarithmical histogram on max value"); break;
111 switch (histogram->channel_mode)
113 case HCHAN_R: t1 = _("linear histogram on red"); break;
114 case HCHAN_G: t1 = _("linear histogram on green"); break;
115 case HCHAN_B: t1 = _("linear histogram on blue"); break;
116 case HCHAN_VAL: t1 = _("linear histogram on value"); break;
117 case HCHAN_RGB: t1 = _("linear histogram on RGB"); break;
118 case HCHAN_MAX: t1 = _("linear histogram on max value"); break;
123 static HistMap *histmap_read(GdkPixbuf *imgpixbuf)
125 gint w, h, i, j, srs, has_alpha, step;
130 w = gdk_pixbuf_get_width(imgpixbuf);
131 h = gdk_pixbuf_get_height(imgpixbuf);
132 srs = gdk_pixbuf_get_rowstride(imgpixbuf);
133 s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
134 has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
136 histmap = g_new0(HistMap, 1);
138 step = 3 + !!(has_alpha);
139 for (i = 0; i < h; i++)
141 guchar *sp = s_pix + (i * srs); /* 8bit */
142 for (j = 0; j < w; j++)
144 guint avg = (sp[0] + sp[1] + sp[2]) / 3;
146 if (sp[1] > max) max = sp[1];
147 if (sp[2] > max) max = sp[2];
149 histmap->histmap[sp[0] * HISTMAP_CHANNELS + HISTMAP_CHANNEL_R]++;
150 histmap->histmap[sp[1] * HISTMAP_CHANNELS + HISTMAP_CHANNEL_G]++;
151 histmap->histmap[sp[2] * HISTMAP_CHANNELS + HISTMAP_CHANNEL_B]++;
152 histmap->histmap[avg * HISTMAP_CHANNELS + HISTMAP_CHANNEL_AVG]++;
153 histmap->histmap[max * HISTMAP_CHANNELS + HISTMAP_CHANNEL_MAX]++;
157 histmap->area = w * h;
161 const HistMap *histmap_get(FileData *fd)
163 if (fd->histmap) return fd->histmap;
167 fd->histmap = histmap_read(fd->pixbuf);
173 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
176 static gint alpha = 250;
180 if (histogram->vgrid == 0) return;
182 add = width / (float)histogram->vgrid;
184 for (i = 1; i < histogram->vgrid; i++)
186 gint xpos = x + (int)(i * add + 0.5);
188 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height, c, c, c, alpha);
192 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
195 static gint alpha = 250;
199 if (histogram->hgrid == 0) return;
201 add = height / (float)histogram->hgrid;
203 for (i = 1; i < histogram->hgrid; i++)
205 gint ypos = y + (int)(i * add + 0.5);
207 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos, c, c, c, alpha);
211 gint histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
213 /* FIXME: use the coordinates correctly */
218 if (!histogram || !histmap) return 0;
221 histogram_vgrid(histogram, pixbuf, x, y, width, height);
222 histogram_hgrid(histogram, pixbuf, x, y, width, height);
224 for (i = 0; i < 1024; i++) {
226 /* this is probably broken for MAX or VAL mode */
229 switch (histogram->channel_mode)
231 case HCHAN_RGB: if ((i % HISTMAP_CHANNELS) < 3) flag = 1; break;
232 case HCHAN_R: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_R) flag = 1; break;
233 case HCHAN_G: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_G) flag = 1; break;
234 case HCHAN_B: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_B) flag = 1; break;
235 case HCHAN_VAL: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_AVG) flag = 1; break;
236 case HCHAN_MAX: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_MAX) flag = 1; break;
238 if (flag && histmap->histmap[i] > max) max = histmap->histmap[i];
240 if (histmap->histmap[i] > max) max = histmap->histmap[i];
245 for (i = 0; i < width; i++)
248 glong v[4] = {0, 0, 0, 0};
252 gint ii = i * HISTMAP_SIZE / width;
253 gint combine = (HISTMAP_SIZE - 1) / width + 1;
255 for (j = 0; j < combine; j++)
257 v[0] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + HISTMAP_CHANNEL_R]; // r
258 v[1] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + HISTMAP_CHANNEL_G]; // g
259 v[2] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + HISTMAP_CHANNEL_B]; // b
260 v[3] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS +
261 ((histogram->channel_mode == HCHAN_VAL) ? HISTMAP_CHANNEL_AVG : HISTMAP_CHANNEL_MAX)]; // value, max
264 for (j = 0; j < 4; j++)
269 for (j = 0; j < 4; j++)
274 for (k = 1; k < 4; k++)
275 if (v[k] > v[max2]) max2 = k;
277 if (histogram->channel_mode >= HCHAN_RGB
278 || max2 == histogram->channel_mode)
287 case HCHAN_R: rplus = r = 255; break;
288 case HCHAN_G: gplus = g = 255; break;
289 case HCHAN_B: bplus = b = 255; break;
292 switch (histogram->channel_mode)
295 if (r == 255 && g == 255 && b == 255)
300 case HCHAN_R: b = 0; g = 0; break;
301 case HCHAN_G: r = 0; b = 0; break;
302 case HCHAN_B: r = 0; g = 0; break;
304 case HCHAN_VAL: r = 0; b = 0; g = 0; break;
309 else if (histogram->log_mode)
310 pt = ((float)log(v[max2])) / logmax * (height - 1);
312 pt = ((float)v[max2])/ max * (height - 1);
314 pixbuf_draw_line(pixbuf,
316 x + i, y + height, x + i, y + height - pt,
326 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
328 if (type != NOTIFY_TYPE_INTERNAL && fd->histmap)
335 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */