我正在尝试将 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/