Hello, I need help flipping a PPM image vertically. The code I have is below, but the flipImage function does not work correctly. Any help would be appreciated!
Code:#include <stdio.h> #include <stdlib.h> typedef struct { unsigned char r; unsigned char b; unsigned char g; } pixel_t; pixel_t *image; #define RGB_COMPONENT_COLOR 255 void flipImage(pixel_t *image, int x, int y); void grayscale(pixel_t *image, int x, int y); void rotate(pixel_t *image, int x, int y); int main(int argc, char *argv[]) { int width; int height; int numPixels; int i, rgb_comp_color = 255; char c; FILE *inFile; inFile = fopen(argv[2], "r"); //check for comments c = getc(inFile); while (c == '#') { do { c = getc(inFile); } while (c != '\n'); c = getc(inFile); } ungetc(c, inFile); //read width and height if (fscanf(inFile, "P6 %d %d", &width, &height) != 2) { fprintf(stderr, "Invalid image size.\n"); exit(1); } //read RGB component if (fscanf(inFile, "%d\n", &rgb_comp_color) != 1) { fprintf(stderr, "Invalid RGB component.\n"); exit(1); } //check RGB depth if (rgb_comp_color != RGB_COMPONENT_COLOR) { fprintf(stderr, "Invalid RGB value.\n"); exit(1); } numPixels = width*height; //allocate space image = malloc (numPixels * sizeof(pixel_t)); //read in pixel data for (i = 0; i < numPixels; i++) { fscanf(inFile, "%c%c%c", &image[i].r, &image[i].g, &image[i].b); } //print new header printf ("P6\n"); printf ("%d %d 255\n", width, height); switch (*argv[1]) { case '1': flipImage(image, width, height); break; case '2': grayscale(image, width, height); break; case '3': //rotate(image, height, width); break; default: printf("Invalid command line argument. User must enter a 1, 2, or 3.\n"); break; } return 0; } void grayscale(pixel_t *image, int x, int y) { int i, average = 0; for (i = 0; i < x*y; i++) { average = (image[i].r + image[i].g + image[i].b) / 3; printf("%c%c%c", average, average, average); } } void flipImage(pixel_t *image, int x, int y) { int r, c; for (c = y-1; c >= 0; c--) { for (r = 0; r < x; r++) { printf("%c%c%c", image[r].r, image[r].g, image[r].b); } } }



3Likes
LinkBack URL
About LinkBacks



