2014-11-22 54 views
0
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]中的所有行。我尝试过测试程序,但是当我测试它时,我的图像最终将图像的整个宽度设置为该颜色。当我尝试更改顶部,左侧,右侧或底部时,它只会改变图像的高度。但是,左右应该会改变图像的宽度。我的心态是(右 - 左)*(底部 - 顶部),这将影响该地区的所有像素。将图像中的区域更改为“彩色”c

我用来做测试,这是:

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

我不知道为什么我总是像整个宽度设置为颜色和变化的左,上,右或底部仅设置高度。任何人都可以帮助我吗?

回答

0

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

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