1 /* aNetHack 0.0.1 tile2bmp.c $ANH-Date: 1451442061 2015/12/30 02:21:01 $ $ANH-Branch: aNetHack-3.6.0 $:$ANH-Revision: 1.15 $ */
2 /* Copyright (c) aNetHack PC Development Team 1995 */
3 /* aNetHack may be freely redistributed. See license for details. */
8 * Initial Creation M.Allison 1994/01/11
9 * 256 colour bmp and statue support M.Allison 2015/04/19
13 /* #pragma warning(4103:disable) */
22 #if defined(UINT32_MAX) && defined(INT32_MAX) && defined(UINT16_MAX)
24 #define UINT16 uint16_t
25 #define UINT32 uint32_t
29 #define UINT8 unsigned char
30 #define UINT16 unsigned short
31 #define UINT32 unsigned long
37 #define COLORS_IN_USE 256
39 /*#define COLORS_IN_USE 16 */ /* 16 colors */
40 #define COLORS_IN_USE 256 /* 256 colors */
45 extern char *FDECL(tilename
, (int, int));
47 #define MAGICTILENO (340 + 440 + 231 + 340)
50 #define MAX_X 320 /* 2 per byte, 4 bits per pixel */
54 #define MAX_X (32 * 40)
55 #define MAX_Y ((MAGICTILENO * 32) / 40) * 2
57 #define MAX_X (16 * 40)
58 #define MAX_Y ((MAGICTILENO * 16) / 40) * 2
62 /* GCC fix by Paolo Bonzini 1999/03/28 */
64 #define PACK __attribute__((packed))
73 return ((x
& 0xff) << 8) | ((x
>> 8) & 0xff);
83 return ((x
& 0xff) << 24) | ((x
& 0xff00) << 8) | ((x
>> 8) & 0xff00)
91 typedef struct tagBMIH
{
99 INT32 biXPelsPerMeter
;
100 INT32 biYPelsPerMeter
;
102 UINT32 biClrImportant
;
103 } PACK BITMAPINFOHEADER
;
105 typedef struct tagBMFH
{
111 } PACK BITMAPFILEHEADER
;
113 typedef struct tagRGBQ
{
124 #define BI_BITFIELDS 3L
125 #endif /* __GNUC__ */
129 BITMAPFILEHEADER bmfh
;
130 BITMAPINFOHEADER bmih
;
132 #define RGBQUAD_COUNT 16
133 RGBQUAD bmaColors
[RGBQUAD_COUNT
];
136 #define RGBQUAD_COUNT 256
138 /*#define RGBQUAD_COUNT 16 */
139 #define RGBQUAD_COUNT 256
141 RGBQUAD bmaColors
[RGBQUAD_COUNT
];
143 #if (COLORS_IN_USE == 16)
144 uchar packtile
[MAX_Y
][MAX_X
];
146 uchar packtile
[MAX_Y
][MAX_X
];
147 /* uchar packtile[TILE_Y][TILE_X]; */
152 #define BMPFILESIZE (sizeof(struct tagBMP))
156 pixel tilepixels
[TILE_Y
][TILE_X
];
158 static void FDECL(build_bmfh
, (BITMAPFILEHEADER
*));
159 static void FDECL(build_bmih
, (BITMAPINFOHEADER
*));
160 static void FDECL(build_bmptile
, (pixel(*) [TILE_X
]));
162 char *tilefiles
[] = {
164 "../win/share/mon32.txt", "../win/share/obj32.txt",
165 "../win/share/oth32.txt",
167 "../win/share/monsters.txt", "../win/share/objects.txt",
168 "../win/share/other.txt",
174 int max_tiles_in_row
= 40;
179 int yoffset
, xoffset
;
191 Fprintf(stderr
, "usage: %s outfile.bmp\n", argv
[0]);
194 strcpy(bmpname
, argv
[1]);
197 bmpfile2
= fopen(ANETHACK_PACKED_TILEFILE
, WRBMODE
);
198 if (bmpfile2
== (FILE *) 0) {
199 Fprintf(stderr
, "Unable to open output file %s\n",
200 ANETHACK_PACKED_TILEFILE
);
206 xoffset
= yoffset
= 0;
210 fp
= fopen(bmpname
, "wb");
212 printf("Error creating tile file %s, aborting.\n", bmpname
);
216 filenum
= pass
% (sizeof(tilefiles
) / sizeof(char *));
217 if (!fopen_text_file(tilefiles
[filenum
], RDTMODE
)) {
218 Fprintf(stderr
, "usage: tile2bmp (from the util directory)\n");
221 num_colors
= colorsinmap
;
222 if (num_colors
> 62) {
223 Fprintf(stderr
, "too many colors (%d)\n", num_colors
);
227 build_bmfh(&bmp
.bmfh
);
228 build_bmih(&bmp
.bmih
);
229 for (i
= 0; i
< MAX_Y
; ++i
)
230 for (j
= 0; j
< MAX_X
; ++j
)
231 bmp
.packtile
[i
][j
] = (uchar
) 0;
232 for (i
= 0; i
< num_colors
; i
++) {
233 bmp
.bmaColors
[i
].rgbRed
= ColorMap
[CM_RED
][i
];
234 bmp
.bmaColors
[i
].rgbGreen
= ColorMap
[CM_GREEN
][i
];
235 bmp
.bmaColors
[i
].rgbBlue
= ColorMap
[CM_BLUE
][i
];
236 bmp
.bmaColors
[i
].rgbReserved
= 0;
240 set_grayscale(pass
== 3);
241 /* printf("Colormap initialized\n"); */
242 while (read_text_tile(tilepixels
)) {
243 build_bmptile(tilepixels
);
246 xoffset
+= (TILE_X
/ 2);
250 if (xoffset
>= MAX_X
) {
255 (void) fclose_text_file();
258 fwrite(&bmp
, sizeof(bmp
), 1, fp
);
260 Fprintf(stderr
, "Total of %d tiles written to %s.\n", tilecount
, bmpname
);
269 BITMAPFILEHEADER
*pbmfh
;
271 pbmfh
->bfType
= leshort(0x4D42);
272 pbmfh
->bfSize
= lelong(BMPFILESIZE
);
273 pbmfh
->bfReserved1
= (UINT32
) 0;
274 pbmfh
->bfReserved2
= (UINT32
) 0;
275 pbmfh
->bfOffBits
= lelong(sizeof(bmp
.bmfh
) + sizeof(bmp
.bmih
)
276 + (RGBQUAD_COUNT
* sizeof(RGBQUAD
)));
281 BITMAPINFOHEADER
*pbmih
;
285 pbmih
->biSize
= lelong(sizeof(bmp
.bmih
));
287 pbmih
->biWidth
= lelong(w
= MAX_X
* 2);
289 pbmih
->biWidth
= lelong(w
= MAX_X
);
291 pbmih
->biHeight
= lelong(h
= MAX_Y
);
292 pbmih
->biPlanes
= leshort(1);
294 pbmih
->biBitCount
= leshort(4);
297 pbmih
->biBitCount
= leshort(8);
302 else if (cClrBits
<= 4)
304 else if (cClrBits
<= 8)
306 else if (cClrBits
<= 16)
308 else if (cClrBits
<= 24)
312 pbmih
->biCompression
= lelong(BI_RGB
);
313 pbmih
->biXPelsPerMeter
= lelong(0);
314 pbmih
->biYPelsPerMeter
= lelong(0);
317 pbmih
->biClrUsed
= lelong(1 << cClrBits
);
319 pbmih
->biClrUsed
= lelong(RGBQUAD_COUNT
);
323 /* pbmih->biSizeImage = lelong(0); */
324 pbmih
->biSizeImage
= lelong(((w
* cClrBits
+ 31) & ~31) / 8 * h
);
326 pbmih
->biSizeImage
= lelong(((w
* cClrBits
+ 31) & ~31) / 8 * h
);
328 pbmih
->biClrImportant
= (DWORD
) 0;
332 build_bmptile(pixels
)
333 pixel (*pixels
)[TILE_X
];
335 int cur_x
, cur_y
, cur_color
, apply_color
;
338 for (cur_y
= 0; cur_y
< TILE_Y
; cur_y
++) {
339 for (cur_x
= 0; cur_x
< TILE_X
; cur_x
++) {
340 for (cur_color
= 0; cur_color
< num_colors
; cur_color
++) {
341 if (ColorMap
[CM_RED
][cur_color
] == pixels
[cur_y
][cur_x
].r
342 && ColorMap
[CM_GREEN
][cur_color
] == pixels
[cur_y
][cur_x
].g
343 && ColorMap
[CM_BLUE
][cur_color
] == pixels
[cur_y
][cur_x
].b
)
346 if (cur_color
>= num_colors
)
347 Fprintf(stderr
, "color not in colormap!\n");
348 y
= (MAX_Y
- 1) - (cur_y
+ yoffset
);
349 apply_color
= cur_color
;
351 x
= (cur_x
/ 2) + xoffset
;
352 bmp
.packtile
[y
][x
] = cur_x
% 2
353 ? (uchar
)(bmp
.packtile
[y
][x
] | cur_color
)
354 : (uchar
)(cur_color
<< 4);
357 bmp
.packtile
[y
][x
] = (uchar
) apply_color
;