C++ 从像素阵列加载颜色值

C++ 从像素阵列加载颜色值,c++,bmp,C++,Bmp,我读了这个关于位图的教程,它真的很有帮助。我需要从像素数组的元素中读取颜色整数值。怎么做? 好的,这是将数据放入rgb数组的代码 BYTE* ConvertBMPToRGBBuffer ( BYTE* Buffer, int width, int height ) { if ( ( NULL == Buffer ) || ( width == 0 ) || ( height == 0 ) ) return NULL; // find the number of padding byt

我读了这个关于位图的教程,它真的很有帮助。我需要从像素数组的元素中读取颜色整数值。怎么做? 好的,这是将数据放入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;
    }
BYTE*ConvertBMPToRGBBuffer(BYTE*Buffer,int-width,int-height)
{
如果((NULL==缓冲区)| |(宽度==0)| |(高度==0))
返回NULL;
//查找填充字节数
int padding=0;
int scanlinebytes=宽度*3;
而((扫描线字节+填充)%4!=0)//DWORD=4字节
填充++;
//获取填充扫描线宽度
int psw=扫描线字节+填充;
//创建新缓冲区
字节*newbuf=新字节[宽度*高度*3];
//现在我们循环使用原始缓冲区的所有字节,
//交换R和B字节以及扫描线
长bufpos=0;
long-newpos=0;
对于(int y=0;y
看起来您的图像是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 ];

伙计,如果你想让人们在这方面帮助你,你应该提供迄今为止你所拥有的代码,而不是期望人们为你做所有的工作。你不想添加标记
gdi
?也许提供一些代码,展示你的一些努力,你已经尝试过的。。。也许你想问一个比“怎么做?”更抽象的问题……现在你的问题是什么?如何获得红色、绿色和蓝色的一个值?是的,这是一个问题,但用户1118321回答了这个问题