2014-11-22 35 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; 
    } 
} 
相關問題