convert external image for test tile and blit hardcoded grid
[retrocrawl] / tools / conv_gimp.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include HDRFILE
6
7 #define NBPL    5
8
9 int main(int argc, char **argv)
10 {
11         int i, j, k, palsz;
12         FILE *fp;
13         char *buf;
14         unsigned char *src;
15
16         if(!argv[1]) {
17                 fprintf(stderr, "missing argument: output name\n");
18                 return 1;
19         }
20
21         if(!(buf = malloc(strlen(argv[1]) + 4))) {
22                 perror("malloc failed");
23                 return 1;
24         }
25         sprintf(buf, "%s.img", argv[1]);
26
27         if(!(fp = fopen(buf, "wb"))) {
28                 perror("failed to open output file");
29                 return 1;
30         }
31
32         src = header_data;
33
34         printf("image: %dx%d\n", width, height);
35         for(i=0; i<height; i++) {
36                 for(j=0; j<NBPL; j++) {
37                         int bit = j;
38                         unsigned char out = 0;
39                         for(k=0; k<width; k++) {
40                                 out = (out << 1) | ((src[k] >> bit) & 1);
41                                 if((k & 7) == 7) {
42                                         fputc(out, fp);
43                                 }
44                         }
45                 }
46                 src += width;
47         }
48         fclose(fp);
49
50         sprintf(buf, "%s.pal", argv[1]);
51         if(!(fp = fopen(buf, "wb"))) {
52                 perror("failed to open palette output file");
53                 return 1;
54         }
55
56         palsz = 1 << NBPL;
57
58         printf("palette:\n");
59         for(i=0; i<palsz; i++) {
60                 int r = header_data_cmap[i][0] >> 4;
61                 int g = header_data_cmap[i][1] >> 4;
62                 int b = header_data_cmap[i][2] >> 4;
63                 unsigned int col = ((r & 0xf) << 8) | ((g & 0xf) << 4) | (b & 0xf);
64                 printf("0x%03x\n", col);
65                 fputc(col >> 8, fp);
66                 fputc(col, fp);
67         }
68         fclose(fp);
69
70         return 0;
71 }