| 1 |
sysadm |
1.1 |
/*
|
| 2 |
|
|
Copyright (C) 2000 Marco Ziech
|
| 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_STAT_H
|
| 46 |
|
|
# include <sys/stat.h>
|
| 47 |
|
|
#endif
|
| 48 |
|
|
#include "compat/statmacros.h"
|
| 49 |
|
|
#include "fileio.h"
|
| 50 |
|
|
#include "tga.h"
|
| 51 |
|
|
#include "bni.h"
|
| 52 |
|
|
#include "common/version.h"
|
| 53 |
|
|
#include "common/setup_after.h"
|
| 54 |
|
|
|
| 55 |
|
|
|
| 56 |
|
|
#define BUFSIZE 1024
|
| 57 |
|
|
|
| 58 |
|
|
|
| 59 |
|
|
static int read_list(char const * progname, t_bnifile * bnifile, char const * name) {
|
| 60 |
|
|
FILE * f;
|
| 61 |
|
|
char line[BUFSIZE];
|
| 62 |
|
|
|
| 63 |
|
|
f = fopen(name,"r");
|
| 64 |
|
|
if (f == NULL) {
|
| 65 |
|
|
fprintf(stderr,"%s: could not open index file \"%s\" for reading (fopen: %s)\n",progname,name,pstrerror(errno));
|
| 66 |
|
|
return -1;
|
| 67 |
|
|
}
|
| 68 |
|
|
bnifile->unknown1 = 0x00000010; /* in case they are not set */
|
| 69 |
|
|
bnifile->unknown2 = 0x00000001;
|
| 70 |
|
|
bnifile->numicons = 0;
|
| 71 |
|
|
bnifile->dataoffset = 16; /* size of header */
|
| 72 |
|
|
bnifile->icons = malloc(1); /* some realloc()s are broken */
|
| 73 |
|
|
while (fgets(line,sizeof(line),f)) {
|
| 74 |
|
|
char cmd[BUFSIZE];
|
| 75 |
|
|
sscanf(line,"%s",cmd);
|
| 76 |
|
|
if (strcmp(cmd,"unknown1") == 0) {
|
| 77 |
|
|
sscanf(line,"unknown1 %08x",&bnifile->unknown1);
|
| 78 |
|
|
}
|
| 79 |
|
|
else if (strcmp(cmd,"unknown2") == 0) {
|
| 80 |
|
|
sscanf(line,"unknown2 %08x",&bnifile->unknown2);
|
| 81 |
|
|
}
|
| 82 |
|
|
else if (strcmp(cmd,"icon") == 0) {
|
| 83 |
|
|
char c;
|
| 84 |
|
|
sscanf(line,"icon %c",&c);
|
| 85 |
|
|
if (c == '!') {
|
| 86 |
|
|
unsigned char tg[4];
|
| 87 |
|
|
int tag;
|
| 88 |
|
|
unsigned int x,y,unknown;
|
| 89 |
|
|
sscanf(line,"icon !%c%c%c%c %u %u %08x",&tg[0],&tg[1],&tg[2],&tg[3],&x,&y,&unknown);
|
| 90 |
|
|
tag = tg[3] + (tg[2] << 8) + (tg[1] << 16) + (tg[0] << 24);
|
| 91 |
|
|
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));
|
| 92 |
|
|
bnifile->icons = realloc(bnifile->icons,((bnifile->numicons+1)*sizeof(t_bniicon)));
|
| 93 |
|
|
bnifile->icons->icon[bnifile->numicons].id = 0;
|
| 94 |
|
|
bnifile->icons->icon[bnifile->numicons].x = x;
|
| 95 |
|
|
bnifile->icons->icon[bnifile->numicons].y = y;
|
| 96 |
|
|
bnifile->icons->icon[bnifile->numicons].tag = tag;
|
| 97 |
|
|
bnifile->icons->icon[bnifile->numicons].unknown = unknown;
|
| 98 |
|
|
bnifile->numicons++;
|
| 99 |
|
|
bnifile->dataoffset += 20;
|
| 100 |
|
|
}
|
| 101 |
|
|
else if (c == '#') {
|
| 102 |
|
|
unsigned int id,x,y,unknown;
|
| 103 |
|
|
sscanf(line,"icon #%08x %u %u %08x",&id,&x,&y,&unknown);
|
| 104 |
|
|
fprintf(stderr,"Icon[%d]: id=0x%x x=%u y=%u unknown=0x%x tag=0x00000000\n",bnifile->numicons,id,x,y,unknown);
|
| 105 |
|
|
bnifile->icons = realloc(bnifile->icons,((bnifile->numicons+1)*sizeof(t_bniicon)));
|
| 106 |
|
|
bnifile->icons->icon[bnifile->numicons].id=id;
|
| 107 |
|
|
bnifile->icons->icon[bnifile->numicons].x = x;
|
| 108 |
|
|
bnifile->icons->icon[bnifile->numicons].y = y;
|
| 109 |
|
|
bnifile->icons->icon[bnifile->numicons].tag=0;
|
| 110 |
|
|
bnifile->icons->icon[bnifile->numicons].unknown = unknown;
|
| 111 |
|
|
bnifile->numicons++;
|
| 112 |
|
|
bnifile->dataoffset += 16;
|
| 113 |
|
|
}
|
| 114 |
|
|
else
|
| 115 |
|
|
fprintf(stderr,"Bad character '%c' in icon specifier for icon %u in index file \"%s\"\n",c,bnifile->numicons+1,name);
|
| 116 |
|
|
}
|
| 117 |
|
|
else
|
| 118 |
|
|
fprintf(stderr,"Unknown command \"%s\" in index file \"%s\"\n",cmd,name);
|
| 119 |
|
|
}
|
| 120 |
|
|
if (fclose(f)<0)
|
| 121 |
|
|
fprintf(stderr,"%s: could not close index file \"%s\" after reading (fclose: %s)\n",progname,name,pstrerror(errno));
|
| 122 |
|
|
return 0;
|
| 123 |
|
|
}
|
| 124 |
|
|
|
| 125 |
|
|
|
| 126 |
|
|
static char * geticonfilename(t_bnifile *bnifile, char const * indir, int i) {
|
| 127 |
|
|
char * name;
|
| 128 |
|
|
|
| 129 |
|
|
if (bnifile->icons->icon[i].id == 0) {
|
| 130 |
|
|
unsigned int tag = bnifile->icons->icon[i].tag;
|
| 131 |
|
|
name = malloc(strlen(indir)+10);
|
| 132 |
|
|
sprintf(name,"%s/%c%c%c%c.tga",indir,((tag >> 24) & 0xff),((tag >> 16) & 0xff),((tag >> 8) & 0xff),((tag >> 0) & 0xff));
|
| 133 |
|
|
} else {
|
| 134 |
|
|
name = malloc(strlen(indir)+16);
|
| 135 |
|
|
sprintf(name,"%s/%08x.tga",indir,bnifile->icons->icon[i].id);
|
| 136 |
|
|
}
|
| 137 |
|
|
return name;
|
| 138 |
|
|
}
|
| 139 |
|
|
|
| 140 |
|
|
|
| 141 |
|
|
static int img2area(t_tgaimg *dst, t_tgaimg *src, int x, int y) {
|
| 142 |
|
|
unsigned char *sdp;
|
| 143 |
|
|
unsigned char *ddp;
|
| 144 |
|
|
int pixelsize;
|
| 145 |
|
|
int i;
|
| 146 |
|
|
|
| 147 |
|
|
pixelsize = getpixelsize(dst);
|
| 148 |
|
|
if (getpixelsize(src) != pixelsize) {
|
| 149 |
|
|
fprintf(stderr,"Error: source pixelsize is %d should be %d!\n",getpixelsize(src),pixelsize);
|
| 150 |
|
|
return -1;
|
| 151 |
|
|
}
|
| 152 |
|
|
if (src->width+x > dst->width) return -1;
|
| 153 |
|
|
if (src->height+y > dst->height) return -1;
|
| 154 |
|
|
sdp = src->data;
|
| 155 |
|
|
ddp = dst->data + (y * dst->width * pixelsize);
|
| 156 |
|
|
for (i = 0; i < src->height; i++) {
|
| 157 |
|
|
ddp += x*pixelsize;
|
| 158 |
|
|
memcpy(ddp,sdp,src->width*pixelsize);
|
| 159 |
|
|
sdp += src->width*pixelsize;
|
| 160 |
|
|
ddp += (dst->width-x)*pixelsize;
|
| 161 |
|
|
}
|
| 162 |
|
|
return 0;
|
| 163 |
|
|
}
|
| 164 |
|
|
|
| 165 |
|
|
|
| 166 |
|
|
static void usage(char const * progname)
|
| 167 |
|
|
{
|
| 168 |
|
|
fprintf(stderr,
|
| 169 |
|
|
"usage: %s [<options>] [--] <input directory> [<BNI file>]\n"
|
| 170 |
|
|
" -h, --help, --usage show this information and exit\n"
|
| 171 |
|
|
" -v, --version print version number and exit\n",progname);
|
| 172 |
|
|
|
| 173 |
|
|
exit(STATUS_FAILURE);
|
| 174 |
|
|
}
|
| 175 |
|
|
|
| 176 |
|
|
|
| 177 |
|
|
extern int main(int argc, char * argv[])
|
| 178 |
|
|
{
|
| 179 |
|
|
char const * indir=NULL;
|
| 180 |
|
|
char const * bnifile=NULL;
|
| 181 |
|
|
FILE * fbni;
|
| 182 |
|
|
struct stat s;
|
| 183 |
|
|
int a;
|
| 184 |
|
|
int forcefile=0;
|
| 185 |
|
|
char dash[]="-"; /* unique address used as flag */
|
| 186 |
|
|
|
| 187 |
|
|
if (argc<1 || !argv || !argv[0])
|
| 188 |
|
|
{
|
| 189 |
|
|
fprintf(stderr,"bad arguments\n");
|
| 190 |
|
|
return STATUS_FAILURE;
|
| 191 |
|
|
}
|
| 192 |
|
|
|
| 193 |
|
|
for (a=1; a<argc; a++)
|
| 194 |
|
|
if (forcefile && !indir)
|
| 195 |
|
|
indir = argv[a];
|
| 196 |
|
|
else if (strcmp(argv[a],"-")==0 && !indir)
|
| 197 |
|
|
indir = dash;
|
| 198 |
|
|
else if (argv[a][0]!='-' && !indir)
|
| 199 |
|
|
indir = argv[a];
|
| 200 |
|
|
else if (forcefile && !bnifile)
|
| 201 |
|
|
bnifile = argv[a];
|
| 202 |
|
|
else if (strcmp(argv[a],"-")==0 && !bnifile)
|
| 203 |
|
|
bnifile = dash;
|
| 204 |
|
|
else if (argv[a][0]!='-' && !bnifile)
|
| 205 |
|
|
bnifile = argv[a];
|
| 206 |
|
|
else if (forcefile || argv[a][0]!='-' || strcmp(argv[a],"-")==0)
|
| 207 |
|
|
{
|
| 208 |
|
|
fprintf(stderr,"%s: extra file argument \"%s\"\n",argv[0],argv[a]);
|
| 209 |
|
|
usage(argv[0]);
|
| 210 |
|
|
}
|
| 211 |
|
|
else if (strcmp(argv[a],"--")==0)
|
| 212 |
|
|
forcefile = 1;
|
| 213 |
|
|
else if (strcmp(argv[a],"-v")==0 || strcmp(argv[a],"--version")==0)
|
| 214 |
|
|
{
|
| 215 |
|
|
printf("version "PVPGN_VERSION"\n");
|
| 216 |
|
|
return STATUS_SUCCESS;
|
| 217 |
|
|
}
|
| 218 |
|
|
else if (strcmp(argv[a],"-h")==0 || strcmp(argv[a],"--help")==0 || strcmp(argv[a],"--usage")
|
| 219 |
|
|
==0)
|
| 220 |
|
|
usage(argv[0]);
|
| 221 |
|
|
else
|
| 222 |
|
|
{
|
| 223 |
|
|
fprintf(stderr,"%s: unknown option \"%s\"\n",argv[0],argv[a]);
|
| 224 |
|
|
usage(argv[0]);
|
| 225 |
|
|
}
|
| 226 |
|
|
|
| 227 |
|
|
if (!indir)
|
| 228 |
|
|
{
|
| 229 |
|
|
fprintf(stderr,"%s: input directory not specified\n",argv[0]);
|
| 230 |
|
|
usage(argv[0]);
|
| 231 |
|
|
}
|
| 232 |
|
|
if (!bnifile)
|
| 233 |
|
|
bnifile = dash;
|
| 234 |
|
|
|
| 235 |
|
|
if (indir==dash)
|
| 236 |
|
|
{
|
| 237 |
|
|
fprintf(stderr,"%s: can not read directory from <stdin>\n",argv[0]);
|
| 238 |
|
|
return STATUS_FAILURE;
|
| 239 |
|
|
}
|
| 240 |
|
|
if (stat(indir,&s)<0) {
|
| 241 |
|
|
fprintf(stderr,"%s: could not stat input directory \"%s\" (stat: %s)\n",argv[0],indir,pstrerror(errno));
|
| 242 |
|
|
return STATUS_FAILURE;
|
| 243 |
|
|
}
|
| 244 |
|
|
if (!S_ISDIR(s.st_mode)) {
|
| 245 |
|
|
fprintf(stderr,"%s: \"%s\" is not a directory\n",argv[0],indir);
|
| 246 |
|
|
return -1;
|
| 247 |
|
|
}
|
| 248 |
|
|
|
| 249 |
|
|
if (bnifile==dash)
|
| 250 |
|
|
fbni = stdout;
|
| 251 |
|
|
else
|
| 252 |
|
|
if (!(fbni = fopen(bnifile,"w")))
|
| 253 |
|
|
{
|
| 254 |
|
|
fprintf(stderr,"%s: could not open BNI file \"%s\" for writing (fopen: %s)\n",argv[0],bnifile,pstrerror(errno));
|
| 255 |
|
|
return STATUS_FAILURE;
|
| 256 |
|
|
}
|
| 257 |
|
|
|
| 258 |
|
|
{
|
| 259 |
|
|
unsigned int i;
|
| 260 |
|
|
unsigned int yline;
|
| 261 |
|
|
t_tgaimg * img;
|
| 262 |
|
|
t_bnifile bni;
|
| 263 |
|
|
char * listfilename;
|
| 264 |
|
|
|
| 265 |
|
|
listfilename = malloc(strlen(indir)+14);
|
| 266 |
|
|
sprintf(listfilename,"%s/bniindex.lst",indir);
|
| 267 |
|
|
fprintf(stderr,"Info: Reading index from file \"%s\"...\n",listfilename);
|
| 268 |
|
|
if (read_list(argv[0],&bni,listfilename)<0)
|
| 269 |
|
|
return STATUS_FAILURE;
|
| 270 |
|
|
fprintf(stderr,"BNIHeader: unknown1=%u unknown2=%u numicons=%u dataoffset=%u\n",bni.unknown1,bni.unknown2,bni.numicons,bni.dataoffset);
|
| 271 |
|
|
if (write_bni(fbni,&bni)<0) {
|
| 272 |
|
|
fprintf(stderr,"Error: Failed to write BNI header.\n");
|
| 273 |
|
|
return STATUS_FAILURE;
|
| 274 |
|
|
}
|
| 275 |
|
|
img = new_tgaimg(0,0,24,tgaimgtype_rlecompressed_truecolor);
|
| 276 |
|
|
for (i = 0; i < bni.numicons; i++) {
|
| 277 |
|
|
if (bni.icons->icon[i].x > img->width) img->width = bni.icons->icon[i].x;
|
| 278 |
|
|
img->height += bni.icons->icon[i].y;
|
| 279 |
|
|
}
|
| 280 |
|
|
fprintf(stderr,"Info: Creating TGA with %ux%ux%ubpp.\n",img->width,img->height,img->bpp);
|
| 281 |
|
|
img->data = malloc(img->width*img->height*getpixelsize(img));
|
| 282 |
|
|
yline = 0;
|
| 283 |
|
|
for (i = 0; i < bni.numicons; i++) {
|
| 284 |
|
|
t_tgaimg *icon;
|
| 285 |
|
|
FILE *f;
|
| 286 |
|
|
char *name;
|
| 287 |
|
|
name = geticonfilename(&bni,indir,i);
|
| 288 |
|
|
f = fopen(name,"r");
|
| 289 |
|
|
if (f==NULL) {
|
| 290 |
|
|
perror("fopen");
|
| 291 |
|
|
free(name);
|
| 292 |
|
|
return STATUS_FAILURE;
|
| 293 |
|
|
}
|
| 294 |
|
|
free(name);
|
| 295 |
|
|
icon = load_tga(f);
|
| 296 |
|
|
if (fclose(f)<0)
|
| 297 |
|
|
fprintf(stderr,"Error: could not close TGA file \"%s\" after reading (fclose: %s)\n",name,pstrerror(errno));
|
| 298 |
|
|
if (icon == NULL) {
|
| 299 |
|
|
fprintf(stderr,"Error: load_tga failed with data from TGA file \"%s\"\n",name);
|
| 300 |
|
|
return STATUS_FAILURE;
|
| 301 |
|
|
}
|
| 302 |
|
|
if (img2area(img,icon,0,yline)<0) {
|
| 303 |
|
|
fprintf(stderr,"Error: inserting icon from TGA file \"%s\" into big TGA failed\n",name);
|
| 304 |
|
|
return STATUS_FAILURE;
|
| 305 |
|
|
}
|
| 306 |
|
|
yline += icon->height;
|
| 307 |
|
|
destroy_img(icon);
|
| 308 |
|
|
}
|
| 309 |
|
|
if (write_tga(fbni,img)<0) {
|
| 310 |
|
|
fprintf(stderr,"Error: Failed to write TGA to BNI file.\n");
|
| 311 |
|
|
return STATUS_FAILURE;
|
| 312 |
|
|
}
|
| 313 |
|
|
if (bnifile!=dash && fclose(fbni)<0) {
|
| 314 |
|
|
fprintf(stderr,"%s: could not close BNI file \"%s\" after writing (fclose: %s)\n",argv[0],bnifile,pstrerror(errno));
|
| 315 |
|
|
return STATUS_FAILURE;
|
| 316 |
|
|
}
|
| 317 |
|
|
}
|
| 318 |
|
|
fprintf(stderr,"Info: Writing to \"%s\" finished.\n",bnifile);
|
| 319 |
|
|
return STATUS_SUCCESS;
|
| 320 |
|
|
}
|