Add util to write p7 image

This commit is contained in:
Abdelrahman Said 2024-07-20 01:14:18 +01:00
parent 837cb6e288
commit 64ec6bfa59
2 changed files with 38 additions and 0 deletions

30
src/pam.c Normal file
View File

@ -0,0 +1,30 @@
#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);
}

8
src/pam.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef PAM_H
#define PAM_H
#include "aliases.h"
void write_p7_image(u64 width, u64 height, u8 *buf, const char *outfile);
#endif // PAM_H