gpt4 book ai didi

c - 使用 C 旋转 BMP 图像

转载 作者:行者123 更新时间:2023-12-04 04:35:35 25 4
gpt4 key购买 nike

未压缩的 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;

这个方法的标题是:
int rotate(PIXEL* original, int rows, int cols, int rotation,
PIXEL** new, int* newrows, int* newcols)

其中 PIXEL* original 包含原始 .bmp 文件

通过调用读取 .bmp 文件的方法获得行和列

旋转 = 是用户给出的旋转因子

最佳答案

这是你的问题。你应该乘以 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com