1 (* Copyright (C) Doom 2D: Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 mempool in '../shared/mempool.pas',
32 utils in '../shared/utils.pas',
33 xstreams in '../shared/xstreams.pas',
34 xparser in '../shared/xparser.pas',
35 xdynrec in '../shared/xdynrec.pas',
37 sfs in '../sfs/sfs.pas',
38 sfsPlainFS in '../sfs/sfsPlainFS.pas',
39 sfsZipFS in '../sfs/sfsZipFS.pas',
41 wadreader in '../shared/wadreader.pas',
42 hashtable in '../shared/hashtable.pas',
43 conbuf in '../shared/conbuf.pas',
44 MAPDEF in '../shared/MAPDEF.pas',
45 CONFIG in '../shared/CONFIG.pas',
46 e_log in '../engine/e_log.pas',
47 ImagingTypes, Imaging, ImagingUtility;
55 optConvertATX: Boolean = false;
56 optConvertTGA: Boolean = false;
57 optLoCaseNames: Boolean = false;
58 optAggressivePng: Boolean = false;
59 optRecompress: Boolean = false;
62 function convertAnimTexture (wadSt: TStream; wadName: AnsiString): TMemoryStream;
65 TextureWAD: PChar = nil;
66 TextData: Pointer = nil;
67 TextureData: Pointer = nil;
70 TextureResource: String;
71 _width, _height, _framecount, _speed: Integer;
72 _backanimation: Boolean;
73 ia: TDynImageDataArray = nil;
76 x, y, ofsx, ofsy, nx, ny: Integer;
78 sto: TMemoryStream = nil;
85 buflen := Integer(wadSt.size);
88 wadSt.ReadBuffer(buf^, buflen);
90 WAD := TWADFile.Create();
91 //WAD.ReadFile(wadName);
92 WAD.ReadMemory(buf, buflen);
94 // ×èòàåì INI-ðåñóðñ àíèì. òåêñòóðû è çàïîìèíàåì åãî óñòàíîâêè:
95 if not WAD.GetResource('TEXT/ANIM', TextData, ResLength) then
97 writeln(Format('Animated texture file "%s" has invalid INI', [wadName]));
102 cfg := TConfig.CreateMem(TextData, ResLength);
104 TextureResource := cfg.ReadStr('', 'resource', '');
105 if TextureResource = '' then
107 writeln(Format('Animated texture WAD file "%s" has no "resource"', [wadName]));
111 _width := cfg.ReadInt('', 'framewidth', 0);
112 _height := cfg.ReadInt('', 'frameheight', 0);
113 _framecount := cfg.ReadInt('', 'framecount', 0);
114 _speed := cfg.ReadInt('', 'waitcount', 0);
115 _backanimation := cfg.ReadBool('', 'backanimation', False);
116 if _speed < 0 then _speed := 0;
118 if (_width < 1) or (_width > 16383) or (_height < 1) or (_height > 16383) then
120 writeln('invalid animation dimensions: ', _width, 'x', _height);
124 if (_framecount < 1) or (_framecount > 1024) then
126 writeln('invalid frame count: ', _framecount);
133 // ×èòàåì ðåñóðñ òåêñòóð (êàäðîâ) àíèì. òåêñòóðû â ïàìÿòü:
134 if not WAD.GetResource('TEXTURES/'+TextureResource, TextureData, ResLength) then
136 writeln(Format('Animated texture WAD file "%s" has no texture "%s"', [wadName, 'TEXTURES/'+TextureResource]));
140 if not LoadImageFromMemory(TextureData, ResLength, img) then
142 writeln(Format('Animated texture file "%s" has invalid texture image', [wadName]));
145 //writeln('texture image: ', img.width, 'x', img.height, ' (', img.width div _width, ' frames)');
150 // now create animation frames
151 GlobalMetadata.ClearMetaItems();
152 GlobalMetadata.ClearMetaItemsForSaving();
154 GlobalMetadata.SetMetaItem(SMetaFrameDelay, _speed*28);
155 if _backanimation then
156 GlobalMetadata.SetMetaItem(SMetaAnimationLoops, 1)
158 GlobalMetadata.SetMetaItem(SMetaAnimationLoops, 0);
160 SetLength(ia, _framecount);
161 //writeln('creating ', length(ia), ' animation frames...');
162 for f := 0 to high(ia) do
164 GlobalMetadata.SetMetaItem(SMetaFrameDelay, _speed*28, f);
165 if _backanimation then
166 GlobalMetadata.SetMetaItem(SMetaAnimationLoops, 1, f)
168 GlobalMetadata.SetMetaItem(SMetaAnimationLoops, 0, f);
171 NewImage(_width, _height, TImageFormat.ifA8R8G8B8, ia[f]);
174 for y := 0 to _height-1 do
176 for x := 0 to _width-1 do
180 if (nx >= 0) and (ny >= 0) and (nx < img.width) and (ny < img.height) then
182 clr := GetPixel32(img, nx, ny);
191 SetPixel32(ia[f], x, y, clr);
194 //writeln('resizing image...');
195 //ResizeImage(ia[f], 320, 200, TResizeFilter.rfNearest);
197 GlobalMetadata.CopyLoadedMetaItemsForSaving;
199 sto := TMemoryStream.Create();
200 //writeln(' ... [', ChangeFileExt(wadName, '.png'), '] (', length(ia), ') ');
201 Imaging.SetOption(ImagingPNGCompressLevel, 9);
202 if optAggressivePng then Imaging.SetOption(ImagingPNGPreFilter, 6);
203 if SaveMultiImageToStream('png', sto, ia) then
211 //writeln(' ...WTF?!');
217 for f := 0 to High(ia) do FreeImage(ia[f]);
220 if TextureWAD <> nil then FreeMem(TextureWAD);
221 if TextData <> nil then FreeMem(TextData);
222 if TextureData <> nil then FreeMem(TextureData);
229 function recompressImageToPng (ist: TStream): TStream;
231 ia: TDynImageDataArray = nil;
232 sto: TMemoryStream = nil;
237 GlobalMetadata.ClearMetaItems();
238 GlobalMetadata.ClearMetaItemsForSaving();
239 if not LoadMultiImageFromStream(ist, ia) then exit;
241 GlobalMetadata.CopyLoadedMetaItemsForSaving;
242 sto := TMemoryStream.Create();
243 Imaging.SetOption(ImagingPNGCompressLevel, 9);
244 if optAggressivePng then Imaging.SetOption(ImagingPNGPreFilter, 6);
245 if SaveMultiImageToStream('png', sto, ia) then
253 for f := 0 to High(ia) do FreeImage(ia[f]);
259 function zpack (ds: TStream; ss: TStream; var aborted: Boolean): LongWord;
270 dstp, srcsize: Int64;
273 //aborted := true; exit;
275 crc := crc32(0, nil, 0);
283 zst.avail_out := OBSize;
286 err := deflateInit2(zst, Z_BEST_COMPRESSION, Z_DEFLATED, -15, 9, 0);
287 if err <> Z_OK then raise Exception.Create(zerror(err));
291 if zst.avail_in = 0 then
293 // read input buffer part
294 rd := ss.read(ib^, IBSize);
295 if rd < 0 then raise Exception.Create('reading error');
296 //writeln(' read ', rd, ' bytes');
298 if rd <> 0 then begin crc := crc32(crc, Pointer(ib), rd); result := crc; end;
302 // now process the whole input
303 while zst.avail_in > 0 do
305 err := deflate(zst, Z_NO_FLUSH);
306 if err <> Z_OK then raise Exception.Create(zerror(err));
307 if zst.avail_out < OBSize then
309 //writeln(' written ', OBSize-zst.avail_out, ' bytes');
310 if ds.position+(OBSize-zst.avail_out)-dstp >= srcsize then
312 // this will be overwritten anyway
316 ds.writeBuffer(ob^, OBSize-zst.avail_out);
318 zst.avail_out := OBSize;
326 err := deflate(zst, Z_FINISH);
327 if (err <> Z_OK) and (err <> Z_STREAM_END) then raise Exception.Create(zerror(err));
328 if zst.avail_out < OBSize then
330 //writeln(' .written ', OBSize-zst.avail_out, ' bytes');
331 if ds.position+(OBSize-zst.avail_out)-dstp >= srcsize then
333 // this will be overwritten anyway
337 ds.writeBuffer(ob^, OBSize-zst.avail_out);
339 zst.avail_out := OBSize;
341 if err <> Z_OK then break;
343 // succesfully flushed?
344 if (err <> Z_STREAM_END) then raise Exception.Create(zerror(err));
356 procedure TProg.putStr (const s: AnsiString; newline: Boolean=false);
359 while lastlen > length(s) do
371 lastlen := length(s);
375 procedure TProg.onProgress (sender: TObject; const percent: double);
379 prc := trunc(percent*100.0);
380 putStr(Format('compressing %-33s %3d%%', [lastname, prc]));
383 procedure TProg.onFileStart (sender: TObject; const fileName: AnsiString);
385 lastname := fileName;
386 putStr(Format('compressing %-33s %3d%%', [lastname, 0]));
389 procedure TProg.onFileEnd (sender: TObject; const ratio: double);
391 putStr(Format('compressed %-33s %f', [lastname, ratio]), true);
396 // returns new file name
397 function detectExt (fpath, fname: AnsiString; fs: TStream): AnsiString;
406 if length(ExtractFileExt(fname)) <> 0 then exit;
407 if fs.size < 16 then exit;
408 buflen := Integer(fs.size);
411 fs.ReadBuffer(buf^, buflen);
413 Move(buf^, (PChar(@st[1]))^, 16);
415 if (st = 'Extended Module:') then
417 result := result+'.xm';
420 if (buf[0] = 'D') and (buf[1] = 'F') and (buf[2] = 'W') and
421 (buf[3] = 'A') and (buf[4] = 'D') and (buf[5] = #$1) then
423 result := result+'.wad';
426 if (buf[0] = 'M') and (buf[1] = 'A') and (buf[2] = 'P') and (buf[3] = #$1) then
428 result := result+'.dfmap';
431 if (buf[0] = 'M') and (buf[1] = 'T') and (buf[2] = 'h') and (buf[3] = 'd') then
433 result := result+'.mid';
436 if (buf[0] = 'R') and (buf[1] = 'I') and (buf[2] = 'F') and (buf[3] = 'F') and
437 (buf[8] = 'W') and (buf[9] = 'A') and (buf[10] = 'V') and (buf[11] = 'E') then
439 result := result+'.wav';
443 for f := 0 to 128-6 do
445 if (buf[f+0] = #$4) and (buf[f+1] = 'L') and
446 (buf[f+2] = 'A') and (buf[f+3] = 'M') and
447 (buf[f+4] = 'E') and (buf[f+5] = '3') then
449 result := result+'.mp3';
454 if (buf[0] = 'I') and (buf[1] = 'D') and (buf[2] = '3') and (buf[3] <= #4) then
456 result := result+'.mp3';
461 if (buf[buflen-128] = 'T') and (buf[buflen-127] = 'A') and (buf[buflen-126] = 'G') then
463 result := result+'.mp3';
467 // targa (stupid hack; this "signature" is not required by specs)
471 Move((buf+buflen-18)^, (PChar(@st[1]))^, 16);
473 if st = 'TRUEVISION-XFILE' then
475 result := result+'.tga';
480 // detect image format
481 img := DetermineMemoryFormat(buf, buflen);
482 if length(img) > 0 then
484 result := result+'.'+img;
487 // check if this is text file
490 for f := 0 to buflen-1 do
492 if buf[f] = #127 then exit;
495 if (buf[f] <> #9) and (buf[f] <> #10) and (buf[f] <> #13) then exit;
498 result := result+'.txt';
510 pkofs: Int64; // offset of file header
516 constructor Create ();
519 constructor TFileInfo.Create ();
525 crc := crc32(0, nil, 0);
531 uni2wint: array [128..255] of Word = (
532 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
533 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
534 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
535 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
536 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
537 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
538 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
539 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
543 function toUtf8 (const s: AnsiString): AnsiString;
549 GetMem(uc, length(s)*8);
550 GetMem(xdc, length(s)*8);
552 FillChar(uc^, length(s)*8, 0);
553 FillChar(xdc^, length(s)*8, 0);
555 for f := 1 to length(s) do
557 if ord(s[f]) < 128 then
558 uc[pos] := UnicodeChar(ord(s[f]))
560 uc[pos] := UnicodeChar(uni2wint[ord(s[f])]);
563 FillChar(xdc^, length(s)*8, 0);
564 f := UnicodeToUtf8(xdc, length(s)*8, uc, pos);
565 while (f > 0) and (xdc[f-1] = #0) do Dec(f);
566 SetLength(result, f);
567 Move(xdc^, result[1], f);
574 // this will write "extra field length" and extra field itself
579 TByteArray = array of Byte;
581 function buildUtfExtra (fname: AnsiString): TByteArray;
588 if fu = fname then begin result := nil; exit; end; // no need to write anything
589 crc := crc32(0, @fname[1], length(fname));
590 sz := 2+2+1+4+length(fu);
591 SetLength(result, sz);
592 result[0] := ord('u');
593 result[1] := ord('p');
595 result[2] := sz and $ff;
596 result[3] := (sz shr 8) and $ff;
598 result[5] := crc and $ff;
599 result[6] := (crc shr 8) and $ff;
600 result[7] := (crc shr 16) and $ff;
601 result[8] := (crc shr 24) and $ff;
602 Move(fu[1], result[9], length(fu));
605 const UtfFlags = (1 shl 10); // bit 11
608 function ZipOne (ds: TStream; fname: AnsiString; st: TStream; dopack: Boolean=true): TFileInfo;
610 oldofs, nfoofs, pkdpos, rd: Int64;
611 sign: packed array [0..3] of Char;
614 aborted: Boolean = false;
619 result := TFileInfo.Create();
620 result.pkofs := ds.position;
621 result.size := st.size;
622 if result.size > 0 then result.method := 8 else result.method := 0;
626 result.pksize := result.size;
629 result.name := fname;
630 ef := buildUtfExtra(result.name);
632 result.name := toUtf8(fname);
634 // write local header
636 ds.writeBuffer(sign, 4);
637 writeInt(ds, Word($0A10)); // version to extract
638 writeInt(ds, Word(UtfFlags)); // flags
639 writeInt(ds, Word(result.method)); // compression method
640 writeInt(ds, Word(0)); // file time
641 writeInt(ds, Word(0)); // file date
642 nfoofs := ds.position;
643 writeInt(ds, LongWord(result.crc)); // crc32
644 writeInt(ds, LongWord(result.pksize)); // packed size
645 writeInt(ds, LongWord(result.size)); // unpacked size
646 writeInt(ds, Word(length(fname))); // name length
648 writeInt(ds, Word(length(ef))); // extra field length
650 writeInt(ds, Word(0)); // extra field length
652 ds.writeBuffer(fname[1], length(fname));
654 if length(ef) > 0 then ds.writeBuffer(ef[0], length(ef));
658 // now write packed data
659 if result.size > 0 then
661 pkdpos := ds.position;
663 result.crc := zpack(ds, st, aborted);
664 result.pksize := ds.position-pkdpos;
665 if {result.pksize >= result.size} aborted then
667 // there's no sence to pack this file, so just store it
669 ds.position := result.pkofs;
672 result := ZipOne(ds, fname, st, false);
678 oldofs := ds.position;
679 ds.position := nfoofs;
680 writeInt(ds, LongWord(result.crc)); // crc32
681 writeInt(ds, LongWord(result.pksize)); // crc32
682 ds.position := oldofs;
692 result.crc := crc32(0, nil, 0);
694 while result.pksize < result.size do
696 rd := result.size-result.pksize;
697 if rd > bufsz then rd := bufsz;
698 st.readBuffer(buf^, rd);
699 ds.writeBuffer(buf^, rd);
700 Inc(result.pksize, rd);
701 result.crc := crc32(result.crc, buf, rd);
707 oldofs := ds.position;
708 ds.position := nfoofs;
709 writeInt(ds, LongWord(result.crc)); // crc32
710 ds.position := oldofs;
716 procedure writeCentralDir (ds: TStream; files: array of TFileInfo);
719 sign: packed array [0..3] of Char;
725 cdofs := ds.position;
726 for f := 0 to high(files) do
729 ef := buildUtfExtra(files[f].name);
732 ds.writeBuffer(sign, 4);
733 writeInt(ds, Word($0A10)); // version made by
734 writeInt(ds, Word($0010)); // version to extract
735 writeInt(ds, Word(UtfFlags)); // flags
736 writeInt(ds, Word(files[f].method)); // compression method
737 writeInt(ds, Word(0)); // file time
738 writeInt(ds, Word(0)); // file date
739 writeInt(ds, LongWord(files[f].crc));
740 writeInt(ds, LongWord(files[f].pksize));
741 writeInt(ds, LongWord(files[f].size));
742 writeInt(ds, Word(length(files[f].name))); // name length
744 writeInt(ds, Word(length(ef))); // extra field length
746 writeInt(ds, Word(0)); // extra field length
748 writeInt(ds, Word(0)); // comment length
749 writeInt(ds, Word(0)); // disk start
750 writeInt(ds, Word(0)); // internal attributes
751 writeInt(ds, LongWord(0)); // external attributes
752 writeInt(ds, LongWord(files[f].pkofs)); // header offset
753 ds.writeBuffer(files[f].name[1], length(files[f].name));
755 if length(ef) > 0 then ds.writeBuffer(ef[0], length(ef));
758 cdend := ds.position;
759 // write end of central dir
761 ds.writeBuffer(sign, 4);
762 writeInt(ds, Word(0)); // disk number
763 writeInt(ds, Word(0)); // disk with central dir
764 writeInt(ds, Word(length(files))); // number of files on this dist
765 writeInt(ds, Word(length(files))); // number of files total
766 writeInt(ds, LongWord(cdend-cdofs)); // size of central directory
767 writeInt(ds, LongWord(cdofs)); // central directory offset
768 writeInt(ds, Word(0)); // archive comment length
773 fs, fo, ast: TStream;
776 infname: AnsiString = '';
777 outfname: AnsiString = '';
780 files: array of TFileInfo;
785 if ParamCount() < 1 then
787 WriteLn('usage: wadcvt file.wad');
791 Imaging.SetOption(ImagingPNGCompressLevel, 9);
792 Imaging.SetOption(ImagingPNGLoadAnimated, 1);
793 Imaging.SetOption(ImagingGIFLoadAnimated, 1);
795 for f := 1 to ParamCount() do
798 if length(arg) = 0 then continue;
799 if not nomoreopts and (arg[1] = '-') then
801 if arg = '--apng' then optConvertATX := true
802 else if arg = '--tga' then optConvertTGA := true
803 else if arg = '--locase' then optLoCaseNames := true
804 else if arg = '--nocase' then optLoCaseNames := false
805 else if arg = '--aggressive' then begin optAggressivePng := true; Imaging.SetOption(ImagingPNGPreFilter, 6); end
806 else if arg = '--recompress' then optRecompress := true
807 else if (arg = '--help') or (arg = '-h') then
809 writeln('usage: wadcvt [options] file.wad');
811 writeln(' --apng convert animated textures to APNG format');
812 writeln(' --tga convert Targa images to PNG format');
814 writeln(' --locase convert file names to lower case');
818 else if arg = '--' then nomoreopts := true
821 writeln('unknown option: "', arg, '"');
827 if length(infname) = 0 then infname := arg
828 else if length(outfname) = 0 then outfname := arg
831 writeln('FATAL: too many arguments!');
837 if not StrEquCI1251(ExtractFileExt(infname), '.wad') and not StrEquCI1251(ExtractFileExt(infname), '.dfwad') then
843 if length(outfname) = 0 then outfname := ChangeFileExt(infname, '.pk3');
845 if not SFSAddDataFile(infname) then begin WriteLn('shit!'); Halt(1); end;
846 dvfn := SFSGetLastVirtualName(infname);
850 fl := SFSFileList(dvfn);
858 optLoCaseNames := true;
861 fo := TFileStream.Create(outfname, fmCreate);
863 for f := 0 to fl.Count-1 do
865 if length(fl[f].fName) = 0 then continue;
866 fs := SFSFileOpen(dvfn+'::'+fl[f].fPath+fl[f].fName);
867 newname := detectExt(fl[f].fPath, fl[f].fName, fs);
869 //fs := SFSFileOpen(dvfn+'::'+fl[f].fPath+fl[f].fName);
872 write(#13'[', f+1, '/', fl.Count, ']: ', fl[f].fPath, newname, ' ', fs.size, ' ... '#27'[K');
874 write('[', f+1, '/', fl.Count, ']: ', fl[f].fPath, newname, ' ', fs.size, ' ... ');
876 //writeln(' : ', newname, ' : [', ExtractFileExt(newname), ']');
877 if optConvertATX and (StrEquCI1251(ExtractFileExt(newname), '.dfwad') or StrEquCI1251(ExtractFileExt(newname), '.wad')) then
879 //writeln(' ANIMTEXT!');
880 ast := convertAnimTexture(fs, newname);
885 newname := ChangeFileExt(newname, '.png');
891 else if optRecompress or (optConvertTGA and (StrEquCI1251(ExtractFileExt(newname), '.tga') or StrEquCI1251(ExtractFileExt(newname), '.bmp'))) then
893 ast := recompressImageToPng(fs);
898 newname := ChangeFileExt(newname, '.png');
905 newname := fl[f].fPath+newname;
906 if optLoCaseNames then for c := 1 to length(newname) do newname[c] := LoCase1251(newname[c]);
907 nfo := ZipOne(fo, newname, fs);
908 write(nfo.pksize, ' DONE');
912 SetLength(files, length(files)+1);
913 files[high(files)] := nfo;
916 writeln(fl.Count, ' files processed.');
918 writeln(#13, fl.Count, ' files processed.'#27'[K');
920 writeCentralDir(fo, files);
924 DeleteFile(outfname);
926 if fo <> nil then fo.Free();