I'm struggling to figure out why this will not work. I read in a PPM image and have to rotate it 90 degrees but nothing I have tried will work. I'm pretty sure most of my problems stem from improper usage of pointers especially in a 1d array iteration context.
/** 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);
}