My goal is to read in a PGM image and produce an image with inverted color values. But when I put in this image, I get back this image. I'm programming in C, using Eclipse and MinGW GCC on Windows 7 (64-bit). Why is the image getting so drastically distorted?
int complement(PGMImage *img) {
int i, j;
// set up new PGM to copy onto
PGMImage* comImg = (PGMImage*)malloc(sizeof(PGMImage));
(*comImg).width = (*img).width;
(*comImg).height = (*img).height;
// invert each pixel
for(i = 0; i <= (*img).width; i++) {
for(j = 0; j <= (*img).height; j++) {
// set inverted value for each new pixel
(*comImg).data[i][j].red = abs((*img).maxVal - (*img).data[i][j].red);
(*comImg).data[i][j].green = abs((*img).maxVal - (*img).data[i][j].green);
(*comImg).data[i][j].blue = abs((*img).maxVal - (*img).data[i][j].blue);
}
}
// save a copy of the complement image
save("C:\\image_complement.pgm", comImg);
printf("A copy of your image has been saved as \"C:\\image_complement.pgm\"\n\n");
void free(comImg);
return 0;
}