2012-02-20 32 views
0

我閱讀了關於位圖的本教程http://tipsandtricks.runicsoft.com/Cpp/BitmapTutorial.html,它確實有助於......我需要從像素數組的元素中讀取顏色整數值。怎麼做? 確定繼承人用於將數據放入數組RGB代碼從像素數組中加載顏色值

BYTE* ConvertBMPToRGBBuffer (BYTE* Buffer, int width, int height) 
{ 

if ((NULL == Buffer) || (width == 0) || (height == 0)) 
    return NULL; 

// find the number of padding bytes 

int padding = 0; 
int scanlinebytes = width * 3; 
while ((scanlinebytes + padding) % 4 != 0)  // DWORD = 4 bytes 
    padding++; 
// get the padded scanline width 
int psw = scanlinebytes + padding; 

// create new buffer 
BYTE* newbuf = new BYTE[width*height*3]; 

// now we loop trough all bytes of the original buffer, 
// swap the R and B bytes and the scanlines 
long bufpos = 0; 
long newpos = 0; 
for (int y = 0; y < height; y++) 
    for (int x = 0; x < 3 * width; x+=3) 
    { 
     newpos = y * 3 * width + x;  
     bufpos = (height - y - 1) * psw + x; 

     newbuf[newpos] = Buffer[bufpos + 2];  
     newbuf[newpos + 1] = Buffer[bufpos+1]; 
     newbuf[newpos + 2] = Buffer[bufpos];  
    } 

return newbuf; 
    } 
+0

哥們,如果你希望人們在這裏幫助你,你應該提供你迄今爲止的代碼,而不是期望人們爲你做所有的工作。 – guitarflow 2012-02-20 23:21:54

+0

難道你不想添加標籤'gdi'?也許提供一些代碼,展示你的一些努力,你嘗試了什麼...也許你想問的問題不會比*「如何做到這一點?」* ... – LihO 2012-02-20 23:23:40

+0

現在你的問題是什麼?如何獲得紅色,綠色和藍色的一個值? – guitarflow 2012-02-20 23:53:01

回答

2

它看起來像你的形象是在RGB交錯格式。要在(x,y)處獲取像素,只需在該位置索引數組。如果你的緩衝區指向一個結構類型,那將是最簡單的。喜歡的東西:

typedef struct RGBPixel { 
    BYTE red; 
    BYTE green; 
    BYTE blue; 
} RGBPixel; 

然後,你可以做這樣的事情:

RGBPixel* pixels = (RGBPixel*)newbuf; 

要得到的(x,y)的像素,你可以這樣做:

RGBPixel aPixel = pixels [ y * width + x ];