2014-01-14 50 views
0

我有下面的代碼RGB565轉換爲RGB888,反之亦然:RGB888到RGB565,反之亦然導致信息丟失?

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); 
} 

問題是在第二線時,它被轉換回RGB888我得到的色彩信息的丟失。任何人都知道更多關於移位和掩蔽的信息能幫助我嗎?

在此先感謝!

+1

你檢查http://stackoverflow.com/questions/2442576/how-does-one-convert-16-bit-rgb565-to-24-bit-rgb888? –

+0

當然有損失。爲什麼你會期望沒有損失? –

+0

是的,我並沒有真正幫助。我知道應該會有損失,但我不認爲會有那麼多損失。 –

回答

0

這行不對?

final int g = (((aPixel) & 0x07E0) >>> 2) & 0xFF; 

應該是:

final int g = (((aPixel) & 0x07E0) >>> 3) & 0xFF; 

我的眼睛你的代碼的其餘部分看起來OK。我不知道這是否解釋它。否則,你可能不得不根據什麼測試來定義「那麼多的損失」。

+0

嗯,不會導致它返回更多不正確的輸出。 –

+0

那麼,這條線是錯的,對吧?該值爲+5移位,因此您需要將其移至-3以從8位字節的頂部開始。你沒有說過問題是什麼。哪些輸出與預期輸出不匹配。 –