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 max[HISTMAP_SIZE];
36 gint channel_mode; /* drawing mode for histogram */
37 gint log_mode; /* logarithmical or not */
38 guint vgrid; /* number of vertical divisions, 0 for none */
39 guint hgrid; /* number of horizontal divisions, 0 for none */
45 } grid_color; /* grid color */
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;
60 histogram->grid_color.R = 160;
61 histogram->grid_color.G = 160;
62 histogram->grid_color.B = 160;
63 histogram->grid_color.A = 250;
68 void histogram_free(Histogram *histogram)
74 gint histogram_set_channel(Histogram *histogram, gint chan)
76 if (!histogram) return 0;
77 options->histogram.last_channel_mode = histogram->channel_mode = chan;
81 gint histogram_get_channel(Histogram *histogram)
83 if (!histogram) return 0;
84 return histogram->channel_mode;
87 gint histogram_set_mode(Histogram *histogram, gint mode)
89 if (!histogram) return 0;
90 options->histogram.last_log_mode = histogram->log_mode = mode;
94 gint histogram_get_mode(Histogram *histogram)
96 if (!histogram) return 0;
97 return histogram->log_mode;
100 gint histogram_toggle_channel(Histogram *histogram)
102 if (!histogram) return 0;
103 return histogram_set_channel(histogram, (histogram_get_channel(histogram)+1)%HCHAN_COUNT);
106 gint histogram_toggle_mode(Histogram *histogram)
108 if (!histogram) return 0;
109 return histogram_set_mode(histogram, !histogram_get_mode(histogram));
112 const gchar *histogram_label(Histogram *histogram)
114 const gchar *t1 = "";
116 if (!histogram) return NULL;
118 if (histogram->log_mode)
119 switch (histogram->channel_mode)
121 case HCHAN_R: t1 = _("logarithmical histogram on red"); break;
122 case HCHAN_G: t1 = _("logarithmical histogram on green"); break;
123 case HCHAN_B: t1 = _("logarithmical histogram on blue"); break;
124 case HCHAN_RGB: t1 = _("logarithmical histogram on RGB"); break;
125 case HCHAN_MAX: t1 = _("logarithmical histogram on max value"); break;
128 switch (histogram->channel_mode)
130 case HCHAN_R: t1 = _("linear histogram on red"); break;
131 case HCHAN_G: t1 = _("linear histogram on green"); break;
132 case HCHAN_B: t1 = _("linear histogram on blue"); break;
133 case HCHAN_RGB: t1 = _("linear histogram on RGB"); break;
134 case HCHAN_MAX: t1 = _("linear histogram on max value"); break;
139 static HistMap *histmap_read(GdkPixbuf *imgpixbuf)
141 gint w, h, i, j, srs, has_alpha, step;
145 w = gdk_pixbuf_get_width(imgpixbuf);
146 h = gdk_pixbuf_get_height(imgpixbuf);
147 srs = gdk_pixbuf_get_rowstride(imgpixbuf);
148 s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
149 has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
151 histmap = g_new0(HistMap, 1);
153 step = 3 + !!(has_alpha);
154 for (i = 0; i < h; i++)
156 guchar *sp = s_pix + (i * srs); /* 8bit */
157 for (j = 0; j < w; j++)
160 if (sp[1] > max) max = sp[1];
161 if (sp[2] > max) max = sp[2];
175 const HistMap *histmap_get(FileData *fd)
177 if (fd->histmap) return fd->histmap;
181 fd->histmap = histmap_read(fd->pixbuf);
187 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
192 if (histogram->vgrid == 0) return;
194 add = width / (float)histogram->vgrid;
196 for (i = 1; i < histogram->vgrid; i++)
198 gint xpos = x + (int)(i * add + 0.5);
200 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
201 histogram->grid_color.R,
202 histogram->grid_color.G,
203 histogram->grid_color.B,
204 histogram->grid_color.A);
208 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
213 if (histogram->hgrid == 0) return;
215 add = height / (float)histogram->hgrid;
217 for (i = 1; i < histogram->hgrid; i++)
219 gint ypos = y + (int)(i * add + 0.5);
221 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
222 histogram->grid_color.R,
223 histogram->grid_color.G,
224 histogram->grid_color.B,
225 histogram->grid_color.A);
229 gint histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
231 /* FIXME: use the coordinates correctly */
235 gint combine = (HISTMAP_SIZE - 1) / width + 1;
236 gint ypos = y + height;
238 if (!histogram || !histmap) return 0;
241 histogram_vgrid(histogram, pixbuf, x, y, width, height);
242 histogram_hgrid(histogram, pixbuf, x, y, width, height);
244 for (i = 0; i < HISTMAP_SIZE; i++)
246 if (histmap->r[i] > max) max = histmap->r[i];
247 if (histmap->g[i] > max) max = histmap->g[i];
248 if (histmap->b[i] > max) max = histmap->b[i];
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 v[3] += histmap->max[p];
275 for (j = 0; combine > 1 && j < 4; j++)
278 for (j = 0; j < 4; j++)
283 for (k = 1; k < 3; k++)
284 if (v[k] > v[chanmax])
287 if (histogram->channel_mode >= HCHAN_RGB
288 || chanmax == histogram->channel_mode)
297 case 0: rplus = r = 255; break;
298 case 1: gplus = g = 255; break;
299 case 2: bplus = b = 255; break;
302 switch (histogram->channel_mode)
305 if (r == 255 && g == 255 && b == 255)
310 case HCHAN_R: b = 0; g = 0; break;
311 case HCHAN_G: r = 0; b = 0; break;
312 case HCHAN_B: r = 0; g = 0; break;
313 case HCHAN_MAX: r = 0; b = 0; g = 0; break;
318 else if (histogram->log_mode)
319 pt = ((gdouble)log(v[chanmax])) / logmax * (height - 1);
321 pt = ((gdouble)v[chanmax]) / max * (height - 1);
323 pixbuf_draw_line(pixbuf,
325 xpos, ypos, xpos, ypos - pt,
336 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
338 if (type != NOTIFY_TYPE_INTERNAL && fd->histmap)
345 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */