unsigned char *readppm(char *filename, int *width, int *height) { unsigned int x, y, ch, px; unsigned char *img; image image; color_component *cp; FILE *fp; fopen_s(&fp, filename, "rb"); image = get_ppm(fp); if (!image) return NULL; img = (unsigned char *)malloc(sizeof(unsigned char) * image->width * image->height * 3); for (y = 0; y < image->height; ++y) { for (x = 0; x < image->width; ++x) { px = (y * image->width + x) * 3; cp = GET_PIXEL(image, x, y); for (ch = 0; ch < 3; ++ch) { img[px + ch] = cp[ch]; } } } *width = image->width; *height = image->height; return img; }
int main() { image source; grayimage idest; source = get_ppm(stdin); idest = tograyscale(source); free_img(source); source = tocolor(idest); output_ppm(stdout, source); free_img(source); free_img((image)idest); return 0; }
image read_image(const char *name) { FILE *pipe; char buf[MAXFULLCMDBUF]; image im; FILE *test = fopen(name, "r"); if ( test == NULL ) { fprintf(stderr, "cannot open file %s\n", name); return NULL; } fclose(test); snprintf(buf, MAXFULLCMDBUF, "convert \"%s\" ppm:-", name); pipe = popen(buf, "r"); if ( pipe != NULL ) { im = get_ppm(pipe); pclose(pipe); return im; } return NULL; }