Debian Codegraph @

src/bniutils/bnibuild.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2000 Marco Ziech
3 Copyright (C) 2000 Ross Combs ([email protected])
4
5 This program is xfree software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include "common/setup_before.h"
20
21 #include <cerrno>
22 #include <cstdint>
23 #include <cstdlib>
24 #include <cstring>
25
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
28 #endif
29
30 #include "compat/statmacros.h"
31 #include "common/xalloc.h"
32 #include "common/version.h"
33 #include "fileio.h"
34 #include "tga.h"
35 #include "bni.h"
36 #include "common/setup_after.h"
37
38 #define BUFSIZE 1024
39
40 using namespace pvpgn;
41 using namespace pvpgn::bni;
42
43 namespace
44 {
45
46 int read_list(char const * progname, t_bnifile * bnifile, char const * name) {
47 std::FILE * f;
48 char line[BUFSIZE];
49
50 f = std::fopen(name, "r");
51 if (f == NULL) {
52 std::fprintf(stderr, "%s: could not open index file \"%s\" for reading (std::fopen: %s)\n", progname, name, std::strerror(errno));
53 return -1;
54 }
55 bnifile->unknown1 = 0x00000010; /* in case they are not set */
56 bnifile->unknown2 = 0x00000001;
57 bnifile->numicons = 0;
58 bnifile->dataoffset = 16; /* size of header */
59 bnifile->icons = (struct bni_iconlist_struct*)xmalloc(1); /* some xrealloc()s are broken */
60 while (std::fgets(line, sizeof(line), f)) {
61 char cmd[BUFSIZE];
62 std::sscanf(line, "%s", cmd);
63 if (std::strcmp(cmd, "unknown1") == 0) {
64 std::sscanf(line, "unknown1 %08x", &bnifile->unknown1);
65 }
66 else if (std::strcmp(cmd, "unknown2") == 0) {
67 std::sscanf(line, "unknown2 %08x", &bnifile->unknown2);
68 }
69 else if (std::strcmp(cmd, "icon") == 0) {
70 char c;
71 std::sscanf(line, "icon %c", &c);
72 if (c == '!') {
73 unsigned char tg[4];
74 int tag;
75 unsigned int x, y, unknown;
76 std::sscanf(line, "icon !%c%c%c%c %u %u %08x", &tg[0], &tg[1], &tg[2], &tg[3], &x, &y, &unknown);
77 tag = tg[3] + (tg[2] << 8) + (tg[1] << 16) + (tg[0] << 24);
78 std::fprintf(stderr, "Icon[%d]: id=0x%x x=%u y=%u unknown=0x%x tag=\"%c%c%c%c\"\n", bnifile->numicons, 0, x, y, unknown, ((tag >> 24) & 0xff), ((tag >> 16) & 0xff), ((tag >> 8) & 0xff), ((tag >> 0) & 0xff));
79 bnifile->icons = (struct bni_iconlist_struct*)xrealloc(bnifile->icons, ((bnifile->numicons + 1)*sizeof(t_bniicon)));
80 bnifile->icons->icon[bnifile->numicons].id = 0;
81 bnifile->icons->icon[bnifile->numicons].x = x;
82 bnifile->icons->icon[bnifile->numicons].y = y;
83 bnifile->icons->icon[bnifile->numicons].tag = tag;
84 bnifile->icons->icon[bnifile->numicons].unknown = unknown;
85 bnifile->numicons++;
86 bnifile->dataoffset += 20;
87 }
88 else if (c == '#') {
89 unsigned int id, x, y, unknown;
90 std::sscanf(line, "icon #%08x %u %u %08x", &id, &x, &y, &unknown);
91 std::fprintf(stderr, "Icon[%d]: id=0x%x x=%u y=%u unknown=0x%x tag=0x00000000\n", bnifile->numicons, id, x, y, unknown);
92 bnifile->icons = (struct bni_iconlist_struct*)xrealloc(bnifile->icons, ((bnifile->numicons + 1)*sizeof(t_bniicon)));
93 bnifile->icons->icon[bnifile->numicons].id = id;
94 bnifile->icons->icon[bnifile->numicons].x = x;
95 bnifile->icons->icon[bnifile->numicons].y = y;
96 bnifile->icons->icon[bnifile->numicons].tag = 0;
97 bnifile->icons->icon[bnifile->numicons].unknown = unknown;
98 bnifile->numicons++;
99 bnifile->dataoffset += 16;
100 }
101 else
102 std::fprintf(stderr, "Bad character '%c' in icon specifier for icon %u in index file \"%s\"\n", c, bnifile->numicons + 1, name);
103 }
104 else
105 std::fprintf(stderr, "Unknown command \"%s\" in index file \"%s\"\n", cmd, name);
106 }
107 if (std::fclose(f) < 0)
108 std::fprintf(stderr, "%s: could not close index file \"%s\" after reading (std::fclose: %s)\n", progname, name, std::strerror(errno));
109 return 0;
110 }
111
112
113 char * geticonfilename(t_bnifile *bnifile, char const * indir, int i) {
114 char * name;
115
116 if (bnifile->icons->icon[i].id == 0) {
117 unsigned int tag = bnifile->icons->icon[i].tag;
118 name = (char*)xmalloc(std::strlen(indir) + 10);
119 std::sprintf(name, "%s/%c%c%c%c.tga", indir, ((tag >> 24) & 0xff), ((tag >> 16) & 0xff), ((tag >> 8) & 0xff), ((tag >> 0) & 0xff));
120 }
121 else {
122 name = (char*)xmalloc(std::strlen(indir) + 16);
123 std::sprintf(name, "%s/%08x.tga", indir, bnifile->icons->icon[i].id);
124 }
125 return name;
126 }
127
128
129 int img2area(t_tgaimg *dst, t_tgaimg *src, int x, int y) {
130 unsigned char *sdp;
131 unsigned char *ddp;
132 int pixelsize;
133 int i;
134
135 pixelsize = getpixelsize(dst);
136 if (getpixelsize(src) != pixelsize) {
137 std::fprintf(stderr, "Error: source pixelsize is %d should be %d!\n", getpixelsize(src), pixelsize);
138 return -1;
139 }
140 if (src->width + x > dst->width) return -1;
141 if (src->height + y > dst->height) return -1;
142 sdp = src->data;
143 ddp = dst->data + (y * dst->width * pixelsize);
144 for (i = 0; i < src->height; i++) {
145 ddp += x*pixelsize;
146 std::memcpy(ddp, sdp, src->width*pixelsize);
147 sdp += src->width*pixelsize;
148 ddp += (dst->width - x)*pixelsize;
149 }
150 return 0;
151 }
152
153
154 void usage(char const * progname)
155 {
156 std::fprintf(stderr,
157 "usage: %s [<options>] [--] <input directory> [<BNI file>]\n"
158 " -h, --help, --usage show this information and exit\n"
159 " -v, --version print version number and exit\n", progname);
160
161 std::exit(EXIT_FAILURE);
162 }
163
164 }
165
166 extern int main(int argc, char * argv[])
167 {
168 char const * indir = NULL;
169 char const * bnifile = NULL;
170 std::FILE * fbni;
171 struct stat s;
172 int a;
173 int forcefile = 0;
174 char dash[] = "-"; /* unique address used as flag */
175
176 if (argc < 1 || !argv || !argv[0])
177 {
178 std::fprintf(stderr, "bad arguments\n");
179 return EXIT_FAILURE;
180 }
181
182 for (a = 1; a < argc; a++)
183 if (forcefile && !indir)
184 indir = argv[a];
185 else if (std::strcmp(argv[a], "-") == 0 && !indir)
186 indir = dash;
187 else if (argv[a][0] != '-' && !indir)
188 indir = argv[a];
189 else if (forcefile && !bnifile)
190 bnifile = argv[a];
191 else if (std::strcmp(argv[a], "-") == 0 && !bnifile)
192 bnifile = dash;
193 else if (argv[a][0] != '-' && !bnifile)
194 bnifile = argv[a];
195 else if (forcefile || argv[a][0] != '-' || std::strcmp(argv[a], "-") == 0)
196 {
197 std::fprintf(stderr, "%s: extra file argument \"%s\"\n", argv[0], argv[a]);
198 usage(argv[0]);
199 }
200 else if (std::strcmp(argv[a], "--") == 0)
201 forcefile = 1;
202 else if (std::strcmp(argv[a], "-v") == 0 || std::strcmp(argv[a], "--version") == 0)
203 {
204 std::printf("version " PVPGN_VERSION "\n");
205 return EXIT_SUCCESS;
206 }
207 else if (std::strcmp(argv[a], "-h") == 0 || std::strcmp(argv[a], "--help") == 0 || std::strcmp(argv[a], "--usage")
208 == 0)
209 usage(argv[0]);
210 else
211 {
212 std::fprintf(stderr, "%s: unknown option \"%s\"\n", argv[0], argv[a]);
213 usage(argv[0]);
214 }
215
216 if (!indir)
217 {
218 std::fprintf(stderr, "%s: input directory not specified\n", argv[0]);
219 usage(argv[0]);
220 }
221 if (!bnifile)
222 bnifile = dash;
223
224 if (indir == dash)
225 {
226 std::fprintf(stderr, "%s: can not read directory from <stdin>\n", argv[0]);
227 return EXIT_FAILURE;
228 }
229 if (stat(indir, &s) < 0) {
230 std::fprintf(stderr, "%s: could not stat input directory \"%s\" (stat: %s)\n", argv[0], indir, std::strerror(errno));
231 return EXIT_FAILURE;
232 }
233 if (!S_ISDIR(s.st_mode)) {
234 std::fprintf(stderr, "%s: \"%s\" is not a directory\n", argv[0], indir);
235 return -1;
236 }
237
238 if (bnifile == dash)
239 fbni = stdout;
240 else
241 if (!(fbni = std::fopen(bnifile, "w")))
242 {
243 std::fprintf(stderr, "%s: could not open BNI file \"%s\" for writing (std::fopen: %s)\n", argv[0], bnifile, std::strerror(errno));
244 return EXIT_FAILURE;
245 }
246
247 {
248 unsigned int i;
249 unsigned int yline;
250 t_tgaimg * img;
251 t_bnifile bni;
252 char * listfilename;
253
254 listfilename = (char*)xmalloc(std::strlen(indir) + 14);
255 std::sprintf(listfilename, "%s/bniindex.lst", indir);
256 std::fprintf(stderr, "Info: Reading index from file \"%s\"...\n", listfilename);
257 if (read_list(argv[0], &bni, listfilename) < 0)
258 return EXIT_FAILURE;
259 std::fprintf(stderr, "BNIHeader: unknown1=%u unknown2=%u numicons=%u dataoffset=%u\n", bni.unknown1, bni.unknown2, bni.numicons, bni.dataoffset);
260 if (write_bni(fbni, &bni) < 0) {
261 std::fprintf(stderr, "Error: Failed to write BNI header.\n");
262 return EXIT_FAILURE;
263 }
264 img = new_tgaimg(0, 0, 24, tgaimgtype_rlecompressed_truecolor);
265 for (i = 0; i < bni.numicons; i++) {
266 if (bni.icons->icon[i].x > img->width) img->width = bni.icons->icon[i].x;
267 img->height += bni.icons->icon[i].y;
268 }
269 std::fprintf(stderr, "Info: Creating TGA with %ux%ux%ubpp.\n", img->width, img->height, img->bpp);
270 img->data = (std::uint8_t*)xmalloc(img->width*img->height*getpixelsize(img));
271 yline = 0;
272 for (i = 0; i < bni.numicons; i++) {
273 t_tgaimg *icon;
274 std::FILE *f;
275 char *name;
276 name = geticonfilename(&bni, indir, i);
277 f = std::fopen(name, "r");
278 if (f == NULL) {
279 std::perror("std::fopen");
280 xfree(name);
281 return EXIT_FAILURE;
282 }
283 xfree(name);
284 icon = load_tga(f);
285 if (std::fclose(f) < 0)
286 std::fprintf(stderr, "Error: could not close TGA file \"%s\" after reading (std::fclose: %s)\n", name, std::strerror(errno));
287 if (icon == NULL) {
288 std::fprintf(stderr, "Error: load_tga failed with data from TGA file \"%s\"\n", name);
289 return EXIT_FAILURE;
290 }
291 if (img2area(img, icon, 0, yline) < 0) {
292 std::fprintf(stderr, "Error: inserting icon from TGA file \"%s\" into big TGA failed\n", name);
293 return EXIT_FAILURE;
294 }
295 yline += icon->height;
296 destroy_img(icon);
297 }
298 if (write_tga(fbni, img) < 0) {
299 std::fprintf(stderr, "Error: Failed to write TGA to BNI file.\n");
300 return EXIT_FAILURE;
301 }
302 if (bnifile != dash && std::fclose(fbni) < 0) {
303 std::fprintf(stderr, "%s: could not close BNI file \"%s\" after writing (std::fclose: %s)\n", argv[0], bnifile, std::strerror(errno));
304 return EXIT_FAILURE;
305 }
306 }
307 std::fprintf(stderr, "Info: Writing to \"%s\" finished.\n", bnifile);
308 return EXIT_SUCCESS;
309 }