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