mesa/format_utils: Add a function to convert a mesa_format to an array format
[mesa.git] / src / mesa / main / texcompress_rgtc_tmp.h
index 9377a6bd3bee2a5db7ea9d0c237c2bc7ec885e81..5fa9de630b6f7ff9ce399124ecc270bc7977a25e 100644 (file)
 
 /* included by texcompress_rgtc to define byte/ubyte compressors */
 
+static void TAG(fetch_texel_rgtc)(unsigned srcRowStride, const TYPE *pixdata,
+                                 unsigned i, unsigned j, TYPE *value, unsigned comps)
+{
+   TYPE decode;
+   const TYPE *blksrc = (pixdata + ((srcRowStride + 3) / 4 * (j / 4) + (i / 4)) * 8 * comps);
+   const TYPE alpha0 = blksrc[0];
+   const TYPE alpha1 = blksrc[1];
+   const char bit_pos = ((j&3) * 4 + (i&3)) * 3;
+   const unsigned char acodelow = blksrc[2 + bit_pos / 8];
+   const unsigned char acodehigh = (3 + bit_pos / 8) < 8 ? blksrc[3 + bit_pos / 8] : 0;
+   const unsigned char code = (acodelow >> (bit_pos & 0x7) |
+      (acodehigh  << (8 - (bit_pos & 0x7)))) & 0x7;
+
+   if (code == 0)
+      decode = alpha0;
+   else if (code == 1)
+      decode = alpha1;
+   else if (alpha0 > alpha1)
+      decode = ((alpha0 * (8 - code) + (alpha1 * (code - 1))) / 7);
+   else if (code < 6)
+      decode = ((alpha0 * (6 - code) + (alpha1 * (code - 1))) / 5);
+   else if (code == 6)
+      decode = T_MIN;
+   else
+      decode = T_MAX;
+
+   *value = decode;
+}
+
 static void TAG(write_rgtc_encoded_channel)(TYPE *blkaddr,
                                            TYPE alphabase1,
                                            TYPE alphabase2,
@@ -43,26 +72,26 @@ static void TAG(write_rgtc_encoded_channel)(TYPE *blkaddr,
    *blkaddr++ = (alphaenc[10] >> 2) | (alphaenc[11] << 1) | (alphaenc[12] << 4) | ((alphaenc[13] & 1) << 7);
    *blkaddr++ = (alphaenc[13] >> 1) | (alphaenc[14] << 2) | (alphaenc[15] << 5);
 }
-static void TAG(encode_rgtc_chan)(TYPE *blkaddr, TYPE srccolors[4][4],
-                            GLint numxpixels, GLint numypixels)
+
+static void TAG(encode_rgtc_ubyte)(TYPE *blkaddr, TYPE srccolors[4][4],
+                            int numxpixels, int numypixels)
 {
    TYPE alphabase[2], alphause[2];
-   GLshort alphatest[2] = { 0 };
-   GLuint alphablockerror1, alphablockerror2, alphablockerror3;
+   short alphatest[2] = { 0 };
+   unsigned int alphablockerror1, alphablockerror2, alphablockerror3;
    TYPE i, j, aindex, acutValues[7];
    TYPE alphaenc1[16], alphaenc2[16], alphaenc3[16];
-   GLboolean alphaabsmin = GL_FALSE;
-   GLboolean alphaabsmax = GL_FALSE;
-   GLshort alphadist;
+   int alphaabsmin = 0, alphaabsmax = 0;
+   short alphadist;
 
    /* find lowest and highest alpha value in block, alphabase[0] lowest, alphabase[1] highest */
    alphabase[0] = T_MAX; alphabase[1] = T_MIN;
    for (j = 0; j < numypixels; j++) {
       for (i = 0; i < numxpixels; i++) {
         if (srccolors[j][i] == T_MIN)
-            alphaabsmin = GL_TRUE;
+            alphaabsmin = 1;
          else if (srccolors[j][i] == T_MAX)
-            alphaabsmax = GL_TRUE;
+            alphaabsmax = 1;
          else {
             if (srccolors[j][i] > alphabase[1])
                alphabase[1] = srccolors[j][i];
@@ -152,7 +181,7 @@ static void TAG(encode_rgtc_chan)(TYPE *blkaddr, TYPE srccolors[4][4],
       fprintf(stderr, "%d ", alphaenc1[i]);
    }
    fprintf(stderr, "cutVals ");
-   for (i = 0; i < 8; i++) {
+   for (i = 0; i < 7; i++) {
       fprintf(stderr, "%d ", acutValues[i]);
    }
    fprintf(stderr, "srcVals ");
@@ -219,8 +248,8 @@ static void TAG(encode_rgtc_chan)(TYPE *blkaddr, TYPE srccolors[4][4],
       /* skip this if the error is already very small
          this encoding is MUCH better on average than #2 though, but expensive! */
       if ((alphablockerror2 > 96) && (alphablockerror1 > 96)) {
-         GLshort blockerrlin1 = 0;
-         GLshort blockerrlin2 = 0;
+         short blockerrlin1 = 0;
+         short blockerrlin2 = 0;
          TYPE nralphainrangelow = 0;
          TYPE nralphainrangehigh = 0;
          alphatest[0] = T_MAX;