未压缩的 24 位 .bmp 文件
我需要旋转 .bmp 文件,旋转倍数为 90。 例如,我有一张图像,并且给定了 +90 或 -90 旋转因子。 我的图像将根据旋转系数向左或向右旋转 90 度。 当文件的尺寸相等时,我的程序工作正常,这意味着高度和宽度相等,但是当我使用不是正方形的图像时,我会出现段错误。
这是我到目前为止所拥有的代码。
if(rotation == 90 || rotation == -270 )
{
/* 90 = -270 */
for(row = 0; row < rows; row++)
{
for(col = 0; col < cols; col++ )
{
PIXEL* o = original+ (row*cols) + col;
PIXEL* n = (*new)+((cols-col-1)*cols) + row;
*n = *o;
}
}
*newcols = cols;
*newrows = rows;
该方法的 header 是:
int rotate(PIXEL* original, int rows, int cols, int rotation,
PIXEL** new, int* newrows, int* newcols)
其中 PIXEL* 原始包含原始 .bmp 文件
行和列是通过调用读取 .bmp 文件的方法获得的
rotation = 是用户给定的旋转系数
最佳答案
这是你的问题。您应该在这里乘以 rows
而不是 cols
:
PIXEL* n = (*new)+((cols-col-1)*rows) + row;
您需要乘以新图像中的行宽,该值与原始图像中的行数相同。
此外,您应该在此处交换行和列:
*newcols = rows;
*newrows = cols;
旋转-90:
PIXEL* n = (*new)+(col*rows) + (rows-row-1);
*newcols = rows;
*newrows = cols;
旋转 180 度:
PIXEL* n = (*new)+((rows-row-1)*cols) + (cols-col-1);
*newcols = cols;
*newrows = rows;
一般来说,公式为:
PIXEL* n = (*new)+(newrow*newcols) + newcol;
您只需要弄清楚如何从先前未旋转的 BMP 中确定 newrow、newcols 和 newcol。画图有帮助。
关于c - 使用C旋转BMP图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19797790/