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 *----------------------------------------------------------------------------
41 constexpr gint HISTMAP_SIZE = 256;
46 gulong r[HISTMAP_SIZE];
47 gulong g[HISTMAP_SIZE];
48 gulong b[HISTMAP_SIZE];
49 gulong max[HISTMAP_SIZE];
51 guint idle_id; /* event source id */
57 Histogram *histogram_new()
61 histogram = g_new0(Histogram, 1);
62 histogram->histogram_channel = HCHAN_DEFAULT;
63 histogram->histogram_mode = 0;
68 histogram->grid_color.R = 160;
69 histogram->grid_color.G = 160;
70 histogram->grid_color.B = 160;
71 histogram->grid_color.A = 250;
76 void histogram_free(Histogram *histogram)
82 gint histogram_set_channel(Histogram *histogram, gint chan)
84 if (!histogram) return 0;
85 histogram->histogram_channel = chan;
89 gint histogram_get_channel(Histogram *histogram)
91 if (!histogram) return 0;
92 return histogram->histogram_channel;
95 gint histogram_set_mode(Histogram *histogram, gint mode)
97 if (!histogram) return 0;
98 histogram->histogram_mode = mode;
102 gint histogram_get_mode(Histogram *histogram)
104 if (!histogram) return 0;
105 return histogram->histogram_mode;
108 gint histogram_toggle_channel(Histogram *histogram)
110 if (!histogram) return 0;
111 return histogram_set_channel(histogram, (histogram_get_channel(histogram)+1)%HCHAN_COUNT);
114 gint histogram_toggle_mode(Histogram *histogram)
116 if (!histogram) return 0;
117 return histogram_set_mode(histogram, !histogram_get_mode(histogram));
120 const gchar *histogram_label(Histogram *histogram)
122 const gchar *t1 = "";
124 if (!histogram) return nullptr;
126 if (histogram->histogram_mode)
127 switch (histogram->histogram_channel)
129 case HCHAN_R: t1 = _("Log Histogram on Red"); break;
130 case HCHAN_G: t1 = _("Log Histogram on Green"); break;
131 case HCHAN_B: t1 = _("Log Histogram on Blue"); break;
132 case HCHAN_RGB: t1 = _("Log Histogram on RGB"); break;
133 case HCHAN_MAX: t1 = _("Log Histogram on value"); break;
136 switch (histogram->histogram_channel)
138 case HCHAN_R: t1 = _("Linear Histogram on Red"); break;
139 case HCHAN_G: t1 = _("Linear Histogram on Green"); break;
140 case HCHAN_B: t1 = _("Linear Histogram on Blue"); break;
141 case HCHAN_RGB: t1 = _("Linear Histogram on RGB"); break;
142 case HCHAN_MAX: t1 = _("Linear Histogram on value"); break;
147 static HistMap *histmap_new()
149 auto histmap = g_new0(HistMap, 1);
153 void histmap_free(HistMap *histmap)
155 if (!histmap) return;
156 if (histmap->idle_id) g_source_remove(histmap->idle_id);
157 if (histmap->pixbuf) g_object_unref(histmap->pixbuf);
161 static gboolean histmap_read(HistMap *histmap, gboolean whole)
172 GdkPixbuf *imgpixbuf = histmap->pixbuf;
174 w = gdk_pixbuf_get_width(imgpixbuf);
175 h = gdk_pixbuf_get_height(imgpixbuf);
176 srs = gdk_pixbuf_get_rowstride(imgpixbuf);
177 s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
178 has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
186 gint lines = 1 + 16384 / w;
187 end_line = histmap->y + lines;
188 if (end_line > h) end_line = h;
191 step = 3 + !!(has_alpha);
192 for (i = histmap->y; i < end_line; i++)
194 guchar *sp = s_pix + (i * srs); /* 8bit */
195 for (j = 0; j < w; j++)
198 if (sp[1] > max) max = sp[1];
199 if (sp[2] > max) max = sp[2];
209 histmap->y = end_line;
210 return end_line >= h;
213 const HistMap *histmap_get(FileData *fd)
215 if (fd->histmap && !fd->histmap->idle_id) return fd->histmap; /* histmap exists and is finished */
220 static gboolean histmap_idle_cb(gpointer data)
222 auto fd = static_cast<FileData *>(data);
223 if (histmap_read(fd->histmap, FALSE))
226 g_object_unref(fd->histmap->pixbuf); /*pixbuf is no longer needed */
227 fd->histmap->pixbuf = nullptr;
228 fd->histmap->idle_id = 0;
229 file_data_send_notification(fd, NOTIFY_HISTMAP);
230 return G_SOURCE_REMOVE;
232 return G_SOURCE_CONTINUE;
235 gboolean histmap_start_idle(FileData *fd)
237 if (fd->histmap || !fd->pixbuf) return FALSE;
239 fd->histmap = histmap_new();
240 fd->histmap->pixbuf = fd->pixbuf;
241 g_object_ref(fd->histmap->pixbuf);
243 fd->histmap->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, histmap_idle_cb, fd, nullptr);
248 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
253 if (histogram->vgrid == 0) return;
255 add = width / static_cast<float>(histogram->vgrid);
257 for (i = 1; i < histogram->vgrid; i++)
259 gint xpos = x + static_cast<int>(i * add + 0.5);
261 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
262 histogram->grid_color.R,
263 histogram->grid_color.G,
264 histogram->grid_color.B,
265 histogram->grid_color.A);
269 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
274 if (histogram->hgrid == 0) return;
276 add = height / static_cast<float>(histogram->hgrid);
278 for (i = 1; i < histogram->hgrid; i++)
280 gint ypos = y + static_cast<int>(i * add + 0.5);
282 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
283 histogram->grid_color.R,
284 histogram->grid_color.G,
285 histogram->grid_color.B,
286 histogram->grid_color.A);
290 gboolean histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
292 /** @FIXME use the coordinates correctly */
296 gint combine = (HISTMAP_SIZE - 1) / width + 1;
297 gint ypos = y + height;
299 if (!histogram || !histmap) return FALSE;
302 histogram_vgrid(histogram, pixbuf, x, y, width, height);
303 histogram_hgrid(histogram, pixbuf, x, y, width, height);
305 /* exclude overexposed and underexposed */
306 for (i = 1; i < HISTMAP_SIZE - 1; i++)
308 if (histmap->r[i] > max) max = histmap->r[i];
309 if (histmap->g[i] > max) max = histmap->g[i];
310 if (histmap->b[i] > max) max = histmap->b[i];
311 if (histmap->max[i] > max) max = histmap->max[i];
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;
370 switch (histogram->histogram_channel)
373 if (r == 255 && g == 255 && b == 255)
378 case HCHAN_R: b = 0; g = 0; break;
379 case HCHAN_G: r = 0; b = 0; break;
380 case HCHAN_B: r = 0; g = 0; break;
381 case HCHAN_MAX: r = 0; b = 0; g = 0; break;
386 else if (histogram->histogram_mode)
387 pt = (static_cast<gdouble>(log(v[chanmax]))) / logmax * (height - 1);
389 pt = (static_cast<gdouble>(v[chanmax])) / max * (height - 1);
391 pixbuf_draw_line(pixbuf,
393 xpos, ypos, xpos, ypos - pt,
404 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer)
406 if ((type & NOTIFY_REREAD) && fd->histmap)
408 DEBUG_1("Notify histogram: %s %04x", fd->path, type);
409 histmap_free(fd->histmap);
410 fd->histmap = nullptr;
414 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */