2012-11-16 77 views
-3

我怎麼能轉換BGRA緩衝區,緩衝區RGBA格式C++如何將BGRA緩衝區轉換爲RGBA緩衝區格式?

void ConvertBetweenBGRAandRGBA(unsigned char* input, int pixel_width,int pixel_hight, 
          unsigned char* output) { 

     for (int y = 0; y < pixel_hight; y++) { 
     for (int x = 0; x < pixel_width; x++) { 
     const unsigned char* pixel_in = &input[y * x * 4]; 

     unsigned char* pixel_out = &output[y * x * 4]; 
     pixel_out[0] = pixel_in[2]; 
     pixel_out[1] = pixel_in[1]; 
     pixel_out[2] = pixel_in[0]; 
     pixel_out[3] = pixel_in[3]; 
    } 
} 

,但我沒有得到背景顏色。

請幫助我嗎?

+0

如果你沒有獲得背景顏色,那你到底在做什麼?給你一個例子的價值和你獲得的價值。你有沒有證實你的循環是做你想做的? –

回答

0

這不是C#,所以請重新標記。

假設這是位圖數據,首先,您需要計算圖像的步幅。步幅是每行像素使用的字節數。這並不總是等於bytes_per_pixel * pixels_per_row。它通常對齊到四個字節,所以在這種情況下(因爲ARGB像素使用每個像素四個字節),你應該沒問題。其次,你得到像素(x,y)地址的公式是錯誤的。像素以行優先順序存儲。這意味着,從像素緩衝區中的偏移量0開始,您將看到一行完整的像素數據;然後是另一個完整的行;等等。每行像素數據都有一個完整的字節步長。

可以做到這一點:

const unsigned char* pixel_in = &input[((y * pixel_width) + x) * 4]; 

但是,如果你的步伐確實等於圖像寬度,你不需要每次都計算出地址,因爲像素將按順序存儲:

void ConvertBetweenBGRAandRGBA(unsigned char* input, int pixel_width, 
    int pixel_height, unsigned char* output) 
{ 
    int offset = 0; 

    for (int y = 0; y < pixel_height; y++) { 
     for (int x = 0; x < pixel_width; x++) { 
      output[offset] = input[offset + 2]; 
      output[offset + 1] = input[offset + 1]; 
      output[offset + 2] = input[offset]; 
      output[offset + 3] = input[offset + 3]; 

      offset += 4; 
     } 
    } 
} 

如果它仍然沒有顯示正確,然後確認正確的像素打包是什麼。它應該是ARGB或BGRA;我從來沒有聽說過RGBA像素。

相關問題