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"
22 *----------------------------------------------------------------------------
24 *----------------------------------------------------------------------------
27 #define HISTMAP_SIZE 256
30 gulong r[HISTMAP_SIZE];
31 gulong g[HISTMAP_SIZE];
32 gulong b[HISTMAP_SIZE];
33 gulong max[HISTMAP_SIZE];
41 Histogram *histogram_new(void)
45 histogram = g_new0(Histogram, 1);
46 histogram->histogram_channel = HCHAN_RGB;
47 histogram->histogram_mode = 0;
52 histogram->grid_color.R = 160;
53 histogram->grid_color.G = 160;
54 histogram->grid_color.B = 160;
55 histogram->grid_color.A = 250;
60 void histogram_free(Histogram *histogram)
66 gint histogram_set_channel(Histogram *histogram, gint chan)
68 if (!histogram) return 0;
69 histogram->histogram_channel = chan;
73 gint histogram_get_channel(Histogram *histogram)
75 if (!histogram) return 0;
76 return histogram->histogram_channel;
79 gint histogram_set_mode(Histogram *histogram, gint mode)
81 if (!histogram) return 0;
82 histogram->histogram_mode = mode;
86 gint histogram_get_mode(Histogram *histogram)
88 if (!histogram) return 0;
89 return histogram->histogram_mode;
92 gint histogram_toggle_channel(Histogram *histogram)
94 if (!histogram) return 0;
95 return histogram_set_channel(histogram, (histogram_get_channel(histogram)+1)%HCHAN_COUNT);
98 gint histogram_toggle_mode(Histogram *histogram)
100 if (!histogram) return 0;
101 return histogram_set_mode(histogram, !histogram_get_mode(histogram));
104 const gchar *histogram_label(Histogram *histogram)
106 const gchar *t1 = "";
108 if (!histogram) return NULL;
110 if (histogram->histogram_mode)
111 switch (histogram->histogram_channel)
113 case HCHAN_R: t1 = _("Log Histogram on Red"); break;
114 case HCHAN_G: t1 = _("Log Histogram on Green"); break;
115 case HCHAN_B: t1 = _("Log Histogram on Blue"); break;
116 case HCHAN_RGB: t1 = _("Log Histogram on RGB"); break;
117 case HCHAN_MAX: t1 = _("Log Histogram on value"); break;
120 switch (histogram->histogram_channel)
122 case HCHAN_R: t1 = _("Linear Histogram on Red"); break;
123 case HCHAN_G: t1 = _("Linear Histogram on Green"); break;
124 case HCHAN_B: t1 = _("Linear Histogram on Blue"); break;
125 case HCHAN_RGB: t1 = _("Linear Histogram on RGB"); break;
126 case HCHAN_MAX: t1 = _("Linear Histogram on value"); break;
131 static HistMap *histmap_new(void)
133 HistMap *histmap = g_new0(HistMap, 1);
134 histmap->idle_id = -1;
138 void histmap_free(HistMap *histmap)
140 if (!histmap) return;
141 if (histmap->idle_id != -1) g_source_remove(histmap->idle_id);
142 if (histmap->pixbuf) g_object_unref(histmap->pixbuf);
146 static gboolean histmap_read(HistMap *histmap, gboolean whole)
148 gint w, h, i, j, srs, has_alpha, step, end_line;
150 GdkPixbuf *imgpixbuf = histmap->pixbuf;
152 w = gdk_pixbuf_get_width(imgpixbuf);
153 h = gdk_pixbuf_get_height(imgpixbuf);
154 srs = gdk_pixbuf_get_rowstride(imgpixbuf);
155 s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
156 has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
164 gint lines = 1 + 16384 / w;
165 end_line = histmap->y + lines;
166 if (end_line > h) end_line = h;
169 step = 3 + !!(has_alpha);
170 for (i = histmap->y; i < end_line; i++)
172 guchar *sp = s_pix + (i * srs); /* 8bit */
173 for (j = 0; j < w; j++)
176 if (sp[1] > max) max = sp[1];
177 if (sp[2] > max) max = sp[2];
187 histmap->y = end_line;
188 return end_line >= h;
191 const HistMap *histmap_get(FileData *fd)
193 if (fd->histmap && fd->histmap->idle_id == -1) return fd->histmap; /* histmap exists and is finished */
198 static gboolean histmap_idle_cb(gpointer data)
201 if (histmap_read(fd->histmap, FALSE))
204 g_object_unref(fd->histmap->pixbuf); /*pixbuf is no longer needed */
205 fd->histmap->pixbuf = NULL;
206 fd->histmap->idle_id = -1;
207 file_data_send_notification(fd, NOTIFY_HISTMAP);
213 gboolean histmap_start_idle(FileData *fd)
215 if (fd->histmap || !fd->pixbuf) return FALSE;
217 fd->histmap = histmap_new();
218 fd->histmap->pixbuf = fd->pixbuf;
219 g_object_ref(fd->histmap->pixbuf);
221 fd->histmap->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, histmap_idle_cb, fd, NULL);
226 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
231 if (histogram->vgrid == 0) return;
233 add = width / (float)histogram->vgrid;
235 for (i = 1; i < histogram->vgrid; i++)
237 gint xpos = x + (int)(i * add + 0.5);
239 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
240 histogram->grid_color.R,
241 histogram->grid_color.G,
242 histogram->grid_color.B,
243 histogram->grid_color.A);
247 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
252 if (histogram->hgrid == 0) return;
254 add = height / (float)histogram->hgrid;
256 for (i = 1; i < histogram->hgrid; i++)
258 gint ypos = y + (int)(i * add + 0.5);
260 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
261 histogram->grid_color.R,
262 histogram->grid_color.G,
263 histogram->grid_color.B,
264 histogram->grid_color.A);
268 gboolean histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
270 /* FIXME: use the coordinates correctly */
274 gint combine = (HISTMAP_SIZE - 1) / width + 1;
275 gint ypos = y + height;
277 if (!histogram || !histmap) return FALSE;
280 histogram_vgrid(histogram, pixbuf, x, y, width, height);
281 histogram_hgrid(histogram, pixbuf, x, y, width, height);
283 /* exclude overexposed and underexposed */
284 for (i = 1; i < HISTMAP_SIZE - 1; i++)
286 if (histmap->r[i] > max) max = histmap->r[i];
287 if (histmap->g[i] > max) max = histmap->g[i];
288 if (histmap->b[i] > max) max = histmap->b[i];
289 if (histmap->max[i] > max) max = histmap->max[i];
297 for (i = 0; i < width; i++)
300 glong v[4] = {0, 0, 0, 0};
304 gint ii = i * HISTMAP_SIZE / width;
308 for (j = 0; j < combine; j++)
311 v[0] += histmap->r[p];
312 v[1] += histmap->g[p];
313 v[2] += histmap->b[p];
314 v[3] += histmap->max[p];
317 for (j = 0; combine > 1 && j < 4; j++)
320 num_chan = (histogram->histogram_channel == HCHAN_RGB) ? 3 : 1;
321 for (j = 0; j < num_chan; j++)
324 if (histogram->histogram_channel == HCHAN_RGB)
327 if (v[HCHAN_G] > v[HCHAN_R]) chanmax = HCHAN_G;
328 if (v[HCHAN_B] > v[chanmax]) chanmax = HCHAN_B;
332 chanmax = histogram->histogram_channel;
343 case HCHAN_R: rplus = r = 255; break;
344 case HCHAN_G: gplus = g = 255; break;
345 case HCHAN_B: bplus = b = 255; break;
348 switch (histogram->histogram_channel)
351 if (r == 255 && g == 255 && b == 255)
356 case HCHAN_R: b = 0; g = 0; break;
357 case HCHAN_G: r = 0; b = 0; break;
358 case HCHAN_B: r = 0; g = 0; break;
359 case HCHAN_MAX: r = 0; b = 0; g = 0; break;
364 else if (histogram->histogram_mode)
365 pt = ((gdouble)log(v[chanmax])) / logmax * (height - 1);
367 pt = ((gdouble)v[chanmax]) / max * (height - 1);
369 pixbuf_draw_line(pixbuf,
371 xpos, ypos, xpos, ypos - pt,
382 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
384 if ((type & (NOTIFY_CHANGE || NOTIFY_REREAD)) && fd->histmap)
386 histmap_free(fd->histmap);
391 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */