2 * Copyright (C) 2008 - 2016 The Geeqie Team
4 * Author: Vladimir Nadvornik
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "histogram.h"
24 #include "pixbuf_util.h"
30 *----------------------------------------------------------------------------
32 *----------------------------------------------------------------------------
35 #define HISTMAP_SIZE 256
38 gulong r[HISTMAP_SIZE];
39 gulong g[HISTMAP_SIZE];
40 gulong b[HISTMAP_SIZE];
41 gulong max[HISTMAP_SIZE];
43 guint idle_id; /* event source id */
49 Histogram *histogram_new(void)
53 histogram = g_new0(Histogram, 1);
54 histogram->histogram_channel = HCHAN_DEFAULT;
55 histogram->histogram_mode = 0;
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 histogram->histogram_channel = chan;
81 gint histogram_get_channel(Histogram *histogram)
83 if (!histogram) return 0;
84 return histogram->histogram_channel;
87 gint histogram_set_mode(Histogram *histogram, gint mode)
89 if (!histogram) return 0;
90 histogram->histogram_mode = mode;
94 gint histogram_get_mode(Histogram *histogram)
96 if (!histogram) return 0;
97 return histogram->histogram_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->histogram_mode)
119 switch (histogram->histogram_channel)
121 case HCHAN_R: t1 = _("Log Histogram on Red"); break;
122 case HCHAN_G: t1 = _("Log Histogram on Green"); break;
123 case HCHAN_B: t1 = _("Log Histogram on Blue"); break;
124 case HCHAN_RGB: t1 = _("Log Histogram on RGB"); break;
125 case HCHAN_MAX: t1 = _("Log Histogram on value"); break;
128 switch (histogram->histogram_channel)
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 value"); break;
139 static HistMap *histmap_new(void)
141 HistMap *histmap = g_new0(HistMap, 1);
145 void histmap_free(HistMap *histmap)
147 if (!histmap) return;
148 if (histmap->idle_id) g_source_remove(histmap->idle_id);
149 if (histmap->pixbuf) g_object_unref(histmap->pixbuf);
153 static gboolean histmap_read(HistMap *histmap, gboolean whole)
155 gint w, h, i, j, srs, has_alpha, step, end_line;
157 GdkPixbuf *imgpixbuf = histmap->pixbuf;
159 w = gdk_pixbuf_get_width(imgpixbuf);
160 h = gdk_pixbuf_get_height(imgpixbuf);
161 srs = gdk_pixbuf_get_rowstride(imgpixbuf);
162 s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
163 has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
171 gint lines = 1 + 16384 / w;
172 end_line = histmap->y + lines;
173 if (end_line > h) end_line = h;
176 step = 3 + !!(has_alpha);
177 for (i = histmap->y; i < end_line; i++)
179 guchar *sp = s_pix + (i * srs); /* 8bit */
180 for (j = 0; j < w; j++)
183 if (sp[1] > max) max = sp[1];
184 if (sp[2] > max) max = sp[2];
194 histmap->y = end_line;
195 return end_line >= h;
198 const HistMap *histmap_get(FileData *fd)
200 if (fd->histmap && !fd->histmap->idle_id) return fd->histmap; /* histmap exists and is finished */
205 static gboolean histmap_idle_cb(gpointer data)
208 if (histmap_read(fd->histmap, FALSE))
211 g_object_unref(fd->histmap->pixbuf); /*pixbuf is no longer needed */
212 fd->histmap->pixbuf = NULL;
213 fd->histmap->idle_id = 0;
214 file_data_send_notification(fd, NOTIFY_HISTMAP);
220 gboolean histmap_start_idle(FileData *fd)
222 if (fd->histmap || !fd->pixbuf) return FALSE;
224 fd->histmap = histmap_new();
225 fd->histmap->pixbuf = fd->pixbuf;
226 g_object_ref(fd->histmap->pixbuf);
228 fd->histmap->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, histmap_idle_cb, fd, NULL);
233 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
238 if (histogram->vgrid == 0) return;
240 add = width / (float)histogram->vgrid;
242 for (i = 1; i < histogram->vgrid; i++)
244 gint xpos = x + (int)(i * add + 0.5);
246 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
247 histogram->grid_color.R,
248 histogram->grid_color.G,
249 histogram->grid_color.B,
250 histogram->grid_color.A);
254 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
259 if (histogram->hgrid == 0) return;
261 add = height / (float)histogram->hgrid;
263 for (i = 1; i < histogram->hgrid; i++)
265 gint ypos = y + (int)(i * add + 0.5);
267 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
268 histogram->grid_color.R,
269 histogram->grid_color.G,
270 histogram->grid_color.B,
271 histogram->grid_color.A);
275 gboolean histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
277 /* FIXME: use the coordinates correctly */
281 gint combine = (HISTMAP_SIZE - 1) / width + 1;
282 gint ypos = y + height;
284 if (!histogram || !histmap) return FALSE;
287 histogram_vgrid(histogram, pixbuf, x, y, width, height);
288 histogram_hgrid(histogram, pixbuf, x, y, width, height);
290 /* exclude overexposed and underexposed */
291 for (i = 1; i < HISTMAP_SIZE - 1; i++)
293 if (histmap->r[i] > max) max = histmap->r[i];
294 if (histmap->g[i] > max) max = histmap->g[i];
295 if (histmap->b[i] > max) max = histmap->b[i];
296 if (histmap->max[i] > max) max = histmap->max[i];
304 for (i = 0; i < width; i++)
307 glong v[4] = {0, 0, 0, 0};
311 gint ii = i * HISTMAP_SIZE / width;
315 for (j = 0; j < combine; j++)
318 v[0] += histmap->r[p];
319 v[1] += histmap->g[p];
320 v[2] += histmap->b[p];
321 v[3] += histmap->max[p];
324 for (j = 0; combine > 1 && j < 4; j++)
327 num_chan = (histogram->histogram_channel == HCHAN_RGB) ? 3 : 1;
328 for (j = 0; j < num_chan; j++)
331 if (histogram->histogram_channel == HCHAN_RGB)
334 if (v[HCHAN_G] > v[HCHAN_R]) chanmax = HCHAN_G;
335 if (v[HCHAN_B] > v[chanmax]) chanmax = HCHAN_B;
339 chanmax = histogram->histogram_channel;
350 case HCHAN_R: rplus = r = 255; break;
351 case HCHAN_G: gplus = g = 255; break;
352 case HCHAN_B: bplus = b = 255; break;
355 switch (histogram->histogram_channel)
358 if (r == 255 && g == 255 && b == 255)
363 case HCHAN_R: b = 0; g = 0; break;
364 case HCHAN_G: r = 0; b = 0; break;
365 case HCHAN_B: r = 0; g = 0; break;
366 case HCHAN_MAX: r = 0; b = 0; g = 0; break;
371 else if (histogram->histogram_mode)
372 pt = ((gdouble)log(v[chanmax])) / logmax * (height - 1);
374 pt = ((gdouble)v[chanmax]) / max * (height - 1);
376 pixbuf_draw_line(pixbuf,
378 xpos, ypos, xpos, ypos - pt,
389 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
391 if ((type & NOTIFY_REREAD) && fd->histmap)
393 DEBUG_1("Notify histogram: %s %04x", fd->path, type);
394 histmap_free(fd->histmap);
399 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */