-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuDFExplorer_Base.pas
1503 lines (1322 loc) · 49.5 KB
/
uDFExplorer_Base.pas
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
{
******************************************************
DoubleFine Explorer
By Bennyboy
Http://quickandeasysoftware.net
******************************************************
}
{
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
}
unit uDFExplorer_Base;
interface
uses
Classes, sysutils, windows, graphics,
GR32, ImagingComponents, ZlibEx, ZlibExGz, ImagingTypes, Imaging, ImagingUtility,
uDFExplorer_Types, uDFExplorer_BaseBundleManager, uMemReader, uDFExplorer_Funcs,
uDFExplorer_FSBManager, uDFExplorer_PAKManager, uDFExplorer_PCKManager,
uDFExplorer_PKGManager, uDFExplorer_PPAKManager, uDFExplorer_LABManager,
uDFExplorer_LPAKManager, uDFExplorer_ISBManager, uVimaDecode;
type
TDFExplorerBase = class
private
fOnDebug: TDebugEvent;
fOnProgress: TProgressEvent;
fOnDoneLoading: TOnDoneLoading;
fBundle: TBundleManager;
fBundleFilename: string;
function GetFileName(Index: integer): string;
function GetFileSize(Index: integer): integer;
function GetFileOffset(Index: integer): int64;
function GetFileType(Index: integer): TFiletype;
function GetFileExtension(Index: integer): string;
function DrawImage(MemStream: TMemoryStream; OutImage: TBitmap32): boolean;
procedure Log(Text: string);
function WriteDDSToStream(SourceStream, DestStream: TStream): boolean;
function WriteDOTTFontToStream(SourceStream, DestStream: TStream): boolean;
function WriteHeaderlessTexDDSToStream(SourceStream, DestStream: TStream): boolean;
function WriteHeaderlessPsychonautsDDSToStream(PsychoDDS: TPsychonautsDDS; SourceStream,
DestStream: TStream): boolean;
function WriteHeaderlessDOTT_DDSToStream(SourceStream, DestStream: TStream): boolean;
function WriteHeaderlessDOTT_DDS_CostumeToStream(SourceStream, DestStream: TStream): boolean;
function WriteHeaderlessFT_chnk_DDSToStream(SourceStream, DestStream: TStream): boolean;
procedure AddDDSHeaderToStream(Width, Height, DataSize: integer; DXTType: TDDSTextureFormat;
DestStream: TStream; IsCubemap: boolean = false);
procedure AddDDSHeaderToStreamNEW(Width, Height, DataSize: integer; DXTType: TDDSTextureFormat;
DestStream: TStream; IsCubemap: boolean = false; IsVolume: boolean = false; MipmapCount: integer = 1);
public
constructor Create(BundleFile: string; Debug: TDebugEvent);
destructor Destroy; override;
function DrawImageGeneric(FileIndex: integer; DestBitmap: TBitmap32): boolean;
function DrawImageDOTTFont(FileIndex: integer; DestBitmap: TBitmap32): boolean;
function DrawImageDDS(FileIndex: integer;
DestBitmap: TBitmap32; DDSType: TDDSType = DDS_NORMAL): boolean;
function SaveDDSToFile(FileIndex: integer; DestDir, FileName: string; DDSType:
TDDSType = DDS_NORMAL): boolean;
function SaveIMCToStream(FileNo: integer; DestStream: TStream): boolean;
procedure Initialise;
procedure SaveFile(FileNo: integer; DestDir, FileName: string; DoLog: boolean = true);
procedure SaveFiles(DestDir: string);
procedure SaveFileToStream(FileNo: integer; DestStream: TStream);
procedure ReadText(FileIndex: integer; DestStrings: TStrings);
procedure ReadCSVText(FileIndex: integer; DestStrings: TStrings);
procedure ReadDelimitedText(FileIndex: integer; DestStrings: TStrings);
property OnDebug: TDebugEvent read FOnDebug write FOnDebug;
property OnDoneLoading: TOnDoneLoading read FOnDoneLoading write FOnDoneLoading;
property OnProgress: TProgressEvent read FOnProgress write FOnProgress;
property FileName[Index: integer]: string read GetFileName;
property FileSize[Index: integer]: integer read GetFileSize;
property FileOffset[Index: integer]: int64 read GetFileOffset;
property FileType[Index: integer]: TFileType read GetFileType;
property FileExtension[Index: integer]: string read GetFileExtension;
end;
implementation
constructor TDFExplorerBase.Create(BundleFile: string; Debug: TDebugEvent);
begin
OnDebug:=Debug;
fBundleFilename:=BundleFile;
try
if Uppercase( ExtractFileExt(BundleFile) ) = '.FSB' then
fBundle:=TFSBManager.Create(BundleFile)
else
if Uppercase( ExtractFileExt(BundleFile) ) = '.PCK' then
fBundle:=TPCKManager.Create(BundleFile)
else
if Uppercase( ExtractFileExt(BundleFile) ) = '.PKG' then
fBundle:=TPKGManager.Create(BundleFile)
else
if Uppercase( ExtractFileExt(BundleFile) ) = '.PPF' then
fBundle:=TPPAKManager.Create(BundleFile)
else
if Uppercase( ExtractFileExt(BundleFile) ) = '.LAB' then
fBundle:=TLABManager.Create(BundleFile)
else
if Uppercase( ExtractFileExt(BundleFile) ) = '.CLE' then
fBundle:=TLPAKManager.Create(BundleFile)
else
if Uppercase( ExtractFileExt(BundleFile) ) = '.DATA' then
fBundle:=TLPAKManager.Create(BundleFile)
else
if Uppercase( ExtractFileExt(BundleFile) ) = '.ISB' then
fBundle:=TISBManager.Create(BundleFile)
else
fBundle:=TPAKManager.Create(BundleFile);
except on E: EInvalidFile do
raise;
end;
end;
destructor TDFExplorerBase.Destroy;
begin
if fBundle <> nil then
FreeandNil(fBundle);
inherited;
end;
//Deal with delimited text and format it nicely
procedure TDFExplorerBase.ReadDelimitedText(FileIndex: integer;
DestStrings: TStrings);
function GetIndent(IndentLevel: integer): String; inline;
var
i: integer;
begin
result :='';
for i := 0 to IndentLevel - 1 do
result := result + ' ';//chr(9); //tab
end;
var
TempStream: TExplorerMemoryStream;
TextLen, i, IndentLevel, SquareBracketLevel: integer;
TempStr: String;
SourceStr: AnsiString;
begin
TempStream:=TExplorerMemoryStream.Create;
DestStrings.BeginUpdate;
try
fBundle.SaveFileToStream(Fileindex, TempStream);
TextLen := TempStream.ReadDWord;
Sourcestr :='';
//Costume Quest 2 doesnt have the 4 byte length and '1' string value.
if TextLen <> TempStream.Size - TempStream.Position then
begin
TempStream.Position := 0;
TextLen := TempStream.Size;
end;
SourceStr := TempStream.ReadAnsiString(TextLen);
IndentLevel := 0;
SquareBracketLevel := 0;
TempStr := '';
for I := 0 to Length(SourceStr) - 1 do
begin
//Remove the 1 character at the start of every file
if (i=1) and (SourceStr[i] = '1') then
begin
//Swallow it and do nothing
continue;
end
else
if SourceStr[i] = '{' then
begin
//First add any previous string if there is one
if Length(TempStr) > 0 then
begin
DestStrings.Add(GetIndent(IndentLevel) + TempStr);
TempStr := '';
end;
DestStrings.Add(GetIndent(IndentLevel) + '{');
inc(IndentLevel);
end
else
if SourceStr[i] = '}' then
begin
//First add any previous string if there is one
if Length(TempStr) > 0 then
begin
DestStrings.Add(GetIndent(IndentLevel) + TempStr);
TempStr := '';
end;
dec(IndentLevel);
DestStrings.Add(GetIndent(IndentLevel) + '}');
end
else
if SourceStr[i] = '[' then
begin
//First add any previous string if there is one
if Length(TempStr) > 0 then
begin
DestStrings.Add(GetIndent(IndentLevel) + TempStr);
TempStr := '';
end;
DestStrings.Add(GetIndent(IndentLevel) + '[');
inc(IndentLevel);
inc(SquareBracketLevel);
end
else
if SourceStr[i] = ']' then
begin
//First add any previous string if there is one
if Length(TempStr) > 0 then
begin
DestStrings.Add(GetIndent(IndentLevel) + TempStr);
TempStr := '';
end;
dec(IndentLevel);
DestStrings.Add(GetIndent(IndentLevel) + ']');
dec(SquareBracketLevel);
end
else
if SourceStr[i] = ';' then //new line
begin
DestStrings.Add(GetIndent(IndentLevel) + TempStr);
TempStr := '';
end
else
if SourceStr[i] = #0 then
//ignore newline char
continue
else
if (SquareBracketLevel > 0) and (SourceStr[i] = ',') then
begin
//First add any previous string if there is one
if Length(TempStr) > 0 then
begin
DestStrings.Add(GetIndent(IndentLevel) + TempStr);
TempStr := '';
end;
//Swallow the comma so its a newline
end
else
Tempstr := TempStr + String(SourceStr[i]);
end;
finally
TempStream.Free;
DestStrings.EndUpdate;
end;
end;
procedure TDFExplorerBase.ReadText(FileIndex: integer; DestStrings: TStrings);
var
TempStream: TExplorerMemoryStream;
begin
TempStream:=TExplorerMemoryStream.Create;
try
fBundle.SaveFileToStream(Fileindex, TempStream);
DestStrings.LoadFromStream(TempStream);
finally
TempStream.Free;
end;
end;
procedure TDFExplorerBase.ReadCSVText(FileIndex: integer;
DestStrings: TStrings);
var
TempStream: TExplorerMemoryStream;
begin
TempStream:=TExplorerMemoryStream.Create;
try
fBundle.SaveFileToStream(Fileindex, TempStream);
DestStrings.LoadFromStream(TempStream);
DestStrings.CommaText := DestStrings.Text; //Probably very slow but...
finally
TempStream.Free;
end;
end;
function TDFExplorerBase.DrawImage(MemStream: TMemoryStream;
OutImage: TBitmap32): boolean;
var
ImgBitmap : TImagingBitmap;
begin
Result := false;
MemStream.Position:=0;
ImgBitmap := TImagingBitmap.Create;
try
MemStream.Position :=0;
ImgBitmap.LoadFromStream(MemStream);
if ImgBitmap.Empty then
Exit;
OutImage.Assign(ImgBitmap);
Result := true;
finally
ImgBitmap.Free;
end;
end;
function TDFExplorerBase.DrawImageDOTTFont(FileIndex: integer;
DestBitmap: TBitmap32): boolean;
var
TempStream, ImageStream: TExplorerMemoryStream;
begin
Result:=false;
TempStream:=TExplorerMemoryStream.Create;
try
fBundle.SaveFileToStream(FileIndex, TempStream);
TempStream.Position:=0;
ImageStream:=TExplorerMemoryStream.Create;
try
if WriteDOTTFontToStream(TempStream, ImageStream) = false then
begin
Log('Image Decode failed! ' + fBundle.FileName[FileIndex]);
Exit;
end;
DestBitmap.Clear();
destbitmap.CombineMode:=cmBlend;
destBitmap.DrawMode:=dmOpaque;
if DrawImage(ImageStream, DestBitmap)=false then
begin
Log('Image Decode failed! ' + fBundle.FileName[FileIndex]);
Exit;
end;
Result:=true;
finally
ImageStream.Free;
end;
finally
TempStream.Free;
end;
end;
function TDFExplorerBase.WriteDOTTFontToStream(SourceStream,
DestStream: TStream): boolean;
var
BlockHeader: dword;
Temp, Height, Width: word;
Datasize, Dataoffset: integer;
TempStream: TMemoryStream;
Img: TImageData;
begin
{
DOTT FTX format is similar to DOTT '.tex' textures
4 bytes FTX1 or FXT2
2 bytes width
2 bytes height
4 bytes uncompressed texture size
4 bytes unknown
x bytes Gzipped texture
Texture isn't DXT -
For FXT1 its 8bpp greyscale or RGB233 (unsure which, probably greyscale as its fonts)
For FXT2 its 16bpp
}
Result := false;
SourceStream.Position := 0;
SourceStream.Read(BlockHeader, 4);
if (BlockHeader = 827872326) or (BlockHeader = 844649542) then //FXT1 FXT2
else
begin
Log('Unrecognised header in DOTT font texture!');
exit;
end;
Sourcestream.Read(Width, 2);
if fBundle.BigEndian then Width := SwapEndianWord(Width);
Sourcestream.Read(Height, 2);
if fBundle.BigEndian then Height := SwapEndianWord(Height);
Sourcestream.Seek(4, soFromCurrent); //Uncompressed size
Sourcestream.Seek(4, soFromCurrent); //Unknown
//Check here if gzipped. DOTT always? has a gzipped dxt texture after the header
SourceStream.Read(Temp, 2);
SourceStream.Seek(-2, soFromCurrent);
if Temp = 35615 {1F8B} then
begin
TempStream := TMemoryStream.Create;
try
TempStream.CopyFrom(SourceStream, SourceStream.Size - 16);
Tempstream.Position := 0;
SourceStream.Size := 16;
GZDecompressStream(tempstream, sourcestream);
{GZDecompressStream(sourcestream, tempstream);
TempStream.SaveToFile('c:\users\ben\desktop\decomp1');}
finally
TempStream.Free;
end;
end;
if BlockHeader = 827872326 then //FXT1
Datasize := Width * Height
else if BlockHeader = 844649542 then //FXT2
Datasize := (Width * Height) *2
else
Datasize := 0;
Dataoffset := 16;
SourceStream.Position := Dataoffset;
DestStream.Position := 0;
DestStream.CopyFrom(SourceStream, DataSize);
InitImage(Img);
try
if BlockHeader = 827872326 then //FXT1
NewImage(Width, Height, ifGray8, Img)
else if BlockHeader = 844649542 then //FXT2
NewImage(Width, Height, ifA1R5G5B5, Img);
DestStream.Position := 0;
DestStream.Read(Img.Bits^, Datasize);
DestStream.Size := 0;
SaveImageToStream('PNG', DestStream, Img);
Result := true;
finally
FreeImage(Img);
end;
end;
function TDFExplorerBase.DrawImageDDS(FileIndex: integer;
DestBitmap: TBitmap32; DDSType: TDDSType = DDS_NORMAL): boolean;
var
TempStream, DDSStream: TExplorerMemoryStream;
DecodeResult: Boolean;
begin
Result:=false;
DecodeResult := false;
TempStream:=TExplorerMemoryStream.Create;
try
fBundle.SaveFileToStream(FileIndex, TempStream);
TempStream.Position:=0;
DDSStream:=TExplorerMemoryStream.Create;
try
case DDSType of
DDS_NORMAL: DecodeResult :=
WriteDDSToStream(Tempstream, DDSStream);
DDS_HEADERLESS: DecodeResult :=
WriteHeaderlessTexDDSToStream(Tempstream, DDSStream);
DDS_HEADERLESS_PSYCHONAUTS: DecodeResult :=
WriteHeaderlessPsychonautsDDSToStream(
TPPAKManager(fBundle).PsychoDDS[FileIndex], Tempstream, DDSStream);
DDS_HEADERLESS_DOTT: DecodeResult :=
WriteHeaderlessDOTT_DDSToStream(TempStream, DDSStream);
DDS_HEADERLESS_DOTT_COSTUME: DecodeResult :=
WriteHeaderlessDOTT_DDS_CostumeToStream(TempStream, DDSStream);
DDS_HEADERLESS_FT_CHNK: DecodeResult :=
WriteHeaderlessFT_chnk_DDSToStream(TempStream, DDSStream);
end;
if DecodeResult = false then
begin
Log('DDS Decode failed! ' + fBundle.FileName[FileIndex]);
Exit;
end;
DestBitmap.Clear();
destbitmap.CombineMode:=cmBlend;
destBitmap.DrawMode:=dmOpaque;
if DrawImage(DDSStream, DestBitmap)=false then
begin
Log('DDS Decode failed! ' + fBundle.FileName[FileIndex]);
Exit;
end;
Result:=true;
finally
DDSStream.Free;
end;
finally
TempStream.Free;
end;
end;
function TDFExplorerBase.WriteDDSToStream(SourceStream, DestStream: TStream): boolean;
const
DDSMagic: cardinal = 542327876; //542327876 = 'DDS '
SearchOffsets: array [0..8] of integer = (0, 28, 36, 40, 48, 68, 144, 156, 180);
var
DDSnum: cardinal;
FoundPos, i: integer;
begin
result := false;
FoundPos := -1;
for I := 0 to High(SearchOffsets) do
begin
SourceStream.Position := SearchOffsets[i];
SourceStream.Read(DDSnum, 4);
if DDSnum = DDSMagic then
begin
FoundPos := SourceStream.Position - 4;
break;
end;
end;
if FoundPos > -1 then
begin
SourceStream.Seek(FoundPos, soFromBeginning);
DestStream.CopyFrom(SourceStream, SourceStream.Size - Sourcestream.Position);
result := true;
end
else
Log('DDS decode failed! Couldnt find identifier!');
//DestStream.SaveToFile('C:\Users\Ben\Desktop\test.dds');
end;
function TDFExplorerBase.WriteHeaderlessDOTT_DDSToStream(SourceStream,
DestStream: TStream): boolean;
var
Height, Width, Datasize, Dataoffset: integer;
Temp: word;
TempStream: TMemoryStream;
Img: TImageData;
Info: TImageFormatInfo;
begin
{
DOTT:
4 bytes MXT5
4 bytes width
4 bytes height
4 bytes Some identifier - mipmaps?
X bytes Rest of file is gzipped image
Unzipped image:
Has mipmaps - there's extra texture data after the main texture
DXT5 texture but is swizzled and stored in YCoCg colour space
If just put into a DDS container then blue channel appears missing -
need to convert the colour space
Full Throttle:
4 bytes DXT5
4 bytes width
4 bytes height
X bytes zlib deflate data (without the 78DA header
}
//First get the width and height and data size
SourceStream.Position := 4;
Sourcestream.Read(Width, 4);
if fBundle.BigEndian then Width := SwapEndianDWord(Width);
Sourcestream.Read(Height, 4);
if fBundle.BigEndian then Height := SwapEndianDWord(Height);
Sourcestream.Seek(4, soFromCurrent); //Unknown - mipmaps maybe?
//Check if gzipped. DOTT always has gzipped dxt files after the 16 byte header
SourceStream.Read(Temp, 2);
SourceStream.Seek(-2, soFromCurrent);
if Temp = 35615 {1F8B} then
begin
Dataoffset := 16;
TempStream := tmemorystream.Create;
try
TempStream.CopyFrom(SourceStream, SourceStream.Size - 16);
Tempstream.Position := 0;
SourceStream.Size := 16;
GZDecompressStream(tempstream, sourcestream);
//GZDecompressStream(sourcestream, tempstream);
//TempStream.SaveToFile('c:\users\ben\desktop\decomp1');
finally
TempStream.Free;
end;
end
else
//Assume its Full Throttle where its deflate without the 78DA header.
begin
Dataoffset := 12;
Sourcestream.Seek(-4, soFromCurrent); //FT doesnt have the extra 4 bytes in the header
TempStream := tmemorystream.Create;
try
TempStream.CopyFrom(SourceStream, SourceStream.Size - 12);
Tempstream.Position := 0;
SourceStream.Size := 12;
ZDecompressStream2(tempstream, sourcestream, -15);
finally
TempStream.Free;
end;
end;
Datasize := Width * Height; //The header on the dxt files is only 16 bytes long
AddDDSHeaderToStream(Width, Height, Datasize, DXT5, DestStream, false);
SourceStream.Position := Dataoffset;
DestStream.CopyFrom(SourceStream, DataSize);
//If Full Throttle can quit here
if Dataoffset = 12 then
begin
Result := true;
exit;
end;
//DOTT needs colour space conversion
DestStream.Position := 0;
InitImage(Img);
try
LoadImageFromStream(DestStream, Img);
ConvertImage(Img, ifA8R8G8B8); //Convert from DXT block format to normal ARGB 8bpp
GetImageFormatInfo( img.Format, Info );
ConvertYCoCgToRGB(Img.Bits, Width * Height, Info.BytesPerPixel); //Convert each pixel colour
DestStream.Size := 0;
SaveImageToStream('DDS', DestStream, Img);
Result := true;
finally
FreeImage(Img);
end;
end;
function TDFExplorerBase.WriteHeaderlessDOTT_DDS_CostumeToStream(SourceStream,
DestStream: TStream): boolean;
var
TempStreamJustMXT5: TExplorerMemoryStream;
HeaderIndex: integer;
begin
Result:=false;
SourceStream.Position:=0;
//Not all DOTT XML costumes have an image inside them but many do
HeaderIndex := FindFileHeader(SourceStream, 0, SourceStream.Size, 'MXT5');
if HeaderIndex = -1 then
begin
Log('XML doesnt contain costume image (not all of them do). In Full Throttle none of them do!');
Exit;
end;
TempStreamJustMXT5 := TExplorerMemoryStream.Create;
try
//Copy it out to a new stream which we can pass to the decoder -
//it expects a stream thats just a MXT5 texture with no additional header
SourceStream.Position := HeaderIndex;
TempStreamJustMXT5.CopyFrom(SourceStream, SourceStream.Size - SourceStream.Position);
TempStreamJustMXT5.Position := 0;
if WriteHeaderlessDOTT_DDSToStream(TempStreamJustMXT5, DestStream) = false then
Exit;
Result:=true;
finally
TempStreamJustMXT5.Free;
end;
end;
function TDFExplorerBase.WriteHeaderlessFT_chnk_DDSToStream(SourceStream,
DestStream: TStream): boolean;
var
HeaderIndex, Width, Height, Datasize: integer;
TempStream: TMemoryStream;
DXT: TDDSTextureFormat;
begin
result := false;
if SourceStream.Size < 12 then
begin
Log('No DDS image in this chunk - its too small');
exit;
end;
SourceStream.Position:=0;
//Find the DXT1 or DXT5 header
HeaderIndex := FindFileHeader(SourceStream, 0, SourceStream.Size, 'DXT5');
DXT := DXT5;
if HeaderIndex = -1 then
begin
HeaderIndex := FindFileHeader(SourceStream, 0, SourceStream.Size, 'DXT1');
DXT := DXT1;
if HeaderIndex = -1 then
begin
Exit;
end;
end;
Sourcestream.Position := HeaderIndex + 4;
SourceStream.Read(Width, 4);
SourceStream.Read(Height, 4);
begin
//Sourcestream.Position := HeaderIndex + 12; //start of compressed data
TempStream := tmemorystream.Create;
try
TempStream.CopyFrom(SourceStream, SourceStream.Size - Sourcestream.Position);
Tempstream.Position := 0;
SourceStream.Size := 0;
ZDecompressStream2(tempstream, sourcestream, -15);
finally
TempStream.Free;
end;
end;
Datasize := Width * Height; //The header on the dxt files is only 16 bytes long
//Tempstream.Position := 0;
//SourceStream.Position := 0;
//TempStream.CopyFrom(SourceStream, SourceStream.Size);
//TempStream.SaveToFile('c:\users\ben\desktop\testfile');
AddDDSHeaderToStream(Width, Height, Datasize, DXT, DestStream, false);
SourceStream.Position := 0;
DestStream.CopyFrom(SourceStream, SourceStream.Size);
DestStream.Position := 0;
result := true;
end;
function TDFExplorerBase.WriteHeaderlessTexDDSToStream(SourceStream,
DestStream: TStream): boolean;
var
TempInt, FirstCompChunkSize, FirstChunkDecompressedSize, SecondChunkCompSize,
SecondChunkDecompressedSize: integer;
Width, Height: word;
TempStream: TMemoryStream;
DXTType: TDDSTextureFormat;
begin
{
4 bytes "TEX "
2 bytes Width
2 bytes Height
4 bytes Unknown
4 bytes Compressed size of 1st image
4 bytes Uncompressed size of 1st image
4 bytes Compressed size of 2nd image
4 bytes Uncompressed size of 2nd image
4 bytes Unknown
x bytes 1st compressed image
x bytes 2nd compressed image
File has a 32 byte header then (usually) 2 zlib compressed images.
1st image is a smaller version of the second image (mipmap?) - its half the size anyway.
There isn't always a second image. If this is the case then the first image is used but
the width and height need halving since the width and height in the header apply to the
second image.
Sometimes the second image has a mipmap as part of the data.
A few files arent compressed. They have no first image and an uncompressed second image.
}
result := false;
SourceStream.Position := 0;
SourceStream.Read(TempInt, 4);
if TempInt <> 542655828 then //'TEX ' header
begin
Log('Couldnt find TEX identifier in headerless dds!');
exit;
end;
SourceStream.Read(Width, 2);
SourceStream.Read(Height, 2);
SourceStream.Seek(4, soFromCurrent); //Unknown
SourceStream.Read(FirstCompChunkSize, 4);
SourceStream.Read(FirstChunkDecompressedSize, 4);
SourceStream.Read(SecondChunkCompSize, 4);
SourceStream.Read(SecondChunkDecompressedSize, 4);
SourceStream.Seek(4, soFromCurrent); //Unknown
//Some files dont have a second compressed image
if SecondChunkCompSize = 0 then //Decompress the first image instead
begin
if Width * Height > FirstChunkDecompressedSize then //correct the dimensions
begin
Width := Width div 2;
Height := Height div 2;
end;
end
else //seek to the second compressed image
SourceStream.Seek(FirstCompChunkSize, soFromCurrent);
TempStream := TMemoryStream.Create;
try
//Have to decompress the stream first - we need to know the size of the data to be
//able to write the DDS header
try
if (SecondChunkCompSize > 0) and (SecondChunkCompSize = SecondChunkDecompressedSize)
then //Not compressed
TempStream.CopyFrom(SourceStream, SourceStream.Size - SourceStream.Position)
else
ZDecompressStream2(SourceStream, TempStream, -15);
except on EZDecompressionError do
begin
Log('Decompression failed in headerless DDS.');
exit;
end;
end;
if SecondChunkCompSize = 0 then
if FirstChunkDecompressedSize <> TempStream.Size then
Log('Decompressed size mismatch!');
//Correct for images with mipmaps - remove them - they sometimes crash the
//internal dds reader
if TempStream.Size > (Width * Height) then //DXT5 with mipmap
begin
Tempstream.Size := (Width * Height);
DXTType := DXT5;
end
else
if TempStream.Size < (Width * Height) then //DXT1 with mipmap
begin
TempStream.Size := ((Width * Height) div 2);
DXTType := DXT1;
end
else
begin
//Log('Unknown DXT type! Assuming DXT5');
DXTType := DXT5;
end;
AddDDSHeaderToStream(Width, Height, TempStream.Size, DXTType, DestStream);
TempStream.Position := 0;
DestStream.CopyFrom(TempStream, TempStream.Size);
Result := true;
finally
TempStream.Free;
end;
end;
function TDFExplorerBase.WriteHeaderlessPsychonautsDDSToStream(PsychoDDS: TPsychonautsDDS;
SourceStream, DestStream: TStream): boolean;
var
TextureSize: integer;
//DXTType : TDDSTextureFormat;
begin
//result := false;
SourceStream.Position := PsychoDDS.DataOffset;
//Just main texture - ignore any mipmaps
TextureSize := PsychoDDS.MainTextureSize;
//Some texture sizes incorrect
if (SourceStream.Size - SourceStream.Position) < TextureSize then
TextureSize := SourceStream.Size - SourceStream.Position;
//AddDDSHeaderToStream(PsychoDDS.Width, PsychoDDS.Height, TextureSize, PsychoDDS.TextureType,
// DestStream, PsychoDDS.IsCubemap);
AddDDSHeaderToStreamNEW(PsychoDDS.Width, PsychoDDS.Height, TextureSize, PsychoDDS.TextureType,
DestStream, PsychoDDS.IsCubemap, False, 1);
DestStream.CopyFrom(SourceStream, TextureSize);
Result := true;
end;
//New method for adding a suitable DDS header. Only used in Psychonauts 1 so far. Eventually move all headerless DDS to this.
//Majority of the code in this method from the Vampyre Imaging Library
procedure TDFExplorerBase.AddDDSHeaderToStreamNEW(Width, Height, DataSize: integer;
DXTType: TDDSTextureFormat; DestStream: TStream; IsCubemap: boolean = false;
IsVolume: boolean = false; MipmapCount: integer = 1);
const
{ Four character codes.}
DDSMagic = UInt32(Byte('D') or (Byte('D') shl 8) or (Byte('S') shl 16) or
(Byte(' ') shl 24));
FOURCC_DXT1 = UInt32(Byte('D') or (Byte('X') shl 8) or (Byte('T') shl 16) or
(Byte('1') shl 24));
FOURCC_DXT3 = UInt32(Byte('D') or (Byte('X') shl 8) or (Byte('T') shl 16) or
(Byte('3') shl 24));
FOURCC_DXT5 = UInt32(Byte('D') or (Byte('X') shl 8) or (Byte('T') shl 16) or
(Byte('5') shl 24));
FOURCC_ATI1 = UInt32(Byte('A') or (Byte('T') shl 8) or (Byte('I') shl 16) or
(Byte('1') shl 24));
FOURCC_ATI2 = UInt32(Byte('A') or (Byte('T') shl 8) or (Byte('I') shl 16) or
(Byte('2') shl 24));
FOURCC_DX10 = UInt32(Byte('D') or (Byte('X') shl 8) or (Byte('1') shl 16) or
(Byte('0') shl 24));
{ Some D3DFORMAT values used in DDS files as FourCC value.}
D3DFMT_A16B16G16R16 = 36;
D3DFMT_R32F = 114;
D3DFMT_A32B32G32R32F = 116;
D3DFMT_R16F = 111;
D3DFMT_A16B16G16R16F = 113;
{ Constans used by TDDSurfaceDesc2.Flags.}
DDSD_CAPS = $00000001;
DDSD_HEIGHT = $00000002;
DDSD_WIDTH = $00000004;
DDSD_PITCH = $00000008;
DDSD_PIXELFORMAT = $00001000;
DDSD_MIPMAPCOUNT = $00020000;
DDSD_LINEARSIZE = $00080000;
DDSD_DEPTH = $00800000;
{ Constans used by TDDSPixelFormat.Flags.}
DDPF_ALPHAPIXELS = $00000001; // used by formats which contain alpha
DDPF_FOURCC = $00000004; // used by DXT and large ARGB formats
DDPF_RGB = $00000040; // used by RGB formats
DDPF_LUMINANCE = $00020000; // used by formats like D3DFMT_L16
DDPF_BUMPLUMINANCE = $00040000; // used by mixed signed-unsigned formats
DDPF_BUMPDUDV = $00080000; // used by signed formats
{ Constans used by TDDSCaps.Caps1.}
DDSCAPS_COMPLEX = $00000008;
DDSCAPS_TEXTURE = $00001000;
DDSCAPS_MIPMAP = $00400000;
{ Constans used by TDDSCaps.Caps2.}
DDSCAPS2_CUBEMAP = $00000200;
DDSCAPS2_POSITIVEX = $00000400;
DDSCAPS2_NEGATIVEX = $00000800;
DDSCAPS2_POSITIVEY = $00001000;
DDSCAPS2_NEGATIVEY = $00002000;
DDSCAPS2_POSITIVEZ = $00004000;
DDSCAPS2_NEGATIVEZ = $00008000;
DDSCAPS2_VOLUME = $00200000;
{ Flags for TDDSurfaceDesc2.Flags used when saving DDS file.}
DDS_SAVE_FLAGS = DDSD_CAPS or DDSD_PIXELFORMAT or DDSD_WIDTH or
DDSD_HEIGHT or DDSD_LINEARSIZE;
type
{ Stores the pixel format information.}
TDDPixelFormat = record
Size: UInt32; // Size of the structure = 32 bytes
Flags: UInt32; // Flags to indicate valid fields
FourCC: UInt32; // Four-char code for compressed textures (DXT)
BitCount: UInt32; // Bits per pixel if uncomp. usually 16,24 or 32
RedMask: UInt32; // Bit mask for the Red component
GreenMask: UInt32; // Bit mask for the Green component
BlueMask: UInt32; // Bit mask for the Blue component
AlphaMask: UInt32; // Bit mask for the Alpha component
end;
{ Specifies capabilities of surface.}
TDDSCaps = record
Caps1: UInt32; // Should always include DDSCAPS_TEXTURE
Caps2: UInt32; // For cubic environment maps
Reserved: array[0..1] of UInt32; // Reserved
end;
{ Record describing DDS file contents.}
TDDSurfaceDesc2 = record
Size: UInt32; // Size of the structure = 124 Bytes
Flags: UInt32; // Flags to indicate valid fields
Height: UInt32; // Height of the main image in pixels
Width: UInt32; // Width of the main image in pixels
PitchOrLinearSize: UInt32; // For uncomp formats number of bytes per
// scanline. For comp it is the size in
// bytes of the main image
Depth: UInt32; // Only for volume text depth of the volume
MipMaps: Int32; // Total number of levels in the mipmap chain
Reserved1: array[0..10] of UInt32; // Reserved
PixelFormat: TDDPixelFormat; // Format of the pixel data
Caps: TDDSCaps; // Capabilities
Reserved2: UInt32; // Reserved
end;
{ DDS file header.}
TDDSFileHeader = record
Magic: UInt32; // File format magic
Desc: TDDSurfaceDesc2; // Surface description
end;
{ Resoirce types for D3D 10+ }
TD3D10ResourceDimension = (
D3D10_RESOURCE_DIMENSION_UNKNOWN = 0,
D3D10_RESOURCE_DIMENSION_BUFFER = 1,
D3D10_RESOURCE_DIMENSION_TEXTURE1D = 2,
D3D10_RESOURCE_DIMENSION_TEXTURE2D = 3,
D3D10_RESOURCE_DIMENSION_TEXTURE3D = 4
);
{ Texture formats for D3D 10+ }