palpic2png.c: improve, make usable with ppic binary files
[rofl0r-openDOW.git] / utils / palpic2png.c
blobf8c0653137f5eabc1a34fd9b5bd6994d69abdb32
1 #include "../palpic.h"
2 #include "leptonica/allheaders.h"
3 #include <string.h>
4 #pragma RcB2 LINK "-llept -lpng -ljpeg -ltiff -lgif -lz"
6 struct palpic *palpic_from_file(const char *fn) {
7 FILE *fi = fopen(fn, "r");
8 if(!fi) return 0;
9 fseek(fi, 0, SEEK_END);
10 off_t len = ftello(fi);
11 fseek(fi, 0, SEEK_SET);
12 struct palpic* f = malloc(len);
13 if(f) fread(f, 1, len, fi);
14 fclose(fi);
15 if(f) palpic_hostformat(f);
16 return f;
19 void palpic_to_png(const struct palpic *f, const char *fn) {
20 PIX* o = pixCreate(f->width, f->height, 32);
21 prgb* palette = palpic_getpalette(f);
22 prgb* bufptr = (prgb*) o->data;
23 size_t i;
24 for(i = 0; i < f->spritecount; i++) {
25 int x, y;
26 const uint8_t *source = palpic_getspritedata(f, i);
27 for(y = 0; y < palpic_getspriteheight(f); y++) {
28 for(x = 0; x < palpic_getspritewidth(f); x++) {
29 *bufptr++ = palette[*source++];
33 pixWritePng(fn, o, 0.0);
34 pixDestroy(&o);
38 #ifdef BUILT_IN
39 #include "../temp.c"
40 #define sprite temp
42 int main() {
43 const struct palpic* f = & sprite .header;
44 palpic_to_png(f, "test.png");
45 return 0;
48 #else
50 int main(int argc, char **argv) {
51 while(*(++argv)) {
52 struct palpic *f = palpic_from_file(*argv);
53 if(!f) {
54 perror("fopen");
55 dprintf(2, "%s\n", *argv);
56 continue;
58 char buf[1024];
59 snprintf(buf, sizeof buf, "%s.png", *argv);
60 palpic_to_png(f, buf);
61 free(f);
63 return 0;
66 #endif