Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

"do i need to write down all 256 8-bit combinations and find out manually which ones correspond to uniform? "

well yes, you're supposed to .

but maybe we can shortcut it for now, here's a precomputed one for 8 neighbours:

const char lookup[256] = {
0, 1, 2, 3, 4, 58, 5, 6, 7, 58, 58, 58, 8, 58, 9, 10,
11, 58, 58, 58, 58, 58, 58, 58, 12, 58, 58, 58, 13, 58, 14, 15,
16, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
17, 58, 58, 58, 58, 58, 58, 58, 18, 58, 58, 58, 19, 58, 20, 21,
22, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
23, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
24, 58, 58, 58, 58, 58, 58, 58, 25, 58, 58, 58, 26, 58, 27, 28,
29, 30, 58, 31, 58, 58, 58, 32, 58, 58, 58, 58, 58, 58, 58, 33,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 34,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 35,
36, 37, 58, 38, 58, 58, 58, 39, 58, 58, 58, 58, 58, 58, 58, 40,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 41,
42, 43, 58, 44, 58, 58, 58, 45, 58, 58, 58, 58, 58, 58, 58, 46,
47, 48, 58, 49, 58, 58, 58, 50, 51, 52, 58, 53, 54, 55, 56, 57 };

and the code, that produced it:

#include <stdio.h>
#include <stdlib.h>
bool bit(unsigned b, unsigned i)
{
    return ((b & (1 << i)) != 0);
}

int main(int argc, char **argv)
{
    printf("const char lookup[256] = {");
    int numUniforms = 0;
    int numNeighbours = 8;
    int numSlots = 1 << numNeighbours;
    for ( int i=0; i<numSlots; i++ )
    {
        if ( i%16 == 0 ) printf("\n");

        int transitions = 0;
        for ( int j=0; j<numNeighbours-1; j++ )
            transitions += (bit(i,j) != bit(i,j+1));
        transitions += (bit(i,numNeighbours-1) != bit(i,0));

        if ( transitions <= 2 )
            printf("%d ",numUniforms++);
        else
            printf("58 ");
        if ( i<numSlots-1 ) 
            printf(",");
    }
    printf("};\n");
    return 0;
}