Skip to content

Instantly share code, notes, and snippets.

@realModusOperandi
Created January 16, 2014 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save realModusOperandi/8449066 to your computer and use it in GitHub Desktop.
Save realModusOperandi/8449066 to your computer and use it in GitHub Desktop.
PPM output (P6)
/* Write an image out to disk in PPM format (P6 variant).
* Will exit if file could not be opened for writing.
*
* the_image: Pointer to a ppm_image struct (see ppm.h) containing the image data to be written.
* outfilepath: String containing the file path that should be written to.
*/
void write_image(ppm_image *the_image, char *outfilepath) {
FILE *outfile;
if ((outfile = fopen(outfilepath, "w")) == NULL) {
fprintf(stderr, "There was a problem opening the output file %s.\n", outfilepath);
exit(1);
}
fprintf(outfile, "P6\n");
fprintf(outfile, "%d\n", the_image->width);
fprintf(outfile, "%d\n", the_image->height);
fprintf(outfile, "%d\n", the_image->maxval);
for (int i = 0; i < the_image->height; i++) {
for (int j = 0; j < the_image->width; j++) {
fputc(the_image->data[i][j].r, outfile);
fputc(the_image->data[i][j].g, outfile);
fputc(the_image->data[i][j].b, outfile);
}
}
fclose(outfile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment