简体   繁体   English

将PPM图像旋转90度

[英]Rotate PPM image 90 degrees

I am trying to rotate a PPM image 90 degrees. 我正在尝试将PPM图像旋转90度。 I am currently able to rotate the image 180 degrees. 我目前能够将图像旋转180度。 I am not sure on how to do this. 我不确定如何执行此操作。

I know that the height and width are swapped, but I am not sure where to go from there. 我知道高度和宽度已交换,但是我不确定从那里去哪里。

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();
}

While I have never implemented any rotation algorithm, I think it would help to actually do it on paper first... 虽然我从未实现过任何旋转算法,但我认为首先实际在纸上进行操作会有所帮助...

Draw a rectangle on a squared paper, where each square represents a pixel. 在方格纸上绘制一个矩形,其中每个正方形代表一个像素。 Label each square with the coordinates of the pixels. 用像素的坐标标记每个正方形。 Then turn the paper 90 degrees, and draw a second equally large rectangle, again with each "pixel" labeled with the coordinates. 然后将纸张旋转90度,并绘制另一个同样大的矩形,每个“像素”再次用坐标标记。

Now when you turn the paper back so the first rectangle is normal, you can easily see where to put the pixels in the output (second) rectangle. 现在,当您将纸张转回以使第一个矩形正常时,您可以轻松地看到将像素放置在输出(第二个)矩形中的位置。

It shouldn't be too hard to find a general algorithm for that. 为此找到通用算法应该不难。

Your approach seems very painful, and is only geared towards PPM. 您的方法似乎很痛苦,并且仅针对PPM。

Instead, you could simply use ImageMagick mogrify or GraphicsMagick gm mogrify utility. 相反,您可以简单地使用ImageMagick mogrifyGraphicsMagick gm mogrify实用程序。 You could download statically compiled version of GraphicsMagick gm with size less than 2MB. 您可以下载大小小于2MB的GraphicsMagick gm静态编译版本。

gm can be considered "swiss army knife" for any image manipulations. 对于任何图像处理, gm都可以视为“瑞士军刀”。 Aside from making it easy, it will support not only PPM, but almost any other known graphics format, effortlessly. 除了使它变得容易之外,它还不费力地不仅支持PPM,而且几乎支持任何其他已知的图形格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM