* Bitfield support for LoadDDS
[LazOpenGLCore.git] / glBitmap.pas
index 88724cf..e1e591d 100644 (file)
@@ -591,7 +591,7 @@ type
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
   TglBitmapFormat = (
-    tfEmpty = 0,
+    tfEmpty = 0, //must be smallest value!
 
     tfAlpha4,
     tfAlpha8,
@@ -645,7 +645,11 @@ type
 
     tfDepth16,
     tfDepth24,
-    tfDepth32
+    tfDepth32,
+
+    tfS3tcDtx1RGBA,
+    tfS3tcDtx3RGBA,
+    tfS3tcDtx5RGBA
   );
 
   TglBitmapFileType = (
@@ -1054,7 +1058,9 @@ type
     fWithAlpha: TglBitmapFormat;
     fWithoutAlpha: TglBitmapFormat;
     fRGBInverted: TglBitmapFormat;
+    fUncompressed: TglBitmapFormat;
     fPixelSize: Single;
+    fIsCompressed: Boolean;
 
     fRange: TglBitmapColorRec;
     fShift: TShiftRec;
@@ -1071,6 +1077,7 @@ type
     property RGBInverted:  TglBitmapFormat read fRGBInverted;
     property Components:   Integer         read GetComponents;
     property PixelSize:    Single          read fPixelSize;
+    property IsCompressed: Boolean         read fIsCompressed;
 
     property glFormat:         Cardinal read fglFormat;
     property glInternalFormat: Cardinal read fglInternalFormat;
@@ -1413,6 +1420,24 @@ type
     constructor Create; override;
   end;
 
+  TfdS3tcDtx1RGBA = class(TFormatDescriptor)
+    procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
+    procedure Unmap(var aData: PByte; var aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
+    constructor Create; override;
+  end;
+
+  TfdS3tcDtx3RGBA = class(TFormatDescriptor)
+    procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
+    procedure Unmap(var aData: PByte; var aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
+    constructor Create; override;
+  end;
+
+  TfdS3tcDtx5RGBA = class(TFormatDescriptor)
+    procedure Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer); override;
+    procedure Unmap(var aData: PByte; var aPixel: TglBitmapPixelData; var aMapData: Pointer); override;
+    constructor Create; override;
+  end;
+
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   TbmpBitfieldFormat = class(TFormatDescriptor)
   private
@@ -1526,7 +1551,11 @@ const
 
     TfdDepth16,
     TfdDepth24,
-    TfdDepth32
+    TfdDepth32,
+
+    TfdS3tcDtx1RGBA,
+    TfdS3tcDtx3RGBA,
+    TfdS3tcDtx5RGBA
   );
 
 var
@@ -1580,8 +1609,6 @@ end;
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 function FormatGetSupportedFiles(const aFormat: TglBitmapFormat): TglBitmapFileTypes;
 begin
-  result := [ftDDS];
-
   if (aFormat in [
         //4 bbp
         tfLuminance4,
@@ -1617,9 +1644,29 @@ begin
         tfRGB10A2, tfRGBA8, tfBGR10A2, tfBGRA8]) then
     result := result + [ftTGA];
 
-  //TODO Supported File Formats!
+  if (aFormat in [
+        //8 bpp
+        tfAlpha8, tfLuminance8, tfLuminance4Alpha4, tfLuminance6Alpha2,
+        tfR3G3B2, tfRGBA2, tfBGRA2,
+
+        //16 bpp
+        tfAlpha16, tfLuminance16, tfLuminance8Alpha8, tfLuminance12Alpha4,
+        tfRGB4, tfR5G6B5, tfRGB5, tfRGBA4, tfRGB5A1,
+        tfBGR4, tfB5G6R5, tfBGR5, tfBGRA4, tfBGR5A1,
+
+        //24 bpp
+        tfRGB8, tfBGR8,
+
+        //32 bbp
+        tfLuminance16Alpha16,
+        tfRGBA8, tfRGB10A2,
+        tfBGRA8, tfBGR10A2,
+
+        //compressed
+        tfS3tcDtx1RGBA, tfS3tcDtx3RGBA, tfS3tcDtx5RGBA]) then
+    result := result + [ftDDS];
 
-  (*
+  (* TODO
   {$IFDEF GLB_SUPPORT_PNG_WRITE}
   if aFormat in [
     tfAlpha4, tfAlpha8, tfAlpha12, tfAlpha16,
@@ -2236,7 +2283,9 @@ begin
   fWithAlpha    := tfEmpty;
   fWithoutAlpha := tfEmpty;
   fRGBInverted  := tfEmpty;
+  fUncompressed := tfEmpty;
   fPixelSize    := 0.0;
+  fIsCompressed := false;
 
   fglFormat         := 0;
   fglInternalFormat := 0;
@@ -3401,6 +3450,84 @@ begin
 end;
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//TfdS3tcDtx1RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TfdS3tcDtx1RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
+begin
+  raise EglBitmapException.Create('mapping for compressed formats is not supported');
+end;
+
+procedure TfdS3tcDtx1RGBA.Unmap(var aData: PByte; var aPixel: TglBitmapPixelData; var aMapData: Pointer);
+begin
+  raise EglBitmapException.Create('mapping for compressed formats is not supported');
+end;
+
+constructor TfdS3tcDtx1RGBA.Create;
+begin
+  inherited Create;
+  fFormat           := tfS3tcDtx1RGBA;
+  fWithAlpha        := tfS3tcDtx1RGBA;
+  fUncompressed     := tfRGB5A1;
+  fPixelSize        := 0.5;
+  fIsCompressed     := true;
+  fglFormat         := GL_COMPRESSED_RGBA;
+  fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
+  fglDataFormat     := GL_UNSIGNED_BYTE;
+end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//TfdS3tcDtx3RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TfdS3tcDtx3RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
+begin
+  raise EglBitmapException.Create('mapping for compressed formats is not supported');
+end;
+
+procedure TfdS3tcDtx3RGBA.Unmap(var aData: PByte; var aPixel: TglBitmapPixelData; var aMapData: Pointer);
+begin
+  raise EglBitmapException.Create('mapping for compressed formats is not supported');
+end;
+
+constructor TfdS3tcDtx3RGBA.Create;
+begin
+  inherited Create;
+  fFormat           := tfS3tcDtx3RGBA;
+  fWithAlpha        := tfS3tcDtx3RGBA;
+  fUncompressed     := tfRGBA8;
+  fPixelSize        := 1.0;
+  fIsCompressed     := true;
+  fglFormat         := GL_COMPRESSED_RGBA;
+  fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
+  fglDataFormat     := GL_UNSIGNED_BYTE;
+end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//TfdS3tcDtx5RGBA/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TfdS3tcDtx5RGBA.Map(const aPixel: TglBitmapPixelData; var aData: PByte; var aMapData: Pointer);
+begin
+  raise EglBitmapException.Create('mapping for compressed formats is not supported');
+end;
+
+procedure TfdS3tcDtx5RGBA.Unmap(var aData: PByte; var aPixel: TglBitmapPixelData; var aMapData: Pointer);
+begin
+  raise EglBitmapException.Create('mapping for compressed formats is not supported');
+end;
+
+constructor TfdS3tcDtx5RGBA.Create;
+begin
+  inherited Create;
+  fFormat           := tfS3tcDtx3RGBA;
+  fWithAlpha        := tfS3tcDtx3RGBA;
+  fUncompressed     := tfRGBA8;
+  fPixelSize        := 1.0;
+  fIsCompressed     := true;
+  fglFormat         := GL_COMPRESSED_RGBA;
+  fglInternalFormat := GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
+  fglDataFormat     := GL_UNSIGNED_BYTE;
+end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //TFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 class procedure TFormatDescriptor.Init;
@@ -6865,7 +6992,7 @@ end;
 //DDS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 const
-  DDS_MAGIC                   = $20534444;
+  DDS_MAGIC: Cardinal         = $20534444;
 
   // DDS_header.dwFlags
   DDSD_CAPS                   = $00000001;
@@ -6879,9 +7006,11 @@ const
 
   // DDS_header.sPixelFormat.dwFlags
   DDPF_ALPHAPIXELS            = $00000001;
+  DDPF_ALPHA                  = $00000002;
   DDPF_FOURCC                 = $00000004;
   DDPF_INDEXED                = $00000020;
   DDPF_RGB                    = $00000040;
+  DDPF_LUMINANCE              = $00020000;
 
   // DDS_header.sCaps.dwCaps1
   DDSCAPS_COMPLEX             = $00000008;
@@ -6922,7 +7051,6 @@ type
   end;
 
   TDDSHeader = packed record
-    dwMagic: Cardinal;
     dwSize: Cardinal;
     dwFlags: Cardinal;
     dwHeight: Cardinal;
@@ -6940,128 +7068,176 @@ type
 function TglBitmap.LoadDDS(const aStream: TStream): Boolean;
 var
   Header: TDDSHeader;
-  StreamPos: Int64;
-  Y, LineSize: Cardinal;
-  RowSize: Cardinal;
-  NewImage, pData: pByte;
-  ddsFormat: TglBitmapFormat;
-
-  function RaiseEx : Exception;
-  begin
-    result := EglBitmapException.Create('LoadDDS - unsupported Pixelformat found.');
-  end;
+  Converter: TbmpBitfieldFormat;
 
   function GetDDSFormat: TglBitmapFormat;
+  var
+    fd: TFormatDescriptor;
+    i: Integer;
+    Range: TglBitmapColorRec;
+    match: Boolean;
   begin
+    result := tfEmpty;
     with Header.PixelFormat do begin
       // Compresses
-      if (dwFlags and DDPF_FOURCC) > 0 then begin
-        (* TODO
+      if ((dwFlags and DDPF_FOURCC) > 0) then begin
         case Header.PixelFormat.dwFourCC of
-          D3DFMT_DXT1: result := ifDXT1;
-          D3DFMT_DXT3: result := ifDXT3;
-          D3DFMT_DXT5: result := ifDXT5;
-        else
-          raise RaiseEx;
+          D3DFMT_DXT1: result := tfS3tcDtx1RGBA;
+          D3DFMT_DXT3: result := tfS3tcDtx3RGBA;
+          D3DFMT_DXT5: result := tfS3tcDtx5RGBA;
+        end;
+      end else if ((Header.PixelFormat.dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE)) > 0) then begin
+
+        //find matching format
+        for result := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
+          fd := TFormatDescriptor.Get(result);
+          if fd.MaskMatch(dwRBitMask, dwGBitMask, dwBBitMask, dwABitMask) and
+             (8 * fd.PixelSize = dwRGBBitCount) then
+            exit;
         end;
-        *)
-        raise RaiseEx;
-      end else
 
-      // RGB
-      if (dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS)) > 0 then begin
-        case dwRGBBitCount of
-          8: begin
-            (* TODO if dwFlags and DDPF_ALPHAPIXELS > 0 then
-              result := tfAlpha
-            else
-            *)
-              result := tfLuminance8;
-          end;
-          16: begin
-            if dwFlags and DDPF_ALPHAPIXELS > 0 then begin
-              // Alpha
-              case CountSetBits(dwRBitMask) of
-                5: result := tfRGB5A1;
-                //TODO 4: result := tfRGBA4;
-              else
-                result := tfLuminance8Alpha8;
-              end;
-            end else begin
-              // no Alpha
-              //TODO result := ifR5G6B5;
-              raise RaiseEx;
+        //find format with same Range
+        Range.r := dwRBitMask;
+        Range.g := dwGBitMask;
+        Range.b := dwBBitMask;
+        Range.a := dwABitMask;
+        for i := 0 to 3 do begin
+          while ((Range.arr[i] and 1) = 0) and (Range.arr[i] > 0) do
+            Range.arr[i] := Range.arr[i] shr 1;
+        end;
+        for result := High(TglBitmapFormat) downto Low(TglBitmapFormat) do begin
+          fd := TFormatDescriptor.Get(result);
+          match := true;
+          for i := 0 to 3 do
+            if (fd.Range.arr[i] <> Range.arr[i]) then begin
+              match := false;
+              break;
             end;
-          end;
-          24: begin
-            if dwRBitMask > dwBBitMask then
-              result := tfBGR8
-            else
-              result := tfRGB8;
-          end;
-        32: begin
-            if CountSetBits(dwRBitMask) = 10 then
-              //TODO result := tfRGB10A2
-              raise RaiseEx
-            else
+          if match then
+            break;
+        end;
 
-            if dwRBitMask > dwBBitMask then
-              result := tfBGRA8
-            else
-              result := tfRGBA8;
-          end;
-        else
-          raise RaiseEx;
+        //no format with same range found -> use default
+        if (result = tfEmpty) then begin
+          if (dwABitMask > 0) then
+            result := tfBGRA8
+          else
+            result := tfBGR8;
         end;
-      end else
-        raise RaiseEx;
+
+        Converter := TbmpBitfieldFormat.Create;
+        Converter.RedMask   := dwRBitMask;
+        Converter.GreenMask := dwGBitMask;
+        Converter.BlueMask  := dwBBitMask;
+        Converter.AlphaMask := dwABitMask;
+        Converter.PixelSize := dwRGBBitCount / 8;
+      end;
     end;
   end;
 
-begin
-  result := false;
+var
+  StreamPos: Int64;
+  x, y, j, LineSize, RowSize, Magic: Cardinal;
+  NewImage, TmpData, RowData, SrcData: PByte;
+  SourceMD, DestMD: Pointer;
+  Pixel: TglBitmapPixelData;
+  ddsFormat: TglBitmapFormat;
+  FormatDesc: TFormatDescriptor;
 
-  // Header
+begin
+  result    := false;
+  Converter := nil;
   StreamPos := aStream.Position;
-  aStream.Read(Header, sizeof(Header));
 
-  if ((Header.dwMagic <> DDS_MAGIC) or (Header.dwSize <> 124) or
-     ((Header.dwFlags and DDSD_PIXELFORMAT) = 0) or ((Header.dwFlags and DDSD_CAPS) = 0)) then begin
+  // Magic
+  aStream.Read(Magic, sizeof(Magic));
+  if (Magic <> DDS_MAGIC) then begin
+    aStream.Position := StreamPos;
+    exit;
+  end;
+
+  //Header
+  aStream.Read(Header, sizeof(Header));
+  if (Header.dwSize <> SizeOf(Header)) or
+     ((Header.dwFlags and (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) <>
+        (DDSD_PIXELFORMAT or DDSD_CAPS or DDSD_WIDTH or DDSD_HEIGHT)) then
+  begin
     aStream.Position := StreamPos;
     exit;
   end;
 
+  if ((Header.Caps.dwCaps1 and DDSCAPS2_CUBEMAP) > 0) then
+    raise EglBitmapException.Create('LoadDDS - CubeMaps are not supported');
+
   ddsFormat := GetDDSFormat;
-  LineSize  := Trunc(Header.dwWidth * TFormatDescriptor.Get(ddsFormat).PixelSize);
-  GetMem(NewImage, Header.dwHeight * LineSize);
   try
-    pData := NewImage;
-
-    // Compressed
-    if (Header.PixelFormat.dwFlags and DDPF_FOURCC) > 0 then begin
-      RowSize := Header.dwPitchOrLinearSize div Header.dwWidth;
-      for Y := 0 to Header.dwHeight -1 do begin
-        aStream.Read(pData^, RowSize);
-        Inc(pData, LineSize);
-      end;
-    end else
+    if (ddsFormat = tfEmpty) then
+      raise EglBitmapException.Create('LoadDDS - unsupported Pixelformat found.');
+
+    FormatDesc := TFormatDescriptor.Get(ddsFormat);
+    LineSize   := Trunc(Header.dwWidth * FormatDesc.PixelSize);
+    GetMem(NewImage, Header.dwHeight * LineSize);
+    try
+      TmpData := NewImage;
+
+      //Converter needed
+      if Assigned(Converter) then begin
+        RowSize := Round(Header.dwWidth * Header.PixelFormat.dwRGBBitCount / 8);
+        GetMem(RowData, RowSize);
+        SourceMD := Converter.CreateMappingData;
+        DestMD   := FormatDesc.CreateMappingData;
+        try
+          for y := 0 to Header.dwHeight-1 do begin
+            TmpData := NewImage + y * LineSize;
+            SrcData := RowData;
+            aStream.Read(SrcData^, RowSize);
+            for x := 0 to Header.dwWidth-1 do begin
+              Converter.Unmap(SrcData, Pixel, SourceMD);
+              //TODO use converter function
+              for j := 0 to 3 do
+                if (Converter.Range.arr[j] <> FormatDesc.Range.arr[j]) then begin
+                  if (Converter.Range.arr[j] > 0) then
+                    Pixel.Data.arr[j] := Round(Pixel.Data.arr[j] / Converter.Range.arr[j] * FormatDesc.Range.arr[j])
+                  else
+                    Pixel.Data.arr[j] := 0;
+                end;
+              FormatDesc.Map(Pixel, TmpData, DestMD);
+            end;
+          end;
+        finally
+          Converter.FreeMappingData(SourceMD);
+          FormatDesc.FreeMappingData(DestMD);
+          FreeMem(RowData);
+        end;
+      end else
 
-    // RGB(A)
-    if (Header.PixelFormat.dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS)) > 0 then begin
-      RowSize := Header.dwPitchOrLinearSize;
+      // Compressed
+      if ((Header.PixelFormat.dwFlags and DDPF_FOURCC) > 0) then begin
+        RowSize := Header.dwPitchOrLinearSize div Header.dwWidth;
+        for Y := 0 to Header.dwHeight-1 do begin
+          aStream.Read(TmpData^, RowSize);
+          Inc(TmpData, LineSize);
+        end;
+      end else
 
-      for Y := 0 to Header.dwHeight -1 do begin
-        aStream.Read(pData^, RowSize);
-        Inc(pData, LineSize);
-      end;
-    end else
-      raise RaiseEx;
+      // Uncompressed
+      if (Header.PixelFormat.dwFlags and (DDPF_RGB or DDPF_ALPHAPIXELS or DDPF_LUMINANCE)) > 0 then begin
+        RowSize := (Header.PixelFormat.dwRGBBitCount * Header.dwWidth) shr 3;
+        for Y := 0 to Header.dwHeight-1 do begin
+          aStream.Read(TmpData^, RowSize);
+          Inc(TmpData, LineSize);
+        end;
+      end else
+        raise EglBitmapException.Create('LoadDDS - unsupported Pixelformat found.');
 
-    SetDataPointer(NewImage, ddsFormat, Header.dwWidth, Header.dwHeight);
-    result := true;
-  except
-    FreeMem(NewImage);
-    raise;
+      SetDataPointer(NewImage, ddsFormat, Header.dwWidth, Header.dwHeight);
+      result := true;
+    except
+      FreeMem(NewImage);
+      raise;
+    end;
+  finally
+    FreeAndNil(Converter);
   end;
 end;
 
@@ -7069,55 +7245,55 @@ end;
 procedure TglBitmap.SaveDDS(const aStream: TStream);
 var
   Header: TDDSHeader;
-  Pix: TglBitmapPixelData;
   FormatDesc: TFormatDescriptor;
 begin
-  //if not FormatIsUncompressed(InternalFormat) then
-  //  raise EglBitmapUnsupportedFormatFormat.Create('SaveDDS - ' + UNSUPPORTED_FORMAT);
+  if not (ftDDS in FormatGetSupportedFiles(Format)) then
+    raise EglBitmapUnsupportedFormatFormat.Create('SaveDDS - ' + UNSUPPORTED_FORMAT);
 
-  (* TODO if Format = tfAlpha8 then
-    FORMAT_DESCRIPTORS[tfLuminance8].PreparePixel(Pix);
-  else    *)
-    TFormatDescriptor.Get(Format).PreparePixel(Pix);
+  FormatDesc := TFormatDescriptor.Get(Format);
 
   // Generell
   FillChar(Header, SizeOf(Header), 0);
-  Header.dwMagic := DDS_MAGIC;
-  Header.dwSize  := 124;
-  Header.dwFlags := DDSD_PITCH or DDSD_CAPS or DDSD_PIXELFORMAT;
-
-  if Width > 0 then begin
-    Header.dwWidth := Width;
-    Header.dwFlags := Header.dwFlags or DDSD_WIDTH;
-  end;
-
-  if Height > 0 then begin
-    Header.dwHeight := Height;
-    Header.dwFlags := Header.dwFlags or DDSD_HEIGHT;
-  end;
+  Header.dwSize  := SizeOf(Header);
+  Header.dwFlags := DDSD_WIDTH or DDSD_HEIGHT or DDSD_CAPS or DDSD_PIXELFORMAT;
 
-  Header.dwPitchOrLinearSize := fRowSize;
-  Header.dwMipMapCount := 1;
+  Header.dwWidth  := Max(1, Width);
+  Header.dwHeight := Max(1, Height);
 
   // Caps
   Header.Caps.dwCaps1 := DDSCAPS_TEXTURE;
 
   // Pixelformat
-  Header.PixelFormat.dwSize  := Sizeof(Header.PixelFormat);
-  Header.PixelFormat.dwFlags := DDPF_RGB;
+  Header.PixelFormat.dwSize := sizeof(Header);
+  if (FormatDesc.IsCompressed) then begin
+    Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_FOURCC;
+    case Format of
+      tfS3tcDtx1RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT1;
+      tfS3tcDtx3RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT3;
+      tfS3tcDtx5RGBA: Header.PixelFormat.dwFourCC := D3DFMT_DXT5;
+    end;
+  end else if (Format in [tfAlpha8, tfAlpha16]) then begin
+    Header.PixelFormat.dwFlags       := Header.PixelFormat.dwFlags or DDPF_ALPHA;
+    Header.PixelFormat.dwRGBBitCount := Round(FormatDesc.PixelSize * 8);
+    Header.PixelFormat.dwABitMask    := FormatDesc.AlphaMask;
+  end else if (FormatDesc.RedMask = FormatDesc.GreenMask) and (FormatDesc.GreenMask = FormatDesc.BlueMask) then begin
+    Header.PixelFormat.dwFlags       := Header.PixelFormat.dwFlags or DDPF_LUMINANCE;
+    Header.PixelFormat.dwRGBBitCount := Round(FormatDesc.PixelSize * 8);
+    Header.PixelFormat.dwRBitMask    := FormatDesc.RedMask;
+    Header.PixelFormat.dwABitMask    := FormatDesc.AlphaMask;
+  end else begin
+    Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_RGB;
+    Header.PixelFormat.dwRGBBitCount := Round(FormatDesc.PixelSize * 8);
+    Header.PixelFormat.dwRBitMask    := FormatDesc.RedMask;
+    Header.PixelFormat.dwGBitMask    := FormatDesc.GreenMask;
+    Header.PixelFormat.dwBBitMask    := FormatDesc.BlueMask;
+    Header.PixelFormat.dwABitMask    := FormatDesc.AlphaMask;
+  end;
 
-  (* TODO tfAlpha8
-  if FORMAT_DESCRIPTORS[Format].HasAlpha and (Format <> tfAlpha8) then
+  if (FormatDesc.HasAlpha) then
     Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHAPIXELS;
-  *)
-
-  FormatDesc := TFormatDescriptor.Get(Format);
-  Header.PixelFormat.dwRGBBitCount  := Trunc(FormatDesc.PixelSize * 8);
-  Header.PixelFormat.dwRBitMask     := FormatDesc.RedMask;
-  Header.PixelFormat.dwGBitMask     := FormatDesc.GreenMask;
-  Header.PixelFormat.dwBBitMask     := FormatDesc.BlueMask;
-  Header.PixelFormat.dwABitMask := FormatDesc.AlphaMask;
 
+  aStream.Write(DDS_MAGIC, sizeof(DDS_MAGIC));
   aStream.Write(Header, SizeOf(Header));
   aStream.Write(Data^, FormatDesc.GetSize(Dimension));
 end;
@@ -7141,8 +7317,7 @@ var
 begin
   inherited SetDataPointer(aData, aFormat, aWidth, aHeight);
 
-  //TODO compression
-  if {FormatIsUncompressed(Format)} true then begin
+  if not TFormatDescriptor.Get(aFormat).IsCompressed then begin
     (* TODO PixelFuncs
     fGetPixelFunc := GetPixel2DUnmap;
     fSetPixelFunc := SetPixel2DUnmap;
@@ -7159,9 +7334,8 @@ begin
     end
       else SetLength(fLines, 0);
   end else begin
-    (*
     SetLength(fLines, 0);
-
+    (*
     fSetPixelFunc := nil;
 
     case Format of
@@ -7185,19 +7359,16 @@ var
 begin
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 
-  (* TODO compression
-  if Self.InternalFormat in [ifDXT1, ifDXT3, ifDXT5] then
-    glCompressedTexImage2D(Target, 0, InternalFormat, Width, Height, 0, Trunc(Width * Height * FormatGetSize(Self.InternalFormat)), Data)
-  else
-  *)
-
   FormatDesc := TFormatDescriptor.Get(Format);
-  if aBuildWithGlu then
+  if FormatDesc.IsCompressed then begin
+    glCompressedTexImage2D(Target, 0, FormatDesc.glInternalFormat, Width, Height, 0, FormatDesc.GetSize(fDimension), Data)
+  end else if aBuildWithGlu then begin
     gluBuild2DMipmaps(aTarget, FormatDesc.Components, Width, Height,
       FormatDesc.glFormat, FormatDesc.glDataFormat, Data)
-  else
+  end else begin
     glTexImage2D(aTarget, 0, FormatDesc.glInternalFormat, Width, Height, 0,
       FormatDesc.glFormat, FormatDesc.glDataFormat, Data);
+  end;
 
   // Freigeben
   if (FreeDataAfterGenTexture) then
@@ -7218,15 +7389,13 @@ var
   Size, w, h: Integer;
   FormatDesc: TFormatDescriptor;
 begin
-  (* TODO compression
-  if not FormatIsUncompressed(Format) then
+  FormatDesc := TFormatDescriptor.Get(Format);
+  if FormatDesc.IsCompressed then
     raise EglBitmapUnsupportedFormatFormat.Create('TglBitmap2D.GrabScreen - ' + UNSUPPORTED_FORMAT);
-  *)
 
-  w := aRight  - aLeft;
-  h := aBottom - aTop;
-  FormatDesc   := TFormatDescriptor.Get(Format);
-  Size         := FormatDesc.GetSize(w, h);
+  w    := aRight  - aLeft;
+  h    := aBottom - aTop;
+  Size := FormatDesc.GetSize(w, h);
   GetMem(Temp, Size);
   try
     glPixelStorei(GL_PACK_ALIGNMENT, 1);
@@ -7266,12 +7435,10 @@ begin
   FormatDesc := TFormatDescriptor.Get(IntFormat);
   GetMem(Temp, FormatDesc.GetSize(TempWidth, TempHeight));
   try
-    (* TODO Compression
-    if FormatIsCompressed(IntFormat) and (GL_VERSION_1_3 or GL_ARB_texture_compression) then
+    if FormatDesc.IsCompressed then
       glGetCompressedTexImage(Target, 0, Temp)
     else
-    *)
-    glGetTexImage(Target, 0, FormatDesc.glInternalFormat, FormatDesc.glDataFormat, Temp);
+     glGetTexImage(Target, 0, FormatDesc.glInternalFormat, FormatDesc.glDataFormat, Temp);
     SetDataPointer(Temp, IntFormat, TempWidth, TempHeight);
   except
     FreeMem(Temp);