tinyrenderer/src/pam.c

31 lines
665 B
C

#include "pam.h"
#include <stdio.h>
#include <string.h>
void write_p7_image(u64 width, u64 height, u8 *buf, const char *outfile) {
FILE *fp = fopen(outfile, "wb");
if (!fp) {
fprintf(stderr, "Couldn't open file: %s\n", outfile);
return;
}
char header[4096] = {0};
snprintf(header, 4095,
"P7\n"
"WIDTH %lu\n"
"HEIGHT %lu\n"
"DEPTH 4\n"
"MAXVAL 255\n"
"TUPLTYPE RGB_ALPHA\n"
"ENDHDR\n",
width, height);
u64 hdr_size = strlen(header);
u64 buf_size = width * height * 4;
fwrite(header, hdr_size, 1, fp);
fwrite(buf, buf_size, 1, fp);
fclose(fp);
}