1 /* NetHack 3.6	tile2img.c	$NHDT-Date: 1432512809 2015/05/25 00:13:29 $  $NHDT-Branch: master $:$NHDT-Revision: 1.6 $ */
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 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);
25 
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
32 
33 FILE *tibfile2;
34 
35 pixel tilepixels[TILE_Y][TILE_X];
36 
37 char *tilefiles[] = { "..\\win\\share\\monsters.txt",
38                       "..\\win\\share\\objects.txt",
39                       "..\\win\\share\\other.txt" };
40 
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;
51 
52 int
main(argc,argv)53 main(argc, argv)
54 int argc;
55 char *argv[];
56 {
57     int i;
58 
59     if (argc != 2) {
60         Fprintf(stderr, "usage: tile2img outfile.img\n");
61         exit(EXIT_FAILURE);
62     } else
63         strcpy(bmpname, argv[1]);
64 
65 #ifdef OBSOLETE
66     bmpfile2 = fopen(NETHACK_PACKED_TILEFILE, WRBMODE);
67     if (bmpfile2 == (FILE *) 0) {
68         Fprintf(stderr, "Unable to open output file %s\n",
69                 NETHACK_PACKED_TILEFILE);
70         exit(EXIT_FAILURE);
71     }
72 #endif
73 
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);
82     }
83     fclose(fp);
84 
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));
88 
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);
93         }
94         num_colors = colorsinmap;
95         if (num_colors > 62) {
96             Fprintf(stderr, "too many colors (%d)\n", num_colors);
97             exit(EXIT_FAILURE);
98         }
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;
106             }
107         }
108         (void) fclose_text_file();
109         ++filenum;
110     }
111     Fprintf(stderr, "Total of %d tiles in memory.\n", tilecount);
112 
113     bitmap_to_file(XIMG, MAX_X, (tilecount / 20 + 1) * 16, 372, 372, 4, 16,
114                    bmpname, get_color, get_pixel);
115 
116     Fprintf(stderr, "Total of %d tiles written to %s.\n", tilecount, bmpname);
117 
118     exit(EXIT_SUCCESS);
119     /*NOTREACHED*/
120     return 0;
121 }
122 
123 void
get_color(unsigned int colind,struct RGB * rgb)124 get_color(unsigned int colind, struct RGB *rgb)
125 {
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;
129 }
130 
131 void
get_pixel(int x,int y,unsigned int * colind)132 get_pixel(int x, int y, unsigned int *colind)
133 {
134     *colind = Bild_daten[y][x];
135 }
136 
137 static void
138 build_ximgtile(pixels)
139 pixel (*pixels)[TILE_X];
140 {
141     int cur_x, cur_y, cur_color;
142     int x, y;
143 
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;
151             }
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;
157         }
158     }
159 }
160