Skip to content

Instantly share code, notes, and snippets.

@nowl
Created February 16, 2011 01:56
Show Gist options
  • Save nowl/828710 to your computer and use it in GitHub Desktop.
Save nowl/828710 to your computer and use it in GitHub Desktop.
simple PPM output
static void
write_header(FILE *out, int width, int height, int max_color_val)
{
fprintf(out, "P3\n%d %d\n%d\n", height, width, max_color_val);
}
static void
write_color_val(FILE *out, int red, int green, int blue)
{
fprintf(out, "%d %d %d\n", red, green, blue);
}
static void
write_image(FILE *out, int *image, int width, int height)
{
int i;
for(i=0; i<width*height; i++)
{
int color = image[i];
write_color_val(out, color&0xff, (color>>8)&0xff, (color>>16)&0xff);
}
}
void
write_to_file(char *filename, int *image, int width, int height)
{
FILE *fout = fopen(filename, "w");
write_header(fout, width, height, 256);
write_image(fout, image, width, height);
fclose(fout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment