[GRASS-SVN] r33046 - in grass/trunk/lib: cairodriver pngdriver

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Aug 23 22:20:30 EDT 2008


Author: glynn
Date: 2008-08-23 22:20:30 -0400 (Sat, 23 Aug 2008)
New Revision: 33046

Modified:
   grass/trunk/lib/cairodriver/Graph.c
   grass/trunk/lib/pngdriver/Draw_line.c
   grass/trunk/lib/pngdriver/Polygon.c
Log:
Fix bug with cairo driver failing to erase background
Fix bug with PNG driver skipping topmost line of polygon
Change PNG driver's line-drawing to use FP throughout



Modified: grass/trunk/lib/cairodriver/Graph.c
===================================================================
--- grass/trunk/lib/cairodriver/Graph.c	2008-08-24 00:42:49 UTC (rev 33045)
+++ grass/trunk/lib/cairodriver/Graph.c	2008-08-24 02:20:30 UTC (rev 33046)
@@ -86,7 +86,7 @@
 
 static void init_file(void)
 {
-    int is_vector;
+    int is_vector = 0;
     int do_read = 0;
     int do_map = 0;
     char *p;

Modified: grass/trunk/lib/pngdriver/Draw_line.c
===================================================================
--- grass/trunk/lib/pngdriver/Draw_line.c	2008-08-24 00:42:49 UTC (rev 33045)
+++ grass/trunk/lib/pngdriver/Draw_line.c	2008-08-24 02:20:30 UTC (rev 33046)
@@ -11,102 +11,62 @@
 
 #include "pngdriver.h"
 
-static void store_xy(int x, int y)
+static void store_xy(double x, double y)
 {
+    int xi = (int) floor(x);
+    int yi = (int) floor(y);
+
     if (x < png.clip_left || x >= png.clip_rite || y < png.clip_top || y >= png.clip_bot)
 	return;
 
-    png.grid[y * png.width + x] = png.current_color;
+    png.grid[yi * png.width + xi] = png.current_color;
 }
 
-static void draw_line(int x1, int y1, int x2, int y2)
+static void swap(double *a, double *b)
 {
-    int x, y, x_end, y_end;
-    int xinc, yinc, error;
-    int delta_x, delta_y;
+    double t = *a; *a = *b; *b = t;
+}
 
-    x = x1;
-    x_end = x2;
-    y = y1;
-    y_end = y2;
+static void draw_line(double x1, double y1, double x2, double y2)
+{
+    double x, y;
+    double dx, dy;
 
-    if (x == x_end && y == y_end) {
-	store_xy(x, y);
-	return;
-    }
+    if (fabs(y1 - y2) > fabs(x1 - x2)) {
+	if (y1 > y2) {
+	    swap(&y1, &y2);
+	    swap(&x1, &x2);
+	}
 
-    /* generate equation */
-    delta_y = y_end - y;
-    delta_x = x_end - x;
+	dy = y2 - y1;
+	dx = x2 - x1;
 
-    /* figure out which way to move x */
-    xinc = 1;
-    if (delta_x < 0) {
-	delta_x = -delta_x;
-	xinc = -1;
-    }
-
-    /* figure out which way to move y */
-    yinc = 1;
-    if (delta_y < 0) {
-	delta_y = -delta_y;
-	yinc = -1;
-    }
-
-    if (delta_x > delta_y) {
-	/* always move x, decide when to move y */
-	/* initialize the error term, and double delta x and delta y */
-	delta_y = delta_y * 2;
-	error = delta_y - delta_x;
-	delta_x = delta_y - (delta_x * 2);
-
-	while (x != x_end) {
-
+	for (y = floor(y1) + 0.5; y < y2; y++) {
+	    x = x1 + (y - y1) * dx / dy;
 	    store_xy(x, y);
-
-	    if (error > 0) {
-		y += yinc;
-		error += delta_x;
-	    }
-	    else
-		error += delta_y;
-
-	    x += xinc;
 	}
     }
     else {
-	/* always move y, decide when to move x */
-	/* initialize the error term, and double delta x and delta y */
-	delta_x = delta_x * 2;
-	error = delta_x - delta_y;
-	delta_y = delta_x - (delta_y * 2);
+	if (x1 > x2) {
+	    swap(&x1, &x2);
+	    swap(&y1, &y2);
+	}
 
-	while (y != y_end) {
+	dx = x2 - x1;
+	dy = y2 - y1;
 
+	for (x = floor(x1) + 0.5; x < x2; x++) {
+	    y = y1 + (x - x1) * dy / dx;
 	    store_xy(x, y);
-
-	    if (error > 0) {
-		x += xinc;
-		error += delta_y;
-	    }
-	    else
-		error += delta_x;
-
-	    y += yinc;
 	}
     }
-
-    store_xy(x, y);
 }
 
-void PNG_draw_line(double fx1, double fy1, double fx2, double fy2)
+void PNG_draw_line(double x1, double y1, double x2, double y2)
 {
-    int x1 = (int) floor(fx1 + 0.5);
-    int y1 = (int) floor(fy1 + 0.5);
-    int x2 = (int) floor(fx2 + 0.5);
-    int y2 = (int) floor(fy2 + 0.5);
+    double ax[4], ay[4];
+    double k = png.linewidth / 2;
     int dx, dy;
-    int i;
 
     if (png.linewidth <= 1) {
 	draw_line(x1, y1, x2, y2);
@@ -114,17 +74,19 @@
 	return;
     }
 
-    dx = abs(x2 - x1);
-    dy = abs(y2 - y1);
-
-    for (i = 0; i < png.linewidth; i++) {
-	int k = i - png.linewidth / 2;
-
-	if (dy > dx)
-	    draw_line(x1 + k, y1, x2 + k, y2);
-	else
-	    draw_line(x1, y1 + k, x2, y2 + k);
+    if (dy > dx) {
+	ax[0] = x1 - k;	ay[0] = y1;
+	ax[1] = x1 + k;	ay[1] = y1;
+	ax[2] = x2 + k;	ay[2] = y2;
+	ax[3] = x2 - k;	ay[3] = y2;
     }
+    else {
+	ax[0] = x1;	ay[0] = y1 - k;
+	ax[1] = x1;	ay[1] = y1 + k;
+	ax[2] = x2;	ay[2] = y2 + k;
+	ax[3] = x2;	ay[3] = y2 - k;
+    }
 
-    png.modified = 1;
+    PNG_Polygon(ax, ay, 4);
 }
+

Modified: grass/trunk/lib/pngdriver/Polygon.c
===================================================================
--- grass/trunk/lib/pngdriver/Polygon.c	2008-08-24 00:42:49 UTC (rev 33045)
+++ grass/trunk/lib/pngdriver/Polygon.c	2008-08-24 02:20:30 UTC (rev 33046)
@@ -119,10 +119,8 @@
     if (y1 > png.clip_bot)
 	y1 = png.clip_bot;
 
-    y0 = floor(y0 + 0.5) + 0.5;
-
-    for (y = y0; y < y1; y++)
-	line(p, n, y + 0.5);
+    for (y = floor(y0 + 0.5) + 0.5; y < y1; y++)
+	line(p, n, y);
 }
 
 void PNG_Polygon(const double *xarray, const double *yarray, int count)
@@ -145,5 +143,7 @@
     points[count].y = yarray[0];
 
     poly(points, count);
+
+    png.modified = 1;
 }
 



More information about the grass-commit mailing list