create a bmp copy with 4 colors + modified default log
[my-code/arm.git] / betty / bmp2b.c
1 /*
2  * bmp2b.c - convert colored 24 bit bmp to the betty display ram format
3  *
4  * author: hackbard@hackdaworld.org
5  *
6  */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15
16 #include "bmp.h"
17
18 #define DX      128
19 #define DY      160
20 #define PM      (DY/8)
21
22 #define BINARY  1
23 #define CHAR    2
24 #define SHOW    3
25
26 int main(int argc,char **argv) {
27
28         int i,fd;
29         t_bmp src,dst;
30         char in[128];
31         char out[128];
32         char blook[128+8];
33         unsigned char stat;
34         unsigned char buf[2];
35         int page,col;
36         int b,pix;
37
38         stat=0;
39         for(i=1;i<argc;i++) {
40                 if(argv[i][0]!='-')
41                         continue;
42                 switch(argv[i][1]) {
43                         case 'i':
44                                 strncpy(in,argv[++i],128);
45                                 break;
46                         case 'o':
47                                 strncpy(out,argv[++i],128);
48                                 break;
49                         case 'b':
50                                 stat=BINARY;
51                                 break;
52                         case 'c':
53                                 stat=CHAR;
54                                 break;
55                         default:
56                                 printf("usage:\n\n");
57                                 printf("%s -i <bitmap> -o <out file> [-b/c]\n",
58                                        argv[0]);
59                                 printf("\n");
60                                 printf("  -b: binary out\n");
61                                 printf("  -c: char array\n\n");
62                                 return -1;
63                 }
64         }
65
66         /* the bitmap infile */
67         bmp_init(&src,1);
68         src.mode=READ;
69         strncpy(src.file,in,128);
70         bmp_read_file(&src);
71
72         /* the bitmap outfile */
73         sprintf(blook,"blook_%s",in);
74         bmp_init(&dst,1);
75         dst.mode=WRITE;
76         strncpy(dst.file,blook,128+8);
77         dst.width=src.width;
78         dst.height=src.height;
79         bmp_alloc_map(&dst);
80
81         if((src.width!=DX)|(src.height=!DY)) {
82                 printf("wrong dimensions: %d %d (need: %d %d)\n",
83                        src.width,src.height,DX,DY);
84                 return -1;
85         }
86
87         /* out file */
88         fd=open(out,O_WRONLY|O_CREAT);
89         if(fd<0) {
90                 perror("open outfile");
91                 return fd;
92         }
93
94         if(stat==CHAR)
95                 dprintf(fd,"unsigned char default_logo[%d]={\n",DX*PM*2);
96
97         for(page=0;page<PM;page++) {
98                 for(col=0;col<DX;col++) {
99                         buf[0]=0;
100                         buf[1]=0;
101                         for(i=0;i<8;i++) {
102                                 pix=((page*8+i)*DX)+col;
103                                 b=src.map[pix].r+src.map[pix].g+src.map[pix].b;
104                                 b/=3;
105                                 if(b<=(0.25*255)) {
106                                         buf[0]|=(1<<i);
107                                         buf[1]|=(1<<i); // 1 1
108                                         dst.map[pix].r=0;
109                                         dst.map[pix].g=0;
110                                         dst.map[pix].b=0;
111                                         continue;
112                                 }
113                                 if(b<=(0.5*255)) {
114                                         buf[0]|=(1<<i); // 1 0
115                                         dst.map[pix].r=0.25*255;
116                                         dst.map[pix].g=0.25*255;
117                                         dst.map[pix].b=0.25*255;
118                                         continue;
119                                 }
120                                 if(b<=(0.75*255)) {
121                                         buf[1]|=(1<<i); // 0 1
122                                         dst.map[pix].r=0.75*255;
123                                         dst.map[pix].g=0.75*255;
124                                         dst.map[pix].b=0.75*255;
125                                         continue;
126                                 }
127                                 // 0 0 .. do nothing!
128                                 dst.map[pix].r=255;
129                                 dst.map[pix].g=255;
130                                 dst.map[pix].b=255;
131                         }
132                         if(stat==BINARY) {
133                                 i=write(fd,buf,2);
134                                 if(i<0) {
135                                         perror("bin write");
136                                         return i;
137                                 }
138                                 if(i!=2) {
139                                         printf("write failure\n");
140                                         return -1;
141                                 }
142                         }
143                         else if(stat==CHAR) {
144                                 dprintf(fd,"\t%02x,%02x%c\n",buf[0],buf[1],
145                                         ((page+1==PM)&(col+1==DX))?' ':',');
146                         }
147                 }
148         }
149
150         if(stat==CHAR)
151                 dprintf(fd,"};\n");
152
153         bmp_write_file(&dst);
154
155         close(fd);
156         bmp_shutdown(&src);
157         bmp_shutdown(&dst);
158
159         return 0;
160 }
161