// activate to enable build-in OpenGL support with dynamically linked methods
// use dglOpenGL.pas if not enabled
-{$DEFINE GLB_NATIVE_OGL_DYNAMIC}
+{.$DEFINE GLB_NATIVE_OGL_DYNAMIC}
// activate to enable the support for SDL_surfaces
-{.$DEFINE GLB_SDL}
+{$DEFINE GLB_SDL}
// activate to enable the support for TBitmap from Delphi (not lazarus)
{.$DEFINE GLB_DELPHI}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// activate to enable the support of SDL_image to load files. (READ ONLY)
// If you enable SDL_image all other libraries will be ignored!
-{.$DEFINE GLB_SDL_IMAGE}
+{$DEFINE GLB_SDL_IMAGE}
// activate to enable png support with the unit pngimage. You can download it from http://pngdelphi.sourceforge.net/
// if you enable pngimage the libPNG will be ignored
EglBitmapException = class(Exception);
EglBitmapSizeToLargeException = class(EglBitmapException);
EglBitmapNonPowerOfTwoException = class(EglBitmapException);
- EglBitmapUnsupportedFormatFormat = class(EglBitmapException);
+ EglBitmapUnsupportedFormat = class(EglBitmapException);
////////////////////////////////////////////////////////////////////////////////////////////////////
TglBitmapFormat = (
constructor Create(const aInstance: Cardinal; const aResourceID: Integer; const aResType: PChar); overload;
{$ENDIF}
private
- {$IFDEF GLB_SUPPORT_PNG_READ}
- function LoadPNG(const aStream: TStream): Boolean; virtual;
- procedure SavePNG(const aStream: TStream); virtual;
- {$ENDIF}
- {$IFDEF GLB_SUPPORT_JPEG_READ}
- function LoadJPEG(const aStream: TStream): Boolean; virtual;
- procedure SaveJPEG(const aStream: TStream); virtual;
- {$ENDIF}
+ {$IFDEF GLB_SUPPORT_PNG_READ} function LoadPNG(const aStream: TStream): Boolean; virtual; {$ENDIF}
+ {$ifdef GLB_SUPPORT_PNG_WRITE} procedure SavePNG(const aStream: TStream); virtual; {$ENDIF}
+
+ {$IFDEF GLB_SUPPORT_JPEG_READ} function LoadJPEG(const aStream: TStream): Boolean; virtual; {$ENDIF}
+ {$IFDEF GLB_SUPPORT_JPEG_WRITE} procedure SaveJPEG(const aStream: TStream); virtual; {$ENDIF}
+
function LoadBMP(const aStream: TStream): Boolean; virtual;
procedure SaveBMP(const aStream: TStream); virtual;
TFormatDescriptor = class(TObject)
private
- function GetRedMask: UInt64;
- function GetGreenMask: UInt64;
- function GetBlueMask: UInt64;
- function GetAlphaMask: UInt64;
+ function GetRedMask: QWord;
+ function GetGreenMask: QWord;
+ function GetBlueMask: QWord;
+ function GetAlphaMask: QWord;
protected
fFormat: TglBitmapFormat;
fWithAlpha: TglBitmapFormat;
property Range: TglBitmapColorRec read fRange;
property Shift: TShiftRec read fShift;
- property RedMask: UInt64 read GetRedMask;
- property GreenMask: UInt64 read GetGreenMask;
- property BlueMask: UInt64 read GetBlueMask;
- property AlphaMask: UInt64 read GetAlphaMask;
+ property RedMask: QWord read GetRedMask;
+ property GreenMask: QWord read GetGreenMask;
+ property BlueMask: QWord read GetBlueMask;
+ property AlphaMask: QWord read GetAlphaMask;
procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); virtual; abstract;
procedure Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer); virtual; abstract;
function IsEmpty: Boolean; virtual;
function HasAlpha: Boolean; virtual;
- function MaskMatch(const aRedMask, aGreenMask, aBlueMask, aAlphaMask: UInt64): Boolean; virtual;
+ function MaskMatch(const aRedMask, aGreenMask, aBlueMask, aAlphaMask: QWord): Boolean; virtual;
procedure PreparePixel(out aPixel: TglBitmapPixelData); virtual;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TbmpBitfieldFormat = class(TFormatDescriptor)
private
- procedure SetRedMask (const aValue: UInt64);
- procedure SetGreenMask(const aValue: UInt64);
- procedure SetBlueMask (const aValue: UInt64);
- procedure SetAlphaMask(const aValue: UInt64);
+ procedure SetRedMask (const aValue: QWord);
+ procedure SetGreenMask(const aValue: QWord);
+ procedure SetBlueMask (const aValue: QWord);
+ procedure SetAlphaMask(const aValue: QWord);
- procedure Update(aMask: UInt64; out aRange: Cardinal; out aShift: Byte);
+ procedure Update(aMask: QWord; out aRange: Cardinal; out aShift: Byte);
public
- property RedMask: UInt64 read GetRedMask write SetRedMask;
- property GreenMask: UInt64 read GetGreenMask write SetGreenMask;
- property BlueMask: UInt64 read GetBlueMask write SetBlueMask;
- property AlphaMask: UInt64 read GetAlphaMask write SetAlphaMask;
+ property RedMask: QWord read GetRedMask write SetRedMask;
+ property GreenMask: QWord read GetGreenMask write SetGreenMask;
+ property BlueMask: QWord read GetBlueMask write SetBlueMask;
+ property AlphaMask: QWord read GetAlphaMask write SetAlphaMask;
property PixelSize: Single read fPixelSize write fPixelSize;
end;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-function GetTopMostBit(aBitSet: UInt64): Integer;
+function GetTopMostBit(aBitSet: QWord): Integer;
begin
result := 0;
while aBitSet > 0 do begin
end;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-function CountSetBits(aBitSet: UInt64): Integer;
+function CountSetBits(aBitSet: QWord): Integer;
begin
result := 0;
while aBitSet > 0 do begin
{$ENDIF}
*)
-(* TODO GLB_SDL_IMAGE
{$IFDEF GLB_SDL_IMAGE}
+/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// SDL Image Helper /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function glBitmapRWseek(context: PSDL_RWops; offset: Integer; whence: Integer): Integer; cdecl;
begin
result := TStream(context^.unknown.data1).Seek(offset, whence);
result^.unknown.data1 := Stream;
end;
{$ENDIF}
-*)
(* TODO LoadFuncs
function LoadTexture(Filename: String; var Texture: Cardinal{$IFDEF GLB_DELPHI}; LoadFromRes : Boolean; Instance: Cardinal{$ENDIF}): Boolean;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//TglBitmapFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-function TFormatDescriptor.GetRedMask: UInt64;
+function TFormatDescriptor.GetRedMask: QWord;
begin
result := fRange.r shl fShift.r;
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-function TFormatDescriptor.GetGreenMask: UInt64;
+function TFormatDescriptor.GetGreenMask: QWord;
begin
result := fRange.g shl fShift.g;
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-function TFormatDescriptor.GetBlueMask: UInt64;
+function TFormatDescriptor.GetBlueMask: QWord;
begin
result := fRange.b shl fShift.b;
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-function TFormatDescriptor.GetAlphaMask: UInt64;
+function TFormatDescriptor.GetAlphaMask: QWord;
begin
result := fRange.a shl fShift.a;
end;
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-function TFormatDescriptor.MaskMatch(const aRedMask, aGreenMask, aBlueMask, aAlphaMask: UInt64): Boolean;
+function TFormatDescriptor.MaskMatch(const aRedMask, aGreenMask, aBlueMask, aAlphaMask: QWord): Boolean;
begin
result := false;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//TBitfieldFormat/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-procedure TbmpBitfieldFormat.SetRedMask(const aValue: UInt64);
+procedure TbmpBitfieldFormat.SetRedMask(const aValue: QWord);
begin
Update(aValue, fRange.r, fShift.r);
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-procedure TbmpBitfieldFormat.SetGreenMask(const aValue: UInt64);
+procedure TbmpBitfieldFormat.SetGreenMask(const aValue: QWord);
begin
Update(aValue, fRange.g, fShift.g);
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-procedure TbmpBitfieldFormat.SetBlueMask(const aValue: UInt64);
+procedure TbmpBitfieldFormat.SetBlueMask(const aValue: QWord);
begin
Update(aValue, fRange.b, fShift.b);
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-procedure TbmpBitfieldFormat.SetAlphaMask(const aValue: UInt64);
+procedure TbmpBitfieldFormat.SetAlphaMask(const aValue: QWord);
begin
Update(aValue, fRange.a, fShift.a);
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-procedure TbmpBitfieldFormat.Update(aMask: UInt64; out aRange: Cardinal; out
+procedure TbmpBitfieldFormat.Update(aMask: QWord; out aRange: Cardinal; out
aShift: Byte);
begin
aShift := 0;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TbmpBitfieldFormat.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
var
- data: UInt64;
+ data: QWord;
s: Integer;
-type
- PUInt64 = ^UInt64;
begin
data :=
((aPixel.Data.r and fRange.r) shl fShift.r) or
1: aData^ := data;
2: PWord(aData)^ := data;
4: PCardinal(aData)^ := data;
- 8: PUInt64(aData)^ := data;
+ 8: PQWord(aData)^ := data;
else
raise EglBitmapException.CreateFmt('invalid pixel size: %.1f', [fPixelSize]);
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TbmpBitfieldFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
var
- data: UInt64;
+ data: QWord;
s, i: Integer;
-type
- PUInt64 = ^UInt64;
begin
s := Round(fPixelSize);
case s of
1: data := aData^;
2: data := PWord(aData)^;
4: data := PCardinal(aData)^;
- 8: data := PUInt64(aData)^;
+ 8: data := PQWord(aData)^;
else
raise EglBitmapException.CreateFmt('invalid pixel size: %.1f', [fPixelSize]);
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TbmpColorTableFormat.Unmap(var aData: PByte; out aPixel: TglBitmapPixelData; var aMapData: Pointer);
-type
- PUInt64 = ^UInt64;
var
- idx: UInt64;
+ idx: QWord;
s: Integer;
bits: Byte;
f: Single;
1: idx := aData^;
2: idx := PWord(aData)^;
4: idx := PCardinal(aData)^;
- 8: idx := PUInt64(aData)^;
+ 8: idx := PQWord(aData)^;
else
raise EglBitmapException.CreateFmt('invalid pixel size: %.3f', [fPixelSize]);
end;
if fFormat = aValue then
exit;
if TFormatDescriptor.Get(Format).PixelSize <> TFormatDescriptor.Get(aValue).PixelSize then
- raise EglBitmapUnsupportedFormatFormat.Create('SetInternalFormat - ' + UNSUPPORTED_FORMAT);
+ raise EglBitmapUnsupportedFormat.Create('SetFormat');
SetDataPointer(Data, aValue, Width, Height);
end;
Row, RowSize: Integer;
SourceData, TmpData: PByte;
TempDepth: Integer;
- Pix: TglBitmapPixelData;
- FormatDesc: TglBitmapFormatDescriptor;
+ FormatDesc: TFormatDescriptor;
function GetRowPointer(Row: Integer): pByte;
begin
- result := Surface.pixels;
+ result := aSurface.pixels;
Inc(result, Row * RowSize);
end;
begin
result := false;
- (* TODO
- if not FormatIsUncompressed(InternalFormat) then
- raise EglBitmapUnsupportedInternalFormat.Create('AssignToSurface - ' + UNSUPPORTED_INTERNAL_FORMAT);
- *)
+ FormatDesc := TFormatDescriptor.Get(Format);
+ if FormatDesc.IsCompressed then
+ raise EglBitmapUnsupportedFormat.Create('AssignToSurface');
- FormatDesc := FORMAT_DESCRIPTORS[Format];
if Assigned(Data) then begin
- case Trunc(FormatDesc.GetSize) of
+ case Trunc(FormatDesc.PixelSize) of
1: TempDepth := 8;
2: TempDepth := 16;
3: TempDepth := 24;
4: TempDepth := 32;
else
- raise EglBitmapException.Create('AssignToSurface - ' + UNSUPPORTED_INTERNAL_FORMAT);
+ raise EglBitmapUnsupportedFormat.Create('AssignToSurface');
end;
- FormatDesc.PreparePixel(Pix);
- with Pix.PixelDesc do
- Surface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, TempDepth,
- RedRange shl RedShift, GreenRange shl GreenShift, BlueRange shl BlueShift, AlphaRange shl AlphaShift);
+ aSurface := SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, TempDepth,
+ FormatDesc.RedMask, FormatDesc.GreenMask, FormatDesc.BlueMask, FormatDesc.AlphaMask);
SourceData := Data;
- RowSize := Ceil(FileWidth * FormatDesc.GetSize);
+ RowSize := FormatDesc.GetSize(FileWidth, 1);
- for Row := 0 to FileHeight -1 do begin
+ for Row := 0 to FileHeight-1 do begin
TmpData := GetRowPointer(Row);
if Assigned(TmpData) then begin
Move(SourceData^, TmpData^, RowSize);
var
pSource, pData, pTempData: PByte;
Row, RowSize, TempWidth, TempHeight: Integer;
- IntFormat, f: TglBitmapInternalFormat;
- FormatDesc: TglBitmapFormatDescriptor;
+ IntFormat: TglBitmapFormat;
+ FormatDesc: TFormatDescriptor;
function GetRowPointer(Row: Integer): pByte;
begin
- result := Surface^.pixels;
+ result := aSurface^.pixels;
Inc(result, Row * RowSize);
end;
begin
result := false;
- if (Assigned(Surface)) then begin
- with Surface^.format^ do begin
- IntFormat := tfEmpty;
- for f := Low(f) to High(f) do begin
- if FORMAT_DESCRIPTORS[f].MaskMatch(RMask, GMask, BMask, AMask) then begin
- IntFormat := f;
+ if (Assigned(aSurface)) then begin
+ with aSurface^.format^ do begin
+ for IntFormat := High(TglBitmapFormat) to Low(TglBitmapFormat) do begin
+ FormatDesc := TFormatDescriptor.Get(IntFormat);
+ if (FormatDesc.MaskMatch(RMask, GMask, BMask, AMask)) then
break;
- end;
end;
if (IntFormat = tfEmpty) then
raise EglBitmapException.Create('AssignFromSurface - Invalid Pixelformat.');
end;
- FormatDesc := FORMAT_DESCRIPTORS[IntFormat];
- TempWidth := Surface^.w;
- TempHeight := Surface^.h;
- RowSize := Trunc(TempWidth * FormatDesc.GetSize);
+ TempWidth := aSurface^.w;
+ TempHeight := aSurface^.h;
+ RowSize := FormatDesc.GetSize(TempWidth, 1);
GetMem(pData, TempHeight * RowSize);
try
pTempData := pData;
AlphaInterleave := 0;
case Format of
- ifLuminance8Alpha8:
+ tfLuminance8Alpha8:
AlphaInterleave := 1;
- ifBGRA8, ifRGBA8:
+ tfBGRA8, tfRGBA8:
AlphaInterleave := 3;
end;
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-function TglBitmap.AddAlphaFromSurface(const aSurface: PSDL_Surface; const aFunc: TglBitmapFunction; const aArgs: PtrInt): Boolean;
+function TglBitmap.AddAlphaFromSurface(const aSurface: PSDL_Surface; const aFunc: TglBitmapFunction = nil; const aArgs: Pointer = nil): Boolean;
var
bmp: TglBitmap2D;
begin
bmp := TglBitmap2D.Create;
try
- bmp.AssignFromSurface(Surface);
- result := AddAlphaFromGlBitmap(bmp, Func, CustomData);
+ bmp.AssignFromSurface(aSurface);
+ result := AddAlphaFromGlBitmap(bmp, aFunc, aArgs);
finally
bmp.Free;
end;
FormatDesc := TFormatDescriptor.Get(Format);
if Assigned(Data) then begin
if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then
- raise EglBitmapUnsupportedFormatFormat.Create('RemoveAlpha - ' + UNSUPPORTED_FORMAT);
+ raise EglBitmapUnsupportedFormat.Create('RemoveAlpha');
result := ConvertTo(FormatDesc.WithoutAlpha);
end;
end;
Surface := IMG_LoadPNG_RW(RWops);
try
AssignFromSurface(Surface);
- Rresult := true;
+ result := true;
finally
SDL_FreeSurface(Surface);
end;
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-{
-procedure glBitmap_libJPEG_error_exit(cinfo: j_common_ptr); cdecl;
-var
- Msg: String;
-begin
- SetLength(Msg, 256);
- cinfo^.err^.format_message(cinfo, pChar(Msg));
- Writeln('ERROR [' + IntToStr(cinfo^.err^.msg_code) + '] ' + Msg);
- cinfo^.global_state := 0;
- jpeg_abort(cinfo);
-end;
-}
-
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-{
-procedure glBitmap_libJPEG_output_message(cinfo: j_common_ptr); cdecl;
-var
- Msg: String;
-begin
- SetLength(Msg, 256);
- cinfo^.err^.format_message(cinfo, pChar(Msg));
- Writeln('OUTPUT [' + IntToStr(cinfo^.err^.msg_code) + '] ' + Msg);
- cinfo^.global_state := 0;
-end;
-}
-
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-{
-procedure glBitmap_libJPEG_init_source(cinfo: j_decompress_ptr); cdecl;
-begin
-end;
-}
-
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function glBitmap_libJPEG_fill_input_buffer(cinfo: j_decompress_ptr): boolean; cdecl;
var
src: glBitmap_libJPEG_source_mgr_ptr;
end;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-{
-procedure glBitmap_libJPEG_term_source(cinfo: j_decompress_ptr); cdecl;
-begin
-end;
-}
-
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-{
-procedure glBitmap_libJPEG_init_destination(cinfo: j_compress_ptr); cdecl;
-begin
-end;
-}
-
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function glBitmap_libJPEG_empty_output_buffer(cinfo: j_compress_ptr): boolean; cdecl;
var
dest: glBitmap_libJPEG_dest_mgr_ptr;
begin
result := false;
- RWops := glBitmapCreateRWops(Stream);
+ RWops := glBitmapCreateRWops(aStream);
try
if IMG_isJPG(RWops) > 0 then begin
Surface := IMG_LoadJPG_RW(RWops);
begin
if not (ftBMP in FormatGetSupportedFiles(Format)) then
- raise EglBitmapUnsupportedFormatFormat.Create('SaveBMP - ' + UNSUPPORTED_FORMAT);
+ raise EglBitmapUnsupportedFormat.Create('SaveBMP');
Converter := nil;
FormatDesc := TFormatDescriptor.Get(Format);
Info.biCompression := BMP_COMP_BITFIELDS;
end;
else
- raise EglBitmapUnsupportedFormatFormat.Create('SaveBMP - ' + UNSUPPORTED_FORMAT);
+ raise EglBitmapUnsupportedFormat.Create('SaveBMP - ' + UNSUPPORTED_FORMAT);
end;
Info.biXPelsPerMeter := 2835;
Info.biYPelsPerMeter := 2835;
function TglBitmap.LoadTGA(const aStream: TStream): Boolean;
var
Header: TTGAHeader;
- ImageData: PByte;
+ ImageData: System.PByte;
StartPosition: Int64;
PixelSize, LineSize: Integer;
tgaFormat: TglBitmapFormat;
procedure ReadUncompressed;
var
i, j: Integer;
- buf, tmp1, tmp2: PByte;
+ buf, tmp1, tmp2: System.PByte;
begin
buf := nil;
if (Counter.X.dir < 0) then
/////////////////////////////////////////////////////////////////
var
- TmpData: PByte;
+ TmpData: System.PByte;
LinePixelsRead: Integer;
procedure CheckLine;
begin
Converter: TFormatDescriptor;
begin
if not (ftTGA in FormatGetSupportedFiles(Format)) then
- raise EglBitmapUnsupportedFormatFormat.Create('SaveTGA - ' + UNSUPPORTED_FORMAT);
+ raise EglBitmapUnsupportedFormat.Create('SaveTGA');
//prepare header
FillChar(Header{%H-}, SizeOf(Header), 0);
var
StreamPos: Int64;
x, y, LineSize, RowSize, Magic: Cardinal;
- NewImage, TmpData, RowData, SrcData: PByte;
+ NewImage, TmpData, RowData, SrcData: System.PByte;
SourceMD, DestMD: Pointer;
Pixel: TglBitmapPixelData;
ddsFormat: TglBitmapFormat;
FormatDesc: TFormatDescriptor;
begin
if not (ftDDS in FormatGetSupportedFiles(Format)) then
- raise EglBitmapUnsupportedFormatFormat.Create('SaveDDS - ' + UNSUPPORTED_FORMAT);
+ raise EglBitmapUnsupportedFormat.Create('SaveDDS');
FormatDesc := TFormatDescriptor.Get(Format);
begin
FormatDesc := TFormatDescriptor.Get(Format);
if FormatDesc.IsCompressed then
- raise EglBitmapUnsupportedFormatFormat.Create('TglBitmap2D.GrabScreen - ' + UNSUPPORTED_FORMAT);
+ raise EglBitmapUnsupportedFormat.Create('TglBitmap2D.GrabScreen');
w := aRight - aLeft;
h := aBottom - aTop;