return a;
}
+inline int byteidx(int bit)
+{
+ return bit >> 3;
+}
+
+inline int bitidx(int bit)
+{
+ return bit & 7;
+}
+
+inline void setbit(unsigned char *arr, int bit, int v = 1)
+{
+ arr[byteidx(bit)] |= (v << bitidx(bit));
+}
+
+inline void xorbit(unsigned char *arr, int bit, int v = 1)
+{
+ arr[byteidx(bit)] ^= (v << bitidx(bit));
+}
+
+inline int testbit(const unsigned char *arr, int bit, int v = 1)
+{
+ return (arr[byteidx(bit)] & (v << bitidx(bit)));
+}
+
#endif
{
// 6
++bitindex;
- out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
+ setbit(&out[2], bitindex);
++bitindex;
- out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
+ setbit(&out[2], bitindex);
if(alpha_0_is_unimportant<ColorDist>())
visible = false;
}
else if(da[3] <= da[0] && da[3] <= da[1])
{
// 7
- out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
+ setbit(&out[2], bitindex);
++bitindex;
- out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
+ setbit(&out[2], bitindex);
++bitindex;
- out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
+ setbit(&out[2], bitindex);
}
else if(da[0] <= da[1])
{
else
{
// 1
- out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
+ setbit(&out[2], bitindex);
if(refine != REFINE_NEVER)
{
++na1;
if(ColorDist(c[0], c[2]) > ColorDist(c[1], c[2]))
{
int bitindex = pindex * 2;
- out[bitindex / 8 + 12] |= (1 << (bitindex % 8));
+ setbit(&out[12], bitindex);
if(refine != REFINE_NEVER)
{
if(!alpha_0_is_unimportant<ColorDist>() || visible)
case DXT3:
{
int bitindex = pindex * 4;
- out[bitindex / 8 + 0] |= (ca[2] << (bitindex % 8));
+ setbit(&out[0], bitindex, ca[2]);
}
if(ColorDist(c[0], c[2]) > ColorDist(c[1], c[2]))
{
int bitindex = pindex * 2;
- out[bitindex / 8 + 12] |= (1 << (bitindex % 8));
+ setbit(&out[12], bitindex);
if(refine != REFINE_NEVER)
{
if(!alpha_0_is_unimportant<ColorDist>() || ca[2])
// the normalmap-uses-alpha-0 hack cannot be used here
int bitindex = pindex * 2;
if(!ca[2])
- out[bitindex / 8 + 4] |= (3 << (bitindex % 8));
+ setbit(&out[4], bitindex, 3);
else if(ColorDist(c[0], c[2]) > ColorDist(c[1], c[2]))
{
- out[bitindex / 8 + 4] |= (1 << (bitindex % 8));
+ setbit(&out[4], bitindex);
if(refine != REFINE_NEVER)
{
++nc1;
// check ENCODED alpha
int bitindex_0 = pindex * 3;
int bitindex_1 = bitindex_0 + 2;
- if(!(out[bitindex_0 / 8 + 2] & (1 << (bitindex_0 % 8))))
- if(out[bitindex_1 / 8 + 2] & (1 << (bitindex_1 % 8)))
+ if(!testbit(&out[2], bitindex_0))
+ if(testbit(&out[2], bitindex_1))
continue;
}
else
}
}
int bitindex = pindex * 2;
- if(out[bitindex / 8 + (dxt == DXT1 ? 4 : 12)] & (1 << (bitindex % 8)))
+ if(testbit(&out[(dxt == DXT1 ? 4 : 12)], bitindex))
{
// we picked an 1
score_01 += ColorDist(c[1], c[4]);
{
int bitindex_set = pindex * 3;
int bitindex_test = bitindex_set + 2;
- if(!(out[bitindex_test / 8 + 2] & (1 << (bitindex_test % 8))))
- out[bitindex_set / 8 + 2] ^= (1 << (bitindex_set % 8));
+ if(!testbit(&out[2], bitindex_test))
+ xorbit(&out[2], bitindex_set);
}
}
}
#include <stdio.h>
#include <string.h>
#include "s2tc_compressor.h"
+#include "s2tc_common.h"
void fetch_2d_texel_rgb_dxt1(GLint srcRowStride, const GLubyte *pixdata,
GLint i, GLint j, GLvoid *texel)
{
// fetches a single texel (i,j) into pixdata (RGB)
GLubyte *t = (GLubyte *) texel;
- const GLubyte *blksrc = (pixdata + ((srcRowStride + 3) / 4 * (j / 4) + (i / 4)) * 8);
+ const GLubyte *blksrc = (pixdata + (((srcRowStride + 3) >> 2) * (j >> 2) + (i >> 2)) * 8);
unsigned int c = blksrc[0] + 256*blksrc[1];
unsigned int c1 = blksrc[2] + 256*blksrc[3];
- int b = (blksrc[4 + (j % 4)] >> (2 * (i % 4))) & 0x03;
+ int b = (blksrc[4 + (j & 3)] >> (2 * (i & 3))) & 0x03;
switch(b)
{
case 0: break;
{
// fetches a single texel (i,j) into pixdata (RGBA)
GLubyte *t = (GLubyte *) texel;
- const GLubyte *blksrc = (pixdata + ((srcRowStride + 3) / 4 * (j / 4) + (i / 4)) * 8);
+ const GLubyte *blksrc = (pixdata + (((srcRowStride + 3) >> 2) * (j >> 2) + (i >> 2)) * 8);
unsigned int c = blksrc[0] + 256*blksrc[1];
unsigned int c1 = blksrc[2] + 256*blksrc[3];
- int b = (blksrc[4 + (j % 4)] >> (2 * (i % 4))) & 0x03;
+ int b = (blksrc[4 + (j & 3)] >> (2 * (i & 3))) & 0x03;
switch(b)
{
case 0: t[3] = 255; break;
{
// fetches a single texel (i,j) into pixdata (RGBA)
GLubyte *t = (GLubyte *) texel;
- const GLubyte *blksrc = (pixdata + ((srcRowStride + 3) / 4 * (j / 4) + (i / 4)) * 16);
+ const GLubyte *blksrc = (pixdata + (((srcRowStride + 3) >> 2) * (j >> 2) + (i >> 2)) * 16);
unsigned int c = blksrc[8] + 256*blksrc[9];
unsigned int c1 = blksrc[10] + 256*blksrc[11];
- int b = (blksrc[12 + (j % 4)] >> (2 * (i % 4))) & 0x03;
+ int b = (blksrc[12 + (j & 3)] >> (2 * (i & 3))) & 0x03;
switch(b)
{
case 0: break;
t[0] = ((c >> 11) & 0x1F); t[0] = (t[0] << 3) | (t[0] >> 2);
t[1] = ((c >> 5) & 0x3F); t[1] = (t[1] << 2) | (t[1] >> 4);
t[2] = ((c ) & 0x1F); t[2] = (t[2] << 3) | (t[2] >> 2);
- int a = (blksrc[(j % 4) * 2 + (i % 4) / 2] >> (4 * (i % 4) % 2)) & 0x0F;
+ int a = (blksrc[(j & 3) * 2 + ((i & 3) >> 1)] >> (4 * (i & 1))) & 0x0F;
t[3] = a | (a << 4);
}
{
// fetches a single texel (i,j) into pixdata (RGBA)
GLubyte *t = (GLubyte *) texel;
- const GLubyte *blksrc = (pixdata + ((srcRowStride + 3) / 4 * (j / 4) + (i / 4)) * 16);
+ const GLubyte *blksrc = (pixdata + (((srcRowStride + 3) >> 2) * (j >> 2) + (i >> 2)) * 16);
unsigned int c = blksrc[8] + 256*blksrc[9];
unsigned int c1 = blksrc[10] + 256*blksrc[11];
- int b = (blksrc[12 + (j % 4)] >> (2 * (i % 4))) & 0x03;
+ int b = (blksrc[12 + (j & 3)] >> (2 * (i & 3))) & 0x03;
switch(b)
{
case 0: break;
unsigned int a = blksrc[0];
unsigned int a1 = blksrc[1];
- int abit = ((j % 4) * 4 + (i % 4)) * 3;
+ int abit = ((j & 3) * 4 + (i & 3)) * 3;
int ab = 0;
- if(blksrc[(abit / 8) + 2] & (1 << (abit % 8)))
+ if(testbit(&blksrc[2], abit))
ab |= 1;
++abit;
- if(blksrc[(abit / 8) + 2] & (1 << (abit % 8)))
+ if(testbit(&blksrc[2], abit))
ab |= 2;
++abit;
- if(blksrc[(abit / 8) + 2] & (1 << (abit % 8)))
+ if(testbit(&blksrc[2], abit))
ab |= 4;
switch(ab)
{