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