#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# how convert image to image 3 bits, 8 colors???

edited December 2015
``````input_.loadPixels();
for(int r=0;r<58;r++){
for(int x=0 ;x< 58;x++){

int inputIndex =  x + r * input_.width;
int rr = (input_.pixels[inputIndex] >> 16) & 240;
int v = (input_.pixels[inputIndex] >> 8) & 240;
int b = input_.pixels[inputIndex] & 240;

input_.pixels[inputIndex] = color(rr, v, b);
}
}
input_.updatePixels();
image(input_, 0, 0);
``````
Tagged:

• edited December 2015

For each pixel:

• Extract red, green, and blue. Each of these will have a value between 0 and 255

• If red is greater than 127 then make it 255. Otherwise make it 0

• Do the same for green and blue. Because each color channel only has two possible values that is 2 * 2 * 2 == 8 colors

On a side note, I am not sure why you are using a bitwise AND of 240 instead of 0xff (which is 255)

• & 240 will leave 3 most significant bits

• edited December 2015

Are you talking about an indexed palette?

• 240 is the top 4 bits... 0xF0, you want 0xE0 or 224

but you can do the bitmask of the entire integer in one go by using 0xFFE0E0E0

• https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text

``````// forum.processing.org/two/discussion/13986/
// how-convert-image-to-image-3-bits-8-colors

// GoToLoop (2015-Dec-15)

static final byte get3BitColor(color c) {
color r = c >> 025 & 4;
color g = c >> 016 & 2;
color b = c >> 7   & 1;
return (byte) (r | g | b);
}

void setup() {
for (int i = 0; i != 10; ++i) {
color c = (color) random(#000000);
byte  b = get3BitColor(c);
println(hex(c, 6), b, binary(b, 3));
}
exit();
}
``````
• ok, thanks

input_.loadPixels(); for(int r=0;r<58;r++){ for(int x=0 ;x< 58;x++){

``````    int inputIndex =  x + r * input_.width;
int rr = (input_.pixels[inputIndex] >> 16) & 128;
int v = (input_.pixels[inputIndex] >> 8) & 128;
int b = input_.pixels[inputIndex] & 128;

input_.pixels[inputIndex] = color(rr, v, b);
}
``````

} input_.updatePixels(); image(input_, 0, 0);

• edited December 2015 Answer ✓

That value & 128 was clever, no need for comparison with 127 (greater or less)

Ended up playing around with a bitwise only solution, figured I'd share but it has been a long time since I played with pixels[] so I am not sure if it is any faster than color(r, g, b) (and used to C / C++ bitwise syntax lately, I guess Java does not like logical NOT being used with an int):

``````void setup() {
size(100, 100);
int randomColor = 0xff000000 | (int)random(0xffffff);
int resultColor = toThreeBitColor(randomColor);
println(hex(randomColor) + ", " + hex(resultColor));
}

void draw() {
}

void mousePressed() {
int randomColor = 0xff000000 | (int)random(0xffffff);
int resultColor = toThreeBitColor(randomColor);
println(hex(randomColor) + ", " + hex(resultColor));
}

int toThreeBitColor(int randomColor) {
int r, g, b;

r = (randomColor >> 16) & 128;
r = r | (r >> 1);
r = r | (r >> 2);
r = r | (r >> 4);

g = (randomColor >> 8) & 128;
g = g | (g >> 1);
g = g | (g >> 2);
g = g | (g >> 4);

b = randomColor & 128;
b = b | (b >> 1);
b = b | (b >> 2);
b = b | (b >> 4);

return 0xff000000 | (r << 16) | (g << 8) | b;
}
``````

Edit: Just realized r, g, and b can be calculated in parallel:

``````int toThreeBitColor(int randomColor) {
int rgb;

rgb = (randomColor & 0x800000) |
(randomColor & 0x8000) |
(randomColor & 0x80);

rgb = rgb | (rgb >> 1);
rgb = rgb | (rgb >> 2);
rgb = rgb | (rgb >> 4);

return 0xff000000 | rgb;
}
``````