We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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);
Answers
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
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
ok, thanks
} input_.updatePixels(); image(input_, 0, 0);
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):
Edit: Just realized r, g, and b can be calculated in parallel: