Commits

ecsv committed 1538e5a

Avoid wrong colors in compressed fxt1 textures

mesa-7.8.1-24878-g3998cfa has better support for fxt1 and can even make the
YUV/ARGB hacks unnecessary. Mesa is now the upstream for this code and
therefore it is a good source when upgrading.

  • Participants
  • Parent commits 753991c

Comments (0)

Files changed (4)

src/GlideHQ/tc-1.1+/fxt1.c

 /*
- * FXT1 codec
- * Version:  1.1
+ * Mesa 3-D graphics library
  *
- * Copyright (C) 2004  Daniel Borca   All Rights Reserved.
+ * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * DANIEL BORCA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
-/* Copyright (C) 2007  Hiroshi Morii <koolsmoky(at)users.sourceforge.net>
- * Added support for ARGB inputs.
+/**
+ * \file texcompress_fxt1.c
+ * GL_3DFX_texture_compression_FXT1 support.
  */
 
 
 #include <stdlib.h>
 #include <string.h>
+#include <assert.h>
 
 #include "types.h"
 #include "internal.h"
 #define LL_RMS_E 255 /* fault tolerance (maximum error) */
 #define ALPHA_TS 2 /* alpha threshold: (255 - ALPHA_TS) deemed opaque */
 #define ISTBLACK(v) (*((dword *)(v)) == 0)
-#define COPY_4UBV(DST, SRC) *((dword *)(DST)) = *((dword *)(SRC))
 
 
 static int
 fxt1_bestcol (float vec[][MAX_COMP], int nv,
-	      byte input[MAX_COMP], int nc)
+              byte input[MAX_COMP], int nc)
 {
-    int i, j, best = -1;
-    float err = 1e9; /* big enough */
+   int i, j, best = -1;
+   float err = 1e9; /* big enough */
 
-    for (j = 0; j < nv; j++) {
-	float e = 0.0F;
-	for (i = 0; i < nc; i++) {
-	    e += (vec[j][i] - input[i]) * (vec[j][i] - input[i]);
-	}
-	if (e < err) {
-	    err = e;
-	    best = j;
-	}
-    }
+   for (j = 0; j < nv; j++) {
+      float e = 0.0F;
+      for (i = 0; i < nc; i++) {
+         e += (vec[j][i] - input[i]) * (vec[j][i] - input[i]);
+      }
+      if (e < err) {
+         err = e;
+         best = j;
+      }
+   }
 
-    return best;
+   return best;
 }
 
 
 static int
 fxt1_worst (float vec[MAX_COMP],
-	    byte input[N_TEXELS][MAX_COMP], int nc, int n)
+            byte input[N_TEXELS][MAX_COMP], int nc, int n)
 {
-    int i, k, worst = -1;
-    float err = -1.0F; /* small enough */
+   int i, k, worst = -1;
+   float err = -1.0F; /* small enough */
 
-    for (k = 0; k < n; k++) {
-	float e = 0.0F;
-	for (i = 0; i < nc; i++) {
-	    e += (vec[i] - input[k][i]) * (vec[i] - input[k][i]);
-	}
-	if (e > err) {
-	    err = e;
-	    worst = k;
-	}
-    }
+   for (k = 0; k < n; k++) {
+      float e = 0.0F;
+      for (i = 0; i < nc; i++) {
+         e += (vec[i] - input[k][i]) * (vec[i] - input[k][i]);
+      }
+      if (e > err) {
+         err = e;
+         worst = k;
+      }
+   }
 
-    return worst;
+   return worst;
 }
 
 
 static int
 fxt1_variance (double variance[MAX_COMP],
-	       byte input[N_TEXELS][MAX_COMP], int nc, int n)
+               byte input[N_TEXELS][MAX_COMP], int nc, int n)
 {
-    int i, k, best = 0;
-    dword sx, sx2;
-    double var, maxvar = -1; /* small enough */
-    double teenth = 1.0 / n;
+   int i, k, best = 0;
+   int sx, sx2;
+   double var, maxvar = -1; /* small enough */
+   double teenth = 1.0 / n;
 
-    for (i = 0; i < nc; i++) {
-	sx = sx2 = 0;
-	for (k = 0; k < n; k++) {
-	    int t = input[k][i];
-	    sx += t;
-	    sx2 += t * t;
-	}
-	var = sx2 * teenth - sx * sx * teenth * teenth;
-	if (maxvar < var) {
-	    maxvar = var;
-	    best = i;
-	}
-	if (variance) {
-	    variance[i] = var;
-	}
-    }
+   for (i = 0; i < nc; i++) {
+      sx = sx2 = 0;
+      for (k = 0; k < n; k++) {
+         int t = input[k][i];
+         sx += t;
+         sx2 += t * t;
+      }
+      var = sx2 * teenth - sx * sx * teenth * teenth;
+      if (maxvar < var) {
+         maxvar = var;
+         best = i;
+      }
+      if (variance) {
+         variance[i] = var;
+      }
+   }
 
-    return best;
+   return best;
 }
 
 
 static int
 fxt1_choose (float vec[][MAX_COMP], int nv,
-	     byte input[N_TEXELS][MAX_COMP], int nc, int n)
+             byte input[N_TEXELS][MAX_COMP], int nc, int n)
 {
 #if 0
-    /* Choose colors from a grid.
-     */
-    int i, j;
+   /* Choose colors from a grid.
+    */
+   int i, j;
 
-    for (j = 0; j < nv; j++) {
-	int m = j * (n - 1) / (nv - 1);
-	for (i = 0; i < nc; i++) {
-	    vec[j][i] = input[m][i];
-	}
-    }
+   for (j = 0; j < nv; j++) {
+      int m = j * (n - 1) / (nv - 1);
+      for (i = 0; i < nc; i++) {
+         vec[j][i] = input[m][i];
+      }
+   }
 #else
-    /* 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).
-     */
-    int i, j, k;
-#ifndef YUV
-    int minSum = 2000; /* big enough */
-#else
-    int minSum = 2000000;
-#endif
-    int maxSum = -1; /* small enough */
-    int minCol = 0; /* phoudoin: silent compiler! */
-    int 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).
+    */
+   int i, j, k;
+   int minSum = 2000; /* big enough */
+   int maxSum = -1; /* small enough */
+   int minCol = 0; /* phoudoin: silent compiler! */
+   int maxCol = 0; /* phoudoin: silent compiler! */
 
-    struct {
-	int flag;
-	dword key;
-	int freq;
-	int idx;
-    } hist[N_TEXELS];
-    int lenh = 0;
+   struct {
+      int flag;
+      int key;
+      int freq;
+      int idx;
+   } hist[N_TEXELS];
+   int lenh = 0;
 
-    memset(hist, 0, sizeof(hist));
+   memset(hist, 0, sizeof(hist));
 
-    for (k = 0; k < n; k++) {
-	int l;
-	dword key = 0;
-	int sum = 0;
-	for (i = 0; i < nc; i++) {
-	    key <<= 8;
-	    key |= input[k][i];
-#ifndef YUV
-	    sum += input[k][i];
-#else
-            /* RGB to YUV conversion according to CCIR 601 specs
-             * Y = 0.299R+0.587G+0.114B
-             * U = 0.713(R - Y) = 0.500R-0.419G-0.081B
-             * V = 0.564(B - Y) = -0.169R-0.331G+0.500B
-             */
-            sum = 299 * input[k][RCOMP] + 587 * input[k][GCOMP] +  114 * input[k][BCOMP];
-#endif
-        }
-	for (l = 0; l < n; l++) {
-	    if (!hist[l].flag) {
-		/* alloc new slot */
-		hist[l].flag = !0;
-		hist[l].key = key;
-		hist[l].freq = 1;
-		hist[l].idx = k;
-		lenh = l + 1;
-		break;
-	    } else if (hist[l].key == key) {
-		hist[l].freq++;
-		break;
-	    }
-	}
-	if (minSum > sum) {
-	    minSum = sum;
-	    minCol = k;
-	}
-	if (maxSum < sum) {
-	    maxSum = sum;
-	    maxCol = k;
-	}
-    }
+   for (k = 0; k < n; k++) {
+      int l;
+      int key = 0;
+      int sum = 0;
+      for (i = 0; i < nc; i++) {
+         key <<= 8;
+         key |= input[k][i];
+         sum += input[k][i];
+      }
+      for (l = 0; l < n; l++) {
+         if (!hist[l].flag) {
+            /* alloc new slot */
+            hist[l].flag = !0;
+            hist[l].key = key;
+            hist[l].freq = 1;
+            hist[l].idx = k;
+            lenh = l + 1;
+            break;
+         } else if (hist[l].key == key) {
+            hist[l].freq++;
+            break;
+         }
+      }
+      if (minSum > sum) {
+         minSum = sum;
+         minCol = k;
+      }
+      if (maxSum < sum) {
+         maxSum = sum;
+         maxCol = k;
+      }
+   }
 
-    if (lenh <= nv) {
-	for (j = 0; j < lenh; j++) {
-	    for (i = 0; i < nc; i++) {
-		vec[j][i] = (float)input[hist[j].idx][i];
-	    }
-	}
-	for (; j < nv; j++) {
-	    for (i = 0; i < nc; i++) {
-		vec[j][i] = vec[0][i];
-	    }
-	}
-	return 0;
-    }
+   if (lenh <= nv) {
+      for (j = 0; j < lenh; j++) {
+         for (i = 0; i < nc; i++) {
+            vec[j][i] = (float)input[hist[j].idx][i];
+         }
+      }
+      for (; j < nv; j++) {
+         for (i = 0; i < nc; i++) {
+            vec[j][i] = vec[0][i];
+         }
+      }
+      return 0;
+   }
 
-    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) / (float)(nv - 1);
-	}
-    }
+   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) / (float)(nv - 1);
+      }
+   }
 #endif
 
-    return !0;
+   return !0;
 }
 
 
 static int
 fxt1_lloyd (float vec[][MAX_COMP], int nv,
-	    byte input[N_TEXELS][MAX_COMP], int nc, int n)
+            byte input[N_TEXELS][MAX_COMP], int nc, int n)
 {
-    /* Use the generalized lloyd's algorithm for VQ:
-     *     find 4 color vectors.
-     *
-     *     for each sample color
-     *         sort to nearest vector.
-     *
-     *     replace each vector with the centroid of it's matching colors.
-     *
-     *     repeat until RMS doesn't improve.
-     *
-     *     if a color vector has no samples, or becomes the same as another
-     *     vector, replace it with the color which is farthest from a sample.
-     *
-     * vec[][MAX_COMP]           initial vectors and resulting colors
-     * nv                        number of resulting colors required
-     * input[N_TEXELS][MAX_COMP] input texels
-     * nc                        number of components in input / vec
-     * n                         number of input samples
-     */
+   /* Use the generalized lloyd's algorithm for VQ:
+    *     find 4 color vectors.
+    *
+    *     for each sample color
+    *         sort to nearest vector.
+    *
+    *     replace each vector with the centroid of its matching colors.
+    *
+    *     repeat until RMS doesn't improve.
+    *
+    *     if a color vector has no samples, or becomes the same as another
+    *     vector, replace it with the color which is farthest from a sample.
+    *
+    * vec[][MAX_COMP]           initial vectors and resulting colors
+    * nv                        number of resulting colors required
+    * input[N_TEXELS][MAX_COMP] input texels
+    * nc                        number of components in input / vec
+    * 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;
+   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;
 
-    int i, j, k, rep;
+   int i, j, k, rep;
 
-    /* the quantizer */
-    for (rep = 0; rep < LL_N_REP; rep++) {
-	/* reset sums & counters */
-	for (j = 0; j < nv; j++) {
-	    for (i = 0; i < nc; i++) {
-		sum[j][i] = 0;
-	    }
-	    cnt[j] = 0;
-	}
-	error = 0;
+   /* the quantizer */
+   for (rep = 0; rep < LL_N_REP; rep++) {
+      /* reset sums & counters */
+      for (j = 0; j < nv; j++) {
+         for (i = 0; i < nc; i++) {
+            sum[j][i] = 0;
+         }
+         cnt[j] = 0;
+      }
+      error = 0;
 
-	/* scan whole block */
-	for (k = 0; k < n; k++) {
+      /* scan whole block */
+      for (k = 0; k < n; k++) {
 #if 1
-	    int best = -1;
-	    float 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]) +
-			  (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) {
-		    e += (vec[j][3] - input[k][3]) * (vec[j][3] - input[k][3]);
-		}
-		if (e < err) {
-		    err = e;
-		    best = j;
-		}
-	    }
+         int best = -1;
+         float 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]) +
+                      (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) {
+               e += (vec[j][3] - input[k][3]) * (vec[j][3] - input[k][3]);
+            }
+            if (e < err) {
+               err = e;
+               best = j;
+            }
+         }
 #else
-	    int best = fxt1_bestcol(vec, nv, input[k], nc, &err);
+         int best = fxt1_bestcol(vec, nv, input[k], nc, &err);
 #endif
-	    /* add in closest color */
-	    for (i = 0; i < nc; i++) {
-		sum[best][i] += input[k][i];
-	    }
-	    /* mark this vector as used */
-	    cnt[best]++;
-	    /* accumulate error */
-	    error += err;
-	}
+         assert(best >= 0);
+         /* add in closest color */
+         for (i = 0; i < nc; i++) {
+            sum[best][i] += input[k][i];
+         }
+         /* mark this vector as used */
+         cnt[best]++;
+         /* accumulate error */
+         error += err;
+      }
 
-	/* check RMS */
-	if ((error < LL_RMS_E) ||
-	    ((error < lasterror) && ((lasterror - error) < LL_RMS_D))) {
-	    return !0; /* good match */
-	}
-	lasterror = error;
+      /* check RMS */
+      if ((error < LL_RMS_E) ||
+          ((error < lasterror) && ((lasterror - error) < LL_RMS_D))) {
+         return !0; /* good match */
+      }
+      lasterror = error;
 
-	/* move each vector to the barycenter of its closest colors */
-	for (j = 0; j < nv; j++) {
-	    if (cnt[j]) {
-		float 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);
-		for (i = 0; i < nc; i++) {
-		    vec[j][i] = input[worst][i];
-		}
-	    }
-	}
-    }
+      /* move each vector to the barycenter of its closest colors */
+      for (j = 0; j < nv; j++) {
+         if (cnt[j]) {
+            float 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);
+            for (i = 0; i < nc; i++) {
+               vec[j][i] = input[worst][i];
+            }
+         }
+      }
+   }
 
-    return 0; /* could not converge fast enough */
+   return 0; /* could not converge fast enough */
 }
 
 
 static void
 fxt1_quantize_CHROMA (dword *cc,
-		      byte input[N_TEXELS][MAX_COMP])
+                      byte 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;
-    qword hi; /* high quadword */
-    dword lohi, lolo; /* low quadword: hi dword, lo dword */
+   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;
+   qword hi; /* high quadword */
+   dword 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);
-    }
+   if (fxt1_choose(vec, n_vect, input, n_comp, N_TEXELS) != 0) {
+      fxt1_lloyd(vec, n_vect, input, n_comp, N_TEXELS);
+   }
 
-    Q_MOV32(hi, 4); /* cc-chroma = "010" + unused bit */
-    for (j = n_vect - 1; j >= 0; j--) {
-	for (i = 0; i < n_comp; i++) {
-	    /* add in colors */
-	    Q_SHL(hi, 5);
-	    Q_OR32(hi, (dword)(vec[j][i] / 8.0F));
-	}
-    }
-    ((qword *)cc)[1] = hi;
+   Q_MOV32(hi, 4); /* cc-chroma = "010" + unused bit */
+   for (j = n_vect - 1; j >= 0; j--) {
+      for (i = 0; i < n_comp; i++) {
+         /* add in colors */
+         Q_SHL(hi, 5);
+         Q_OR32(hi, (dword)(vec[j][i] / 8.0F));
+      }
+   }
+   ((qword *)cc)[1] = hi;
 
-    lohi = lolo = 0;
-    /* right microtile */
-    for (k = N_TEXELS - 1; k >= N_TEXELS / 2; k--) {
-	lohi <<= 2;
-	lohi |= fxt1_bestcol(vec, n_vect, input[k], n_comp);
-    }
-    /* left microtile */
-    for (; k >= 0; k--) {
-	lolo <<= 2;
-	lolo |= fxt1_bestcol(vec, n_vect, input[k], n_comp);
-    }
-    cc[1] = lohi;
-    cc[0] = lolo;
+   lohi = lolo = 0;
+   /* right microtile */
+   for (k = N_TEXELS - 1; k >= N_TEXELS/2; k--) {
+      lohi <<= 2;
+      lohi |= fxt1_bestcol(vec, n_vect, input[k], n_comp);
+   }
+   /* left microtile */
+   for (; k >= 0; k--) {
+      lolo <<= 2;
+      lolo |= fxt1_bestcol(vec, n_vect, input[k], n_comp);
+   }
+   cc[1] = lohi;
+   cc[0] = lolo;
 }
 
 
 static void
 fxt1_quantize_ALPHA0 (dword *cc,
-		      byte input[N_TEXELS][MAX_COMP],
-		      byte reord[N_TEXELS][MAX_COMP], int n)
+                      byte input[N_TEXELS][MAX_COMP],
+                      byte reord[N_TEXELS][MAX_COMP], int 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;
-    qword hi; /* high quadword */
-    dword lohi, lolo; /* low quadword: hi dword, lo dword */
+   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;
+   qword hi; /* high quadword */
+   dword lohi, lolo; /* low quadword: hi dword, lo dword */
 
-    /* the last vector indicates zero */
-    for (i = 0; i < n_comp; i++) {
-	vec[n_vect][i] = 0;
-    }
+   /* the last vector indicates zero */
+   for (i = 0; i < n_comp; i++) {
+      vec[n_vect][i] = 0;
+   }
 
-    /* the first n texels in reord are guaranteed to be non-zero */
-    if (fxt1_choose(vec, n_vect, reord, n_comp, n) != 0) {
-	fxt1_lloyd(vec, n_vect, reord, n_comp, n);
-    }
+   /* the first n texels in reord are guaranteed to be non-zero */
+   if (fxt1_choose(vec, n_vect, reord, n_comp, n) != 0) {
+      fxt1_lloyd(vec, n_vect, reord, n_comp, n);
+   }
 
-    Q_MOV32(hi, 6); /* alpha = "011" + lerp = 0 */
-    for (j = n_vect - 1; j >= 0; j--) {
-	/* add in alphas */
-	Q_SHL(hi, 5);
-	Q_OR32(hi, (dword)(vec[j][ACOMP] / 8.0F));
-    }
-    for (j = n_vect - 1; j >= 0; j--) {
-	for (i = 0; i < n_comp - 1; i++) {
-	    /* add in colors */
-	    Q_SHL(hi, 5);
-	    Q_OR32(hi, (dword)(vec[j][i] / 8.0F));
-	}
-    }
-    ((qword *)cc)[1] = hi;
+   Q_MOV32(hi, 6); /* alpha = "011" + lerp = 0 */
+   for (j = n_vect - 1; j >= 0; j--) {
+      /* add in alphas */
+      Q_SHL(hi, 5);
+      Q_OR32(hi, (dword)(vec[j][ACOMP] / 8.0F));
+   }
+   for (j = n_vect - 1; j >= 0; j--) {
+      for (i = 0; i < n_comp - 1; i++) {
+         /* add in colors */
+         Q_SHL(hi, 5);
+         Q_OR32(hi, (dword)(vec[j][i] / 8.0F));
+      }
+   }
+   ((qword *)cc)[1] = hi;
 
-    lohi = lolo = 0;
-    /* right microtile */
-    for (k = N_TEXELS - 1; k >= N_TEXELS / 2; k--) {
-	lohi <<= 2;
-	lohi |= fxt1_bestcol(vec, n_vect + 1, input[k], n_comp);
-    }
-    /* left microtile */
-    for (; k >= 0; k--) {
-	lolo <<= 2;
-	lolo |= fxt1_bestcol(vec, n_vect + 1, input[k], n_comp);
-    }
-    cc[1] = lohi;
-    cc[0] = lolo;
+   lohi = lolo = 0;
+   /* right microtile */
+   for (k = N_TEXELS - 1; k >= N_TEXELS/2; k--) {
+      lohi <<= 2;
+      lohi |= fxt1_bestcol(vec, n_vect + 1, input[k], n_comp);
+   }
+   /* left microtile */
+   for (; k >= 0; k--) {
+      lolo <<= 2;
+      lolo |= fxt1_bestcol(vec, n_vect + 1, input[k], n_comp);
+   }
+   cc[1] = lohi;
+   cc[0] = lolo;
 }
 
 
 static void
 fxt1_quantize_ALPHA1 (dword *cc,
-		      byte input[N_TEXELS][MAX_COMP])
+                      byte 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;
-    qword hi; /* high quadword */
-    dword lohi, lolo; /* low quadword: hi dword, lo dword */
+   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;
+   qword hi; /* high quadword */
+   dword 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;
+   int minSum;
+   int maxSum;
+   int minColL = 0, maxColL = 0;
+   int minColR = 0, maxColR = 0;
+   int sumL = 0, sumR = 0;
+   int nn_comp;
+   /* 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).
+    */
+   nn_comp = n_comp;
+   while ((minColL == maxColL) && nn_comp) {
+       minSum = 2000; /* big enough */
+       maxSum = -1; /* small enough */
+       for (k = 0; k < N_TEXELS / 2; k++) {
+           int sum = 0;
+           for (i = 0; i < nn_comp; i++) {
+               sum += input[k][i];
+           }
+           if (minSum > sum) {
+               minSum = sum;
+               minColL = k;
+           }
+           if (maxSum < sum) {
+               maxSum = sum;
+               maxColL = k;
+           }
+           sumL += sum;
+       }
+       
+       nn_comp--;
+   }
 
-    /* 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).
-     */
-#ifndef YUV
-    minSum = 2000; /* big enough */
-#else
-    minSum = 2000000;
-#endif
-    maxSum = -1; /* small enough */
-    for (k = 0; k < N_TEXELS / 2; k++) {
-	int sum = 0;
-#ifndef YUV
-	for (i = 0; i < n_comp; i++) {
-	    sum += input[k][i];
-	}
-#else
-        sum = 299 * input[k][RCOMP] + 587 * input[k][GCOMP] +  114 * input[k][BCOMP];
-#endif
-	if (minSum > sum) {
-	    minSum = sum;
-	    minColL = k;
-	}
-	if (maxSum < sum) {
-	    maxSum = sum;
-	    maxColL = k;
-	}
-	sumL += sum;
-    }
-#ifndef YUV
-    minSum = 2000; /* big enough */
-#else
-    minSum = 2000000;
-#endif
-    maxSum = -1; /* small enough */
-    for (; k < N_TEXELS; k++) {
-	int sum = 0;
-#ifndef YUV
-	for (i = 0; i < n_comp; i++) {
-	    sum += input[k][i];
-	}
-#else
-        sum = 299 * input[k][RCOMP] + 587 * input[k][GCOMP] +  114 * input[k][BCOMP];
-#endif
-	if (minSum > sum) {
-	    minSum = sum;
-	    minColR = k;
-	}
-	if (maxSum < sum) {
-	    maxSum = sum;
-	    maxColR = k;
-	}
-	sumR += sum;
-    }
+   nn_comp = n_comp;
+   while ((minColR == maxColR) && nn_comp) {
+       minSum = 2000; /* big enough */
+       maxSum = -1; /* small enough */
+       for (k = N_TEXELS / 2; k < N_TEXELS; k++) {
+           int sum = 0;
+           for (i = 0; i < nn_comp; i++) {
+               sum += input[k][i];
+           }
+           if (minSum > sum) {
+               minSum = sum;
+               minColR = k;
+           }
+           if (maxSum < sum) {
+               maxSum = sum;
+               maxColR = k;
+           }
+           sumR += sum;
+       }
 
-    /* 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.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];
-	}
-    }
+       nn_comp--;
+   }
 
-    /* left microtile */
-    cc[0] = 0;
-    if (minColL != maxColL) {
-	/* compute interpolation vector */
-	MAKEIVEC(n_vect, n_comp, iv, b, vec[0], vec[1]);
+   /* 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.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];
+      }
+   }
 
-	/* add in texels */
-	lolo = 0;
-	for (k = N_TEXELS / 2 - 1; k >= 0; k--) {
-	    int texel;
-	    /* interpolate color */
-	    CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
-	    /* add in texel */
-	    lolo <<= 2;
-	    lolo |= texel;
-	}
+   /* left microtile */
+   cc[0] = 0;
+   if (minColL != maxColL) {
+      /* compute interpolation vector */
+      MAKEIVEC(n_vect, n_comp, iv, b, vec[0], vec[1]);
 
-	cc[0] = lolo;
-    }
+      /* add in texels */
+      lolo = 0;
+      for (k = N_TEXELS / 2 - 1; k >= 0; k--) {
+         int texel;
+         /* interpolate color */
+         CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
+         /* add in texel */
+         lolo <<= 2;
+         lolo |= texel;
+      }
+      
+      cc[0] = lolo;
+   }
 
-    /* right microtile */
-    cc[1] = 0;
-    if (minColR != maxColR) {
-	/* compute interpolation vector */
-	MAKEIVEC(n_vect, n_comp, iv, b, vec[2], vec[1]);
+   /* right microtile */
+   cc[1] = 0;
+   if (minColR != maxColR) {
+      /* compute interpolation vector */
+      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;
-	    /* interpolate color */
-	    CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
-	    /* add in texel */
-	    lohi <<= 2;
-	    lohi |= texel;
-	}
+      /* add in texels */
+      lohi = 0;
+      for (k = N_TEXELS - 1; k >= N_TEXELS / 2; k--) {
+         int texel;
+         /* interpolate color */
+         CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
+         /* add in texel */
+         lohi <<= 2;
+         lohi |= texel;
+      }
 
-	cc[1] = lohi;
-    }
+      cc[1] = lohi;
+   }
 
-    Q_MOV32(hi, 7); /* alpha = "011" + lerp = 1 */
-    for (j = n_vect - 1; j >= 0; j--) {
-	/* add in alphas */
-	Q_SHL(hi, 5);
-	Q_OR32(hi, (dword)(vec[j][ACOMP] / 8.0F));
-    }
-    for (j = n_vect - 1; j >= 0; j--) {
-	for (i = 0; i < n_comp - 1; i++) {
-	    /* add in colors */
-	    Q_SHL(hi, 5);
-	    Q_OR32(hi, (dword)(vec[j][i] / 8.0F));
-	}
-    }
-    ((qword *)cc)[1] = hi;
+   Q_MOV32(hi, 7); /* alpha = "011" + lerp = 1 */
+   for (j = n_vect - 1; j >= 0; j--) {
+      /* add in alphas */
+      Q_SHL(hi, 5);
+      Q_OR32(hi, (dword)(vec[j][ACOMP] / 8.0F));
+   }
+   for (j = n_vect - 1; j >= 0; j--) {
+      for (i = 0; i < n_comp - 1; i++) {
+         /* add in colors */
+         Q_SHL(hi, 5);
+         Q_OR32(hi, (dword)(vec[j][i] / 8.0F));
+      }
+   }
+   ((qword *)cc)[1] = hi;
 }
 
 
 static void
 fxt1_quantize_HI (dword *cc,
-		  byte input[N_TEXELS][MAX_COMP],
-		  byte reord[N_TEXELS][MAX_COMP], int n)
+                  byte input[N_TEXELS][MAX_COMP],
+                  byte reord[N_TEXELS][MAX_COMP], int n)
 {
-    const int n_vect = 6; /* highest vector number */
-    const int n_comp = 3; /* 3 components: R, G, B */
-    float b = 0.0F;       /* phoudoin: silent compiler! */
-    float iv[MAX_COMP];   /* interpolation vector */
-    int i, k;
-    dword hihi; /* high quadword: hi dword */
+   const int n_vect = 6; /* highest vector number */
+   const int n_comp = 3; /* 3 components: R, G, B */
+   float b = 0.0F;       /* phoudoin: silent compiler! */
+   float iv[MAX_COMP];   /* interpolation vector */
+   int i, k;
+   dword hihi; /* high quadword: hi dword */
 
-#ifndef YUV
-    int minSum = 2000; /* big enough */
-#else
-    int minSum = 2000000;
-#endif
-    int maxSum = -1; /* small enough */
-    int minCol = 0; /* phoudoin: silent compiler! */
-    int maxCol = 0; /* phoudoin: silent compiler! */
+   int minSum = 2000; /* big enough */
+   int maxSum = -1; /* small enough */
+   int minCol = 0; /* phoudoin: silent compiler! */
+   int 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;
-#ifndef YUV
-	for (i = 0; i < n_comp; i++) {
-	    sum += reord[k][i];
-	}
-#else
-        sum = 299 * input[k][RCOMP] + 587 * input[k][GCOMP] +  114 * input[k][BCOMP];
-#endif
-	if (minSum > sum) {
-	    minSum = sum;
-	    minCol = k;
-	}
-	if (maxSum < sum) {
-	    maxSum = sum;
-	    maxCol = k;
-	}
-    }
+   /* 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;
+      for (i = 0; i < n_comp; i++) {
+         sum += reord[k][i];
+      }
+      if (minSum > sum) {
+         minSum = sum;
+         minCol = k;
+      }
+      if (maxSum < sum) {
+         maxSum = sum;
+         maxCol = k;
+      }
+   }
 
-    hihi = 0; /* cc-hi = "00" */
-    for (i = 0; i < n_comp; i++) {
-	/* add in colors */
-	hihi <<= 5;
-	hihi |= reord[maxCol][i] >> 3;
-    }
-    for (i = 0; i < n_comp; i++) {
-	/* add in colors */
-	hihi <<= 5;
-	hihi |= reord[minCol][i] >> 3;
-    }
-    cc[3] = hihi;
-    cc[0] = cc[1] = cc[2] = 0;
+   hihi = 0; /* cc-hi = "00" */
+   for (i = 0; i < n_comp; i++) {
+      /* add in colors */
+      hihi <<= 5;
+      hihi |= reord[maxCol][i] >> 3;
+   }
+   for (i = 0; i < n_comp; i++) {
+      /* add in colors */
+      hihi <<= 5;
+      hihi |= reord[minCol][i] >> 3;
+   }
+   cc[3] = hihi;
+   cc[0] = cc[1] = cc[2] = 0;
 
-    /* compute interpolation vector */
-    if (minCol != maxCol) {
-	MAKEIVEC(n_vect, n_comp, iv, b, reord[minCol], reord[maxCol]);
-    }
+   /* compute interpolation vector */
+   if (minCol != maxCol) {
+      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;
-	dword *kk = (dword *)((byte *)cc + t / 8);
-	int texel = n_vect + 1; /* transparent black */
+   /* add in texels */
+   for (k = N_TEXELS - 1; k >= 0; k--) {
+      int t = k * 3;
+      dword *kk = (dword *)((char *)cc + t / 8);
+      int texel = n_vect + 1; /* transparent black */
 
-	if (!ISTBLACK(input[k])) {
-	    if (minCol != maxCol) {
-		/* interpolate color */
-		CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
-		/* add in texel */
-		kk[0] |= texel << (t & 7);
-	    }
-	} else {
-	    /* add in texel */
-	    kk[0] |= texel << (t & 7);
-	}
-    }
+      if (!ISTBLACK(input[k])) {
+         if (minCol != maxCol) {
+            /* interpolate color */
+            CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
+            /* add in texel */
+            kk[0] |= texel << (t & 7);
+         }
+      } else {
+         /* add in texel */
+         kk[0] |= texel << (t & 7);
+      }
+   }
 }
 
 
 static void
 fxt1_quantize_MIXED1 (dword *cc,
-		      byte input[N_TEXELS][MAX_COMP])
+                      byte 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 */
-    byte vec[2 * 2][MAX_COMP]; /* 2 extrema for each sub-block */
-    float b, iv[MAX_COMP]; /* interpolation vector */
-    int i, j, k;
-    qword hi; /* high quadword */
-    dword lohi, lolo; /* low quadword: hi dword, lo dword */
+   const int n_vect = 2; /* highest vector number in each microtile */
+   const int n_comp = 3; /* 3 components: R, G, B */
+   byte vec[2 * 2][MAX_COMP]; /* 2 extrema for each sub-block */
+   float b, iv[MAX_COMP]; /* interpolation vector */
+   int i, j, k;
+   qword hi; /* high quadword */
+   dword lohi, lolo; /* low quadword: hi dword, lo dword */
 
-    int minSum;
-    int maxSum;
-    int minColL = 0, maxColL = -1;
-    int minColR = 0, maxColR = -1;
+   int minSum;
+   int maxSum;
+   int minColL = 0, maxColL = -1;
+   int 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).
-     */
-#ifndef YUV
-    minSum = 2000; /* big enough */
-#else
-    minSum = 2000000;
-#endif
-    maxSum = -1; /* small enough */
-    for (k = 0; k < N_TEXELS / 2; k++) {
-	if (!ISTBLACK(input[k])) {
-	    int sum = 0;
-#ifndef YUV
-	    for (i = 0; i < n_comp; i++) {
-		sum += input[k][i];
-	    }
-#else
-            sum = 299 * input[k][RCOMP] + 587 * input[k][GCOMP] +  114 * input[k][BCOMP];
-#endif
-	    if (minSum > sum) {
-		minSum = sum;
-		minColL = k;
-	    }
-	    if (maxSum < sum) {
-		maxSum = sum;
-		maxColL = k;
-	    }
-	}
-    }
-#ifndef YUV
-    minSum = 2000; /* big enough */
-#else
-    minSum = 2000000;
-#endif
-    maxSum = -1; /* small enough */
-    for (; k < N_TEXELS; k++) {
-	if (!ISTBLACK(input[k])) {
-	    int sum = 0;
-#ifndef YUV
-	    for (i = 0; i < n_comp; i++) {
-		sum += input[k][i];
-	    }
-#else
-            sum = 299 * input[k][RCOMP] + 587 * input[k][GCOMP] +  114 * input[k][BCOMP];
-#endif
-	    if (minSum > sum) {
-		minSum = sum;
-		minColR = k;
-	    }
-	    if (maxSum < sum) {
-		maxSum = sum;
-		maxColR = k;
-	    }
-	}
-    }
+   /* 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 = 2000; /* big enough */
+   maxSum = -1; /* small enough */
+   for (k = 0; k < N_TEXELS / 2; k++) {
+      if (!ISTBLACK(input[k])) {
+         int sum = 0;
+         for (i = 0; i < n_comp; i++) {
+            sum += input[k][i];
+         }
+         if (minSum > sum) {
+            minSum = sum;
+            minColL = k;
+         }
+         if (maxSum < sum) {
+            maxSum = sum;
+            maxColL = k;
+         }
+      }
+   }
+   minSum = 2000; /* big enough */
+   maxSum = -1; /* small enough */
+   for (; k < N_TEXELS; k++) {
+      if (!ISTBLACK(input[k])) {
+         int sum = 0;
+         for (i = 0; i < n_comp; i++) {
+            sum += input[k][i];
+         }
+         if (minSum > sum) {
+            minSum = sum;
+            minColR = k;
+         }
+         if (maxSum < sum) {
+            maxSum = sum;
+            maxColR = k;
+         }
+      }
+   }
 
-    /* left microtile */
-    if (maxColL == -1) {
-	/* all transparent black */
-	cc[0] = 0xFFFFFFFF;
-	for (i = 0; i < n_comp; i++) {
-	    vec[0][i] = 0;
-	    vec[1][i] = 0;
-	}
-    } else {
-	cc[0] = 0;
-	for (i = 0; i < n_comp; i++) {
-	    vec[0][i] = input[minColL][i];
-	    vec[1][i] = input[maxColL][i];
-	}
-	if (minColL != maxColL) {
-	    /* compute interpolation vector */
-	    MAKEIVEC(n_vect, n_comp, iv, b, vec[0], vec[1]);
+   /* left microtile */
+   if (maxColL == -1) {
+      /* all transparent black */
+      cc[0] = ~0u;
+      for (i = 0; i < n_comp; i++) {
+         vec[0][i] = 0;
+         vec[1][i] = 0;
+      }
+   } else {
+      cc[0] = 0;
+      for (i = 0; i < n_comp; i++) {
+         vec[0][i] = input[minColL][i];
+         vec[1][i] = input[maxColL][i];
+      }
+      if (minColL != maxColL) {
+         /* compute interpolation vector */
+         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 */
-		if (!ISTBLACK(input[k])) {
-		    /* interpolate color */
-		    CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
-		}
-		/* add in texel */
-		lolo <<= 2;
-		lolo |= texel;
-	    }
-	    cc[0] = lolo;
-	}
-    }
+         /* add in texels */
+         lolo = 0;
+         for (k = N_TEXELS / 2 - 1; k >= 0; k--) {
+            int texel = n_vect + 1; /* transparent black */
+            if (!ISTBLACK(input[k])) {
+               /* interpolate color */
+               CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
+            }
+            /* add in texel */
+            lolo <<= 2;
+            lolo |= texel;
+         }
+         cc[0] = lolo;
+      }
+   }
 
-    /* right microtile */
-    if (maxColR == -1) {
-	/* all transparent black */
-	cc[1] = 0xFFFFFFFF;
-	for (i = 0; i < n_comp; i++) {
-	    vec[2][i] = 0;
-	    vec[3][i] = 0;
-	}
-    } else {
-	cc[1] = 0;
-	for (i = 0; i < n_comp; i++) {
-	    vec[2][i] = input[minColR][i];
-	    vec[3][i] = input[maxColR][i];
-	}
-	if (minColR != maxColR) {
-	    /* compute interpolation vector */
-	    MAKEIVEC(n_vect, n_comp, iv, b, vec[2], vec[3]);
+   /* right microtile */
+   if (maxColR == -1) {
+      /* all transparent black */
+      cc[1] = ~0u;
+      for (i = 0; i < n_comp; i++) {
+         vec[2][i] = 0;
+         vec[3][i] = 0;
+      }
+   } else {
+      cc[1] = 0;
+      for (i = 0; i < n_comp; i++) {
+         vec[2][i] = input[minColR][i];
+         vec[3][i] = input[maxColR][i];
+      }
+      if (minColR != maxColR) {
+         /* compute interpolation vector */
+         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 */
-		if (!ISTBLACK(input[k])) {
-		    /* interpolate color */
-		    CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
-		}
-		/* add in texel */
-		lohi <<= 2;
-		lohi |= texel;
-	    }
-	    cc[1] = lohi;
-	}
-    }
+         /* add in texels */
+         lohi = 0;
+         for (k = N_TEXELS - 1; k >= N_TEXELS / 2; k--) {
+            int texel = n_vect + 1; /* transparent black */
+            if (!ISTBLACK(input[k])) {
+               /* interpolate color */
+               CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
+            }
+            /* add in texel */
+            lohi <<= 2;
+            lohi |= texel;
+         }
+         cc[1] = lohi;
+      }
+   }
 
-    Q_MOV32(hi, 9 | (vec[3][GCOMP] & 4) | ((vec[1][GCOMP] >> 1) & 2)); /* chroma = "1" */
-    for (j = 2 * 2 - 1; j >= 0; j--) {
-	for (i = 0; i < n_comp; i++) {
-	    /* add in colors */
-	    Q_SHL(hi, 5);
-	    Q_OR32(hi, vec[j][i] >> 3);
-	}
-    }
-    ((qword *)cc)[1] = hi;
+   Q_MOV32(hi, 9 | (vec[3][GCOMP] & 4) | ((vec[1][GCOMP] >> 1) & 2)); /* chroma = "1" */
+   for (j = 2 * 2 - 1; j >= 0; j--) {
+      for (i = 0; i < n_comp; i++) {
+         /* add in colors */
+         Q_SHL(hi, 5);
+         Q_OR32(hi, vec[j][i] >> 3);
+      }
+   }
+   ((qword *)cc)[1] = hi;
 }
 
 
 static void
 fxt1_quantize_MIXED0 (dword *cc,
-		      byte input[N_TEXELS][MAX_COMP])
+                      byte 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 */
-    byte vec[2 * 2][MAX_COMP]; /* 2 extrema for each sub-block */
-    float b, iv[MAX_COMP]; /* interpolation vector */
-    int i, j, k;
-    qword hi; /* high quadword */
-    dword lohi, lolo; /* low quadword: hi dword, lo dword */
+   const int n_vect = 3; /* highest vector number in each microtile */
+   const int n_comp = 3; /* 3 components: R, G, B */
+   byte vec[2 * 2][MAX_COMP]; /* 2 extrema for each sub-block */
+   float b, iv[MAX_COMP]; /* interpolation vector */
+   int i, j, k;
+   qword hi; /* high quadword */
+   dword lohi, lolo; /* low quadword: hi dword, lo dword */
 
-    int minColL = 0, maxColL = 0;
-    int minColR = 0, maxColR = 0;
+   int minColL = 0, maxColL = 0;
+   int minColR = 0, maxColR = 0;
 #if 0
-    int minSum;
-    int maxSum;
+   int minSum;
+   int 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).
-     */
-#ifndef YUV
-    minSum = 2000; /* big enough */
+   /* 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 = 2000; /* big enough */
+   maxSum = -1; /* small enough */
+   for (k = 0; k < N_TEXELS / 2; k++) {
+      int sum = 0;
+      for (i = 0; i < n_comp; i++) {
+         sum += input[k][i];
+      }
+      if (minSum > sum) {
+         minSum = sum;
+         minColL = k;
+      }
+      if (maxSum < sum) {
+         maxSum = sum;
+         maxColL = k;
+      }
+   }
+   minSum = 2000; /* big enough */
+   maxSum = -1; /* small enough */
+   for (; k < N_TEXELS; k++) {
+      int sum = 0;
+      for (i = 0; i < n_comp; i++) {
+         sum += input[k][i];
+      }
+      if (minSum > sum) {
+         minSum = sum;
+         minColR = k;
+      }
+      if (maxSum < sum) {
+         maxSum = sum;
+         maxColR = k;
+      }
+   }
 #else
-    minSum = 2000000;
-#endif
-    maxSum = -1; /* small enough */
-    for (k = 0; k < N_TEXELS / 2; k++) {
-	int sum = 0;
-#ifndef YUV
-	for (i = 0; i < n_comp; i++) {
-	    sum += input[k][i];
-	}
-#else
-        sum = 299 * input[k][RCOMP] + 587 * input[k][GCOMP] +  114 * input[k][BCOMP];
-#endif
-	if (minSum > sum) {
-	    minSum = sum;
-	    minColL = k;
-	}
-	if (maxSum < sum) {
-	    maxSum = sum;
-	    maxColL = k;
-	}
-    }
-    minSum = 2000; /* big enough */
-    maxSum = -1; /* small enough */
-    for (; k < N_TEXELS; k++) {
-	int sum = 0;
-#ifndef YUV
-	for (i = 0; i < n_comp; i++) {
-	    sum += input[k][i];
-	}
-#else
-        sum = 299 * input[k][RCOMP] + 587 * input[k][GCOMP] +  114 * input[k][BCOMP];
-#endif
-	if (minSum > sum) {
-	    minSum = sum;
-	    minColR = k;
-	}
-	if (maxSum < sum) {
-	    maxSum = sum;
-	    maxColR = k;
-	}
-    }
-#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);
+   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);
 
-    /* Scan the channel with max variance for lo & hi
-     * and use those as the two representative colors.
-     */
-    minVal = 2000; /* big enough */
-    maxVal = -1; /* small enough */
-    for (k = 0; k < N_TEXELS / 2; k++) {
-	int t = input[k][maxVarL];
-	if (minVal > t) {
-	    minVal = t;
-	    minColL = k;
-	}
-	if (maxVal < t) {
-	    maxVal = t;
-	    maxColL = k;
-	}
-    }
-    minVal = 2000; /* big enough */
-    maxVal = -1; /* small enough */
-    for (; k < N_TEXELS; k++) {
-	int t = input[k][maxVarR];
-	if (minVal > t) {
-	    minVal = t;
-	    minColR = k;
-	}
-	if (maxVal < t) {
-	    maxVal = t;
-	    maxColR = k;
-	}
-    }
+   /* Scan the channel with max variance for lo & hi
+    * and use those as the two representative colors.
+    */
+   minVal = 2000; /* big enough */
+   maxVal = -1; /* small enough */
+   for (k = 0; k < N_TEXELS / 2; k++) {
+      int t = input[k][maxVarL];
+      if (minVal > t) {
+         minVal = t;
+         minColL = k;
+      }
+      if (maxVal < t) {
+         maxVal = t;
+         maxColL = k;
+      }
+   }
+   minVal = 2000; /* big enough */
+   maxVal = -1; /* small enough */
+   for (; k < N_TEXELS; k++) {
+      int t = input[k][maxVarR];
+      if (minVal > t) {
+         minVal = t;
+         minColR = k;
+      }
+      if (maxVal < t) {
+         maxVal = t;
+         maxColR = k;
+      }
+   }
 #endif
 
-    /* left microtile */
-    cc[0] = 0;
-    for (i = 0; i < n_comp; i++) {
-	vec[0][i] = input[minColL][i];
-	vec[1][i] = input[maxColL][i];
-    }
-    if (minColL != maxColL) {
-	/* compute interpolation vector */
-	MAKEIVEC(n_vect, n_comp, iv, b, vec[0], vec[1]);
+   /* left microtile */
+   cc[0] = 0;
+   for (i = 0; i < n_comp; i++) {
+      vec[0][i] = input[minColL][i];
+      vec[1][i] = input[maxColL][i];
+   }
+   if (minColL != maxColL) {
+      /* compute interpolation vector */
+      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;
-	    /* interpolate color */
-	    CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
-	    /* add in texel */
-	    lolo <<= 2;
-	    lolo |= texel;
-	}
+      /* add in texels */
+      lolo = 0;
+      for (k = N_TEXELS / 2 - 1; k >= 0; k--) {
+         int texel;
+         /* interpolate color */
+         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)) {
-	    for (i = 0; i < n_comp; i++) {
-		vec[1][i] = input[minColL][i];
-		vec[0][i] = input[maxColL][i];
-	    }
-	    lolo = ~lolo;
-	}
+      /* funky encoding for LSB of green */
+      if ((int)((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];
+         }
+         lolo = ~lolo;
+      }
+      
+      cc[0] = lolo;
+   }
 
-	cc[0] = lolo;
-    }
+   /* right microtile */
+   cc[1] = 0;
+   for (i = 0; i < n_comp; i++) {
+      vec[2][i] = input[minColR][i];
+      vec[3][i] = input[maxColR][i];
+   }
+   if (minColR != maxColR) {
+      /* compute interpolation vector */
+      MAKEIVEC(n_vect, n_comp, iv, b, vec[2], vec[3]);
 
-    /* right microtile */
-    cc[1] = 0;
-    for (i = 0; i < n_comp; i++) {
-	vec[2][i] = input[minColR][i];
-	vec[3][i] = input[maxColR][i];
-    }
-    if (minColR != maxColR) {
-	/* compute interpolation vector */
-	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;
+         /* interpolate color */
+         CALCCDOT(texel, n_vect, n_comp, iv, b, input[k]);
+         /* add in texel */
+         lohi <<= 2;
+         lohi |= texel;
+      }
 
-	/* add in texels */
-	lohi = 0;
-	for (k = N_TEXELS - 1; k >= N_TEXELS / 2; k--) {
-	    int texel;
-	    /* interpolate color */
-	    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)) {
+         for (i = 0; i < n_comp; i++) {
+            vec[3][i] = input[minColR][i];
+            vec[2][i] = input[maxColR][i];
+         }
+         lohi = ~lohi;
+      }
 
-	/* funky encoding for LSB of green */
-	if ((int)((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];
-	    }
-	    lohi = ~lohi;
-	}
+      cc[1] = lohi;
+   }
 
-	cc[1] = lohi;
-    }
-
-    Q_MOV32(hi, 8 | (vec[3][GCOMP] & 4) | ((vec[1][GCOMP] >> 1) & 2)); /* chroma = "1" */
-    for (j = 2 * 2 - 1; j >= 0; j--) {
-	for (i = 0; i < n_comp; i++) {
-	    /* add in colors */
-	    Q_SHL(hi, 5);
-	    Q_OR32(hi, vec[j][i] >> 3);
-	}
-    }
-    ((qword *)cc)[1] = hi;
+   Q_MOV32(hi, 8 | (vec[3][GCOMP] & 4) | ((vec[1][GCOMP] >> 1) & 2)); /* chroma = "1" */
+   for (j = 2 * 2 - 1; j >= 0; j--) {
+      for (i = 0; i < n_comp; i++) {
+         /* add in colors */
+         Q_SHL(hi, 5);
+         Q_OR32(hi, vec[j][i] >> 3);
+      }
+   }
+   ((qword *)cc)[1] = hi;
 }
 
 
 static void
 fxt1_quantize (dword *cc, const byte *lines[], int comps)
 {
-    int trualpha;
-    byte reord[N_TEXELS][MAX_COMP];
+   int trualpha;
+   byte reord[N_TEXELS][MAX_COMP];
 
-    byte input[N_TEXELS][MAX_COMP];
-#ifndef ARGB
-    int i;
-#endif
-    int k, l;
+   byte input[N_TEXELS][MAX_COMP];
+   int i, k, l;
 
-    if (comps == 3) {
-	/* make the whole block opaque */
-	memset(input, -1, sizeof(input));
-    }
+   if (comps == 3) {
+      /* make the whole block opaque */
+      memset(input, -1, sizeof(input));
+   }
 
-    /* 8 texels each line */
-#ifndef ARGB
-    for (l = 0; l < 4; l++) {
-	for (k = 0; k < 4; k++) {
-	    for (i = 0; i < comps; i++) {
-		input[k + l * 4][i] = *lines[l]++;
-	    }
-	}
-	for (; k < 8; k++) {
-	    for (i = 0; i < comps; i++) {
-		input[k + l * 4 + 12][i] = *lines[l]++;
-	    }
-	}
-    }
-#else
-    /* H.Morii - support for ARGB inputs */
-    for (l = 0; l < 4; l++) {
-	for (k = 0; k < 4; k++) {
-          input[k + l * 4][2] = *lines[l]++;
-          input[k + l * 4][1] = *lines[l]++;
-          input[k + l * 4][0] = *lines[l]++;
-          if (comps == 4) input[k + l * 4][3] = *lines[l]++;
-	}
-	for (; k < 8; k++) {
-          input[k + l * 4 + 12][2] = *lines[l]++;
-          input[k + l * 4 + 12][1] = *lines[l]++;
-          input[k + l * 4 + 12][0] = *lines[l]++;
-          if (comps == 4) input[k + l * 4 + 12][3] = *lines[l]++;
-	}
-    }
-#endif
+   /* 8 texels each line */
+   for (l = 0; l < 4; l++) {
+      for (k = 0; k < 4; k++) {
+         for (i = 0; i < comps; i++) {
+            input[k + l * 4][i] = *lines[l]++;
+         }
+      }
+      for (; k < 8; k++) {
+         for (i = 0; i < comps; i++) {
+            input[k + l * 4 + 12][i] = *lines[l]++;
+         }
+      }
+   }
 
-    /* block layout:
-     * 00, 01, 02, 03, 08, 09, 0a, 0b
-     * 10, 11, 12, 13, 18, 19, 1a, 1b
-     * 04, 05, 06, 07, 0c, 0d, 0e, 0f
-     * 14, 15, 16, 17, 1c, 1d, 1e, 1f
-     */
+   /* block layout:
+    * 00, 01, 02, 03, 08, 09, 0a, 0b
+    * 10, 11, 12, 13, 18, 19, 1a, 1b
+    * 04, 05, 06, 07, 0c, 0d, 0e, 0f
+    * 14, 15, 16, 17, 1c, 1d, 1e, 1f
+    */
 
-    /* [dBorca]
-     * stupidity flows forth from this
-     */
-    l = N_TEXELS;
-    trualpha = 0;
-    if (comps == 4) {
-	/* skip all transparent black texels */
-	l = 0;
-	for (k = 0; k < N_TEXELS; k++) {
-	    /* test all components against 0 */
-	    if (!ISTBLACK(input[k])) {
-		/* texel is not transparent black */
-		COPY_4UBV(reord[l], input[k]);
-		if (reord[l][ACOMP] < (255 - ALPHA_TS)) {
-		    /* non-opaque texel */
-		    trualpha = !0;
-		}
-		l++;
-	    }
-	}
-    }
+   /* [dBorca]
+    * stupidity flows forth from this
+    */
+   l = N_TEXELS;
+   trualpha = 0;
+   if (comps == 4) {
+      /* skip all transparent black texels */
+      l = 0;
+      for (k = 0; k < N_TEXELS; k++) {
+         /* test all components against 0 */
+         if (!ISTBLACK(input[k])) {
+            /* texel is not transparent black */
+            COPY_4UBV(reord[l], input[k]);
+            if (reord[l][ACOMP] < (255 - ALPHA_TS)) {
+               /* non-opaque texel */
+               trualpha = !0;
+            }
+            l++;
+         }
+      }
+   }
 
 #if 0
-    if (trualpha) {
-	fxt1_quantize_ALPHA0(cc, input, reord, l);
-    } else if (l == 0) {
-	cc[0] = cc[1] = cc[2] = -1;
-	cc[3] = 0;
-    } else if (l < N_TEXELS) {
-	fxt1_quantize_HI(cc, input, reord, l);
-    } else {
-	fxt1_quantize_CHROMA(cc, input);
-    }
-    (void)fxt1_quantize_ALPHA1;
-    (void)fxt1_quantize_MIXED1;
-    (void)fxt1_quantize_MIXED0;
+   if (trualpha) {
+      fxt1_quantize_ALPHA0(cc, input, reord, l);
+   } else if (l == 0) {
+      cc[0] = cc[1] = cc[2] = -1;
+      cc[3] = 0;
+   } else if (l < N_TEXELS) {
+      fxt1_quantize_HI(cc, input, reord, l);
+   } else {
+      fxt1_quantize_CHROMA(cc, input);
+   }
+   (void)fxt1_quantize_ALPHA1;
+   (void)fxt1_quantize_MIXED1;
+   (void)fxt1_quantize_MIXED0;
 #else
-    if (trualpha) {
-	fxt1_quantize_ALPHA1(cc, input);
-    } else if (l == 0) {
-	cc[0] = cc[1] = cc[2] = 0xFFFFFFFF;
-	cc[3] = 0;
-    } else if (l < N_TEXELS) {
-	fxt1_quantize_MIXED1(cc, input);
-    } else {
-	fxt1_quantize_MIXED0(cc, input);
-    }
-    (void)fxt1_quantize_ALPHA0;
-    (void)fxt1_quantize_HI;
-    (void)fxt1_quantize_CHROMA;
+   if (trualpha) {
+      fxt1_quantize_ALPHA1(cc, input);
+   } else if (l == 0) {
+      cc[0] = cc[1] = cc[2] = ~0u;
+      cc[3] = 0;
+   } else if (l < N_TEXELS) {
+      fxt1_quantize_MIXED1(cc, input);
+   } else {
+      fxt1_quantize_MIXED0(cc, input);
+   }
+   (void)fxt1_quantize_ALPHA0;
+   (void)fxt1_quantize_HI;
+   (void)fxt1_quantize_CHROMA;
 #endif
 }
 
 
-TAPI int TAPIENTRY
-fxt1_encode (int width, int height, int comps,
-	     const void *source, int srcRowStride,
-	     void *dest, int destRowStride)
+
+/**
+ * Upscale an image by replication, not (typical) stretching.
+ * We use this when the image width or height is less than a
+ * certain size (4, 8) and we need to upscale an image.
+ */
+static void
+upscale_teximage2d(int inWidth, int inHeight,
+                   int outWidth, int outHeight,
+                   int comps, const byte *src, int srcRowStride,
+                   byte *dest )
 {
-    int x, y;
-    const byte *data;
-    dword *encoded = (dword *)dest;
-    void *newSource = NULL;
+   int i, j, k;
 
-    /* Replicate image if width is not M8 or height is not M4 */
-    if ((width & 7) | (height & 3)) {
-	int newWidth = (width + 7) & ~7;
-	int newHeight = (height + 3) & ~3;
-	newSource = malloc(comps * newWidth * newHeight * sizeof(byte *));
-	_mesa_upscale_teximage2d(width, height, newWidth, newHeight,
-				 comps, (const byte *)source,
-				 srcRowStride, (byte *)newSource);
-	source = newSource;
-	width = newWidth;
-	height = newHeight;
-	srcRowStride = comps * newWidth;
-    }
+   assert(outWidth >= inWidth);
+   assert(outHeight >= inHeight);
+#if 0
+   ASSERT(inWidth == 1 || inWidth == 2 || inHeight == 1 || inHeight == 2);
+   ASSERT((outWidth & 3) == 0);
+   ASSERT((outHeight & 3) == 0);
+#endif
 
-    data = (const byte *)source;
-    destRowStride = (destRowStride - width * 2) / 4;
-    for (y = 0; y < height; y += 4) {
-	unsigned int offs = 0 + (y + 0) * srcRowStride;
-	for (x = 0; x < width; x += 8) {
-	    const byte *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 */
-	    encoded += 4;
-	}
-	encoded += destRowStride;
-    }
+   for (i = 0; i < outHeight; i++) {
+      const int ii = i % inHeight;
+      for (j = 0; j < outWidth; j++) {
+         const int jj = j % inWidth;
+         for (k = 0; k < comps; k++) {
+            dest[(i * outWidth + j) * comps + k]
+               = src[ii * srcRowStride + jj * comps + k];
+         }
+      }
+   }
+}
 
-    if (newSource != NULL) {
-	free(newSource);
-    }
+TAPI void TAPIENTRY
+fxt1_encode (dword width, dword height, int comps,
+             const void *source, int srcRowStride,
+             void *dest, int destRowStride)
+{
+   dword x, y;
+   const byte *data;
+   dword *encoded = (dword *)dest;
+   void *newSource = NULL, *newSourcetmp = NULL;
 
-    return 0;
+   assert(comps == 3 || comps == 4);
+
+   if (comps == 3)
+       newSource = reorder_source_3_alloc(source, width, height, srcRowStride);
+   if (comps == 4)
+       newSource = reorder_source_4_alloc(source, width, height, srcRowStride);
+   if (!newSource)
+       goto cleanUp;
+   source = newSource;
+
+   /* Replicate image if width is not M8 or height is not M4 */
+   if ((width & 7) | (height & 3)) {
+      int newWidth = (width + 7) & ~7;
+      int newHeight = (height + 3) & ~3;
+      newSourcetmp = malloc(comps * newWidth * newHeight * sizeof(byte));
+      free(newSource);
+      newSource = newSourcetmp;
+      if (!newSource) {
+         goto cleanUp;
+      }
+      upscale_teximage2d(width, height, newWidth, newHeight,
+                         comps, (const byte *) source,
+                         srcRowStride, (byte *) newSource);
+      source = newSource;
+      width = newWidth;
+      height = newHeight;
+      srcRowStride = comps * newWidth;
+   }
+
+   data = (const byte *) source;
+   destRowStride = (destRowStride - width * 2) / 4;
+   for (y = 0; y < height; y += 4) {
+      dword offs = 0 + (y + 0) * srcRowStride;
+      for (x = 0; x < width; x += 8) {
+         const byte *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 */
+         encoded += 4;
+      }
+      encoded += destRowStride;
+   }
+
+ cleanUp:
+   free(newSource);
 }
 
 
 
 /* lookup table for scaling 5 bit colors up to 8 bits */
 static const byte _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,
-    197, 206, 214, 222, 230, 239, 247, 255
+   0,   8,   16,  25,  33,  41,  49,  58,
+   66,  74,  82,  90,  99,  107, 115, 123,
+   132, 140, 148, 156, 165, 173, 181, 189,
+   197, 206, 214, 222, 230, 239, 247, 255
 };
 
 /* lookup table for scaling 6 bit colors up to 8 bits */
 static const byte _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,
-    97,  101, 105, 109, 113, 117, 121, 125,
-    130, 134, 138, 142, 146, 150, 154, 158,
-    162, 166, 170, 174, 178, 182, 186, 190,
-    194, 198, 202, 206, 210, 215, 219, 223,
-    227, 231, 235, 239, 243, 247, 251, 255
+   0,   4,   8,   12,  16,  20,  24,  28,
+   32,  36,  40,  45,  49,  53,  57,  61,
+   65,  69,  73,  77,  81,  85,  89,  93,