changeset 290:4bbde8a38ad4

improved histogram drawing
author nadvornik
date Wed, 09 Apr 2008 20:49:32 +0000
parents 6a7298988a7a
children 09a676f57a3d
files src/histogram.c src/image-overlay.c src/typedefs.h
diffstat 3 files changed, 38 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/histogram.c	Wed Apr 09 16:37:54 2008 +0000
+++ b/src/histogram.c	Wed Apr 09 20:49:32 2008 +0000
@@ -22,6 +22,14 @@
  *----------------------------------------------------------------------------
  */
 
+#define HISTOGRAM_SIZE 256
+ 
+struct _Histogram {
+	gulong histmap[HISTOGRAM_SIZE*4];
+	gint histogram_chan;
+	gint histogram_logmode;
+};
+
 
 Histogram *histogram_new()
 {
@@ -105,25 +113,25 @@
 	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 @@
 	}
 
 	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 @@
 			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;
 			}
--- a/src/image-overlay.c	Wed Apr 09 16:37:54 2008 +0000
+++ b/src/image-overlay.c	Wed Apr 09 20:49:32 2008 +0000
@@ -74,6 +74,7 @@
 
 #define IMAGE_OSD_DEFAULT_DURATION 30
 
+#define HISTOGRAM_HEIGHT 140
 /*
  *----------------------------------------------------------------------------
  * image histogram
@@ -326,12 +327,6 @@
 			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 @@
 			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 @@
 		{
 		histogram_read(lw->histogram, imgpixbuf);
 		if (width < 266) width = 266;
-		height += 256;
+		height += HISTOGRAM_HEIGHT + 5;
 		}
 
 
@@ -380,7 +381,7 @@
 	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);
 
--- a/src/typedefs.h	Wed Apr 09 16:37:54 2008 +0000
+++ b/src/typedefs.h	Wed Apr 09 20:49:32 2008 +0000
@@ -397,13 +397,6 @@
 	gint ref;
 };
 
-struct _Histogram {
-	gulong histmap[256*4];
-	gint histogram_chan;
-	gint histogram_logmode;
-};
-
-
 struct _LayoutWindow
 {
 	gchar *path;