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
29 gulong r[HISTMAP_SIZE];
30 gulong g[HISTMAP_SIZE];
31 gulong b[HISTMAP_SIZE];
32 gulong avg[HISTMAP_SIZE];
33 gulong max[HISTMAP_SIZE];
37 gint channel_mode; /* drawing mode for histogram */
38 gint log_mode; /* logarithmical or not */
39 guint vgrid; /* number of vertical divisions, 0 for none */
40 guint hgrid; /* number of horizontal divisions, 0 for none */
46 } grid_color; /* grid color */
50 Histogram *histogram_new(void)
54 histogram = g_new0(Histogram, 1);
55 histogram->channel_mode = options->histogram.last_channel_mode;
56 histogram->log_mode = options->histogram.last_log_mode;
61 histogram->grid_color.R = 160;
62 histogram->grid_color.G = 160;
63 histogram->grid_color.B = 160;
64 histogram->grid_color.A = 250;
69 void histogram_free(Histogram *histogram)
75 gint histogram_set_channel(Histogram *histogram, gint chan)
77 if (!histogram) return 0;
78 options->histogram.last_channel_mode = histogram->channel_mode = chan;
82 gint histogram_get_channel(Histogram *histogram)
84 if (!histogram) return 0;
85 return histogram->channel_mode;
88 gint histogram_set_mode(Histogram *histogram, gint mode)
90 if (!histogram) return 0;
91 options->histogram.last_log_mode = histogram->log_mode = mode;
95 gint histogram_get_mode(Histogram *histogram)
97 if (!histogram) return 0;
98 return histogram->log_mode;
101 const gchar *histogram_label(Histogram *histogram)
103 const gchar *t1 = "";
105 if (!histogram) return NULL;
107 if (histogram->log_mode)
108 switch (histogram->channel_mode)
110 case HCHAN_R: t1 = _("logarithmical histogram on red"); break;
111 case HCHAN_G: t1 = _("logarithmical histogram on green"); break;
112 case HCHAN_B: t1 = _("logarithmical histogram on blue"); break;
113 case HCHAN_VAL: t1 = _("logarithmical histogram on value"); break;
114 case HCHAN_RGB: t1 = _("logarithmical histogram on RGB"); break;
115 case HCHAN_MAX: t1 = _("logarithmical histogram on max value"); break;
118 switch (histogram->channel_mode)
120 case HCHAN_R: t1 = _("linear histogram on red"); break;
121 case HCHAN_G: t1 = _("linear histogram on green"); break;
122 case HCHAN_B: t1 = _("linear histogram on blue"); break;
123 case HCHAN_VAL: t1 = _("linear histogram on value"); break;
124 case HCHAN_RGB: t1 = _("linear histogram on RGB"); break;
125 case HCHAN_MAX: t1 = _("linear histogram on max value"); break;
130 static HistMap *histmap_read(GdkPixbuf *imgpixbuf)
132 gint w, h, i, j, srs, has_alpha, step;
137 w = gdk_pixbuf_get_width(imgpixbuf);
138 h = gdk_pixbuf_get_height(imgpixbuf);
139 srs = gdk_pixbuf_get_rowstride(imgpixbuf);
140 s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
141 has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
143 histmap = g_new0(HistMap, 1);
145 step = 3 + !!(has_alpha);
146 for (i = 0; i < h; i++)
148 guchar *sp = s_pix + (i * srs); /* 8bit */
149 for (j = 0; j < w; j++)
151 guint avg = (sp[0] + sp[1] + sp[2]) / 3;
153 if (sp[1] > max) max = sp[1];
154 if (sp[2] > max) max = sp[2];
169 const HistMap *histmap_get(FileData *fd)
171 if (fd->histmap) return fd->histmap;
175 fd->histmap = histmap_read(fd->pixbuf);
181 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
186 if (histogram->vgrid == 0) return;
188 add = width / (float)histogram->vgrid;
190 for (i = 1; i < histogram->vgrid; i++)
192 gint xpos = x + (int)(i * add + 0.5);
194 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
195 histogram->grid_color.R,
196 histogram->grid_color.G,
197 histogram->grid_color.B,
198 histogram->grid_color.A);
202 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
207 if (histogram->hgrid == 0) return;
209 add = height / (float)histogram->hgrid;
211 for (i = 1; i < histogram->hgrid; i++)
213 gint ypos = y + (int)(i * add + 0.5);
215 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
216 histogram->grid_color.R,
217 histogram->grid_color.G,
218 histogram->grid_color.B,
219 histogram->grid_color.A);
223 gint histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
225 /* FIXME: use the coordinates correctly */
229 gint combine = (HISTMAP_SIZE - 1) / width + 1;
230 gint ypos = y + height;
232 if (!histogram || !histmap) return 0;
235 histogram_vgrid(histogram, pixbuf, x, y, width, height);
236 histogram_hgrid(histogram, pixbuf, x, y, width, height);
238 switch (histogram->channel_mode)
243 for (i = 0; i < HISTMAP_SIZE; i++)
245 if (histmap->r[i] > max) max = histmap->r[i];
246 if (histmap->g[i] > max) max = histmap->g[i];
247 if (histmap->b[i] > max) max = histmap->b[i];
250 case HCHAN_R: for (i = 0; i < HISTMAP_SIZE; i++) if (histmap->r[i] > max) max = histmap->r[i]; break;
251 case HCHAN_G: for (i = 0; i < HISTMAP_SIZE; i++) if (histmap->g[i] > max) max = histmap->g[i]; break;
252 case HCHAN_B: for (i = 0; i < HISTMAP_SIZE; i++) if (histmap->b[i] > max) max = histmap->b[i]; break;
256 for (i = 0; i < width; i++)
259 glong v[4] = {0, 0, 0, 0};
263 gint ii = i * HISTMAP_SIZE / width;
266 for (j = 0; j < combine; j++)
269 v[0] += histmap->r[p];
270 v[1] += histmap->g[p];
271 v[2] += histmap->b[p];
272 if (histogram->channel_mode == HCHAN_VAL)
274 v[3] += histmap->avg[p];
278 v[3] += histmap->max[p];
282 for (j = 0; j < 4; j++)
287 for (k = 1; k < 4; k++)
288 if (v[k] > v[chanmax]) chanmax = k;
290 if (histogram->channel_mode >= HCHAN_RGB
291 || chanmax == histogram->channel_mode)
300 case 0: rplus = r = 255; break;
301 case 1: gplus = g = 255; break;
302 case 2: bplus = b = 255; break;
305 switch (histogram->channel_mode)
308 if (r == 255 && g == 255 && b == 255)
313 case HCHAN_R: b = 0; g = 0; break;
314 case HCHAN_G: r = 0; b = 0; break;
315 case HCHAN_B: r = 0; g = 0; break;
317 case HCHAN_VAL: r = 0; b = 0; g = 0; break;
322 else if (histogram->log_mode)
323 pt = ((gdouble)log(v[chanmax])) / logmax * (height - 1);
325 pt = ((gdouble)v[chanmax]) / max * (height - 1);
327 pixbuf_draw_line(pixbuf,
329 xpos, ypos, xpos, ypos - pt,
340 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
342 if (type != NOTIFY_TYPE_INTERNAL && fd->histmap)
349 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */