* added pngimage Support
[glBitmap.git] / glBitmap.pas
index d39e7c5..f745956 100644 (file)
@@ -1,6 +1,10 @@
 {***********************************************************
 glBitmap by Steffen Xonna aka Lossy eX (2003-2008)
 http://www.opengl24.de/index.php?cat=header&file=glbitmap
+
+modified by Delphi OpenGL Community (http://delphigl.com/)
+
+
 ------------------------------------------------------------
 The contents of this file are used with permission, subject to
 the Mozilla Public License Version 1.1 (the "License"); you may
@@ -227,7 +231,7 @@ unit glBitmap;
 
 // 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}
@@ -241,26 +245,26 @@ unit glBitmap;
 // If you enable SDL_image all other libraries will be ignored!
 {.$DEFINE GLB_SDL_IMAGE}
 
-// activate to enable png support with the unit pngimage. You can download it from http://pngdelphi.sourceforge.net/
+// PNG /////////////////////////////////////////////////////////////////////////////////////////////
+// activate to enable png support with the unit pngimage -> http://pngdelphi.sourceforge.net/
 // if you enable pngimage the libPNG will be ignored
-{.$DEFINE GLB_PNGIMAGE}
+{$DEFINE GLB_PNGIMAGE}
 
-// activate to use the libPNG http://www.libpng.org/
-// You will need an aditional header.
-// http://www.opengl24.de/index.php?cat=header&file=libpng
+// activate to use the libPNG -> http://www.libpng.org/
+// You will need an aditional header -> http://www.opengl24.de/index.php?cat=header&file=libpng
 {.$DEFINE GLB_LIB_PNG}
 
+// JPEG ////////////////////////////////////////////////////////////////////////////////////////////
 // if you enable delphi jpegs the libJPEG will be ignored
 {.$DEFINE GLB_DELPHI_JPEG}
 
-// activateto use the libJPEG http://www.ijg.org/
-// You will need an aditional header.
-// http://www.opengl24.de/index.php?cat=header&file=libjpeg
+// activate to use the libJPEG -> http://www.ijg.org/
+// You will need an aditional header -> http://www.opengl24.de/index.php?cat=header&file=libjpeg
 {.$DEFINE GLB_LIB_JPEG}
 
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// PRIVATE: DO not change anything! //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// PRIVATE: do not change anything! //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 // Delphi Versions
 {$IFDEF fpc}
@@ -678,12 +682,6 @@ var
 
 type
 ////////////////////////////////////////////////////////////////////////////////////////////////////
-  EglBitmapException               = class(Exception);
-  EglBitmapSizeToLargeException    = class(EglBitmapException);
-  EglBitmapNonPowerOfTwoException  = class(EglBitmapException);
-  EglBitmapUnsupportedFormatFormat = class(EglBitmapException);
-
-////////////////////////////////////////////////////////////////////////////////////////////////////
   TglBitmapFormat = (
     tfEmpty = 0, //must be smallest value!
 
@@ -765,6 +763,14 @@ type
      nm3x3,
      nm5x5);
 
+ ////////////////////////////////////////////////////////////////////////////////////////////////////
+   EglBitmapException               = class(Exception);
+   EglBitmapSizeToLargeException    = class(EglBitmapException);
+   EglBitmapNonPowerOfTwoException  = class(EglBitmapException);
+   EglBitmapUnsupportedFormat       = class(EglBitmapException)
+     constructor Create(const aFormat: TglBitmapFormat);
+   end;
+
 ////////////////////////////////////////////////////////////////////////////////////////////////////
   TglBitmapColorRec = packed record
   case Integer of
@@ -977,14 +983,12 @@ type
     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;
 
@@ -1128,7 +1132,7 @@ implementation
     *)
 
 uses
-  Math, syncobjs;
+  Math, syncobjs, typinfo;
 
 type
 ////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -1140,10 +1144,10 @@ type
 
   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;
@@ -1177,10 +1181,10 @@ type
     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;
@@ -1193,7 +1197,7 @@ type
 
     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;
 
@@ -1532,17 +1536,17 @@ type
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   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;
 
@@ -1653,6 +1657,12 @@ var
   FormatDescriptorCS: TCriticalSection;
   FormatDescriptors: array[TglBitmapFormat] of TFormatDescriptor;
 
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+constructor EglBitmapUnsupportedFormat.Create(const aFormat: TglBitmapFormat);
+begin
+  inherited Create('unsupported format: ' + GetEnumName(TypeInfo(TglBitmapFormat), Integer(aFormat)));
+end;
+
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 function glBitmapPosition(X, Y: Integer): TglBitmapPixelPosition;
 begin
@@ -1759,39 +1769,18 @@ begin
         tfS3tcDtx1RGBA, tfS3tcDtx3RGBA, tfS3tcDtx5RGBA]) then
     result := result + [ftDDS];
 
-  (* TODO
   {$IFDEF GLB_SUPPORT_PNG_WRITE}
   if aFormat in [
-    tfAlpha4, tfAlpha8, tfAlpha12, tfAlpha16,
-    tfLuminance4, tfLuminance8, tfLuminance12, tfLuminance16,
-    tfuminance4Alpha4, tfLuminance6Alpha2, tfLuminance8Alpha8, tfLuminance12Alpha4, tfLuminance12Alpha12, tfLuminance16Alpha16,
-    tfR3G3B2, tfRGB4, tfRGB5, tfRGB8, tfRGB10, tfRGB12, tfRGB16,
-    tfRGBA2, tfRGBA4, tfRGB5A1, tfRGBA8, tfRGB10A2, tfRGBA12, tfRGBA16,
-    tfDepth16, tfDepth24, tfDepth32]
-  then
+      tfAlpha8, tfLuminance8, tfLuminance8Alpha8,
+      tfRGB8, tfRGBA8,
+      tfBGR8, tfBGRA8] then
     result := result + [ftPNG];
   {$ENDIF}
 
   {$IFDEF GLB_SUPPORT_JPEG_WRITE}
-  if Format in [
-    tfAlpha4, tfAlpha8, tfAlpha12, tfAlpha16,
-    tfLuminance4, tfLuminance8, tfLuminance12, tfLuminance16,
-    tfR3G3B2, tfRGB4, tfRGB5, tfRGB8, tfRGB10, tfRGB12, tfRGB16,
-    tfDepth16, tfDepth24, tfDepth32]
-  then
+  if aFormat in [tfAlpha8, tfLuminance8, tfRGB8, tfBGR8] then
     result := result + [ftJPEG];
   {$ENDIF}
-
-  if aFormat in [
-    tfAlpha4, tfAlpha8, tfAlpha12, tfAlpha16,
-    tfLuminance4, tfLuminance8, tfLuminance12, tfLuminance16,
-    tfuminance4Alpha4, tfLuminance6Alpha2, tfLuminance8Alpha8, tfLuminance12Alpha4, tfLuminance12Alpha12, tfLuminance16Alpha16,
-    tfR3G3B2, tfRGB4, tfRGB5, tfRGB8, tfRGB10, tfRGB12, tfRGB16,
-    tfRGBA2, tfRGBA4, tfRGB5A1, tfRGBA8, tfRGB10A2, tfRGBA12, tfRGBA16,
-    tfDepth16, tfDepth24, tfDepth32]
-  then
-    result := result + [ftDDS, ftTGA, ftBMP];
-  *)
 end;
 
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -1803,7 +1792,7 @@ begin
 end;
 
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-function GetTopMostBit(aBitSet: UInt64): Integer;
+function GetTopMostBit(aBitSet: QWord): Integer;
 begin
   result := 0;
   while aBitSet > 0 do begin
@@ -1813,7 +1802,7 @@ begin
 end;
 
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-function CountSetBits(aBitSet: UInt64): Integer;
+function CountSetBits(aBitSet: QWord): Integer;
 begin
   result := 0;
   while aBitSet > 0 do begin
@@ -2126,8 +2115,10 @@ end;
 {$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);
@@ -2162,7 +2153,6 @@ begin
   result^.unknown.data1 := Stream;
 end;
 {$ENDIF}
-*)
 
 (* TODO LoadFuncs
 function LoadTexture(Filename: String; var Texture: Cardinal{$IFDEF GLB_DELPHI}; LoadFromRes : Boolean; Instance: Cardinal{$ENDIF}): Boolean;
@@ -2366,25 +2356,25 @@ end;
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //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;
@@ -2447,7 +2437,7 @@ begin
 end;
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-function TFormatDescriptor.MaskMatch(const aRedMask, aGreenMask, aBlueMask, aAlphaMask: UInt64): Boolean;
+function TFormatDescriptor.MaskMatch(const aRedMask, aGreenMask, aBlueMask, aAlphaMask: QWord): Boolean;
 begin
   result := false;
 
@@ -3781,31 +3771,31 @@ end;
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //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;
@@ -3829,10 +3819,8 @@ end;
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 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
@@ -3844,7 +3832,7 @@ begin
     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;
@@ -3854,17 +3842,15 @@ 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;
@@ -3956,10 +3942,8 @@ 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;
@@ -3972,7 +3956,7 @@ begin
     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;
@@ -4227,7 +4211,7 @@ begin
   if fFormat = aValue then
     exit;
   if TFormatDescriptor.Get(Format).PixelSize <> TFormatDescriptor.Get(aValue).PixelSize then
-    raise EglBitmapUnsupportedFormatFormat.Create('SetInternalFormat - ' + UNSUPPORTED_FORMAT);
+    raise EglBitmapUnsupportedFormat.Create(Format);
   SetDataPointer(Data, aValue, Width, Height);
 end;
 
@@ -4498,7 +4482,7 @@ procedure TglBitmap.SaveToStream(const aStream: TStream; const aFileType: TglBit
 begin
   case aFileType of
     {$IFDEF GLB_SUPPORT_PNG_WRITE}
-    ftPNG:  SavePng(aStream);
+    ftPNG:  SavePNG(aStream);
     {$ENDIF}
     {$IFDEF GLB_SUPPORT_JPEG_WRITE}
     ftJPEG: SaveJPEG(aStream);
@@ -4603,42 +4587,37 @@ var
   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(Format);
 
-  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(Format);
     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);
@@ -4654,34 +4633,31 @@ function TglBitmap.AssignFromSurface(const aSurface: PSDL_Surface): Boolean;
 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;
@@ -4721,9 +4697,9 @@ begin
 
       AlphaInterleave := 0;
       case Format of
-        ifLuminance8Alpha8:
+        tfLuminance8Alpha8:
           AlphaInterleave := 1;
-        ifBGRA8, ifRGBA8:
+        tfBGRA8, tfRGBA8:
           AlphaInterleave := 3;
       end;
 
@@ -4745,14 +4721,14 @@ begin
 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;
@@ -4967,10 +4943,8 @@ end;
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 function TglBitmap.AddAlphaFromFunc(const aFunc: TglBitmapFunction; const aArgs: Pointer): Boolean;
 begin
-  (* TODO
-  if not FormatIsUncompressed(InternalFormat) then
-    raise EglBitmapUnsupportedFormatFormat.Create('AddAlphaFromFunc - ' + UNSUPPORTED_FORMAT);
-  *)
+  if TFormatDescriptor.Get(Format).IsCompressed then
+    raise EglBitmapUnsupportedFormat.Create(Format);
   result := AddFunc(Self, aFunc, false, TFormatDescriptor.Get(Format).WithAlpha, aArgs);
 end;
 
@@ -5147,7 +5121,7 @@ begin
   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(Format);
     result := ConvertTo(FormatDesc.WithoutAlpha);
   end;
 end;
@@ -5522,7 +5496,7 @@ begin
       Surface := IMG_LoadPNG_RW(RWops);
       try
         AssignFromSurface(Surface);
-        Rresult := true;
+        result := true;
       finally
         SDL_FreeSurface(Surface);
       end;
@@ -5548,7 +5522,7 @@ var
   png_info: png_infop;
 
   TempHeight, TempWidth: Integer;
-  Format: TglBitmapInternalFormat;
+  Format: TglBitmapFormat;
 
   png_data: pByte;
   png_rows: array of pByte;
@@ -5561,9 +5535,9 @@ begin
 
   try
     // signature
-    StreamPos := Stream.Position;
-    Stream.Read(signature, 8);
-    Stream.Position := StreamPos;
+    StreamPos := aStream.Position;
+    aStream.Read(signature{%H-}, 8);
+    aStream.Position := StreamPos;
 
     if png_check_sig(@signature, 8) <> 0 then begin
       // png read struct
@@ -5579,7 +5553,7 @@ begin
       end;
 
       // set read callback
-      png_set_read_fn(png, stream, glBitmap_libPNG_read_func);
+      png_set_read_fn(png, aStream, glBitmap_libPNG_read_func);
 
       // read informations
       png_read_info(png, png_info);
@@ -5651,42 +5625,44 @@ function TglBitmap.LoadPNG(const aStream: TStream): Boolean;
 var
   StreamPos: Int64;
   Png: TPNGObject;
-  Header: Array[0..7] of Byte;
+  Header: String[8];
   Row, Col, PixSize, LineSize: Integer;
   NewImage, pSource, pDest, pAlpha: pByte;
-  Format: TglBitmapInternalFormat;
+  PngFormat: TglBitmapFormat;
+  FormatDesc: TFormatDescriptor;
 
 const
-  PngHeader: Array[0..7] of Byte = (#137, #80, #78, #71, #13, #10, #26, #10);
+  PngHeader: String[8] = #137#80#78#71#13#10#26#10;
 
 begin
   result := false;
 
-  StreamPos := Stream.Position;
-  Stream.Read(Header[0], SizeOf(Header));
-  Stream.Position := StreamPos;
+  StreamPos := aStream.Position;
+  aStream.Read(Header[0], SizeOf(Header));
+  aStream.Position := StreamPos;
 
   {Test if the header matches}
   if Header = PngHeader then begin
     Png := TPNGObject.Create;
     try
-      Png.LoadFromStream(Stream);
+      Png.LoadFromStream(aStream);
 
       case Png.Header.ColorType of
         COLOR_GRAYSCALE:
-          Format := ifLuminance;
+          PngFormat := tfLuminance8;
         COLOR_GRAYSCALEALPHA:
-          Format := ifLuminanceAlpha;
+          PngFormat := tfLuminance8Alpha8;
         COLOR_RGB:
-          Format := ifBGR8;
+          PngFormat := tfBGR8;
         COLOR_RGBALPHA:
-          Format := ifBGRA8;
+          PngFormat := tfBGRA8;
         else
           raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
       end;
 
-      PixSize := Trunc(FormatGetSize(Format));
-      LineSize := Integer(Png.Header.Width) * PixSize;
+      FormatDesc := TFormatDescriptor.Get(PngFormat);
+      PixSize    := Round(FormatDesc.PixelSize);
+      LineSize   := FormatDesc.GetSize(Png.Header.Width, 1);
 
       GetMem(NewImage, LineSize * Integer(Png.Header.Height));
       try
@@ -5723,7 +5699,7 @@ begin
             raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.');
         end;
 
-        SetDataPointer(NewImage, Format, Png.Header.Width, Png.Header.Height);
+        SetDataPointer(NewImage, PngFormat, Png.Header.Width, Png.Header.Height);
 
         result := true;
       except
@@ -5757,27 +5733,30 @@ var
   LineSize: Integer;
   ColorType: Integer;
   Row: Integer;
+  FormatDesc: TFormatDescriptor;
 begin
-  if not (ftPNG in FormatGetSupportedFiles (InternalFormat)) then
-    raise EglBitmapUnsupportedInternalFormat.Create('SavePng - ' + UNSUPPORTED_INTERNAL_FORMAT);
+  if not (ftPNG in FormatGetSupportedFiles(Format)) then
+    raise EglBitmapUnsupportedFormat.Create(Format);
 
   if not init_libPNG then
-    raise Exception.Create('SavePNG - unable to initialize libPNG.');
+    raise Exception.Create('unable to initialize libPNG.');
 
   try
-    case FInternalFormat of
-      ifAlpha, ifLuminance, ifDepth8:
+    case Format of
+      tfAlpha8, tfLuminance8:
         ColorType := PNG_COLOR_TYPE_GRAY;
-      ifLuminanceAlpha:
+      tfLuminance8Alpha8:
         ColorType := PNG_COLOR_TYPE_GRAY_ALPHA;
-      ifBGR8, ifRGB8:
+      tfBGR8, tfRGB8:
         ColorType := PNG_COLOR_TYPE_RGB;
-      ifBGRA8, ifRGBA8:
+      tfBGRA8, tfRGBA8:
         ColorType := PNG_COLOR_TYPE_RGBA;
       else
-        raise EglBitmapUnsupportedInternalFormat.Create('SavePng - ' + UNSUPPORTED_INTERNAL_FORMAT);
+        raise EglBitmapUnsupportedFormat.Create(Format);
     end;
-    LineSize := Trunc(FormatGetSize(FInternalFormat) * Width);
+
+    FormatDesc := TFormatDescriptor.Get(Format);
+    LineSize := FormatDesc.GetSize(Width, 1);
 
     // creating array for scanline
     SetLength(png_rows, Height);
@@ -5800,12 +5779,12 @@ begin
       end;
 
       // set read callback
-      png_set_write_fn(png, stream, glBitmap_libPNG_write_func, nil);
+      png_set_write_fn(png, aStream, glBitmap_libPNG_write_func, nil);
 
       // set compression
       png_set_compression_level(png, 6);
 
-      if InternalFormat in [ifBGR8, ifBGRA8] then
+      if Format in [tfBGR8, tfBGRA8] then
         png_set_bgr(png);
 
       png_set_IHDR(png, png_info, Width, Height, 8, ColorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
@@ -5835,32 +5814,32 @@ var
   pTemp: pByte;
   Temp: Byte;
 begin
-  if not (ftPNG in FormatGetSupportedFiles (InternalFormat)) then
-    raise EglBitmapUnsupportedInternalFormat.Create('SavePng - ' + UNSUPPORTED_INTERNAL_FORMAT);
+  if not (ftPNG in FormatGetSupportedFiles (Format)) then
+    raise EglBitmapUnsupportedFormat.Create(Format);
 
-  case FInternalFormat of
-    ifAlpha, ifLuminance, ifDepth8: begin
+  case Format of
+    tfAlpha8, tfLuminance8: begin
       ColorType := COLOR_GRAYSCALE;
-      PixSize := 1;
-      Alpha := false;
+      PixSize   := 1;
+      Alpha     := false;
     end;
-    ifLuminanceAlpha: begin
+    tfLuminance8Alpha8: begin
       ColorType := COLOR_GRAYSCALEALPHA;
-      PixSize := 1;
-      Alpha := true;
+      PixSize   := 1;
+      Alpha     := true;
     end;
-    ifBGR8, ifRGB8: begin
+    tfBGR8, tfRGB8: begin
       ColorType := COLOR_RGB;
-      PixSize := 3;
-      Alpha := false;
+      PixSize   := 3;
+      Alpha     := false;
     end;
-    ifBGRA8, ifRGBA8: begin
+    tfBGRA8, tfRGBA8: begin
       ColorType := COLOR_RGBALPHA;
-      PixSize := 3;
-      Alpha := true
+      PixSize   := 3;
+      Alpha     := true
     end;
   else
-    raise EglBitmapUnsupportedInternalFormat.Create('SavePng - ' + UNSUPPORTED_INTERNAL_FORMAT);
+    raise EglBitmapUnsupportedFormat.Create(Format);
   end;
 
   Png := TPNGObject.CreateBlank(ColorType, 8, Width, Height);
@@ -5880,7 +5859,7 @@ begin
       end;
 
       // convert RGB line to BGR
-      if InternalFormat in [ifRGB8, ifRGBA8] then begin
+      if Format in [tfRGB8, tfRGBA8] then begin
         pTemp := png.ScanLine[Y];
         for X := 0 to Width -1 do begin
           Temp := pByteArray(pTemp)^[0];
@@ -5893,7 +5872,7 @@ begin
 
     // Save to Stream
     Png.CompressionLevel := 6;
-    Png.SaveToStream(Stream);
+    Png.SaveToStream(aStream);
   finally
     FreeAndNil(Png);
   end;
@@ -5922,39 +5901,34 @@ type
     DestBuffer: array [1..4096] of byte;
   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);
+  //DUMMY
 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;
+  //DUMMY
 end;
-}
 
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-{
+
 procedure glBitmap_libJPEG_init_source(cinfo: j_decompress_ptr); cdecl;
 begin
+  //DUMMY
+end;
+
+procedure glBitmap_libJPEG_term_source(cinfo: j_decompress_ptr); cdecl;
+begin
+  //DUMMY
+end;
+
+
+procedure glBitmap_libJPEG_init_destination(cinfo: j_compress_ptr); cdecl;
+begin
+  //DUMMY
 end;
-}
+
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 function glBitmap_libJPEG_fill_input_buffer(cinfo: j_decompress_ptr): boolean; cdecl;
@@ -5998,20 +5972,6 @@ begin
 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;
@@ -6062,7 +6022,7 @@ var
 begin
   result := false;
 
-  RWops := glBitmapCreateRWops(Stream);
+  RWops := glBitmapCreateRWops(aStream);
   try
     if IMG_isJPG(RWops) > 0 then begin
       Surface := IMG_LoadJPG_RW(RWops);
@@ -6088,12 +6048,14 @@ var
   jpeg: jpeg_decompress_struct;
   jpeg_err: jpeg_error_mgr;
 
-  IntFormat: TglBitmapInternalFormat;
+  IntFormat: TglBitmapFormat;
   pImage: pByte;
   TempHeight, TempWidth: Integer;
 
   pTemp: pByte;
   Row: Integer;
+
+  FormatDesc: TFormatDescriptor;
 begin
   result := false;
 
@@ -6102,18 +6064,18 @@ begin
 
   try
     // reading first two bytes to test file and set cursor back to begin
-    StreamPos := Stream.Position;
-    Stream.Read(Temp[0], 2);
-    Stream.Position := StreamPos;
+    StreamPos := aStream.Position;
+    aStream.Read({%H-}Temp[0], 2);
+    aStream.Position := StreamPos;
 
     // if Bitmap then read file.
     if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin
-      FillChar(jpeg, SizeOf(jpeg_decompress_struct), $00);
-      FillChar(jpeg_err, SizeOf(jpeg_error_mgr), $00);
+      FillChar(jpeg{%H-}, SizeOf(jpeg_decompress_struct), $00);
+      FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
 
       // error managment
       jpeg.err := jpeg_std_error(@jpeg_err);
-      jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
+      jpeg_err.error_exit     := glBitmap_libJPEG_error_exit;
       jpeg_err.output_message := glBitmap_libJPEG_output_message;
 
       // decompression struct
@@ -6133,7 +6095,7 @@ begin
         pub.bytes_in_buffer := 0;     // forces fill_input_buffer on first read
         pub.next_input_byte := nil;   // until buffer loaded
 
-        SrcStream := Stream;
+        SrcStream := aStream;
       end;
 
       // set global decoding state
@@ -6147,11 +6109,11 @@ begin
         JCS_GRAYSCALE:
           begin
             jpeg.out_color_space := JCS_GRAYSCALE;
-            IntFormat := ifLuminance;
+            IntFormat := tfLuminance8;
           end;
         else
           jpeg.out_color_space := JCS_RGB;
-          IntFormat := ifRGB8;
+          IntFormat := tfRGB8;
       end;
 
       // reading image
@@ -6160,14 +6122,16 @@ begin
       TempHeight := jpeg.output_height;
       TempWidth := jpeg.output_width;
 
+      FormatDesc := TFormatDescriptor.Get(IntFormat);
+
       // creating new image
-      GetMem(pImage, FormatGetImageSize(glBitmapPosition(TempWidth, TempHeight), IntFormat));
+      GetMem(pImage, FormatDesc.GetSize(TempWidth, TempHeight));
       try
         pTemp := pImage;
 
         for Row := 0 to TempHeight -1 do begin
           jpeg_read_scanlines(@jpeg, @pTemp, 1);
-          Inc(pTemp, Trunc(FormatGetSize(IntFormat) * TempWidth));
+          Inc(pTemp, FormatDesc.GetSize(TempWidth, 1));
         end;
 
         // finish decompression
@@ -6226,9 +6190,9 @@ end;
 {$ENDIF}
 
 {$IFDEF GLB_SUPPORT_JPEG_WRITE}
-{$IF DEFEFINED(GLB_LIB_JPEG)}
+{$IF DEFINED(GLB_LIB_JPEG)}
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-procedure TglBitmap.SaveJPEG(Stream: TStream);
+procedure TglBitmap.SaveJPEG(const aStream: TStream);
 var
   jpeg: jpeg_compress_struct;
   jpeg_err: jpeg_error_mgr;
@@ -6250,18 +6214,18 @@ var
 
 begin
   if not (ftJPEG in FormatGetSupportedFiles(Format)) then
-    raise EglBitmapUnsupportedInternalFormat.Create('SaveJpg - ' + UNSUPPORTED_INTERNAL_FORMAT);
+    raise EglBitmapUnsupportedFormat.Create(Format);
 
   if not init_libJPEG then
     raise Exception.Create('SaveJPG - unable to initialize libJPEG.');
 
   try
-    FillChar(jpeg, SizeOf(jpeg_compress_struct), $00);
-    FillChar(jpeg_err, SizeOf(jpeg_error_mgr), $00);
+    FillChar(jpeg{%H-}, SizeOf(jpeg_compress_struct), $00);
+    FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00);
 
     // error managment
     jpeg.err := jpeg_std_error(@jpeg_err);
-    jpeg_err.error_exit := glBitmap_libJPEG_error_exit;
+    jpeg_err.error_exit     := glBitmap_libJPEG_error_exit;
     jpeg_err.output_message := glBitmap_libJPEG_output_message;
 
     // compression struct
@@ -6279,21 +6243,21 @@ begin
       pub.next_output_byte  := @DestBuffer[1];
       pub.free_in_buffer    := Length(DestBuffer);
 
-      DestStream := Stream;
+      DestStream := aStream;
     end;
 
     // very important state
     jpeg.global_state := CSTATE_START;
     jpeg.image_width  := Width;
     jpeg.image_height := Height;
-    case InternalFormat of
-      ifAlpha, ifLuminance, ifDepth8: begin
+    case Format of
+      tfAlpha8, tfLuminance8: begin
         jpeg.input_components := 1;
-        jpeg.in_color_space := JCS_GRAYSCALE;
+        jpeg.in_color_space   := JCS_GRAYSCALE;
       end;
-      ifRGB8, ifBGR8: begin
+      tfRGB8, tfBGR8: begin
         jpeg.input_components := 3;
-        jpeg.in_color_space := JCS_RGB;
+        jpeg.in_color_space   := JCS_RGB;
       end;
     end;
 
@@ -6302,7 +6266,7 @@ begin
     jpeg_start_compress(@jpeg, true);
     pTemp := Data;
 
-    if InternalFormat = ifBGR8 then
+    if Format = tfBGR8 then
       GetMem(pTemp2, fRowSize)
     else
       pTemp2 := pTemp;
@@ -6310,7 +6274,7 @@ begin
     try
       for Row := 0 to jpeg.image_height -1 do begin
         // prepare row
-        if InternalFormat = ifBGR8 then
+        if Format = tfBGR8 then
           CopyRow(pTemp2, pTemp)
         else
           pTemp2 := pTemp;
@@ -6321,7 +6285,7 @@ begin
       end;
     finally
       // free memory
-      if InternalFormat = ifBGR8 then
+      if Format = tfBGR8 then
         FreeMem(pTemp2);
     end;
     jpeg_finish_compress(@jpeg);
@@ -6622,7 +6586,7 @@ var
 
 begin
   if not (ftBMP in FormatGetSupportedFiles(Format)) then
-    raise EglBitmapUnsupportedFormatFormat.Create('SaveBMP - ' + UNSUPPORTED_FORMAT);
+    raise EglBitmapUnsupportedFormat.Create(Format);
 
   Converter  := nil;
   FormatDesc := TFormatDescriptor.Get(Format);
@@ -6687,7 +6651,7 @@ begin
         Info.biCompression := BMP_COMP_BITFIELDS;
       end;
     else
-      raise EglBitmapUnsupportedFormatFormat.Create('SaveBMP - ' + UNSUPPORTED_FORMAT);
+      raise EglBitmapUnsupportedFormat.Create(Format);
     end;
     Info.biXPelsPerMeter := 2835;
     Info.biYPelsPerMeter := 2835;
@@ -6804,7 +6768,7 @@ const
 function TglBitmap.LoadTGA(const aStream: TStream): Boolean;
 var
   Header: TTGAHeader;
-  ImageData: PByte;
+  ImageData: System.PByte;
   StartPosition: Int64;
   PixelSize, LineSize: Integer;
   tgaFormat: TglBitmapFormat;
@@ -6822,7 +6786,7 @@ const
   procedure ReadUncompressed;
   var
     i, j: Integer;
-    buf, tmp1, tmp2: PByte;
+    buf, tmp1, tmp2: System.PByte;
   begin
     buf := nil;
     if (Counter.X.dir < 0) then
@@ -6856,7 +6820,7 @@ const
 
     /////////////////////////////////////////////////////////////////
     var
-      TmpData: PByte;
+      TmpData: System.PByte;
       LinePixelsRead: Integer;
     procedure CheckLine;
     begin
@@ -7083,7 +7047,7 @@ var
   Converter: TFormatDescriptor;
 begin
   if not (ftTGA in FormatGetSupportedFiles(Format)) then
-    raise EglBitmapUnsupportedFormatFormat.Create('SaveTGA - ' + UNSUPPORTED_FORMAT);
+    raise EglBitmapUnsupportedFormat.Create(Format);
 
   //prepare header
   FillChar(Header{%H-}, SizeOf(Header), 0);
@@ -7296,7 +7260,7 @@ var
 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;
@@ -7399,7 +7363,7 @@ var
   FormatDesc: TFormatDescriptor;
 begin
   if not (ftDDS in FormatGetSupportedFiles(Format)) then
-    raise EglBitmapUnsupportedFormatFormat.Create('SaveDDS - ' + UNSUPPORTED_FORMAT);
+    raise EglBitmapUnsupportedFormat.Create(Format);
 
   FormatDesc := TFormatDescriptor.Get(Format);
 
@@ -7542,7 +7506,7 @@ var
 begin
   FormatDesc := TFormatDescriptor.Get(Format);
   if FormatDesc.IsCompressed then
-    raise EglBitmapUnsupportedFormatFormat.Create('TglBitmap2D.GrabScreen - ' + UNSUPPORTED_FORMAT);
+    raise EglBitmapUnsupportedFormat.Create(Format);
 
   w    := aRight  - aLeft;
   h    := aBottom - aTop;
@@ -7792,10 +7756,8 @@ var
   end;
 
 begin
-  (* TODO Compression
-  if not FormatIsUncompressed(InternalFormat) then
-    raise EglBitmapUnsupportedFormatFormat.Create('TglBitmap2D.ToNormalMap - ' + UNSUPPORTED_FORMAT);
-    *)
+  if TFormatDescriptor.Get(Format).IsCompressed then
+    raise EglBitmapUnsupportedFormat.Create(Format);
 
   if aScale > 100 then
     Rec.Scale := 100