improved histogram drawing
authorVladimir Nadvornik <nadvornik@suse.cz>
Wed, 9 Apr 2008 20:49:32 +0000 (20:49 +0000)
committerVladimir Nadvornik <nadvornik@suse.cz>
Wed, 9 Apr 2008 20:49:32 +0000 (20:49 +0000)
src/histogram.c
src/image-overlay.c
src/typedefs.h

index de6e81c..fe393c1 100644 (file)
  *----------------------------------------------------------------------------
  */
 
+#define HISTOGRAM_SIZE 256
+struct _Histogram {
+       gulong histmap[HISTOGRAM_SIZE*4];
+       gint histogram_chan;
+       gint histogram_logmode;
+};
+
 
 Histogram *histogram_new()
 {
@@ -105,25 +113,25 @@ gulong histogram_read(Histogram *histogram, GdkPixbuf *imgpixbuf)
        s_pix = gdk_pixbuf_get_pixels(imgpixbuf);
        has_alpha = gdk_pixbuf_get_has_alpha(imgpixbuf);
 
-       for (i = 0; i < 256*4; i++) histogram->histmap[i] = 0;
+       for (i = 0; i < HISTOGRAM_SIZE*4; i++) histogram->histmap[i] = 0;
 
        for (i = 0; i < h; i++)
                {
                guchar *sp = s_pix + (i * srs); /* 8bit */
                for (j = 0; j < w; j++)
                        {
-                       histogram->histmap[sp[0]+0]++;
-                       histogram->histmap[sp[1]+256]++;
-                       histogram->histmap[sp[2]+512]++;
+                       histogram->histmap[sp[0] + 0 * HISTOGRAM_SIZE]++;
+                       histogram->histmap[sp[1] + 1 * HISTOGRAM_SIZE]++;
+                       histogram->histmap[sp[2] + 2 * HISTOGRAM_SIZE]++;
                        if (histogram->histogram_chan == HCHAN_MAX)
                                {
                                guchar t = sp[0];
                                if (sp[1]>t) t = sp[1];
                                if (sp[2]>t) t = sp[2];
-                               histogram->histmap[t+768]++;
+                               histogram->histmap[t + 3 * HISTOGRAM_SIZE]++;
                                }
                        else
-                               histogram->histmap[768+(sp[0]+sp[1]+sp[2])/3]++;
+                               histogram->histmap[3 * HISTOGRAM_SIZE + (sp[0]+sp[1]+sp[2])/3]++;
                        sp += 3;
                        if (has_alpha) sp++;
                        }
@@ -158,18 +166,23 @@ gint histogram_draw(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gin
        }
 
        logmax = log(max);
-       for (i=0; i<256; i++)
+       for (i=0; i<width; i++)
                {
                gint j;
-               glong v[4];
+               glong v[4] = {0, 0, 0, 0};
                gint rplus = 0;
                gint gplus = 0;
                gint bplus = 0;
+               gint ii = i * HISTOGRAM_SIZE / width;
+               gint combine  = (HISTOGRAM_SIZE - 1) / width + 1;
 
-               v[0] = histogram->histmap[i+0*256]; // r
-               v[1] = histogram->histmap[i+1*256]; // g
-               v[2] = histogram->histmap[i+2*256]; // b
-               v[3] = histogram->histmap[i+3*256]; // value, max
+               for (j = 0; j < combine; j++)
+                       {
+                       v[0] += histogram->histmap[ii + j + 0*HISTOGRAM_SIZE]; // r
+                       v[1] += histogram->histmap[ii + j + 1*HISTOGRAM_SIZE]; // g
+                       v[2] += histogram->histmap[ii + j + 2*HISTOGRAM_SIZE]; // b
+                       v[3] += histogram->histmap[ii + j + 3*HISTOGRAM_SIZE]; // value, max
+                       }
                
                for (j=0; j<4; j++)
                        {
@@ -209,14 +222,14 @@ gint histogram_draw(Histogram *histogram, GdkPixbuf *pixbuf, gint x, gint y, gin
                        if (v[max2] == 0)
                                pt = 0;
                        else if (histogram->histogram_logmode)
-                               pt = ((float)log(v[max2]))/logmax*255;
+                               pt = ((float)log(v[max2])) / logmax * (height - 1);
                        else
-                               pt = ((float)v[max2])/max*255;
+                               pt = ((float)v[max2])/ max * (height - 1);
                        if (histogram->histogram_chan >= HCHAN_RGB 
                            || max2 == histogram->histogram_chan)
                                pixbuf_draw_line(pixbuf, 
-                                       0+5, height-255, 256, 256,
-                                       i+5, height, i+5, height-pt, 
+                                       x, y, width, height,
+                                       x + i, y + height, x + i, y + height-pt, 
                                        r, g, b, 255);
                        v[max2] = -1;
                        }
index 73fb79e..c6118a7 100644 (file)
@@ -74,6 +74,7 @@ static OSDIcon osd_icons[] = {
 
 #define IMAGE_OSD_DEFAULT_DURATION 30
 
+#define HISTOGRAM_HEIGHT 140
 /*
  *----------------------------------------------------------------------------
  * image histogram
@@ -326,12 +327,6 @@ static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
                        active_marks += fd->marks[mark];
                        }
 
-               if (with_hist)
-                       {
-                       text2 = g_strdup_printf("%s\n%s", text, histogram_label(lw->histogram));
-                       g_free(text);
-                       text = text2;
-                       }
 
                if (active_marks > 0)
                        {
@@ -348,6 +343,12 @@ static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
                        text = text2;
                        }
 
+               if (with_hist)
+                       {
+                       text2 = g_strdup_printf("%s\n%s", text, histogram_label(lw->histogram));
+                       g_free(text);
+                       text = text2;
+                       }
                }
        }
         
@@ -364,7 +365,7 @@ static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
                {
                histogram_read(lw->histogram, imgpixbuf);
                if (width < 266) width = 266;
-               height += 256;
+               height += HISTOGRAM_HEIGHT + 5;
                }
 
 
@@ -380,7 +381,7 @@ static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
        pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
 
        if (with_hist)
-               histogram_draw(lw->histogram, pixbuf, 0, 0, width, height);
+               histogram_draw(lw->histogram, pixbuf, 5, height - HISTOGRAM_HEIGHT - 5 , width - 10, HISTOGRAM_HEIGHT);
                
        pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
 
index 2c963f5..7ab7392 100644 (file)
@@ -397,13 +397,6 @@ struct _FileData {
        gint ref;
 };
 
-struct _Histogram {
-       gulong histmap[256*4];
-       gint histogram_chan;
-       gint histogram_logmode;
-};
-
-
 struct _LayoutWindow
 {
        gchar *path;