1 /* SCCS Id: @(#)tile2bmp.c 3.4 2002/03/14 */
2 /* Copyright (c) NetHack PC Development Team 1995 */
3 /* NetHack may be freely redistributed. See license for details. */
8 * Initial Creation M.Allison 1994/01/11
12 /* #pragma warning(4103:disable) */
20 /* #define COLORS_IN_USE MAXCOLORMAPSIZE /* 256 colors */
22 #define COLORS_IN_USE 256
24 #define COLORS_IN_USE 16 /* 16 colors */
29 extern char *FDECL(tilename, (int, int));
32 #define MAX_X 320 /* 2 per byte, 4 bits per pixel */
36 #define MAX_X (32 * 40)
39 #define MAX_X 640 /* 1 per byte, 8 bits per pixel */
44 /* GCC fix by Paolo Bonzini 1999/03/28 */
46 #define PACK __attribute__((packed))
51 static short leshort(short x)
54 return ((x&0xff)<<8)|((x>>8)&0xff);
61 static long lelong(long x)
64 return ((x&0xff)<<24)|((x&0xff00)<<8)|((x>>8)&0xff00)|((x>>24)&0xff);
71 typedef struct tagBMIH {
75 unsigned short biPlanes;
76 unsigned short biBitCount;
77 unsigned long biCompression;
78 unsigned long biSizeImage;
81 unsigned long biClrUsed;
82 unsigned long biClrImportant;
83 } PACK BITMAPINFOHEADER;
85 typedef struct tagBMFH {
86 unsigned short bfType;
88 unsigned short bfReserved1;
89 unsigned short bfReserved2;
90 unsigned long bfOffBits;
91 } PACK BITMAPFILEHEADER;
93 typedef struct tagRGBQ {
94 unsigned char rgbBlue;
95 unsigned char rgbGreen;
97 unsigned char rgbReserved;
99 #define UINT unsigned int
100 #define DWORD unsigned long
102 #define WORD unsigned short
106 #define BI_BITFIELDS 3L
107 #endif /* __GNUC__ */
111 BITMAPFILEHEADER bmfh;
112 BITMAPINFOHEADER bmih;
114 #define RGBQUAD_COUNT 16
115 RGBQUAD bmaColors[RGBQUAD_COUNT];
118 #define RGBQUAD_COUNT 256
120 #define RGBQUAD_COUNT 16
122 RGBQUAD bmaColors[RGBQUAD_COUNT];
124 #if (COLORS_IN_USE==16)
125 uchar packtile[MAX_Y][MAX_X];
127 uchar packtile[MAX_Y][MAX_X];
128 /* uchar packtile[TILE_Y][TILE_X]; */
133 #define BMPFILESIZE (sizeof(struct tagBMP))
137 pixel tilepixels[TILE_Y][TILE_X];
139 static void FDECL(build_bmfh,(BITMAPFILEHEADER *));
140 static void FDECL(build_bmih,(BITMAPINFOHEADER *));
141 static void FDECL(build_bmptile,(pixel (*)[TILE_X]));
143 char *tilefiles[] = {
145 "../win/share/mon32.txt",
146 "../win/share/obj32.txt",
147 "../win/share/oth32.txt"
149 "../win/share/monsters.txt",
150 "../win/share/objects.txt",
151 "../win/share/other.txt"
157 int max_tiles_in_row = 40;
173 Fprintf(stderr, "usage: %s outfile.bmp\n", argv[0]);
176 strcpy(bmpname, argv[1]);
179 bmpfile2 = fopen(NETHACK_PACKED_TILEFILE, WRBMODE);
180 if (bmpfile2 == (FILE *)0) {
181 Fprintf(stderr, "Unable to open output file %s\n",
182 NETHACK_PACKED_TILEFILE);
188 xoffset = yoffset = 0;
191 fp = fopen(bmpname,"wb");
193 printf("Error creating tile file %s, aborting.\n",bmpname);
196 while (filenum < (sizeof(tilefiles) / sizeof(char *))) {
197 if (!fopen_text_file(tilefiles[filenum], RDTMODE)) {
199 "usage: tile2bmp (from the util directory)\n");
202 num_colors = colorsinmap;
203 if (num_colors > 62) {
204 Fprintf(stderr, "too many colors (%d)\n", num_colors);
208 build_bmfh(&bmp.bmfh);
209 build_bmih(&bmp.bmih);
210 for (i = 0; i < MAX_Y; ++i)
211 for (j = 0; j < MAX_X; ++j)
212 bmp.packtile[i][j] = (uchar)0;
213 for (i = 0; i < num_colors; i++) {
214 bmp.bmaColors[i].rgbRed = ColorMap[CM_RED][i];
215 bmp.bmaColors[i].rgbGreen = ColorMap[CM_GREEN][i];
216 bmp.bmaColors[i].rgbBlue = ColorMap[CM_BLUE][i];
217 bmp.bmaColors[i].rgbReserved = 0;
221 /* printf("Colormap initialized\n"); */
222 while (read_text_tile(tilepixels)) {
223 build_bmptile(tilepixels);
226 xoffset += (TILE_X / 2);
230 if (xoffset >= MAX_X) {
235 (void) fclose_text_file();
238 fwrite(&bmp, sizeof(bmp), 1, fp);
240 Fprintf(stderr, "Total of %d tiles written to %s.\n",
251 BITMAPFILEHEADER *pbmfh;
253 pbmfh->bfType = leshort(0x4D42);
254 pbmfh->bfSize = lelong(BMPFILESIZE);
255 pbmfh->bfReserved1 = (UINT)0;
256 pbmfh->bfReserved2 = (UINT)0;
257 pbmfh->bfOffBits = lelong(sizeof(bmp.bmfh) + sizeof(bmp.bmih) +
258 (RGBQUAD_COUNT * sizeof(RGBQUAD)));
263 BITMAPINFOHEADER *pbmih;
267 pbmih->biSize = lelong(sizeof(bmp.bmih));
269 pbmih->biWidth = lelong(w = MAX_X * 2);
271 pbmih->biWidth = lelong(w = MAX_X);
273 pbmih->biHeight = lelong(h = MAX_Y);
274 pbmih->biPlanes = leshort(1);
276 pbmih->biBitCount = leshort(4);
279 pbmih->biBitCount = leshort(8);
284 else if (cClrBits <= 4)
286 else if (cClrBits <= 8)
288 else if (cClrBits <= 16)
290 else if (cClrBits <= 24)
293 pbmih->biCompression = lelong(BI_RGB);
294 pbmih->biXPelsPerMeter = lelong(0);
295 pbmih->biYPelsPerMeter = lelong(0);
298 pbmih->biClrUsed = lelong(1<<cClrBits);
300 pbmih->biClrUsed = lelong(RGBQUAD_COUNT);
304 pbmih->biSizeImage = lelong(0);
306 pbmih->biSizeImage = lelong(((w * cClrBits +31) & ~31) /8 * h);
308 pbmih->biClrImportant = (DWORD)0;
312 build_bmptile(pixels)
313 pixel (*pixels)[TILE_X];
315 int cur_x, cur_y, cur_color;
318 for (cur_y = 0; cur_y < TILE_Y; cur_y++) {
319 for (cur_x = 0; cur_x < TILE_X; cur_x++) {
320 for (cur_color = 0; cur_color < num_colors; cur_color++) {
321 if (ColorMap[CM_RED][cur_color] == pixels[cur_y][cur_x].r &&
322 ColorMap[CM_GREEN][cur_color]== pixels[cur_y][cur_x].g &&
323 ColorMap[CM_BLUE][cur_color] == pixels[cur_y][cur_x].b)
326 if (cur_color >= num_colors)
327 Fprintf(stderr, "color not in colormap!\n");
328 y = (MAX_Y - 1) - (cur_y + yoffset);
330 x = (cur_x / 2) + xoffset;
331 bmp.packtile[y][x] = cur_x%2 ?
332 (uchar)(bmp.packtile[y][x] | cur_color) :
333 (uchar)(cur_color<<4);
336 bmp.packtile[y][x] = (uchar)cur_color;