Trim trailing white spaces.
[geeqie.git] / src / histogram.c
index 8f65376..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
@@ -32,7 +32,7 @@ struct _HistMap {
        gulong b[HISTMAP_SIZE];
        gulong max[HISTMAP_SIZE];
        
-       gint idle_id;
+       guint idle_id; /* event source id */
        GdkPixbuf *pixbuf;
        gint y;
 };
@@ -43,7 +43,7 @@ Histogram *histogram_new(void)
        Histogram *histogram;
 
        histogram = g_new0(Histogram, 1);
-       histogram->histogram_channel = HCHAN_RGB;
+       histogram->histogram_channel = HCHAN_DEFAULT;
        histogram->histogram_mode = 0;
 
        /* grid */
@@ -131,14 +131,13 @@ const gchar *histogram_label(Histogram *histogram)
 static HistMap *histmap_new(void)
 {
        HistMap *histmap = g_new0(HistMap, 1);
-       histmap->idle_id = -1;
        return histmap;
 }
 
 void histmap_free(HistMap *histmap)
 {
        if (!histmap) return;
-       if (histmap->idle_id != -1) g_source_remove(histmap->idle_id);
+       if (histmap->idle_id) g_source_remove(histmap->idle_id);
        if (histmap->pixbuf) g_object_unref(histmap->pixbuf);
        g_free(histmap);
 }
@@ -185,12 +184,12 @@ static gboolean histmap_read(HistMap *histmap, gboolean whole)
                        }
                }
        histmap->y = end_line;
-       return end_line >= h;   
+       return end_line >= h;
 }
 
 const HistMap *histmap_get(FileData *fd)
 {
-       if (fd->histmap && fd->histmap->idle_id == -1) return fd->histmap; /* histmap exists and is finished */
+       if (fd->histmap && !fd->histmap->idle_id) return fd->histmap; /* histmap exists and is finished */
        
        return NULL;
 }
@@ -203,7 +202,7 @@ static gboolean histmap_idle_cb(gpointer data)
                /* finished */
                g_object_unref(fd->histmap->pixbuf); /*pixbuf is no longer needed */
                fd->histmap->pixbuf = NULL;
-               fd->histmap->idle_id = -1;
+               fd->histmap->idle_id = 0;
                file_data_send_notification(fd, NOTIFY_HISTMAP);
                return FALSE;
                }
@@ -280,11 +279,13 @@ gboolean histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf
        histogram_vgrid(histogram, pixbuf, x, y, width, height);
        histogram_hgrid(histogram, pixbuf, x, y, width, height);
 
-       for (i = 0; i < HISTMAP_SIZE; i++)
+       /* 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];
                }
 
        if (max > 0)
@@ -301,6 +302,7 @@ gboolean histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf
                gint bplus = 0;
                gint ii = i * HISTMAP_SIZE / width;
                gint xpos = x + i;
+               gint num_chan;
 
                for (j = 0; j < combine; j++)
                        {
@@ -314,15 +316,21 @@ gboolean histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf
                for (j = 0; combine > 1 && j < 4; j++)
                        v[j] /= combine;
                
-               for (j = 0; j < 4; j++)
+               num_chan = (histogram->histogram_channel == HCHAN_RGB) ? 3 : 1;
+               for (j = 0; j < num_chan; j++)
                        {
-                       gint chanmax = HCHAN_R;
-               
-                       if (v[HCHAN_G] > v[HCHAN_R]) chanmax = HCHAN_G;
-                       if (v[HCHAN_B] > v[HCHAN_G]) chanmax = HCHAN_B;
-                               
-                       if (histogram->histogram_channel >= HCHAN_RGB
-                           || chanmax == histogram->histogram_channel)
+                       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;
+                               }
+                       
                                {
                                gulong pt;
                                gint r = rplus;
@@ -372,8 +380,9 @@ gboolean histogram_draw(Histogram *histogram, const HistMap *histmap, GdkPixbuf
 
 void histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
 {
-       if ((type & (NOTIFY_CHANGE || NOTIFY_REREAD)) && fd->histmap)
+       if ((type & NOTIFY_REREAD) && fd->histmap)
                {
+               DEBUG_1("Notify histogram: %s %04x", fd->path, type);
                histmap_free(fd->histmap);
                fd->histmap = NULL;
                }