-1
我正在努力弄清楚爲什麼這不起作用。我讀了PPM圖像,必須將其旋轉90度,但沒有任何我已經嘗試過的。我很確定,我的大部分問題都是由於指針的使用不當造成的,特別是在1d數組迭代上下文中。在C中旋轉PPM圖像90度
/** rotate.c
CpSc 2100: homework 2
Rotate a PPM image right 90 degrees
**/
#include "image.h"
typedef struct pixel_type
{
unsigned char red;
unsigned char green;
unsigned char blue;
} color_t;
image_t *rotate(image_t *inImage)
{
image_t *rotateImage;
int rows = inImage->rows;
int cols = inImage->columns;
color_t *inptr;
color_t *outptr;
color_t *pixptr;
int width = rows;
int height = cols;
int i, k;
/* malloc an image_t struct for the rotated image */
rotateImage = (image_t *) malloc(sizeof(image_t));
if(rotateImage == NULL)
{
fprintf(stderr, "Could not malloc memory for rotateImage. Exiting\n");
}
/* malloc memory for the image itself and assign the
address to the image pointer in the image_t struct */
rotateImage -> image = (color_t *) malloc(sizeof(color_t) * rows * cols);
if(rotateImage -> image == NULL)
{
fprintf(stderr, "Could not malloc memory for image. Exiting\n");
}
/* assign the appropriate rows, columns, and brightness
to the image_t structure created above */
rotateImage -> rows = cols;
rotateImage -> columns = rows;
rotateImage -> brightness = inImage -> brightness;
inptr = inImage -> image;
outptr = rotateImage -> image;
/* write the code to create the rotated image */
for(i = 0; i<height; i++)
{
for(k = 0; k<width; k++)
{
outptr[(height * k)+(height-i - 1)] = inptr[(width*i)+k];
}
}
rotateImage -> image = outptr;
return(rotateImage);
}
也許你能形容現在的代碼做什麼,以及如何與你的期望不同。 –
嗯,我希望兩個循環遍歷所有不同的可能像素,並將它們分配給圖像上的旋轉位置。我期望它能夠工作,因爲我在紙面上看過它並且工作正常。 –
[不要在C中投入malloc的結果](http://stackoverflow.com/q/605845/995714) –