从像素数组加载颜色值

发布于 2025-01-07 15:45:11 字数 1199 浏览 1 评论 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;
    }

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷你仙 2025-01-14 15:45:11

您的图像看起来像是 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 ];

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:

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

Then you could do something like this:

RGBPixel* pixels = (RGBPixel*)newbuf;

To get a pixel at (x,y), you'd do this:

RGBPixel aPixel = pixels [ y * width + x ];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文