2 * Convert the given input files into an output file that is expected
6 * + Two dimensional byte arrays are in row order and are not padded
7 * between rows (x11_colormap[][]).
9 #include "hack.h" /* for MAX_GLYPH */
11 #include "tile2x11.h" /* x11 output file header structure */
13 #define OUTNAME "x11tiles" /* output file name */
14 /* #define PRINT_COLORMAP */ /* define to print the colormap */
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];
23 /* Look up the given pixel and return its colormap index. */
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])
37 if (i == header.ncolors) {
38 Fprintf(stderr, "can't find color: [%u,%u,%u]\n", pix.r, pix.g, pix.b);
41 return (unsigned char) (i & 0xFF);
45 /* Convert the tiles in the file to our format of bytes. */
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 */
51 unsigned char *tb = *tb_ptr;
52 unsigned long count = 0;
53 pixel tile[TILE_Y][TILE_X];
56 while (read_text_tile(tile)) {
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;
65 /* repoint at the upper-left corner of the next tile */
67 if (header.per_row == 1 || (total % header.per_row) == 0)
68 *tb_ptr += TILE_X * (TILE_Y - 1) * header.per_row;
76 /* Merge the current text colormap (ColorMap) with ours (x11_colormap). */
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])
89 if (j >= MAXCOLORMAPSIZE) {
90 Fprintf(stderr, "colormap overflow\n");
94 if (j == header.ncolors) { /* couldn't find it */
96 printf("color %2d: %3d %3d %3d\n", header.ncolors,
97 ColorMap[CM_RED][i], ColorMap[CM_GREEN][i],
98 ColorMap[CM_BLUE][i]);
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];
110 /* Open the given file, read & merge the colormap, convert the tiles. */
117 if (!fopen_text_file(fname, RDTMODE)) {
118 Fprintf(stderr, "can't open file \"%s\"\n", fname);
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;
136 if (header.ncolors > 64) {
137 Fprintf(stderr, "Sorry, only configured for up to 64 colors\n");
139 /* All you need to do is add more char per color - below */
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,
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 */
157 for (i = 0; i < (header.tile_height*header.ntiles)/header.per_row; i++) {
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);
164 Fprintf(fp, "\",\n");
167 return fprintf(fp, "};\n") >= 0;
179 header.version = 2; /* version 1 had no per_row field */
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;
187 Fprintf(stderr, "usage: %s txt_file1 [txt_file2 ...]\n", argv[0]);
191 fp = fopen(OUTNAME, "w");
193 Fprintf(stderr, "can't open output file\n");
197 /* don't leave garbage at end of partial row */
198 (void) memset((genericptr_t)tile_bytes, 0, sizeof(tile_bytes));
200 for (i = 1; i < argc; i++)
201 process_file(argv[i]);
202 Fprintf(stderr, "Total tiles: %ld\n", header.ntiles);
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);
210 if (xpm_write(fp) == 0) {
211 Fprintf(stderr, "can't write XPM file\n");
215 if (fwrite((char *)&header, sizeof(x11_header), 1, fp) == 0) {
216 Fprintf(stderr, "can't open output header\n");
220 if (fwrite((char *)x11_colormap, 1, header.ncolors*3, fp) == 0) {
221 Fprintf(stderr, "can't write output colormap\n");
225 if (fwrite((char *)tile_bytes, 1,
226 (int) header.ntiles*header.tile_width*header.tile_height, fp) == 0) {
228 Fprintf(stderr, "can't write tile bytes\n");