我正在尝试将 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();
}