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 const gchar *histogram_label(Histogram *histogram)
102 const gchar *t1 = "";
104 if (!histogram) return NULL;
106 if (histogram->log_mode)
107 switch (histogram->channel_mode)
109 case HCHAN_R: t1 = _("logarithmical histogram on red"); break;
110 case HCHAN_G: t1 = _("logarithmical histogram on green"); break;
111 case HCHAN_B: t1 = _("logarithmical histogram on blue"); break;
112 case HCHAN_RGB: t1 = _("logarithmical histogram on RGB"); break;
113 case HCHAN_MAX: t1 = _("logarithmical histogram on max value"); break;
116 switch (histogram->channel_mode)
118 case HCHAN_R: t1 = _("linear histogram on red"); break;
119 case HCHAN_G: t1 = _("linear histogram on green"); break;
120 case HCHAN_B: t1 = _("linear histogram on blue"); break;
121 case HCHAN_RGB: t1 = _("linear histogram on RGB"); break;
122 case HCHAN_MAX: t1 = _("linear histogram on max value"); break;
127 static HistMap *histmap_read(GdkPixbuf *imgpixbuf)
129 gint w, h, i, j, srs, has_alpha, step;
133 w = gdk_pixbuf_get_width(imgpixbuf);
134 h = gdk_pixbuf_get_height(imgpixbuf);
135 srs = gdk_pixbuf_get_rowstride(imgpixbuf);
136 s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
137 has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
139 histmap = g_new0(HistMap, 1);
141 step = 3 + !!(has_alpha);
142 for (i = 0; i < h; i++)
144 guchar *sp = s_pix + (i * srs); /* 8bit */
145 for (j = 0; j < w; j++)
148 if (sp[1] > max) max = sp[1];
149 if (sp[2] > max) max = sp[2];
163 const HistMap *histmap_get(FileData *fd)
165 if (fd->histmap) return fd->histmap;
169 fd->histmap = histmap_read(fd->pixbuf);
175 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
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,
189 histogram->grid_color.R,
190 histogram->grid_color.G,
191 histogram->grid_color.B,
192 histogram->grid_color.A);
196 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
201 if (histogram->hgrid == 0) return;
203 add = height / (float)histogram->hgrid;
205 for (i = 1; i < histogram->hgrid; i++)
207 gint ypos = y + (int)(i * add + 0.5);
209 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
210 histogram->grid_color.R,
211 histogram->grid_color.G,
212 histogram->grid_color.B,
213 histogram->grid_color.A);
217 gint histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
219 /* FIXME: use the coordinates correctly */
223 gint combine = (HISTMAP_SIZE - 1) / width + 1;
224 gint ypos = y + height;
226 if (!histogram || !histmap) return 0;
229 histogram_vgrid(histogram, pixbuf, x, y, width, height);
230 histogram_hgrid(histogram, pixbuf, x, y, width, height);
232 for (i = 0; i < HISTMAP_SIZE; i++)
234 if (histmap->r[i] > max) max = histmap->r[i];
235 if (histmap->g[i] > max) max = histmap->g[i];
236 if (histmap->b[i] > max) max = histmap->b[i];
244 for (i = 0; i < width; i++)
247 glong v[4] = {0, 0, 0, 0};
251 gint ii = i * HISTMAP_SIZE / width;
254 for (j = 0; j < combine; j++)
257 v[0] += histmap->r[p];
258 v[1] += histmap->g[p];
259 v[2] += histmap->b[p];
260 v[3] += histmap->max[p];
263 for (j = 0; combine > 1 && j < 4; j++)
266 for (j = 0; j < 4; j++)
271 for (k = 1; k < 3; k++)
272 if (v[k] > v[chanmax])
275 if (histogram->channel_mode >= HCHAN_RGB
276 || chanmax == histogram->channel_mode)
285 case 0: rplus = r = 255; break;
286 case 1: gplus = g = 255; break;
287 case 2: bplus = b = 255; break;
290 switch (histogram->channel_mode)
293 if (r == 255 && g == 255 && b == 255)
298 case HCHAN_R: b = 0; g = 0; break;
299 case HCHAN_G: r = 0; b = 0; break;
300 case HCHAN_B: r = 0; g = 0; break;
301 case HCHAN_MAX: r = 0; b = 0; g = 0; break;
306 else if (histogram->log_mode)
307 pt = ((gdouble)log(v[chanmax])) / logmax * (height - 1);
309 pt = ((gdouble)v[chanmax]) / max * (height - 1);
311 pixbuf_draw_line(pixbuf,
313 xpos, ypos, xpos, ypos - pt,
324 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
326 if (type != NOTIFY_TYPE_INTERNAL && fd->histmap)
333 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */