从像素数组加载颜色值
我阅读了本教程 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;
}
I read this tutorial http://tipsandtricks.runicsoft.com/Cpp/BitmapTutorial.html about bitmap and it really helped..I need to read color integer values from elements of pixel array. How to do that?
Ok heres the code for putting data into rgb array
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的图像看起来像是 RGB 交错格式。要获取 (x,y) 处的像素,只需在该位置索引数组即可。如果您的缓冲区指向结构类型,那将是最简单的。像这样:
然后你可以这样做:
要获得 (x,y) 处的像素,你可以这样做:
It looks like your image is in RGB interleaved format. To get a pixel at (x,y), simply index the array at that location. It would be easiest if your buffer pointed to a structure type. Something like:
Then you could do something like this:
To get a pixel at (x,y), you'd do this: