I have the following code to convert RGB565 to RGB888 and vice versa:
public static void main(String[] args) {
// TODO code application logic here
System.out.println(Integer.toHexString(RGB888ToRGB565(0x11ffffff)));
System.out.println(Integer.toHexString(RGB565ToRGB888(RGB888ToRGB565(0x7FC9FF))));
}
static int RGB888ToRGB565(int red, int green, int blue) {
final int B = (blue >>> 3) & 0x001F;
final int G = ((green >>> 2) << 5) & 0x07E0;
final int R = ((red >>> 3) << 11) & 0xF800;
return (R | G | B);
}
static int RGB888ToRGB565(int aPixel) {
//aPixel <<= 8;
//System.out.println(Integer.toHexString(aPixel));
final int red = (aPixel >> 16) & 0xFF;
final int green = (aPixel >> 8) & 0xFF;
final int blue = (aPixel) & 0xFF;
return RGB888ToRGB565(red, green, blue);
}
static int RGB565ToRGB888(int aPixel) {
final int b = (((aPixel) & 0x001F) << 3) & 0xFF;
final int g = (((aPixel) & 0x07E0) >>> 2) & 0xFF;
final int r = (((aPixel) & 0xF800) >>> 8) & 0xFF;
// return RGBA
return 0x000000ff | (r << 24) | (g << 16) | (b << 8);
}
Problem is in the second line when it gets transformed back to rgb888 I get a loss of color information. Can anyone who knows more about bitshifting and masking help me out?
Thanks in advance!