/* Example function to write an RGB .ppm image file Author: Stan Sclaroff Date: November 19, 2003 Arguments: unsigned byte *buff -- a pointer to an RGB image, one byte per R,G,B per pixel. int width, int height -- image dimensions (in pixels) char *fpath -- the file name path for the image output file Returns: 1 if successful, 0 if unsuccessful */ #include int write_rgb_ppm(unsigned char *buff, int width, int height, char *fpath) { FILE *f; /* open the file for writing (overwrite any previous version) */ if((f = fopen(fpath,"wb")) == 0) { fprintf(stderr,"Cannot open image output file (name = %s)\n",fpath); return 0; } /* the ASCII header of the PPM file */ fprintf(f,"P6\n"); /* magic number for PPM files */ fprintf(f,"%i %i\n",width,height); /* dimensions */ fprintf(f,"255\r"); /* max intensity value */ /* write the pixels, 3 bytes/pixel */ fwrite(buff,3,width*height,f); fclose(f); return 1; }