bullshit commit, sync for travel (to zn00H!) :)
[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,"const 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                                 // bmp: bottom rows first ... (i forgot that!)
103                                 pix=((DY-1-(page*8+i))*DX)+col;
104                                 b=src.map[pix].r+src.map[pix].g+src.map[pix].b;
105                                 b/=3;
106                                 if(b<=(0.25*255)) {
107                                         buf[0]|=(1<<i);
108                                         buf[1]|=(1<<i); // 1 1
109                                         dst.map[pix].r=0;
110                                         dst.map[pix].g=0;
111                                         dst.map[pix].b=0;
112                                         continue;
113                                 }
114                                 if(b<=(0.5*255)) {
115                                         buf[0]|=(1<<i); // 1 0
116                                         dst.map[pix].r=0.25*255;
117                                         dst.map[pix].g=0.25*255;
118                                         dst.map[pix].b=0.25*255;
119                                         continue;
120                                 }
121                                 if(b<=(0.75*255)) {
122                                         buf[1]|=(1<<i); // 0 1
123                                         dst.map[pix].r=0.75*255;
124                                         dst.map[pix].g=0.75*255;
125                                         dst.map[pix].b=0.75*255;
126                                         continue;
127                                 }
128                                 // 0 0 .. do nothing!
129                                 dst.map[pix].r=255;
130                                 dst.map[pix].g=255;
131                                 dst.map[pix].b=255;
132                         }
133                         if(stat==BINARY) {
134                                 i=write(fd,buf,2);
135                                 if(i<0) {
136                                         perror("bin write");
137                                         return i;
138                                 }
139                                 if(i!=2) {
140                                         printf("write failure\n");
141                                         return -1;
142                                 }
143                         }
144                         else if(stat==CHAR) {
145                                 dprintf(fd,"\t0x%02x,0x%02x%c\n",buf[0],buf[1],
146                                         ((page+1==PM)&(col+1==DX))?' ':',');
147                         }
148                 }
149         }
150
151         if(stat==CHAR)
152                 dprintf(fd,"};\n");
153
154         bmp_write_file(&dst);
155
156         close(fd);
157         bmp_shutdown(&src);
158         bmp_shutdown(&dst);
159
160         return 0;
161 }
162