how to implement bitwise color codin in blackberry
i am new to blackberry.Currently i am working upon bitmap color coding
The code which i am implement for changing colors of image are as follows: Bitmap greyScale = new Bitmap(original.getWidth(), original.getHeight());
int[] argb = new int[original.getWidth() * original.getHeight()];
original.getARGB(argb, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
for (int i = argb.length - 1; i >= 0; --i)
{ int alpha = argb[i] >> 24; int red = argb[i] >> 16 & 0xFF; int green = argb[i] >> 8 & 0xFF; int blue = 255-argb[i] & 0xFF;
int grey = (int) (0.3 * red + 0.59 * green + 0.11 * blue);
int red2 = red * contrast/10+brightness;
if (red2>0xFF) red2 = 0xFF;
if (red2<0) red2 = 0;
int green2 = green * contrast/10+brightness;
if (green2>0xFF) green2 = 0xFF;
if (green2<0) green2 = 0;
int blue2 = blue * contrast/10+brightness;
if (blue2>0xFF) blue2 = 0xFF;
if (blue2<0) blue2 = 0;
int composite = (alpha << 24) | (red2 << 16) | (green2 << 8) | blue2|red|green;
argb[i] = composite;
}
greyScale.setARGB(argb, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
return greyScale;
}
I am capable of implementing this method and开发者_如何学运维 capable of obtaining various colors also. But can anyone help me that how Argb is working on the entire code for colors.
regards Pinkesh Gupta
Thanks, I am using your code with some modifications. To answer your question,
Each byte in argb is a 32-bit value representing one pixel in 0xAARRGGBB format:
- 8 bits alpha (transparency): 0 for transparent and 255 for opaque
- 8 bits red
- 8 bits green
- 8 bits blue
So 0xFFFFFFFF would be opaque white.
The code to extract the values works as follows: To get the apha value, shift everything right by 24 places leaving only the first 8 bits
int alpha = argb[i] >> 24
To get red, shift right by 16, leaving the top 16 bits. Then blank out the top 8 bits using the AND operator. This leaves the second 8 bits
int red = argb[i] >> 16 & 0xFF
For green, shift by 8 bits and blank out first 16 bits, leaving the third 8 bits
int green = argb[i] >> 8 & 0xFF
And finally for blue, just blank out the top 24 bits
int blue = argb[i] & 0xFF;
At this point you can make changes to the alpha,red,green and blue values. I am using
int grey = ( red + green + blue ) / 3;
red = grey;
green = grey;
red = grey;
To get a grayscale version of the bitmap.
Finally, to make up a new value, shift every part to the correct place and superimpose them (which happens to be the same as adding them in this case)
int newval = (alpha << 24) | (red << 16) | (green << 8) | blue;
Then set argb[i] = newval
to replace the pixel.
精彩评论