将图像区域更改为" color"在c

时间:2014-11-22 22:20:22

标签: c image colors

void region_set( uint8_t array[], 
         unsigned int cols, 
         unsigned int rows,
         unsigned int left,
         unsigned int top,
         unsigned int right,
         unsigned int bottom,
         uint8_t color )
{
    for (int x = 0; x < ((right-left)*(bottom-top)); x++)
    {
        array[x] = color;
    }
}

此代码的功能是将某个图像阵列区域中的每个像素设置为颜色。该区域包括[left,right-1]包含的所有列以及[top,bottom-1]包含的所有行。我试过测试程序,但是当我测试它时,我的图像最终将图像的整个宽度设置为颜色。当我尝试更改顶部,左侧,右侧或底部时,它只更改了图像的高度。但是左右两侧应该改变图像的宽度。我的心态是(左 - 右)*(从下到上)会影响该区域的所有像素。

我以前做过的测试是:

  region_set (img, 256, 256, 100, 80, 156, 160, 128);

我不知道为什么我的图像总是将整个宽度设置为颜色,而改变左,上,右或底只会改变高度。有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您需要将2D X / Y坐标转换为图像数组中的1D索引。然后,有两个嵌套循环迭代所需区域是一件简单的事情,例如(代码未测试):

for (int y = top; y <= bottom; ++y)
{
    for (int x = left; x <= right; ++x)
    {
        int i = x + y * cols;
        array[i] = color;
    }
}