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"
26 #include <glib-object.h>
31 #include "pixbuf-util.h"
34 *----------------------------------------------------------------------------
36 *----------------------------------------------------------------------------
42 constexpr gint HISTMAP_SIZE = 256;
47 gulong r[HISTMAP_SIZE];
48 gulong g[HISTMAP_SIZE];
49 gulong b[HISTMAP_SIZE];
50 gulong max[HISTMAP_SIZE];
52 guint idle_id; /* event source id */
58 Histogram *histogram_new()
62 histogram = g_new0(Histogram, 1);
63 histogram->histogram_channel = HCHAN_DEFAULT;
64 histogram->histogram_mode = 0;
69 histogram->grid_color.R = 160;
70 histogram->grid_color.G = 160;
71 histogram->grid_color.B = 160;
72 histogram->grid_color.A = 250;
77 void histogram_free(Histogram *histogram)
83 gint histogram_set_channel(Histogram *histogram, gint chan)
85 if (!histogram) return 0;
86 histogram->histogram_channel = chan;
90 gint histogram_get_channel(Histogram *histogram)
92 if (!histogram) return 0;
93 return histogram->histogram_channel;
96 gint histogram_set_mode(Histogram *histogram, gint mode)
98 if (!histogram) return 0;
99 histogram->histogram_mode = mode;
103 gint histogram_get_mode(Histogram *histogram)
105 if (!histogram) return 0;
106 return histogram->histogram_mode;
109 gint histogram_toggle_channel(Histogram *histogram)
111 if (!histogram) return 0;
112 return histogram_set_channel(histogram, (histogram_get_channel(histogram)+1)%HCHAN_COUNT);
115 gint histogram_toggle_mode(Histogram *histogram)
117 if (!histogram) return 0;
118 return histogram_set_mode(histogram, !histogram_get_mode(histogram));
121 const gchar *histogram_label(Histogram *histogram)
123 const gchar *t1 = "";
125 if (!histogram) return nullptr;
127 if (histogram->histogram_mode)
128 switch (histogram->histogram_channel)
130 case HCHAN_R: t1 = _("Log Histogram on Red"); break;
131 case HCHAN_G: t1 = _("Log Histogram on Green"); break;
132 case HCHAN_B: t1 = _("Log Histogram on Blue"); break;
133 case HCHAN_RGB: t1 = _("Log Histogram on RGB"); break;
134 case HCHAN_MAX: t1 = _("Log Histogram on value"); break;
139 switch (histogram->histogram_channel)
141 case HCHAN_R: t1 = _("Linear Histogram on Red"); break;
142 case HCHAN_G: t1 = _("Linear Histogram on Green"); break;
143 case HCHAN_B: t1 = _("Linear Histogram on Blue"); break;
144 case HCHAN_RGB: t1 = _("Linear Histogram on RGB"); break;
145 case HCHAN_MAX: t1 = _("Linear Histogram on value"); break;
152 static HistMap *histmap_new()
154 auto histmap = g_new0(HistMap, 1);
158 void histmap_free(HistMap *histmap)
160 if (!histmap) return;
161 if (histmap->idle_id) g_source_remove(histmap->idle_id);
162 if (histmap->pixbuf) g_object_unref(histmap->pixbuf);
166 static gboolean histmap_read(HistMap *histmap, gboolean whole)
177 GdkPixbuf *imgpixbuf = histmap->pixbuf;
179 w = gdk_pixbuf_get_width(imgpixbuf);
180 h = gdk_pixbuf_get_height(imgpixbuf);
181 srs = gdk_pixbuf_get_rowstride(imgpixbuf);
182 s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
183 has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
191 gint lines = 1 + 16384 / w;
192 end_line = histmap->y + lines;
193 if (end_line > h) end_line = h;
196 step = 3 + !!(has_alpha);
197 for (i = histmap->y; i < end_line; i++)
199 guchar *sp = s_pix + (i * srs); /* 8bit */
200 for (j = 0; j < w; j++)
202 guint max = std::max({sp[0], sp[1], sp[2]});
212 histmap->y = end_line;
213 return end_line >= h;
216 const HistMap *histmap_get(FileData *fd)
218 if (fd->histmap && !fd->histmap->idle_id) return fd->histmap; /* histmap exists and is finished */
223 static gboolean histmap_idle_cb(gpointer data)
225 auto fd = static_cast<FileData *>(data);
226 if (histmap_read(fd->histmap, FALSE))
229 g_object_unref(fd->histmap->pixbuf); /*pixbuf is no longer needed */
230 fd->histmap->pixbuf = nullptr;
231 fd->histmap->idle_id = 0;
232 file_data_send_notification(fd, NOTIFY_HISTMAP);
233 return G_SOURCE_REMOVE;
235 return G_SOURCE_CONTINUE;
238 gboolean histmap_start_idle(FileData *fd)
240 if (fd->histmap || !fd->pixbuf) return FALSE;
242 fd->histmap = histmap_new();
243 fd->histmap->pixbuf = fd->pixbuf;
244 g_object_ref(fd->histmap->pixbuf);
246 fd->histmap->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, histmap_idle_cb, fd, nullptr);
251 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
256 if (histogram->vgrid == 0) return;
258 add = width / static_cast<float>(histogram->vgrid);
260 for (i = 1; i < histogram->vgrid; i++)
262 gint xpos = x + static_cast<int>(i * add + 0.5);
264 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
265 histogram->grid_color.R,
266 histogram->grid_color.G,
267 histogram->grid_color.B,
268 histogram->grid_color.A);
272 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
277 if (histogram->hgrid == 0) return;
279 add = height / static_cast<float>(histogram->hgrid);
281 for (i = 1; i < histogram->hgrid; i++)
283 gint ypos = y + static_cast<int>(i * add + 0.5);
285 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
286 histogram->grid_color.R,
287 histogram->grid_color.G,
288 histogram->grid_color.B,
289 histogram->grid_color.A);
293 gboolean histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
295 /** @FIXME use the coordinates correctly */
299 gint combine = (HISTMAP_SIZE - 1) / width + 1;
300 gint ypos = y + height;
302 if (!histogram || !histmap) return FALSE;
305 histogram_vgrid(histogram, pixbuf, x, y, width, height);
306 histogram_hgrid(histogram, pixbuf, x, y, width, height);
308 /* exclude overexposed and underexposed */
309 for (i = 1; i < HISTMAP_SIZE - 1; i++)
311 max = std::max({histmap->r[i], histmap->g[i], histmap->b[i], histmap->max[i], max});
319 for (i = 0; i < width; i++)
322 glong v[4] = {0, 0, 0, 0};
326 gint ii = i * HISTMAP_SIZE / width;
330 for (j = 0; j < combine; j++)
333 v[0] += histmap->r[p];
334 v[1] += histmap->g[p];
335 v[2] += histmap->b[p];
336 v[3] += histmap->max[p];
339 for (j = 0; combine > 1 && j < 4; j++)
342 num_chan = (histogram->histogram_channel == HCHAN_RGB) ? 3 : 1;
343 for (j = 0; j < num_chan; j++)
346 if (histogram->histogram_channel == HCHAN_RGB)
349 if (v[HCHAN_G] > v[HCHAN_R]) chanmax = HCHAN_G;
350 if (v[HCHAN_B] > v[chanmax]) chanmax = HCHAN_B;
354 chanmax = histogram->histogram_channel;
365 case HCHAN_R: rplus = r = 255; break;
366 case HCHAN_G: gplus = g = 255; break;
367 case HCHAN_B: bplus = b = 255; break;
372 switch (histogram->histogram_channel)
375 if (r == 255 && g == 255 && b == 255)
380 case HCHAN_R: b = 0; g = 0; break;
381 case HCHAN_G: r = 0; b = 0; break;
382 case HCHAN_B: r = 0; g = 0; break;
383 case HCHAN_MAX: r = 0; b = 0; g = 0; break;
390 else if (histogram->histogram_mode)
391 pt = (static_cast<gdouble>(log(v[chanmax]))) / logmax * (height - 1);
393 pt = (static_cast<gdouble>(v[chanmax])) / max * (height - 1);
395 pixbuf_draw_line(pixbuf,
397 xpos, ypos, xpos, ypos - pt,
408 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer)
410 if ((type & NOTIFY_REREAD) && fd->histmap)
412 DEBUG_1("Notify histogram: %s %04x", fd->path, type);
413 histmap_free(fd->histmap);
414 fd->histmap = nullptr;
418 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */