* refactored FormatDescriptors
[glBitmap.git] / glBitmap.pas
index 1a21c20..04f4a9f 100644 (file)
@@ -583,23 +583,25 @@ var
 *)
 
 type
-  EglBitmapException                 = class(Exception);
-  EglBitmapSizeToLargeException      = class(EglBitmapException);
-  EglBitmapNonPowerOfTwoException    = class(EglBitmapException);
-  EglBitmapUnsupportedInternalFormat = class(EglBitmapException);
+////////////////////////////////////////////////////////////////////////////////////////////////////
+  EglBitmapException               = class(Exception);
+  EglBitmapSizeToLargeException    = class(EglBitmapException);
+  EglBitmapNonPowerOfTwoException  = class(EglBitmapException);
+  EglBitmapUnsupportedFormatFormat = class(EglBitmapException);
 
+////////////////////////////////////////////////////////////////////////////////////////////////////
   TglBitmapPixelDesc = packed record
     RedRange: Cardinal;
-    GreenRange: Cardinal;
-    BlueRange: Cardinal;
-    AlphaRange: Cardinal;
-
     RedShift: Shortint;
+    GreenRange: Cardinal;
     GreenShift: Shortint;
+    BlueRange: Cardinal;
     BlueShift: Shortint;
+    AlphaRange: Cardinal;
     AlphaShift: Shortint;
   end;
 
+////////////////////////////////////////////////////////////////////////////////////////////////////
   TglBitmapPixelData = packed record
     Red: Cardinal;
     Green: Cardinal;
@@ -608,6 +610,14 @@ type
     PixelDesc: TglBitmapPixelDesc;
   end;
 
+////////////////////////////////////////////////////////////////////////////////////////////////////
+  TglBitmapFormatDesc = packed record
+    Format: Cardinal;
+    InternalFormat: Cardinal;
+    DataType: Cardinal;
+  end;
+
+////////////////////////////////////////////////////////////////////////////////////////////////////
   TglBitmapPixelPositionFields = set of (ffX, ffY);
   TglBitmapPixelPosition = record
     Fields : TglBitmapPixelPositionFields;
@@ -615,12 +625,8 @@ type
     Y : Word;
   end;
 
-const
-  NULL_SIZE: TglBitmapPixelPosition = (Fields: []; X: 0; Y: 0);
-
-type
+////////////////////////////////////////////////////////////////////////////////////////////////////
   TglBitmap = class;
-
   TglBitmapFunctionRec = record
     Sender : TglBitmap;
     Size: TglBitmapPixelPosition;
@@ -629,17 +635,9 @@ type
     Dest: TglBitmapPixelData;
     CustomData: Pointer;
   end;
-
   TglBitmapFunction = procedure(var FuncRec: TglBitmapFunctionRec);
 
-  TglBitmapGetPixel = procedure (
-    const Pos: TglBitmapPixelPosition;
-    var Pixel: TglBitmapPixelData) of object;
-
-  TglBitmapSetPixel = procedure (
-    const Pos: TglBitmapPixelPosition;
-    const Pixel: TglBitmapPixelData) of object;
-
+////////////////////////////////////////////////////////////////////////////////////////////////////
   TglBitmapFileType = (
       {$IFDEF GLB_SUPPORT_PNG_WRITE} ftPNG,  {$ENDIF}
       {$IFDEF GLB_SUPPORT_JPEG_WRITE}ftJPEG, {$ENDIF}
@@ -652,66 +650,95 @@ type
     mmNone,
     mmMipmap,
     mmMipmapGlu);
+
   TglBitmapNormalMapFunc = (
     nm4Samples,
     nmSobel,
     nm3x3,
     nm5x5);
+
   TglBitmapFormat = (
-    tfRed = GL_RED,
-    tfGreen = GL_GREEN,
-    tfBlue = GL_BLUE,
-    tfAlpha = GL_ALPHA,
-    tfRGB = GL_RGB,
-    tfBGR = GL_BGR,
-    tfRGBA = GL_RGBA,
-    tfBGRA = GL_BGRA,
-    tfLuminance = GL_LUMINANCE,
-    tfLuminanceAlpha = GL_LUMINANCE_ALPHA
-  );
-  TglBitmapInternalFormat = (
-    ifAlpha4 = GL_ALPHA4,
-    ifAlpha8 = GL_ALPHA8,
-    ifAlpha12 = GL_ALPHA12,
-    ifAlpha16 = GL_ALPHA16,
-
-    ifLuminance4 = GL_LUMINANCE4,
-    ifLuminance8 = GL_LUMINANCE8,
-    ifLuminance12 = GL_LUMINANCE12,
-    ifLuminance16 = GL_LUMINANCE16,
-
-    ifLuminance4Alpha4 = GL_LUMINANCE4_ALPHA4,
-    ifLuminance6Alpha2 = GL_LUMINANCE6_ALPHA2,
-    ifLuminance8Alpha8 = GL_LUMINANCE8_ALPHA8,
-    ifLuminance12Alpha4 = GL_LUMINANCE12_ALPHA4,
-    ifLuminance12Alpha12 = GL_LUMINANCE12_ALPHA12,
-    ifLuminance16Alpha16 = GL_LUMINANCE16_ALPHA16,
-
-    ifR3G3B2 = GL_R3_G3_B2,
-    ifRGB4 = GL_RGB4,
-    ifRGB5 = GL_RGB5,
-    ifRGB8 = GL_RGB8,
-    ifRGB10 = GL_RGB10,
-    ifRGB12 = GL_RGB12,
-    ifRGB16 = GL_RGB16,
-
-    ifRGBA2 = GL_RGBA2,
-    ifRGBA4 = GL_RGBA4,
-    ifRGB5A1 = GL_RGB5_A1,
-    ifRGBA8 = GL_RGBA8,
-    ifRGB10A2 = GL_RGB10_A2,
-    ifRGBA12 = GL_RGBA12,
-    ifRGBA16 = GL_RGBA16,
-
-    ifDepth16 = GL_DEPTH_COMPONENT16,
-    ifDepth24 = GL_DEPTH_COMPONENT24,
-    ifDepth32 = GL_DEPTH_COMPONENT32
+    tfEmpty = 0,
+    { TODO
+    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,
+    }
+    tfBGR8,
+    tfBGRA8,
+    {
+    tfDepth16,
+    tfDepth24,
+    tfDepth32 }
   );
 
-  // Pixelmapping
+////////////////////////////////////////////////////////////////////////////////////////////////////
+  TglBitmapGetPixel  = procedure(const Pos: TglBitmapPixelPosition; var   Pixel: TglBitmapPixelData) of object;
+  TglBitmapSetPixel  = procedure(const Pos: TglBitmapPixelPosition; const Pixel: TglBitmapPixelData) of object;
+
   TglBitmapMapFunc   = procedure(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
   TglBitmapUnMapFunc = procedure(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData);
 
+////////////////////////////////////////////////////////////////////////////////////////////////////
+  TglBitmapFormatDescriptor = class(TObject)
+  public
+    //virtual abstract
+    class function GetFormat:     TglBitmapFormat;     virtual; abstract;
+    class function GetPixelDesc:  TglBitmapPixelDesc;  virtual; abstract;
+    class function GetFormatDesc: TglBitmapFormatDesc; virtual; abstract;
+
+    class procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte); virtual; abstract;
+    class procedure Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData); virtual; abstract;
+
+    //virtual
+    class function WithoutAlpha: TglBitmapFormat; virtual;
+    class function WithAlpha:    TglBitmapFormat; virtual;
+
+    class function IsEmpty:  Boolean; virtual;
+    class function HasAlpha: Boolean; virtual;
+    class function MaskMatch(const aRedMask, aGreenMask, aBlueMask, aAlphaMask: UInt64): Boolean; virtual;
+
+    class procedure PreparePixel(var aPixel: TglBitmapPixelData); virtual;
+
+    (* TODO
+    function FormatIsCompressed(Format: TglBitmapInternalFormat): boolean;
+    function FormatIsUncompressed(Format: TglBitmapInternalFormat): boolean;
+    function LoadTexture(Filename: String; var Texture: Cardinal{$IFDEF GLB_DELPHI}; LoadFromRes : Boolean; Instance: Cardinal = 0{$ENDIF}): Boolean;
+    function LoadCubeMap(PositiveX, NegativeX, PositiveY, NegativeY, PositiveZ, NegativeZ: String; var Texture: Cardinal{$IFDEF GLB_DELPHI}; LoadFromRes : Boolean; Instance: Cardinal = 0{$ENDIF}): Boolean;
+    function LoadNormalMap(Size: Integer; var Texture: Cardinal): Boolean;
+    *)
+  end;
+  TglBitmapFormatDescClass = class of TglBitmapFormatDescriptor;
+
   // Base Class
   TglBitmap = class
   protected
@@ -727,7 +754,6 @@ type
     fDimension: TglBitmapPixelPosition;
     fMipMap: TglBitmapMipMap;
     fFormat: TglBitmapFormat;
-    fInternalFormat: TglBitmapInternalFormat;
 
     // Mapping
     fPixelSize: Integer;
@@ -768,7 +794,6 @@ type
     procedure SetMipMap(const aValue: TglBitmapMipMap);
     procedure SetTarget(const aValue: Cardinal);
     procedure SetAnisotropic(const aValue: Integer);
-    procedure SetInternalFormat(const aValue: TglBitmapInternalFormat);
 
     //Load
     {$IFDEF GLB_SUPPORT_PNG_READ}
@@ -793,10 +818,10 @@ type
     procedure SaveBMP(Stream: TStream); virtual;
 
     procedure CreateID;
-    procedure SetupParameters(var BuildWithGlu: Boolean);
-    procedure SelectFormat(DataFormat: TglBitmapInternalFormat; var glFormat, glInternalFormat, glType: Cardinal; CanConvertImage: Boolean = True);
+    procedure SetupParameters(var aBuildWithGlu: Boolean);
+    procedure SelectFormat(const aFormat: TglBitmapFormat; var glFormat, glInternalFormat, glType: Cardinal);
 
-    procedure SetDataPointer(NewData: pByte; Format: TglBitmapInternalFormat; Width: Integer = -1; Height: Integer = -1); virtual;
+    procedure SetDataPointer(NewData: pByte; Format: TglBitmapFormat; Width: Integer = -1; Height: Integer = -1); virtual;
     procedure GenTexture(TestTextureSize: Boolean = True); virtual; abstract;
 
     function FlipHorz: Boolean; virtual;
@@ -805,12 +830,11 @@ type
     property Width:  Integer read GetWidth;
     property Height: Integer read GetHeight;
   public
-    property ID:             Cardinal                read fID             write SetID;
-    property Target:         Cardinal                read fTarget         write SetTarget;
-    property Format:         TglBitmapFormat         read fFormat         write SetFormat;
-    property InternalFormat: TglBitmapInternalFormat read fInternalFormat write SetInternalFormat;
-    property MipMap:         TglBitmapMipMap         read fMipMap      write SetMipMap;
-    property Anisotropic:    Integer                 read fAnisotropic write SetAnisotropic;
+    property ID:           Cardinal        read fID          write SetID;
+    property Target:       Cardinal        read fTarget      write SetTarget;
+    property Format:       TglBitmapFormat read fFormat      write SetFormat;
+    property MipMap:       TglBitmapMipMap read fMipMap      write SetMipMap;
+    property Anisotropic:  Integer         read fAnisotropic write SetAnisotropic;
 
     property Filename:    String     read fFilename;
     property CustomName:  String     read fCustomName  write SetCustomName;
@@ -831,17 +855,16 @@ type
     procedure LoadFromFile(const aFileName: String);
     procedure LoadFromStream(const aStream: TStream); virtual;
     procedure LoadFromFunc(const aSize: TglBitmapPixelPosition; const aFunc: TglBitmapFunction;
-      const aFormat: TglBitmapFormat; const aInternalFormat: TglBitmapInternalFormat;
-      const aArgs: PtrInt = 0);
+      const aFormat: TglBitmapFormat; const aArgs: PtrInt = 0);
     {$IFDEF GLB_DELPHI}
-    procedure LoadFromResource(Instance: Cardinal; Resource: String; ResType: PChar = nil);
-    procedure LoadFromResourceID(Instance: Cardinal; ResourceID: Integer; ResType: PChar);
+    procedure LoadFromResource(const aInstance: Cardinal; aResource: String; const aResType: PChar = nil);
+    procedure LoadFromResourceID(const sInstance: Cardinal; aResourceID: Integer; const aResType: PChar);
     {$ENDIF}
 
     procedure SaveToFile(const aFileName: String; const aFileType: TglBitmapFileType);
     procedure SaveToStream(const aStream: TStream; const aFileType: TglBitmapFileType); virtual;
 
-    //function AddFunc(const aSource: TglBitmap; const aFunc: TglBitmapFunction; const aCreateTemp: Boolean; Format: TglBitmapInternalFormat; CustomData: Pointer = nil): boolean; overload;
+    //function AddFunc(const aSource: TglBitmap; const aFunc: TglBitmapFunction; const aCreateTemp: Boolean; Format: TglBitmapFormat; CustomData: Pointer = nil): boolean; overload;
     //function AddFunc(const aFunc: TglBitmapFunction; CreateTemp: Boolean; CustomData: Pointer = nil): boolean; overload;
 (* TODO
     {$IFDEF GLB_SDL}
@@ -881,7 +904,7 @@ type
 
     function RemoveAlpha: Boolean; virtual;
     function Clone: TglBitmap;
-    function ConvertTo(const aFormat: TglBitmapFormat; const aInternalFormat: TglBitmapInternalFormat): Boolean; virtual;
+    function ConvertTo(const aFormat: TglBitmapFormat; const aInternalFormat: TglBitmapFormat): Boolean; virtual;
     procedure SetBorderColor(Red, Green, Blue, Alpha: Single);
     procedure Invert(const aUseRGB: Boolean = true; aUseAlpha: Boolean = false);
     procedure FreeData;
@@ -910,8 +933,8 @@ type
     constructor Create(Instance: Cardinal; Resource: String; ResType: PChar = nil); overload;
     constructor Create(Instance: Cardinal; ResourceID: Integer; ResType: PChar); overload;
     {$ENDIF}
-    constructor Create(Size: TglBitmapPixelPosition; Format: TglBitmapInternalFormat); overload;
-    constructor Create(Size: TglBitmapPixelPosition; Format: TglBitmapInternalFormat; Func: TglBitmapFunction; CustomData: Pointer = nil); overload;
+    constructor Create(Size: TglBitmapPixelPosition; Format: TglBitmapFormat); overload;
+    constructor Create(Size: TglBitmapPixelPosition; Format: TglBitmapFormat; Func: TglBitmapFunction; CustomData: Pointer = nil); overload;
   end;
 
 
@@ -929,7 +952,7 @@ type
 
     procedure SetPixel2DUnmap(const Pos: TglBitmapPixelPosition; const Pixel: TglBitmapPixelData);
 
-    procedure SetDataPointer(Data: pByte; Format: TglBitmapInternalFormat; Width: Integer = -1; Height: Integer = -1); override;
+    procedure SetDataPointer(Data: pByte; Format: TglBitmapFormat; Width: Integer = -1; Height: Integer = -1); override;
     procedure UploadData (Target, Format, InternalFormat, Typ: Cardinal; BuildWithGlu: Boolean);
   public
     property Width;
@@ -938,7 +961,7 @@ type
 
     procedure AfterConstruction; override;
 
-    procedure GrabScreen(const aTop, aLeft, aRight, aBottom: Integer; const aFormat: TglBitmapFormat; const aInternalFormat: TglBitmapInternalFormat);
+    procedure GrabScreen(const aTop, aLeft, aRight, aBottom: Integer; const aFormat: TglBitmapFormat);
     procedure GetDataFromTexture;
     procedure ToNormalMap(const aFunc: TglBitmapNormalMapFunc = nm3x3; const aScale: Single = 2; const aUseAlpha: Boolean = False);
     procedure GenTexture(TestTextureSize: Boolean = True); override;
@@ -992,10 +1015,13 @@ type
   end;
 *)
 
+const
+  NULL_SIZE: TglBitmapPixelPosition = (Fields: []; X: 0; Y: 0);
+
 procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
 procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
+procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
 procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
-procedure glBitmapSetDefaultInternalFormat(const aInternalFormat: TglBitmapInternalFormat);
 procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
 procedure glBitmapSetDefaultWrap(
   const S: Cardinal = GL_CLAMP_TO_EDGE;
@@ -1004,37 +1030,21 @@ procedure glBitmapSetDefaultWrap(
 
 function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
 function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
+function glBitmapGetDefaultMipmap: TglBitmapMipMap;
 function glBitmapGetDefaultFormat: TglBitmapFormat;
-function glBitmapGetDefaultInternalFormat: TglBitmapInternalFormat;
 procedure glBitmapGetDefaultFilter(var aMin, aMag: Cardinal);
 procedure glBitmapGetDefaultTextureWrap(var S, T, R: Cardinal);
 
-// position / size
 function glBitmapPosition(X: Integer = -1; Y: Integer = -1): TglBitmapPixelPosition;
 
 // Formatfunctions
-function FormatGetSize(Format: TglBitmapInternalFormat): Single;
-function FormatIsCompressed(Format: TglBitmapInternalFormat): boolean;
-function FormatIsUncompressed(Format: TglBitmapInternalFormat): boolean;
-function FormatIsEmpty(Format: TglBitmapInternalFormat): boolean;
-function FormatHasAlpha(Format: TglBitmapInternalFormat): Boolean;
-procedure FormatPreparePixel(var Pixel: TglBitmapPixelData; Format: TglBitmapInternalFormat);
-function FormatGetWithoutAlpha(Format: TglBitmapInternalFormat): TglBitmapInternalFormat;
-function FormatGetWithAlpha(Format: TglBitmapInternalFormat): TglBitmapInternalFormat;
-function FormatCheckFormat(RedMask, GreenMask, BlueMask, AlphaMask: Cardinal; Format: TglBitmapInternalFormat): boolean;
-
-
-(* TODO
-function LoadTexture(Filename: String; var Texture: Cardinal{$IFDEF GLB_DELPHI}; LoadFromRes : Boolean; Instance: Cardinal = 0{$ENDIF}): Boolean;
-function LoadCubeMap(PositiveX, NegativeX, PositiveY, NegativeY, PositiveZ, NegativeZ: String; var Texture: Cardinal{$IFDEF GLB_DELPHI}; LoadFromRes : Boolean; Instance: Cardinal = 0{$ENDIF}): Boolean;
-function LoadNormalMap(Size: Integer; var Texture: Cardinal): Boolean;
-*)
+function FormatGetSize(const aFormat: TglBitmapFormat): Single;
 
 var
   glBitmapDefaultDeleteTextureOnFree: Boolean;
   glBitmapDefaultFreeDataAfterGenTextures: Boolean;
   glBitmapDefaultFormat: TglBitmapFormat;
-  glBitmapDefaultInternalFormat: TglBitmapInternalFormat;
+  glBitmapDefaultMipmap: TglBitmapMipMap;
   glBitmapDefaultFilterMin: Cardinal;
   glBitmapDefaultFilterMag: Cardinal;
   glBitmapDefaultWrapS: Cardinal;
@@ -1050,7 +1060,180 @@ implementation
 uses
   Math;
 
-(* TODO
+type
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+  TfdEmpty = class(TglBitmapFormatDescriptor)
+  public
+    class function GetFormat: TglBitmapFormat; override;
+    class function GetPixelDesc: TglBitmapPixelDesc; override;
+    class function GetFormatDesc: TglBitmapFormatDesc; override;
+
+    class procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte); override;
+    class procedure Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData); override;
+  end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+  TfdLuminance8 = class(TglBitmapFormatDescriptor)
+  public
+    class function GetFormat: TglBitmapFormat; override;
+    class function GetPixelDesc: TglBitmapPixelDesc; override;
+    class function GetFormatDesc: TglBitmapFormatDesc; override;
+    class function WithAlpha: TglBitmapFormat; override;
+
+    class procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte); override;
+    class procedure Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData); override;
+  end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+  TfdLuminance8Alpha8 = class(TglBitmapFormatDescriptor)
+    public
+      class function GetFormat: TglBitmapFormat; override;
+      class function GetPixelDesc: TglBitmapPixelDesc; override;
+      class function GetFormatDesc: TglBitmapFormatDesc; override;
+      class function WithoutAlpha: TglBitmapFormat; override;
+
+      class procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte); override;
+      class procedure Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData); override;
+    end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+  TfdRGB8 = class(TglBitmapFormatDescriptor)
+  public
+    class function GetFormat: TglBitmapFormat; override;
+    class function GetPixelDesc: TglBitmapPixelDesc; override;
+    class function GetFormatDesc: TglBitmapFormatDesc; override;
+    class function WithAlpha: TglBitmapFormat; override;
+
+    class procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte); override;
+    class procedure Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData); override;
+  end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+  TfdRGBA8 = class(TglBitmapFormatDescriptor)
+  public
+    class function GetFormat: TglBitmapFormat; override;
+    class function GetPixelDesc: TglBitmapPixelDesc; override;
+    class function GetFormatDesc: TglBitmapFormatDesc; override;
+    class function WithoutAlpha: TglBitmapFormat; override;
+
+    class procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte); override;
+    class procedure Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData); override;
+  end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+  TfdBGR8 = class(TglBitmapFormatDescriptor)
+  public
+    class function GetFormat: TglBitmapFormat; override;
+    class function GetPixelDesc: TglBitmapPixelDesc; override;
+    class function GetFormatDesc: TglBitmapFormatDesc; override;
+    class function WithAlpha: TglBitmapFormat; override;
+
+    class procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte); override;
+    class procedure Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData); override;
+  end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+  TfdBGRA8 = class(TglBitmapFormatDescriptor)
+  public
+    class function GetFormat: TglBitmapFormat; override;
+    class function GetPixelDesc: TglBitmapPixelDesc; override;
+    class function GetFormatDesc: TglBitmapFormatDesc; override;
+    class function WithoutAlpha: TglBitmapFormat; override;
+
+    class procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte); override;
+    class procedure Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData); override;
+  end;
+
+const
+  LUMINANCE_WEIGHT_R = 0.30;
+  LUMINANCE_WEIGHT_G = 0.59;
+  LUMINANCE_WEIGHT_B = 0.11;
+  UNSUPPORTED_INTERNAL_FORMAT = 'the given format isn''t supported by this function.';
+
+{$REGION Private Helper}
+/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function glBitmapPosition(X, Y: Integer): TglBitmapPixelPosition;
+begin
+  Result.Fields := [];
+
+  if X >= 0 then
+    Result.Fields := Result.Fields + [ffX];
+  if Y >= 0 then
+    Result.Fields := Result.Fields + [ffY];
+
+  Result.X := Max(0, X);
+  Result.Y := Max(0, Y);
+end;
+
+/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function FormatGetImageSize(const aSize: TglBitmapPixelPosition; const aFormat: TglBitmapFormat): Integer;
+begin
+  if (aSize.X = 0) and (aSize.Y = 0) then
+    Result := 0
+  else
+    Result := Ceil(Max(aSize.Y, 1) * Max(aSize.X, 1) * FormatGetSize(aFormat));
+end;
+
+/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function FormatGetSupportedFiles(const aFormat: TglBitmapFormat): TglBitmapFileTypes;
+begin
+  //TODO check Formats!
+  result := [];
+
+  {$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
+    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
+    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;
+
+/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function IsPowerOfTwo(aNumber: Integer): Boolean;
+begin
+  while (aNumber and 1) = 0 do
+    aNumber := aNumber shr 1;
+  result := aNumber = 1;
+end;
+
+/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function GetBitSize(aBitSet: Cardinal): Integer;
+begin
+  result := 0;
+  while aBitSet > 0 do begin
+    if (aBitSet and 1) = 1 then
+      inc(result);
+    aBitSet := aBitSet shr 1;
+  end;
+end;
+{$ENDREGION}
+
+(* GLB_NO_NATIVE_GL
 {$IFNDEF GLB_NO_NATIVE_GL}
 procedure ReadOpenGLExtensions;
 var
 {$ENDIF}
 *)
 
-/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-function glBitmapPosition(X, Y: Integer): TglBitmapPixelPosition;
+(* TODO GLB_DELPHI
+{$IFDEF GLB_DELPHI}
+function CreateGrayPalette: HPALETTE;
+var
+  Idx: Integer;
+  Pal: PLogPalette;
 begin
-  Result.Fields := [];
-
-  if X >= 0 then
-    Result.Fields := Result.Fields + [ffX];
-  if Y >= 0 then
-    Result.Fields := Result.Fields + [ffY];
-
-  Result.X := Max(0, X);
-  Result.Y := Max(0, Y);
-end;
+  GetMem(Pal, SizeOf(TLogPalette) + (SizeOf(TPaletteEntry) * 256));
 
+  Pal.palVersion := $300;
+  Pal.palNumEntries := 256;
 
-const
-  LUMINANCE_WEIGHT_R = 0.30;
-  LUMINANCE_WEIGHT_G = 0.59;
-  LUMINANCE_WEIGHT_B = 0.11;
-  UNSUPPORTED_INTERNAL_FORMAT = 'the given format isn''t supported by this function.';
+  {$IFOPT R+}
+    {$DEFINE GLB_TEMPRANGECHECK}
+    {$R-}
+  {$ENDIF}
 
-{$REGION PixelDescription}
-const
-  //ifAlpha4////////////////////////////////////////////////////////////////////////////////////////
-  PIXEL_DESC_ALPHA4: TglBitmapPixelDesc = (
-    RedRange:   $00000000; RedShift:   0;
-    GreenRange: $00000000; GreenShift: 0;
-    BlueRange:  $00000000; BlueShift:  0;
-    AlphaRange: $0000000F; AlphaShift: 0);
-
-  //ifAlpha8
-  PIXEL_DESC_ALPHA8: TglBitmapPixelDesc = (
-    RedRange:   $00000000; RedShift:   0;
-    GreenRange: $00000000; GreenShift: 0;
-    BlueRange:  $00000000; BlueShift:  0;
-    AlphaRange: $000000FF; AlphaShift: 0);
-
-  //ifAlpha12
-  PIXEL_DESC_ALPHA12: TglBitmapPixelDesc = (
-    RedRange:   $00000000; RedShift:   0;
-    GreenRange: $00000000; GreenShift: 0;
-    BlueRange:  $00000000; BlueShift:  0;
-    AlphaRange: $00000FFF; AlphaShift: 0);
-
-  //ifAlpha16
-  PIXEL_DESC_ALPHA16: TglBitmapPixelDesc = (
-    RedRange:   $00000000; RedShift:   0;
-    GreenRange: $00000000; GreenShift: 0;
-    BlueRange:  $00000000; BlueShift:  0;
-    AlphaRange: $0000FFFF; AlphaShift: 0);
-
-  //ifLuminance4////////////////////////////////////////////////////////////////////////////////////
-  PIXEL_DESC_LUMINANCE4: TglBitmapPixelDesc = (
-    RedRange:   $0000000F; RedShift:   0;
-    GreenRange: $0000000F; GreenShift: 0;
-    BlueRange:  $0000000F; BlueShift:  0;
-    AlphaRange: $00000000; AlphaShift: 0);
-
-  //ifLuminance8
-  PIXEL_DESC_LUMINANCE8: TglBitmapPixelDesc = (
-    RedRange:   $000000FF; RedShift:   0;
-    GreenRange: $000000FF; GreenShift: 0;
-    BlueRange:  $000000FF; BlueShift:  0;
-    AlphaRange: $00000000; AlphaShift: 0);
-
-  //ifLuminance12
-  PIXEL_DESC_LUMINANCE12: TglBitmapPixelDesc = (
-    RedRange:   $00000FFF; RedShift:   0;
-    GreenRange: $00000FFF; GreenShift: 0;
-    BlueRange:  $00000FFF; BlueShift:  0;
-    AlphaRange: $00000000; AlphaShift: 0);
-
-  //ifLuminance16
-  PIXEL_DESC_LUMINANCE16: TglBitmapPixelDesc = (
-    RedRange:   $0000FFFF; RedShift:   0;
-    GreenRange: $0000FFFF; GreenShift: 0;
-    BlueRange:  $0000FFFF; BlueShift:  0;
-    AlphaRange: $00000000; AlphaShift: 0);
-
-  //ifLuminance4Alpha4//////////////////////////////////////////////////////////////////////////////
-  PIXEL_DESC_LUMINANCE4_ALPHA4: TglBitmapPixelDesc = (
-    RedRange:   $0000000F; RedShift:   0;
-    GreenRange: $0000000F; GreenShift: 0;
-    BlueRange:  $0000000F; BlueShift:  0;
-    AlphaRange: $0000000F; AlphaShift: 4);
-  //ifLuminance6Alpha2
-  PIXEL_DESC_LUMINANCE6_ALPHA2: TglBitmapPixelDesc = (
-    RedRange:   $0000003F; RedShift:   0;
-    GreenRange: $0000003F; GreenShift: 0;
-    BlueRange:  $0000003F; BlueShift:  0;
-    AlphaRange: $00000003; AlphaShift: 6);
-
-  //ifLuminance8Alpha8
-  PIXEL_DESC_LUMINANCE8_ALPHA8: TglBitmapPixelDesc = (
-    RedRange:   $000000FF; RedShift:   0;
-    GreenRange: $000000FF; GreenShift: 0;
-    BlueRange:  $000000FF; BlueShift:  0;
-    AlphaRange: $000000FF; AlphaShift: 8);
-
-  //ifLuminance12Alpha4
-  PIXEL_DESC_LUMINANCE12_ALPHA4: TglBitmapPixelDesc = (
-    RedRange:   $00000FFF; RedShift:   0;
-    GreenRange: $00000FFF; GreenShift: 0;
-    BlueRange:  $00000FFF; BlueShift:  0;
-    AlphaRange: $0000000F; AlphaShift: 12);
-
-  //ifLuminance12Alpha12
-  PIXEL_DESC_LUMINANCE12_ALPHA12: TglBitmapPixelDesc = (
-    RedRange:   $00000FFF; RedShift:   0;
-    GreenRange: $00000FFF; GreenShift: 0;
-    BlueRange:  $00000FFF; BlueShift:  0;
-    AlphaRange: $00000FFF; AlphaShift: 12);
-
-  //ifLuminance16Alpha16
-  PIXEL_DESC_LUMINANCE16_ALPHA16: TglBitmapPixelDesc = (
-    RedRange:   $0000FFFF; RedShift:   0;
-    GreenRange: $0000FFFF; GreenShift: 0;
-    BlueRange:  $0000FFFF; BlueShift:  0;
-    AlphaRange: $0000FFFF; AlphaShift: 16);
-
-  //ifR3G3B2////////////////////////////////////////////////////////////////////////////////////////
-  PIXEL_DESC_R3_G3_B2: TglBitmapPixelDesc = (
-    RedRange:   $00000007; RedShift:   0;
-    GreenRange: $00000007; GreenShift: 3;
-    BlueRange:  $00000003; BlueShift:  6;
-    AlphaRange: $00000000; AlphaShift: 0);
-
-  //ifRGB4
-  PIXEL_DESC_RGB4: TglBitmapPixelDesc = (
-    RedRange:   $0000000F; RedShift:   0;
-    GreenRange: $0000000F; GreenShift: 4;
-    BlueRange:  $0000000F; BlueShift:  8;
-    AlphaRange: $00000000; AlphaShift: 0);
-
-  //ifRGB5
-  PIXEL_DESC_RGB5: TglBitmapPixelDesc = (
-    RedRange:   $0000001F; RedShift:   0;
-    GreenRange: $0000001F; GreenShift: 5;
-    BlueRange:  $0000001F; BlueShift:  10;
-    AlphaRange: $00000000; AlphaShift: 0);
-
-  //ifRGB8
-  PIXEL_DESC_RGB8: TglBitmapPixelDesc = (
-    RedRange:   $000000FF; RedShift:   0;
-    GreenRange: $000000FF; GreenShift: 8;
-    BlueRange:  $000000FF; BlueShift:  16;
-    AlphaRange: $00000000; AlphaShift: 0);
-
-  //ifRGB10
-  PIXEL_DESC_RGB10: TglBitmapPixelDesc = (
-    RedRange:   $000003FF; RedShift:   0;
-    GreenRange: $000003FF; GreenShift: 10;
-    BlueRange:  $000003FF; BlueShift:  20;
-    AlphaRange: $000003FF; AlphaShift: 0);
-
-  //ifRGB12
-  PIXEL_DESC_RGB12: TglBitmapPixelDesc = (
-    RedRange:   $00000000; RedShift:   0;
-    GreenRange: $00000000; GreenShift: 0;
-    BlueRange:  $00000000; BlueShift:  0;
-    AlphaRange: $00000000; AlphaShift: 0);
-
-  //ifRGB16
-  PIXEL_DESC_RGB16: TglBitmapPixelDesc = (
-    RedRange:   $0000FFFF; RedShift:   0;
-    GreenRange: $0000FFFF; GreenShift: 16;
-    BlueRange:  $0000FFFF; BlueShift:  32;
-    AlphaRange: $0000FFFF; AlphaShift: 0);
-
-  //ifRGBA2/////////////////////////////////////////////////////////////////////////////////////////
-  PIXEL_DESC_RGBA2: TglBitmapPixelDesc = (
-    RedRange:   $00000003; RedShift:   0;
-    GreenRange: $00000003; GreenShift: 2;
-    BlueRange:  $00000003; BlueShift:  4;
-    AlphaRange: $00000003; AlphaShift: 6);
-
-  //ifRGBA4
-  PIXEL_DESC_RGBA4: TglBitmapPixelDesc = (
-    RedRange:   $0000000F; RedShift:   0;
-    GreenRange: $0000000F; GreenShift: 4;
-    BlueRange:  $0000000F; BlueShift:  8;
-    AlphaRange: $0000000F; AlphaShift: 12);
-
-  //ifRGB5A1
-  PIXEL_DESC_RGB5_A1: TglBitmapPixelDesc = (
-    RedRange:   $0000001F; RedShift:   0;
-    GreenRange: $0000001F; GreenShift: 5;
-    BlueRange:  $0000001F; BlueShift:  10;
-    AlphaRange: $00000001; AlphaShift: 11);
-
-  //ifRGBA8
-  PIXEL_DESC_RGBA8: TglBitmapPixelDesc = (
-    RedRange:   $000000FF; RedShift:   0;
-    GreenRange: $000000FF; GreenShift: 8;
-    BlueRange:  $000000FF; BlueShift:  16;
-    AlphaRange: $000000FF; AlphaShift: 24);
-
-  //ifRGB10A2
-  PIXEL_DESC_RGB10_A2: TglBitmapPixelDesc = (
-    RedRange:   $000003FF; RedShift:   0;
-    GreenRange: $000003FF; GreenShift: 10;
-    BlueRange:  $000003FF; BlueShift:  20;
-    AlphaRange: $00000003; AlphaShift: 22);
-
-  //ifRGBA12
-  PIXEL_DESC_RGBA12: TglBitmapPixelDesc = (
-    RedRange:   $00000FFF; RedShift:   0;
-    GreenRange: $00000FFF; GreenShift: 12;
-    BlueRange:  $00000FFF; BlueShift:  24;
-    AlphaRange: $00000FFF; AlphaShift: 36);
-
-  //ifRGBA16
-  PIXEL_DESC_RGBA16: TglBitmapPixelDesc = (
-    RedRange:   $0000FFFF; RedShift:   0;
-    GreenRange: $0000FFFF; GreenShift: 16;
-    BlueRange:  $0000FFFF; BlueShift:  32;
-    AlphaRange: $0000FFFF; AlphaShift: 48);
-
-  //ifDepthComponent16//////////////////////////////////////////////////////////////////////////////
-  PIXEL_DESC_DEPTH16: TglBitmapPixelDesc = (
-    RedRange:   $0000FFFF; RedShift:   0;
-    GreenRange: $0000FFFF; GreenShift: 0;
-    BlueRange:  $0000FFFF; BlueShift:  0;
-    AlphaRange: $0000FFFF; AlphaShift: 0);
-
-  //ifDepthComponent24
-  PIXEL_DESC_DEPTH24: TglBitmapPixelDesc = (
-    RedRange:   $00FFFFFF; RedShift:   0;
-    GreenRange: $00FFFFFF; GreenShift: 0;
-    BlueRange:  $00FFFFFF; BlueShift:  0;
-    AlphaRange: $00FFFFFF; AlphaShift: 0);
-
-  //ifDepthComponent32
-  PIXEL_DESC_DEPTH32: TglBitmapPixelDesc = (
-    RedRange:   $FFFFFFFF; RedShift:   0;
-    GreenRange: $FFFFFFFF; GreenShift: 0;
-    BlueRange:  $FFFFFFFF; BlueShift:  0;
-    AlphaRange: $00000000; AlphaShift: 0);
-{$ENDREGION}
+  for Idx := 0 to 256 - 1 do begin
+    Pal.palPalEntry[Idx].peRed   := Idx;
+    Pal.palPalEntry[Idx].peGreen := Idx;
+    Pal.palPalEntry[Idx].peBlue  := Idx;
+    Pal.palPalEntry[Idx].peFlags := 0;
+  end;
 
-{$REGION MapFunctions}
-//ALPHA/////////////////////////////////////////////////////////////////////////////////////////////
-procedure MapAlpha4(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
-begin
-  //TODO
-end;
+  {$IFDEF GLB_TEMPRANGECHECK}
+    {$UNDEF GLB_TEMPRANGECHECK}
+    {$R+}
+  {$ENDIF}
 
-procedure MapAlpha8(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
-begin
-  aData^ := aPixel.Alpha;
-  inc(aData);
-end;
+  Result := CreatePalette(Pal^);
 
-procedure MapAlpha12(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
-begin
-  //TODO
+  FreeMem(Pal);
 end;
+{$ENDIF}
+*)
 
-procedure MapAlpha16(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+(* TODO GLB_SDL_IMAGE
+{$IFDEF GLB_SDL_IMAGE}
+function glBitmapRWseek(context: PSDL_RWops; offset: Integer; whence: Integer): Integer; cdecl;
 begin
-  PWord(aData)^ := aPixel.Alpha;
-  inc(aData, 2);
+  Result := TStream(context^.unknown.data1).Seek(offset, whence);
 end;
 
-//LUMINANCE/////////////////////////////////////////////////////////////////////////////////////////
-procedure MapLuminance4(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+function glBitmapRWread(context: PSDL_RWops; Ptr: Pointer; size: Integer; maxnum : Integer): Integer; cdecl;
 begin
-  //TODO
+  Result := TStream(context^.unknown.data1).Read(Ptr^, size * maxnum);
 end;
 
-procedure MapLuminance8(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+function glBitmapRWwrite(context: PSDL_RWops; Ptr: Pointer; size: Integer; num: Integer): Integer; cdecl;
 begin
-  aData^ := Trunc(
-    aPixel.Red   * LUMINANCE_WEIGHT_R +
-    aPixel.Green * LUMINANCE_WEIGHT_G +
-    aPixel.Blue  * LUMINANCE_WEIGHT_B);
-  inc(aData);
+  Result := TStream(context^.unknown.data1).Write(Ptr^, size * num);
 end;
 
-procedure MapLuminance12(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+function glBitmapRWclose(context: PSDL_RWops): Integer; cdecl;
 begin
-  //TODO
+  Result := 0;
 end;
 
-procedure MapLuminance16(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+function glBitmapCreateRWops(Stream: TStream): PSDL_RWops;
 begin
-  PWord(aData)^ := Trunc(
-    aPixel.Red   * LUMINANCE_WEIGHT_R +
-    aPixel.Green * LUMINANCE_WEIGHT_G +
-    aPixel.Blue  * LUMINANCE_WEIGHT_B);
-  inc(aData, 2);
-end;
+  Result := SDL_AllocRW;
 
-//LUMINANCE_ALPHA///////////////////////////////////////////////////////////////////////////////////
-procedure MapLuminance4Alpha4(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
-begin
-  aData^ :=
-    ((Trunc(aPixel.Red   * LUMINANCE_WEIGHT_R +
-            aPixel.Green * LUMINANCE_WEIGHT_G +
-            aPixel.Blue  * LUMINANCE_WEIGHT_B) and
-      aPixel.PixelDesc.RedRange) shl aPixel.PixelDesc.RedShift) or
-    ((aPixel.Alpha and aPixel.PixelDesc.AlphaRange) shl aPixel.PixelDesc.AlphaShift;
-  inc(aData);
-end;
+  if Result = nil then
+    raise EglBitmapException.Create('glBitmapCreateRWops - SDL_AllocRW failed.');
 
-procedure MapLuminance6Alpha2(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
-begin
-  MapLuminance4Alpha4(aPixel, aData, aBitOffset);
+  Result^.seek := glBitmapRWseek;
+  Result^.read := glBitmapRWread;
+  Result^.write := glBitmapRWwrite;
+  Result^.close := glBitmapRWclose;
+  Result^.unknown.data1 := Stream;
 end;
+{$ENDIF}
+*)
 
-procedure MapLuminance8Alpha8(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+(* TODO LoadFuncs
+function LoadTexture(Filename: String; var Texture: Cardinal{$IFDEF GLB_DELPHI}; LoadFromRes : Boolean; Instance: Cardinal{$ENDIF}): Boolean;
+var
+  glBitmap: TglBitmap2D;
 begin
-  PWord(aData)^ :=
-    ((Trunc(aPixel.Red   * LUMINANCE_WEIGHT_R +
-            aPixel.Green * LUMINANCE_WEIGHT_G +
-            aPixel.Blue  * LUMINANCE_WEIGHT_B) and
-      aPixel.PixelDesc.RedRange) shl aPixel.PixelDesc.RedShift) or
-    ((aPixel.Alpha and aPixel.PixelDesc.AlphaRange) shl aPixel.PixelDesc.AlphaShift;
-  inc(aData, 2);
-end;
+  Result := false;
+  Texture := 0;
 
-procedure MapLuminance12Alpha4(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
-begin
-  MapLuminance8Alpha8(aPixel, aData, aBitOffset);
-end;
+  {$IFDEF GLB_DELPHI}
+  if Instance = 0 then
+    Instance := HInstance;
 
-procedure MapLuminance12Alpha12(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
-begin
-  //TODO
-end;
+  if (LoadFromRes) then
+    glBitmap := TglBitmap2D.CreateFromResourceName(Instance, FileName)
+  else
+  {$ENDIF}
+    glBitmap := TglBitmap2D.Create(FileName);
 
-procedure MapLuminance16Alpha16(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
-begin
-  PCardinal(aData)^ :=
-    ((Trunc(aPixel.Red   * LUMINANCE_WEIGHT_R +
-            aPixel.Green * LUMINANCE_WEIGHT_G +
-            aPixel.Blue  * LUMINANCE_WEIGHT_B) and
-      aPixel.PixelDesc.RedRange) shl aPixel.PixelDesc.RedShift) or
-    ((aPixel.Alpha and aPixel.PixelDesc.AlphaRange) shl aPixel.PixelDesc.AlphaShift;
-  inc(aData, 4);
+  try
+    glBitmap.DeleteTextureOnFree := False;
+    glBitmap.FreeDataAfterGenTexture := False;
+    glBitmap.GenTexture(True);
+    if (glBitmap.ID > 0) then begin
+      Texture := glBitmap.ID;
+      Result := True;
+    end;
+  finally
+    glBitmap.Free;
+  end;
 end;
 
-//RGB///////////////////////////////////////////////////////////////////////////////////////////////
-procedure MapR3G3B2(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+function LoadCubeMap(PositiveX, NegativeX, PositiveY, NegativeY, PositiveZ, NegativeZ: String; var Texture: Cardinal{$IFDEF GLB_DELPHI}; LoadFromRes : Boolean; Instance: Cardinal{$ENDIF}): Boolean;
+var
+  CM: TglBitmapCubeMap;
 begin
-  aData^ :=
-    ((aPixel.Red   and aPixel.PixelDesc.RedRange)   shl aPixel.PixelDesc.RedShift)   or
-    ((aPixel.Green and aPixel.PixelDesc.GreenRange) shl aPixel.PixelDesc.GreenShift) or
-    ((aPixel.Blue  and aPixel.PixelDesc.BlueRange)  shl aPixel.PixelDesc.BlueShift);
-  inc(aData);
-end;
+  Texture := 0;
 
-procedure MapRGB4(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
-begin
-  //TODO
-end;
+  {$IFDEF GLB_DELPHI}
+  if Instance = 0 then
+    Instance := HInstance;
+  {$ENDIF}
 
-procedure MapRGB5(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
-begin
-  //TODO
-end;
+  CM := TglBitmapCubeMap.Create;
+  try
+    CM.DeleteTextureOnFree := False;
 
-procedure MapRGB8(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
-begin
-  PCardinal(aData)^ :=
-    ((aPixel.Red   and aPixel.PixelDesc.RedRange)   shl aPixel.PixelDesc.RedShift)   or
-    ((aPixel.Green and aPixel.PixelDesc.GreenRange) shl aPixel.PixelDesc.GreenShift) or
-    ((aPixel.Blue  and aPixel.PixelDesc.BlueRange)  shl aPixel.PixelDesc.BlueShift);
-  inc(aData, 3);
-end;
+    // Maps
+    {$IFDEF GLB_DELPHI}
+    if (LoadFromRes) then
+      CM.LoadFromResource(Instance, PositiveX)
+    else
+    {$ENDIF}
+      CM.LoadFromFile(PositiveX);
+    CM.GenerateCubeMap(GL_TEXTURE_CUBE_MAP_POSITIVE_X);
 
-procedure MapRGB10(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
-begin
-  //TODO
+    {$IFDEF GLB_DELPHI}
+    if (LoadFromRes) then
+      CM.LoadFromResource(Instance, NegativeX)
+    else
+    {$ENDIF}
+      CM.LoadFromFile(NegativeX);
+    CM.GenerateCubeMap(GL_TEXTURE_CUBE_MAP_NEGATIVE_X);
+
+    {$IFDEF GLB_DELPHI}
+    if (LoadFromRes) then
+      CM.LoadFromResource(Instance, PositiveY)
+    else
+    {$ENDIF}
+      CM.LoadFromFile(PositiveY);
+    CM.GenerateCubeMap(GL_TEXTURE_CUBE_MAP_POSITIVE_Y);
+
+    {$IFDEF GLB_DELPHI}
+    if (LoadFromRes) then
+      CM.LoadFromResource(Instance, NegativeY)
+    else
+    {$ENDIF}
+      CM.LoadFromFile(NegativeY);
+    CM.GenerateCubeMap(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y);
+
+    {$IFDEF GLB_DELPHI}
+    if (LoadFromRes) then
+      CM.LoadFromResource(Instance, PositiveZ)
+    else
+    {$ENDIF}
+      CM.LoadFromFile(PositiveZ);
+    CM.GenerateCubeMap(GL_TEXTURE_CUBE_MAP_POSITIVE_Z);
+
+    {$IFDEF GLB_DELPHI}
+    if (LoadFromRes) then
+      CM.LoadFromResource(Instance, NegativeZ)
+    else
+    {$ENDIF}
+      CM.LoadFromFile(NegativeZ);
+    CM.GenerateCubeMap(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z);
+
+    Texture := CM.ID;
+    Result := True;
+  finally
+    CM.Free;
+  end;
 end;
 
-procedure MapRGB12(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+function LoadNormalMap(Size: Integer; var Texture: Cardinal): Boolean;
+var
+  NM: TglBitmapNormalMap;
 begin
-  //TODO
+  Texture := 0;
+
+  NM := TglBitmapNormalMap.Create;
+  try
+    NM.DeleteTextureOnFree := False;
+    NM.GenerateNormalMap(Size);
+
+    Texture := NM.ID;
+    Result := True;
+  finally
+    NM.Free;
+  end;
 end;
+*)
 
-procedure MapRGB16(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+{$REGION default Setter and Gettter}
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure glBitmapSetDefaultDeleteTextureOnFree(const aDeleteTextureOnFree: Boolean);
 begin
-  PWord(aData)^ := aPixel.Red;
-  inc(aData, 2);
-  PWord(aData)^ := aPixel.Green;
-  inc(aData, 2);
-  PWord(aData)^ := aPixel.Blue;
-  inc(aData, 2);
+  glBitmapDefaultDeleteTextureOnFree := aDeleteTextureOnFree;
 end;
 
-//RGBA//////////////////////////////////////////////////////////////////////////////////////////////
-procedure MapRGBA2(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure glBitmapSetDefaultFreeDataAfterGenTexture(const aFreeData: Boolean);
 begin
-  aData^ :=
-    ((aPixel.Red   and aPixel.PixelDesc.RedRange)   shl aPixel.PixelDesc.RedShift)   or
-    ((aPixel.Green and aPixel.PixelDesc.GreenRange) shl aPixel.PixelDesc.GreenShift) or
-    ((aPixel.Blue  and aPixel.PixelDesc.BlueRange)  shl aPixel.PixelDesc.BlueShift)  or
-    ((aPixel.Alpha and aPixel.PixelDesc.AlphaRange) shl aPixel.PixelDesc.AlphaShift);
-  inc(aData);
+  glBitmapDefaultFreeDataAfterGenTextures := aFreeData;
 end;
 
-procedure MapRGBA4(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure glBitmapSetDefaultMipmap(const aValue: TglBitmapMipMap);
 begin
-  PWord(aData)^ :=
-    ((aPixel.Red   and aPixel.PixelDesc.RedRange)   shl aPixel.PixelDesc.RedShift)   or
-    ((aPixel.Green and aPixel.PixelDesc.GreenRange) shl aPixel.PixelDesc.GreenShift) or
-    ((aPixel.Blue  and aPixel.PixelDesc.BlueRange)  shl aPixel.PixelDesc.BlueShift)  or
-    ((aPixel.Alpha and aPixel.PixelDesc.AlphaRange) shl aPixel.PixelDesc.AlphaShift);
-  inc(aData, 2);
+  glBitmapDefaultMipmap := aValue;
 end;
 
-procedure MapRGB5A1(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure glBitmapSetDefaultFormat(const aFormat: TglBitmapFormat);
 begin
-  PWord(aData)^ :=
-    ((aPixel.Red   and aPixel.PixelDesc.RedRange)   shl aPixel.PixelDesc.RedShift)   or
-    ((aPixel.Green and aPixel.PixelDesc.GreenRange) shl aPixel.PixelDesc.GreenShift) or
-    ((aPixel.Blue  and aPixel.PixelDesc.BlueRange)  shl aPixel.PixelDesc.BlueShift)  or
-    ((aPixel.Alpha and aPixel.PixelDesc.AlphaRange) shl aPixel.PixelDesc.AlphaShift);
-  inc(aData, 2);
+  glBitmapDefaultFormat := aFormat;
 end;
 
-procedure MapRGBA8(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure glBitmapSetDefaultFilter(const aMin, aMag: Integer);
 begin
-  PCardinal(aData)^ :=
-    ((aPixel.Red   and aPixel.PixelDesc.RedRange)   shl aPixel.PixelDesc.RedShift)   or
-    ((aPixel.Green and aPixel.PixelDesc.GreenRange) shl aPixel.PixelDesc.GreenShift) or
-    ((aPixel.Blue  and aPixel.PixelDesc.BlueRange)  shl aPixel.PixelDesc.BlueShift)  or
-    ((aPixel.Alpha and aPixel.PixelDesc.AlphaRange) shl aPixel.PixelDesc.AlphaShift);
-  inc(aData, 4);
+  glBitmapDefaultFilterMin := aMin;
+  glBitmapDefaultFilterMag := aMag;
 end;
 
-procedure MapRGB10A2(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure glBitmapSetDefaultWrap(const S: Cardinal = GL_CLAMP_TO_EDGE; const T: Cardinal = GL_CLAMP_TO_EDGE; const R: Cardinal = GL_CLAMP_TO_EDGE);
 begin
-  PCardinal(aData)^ :=
-    ((aPixel.Red   and aPixel.PixelDesc.RedRange)   shl aPixel.PixelDesc.RedShift)   or
-    ((aPixel.Green and aPixel.PixelDesc.GreenRange) shl aPixel.PixelDesc.GreenShift) or
-    ((aPixel.Blue  and aPixel.PixelDesc.BlueRange)  shl aPixel.PixelDesc.BlueShift)  or
-    ((aPixel.Alpha and aPixel.PixelDesc.AlphaRange) shl aPixel.PixelDesc.AlphaShift);
-  inc(aData, 4);
+  glBitmapDefaultWrapS := S;
+  glBitmapDefaultWrapT := T;
+  glBitmapDefaultWrapR := R;
 end;
 
-procedure MapRGBA12(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
 begin
-  //TODO
+  result := glBitmapDefaultDeleteTextureOnFree;
 end;
 
-procedure MapRGBA16(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
 begin
-  PWord(aData)^ := aPixel.Red;
-  inc(aData, 2);
-  PWord(aData)^ := aPixel.Green;
-  inc(aData, 2);
-  PWord(aData)^ := aPixel.Blue;
-  inc(aData, 2);
-  PWord(aData)^ := aPixel.Alpha;
-  inc(aData, 2);
+  result := glBitmapDefaultFreeDataAfterGenTextures;
 end;
 
-//DEPTH/////////////////////////////////////////////////////////////////////////////////////////////
-procedure MapDepth16(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function glBitmapGetDefaultMipmap: TglBitmapMipMap;
 begin
-  PWord(aData)^ := (aPixel.Red + aPixel.Green + aPixel.Blue) div 3;
-  inc(aData, 2);
+  result := glBitmapDefaultMipmap;
 end;
 
-procedure MapDepth24(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function glBitmapGetDefaultFormat: TglBitmapFormat;
 begin
-  //TODO
+  result := glBitmapDefaultFormat;
 end;
 
-procedure MapDepth32(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure glBitmapGetDefaultFilter(var aMin, aMag: Cardinal);
 begin
-  PCardinal(aData)^ := (aPixel.Red + aPixel.Green + aPixel.Blue) div 3;
-  inc(aData, 4);
+  aMin := glBitmapDefaultFilterMin;
+  aMag := glBitmapDefaultFilterMag;
 end;
 
-function FormatGetMapFunc(const aInternalFormat: TglBitmapInternalFormat): TglBitmapMapFunc;
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure glBitmapGetDefaultTextureWrap(var S, T, R: Cardinal);
 begin
-  case aInternalFormat of
-    ifAlpha4:  result := MapAlpha4;
-    ifAlpha8:  result := MapAlpha8;
-    ifAlpha12: result := MapAlpha12;
-    ifAlpha16: result := MapAlpha16;
-
-    ifLuminance4:  result := MapLuminance4;
-    ifLuminance8:  result := MapLuminance8;
-    ifLuminance12: result := MapLuminance8;
-    ifLuminance16: result := MapLuminance8;
-
-    ifLuminance4Alpha4:   result := MapLuminance4Alpha4;
-    ifLuminance6Alpha2:   result := MapLuminance6Alpha2;
-    ifLuminance8Alpha8:   result := MapLuminance8Alpha8;
-    ifLuminance12Alpha4:  result := MapLuminance12Alpha4;
-    ifLuminance12Alpha12: result := MapLuminance12Alpha12;
-    ifLuminance16Alpha16: result := MapLuminance16Alpha16;
-
-    ifR3G3B2: result := MapR3G3B2;
-    ifRGB4:   result := MapRGB4;
-    ifRGB5:   result := MapRGB5;
-    ifRGB8:   result := MapRGB8;
-    ifRGB10:  result := MapRGB10;
-    ifRGB12:  result := MapRGB12;
-    ifRGB16:  result := MapRGB16;
-
-    ifRGBA2:   result := MapRGBA2;
-    ifRGBA4:   result := MapRGBA4;
-    ifRGB5A1:  result := MapRGB5A1;
-    ifRGBA8:   result := MapRGBA8;
-    ifRGB10A2: result := MapRGB10A2;
-    ifRGBA12:  result := MapRGBA12;
-    ifRGBA16:  result := MapRGBA16;
-
-    ifDepth16: result := MapDepth16;
-    ifDepth24: result := MapDepth24;
-    ifDepth32: result := MapDepth32;
-  else
-    raise EglBitmapUnsupportedInternalFormat.Create('FormatGetMapFunc - ' + UNSUPPORTED_INTERNAL_FORMAT);
-  end;
+  S := glBitmapDefaultWrapS;
+  T := glBitmapDefaultWrapT;
+  R := glBitmapDefaultWrapR;
 end;
 {$ENDREGION}
 
-{$REGION UnmapFunctions}
-//ALPHA/////////////////////////////////////////////////////////////////////////////////////////////
-procedure UnmapAlpha4(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+{$REGION TglBitmapFormatDescriptor}
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//TglBitmapFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class function TglBitmapFormatDescriptor.WithoutAlpha: TglBitmapFormat;
 begin
-  //TODO
+  if not HasAlpha then
+    result := GetFormat
+  else
+    result := tfEmpty;
 end;
 
-procedure UnmapAlpha8(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class function TglBitmapFormatDescriptor.WithAlpha: TglBitmapFormat;
 begin
+  if HasAlpha then
+    result := GetFormat
+  else
+    result := tfEmpty;
+end;
 
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class function TglBitmapFormatDescriptor.IsEmpty: Boolean;
+begin
+  result := (GetFormat = tfEmpty);
 end;
 
-procedure UnmapAlpha12(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class function TglBitmapFormatDescriptor.HasAlpha: Boolean;
 begin
-  //TODO
+  result := (GetPixelDesc.AlphaRange > 0);
 end;
 
-procedure UnmapAlpha16(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class function TglBitmapFormatDescriptor.MaskMatch(const aRedMask, aGreenMask, aBlueMask, aAlphaMask: UInt64): Boolean;
+var
+  PixelDesc: TglBitmapPixelDesc;
 begin
+  result := False;
+
+  if (aRedMask = 0) and (aGreenMask = 0) and (aBlueMask = 0) and (aAlphaMask = 0) then
+    raise EglBitmapException.Create('FormatCheckFormat - All Masks are 0');
 
+  PixelDesc := GetPixelDesc;
+  with PixelDesc do begin
+    if (aRedMask   <> 0) and (aRedMask   <> (RedRange   shl RedShift)) then
+      exit;
+    if (aGreenMask <> 0) and (aGreenMask <> (GreenRange shl GreenShift)) then
+      exit;
+    if (aBlueMask  <> 0) and (aBlueMask  <> (BlueRange  shl BlueShift)) then
+      exit;
+    if (aAlphaMask <> 0) and (aAlphaMask <> (AlphaRange shl AlphaShift)) then
+      exit;
+  end;
+  result := True;
 end;
 
-//LUMINANCE/////////////////////////////////////////////////////////////////////////////////////////
-procedure UnmapLuminance4(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class procedure TglBitmapFormatDescriptor.PreparePixel(var aPixel: TglBitmapPixelData);
 begin
-  //TODO
+  FillChar(aPixel, SizeOf(aPixel), 0);
+  with GetPixelDesc do begin
+    aPixel.Red   := RedRange;
+    aPixel.Green := GreenRange;
+    aPixel.Blue  := BlueRange;
+    aPixel.Alpha := AlphaRange;
+  end;
 end;
+{$ENDREGION}
 
-procedure UnmapLuminance8(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+{$REGION TfdEmpty}
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//TfdEmpty////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class function TfdEmpty.GetFormat: TglBitmapFormat;
 begin
-
+  result := tfEmpty;
 end;
 
-procedure UnmapLuminance12(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class function TfdEmpty.GetPixelDesc: TglBitmapPixelDesc;
 begin
-  //TODO
+  with result do begin
+    RedRange   := $00000000; RedShift   := 0;
+    GreenRange := $00000000; GreenShift := 0;
+    BlueRange  := $00000000; BlueShift  := 0;
+    AlphaRange := $00000000; AlphaShift := 0;
+  end;
 end;
 
-procedure UnmapLuminance16(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class function TfdEmpty.GetFormatDesc: TglBitmapFormatDesc;
 begin
-
+  with result do begin
+    Format         := 0;
+    InternalFormat := 0;
+    DataType       := 0;
+  end;
 end;
 
-//LUMINANCE_ALPHA///////////////////////////////////////////////////////////////////////////////////
-procedure UnmapLuminance4Alpha4(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class procedure TfdEmpty.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
 begin
-
+  raise EglBitmapException.Create('format does not support mapping');
 end;
 
-procedure UnmapLuminance6Alpha2(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class procedure TfdEmpty.Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData);
 begin
-
+  raise EglBitmapException.Create('format does not support unmapping');
 end;
+{$ENDREGION}
 
-procedure UnmapLuminance8Alpha8(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+{$REGION TfdLuminance8}
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//TfdLuminance8///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class function TfdLuminance8.GetFormat: TglBitmapFormat;
 begin
-
+  result := tfEmpty;
 end;
 
-procedure UnmapLuminance12Alpha4(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class function TfdLuminance8.GetPixelDesc: TglBitmapPixelDesc;
 begin
-
+  with result do begin
+    RedRange   := $000000FF; RedShift   := 0;
+    GreenRange := $000000FF; GreenShift := 0;
+    BlueRange  := $000000FF; BlueShift  := 0;
+    AlphaRange := $00000000; AlphaShift := 0;
+  end;
 end;
 
-procedure UnmapLuminance12Alpha12(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class function TfdLuminance8.GetFormatDesc: TglBitmapFormatDesc;
 begin
-  //TODO
+  with result do begin
+    Format         := GL_LUMINANCE;
+    InternalFormat := GL_LUMINANCE8;
+    DataType       := GL_UNSIGNED_BYTE;
+  end;
 end;
 
-procedure UnmapLuminance16Alpha16(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class function TfdLuminance8.WithAlpha: TglBitmapFormat;
 begin
-
+  result := tfLuminance8Alpha8;
 end;
 
-//RGB///////////////////////////////////////////////////////////////////////////////////////////////
-procedure UnmapR3G3B2(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class procedure TfdLuminance8.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
 begin
-
+  aData^ := Trunc(
+    aPixel.Red   * LUMINANCE_WEIGHT_R +
+    aPixel.Green * LUMINANCE_WEIGHT_G +
+    aPixel.Blue  * LUMINANCE_WEIGHT_B);
+  inc(aData);
 end;
 
-procedure UnmapRGB4(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class procedure TfdLuminance8.Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData);
 begin
-  //TODO
+  aPixel.Red   := aData^;
+  aPixel.Green := aData^;
+  aPixel.Blue  := aData^;
+  aPixel.Alpha := 0;
+  inc(aData);
 end;
+{$ENDREGION}
 
-procedure UnmapRGB5(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+{$REGION TfdLuminance8Alpha8}
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//TfdLuminance8Alpha8/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class function TfdLuminance8Alpha8.GetFormat: TglBitmapFormat;
 begin
-  //TODO
+  result := tfLuminance8Alpha8;
 end;
 
-procedure UnmapRGB8(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class function TfdLuminance8Alpha8.GetPixelDesc: TglBitmapPixelDesc;
 begin
-
+  with result do begin
+    RedRange   := $000000FF; RedShift   := 0;
+    GreenRange := $000000FF; GreenShift := 0;
+    BlueRange  := $000000FF; BlueShift  := 0;
+    AlphaRange := $000000FF; AlphaShift := 8;
+  end;
 end;
 
-procedure UnmapRGB10(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class function TfdLuminance8Alpha8.GetFormatDesc: TglBitmapFormatDesc;
 begin
-  //TODO
+  with result do begin
+    Format         := GL_LUMINANCE_ALPHA;
+    InternalFormat := GL_LUMINANCE8_ALPHA8;
+    DataType       := GL_UNSIGNED_BYTE;
+  end;
 end;
 
-procedure UnmapRGB12(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class function TfdLuminance8Alpha8.WithoutAlpha: TglBitmapFormat;
 begin
-  //TODO
+  result := tfLuminance8;
 end;
 
-procedure UnmapRGB16(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class procedure TfdLuminance8Alpha8.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
 begin
+  aData^ := Trunc(
+    aPixel.Red   * LUMINANCE_WEIGHT_R +
+    aPixel.Green * LUMINANCE_WEIGHT_G +
+    aPixel.Blue  * LUMINANCE_WEIGHT_B);
+  inc(aData);
 
+  aData^ := aPixel.Alpha;
+  inc(aData);
 end;
 
-//RGBA//////////////////////////////////////////////////////////////////////////////////////////////
-procedure UnmapRGBA2(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class procedure TfdLuminance8Alpha8.Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData);
 begin
+  aPixel.Red   := aData^;
+  aPixel.Green := aData^;
+  aPixel.Blue  := aData^;
+  inc(aData);
 
+  aPixel.Alpha := aData^;
+  inc(aData);
 end;
+{$ENDREGION}
 
-procedure UnmapRGBA4(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+{$REGION TfdRGB8}
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//TfdRGB8/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class function TfdRGB8.GetFormat: TglBitmapFormat;
 begin
-
+  result := tfRGB8;
 end;
 
-procedure UnmapRGB5A1(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class function TfdRGB8.GetPixelDesc: TglBitmapPixelDesc;
 begin
-
+  with result do begin
+    RedRange   := $000000FF; RedShift   := 0;
+    GreenRange := $000000FF; GreenShift := 8;
+    BlueRange  := $000000FF; BlueShift  := 16;
+    AlphaRange := $00000000; AlphaShift := 0;
+  end;
 end;
 
-procedure UnmapRGBA8(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class function TfdRGB8.GetFormatDesc: TglBitmapFormatDesc;
 begin
-
+  with result do begin
+    Format         := GL_LUMINANCE;
+    InternalFormat := GL_LUMINANCE8;
+    DataType       := GL_UNSIGNED_BYTE;
+  end;
 end;
 
-procedure UnmapRGB10A2(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class function TfdRGB8.WithAlpha: TglBitmapFormat;
 begin
-
+  result := tfRGBA8;
 end;
 
-procedure UnmapRGBA12(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class procedure TfdRGB8.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
 begin
-  //TODO
+  aData^ := aPixel.Red;
+  inc(aData);
+  aData^ := aPixel.Green;
+  inc(aData);
+  aData^ := aPixel.Blue;
+  inc(aData);
 end;
 
-procedure UnmapRGBA16(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class procedure TfdRGB8.Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData);
 begin
-
+  aPixel.Red := aData^;
+  inc(aData);
+  aPixel.Green := aData^;
+  inc(aData);
+  aPixel.Blue := aData^;
+  inc(aData);
+  aPixel.Alpha := 0;
 end;
+{$ENDREGION}
 
-//DEPTH/////////////////////////////////////////////////////////////////////////////////////////////
-procedure UnmapDepth16(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+{$REGION TfdRGBA8}
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//TfdRGBA8////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class function TfdRGBA8.GetFormat: TglBitmapFormat;
 begin
-
+  result := tfRGBA8;
 end;
 
-procedure UnmapDepth24(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class function TfdRGBA8.GetPixelDesc: TglBitmapPixelDesc;
 begin
-  //TODO
+  with result do begin
+    RedRange   := $000000FF; RedShift   := 0;
+    GreenRange := $000000FF; GreenShift := 8;
+    BlueRange  := $000000FF; BlueShift  := 16;
+    AlphaRange := $000000FF; AlphaShift := 24;
+  end;
 end;
 
-procedure UnmapDepth32(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+class function TfdRGBA8.GetFormatDesc: TglBitmapFormatDesc;
 begin
-
+  with result do begin
+    Format         := GL_RGB;
+    InternalFormat := GL_RGB8;
+    DataType       := GL_UNSIGNED_BYTE;
+  end;
 end;
 
-
-
-
-procedure UnMapAlpha(var pData: pByte; var Pixel: TglBitmapPixelData);
+class function TfdRGBA8.WithoutAlpha: TglBitmapFormat;
 begin
-  Pixel.Alpha := pData^;
-  Pixel.Red   := Pixel.PixelDesc.RedRange;
-  Pixel.Green := Pixel.PixelDesc.GreenRange;
-  Pixel.Blue  := Pixel.PixelDesc.BlueRange;
-
-  Inc(pData);
+  result := tfRGB8;
 end;
 
-
-procedure UnMapLuminance(var pData: pByte; var Pixel: TglBitmapPixelData);
+class procedure TfdRGBA8.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
 begin
-  Pixel.Alpha := 255;
-  Pixel.Red   := pData^;
-  Pixel.Green := pData^;
-  Pixel.Blue  := pData^;
-
-  Inc(pData);
+  aData^ := aPixel.Red;
+  inc(aData);
+  aData^ := aPixel.Green;
+  inc(aData);
+  aData^ := aPixel.Blue;
+  inc(aData);
+  aData^ := aPixel.Alpha;
+  inc(aData);
 end;
 
-
-procedure UnMapDepth8(var pData: pByte; var Pixel: TglBitmapPixelData);
+class procedure TfdRGBA8.Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData);
 begin
-  Pixel.Alpha := 255;
-  Pixel.Red   := pData^;
-  Pixel.Green := pData^;
-  Pixel.Blue  := pData^;
-
-  Inc(pData);
+  aPixel.Red := aData^;
+  inc(aData);
+  aPixel.Green := aData^;
+  inc(aData);
+  aPixel.Blue := aData^;
+  inc(aData);
+  aPixel.Alpha := aData^;
+  inc(aData);
 end;
+{$ENDREGION}
 
-
-procedure UnMapLuminanceAlpha(var pData: pByte; var Pixel: TglBitmapPixelData);
+{$REGION TfdBGR8}
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//TfdBGR8/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class function TfdBGR8.GetFormat: TglBitmapFormat;
 begin
-  Pixel.Red   := pData^;
-  Pixel.Green := pData^;
-  Pixel.Blue  := pData^;
-  Inc(pData);
-
-  Pixel.Alpha := pData^;
-  Inc(pData);
+  result := tfBGR8;
 end;
 
-
-procedure UnMapRGBA4(var pData: pByte; var Pixel: TglBitmapPixelData);
-var
-  Temp: Word;
+class function TfdBGR8.GetPixelDesc: TglBitmapPixelDesc;
 begin
-  Temp := pWord(pData)^;
-
-  Pixel.Alpha := Temp shr PIXEL_DESC_RGBA4.AlphaShift and PIXEL_DESC_RGBA4.AlphaRange;
-  Pixel.Red   := Temp shr PIXEL_DESC_RGBA4.RedShift   and PIXEL_DESC_RGBA4.RedRange;
-  Pixel.Green := Temp shr PIXEL_DESC_RGBA4.GreenShift and PIXEL_DESC_RGBA4.GreenRange;
-  Pixel.Blue  := Temp                                 and PIXEL_DESC_RGBA4.BlueRange;
-
-  Inc(pData, 2);
+  with result do begin
+    RedRange   := $000000FF; RedShift   := 16;
+    GreenRange := $000000FF; GreenShift := 8;
+    BlueRange  := $000000FF; BlueShift  := 0;
+    AlphaRange := $00000000; AlphaShift := 0;
+  end;
 end;
 
-
-procedure UnMapR5G6B5(var pData: pByte; var Pixel: TglBitmapPixelData);
-var
-  Temp: Word;
+class function TfdBGR8.GetFormatDesc: TglBitmapFormatDesc;
 begin
-  Temp := pWord(pData)^;
-
-  Pixel.Alpha := Pixel.PixelDesc.AlphaRange;
-  Pixel.Red   := Temp shr PIXEL_DESC_R5G6B5.RedShift   and PIXEL_DESC_R5G6B5.RedRange;
-  Pixel.Green := Temp shr PIXEL_DESC_R5G6B5.GreenShift and PIXEL_DESC_R5G6B5.GreenRange;
-  Pixel.Blue  := Temp                                  and PIXEL_DESC_R5G6B5.BlueRange;
-
-  Inc(pData, 2);
+  with result do begin
+    Format         := GL_BGR;
+    InternalFormat := GL_RGB8;
+    DataType       := GL_UNSIGNED_BYTE;
+  end;
 end;
 
-
-procedure UnMapRGB5A1(var pData: pByte; var Pixel: TglBitmapPixelData);
-var
-  Temp: Word;
+class function TfdBGR8.WithAlpha: TglBitmapFormat;
 begin
-  Temp := pWord(pData)^;
-
-  Pixel.Alpha := Temp shr PIXEL_DESC_RGB5A1.AlphaShift and PIXEL_DESC_RGB5A1.AlphaRange;
-  Pixel.Red   := Temp shr PIXEL_DESC_RGB5A1.RedShift   and PIXEL_DESC_RGB5A1.RedRange;
-  Pixel.Green := Temp shr PIXEL_DESC_RGB5A1.GreenShift and PIXEL_DESC_RGB5A1.GreenRange;
-  Pixel.Blue  := Temp                                  and PIXEL_DESC_RGB5A1.BlueRange;
-
-  Inc(pData, 2);
+  result := tfBGRA8;
 end;
 
-
-procedure UnMapRGB8(var pData: pByte; var Pixel: TglBitmapPixelData);
+class procedure TfdBGR8.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
 begin
-  Pixel.Alpha := Pixel.PixelDesc.AlphaRange;
-
-  Pixel.Red   := pData^;
-  Inc(pData);
-
-  Pixel.Green := pData^;
-  Inc(pData);
-
-  Pixel.Blue  := pData^;
-  Inc(pData);
+  aData^ := aPixel.Blue;
+  inc(aData);
+  aData^ := aPixel.Green;
+  inc(aData);
+  aData^ := aPixel.Red;
+  inc(aData);
 end;
 
-
-procedure UnMapBGR8(var pData: pByte; var Pixel: TglBitmapPixelData);
+class procedure TfdBGR8.Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData);
 begin
-  Pixel.Alpha := Pixel.PixelDesc.AlphaRange;
-
-  Pixel.Blue  := pData^;
-  Inc(pData);
-
-  Pixel.Green := pData^;
-  Inc(pData);
-
-  Pixel.Red   := pData^;
-  Inc(pData);
+  aPixel.Blue := aData^;
+  inc(aData);
+  aPixel.Green := aData^;
+  inc(aData);
+  aPixel.Red := aData^;
+  inc(aData);
 end;
+{$ENDREGION}
 
-
-procedure UnMapRGBA8(var pData: pByte; var Pixel: TglBitmapPixelData);
+{$REGION TfdBGRA8}
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//TfdBGRA8////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class function TfdBGRA8.GetFormat: TglBitmapFormat;
 begin
-  Pixel.Red   := pData^;
-  Inc(pData);
-
-  Pixel.Green := pData^;
-  Inc(pData);
-
-  Pixel.Blue  := pData^;
-  Inc(pData);
-
-  Pixel.Alpha := pData^;
-  Inc(pData);
+  result := tfBGRA8;
 end;
 
-
-procedure UnMapBGRA8(var pData: pByte; var Pixel: TglBitmapPixelData);
+class function TfdBGRA8.GetPixelDesc: TglBitmapPixelDesc;
 begin
-  Pixel.Blue  := pData^;
-  Inc(pData);
-
-  Pixel.Green := pData^;
-  Inc(pData);
+  with result do begin
+    RedRange   := $000000FF; RedShift   := 16;
+    GreenRange := $000000FF; GreenShift := 8;
+    BlueRange  := $000000FF; BlueShift  := 0;
+    AlphaRange := $000000FF; AlphaShift := 24;
+  end;
+end;
 
-  Pixel.Red   := pData^;
-  Inc(pData);
+class function TfdBGRA8.GetFormatDesc: TglBitmapFormatDesc;
+begin
+  with result do begin
+    Format         := GL_BGRA;
+    InternalFormat := GL_RGBA8;
+    DataType       := GL_UNSIGNED_BYTE;
+  end;
+end;
 
-  Pixel.Alpha := pData^;
-  Inc(pData);
+class function TfdBGRA8.WithoutAlpha: TglBitmapFormat;
+begin
+  result := tfBGR8;
 end;
 
+class procedure TfdBGRA8.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aBitOffset: Byte);
+begin
+  aData^ := aPixel.Blue;
+  inc(aData);
+  aData^ := aPixel.Green;
+  inc(aData);
+  aData^ := aPixel.Red;
+  inc(aData);
+  aData^ := aPixel.Alpha;
+  inc(aData);
+end;
 
-procedure UnMapRGB10A2(var pData: pByte; var Pixel: TglBitmapPixelData);
-var
-  Temp: DWord;
-begin
-  Temp := pDWord(pData)^;
-
-  Pixel.Alpha := Temp shr PIXEL_DESC_RGB10A2.AlphaShift and PIXEL_DESC_RGB10A2.AlphaRange;
-  Pixel.Red   := Temp shr PIXEL_DESC_RGB10A2.RedShift   and PIXEL_DESC_RGB10A2.RedRange;
-  Pixel.Green := Temp shr PIXEL_DESC_RGB10A2.GreenShift and PIXEL_DESC_RGB10A2.GreenRange;
-  Pixel.Blue  := Temp                                   and PIXEL_DESC_RGB10A2.BlueRange;
-
-  Inc(pData, 4);
-end;
-
-
-function FormatGetUnMapFunc(const aInternalFormat: TglBitmapInternalFormat): TglBitmapUnMapFunc;
-begin
-  case aInternalFormat of
-    ifAlpha4:  result := UnmapAlpha4;
-    ifAlpha8:  result := UnmapAlpha8;
-    ifAlpha12: result := UnmapAlpha12;
-    ifAlpha16: result := UnmapAlpha16;
-
-    ifLuminance4:  result := UnmapLuminance4;
-    ifLuminance8:  result := UnmapLuminance8;
-    ifLuminance12: result := UnmapLuminance8;
-    ifLuminance16: result := UnmapLuminance8;
-
-    ifLuminance4Alpha4:   result := UnmapLuminance4Alpha4;
-    ifLuminance6Alpha2:   result := UnmapLuminance6Alpha2;
-    ifLuminance8Alpha8:   result := UnmapLuminance8Alpha8;
-    ifLuminance12Alpha4:  result := UnmapLuminance12Alpha4;
-    ifLuminance12Alpha12: result := UnmapLuminance12Alpha12;
-    ifLuminance16Alpha16: result := UnmapLuminance16Alpha16;
-
-    ifR3G3B2: result := UnmapR3G3B2;
-    ifRGB4:   result := UnmapRGB4;
-    ifRGB5:   result := UnmapRGB5;
-    ifRGB8:   result := UnmapRGB8;
-    ifRGB10:  result := UnmapRGB10;
-    ifRGB12:  result := UnmapRGB12;
-    ifRGB16:  result := UnmapRGB16;
-
-    ifRGBA2:   result := UnmapRGBA2;
-    ifRGBA4:   result := UnmapRGBA4;
-    ifRGB5A1:  result := UnmapRGB5A1;
-    ifRGBA8:   result := UnmapRGBA8;
-    ifRGB10A2: result := UnmapRGB10A2;
-    ifRGBA12:  result := UnmapRGBA12;
-    ifRGBA16:  result := UnmapRGBA16;
-
-    ifDepth16: result := UnmapDepth16;
-    ifDepth24: result := UnmapDepth24;
-    ifDepth32: result := UnmapDepth32;
-  else
-    raise EglBitmapUnsupportedInternalFormat.Create('FormatGetMapFunc - ' + UNSUPPORTED_INTERNAL_FORMAT);
-  end;
+class procedure TfdBGRA8.Unmap(var aData: PByte; var aBitOffset: Byte; var aPixel: TglBitmapPixelData);
+begin
+  aPixel.Blue := aData^;
+  inc(aData);
+  aPixel.Green := aData^;
+  inc(aData);
+  aPixel.Red := aData^;
+  inc(aData);
+  aPixel.Alpha := aData^;
+  inc(aData);
 end;
 {$ENDREGION}
 
-{*
-** Tools
-*}
-function FormatGetSize (Format: TglBitmapInternalFormat): Single;
-begin
-  case Format of
-    ifEmpty:
-      Result := 0;
-    ifDXT1:
-      Result := 0.5;
-    ifAlpha, ifLuminance, ifDepth8, ifDXT3, ifDXT5:
-      Result := 1;
-    ifLuminanceAlpha, ifRGBA4, ifRGB5A1, ifR5G6B5:
-      Result := 2;
-    ifBGR8, ifRGB8:
-      Result := 3;
-    ifBGRA8, ifRGBA8, ifRGB10A2:
-      Result := 4;
-    else
-      raise EglBitmapUnsupportedInternalFormat.Create('FormatGetSize - ' + UNSUPPORTED_INTERNAL_FORMAT);
-  end;
+{$REGION TglBitmap }
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//TglBitmap///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function TglBitmap.GetHeight: Integer;
+begin
+  if (ffY in fDimension.Fields) then
+    result := fDimension.Y
+  else
+    result := -1;
 end;
 
-
-function FormatIsCompressed(Format: TglBitmapInternalFormat): boolean;
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+function TglBitmap.GetWidth: Integer;
 begin
-  Result := Format in [ifDXT1, ifDXT3, ifDXT5];
+  if (ffX in fDimension.Fields) then
+    result := fDimension.X
+  else
+    result := -1;
 end;
 
-
-function FormatIsUncompressed(Format: TglBitmapInternalFormat): boolean;
+{$REGION Setter}
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SetCustomData(const aValue: Pointer);
 begin
-  Result := Format in [ifAlpha, ifLuminance, ifDepth8, ifLuminanceAlpha, ifRGBA4, ifRGB5A1, ifR5G6B5, ifBGR8, ifRGB8, ifBGRA8, ifRGBA8, ifRGB10A2];
+  if fCustomData = aValue then
+    exit;
+  fCustomData := aValue;
 end;
 
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SetCustomName(const aValue: String);
+begin
+  if fCustomName = aValue then
+    exit;
+  fCustomName := aValue;
+end;
 
-function FormatIsEmpty(Format: TglBitmapInternalFormat): boolean;
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SetCustomNameW(const aValue: WideString);
 begin
-  Result := Format = ifEmpty;
+  if fCustomNameW = aValue then
+    exit;
+  fCustomNameW := aValue;
 end;
 
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SetDeleteTextureOnFree(const aValue: Boolean);
+begin
+  if fDeleteTextureOnFree = aValue then
+    exit;
+  fDeleteTextureOnFree := aValue;
+end;
 
-function FormatHasAlpha(Format: TglBitmapInternalFormat): Boolean;
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SetFormat(const aValue: TglBitmapFormat);
 begin
-  Result := Format in [ifDXT1, ifDXT3, ifDXT5 ,ifAlpha, ifLuminanceAlpha, ifRGBA4, ifRGB5A1, ifBGRA8, ifRGBA8, ifRGB10A2];
+  if fFormat = aValue then
+    exit;
+  fFormat := aValue;
 end;
 
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SetFreeDataAfterGenTexture(const aValue: Boolean);
+begin
+  if fFreeDataAfterGenTexture = aValue then
+    exit;
+  fFreeDataAfterGenTexture := aValue;
+end;
 
-procedure FormatPreparePixel(var Pixel: TglBitmapPixelData; Format: TglBitmapInternalFormat);
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SetID(const aValue: Cardinal);
 begin
-  FillChar(Pixel, SizeOf(Pixel), #0);
+  if fID = aValue then
+    exit;
+  fID := aValue;
+end;
 
-  case Format of
-    ifAlpha:
-      Pixel.PixelDesc := PIXEL_DESC_ALPHA;
-    ifLuminance:
-      Pixel.PixelDesc := PIXEL_DESC_LUMINANCE;
-    ifDepth8:
-      Pixel.PixelDesc := PIXEL_DESC_DEPTH8;
-    ifLuminanceAlpha:
-      Pixel.PixelDesc := PIXEL_DESC_LUMINANCEALPHA;
-    ifRGBA4:
-      Pixel.PixelDesc := PIXEL_DESC_RGBA4;
-    ifR5G6B5:
-      Pixel.PixelDesc := PIXEL_DESC_R5G6B5;
-    ifRGB5A1:
-      Pixel.PixelDesc := PIXEL_DESC_RGB5A1;
-    ifDXT1, ifDXT3, ifDXT5, ifBGRA8:
-      Pixel.PixelDesc := PIXEL_DESC_BGRA8;
-    ifBGR8:
-      Pixel.PixelDesc := PIXEL_DESC_BGR8;
-    ifRGB8:
-      Pixel.PixelDesc := PIXEL_DESC_RGB8;
-    ifRGBA8:
-      Pixel.PixelDesc := PIXEL_DESC_RGBA8;
-    ifRGB10A2:
-      Pixel.PixelDesc := PIXEL_DESC_RGB10A2;
-  end;
-
-  Pixel.Red   := Pixel.PixelDesc.RedRange;
-  Pixel.Green := Pixel.PixelDesc.GreenRange;
-  Pixel.Blue  := Pixel.PixelDesc.BlueRange;
-  Pixel.Alpha := Pixel.PixelDesc.AlphaRange;
-end;
-
-
-function FormatGetWithoutAlpha(Format: TglBitmapInternalFormat): TglBitmapInternalFormat;
-begin
-  case Format of
-    ifAlpha:
-      Result := ifLuminance;
-    ifLuminanceAlpha:
-      Result := ifLuminance;
-    ifRGBA4:
-      Result := ifR5G6B5;
-    ifRGB5A1:
-      Result := ifR5G6B5;
-    ifBGRA8:
-      Result := ifBGR8;
-    ifRGBA8:
-      Result := ifRGB8;
-    ifRGB10A2:
-      Result := ifRGB8;
-    else
-      Result := Format;
-  end;
-end;
-
-
-function FormatGetWithAlpha(Format: TglBitmapInternalFormat): TglBitmapInternalFormat;
-begin
-  case Format of
-    ifLuminance:
-      Result := ifLuminanceAlpha;
-    ifR5G6B5:
-      Result := ifRGB5A1;
-    ifBGR8:
-      Result := ifBGRA8;
-    ifRGB8:
-      Result := ifRGBA8;
-    else
-      Result := Format;
-  end;
-end;
-
-
-function FormatGetUncompressed(Format: TglBitmapInternalFormat): TglBitmapInternalFormat;
-begin
-  case Format of
-    ifDXT1:
-      Result := ifRGB5A1;
-    ifDXT3:
-      Result := ifRGBA8;
-    ifDXT5:
-      Result := ifRGBA8;
-    else
-      Result := Format;
-  end;
-end;
-
-
-function FormatGetImageSize(Size: TglBitmapPixelPosition; Format: TglBitmapInternalFormat): Integer;
-begin
-  if (Size.X = 0) and (Size.Y = 0) then
-    Result := 0
-  else
-    Result := Trunc(Max(Size.Y, 1) * Max(Size.X, 1) * FormatGetSize(Format));
-end;
-
-
-function FormatGetSupportedFiles(Format: TglBitmapInternalFormat): TglBitmapFileTypes;
-begin
-  Result := [];
-
-  {$IFDEF GLB_SUPPORT_PNG_WRITE}
-  if Format in [ifLuminance, ifAlpha, ifDepth8, ifLuminanceAlpha, ifBGR8, ifBGRA8, ifRGB8, ifRGBA8] then
-    Result := Result + [ftPNG];
-  {$ENDIF}
-
-  {$IFDEF GLB_SUPPORT_JPEG_WRITE}
-  if Format in [ifLuminance, ifAlpha, ifDepth8, ifRGB8, ifBGR8] then
-    Result := Result + [ftJPEG];
-  {$ENDIF}
-
-  Result := Result + [ftDDS];
-
-  if Format in [ifLuminance, ifAlpha, ifDepth8, ifLuminanceAlpha, ifBGR8, ifRGB8, ifBGRA8, ifRGBA8] then
-    Result := Result + [ftTGA];
-
-  if Format in [ifLuminance, ifAlpha, ifDepth8, ifLuminanceAlpha, ifRGBA4, ifRGB5A1, ifR5G6B5, ifRGB8, ifBGR8, ifRGBA8, ifBGRA8, ifRGB10A2] then
-    Result := Result + [ftBMP];
-end;
-
-
-function FormatCheckFormat(RedMask, GreenMask, BlueMask, AlphaMask: Cardinal; Format: TglBitmapInternalFormat): boolean;
-var
-  Pix: TglBitmapPixelData;
-begin
-  Result := False;
-
-  if (RedMask = 0) and (GreenMask = 0) and (BlueMask = 0) and (AlphaMask = 0) then
-    raise EglBitmapException.Create('FormatCheckFormat - All Masks are 0');
-
-  FormatPreparePixel(Pix, Format);
-
-  with Pix.PixelDesc do begin
-    if RedMask <> 0 then
-      if (RedMask <> (RedRange shl RedShift)) then
-        Exit;
-
-    if GreenMask <> 0 then
-      if (GreenMask <> (GreenRange shl GreenShift)) then
-        Exit;
-
-    if BlueMask <> 0 then
-      if (BlueMask <> (BlueRange shl BlueShift)) then
-        Exit;
-
-    if AlphaMask <> 0 then
-      if (AlphaMask <> (AlphaRange shl AlphaShift)) then
-        Exit;
-
-    Result := True;
-  end;
-end;
-
-
-function IsPowerOfTwo(Number: Integer): Boolean;
-begin
-  while Number and 1 = 0 do
-    Number := Number shr 1;
-
-  Result := Number = 1;
-end;
-
-
-function GetBitSize(BitSet: Cardinal): Integer;
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SetMipMap(const aValue: TglBitmapMipMap);
 begin
-  Result := 0;
-
-  while BitSet > 0 do begin
-    if (BitSet and $1) = 1 then
-      Inc(Result);
-
-    BitSet := BitSet shr 1;
-  end;
+  if fMipMap = aValue then
+    exit;
+  fMipMap := aValue;
 end;
 
-
-procedure SwapRGB(pData: pByte; Width: Integer; HasAlpha: Boolean);
-type
-  PRGBPix = ^TRGBPix;
-  TRGBPix = array [0..2] of byte;
-var
-  Temp: Byte;
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SetTarget(const aValue: Cardinal);
 begin
-  while Width > 0 do begin
-    Temp := pRGBPIX(pData)^[0];
-    pRGBPIX(pData)^[0] := pRGBPIX(pData)^[2];
-    pRGBPIX(pData)^[2] := Temp;
-
-    if HasAlpha then
-      Inc(pData, 4)
-    else
-      Inc(pData, 3);
-
-    Dec(Width);
-  end;
+  if fTarget = aValue then
+    exit;
+  fTarget := aValue;
 end;
 
-
-{$IFDEF GLB_DELPHI}
-function CreateGrayPalette: HPALETTE;
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SetAnisotropic(const aValue: Integer);
 var
-  Idx: Integer;
-  Pal: PLogPalette;
+  MaxAnisotropic: Integer;
 begin
-  GetMem(Pal, SizeOf(TLogPalette) + (SizeOf(TPaletteEntry) * 256));
-
-  Pal.palVersion := $300;
-  Pal.palNumEntries := 256;
-
-  {$IFOPT R+}
-    {$DEFINE GLB_TEMPRANGECHECK}
-    {$R-}
-  {$ENDIF}
-
-  for Idx := 0 to 256 - 1 do begin
-    Pal.palPalEntry[Idx].peRed   := Idx;
-    Pal.palPalEntry[Idx].peGreen := Idx;
-    Pal.palPalEntry[Idx].peBlue  := Idx;
-    Pal.palPalEntry[Idx].peFlags := 0;
+  fAnisotropic := Value;
+  if (ID > 0) then begin
+    if GL_EXT_texture_filter_anisotropic then begin
+      if fAnisotropic > 0 then begin
+        Bind(False);
+        glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, @MaxAnisotropic);
+        if aValue > MaxAnisotropic then
+          fAnisotropic := MaxAnisotropic;
+        glTexParameteri(Target, GL_TEXTURE_MAX_ANISOTROPY_EXT, fAnisotropic);
+      end;
+    end else begin
+      fAnisotropic := 0;
+    end;
   end;
-
-  {$IFDEF GLB_TEMPRANGECHECK}
-    {$UNDEF GLB_TEMPRANGECHECK}
-    {$R+}
-  {$ENDIF}
-
-  Result := CreatePalette(Pal^);
-
-  FreeMem(Pal);
-end;
-{$ENDIF}
-
-
-{$IFDEF GLB_SDL_IMAGE}
-function glBitmapRWseek(context: PSDL_RWops; offset: Integer; whence: Integer): Integer; cdecl;
-begin
-  Result := TStream(context^.unknown.data1).Seek(offset, whence);
 end;
+{$ENDREGION}
 
-
-function glBitmapRWread(context: PSDL_RWops; Ptr: Pointer; size: Integer; maxnum : Integer): Integer; cdecl;
+procedure TglBitmap.AfterConstruction;
 begin
-  Result := TStream(context^.unknown.data1).Read(Ptr^, size * maxnum);
-end;
+  inherited AfterConstruction;
 
+  fID         := 0;
+  fTarget     := 0;
+  fIsResident := False;
 
-function glBitmapRWwrite(context: PSDL_RWops; Ptr: Pointer; size: Integer; num: Integer): Integer; cdecl;
-begin
-  Result := TStream(context^.unknown.data1).Write(Ptr^, size * num);
-end;
-
+  fFormat                  := glBitmapGetDefaultFormat;
+  fMipMap                  := glBitmapDefaultMipmap;
+  fFreeDataAfterGenTexture := glBitmapGetDefaultFreeDataAfterGenTexture;
+  fDeleteTextureOnFree     := glBitmapGetDefaultDeleteTextureOnFree;
 
-function glBitmapRWclose(context: PSDL_RWops): Integer; cdecl;
-begin
-  Result := 0;
+  glBitmapGetDefaultFilter     (fFilterMin, fFilterMag);
+  glBitmapGetDefaultTextureWrap(fWrapS, fWrapT, fWrapR);
 end;
 
-
-function glBitmapCreateRWops(Stream: TStream): PSDL_RWops;
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.BeforeDestruction;
 begin
-  Result := SDL_AllocRW;
-
-  if Result = nil then
-    raise EglBitmapException.Create('glBitmapCreateRWops - SDL_AllocRW failed.');
-
-  Result^.seek := glBitmapRWseek;
-  Result^.read := glBitmapRWread;
-  Result^.write := glBitmapRWwrite;
-  Result^.close := glBitmapRWclose;
-  Result^.unknown.data1 := Stream;
+  SetDataPointer(nil, ifEmpty);
+  if (ID > 0) and fDeleteTextureOnFree then
+    glDeleteTextures(1, @ID);
+  inherited BeforeDestruction;
 end;
-{$ENDIF}
 
-
-{*
-** Helper functions
-*}
-function LoadTexture(Filename: String; var Texture: Cardinal{$IFDEF GLB_DELPHI}; LoadFromRes : Boolean; Instance: Cardinal{$ENDIF}): Boolean;
-var
-  glBitmap: TglBitmap2D;
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.CreateID;
 begin
-  Result := false;
-  Texture := 0;
-
-  {$IFDEF GLB_DELPHI}
-  if Instance = 0 then
-    Instance := HInstance;
-
-  if (LoadFromRes) then
-    glBitmap := TglBitmap2D.CreateFromResourceName(Instance, FileName)
-  else
-  {$ENDIF}
-    glBitmap := TglBitmap2D.Create(FileName);
-
-  try
-    glBitmap.DeleteTextureOnFree := False;
-    glBitmap.FreeDataAfterGenTexture := False;
-    glBitmap.GenTexture(True);
-    if (glBitmap.ID > 0) then begin
-      Texture := glBitmap.ID;
-      Result := True;
-    end;
-  finally
-    glBitmap.Free;
-  end;
+  if ID <> 0 then
+    glDeleteTextures(1, @ID);
+  glGenTextures(1, @ID);
+  Bind(false);
 end;
 
-
-function LoadCubeMap(PositiveX, NegativeX, PositiveY, NegativeY, PositiveZ, NegativeZ: String; var Texture: Cardinal{$IFDEF GLB_DELPHI}; LoadFromRes : Boolean; Instance: Cardinal{$ENDIF}): Boolean;
-var
-  CM: TglBitmapCubeMap;
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SetupParameters(var aBuildWithGlu: Boolean);
 begin
-  Texture := 0;
-
-  {$IFDEF GLB_DELPHI}
-  if Instance = 0 then
-    Instance := HInstance;
-  {$ENDIF}
-
-  CM := TglBitmapCubeMap.Create;
-  try
-    CM.DeleteTextureOnFree := False;
-
-    // Maps
-    {$IFDEF GLB_DELPHI}
-    if (LoadFromRes) then
-      CM.LoadFromResource(Instance, PositiveX)
-    else
-    {$ENDIF}
-      CM.LoadFromFile(PositiveX);
-    CM.GenerateCubeMap(GL_TEXTURE_CUBE_MAP_POSITIVE_X);
-
-    {$IFDEF GLB_DELPHI}
-    if (LoadFromRes) then
-      CM.LoadFromResource(Instance, NegativeX)
-    else
-    {$ENDIF}
-      CM.LoadFromFile(NegativeX);
-    CM.GenerateCubeMap(GL_TEXTURE_CUBE_MAP_NEGATIVE_X);
-
-    {$IFDEF GLB_DELPHI}
-    if (LoadFromRes) then
-      CM.LoadFromResource(Instance, PositiveY)
-    else
-    {$ENDIF}
-      CM.LoadFromFile(PositiveY);
-    CM.GenerateCubeMap(GL_TEXTURE_CUBE_MAP_POSITIVE_Y);
-
-    {$IFDEF GLB_DELPHI}
-    if (LoadFromRes) then
-      CM.LoadFromResource(Instance, NegativeY)
-    else
-    {$ENDIF}
-      CM.LoadFromFile(NegativeY);
-    CM.GenerateCubeMap(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y);
-
-    {$IFDEF GLB_DELPHI}
-    if (LoadFromRes) then
-      CM.LoadFromResource(Instance, PositiveZ)
-    else
-    {$ENDIF}
-      CM.LoadFromFile(PositiveZ);
-    CM.GenerateCubeMap(GL_TEXTURE_CUBE_MAP_POSITIVE_Z);
+  // Set Up Parameters
+  SetWrap(fWrapS, fWrapT, fWrapR);
+  SetFilter(fFilterMin, fFilterMag);
+  SetAnisotropic(fAnisotropic);
+  SetBorderColor(fBorderColor[0], fBorderColor[1], fBorderColor[2], fBorderColor[3]);
 
-    {$IFDEF GLB_DELPHI}
-    if (LoadFromRes) then
-      CM.LoadFromResource(Instance, NegativeZ)
+  // Mip Maps Generation Mode
+  aBuildWithGlu := False;
+  if (MipMap = mmMipmap) then begin
+    if (GL_VERSION_1_4 or GL_SGIS_generate_mipmap) then
+      glTexParameteri(Target, GL_GENERATE_MIPMAP, GL_TRUE)
     else
-    {$ENDIF}
-      CM.LoadFromFile(NegativeZ);
-    CM.GenerateCubeMap(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z);
-
-    Texture := CM.ID;
-    Result := True;
-  finally
-    CM.Free;
-  end;
+      BuildWithGlu := True;
+  end else if (MipMap = mmMipmapGlu) then
+    BuildWithGlu := True;
 end;
 
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SelectFormat(const aFormat: TglBitmapFormat; var glFormat, glInternalFormat, glType: Cardinal);
 
-function LoadNormalMap(Size: Integer; var Texture: Cardinal): Boolean;
-var
-  NM: TglBitmapNormalMap;
-begin
-  Texture := 0;
-
-  NM := TglBitmapNormalMap.Create;
-  try
-    NM.DeleteTextureOnFree := False;
-    NM.GenerateNormalMap(Size);
-
-    Texture := NM.ID;
-    Result := True;
-  finally
-    NM.Free;
+  procedure Check12;
+  begin
+    if not GL_VERSION_1_2 then
+      raise EglBitmapUnsupportedFormatFormat.Create('SelectFormat - You need at least OpenGL 1.2 to support these format.');
   end;
-end;
-
-
-procedure glBitmapSetDefaultFormat(Format: TglBitmapFormat);
-begin
-  glBitmapDefaultFormat := Format;
-end;
-
-
-procedure glBitmapSetDefaultDeleteTextureOnFree(DeleteTextureOnFree: Boolean);
-begin
-  glBitmapDefaultDeleteTextureOnFree := DeleteTextureOnFree;
-end;
 
-
-procedure glBitmapSetDefaultFilter(Min, Mag: Integer);
 begin
-  glBitmapDefaultFilterMin := Min;
-  glBitmapDefaultFilterMag := Mag;
-end;
+  PIXEL_DESC_ALPHA12;
+  glType := GL_UNSIGNED_BYTE;
+  glInternalFormat := Cardinal(aFormat);
 
+  case aFormat of
+    tfAlpha4, tfAlpha8, tfAlpha12, tfAlpha16:
+      glFormat := GL_ALPHA;
 
-procedure glBitmapSetDefaultWrap(S: Integer; T: Integer; R: Integer);
-begin
-  glBitmapDefaultWrapS := S;
-  glBitmapDefaultWrapT := T;
-  glBitmapDefaultWrapR := R;
-end;
+    tfLuminance4, tfLuminance8, tfLuminance12, tfLuminance16:
+      glFormat := GL_LUMINANCE;
 
+    tfuminance4Alpha4, tfLuminance6Alpha2, tfLuminance8Alpha8,
+    tfLuminance12Alpha4, tfLuminance12Alpha12, tfLuminance16Alpha16:
+      glFormat := GL_LUMINANCE_ALPHA;
 
-procedure glBitmapSetDefaultFreeDataAfterGenTexture(FreeData: Boolean);
-begin
-  glBitmapDefaultFreeDataAfterGenTextures := FreeData;
-end;
+    tfR3G3B2, tfRGB4, tfRGB5, tfRGB8, tfRGB10, tfRGB12, tfRGB16:
+      glFormat := GL_RGB;
 
+    tfRGBA2, tfRGBA4, tfRGB5A1, tfRGBA8, tfRGB10A2, tfRGBA12, tfRGBA16:
+      glFormat := GL_RGBA;
 
-function glBitmapGetDefaultFormat: TglBitmapFormat;
-begin
-  Result := glBitmapDefaultFormat;
-end;
+    tfDepth16, tfDepth24, tfDepth32:
+      glFormat := GL_DEPTH_COMPONENT;
+  else
+    glFormat := 0;
+  end;
 
+  case aFormat of
+    tfRGBA4:
+      glType := GL_UNSIGNED_SHORT_4_4_4_4;
+    tfRGB5A1:
+      glType := GL_UNSIGNED_SHORT_5_5_5_1;
+    tfRG
+  end;
 
-function glBitmapGetDefaultDeleteTextureOnFree: Boolean;
-begin
-  Result := glBitmapDefaultDeleteTextureOnFree;
-end;
 
+  // selecting Format
+  case DataFormat of
+    ifAlpha:
+      glFormat := GL_ALPHA;
+    ifLuminance:
+      glFormat := GL_LUMINANCE;
+    ifDepth8:
+      glFormat := GL_DEPTH_COMPONENT;
+    ifLuminanceAlpha:
+      glFormat := GL_LUMINANCE_ALPHA;
+    ifBGR8:
+      begin
+        if (GL_VERSION_1_2 or GL_EXT_bgra) then begin
+          glFormat := GL_BGR;
+        end else begin
+          if CanConvertImage then
+            ConvertTo(tfRGB8);
+          glFormat := GL_RGB;
+        end;
+      end;
+    ifBGRA8:
+      begin
+        if (GL_VERSION_1_2 or GL_EXT_bgra) then begin
+          glFormat := GL_BGRA;
+        end else begin
+          if CanConvertImage then
+            ConvertTo(tfRGBA8);
+          glFormat := GL_RGBA;
+        end;
+      end;
+    tfRGB8:
+      glFormat := GL_RGB;
+    tfRGBA8:
+      glFormat := GL_RGBA;
+    tfRGBA4:
+      begin
+        Check12;
+        glFormat := GL_BGRA;
+        glType := GL_UNSIGNED_SHORT_4_4_4_4_REV;
+      end;
+    tfRGB5A1:
+      begin
+        Check12;
+        glFormat := GL_BGRA;
+        glType := GL_UNSIGNED_SHORT_1_5_5_5_REV;
+      end;
+    tfRGB10A2:
+      begin
+        Check12;
+        glFormat := GL_BGRA;
+        glType := GL_UNSIGNED_INT_2_10_10_10_REV;
+      end;
+    ifR5G6B5:
+      begin
+        Check12;
+        glFormat := GL_RGB;
+        glType := GL_UNSIGNED_SHORT_5_6_5;
+      end;
+    else
+      glFormat := 0;
+  end;
 
-procedure glBitmapGetDefaultFilter(var Min, Mag: Integer);
-begin
-  Min := glBitmapDefaultFilterMin;
-  Mag := glBitmapDefaultFilterMag;
+  // Selecting InternalFormat
+  case DataFormat of
+    ifDXT1, ifDXT3, ifDXT5:
+      begin
+        if GL_EXT_texture_compression_s3tc then begin
+          case DataFormat of
+            ifDXT1:
+              glInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
+            ifDXT3:
+              glInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
+            ifDXT5:
+              glInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
+          end;
+        end else begin
+          // Compression isn't supported so convert to RGBA
+          if CanConvertImage then
+            ConvertTo(tfRGBA8);
+          glFormat := GL_RGBA;
+          glInternalFormat := GL_RGBA8;
+        end;
+      end;
+    ifAlpha:
+      begin
+        case Format of
+          tf4BitsPerChanel:
+            glInternalFormat := GL_ALPHA4;
+          tf8BitsPerChanel:
+            glInternalFormat := GL_ALPHA8;
+          tfCompressed:
+            begin
+              if (GL_ARB_texture_compression or GL_VERSION_1_3) then
+                glInternalFormat := GL_COMPRESSED_ALPHA
+              else
+                glInternalFormat := GL_ALPHA;
+            end;
+          else
+            glInternalFormat := GL_ALPHA;
+        end;
+      end;
+    ifLuminance:
+      begin
+        case Format of
+          tf4BitsPerChanel:
+            glInternalFormat := GL_LUMINANCE4;
+          tf8BitsPerChanel:
+            glInternalFormat := GL_LUMINANCE8;
+          tfCompressed:
+            begin
+              if (GL_ARB_texture_compression or GL_VERSION_1_3) then
+                glInternalFormat := GL_COMPRESSED_LUMINANCE
+              else
+                glInternalFormat := GL_LUMINANCE;
+            end;
+          else
+            glInternalFormat := GL_LUMINANCE;
+        end;
+      end;
+    ifDepth8:
+      begin
+        glInternalFormat := GL_DEPTH_COMPONENT;
+      end;
+    ifLuminanceAlpha:
+      begin
+        case Format of
+          tf4BitsPerChanel:
+            glInternalFormat := GL_LUMINANCE4_ALPHA4;
+          tf8BitsPerChanel:
+            glInternalFormat := GL_LUMINANCE8_ALPHA8;
+          tfCompressed:
+            begin
+              if (GL_ARB_texture_compression or GL_VERSION_1_3) then
+                glInternalFormat := GL_COMPRESSED_LUMINANCE_ALPHA
+              else
+                glInternalFormat := GL_LUMINANCE_ALPHA;
+            end;
+          else
+            glInternalFormat := GL_LUMINANCE_ALPHA;
+        end;
+      end;
+    ifBGR8, tfRGB8:
+      begin
+        case Format of
+          tf4BitsPerChanel:
+            glInternalFormat := GL_RGB4;
+          tf8BitsPerChanel:
+            glInternalFormat := GL_RGB8;
+          tfCompressed:
+            begin
+              if (GL_ARB_texture_compression or GL_VERSION_1_3) then begin
+                glInternalFormat := GL_COMPRESSED_RGB
+              end else begin
+                if (GL_EXT_texture_compression_s3tc) then
+                  glInternalFormat := GL_COMPRESSED_RGB_S3TC_DXT1_EXT
+                else
+                  glInternalFormat := GL_RGB;
+              end;
+            end;
+          else
+            glInternalFormat := GL_RGB;
+        end;
+      end;
+    ifBGRA8, tfRGBA8, tfRGBA4, tfRGB5A1, tfRGB10A2, ifR5G6B5:
+      begin
+        case Format of
+          tf4BitsPerChanel:
+            glInternalFormat := GL_RGBA4;
+          tf8BitsPerChanel:
+            glInternalFormat := GL_RGBA8;
+          tfCompressed:
+            begin
+              if (GL_ARB_texture_compression or GL_VERSION_1_3) then begin
+                glInternalFormat := GL_COMPRESSED_RGBA
+              end else begin
+                if (GL_EXT_texture_compression_s3tc) then
+                  glInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
+                else
+                  glInternalFormat := GL_RGBA;
+              end;
+            end;
+          else
+            glInternalFormat := GL_RGBA;
+        end;
+      end;
+  end;
 end;
 
 
-procedure glBitmapGetDefaultTextureWrap(var S, T, R: Integer);
-begin
-  S := glBitmapDefaultWrapS;
-  T := glBitmapDefaultWrapT;
-  R := glBitmapDefaultWrapR;
-end;
 
 
-function glBitmapGetDefaultFreeDataAfterGenTexture: Boolean;
-begin
-  Result := glBitmapDefaultFreeDataAfterGenTextures;
-end;
 
 
-{ TglBitmap }
 
-procedure TglBitmap.AfterConstruction;
-begin
-  inherited;
 
-  fID := 0;
-  fTarget := 0;
-  fMipMap := mmMipmap;
-  fIsResident := False;
 
-  // get defaults
-  fFreeDataAfterGenTexture := glBitmapGetDefaultFreeDataAfterGenTexture;
-  fDeleteTextureOnFree := glBitmapGetDefaultDeleteTextureOnFree;
 
-  fFormat := glBitmapGetDefaultFormat;
 
-  glBitmapGetDefaultFilter(fFilterMin, fFilterMag);
-  glBitmapGetDefaultTextureWrap(fWrapS, fWrapT, fWrapR);
-end;
 
 
-procedure TglBitmap.BeforeDestruction;
-begin
-  SetDataPointer(nil, ifEmpty);
 
-  if ((ID > 0) and (fDeleteTextureOnFree)) then
-    glDeleteTextures(1, @ID);
 
-  inherited;
-end;
 
 
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 constructor TglBitmap.Create;
 begin
   {$IFNDEF GLB_NO_NATIVE_GL}
@@ -2693,7 +2566,7 @@ end;
 
 
 constructor TglBitmap.Create(Size: TglBitmapPixelPosition;
-  Format: TglBitmapInternalFormat);
+  Format: TglBitmapFormat);
 var
   Image: pByte;
   ImageSize: Integer;
@@ -2714,7 +2587,7 @@ end;
 
 
 constructor TglBitmap.Create(Size: TglBitmapPixelPosition;
-  Format: TglBitmapInternalFormat; Func: TglBitmapFunction; CustomData: Pointer);
+  Format: TglBitmapFormat; Func: TglBitmapFunction; CustomData: Pointer);
 begin
   Create;
   LoadFromFunc(Size, Func, Format, CustomData);
@@ -2771,7 +2644,7 @@ begin
 end;
 
 
-procedure TglBitmap.LoadFromFile(FileName: String);
+procedure TglBitmap.LoadFromFile(const aFileName: String);
 var
   FS: TFileStream;
 begin
@@ -2788,7 +2661,7 @@ begin
 end;
 
 
-procedure TglBitmap.LoadFromStream(Stream: TStream);
+procedure TglBitmap.LoadFromStream(const aStream: TStream);
 begin
   {$IFDEF GLB_SUPPORT_PNG_READ}
   if not LoadPNG(Stream) then
@@ -2845,8 +2718,9 @@ end;
 
 
 
-procedure TglBitmap.LoadFromFunc(Size: TglBitmapPixelPosition;
-  Func: TglBitmapFunction; Format: TglBitmapInternalFormat; CustomData: Pointer);
+procedure TglBitmap.LoadFromFunc(const aSize: TglBitmapPixelPosition;
+  const aFunc: TglBitmapFunction; const aFormat: TglBitmapFormat;
+  const aArgs: PtrInt);
 var
   Image: pByte;
   ImageSize: Integer;
@@ -2866,7 +2740,8 @@ begin
 end;
 
 
-procedure TglBitmap.SaveToFile(FileName: String; FileType: TglBitmapFileType);
+procedure TglBitmap.SaveToFile(const aFileName: String;
+  const aFileType: TglBitmapFileType);
 var
   FS: TFileStream;
 begin
@@ -2880,7 +2755,8 @@ begin
 end;
 
 
-procedure TglBitmap.SaveToStream(Stream: TStream; FileType: TglBitmapFileType);
+procedure TglBitmap.SaveToStream(const aStream: TStream;
+  const aFileType: TglBitmapFileType);
 begin
   case FileType of
     {$IFDEF GLB_SUPPORT_PNG_WRITE}
@@ -3471,7 +3347,7 @@ begin
 end;
 
 
-procedure TglBitmap.SetFilter(Min, Mag: Integer);
+procedure TglBitmap.SetFilter(const aMin, aMag: Cardinal);
 begin
   case Min of
     GL_NEAREST:
@@ -3520,7 +3396,8 @@ begin
 end;
 
 
-procedure TglBitmap.SetWrap(S: Integer; T: Integer; R: Integer);
+procedure TglBitmap.SetWrap(const S: Cardinal; const T: Cardinal;
+  const R: Cardinal);
 begin
   case S of
     GL_CLAMP:
@@ -3622,7 +3499,7 @@ end;
 
 
 procedure TglBitmap.SetDataPointer(NewData: pByte;
-  Format: TglBitmapInternalFormat; Width: Integer; Height: Integer);
+  Format: TglBitmapFormat; Width: Integer; Height: Integer);
 begin
   // Data
   if Data <> NewData then begin
@@ -4250,7 +4127,7 @@ var
   Info: TBMPInfo;
   NewImage, pData: pByte;
 
-  Format: TglBitmapInternalFormat;
+  Format: TglBitmapFormat;
   LineSize, Padding, LineIdx: Integer;
   RedMask, GreenMask, BlueMask, AlphaMask: Cardinal;
 
@@ -4309,16 +4186,16 @@ begin
       16:
         begin
           if (RedMask = 0) and (GreenMask = 0) and (BlueMask = 0) then begin
-              Format := ifRGB5A1;
+              Format := tfRGB5A1;
           end else begin
             if FormatCheckFormat(RedMask, GreenMask, BlueMask, AlphaMask, ifLuminanceAlpha) then
               Format := ifLuminanceAlpha;
 
-            if FormatCheckFormat(RedMask, GreenMask, BlueMask, AlphaMask, ifRGBA4) then
-              Format := ifRGBA4;
+            if FormatCheckFormat(RedMask, GreenMask, BlueMask, AlphaMask, tfRGBA4) then
+              Format := tfRGBA4;
 
-            if FormatCheckFormat(RedMask, GreenMask, BlueMask, 0, ifRGB5A1) then
-              Format := ifRGB5A1;
+            if FormatCheckFormat(RedMask, GreenMask, BlueMask, 0, tfRGB5A1) then
+              Format := tfRGB5A1;
 
             if FormatCheckFormat(RedMask, GreenMask, BlueMask, 0, ifR5G6B5) then
               Format := ifR5G6B5;
@@ -4330,14 +4207,14 @@ begin
           if (RedMask = 0) and (GreenMask = 0) and (BlueMask = 0) then begin
             Format := ifBGRA8;
           end else begin
-            if FormatCheckFormat(RedMask, GreenMask, BlueMask, AlphaMask, ifRGBA8) then
-              Format := ifRGBA8;
+            if FormatCheckFormat(RedMask, GreenMask, BlueMask, AlphaMask, tfRGBA8) then
+              Format := tfRGBA8;
 
             if FormatCheckFormat(RedMask, GreenMask, BlueMask, AlphaMask, ifBGRA8) then
               Format := ifBGRA8;
 
-            if FormatCheckFormat(RedMask, GreenMask, BlueMask, AlphaMask, ifRGB10A2) then
-              Format := ifRGB10A2;
+            if FormatCheckFormat(RedMask, GreenMask, BlueMask, AlphaMask, tfRGB10A2) then
+              Format := tfRGB10A2;
           end;
         end;
     end;
@@ -4375,7 +4252,7 @@ begin
   end
     else Stream.Position := StreamPos;
 end;
-
+{$ENDREGION}
 
 const
   DDS_MAGIC                   = $20534444;
@@ -4446,62 +4323,10 @@ type
     dwReserved: array[0..10] of Cardinal;
     PixelFormat: TDDSPixelFormat;
     Caps: TDDSCaps;
-    dwReserved2: Cardinal;
-  end;
-
-procedure TglBitmap.SetCustomData(const aValue: Pointer);
-begin
-  if fCustomData = aValue then Exit;
-  fCustomData := aValue;
-end;
-
-procedure TglBitmap.SetCustomName(const aValue: String);
-begin
-  if fCustomName = aValue then Exit;
-  fCustomName := aValue;
-end;
-
-procedure TglBitmap.SetCustomNameW(const aValue: WideString);
-begin
-  if fCustomNameW = aValue then Exit;
-  fCustomNameW := aValue;
-end;
-
-procedure TglBitmap.SetDeleteTextureOnFree(const aValue: Boolean);
-begin
-  if fDeleteTextureOnFree = aValue then Exit;
-  fDeleteTextureOnFree := aValue;
-end;
-
-procedure TglBitmap.SetFormat(const aValue: TglBitmapFormat);
-begin
-  if fFormat = aValue then Exit;
-  fFormat := aValue;
-end;
-
-procedure TglBitmap.SetFreeDataAfterGenTexture(const aValue: Boolean);
-begin
-  if fFreeDataAfterGenTexture = aValue then Exit;
-  fFreeDataAfterGenTexture := aValue;
-end;
-
-procedure TglBitmap.SetID(const aValue: Cardinal);
-begin
-  if fID = aValue then Exit;
-  fID := aValue;
-end;
-
-procedure TglBitmap.SetMipMap(const aValue: TglBitmapMipMap);
-begin
-  if fMipMap = aValue then Exit;
-  fMipMap := aValue;
-end;
+    dwReserved2: Cardinal;
+  end;
+
 
-procedure TglBitmap.SetTarget(const aValue: Cardinal);
-begin
-  if fTarget = aValue then Exit;
-  fTarget := aValue;
-end;
 
 function TglBitmap.LoadDDS(Stream: TStream): Boolean;
 var
@@ -4512,7 +4337,7 @@ var
 //  MipMapCount, X, Y, XSize, YSize: Cardinal;
   RowSize: Cardinal;
   NewImage, pData: pByte;
-  Format: TglBitmapInternalFormat;
+  Format: TglBitmapFormat;
 
 
   function RaiseEx : Exception;
@@ -4521,7 +4346,7 @@ var
   end;
 
   
-  function GetInternalFormat: TglBitmapInternalFormat;
+  function GetInternalFormat: TglBitmapFormat;
   begin
     with Header.PixelFormat do begin
       // Compresses
@@ -4550,8 +4375,8 @@ var
               if dwFlags and DDPF_ALPHAPIXELS > 0 then begin
                 // Alpha
                 case GetBitSize(dwRBitMask) of
-                  5: Result := ifRGB5A1;
-                  4: Result := ifRGBA4;
+                  5: Result := tfRGB5A1;
+                  4: Result := tfRGBA4;
                 else
                   Result := ifLuminanceAlpha;
                 end;
@@ -4565,18 +4390,18 @@ var
               if dwRBitMask > dwBBitMask then
                 Result := ifBGR8
               else
-                Result := ifRGB8;
+                Result := tfRGB8;
             end;
           32:
             begin
               if GetBitSize(dwRBitMask) = 10 then
-                Result := ifRGB10A2
+                Result := tfRGB10A2
               else
 
               if dwRBitMask > dwBBitMask then
                 Result := ifBGRA8
               else
-                Result := ifRGBA8;
+                Result := tfRGBA8;
             end;
           else
             raise RaiseEx;
@@ -4670,7 +4495,7 @@ var
   NewImage, pData: PByte;
   StreamPos: Int64;
   PixelSize, LineSize, YStart, YEnd, YInc: Integer;
-  Format: TglBitmapInternalFormat;
+  Format: TglBitmapFormat;
 
 const
   CACHE_SIZE = $4000;
@@ -5088,7 +4913,7 @@ var
   Pix: TglBitmapPixelData;
 begin
   if not FormatIsUncompressed(InternalFormat) then
-    raise EglBitmapUnsupportedInternalFormat.Create('SaveDDS - ' + UNSUPPORTED_INTERNAL_FORMAT);
+    raise EglBitmapUnsupportedFormatFormat.Create('SaveDDS - ' + UNSUPPORTED_INTERNAL_FORMAT);
 
   if InternalFormat = ifAlpha then
     FormatPreparePixel(Pix, ifLuminance)
@@ -5164,7 +4989,7 @@ var
 
 begin
   if not (ftTGA in FormatGetSupportedFiles (InternalFormat)) then 
-    raise EglBitmapUnsupportedInternalFormat.Create('SaveTGA - ' + UNSUPPORTED_INTERNAL_FORMAT);
+    raise EglBitmapUnsupportedFormatFormat.Create('SaveTGA - ' + UNSUPPORTED_INTERNAL_FORMAT);
 
   FillChar(Header, SizeOf(Header), 0);
 
@@ -5179,18 +5004,18 @@ begin
         Header.ImageType := TGA_UNCOMPRESSED_GRAY;
         Header.Bpp := 16;
       end;
-    ifRGB8, ifBGR8:
+    tfRGB8, ifBGR8:
       begin
         Header.ImageType := TGA_UNCOMPRESSED_RGB;
         Header.Bpp := 24;
       end;
-    ifRGBA8, ifBGRA8:
+    tfRGBA8, ifBGRA8:
       begin
         Header.ImageType := TGA_UNCOMPRESSED_RGB;
         Header.Bpp := 32;
       end;
     else
-      raise EglBitmapUnsupportedInternalFormat.Create('SaveTGA - ' + UNSUPPORTED_INTERNAL_FORMAT);
+      raise EglBitmapUnsupportedFormatFormat.Create('SaveTGA - ' + UNSUPPORTED_INTERNAL_FORMAT);
   end;
 
   Header.Width := Width;
@@ -5204,14 +5029,14 @@ begin
 
   // convert RGB(A) to BGR(A)
   Size := FormatGetImageSize(glBitmapPosition(Width, Height), InternalFormat);
-  if InternalFormat in [ifRGB8, ifRGBA8] then begin
+  if InternalFormat in [tfRGB8, tfRGBA8] then begin
     GetMem(pTemp, Size);
   end else
     pTemp := Data;
 
   try
     // convert data
-    if InternalFormat in [ifRGB8, ifRGBA8] then begin
+    if InternalFormat in [tfRGB8, tfRGBA8] then begin
       Move(Data^, pTemp^, Size);
       ConvertData(pTemp);
     end;
@@ -5220,7 +5045,7 @@ begin
     Stream.Write(pTemp^, Size);
   finally
     // free tempdata
-    if InternalFormat in [ifRGB8, ifRGBA8] then
+    if InternalFormat in [tfRGB8, tfRGBA8] then
       FreeMem(pTemp);
   end;
 end;
@@ -5402,7 +5227,7 @@ var
 
 begin
   if not (ftBMP in FormatGetSupportedFiles(InternalFormat)) then
-    raise EglBitmapUnsupportedInternalFormat.Create('SaveBMP - ' + UNSUPPORTED_INTERNAL_FORMAT);
+    raise EglBitmapUnsupportedFormatFormat.Create('SaveBMP - ' + UNSUPPORTED_INTERNAL_FORMAT);
 
   ImageSize := Trunc(Width * Height * FormatGetSize(InternalFormat));
 
@@ -5430,20 +5255,20 @@ begin
         Info.biClrUsed := 256;
         Info.biClrImportant := 256;
       end;
-    ifLuminanceAlpha, ifRGBA4, ifR5G6B5, ifRGB5A1:
+    ifLuminanceAlpha, tfRGBA4, ifR5G6B5, tfRGB5A1:
       begin
         Info.biBitCount := 16;
         Info.biCompression := BMP_COMP_BITFIELDS;
       end;
-    ifBGR8, ifRGB8:
+    ifBGR8, tfRGB8:
       Info.biBitCount := 24;
-    ifBGRA8, ifRGBA8, ifRGB10A2:
+    ifBGRA8, tfRGBA8, tfRGB10A2:
       begin
         Info.biBitCount := 32;
         Info.biCompression := BMP_COMP_BITFIELDS;
       end;
     else
-      raise EglBitmapUnsupportedInternalFormat.Create('SaveBMP - ' + UNSUPPORTED_INTERNAL_FORMAT);
+      raise EglBitmapUnsupportedFormatFormat.Create('SaveBMP - ' + UNSUPPORTED_INTERNAL_FORMAT);
   end;
   Info.biXPelsPerMeter := 2835;
   Info.biYPelsPerMeter := 2835;
@@ -5495,7 +5320,7 @@ begin
 
   // prepare row buffer. But only for RGB because RGBA supports color masks
   // so it's possible to change color within the image.
-  if InternalFormat = ifRGB8 then
+  if InternalFormat = tfRGB8 then
     GetMem(pTemp, fRowSize)
   else
     pTemp := nil;
@@ -5504,7 +5329,7 @@ begin
     // write image data
     for LineIdx := 0 to Height - 1 do begin
       // preparing row
-      if InternalFormat = ifRGB8 then begin
+      if InternalFormat = tfRGB8 then begin
         Move(pData^, pTemp^, fRowSize);
         SwapRGB(pTemp, Width, False);
       end else
@@ -5519,7 +5344,7 @@ begin
     end;
   finally
     // destroy row buffer
-    if InternalFormat = ifRGB8 then
+    if InternalFormat = tfRGB8 then
       FreeMem(pTemp);
   end;
 end;
@@ -5560,238 +5385,8 @@ begin
 end;
 
 
-procedure TglBitmap.CreateID;
-begin
-  // Generate Texture
-  if ID <> 0 then
-    glDeleteTextures(1, @ID);
-
-  glGenTextures(1, @ID);
-
-  Bind(False);
-end;
-
-
-procedure TglBitmap.SetupParameters(var BuildWithGlu: Boolean);
-begin
-  // Set up parameters
-  SetWrap(fWrapS, fWrapT, fWrapR);
-  SetFilter(fFilterMin, fFilterMag);
-  SetAnisotropic(fAnisotropic);
-  SetBorderColor(fBorderColor[0], fBorderColor[1], fBorderColor[2], fBorderColor[3]);
-
-  // Mip Maps generation Mode
-  BuildWithGlu := False;
-
-  if (MipMap = mmMipmap) then begin
-    if (GL_VERSION_1_4 or GL_SGIS_generate_mipmap) then
-      glTexParameteri(Target, GL_GENERATE_MIPMAP, GL_TRUE)
-    else
-      BuildWithGlu := True;
-  end else
-  if (MipMap = mmMipmapGlu) then
-    BuildWithGlu := True;
-end;
-
-
-procedure TglBitmap.SelectFormat(DataFormat: TglBitmapInternalFormat; var glFormat, glInternalFormat, glType: Cardinal; CanConvertImage: Boolean = True);
-
-  procedure Check12;
-  begin
-    if not GL_VERSION_1_2 then
-      raise EglBitmapUnsupportedInternalFormat.Create('SelectFormat - You need at least OpenGL 1.2 to support these format.');
-  end;
-
-begin
-  glType := GL_UNSIGNED_BYTE;
 
-  // selecting Format
-  case DataFormat of
-    ifAlpha:
-      glFormat := GL_ALPHA;
-    ifLuminance:
-      glFormat := GL_LUMINANCE;
-    ifDepth8:
-      glFormat := GL_DEPTH_COMPONENT;
-    ifLuminanceAlpha:
-      glFormat := GL_LUMINANCE_ALPHA;
-    ifBGR8:
-      begin
-        if (GL_VERSION_1_2 or GL_EXT_bgra) then begin
-          glFormat := GL_BGR;
-        end else begin
-          if CanConvertImage then
-            ConvertTo(ifRGB8);
-          glFormat := GL_RGB;
-        end;
-      end;
-    ifBGRA8:
-      begin
-        if (GL_VERSION_1_2 or GL_EXT_bgra) then begin
-          glFormat := GL_BGRA;
-        end else begin
-          if CanConvertImage then
-            ConvertTo(ifRGBA8);
-          glFormat := GL_RGBA;
-        end;
-      end;
-    ifRGB8:
-      glFormat := GL_RGB;
-    ifRGBA8:
-      glFormat := GL_RGBA;
-    ifRGBA4:
-      begin
-        Check12;
-        glFormat := GL_BGRA;
-        glType := GL_UNSIGNED_SHORT_4_4_4_4_REV; 
-      end;
-    ifRGB5A1:
-      begin
-        Check12;
-        glFormat := GL_BGRA;
-        glType := GL_UNSIGNED_SHORT_1_5_5_5_REV;
-      end;
-    ifRGB10A2:
-      begin
-        Check12;
-        glFormat := GL_BGRA;
-        glType := GL_UNSIGNED_INT_2_10_10_10_REV;
-      end;
-    ifR5G6B5:
-      begin
-        Check12;
-        glFormat := GL_RGB;
-        glType := GL_UNSIGNED_SHORT_5_6_5;
-      end;
-    else
-      glFormat := 0;
-  end;
 
-  // Selecting InternalFormat
-  case DataFormat of
-    ifDXT1, ifDXT3, ifDXT5:
-      begin
-        if GL_EXT_texture_compression_s3tc then begin
-          case DataFormat of
-            ifDXT1:
-              glInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
-            ifDXT3:
-              glInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
-            ifDXT5:
-              glInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
-          end;
-        end else begin
-          // Compression isn't supported so convert to RGBA
-          if CanConvertImage then
-            ConvertTo(ifRGBA8);
-          glFormat := GL_RGBA;
-          glInternalFormat := GL_RGBA8;
-        end;
-      end;
-    ifAlpha:
-      begin
-        case Format of
-          tf4BitsPerChanel:
-            glInternalFormat := GL_ALPHA4;
-          tf8BitsPerChanel:
-            glInternalFormat := GL_ALPHA8;
-          tfCompressed:
-            begin
-              if (GL_ARB_texture_compression or GL_VERSION_1_3) then
-                glInternalFormat := GL_COMPRESSED_ALPHA
-              else
-                glInternalFormat := GL_ALPHA;
-            end;
-          else
-            glInternalFormat := GL_ALPHA;
-        end;
-      end;
-    ifLuminance:
-      begin
-        case Format of
-          tf4BitsPerChanel:
-            glInternalFormat := GL_LUMINANCE4;
-          tf8BitsPerChanel:
-            glInternalFormat := GL_LUMINANCE8;
-          tfCompressed:
-            begin
-              if (GL_ARB_texture_compression or GL_VERSION_1_3) then
-                glInternalFormat := GL_COMPRESSED_LUMINANCE
-              else
-                glInternalFormat := GL_LUMINANCE;
-            end;
-          else
-            glInternalFormat := GL_LUMINANCE;
-        end;
-      end;
-    ifDepth8:
-      begin
-        glInternalFormat := GL_DEPTH_COMPONENT;
-      end;
-    ifLuminanceAlpha:
-      begin
-        case Format of
-          tf4BitsPerChanel:
-            glInternalFormat := GL_LUMINANCE4_ALPHA4;
-          tf8BitsPerChanel:
-            glInternalFormat := GL_LUMINANCE8_ALPHA8;
-          tfCompressed:
-            begin
-              if (GL_ARB_texture_compression or GL_VERSION_1_3) then
-                glInternalFormat := GL_COMPRESSED_LUMINANCE_ALPHA
-              else
-                glInternalFormat := GL_LUMINANCE_ALPHA;
-            end;
-          else
-            glInternalFormat := GL_LUMINANCE_ALPHA;
-        end;
-      end;
-    ifBGR8, ifRGB8:
-      begin
-        case Format of
-          tf4BitsPerChanel:
-            glInternalFormat := GL_RGB4;
-          tf8BitsPerChanel:
-            glInternalFormat := GL_RGB8;
-          tfCompressed:
-            begin
-              if (GL_ARB_texture_compression or GL_VERSION_1_3) then begin
-                glInternalFormat := GL_COMPRESSED_RGB
-              end else begin
-                if (GL_EXT_texture_compression_s3tc) then
-                  glInternalFormat := GL_COMPRESSED_RGB_S3TC_DXT1_EXT
-                else
-                  glInternalFormat := GL_RGB;
-              end;
-            end;
-          else
-            glInternalFormat := GL_RGB;
-        end;
-      end;
-    ifBGRA8, ifRGBA8, ifRGBA4, ifRGB5A1, ifRGB10A2, ifR5G6B5:
-      begin
-        case Format of
-          tf4BitsPerChanel:
-            glInternalFormat := GL_RGBA4;
-          tf8BitsPerChanel:
-            glInternalFormat := GL_RGBA8;
-          tfCompressed:
-            begin
-              if (GL_ARB_texture_compression or GL_VERSION_1_3) then begin
-                glInternalFormat := GL_COMPRESSED_RGBA
-              end else begin
-                if (GL_EXT_texture_compression_s3tc) then
-                  glInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
-                else
-                  glInternalFormat := GL_RGBA;
-              end;
-            end;
-          else
-            glInternalFormat := GL_RGBA;
-        end;
-      end;
-  end;
-end;
 
 
 function TglBitmap.FlipHorz: Boolean;
@@ -5861,36 +5456,12 @@ begin
 end;
 
 
-procedure TglBitmap.SetAnisotropic(const aValue: Integer);
-var
-  MaxAniso: Integer;
-begin
-  fAnisotropic := Value;
-
-  if (ID > 0) then begin
-    if GL_EXT_texture_filter_anisotropic then begin
-      if fAnisotropic > 0 then begin
-        Bind(False);
-
-        glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, @MaxAniso);
-
-        if Value > MaxAniso then
-          fAnisotropic := MaxAniso;
-
-        glTexParameteri(Target, GL_TEXTURE_MAX_ANISOTROPY_EXT, fAnisotropic);
-      end;
-    end else begin
-      fAnisotropic := 0;
-    end;
-  end;
-end;
-
 
-procedure TglBitmap.SetInternalFormat(const aValue: TglBitmapInternalFormat);
+procedure TglBitmap.SetInternalFormat(const aValue: TglBitmapFormat);
 begin
   if InternalFormat <> Value then begin
     if FormatGetSize(Value) <> FormatGetSize(InternalFormat) then
-      raise EglBitmapUnsupportedInternalFormat.Create('SetInternalFormat - ' + UNSUPPORTED_INTERNAL_FORMAT);
+      raise EglBitmapUnsupportedFormatFormat.Create('SetInternalFormat - ' + UNSUPPORTED_INTERNAL_FORMAT);
 
     // Update whatever
     SetDataPointer(Data, Value);
@@ -5906,7 +5477,7 @@ end;
 
 
 function TglBitmap.AddFunc(Source: TglBitmap; Func: TglBitmapFunction;
-  CreateTemp: Boolean; Format: TglBitmapInternalFormat; CustomData: Pointer): boolean;
+  CreateTemp: Boolean; Format: TglBitmapFormat; CustomData: Pointer): boolean;
 var
   pDest, NewImage, pSource: pByte;
   TempHeight, TempWidth: Integer;
@@ -6062,7 +5633,7 @@ begin
 end;
 
 
-function TglBitmap.ConvertTo(NewFormat: TglBitmapInternalFormat): boolean;
+function TglBitmap.ConvertTo(NewFormat: TglBitmapFormat): boolean;
 var
   Source, Dest: TglBitmapPixelData;
   PixelDesc: TglBitmapPixelDesc;
@@ -6123,7 +5694,7 @@ begin
 
   if (Assigned(Data)) then begin
     if not (FormatIsUncompressed(InternalFormat) or FormatHasAlpha(InternalFormat)) then
-      raise EglBitmapUnsupportedInternalFormat.Create('RemoveAlpha - ' + UNSUPPORTED_INTERNAL_FORMAT);
+      raise EglBitmapUnsupportedFormatFormat.Create('RemoveAlpha - ' + UNSUPPORTED_INTERNAL_FORMAT);
 
     Result := ConvertTo(FormatGetWithoutAlpha(InternalFormat));
   end;
@@ -6133,30 +5704,12 @@ end;
 function TglBitmap.AddAlphaFromFunc(Func: TglBitmapFunction; CustomData: Pointer): boolean;
 begin
   if not FormatIsUncompressed(InternalFormat) then
-    raise EglBitmapUnsupportedInternalFormat.Create('AddAlphaFromFunc - ' + UNSUPPORTED_INTERNAL_FORMAT);
+    raise EglBitmapUnsupportedFormatFormat.Create('AddAlphaFromFunc - ' + UNSUPPORTED_INTERNAL_FORMAT);
 
   Result := AddFunc(Self, Func, False, FormatGetWithAlpha(InternalFormat), CustomData);
 end;
 
 
-function TglBitmap.GetHeight: Integer;
-begin
-  if ffY in fDimension.Fields then
-    Result := fDimension.Y
-  else
-    Result := -1;
-end;
-
-
-function TglBitmap.GetWidth: Integer;
-begin
-  if ffX in fDimension.Fields then
-    Result := fDimension.X
-  else
-    Result := -1;
-end;
-
-
 function TglBitmap.GetFileHeight: Integer;
 begin
   Result := Max(1, Height);
@@ -6263,7 +5816,7 @@ end;
 
 { TglBitmap2D }
 
-procedure TglBitmap2D.SetDataPointer(Data: pByte; Format: TglBitmapInternalFormat; Width, Height: Integer);
+procedure TglBitmap2D.SetDataPointer(Data: pByte; Format: TglBitmapFormat; Width, Height: Integer);
 var
   Idx, LineWidth: Integer;
 begin
@@ -6734,7 +6287,7 @@ var
 
 begin
   if not FormatIsUncompressed(InternalFormat) then
-    raise EglBitmapUnsupportedInternalFormat.Create('TglBitmap2D.ToNormalMap - ' + UNSUPPORTED_INTERNAL_FORMAT);
+    raise EglBitmapUnsupportedFormatFormat.Create('TglBitmap2D.ToNormalMap - ' + UNSUPPORTED_INTERNAL_FORMAT);
 
   if Scale > 100 then
     Rec.Scale := 100
@@ -6855,14 +6408,14 @@ begin
 end;
 
 
-procedure TglBitmap2D.GrabScreen(Top, Left, Right, Bottom: Integer; Format: TglBitmapInternalFormat);
+procedure TglBitmap2D.GrabScreen(Top, Left, Right, Bottom: Integer; Format: TglBitmapFormat);
 var
   Temp: pByte;
   Size: Integer;
   glFormat, glInternalFormat, glType: Cardinal;
 begin
   if not FormatIsUncompressed(Format) then
-    raise EglBitmapUnsupportedInternalFormat.Create('TglBitmap2D.GrabScreen - ' + UNSUPPORTED_INTERNAL_FORMAT);
+    raise EglBitmapUnsupportedFormatFormat.Create('TglBitmap2D.GrabScreen - ' + UNSUPPORTED_INTERNAL_FORMAT);
 
   // Only to select Formats
   SelectFormat(Format, glFormat, glInternalFormat, glType, False);
@@ -6890,7 +6443,7 @@ var
   Temp: pByte;
   TempWidth, TempHeight, RedSize, GreenSize, BlueSize, AlphaSize, LumSize: Integer;
   TempType, TempIntFormat: Cardinal;
-  IntFormat: TglBitmapInternalFormat;
+  IntFormat: TglBitmapFormat;
 begin
   Bind;
 
@@ -6921,20 +6474,20 @@ begin
         TempType := GL_UNSIGNED_SHORT_5_6_5;
       end;
     GL_RGB, GL_RGB8:
-      IntFormat := ifRGB8;
+      IntFormat := tfRGB8;
     GL_RGBA, GL_RGBA4, GL_RGBA8:
       begin
         if (RedSize = 4) and (BlueSize = 4) and (GreenSize = 4) and (AlphaSize = 4) then begin
-          IntFormat := ifRGBA4;
+          IntFormat := tfRGBA4;
           TempIntFormat := GL_BGRA;
           TempType := GL_UNSIGNED_SHORT_4_4_4_4_REV;
         end else
         if (RedSize = 5) and (BlueSize = 5) and (GreenSize = 5) and (AlphaSize = 1) then begin
-          IntFormat := ifRGB5A1;
+          IntFormat := tfRGB5A1;
           TempIntFormat := GL_BGRA;
           TempType := GL_UNSIGNED_SHORT_1_5_5_5_REV;
         end else begin
-          IntFormat := ifRGBA8;
+          IntFormat := tfRGBA8;
         end;
       end;
     GL_BGR:
@@ -7333,15 +6886,14 @@ begin
   GenerateCubeMap(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, TestTextureSize);
 end;
 
-
-
 initialization
-  glBitmapSetDefaultFormat(tfDefault);
+  glBitmapSetDefaultFormat(tfEmpty);
+  glBitmapSetDefaultMipmap(mmMipmap);
   glBitmapSetDefaultFilter(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
-  glBitmapSetDefaultWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
+  glBitmapSetDefaultWrap  (GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
 
-  glBitmapSetDefaultFreeDataAfterGenTexture(True);
-  glBitmapSetDefaultDeleteTextureOnFree(True);
+  glBitmapSetDefaultFreeDataAfterGenTexture(true);
+  glBitmapSetDefaultDeleteTextureOnFree    (true);
 
 finalization