
simple quantizing
I'm trying to do scaled quantizing on a 8 bit image.
I get input int:bits, which is 18 and the intensity of every pixel, 0255.
I need to scale and quantize it.
Example:
bits = 1 // = 2 colors
pixlesin[0] = 129
// will make the out to 255
pixlesout[0] = 255 // while 129 is closer to 255 than 0
bits = 2 // = 4 colors
pixlesin[1] = 24
// will make the out to 0
pixlesout[1] = 0 // while 24 is closer to 0 than 85,170,255
and so on...
how can i make a nice formula for this? (without if and switch..)

Code:
unsigned char out, mask;
#define BITS 8
mask = ~0 << ( BITS bits );
out = pixlesin[ n ] & mask;
I haven't tested this, so it may be wrong but this may give you an idéa at least.