Trim trailing white spaces.
[geeqie.git] / src / histogram.c
index 20dce7a..8e379fb 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Geeqie
- * Copyright (C) 2008 - 2009 The Geeqie Team
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Author: Vladimir Nadvornik
  * based on a patch by Uwe Ohse
@@ -14,6 +14,7 @@
 #include "histogram.h"
 
 #include "pixbuf_util.h"
+#include "filedata.h"
 
 #include <math.h>
 
  */
 
 #define HISTMAP_SIZE 256
-typedef enum {
-       HISTMAP_CHANNEL_R = 0,
-       HISTMAP_CHANNEL_G,
-       HISTMAP_CHANNEL_B,
-       HISTMAP_CHANNEL_AVG,
-       HISTMAP_CHANNEL_MAX,
-       HISTMAP_CHANNELS
-} HistMapChannels;
 
 struct _HistMap {
-       gulong histmap[HISTMAP_SIZE * HISTMAP_CHANNELS];
-       gulong area;
-};
-
-struct _Histogram {
-       gint histogram_chan;
-       gint histogram_logmode;
+       gulong r[HISTMAP_SIZE];
+       gulong g[HISTMAP_SIZE];
+       gulong b[HISTMAP_SIZE];
+       gulong max[HISTMAP_SIZE];
+       
+       guint idle_id; /* event source id */
+       GdkPixbuf *pixbuf;
+       gint y;
 };
 
 
@@ -49,8 +43,16 @@ Histogram *histogram_new(void)
        Histogram *histogram;
 
        histogram = g_new0(Histogram, 1);
-       histogram->histogram_chan = options->histogram.last_channel_mode;
-       histogram->histogram_logmode = options->histogram.last_log_mode;
+       histogram->histogram_channel = HCHAN_DEFAULT;
+       histogram->histogram_mode = 0;
+
+       /* grid */
+       histogram->vgrid = 5;
+       histogram->hgrid = 3;
+       histogram->grid_color.R = 160;
+       histogram->grid_color.G = 160;
+       histogram->grid_color.B = 160;
+       histogram->grid_color.A = 250;
 
        return histogram;
 }
@@ -64,27 +66,39 @@ void histogram_free(Histogram *histogram)
 gint histogram_set_channel(Histogram *histogram, gint chan)
 {
        if (!histogram) return 0;
-       options->histogram.last_channel_mode = histogram->histogram_chan = chan;
+       histogram->histogram_channel = chan;
        return chan;
 }
 
 gint histogram_get_channel(Histogram *histogram)
 {
        if (!histogram) return 0;
-       return histogram->histogram_chan;
+       return histogram->histogram_channel;
 }
 
 gint histogram_set_mode(Histogram *histogram, gint mode)
 {
        if (!histogram) return 0;
-       options->histogram.last_log_mode = histogram->histogram_logmode = mode;
+       histogram->histogram_mode = mode;
        return mode;
 }
 
 gint histogram_get_mode(Histogram *histogram)
 {
        if (!histogram) return 0;
-       return histogram->histogram_logmode;
+       return histogram->histogram_mode;
+}
+
+gint histogram_toggle_channel(Histogram *histogram)
+{
+       if (!histogram) return 0;
+       return histogram_set_channel(histogram, (histogram_get_channel(histogram)+1)%HCHAN_COUNT);
+}
+
+gint histogram_toggle_mode(Histogram *histogram)
+{
+       if (!histogram) return 0;
+       return histogram_set_mode(histogram, !histogram_get_mode(histogram));
 }
 
 const gchar *histogram_label(Histogram *histogram)
@@ -93,109 +107,192 @@ const gchar *histogram_label(Histogram *histogram)
        
        if (!histogram) return NULL;
 
-       if (histogram->histogram_logmode)
-               switch (histogram->histogram_chan)
+       if (histogram->histogram_mode)
+               switch (histogram->histogram_channel)
                        {
-                       case HCHAN_R:   t1 = _("logarithmical histogram on red"); break;
-                       case HCHAN_G:   t1 = _("logarithmical histogram on green"); break;
-                       case HCHAN_B:   t1 = _("logarithmical histogram on blue"); break;
-                       case HCHAN_VAL: t1 = _("logarithmical histogram on value"); break;
-                       case HCHAN_RGB: t1 = _("logarithmical histogram on RGB"); break;
-                       case HCHAN_MAX: t1 = _("logarithmical histogram on max value"); break;
+                       case HCHAN_R:   t1 = _("Log Histogram on Red"); break;
+                       case HCHAN_G:   t1 = _("Log Histogram on Green"); break;
+                       case HCHAN_B:   t1 = _("Log Histogram on Blue"); break;
+                       case HCHAN_RGB: t1 = _("Log Histogram on RGB"); break;
+                       case HCHAN_MAX: t1 = _("Log Histogram on value"); break;
                        }
        else
-               switch (histogram->histogram_chan)
+               switch (histogram->histogram_channel)
                        {
-                       case HCHAN_R:   t1 = _("linear histogram on red"); break;
-                       case HCHAN_G:   t1 = _("linear histogram on green"); break;
-                       case HCHAN_B:   t1 = _("linear histogram on blue"); break;
-                       case HCHAN_VAL: t1 = _("linear histogram on value"); break;
-                       case HCHAN_RGB: t1 = _("linear histogram on RGB"); break;
-                       case HCHAN_MAX: t1 = _("linear histogram on max value"); break;
+                       case HCHAN_R:   t1 = _("Linear Histogram on Red"); break;
+                       case HCHAN_G:   t1 = _("Linear Histogram on Green"); break;
+                       case HCHAN_B:   t1 = _("Linear Histogram on Blue"); break;
+                       case HCHAN_RGB: t1 = _("Linear Histogram on RGB"); break;
+                       case HCHAN_MAX: t1 = _("Linear Histogram on value"); break;
                        }
        return t1;
 }
 
-static HistMap *histmap_read(GdkPixbuf *imgpixbuf)
+static HistMap *histmap_new(void)
 {
-       gint w, h, i, j, srs, has_alpha, step;
-       guchar *s_pix;
+       HistMap *histmap = g_new0(HistMap, 1);
+       return histmap;
+}
 
-       HistMap *histmap;
+void histmap_free(HistMap *histmap)
+{
+       if (!histmap) return;
+       if (histmap->idle_id) g_source_remove(histmap->idle_id);
+       if (histmap->pixbuf) g_object_unref(histmap->pixbuf);
+       g_free(histmap);
+}
+
+static gboolean histmap_read(HistMap *histmap, gboolean whole)
+{
+       gint w, h, i, j, srs, has_alpha, step, end_line;
+       guchar *s_pix;
+       GdkPixbuf *imgpixbuf = histmap->pixbuf;
        
        w = gdk_pixbuf_get_width(imgpixbuf);
        h = gdk_pixbuf_get_height(imgpixbuf);
        srs = gdk_pixbuf_get_rowstride(imgpixbuf);
        s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
        has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
-
-       histmap = g_new0(HistMap, 1);
+       
+       if (whole)
+               {
+               end_line = h;
+               }
+       else
+               {
+               gint lines = 1 + 16384 / w;
+               end_line = histmap->y + lines;
+               if (end_line > h) end_line = h;
+               }
 
        step = 3 + !!(has_alpha);
-       for (i = 0; i < h; i++)
+       for (i = histmap->y; i < end_line; i++)
                {
                guchar *sp = s_pix + (i * srs); /* 8bit */
                for (j = 0; j < w; j++)
                        {
-                       guint avg = (sp[0] + sp[1] + sp[2]) / 3;
                        guint max = sp[0];
                        if (sp[1] > max) max = sp[1];
                        if (sp[2] > max) max = sp[2];
+               
+                       histmap->r[sp[0]]++;
+                       histmap->g[sp[1]]++;
+                       histmap->b[sp[2]]++;
+                       histmap->max[max]++;
 
-                       histmap->histmap[sp[0] * HISTMAP_CHANNELS + HISTMAP_CHANNEL_R]++;
-                       histmap->histmap[sp[1] * HISTMAP_CHANNELS + HISTMAP_CHANNEL_G]++;
-                       histmap->histmap[sp[2] * HISTMAP_CHANNELS + HISTMAP_CHANNEL_B]++;
-                       histmap->histmap[avg   * HISTMAP_CHANNELS + HISTMAP_CHANNEL_AVG]++;
-                       histmap->histmap[max   * HISTMAP_CHANNELS + HISTMAP_CHANNEL_MAX]++;
                        sp += step;
                        }
                }
-       histmap->area = w * h;
-       return histmap;
+       histmap->y = end_line;
+       return end_line >= h;
 }
 
 const HistMap *histmap_get(FileData *fd)
 {
-       if (fd->histmap) return fd->histmap;
+       if (fd->histmap && !fd->histmap->idle_id) return fd->histmap; /* histmap exists and is finished */
        
-       if (fd->pixbuf) 
+       return NULL;
+}
+
+static gboolean histmap_idle_cb(gpointer data)
+{
+       FileData *fd = data;
+       if (histmap_read(fd->histmap, FALSE))
                {
-               fd->histmap = histmap_read(fd->pixbuf);
-               return fd->histmap;
+               /* finished */
+               g_object_unref(fd->histmap->pixbuf); /*pixbuf is no longer needed */
+               fd->histmap->pixbuf = NULL;
+               fd->histmap->idle_id = 0;
+               file_data_send_notification(fd, NOTIFY_HISTMAP);
+               return FALSE;
+               }
+       return TRUE;
+}
+
+gboolean histmap_start_idle(FileData *fd)
+{
+       if (fd->histmap || !fd->pixbuf) return FALSE;
+
+       fd->histmap = histmap_new();
+       fd->histmap->pixbuf = fd->pixbuf;
+       g_object_ref(fd->histmap->pixbuf);
+
+       fd->histmap->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, histmap_idle_cb, fd, NULL);
+       return TRUE;
+}
+
+
+static void histogram_vgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
+{
+       guint i;
+       float add;
+       
+       if (histogram->vgrid == 0) return;
+
+       add = width / (float)histogram->vgrid;
+
+       for (i = 1; i < histogram->vgrid; i++)
+               {
+               gint xpos = x + (int)(i * add + 0.5);
+
+               pixbuf_draw_line(pixbuf, x, y, width, height, xpos, y, xpos, y + height,
+                                histogram->grid_color.R,
+                                histogram->grid_color.G,
+                                histogram->grid_color.B,
+                                histogram->grid_color.A);
                }
-       return NULL;
 }
 
-gint histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
+static void histogram_hgrid(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
+{
+       guint i;
+       float add;
+       
+       if (histogram->hgrid == 0) return;
+
+       add = height / (float)histogram->hgrid;
+
+       for (i = 1; i < histogram->hgrid; i++)
+               {
+               gint ypos = y + (int)(i * add + 0.5);
+       
+               pixbuf_draw_line(pixbuf, x, y, width, height, x, ypos, x + width, ypos,
+                                histogram->grid_color.R,
+                                histogram->grid_color.G,
+                                histogram->grid_color.B,
+                                histogram->grid_color.A);
+               }
+}
+
+gboolean histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pixbuf, gint x, gint y, gint width, gint height)
 {
        /* FIXME: use the coordinates correctly */
        gint i;
        gulong max = 0;
        gdouble logmax;
+       gint combine = (HISTMAP_SIZE - 1) / width + 1;
+       gint ypos = y + height;
+       
+       if (!histogram || !histmap) return FALSE;
+       
+       /* Draw the grid */
+       histogram_vgrid(histogram, pixbuf, x, y, width, height);
+       histogram_hgrid(histogram, pixbuf, x, y, width, height);
 
-       if (!histogram || !histmap) return 0;
-
-       for (i = 0; i < 1024; i++) {
-#if 0
-               /* this is probably broken for MAX or VAL mode */
-               gint flag = 0;
+       /* exclude overexposed and underexposed */
+       for (i = 1; i < HISTMAP_SIZE - 1; i++)
+               {
+               if (histmap->r[i] > max) max = histmap->r[i];
+               if (histmap->g[i] > max) max = histmap->g[i];
+               if (histmap->b[i] > max) max = histmap->b[i];
+               if (histmap->max[i] > max) max = histmap->max[i];
+               }
 
-               switch (histogram->histogram_chan)
-                       {
-                       case HCHAN_RGB: if ((i % HISTMAP_CHANNELS) < 3) flag = 1; break;
-                       case HCHAN_R:   if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_R) flag = 1; break;
-                       case HCHAN_G:   if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_G) flag = 1; break;
-                       case HCHAN_B:   if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_B) flag = 1; break;
-                       case HCHAN_VAL: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_AVG) flag = 1; break;
-                       case HCHAN_MAX: if ((i % HISTMAP_CHANNELS) == HISTMAP_CHANNEL_MAX) flag = 1; break;
-                       }
-               if (flag && histmap->histmap[i] > max) max = histmap->histmap[i];
-#else
-               if (histmap->histmap[i] > max) max = histmap->histmap[i];
-#endif
-       }
+       if (max > 0)
+               logmax = log(max);
+       else
+               logmax = 1.0;
 
-       logmax = log(max);
        for (i = 0; i < width; i++)
                {
                gint j;
@@ -204,73 +301,77 @@ gint histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pix
                gint gplus = 0;
                gint bplus = 0;
                gint ii = i * HISTMAP_SIZE / width;
-               gint combine  = (HISTMAP_SIZE - 1) / width + 1;
+               gint xpos = x + i;
+               gint num_chan;
 
                for (j = 0; j < combine; j++)
                        {
-                       v[0] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + HISTMAP_CHANNEL_R]; // r
-                       v[1] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + HISTMAP_CHANNEL_G]; // g
-                       v[2] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + HISTMAP_CHANNEL_B]; // b
-                       v[3] += histmap->histmap[(ii + j) * HISTMAP_CHANNELS + 
-                               ((histogram->histogram_chan == HCHAN_VAL) ? HISTMAP_CHANNEL_AVG : HISTMAP_CHANNEL_MAX)]; // value, max
+                       guint p = ii + j;
+                       v[0] += histmap->r[p];
+                       v[1] += histmap->g[p];
+                       v[2] += histmap->b[p];
+                       v[3] += histmap->max[p];
                        }
-                       
-               for (j = 0; j < 4; j++)
-                       {
+       
+               for (j = 0; combine > 1 && j < 4; j++)
                        v[j] /= combine;
-                       }
-
-               for (j = 0; j < 4; j++)
-                       {
-                       gint max2 = 0;
-                       gint k;
                
-                       for (k = 1; k < 4; k++)
-                               if (v[k] > v[max2]) max2 = k;
+               num_chan = (histogram->histogram_channel == HCHAN_RGB) ? 3 : 1;
+               for (j = 0; j < num_chan; j++)
+                       {
+                       gint chanmax;
+                       if (histogram->histogram_channel == HCHAN_RGB)
+                               {
+                               chanmax = HCHAN_R;
+                               if (v[HCHAN_G] > v[HCHAN_R]) chanmax = HCHAN_G;
+                               if (v[HCHAN_B] > v[chanmax]) chanmax = HCHAN_B;
+                               }
+                       else
+                               {
+                               chanmax = histogram->histogram_channel;
+                               }
                        
-                       if (histogram->histogram_chan >= HCHAN_RGB
-                           || max2 == histogram->histogram_chan)
                                {
                                gulong pt;
                                gint r = rplus;
                                gint g = gplus;
                                gint b = bplus;
 
-                               switch (max2)
+                               switch (chanmax)
                                        {
                                        case HCHAN_R: rplus = r = 255; break;
                                        case HCHAN_G: gplus = g = 255; break;
                                        case HCHAN_B: bplus = b = 255; break;
                                        }
 
-                               switch (histogram->histogram_chan)
+                               switch (histogram->histogram_channel)
                                        {
                                        case HCHAN_RGB:
                                                if (r == 255 && g == 255 && b == 255)
                                                        {
-                                                       r = 0; b = 0; g = 0;
+                                                       r = 0;  b = 0;  g = 0;
                                                        }
                                                break;
-                                       case HCHAN_R:          b = 0; g = 0; break;
-                                       case HCHAN_G:   r = 0; b = 0;        break;
-                                       case HCHAN_B:   r = 0;        g = 0; break;
-                                       case HCHAN_MAX:
-                                       case HCHAN_VAL: r = 0; b = 0; g = 0; break;
+                                       case HCHAN_R:           b = 0;  g = 0;  break;
+                                       case HCHAN_G:   r = 0;  b = 0;          break;
+                                       case HCHAN_B:   r = 0;          g = 0;  break;
+                                       case HCHAN_MAX: r = 0;  b = 0;  g = 0;  break;
                                        }
                                
-                               if (v[max2] == 0)
+                               if (v[chanmax] == 0)
                                        pt = 0;
-                               else if (histogram->histogram_logmode)
-                                       pt = ((float)log(v[max2])) / logmax * (height - 1);
+                               else if (histogram->histogram_mode)
+                                       pt = ((gdouble)log(v[chanmax])) / logmax * (height - 1);
                                else
-                                       pt = ((float)v[max2])/ max * (height - 1);
+                                       pt = ((gdouble)v[chanmax]) / max * (height - 1);
 
                                pixbuf_draw_line(pixbuf,
                                        x, y, width, height,
-                                       x + i, y + height, x + i, y + height - pt,
+                                       xpos, ypos, xpos, ypos - pt,
                                        r, g, b, 255);
                                }
-                       v[max2] = -1;
+
+                       v[chanmax] = -1;
                        }
                }
 
@@ -279,9 +380,10 @@ gint histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf *pix
 
 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
 {
-       if (type != NOTIFY_TYPE_INTERNAL && fd->histmap)
+       if ((type & NOTIFY_REREAD) && fd->histmap)
                {
-               g_free(fd->histmap);
+               DEBUG_1("Notify histogram: %s %04x", fd->path, type);
+               histmap_free(fd->histmap);
                fd->histmap = NULL;
                }
 }