commit daf888310aed1fba5dc05086ec5ea9903e456fe2
parent b7df3e8c42890de3468ccdd7c7b090dea08b23aa
Author: bsandro <email@bsandro.tech>
Date: Sun, 4 Jan 2026 03:16:17 +0200
Command-line version supports writing to PAM file format now
Diffstat:
| M | cli/main.c | | | 34 | ++++++++++++++++++++++++++++++++++ |
1 file changed, 34 insertions(+), 0 deletions(-)
diff --git a/cli/main.c b/cli/main.c
@@ -213,6 +213,38 @@ int write_webp(AnimatedImage *img, const char *fname, int cols) {
return 0;
}
+int write_pam(AnimatedImage *img, const char *fname, int cols) {
+ int rows = (int)img->frame_count/cols;
+ if ((int)img->frame_count%cols > 0) rows++;
+ FILE *fp = fopen(fname, "wb");
+ if (fp==NULL) {
+ fputs("Error opening output file", stderr);
+ return 1;
+ }
+ fprintf(fp, "P7\n");
+ fprintf(fp, "WIDTH %d\n", img->width*cols);
+ fprintf(fp, "HEIGHT %d\n", img->height*rows);
+ fprintf(fp, "DEPTH 4\n");
+ fprintf(fp, "MAXVAL 255\n");
+ fprintf(fp, "TUPLTYPE RGB_ALPHA\n");
+ fprintf(fp, "ENDHDR\n");
+
+ size_t line_size = img->width*sizeof(uint32_t);
+ for (int row=0; row<rows; ++row) {
+ for (int y=0; y<img->height; ++y) {
+ for (int col=0; col<cols; ++col) {
+ uint32_t offset=row*cols+col;
+ if (offset<img->frame_count) {
+ fwrite(img->frames[offset].rgba+y*line_size, sizeof(uint8_t), line_size, fp);
+ }
+ }
+ }
+ }
+ fclose(fp);
+ printf("Output: %s: %dx%dpx\n", fname, img->width*cols, img->height*rows);
+ return 0;
+}
+
int save_file(AnimatedImage *img, char *out, int cols, int width) {
char out_gen[PATH_MAX] = {0};
if (out==NULL) {
@@ -235,6 +267,8 @@ int save_file(AnimatedImage *img, char *out, int cols, int width) {
fputs("Invalid width", stderr);
return 1;
}
+ int out_len = strlen(out);
+ if (out_len>4&&strncmp(out+out_len-4, ".pam", 4)==0) return write_pam(img, out, cols);
return write_webp(img, out, cols);
}