try
PWIL_Header = ^TWIL_Header;
TWIL_Header = packed record
Valid : Byte;
Lib_Info : array [0..20] of Char;
Lib_Version : Word;
Total_Index : Word;
end;
PWIX_Header = ^TWIX_Header;
TWIX_Header = packed record
Lib_Info : array [0..19] of Char;
Total_Index : Word;
end;
PWIL_Img_Header = ^TWIL_Img_Header;
TWIL_Img_Header = packed record
Width : Word;
Height : Word;
Offset_X : Smallint;
Offset_Y : Smallint;
Shadow_type : Byte;
Shadow_Offset_X : Smallint;
Shadow_Offset_Y : Smallint;
FileSize : Cardinal; // FileSize Only in Type2
end;
PImageContainer = ^TImageContainer;
TImageContainer = record
Width : Word;
Height : Word;
POW2H : Word;
POW2W : Word;
OffsetX : Integer;
OffsetY : Integer;
Texture : IDIRECT3DTEXTURE9;
LastUseTime : LongWord;
end;
TImageArrary = array[0..MaxListSize div 4] of TImageContainer;
PTImageArrary = ^TImageArrary;
TMyWILWixReader = class
private
FWILPointer : PByte;
FWIXPointer : PByte;
function OpenFileMMF(FileName: String): HResult;
procedure CloseMMF(MMFPointer: Pointer);
//... some internal function
public
FCashedImageArray : PTImageArrary;
function ReadImageFromIndex(IndexID: Integer; ImageContainer: TImageContainer): HResult;
//... some public function
end;
....
implementation
....
// the function can read Type 1 and 2 File
function TMyWILWixReader.ReadImageFromIndex(IndexID: Integer; ImageContainer: TImageContainer): HResult;
var
Hr : HResult;
FWILHeader : TWIL_Header;
FWILImage : TWIL_Img_Header;
d3dlr : TD3DLocked_Rect;
FPosition : Integer;
FReadPosition : Integer;
FSeek : Integer;
Pow2Height : Integer;
Pow2Width : Integer;
xCount : Integer;
xInc : Integer;
BGR : Integer;
ColorCount : Integer;
vHelp : Integer;
EncodeRGB : Integer;
PEncodeRGB : PInteger;
PBGR, PCount : PInteger;
PXInc : PInteger;
PColorCount : PInteger;
procedure MakePowerOfTwoHW(var H, W: Integer; valH, valW: Word);
begin
H := 1 shl ceil(log2(valH));
W := 1 shl ceil(log2(valW));
end;
begin
if (IndexID < $00) or (IndexID > $FFFF) then Exit;
try
CopyMemory(@FPosition, PInteger(Integer(FWIXPointer) + ((IndexID * 4) + 28)) , sizeOf(FPosition));
if FPosition <=0 then Exit;
FillChar(FWILHeader, SizeOf(TWIL_Header), #0);
CopyMemory(@FWILHeader ,FWILPointer ,sizeOf(TWIL_Header));
case FWILHeader.Lib_Version of
17 : FSeek := 17; //type 1
5000 : FSeek := 21; //type 2
else exit;
end;
PEncodeRGB := @EncodeRGB;
PColorCount:= @ColorCount;
PBGR := @BGR;
PCount := @xCount;
PXInc := @xInc;
FillChar(FWILImage, SizeOf(TWIL_Img_Header), #0);
CopyMemory(@FWILImage, PInteger(Integer(FWILPointer) + FPosition) ,sizeOf(TWIL_Img_Header));
FReadPosition := FPossition + FSeek;
MakePowerOfTwoHW(Pow2Height ,Pow2Width ,FWILImage.Height, FWILImage.Width);
// .....
// put all the things to the ImageContainer Height ,Width ,Offsets from WILImageHeader ec...
// .....
if Failed(D3DXCreateTexture(D3DDevice ,Pow2Width ,Pow2Height ,0 ,0 ,D3DFMT_A8B8G8R8 ,D3DPOOL_MANAGED, ImageContainer.Texture)) then
begin
Result := E_Fail;
Exit;
end;
Hr := ImageContainer.Texture.LockRect(0, d3dlr, nil, 0);
if Hr <> D3D_OK then
begin
Result := E_Fail;
exit;
end;
try
for y:=0 to FWILImage.Height-1 do
begin
PXInc^ := 0;
x := 0;
PCount^ := 0;
CopyMemory(PCount, PInteger(Integer(FWILPointer) + FReadPosition), sizeOf(Word));
Inc(FReadPosition, 2);
while x < PCount^ do
begin
Inc(x);
case PByte(Integer(FWILPointer)+ FReadPosition)^ of
192: begin // Black
// I don't Read here the Black Color, it is not need if I work with DirectX Texture
// so it is faster ^^. Only I Inc the counters
Inc(x);
Inc(FReadPosition, 2);
Inc(PxInc^, PWord(Integer(FWILPointer)+ FReadPosition)^);
Inc(FReadPosition, 2);
end;
//
// Here I show you a optimize way, I know, it is not so easy to understand
// It is a bit like the Kaori Initial Code, I only have change some things
// to get it faster. Don't Modifi it, if you don't have Knowlage !!
//
193: begin // Color
Inc(FReadPosition, 2);
Inc(x);
vHelp := 0;
PColorCount^ := 0;
CopyMemory(PColorCount, PLongWord(Integer(FWILPointer)+ FReadPosition), 2);
Inc(FReadPosition, 2);
while vHelp < PColorCount^ do
begin
PEncodeRGB^ := 0;
CopyMemory(PEncodeRGB, PInteger(Integer(FWILPointer)+ FReadPosition), 2);
PBGR^ := ($FF00 or (Trunc((((PEncodeRGB^ and 63488) shr 11) * 8.225806)))) shl 8;
PBGR^ := (PBGR^ or (Trunc((((PEncodeRGB^ and 2016) shr 5) * 4.047619)))) shl 8;
PBGR^ := (PBGR^ or (Trunc((((PEncodeRGB^ and 31) shr 0) * 8.225806))));
PInteger(Integer(d3dlr.pBits) + Y * d3dlr.Pitch + PXInc^ * 4 )^ := PBGR^;
Inc(x);
Inc(FReadPosition, 2);
Inc(vHelp);
Inc(PxInc^);
end;// while do
end;// if
194: begin // Alpha
Inc(FReadPosition, 2);
Inc(x);
vHelp := 0;
PColorCount^ := 0;
CopyMemory(PColorCount, PInteger(Integer(FWILPointer)+ FReadPosition), 2);
Inc(FReadPosition, 2);
while vHelp < PColorCount^ do
begin
PEncodeRGB^ := 0;
CopyMemory(PEncodeRGB, PInteger(Integer(FWILPointer)+ FReadPosition), 2);
PBGR^ := ($7F00 or (Trunc((((PEncodeRGB^ and 63488) shr 11) * 8.225806) + 0.5))) shl 8;
PBGR^ := (PBGR^ or (Trunc((((PEncodeRGB^ and 2016) shr 5) * 4.047619) + 0.5))) shl 8;
PBGR^ := (PBGR^ or (Trunc((((PEncodeRGB^ and 31) shr 0) * 8.225806) + 0.5)));
PInteger(Integer(d3dlr.pBits) + Y * d3dlr.Pitch + PXInc^ * 4)^ := PBGR^;
Inc(x);
Inc(FReadPosition, 2);
Inc(vHelp);
Inc(PxInc^);
end;// while do
end;// if
end;// case of
end;// while do
end;// for to do
finally
ImageContainer.Texture.UnlockRect(0);
end;
except
// Handle Error
end;
end;