Created
January 16, 2014 02:52
-
-
Save realModusOperandi/8449066 to your computer and use it in GitHub Desktop.
PPM output (P6)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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