1 /*   SCCS Id: @(#)txt2bmp.c   3.3     95/01/26                     */
2 /*   Copyright (c) NetHack PC Development Team 1993, 1994, 1995     */
3 /*   NetHack may be freely redistributed.  See license for details. */
4 
5 /*
6  * This creates a txt2bmp.exe
7  *
8  * txt2bmp.exe -i generates 'index'
9  * txt2bmp.exe -h generates 'allegfx.h' THIS FILE IS OBSOLETE
10  * txt2bmp.exe -fXX generates all the *.bmp
11  * txt2bmp.exe -bXX generates slamXX.bmp
12  * txt2bmp.exe -c txtname bmpname converts txtname to a single bmpname
13  * txt2bmp.exe -p bmpname changes the bitmap to a 256 color bmp with
14  *		  the pink as the first color
15  *
16  * Edit History:
17  *
18  *      Initial Creation                        K.Youngblood    ??/??/??
19  *      added 32x32 text tile support		W.Cheung	00/06/19
20  *	added direct write to files		W.Cheung	00/06/21
21  *
22  */
23 
24 #include <allegro.h>
25 
26 #include "hack.h"
27 #include "pcvideo.h"
28 #include "tile.h"
29 #include "pctiles.h"
30 
31 #include <ctype.h>
32 #include <dos.h>
33 #ifndef MONITOR_HEAP
34 #include <stdlib.h>
35 #endif
36 #include <time.h>
37 
38 extern char *FDECL(tilename, (int, int));
39 
40 #define Fprintf (void) fprintf
41 #define Fclose  (void) fclose
42 
43 static int num_colors;
44 static pixel pixels[MAX_TILE_Y][MAX_TILE_X];
45 static BITMAP *bigtile_bmp;
46 
47 static struct tibhdr_struct tibheader;
48 
49 /* [WAC] Use the 32x32x262 color tiles */
50 
51 static const char
52     *tilefiles3d[] = {  "../win/share/mon3d.txt",
53                         "../win/share/obj3d.txt",
54                         "../win/share/oth3d.txt"},
55     *tilefiles32[] = {  "../win/share/mon32.txt",
56                         "../win/share/obj32.txt",
57                         "../win/share/oth32.txt"},
58     *tilefiles16[] = {  "../win/share/monsters.txt",
59                         "../win/share/objects.txt",
60                         "../win/share/other.txt"};
61 
62 static const char
63     *Dont_Edit_Code =
64 	"/* This source file is generated by 'txt2bmp'.  Do not edit. */\n",
65     *Dont_Edit_Index =
66 	"# This index file was generated by 'txt2bmp'.\n";
67 
68 static int tilecount;
69 static int filenum;
70 static int paletteflag;
71 
72 #define TRANS_COLOR MASK_COLOR_24
73 
74 static const char
75     *bigtile_file3d = "../src/slam3D.bmp",
76     *bigtile_file32 = "../src/slam32.bmp",
77     *bigtile_file16 = "../src/slam16.bmp",
78     *index_file = "../src/index",
79     *allegfx_file = "../include/allegfx.h";
80 
81 /* #define FINAL_COLORDEPTH_8_BPP */
82 
83 /* Currently interpolation is OFF */
84 /* Should handle all sizes tile files now */
85 void
86 make_bitmap(pixels, tilecount, trans, filename, make_big)
87 pixel (*pixels)[MAX_TILE_X];
88 int tilecount;
89 int trans;
90 char *filename;
91 boolean make_big;
92 {
93         int i, j, x, y;
94         long color, color2;
95         char cmd[60];
96         BITMAP *tilebmp;
97 
98         tilebmp = create_bitmap(tile_x, tile_y);
99 
100         color2 = 0L;
101         color2 |= 71;
102         color2 |= (108) << 8;
103         color2 |= (108) << 16;
104 
105         /* load the origional tile */
106 
107         for (j = 0; j < tile_y; j++) {
108                 for (i = 0; i < tile_x; i++)
109                 {
110                         color = 0L;
111                         color |= (pixels[j][i].r & 0x0000ff);
112                         color |= (pixels[j][i].g & 0x0000ff) << 8;
113                         color |= (pixels[j][i].b & 0x0000ff) << 16;
114                         putpixel(tilebmp, i, j, color);
115                 }
116         }
117 
118 #if 0
119         drawing_mode(DRAW_MODE_TRANS, tilebmp, 0, 0);
120         set_trans_blender(255, 255, 255, 127);
121 
122         for(y = 0; y < tile_y; y++)
123                 for(x = 1; x < tile_x; x += 2)
124                 {
125                         color=getpixel(tilebmp, min(x+1,tile_x - 2),y);
126                         putpixel(tilebmp, x, y, color);
127                 }
128         /* interpolate horizontal lines */
129 
130         for(x = 0; x < tile_x; x++)
131                 for(y = 1; y < tile_y; y += 2)
132                 {
133                         color = getpixel(tilebmp, x, min(y+1,tile_y - 2));
134                         putpixel(tilebmp, x, y, color);
135                 }
136 #endif
137 
138         drawing_mode(DRAW_MODE_SOLID, tilebmp, 0, 0);
139 
140         if(trans)
141         {
142 		/* WAC probably slower, but more accurate */
143 		for (x = 0; x < tile_x; x++)
144 		    for (y = 0; y < tile_y; y++)
145 	                if(getpixel(tilebmp, x, y) == color2)
146         	                putpixel(tilebmp,x,y, TRANS_COLOR);
147 #if 0
148                 if(getpixel(tilebmp, 0, 0) == color2)
149                         floodfill(tilebmp,0,0, TRANS_COLOR);
150                 if(getpixel(tilebmp, 0, 31) == color2)
151                         floodfill(tilebmp,0,31, TRANS_COLOR);
152                 if(getpixel(tilebmp, 31, 0) == color2)
153                         floodfill(tilebmp,31,0, TRANS_COLOR);
154                 if(getpixel(tilebmp, 31, 31) == color2)
155                         floodfill(tilebmp,31,31, TRANS_COLOR);
156 #endif
157         }
158 
159 	if (!make_big) {
160             save_bitmap(filename, tilebmp, 0);
161 	} else {
162 	    int col = (int)(tilecount % TILES_PER_ROW);
163 	    int row = (int)(tilecount / TILES_PER_ROW);
164 #ifdef DEBUG
165 	    Fprintf(stderr, "col: %i row: %i\n", col, row);
166 #endif
167 	    blit(tilebmp, bigtile_bmp, 0, 0, col * tile_x, row * tile_y, tile_x, tile_y);
168 	}
169         destroy_bitmap(tilebmp);
170 }
171 
172 int
main(argc,argv)173 main(argc, argv)
174 int argc;
175 char *argv[];
176 {
177         int i;
178         struct tm *newtime;
179         time_t aclock;
180         char *paletteptr;
181         char cmd2[60];
182         char filename[60];
183         char util_mode;
184         FILE *ofp; /* Output for -i, -h */
185         const char **tilefiles;
186         char bigtile_file[BUFSZ];
187         boolean trans_background = FALSE;
188 
189 	/* All operations assume 24 bit internal files */
190         set_color_depth(24);
191 
192         if (argc < 2) {
193 	    	Fprintf(stderr, "Bad arg count (%d).\n", argc-1);
194 	    	(void) fflush(stderr);
195                 exit(EXIT_FAILURE);
196         }
197 
198         util_mode = argv[1][1];
199 
200         if (util_mode == 'I') util_mode = 'i';
201         if (util_mode == 'H') util_mode = 'h';
202         if (util_mode == 'F') util_mode = 'f';
203         if (util_mode == 'B') util_mode = 'b';
204         if (util_mode == 'C') util_mode = 'c';
205 
206         if (util_mode != 'i' && util_mode != 'h' && util_mode != 'f'
207 			     && util_mode != 'b' && util_mode != 'c'
208 			     && util_mode != 'p') {
209 	    	Fprintf(stderr, "Unknown option '-%c'.\n Use -i, -h, -f, -b, -c or -p.\n",
210 	    					util_mode);
211 	    	(void) fflush(stderr);
212                 exit(EXIT_FAILURE);
213         }
214 
215         if (util_mode == 'p') {
216             if (argc != 3) {
217 	    	Fprintf(stderr, "Bad arg count (%d).\n", argc-1);
218 	    	(void) fflush(stderr);
219                 exit(EXIT_FAILURE);
220             } else {
221 #ifdef FINAL_COLORDEPTH_8_BPP
222             	PALETTE tmp_pal;
223             	BITMAP* tmp_bmp;
224         	char rsvd[256];
225         	int i;
226 
227         	Strcpy(bigtile_file, argv[2]);
228             	bigtile_bmp = load_bitmap(bigtile_file,tmp_pal);
229 
230             	if (!bigtile_bmp) {
231 	    		Fprintf(stderr, "Unable to load %s.\n", argv[2]);
232 	    		(void) fflush(stderr);
233                 	exit(EXIT_FAILURE);
234             	}
235 
236 		tmp_pal[0].r = tmp_pal[0].b = 63;
237 		tmp_pal[0].g = 0;
238 
239 		rsvd[0] = 1;
240 		for (i = 1; i < 256; i++) rsvd[i] = 0;
241 
242         	generate_optimized_palette(bigtile_bmp, tmp_pal, rsvd);
243         	select_palette(tmp_pal);
244 
245         	tmp_bmp = create_bitmap_ex(8, bigtile_bmp->w, bigtile_bmp->h);
246 
247 		blit(bigtile_bmp, tmp_bmp,  0, 0, 0, 0,bigtile_bmp->w, bigtile_bmp->h);
248 
249         	save_bitmap(bigtile_file, tmp_bmp, tmp_pal);
250 
251         	destroy_bitmap(bigtile_bmp);
252         	destroy_bitmap(tmp_bmp);
253 
254 #endif
255 	        exit(EXIT_SUCCESS);
256             }
257         }
258 
259         if ((util_mode == 'c' && argc != 4) || (util_mode != 'c' && argc != 2)) {
260 	    	Fprintf(stderr, "Bad arg count (%d).\n", argc-1);
261 	    	(void) fflush(stderr);
262                 exit(EXIT_FAILURE);
263         }
264 
265         if (util_mode != 'c') {
266 	    if (argv[1][2] == '1' && argv[1][3] == '6') {
267     		Fprintf(stderr, "Using 16x16 text tile files\n");
268     		(void) fflush(stderr);
269 		tilefiles = tilefiles16;
270 		Strcpy(bigtile_file, bigtile_file16);
271 	    } else if (argv[1][2] == '3' &&
272 		    	(argv[1][3] == 'D' || argv[1][3] == 'd')) {
273     		Fprintf(stderr, "Using 48x64 text tile files\n");
274     		(void) fflush(stderr);
275 		tilefiles = tilefiles3d;
276 		Strcpy(bigtile_file, bigtile_file3d);
277 		trans_background = TRUE;
278 	    } else {
279 	       	/* Default mode is 32 */
280     		Fprintf(stderr, "Using 32x32 text tile files\n");
281     		(void) fflush(stderr);
282 		tilefiles = tilefiles32;
283 		Strcpy(bigtile_file, bigtile_file32);
284 	    }
285         } else {
286         	Strcpy(bigtile_file, argv[3]);
287         }
288 
289         time(&aclock);
290         newtime = localtime(&aclock);
291 
292         tilecount = 0;
293         paletteflag = 0;
294         filenum = 0;
295 
296         /* Open file (if needed) */
297         if (util_mode == 'i') {
298 	    if (!(ofp = fopen(index_file, WRTMODE))) {
299 		perror(index_file);
300 		exit(EXIT_FAILURE);
301 	    }
302 	    Fprintf(ofp,Dont_Edit_Index);
303         } else if (util_mode == 'h') {
304 	    if (!(ofp = fopen(allegfx_file, WRTMODE))) {
305 		perror(allegfx_file);
306 		exit(EXIT_FAILURE);
307 	    }
308     	    Fprintf(ofp,"/*\tSCCS Id: @(#)allegfx.h\t3.2\t96/05/17 */\n\n");
309 	    Fprintf(ofp,Dont_Edit_Code);
310 	    Fprintf(ofp,"/*\tNOTE: This file is completely obselete! */\n"
311 		"/*\tI have no idea why you made it */\n\n");
312         } else if (util_mode == 'b' || util_mode == 'c') {
313             bigtile_bmp = NULL;
314         }
315 
316         while (filenum < 3) {
317                 int index = 0;
318                 boolean trans_mode = ((filenum < 2) || trans_background);
319 
320 		if (util_mode != 'c') {
321                     if (!fopen_text_file(tilefiles[filenum], RDTMODE)) {
322                         Fprintf(stderr,
323                          "Cannot find file '%s'.\n", tilefiles[filenum]);
324                         exit(EXIT_FAILURE);
325                     }
326 		} else if (!fopen_text_file(argv[2], RDTMODE)) {
327                         Fprintf(stderr,
328                          "Cannot find file '%s'.\n", tilefiles[filenum]);
329                         exit(EXIT_FAILURE);
330 		}
331 
332                 num_colors = colorsinmap;
333                 if (num_colors > MAXCOLORMAPSIZE) {
334                         Fprintf(stderr, "too many colors (%d)\n", num_colors);
335                         exit(EXIT_FAILURE);
336                 }
337                 if (!paletteflag) {
338                         paletteptr = tibheader.palette;
339                         for (i = 0; i < num_colors; i++) {
340                                 *paletteptr++ = ColorMap[CM_RED][i],
341                                 *paletteptr++ = ColorMap[CM_GREEN][i],
342                                 *paletteptr++ = ColorMap[CM_BLUE][i];
343                         }
344                         paletteflag++;
345                 }
346 
347 	    	if ((util_mode == 'b' || util_mode == 'c') && !bigtile_bmp) {
348 	    	    /* Only make this once */
349 	            bigtile_bmp = create_bitmap(tile_x * TILES_PER_ROW, tile_y * TILES_PER_COL);
350 	    	}
351 
352                 while (read_text_tile(pixels)) {
353                         sprintf(filename, "%c%03d%s",
354                                 (!filenum) ? 'm' : (filenum == 1) ? 'o' : 'e',
355                                 index, ".bmp");
356 			if (util_mode == 'i') {
357                             Fprintf(ofp, "%04d (%s) : %s\n",
358                                 tilecount, tilename(filenum+1,index),
359                                 filename);
360 			} else if (util_mode == 'f') {
361                                 make_bitmap(pixels, tilecount, trans_mode
362                                             ,filename, FALSE);
363 			} else if (util_mode == 'b' || util_mode == 'c') {
364                                 make_bitmap(pixels, tilecount, trans_mode
365                                             ,filename, TRUE);
366 			}
367 
368                         tilecount++;
369                         index++;
370                 }
371 
372                 if (util_mode == 'h')
373                     switch(filenum)
374                     {
375 		            case 0:
376 		                Fprintf(ofp, "#define NUMBER_OF_MONS %d\n",
377 		                        tilecount);
378 		                break;
379 		            case 1:
380 		                Fprintf(ofp, "#define NUMBER_OF_OBJS %d\n",
381 		                        tilecount);
382 		                break;
383 		            case 2:
384 		                Fprintf(ofp, "#define NUMBER_OF_TILES %d\n",
385 		                        tilecount);
386 		                break;
387                     }
388 
389 		if (util_mode == 'f')
390 		    Fprintf(stderr, "%d tiles processed from %s\n",
391 					index, tilefiles[filenum]);
392 
393                 (void) fclose_text_file();
394                 ++filenum;
395 
396                 /* Only process 1 file */
397                 if (util_mode == 'c') break;
398         }
399 
400 	/* Close file */
401         if ((util_mode == 'i') || (util_mode == 'h'))
402         	Fclose(ofp);
403         else if (util_mode == 'f')
404 		Fprintf(stderr, "Total of %d bmp tiles written.\n", tilecount);
405         else if (util_mode == 'b' || util_mode == 'c') {
406         	PALETTE tmp_pal;
407 #ifdef FINAL_COLORDEPTH_8_BPP
408         	BITMAP *tmp_bmp;
409         	char rsvd[256];
410         	int i;
411 
412 		tmp_pal[0].r = tmp_pal[0].b = 63;
413 		tmp_pal[0].g = 0;
414 
415 		rsvd[0] = 1;
416 		for (i = 1; i < 256; i++) rsvd[i] = 0;
417 
418         	generate_optimized_palette(bigtile_bmp, tmp_pal, rsvd);
419         	select_palette(tmp_pal);
420 
421         	tmp_bmp = create_bitmap_ex(8, TILES_PER_ROW * tile_x,
422        				(int)(tile_y * (1 + (tilecount / TILES_PER_ROW))));
423 
424 		blit(bigtile_bmp, tmp_bmp,  0, 0, 0, 0,bigtile_bmp->w, bigtile_bmp->h);
425 
426         	save_bitmap(bigtile_file, tmp_bmp, tmp_pal);
427 
428         	destroy_bitmap(bigtile_bmp);
429         	destroy_bitmap(tmp_bmp);
430 
431 #else
432         	save_bitmap(bigtile_file, bigtile_bmp, tmp_pal);
433 
434         	destroy_bitmap(bigtile_bmp);
435 
436 #endif
437         }
438 
439         exit(EXIT_SUCCESS);
440         /*NOTREACHED*/
441         return 0;
442 }
443 
444