class TestFilter extends RGBImageFilter {
public int filterRGB(int x, int y, int pixel) { //0xffffffff
float redfilteredPixel;
int restPixel;
redfilteredPixel = (pixel & 0x00ff0000); //red pixel
restPixel = (pixel & 0xff00ffff); //"restpixel"
redfilteredPixel *= 1.1f;
int redpixel = (int) redfilteredPixel & 0x00ff0000;
return (redpixel | restPixel);
}
}
It is for a school project, but I should only change the middle part of the method.
The way you wrote this pretty much works already. The problem is what happens when the result is too big to fit in the 8 bits you have for the red field.
You can just add a check after you convert back to int, like this:
int redpixel = (int) redfilteredPixel & 0x00ff0000;
if (redpixel > 0x00FF0000)
redpixel = 0x00FF0000;
That will work, but it's a little unusual. Normally code for doing this would not convert to float.
Also, it would be easier to understand if you converted the red value all the way to an int in [0,255], but it isn't necessary to do that in this case (+10% works the same either way), and usually when you're writing low-level pixel code like this it's better to do it the fast way.