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.
21 #include "histogram.h"
25 #include <glib-object.h>
30 #include "pixbuf-util.h"
33 *----------------------------------------------------------------------------
35 *----------------------------------------------------------------------------
43 gulong r[HISTMAP_SIZE];
44 gulong g[HISTMAP_SIZE];
45 gulong b[HISTMAP_SIZE];
46 gulong max[HISTMAP_SIZE];
48 guint idle_id; /* event source id */
54 Histogram *histogram_new()
58 histogram = g_new0(Histogram, 1);
59 histogram->histogram_channel = HCHAN_DEFAULT;
60 histogram->histogram_mode = 0;
65 histogram->grid_color.R = 160;
66 histogram->grid_color.G = 160;
67 histogram->grid_color.B = 160;
68 histogram->grid_color.A = 250;
73 void histogram_free(Histogram *histogram)
79 gint histogram_set_channel(Histogram *histogram, gint chan)
81 if (!histogram) return 0;
82 histogram->histogram_channel = chan;
86 gint histogram_get_channel(Histogram *histogram)
88 if (!histogram) return 0;
89 return histogram->histogram_channel;
92 gint histogram_set_mode(Histogram *histogram, gint mode)
94 if (!histogram) return 0;
95 histogram->histogram_mode = mode;
99 gint histogram_get_mode(Histogram *histogram)
101 if (!histogram) return 0;
102 return histogram->histogram_mode;
105 gint histogram_toggle_channel(Histogram *histogram)
107 if (!histogram) return 0;
108 return histogram_set_channel(histogram, (histogram_get_channel(histogram)+1)%HCHAN_COUNT);
111 gint histogram_toggle_mode(Histogram *histogram)
113 if (!histogram) return 0;
114 return histogram_set_mode(histogram, !histogram_get_mode(histogram));
117 const gchar *histogram_label(Histogram *histogram)
119 const gchar *t1 = "";
121 if (!histogram) return nullptr;
123 if (histogram->histogram_mode)
124 switch (histogram->histogram_channel)
126 case HCHAN_R: t1 = _("Log Histogram on Red"); break;
127 case HCHAN_G: t1 = _("Log Histogram on Green"); break;
128 case HCHAN_B: t1 = _("Log Histogram on Blue"); break;
129 case HCHAN_RGB: t1 = _("Log Histogram on RGB"); break;
130 case HCHAN_MAX: t1 = _("Log Histogram on value"); break;
133 switch (histogram->histogram_channel)
135 case HCHAN_R: t1 = _("Linear Histogram on Red"); break;
136 case HCHAN_G: t1 = _("Linear Histogram on Green"); break;
137 case HCHAN_B: t1 = _("Linear Histogram on Blue"); break;
138 case HCHAN_RGB: t1 = _("Linear Histogram on RGB"); break;
139 case HCHAN_MAX: t1 = _("Linear Histogram on value"); break;
144 static HistMap *histmap_new()
146 auto histmap = g_new0(HistMap, 1);
150 void histmap_free(HistMap *histmap)
152 if (!histmap) return;
153 if (histmap->idle_id) g_source_remove(histmap->idle_id);
154 if (histmap->pixbuf) g_object_unref(histmap->pixbuf);
158 static gboolean histmap_read(HistMap *histmap, gboolean whole)
169 GdkPixbuf *imgpixbuf = histmap->pixbuf;
171 w = gdk_pixbuf_get_width(imgpixbuf);
172 h = gdk_pixbuf_get_height(imgpixbuf);
173 srs = gdk_pixbuf_get_rowstride(imgpixbuf);
174 s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
175 has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
183 gint lines = 1 + 16384 / w;
184 end_line = histmap->y + lines;
185 if (end_line > h) end_line = h;
188 step = 3 + !!(has_alpha);
189 for (i = histmap->y; i < end_line; i++)
191 guchar *sp = s_pix + (i * srs); /* 8bit */
192 for (j = 0; j < w; j++)
195 if (sp[1] > max) max = sp[1];
196 if (sp[2] > max) max = sp[2];
206 histmap->y = end_line;
207 return end_line >= h;
210 const HistMap *histmap_get(FileData *fd)
212 if (fd->histmap && !fd->histmap->idle_id) return fd->histmap; /* histmap exists and is finished */
217 static gboolean histmap_idle_cb(gpointer data)
219 auto fd = static_cast<FileData *>(data);
220 if (histmap_read(fd->histmap, FALSE))
223 g_object_unref(fd->histmap->pixbuf); /*pixbuf is no longer needed */
224 fd->histmap->pixbuf = nullptr;
225 fd->histmap->idle_id = 0;
226 file_data_send_notification(fd, NOTIFY_HISTMAP);
227 return G_SOURCE_REMOVE;
229 return G_SOURCE_CONTINUE;
232 gboolean histmap_start_idle(FileData *fd)
234 if (fd->histmap || !fd->pixbuf) return FALSE;
236 fd->histmap = histmap_new();
237 fd->histmap->pixbuf = fd->pixbuf;
238 g_object_ref(fd->histmap->pixbuf);
240 fd->histmap->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, histmap_idle_cb, fd, nullptr);
245 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
250 if (histogram->vgrid == 0) return;
252 add = width / static_cast<float>(histogram->vgrid);
254 for (i = 1; i < histogram->vgrid; i++)
256 gint xpos = x + static_cast<int>(i * add + 0.5);
258 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
259 histogram->grid_color.R,
260 histogram->grid_color.G,
261 histogram->grid_color.B,
262 histogram->grid_color.A);
266 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
271 if (histogram->hgrid == 0) return;
273 add = height / static_cast<float>(histogram->hgrid);
275 for (i = 1; i < histogram->hgrid; i++)
277 gint ypos = y + static_cast<int>(i * add + 0.5);
279 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
280 histogram->grid_color.R,
281 histogram->grid_color.G,
282 histogram->grid_color.B,
283 histogram->grid_color.A);
287 gboolean histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
289 /** @FIXME use the coordinates correctly */
293 gint combine = (HISTMAP_SIZE - 1) / width + 1;
294 gint ypos = y + height;
296 if (!histogram || !histmap) return FALSE;
299 histogram_vgrid(histogram, pixbuf, x, y, width, height);
300 histogram_hgrid(histogram, pixbuf, x, y, width, height);
302 /* exclude overexposed and underexposed */
303 for (i = 1; i < HISTMAP_SIZE - 1; i++)
305 if (histmap->r[i] > max) max = histmap->r[i];
306 if (histmap->g[i] > max) max = histmap->g[i];
307 if (histmap->b[i] > max) max = histmap->b[i];
308 if (histmap->max[i] > max) max = histmap->max[i];
316 for (i = 0; i < width; i++)
319 glong v[4] = {0, 0, 0, 0};
323 gint ii = i * HISTMAP_SIZE / width;
327 for (j = 0; j < combine; j++)
330 v[0] += histmap->r[p];
331 v[1] += histmap->g[p];
332 v[2] += histmap->b[p];
333 v[3] += histmap->max[p];
336 for (j = 0; combine > 1 && j < 4; j++)
339 num_chan = (histogram->histogram_channel == HCHAN_RGB) ? 3 : 1;
340 for (j = 0; j < num_chan; j++)
343 if (histogram->histogram_channel == HCHAN_RGB)
346 if (v[HCHAN_G] > v[HCHAN_R]) chanmax = HCHAN_G;
347 if (v[HCHAN_B] > v[chanmax]) chanmax = HCHAN_B;
351 chanmax = histogram->histogram_channel;
362 case HCHAN_R: rplus = r = 255; break;
363 case HCHAN_G: gplus = g = 255; break;
364 case HCHAN_B: bplus = b = 255; break;
367 switch (histogram->histogram_channel)
370 if (r == 255 && g == 255 && b == 255)
375 case HCHAN_R: b = 0; g = 0; break;
376 case HCHAN_G: r = 0; b = 0; break;
377 case HCHAN_B: r = 0; g = 0; break;
378 case HCHAN_MAX: r = 0; b = 0; g = 0; break;
383 else if (histogram->histogram_mode)
384 pt = (static_cast<gdouble>(log(v[chanmax]))) / logmax * (height - 1);
386 pt = (static_cast<gdouble>(v[chanmax])) / max * (height - 1);
388 pixbuf_draw_line(pixbuf,
390 xpos, ypos, xpos, ypos - pt,
401 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer)
403 if ((type & NOTIFY_REREAD) && fd->histmap)
405 DEBUG_1("Notify histogram: %s %04x", fd->path, type);
406 histmap_free(fd->histmap);
407 fd->histmap = nullptr;
411 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */