1 /*	SCCS Id: @(#)tile2bmp.c	3.2	95/09/06	*/
2 /*   Copyright (c) NetHack PC Development Team 1995                 */
3 /*   NetHack may be freely redistributed.  See license for details. */
4 
5 /*
6  * Edit History:
7  *
8  *	Initial Creation			M.Allison	94/01/11
9  * Marvin was here 			Marvin		97/01/11
10  *
11  */
12 
13 /* #include <stdlib.h>	*/
14 #include "hack.h"
15 #include "tile.h"
16 #include "bitmfile.h"
17 
18 /* #define COLORS_IN_USE MAXCOLORMAPSIZE       /* 256 colors */
19 #define COLORS_IN_USE 16                       /* 16 colors */
20 
21 
22 extern char *FDECL(tilename, (int, int));
23 static void FDECL(build_ximgtile,(pixel (*)[TILE_X]));
24 void get_color(unsigned int colind, struct RGB *rgb);
25 void get_pixel(int x, int y, unsigned int *colind);
26 
27 #if COLORS_IN_USE==16
28 #define MAX_X 320		/* 2 per byte, 4 bits per pixel */
29 #else
30 #define MAX_X 640
31 #endif
32 #define MAX_Y 1200
33 
34 FILE *tibfile2;
35 
36 pixel tilepixels[TILE_Y][TILE_X];
37 
38 char *tilefiles[] = {	"..\\win\\share\\monsters.txt",
39 			"..\\win\\share\\objects.txt",
40 			"..\\win\\share\\other.txt"};
41 
42 unsigned int **Bild_daten;
43 int num_colors = 0;
44 int tilecount;
45 int max_tiles_in_row = 40;
46 int tiles_in_row;
47 int filenum;
48 int initflag;
49 int yoffset,xoffset;
50 char bmpname[128];
51 FILE *fp;
52 
53 int
main(argc,argv)54 main(argc, argv)
55 int argc;
56 char *argv[];
57 {
58 	int i;
59 
60 	if (argc != 2) {
61 		Fprintf(stderr, "usage: tile2img outfile.img\n");
62 		exit(EXIT_FAILURE);
63 	} else
64 		strcpy(bmpname, argv[1]);
65 
66 #ifdef OBSOLETE
67 	bmpfile2 = fopen(NETHACK_PACKED_TILEFILE, WRBMODE);
68 	if (bmpfile2 == (FILE *)0) {
69 		Fprintf(stderr, "Unable to open output file %s\n",
70 				NETHACK_PACKED_TILEFILE);
71 		exit(EXIT_FAILURE);
72 	}
73 #endif
74 
75 	tilecount = 0;
76 	xoffset = yoffset = 0;
77 	initflag = 0;
78 	filenum = 0;
79 	fp = fopen(bmpname,"wb");
80 	if (!fp) {
81 	    printf("Error creating tile file %s, aborting.\n",bmpname);
82 	    exit(1);
83 	}
84 	fclose(fp);
85 
86 	Bild_daten=(unsigned int **)malloc(MAX_Y*sizeof(unsigned int *));
87 	for(i=0;i<MAX_Y;i++)
88 		Bild_daten[i]=(unsigned int *)malloc(MAX_X*sizeof(unsigned int));
89 
90 	while (filenum < 3) {
91 		if (!fopen_text_file(tilefiles[filenum], RDTMODE)) {
92 			Fprintf(stderr,
93 				"usage: tile2img (from the util directory)\n");
94 			exit(EXIT_FAILURE);
95 		}
96 		num_colors = colorsinmap;
97 		if (num_colors > 62) {
98 			Fprintf(stderr, "too many colors (%d)\n", num_colors);
99 			exit(EXIT_FAILURE);
100 		}
101 		while (read_text_tile(tilepixels)) {
102 			build_ximgtile(tilepixels);
103 			tilecount++;
104 			xoffset += TILE_X;
105 			if (xoffset >= MAX_X) {
106 				yoffset += TILE_Y;
107 				xoffset = 0;
108 			}
109 		}
110 		(void) fclose_text_file();
111 		++filenum;
112 	}
113 	Fprintf(stderr, "Total of %d tiles in memory.\n",tilecount);
114 
115 	bitmap_to_file(XIMG, MAX_X, (tilecount/20+1)*16, 372, 372, 4, 16, bmpname, get_color, get_pixel ) ;
116 
117 	Fprintf(stderr, "Total of %d tiles written to %s.\n",tilecount, bmpname);
118 
119 	exit(EXIT_SUCCESS);
120 	/*NOTREACHED*/
121 	return 0;
122 }
123 
get_color(unsigned int colind,struct RGB * rgb)124 void get_color(unsigned int colind, struct RGB *rgb){
125 	rgb->r=(1000L*(long)ColorMap[CM_RED][colind])/0xFF;
126 	rgb->g=(1000L*(long)ColorMap[CM_GREEN][colind])/0xFF;
127 	rgb->b=(1000L*(long)ColorMap[CM_BLUE][colind])/0xFF;
128 }
129 
get_pixel(int x,int y,unsigned int * colind)130 void get_pixel(int x, int y, unsigned int *colind){
131 	*colind=Bild_daten[y][x];
132 }
133 
134 static void
135 build_ximgtile(pixels)
136 pixel (*pixels)[TILE_X];
137 {
138 	int cur_x, cur_y, cur_color;
139 	int x,y;
140 
141 	for (cur_y = 0; cur_y < TILE_Y; cur_y++) {
142 		for (cur_x = 0; cur_x < TILE_X; cur_x++) {
143 		    for (cur_color = 0; cur_color < num_colors; cur_color++) {
144 				if (ColorMap[CM_RED][cur_color] == pixels[cur_y][cur_x].r &&
145 				    ColorMap[CM_GREEN][cur_color] == pixels[cur_y][cur_x].g &&
146 				    ColorMap[CM_BLUE][cur_color] == pixels[cur_y][cur_x].b)
147 					break;
148 		    }
149 		    if (cur_color >= num_colors)
150 				Fprintf(stderr, "color not in colormap!\n");
151 		    y = cur_y + yoffset;
152 		    x = cur_x + xoffset;
153 		    Bild_daten[y][x] =cur_color;
154 		}
155 	}
156 }
157