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 *----------------------------------------------------------------------------
40 gulong r[HISTMAP_SIZE];
41 gulong g[HISTMAP_SIZE];
42 gulong b[HISTMAP_SIZE];
43 gulong max[HISTMAP_SIZE];
45 guint idle_id; /* event source id */
51 Histogram *histogram_new()
55 histogram = g_new0(Histogram, 1);
56 histogram->histogram_channel = HCHAN_DEFAULT;
57 histogram->histogram_mode = 0;
62 histogram->grid_color.R = 160;
63 histogram->grid_color.G = 160;
64 histogram->grid_color.B = 160;
65 histogram->grid_color.A = 250;
70 void histogram_free(Histogram *histogram)
76 gint histogram_set_channel(Histogram *histogram, gint chan)
78 if (!histogram) return 0;
79 histogram->histogram_channel = chan;
83 gint histogram_get_channel(Histogram *histogram)
85 if (!histogram) return 0;
86 return histogram->histogram_channel;
89 gint histogram_set_mode(Histogram *histogram, gint mode)
91 if (!histogram) return 0;
92 histogram->histogram_mode = mode;
96 gint histogram_get_mode(Histogram *histogram)
98 if (!histogram) return 0;
99 return histogram->histogram_mode;
102 gint histogram_toggle_channel(Histogram *histogram)
104 if (!histogram) return 0;
105 return histogram_set_channel(histogram, (histogram_get_channel(histogram)+1)%HCHAN_COUNT);
108 gint histogram_toggle_mode(Histogram *histogram)
110 if (!histogram) return 0;
111 return histogram_set_mode(histogram, !histogram_get_mode(histogram));
114 const gchar *histogram_label(Histogram *histogram)
116 const gchar *t1 = "";
118 if (!histogram) return nullptr;
120 if (histogram->histogram_mode)
121 switch (histogram->histogram_channel)
123 case HCHAN_R: t1 = _("Log Histogram on Red"); break;
124 case HCHAN_G: t1 = _("Log Histogram on Green"); break;
125 case HCHAN_B: t1 = _("Log Histogram on Blue"); break;
126 case HCHAN_RGB: t1 = _("Log Histogram on RGB"); break;
127 case HCHAN_MAX: t1 = _("Log Histogram on value"); break;
130 switch (histogram->histogram_channel)
132 case HCHAN_R: t1 = _("Linear Histogram on Red"); break;
133 case HCHAN_G: t1 = _("Linear Histogram on Green"); break;
134 case HCHAN_B: t1 = _("Linear Histogram on Blue"); break;
135 case HCHAN_RGB: t1 = _("Linear Histogram on RGB"); break;
136 case HCHAN_MAX: t1 = _("Linear Histogram on value"); break;
141 static HistMap *histmap_new()
143 auto histmap = g_new0(HistMap, 1);
147 void histmap_free(HistMap *histmap)
149 if (!histmap) return;
150 if (histmap->idle_id) g_source_remove(histmap->idle_id);
151 if (histmap->pixbuf) g_object_unref(histmap->pixbuf);
155 static gboolean histmap_read(HistMap *histmap, gboolean whole)
157 gint w, h, i, j, srs, has_alpha, step, end_line;
159 GdkPixbuf *imgpixbuf = histmap->pixbuf;
161 w = gdk_pixbuf_get_width(imgpixbuf);
162 h = gdk_pixbuf_get_height(imgpixbuf);
163 srs = gdk_pixbuf_get_rowstride(imgpixbuf);
164 s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
165 has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
173 gint lines = 1 + 16384 / w;
174 end_line = histmap->y + lines;
175 if (end_line > h) end_line = h;
178 step = 3 + !!(has_alpha);
179 for (i = histmap->y; i < end_line; i++)
181 guchar *sp = s_pix + (i * srs); /* 8bit */
182 for (j = 0; j < w; j++)
185 if (sp[1] > max) max = sp[1];
186 if (sp[2] > max) max = sp[2];
196 histmap->y = end_line;
197 return end_line >= h;
200 const HistMap *histmap_get(FileData *fd)
202 if (fd->histmap && !fd->histmap->idle_id) return fd->histmap; /* histmap exists and is finished */
207 static gboolean histmap_idle_cb(gpointer data)
209 auto fd = static_cast<FileData *>(data);
210 if (histmap_read(fd->histmap, FALSE))
213 g_object_unref(fd->histmap->pixbuf); /*pixbuf is no longer needed */
214 fd->histmap->pixbuf = nullptr;
215 fd->histmap->idle_id = 0;
216 file_data_send_notification(fd, NOTIFY_HISTMAP);
217 return G_SOURCE_REMOVE;
219 return G_SOURCE_CONTINUE;
222 gboolean histmap_start_idle(FileData *fd)
224 if (fd->histmap || !fd->pixbuf) return FALSE;
226 fd->histmap = histmap_new();
227 fd->histmap->pixbuf = fd->pixbuf;
228 g_object_ref(fd->histmap->pixbuf);
230 fd->histmap->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, histmap_idle_cb, fd, nullptr);
235 static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
240 if (histogram->vgrid == 0) return;
242 add = width / static_cast<float>(histogram->vgrid);
244 for (i = 1; i < histogram->vgrid; i++)
246 gint xpos = x + static_cast<int>(i * add + 0.5);
248 pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
249 histogram->grid_color.R,
250 histogram->grid_color.G,
251 histogram->grid_color.B,
252 histogram->grid_color.A);
256 static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
261 if (histogram->hgrid == 0) return;
263 add = height / static_cast<float>(histogram->hgrid);
265 for (i = 1; i < histogram->hgrid; i++)
267 gint ypos = y + static_cast<int>(i * add + 0.5);
269 pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
270 histogram->grid_color.R,
271 histogram->grid_color.G,
272 histogram->grid_color.B,
273 histogram->grid_color.A);
277 gboolean histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
279 /** @FIXME use the coordinates correctly */
283 gint combine = (HISTMAP_SIZE - 1) / width + 1;
284 gint ypos = y + height;
286 if (!histogram || !histmap) return FALSE;
289 histogram_vgrid(histogram, pixbuf, x, y, width, height);
290 histogram_hgrid(histogram, pixbuf, x, y, width, height);
292 /* exclude overexposed and underexposed */
293 for (i = 1; i < HISTMAP_SIZE - 1; i++)
295 if (histmap->r[i] > max) max = histmap->r[i];
296 if (histmap->g[i] > max) max = histmap->g[i];
297 if (histmap->b[i] > max) max = histmap->b[i];
298 if (histmap->max[i] > max) max = histmap->max[i];
306 for (i = 0; i < width; i++)
309 glong v[4] = {0, 0, 0, 0};
313 gint ii = i * HISTMAP_SIZE / width;
317 for (j = 0; j < combine; j++)
320 v[0] += histmap->r[p];
321 v[1] += histmap->g[p];
322 v[2] += histmap->b[p];
323 v[3] += histmap->max[p];
326 for (j = 0; combine > 1 && j < 4; j++)
329 num_chan = (histogram->histogram_channel == HCHAN_RGB) ? 3 : 1;
330 for (j = 0; j < num_chan; j++)
333 if (histogram->histogram_channel == HCHAN_RGB)
336 if (v[HCHAN_G] > v[HCHAN_R]) chanmax = HCHAN_G;
337 if (v[HCHAN_B] > v[chanmax]) chanmax = HCHAN_B;
341 chanmax = histogram->histogram_channel;
352 case HCHAN_R: rplus = r = 255; break;
353 case HCHAN_G: gplus = g = 255; break;
354 case HCHAN_B: bplus = b = 255; break;
357 switch (histogram->histogram_channel)
360 if (r == 255 && g == 255 && b == 255)
365 case HCHAN_R: b = 0; g = 0; break;
366 case HCHAN_G: r = 0; b = 0; break;
367 case HCHAN_B: r = 0; g = 0; break;
368 case HCHAN_MAX: r = 0; b = 0; g = 0; break;
373 else if (histogram->histogram_mode)
374 pt = (static_cast<gdouble>(log(v[chanmax]))) / logmax * (height - 1);
376 pt = (static_cast<gdouble>(v[chanmax])) / max * (height - 1);
378 pixbuf_draw_line(pixbuf,
380 xpos, ypos, xpos, ypos - pt,
391 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer)
393 if ((type & NOTIFY_REREAD) && fd->histmap)
395 DEBUG_1("Notify histogram: %s %04x", fd->path, type);
396 histmap_free(fd->histmap);
397 fd->histmap = nullptr;
401 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */