【发布时间】:2016-07-17 18:28:53
【问题描述】:
我写了一些代码来尝试回答这个问题。
我得到了一个程序来绘制一个使用 X11 进行灰度缩放的图像。
有人要求我编写一个函数来水平/垂直翻转图像。
这是我的代码 sn-p
// flip the image, left-to-right, like in a mirror.
void flip_horizontal( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int i;
int j;
for (i=0; i<rows;i++)
{
for (j=0; j<cols;j++)
{
array[rows*i+j]=array[rows*i+(cols-1-j)];
}
}
}
// flip the image top-to-bottom.
void flip_vertical( uint8_t array[],
unsigned int cols,
unsigned int rows )
{
int i=0;
int j=0;
for (i=0; i<rows;i++)
{
for (j=0; j<cols;j++)
{
array[rows*i+j]=array[rows*(rows-1-i)+j];
}
}
return;
}
我遇到的问题是我的水平函数仅将图像翻转一半,另一半保留其原始值。
我的垂直翻转功能也是一团糟,生成的图像根本不像它应该的那样,所以我试图调试我在编写函数的逻辑中犯了错误的地方。
我正在使用平面索引方法来访问二维数组值。
【问题讨论】:
-
在 C 中
=并不意味着“交换”。即使是这样,你还是发出了经典的“单词反转”咆哮,交换字母并再次交换它们。 -
平面索引:
array[rows*i+j]-->array[cols*i+j]