Commits

Anonymous committed 69f5990

- MFB: #77, gdImageLine does not draw all vertical lines

  • Participants
  • Parent commits 68d55ca

Comments (0)

Files changed (3)

 
 static void gdImageAALine (gdImagePtr im, int x1, int y1, int x2, int y2, int col);
 
+static void gdImageHLine(gdImagePtr im, int y, int x1, int x2, int col)
+{
+	if (im->thick > 1) {
+		int thickhalf = im->thick >> 1;
+		gdImageFilledRectangle(im, x1, y - thickhalf, x2, y + im->thick - thickhalf - 1, col);
+	} else {
+		if (x2 < x1) {
+			int t = x2;
+			x2 = x1;
+			x1 = t;
+		}
+
+		for (;x1 <= x2; x1++) {
+			gdImageSetPixel(im, x1, y, col);
+		}
+	}
+	return;
+}
+
+static void gdImageVLine(gdImagePtr im, int x, int y1, int y2, int col)
+{
+	if (im->thick > 1) {
+		int thickhalf = im->thick >> 1;
+		gdImageFilledRectangle(im, x - thickhalf, y1, x + im->thick - thickhalf - 1, y2, col);
+	} else {
+		if (y2 < y1) {
+			int t = y1;
+			y2 = y1;
+			y1 = t;
+		}
+
+		for (;y1 <= y2; y1++) {
+			gdImageSetPixel(im, x, y1, col);
+		}
+	}
+	return;
+}
+
 /* Bresenham as presented in Foley & Van Dam */
 BGD_DECLARE(void) gdImageLine (gdImagePtr im, int x1, int y1, int x2, int y2, int color)
 {
   int wid;
   int w, wstart;
   int thick;
+
   if (color == gdAntiAliased)
     {
       /* 
 
   dx = abs (x2 - x1);
   dy = abs (y2 - y1);
+
+	if (dx == 0) {
+		gdImageVLine(im, x1, y1, y2, color);
+		return;
+	} else if (dy == 0) {
+		gdImageHLine(im, y1, x1, x2, color);
+		return;
+	}
+
   if (dy <= dx)
     {
       /* More-or-less horizontal. use wid for vertical stroke */
 	/* keep them as 32bits */
 	long x, y, inc;
 	long dx, dy,tmp;
+
 	if (!im->trueColor) {
 		/* TBB: don't crash when the image is of the wrong type */
 		gdImageLine(im, x1, y1, x2, y2, col);
 	dx = x2 - x1;
 	dy = y2 - y1;
 
+	/* Axis aligned lines */
+	if (dx == 0) {
+		gdImageHLine(im, y1, x1, x2, col);
+		return;
+	} else if (dy == 0) {
+		gdImageVLine(im, x1, y1, y2, col);
+		return;
+	}
+
 	if (dx == 0 && dy == 0) {
 		/* TBB: allow setting points */
 		gdImageSetAAPixelColor(im, x1, y1, col, 0xFF);

src/tests/gdimageline/bug00077.c

+#include "gdtest.h"
+#include "gd.h"
+
+int main()
+{
+ 	gdImagePtr im;
+	const char *exp = "test.png";
+	const int files_cnt = 4;
+	FILE *fp;
+	int i = 0;
+	int error = 0;
+
+	char path[1024];
+
+
+	im = gdImageCreateTrueColor(11, 11);
+	gdImageFilledRectangle(im, 0, 0, 10, 10, 0xFFFFFF);
+	gdImageSetThickness(im, 1);
+
+	gdImageLine(im, 0, 10, 0, 0, 0x0);
+	gdImageLine(im, 5, 10, 5, 0, 0x0);
+	gdImageLine(im, 10, 5, 0, 5, 0x0);
+	gdImageLine(im, 10, 10, 0, 10, 0x0);
+
+	sprintf(path, "%s/gdimageline/%s", GDTEST_TOP_DIR, exp);
+
+	if (!gdAssertImageEqualsToFile(path, im)) {
+		error = 1;
+	}
+
+	gdImageDestroy(im);
+
+	return error;
+}

src/tests/gdimageline/bug00077_exp.png

Added
New image