3 // [HGM] This program converts the WinBoard monochrome .bmp piece-symbol bitmap files
\r
4 // to .xpm C-source files suitable for inclusion in xboard as buit-in pixmap.
\r
5 // It tries all bitmaps in bulk. Run it once in an empty subdirectory 'pixmaps'of the
\r
6 // root directory of the source tree, and it will fill it with pixmaps.
\r
7 // It overwrites any pixmaps of the same name that already exist. So if you want to
\r
8 // keep the pixmaps you already have, move them elsewhere, run convert, and move them back.
\r
11 int sizeList[] = { 21, 25, 29, 33, 37, 40, 45, 49, 54, 58, 64, 72, 80, 87, 95, 108, 116, 129 };
\r
12 char *(pieceList[]) = {"p", "n", "b", "r", "q", "f", "e", "as", "c", "w", "m",
\r
13 "o", "h", "a", "dk", "g", "d", "v", "l", "s", "u", "k",
\r
14 "wp", "wl", "wn", "ws", "cv", NULL };
\r
15 char kindList[] = "sow";
\r
17 void Load(FILE *f, char data[130][130], char*name)
\r
19 int i, j, k; char h, w, c;
\r
21 if(fscanf(f, "BM%c", &i) != 1) { printf("%s does not have bitmap format\n", name); exit(0); }
\r
22 for(i=0; i<15; i++) fgetc(f); fscanf(f, "%c%c%c%c%c", &h, &i, &i, &i, &w);
\r
23 for(i=0; i<39; i++) fgetc(f); // no checking is done to see if the format is as we expect!!!
\r
25 // printf("h=%d, w=%d\n", h, w);
\r
26 for(i=0; i<h; i++) {
\r
27 for(j=0; j<w; j+=32) {
\r
29 for(k=0; k<8; k++) {
\r
30 data[i][j+k] = c&0x80; c <<= 1;
\r
33 for(k=0; k<8; k++) {
\r
34 data[i][j+k+8] = c&0x80; c <<= 1;
\r
37 for(k=0; k<8; k++) {
\r
38 data[i][j+k+16] = c&0x80; c <<= 1;
\r
41 for(k=0; k<8; k++) {
\r
42 data[i][j+k+24] = c&0x80; c <<= 1;
\r
50 void Paint(char dest[130][130], char src[130][130], int size, char c)
\r
51 { // copy monochrome pixmap to destination as if (transparent, c)
\r
54 for(i=0; i<size; i++) for(j=0; j<size; j++) if(!src[i][j]) dest[i][j] = c;
\r
57 FloodFill(char a[130][130], int size, int x, int y)
\r
59 char old = 'X', new = '.';
\r
60 if(a[x][y] != old) return; else {
\r
62 if(x > 0) FloodFill(a, size, x-1, y);
\r
63 if(y > 0) FloodFill(a, size, x, y-1);
\r
64 if(x < size-1) FloodFill(a, size, x+1, y);
\r
65 if(y < size-1) FloodFill(a, size, x, y+1);
\r
70 void Save(FILE *f, char *name, char data[130][130], int size, char *col, int depth)
\r
71 { // write out data in source format for d x d pixmap with specified square color
\r
74 fprintf(f, "/* XPM */\n");
\r
75 fprintf(f, "static char *%s[] = {\n", name);
\r
76 fprintf(f, "/* columns rows colors chars-per-pixel */\n");
\r
77 fprintf(f, "\"%d %d %d 1\",\n", size, size, depth);
\r
78 fprintf(f, "\" c black s dark_piece\",\n");
\r
79 fprintf(f, "\". %s\",\n", col);
\r
80 if(depth==3) fprintf(f, "\"X c white s light_piece\",\n");
\r
81 fprintf(f, "/* pixels */\n");
\r
82 for(i=size-1; i>=0; i--) {
\r
83 fprintf(f, "\"%s\"%s\n", data[i], i==0 ? "" : ",");
\r
91 char data[130][130], oData[130][130], sData[130][130], wData[130][130];
\r
93 main(int argc, char **argv)
\r
95 int i, j, k, d, cnt, p, s, t; char c, h, w, name[80], buf[80], transparent;
\r
98 transparent = argc > 1 && !strcmp(argv[1], "-t");
\r
100 for(s=0; s<18; s++) for(p=0; pieceList[p] != NULL; p++) {
\r
102 // Load the 3 kinds of Windows monochrome bitmaps (outline, solid, white fill)
\r
104 sprintf(buf, "../winboard/bitmaps/%s%d%c.bmp", pieceList[p], sizeList[s], 'o');
\r
105 printf("try %s\n", buf);
\r
106 f = fopen(buf, "rb");
\r
107 if(f == NULL) continue;
\r
108 Load(f, oData, buf);
\r
110 sprintf(buf, "../winboard/bitmaps/%s%d%c.bmp", pieceList[p], sizeList[s], 's');
\r
111 f = fopen(buf, "rb");
\r
112 if(f == NULL) continue;
\r
113 Load(f, sData, buf);
\r
115 sprintf(buf, "../winboard/bitmaps/%s%d%c.bmp", pieceList[p], sizeList[s], 'w');
\r
116 if(pieceList[p][0]=='w')
\r
117 sprintf(buf, "../winboard/bitmaps/%s%d%c.bmp", "w", sizeList[s], 'w');
\r
118 f = fopen(buf, "rb");
\r
119 if(f == NULL) continue;
\r
120 Load(f, wData, buf);
\r
122 printf("%s loaded\n", buf);
\r
123 // construct pixmaps as character arrays
\r
125 for(i=0; i<d; i++) { for(j=0; j<d; j++) data[i][j] = transparent? '.' : 'X'; data[i][d] = 0; } // fill square
\r
127 Paint(data, sData, d, ' '); // overay with solid piece bitmap
\r
129 if(!transparent) { // background was painted same color as piece details; flood-fill it from corners
\r
130 FloodFill(data, d, 0, 0);
\r
131 FloodFill(data, d, 0, d-1);
\r
132 FloodFill(data, d, d-1, 0);
\r
133 FloodFill(data, d, d-1, d-1);
\r
136 sprintf(buf, "%s%s%d.xpm", pieceList[p], "dd", d);
\r
137 sprintf(name, "%s%s%d", pieceList[p], "dd", d);
\r
138 f = fopen(buf, "w");
\r
139 Save(f, name, data, d, "c green s dark_square", 3);
\r
141 sprintf(buf, "%s%s%d.xpm", pieceList[p], "dl", d);
\r
142 sprintf(name, "%s%s%d", pieceList[p], "dl", d);
\r
143 f = fopen(buf, "w");
\r
144 Save(f, name, data, d, "c gray s light_square", 3); // silly duplication; pixmap is te same, but other color
\r
146 // now the light piece
\r
147 for(i=0; i<d; i++) { for(j=0; j<d; j++) data[i][j] = '.'; data[i][d] = 0; } // fill square
\r
149 Paint(data, wData, d, 'X'); // overay with white-filler piece bitmap
\r
150 Paint(data, oData, d, ' '); // overay with outline piece bitmaps
\r
152 sprintf(buf, "%s%s%d.xpm", pieceList[p], "ld", d);
\r
153 sprintf(name, "%s%s%d", pieceList[p], "ld", d);
\r
154 f = fopen(buf, "w");
\r
155 Save(f, name, data, d, "c green s dark_square", 3);
\r
157 sprintf(buf, "%s%s%d.xpm", pieceList[p], "ll", d);
\r
158 sprintf(name, "%s%s%d", pieceList[p], "ll", d);
\r
159 f = fopen(buf, "w");
\r
160 Save(f, name, data, d, "c gray s light_square", 3);
\r
162 printf("%s saved\n", buf);
\r