Debian Codegraph @

src/bniutils/bniextract.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2000 Marco Ziech ([email protected])
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/mkdir.h"
31 #include "compat/statmacros.h"
32 #include "common/xalloc.h"
33 #include "common/version.h"
34 #include "fileio.h"
35 #include "tga.h"
36 #include "bni.h"
37 #include "common/setup_after.h"
38
39 using namespace pvpgn::bni;
40 using namespace pvpgn;
41
42 namespace
43 {
44
45 /* extract a portion of an image creating a new image */
46 t_tgaimg * area2img(t_tgaimg *src, int x, int y, int width, int height, t_tgaimgtype type) {
47 t_tgaimg *dst;
48 int pixelsize;
49 unsigned char *datap;
50 unsigned char *destp;
51 int i;
52
53 if (src == NULL) return NULL;
54 if ((x + width) > src->width) return NULL;
55 if ((y + height) > src->height) return NULL;
56 pixelsize = getpixelsize(src);
57 if (pixelsize == 0) return NULL;
58
59 dst = new_tgaimg(width, height, src->bpp, type);
60 dst->data = (std::uint8_t*)xmalloc(width*height*pixelsize);
61
62 datap = src->data;
63 datap += y*src->width*pixelsize;
64 destp = dst->data;
65 for (i = 0; i < height; i++) {
66 datap += x*pixelsize;
67 std::memcpy(destp, datap, width*pixelsize);
68 destp += width*pixelsize;
69 datap += (src->width - x)*pixelsize;
70 }
71 return dst;
72 }
73
74
75 void usage(char const * progname)
76 {
77 std::fprintf(stderr,
78 "usage: %s [<options>] [--] <BNI file> <output directory>\n"
79 " -h, --help, --usage show this information and exit\n"
80 " -v, --version print version number and exit\n", progname);
81
82 std::exit(EXIT_FAILURE);
83 }
84
85 }
86
87 extern int main(int argc, char * argv[])
88 {
89 char const * outdir = NULL;
90 char const * bnifile = NULL;
91 std::FILE * fbni;
92 struct stat s;
93 int a;
94 int forcefile = 0;
95 char dash[] = "-"; /* unique address used as flag */
96
97 if (argc < 1 || !argv || !argv[0])
98 {
99 std::fprintf(stderr, "bad arguments\n");
100 return EXIT_FAILURE;
101 }
102
103 for (a = 1; a < argc; a++)
104 if (forcefile && !bnifile)
105 bnifile = argv[a];
106 else if (std::strcmp(argv[a], "-") == 0 && !bnifile)
107 bnifile = dash;
108 else if (argv[a][0] != '-' && !bnifile)
109 bnifile = argv[a];
110 else if (forcefile && !outdir)
111 outdir = argv[a];
112 else if (std::strcmp(argv[a], "-") == 0 && !outdir)
113 outdir = dash;
114 else if (argv[a][0] != '-' && !outdir)
115 outdir = argv[a];
116 else if (forcefile || argv[a][0] != '-' || std::strcmp(argv[a], "-") == 0)
117 {
118 std::fprintf(stderr, "%s: extra file argument \"%s\"\n", argv[0], argv[a]);
119 usage(argv[0]);
120 }
121 else if (std::strcmp(argv[a], "--") == 0)
122 forcefile = 1;
123 else if (std::strcmp(argv[a], "-v") == 0 || std::strcmp(argv[a], "--version") == 0)
124 {
125 std::printf("version " PVPGN_VERSION "\n");
126 return EXIT_SUCCESS;
127 }
128 else if (std::strcmp(argv[a], "-h") == 0 || std::strcmp(argv[a], "--help") == 0 || std::strcmp(argv[a], "--usage")
129 == 0)
130 usage(argv[0]);
131 else
132 {
133 std::fprintf(stderr, "%s: unknown option \"%s\"\n", argv[0], argv[a]);
134 usage(argv[0]);
135 }
136
137 if (!bnifile)
138 {
139 std::fprintf(stderr, "%s: BNI file not specified\n", argv[0]);
140 usage(argv[0]);
141 }
142 if (!outdir)
143 {
144 std::fprintf(stderr, "%s: output directory not specified\n", argv[0]);
145 usage(argv[0]);
146 }
147
148 if (bnifile == dash)
149 fbni = stdin;
150 else
151 if (!(fbni = std::fopen(bnifile, "r")))
152 {
153 std::fprintf(stderr, "%s: could not open BNI file \"%s\" for reading (std::fopen: %s)\n", argv[0], bnifile, std::strerror(errno));
154 return EXIT_FAILURE;
155 }
156
157 if (outdir == dash)
158 {
159 std::fprintf(stderr, "%s: can not write directory to <stdout>\n", argv[0]);
160 if (bnifile != dash && std::fclose(fbni) < 0)
161 std::fprintf(stderr, "%s: could not close BNI file \"%s\" after reading (std::fclose: %s)\n", argv[0], bnifile, std::strerror(errno));
162 return EXIT_FAILURE;
163 }
164 if (stat(outdir, &s) < 0) {
165 if (errno == ENOENT) {
166 std::fprintf(stderr, "Info: Creating directory \"%s\" ...\n", outdir);
167 if (p_mkdir(outdir, S_IRWXU + S_IRWXG + S_IRWXO) < 0) {
168 std::fprintf(stderr, "%s: could not create output directory \"%s\" (mkdir: %s)", argv[0], outdir, std::strerror(errno));
169 if (bnifile != dash && std::fclose(fbni) < 0)
170 std::fprintf(stderr, "%s: could not close BNI file \"%s\" after reading (std::fclose: %s)\n", argv[0], bnifile, std::strerror(errno));
171 return EXIT_FAILURE;
172 }
173 }
174 else {
175 std::fprintf(stderr, "%s: could not stat output directory \"%s\" (stat: %s)\n", argv[0], outdir, std::strerror(errno));
176 if (bnifile != dash && std::fclose(fbni) < 0)
177 std::fprintf(stderr, "%s: could not close BNI file \"%s\" after reading (std::fclose: %s)\n", argv[0], bnifile, std::strerror(errno));
178 return EXIT_FAILURE;
179 }
180 }
181 else
182 if (S_ISDIR(s.st_mode) == 0) {
183 std::fprintf(stderr, "%s: \"%s\" is not a directory\n", argv[0], outdir);
184 if (bnifile != dash && std::fclose(fbni) < 0)
185 std::fprintf(stderr, "%s: could not close BNI file \"%s\" after reading (std::fclose: %s)\n", argv[0], bnifile, std::strerror(errno));
186 return EXIT_FAILURE;
187 }
188
189 {
190 unsigned int i;
191 int curry;
192 t_tgaimg * iconimg;
193 t_bnifile * bni;
194 std::FILE * indexfile;
195 char * indexfilename;
196
197 std::fprintf(stderr, "Info: Loading \"%s\" ...\n", bnifile);
198 bni = load_bni(fbni);
199 if (bni == NULL) return EXIT_FAILURE;
200 if (std::fseek(fbni, bni->dataoffset, SEEK_SET) < 0) {
201 std::fprintf(stderr, "%s: could not seek to TGA data offset %lu (std::fseek: %s)\n", argv[0], (unsigned long int)bni->dataoffset, std::strerror(errno));
202 if (bnifile != dash && std::fclose(fbni) < 0)
203 std::fprintf(stderr, "%s: could not close BNI file \"%s\" after reading (std::fclose: %s)\n", argv[0], bnifile, std::strerror(errno));
204 return EXIT_FAILURE;
205 }
206 std::fprintf(stderr, "Info: Loading image ...\n");
207 iconimg = load_tga(fbni);
208 if (iconimg == NULL) return EXIT_FAILURE;
209
210 std::fprintf(stderr, "Info: Extracting icons ...\n");
211 indexfilename = (char*)xmalloc(std::strlen(outdir) + 14);
212 std::sprintf(indexfilename, "%s/bniindex.lst", outdir);
213 std::fprintf(stderr, "Info: Writing Index to \"%s\" ... \n", indexfilename);
214 indexfile = std::fopen(indexfilename, "w");
215 if (indexfile == NULL) {
216 std::fprintf(stderr, "%s: could not open index file \"%s\" for writing (std::fopen: %s)\n", argv[0], indexfilename, std::strerror(errno));
217 if (bnifile != dash && std::fclose(fbni) < 0)
218 std::fprintf(stderr, "%s: could not close BNI file \"%s\" after reading (std::fclose: %s)\n", argv[0], bnifile, std::strerror(errno));
219 return EXIT_FAILURE;
220 }
221 std::fprintf(indexfile, "unknown1 %08x\n", bni->unknown1);
222 std::fprintf(indexfile, "unknown2 %08x\n", bni->unknown2);
223 curry = 0;
224 for (i = 0; i < bni->numicons; i++) {
225 std::FILE *dsttga;
226 char *name;
227 t_tgaimg *icn;
228 icn = area2img(iconimg, 0, curry, bni->icons->icon[i].x, bni->icons->icon[i].y, tgaimgtype_uncompressed_truecolor);
229 if (icn == NULL) {
230 std::fprintf(stderr, "Error: area2img failed!\n");
231 return EXIT_FAILURE;
232 }
233 if (bni->icons->icon[i].id == 0) {
234 int tag = bni->icons->icon[i].tag;
235 name = (char*)xmalloc(std::strlen(outdir) + 10);
236 std::sprintf(name, "%s/%c%c%c%c.tga", outdir, ((tag >> 24) & 0xff), ((tag >> 16) & 0xff), ((tag >> 8) & 0xff), ((tag >> 0) & 0xff));
237 }
238 else {
239 name = (char*)xmalloc(std::strlen(outdir) + 16);
240 std::sprintf(name, "%s/%08x.tga", outdir, bni->icons->icon[i].id);
241 }
242 std::fprintf(stderr, "Info: Writing icon %u(%ux%u) to file \"%s\" ... \n", i + 1, icn->width, icn->height, name);
243 curry += icn->height;
244 dsttga = std::fopen(name, "w");
245 if (dsttga == NULL) {
246 std::fprintf(stderr, "%s: could not open ouptut TGA file \"%s\" for writing (std::fopen: %s)\n", argv[0], name, std::strerror(errno));
247 }
248 else {
249 if (write_tga(dsttga, icn) < 0) {
250 std::fprintf(stderr, "Error: Writing to TGA failed.\n");
251 }
252 else {
253 int tag = bni->icons->icon[i].tag;
254 if (bni->icons->icon[i].id == 0) {
255 std::fprintf(indexfile, "icon !%c%c%c%c %d %d %08x\n", ((tag >> 24) & 0xff), ((tag >> 16) & 0xff), ((tag >> 8) & 0xff), ((tag >> 0) & 0xff), bni->icons->icon[i].x, bni->icons->icon[i].y, bni->icons->icon[i].unknown);
256 }
257 else {
258 std::fprintf(indexfile, "icon #%08x %d %d %08x\n", bni->icons->icon[i].id, bni->icons->icon[i].x, bni->icons->icon[i].y, bni->icons->icon[i].unknown);
259 }
260 }
261 if (std::fclose(dsttga) < 0)
262 std::fprintf(stderr, "%s: could not close TGA file \"%s\" after writing (std::fclose: %s)\n", argv[0], name, std::strerror(errno));
263 }
264 xfree(name);
265 destroy_img(icn);
266 }
267 destroy_img(iconimg);
268 if (std::fclose(indexfile) < 0) {
269 std::fprintf(stderr, "%s: could not close index file \"%s\" after writing (std::fclose: %s)\n", argv[0], indexfilename, std::strerror(errno));
270 return EXIT_FAILURE;
271 }
272 xfree(indexfilename);
273 }
274 if (bnifile != dash && std::fclose(fbni) < 0)
275 std::fprintf(stderr, "%s: could not close BNI file \"%s\" after reading (std::fclose: %s)\n", argv[0], bnifile, std::strerror(errno));
276
277 return EXIT_SUCCESS;
278 }