added a bitmap to betty lcd ram conversion tool
[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
25 int main(int argc,char **argv) {
26
27         int i,fd;
28         t_bmp src;
29         char in[128];
30         char out[128];
31         unsigned char stat;
32         unsigned char buf[2];
33         int page,col;
34         int b,pix;
35
36         stat=0;
37         for(i=1;i<argc;i++) {
38                 if(argv[i][0]!='-')
39                         continue;
40                 switch(argv[i][1]) {
41                         case 'i':
42                                 strncpy(in,argv[++i],128);
43                                 break;
44                         case 'o':
45                                 strncpy(out,argv[++i],128);
46                                 break;
47                         case 'b':
48                                 stat=BINARY;
49                                 break;
50                         case 'c':
51                                 stat=CHAR;
52                                 break;
53                         default:
54                                 printf("usage:\n\n");
55                                 printf("%s -i <bitmap> -o <out file> [-b/c]\n",
56                                        argv[0]);
57                                 printf("\n");
58                                 printf("  -b: binary out\n");
59                                 printf("  -c: char array\n\n");
60                                 return -1;
61                 }
62         }
63
64         /* the bitmap infile */
65         bmp_init(&src,1);
66         src.mode=READ;
67         strcpy(src.file,in);
68         bmp_read_file(&src);
69
70         if((src.width!=DX)|(src.height=!DY)) {
71                 printf("wrong dimensions: %d %d (need: %d %d)\n",
72                        src.width,src.height,DX,DY);
73                 return -1;
74         }
75
76         /* out file */
77         fd=open(out,O_WRONLY|O_CREAT);
78         if(fd<0) {
79                 perror("open outfile");
80                 return fd;
81         }
82
83         if(stat==CHAR)
84                 dprintf(fd,"unsigned char default_logo[%d]={\n",DX*PM*2);
85
86         for(page=0;page<PM;page++) {
87                 for(col=0;col<DX;col++) {
88                         buf[0]=0;
89                         buf[1]=0;
90                         for(i=0;i<8;i++) {
91                                 pix=((page*8+i)*DX)+col;
92                                 b=src.map[pix].r+src.map[pix].g+src.map[pix].b;
93                                 b/=3;
94                                 if(b<=(0.25*255)) {
95                                         buf[0]|=(1<<i);
96                                         buf[1]|=(1<<i); // 1 1
97                                         continue;
98                                 }
99                                 if(b<=(0.5*255)) {
100                                         buf[0]|=(1<<i); // 1 0
101                                         continue;
102                                 }
103                                 if(b<=(0.75*255)) {
104                                         buf[1]|=(1<<i); // 0 1
105                                         continue;
106                                 }
107                                 // 0 0 .. do nothing!
108                         }
109                         if(stat==BINARY) {
110                                 i=write(fd,buf,2);
111                                 if(i<0) {
112                                         perror("bin write");
113                                         return i;
114                                 }
115                                 if(i!=2) {
116                                         printf("write failure\n");
117                                         return -1;
118                                 }
119                         }
120                         else if(stat==CHAR) {
121                                 dprintf(fd,"\t%02x,%02x%c\n",buf[0],buf[1],
122                                         ((page+1==PM)&(col+1==DX))?' ':',');
123                         }
124                 }
125         }
126
127         if(stat==CHAR)
128                 dprintf(fd,"};\n");
129
130         close(fd);
131         bmp_shutdown(&src);
132
133         return 0;
134 }
135