unit uUtilities;

interface

uses GR32, Windows, Dialogs, Math;

type    TIndexRecord = packed record
                case Integer of
                        0: ( Offset, Length, Extra : Integer );
                        1: ( B : array [0..11] of Byte );
                end;

const   typ_map = 0;
        typ_staidx = 1;
        typ_statics = 2;
        typ_artidx = 3;
        typ_art = 4;
        typ_animidx = 5;
        typ_anim = 6;
        typ_soundidx = 7;
        typ_sound = 8;
        typ_texidx = 9;
        typ_texmaps = 10;
        typ_gumpidx = 11;
        typ_gumpart = 12;
        typ_multiidx = 13;
        typ_multimul = 14;
        typ_skillsidx = 15;
        typ_skillsmul = 16;
        typ_tiledata = 17;
        typ_animdata = 18;       

function Color15To32( C: Word ): TColor32;
function MakeTransparent( Bitmap : TBitmap32; TransparenteFarbe, FehlFarbe : TColor32 ) : TBitmap32;
function GetNextBit( Zahl : Integer ) : Integer;
function Color24to15( Color : Integer ) : Word; 

implementation

uses uConfig, SysUtils, Messages, Forms;

{ Converts a 15 bit color value to 32 bit }
function Color15To32(C : Word) : TColor32;
begin
        // Should be blue
        // ( C and $1F) * 8)

        // Should be green
        // (C shr 5) and $1F) * 8)

        // Shoudl be Red
        // (C shr 10) and $1F) * 8)

	Result := Color32
                  (
        	( ( C shr 10 ) and $1F ) * 8,
                ( ( C shr 5  ) and $1F ) * 8,
                  ( C and $1F) * 8
                  );
end;

function MakeTransparent( Bitmap : TBitmap32; TransparenteFarbe, FehlFarbe : TColor32 ) : TBitmap32;
var     x, y : Integer;
begin
        Result := TBitmap32.Create;
        Result.SetSize( Bitmap.Width, Bitmap.Height );
        Result.Draw( 0, 0, Bitmap );

        for x := 0 to Result.Width-1 do
                for y := 0 to Result.Height-1 do
                        if Result.Pixel[x,y] = Fehlfarbe then
                                Result.Pixel[x,y] := TransparenteFarbe;
end;

function GetNextBit( Zahl : Integer ) : Integer;
begin
        Result := Ceil( ln( Zahl ) / ln( 2 ) );
        Result := Round( exp( Result*ln( 2 ) ) );
end;

function Color24to15( Color : Integer ) : Word;
begin
        Result := ((Color and $F80000) shr 19) or ((Color and $F800) shr 6) or ((Color and $F8) shl 7);
end;

end.
