Anonymous avatar Anonymous committed 1cdea74

- sync to 2.0.1

Comments (0)

Files changed (38)

 #include "gd.h"
 #include "gdhelpers.h"
 
-#ifdef _OSD_POSIX	/* BS2000 uses the EBCDIC char set instead of ASCII */
+#ifdef _OSD_POSIX		/* BS2000 uses the EBCDIC char set instead of ASCII */
 #define CHARSET_EBCDIC
-#define __attribute__(any) /*nothing*/
-#endif /*_OSD_POSIX*/
+#define __attribute__(any)	/*nothing */
+#endif
+/*_OSD_POSIX*/
 
 #ifndef CHARSET_EBCDIC
 #define ASC(ch)  ch
-#else /*CHARSET_EBCDIC*/
+#else /*CHARSET_EBCDIC */
 #define ASC(ch) gd_toascii[(unsigned char)ch]
-static const unsigned char gd_toascii[256] = {
-/*00*/ 0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f,
-       0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /*................*/
-/*10*/ 0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97,
-       0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f, /*................*/
-/*20*/ 0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b,
-       0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, /*................*/
-/*30*/ 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04,
-       0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, /*................*/
-/*40*/ 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5,
-       0xe7, 0xf1, 0x60, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, /* .........`.<(+|*/
-/*50*/ 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef,
-       0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x9f, /*&.........!$*);.*/
-/*60*/ 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5,
-       0xc7, 0xd1, 0x5e, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, /*-/........^,%_>?*/
-/*70*/ 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf,
-       0xcc, 0xa8, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, /*..........:#@'="*/
-/*80*/ 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
-       0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, /*.abcdefghi......*/
-/*90*/ 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
-       0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, /*.jklmnopqr......*/
-/*a0*/ 0xb5, 0xaf, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
-       0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0xdd, 0xde, 0xae, /*..stuvwxyz......*/
-/*b0*/ 0xa2, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc,
-       0xbd, 0xbe, 0xac, 0x5b, 0x5c, 0x5d, 0xb4, 0xd7, /*...........[\]..*/
-/*c0*/ 0xf9, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
-       0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, /*.ABCDEFGHI......*/
-/*d0*/ 0xa6, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
-       0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xdb, 0xfa, 0xff, /*.JKLMNOPQR......*/
-/*e0*/ 0xd9, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
-       0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, /*..STUVWXYZ......*/
-/*f0*/ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
-	    0x38, 0x39, 0xb3, 0x7b, 0xdc, 0x7d, 0xda, 0x7e  /*0123456789.{.}.~*/
+static const unsigned char gd_toascii[256] =
+{
+/*00 */ 0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f,
+  0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,	/*................ */
+/*10 */ 0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97,
+  0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f,	/*................ */
+/*20 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b,
+  0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07,	/*................ */
+/*30 */ 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04,
+  0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a,	/*................ */
+/*40 */ 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5,
+  0xe7, 0xf1, 0x60, 0x2e, 0x3c, 0x28, 0x2b, 0x7c,	/* .........`.<(+| */
+/*50 */ 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef,
+  0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x9f,	/*&.........!$*);. */
+/*60 */ 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5,
+  0xc7, 0xd1, 0x5e, 0x2c, 0x25, 0x5f, 0x3e, 0x3f,
+/*-/........^,%_>?*/
+/*70 */ 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf,
+  0xcc, 0xa8, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22,	/*..........:#@'=" */
+/*80 */ 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
+  0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1,	/*.abcdefghi...... */
+/*90 */ 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
+  0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4,	/*.jklmnopqr...... */
+/*a0 */ 0xb5, 0xaf, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
+  0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0xdd, 0xde, 0xae,	/*..stuvwxyz...... */
+/*b0 */ 0xa2, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc,
+  0xbd, 0xbe, 0xac, 0x5b, 0x5c, 0x5d, 0xb4, 0xd7,	/*...........[\].. */
+/*c0 */ 0xf9, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
+  0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5,	/*.ABCDEFGHI...... */
+/*d0 */ 0xa6, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
+  0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xdb, 0xfa, 0xff,	/*.JKLMNOPQR...... */
+/*e0 */ 0xd9, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
+  0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5,	/*..STUVWXYZ...... */
+/*f0 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
+  0x38, 0x39, 0xb3, 0x7b, 0xdc, 0x7d, 0xda, 0x7e	/*0123456789.{.}.~ */
 };
-#endif /*CHARSET_EBCDIC*/
+#endif /*CHARSET_EBCDIC */
 
 extern int gdCosT[];
 extern int gdSinT[];
 
-static void gdImageBrushApply(gdImagePtr im, int x, int y);
-static void gdImageTileApply(gdImagePtr im, int x, int y);
+static void gdImageBrushApply (gdImagePtr im, int x, int y);
+static void gdImageTileApply (gdImagePtr im, int x, int y);
 
-gdImagePtr gdImageCreate(int sx, int sy)
+gdImagePtr
+gdImageCreate (int sx, int sy)
 {
-	int i;
-	gdImagePtr im;
-	im = (gdImage *) gdMalloc(sizeof(gdImage));
-	memset(im, 0, sizeof(gdImage));
-	/* Row-major ever since gd 1.3 */
-	im->pixels = (unsigned char **) gdMalloc(sizeof(unsigned char *) * sy);
-	im->polyInts = 0;
-	im->polyAllocated = 0;
-	im->brush = 0;
-	im->tile = 0;
-	im->style = 0;
-	for (i=0; (i<sy); i++) {
-		/* Row-major ever since gd 1.3 */
-		im->pixels[i] = (unsigned char *) gdCalloc(
-			sx, sizeof(unsigned char));
-	}	
-	im->sx = sx;
-	im->sy = sy;
-	im->colorsTotal = 0;
-	im->transparent = (-1);
-	im->interlace = 0;
-	im->thick = 1;
-        for (i=0; (i < gdMaxColors); i++) {
-           im->open[i] = 1;
-	   im->red[i] = 0;
-           im->green[i] = 0;
-           im->blue[i] = 0;
-	};
-	im->trueColor = 0;
-	im->tpixels = 0;
-	return im;
+  int i;
+  gdImagePtr im;
+  im = (gdImage *) gdMalloc (sizeof (gdImage));
+  memset (im, 0, sizeof (gdImage));
+  /* Row-major ever since gd 1.3 */
+  im->pixels = (unsigned char **) gdMalloc (sizeof (unsigned char *) * sy);
+  im->polyInts = 0;
+  im->polyAllocated = 0;
+  im->brush = 0;
+  im->tile = 0;
+  im->style = 0;
+  for (i = 0; (i < sy); i++)
+    {
+      /* Row-major ever since gd 1.3 */
+      im->pixels[i] = (unsigned char *) gdCalloc (
+						sx, sizeof (unsigned char));
+    }
+  im->sx = sx;
+  im->sy = sy;
+  im->colorsTotal = 0;
+  im->transparent = (-1);
+  im->interlace = 0;
+  im->thick = 1;
+  for (i = 0; (i < gdMaxColors); i++)
+    {
+      im->open[i] = 1;
+      im->red[i] = 0;
+      im->green[i] = 0;
+      im->blue[i] = 0;
+    };
+  im->trueColor = 0;
+  im->tpixels = 0;
+  return im;
 }
 
-gdImagePtr gdImageCreateTrueColor(int sx, int sy)
+gdImagePtr
+gdImageCreateTrueColor (int sx, int sy)
 {
-	int i;
-	gdImagePtr im;
-	im = (gdImage *) gdMalloc(sizeof(gdImage));
-	memset(im, 0, sizeof(gdImage));
-	im->tpixels = (int **) gdMalloc(sizeof(int *) * sy);
-	im->polyInts = 0;
-	im->polyAllocated = 0;
-	im->brush = 0;
-	im->tile = 0;
-	im->style = 0;
-	for (i=0; (i<sy); i++) {
-		im->tpixels[i] = (int *) gdCalloc(
-			sx, sizeof(int));
-	}	
-	im->sx = sx;
-	im->sy = sy;
-	im->transparent = (-1);
-	im->interlace = 0;
-	im->trueColor = 1;
-	im->saveAlphaFlag = 1;
-	im->alphaBlendingFlag = 0;
-	im->thick = 1;
-	return im;
+  int i;
+  gdImagePtr im;
+  im = (gdImage *) gdMalloc (sizeof (gdImage));
+  memset (im, 0, sizeof (gdImage));
+  im->tpixels = (int **) gdMalloc (sizeof (int *) * sy);
+  im->polyInts = 0;
+  im->polyAllocated = 0;
+  im->brush = 0;
+  im->tile = 0;
+  im->style = 0;
+  for (i = 0; (i < sy); i++)
+    {
+      im->tpixels[i] = (int *) gdCalloc (
+					  sx, sizeof (int));
+    }
+  im->sx = sx;
+  im->sy = sy;
+  im->transparent = (-1);
+  im->interlace = 0;
+  im->trueColor = 1;
+  im->saveAlphaFlag = 1;
+  im->alphaBlendingFlag = 0;
+  im->thick = 1;
+  return im;
 }
 
-void gdImageDestroy(gdImagePtr im)
+void
+gdImageDestroy (gdImagePtr im)
 {
-	int i;
-	if (im->pixels) {
-		for (i=0; (i<im->sy); i++) {
-			gdFree(im->pixels[i]);
-		}	
-		gdFree(im->pixels);
-	}
-	if (im->tpixels) {
-		for (i=0; (i<im->sy); i++) {
-			gdFree(im->tpixels[i]);
-		}	
-		gdFree(im->tpixels);
-	}
-	if (im->polyInts) {
-			gdFree(im->polyInts);
-	}
-	if (im->style) {
-		gdFree(im->style);
-	}
-	gdFree(im);
+  int i;
+  if (im->pixels)
+    {
+      for (i = 0; (i < im->sy); i++)
+	{
+	  gdFree (im->pixels[i]);
+	}
+      gdFree (im->pixels);
+    }
+  if (im->tpixels)
+    {
+      for (i = 0; (i < im->sy); i++)
+	{
+	  gdFree (im->tpixels[i]);
+	}
+      gdFree (im->tpixels);
+    }
+  if (im->polyInts)
+    {
+      gdFree (im->polyInts);
+    }
+  if (im->style)
+    {
+      gdFree (im->style);
+    }
+  gdFree (im);
 }
 
-int gdImageColorClosest(gdImagePtr im, int r, int g, int b)
+int
+gdImageColorClosest (gdImagePtr im, int r, int g, int b)
 {
-	return gdImageColorClosestAlpha(im, r, g, b, gdAlphaOpaque);
+  return gdImageColorClosestAlpha (im, r, g, b, gdAlphaOpaque);
 }
 
-int gdImageColorClosestAlpha(gdImagePtr im, int r, int g, int b, int a)
+int
+gdImageColorClosestAlpha (gdImagePtr im, int r, int g, int b, int a)
 {
-	int i;
-	long rd, gd, bd, ad;
-	int ct = (-1);
-	int first = 1;
-	long mindist = 0;
-	if (im->trueColor) {
-		return gdTrueColorAlpha(r, g, b, a);
-	}
-	for (i=0; (i<(im->colorsTotal)); i++) {
-		long dist;
-		if (im->open[i]) {
-			continue;
-		}
-		rd = (im->red[i] - r);	
-		gd = (im->green[i] - g);
-		bd = (im->blue[i] - b);
-		ad = (im->blue[i] - b);
-		dist = rd * rd + gd * gd + bd * bd + ad * ad;
-		if (first || (dist < mindist)) {
-			mindist = dist;	
-			ct = i;
-			first = 0;
-		}
+  int i;
+  long rd, gd, bd, ad;
+  int ct = (-1);
+  int first = 1;
+  long mindist = 0;
+  if (im->trueColor)
+    {
+      return gdTrueColorAlpha (r, g, b, a);
+    }
+  for (i = 0; (i < (im->colorsTotal)); i++)
+    {
+      long dist;
+      if (im->open[i])
+	{
+	  continue;
+	}
+      rd = (im->red[i] - r);
+      gd = (im->green[i] - g);
+      bd = (im->blue[i] - b);
+      ad = (im->blue[i] - b);
+      dist = rd * rd + gd * gd + bd * bd + ad * ad;
+      if (first || (dist < mindist))
+	{
+	  mindist = dist;
+	  ct = i;
+	  first = 0;
 	}
-	return ct;
+    }
+  return ct;
 }
 
 /* This code is taken from http://www.acm.org/jgt/papers/SmithLyons96/hwb_rgb.html, an article
  * It has been modified to return the converted value as a * parameter. 
  */
 
-#define RETURN_HWB(h, w, b) {HWB->H = h; HWB->W = w; HWB->B = b; return HWB;} 
-#define RETURN_RGB(r, g, b) {RGB->R = r; RGB->G = g; RGB->B = b; return RGB;} 
+#define RETURN_HWB(h, w, b) {HWB->H = h; HWB->W = w; HWB->B = b; return HWB;}
+#define RETURN_RGB(r, g, b) {RGB->R = r; RGB->G = g; RGB->B = b; return RGB;}
 #define HWB_UNDEFINED -1
 #define SETUP_RGB(s, r, g, b) {s.R = r/255.0; s.G = g/255.0; s.B = b/255.0;}
 
  * red always maps to 6 in this implementation. Therefore UNDEFINED can be 
  * defined as 0 in situations where only unsigned numbers are desired.
  */
-typedef struct {float R, G, B;} RGBType; 
-typedef struct {float H, W, B;} HWBType;
-
-static HWBType* RGB_to_HWB( RGBType RGB, HWBType* HWB) {
-
-	/*
-	 * RGB are each on [0, 1]. W and B are returned on [0, 1] and H is  
-	 * returned on [0, 6]. Exception: H is returned UNDEFINED if W == 1 - B.  
-	 */
-
- 	float R = RGB.R, G = RGB.G, B = RGB.B, w, v, b, f;  
-	int i;  
-  
- 	w = MIN3(R, G, B);  
- 	v = MAX3(R, G, B);  
- 	b = 1 - v;  
-	if (v == w) RETURN_HWB(HWB_UNDEFINED, w, b);  
- 	f = (R == w) ? G - B : ((G == w) ? B - R : R - G);  
- 	i = (R == w) ? 3 : ((G == w) ? 5 : 1);  
-	RETURN_HWB(i - f /(v - w), w, b);  
+typedef struct
+{
+  float R, G, B;
+}
+RGBType;
+typedef struct
+  {
+    float H, W, B;
+  }
+HWBType;
+
+static HWBType *
+RGB_to_HWB (RGBType RGB, HWBType * HWB)
+{
+
+  /*
+   * RGB are each on [0, 1]. W and B are returned on [0, 1] and H is  
+   * returned on [0, 6]. Exception: H is returned UNDEFINED if W == 1 - B.  
+   */
+
+  float R = RGB.R, G = RGB.G, B = RGB.B, w, v, b, f;
+  int i;
+
+  w = MIN3 (R, G, B);
+  v = MAX3 (R, G, B);
+  b = 1 - v;
+  if (v == w)
+    RETURN_HWB (HWB_UNDEFINED, w, b);
+  f = (R == w) ? G - B : ((G == w) ? B - R : R - G);
+  i = (R == w) ? 3 : ((G == w) ? 5 : 1);
+  RETURN_HWB (i - f / (v - w), w, b);
 
 }
 
-static float HWB_Diff(int r1, int g1, int b1, int r2, int g2, int b2) {
-	RGBType		RGB1, RGB2;
-	HWBType		HWB1, HWB2;
-	float		diff;
+static float
+HWB_Diff (int r1, int g1, int b1, int r2, int g2, int b2)
+{
+  RGBType RGB1, RGB2;
+  HWBType HWB1, HWB2;
+  float diff;
 
-	SETUP_RGB(RGB1, r1, g1, b1);
-	SETUP_RGB(RGB2, r2, g2, b2);	
+  SETUP_RGB (RGB1, r1, g1, b1);
+  SETUP_RGB (RGB2, r2, g2, b2);
 
-	RGB_to_HWB(RGB1, &HWB1);
-	RGB_to_HWB(RGB2, &HWB2);
+  RGB_to_HWB (RGB1, &HWB1);
+  RGB_to_HWB (RGB2, &HWB2);
 
-	/*
-	 * I made this bit up; it seems to produce OK results, and it is certainly
-	 * more visually correct than the current RGB metric. (PJW)
-	 */
+  /*
+   * I made this bit up; it seems to produce OK results, and it is certainly
+   * more visually correct than the current RGB metric. (PJW)
+   */
 
-	if ( (HWB1.H == HWB_UNDEFINED) || (HWB2.H == HWB_UNDEFINED) ) {
-		diff = 0; /* Undefined hues always match... */
-	} else {
-		diff = abs(HWB1.H - HWB2.H);
-		if (diff > 3) {
-			diff = 6 - diff; /* Remember, it's a colour circle */
-		}
+  if ((HWB1.H == HWB_UNDEFINED) || (HWB2.H == HWB_UNDEFINED))
+    {
+      diff = 0;			/* Undefined hues always match... */
+    }
+  else
+    {
+      diff = abs (HWB1.H - HWB2.H);
+      if (diff > 3)
+	{
+	  diff = 6 - diff;	/* Remember, it's a colour circle */
 	}
+    }
 
-	diff = diff*diff + (HWB1.W - HWB2.W)*(HWB1.W - HWB2.W) + (HWB1.B - HWB2.B)*(HWB1.B - HWB2.B);
+  diff = diff * diff + (HWB1.W - HWB2.W) * (HWB1.W - HWB2.W) + (HWB1.B - HWB2.B) * (HWB1.B - HWB2.B);
 
-	return diff;
+  return diff;
 }
 
 
  * This is not actually used, but is here for completeness, in case someone wants to
  * use the HWB stuff for anything else...
  */
-static RGBType* HWB_to_RGB( HWBType HWB, RGBType* RGB ) {
-
-	/* 
-	 * H is given on [0, 6] or UNDEFINED. W and B are given on [0, 1].  
-	 * RGB are each returned on [0, 1]. 
-	 */
- 
-	float h = HWB.H, w = HWB.W, b = HWB.B, v, n, f;  
-	int i;  
-  
-	v = 1 - b;  
-	if (h == HWB_UNDEFINED) RETURN_RGB(v, v, v);  
-	i = floor(h);  
-	f = h - i;  
-	if (i & 1) f = 1 - f; /* if i is odd */ 
-	n = w + f * (v - w); /* linear interpolation between w and v */ 
-	switch (i) {  
-		case 6:  
-		case 0: RETURN_RGB(v, n, w);  
-		case 1: RETURN_RGB(n, v, w);  
-		case 2: RETURN_RGB(w, v, n);  
-		case 3: RETURN_RGB(w, n, v);  
-		case 4: RETURN_RGB(n, w, v);  
-		case 5: RETURN_RGB(v, w, n);  
-	}  
-
-    return RGB;
+static RGBType *
+HWB_to_RGB (HWBType HWB, RGBType * RGB)
+{
+
+  /* 
+   * H is given on [0, 6] or UNDEFINED. W and B are given on [0, 1].  
+   * RGB are each returned on [0, 1]. 
+   */
+
+  float h = HWB.H, w = HWB.W, b = HWB.B, v, n, f;
+  int i;
+
+  v = 1 - b;
+  if (h == HWB_UNDEFINED)
+    RETURN_RGB (v, v, v);
+  i = floor (h);
+  f = h - i;
+  if (i & 1)
+    f = 1 - f;			/* if i is odd */
+  n = w + f * (v - w);		/* linear interpolation between w and v */
+  switch (i)
+    {
+    case 6:
+    case 0:
+      RETURN_RGB (v, n, w);
+    case 1:
+      RETURN_RGB (n, v, w);
+    case 2:
+      RETURN_RGB (w, v, n);
+    case 3:
+      RETURN_RGB (w, n, v);
+    case 4:
+      RETURN_RGB (n, w, v);
+    case 5:
+      RETURN_RGB (v, w, n);
+    }
+
+  return RGB;
 
 }
 
-int gdImageColorClosestHWB(gdImagePtr im, int r, int g, int b)
+int
+gdImageColorClosestHWB (gdImagePtr im, int r, int g, int b)
 {
-	int i;
-	/* long rd, gd, bd; */
-	int ct = (-1);
-	int first = 1;
-	float mindist = 0;
-	if (im->trueColor) {
-		return gdTrueColor(r, g, b);
-	}
-	for (i=0; (i<(im->colorsTotal)); i++) {
-		float dist;
-		if (im->open[i]) {
-			continue;
-		}
-		dist = HWB_Diff(im->red[i], im->green[i], im->blue[i], r, g, b); 
-		if (first || (dist < mindist)) {
-			mindist = dist;	
-			ct = i;
-			first = 0;
-		}
+  int i;
+  /* long rd, gd, bd; */
+  int ct = (-1);
+  int first = 1;
+  float mindist = 0;
+  if (im->trueColor)
+    {
+      return gdTrueColor (r, g, b);
+    }
+  for (i = 0; (i < (im->colorsTotal)); i++)
+    {
+      float dist;
+      if (im->open[i])
+	{
+	  continue;
+	}
+      dist = HWB_Diff (im->red[i], im->green[i], im->blue[i], r, g, b);
+      if (first || (dist < mindist))
+	{
+	  mindist = dist;
+	  ct = i;
+	  first = 0;
 	}
-	return ct;
+    }
+  return ct;
 }
 
-int gdImageColorExact(gdImagePtr im, int r, int g, int b)
+int
+gdImageColorExact (gdImagePtr im, int r, int g, int b)
 {
-	return gdImageColorExactAlpha(im, r, g, b, gdAlphaOpaque);
+  return gdImageColorExactAlpha (im, r, g, b, gdAlphaOpaque);
 }
 
-int gdImageColorExactAlpha(gdImagePtr im, int r, int g, int b, int a)
+int
+gdImageColorExactAlpha (gdImagePtr im, int r, int g, int b, int a)
 {
-	int i;
-	if (im->trueColor) {
-		return gdTrueColorAlpha(r, g, b, a);
+  int i;
+  if (im->trueColor)
+    {
+      return gdTrueColorAlpha (r, g, b, a);
+    }
+  for (i = 0; (i < (im->colorsTotal)); i++)
+    {
+      if (im->open[i])
+	{
+	  continue;
 	}
-	for (i=0; (i<(im->colorsTotal)); i++) {
-		if (im->open[i]) {
-			continue;
-		}
-		if ((im->red[i] == r) && 
-			(im->green[i] == g) &&
-			(im->blue[i] == b) &&
-			(im->alpha[i] == a))
-		{
-			return i;
-		}
+      if ((im->red[i] == r) &&
+	  (im->green[i] == g) &&
+	  (im->blue[i] == b) &&
+	  (im->alpha[i] == a))
+	{
+	  return i;
 	}
-	return -1;
+    }
+  return -1;
 }
 
-int gdImageColorAllocate(gdImagePtr im, int r, int g, int b)
+int
+gdImageColorAllocate (gdImagePtr im, int r, int g, int b)
 {
-	return gdImageColorAllocateAlpha(im, r, g, b, gdAlphaOpaque);
+  return gdImageColorAllocateAlpha (im, r, g, b, gdAlphaOpaque);
 }
 
-int gdImageColorAllocateAlpha(gdImagePtr im, int r, int g, int b, int a)
+int
+gdImageColorAllocateAlpha (gdImagePtr im, int r, int g, int b, int a)
 {
-	int i;
-	int ct = (-1);
-	if (im->trueColor) {
-		return gdTrueColorAlpha(r, g, b, a);
-	}
-	for (i=0; (i<(im->colorsTotal)); i++) {
-		if (im->open[i]) {
-			ct = i;
-			break;
-		}
-	}	
-	if (ct == (-1)) {
-		ct = im->colorsTotal;
-		if (ct == gdMaxColors) {
-			return -1;
-		}
-		im->colorsTotal++;
-	}
-	im->red[ct] = r;
-	im->green[ct] = g;
-	im->blue[ct] = b;
-	im->alpha[ct] = a;
-	im->open[ct] = 0;
-	return ct;
+  int i;
+  int ct = (-1);
+  if (im->trueColor)
+    {
+      return gdTrueColorAlpha (r, g, b, a);
+    }
+  for (i = 0; (i < (im->colorsTotal)); i++)
+    {
+      if (im->open[i])
+	{
+	  ct = i;
+	  break;
+	}
+    }
+  if (ct == (-1))
+    {
+      ct = im->colorsTotal;
+      if (ct == gdMaxColors)
+	{
+	  return -1;
+	}
+      im->colorsTotal++;
+    }
+  im->red[ct] = r;
+  im->green[ct] = g;
+  im->blue[ct] = b;
+  im->alpha[ct] = a;
+  im->open[ct] = 0;
+  return ct;
 }
 
 /*
  * return a color index in one search over the color table.
  */
 
-int gdImageColorResolve(gdImagePtr im, int r, int g, int b)
+int
+gdImageColorResolve (gdImagePtr im, int r, int g, int b)
 {
-	return gdImageColorResolveAlpha(im, r, g, b, gdAlphaOpaque);
+  return gdImageColorResolveAlpha (im, r, g, b, gdAlphaOpaque);
 }
 
-int gdImageColorResolveAlpha(gdImagePtr im, int r, int g, int b, int a)
+int
+gdImageColorResolveAlpha (gdImagePtr im, int r, int g, int b, int a)
 {
-        int c;
-        int ct = -1;
-        int op = -1;
-        long rd, gd, bd, ad, dist;
-        long mindist = 4*255*255;  /* init to max poss dist */
-	if (im->trueColor) {
-		return gdTrueColorAlpha(r, g, b, a);
-	}
-
-        for (c = 0; c < im->colorsTotal; c++) {
-                if (im->open[c]) {
-                        op = c;                         /* Save open slot */
-                        continue;                       /* Color not in use */
-                }
-                rd = (long)(im->red  [c] - r);
-                gd = (long)(im->green[c] - g);
-                bd = (long)(im->blue [c] - b);
-                ad = (long)(im->alpha [c] - a);
-                dist = rd * rd + gd * gd + bd * bd + ad * ad;
-                if (dist < mindist) {
-                        if (dist == 0) {
-                                return c;               /* Return exact match color */
-                        }
-                        mindist = dist;
-                        ct = c;
-                }
-        }
-        /* no exact match.  We now know closest, but first try to allocate exact */
-        if (op == -1) {
-                op = im->colorsTotal;
-                if (op == gdMaxColors) {    /* No room for more colors */
-                        return ct;              /* Return closest available color */
-                }
-                im->colorsTotal++;
-        }
-        im->red  [op] = r;
-        im->green[op] = g;
-        im->blue [op] = b;
-        im->alpha [op] = a;
-        im->open [op] = 0;
-        return op;                                      /* Return newly allocated color */
-}
+  int c;
+  int ct = -1;
+  int op = -1;
+  long rd, gd, bd, ad, dist;
+  long mindist = 4 * 255 * 255;	/* init to max poss dist */
+  if (im->trueColor)
+    {
+      return gdTrueColorAlpha (r, g, b, a);
+    }
 
-void gdImageColorDeallocate(gdImagePtr im, int color)
-{
-	if (im->trueColor) {
-		return;
+  for (c = 0; c < im->colorsTotal; c++)
+    {
+      if (im->open[c])
+	{
+	  op = c;		/* Save open slot */
+	  continue;		/* Color not in use */
+	}
+      rd = (long) (im->red[c] - r);
+      gd = (long) (im->green[c] - g);
+      bd = (long) (im->blue[c] - b);
+      ad = (long) (im->alpha[c] - a);
+      dist = rd * rd + gd * gd + bd * bd + ad * ad;
+      if (dist < mindist)
+	{
+	  if (dist == 0)
+	    {
+	      return c;		/* Return exact match color */
+	    }
+	  mindist = dist;
+	  ct = c;
 	}
-	/* Mark it open. */
-	im->open[color] = 1;
+    }
+  /* no exact match.  We now know closest, but first try to allocate exact */
+  if (op == -1)
+    {
+      op = im->colorsTotal;
+      if (op == gdMaxColors)
+	{			/* No room for more colors */
+	  return ct;		/* Return closest available color */
+	}
+      im->colorsTotal++;
+    }
+  im->red[op] = r;
+  im->green[op] = g;
+  im->blue[op] = b;
+  im->alpha[op] = a;
+  im->open[op] = 0;
+  return op;			/* Return newly allocated color */
 }
 
-void gdImageColorTransparent(gdImagePtr im, int color)
+void
+gdImageColorDeallocate (gdImagePtr im, int color)
 {
-	if (!im->trueColor) {
-		if (im->transparent != -1) {
-			im->alpha[im->transparent] = gdAlphaOpaque;
-		}
-		if (color != -1) {
-			im->alpha[color] = gdAlphaTransparent;
-		} 
-	} 
-	im->transparent = color;
+  if (im->trueColor)
+    {
+      return;
+    }
+  /* Mark it open. */
+  im->open[color] = 1;
 }
 
-void gdImagePaletteCopy(gdImagePtr to, gdImagePtr from)
+void
+gdImageColorTransparent (gdImagePtr im, int color)
 {
-		int i;
-	int x, y, p;
-	int xlate[256];
-	if (to->trueColor) {
-		return;
+  if (!im->trueColor)
+    {
+      if (im->transparent != -1)
+	{
+	  im->alpha[im->transparent] = gdAlphaOpaque;
 	}
-	if (from->trueColor) {
-		return;
+      if (color != -1)
+	{
+	  im->alpha[color] = gdAlphaTransparent;
 	}
+    }
+  im->transparent = color;
+}
 
-	for (i=0; i < 256 ; i++) {
-		xlate[i] = -1;
-	};
+void
+gdImagePaletteCopy (gdImagePtr to, gdImagePtr from)
+{
+  int i;
+  int x, y, p;
+  int xlate[256];
+  if (to->trueColor)
+    {
+      return;
+    }
+  if (from->trueColor)
+    {
+      return;
+    }
 
-	for (x=0 ; x < (to->sx) ; x++) {
-		for (y=0 ; y < (to->sy) ; y++) {
-			p = gdImageGetPixel(to, x, y);
-			if (xlate[p] == -1) {
-				/* This ought to use HWB, but we don't have an alpha-aware
-					version of that yet. */
-				xlate[p] = gdImageColorClosestAlpha(from, to->red[p], to->green[p], to->blue[p], to->alpha[p]);
-				/*printf("Mapping %d (%d, %d, %d, %d) to %d (%d, %d, %d, %d)\n", */
-				/*	p,  to->red[p], to->green[p], to->blue[p], to->alpha[p], */
-				/*	xlate[p], from->red[xlate[p]], from->green[xlate[p]], from->blue[xlate[p]], from->alpha[xlate[p]]); */
-			};
-			gdImageSetPixel(to, x, y, xlate[p]);
-		};
-	};
+  for (i = 0; i < 256; i++)
+    {
+      xlate[i] = -1;
+    };
 
-        for (i=0; (i < (from->colorsTotal) ) ; i++) {
-		/*printf("Copying color %d (%d, %d, %d, %d)\n", i, from->red[i], from->blue[i], from->green[i], from->alpha[i]); */
-		to->red[i] = from->red[i];
-                to->blue[i] = from->blue[i];
-                to->green[i] = from->green[i];
-                to->alpha[i] = from->alpha[i];
-		to->open[i] = 0;
-        };
-
-	for (i=from->colorsTotal ; (i < to->colorsTotal) ; i++) { 
-		to->open[i] = 1;
+  for (x = 0; x < (to->sx); x++)
+    {
+      for (y = 0; y < (to->sy); y++)
+	{
+	  p = gdImageGetPixel (to, x, y);
+	  if (xlate[p] == -1)
+	    {
+	      /* This ought to use HWB, but we don't have an alpha-aware
+	         version of that yet. */
+	      xlate[p] = gdImageColorClosestAlpha (from, to->red[p], to->green[p], to->blue[p], to->alpha[p]);
+	      /*printf("Mapping %d (%d, %d, %d, %d) to %d (%d, %d, %d, %d)\n", */
+	      /*      p,  to->red[p], to->green[p], to->blue[p], to->alpha[p], */
+	      /*      xlate[p], from->red[xlate[p]], from->green[xlate[p]], from->blue[xlate[p]], from->alpha[xlate[p]]); */
+	    };
+	  gdImageSetPixel (to, x, y, xlate[p]);
 	};
+    };
+
+  for (i = 0; (i < (from->colorsTotal)); i++)
+    {
+      /*printf("Copying color %d (%d, %d, %d, %d)\n", i, from->red[i], from->blue[i], from->green[i], from->alpha[i]); */
+      to->red[i] = from->red[i];
+      to->blue[i] = from->blue[i];
+      to->green[i] = from->green[i];
+      to->alpha[i] = from->alpha[i];
+      to->open[i] = 0;
+    };
+
+  for (i = from->colorsTotal; (i < to->colorsTotal); i++)
+    {
+      to->open[i] = 1;
+    };
 
-	to->colorsTotal = from->colorsTotal;
+  to->colorsTotal = from->colorsTotal;
 
 }
 
-void gdImageSetPixel(gdImagePtr im, int x, int y, int color)
+void
+gdImageSetPixel (gdImagePtr im, int x, int y, int color)
 {
-	int p;
-	switch(color) {
-		case gdStyled:
-		if (!im->style) {
-			/* Refuse to draw if no style is set. */
-			return;
-		} else {
-			p = im->style[im->stylePos++];
-		}
-		if (p != (gdTransparent)) {
-			gdImageSetPixel(im, x, y, p);
-		}
-		im->stylePos = im->stylePos %  im->styleLength;
-		break;
-		case gdStyledBrushed:
-		if (!im->style) {
-			/* Refuse to draw if no style is set. */
-			return;
-		}
-		p = im->style[im->stylePos++];
-		if ((p != gdTransparent) && (p != 0)) {
-			gdImageSetPixel(im, x, y, gdBrushed);
+  int p;
+  switch (color)
+    {
+    case gdStyled:
+      if (!im->style)
+	{
+	  /* Refuse to draw if no style is set. */
+	  return;
+	}
+      else
+	{
+	  p = im->style[im->stylePos++];
+	}
+      if (p != (gdTransparent))
+	{
+	  gdImageSetPixel (im, x, y, p);
+	}
+      im->stylePos = im->stylePos % im->styleLength;
+      break;
+    case gdStyledBrushed:
+      if (!im->style)
+	{
+	  /* Refuse to draw if no style is set. */
+	  return;
+	}
+      p = im->style[im->stylePos++];
+      if ((p != gdTransparent) && (p != 0))
+	{
+	  gdImageSetPixel (im, x, y, gdBrushed);
+	}
+      im->stylePos = im->stylePos % im->styleLength;
+      break;
+    case gdBrushed:
+      gdImageBrushApply (im, x, y);
+      break;
+    case gdTiled:
+      gdImageTileApply (im, x, y);
+      break;
+    default:
+      if (gdImageBoundsSafe (im, x, y))
+	{
+	  if (im->trueColor)
+	    {
+	      if (im->alphaBlendingFlag)
+		{
+		  im->tpixels[y][x] =
+		    gdAlphaBlend (im->tpixels[y][x],
+				  color);
 		}
-		im->stylePos = im->stylePos %  im->styleLength;
-		break;
-		case gdBrushed:
-		gdImageBrushApply(im, x, y);
-		break;
-		case gdTiled:
-		gdImageTileApply(im, x, y);
-		break;
-		default:
-		if (gdImageBoundsSafe(im, x, y)) {
-			if (im->trueColor) {
-				if (im->alphaBlendingFlag) {
-					im->tpixels[y][x] = 
-						gdAlphaBlend(im->tpixels[y][x],
-							color);
-				} else {
-					im->tpixels[y][x] = color;
-				}
-			} else {
-				im->pixels[y][x] = color;
-			}
+	      else
+		{
+		  im->tpixels[y][x] = color;
 		}
-		break;
+	    }
+	  else
+	    {
+	      im->pixels[y][x] = color;
+	    }
 	}
+      break;
+    }
 }
 
-static void gdImageBrushApply(gdImagePtr im, int x, int y)
+static void
+gdImageBrushApply (gdImagePtr im, int x, int y)
 {
-	int lx, ly;
-	int hy;
-	int hx;
-	int x1, y1, x2, y2;
-	int srcx, srcy;
-	if (!im->brush) {
-		return;
-	}
-	hy = gdImageSY(im->brush)/2;
-	y1 = y - hy;
-	y2 = y1 + gdImageSY(im->brush);	
-	hx = gdImageSX(im->brush)/2;
-	x1 = x - hx;
-	x2 = x1 + gdImageSX(im->brush);
-	srcy = 0;
-	if (im->trueColor) {
-		for (ly = y1; (ly < y2); ly++) {
-			srcx = 0;
-			for (lx = x1; (lx < x2); lx++) {
-				int p;
-				p = gdImageGetTrueColorPixel(
-					im->brush, srcx, srcy);
-				gdImageSetPixel(im, lx, ly,
-					p);
-				srcx++;
-			}
-			srcy++;
-		}	
-	} else {
-		for (ly = y1; (ly < y2); ly++) {
-			srcx = 0;
-			for (lx = x1; (lx < x2); lx++) {
-				int p;
-				p = gdImageGetPixel(im->brush, srcx, srcy);
-				/* Allow for non-square brushes! */
-				if (p != gdImageGetTransparent(im->brush)) {
-					/* Truecolor brush. Very slow
-						on a palette destination. */
-					if (im->brush->trueColor) {
-						gdImageSetPixel(im, lx, ly,
-							gdImageColorResolveAlpha(
-								im, 
-								gdTrueColorGetRed(p),
-								gdTrueColorGetGreen(p),
-								gdTrueColorGetBlue(p),
-								gdTrueColorGetAlpha(p)));
-					} else {
-						gdImageSetPixel(im, lx, ly,
-							im->brushColorMap[p]);
-					}
-				}
-				srcx++;
-			}
-			srcy++;
-		}	
+  int lx, ly;
+  int hy;
+  int hx;
+  int x1, y1, x2, y2;
+  int srcx, srcy;
+  if (!im->brush)
+    {
+      return;
+    }
+  hy = gdImageSY (im->brush) / 2;
+  y1 = y - hy;
+  y2 = y1 + gdImageSY (im->brush);
+  hx = gdImageSX (im->brush) / 2;
+  x1 = x - hx;
+  x2 = x1 + gdImageSX (im->brush);
+  srcy = 0;
+  if (im->trueColor)
+    {
+      for (ly = y1; (ly < y2); ly++)
+	{
+	  srcx = 0;
+	  for (lx = x1; (lx < x2); lx++)
+	    {
+	      int p;
+	      p = gdImageGetTrueColorPixel (
+					     im->brush, srcx, srcy);
+	      gdImageSetPixel (im, lx, ly,
+			       p);
+	      srcx++;
+	    }
+	  srcy++;
 	}
-}		
-
-static void gdImageTileApply(gdImagePtr im, int x, int y)
-{
-	int srcx, srcy;
-	int p;
-	if (!im->tile) {
-		return;
-	}
-	srcx = x % gdImageSX(im->tile);
-	srcy = y % gdImageSY(im->tile);
-	if (im->trueColor) {
-		p = gdImageGetTrueColorPixel(im->tile, srcx, srcy);
-		gdImageSetPixel(im, x, y, p);
-	} else {
-		/* Allow for transparency */
-		if (p != gdImageGetTransparent(im->tile)) {
-			if (im->tile->trueColor) {
-				/* Truecolor tile. Very slow
-					on a palette destination. */
-				gdImageSetPixel(im, x, y,
-					gdImageColorResolveAlpha(
-						im, 
-						gdTrueColorGetRed(p),
-						gdTrueColorGetGreen(p),
-						gdTrueColorGetBlue(p),
-						gdTrueColorGetAlpha(p)));
-			} else {
-				gdImageSetPixel(im, x, y,
-					im->tileColorMap[p]);
-			}
+    }
+  else
+    {
+      for (ly = y1; (ly < y2); ly++)
+	{
+	  srcx = 0;
+	  for (lx = x1; (lx < x2); lx++)
+	    {
+	      int p;
+	      p = gdImageGetPixel (im->brush, srcx, srcy);
+	      /* Allow for non-square brushes! */
+	      if (p != gdImageGetTransparent (im->brush))
+		{
+		  /* Truecolor brush. Very slow
+		     on a palette destination. */
+		  if (im->brush->trueColor)
+		    {
+		      gdImageSetPixel (im, lx, ly,
+				       gdImageColorResolveAlpha (
+								  im,
+						      gdTrueColorGetRed (p),
+						    gdTrueColorGetGreen (p),
+						     gdTrueColorGetBlue (p),
+						  gdTrueColorGetAlpha (p)));
+		    }
+		  else
+		    {
+		      gdImageSetPixel (im, lx, ly,
+				       im->brushColorMap[p]);
+		    }
 		}
+	      srcx++;
+	    }
+	  srcy++;
 	}
-}		
+    }
+}
 
-int gdImageGetPixel(gdImagePtr im, int x, int y)
+static void
+gdImageTileApply (gdImagePtr im, int x, int y)
 {
-	if (gdImageBoundsSafe(im, x, y)) {
-		if (im->trueColor) {
-			return im->tpixels[y][x];
-		} else {
-			return im->pixels[y][x];
-		}
-	} else {
-		return 0;
+  int srcx, srcy;
+  int p;
+  if (!im->tile)
+    {
+      return;
+    }
+  srcx = x % gdImageSX (im->tile);
+  srcy = y % gdImageSY (im->tile);
+  if (im->trueColor)
+    {
+      p = gdImageGetTrueColorPixel (im->tile, srcx, srcy);
+      gdImageSetPixel (im, x, y, p);
+    }
+  else
+    {
+      /* Allow for transparency */
+      if (p != gdImageGetTransparent (im->tile))
+	{
+	  if (im->tile->trueColor)
+	    {
+	      /* Truecolor tile. Very slow
+	         on a palette destination. */
+	      gdImageSetPixel (im, x, y,
+			       gdImageColorResolveAlpha (
+							  im,
+						      gdTrueColorGetRed (p),
+						    gdTrueColorGetGreen (p),
+						     gdTrueColorGetBlue (p),
+						  gdTrueColorGetAlpha (p)));
+	    }
+	  else
+	    {
+	      gdImageSetPixel (im, x, y,
+			       im->tileColorMap[p]);
+	    }
 	}
+    }
 }
 
-int gdImageGetTrueColorPixel(gdImagePtr im, int x, int y)
+int
+gdImageGetPixel (gdImagePtr im, int x, int y)
 {
-	int p = gdImageGetPixel(im, x, y);
-	if (!im->trueColor) {
-		return gdTrueColorAlpha(im->red[p], im->green[p], im->blue[p],
-			(im->transparent == p) ? gdAlphaTransparent :
-			gdAlphaOpaque);
-	} else {
-		return p;
+  if (gdImageBoundsSafe (im, x, y))
+    {
+      if (im->trueColor)
+	{
+	  return im->tpixels[y][x];
+	}
+      else
+	{
+	  return im->pixels[y][x];
 	}
+    }
+  else
+    {
+      return 0;
+    }
+}
+
+int
+gdImageGetTrueColorPixel (gdImagePtr im, int x, int y)
+{
+  int p = gdImageGetPixel (im, x, y);
+  if (!im->trueColor)
+    {
+      return gdTrueColorAlpha (im->red[p], im->green[p], im->blue[p],
+			       (im->transparent == p) ? gdAlphaTransparent :
+			       gdAlphaOpaque);
+    }
+  else
+    {
+      return p;
+    }
 }
 
 /* Bresenham as presented in Foley & Van Dam */
-void gdImageLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color)
+void
+gdImageLine (gdImagePtr im, int x1, int y1, int x2, int y2, int color)
 {
-	int dx, dy, incr1, incr2, d, x, y, xend, yend, xdirflag, ydirflag;
-	int wid;
-	int w, wstart;
-	int thick = im->thick;
-	dx = abs(x2-x1);
-	dy = abs(y2-y1);
-	if (dy <= dx) {
-		/* More-or-less horizontal. use wid for vertical stroke */
-		wid = thick * cos ( atan2(dy, dx) );
-		if (wid==0) wid=1;
-
-		d = 2*dy - dx;
-		incr1 = 2*dy;
-		incr2 = 2 * (dy - dx);
-		if (x1 > x2) {
-			x = x2;
-			y = y2;
-			ydirflag = (-1);
-			xend = x1;
-		} else {
-			x = x1;
-			y = y1;
-			ydirflag = 1;
-			xend = x2;
-		}
+  int dx, dy, incr1, incr2, d, x, y, xend, yend, xdirflag, ydirflag;
+  int wid;
+  int w, wstart;
+  int thick = im->thick;
+  dx = abs (x2 - x1);
+  dy = abs (y2 - y1);
+  if (dy <= dx)
+    {
+      /* More-or-less horizontal. use wid for vertical stroke */
+      wid = thick * cos (atan2 (dy, dx));
+      if (wid == 0)
+	wid = 1;
+
+      d = 2 * dy - dx;
+      incr1 = 2 * dy;
+      incr2 = 2 * (dy - dx);
+      if (x1 > x2)
+	{
+	  x = x2;
+	  y = y2;
+	  ydirflag = (-1);
+	  xend = x1;
+	}
+      else
+	{
+	  x = x1;
+	  y = y1;
+	  ydirflag = 1;
+	  xend = x2;
+	}
 
-		/* Set up line thickness */
-		wstart = y - wid/2;
-		for ( w = wstart; w < wstart + wid; w++ )
-		  gdImageSetPixel(im, x, w, color);
-		  
-		if (((y2 - y1) * ydirflag) > 0) {
-			while (x < xend) {
-				x++;
-				if (d <0) {
-					d+=incr1;
-				} else {
-					y++;
-					d+=incr2;
-				}
-				wstart = y - wid/2;
-				for ( w = wstart; w < wstart + wid; w++)
-				  gdImageSetPixel(im, x, w, color);
-			}
-		} else {
- 			while (x < xend) {
-				x++;
-				if (d <0) {
-					d+=incr1;
-				} else {
-					y--;
-					d+=incr2;
-				}
-				wstart = y - wid / 2;
-				for ( w=wstart; w < wstart+wid; w++)
-				  gdImageSetPixel(im, x, w, color);
-			}
-		}		
-	} else {
-  		/* More-or-less vertical. use wid for horizontal stroke */
-		wid = thick * sin ( atan2(dy, dx) );
-		if (wid == 0 ) wid=1;
-
-		d = 2*dx - dy;
-		incr1 = 2*dx;
-		incr2 = 2 * (dx - dy);
-		if (y1 > y2) {
-			y = y2;
-			x = x2;
-			yend = y1;
-			xdirflag = (-1);
-		} else {
-			y = y1;
-			x = x1;
-			yend = y2;
-			xdirflag = 1;
-		}
+      /* Set up line thickness */
+      wstart = y - wid / 2;
+      for (w = wstart; w < wstart + wid; w++)
+	gdImageSetPixel (im, x, w, color);
 
-		/* Set up line thickness */
-		wstart = x - wid/2;
-		for ( w = wstart; w < wstart + wid; w++ )
-		  gdImageSetPixel(im, w, y, color);
-
-		if (((x2 - x1) * xdirflag) > 0) {
-			while (y < yend) {
-				y++;
-				if (d <0) {
-					d+=incr1;
-				} else {
-					x++;
-					d+=incr2;
-				}
-				wstart = x - wid/2;
-				for ( w=wstart; w < wstart + wid; w++)
-				  gdImageSetPixel(im, w, y, color);
-			}
-		} else {
-			while (y < yend) {
-				y++;
-				if (d <0) {
-					d+=incr1;
-				} else {
-					x--;
-					d+=incr2;
-				}
-				wstart = x - wid/2;
-				for ( w = wstart; w < wstart+wid; w++)
-				  gdImageSetPixel(im, w, y, color);
-			}
+      if (((y2 - y1) * ydirflag) > 0)
+	{
+	  while (x < xend)
+	    {
+	      x++;
+	      if (d < 0)
+		{
+		  d += incr1;
 		}
-	}
-}
-static void dashedSet(gdImagePtr im, int x, int y, int color,
-	int *onP, int *dashStepP, int wid, int vert);
-
-void gdImageDashedLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color)
-{
-	int dx, dy, incr1, incr2, d, x, y, xend, yend, xdirflag, ydirflag;
-	int dashStep = 0;
-	int on = 1;
-        int wid;
-        int w, wstart, vert;
-	int thick = im->thick;
-
-	dx = abs(x2-x1);
-	dy = abs(y2-y1);
-	if (dy <= dx) {
-		/* More-or-less horizontal. use wid for vertical stroke */
-		wid = thick * sin ( atan2(dy, dx) );
-		vert=1;
-
-		d = 2*dy - dx;
-		incr1 = 2*dy;
-		incr2 = 2 * (dy - dx);
-		if (x1 > x2) {
-			x = x2;
-			y = y2;
-			ydirflag = (-1);
-			xend = x1;
-		} else {
-			x = x1;
-			y = y1;
-			ydirflag = 1;
-			xend = x2;
+	      else
+		{
+		  y++;
+		  d += incr2;
 		}
-		dashedSet(im, x, y, color, &on, &dashStep, wid, vert);
-		if (((y2 - y1) * ydirflag) > 0) {
-			while (x < xend) {
-				x++;
-				if (d <0) {
-					d+=incr1;
-				} else {
-					y++;
-					d+=incr2;
-				}
-				dashedSet(im, x, y, color, &on, &dashStep, wid, vert);
-			}
-		} else {
-			while (x < xend) {
-				x++;
-				if (d <0) {
-					d+=incr1;
-				} else {
-					y--;
-					d+=incr2;
-				}
-		                dashedSet(im, x, y, color, &on, &dashStep, wid, vert);
-			}
-		}		
-	} else {
-		/* More-or-less vertical. use wid for horizontal stroke */
-		wid = thick * sin ( atan2(dy, dx) );
-		vert = 0;
-
-		d = 2*dx - dy;
-		incr1 = 2*dx;
-		incr2 = 2 * (dx - dy);
-		if (y1 > y2) {
-			y = y2;
-			x = x2;
-			yend = y1;
-			xdirflag = (-1);
-		} else {
-			y = y1;
-			x = x1;
-			yend = y2;
-			xdirflag = 1;
+	      wstart = y - wid / 2;
+	      for (w = wstart; w < wstart + wid; w++)
+		gdImageSetPixel (im, x, w, color);
+	    }
+	}
+      else
+	{
+	  while (x < xend)
+	    {
+	      x++;
+	      if (d < 0)
+		{
+		  d += incr1;
 		}
-                dashedSet(im, x, y, color, &on, &dashStep, wid, vert);
-		if (((x2 - x1) * xdirflag) > 0) {
-			while (y < yend) {
-				y++;
-				if (d <0) {
-					d+=incr1;
-				} else {
-					x++;
-					d+=incr2;
-				}
-                                dashedSet(im, x, y, color, &on, &dashStep, wid, vert);
-			}
-		} else {
-			while (y < yend) {
-				y++;
-				if (d <0) {
-					d+=incr1;
-				} else {
-					x--;
-					d+=incr2;
-				}
-                                dashedSet(im, x, y, color, &on, &dashStep, wid, vert);
-			}
+	      else
+		{
+		  y--;
+		  d += incr2;
 		}
+	      wstart = y - wid / 2;
+	      for (w = wstart; w < wstart + wid; w++)
+		gdImageSetPixel (im, x, w, color);
+	    }
+	}
+    }
+  else
+    {
+      /* More-or-less vertical. use wid for horizontal stroke */
+      wid = thick * sin (atan2 (dy, dx));
+      if (wid == 0)
+	wid = 1;
+
+      d = 2 * dx - dy;
+      incr1 = 2 * dx;
+      incr2 = 2 * (dx - dy);
+      if (y1 > y2)
+	{
+	  y = y2;
+	  x = x2;
+	  yend = y1;
+	  xdirflag = (-1);
+	}
+      else
+	{
+	  y = y1;
+	  x = x1;
+	  yend = y2;
+	  xdirflag = 1;
 	}
-}
 
-static void dashedSet(gdImagePtr im, int x, int y, int color,
-	int *onP, int *dashStepP, int wid, int vert)
-{
-	int dashStep = *dashStepP;
-	int on = *onP;
-	int w, wstart;
-
-	dashStep++;
-	if (dashStep == gdDashSize) {
-		dashStep = 0;
-		on = !on;
-	}
-	if (on) {
-          if (vert) { 
-	    wstart = y - wid / 2; 
-	    for (w=wstart; w < wstart+wid;w++) 
-	      gdImageSetPixel(im, x, w, color); 
-	  }
-	  else 
-	  { 
-	    wstart = x - wid / 2; 
-	    for (w=wstart; w < wstart+wid;w++) 
-	      gdImageSetPixel(im, w, y, color); 
-	  }
-	}
-	*dashStepP = dashStep;
-	*onP = on;
-}
-	
-int gdImageBoundsSafe(gdImagePtr im, int x, int y)
-{
-	return (!(((y < 0) || (y >= im->sy)) ||
-		((x < 0) || (x >= im->sx))));
-}
+      /* Set up line thickness */
+      wstart = x - wid / 2;
+      for (w = wstart; w < wstart + wid; w++)
+	gdImageSetPixel (im, w, y, color);
 
-void gdImageChar(gdImagePtr im, gdFontPtr f, int x, int y, 
-	int c, int color)
-{
-	int cx, cy;
-	int px, py;
-	int fline;
-	cx = 0;
-	cy = 0;
-#ifdef CHARSET_EBCDIC
-        c = ASC(c);
-#endif /*CHARSET_EBCDIC*/
-	if ((c < f->offset) || (c >= (f->offset + f->nchars))) {
-		return;
-	}
-	fline = (c - f->offset) * f->h * f->w;
-	for (py = y; (py < (y + f->h)); py++) {
-		for (px = x; (px < (x + f->w)); px++) {
-			if (f->data[fline + cy * f->w + cx]) {
-				gdImageSetPixel(im, px, py, color);	
-			}
-			cx++;
+      if (((x2 - x1) * xdirflag) > 0)
+	{
+	  while (y < yend)
+	    {
+	      y++;
+	      if (d < 0)
+		{
+		  d += incr1;
 		}
-		cx = 0;
-		cy++;
+	      else
+		{
+		  x++;
+		  d += incr2;
+		}
+	      wstart = x - wid / 2;
+	      for (w = wstart; w < wstart + wid; w++)
+		gdImageSetPixel (im, w, y, color);
+	    }
 	}
-}
-
-void gdImageCharUp(gdImagePtr im, gdFontPtr f, 
-	int x, int y, int c, int color)
-{
-	int cx, cy;
-	int px, py;
-	int fline;
-	cx = 0;
-	cy = 0;
-#ifdef CHARSET_EBCDIC
-        c = ASC(c);
-#endif /*CHARSET_EBCDIC*/
-	if ((c < f->offset) || (c >= (f->offset + f->nchars))) {
-		return;
-	}
-	fline = (c - f->offset) * f->h * f->w;
-	for (py = y; (py > (y - f->w)); py--) {
-		for (px = x; (px < (x + f->h)); px++) {
-			if (f->data[fline + cy * f->w + cx]) {
-				gdImageSetPixel(im, px, py, color);	
-			}
-			cy++;
+      else
+	{
+	  while (y < yend)
+	    {
+	      y++;
+	      if (d < 0)
+		{
+		  d += incr1;
+		}
+	      else
+		{
+		  x--;
+		  d += incr2;
 		}
-		cy = 0;
-		cx++;
+	      wstart = x - wid / 2;
+	      for (w = wstart; w < wstart + wid; w++)
+		gdImageSetPixel (im, w, y, color);
+	    }
 	}
+    }
 }
+static void dashedSet (gdImagePtr im, int x, int y, int color,
+		       int *onP, int *dashStepP, int wid, int vert);
 
-void gdImageString(gdImagePtr im, gdFontPtr f, 
-	int x, int y, unsigned char *s, int color)
+void
+gdImageDashedLine (gdImagePtr im, int x1, int y1, int x2, int y2, int color)
 {
-	int i;
-	int l;
-	l = strlen((char *) s);
-	for (i=0; (i<l); i++) {
-		gdImageChar(im, f, x, y, s[i], color);
-		x += f->w;
-	}
-}
+  int dx, dy, incr1, incr2, d, x, y, xend, yend, xdirflag, ydirflag;
+  int dashStep = 0;
+  int on = 1;
+  int wid;
+  int w, wstart, vert;
+  int thick = im->thick;
 
-void gdImageStringUp(gdImagePtr im, gdFontPtr f, 
-	int x, int y, unsigned char *s, int color)
-{
-	int i;
-	int l;
-	l = strlen((char *) s);
-	for (i=0; (i<l); i++) {
-		gdImageCharUp(im, f, x, y, s[i], color);
-		y -= f->w;
+  dx = abs (x2 - x1);
+  dy = abs (y2 - y1);
+  if (dy <= dx)
+    {
+      /* More-or-less horizontal. use wid for vertical stroke */
+      wid = thick * sin (atan2 (dy, dx));
+      vert = 1;
+
+      d = 2 * dy - dx;
+      incr1 = 2 * dy;
+      incr2 = 2 * (dy - dx);
+      if (x1 > x2)
+	{
+	  x = x2;
+	  y = y2;
+	  ydirflag = (-1);
+	  xend = x1;
 	}
-}
-
-static int strlen16(unsigned short *s);
-
-void gdImageString16(gdImagePtr im, gdFontPtr f, 
-	int x, int y, unsigned short *s, int color)
-{
-	int i;
-	int l;
-	l = strlen16(s);
-	for (i=0; (i<l); i++) {
-		gdImageChar(im, f, x, y, s[i], color);
-		x += f->w;
+      else
+	{
+	  x = x1;
+	  y = y1;
+	  ydirflag = 1;
+	  xend = x2;
 	}
-}
-
-void gdImageStringUp16(gdImagePtr im, gdFontPtr f, 
-	int x, int y, unsigned short *s, int color)
-{
-	int i;
-	int l;
-	l = strlen16(s);
-	for (i=0; (i<l); i++) {
-		gdImageCharUp(im, f, x, y, s[i], color);
-		y -= f->w;
+      dashedSet (im, x, y, color, &on, &dashStep, wid, vert);
+      if (((y2 - y1) * ydirflag) > 0)
+	{
+	  while (x < xend)
+	    {
+	      x++;
+	      if (d < 0)
+		{
+		  d += incr1;
+		}
+	      else
+		{
+		  y++;
+		  d += incr2;
+		}
+	      dashedSet (im, x, y, color, &on, &dashStep, wid, vert);
+	    }
 	}
-}
-
-static int strlen16(unsigned short *s)
-{
-	int len = 0;
-	while (*s) {
-		s++;
-		len++;
+      else
+	{
+	  while (x < xend)
+	    {
+	      x++;
+	      if (d < 0)
+		{
+		  d += incr1;
+		}
+	      else
+		{
+		  y--;
+		  d += incr2;
+		}
+	      dashedSet (im, x, y, color, &on, &dashStep, wid, vert);
+	    }
+	}
+    }
+  else
+    {
+      /* More-or-less vertical. use wid for horizontal stroke */
+      wid = thick * sin (atan2 (dy, dx));
+      vert = 0;
+
+      d = 2 * dx - dy;
+      incr1 = 2 * dx;
+      incr2 = 2 * (dx - dy);
+      if (y1 > y2)
+	{
+	  y = y2;
+	  x = x2;
+	  yend = y1;
+	  xdirflag = (-1);
+	}
+      else
+	{
+	  y = y1;
+	  x = x1;
+	  yend = y2;
+	  xdirflag = 1;
+	}
+      dashedSet (im, x, y, color, &on, &dashStep, wid, vert);
+      if (((x2 - x1) * xdirflag) > 0)
+	{
+	  while (y < yend)
+	    {
+	      y++;
+	      if (d < 0)
+		{
+		  d += incr1;
+		}
+	      else
+		{
+		  x++;
+		  d += incr2;
+		}
+	      dashedSet (im, x, y, color, &on, &dashStep, wid, vert);
+	    }
+	}
+      else
+	{
+	  while (y < yend)
+	    {
+	      y++;
+	      if (d < 0)
+		{
+		  d += incr1;
+		}
+	      else
+		{
+		  x--;
+		  d += incr2;
+		}
+	      dashedSet (im, x, y, color, &on, &dashStep, wid, vert);
+	    }
 	}
-	return len;
+    }
 }
 
-#ifndef HAVE_LSQRT
-/* If you don't have a nice square root function for longs, you can use
-** this hack
-*/
-long lsqrt(long n)
+static void
+dashedSet (gdImagePtr im, int x, int y, int color,
+	   int *onP, int *dashStepP, int wid, int vert)
 {
-  long result = (long)sqrt((double)n);
-  return result;
+  int dashStep = *dashStepP;
+  int on = *onP;
+  int w, wstart;
+
+  dashStep++;
+  if (dashStep == gdDashSize)
+    {
+      dashStep = 0;
+      on = !on;
+    }
+  if (on)
+    {
+      if (vert)
+	{
+	  wstart = y - wid / 2;
+	  for (w = wstart; w < wstart + wid; w++)
+	    gdImageSetPixel (im, x, w, color);
+	}
+      else
+	{
+	  wstart = x - wid / 2;
+	  for (w = wstart; w < wstart + wid; w++)
+	    gdImageSetPixel (im, w, y, color);
+	}
+    }
+  *dashStepP = dashStep;
+  *onP = on;
 }
-#endif
 
-void gdImageEllipse (gdImagePtr im, int cx, int cy, int w, int h, int color)
+int
+gdImageBoundsSafe (gdImagePtr im, int x, int y)
 {
-  long d, b_sq, b_sq_4, b_sq_6;
-  long a_sq, a_sq_4, a_sq_6;
-  int x, y, switchem;
-  long lsqrt(long);
-  int pix, half, pstart;
-  int thick = im->thick;
+  return (!(((y < 0) || (y >= im->sy)) ||
+	    ((x < 0) || (x >= im->sx))));
+}
 
-  half = thick/2;
-  w /= 2; /* ImageArc uses diameter, not radius */
-  h /= 2;
-      
-  d = 2 * (long) h * h + (long) w * w - 2 * (long)w * w * h;
-  b_sq = (long)h * h;
-  b_sq_4 = 4 * (long) h * h;
-  b_sq_6 = 6 * (long) h * h;
-  a_sq = (long) w * w;
-  a_sq_4 = 4 * (long)w * w;
-  a_sq_6 = 6 * (long)w * w;
+void
+gdImageChar (gdImagePtr im, gdFontPtr f, int x, int y,
+	     int c, int color)
+{
+  int cx, cy;
+  int px, py;
+  int fline;
+  cx = 0;
+  cy = 0;
+#ifdef CHARSET_EBCDIC
+  c = ASC (c);
+#endif /*CHARSET_EBCDIC */
+  if ((c < f->offset) || (c >= (f->offset + f->nchars)))
+    {
+      return;
+    }
+  fline = (c - f->offset) * f->h * f->w;
+  for (py = y; (py < (y + f->h)); py++)
+    {
+      for (px = x; (px < (x + f->w)); px++)
+	{
+	  if (f->data[fline + cy * f->w + cx])
+	    {
+	      gdImageSetPixel (im, px, py, color);
+	    }
+	  cx++;
+	}
+      cx = 0;
+      cy++;
+    }
+}
 
-  x = 0;
-  y = -h;
-  switchem = a_sq / lsqrt( a_sq + b_sq );
+void
+gdImageCharUp (gdImagePtr im, gdFontPtr f,
+	       int x, int y, int c, int color)
+{
+  int cx, cy;
+  int px, py;
+  int fline;
+  cx = 0;
+  cy = 0;
+#ifdef CHARSET_EBCDIC
+  c = ASC (c);
+#endif /*CHARSET_EBCDIC */
+  if ((c < f->offset) || (c >= (f->offset + f->nchars)))
+    {
+      return;
+    }
+  fline = (c - f->offset) * f->h * f->w;
+  for (py = y; (py > (y - f->w)); py--)
+    {
+      for (px = x; (px < (x + f->h)); px++)
+	{
+	  if (f->data[fline + cy * f->w + cx])
+	    {
+	      gdImageSetPixel (im, px, py, color);
+	    }
+	  cy++;
+	}
+      cy = 0;
+      cx++;
+    }
+}
 
-  while ( x <= switchem )
-  {
-    pstart = y - half;
-    for (pix = pstart; pix < pstart + thick; pix++)
-    {
-      gdImageSetPixel(im, cx+x, cy+pix, color);
-      gdImageSetPixel(im, cx-x, cy+pix, color);
-      gdImageSetPixel(im, cx+x, cy-pix, color);
-      gdImageSetPixel(im, cx-x, cy-pix, color);
-    }
-    if ( d < 0 )
-      d += b_sq_4 * x++ + b_sq_6;
-    else
-      d += b_sq_4*x++ + b_sq_6 + a_sq_4*(++y);
-  }
+void
+gdImageString (gdImagePtr im, gdFontPtr f,
+	       int x, int y, unsigned char *s, int color)
+{
+  int i;
+  int l;
+  l = strlen ((char *) s);
+  for (i = 0; (i < l); i++)
+    {
+      gdImageChar (im, f, x, y, s[i], color);
+      x += f->w;
+    }
+}
 
-  /* Middlesplat!
-  ** Go a little further if the thickness is not nominal...
-  */
-  if ( thick > 1 )
-  {
-    int xp = x;
-    int yp = y;
-    int dp = d;
-    int thick2 = thick + 2;
-    int half2 = half + 1;
-
-    while ( xp <= switchem + half )
-    {
-      pstart = yp - half2;
-      for (pix = pstart; pix < pstart + thick2; pix++)
-      {
-        gdImageSetPixel(im, cx+xp, cy+pix, color);
-        gdImageSetPixel(im, cx-xp, cy+pix, color);
-        gdImageSetPixel(im, cx+xp, cy-pix, color);
-        gdImageSetPixel(im, cx-xp, cy-pix, color);
-      }
-      if ( dp < 0 )
-        dp += b_sq_4 * xp++ + b_sq_6;
-      else
-        dp += b_sq_4*xp++ + b_sq_6 + a_sq_4*(++yp);
+void
+gdImageStringUp (gdImagePtr im, gdFontPtr f,
+		 int x, int y, unsigned char *s, int color)
+{
+  int i;
+  int l;
+  l = strlen ((char *) s);
+  for (i = 0; (i < l); i++)
+    {
+      gdImageCharUp (im, f, x, y, s[i], color);
+      y -= f->w;
     }
-  }
-  
-  d += -2*(long)b_sq + 2*(long)a_sq - 2*(long)b_sq*(x-1) + 2*(long)a_sq*(y-1);
+}
 
-  while ( y <= 0 )
-  {
-    pstart = x - half;
-    for (pix = pstart; pix < pstart + thick; pix++)
-    {
-      gdImageSetPixel(im, cx+pix, cy+y, color);
-      gdImageSetPixel(im, cx-pix, cy+y, color);
-      gdImageSetPixel(im, cx+pix, cy-y, color);
-      gdImageSetPixel(im, cx-pix, cy-y, color);
-    }
-    
-    if (d < 0)
-      d += a_sq_4*y++ + a_sq_6 + b_sq_4*(++x);
-    else
-      d += a_sq_4*y++ + a_sq_6;
-  }
+static int strlen16 (unsigned short *s);
+
+void
+gdImageString16 (gdImagePtr im, gdFontPtr f,
+		 int x, int y, unsigned short *s, int color)
+{
+  int i;
+  int l;
+  l = strlen16 (s);
+  for (i = 0; (i < l); i++)
+    {
+      gdImageChar (im, f, x, y, s[i], color);
+      x += f->w;
+    }
 }
 
+void
+gdImageStringUp16 (gdImagePtr im, gdFontPtr f,
+		   int x, int y, unsigned short *s, int color)
+{
+  int i;
+  int l;
+  l = strlen16 (s);
+  for (i = 0; (i < l); i++)
+    {
+      gdImageCharUp (im, f, x, y, s[i], color);
+      y -= f->w;
+    }
+}
+
+static int
+strlen16 (unsigned short *s)
+{
+  int len = 0;
+  while (*s)
+    {
+      s++;
+      len++;
+    }
+  return len;
+}
+
+#ifndef HAVE_LSQRT
+/* If you don't have a nice square root function for longs, you can use
+   ** this hack
+ */
+long
+lsqrt (long n)
+{
+  long result = (long) sqrt ((double) n);
+  return result;
+}
+#endif
+
 /* s and e are integers modulo 360 (degrees), with 0 degrees
-  being the rightmost extreme and degrees changing clockwise.
-  cx and cy are the center in pixels; w and h are the horizontal 
-  and vertical diameter in pixels. Nice interface, but slow, since
-  I don't yet use Bresenham (I'm using an inefficient but
-  simple solution with too much work going on in it; generalizing
-  Bresenham to ellipses and partial arcs of ellipses is non-trivial,
-  at least for me) and there are other inefficiencies (small circles
-  do far too much work). */
-
-static int lineside ( int x, int y, int dx, int dy, int x0, int y0 )
+   being the rightmost extreme and degrees changing clockwise.
+   cx and cy are the center in pixels; w and h are the horizontal 
+   and vertical diameter in pixels. Nice interface, but slow.
+   See gd_arc_f_buggy.c for a better version that doesn't 
+   seem to be bug-free yet. */
+
+void 
+gdImageArc (gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color)
 {
-  return ( y - y0) * dx - dy * ( x - x0 ) < 0;
+  gdImageFilledArc (im, cx, cy, w, h, s, e, color, gdNoFill);
 }
 
-void gdImageArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color)
+void 
+gdImageFilledArc (gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color, int style)
 {
-	int i;
-	double w2, h2;
-	int xr0, xr1, yr0, yr1; /* starting and ending coordinates */
-	int dx, dy;             /* slope of the resulting line */
-	int end;
-        long d, b_sq, b_sq_4, b_sq_6;
-        long a_sq, a_sq_4, a_sq_6;
-        int x, y, switchem;
-        long lsqrt(long);
-        int pix, half, pstart;
-	int thick = im->thick;
-
-        half = thick/2;
-
-	w2 = w / 2.0;
-	h2 = h / 2.0;
-	while (s > 360) s -= 360;
-	while (s < 0 ) s += 360;
-	while (e < s ) e += 360;
-
-	if ( e - s >= 359 )
-	{ 
-          gdImageEllipse (im, cx, cy, w, h, color);
-	  return;
+  gdPoint pts[3];
+  int i;
+  int lx = 0, ly = 0;
+  int fx, fy;
+  int w2, h2;
+  w2 = w / 2;
+  h2 = h / 2;
+  while (e < s)
+    {
+      e += 360;
+    }
+  for (i = s; (i <= e); i++)
+    {
+      int x, y;
+      x = ((long) gdCosT[i % 360] * (long) w2 / 1024) + cx;
+      y = ((long) gdSinT[i % 360] * (long) h2 / 1024) + cy;
+      if (i != s)
+	{
+	  if (!(style & gdChord))
+	    {
+	      if (style & gdNoFill)
+		{
+		  gdImageLine (im, lx, ly, x, y, color);
+		}
+	      else
+		{
+		  /* This is expensive! */
+		  pts[0].x = lx;
+		  pts[0].y = ly;
+		  pts[1].x = x;
+		  pts[1].y = y;
+		  pts[2].x = cx;
+		  pts[2].y = cy;
+		  gdImageFilledPolygon (im, pts, 3, color);
+		}
+	    }
+	}
+      else
+	{
+	  fx = x;
+	  fy = y;
+	}
+      lx = x;
+      ly = y;
+    }
+  if (style & gdChord)
+    {
+      if (style & gdNoFill)
+	{
+	  if (style & gdEdged)
+	    {
+	      gdImageLine (im, cx, cy, lx, ly, color);
+	      gdImageLine (im, cx, cy, fx, fy, color);
+	    }
+	  gdImageLine (im, fx, fy, lx, ly, color);
+	}
+      else
+	{
+	  pts[0].x = fx;
+	  pts[0].y = fy;
+	  pts[1].x = lx;
+	  pts[1].y = ly;
+	  pts[2].x = cx;
+	  pts[2].y = cy;
+	  gdImageFilledPolygon (im, pts, 3, color);
+	}
+    }
+  else
+    {
+      if (style & gdNoFill)
+	{
+	  if (style & gdEdged)
+	    {
+	      gdImageLine (im, cx, cy, lx, ly, color);
+	      gdImageLine (im, cx, cy, fx, fy, color);
+	    }
 	}
+    }
+}
 
-	xr0 = xr1 = cx;
-	yr0 = yr1 = cy;
-	xr0 += w2 * cos ( (s * 2.0 * 3.14159265) / 360.0 );
-	yr0 += h2 * sin ( (s * 2.0 * 3.14159265) / 360.0 );
-	xr1 += w2 * cos ( (e * 2.0 * 3.14159265 ) / 360.0 );
-	yr1 += h2 * sin ( (e * 2.0 * 3.14159265 ) / 360.0 );
-        dx = xr1 - xr0;
-        dy = yr1 - yr0;
-
-	/* From here it should be just like the ellipse, but qualified to be on 
-	** ONE side of the chord from xr0,yr0 to xr1, yr1 (positive)
-	*/
-        d = 2 * (long) h2 * h2 + (long) w2 * w2 - 2 * (long)w2 * w2 * h2;
-        b_sq = (long)h2 * h2;
-        b_sq_4 = 4 * (long) h2 * h2;
-        b_sq_6 = 6 * (long) h2 * h2;
-        a_sq = (long) w2 * w2;
-        a_sq_4 = 4 * (long)w2 * w2;
-        a_sq_6 = 6 * (long)w2 * w2;
-
-        x = 0;
-        y = -h2;
-        switchem = a_sq / lsqrt( a_sq + b_sq );
-
-        while ( x <= switchem )
-        {
-          pstart = y - half;
-          for (pix = pstart; pix < pstart + thick; pix++)
-          {
-            int xx, yy;
-
-            xx = cx+x; yy = cy+pix;
-            if ( lineside ( xx, yy, dx, dy, xr0, yr0 ) )
-              gdImageSetPixel(im, xx, yy, color);
-
-            xx = cx - x;
-            if ( lineside ( xx, yy, dx, dy, xr0, yr0 ) )
-              gdImageSetPixel(im, xx, yy, color);
-
-            xx = cx+x; yy = cy-pix;
-            if ( lineside ( xx, yy, dx, dy, xr0, yr0 ) )
-              gdImageSetPixel(im, xx, yy, color);
-
-            xx = cx-x;
-            if ( lineside ( xx, yy, dx, dy, xr0, yr0 ) )
-              gdImageSetPixel(im, xx, yy, color);
-          }
-          if ( d < 0 )
-            d += b_sq_4 * x++ + b_sq_6;
-          else
-            d += b_sq_4*x++ + b_sq_6 + a_sq_4*(++y);
-        }
-
-        /* Middlesplat!
-        ** Go a little further if the thickness is not nominal...
-        */
-        if ( thick > 1 )
-        {
-          int xp = x;
-          int yp = y;
-          int dp = d;
-          int thick2 = thick + 2;
-          int half2 = half + 1;
-      
-          while ( xp <= switchem + half )
-          {
-            pstart = yp - half2;
-            for (pix = pstart; pix < pstart + thick2; pix++)
-            {
-              int xx, yy;
-
-              xx = cx+xp; yy = cy+pix;
-              if ( lineside ( xx, yy, dx, dy, xr0, yr0 ) )
-                gdImageSetPixel(im, xx, yy, color);
-
-              xx = cx - xp;
-              if ( lineside ( xx, yy, dx, dy, xr0, yr0 ) )
-                gdImageSetPixel(im, xx, yy, color);
-
-              xx = cx+xp; yy = cy-pix;
-              if ( lineside ( xx, yy, dx, dy, xr0, yr0 ) )
-                gdImageSetPixel(im, xx, yy, color);
-
-              xx = cx-xp;
-              if ( lineside ( xx, yy, dx, dy, xr0, yr0 ) )
-                gdImageSetPixel(im, xx, yy, color);
-            }
-            if ( dp < 0 )
-              dp += b_sq_4 * xp++ + b_sq_6;
-            else
-              dp += b_sq_4*xp++ + b_sq_6 + a_sq_4*(++yp);
-          }
-        }
-