From a2d8f1e44046f768b88e9f2b08b88eb2897e63f6 Mon Sep 17 00:00:00 2001 From: rofl0r Date: Sat, 2 Nov 2019 19:15:19 +0000 Subject: [PATCH] palpic2png.c: improve, make usable with ppic binary files --- utils/palpic2png.c | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/utils/palpic2png.c b/utils/palpic2png.c index 86cea25..f8c0653 100644 --- a/utils/palpic2png.c +++ b/utils/palpic2png.c @@ -3,11 +3,20 @@ #include #pragma RcB2 LINK "-llept -lpng -ljpeg -ltiff -lgif -lz" -#include "../temp.c" -#define sprite temp +struct palpic *palpic_from_file(const char *fn) { + FILE *fi = fopen(fn, "r"); + if(!fi) return 0; + fseek(fi, 0, SEEK_END); + off_t len = ftello(fi); + fseek(fi, 0, SEEK_SET); + struct palpic* f = malloc(len); + if(f) fread(f, 1, len, fi); + fclose(fi); + if(f) palpic_hostformat(f); + return f; +} -int main() { - const struct palpic* f = & sprite .header; +void palpic_to_png(const struct palpic *f, const char *fn) { PIX* o = pixCreate(f->width, f->height, 32); prgb* palette = palpic_getpalette(f); prgb* bufptr = (prgb*) o->data; @@ -21,6 +30,37 @@ int main() { } } } - pixWritePng("test.png", o, 0.0); + pixWritePng(fn, o, 0.0); + pixDestroy(&o); +} + + +#ifdef BUILT_IN +#include "../temp.c" +#define sprite temp + +int main() { + const struct palpic* f = & sprite .header; + palpic_to_png(f, "test.png"); + return 0; +} + +#else + +int main(int argc, char **argv) { + while(*(++argv)) { + struct palpic *f = palpic_from_file(*argv); + if(!f) { + perror("fopen"); + dprintf(2, "%s\n", *argv); + continue; + } + char buf[1024]; + snprintf(buf, sizeof buf, "%s.png", *argv); + palpic_to_png(f, buf); + free(f); + } return 0; } + +#endif -- 2.11.4.GIT