Description: Make CUPS Raster output device work in multi-threaded mode
Origin: http://svn.ghostscript.com/viewvc?view=rev&revision=11135
Author: James Cloos
Bug: http://bugs.ghostscript.com/show_bug.cgi?id=691241
Forwarded: yes
Last-Update: 2010-08-19
--- a/cups/gdevcups.c
+++ b/cups/gdevcups.c
@@ -196,6 +196,28 @@
   cups_raster_t		*stream;	/* Raster stream */
   cups_page_header_t	header;		/* PostScript page device info */
   int			landscape;	/* Non-zero if this is landscape */
+  int			width_old;	/* Previous width */
+  int			height_old;	/* Previous height */
+  int			colorspace_old;	/* Previous color space */
+  int			bitspercolor_old;/* Previous bits per color */
+  int			lastpage;
+  int			HaveProfile;	/* Has a color profile been defined? */
+  char			*Profile;	/* Current simple color profile string */
+  ppd_file_t		*PPD;		/* PPD file for this device */
+  unsigned char		RevLower1[16];	/* Lower 1-bit reversal table */
+  unsigned char 	RevUpper1[16];	/* Upper 1-bit reversal table */
+  unsigned char		RevLower2[16];	/* Lower 2-bit reversal table */
+  unsigned char		RevUpper2[16];	/* Upper 2-bit reversal table */
+#ifdef GX_COLOR_INDEX_TYPE
+  gx_color_value	DecodeLUT[65536];/* Output color to RGB value LUT */
+#else
+  gx_color_value	DecodeLUT[256];	/* Output color to RGB value LUT */
+#endif /* GX_COLOR_INDEX_TYPE */
+  unsigned short	EncodeLUT[gx_max_color_value + 1];/* RGB value to output color LUT */
+  int			Density[CUPS_MAX_VALUE + 1];/* Density LUT */
+  int			Matrix[3][3][CUPS_MAX_VALUE + 1];/* Color transform matrix LUT */
+
+  /* Used by cups_put_params(): */
 } gx_device_cups;
 
 private gx_device_procs	cups_procs =
@@ -375,54 +397,30 @@
     ""                                  /* cupsPageSizeName */
 #endif /* CUPS_RASTER_SYNCv1 */
   },
-  0                                     /* landscape */
+  0,                                    /* landscape */
+  0,                                    /* width_old */
+  0,                                    /* height_old */
+  0,                                    /* colorspace_old */
+  0,                                    /* bitspercolor_old */
+  0,                                    /* lastpage */
+  0,                                    /* HaveProfile */
+  NULL,                                 /* Profile */
+  NULL,                                 /* PPD */
+  { 0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
+    0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f },/* RevLower1 */
+  { 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
+    0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0 },/* RevUpper1 */
+  { 0x00, 0x04, 0x08, 0x0c, 0x01, 0x05, 0x09, 0x0d,
+    0x02, 0x06, 0x0a, 0x0e, 0x03, 0x07, 0x0b, 0x0f },/* RevLower2 */
+  { 0x00, 0x40, 0x80, 0xc0, 0x10, 0x50, 0x90, 0xd0,
+    0x20, 0x60, 0xa0, 0xe0, 0x30, 0x70, 0xb0, 0xf0 },/* RevUpper2 */
+  { },                                  /* DecodeLUT */
+  { },                                  /* EncodeLUT */
+  { },                                  /* Density */
+  { }                                   /* Matrix */
 };
 
 /*
- * Globals...
- */
-
-#ifdef GX_COLOR_INDEX_TYPE
-static gx_color_value	cupsDecodeLUT[65536];
-					/* Output color to RGB value LUT */
-#else
-static gx_color_value	cupsDecodeLUT[256];
-					/* Output color to RGB value LUT */
-#endif /* GX_COLOR_INDEX_TYPE */
-static unsigned short	cupsEncodeLUT[gx_max_color_value + 1];
-					/* RGB value to output color LUT */
-
-static ppd_file_t	*cupsPPD = 0;	/* PPD file for this device */
-static char		*cupsProfile = NULL;
-					/* Current simple color profile string */
-static int		cupsHaveProfile = 0;
-					/* Has a color profile been defined? */
-static int		cupsMatrix[3][3][CUPS_MAX_VALUE + 1];
-					/* Color transform matrix LUT */
-static int		cupsDensity[CUPS_MAX_VALUE + 1];
-					/* Density LUT */
-static unsigned char	cupsRevLower1[16] =
-			{		/* Lower 1-bit reversal table */
-			  0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
-			  0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f
-			},
-			cupsRevUpper1[16] =
-			{		/* Upper 1-bit reversal table */
-			  0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
-			  0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0
-			},
-			cupsRevLower2[16] =
-			{		/* Lower 2-bit reversal table */
-			  0x00, 0x04, 0x08, 0x0c, 0x01, 0x05, 0x09, 0x0d,
-			  0x02, 0x06, 0x0a, 0x0e, 0x03, 0x07, 0x0b, 0x0f
-			},
-			cupsRevUpper2[16] =
-			{		/* Upper 2-bit reversal table */
-			  0x00, 0x40, 0x80, 0xc0, 0x10, 0x50, 0x90, 0xd0,
-			  0x20, 0x60, 0xa0, 0xe0, 0x30, 0x70, 0xb0, 0xf0
-			};
-
-/*
  * Local functions...
  */
 
@@ -453,16 +451,16 @@
   }
 
 #if 0 /* Can't do this here because put_params() might close the device */
-  if (cupsPPD != NULL)
+  if (cups->PPD != NULL)
   {
-    ppdClose(cupsPPD);
-    cupsPPD = NULL;
+    ppdClose(cups->PPD);
+    cups->PPD = NULL;
   }
 
-  if (cupsProfile != NULL)
+  if (cups->Profile != NULL)
   {
-    free(cupsProfile);
-    cupsProfile = NULL;
+    free(cups->Profile);
+    cups->Profile = NULL;
   }
 #endif /* 0 */
 
@@ -503,9 +501,9 @@
     mask  = (1 << shift) - 1;
 
     for (i = cups->color_info.num_components - 1; i > 0; i --, ci >>= shift)
-      cv[i] = cupsDecodeLUT[ci & mask];
+      cv[i] = cups->DecodeLUT[ci & mask];
 
-    cv[0] = cupsDecodeLUT[ci & mask];
+    cv[0] = cups->DecodeLUT[ci & mask];
   }
 
   return (0);
@@ -533,10 +531,10 @@
 
   shift = cups->header.cupsBitsPerColor;
 
-  for (ci = cupsEncodeLUT[cv[0]], i = 1;
+  for (ci = cups->EncodeLUT[cv[0]], i = 1;
        i < cups->color_info.num_components;
        i ++)
-    ci = (ci << shift) | cupsEncodeLUT[cv[i]];
+    ci = (ci << shift) | cups->EncodeLUT[cv[i]];
 
 #ifdef DEBUG
   dprintf2("DEBUG2: cv[0]=%d -> %llx\n", cv[0], ci);
@@ -1135,7 +1133,7 @@
 	  c0 = 0;
 	else if (c0 > frac_1)
 	  c0 = frac_1;
-	out[0] = frac_1 - (frac)cupsDensity[c0];
+	out[0] = frac_1 - (frac)cups->Density[c0];
         break;
 
     case CUPS_CSPACE_RGBA :
@@ -1158,26 +1156,26 @@
 	  c0 = 0;
 	else if (c0 > frac_1)
 	  c0 = frac_1;
-	out[0] = frac_1 - (frac)cupsDensity[c0];
+	out[0] = frac_1 - (frac)cups->Density[c0];
 
         if (c1 < 0)
 	  c1 = 0;
 	else if (c1 > frac_1)
 	  c1 = frac_1;
-	out[1] = frac_1 - (frac)cupsDensity[c1];
+	out[1] = frac_1 - (frac)cups->Density[c1];
 
         if (c2 < 0)
 	  c2 = 0;
 	else if (c2 > frac_1)
 	  c2 = frac_1;
-	out[2] = frac_1 - (frac)cupsDensity[c2];
+	out[2] = frac_1 - (frac)cups->Density[c2];
 
         if (cups->header.cupsColorSpace == CUPS_CSPACE_RGBW) {
 	  if (c3 < 0)
 	    c3 = 0;
 	  else if (c3 > frac_1)
 	    c3 = frac_1;
-	  out[3] = frac_1 - (frac)cupsDensity[c3];
+	  out[3] = frac_1 - (frac)cups->Density[c3];
 	}
         break;
 
@@ -1188,9 +1186,9 @@
 	if (c0 < 0)
 	  out[0] = 0;
 	else if (c0 > frac_1)
-	  out[0] = (frac)cupsDensity[frac_1];
+	  out[0] = (frac)cups->Density[frac_1];
 	else
-	  out[0] = (frac)cupsDensity[c0];
+	  out[0] = (frac)cups->Density[c0];
         break;
 
     case CUPS_CSPACE_CMY :
@@ -1201,23 +1199,23 @@
         if (c0 < 0)
 	  out[0] = 0;
 	else if (c0 > frac_1)
-	  out[0] = (frac)cupsDensity[frac_1];
+	  out[0] = (frac)cups->Density[frac_1];
 	else
-	  out[0] = (frac)cupsDensity[c0];
+	  out[0] = (frac)cups->Density[c0];
 
         if (c1 < 0)
 	  out[1] = 0;
 	else if (c1 > frac_1)
-	  out[1] = (frac)cupsDensity[frac_1];
+	  out[1] = (frac)cups->Density[frac_1];
 	else
-	  out[1] = (frac)cupsDensity[c1];
+	  out[1] = (frac)cups->Density[c1];
 
         if (c2 < 0)
 	  out[2] = 0;
 	else if (c2 > frac_1)
-	  out[2] = (frac)cupsDensity[frac_1];
+	  out[2] = (frac)cups->Density[frac_1];
 	else
-	  out[2] = (frac)cupsDensity[c2];
+	  out[2] = (frac)cups->Density[c2];
         break;
 
     case CUPS_CSPACE_YMC :
@@ -1228,53 +1226,53 @@
         if (c0 < 0)
 	  out[0] = 0;
 	else if (c0 > frac_1)
-	  out[0] = (frac)cupsDensity[frac_1];
+	  out[0] = (frac)cups->Density[frac_1];
 	else
-	  out[0] = (frac)cupsDensity[c0];
+	  out[0] = (frac)cups->Density[c0];
 
         if (c1 < 0)
 	  out[1] = 0;
 	else if (c1 > frac_1)
-	  out[1] = (frac)cupsDensity[frac_1];
+	  out[1] = (frac)cups->Density[frac_1];
 	else
-	  out[1] = (frac)cupsDensity[c1];
+	  out[1] = (frac)cups->Density[c1];
 
         if (c2 < 0)
 	  out[2] = 0;
 	else if (c2 > frac_1)
-	  out[2] = (frac)cupsDensity[frac_1];
+	  out[2] = (frac)cups->Density[frac_1];
 	else
-	  out[2] = (frac)cupsDensity[c2];
+	  out[2] = (frac)cups->Density[c2];
         break;
 
     case CUPS_CSPACE_CMYK :
         if (c < 0)
 	  out[0] = 0;
 	else if (c > frac_1)
-	  out[0] = (frac)cupsDensity[frac_1];
+	  out[0] = (frac)cups->Density[frac_1];
 	else
-	  out[0] = (frac)cupsDensity[c];
+	  out[0] = (frac)cups->Density[c];
 
         if (m < 0)
 	  out[1] = 0;
 	else if (m > frac_1)
-	  out[1] = (frac)cupsDensity[frac_1];
+	  out[1] = (frac)cups->Density[frac_1];
 	else
-	  out[1] = (frac)cupsDensity[m];
+	  out[1] = (frac)cups->Density[m];
 
         if (y < 0)
 	  out[2] = 0;
 	else if (y > frac_1)
-	  out[2] = (frac)cupsDensity[frac_1];
+	  out[2] = (frac)cups->Density[frac_1];
 	else
-	  out[2] = (frac)cupsDensity[y];
+	  out[2] = (frac)cups->Density[y];
 
         if (k < 0)
 	  out[3] = 0;
 	else if (k > frac_1)
-	  out[3] = (frac)cupsDensity[frac_1];
+	  out[3] = (frac)cups->Density[frac_1];
 	else
-	  out[3] = (frac)cupsDensity[k];
+	  out[3] = (frac)cups->Density[k];
         break;
 
     case CUPS_CSPACE_YMCK :
@@ -1283,30 +1281,30 @@
         if (y < 0)
 	  out[0] = 0;
 	else if (y > frac_1)
-	  out[0] = (frac)cupsDensity[frac_1];
+	  out[0] = (frac)cups->Density[frac_1];
 	else
-	  out[0] = (frac)cupsDensity[y];
+	  out[0] = (frac)cups->Density[y];
 
         if (m < 0)
 	  out[1] = 0;
 	else if (m > frac_1)
-	  out[1] = (frac)cupsDensity[frac_1];
+	  out[1] = (frac)cups->Density[frac_1];
 	else
-	  out[1] = (frac)cupsDensity[m];
+	  out[1] = (frac)cups->Density[m];
 
         if (c < 0)
 	  out[2] = 0;
 	else if (c > frac_1)
-	  out[2] = (frac)cupsDensity[frac_1];
+	  out[2] = (frac)cups->Density[frac_1];
 	else
-	  out[2] = (frac)cupsDensity[c];
+	  out[2] = (frac)cups->Density[c];
 
         if (k < 0)
 	  out[3] = 0;
 	else if (k > frac_1)
-	  out[3] = (frac)cupsDensity[frac_1];
+	  out[3] = (frac)cups->Density[frac_1];
 	else
-	  out[3] = (frac)cupsDensity[k];
+	  out[3] = (frac)cups->Density[k];
         break;
 
     case CUPS_CSPACE_KCMYcm :
@@ -1314,30 +1312,30 @@
         if (k < 0)
 	  out[0] = 0;
 	else if (k > frac_1)
-	  out[0] = (frac)cupsDensity[frac_1];
+	  out[0] = (frac)cups->Density[frac_1];
 	else
-	  out[0] = (frac)cupsDensity[k];
+	  out[0] = (frac)cups->Density[k];
 
         if (c < 0)
 	  out[1] = 0;
 	else if (c > frac_1)
-	  out[1] = (frac)cupsDensity[frac_1];
+	  out[1] = (frac)cups->Density[frac_1];
 	else
-	  out[1] = (frac)cupsDensity[c];
+	  out[1] = (frac)cups->Density[c];
 
         if (m < 0)
 	  out[2] = 0;
 	else if (m > frac_1)
-	  out[2] = (frac)cupsDensity[frac_1];
+	  out[2] = (frac)cups->Density[frac_1];
 	else
-	  out[2] = (frac)cupsDensity[m];
+	  out[2] = (frac)cups->Density[m];
 
         if (y < 0)
 	  out[3] = 0;
 	else if (y > frac_1)
-	  out[3] = (frac)cupsDensity[frac_1];
+	  out[3] = (frac)cups->Density[frac_1];
 	else
-	  out[3] = (frac)cupsDensity[y];
+	  out[3] = (frac)cups->Density[y];
         break;
 
 #  ifdef CUPS_RASTER_HAVE_COLORIMETRIC
@@ -1532,9 +1530,9 @@
 	  }
 	}
 
-        out[0] = cupsDecodeLUT[c0];
-        out[1] = cupsDecodeLUT[c1];
-        out[2] = cupsDecodeLUT[c2];
+        out[0] = cups->DecodeLUT[c0];
+        out[1] = cups->DecodeLUT[c1];
+        out[2] = cups->DecodeLUT[c2];
         break;
 #  endif /* CUPS_RASTER_HAVE_COLORIMETRIC */
   }
@@ -1626,21 +1624,21 @@
   * Do color correction as needed...
   */
 
-  if (cupsHaveProfile)
+  if (cups->HaveProfile)
   {
    /*
     * Color correct CMY...
     */
 
-    tc = cupsMatrix[0][0][c] +
-         cupsMatrix[0][1][m] +
-	 cupsMatrix[0][2][y];
-    tm = cupsMatrix[1][0][c] +
-         cupsMatrix[1][1][m] +
-	 cupsMatrix[1][2][y];
-    ty = cupsMatrix[2][0][c] +
-         cupsMatrix[2][1][m] +
-	 cupsMatrix[2][2][y];
+    tc = cups->Matrix[0][0][c] +
+         cups->Matrix[0][1][m] +
+	 cups->Matrix[0][2][y];
+    tm = cups->Matrix[1][0][c] +
+         cups->Matrix[1][1][m] +
+	 cups->Matrix[1][2][y];
+    ty = cups->Matrix[2][0][c] +
+         cups->Matrix[2][1][m] +
+	 cups->Matrix[2][2][y];
 
     if (tc < 0)
       c = 0;
@@ -1709,18 +1707,18 @@
   * Density correct...
   */
 
-  if (cupsHaveProfile)
+  if (cups->HaveProfile)
   {
-    c = cupsDensity[c];
-    m = cupsDensity[m];
-    y = cupsDensity[y];
-    k = cupsDensity[k];
+    c = cups->Density[c];
+    m = cups->Density[m];
+    y = cups->Density[y];
+    k = cups->Density[k];
   }
 
-  ic = cupsEncodeLUT[c];
-  im = cupsEncodeLUT[m];
-  iy = cupsEncodeLUT[y];
-  ik = cupsEncodeLUT[k];
+  ic = cups->EncodeLUT[c];
+  im = cups->EncodeLUT[m];
+  iy = cups->EncodeLUT[y];
+  ik = cups->EncodeLUT[k];
 
  /*
   * Convert the CMYK color to a color index...
@@ -1936,42 +1934,42 @@
     case CUPS_CSPACE_SILVER :
         prgb[0] =
         prgb[1] =
-        prgb[2] = cupsDecodeLUT[c3];
+        prgb[2] = cups->DecodeLUT[c3];
         break;
 
     case CUPS_CSPACE_W :
         prgb[0] =
         prgb[1] =
-        prgb[2] = cupsDecodeLUT[c3];
+        prgb[2] = cups->DecodeLUT[c3];
         break;
 
     case CUPS_CSPACE_RGB :
-        prgb[0] = cupsDecodeLUT[c1];
-        prgb[1] = cupsDecodeLUT[c2];
-        prgb[2] = cupsDecodeLUT[c3];
+        prgb[0] = cups->DecodeLUT[c1];
+        prgb[1] = cups->DecodeLUT[c2];
+        prgb[2] = cups->DecodeLUT[c3];
         break;
 
     case CUPS_CSPACE_RGBA :
-        prgb[0] = cupsDecodeLUT[c0];
-        prgb[1] = cupsDecodeLUT[c1];
-        prgb[2] = cupsDecodeLUT[c2];
+        prgb[0] = cups->DecodeLUT[c0];
+        prgb[1] = cups->DecodeLUT[c1];
+        prgb[2] = cups->DecodeLUT[c2];
         break;
 
     case CUPS_CSPACE_CMY :
-        prgb[0] = cupsDecodeLUT[c1];
-        prgb[1] = cupsDecodeLUT[c2];
-        prgb[2] = cupsDecodeLUT[c3];
+        prgb[0] = cups->DecodeLUT[c1];
+        prgb[1] = cups->DecodeLUT[c2];
+        prgb[2] = cups->DecodeLUT[c3];
         break;
 
     case CUPS_CSPACE_YMC :
-        prgb[0] = cupsDecodeLUT[c3];
-        prgb[1] = cupsDecodeLUT[c2];
-        prgb[2] = cupsDecodeLUT[c1];
+        prgb[0] = cups->DecodeLUT[c3];
+        prgb[1] = cups->DecodeLUT[c2];
+        prgb[2] = cups->DecodeLUT[c1];
         break;
 
     case CUPS_CSPACE_KCMY :
     case CUPS_CSPACE_KCMYcm :
-        k    = cupsDecodeLUT[c0];
+        k    = cups->DecodeLUT[c0];
         divk = gx_max_color_value - k;
         if (divk == 0)
         {
@@ -1992,13 +1990,13 @@
 
     case CUPS_CSPACE_RGBW :
        /*
-        * cupsDecodeLUT actually maps to RGBW, not CMYK...
+        * cups->DecodeLUT actually maps to RGBW, not CMYK...
 	*/
 
-        k = cupsDecodeLUT[c3];
-        c = cupsDecodeLUT[c0] + k;
-        m = cupsDecodeLUT[c1] + k;
-        y = cupsDecodeLUT[c2] + k;
+        k = cups->DecodeLUT[c3];
+        c = cups->DecodeLUT[c0] + k;
+        m = cups->DecodeLUT[c1] + k;
+        y = cups->DecodeLUT[c2] + k;
 
         if (c > gx_max_color_value)
 	  prgb[0] = gx_max_color_value;
@@ -2017,7 +2015,7 @@
         break;
 
     case CUPS_CSPACE_CMYK :
-        k    = cupsDecodeLUT[c3];
+        k    = cups->DecodeLUT[c3];
         divk = gx_max_color_value - k;
         if (divk == 0)
         {
@@ -2039,7 +2037,7 @@
     case CUPS_CSPACE_YMCK :
     case CUPS_CSPACE_GMCK :
     case CUPS_CSPACE_GMCS :
-        k    = cupsDecodeLUT[c3];
+        k    = cups->DecodeLUT[c3];
         divk = gx_max_color_value - k;
         if (divk == 0)
         {
@@ -2127,7 +2125,7 @@
   * Do color correction as needed...
   */
 
-  if (cupsHaveProfile)
+  if (cups->HaveProfile)
   {
    /*
     * Compute CMYK values...
@@ -2149,17 +2147,17 @@
     * Color correct CMY...
     */
 
-    tc = cupsMatrix[0][0][ic] +
-         cupsMatrix[0][1][im] +
-	 cupsMatrix[0][2][iy] +
+    tc = cups->Matrix[0][0][ic] +
+         cups->Matrix[0][1][im] +
+	 cups->Matrix[0][2][iy] +
 	 ik;
-    tm = cupsMatrix[1][0][ic] +
-         cupsMatrix[1][1][im] +
-	 cupsMatrix[1][2][iy] +
+    tm = cups->Matrix[1][0][ic] +
+         cups->Matrix[1][1][im] +
+	 cups->Matrix[1][2][iy] +
 	 ik;
-    ty = cupsMatrix[2][0][ic] +
-         cupsMatrix[2][1][im] +
-	 cupsMatrix[2][2][iy] +
+    ty = cups->Matrix[2][0][ic] +
+         cups->Matrix[2][1][im] +
+	 cups->Matrix[2][2][iy] +
 	 ik;
 
    /*
@@ -2169,23 +2167,23 @@
     if (tc < 0)
       r = gx_max_color_value;
     else if (tc > gx_max_color_value)
-      r = gx_max_color_value - cupsDensity[gx_max_color_value];
+      r = gx_max_color_value - cups->Density[gx_max_color_value];
     else
-      r = gx_max_color_value - cupsDensity[tc];
+      r = gx_max_color_value - cups->Density[tc];
 
     if (tm < 0)
       g = gx_max_color_value;
     else if (tm > gx_max_color_value)
-      g = gx_max_color_value - cupsDensity[gx_max_color_value];
+      g = gx_max_color_value - cups->Density[gx_max_color_value];
     else
-      g = gx_max_color_value - cupsDensity[tm];
+      g = gx_max_color_value - cups->Density[tm];
 
     if (ty < 0)
       b = gx_max_color_value;
     else if (ty > gx_max_color_value)
-      b = gx_max_color_value - cupsDensity[gx_max_color_value];
+      b = gx_max_color_value - cups->Density[gx_max_color_value];
     else
-      b = gx_max_color_value - cupsDensity[ty];
+      b = gx_max_color_value - cups->Density[ty];
   }
 
  /*
@@ -2195,13 +2193,13 @@
   switch (cups->header.cupsColorSpace)
   {
     case CUPS_CSPACE_W :
-        i = cupsEncodeLUT[(r * 31 + g * 61 + b * 8) / 100];
+        i = cups->EncodeLUT[(r * 31 + g * 61 + b * 8) / 100];
         break;
 
     case CUPS_CSPACE_RGB :
-        ic = cupsEncodeLUT[r];
-        im = cupsEncodeLUT[g];
-        iy = cupsEncodeLUT[b];
+        ic = cups->EncodeLUT[r];
+        im = cups->EncodeLUT[g];
+        iy = cups->EncodeLUT[b];
 
         switch (cups->header.cupsBitsPerColor)
         {
@@ -2256,9 +2254,9 @@
 	}
 
     case CUPS_CSPACE_RGBA :
-        ic = cupsEncodeLUT[r];
-        im = cupsEncodeLUT[g];
-        iy = cupsEncodeLUT[b];
+        ic = cups->EncodeLUT[r];
+        im = cups->EncodeLUT[g];
+        iy = cups->EncodeLUT[b];
 
         switch (cups->header.cupsBitsPerColor)
         {
@@ -2283,13 +2281,13 @@
         break;
 
     default :
-        i = cupsEncodeLUT[gx_max_color_value - (r * 31 + g * 61 + b * 8) / 100];
+        i = cups->EncodeLUT[gx_max_color_value - (r * 31 + g * 61 + b * 8) / 100];
         break;
 
     case CUPS_CSPACE_CMY :
-        ic = cupsEncodeLUT[gx_max_color_value - r];
-        im = cupsEncodeLUT[gx_max_color_value - g];
-        iy = cupsEncodeLUT[gx_max_color_value - b];
+        ic = cups->EncodeLUT[gx_max_color_value - r];
+        im = cups->EncodeLUT[gx_max_color_value - g];
+        iy = cups->EncodeLUT[gx_max_color_value - b];
 
         switch (cups->header.cupsBitsPerColor)
         {
@@ -2314,9 +2312,9 @@
         break;
 
     case CUPS_CSPACE_YMC :
-        ic = cupsEncodeLUT[gx_max_color_value - r];
-        im = cupsEncodeLUT[gx_max_color_value - g];
-        iy = cupsEncodeLUT[gx_max_color_value - b];
+        ic = cups->EncodeLUT[gx_max_color_value - r];
+        im = cups->EncodeLUT[gx_max_color_value - g];
+        iy = cups->EncodeLUT[gx_max_color_value - b];
 
         switch (cups->header.cupsBitsPerColor)
         {
@@ -2345,10 +2343,10 @@
 	  ik = (int)((float)ik * (float)ik * (float)ik /
 	             ((float)mk * (float)mk));
 
-        ic = cupsEncodeLUT[ic - ik];
-        im = cupsEncodeLUT[im - ik];
-        iy = cupsEncodeLUT[iy - ik];
-        ik = cupsEncodeLUT[ik];
+        ic = cups->EncodeLUT[ic - ik];
+        im = cups->EncodeLUT[im - ik];
+        iy = cups->EncodeLUT[iy - ik];
+        ik = cups->EncodeLUT[ik];
 
         switch (cups->header.cupsBitsPerColor)
         {
@@ -2389,10 +2387,10 @@
 	  ik = (int)((float)ik * (float)ik * (float)ik /
 	             ((float)mk * (float)mk));
 
-        ic = cupsEncodeLUT[ic - ik];
-        im = cupsEncodeLUT[im - ik];
-        iy = cupsEncodeLUT[iy - ik];
-        ik = cupsEncodeLUT[ik];
+        ic = cups->EncodeLUT[ic - ik];
+        im = cups->EncodeLUT[im - ik];
+        iy = cups->EncodeLUT[iy - ik];
+        ik = cups->EncodeLUT[ik];
 
         switch (cups->header.cupsBitsPerColor)
         {
@@ -2428,10 +2426,10 @@
 	    ik = (int)((float)ik * (float)ik * (float)ik /
 	               ((float)mk * (float)mk));
 
-          ic = cupsEncodeLUT[ic - ik];
-          im = cupsEncodeLUT[im - ik];
-          iy = cupsEncodeLUT[iy - ik];
-          ik = cupsEncodeLUT[ik];
+          ic = cups->EncodeLUT[ic - ik];
+          im = cups->EncodeLUT[im - ik];
+          iy = cups->EncodeLUT[iy - ik];
+          ik = cups->EncodeLUT[ik];
 	  if (ik)
 	    i = 32;
 	  else if (ic && im)
@@ -2461,10 +2459,10 @@
 	  ik = (int)((float)ik * (float)ik * (float)ik /
 	             ((float)mk * (float)mk));
 
-        ic = cupsEncodeLUT[ic - ik];
-        im = cupsEncodeLUT[im - ik];
-        iy = cupsEncodeLUT[iy - ik];
-        ik = cupsEncodeLUT[ik];
+        ic = cups->EncodeLUT[ic - ik];
+        im = cups->EncodeLUT[im - ik];
+        iy = cups->EncodeLUT[iy - ik];
+        ik = cups->EncodeLUT[ik];
 
         switch (cups->header.cupsBitsPerColor)
         {
@@ -2664,8 +2662,8 @@
   if ((code = gdev_prn_open(pdev)) != 0)
     return (code);
 
-  if (cupsPPD == NULL)
-    cupsPPD = ppdOpenFile(getenv("PPD"));
+  if (cups->PPD == NULL)
+    cups->PPD = ppdOpenFile(getenv("PPD"));
 
   return (0);
 }
@@ -2772,7 +2770,7 @@
   if (num_copies < 1)
     num_copies = 1;
 
-  if (cupsPPD != NULL && !cupsPPD->manual_copies)
+  if (cups->PPD != NULL && !cups->PPD->manual_copies)
   {
     cups->header.NumCopies = num_copies;
     num_copies = 1;
@@ -2851,17 +2849,12 @@
                         height,		/* New height of page */
                         colorspace,     /* New color space */
                         bitspercolor;   /* New bits per color */
-  static int            width_old = 0,  /* Previous width */
-                        height_old = 0, /* Previous height */
-                        colorspace_old = 0,/* Previous color space */
-                        bitspercolor_old = 0;/* Previous bits per color */
   ppd_attr_t            *backside = NULL,
                         *backsiderequiresflippedmargins = NULL;
   float                 swap;
   int                   xflip = 0,
                         yflip = 0;
   int                   found = 0;
-  static int            lastpage = 0;
 
   dprintf2("DEBUG2: cups_put_params(%p, %p)\n", pdev, plist);
 
@@ -2965,9 +2958,9 @@
      page without necessarily having a page size change in the PostScript
      code, as for some printers margins have to be flipped on the back sides of
      the sheets (even pages) when printing duplex */
-  if (cups->page != lastpage) {
+  if (cups->page != cups->lastpage) {
     size_set = 1;
-    lastpage = cups->page;
+    cups->lastpage = cups->page;
   }
 
   stringoption(MediaClass, "MediaClass")
@@ -3041,17 +3034,17 @@
   stringoption(cupsPageSizeName, "cupsPageSizeName");
 #endif /* CUPS_RASTER_SYNCv1 */
 
-  if ((code = param_read_string(plist, "cupsProfile", &stringval)) < 0)
+  if ((code = param_read_string(plist, "cups->Profile", &stringval)) < 0)
   {
-    param_signal_error(plist, "cupsProfile", code);
+    param_signal_error(plist, "cups->Profile", code);
     return (code);
   }
   else if (code == 0)
   {
-    if (cupsProfile != NULL)
-      free(cupsProfile);
+    if (cups->Profile != NULL)
+      free(cups->Profile);
 
-    cupsProfile = strdup((char *)stringval.data);
+    cups->Profile = strdup((char *)stringval.data);
   }
 
   cups_set_color_info(pdev);
@@ -3080,22 +3073,22 @@
 
     cups->landscape = 0;
 
-    if (cupsPPD != NULL)
+    if (cups->PPD != NULL)
     {
       dprintf1("DEBUG2: cups->header.Duplex = %d\n", cups->header.Duplex);
       dprintf1("DEBUG2: cups->header.Tumble = %d\n", cups->header.Tumble);
       dprintf1("DEBUG2: cups->page = %d\n", cups->page);
-      dprintf1("DEBUG2: cupsPPD = %p\n", cupsPPD);
+      dprintf1("DEBUG2: cups->PPD = %p\n", cups->PPD);
 
-      backside = ppdFindAttr(cupsPPD, "cupsBackSide", NULL); 
+      backside = ppdFindAttr(cups->PPD, "cupsBackSide", NULL); 
       if (backside) {
 	dprintf1("DEBUG2: cupsBackSide = %s\n", backside->value);
-	cupsPPD->flip_duplex = 0;
+	cups->PPD->flip_duplex = 0;
       }
-      dprintf1("DEBUG2: cupsPPD->flip_duplex = %d\n", cupsPPD->flip_duplex);
+      dprintf1("DEBUG2: cups->PPD->flip_duplex = %d\n", cups->PPD->flip_duplex);
 
       backsiderequiresflippedmargins =
-	ppdFindAttr(cupsPPD, "APDuplexRequiresFlippedMargin", NULL);
+	ppdFindAttr(cups->PPD, "APDuplexRequiresFlippedMargin", NULL);
       if (backsiderequiresflippedmargins)
 	dprintf1("DEBUG2: APDuplexRequiresFlippedMargin = %s\n",
 		 backsiderequiresflippedmargins->value);
@@ -3132,7 +3125,7 @@
       }
       else if (cups->header.Duplex &&
 	       ((!cups->header.Tumble &&
-		 (cupsPPD->flip_duplex ||
+		 (cups->PPD->flip_duplex ||
 		  (backside && !strcasecmp(backside->value, "Rotated")))) ||
 		(cups->header.Tumble &&
 		 (backside && !strcasecmp(backside->value, "ManualTumble")))) &&
@@ -3162,7 +3155,7 @@
 
       if (strlen(cups->header.cupsPageSizeName) != 0) {
 	found = 0;
-	for (i = cupsPPD->num_sizes, size = cupsPPD->sizes;
+	for (i = cups->PPD->num_sizes, size = cups->PPD->sizes;
 	     i > 0;
 	     i --, size ++)
 	  if (strcasecmp(cups->header.cupsPageSizeName, size->name) == 0) {
@@ -3178,7 +3171,7 @@
       * Find the matching page size...
       */
 
-      for (i = cupsPPD->num_sizes, size = cupsPPD->sizes;
+      for (i = cups->PPD->num_sizes, size = cups->PPD->sizes;
            i > 0;
            i --, size ++)
 	if (fabs(cups->MediaSize[1] - size->length) < 5.0 &&
@@ -3233,7 +3226,7 @@
 	* landscape orientation...
 	*/
 
-	for (i = cupsPPD->num_sizes, size = cupsPPD->sizes;
+	for (i = cups->PPD->num_sizes, size = cups->PPD->sizes;
              i > 0;
              i --, size ++)
 	  if (fabs(cups->MediaSize[0] - size->length) < 5.0 &&
@@ -3292,7 +3285,7 @@
 	  dprintf("DEBUG: size = Custom\n");
 
 	  for (i = 0; i < 4; i ++)
-            margins[i] = cupsPPD->custom_margins[i] / 72.0;
+            margins[i] = cups->PPD->custom_margins[i] / 72.0;
 	  if (xflip == 1)
 	  {
 	    swap = margins[0]; margins[0] = margins[2]; margins[2] = swap;
@@ -3402,14 +3395,14 @@
     */
 
     if (pdev->is_open &&
-	(width != width_old || height != height_old ||
-	 colorspace != colorspace_old || bitspercolor != bitspercolor_old))
+	(width != cups->width_old || height != cups->height_old ||
+	 colorspace != cups->colorspace_old || bitspercolor != cups->bitspercolor_old))
     {
 
-      width_old = width;
-      height_old = height;
-      colorspace_old = colorspace;
-      bitspercolor_old = bitspercolor;
+      cups->width_old = width;
+      cups->height_old = height;
+      cups->colorspace_old = colorspace;
+      cups->bitspercolor_old = bitspercolor;
 
      /*
       * Device is open and size has changed, so reallocate...
@@ -3438,7 +3431,7 @@
     }
   }
 
-  dprintf1("DEBUG2: ppd = %p\n", cupsPPD);
+  dprintf1("DEBUG2: ppd = %p\n", cups->PPD);
   dprintf2("DEBUG2: PageSize = [ %.3f %.3f ]\n",
 	   pdev->MediaSize[0], pdev->MediaSize[1]);
   dprintf4("DEBUG2: margins = [ %.3f %.3f %.3f %.3f ]\n",
@@ -3720,20 +3713,20 @@
       j = ((j & 255) << 8) | ((j >> 8) & 255);
 #endif /* !ARCH_IS_BIG_ENDIAN */
 
-    cupsEncodeLUT[i] = j;
+    cups->EncodeLUT[i] = j;
 
 #ifdef DEBUG
-    if (i == 0 || cupsEncodeLUT[i] != cupsEncodeLUT[i - 1])
-      dprintf2("DEBUG2: cupsEncodeLUT[%d] = %d\n", i, (int)cupsEncodeLUT[i]);
+    if (i == 0 || cups->EncodeLUT[i] != cups->EncodeLUT[i - 1])
+      dprintf2("DEBUG2: cups->EncodeLUT[%d] = %d\n", i, (int)cups->EncodeLUT[i]);
 #endif /* DEBUG */
   }
 
-  dprintf1("DEBUG2: cupsEncodeLUT[0] = %d\n", (int)cupsEncodeLUT[0]);
-  dprintf2("DEBUG2: cupsEncodeLUT[%d] = %d\n", gx_max_color_value,
-	   (int)cupsEncodeLUT[gx_max_color_value]);
+  dprintf1("DEBUG2: cups->EncodeLUT[0] = %d\n", (int)cups->EncodeLUT[0]);
+  dprintf2("DEBUG2: cups->EncodeLUT[%d] = %d\n", gx_max_color_value,
+	   (int)cups->EncodeLUT[gx_max_color_value]);
 
   for (i = 0; i < cups->color_info.dither_grays; i ++)
-    cupsDecodeLUT[i] = gx_max_color_value * i / max_lut;
+    cups->DecodeLUT[i] = gx_max_color_value * i / max_lut;
 
   dprintf2("DEBUG: num_components = %d, depth = %d\n",
 	   cups->color_info.num_components, cups->color_info.depth);
@@ -3750,24 +3743,24 @@
   * Set the color profile as needed...
   */
 
-  cupsHaveProfile = 0;
+  cups->HaveProfile = 0;
 
 #ifdef dev_t_proc_encode_color
-  if (cupsProfile)
+  if (cups->Profile)
 #else
-  if (cupsProfile && cups->header.cupsBitsPerColor == 8)
+  if (cups->Profile && cups->header.cupsBitsPerColor == 8)
 #endif /* dev_t_proc_encode_color */
   {
-    dprintf1("DEBUG: Using user-defined profile \"%s\"...\n", cupsProfile);
+    dprintf1("DEBUG: Using user-defined profile \"%s\"...\n", cups->Profile);
 
-    if (sscanf(cupsProfile, "%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f", &d, &g,
+    if (sscanf(cups->Profile, "%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f", &d, &g,
                m[0] + 0, m[0] + 1, m[0] + 2,
                m[1] + 0, m[1] + 1, m[1] + 2,
                m[2] + 0, m[2] + 1, m[2] + 2) != 11)
       dprintf("ERROR: User-defined profile does not contain 11 integers!\n");
     else
     {
-      cupsHaveProfile = 1;
+      cups->HaveProfile = 1;
 
       d       *= 0.001f;
       g       *= 0.001f;
@@ -3783,9 +3776,9 @@
     }
   }
 #ifdef dev_t_proc_encode_color
-  else if (cupsPPD)
+  else if (cups->PPD)
 #else
-  else if (cupsPPD && cups->header.cupsBitsPerColor == 8)
+  else if (cups->PPD && cups->header.cupsBitsPerColor == 8)
 #endif /* dev_t_proc_encode_color */
   {
    /*
@@ -3798,8 +3791,8 @@
     else
       sprintf(resolution, "%.0fdpi", pdev->HWResolution[0]);
 
-    for (i = 0, profile = cupsPPD->profiles;
-         i < cupsPPD->num_profiles;
+    for (i = 0, profile = cups->PPD->profiles;
+         i < cups->PPD->num_profiles;
 	 i ++, profile ++)
       if ((strcmp(profile->resolution, resolution) == 0 ||
            profile->resolution[0] == '-') &&
@@ -3811,11 +3804,11 @@
     * If we found a color profile, use it!
     */
 
-    if (i < cupsPPD->num_profiles)
+    if (i < cups->PPD->num_profiles)
     {
       dprintf("DEBUG: Using color profile in PPD file!\n");
 
-      cupsHaveProfile = 1;
+      cups->HaveProfile = 1;
 
       d = profile->density;
       g = profile->gamma;
@@ -3824,34 +3817,34 @@
     }
   }
 
-  if (cupsHaveProfile)
+  if (cups->HaveProfile)
   {
     for (i = 0; i < 3; i ++)
       for (j = 0; j < 3; j ++)
 	for (k = 0; k <= CUPS_MAX_VALUE; k ++)
 	{
-          cupsMatrix[i][j][k] = (int)((float)k * m[i][j] + 0.5);
+          cups->Matrix[i][j][k] = (int)((float)k * m[i][j] + 0.5);
 
           if ((k & 4095) == 0)
-            dprintf4("DEBUG2: cupsMatrix[%d][%d][%d] = %d\n",
-		     i, j, k, cupsMatrix[i][j][k]);
+            dprintf4("DEBUG2: cups->Matrix[%d][%d][%d] = %d\n",
+		     i, j, k, cups->Matrix[i][j][k]);
         }
 
 
     for (k = 0; k <= CUPS_MAX_VALUE; k ++)
     {
-      cupsDensity[k] = (int)((float)CUPS_MAX_VALUE * d *
+      cups->Density[k] = (int)((float)CUPS_MAX_VALUE * d *
 	                     pow((float)k / (float)CUPS_MAX_VALUE, g) +
 			     0.5);
 
       if ((k & 4095) == 0)
-        dprintf2("DEBUG2: cupsDensity[%d] = %d\n", k, cupsDensity[k]);
+        dprintf2("DEBUG2: cups->Density[%d] = %d\n", k, cups->Density[k]);
     }
   }
   else
   {
     for (k = 0; k <= CUPS_MAX_VALUE; k ++)
-      cupsDensity[k] = k;
+      cups->Density[k] = k;
   }
 }
 
@@ -3895,18 +3888,18 @@
   dprintf1("DEBUG2: cups->header.Duplex = %d\n", cups->header.Duplex);
   dprintf1("DEBUG2: cups->header.Tumble = %d\n", cups->header.Tumble);
   dprintf1("DEBUG2: cups->page = %d\n", cups->page);
-  dprintf1("DEBUG2: cupsPPD = %p\n", cupsPPD);
+  dprintf1("DEBUG2: cups->PPD = %p\n", cups->PPD);
 
-  if (cupsPPD) {
-    backside = ppdFindAttr(cupsPPD, "cupsBackSide", NULL);
+  if (cups->PPD) {
+    backside = ppdFindAttr(cups->PPD, "cupsBackSide", NULL);
     if (backside) {
       dprintf1("DEBUG2: cupsBackSide = %s\n", backside->value);
-      cupsPPD->flip_duplex = 0;
+      cups->PPD->flip_duplex = 0;
     }
   }
-  if (cups->header.Duplex && cupsPPD &&
+  if (cups->header.Duplex && cups->PPD &&
       ((!cups->header.Tumble &&
-	(cupsPPD->flip_duplex ||
+	(cups->PPD->flip_duplex ||
 	 (backside && !strcasecmp(backside->value, "Rotated")))) ||
        (cups->header.Tumble &&
 	(backside && (!strcasecmp(backside->value, "Flipped") ||
@@ -3915,9 +3908,9 @@
     xflip = 1;
   else
     xflip = 0;
-  if (cups->header.Duplex && cupsPPD &&
+  if (cups->header.Duplex && cups->PPD &&
       ((!cups->header.Tumble &&
-	(cupsPPD->flip_duplex ||
+	(cups->PPD->flip_duplex ||
 	 (backside && (!strcasecmp(backside->value, "Flipped") ||
 		       !strcasecmp(backside->value, "Rotated"))))) ||
        (cups->header.Tumble &&
@@ -3973,8 +3966,8 @@
 	           count > 0;
 		   count --, srcptr --, dstptr ++)
 	      {
-	        *dstptr = cupsRevUpper1[*srcptr & 15] |
-		          cupsRevLower1[*srcptr >> 4];
+	        *dstptr = cups->RevUpper1[*srcptr & 15] |
+		          cups->RevLower1[*srcptr >> 4];
               }
 	      break;
 
@@ -3983,8 +3976,8 @@
 	           count > 0;
 		   count --, srcptr --, dstptr ++)
 	      {
-	        *dstptr = cupsRevUpper2[*srcptr & 15] |
-		          cupsRevLower2[*srcptr >> 4];
+	        *dstptr = cups->RevUpper2[*srcptr & 15] |
+		          cups->RevLower2[*srcptr >> 4];
               }
 	      break;
 
@@ -4116,18 +4109,18 @@
   dprintf1("DEBUG2: cups->header.Duplex = %d\n", cups->header.Duplex);
   dprintf1("DEBUG2: cups->header.Tumble = %d\n", cups->header.Tumble);
   dprintf1("DEBUG2: cups->page = %d\n", cups->page);
-  dprintf1("DEBUG2: cupsPPD = %p\n", cupsPPD);
+  dprintf1("DEBUG2: cups->PPD = %p\n", cups->PPD);
 
-  if (cupsPPD) {
-    backside = ppdFindAttr(cupsPPD, "cupsBackSide", NULL);
+  if (cups->PPD) {
+    backside = ppdFindAttr(cups->PPD, "cupsBackSide", NULL);
     if (backside) {
       dprintf1("DEBUG2: cupsBackSide = %s\n", backside->value);
-      cupsPPD->flip_duplex = 0;
+      cups->PPD->flip_duplex = 0;
     }
   }
-  if (cups->header.Duplex && cupsPPD &&
+  if (cups->header.Duplex && cups->PPD &&
       ((!cups->header.Tumble &&
-	(cupsPPD->flip_duplex ||
+	(cups->PPD->flip_duplex ||
 	 (backside && !strcasecmp(backside->value, "Rotated")))) ||
        (cups->header.Tumble &&
 	(backside && (!strcasecmp(backside->value, "Flipped") ||
@@ -4136,9 +4129,9 @@
     xflip = 1;
   else
     xflip = 0;
-  if (cups->header.Duplex && cupsPPD &&
+  if (cups->header.Duplex && cups->PPD &&
       ((!cups->header.Tumble &&
-	(cupsPPD->flip_duplex ||
+	(cups->PPD->flip_duplex ||
 	 (backside && (!strcasecmp(backside->value, "Flipped") ||
 		       !strcasecmp(backside->value, "Rotated"))))) ||
        (cups->header.Tumble &&
