OSDN Git Service

guarantee an alter on oracle
[nethackexpress/trunk.git] / win / X11 / tile2x11.c
1 /*
2  * Convert the given input files into an output file that is expected
3  * by nethack.
4  * 
5  * Assumptions:
6  *      + Two dimensional byte arrays are in row order and are not padded
7  *        between rows (x11_colormap[][]).
8  */
9 #include "hack.h"               /* for MAX_GLYPH */
10 #include "tile.h"
11 #include "tile2x11.h"           /* x11 output file header structure */
12
13 #define OUTNAME "x11tiles"      /* output file name */
14 /* #define PRINT_COLORMAP */    /* define to print the colormap */
15
16
17 x11_header      header;
18 unsigned char   tile_bytes[TILE_X*TILE_Y*(MAX_GLYPH+TILES_PER_ROW)];
19 unsigned char   *curr_tb = tile_bytes;
20 unsigned char   x11_colormap[MAXCOLORMAPSIZE][3];
21
22
23 /* Look up the given pixel and return its colormap index. */
24 static unsigned char
25 pix_to_colormap(pix)
26     pixel pix;
27 {
28     int i;
29
30     for (i = 0; i < header.ncolors; i++) {
31         if (pix.r == ColorMap[CM_RED][i] &&
32                 pix.g == ColorMap[CM_GREEN][i] &&
33                 pix.b == ColorMap[CM_BLUE][i])
34             break;
35     }
36
37     if (i == header.ncolors) {
38         Fprintf(stderr, "can't find color: [%u,%u,%u]\n", pix.r, pix.g, pix.b);
39         exit(1);
40     }
41     return (unsigned char) (i & 0xFF);
42 }
43
44
45 /* Convert the tiles in the file to our format of bytes. */
46 static unsigned long
47 convert_tiles(tb_ptr, total)
48     unsigned char **tb_ptr;     /* pointer to a tile byte pointer */
49     unsigned long total;        /* total tiles so far */
50 {
51     unsigned char *tb = *tb_ptr;
52     unsigned long count = 0;
53     pixel tile[TILE_Y][TILE_X];
54     int x, y;
55
56     while (read_text_tile(tile)) {
57         count++;
58         total++;
59         for (y = 0; y < TILE_Y; y++) {
60             for (x = 0; x < TILE_X; x++)
61                 tb[x] = pix_to_colormap(tile[y][x]);
62             tb += TILE_X * header.per_row;
63         }
64
65         /* repoint at the upper-left corner of the next tile */
66         *tb_ptr += TILE_X;
67         if (header.per_row == 1 || (total % header.per_row) == 0)
68             *tb_ptr += TILE_X * (TILE_Y - 1) * header.per_row;
69         tb = *tb_ptr;
70     }
71
72     return count;
73 }
74
75
76 /* Merge the current text colormap (ColorMap) with ours (x11_colormap). */
77 static void
78 merge_text_colormap()
79 {
80     int i, j;
81
82     for (i = 0; i < colorsinmap; i++) {
83         for (j = 0; j < header.ncolors; j++)
84             if (x11_colormap[j][CM_RED] == ColorMap[CM_RED][i] &&
85                     x11_colormap[j][CM_GREEN] == ColorMap[CM_GREEN][i] &&
86                     x11_colormap[j][CM_BLUE] == ColorMap[CM_BLUE][i])
87                 break;
88
89         if (j >= MAXCOLORMAPSIZE) {
90             Fprintf(stderr, "colormap overflow\n");
91             exit(1);
92         }
93
94         if (j == header.ncolors) {      /* couldn't find it */
95 #ifdef PRINT_COLORMAP
96             printf("color %2d: %3d %3d %3d\n", header.ncolors,
97                 ColorMap[CM_RED][i], ColorMap[CM_GREEN][i],
98                 ColorMap[CM_BLUE][i]);
99 #endif
100
101             x11_colormap[j][CM_RED] = ColorMap[CM_RED][i];
102             x11_colormap[j][CM_GREEN] = ColorMap[CM_GREEN][i];
103             x11_colormap[j][CM_BLUE] = ColorMap[CM_BLUE][i];
104             header.ncolors++;
105         }
106     }
107 }
108
109
110 /* Open the given file, read & merge the colormap, convert the tiles. */
111 static void
112 process_file(fname)
113     char *fname;
114 {
115     unsigned long count;
116
117     if (!fopen_text_file(fname, RDTMODE)) {
118         Fprintf(stderr, "can't open file \"%s\"\n", fname);
119         exit(1);
120     }
121     merge_text_colormap();
122     count = convert_tiles(&curr_tb, header.ntiles);
123     Fprintf(stderr, "%s: %lu tiles\n", fname, count);
124     header.ntiles += count;
125     fclose_text_file();
126 }
127
128
129 #ifdef USE_XPM
130 static int
131 xpm_write(fp)
132 FILE *fp;
133 {
134     int i, j, n;
135
136     if (header.ncolors > 64) {
137         Fprintf(stderr, "Sorry, only configured for up to 64 colors\n");
138         exit(1);
139         /* All you need to do is add more char per color - below */
140     }
141
142     Fprintf(fp, "/* XPM */\n");
143     Fprintf(fp, "static char* nhtiles[] = {\n");
144     Fprintf(fp, "\"%lu %lu %lu %d\",\n",
145                 header.tile_width*header.per_row,
146                 (header.tile_height*header.ntiles)/header.per_row,
147                 header.ncolors,
148                 1 /* char per color */);
149     for (i = 0; i < header.ncolors; i++)
150         Fprintf(fp, "\"%c  c #%02x%02x%02x\",\n",
151                 i+'0', /* just one char per color */
152                 x11_colormap[i][0],
153                 x11_colormap[i][1],
154                 x11_colormap[i][2]);
155
156     n = 0;
157     for (i = 0; i < (header.tile_height*header.ntiles)/header.per_row; i++) {
158         Fprintf(fp, "\"");
159         for (j = 0; j < header.tile_width*header.per_row; j++) {
160             /* just one char per color */
161             fputc(tile_bytes[n++]+'0', fp);
162         }
163
164         Fprintf(fp, "\",\n");
165     }
166
167     return fprintf(fp, "};\n") >= 0;
168 }
169 #endif  /* USE_XPM */
170
171 int
172 main(argc, argv)
173     int argc;
174     char **argv;
175 {
176     FILE *fp;
177     int i;
178
179     header.version      = 2;            /* version 1 had no per_row field */
180     header.ncolors      = 0;
181     header.tile_width   = TILE_X;
182     header.tile_height  = TILE_Y;
183     header.ntiles       = 0;            /* updated as we read in files */
184     header.per_row      = TILES_PER_ROW;
185
186     if (argc == 1) {
187         Fprintf(stderr, "usage: %s txt_file1 [txt_file2 ...]\n", argv[0]);
188         exit(1);
189     }
190
191     fp = fopen(OUTNAME, "w");
192     if (!fp) {
193         Fprintf(stderr, "can't open output file\n");
194         exit(1);
195     }
196
197     /* don't leave garbage at end of partial row */
198     (void) memset((genericptr_t)tile_bytes, 0, sizeof(tile_bytes));
199
200     for (i = 1; i < argc; i++)
201         process_file(argv[i]);
202     Fprintf(stderr, "Total tiles: %ld\n", header.ntiles);
203
204     /* round size up to the end of the row */
205     if ((header.ntiles % header.per_row) != 0) {
206         header.ntiles += header.per_row - (header.ntiles % header.per_row);
207     }
208
209 #ifdef USE_XPM
210     if (xpm_write(fp) == 0) {
211         Fprintf(stderr, "can't write XPM file\n");
212         exit(1);
213     }
214 #else
215     if (fwrite((char *)&header, sizeof(x11_header), 1, fp) == 0) {
216         Fprintf(stderr, "can't open output header\n");
217         exit(1);
218     }
219
220     if (fwrite((char *)x11_colormap, 1, header.ncolors*3, fp) == 0) {
221         Fprintf(stderr, "can't write output colormap\n");
222         exit(1);
223     }
224
225     if (fwrite((char *)tile_bytes, 1,
226         (int) header.ntiles*header.tile_width*header.tile_height, fp) == 0) {
227
228         Fprintf(stderr, "can't write tile bytes\n");
229         exit(1);
230     }
231 #endif
232
233     fclose(fp);
234     return 0;
235 }