【发布时间】:2015-06-27 23:12:42
【问题描述】:
我得到一个包含图像像素颜色值的一维数组。该数组的大小为 cols*rows。我想更改特定区域的颜色。函数调用被赋予一维数组、它的大小、左、上、下、右以及所需的颜色。我试图做的是将一维数组的值复制到二维数组中,对二维数组执行操作,然后将新值复制回一维数组。我不断收到分段错误错误。代码如下:
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 )
{
if(!(left == right || top == bottom)){
uint8_t Array2[cols][rows];
int x, y;
for(int i= 0; i < rows * cols; i++){
x = i / rows;
y = i % cols;
Array2[y][x] = array[i];
}
for(int y=left; y < right-1; y++){
for(int x = top; x < bottom-1 ; x++){
Array2[y][x] = color;
}
}
for(int i= 0; i < rows * cols; i++){
x = i / rows;
y = i % cols;
array[i] = Array2[y][x];
}
}
}
我使用此代码水平镜像图像并且它有效:
for(int x=0; x<cols; x++){
for(int y =0; y< rows/2; y++){
holdVal=array[x + y * rows];
array[x + y * rows] = array[(rows-1-y)* rows + x];
array[(rows-1-y) * rows + x] = holdVal;
}
}
然后我对其进行了调整以尝试使其适用于 region_set 函数:
for(int y=left; y< right-1; y++){
for(int x = top; x< bottom - 1; x++){
array[x + y * cols] = color;
}
}
到目前为止没有运气。
【问题讨论】:
-
uint8_t Array2[cols][rows];-->uint8_t Array2[rows][cols];?