c - 将 PPM 图像旋转 90 度

标签 c ppm

我正在尝试将 PPM 图像旋转 90 度。我目前能够将图像旋转 180 度。 我不确定该怎么做。

我知道高度和宽度被交换了,但我不确定从那里去哪里。

void write_ppm_image(const char *filename, Image_ppm *img)
{
FILE *fp;
//open file for output
fp = fopen(filename, "wb");
if (!fp) {
    fprintf(stderr, "Unable to open file '%s'\n", filename);
    exit(1);
}

//write the header file
//image format
fprintf(fp, "P6\n");

//comments
fprintf(fp, "# Created by %s\n",CREATOR);

//image size
fprintf(fp, "%d %d\n",img->x,img->y);

// rgb component depth
fprintf(fp, "%d\n",RGB_COMPONENT_COLOR);

// pixel data
fwrite(img->data, 3 * img->x, img->y, fp);
fclose(fp);
}


 void rotatePPM(Image_ppm *img)
 {
int x = 0, y = 0;
int size = img->x*img->y;
int width = img->x;
int height = img->y;

if(img)
{

    for(i=0;i<size;i++)
    {
        int temp = img->data[i].red;
        int temp_one = img->data[i].green;
        int temp_two = img->data[i].blue;
        img->data[i].red = img->data[size].red;
        img->data[i].green = img->data[size].green;
        img->data[i].blue = img->data[size].blue;
        img->data[size].red = temp;
        img->data[size].green = temp_one;
        img->data[size].blue = temp_two;
        size--;
    }

}
 }

 int main()
 {
char  filename[256];
printf("Enter the name of a PPM image file: \n");
scanf("%s",filename);
Image_ppm *image;
image = read_ppm_image(filename);
rotatePPM(image);
write_ppm_image("can_bottom3.ppm",image);
printf("Press any key...");
getchar();
}

最佳答案

虽然我从未实现过任何旋转算法,但我认为首先在纸上实际实现它会有所帮助......

在方格纸上画一个矩形,每个方格代表一个像素。用像素的坐标标记每个正方形。然后将纸旋转 90 度,绘制第二个同样大的矩形,同样每个“像素”都标有坐标。

现在,当您将纸张翻过来使第一个矩形正常时,您可以轻松地看到将像素放置在输出(第二个)矩形中的位置。

为此找到通用算法应该不难。

关于c - 将 PPM 图像旋转 90 度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15781703/

相关文章:

c - 结构不取值

C 文件处理查询

c - 使用 C 函数扩展 PostgreSQL 时实现高性能事务

c - 我将如何使用数组来打印图像?

Java ImageIO读取ppm格式的图像返回null

c - 为什么这个字符串反转程序不起作用?

c - glibc 和 SSE 功能

c++ - 旋转图像 PPM

c++ - 将 RGB 图像保存为 PPM 格式

c - 写入 PNM P6 的问题