fix for-loop in _mesa_GetDouble to avoid out of bounds memory read
[mesa.git] / src / mesa / main / texcompress_fxt1.c
index c33a00373708936a6f5912d3d20f7f9ead55e82b..d5e2c790f609201bea537347caac16ce19d5a66c 100644 (file)
 #include "texstore.h"
 
 
-int
-fxt1_encode (GLcontext *ctx,
-             unsigned int width, unsigned int height,
-             int srcFormat,
-             const void *source, int srcRowStride,
-             void *dest, int destRowStride);
+static GLint
+fxt1_encode (GLuint width, GLuint height, GLint comps,
+             const void *source, GLint srcRowStride,
+             void *dest, GLint destRowStride);
+
 void
-fxt1_decode_1 (const void *texture, int width,
-               int i, int j, unsigned char *rgba);
+fxt1_decode_1 (const void *texture, GLint stride,
+               GLint i, GLint j, GLubyte *rgba);
 
 
 /**
@@ -107,7 +106,7 @@ texstore_rgb_fxt1(STORE_PARAMS)
                                         GL_COMPRESSED_RGB_FXT1_3DFX,
                                         texWidth, (GLubyte *) dstAddr);
 
-   fxt1_encode(ctx, srcWidth, srcHeight, srcFormat, pixels, srcRowStride,
+   fxt1_encode(srcWidth, srcHeight, 3, pixels, srcRowStride,
                dst, dstRowStride);
 
    if (tempImage)
@@ -163,7 +162,7 @@ texstore_rgba_fxt1(STORE_PARAMS)
                                         GL_COMPRESSED_RGBA_FXT1_3DFX,
                                         texWidth, (GLubyte *) dstAddr);
 
-   fxt1_encode(ctx, srcWidth, srcHeight, srcFormat, pixels, srcRowStride,
+   fxt1_encode(srcWidth, srcHeight, 4, pixels, srcRowStride,
                dst, dstRowStride);
 
    if (tempImage)
@@ -178,7 +177,7 @@ fetch_texel_2d_rgba_fxt1( const struct gl_texture_image *texImage,
                           GLint i, GLint j, GLint k, GLchan *texel )
 {
    (void) k;
-   fxt1_decode_1(texImage->Data, texImage->Width, i, j, texel);
+   fxt1_decode_1(texImage->Data, texImage->RowStride, i, j, texel);
 }
 
 
@@ -189,7 +188,7 @@ fetch_texel_2d_f_rgba_fxt1( const struct gl_texture_image *texImage,
    /* just sample as GLchan and convert to float here */
    GLchan rgba[4];
    (void) k;
-   fxt1_decode_1(texImage->Data, texImage->Width, i, j, rgba);
+   fxt1_decode_1(texImage->Data, texImage->RowStride, i, j, rgba);
    texel[RCOMP] = CHAN_TO_FLOAT(rgba[RCOMP]);
    texel[GCOMP] = CHAN_TO_FLOAT(rgba[GCOMP]);
    texel[BCOMP] = CHAN_TO_FLOAT(rgba[BCOMP]);
@@ -202,7 +201,7 @@ fetch_texel_2d_rgb_fxt1( const struct gl_texture_image *texImage,
                          GLint i, GLint j, GLint k, GLchan *texel )
 {
    (void) k;
-   fxt1_decode_1(texImage->Data, texImage->Width, i, j, texel);
+   fxt1_decode_1(texImage->Data, texImage->RowStride, i, j, texel);
    texel[ACOMP] = 255;
 }
 
@@ -214,11 +213,11 @@ fetch_texel_2d_f_rgb_fxt1( const struct gl_texture_image *texImage,
    /* just sample as GLchan and convert to float here */
    GLchan rgba[4];
    (void) k;
-   fxt1_decode_1(texImage->Data, texImage->Width, i, j, rgba);
+   fxt1_decode_1(texImage->Data, texImage->RowStride, i, j, rgba);
    texel[RCOMP] = CHAN_TO_FLOAT(rgba[RCOMP]);
    texel[GCOMP] = CHAN_TO_FLOAT(rgba[GCOMP]);
    texel[BCOMP] = CHAN_TO_FLOAT(rgba[BCOMP]);
-   texel[ACOMP] = 1.0;
+   texel[ACOMP] = 1.0F;
 }
 
 
@@ -273,9 +272,9 @@ const struct gl_texture_format _mesa_texformat_rgba_fxt1 = {
  *
  * The encoder was built by reversing the decoder,
  * and is vaguely based on Texus2 by 3dfx. Note that this code
- * is merely a proof of concept, since it is higly UNoptimized;
- * moreover, it is sub-optimal due to inital conditions passed
- * to Lloyd's algorithm (the interpolation modes are worse).
+ * is merely a proof of concept, since it is highly UNoptimized;
+ * moreover, it is sub-optimal due to initial conditions passed
+ * to Lloyd's algorithm (the interpolation modes are even worse).
 \***************************************************************************/
 
 
@@ -286,10 +285,13 @@ const struct gl_texture_format _mesa_texformat_rgba_fxt1 = {
 #define LL_RMS_D 10 /* fault tolerance (maximum delta) */
 #define LL_RMS_E 255 /* fault tolerance (maximum error) */
 #define ALPHA_TS 2 /* alpha threshold: (255 - ALPHA_TS) deemed opaque */
-#define ISTBLACK(v) (*((unsigned long *)(v)) == 0)
+#define ISTBLACK(v) (*((GLuint *)(v)) == 0)
 
 
-#ifdef __GNUC__
+/*
+ * Define a 64-bit unsigned integer type and macros
+ */
+#if defined(__GNUC__) && !defined(__cplusplus)
 
 #define FX64_NATIVE 1
 
@@ -304,7 +306,7 @@ typedef unsigned long long Fx64;
 #define FX64_NATIVE 0
 
 typedef struct {
-        unsigned long lo, hi;
+   GLuint lo, hi;
 } Fx64;
 
 #define FX64_MOV32(a, b) a.lo = b
@@ -324,15 +326,55 @@ typedef struct {
 #endif /* !__GNUC__ */
 
 
-static int
-fxt1_bestcol (float vec[][MAX_COMP], int nv,
-              unsigned char input[MAX_COMP], int nc)
+#define F(i) (GLfloat)1 /* can be used to obtain an oblong metric: 0.30 / 0.59 / 0.11 */
+#define SAFECDOT 1 /* for paranoids */
+
+#define MAKEIVEC(NV, NC, IV, B, V0, V1)  \
+   do {                                  \
+      /* compute interpolation vector */ \
+      GLfloat d2 = 0.0F;                 \
+      GLfloat rd2;                       \
+                                         \
+      for (i = 0; i < NC; i++) {         \
+         IV[i] = (V1[i] - V0[i]) * F(i); \
+         d2 += IV[i] * IV[i];            \
+      }                                  \
+      rd2 = (GLfloat)NV / d2;            \
+      B = 0;                             \
+      for (i = 0; i < NC; i++) {         \
+         IV[i] *= F(i);                  \
+         B -= IV[i] * V0[i];             \
+         IV[i] *= rd2;                   \
+      }                                  \
+      B = B * rd2 + 0.5f;                \
+   } while (0)
+
+#define CALCCDOT(TEXEL, NV, NC, IV, B, V)\
+   do {                                  \
+      GLfloat dot = 0.0F;                \
+      for (i = 0; i < NC; i++) {         \
+         dot += V[i] * IV[i];            \
+      }                                  \
+      TEXEL = (GLint)(dot + B);          \
+      if (SAFECDOT) {                    \
+         if (TEXEL < 0) {                \
+            TEXEL = 0;                   \
+         } else if (TEXEL > NV) {        \
+            TEXEL = NV;                  \
+         }                               \
+      }                                  \
+   } while (0)
+
+
+static GLint
+fxt1_bestcol (GLfloat vec[][MAX_COMP], GLint nv,
+              GLubyte input[MAX_COMP], GLint nc)
 {
-   int i, j, best = -1;
-   float err = 1e9; /* big enough */
+   GLint i, j, best = -1;
+   GLfloat err = 1e9; /* big enough */
 
    for (j = 0; j < nv; j++) {
-      float e = 0;
+      GLfloat e = 0.0F;
       for (i = 0; i < nc; i++) {
          e += (vec[j][i] - input[i]) * (vec[j][i] - input[i]);
       }
@@ -346,15 +388,15 @@ fxt1_bestcol (float vec[][MAX_COMP], int nv,
 }
 
 
-static int
-fxt1_worst (float vec[MAX_COMP],
-            unsigned char input[N_TEXELS][MAX_COMP], int nc, int n)
+static GLint
+fxt1_worst (GLfloat vec[MAX_COMP],
+            GLubyte input[N_TEXELS][MAX_COMP], GLint nc, GLint n)
 {
-   int i, k, worst = -1;
-   float err = -1; /* small enough */
+   GLint i, k, worst = -1;
+   GLfloat err = -1.0F; /* small enough */
 
    for (k = 0; k < n; k++) {
-      float e = 0;
+      GLfloat e = 0.0F;
       for (i = 0; i < nc; i++) {
          e += (vec[i] - input[k][i]) * (vec[i] - input[k][i]);
       }
@@ -368,19 +410,19 @@ fxt1_worst (float vec[MAX_COMP],
 }
 
 
-static int
-fxt1_variance (double variance[MAX_COMP],
-               unsigned char input[N_TEXELS][MAX_COMP], int nc, int n)
+static GLint
+fxt1_variance (GLdouble variance[MAX_COMP],
+               GLubyte input[N_TEXELS][MAX_COMP], GLint nc, GLint n)
 {
-   int i, k, best = 0;
-   int sx, sx2;
-   double var, maxvar = -1; /* small enough */
-   double teenth = 1.0 / n;
+   GLint i, k, best = 0;
+   GLint sx, sx2;
+   GLdouble var, maxvar = -1; /* small enough */
+   GLdouble teenth = 1.0 / n;
 
    for (i = 0; i < nc; i++) {
       sx = sx2 = 0;
       for (k = 0; k < n; k++) {
-         int t = input[k][i];
+         GLint t = input[k][i];
          sx += t;
          sx2 += t * t;
       }
@@ -398,17 +440,17 @@ fxt1_variance (double variance[MAX_COMP],
 }
 
 
-static int
-fxt1_choose (float vec[][MAX_COMP], int nv,
-            unsigned char input[N_TEXELS][MAX_COMP], int nc, int n)
+static GLint
+fxt1_choose (GLfloat vec[][MAX_COMP], GLint nv,
+             GLubyte input[N_TEXELS][MAX_COMP], GLint nc, GLint n)
 {
 #if 0
    /* Choose colors from a grid.
     */
-   int i, j;
+   GLint i, j;
 
    for (j = 0; j < nv; j++) {
-      int m = j * (n - 1) / (nv - 1);
+      GLint m = j * (n - 1) / (nv - 1);
       for (i = 0; i < nc; i++) {
          vec[j][i] = input[m][i];
       }
@@ -418,26 +460,26 @@ fxt1_choose (float vec[][MAX_COMP], int nv,
     * the 8x4 tile and use those as the two representative colors.
     * There are probably better algorithms to use (histogram-based).
     */
-   int i, j, k;
-   int minSum = 1000; /* big enough */
-   int maxSum = -1; /* small enough */
-   int minCol = 0; /* phoudoin: silent compiler! */
-   int maxCol = 0; /* phoudoin: silent compiler! */
+   GLint i, j, k;
+   GLint minSum = 2000; /* big enough */
+   GLint maxSum = -1; /* small enough */
+   GLint minCol = 0; /* phoudoin: silent compiler! */
+   GLint maxCol = 0; /* phoudoin: silent compiler! */
 
    struct {
-      int flag;
-      int key;
-      int freq;
-      int idx;
+      GLint flag;
+      GLint key;
+      GLint freq;
+      GLint idx;
    } hist[N_TEXELS];
-   int lenh = 0;
+   GLint lenh = 0;
 
    memset(hist, 0, sizeof(hist));
 
    for (k = 0; k < n; k++) {
-      int l;
-      int key = 0;
-      int sum = 0;
+      GLint l;
+      GLint key = 0;
+      GLint sum = 0;
       for (i = 0; i < nc; i++) {
          key <<= 8;
          key |= input[k][i];
@@ -470,7 +512,7 @@ fxt1_choose (float vec[][MAX_COMP], int nv,
    if (lenh <= nv) {
       for (j = 0; j < lenh; j++) {
          for (i = 0; i < nc; i++) {
-            vec[j][i] = (float)input[hist[j].idx][i];
+            vec[j][i] = (GLfloat)input[hist[j].idx][i];
          }
       }
       for (; j < nv; j++) {
@@ -483,7 +525,7 @@ fxt1_choose (float vec[][MAX_COMP], int nv,
 
    for (j = 0; j < nv; j++) {
       for (i = 0; i < nc; i++) {
-         vec[j][i] = ((nv - 1 - j) * input[minCol][i] + j * input[maxCol][i] + (nv - 1) / 2) / (nv - 1);
+         vec[j][i] = ((nv - 1 - j) * input[minCol][i] + j * input[maxCol][i] + (nv - 1) / 2) / (GLfloat)(nv - 1);
       }
    }
 #endif
@@ -492,9 +534,9 @@ fxt1_choose (float vec[][MAX_COMP], int nv,
 }
 
 
-static int
-fxt1_lloyd (float vec[][MAX_COMP], int nv,
-            unsigned char input[N_TEXELS][MAX_COMP], int nc, int n)
+static GLint
+fxt1_lloyd (GLfloat vec[][MAX_COMP], GLint nv,
+            GLubyte input[N_TEXELS][MAX_COMP], GLint nc, GLint n)
 {
    /* Use the generalized lloyd's algorithm for VQ:
     *     find 4 color vectors.
@@ -516,11 +558,11 @@ fxt1_lloyd (float vec[][MAX_COMP], int nv,
     * n                         number of input samples
     */
 
-   int sum[MAX_VECT][MAX_COMP]; /* used to accumulate closest texels */
-   int cnt[MAX_VECT]; /* how many times a certain vector was chosen */
-   float error, lasterror = 1e9;
+   GLint sum[MAX_VECT][MAX_COMP]; /* used to accumulate closest texels */
+   GLint cnt[MAX_VECT]; /* how many times a certain vector was chosen */
+   GLfloat error, lasterror = 1e9;
 
-   int i, j, k, rep;
+   GLint i, j, k, rep;
 
    /* the quantizer */
    for (rep = 0; rep < LL_N_REP; rep++) {
@@ -536,11 +578,11 @@ fxt1_lloyd (float vec[][MAX_COMP], int nv,
       /* scan whole block */
       for (k = 0; k < n; k++) {
 #if 1
-         int best = -1;
-         float err = 1e9; /* big enough */
+         GLint best = -1;
+         GLfloat err = 1e9; /* big enough */
          /* determine best vector */
          for (j = 0; j < nv; j++) {
-            float e = (vec[j][0] - input[k][0]) * (vec[j][0] - input[k][0]) +
+            GLfloat e = (vec[j][0] - input[k][0]) * (vec[j][0] - input[k][0]) +
                       (vec[j][1] - input[k][1]) * (vec[j][1] - input[k][1]) +
                       (vec[j][2] - input[k][2]) * (vec[j][2] - input[k][2]);
             if (nc == 4) {
@@ -552,7 +594,7 @@ fxt1_lloyd (float vec[][MAX_COMP], int nv,
             }
          }
 #else
-         int best = fxt1_bestcol(vec, n_vect, input[k], n_comp, &err);
+         GLint best = fxt1_bestcol(vec, nv, input[k], nc, &err);
 #endif
          /* add in closest color */
          for (i = 0; i < nc; i++) {
@@ -574,13 +616,13 @@ fxt1_lloyd (float vec[][MAX_COMP], int nv,
       /* move each vector to the barycenter of its closest colors */
       for (j = 0; j < nv; j++) {
          if (cnt[j]) {
-            float div = 1.0 / cnt[j];
+            GLfloat div = 1.0F / cnt[j];
             for (i = 0; i < nc; i++) {
                vec[j][i] = div * sum[j][i];
             }
          } else {
             /* this vec has no samples or is identical with a previous vec */
-            int worst = fxt1_worst(vec[j], input, nc, n);
+            GLint worst = fxt1_worst(vec[j], input, nc, n);
             for (i = 0; i < nc; i++) {
                vec[j][i] = input[worst][i];
             }
@@ -593,15 +635,15 @@ fxt1_lloyd (float vec[][MAX_COMP], int nv,
 
 
 static void
-fxt1_quantize_CHROMA (unsigned long *cc,
-                      unsigned char input[N_TEXELS][MAX_COMP])
+fxt1_quantize_CHROMA (GLuint *cc,
+                      GLubyte input[N_TEXELS][MAX_COMP])
 {
-   const int n_vect = 4; /* 4 base vectors to find */
-   const int n_comp = 3; /* 3 components: R, G, B */
-   float vec[MAX_VECT][MAX_COMP];
-   int i, j, k;
+   const GLint n_vect = 4; /* 4 base vectors to find */
+   const GLint n_comp = 3; /* 3 components: R, G, B */
+   GLfloat vec[MAX_VECT][MAX_COMP];
+   GLint i, j, k;
    Fx64 hi; /* high quadword */
-   unsigned long lohi, lolo; /* low quadword: hi dword, lo dword */
+   GLuint lohi, lolo; /* low quadword: hi dword, lo dword */
 
    if (fxt1_choose(vec, n_vect, input, n_comp, N_TEXELS) != 0) {
       fxt1_lloyd(vec, n_vect, input, n_comp, N_TEXELS);
@@ -612,7 +654,7 @@ fxt1_quantize_CHROMA (unsigned long *cc,
       for (i = 0; i < n_comp; i++) {
          /* add in colors */
          FX64_SHL(hi, 5);
-         FX64_OR32(hi, (unsigned int)(vec[j][i] / 8.0));
+         FX64_OR32(hi, (GLuint)(vec[j][i] / 8.0F));
       }
    }
    ((Fx64 *)cc)[1] = hi;
@@ -634,16 +676,16 @@ fxt1_quantize_CHROMA (unsigned long *cc,
 
 
 static void
-fxt1_quantize_ALPHA0 (unsigned long *cc,
-                      unsigned char input[N_TEXELS][MAX_COMP],
-                      unsigned char reord[N_TEXELS][MAX_COMP], int n)
+fxt1_quantize_ALPHA0 (GLuint *cc,
+                      GLubyte input[N_TEXELS][MAX_COMP],
+                      GLubyte reord[N_TEXELS][MAX_COMP], GLint n)
 {
-   const int n_vect = 3; /* 3 base vectors to find */
-   const int n_comp = 4; /* 4 components: R, G, B, A */
-   float vec[MAX_VECT][MAX_COMP];
-   int i, j, k;
+   const GLint n_vect = 3; /* 3 base vectors to find */
+   const GLint n_comp = 4; /* 4 components: R, G, B, A */
+   GLfloat vec[MAX_VECT][MAX_COMP];
+   GLint i, j, k;
    Fx64 hi; /* high quadword */
-   unsigned long lohi, lolo; /* low quadword: hi dword, lo dword */
+   GLuint lohi, lolo; /* low quadword: hi dword, lo dword */
 
    /* the last vector indicates zero */
    for (i = 0; i < n_comp; i++) {
@@ -659,13 +701,13 @@ fxt1_quantize_ALPHA0 (unsigned long *cc,
    for (j = n_vect - 1; j >= 0; j--) {
       /* add in alphas */
       FX64_SHL(hi, 5);
-      FX64_OR32(hi, (unsigned int)(vec[j][ACOMP] / 8.0));
+      FX64_OR32(hi, (GLuint)(vec[j][ACOMP] / 8.0F));
    }
    for (j = n_vect - 1; j >= 0; j--) {
       for (i = 0; i < n_comp - 1; i++) {
          /* add in colors */
          FX64_SHL(hi, 5);
-         FX64_OR32(hi, (unsigned int)(vec[j][i] / 8.0));
+         FX64_OR32(hi, (GLuint)(vec[j][i] / 8.0F));
       }
    }
    ((Fx64 *)cc)[1] = hi;
@@ -687,31 +729,31 @@ fxt1_quantize_ALPHA0 (unsigned long *cc,
 
 
 static void
-fxt1_quantize_ALPHA1 (unsigned long *cc,
-                      unsigned char input[N_TEXELS][MAX_COMP])
+fxt1_quantize_ALPHA1 (GLuint *cc,
+                      GLubyte input[N_TEXELS][MAX_COMP])
 {
-   const int n_vect = 3; /* highest vector number in each microtile */
-   const int n_comp = 4; /* 4 components: R, G, B, A */
-   float vec[1 + 1 + 1][MAX_COMP]; /* 1.5 extrema for each sub-block */
-   float b, iv[MAX_COMP]; /* interpolation vector */
-   int i, j, k;
+   const GLint n_vect = 3; /* highest vector number in each microtile */
+   const GLint n_comp = 4; /* 4 components: R, G, B, A */
+   GLfloat vec[1 + 1 + 1][MAX_COMP]; /* 1.5 extrema for each sub-block */
+   GLfloat b, iv[MAX_COMP]; /* interpolation vector */
+   GLint i, j, k;
    Fx64 hi; /* high quadword */
-   unsigned long lohi, lolo; /* low quadword: hi dword, lo dword */
+   GLuint lohi, lolo; /* low quadword: hi dword, lo dword */
 
-   int minSum;
-   int maxSum;
-   int minColL = 0, maxColL = 0;
-   int minColR = 0, maxColR = 0;
-   int sumL = 0, sumR = 0;
+   GLint minSum;
+   GLint maxSum;
+   GLint minColL = 0, maxColL = 0;
+   GLint minColR = 0, maxColR = 0;
+   GLint sumL = 0, sumR = 0;
 
    /* Our solution here is to find the darkest and brightest colors in
     * the 4x4 tile and use those as the two representative colors.
     * There are probably better algorithms to use (histogram-based).
     */
-   minSum = 1000; /* big enough */
+   minSum = 2000; /* big enough */
    maxSum = -1; /* small enough */
    for (k = 0; k < N_TEXELS / 2; k++) {
-      int sum = 0;
+      GLint sum = 0;
       for (i = 0; i < n_comp; i++) {
          sum += input[k][i];
       }
@@ -725,10 +767,10 @@ fxt1_quantize_ALPHA1 (unsigned long *cc,
       }
       sumL += sum;
    }
-   minSum = 1000; /* big enough */
+   minSum = 2000; /* big enough */
    maxSum = -1; /* small enough */
    for (; k < N_TEXELS; k++) {
-      int sum = 0;
+      GLint sum = 0;
       for (i = 0; i < n_comp; i++) {
          sum += input[k][i];
       }
@@ -744,71 +786,49 @@ fxt1_quantize_ALPHA1 (unsigned long *cc,
    }
 
    /* choose the common vector (yuck!) */
-{
-   int j1, j2;
-   int v1 = 0, v2 = 0;
-   float err = 1e9; /* big enough */
-   float tv[2 * 2][MAX_COMP]; /* 2 extrema for each sub-block */
-   for (i = 0; i < n_comp; i++) {
-      tv[0][i] = input[minColL][i];
-      tv[1][i] = input[maxColL][i];
-      tv[2][i] = input[minColR][i];
-      tv[3][i] = input[maxColR][i];
-   }
-   for (j1 = 0; j1 < 2; j1++) {
-      for (j2 = 2; j2 < 4; j2++) {
-          float e = 0;
-          for (i = 0; i < n_comp; i++) {
-             e += (tv[j1][i] - tv[j2][i]) * (tv[j1][i] - tv[j2][i]);
-          }
-          if (e < err) {
-             err = e;
-             v1 = j1;
-             v2 = j2;
-          }
+   {
+      GLint j1, j2;
+      GLint v1 = 0, v2 = 0;
+      GLfloat err = 1e9; /* big enough */
+      GLfloat tv[2 * 2][MAX_COMP]; /* 2 extrema for each sub-block */
+      for (i = 0; i < n_comp; i++) {
+         tv[0][i] = input[minColL][i];
+         tv[1][i] = input[maxColL][i];
+         tv[2][i] = input[minColR][i];
+         tv[3][i] = input[maxColR][i];
+      }
+      for (j1 = 0; j1 < 2; j1++) {
+         for (j2 = 2; j2 < 4; j2++) {
+            GLfloat e = 0.0F;
+            for (i = 0; i < n_comp; i++) {
+               e += (tv[j1][i] - tv[j2][i]) * (tv[j1][i] - tv[j2][i]);
+            }
+            if (e < err) {
+               err = e;
+               v1 = j1;
+               v2 = j2;
+            }
+         }
+      }
+      for (i = 0; i < n_comp; i++) {
+         vec[0][i] = tv[1 - v1][i];
+         vec[1][i] = (tv[v1][i] * sumL + tv[v2][i] * sumR) / (sumL + sumR);
+         vec[2][i] = tv[5 - v2][i];
       }
    }
-   for (i = 0; i < n_comp; i++) {
-      vec[0][i] = tv[1 - v1][i];
-      vec[1][i] = (tv[v1][i] * sumL + tv[v2][i] * sumR) / (sumL + sumR);
-      vec[2][i] = tv[5 - v2][i];
-   }
-}
 
    /* left microtile */
    cc[0] = 0;
    if (minColL != maxColL) {
       /* compute interpolation vector */
-      float d2 = 0;
-      float rd2;
-
-      for (i = 0; i < n_comp; i++) {
-         iv[i] = vec[1][i] - vec[0][i];
-         d2 += iv[i] * iv[i];
-      }
-      rd2 = (float)n_vect / d2;
-      b = 0;
-      for (i = 0; i < n_comp; i++) {
-         b -= iv[i] * vec[0][i];
-         iv[i] *= rd2;
-      }
-      b = b * rd2 + 0.5f;
+      MAKEIVEC(n_vect, n_comp, iv, b, vec[0], vec[1]);
 
       /* add in texels */
       lolo = 0;
       for (k = N_TEXELS / 2 - 1; k >= 0; k--) {
-         int texel;
+         GLint texel;
          /* interpolate color */
-         float dot = 0;
-         for (i = 0; i < n_comp; i++) {
-            dot += input[k][i] * iv[i];
-         }
-         texel = (int)(dot + b);
-         if (texel < 0) {
-            texel = 0;
-         } else if (texel > n_vect) {
-            texel = n_vect;
-         }
+         CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
          /* add in texel */
          lolo <<= 2;
          lolo |= texel;
@@ -821,36 +841,14 @@ fxt1_quantize_ALPHA1 (unsigned long *cc,
    cc[1] = 0;
    if (minColR != maxColR) {
       /* compute interpolation vector */
-      float d2 = 0;
-      float rd2;
-
-      for (i = 0; i < n_comp; i++) {
-         iv[i] = vec[1][i] - vec[2][i];
-         d2 += iv[i] * iv[i];
-      }
-      rd2 = (float)n_vect / d2;
-      b = 0;
-      for (i = 0; i < n_comp; i++) {
-         b -= iv[i] * vec[2][i];
-         iv[i] *= rd2;
-      }
-      b = b * rd2 + 0.5f;
+      MAKEIVEC(n_vect, n_comp, iv, b, vec[2], vec[1]);
 
       /* add in texels */
       lohi = 0;
       for (k = N_TEXELS - 1; k >= N_TEXELS / 2; k--) {
-         int texel;
+         GLint texel;
          /* interpolate color */
-         float dot = 0;
-         for (i = 0; i < n_comp; i++) {
-            dot += input[k][i] * iv[i];
-         }
-         texel = (int)(dot + b);
-         if (texel < 0) {
-            texel = 0;
-         } else if (texel > n_vect) {
-            texel = n_vect;
-         }
+         CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
          /* add in texel */
          lohi <<= 2;
          lohi |= texel;
@@ -863,13 +861,13 @@ fxt1_quantize_ALPHA1 (unsigned long *cc,
    for (j = n_vect - 1; j >= 0; j--) {
       /* add in alphas */
       FX64_SHL(hi, 5);
-      FX64_OR32(hi, (unsigned int)(vec[j][ACOMP] / 8.0));
+      FX64_OR32(hi, (GLuint)(vec[j][ACOMP] / 8.0F));
    }
    for (j = n_vect - 1; j >= 0; j--) {
       for (i = 0; i < n_comp - 1; i++) {
          /* add in colors */
          FX64_SHL(hi, 5);
-         FX64_OR32(hi, (unsigned int)(vec[j][i] / 8.0));
+         FX64_OR32(hi, (GLuint)(vec[j][i] / 8.0F));
       }
    }
    ((Fx64 *)cc)[1] = hi;
@@ -877,28 +875,28 @@ fxt1_quantize_ALPHA1 (unsigned long *cc,
 
 
 static void
-fxt1_quantize_HI (unsigned long *cc,
-                  unsigned char input[N_TEXELS][MAX_COMP],
-                  unsigned char reord[N_TEXELS][MAX_COMP], int n)
+fxt1_quantize_HI (GLuint *cc,
+                  GLubyte input[N_TEXELS][MAX_COMP],
+                  GLubyte reord[N_TEXELS][MAX_COMP], GLint n)
 {
-   const int n_vect = 6; /* highest vector number */
-   const int n_comp = 3; /* 3 components: R, G, B */
-   float b = 0.0;              /* phoudoin: silent compiler! */
-   float iv[MAX_COMP]; /* interpolation vector */
-   int i, k;
-   unsigned long hihi; /* high quadword: hi dword */
-
-   int minSum = 1000; /* big enough */
-   int maxSum = -1; /* small enough */
-   int minCol = 0;     /* phoudoin: silent compiler! */
-   int maxCol = 0;     /* phoudoin: silent compiler! */
+   const GLint n_vect = 6; /* highest vector number */
+   const GLint n_comp = 3; /* 3 components: R, G, B */
+   GLfloat b = 0.0F;       /* phoudoin: silent compiler! */
+   GLfloat iv[MAX_COMP];   /* interpolation vector */
+   GLint i, k;
+   GLuint hihi; /* high quadword: hi dword */
+
+   GLint minSum = 2000; /* big enough */
+   GLint maxSum = -1; /* small enough */
+   GLint minCol = 0; /* phoudoin: silent compiler! */
+   GLint maxCol = 0; /* phoudoin: silent compiler! */
 
    /* Our solution here is to find the darkest and brightest colors in
     * the 8x4 tile and use those as the two representative colors.
     * There are probably better algorithms to use (histogram-based).
     */
    for (k = 0; k < n; k++) {
-      int sum = 0;
+      GLint sum = 0;
       for (i = 0; i < n_comp; i++) {
          sum += reord[k][i];
       }
@@ -928,41 +926,19 @@ fxt1_quantize_HI (unsigned long *cc,
 
    /* compute interpolation vector */
    if (minCol != maxCol) {
-      float d2 = 0;
-      float rd2;
-
-      for (i = 0; i < n_comp; i++) {
-         iv[i] = reord[maxCol][i] - reord[minCol][i];
-         d2 += iv[i] * iv[i];
-      }
-      rd2 = (float)n_vect / d2;
-      b = 0;
-      for (i = 0; i < n_comp; i++) {
-         b -= iv[i] * reord[minCol][i];
-         iv[i] *= rd2;
-      }
-      b = b * rd2 + 0.5f;
+      MAKEIVEC(n_vect, n_comp, iv, b, reord[minCol], reord[maxCol]);
    }
 
    /* add in texels */
    for (k = N_TEXELS - 1; k >= 0; k--) {
-      int t = k * 3;
-      unsigned long *kk = (unsigned long *)((unsigned long)cc + t / 8);
-      int texel = n_vect + 1; /* transparent black */
+      GLint t = k * 3;
+      GLuint *kk = (GLuint *)((char *)cc + t / 8);
+      GLint texel = n_vect + 1; /* transparent black */
 
       if (!ISTBLACK(input[k])) {
          if (minCol != maxCol) {
             /* interpolate color */
-            float dot = 0;
-            for (i = 0; i < n_comp; i++) {
-               dot += input[k][i] * iv[i];
-            }
-            texel = (int)(dot + b);
-            if (texel < 0) {
-               texel = 0;
-            } else if (texel > n_vect) {
-               texel = n_vect;
-            }
+            CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
             /* add in texel */
             kk[0] |= texel << (t & 7);
          }
@@ -975,31 +951,31 @@ fxt1_quantize_HI (unsigned long *cc,
 
 
 static void
-fxt1_quantize_MIXED1 (unsigned long *cc,
-                      unsigned char input[N_TEXELS][MAX_COMP])
+fxt1_quantize_MIXED1 (GLuint *cc,
+                      GLubyte input[N_TEXELS][MAX_COMP])
 {
-   const int n_vect = 2; /* highest vector number in each microtile */
-   const int n_comp = 3; /* 3 components: R, G, B */
-   unsigned char vec[2 * 2][MAX_COMP]; /* 2 extrema for each sub-block */
-   float b, iv[MAX_COMP]; /* interpolation vector */
-   int i, j, k;
+   const GLint n_vect = 2; /* highest vector number in each microtile */
+   const GLint n_comp = 3; /* 3 components: R, G, B */
+   GLubyte vec[2 * 2][MAX_COMP]; /* 2 extrema for each sub-block */
+   GLfloat b, iv[MAX_COMP]; /* interpolation vector */
+   GLint i, j, k;
    Fx64 hi; /* high quadword */
-   unsigned long lohi, lolo; /* low quadword: hi dword, lo dword */
+   GLuint lohi, lolo; /* low quadword: hi dword, lo dword */
 
-   int minSum;
-   int maxSum;
-   int minColL = 0, maxColL = -1;
-   int minColR = 0, maxColR = -1;
+   GLint minSum;
+   GLint maxSum;
+   GLint minColL = 0, maxColL = -1;
+   GLint minColR = 0, maxColR = -1;
 
    /* Our solution here is to find the darkest and brightest colors in
     * the 4x4 tile and use those as the two representative colors.
     * There are probably better algorithms to use (histogram-based).
     */
-   minSum = 1000; /* big enough */
+   minSum = 2000; /* big enough */
    maxSum = -1; /* small enough */
    for (k = 0; k < N_TEXELS / 2; k++) {
       if (!ISTBLACK(input[k])) {
-         int sum = 0;
+         GLint sum = 0;
          for (i = 0; i < n_comp; i++) {
             sum += input[k][i];
          }
@@ -1013,11 +989,11 @@ fxt1_quantize_MIXED1 (unsigned long *cc,
          }
       }
    }
-   minSum = 1000; /* big enough */
+   minSum = 2000; /* big enough */
    maxSum = -1; /* small enough */
    for (; k < N_TEXELS; k++) {
       if (!ISTBLACK(input[k])) {
-         int sum = 0;
+         GLint sum = 0;
          for (i = 0; i < n_comp; i++) {
             sum += input[k][i];
          }
@@ -1035,7 +1011,7 @@ fxt1_quantize_MIXED1 (unsigned long *cc,
    /* left microtile */
    if (maxColL == -1) {
       /* all transparent black */
-      cc[0] = -1;
+      cc[0] = ~0u;
       for (i = 0; i < n_comp; i++) {
          vec[0][i] = 0;
          vec[1][i] = 0;
@@ -1048,37 +1024,15 @@ fxt1_quantize_MIXED1 (unsigned long *cc,
       }
       if (minColL != maxColL) {
          /* compute interpolation vector */
-         float d2 = 0;
-         float rd2;
-   
-         for (i = 0; i < n_comp; i++) {
-            iv[i] = vec[1][i] - vec[0][i];
-            d2 += iv[i] * iv[i];
-         }
-         rd2 = (float)n_vect / d2;
-         b = 0;
-         for (i = 0; i < n_comp; i++) {
-            b -= iv[i] * vec[0][i];
-            iv[i] *= rd2;
-         }
-         b = b * rd2 + 0.5f;
+         MAKEIVEC(n_vect, n_comp, iv, b, vec[0], vec[1]);
 
          /* add in texels */
          lolo = 0;
          for (k = N_TEXELS / 2 - 1; k >= 0; k--) {
-            int texel = n_vect + 1; /* transparent black */
+            GLint texel = n_vect + 1; /* transparent black */
             if (!ISTBLACK(input[k])) {
                /* interpolate color */
-               float dot = 0;
-               for (i = 0; i < n_comp; i++) {
-                  dot += input[k][i] * iv[i];
-               }
-               texel = (int)(dot + b);
-               if (texel < 0) {
-                  texel = 0;
-               } else if (texel > n_vect) {
-                  texel = n_vect;
-               }
+               CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
             }
             /* add in texel */
             lolo <<= 2;
@@ -1091,7 +1045,7 @@ fxt1_quantize_MIXED1 (unsigned long *cc,
    /* right microtile */
    if (maxColR == -1) {
       /* all transparent black */
-      cc[1] = -1;
+      cc[1] = ~0u;
       for (i = 0; i < n_comp; i++) {
          vec[2][i] = 0;
          vec[3][i] = 0;
@@ -1104,37 +1058,15 @@ fxt1_quantize_MIXED1 (unsigned long *cc,
       }
       if (minColR != maxColR) {
          /* compute interpolation vector */
-         float d2 = 0;
-         float rd2;
-   
-         for (i = 0; i < n_comp; i++) {
-            iv[i] = vec[3][i] - vec[2][i];
-            d2 += iv[i] * iv[i];
-         }
-         rd2 = (float)n_vect / d2;
-         b = 0;
-         for (i = 0; i < n_comp; i++) {
-            b -= iv[i] * vec[2][i];
-            iv[i] *= rd2;
-         }
-         b = b * rd2 + 0.5f;
+         MAKEIVEC(n_vect, n_comp, iv, b, vec[2], vec[3]);
 
          /* add in texels */
          lohi = 0;
          for (k = N_TEXELS - 1; k >= N_TEXELS / 2; k--) {
-            int texel = n_vect + 1; /* transparent black */
+            GLint texel = n_vect + 1; /* transparent black */
             if (!ISTBLACK(input[k])) {
                /* interpolate color */
-               float dot = 0;
-               for (i = 0; i < n_comp; i++) {
-                  dot += input[k][i] * iv[i];
-               }
-               texel = (int)(dot + b);
-               if (texel < 0) {
-                  texel = 0;
-               } else if (texel > n_vect) {
-                  texel = n_vect;
-               }
+               CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
             }
             /* add in texel */
             lohi <<= 2;
@@ -1157,31 +1089,31 @@ fxt1_quantize_MIXED1 (unsigned long *cc,
 
 
 static void
-fxt1_quantize_MIXED0 (unsigned long *cc,
-                      unsigned char input[N_TEXELS][MAX_COMP])
+fxt1_quantize_MIXED0 (GLuint *cc,
+                      GLubyte input[N_TEXELS][MAX_COMP])
 {
-   const int n_vect = 3; /* highest vector number in each microtile */
-   const int n_comp = 3; /* 3 components: R, G, B */
-   unsigned char vec[2 * 2][MAX_COMP]; /* 2 extrema for each sub-block */
-   float b, iv[MAX_COMP]; /* interpolation vector */
-   int i, j, k;
+   const GLint n_vect = 3; /* highest vector number in each microtile */
+   const GLint n_comp = 3; /* 3 components: R, G, B */
+   GLubyte vec[2 * 2][MAX_COMP]; /* 2 extrema for each sub-block */
+   GLfloat b, iv[MAX_COMP]; /* interpolation vector */
+   GLint i, j, k;
    Fx64 hi; /* high quadword */
-   unsigned long lohi, lolo; /* low quadword: hi dword, lo dword */
+   GLuint lohi, lolo; /* low quadword: hi dword, lo dword */
 
-   int minColL = 0, maxColL = 0;
-   int minColR = 0, maxColR = 0;
+   GLint minColL = 0, maxColL = 0;
+   GLint minColR = 0, maxColR = 0;
 #if 0
-   int minSum;
-   int maxSum;
+   GLint minSum;
+   GLint maxSum;
 
    /* Our solution here is to find the darkest and brightest colors in
     * the 4x4 tile and use those as the two representative colors.
     * There are probably better algorithms to use (histogram-based).
     */
-   minSum = 1000; /* big enough */
+   minSum = 2000; /* big enough */
    maxSum = -1; /* small enough */
    for (k = 0; k < N_TEXELS / 2; k++) {
-      int sum = 0;
+      GLint sum = 0;
       for (i = 0; i < n_comp; i++) {
          sum += input[k][i];
       }
@@ -1194,10 +1126,10 @@ fxt1_quantize_MIXED0 (unsigned long *cc,
          maxColL = k;
       }
    }
-   minSum = 1000; /* big enough */
+   minSum = 2000; /* big enough */
    maxSum = -1; /* small enough */
    for (; k < N_TEXELS; k++) {
-      int sum = 0;
+      GLint sum = 0;
       for (i = 0; i < n_comp; i++) {
          sum += input[k][i];
       }
@@ -1211,18 +1143,18 @@ fxt1_quantize_MIXED0 (unsigned long *cc,
       }
    }
 #else
-   int minVal;
-   int maxVal;
-   int maxVarL = fxt1_variance(NULL, input, n_comp, N_TEXELS / 2);
-   int maxVarR = fxt1_variance(NULL, &input[N_TEXELS / 2], n_comp, N_TEXELS / 2);
+   GLint minVal;
+   GLint maxVal;
+   GLint maxVarL = fxt1_variance(NULL, input, n_comp, N_TEXELS / 2);
+   GLint maxVarR = fxt1_variance(NULL, &input[N_TEXELS / 2], n_comp, N_TEXELS / 2);
 
    /* Scan the channel with max variance for lo & hi
     * and use those as the two representative colors.
     */
-   minVal = 1000; /* big enough */
+   minVal = 2000; /* big enough */
    maxVal = -1; /* small enough */
    for (k = 0; k < N_TEXELS / 2; k++) {
-      int t = input[k][maxVarL];
+      GLint t = input[k][maxVarL];
       if (minVal > t) {
          minVal = t;
          minColL = k;
@@ -1232,10 +1164,10 @@ fxt1_quantize_MIXED0 (unsigned long *cc,
          maxColL = k;
       }
    }
-   minVal = 1000; /* big enough */
+   minVal = 2000; /* big enough */
    maxVal = -1; /* small enough */
    for (; k < N_TEXELS; k++) {
-      int t = input[k][maxVarR];
+      GLint t = input[k][maxVarR];
       if (minVal > t) {
          minVal = t;
          minColR = k;
@@ -1255,43 +1187,21 @@ fxt1_quantize_MIXED0 (unsigned long *cc,
    }
    if (minColL != maxColL) {
       /* compute interpolation vector */
-      float d2 = 0;
-      float rd2;
-
-      for (i = 0; i < n_comp; i++) {
-         iv[i] = vec[1][i] - vec[0][i];
-         d2 += iv[i] * iv[i];
-      }
-      rd2 = (float)n_vect / d2;
-      b = 0;
-      for (i = 0; i < n_comp; i++) {
-         b -= iv[i] * vec[0][i];
-         iv[i] *= rd2;
-      }
-      b = b * rd2 + 0.5f;
+      MAKEIVEC(n_vect, n_comp, iv, b, vec[0], vec[1]);
 
       /* add in texels */
       lolo = 0;
       for (k = N_TEXELS / 2 - 1; k >= 0; k--) {
-         int texel;
+         GLint texel;
          /* interpolate color */
-         float dot = 0;
-         for (i = 0; i < n_comp; i++) {
-            dot += input[k][i] * iv[i];
-         }
-         texel = (int)(dot + b);
-         if (texel < 0) {
-            texel = 0;
-         } else if (texel > n_vect) {
-            texel = n_vect;
-         }
+         CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
          /* add in texel */
          lolo <<= 2;
          lolo |= texel;
       }
 
       /* funky encoding for LSB of green */
-      if ((int)((lolo >> 1) & 1) != (((vec[1][GCOMP] ^ vec[0][GCOMP]) >> 2) & 1)) {
+      if ((GLint)((lolo >> 1) & 1) != (((vec[1][GCOMP] ^ vec[0][GCOMP]) >> 2) & 1)) {
          for (i = 0; i < n_comp; i++) {
             vec[1][i] = input[minColL][i];
             vec[0][i] = input[maxColL][i];
@@ -1310,43 +1220,21 @@ fxt1_quantize_MIXED0 (unsigned long *cc,
    }
    if (minColR != maxColR) {
       /* compute interpolation vector */
-      float d2 = 0;
-      float rd2;
-
-      for (i = 0; i < n_comp; i++) {
-         iv[i] = vec[3][i] - vec[2][i];
-         d2 += iv[i] * iv[i];
-      }
-      rd2 = (float)n_vect / d2;
-      b = 0;
-      for (i = 0; i < n_comp; i++) {
-         b -= iv[i] * vec[2][i];
-         iv[i] *= rd2;
-      }
-      b = b * rd2 + 0.5f;
+      MAKEIVEC(n_vect, n_comp, iv, b, vec[2], vec[3]);
 
       /* add in texels */
       lohi = 0;
       for (k = N_TEXELS - 1; k >= N_TEXELS / 2; k--) {
-         int texel;
+         GLint texel;
          /* interpolate color */
-         float dot = 0;
-         for (i = 0; i < n_comp; i++) {
-            dot += input[k][i] * iv[i];
-         }
-         texel = (int)(dot + b);
-         if (texel < 0) {
-            texel = 0;
-         } else if (texel > n_vect) {
-            texel = n_vect;
-         }
+         CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
          /* add in texel */
          lohi <<= 2;
          lohi |= texel;
       }
 
       /* funky encoding for LSB of green */
-      if ((int)((lohi >> 1) & 1) != (((vec[3][GCOMP] ^ vec[2][GCOMP]) >> 2) & 1)) {
+      if ((GLint)((lohi >> 1) & 1) != (((vec[3][GCOMP] ^ vec[2][GCOMP]) >> 2) & 1)) {
          for (i = 0; i < n_comp; i++) {
             vec[3][i] = input[minColR][i];
             vec[2][i] = input[maxColR][i];
@@ -1370,15 +1258,18 @@ fxt1_quantize_MIXED0 (unsigned long *cc,
 
 
 static void
-fxt1_quantize (unsigned long *cc, const unsigned char *lines[], int comps)
+fxt1_quantize (GLuint *cc, const GLubyte *lines[], GLint comps)
 {
-   int trualpha;
-   unsigned char reord[N_TEXELS][MAX_COMP];
+   GLint trualpha;
+   GLubyte reord[N_TEXELS][MAX_COMP];
 
-   unsigned char input[N_TEXELS][MAX_COMP];
-   int i, k, l;
+   GLubyte input[N_TEXELS][MAX_COMP];
+   GLint i, k, l;
 
-   memset(input, -1, sizeof(input));
+   if (comps == 3) {
+      /* make the whole block opaque */
+      memset(input, -1, sizeof(input));
+   }
 
    /* 8 texels each line */
    for (l = 0; l < 4; l++) {
@@ -1441,7 +1332,7 @@ fxt1_quantize (unsigned long *cc, const unsigned char *lines[], int comps)
    if (trualpha) {
       fxt1_quantize_ALPHA1(cc, input);
    } else if (l == 0) {
-      cc[0] = cc[1] = cc[2] = -1;
+      cc[0] = cc[1] = cc[2] = ~0u;
       cc[3] = 0;
    } else if (l < N_TEXELS) {
       fxt1_quantize_MIXED1(cc, input);
@@ -1455,56 +1346,51 @@ fxt1_quantize (unsigned long *cc, const unsigned char *lines[], int comps)
 }
 
 
-int
-fxt1_encode (GLcontext *ctx,
-             unsigned int width, unsigned int height,
-             int srcFormat,
-             const void *source, int srcRowStride,
-             void *dest, int destRowStride)
+static GLint
+fxt1_encode (GLuint width, GLuint height, GLint comps,
+             const void *source, GLint srcRowStride,
+             void *dest, GLint destRowStride)
 {
-   const int comps = (srcFormat == GL_RGB) ? 3 : 4;
-   unsigned int x, y;
-   const unsigned char *data;
-   unsigned long *encoded = dest;
+   GLuint x, y;
+   const GLubyte *data;
+   GLuint *encoded = (GLuint *)dest;
    GLubyte *newSource = NULL;
 
-   (void) ctx;
-
-   /*
-    * Rescale image if width is less than 8 or height is less than 4.
-    */
-   if (width < 8 || height < 4) {
+   /* Replicate image if width is not M8 or height is not M4 */
+   if ((width & 7) | (height & 3)) {
       GLint newWidth = (width + 7) & ~7;
       GLint newHeight = (height + 3) & ~3;
-      newSource = MALLOC(comps * newWidth * newHeight * sizeof(GLchan));
+      newSource = (GLubyte *)
+         _mesa_malloc(comps * newWidth * newHeight * sizeof(GLubyte *));
       _mesa_upscale_teximage2d(width, height, newWidth, newHeight,
-                               comps, source, srcRowStride, newSource);
+                               comps, (const GLchan *) source,
+                               srcRowStride, newSource);
       source = newSource;
       width = newWidth;
       height = newHeight;
       srcRowStride = comps * newWidth;
    }
 
-   data = source;
+   data = (const GLubyte *) source;
    destRowStride = (destRowStride - width * 2) / 4;
    for (y = 0; y < height; y += 4) {
-      unsigned int offs = 0 + (y + 0) * srcRowStride;
+      GLuint offs = 0 + (y + 0) * srcRowStride;
       for (x = 0; x < width; x += 8) {
-         const unsigned char *lines[4];
+         const GLubyte *lines[4];
          lines[0] = &data[offs];
          lines[1] = lines[0] + srcRowStride;
          lines[2] = lines[1] + srcRowStride;
          lines[3] = lines[2] + srcRowStride;
          offs += 8 * comps;
          fxt1_quantize(encoded, lines, comps);
-         /* 128 bits per 8x4 block = 4bpp */
+         /* 128 bits per 8x4 block */
          encoded += 4;
       }
       encoded += destRowStride;
    }
 
    if (newSource != NULL) {
-      FREE(newSource);
+      _mesa_free(newSource);
    }
 
    return 0;
@@ -1520,7 +1406,7 @@ fxt1_encode (GLcontext *ctx,
 
 
 /* lookup table for scaling 5 bit colors up to 8 bits */
-static unsigned char _rgb_scale_5[] = {
+static const GLubyte _rgb_scale_5[] = {
    0,   8,   16,  25,  33,  41,  49,  58,
    66,  74,  82,  90,  99,  107, 115, 123,
    132, 140, 148, 156, 165, 173, 181, 189,
@@ -1528,7 +1414,7 @@ static unsigned char _rgb_scale_5[] = {
 };
 
 /* lookup table for scaling 6 bit colors up to 8 bits */
-static unsigned char _rgb_scale_6[] = {
+static const GLubyte _rgb_scale_6[] = {
    0,   4,   8,   12,  16,  20,  24,  28,
    32,  36,  40,  45,  49,  53,  57,  61,
    65,  69,  73,  77,  81,  85,  89,  93,
@@ -1540,26 +1426,26 @@ static unsigned char _rgb_scale_6[] = {
 };
 
 
-#define CC_SEL(cc, which) ((cc)[(which) / 32] >> ((which) & 31))
+#define CC_SEL(cc, which) (((GLuint *)(cc))[(which) / 32] >> ((which) & 31))
 #define UP5(c) _rgb_scale_5[(c) & 31]
 #define UP6(c, b) _rgb_scale_6[(((c) & 31) << 1) | ((b) & 1)]
 #define LERP(n, t, c0, c1) (((n) - (t)) * (c0) + (t) * (c1) + (n) / 2) / (n)
-#define ZERO_4UBV(v) *((unsigned long *)(v)) = 0
+#define ZERO_4UBV(v) *((GLuint *)(v)) = 0
 
 
 static void
-fxt1_decode_1HI (unsigned long code, int t, unsigned char *rgba)
+fxt1_decode_1HI (const GLubyte *code, GLint t, GLubyte *rgba)
 {
-   const unsigned long *cc;
+   const GLuint *cc;
 
    t *= 3;
-   cc = (unsigned long *)(code + t / 8);
+   cc = (const GLuint *)(code + t / 8);
    t = (cc[0] >> (t & 7)) & 7;
 
    if (t == 7) {
       ZERO_4UBV(rgba);
    } else {
-      cc = (unsigned long *)(code + 12);
+      cc = (const GLuint *)(code + 12);
       if (t == 0) {
          rgba[BCOMP] = UP5(CC_SEL(cc, 0));
          rgba[GCOMP] = UP5(CC_SEL(cc, 5));
@@ -1579,12 +1465,12 @@ fxt1_decode_1HI (unsigned long code, int t, unsigned char *rgba)
 
 
 static void
-fxt1_decode_1CHROMA (unsigned long code, int t, unsigned char *rgba)
+fxt1_decode_1CHROMA (const GLubyte *code, GLint t, GLubyte *rgba)
 {
-   const unsigned long *cc;
-   unsigned long kk;
+   const GLuint *cc;
+   GLuint kk;
 
-   cc = (unsigned long *)code;
+   cc = (const GLuint *)code;
    if (t & 16) {
       cc++;
       t &= 15;
@@ -1592,7 +1478,7 @@ fxt1_decode_1CHROMA (unsigned long code, int t, unsigned char *rgba)
    t = (cc[0] >> (t * 2)) & 3;
 
    t *= 15;
-   cc = (unsigned long *)(code + 8 + t / 8);
+   cc = (const GLuint *)(code + 8 + t / 8);
    kk = cc[0] >> (t & 7);
    rgba[BCOMP] = UP5(kk);
    rgba[GCOMP] = UP5(kk >> 5);
@@ -1602,18 +1488,18 @@ fxt1_decode_1CHROMA (unsigned long code, int t, unsigned char *rgba)
 
 
 static void
-fxt1_decode_1MIXED (unsigned long code, int t, unsigned char *rgba)
+fxt1_decode_1MIXED (const GLubyte *code, GLint t, GLubyte *rgba)
 {
-   const unsigned long *cc;
-   unsigned int col[2][3];
-   int glsb, selb;
+   const GLuint *cc;
+   GLuint col[2][3];
+   GLint glsb, selb;
 
-   cc = (unsigned long *)code;
+   cc = (const GLuint *)code;
    if (t & 16) {
       t &= 15;
       t = (cc[1] >> (t * 2)) & 3;
       /* col 2 */
-      col[0][BCOMP] = (*(unsigned long *)(code + 11)) >> 6;
+      col[0][BCOMP] = (*(const GLuint *)(code + 11)) >> 6;
       col[0][GCOMP] = CC_SEL(cc, 99);
       col[0][RCOMP] = CC_SEL(cc, 104);
       /* col 3 */
@@ -1680,20 +1566,20 @@ fxt1_decode_1MIXED (unsigned long code, int t, unsigned char *rgba)
 
 
 static void
-fxt1_decode_1ALPHA (unsigned long code, int t, unsigned char *rgba)
+fxt1_decode_1ALPHA (const GLubyte *code, GLint t, GLubyte *rgba)
 {
-   const unsigned long *cc;
+   const GLuint *cc;
 
-   cc = (unsigned long *)code;
+   cc = (const GLuint *)code;
    if (CC_SEL(cc, 124) & 1) {
       /* lerp == 1 */
-      unsigned int col0[4];
+      GLuint col0[4];
 
       if (t & 16) {
          t &= 15;
          t = (cc[1] >> (t * 2)) & 3;
          /* col 2 */
-         col0[BCOMP] = (*(unsigned long *)(code + 11)) >> 6;
+         col0[BCOMP] = (*(const GLuint *)(code + 11)) >> 6;
          col0[GCOMP] = CC_SEL(cc, 99);
          col0[RCOMP] = CC_SEL(cc, 104);
          col0[ACOMP] = CC_SEL(cc, 119);
@@ -1734,11 +1620,11 @@ fxt1_decode_1ALPHA (unsigned long code, int t, unsigned char *rgba)
       if (t == 3) {
          ZERO_4UBV(rgba);
       } else {
-         unsigned long kk;
-         cc = (unsigned long *)code;
+         GLuint kk;
+         cc = (const GLuint *)code;
          rgba[ACOMP] = UP5(cc[3] >> (t * 5 + 13));
          t *= 15;
-         cc = (unsigned long *)(code + 8 + t / 8);
+         cc = (const GLuint *)(code + 8 + t / 8);
          kk = cc[0] >> (t & 7);
          rgba[BCOMP] = UP5(kk);
          rgba[GCOMP] = UP5(kk >> 5);
@@ -1749,10 +1635,10 @@ fxt1_decode_1ALPHA (unsigned long code, int t, unsigned char *rgba)
 
 
 void
-fxt1_decode_1 (const void *texture, int width,
-               int i, int j, unsigned char *rgba)
+fxt1_decode_1 (const void *texture, GLint stride, /* in pixels */
+               GLint i, GLint j, GLubyte *rgba)
 {
-   static void (*decode_1[]) (unsigned long, int, unsigned char *) = {
+   static void (*decode_1[]) (const GLubyte *, GLint, GLubyte *) = {
       fxt1_decode_1HI,     /* cc-high   = "00?" */
       fxt1_decode_1HI,     /* cc-high   = "00?" */
       fxt1_decode_1CHROMA, /* cc-chroma = "010" */
@@ -1763,10 +1649,10 @@ fxt1_decode_1 (const void *texture, int width,
       fxt1_decode_1MIXED   /* mixed     = "1??" */
    };
 
-   unsigned long code = (unsigned long)texture +
-                        ((j / 4) * (width / 8) + (i / 8)) * 16;
-   int mode = CC_SEL((unsigned long *)code, 125);
-   int t = i & 7;
+   const GLubyte *code = (const GLubyte *)texture +
+                         ((j / 4) * (stride / 8) + (i / 8)) * 16;
+   GLint mode = CC_SEL(code, 125);
+   GLint t = i & 7;
 
    if (t & 4) {
       t += 12;