forked from Wargus/wargus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wartool.cpp
3217 lines (2835 loc) · 72.5 KB
/
wartool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// _________ __ __
// / _____// |_____________ _/ |______ ____ __ __ ______
// \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
// / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
// /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
// \/ \/ \//_____/ \/
// ______________________ ______________________
// T H E W A R B E G I N S
// Utility for Stratagus - A free fantasy real time strategy game engine
//
/**@name wartool.c - Extract files from war archives. */
//
// (c) Copyright 1999-2016 by Lutz Sammer, Nehal Mistry, Jimmy Salmon,
// Pali Rohár and cybermind
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 dated June, 1991.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//@{
/*----------------------------------------------------------------------------
-- Includes
----------------------------------------------------------------------------*/
#include "wargus.h"
#include "wartool.h"
#include <stratagus-gameutils.h>
#if defined(_MSC_VER) || defined(WIN32)
#include <windows.h>
#endif
#ifdef _MSC_VER
#define DEBUG _DEBUG
#include <direct.h>
#include <io.h>
#else
#define __USE_XOPEN_EXTENDED 1 // to get strdup
#include <unistd.h>
#endif
#include <limits.h>
#include <stdlib.h>
#include "endian.h"
#include "xmi2mid.h"
#include "rip_music.h"
#include "pud.h"
#ifndef __GNUC__
#define __attribute__(args) // Does nothing for non GNU CC
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
//----------------------------------------------------------------------------
/**
** Palette N27, for credits cursor
*/
static unsigned char* Pal27;
/**
** Original archive buffer.
*/
static unsigned char* ArchiveBuffer;
/**
** Offsets for each entry into original archive buffer.
*/
static unsigned char** ArchiveOffsets;
/**
** Fake empty entry
*/
static unsigned int EmptyEntry[] = { 1, 1, 1 };
static char* ArchiveDir;
/**
** What CD Type is it?
*/
static int CDType;
// Width of game font
static int game_font_width;
/**
** File names.
*/
static char* UnitNames[110];
static int UnitNamesLast = 0;
//----------------------------------------------------------------------------
// TOOLS
//----------------------------------------------------------------------------
/**
** Check if path exists, if not make all directories.
*/
void CheckPath(const char* path)
{
char* cp;
char* s;
#ifdef WIN32
cp = _strdup(path);
#else
cp = strdup(path);
#endif
s = strrchr(cp, '/');
if (s) {
*s = '\0'; // remove file
s = cp;
for (;;) { // make each path element
s = strchr(s, '/');
if (s) {
*s = '\0';
}
#if defined(_MSC_VER) || defined(WIN32)
_mkdir(cp);
#else
mkdir(cp, 0777);
#endif
if (s) {
*s++ = '/';
} else {
break;
}
}
}
free(cp);
}
/**
** Given a file name that would appear in a PC archive convert it to what
** would appear on the Mac.
*/
void ConvertToMac(char* filename)
{
if (!strcmp(filename, "rezdat.war")) {
strcpy(filename, "War Resources");
return;
}
if (!strcmp(filename, "strdat.war")) {
strcpy(filename, "War Strings");
return;
}
if (!strcmp(filename, "maindat.war")) {
strcpy(filename, "War Data");
return;
}
if (!strcmp(filename, "snddat.war")) {
strcpy(filename, "War Music");
return;
}
if (!strcmp(filename, "muddat.cud")) {
strcpy(filename, "War Movies");
return;
}
if (!strcmp(filename, "sfxdat.sud")) {
strcpy(filename, "War Sounds");
return;
}
}
//----------------------------------------------------------------------------
// PNG
//----------------------------------------------------------------------------
/**
** Save a png file.
**
** @param name File name
** @param image Graphic data
** @param w Graphic width
** @param h Graphic height
** @param pal Palette
** @param transparent Image uses transparency
*/
int SavePNG(const char* name, unsigned char* image, int x, int y, int w,
int h, int pitch, unsigned char* pal, int transparent)
{
FILE* fp;
png_structp png_ptr;
png_infop info_ptr;
unsigned char** lines;
int i;
if (!(fp = fopen(name, "wb"))) {
printf("%s:", name);
perror("Can't open file");
fflush(stdout); fflush(stderr);
return 1;
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fclose(fp);
return 1;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, NULL);
fclose(fp);
return 1;
}
if (setjmp(png_jmpbuf(png_ptr))) {
// FIXME: must free buffers!!
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return 1;
}
png_init_io(png_ptr, fp);
// zlib parameters
png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
// prepare the file information
#if PNG_LIBPNG_VER >= 10504
png_set_IHDR(png_ptr, info_ptr, w, h, 8, PNG_COLOR_TYPE_PALETTE, 0,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_set_PLTE(png_ptr, info_ptr, (png_colorp)pal, 256);
#else
info_ptr->width = w;
info_ptr->height = h;
info_ptr->bit_depth = 8;
info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
info_ptr->interlace_type = 0;
info_ptr->valid |= PNG_INFO_PLTE;
info_ptr->palette = (png_colorp)pal;
info_ptr->num_palette = 256;
#endif
if (transparent) {
unsigned char* p;
unsigned char* end;
png_byte trans[256];
p = image + y * pitch + x;
end = image + (y + h) * pitch + x;
while (p < end) {
if (!*p) {
*p = 0xFF;
}
++p;
}
memset(trans, 0xFF, sizeof(trans));
trans[255] = 0x0;
png_set_tRNS(png_ptr, info_ptr, trans, 256, 0);
}
// write the file header information
png_write_info(png_ptr, info_ptr);
// set transformation
// prepare image
lines = (unsigned char **)malloc(h * sizeof(*lines));
if (!lines) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return 1;
}
for (i = 0; i < h; ++i) {
lines[i] = image + (i + y) * pitch + x;
}
png_write_image(png_ptr, lines);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
free(lines);
return 0;
}
//----------------------------------------------------------------------------
// Archive
//----------------------------------------------------------------------------
/**
** Open the archive file.
**
** @param file Archive file name
** @param type Archive type requested
*/
int OpenArchive(const char* file, int type)
{
int f;
struct stat stat_buf;
unsigned char* buf;
unsigned char* cp;
unsigned char** op;
int entries;
int i;
//
// Open the archive file
//
f = open(file, O_RDONLY | O_BINARY, 0);
if (f == -1) {
error("Can't open file", file);
}
if (stat(file, &stat_buf)) {
error("Can't stat file", file);
}
// printf("Filesize %ld %ldk\n",
// (long)stat_buf.st_size, stat_buf.st_size / 1024);
//
// Read in the archive
//
buf = (unsigned char *)malloc(stat_buf.st_size);
if (!buf) {
printf("Can't malloc %ld\n", (long)stat_buf.st_size);
error("Memory error", "Could not allocate enough memory to read archive.");
}
if (read(f, buf, stat_buf.st_size) != stat_buf.st_size) {
printf("Can't read %ld\n", (long)stat_buf.st_size);
error("Memory error", "Could not read archive into RAM.");
}
close(f);
cp = buf;
i = FetchLE32(cp);
// printf("Magic\t%08X\t", i);
if (i != 0x19) {
printf("Wrong magic %08x, expected %08x\n", i, 0x00000019);
error("Archive version error", "This version of the data is not supported");
}
entries = FetchLE16(cp);
// printf("Entries\t%5d\t", entries);
i = FetchLE16(cp);
// printf("ID\t%d\n", i);
if (i != type) {
printf("Wrong type %08x, expected %08x\n", i, type);
error("Archive version error", "This version of the data is not supported");
}
//
// Read offsets.
//
op = (unsigned char **)malloc((entries + 1) * sizeof(unsigned char*));
if (!op) {
printf("Can't malloc %d entries\n", entries);
error("Memory error", "Could not allocate enough memory to read archive.");
}
for (i = 0; i < entries; ++i) {
op[i] = buf + FetchLE32(cp);
// check if entry size is not bigger then archive size
if (op[i] >= buf + stat_buf.st_size - 4) {
op[i] = (unsigned char *)&EmptyEntry;
printf("Ignore entry %d in archive (invalid offset)\n", i);
fflush(stdout);
} else {
unsigned char* dp = op[i];
size_t length = FetchLE32(dp);
int flags = length >> 24;
length &= 0x00FFFFFF;
if (flags == 0x00 && op[i] + length >= buf + stat_buf.st_size) {
op[i] = (unsigned char *)&EmptyEntry;
printf("Ignore entry %d in archive (invalid uncompressed length)\n", i);
fflush(stdout);
}
}
// printf("Offset\t%d\n", op[i]);
}
op[i] = buf + stat_buf.st_size;
ArchiveOffsets = op;
ArchiveBuffer = buf;
return 0;
}
/**
** Extract/uncompress entry.
**
** @param cp Pointer to compressed entry
** @param lenp Return pointer of length of the entry
**
** @return Pointer to uncompressed entry
*/
unsigned char* ExtractEntry(unsigned char* cp, size_t* lenp)
{
unsigned char* dp;
unsigned char* dest;
size_t uncompressed_length;
int flags;
uncompressed_length = FetchLE32(cp);
flags = uncompressed_length >> 24;
uncompressed_length &= 0x00FFFFFF;
// printf("Entry length %8d flags %02x\t", uncompressed_length, flags);
dp = dest = (unsigned char *)malloc(uncompressed_length);
if (!dest) {
printf("Can't malloc %d\n", (int)uncompressed_length);
error("Memory error", "Could not allocate enough memory to read archive.");
}
if (flags == 0x20) {
unsigned char buf[8192] = {'\0'};
unsigned char* ep;
int bi;
// printf("Compressed entry\n");
bi = 0;
memset(buf, 0, sizeof(buf));
ep = dp + uncompressed_length;
// FIXME: If the decompression is too slow, optimise this loop :->
while (dp < ep) {
int i;
int bflags;
bflags = FetchByte(cp);
// printf("Ctrl %02x ", bflags);
for (i = 0; i < 8; ++i) {
int j;
int o;
if (bflags & 1) {
j = FetchByte(cp);
*dp++ = j;
buf[bi++ & 0xFFF] = j;
// printf("=%02x", j);
} else {
o = FetchLE16(cp);
// printf("*%d,%d", o >> 12, o & 0xFFF);
j = (o >> 12) + 3;
o &= 0xFFF;
while (j--) {
buf[bi++ & 0xFFF] = *dp++ = buf[o++ & 0xFFF];
if (dp == ep) {
break;
}
}
}
if (dp == ep) {
break;
}
bflags >>= 1;
}
// printf("\n");
}
//if (dp != ep) printf("%p,%p %d\n", dp, ep, dp - dest);
} else if (flags == 0x00) {
// printf("Uncompressed entry\n");
memcpy(dest, cp, uncompressed_length);
} else {
printf("Unknown flags %x\n", flags);
error("Archive version error", "This version of the data is not supported");
}
// return resulting length
if (lenp) {
*lenp = uncompressed_length;
}
return dest;
}
/**
** Close the archive file.
*/
int CloseArchive(void)
{
free(ArchiveBuffer);
free(ArchiveOffsets);
ArchiveBuffer = 0;
ArchiveOffsets = 0;
return 0;
}
#ifdef USE_STORMLIB
int ExtractMPQFile(char *szArchiveName, char *szArchivedFile, char *szFileName, bool compress)
{
HANDLE hMpq = NULL; // Open archive handle
HANDLE hFile = NULL; // Archived file handle
FILE *file = NULL; // Disk file handle
gzFile gzfile = NULL; // Compressed file handle
int nError = ERROR_SUCCESS; // Result value
// Open an archive, e.g. "d2music.mpq"
if(nError == ERROR_SUCCESS) {
if(!SFileOpenArchive(szArchiveName, 0, STREAM_FLAG_READ_ONLY, &hMpq))
nError = GetLastError();
}
// Open a file in the archive, e.g. "data\global\music\Act1\tristram.wav"
if(nError == ERROR_SUCCESS) {
if(!SFileOpenFileEx(hMpq, szArchivedFile, 0, &hFile))
nError = GetLastError();
}
// Create the target file
if(nError == ERROR_SUCCESS) {
CheckPath(szFileName);
if (compress) {
gzfile = gzopen(szFileName, "wb9");
} else {
file = fopen(szFileName, "wb");
}
}
// Read the file from the archive
if(nError == ERROR_SUCCESS) {
char szBuffer[0x10000];
DWORD dwBytes = 1;
while(dwBytes > 0) {
SFileReadFile(hFile, szBuffer, sizeof(szBuffer), &dwBytes, NULL);
if(dwBytes > 0) {
if (compress) {
gzwrite(gzfile, szBuffer, dwBytes);
} else {
fwrite(szBuffer, 1, dwBytes, file);
}
}
}
}
// Cleanup and exit
if(file != NULL) {
fclose(file);
}
if(gzfile != NULL) {
gzclose(gzfile);
}
if(hFile != NULL)
SFileCloseFile(hFile);
if(hMpq != NULL)
SFileCloseArchive(hMpq);
return nError;
}
#endif
//----------------------------------------------------------------------------
// Palette
//----------------------------------------------------------------------------
/**
** Convert palette.
**
** @param pal Pointer to palette
**
** @return Pointer to palette
*/
unsigned char* ConvertPalette(unsigned char* pal)
{
int i;
// PNG needs 0-256
for (i = 0; i < 768; ++i) {
pal[i] <<= 2;
}
return pal;
}
/**
** Convert rgb to my format.
*/
int ConvertRgb(const char* file, int rgbe)
{
unsigned char* rgbp;
char buf[8192] = {'\0'};
FILE* f;
int i;
size_t l;
rgbp = ExtractEntry(ArchiveOffsets[rgbe], &l);
ConvertPalette(rgbp);
//
// Generate RGB File.
//
sprintf(buf, "%s/%s/%s.rgb", Dir, TILESET_PATH, file);
CheckPath(buf);
f = fopen(buf, "wb");
if (!f) {
perror("");
printf("Can't open %s\n", buf);
error("Memory error", "Could not allocate enough memory to read archive.");
}
if (l != fwrite(rgbp, 1, l, f)) {
printf("Can't write %d bytes\n", (int)l);
fflush(stdout);
}
fclose(f);
//
// Generate GIMP palette
//
sprintf(buf, "%s/%s/%s.gimp", Dir, TILESET_PATH, file);
CheckPath(buf);
f = fopen(buf, "wb");
if (!f) {
perror("");
printf("Can't open %s\n", buf);
error("Memory error", "Could not allocate enough memory to read archive.");
}
fprintf(f, "GIMP Palette\n# Stratagus %c%s -- GIMP Palette file\n",
toupper(*file), file + 1);
for (i = 0; i < 256; ++i) {
// FIXME: insert nice names!
fprintf(f, "%d %d %d\t#%d\n",
rgbp[i * 3], rgbp[i * 3 + 1], rgbp[i * 3 + 2], i);
}
fclose(f);
free(rgbp);
return 0;
}
//----------------------------------------------------------------------------
// Tileset
//----------------------------------------------------------------------------
/**
** Count used mega tiles for map.
*/
int CountUsedTiles(const unsigned char* map, const unsigned char* mega,
int* map2tile)
{
int i;
int j;
int used;
const unsigned char* tp;
int img2tile[0x9E0];
memset(map2tile, 0, sizeof(map2tile));
//
// Build conversion table.
//
for (i = 0; i < 0x9E; ++i) {
tp = map + i * 42;
// printf("%02X:", i);
for (j = 0; j < 0x10; ++j) {
// printf("%04X ", AccessLE16(tp + j * 2));
map2tile[(i << 4) | j] = AccessLE16(tp + j * 2);
}
// printf("\n");
}
//
// Mark all used mega tiles.
//
used = 0;
for (i = 0; i < 0x9E0; ++i) {
if (!map2tile[i]) {
continue;
}
for (j = 0; j < used; ++j) {
if (img2tile[j] == map2tile[i]) {
break;
}
}
if (j == used) {
//
// Check unique mega tiles.
//
for (j = 0; j < used; ++j) {
if (!memcmp(mega + img2tile[j] * 32, mega + map2tile[i] * 32, 32)) {
break;
}
}
if (j == used) {
img2tile[used++] = map2tile[i];
}
}
}
// printf("Used mega tiles %d\n", used);
#if 0
for (i = 0; i < used; ++i) {
if (!(i % 16)) {
printf("\n");
}
printf("%3d ",img2tile[i]);
}
printf("\n");
#endif
return used;
}
/**
** Convert for ccl.
*/
static void SaveCCL(char* name, unsigned char* map __attribute__((unused)),
const int* map2tile)
{
int i;
char* cp;
FILE* f;
char file[8192] = {'\0'};
char tileset[8192] = {'\0'};
f = stdout;
// FIXME: open file!
// remove leading path
if ((cp = strrchr(name, '/'))) {
++cp;
} else {
cp = (char*)name;
}
strcpy(file, cp);
strcpy(tileset, cp);
// remove suffix
if ((cp = strrchr(tileset, '.'))) {
*cp = '\0';
}
fprintf(f, "(tileset Tileset%c%s \"%s\" \"%s\"\n",
toupper(*tileset), tileset + 1, tileset, file);
fprintf(f, " #(");
for (i = 0; i < 0x9E0; ++i) {
if (i & 15) {
fprintf(f, " ");
} else if (i) {
fprintf(f, "\t; %03X\n ", i - 16);
}
fprintf(f, "%3d", map2tile[i]);
}
fprintf(f, " ))\n");
// fclose(f);
}
/**
** Decode a minitile into the image.
*/
void DecodeMiniTile(unsigned char* image, int ix, int iy, int iadd,
unsigned char* mini, int index, int flipx, int flipy)
{
static const int flip[] = {
7, 6, 5, 4, 3, 2, 1, 0, 8
};
int x;
int y;
for (y = 0; y < 8; ++y) {
for (x = 0; x < 8; ++x) {
image[(y + iy * 8) * iadd + ix * 8 + x] = mini[index +
(flipy ? flip[y] : y) * 8 + (flipx ? flip[x] : x)];
}
}
}
/**
** Convert tiles into image.
*/
unsigned char* ConvertTile(unsigned char* mini, const unsigned char* mega, int msize,
const unsigned char* map __attribute__((unused)), int *wp, int *hp)
{
unsigned char* image;
const unsigned short* mp;
int height;
int width;
int i;
int x;
int y;
int offset;
int numtiles;
// printf("Tiles in mega %d\t", msize / 32);
numtiles = msize / 32;
width = TILE_PER_ROW * 32;
height = ((numtiles + TILE_PER_ROW - 1) / TILE_PER_ROW) * 32;
// printf("Image %dx%d\n", width, height);
image = (unsigned char *)malloc(height * width);
memset(image, 0, height * width);
for (i = 0; i < numtiles; ++i) {
//mp = (const unsigned short*)(mega + img2tile[i] * 32);
mp = (const unsigned short*)(mega + i * 32);
if (i < 16) { // fog of war
for (y = 0; y < 32; ++y) {
offset = i * 32 * 32 + y * 32;
memcpy(image + (i % TILE_PER_ROW) * 32 +
(((i / TILE_PER_ROW) * 32) + y) * width,
mini + offset, 32);
}
} else { // normal tile
for (y = 0; y < 4; ++y) {
for (x = 0; x < 4; ++x) {
offset = ConvertLE16(mp[x + y * 4]);
DecodeMiniTile(image,
x + ((i % TILE_PER_ROW) * 4), y + (i / TILE_PER_ROW) * 4, width,
mini, (offset & 0xFFFC) * 16, offset & 2, offset & 1);
}
}
}
}
*wp = width;
*hp = height;
return image;
}
/**
** Convert a tileset to my format.
*/
int ConvertTileset(const char* file, int pale, int mege, int mine, int mape)
{
unsigned char* palp;
unsigned char* megp;
unsigned char* minp;
unsigned char* mapp;
unsigned char* image;
int w;
int h;
size_t megl;
char buf[8192] = {'\0'};
palp = ExtractEntry(ArchiveOffsets[pale], NULL);
megp = ExtractEntry(ArchiveOffsets[mege], &megl);
minp = ExtractEntry(ArchiveOffsets[mine], NULL);
mapp = ExtractEntry(ArchiveOffsets[mape], NULL);
// printf("%s:\t", file);
image = ConvertTile(minp, megp, megl, mapp, &w, &h);
free(megp);
free(minp);
free(mapp);
ConvertPalette(palp);
sprintf(buf, "%s/%s/%s.png", Dir, TILESET_PATH, file);
CheckPath(buf);
SavePNG(buf, image, 0, 0, w, h, w, palp, 1);
free(image);
free(palp);
return 0;
}
//----------------------------------------------------------------------------
// Graphics
//----------------------------------------------------------------------------
/**
** Decode a entry(frame) into image.
*/
void DecodeGfxEntry(int index, unsigned char* start,
unsigned char* image, int ix, int iy, int iadd)
{
unsigned char* bp;
unsigned char* sp;
unsigned char* dp;
int xoff;
int yoff;
int width;
int height;
int offset;
unsigned char* rows;
int h;
int w;
int ctrl;
bp = start + index * 8;
xoff = FetchByte(bp);
yoff = FetchByte(bp);
width = FetchByte(bp);
height = FetchByte(bp);
offset = FetchLE32(bp);
// printf("%2d: +x %2d +y %2d width %2d height %2d offset %d\n",
// index, xoff, yoff, width, height, offset);
rows = start + offset - 6;
dp = image + xoff - ix + (yoff - iy) * iadd;
for (h = 0; h < height; ++h) {
// printf("%2d: row-offset %2d\t", index, AccessLE16(rows + h * 2));
sp = rows + AccessLE16(rows + h * 2);
for (w = 0; w < width; ) {
ctrl = *sp++;
// printf("%02X", ctrl);
if (ctrl & 0x80) { // transparent
ctrl &= 0x7F;
// printf("-%d,", ctrl);
memset(dp+h*iadd+w,255,ctrl);
w+=ctrl;
} else if (ctrl & 0x40) { // repeat
ctrl &= 0x3F;
// printf("*%d,", ctrl);
memset(dp + h * iadd + w, *sp++, ctrl);
w += ctrl;
} else { // set pixels
ctrl &= 0x3F;
// printf("=%d,", ctrl);
memcpy(dp + h * iadd + w, sp, ctrl);
sp += ctrl;
w += ctrl;
}
}
//dp[h * iadd + width - 1] = 0;
// printf("\n");
}
}
/**
** Decode a entry(frame) into image.
*/
void DecodeGfuEntry(int index, unsigned char* start,
unsigned char* image, int ix, int iy, int iadd)
{
unsigned char* bp;
unsigned char* sp;
unsigned char* dp;
int i;
int xoff;
int yoff;
int width;
int height;
int offset;
bp = start + index * 8;
xoff = FetchByte(bp);
yoff = FetchByte(bp);
width = FetchByte(bp);
height = FetchByte(bp);
offset = FetchLE32(bp);
// High bit of width
if (offset < 0) {
offset &= 0x7FFFFFFF;
width += 256;
}
// printf("%2d: +x %2d +y %2d width %2d height %2d offset %d\n",
// index, xoff, yoff, width, height, offset);
sp = start + offset - 6;
dp = image + xoff - ix + (yoff - iy) * iadd;
for (i = 0; i < height; ++i) {
memcpy(dp, sp, width);
dp += iadd;
sp += width;
}
}
/**
** Convert graphics into image.
*/
unsigned char* ConvertGraphic(int gfx, unsigned char* bp, int *wp, int *hp,
unsigned char* bp2, int start2)
{
int i;
int count;
int length;
int max_width;
int max_height;
int minx;
int miny;
int best_width;
int best_height;
unsigned char* image;
int IPR;
// Init pointer to 2nd animation
if (bp2) {
count = FetchLE16(bp2);
max_width = FetchLE16(bp2);
max_height = FetchLE16(bp2);