+ if (ddsFormat = tfEmpty) then
+ raise EglBitmap.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;
+ inc(TmpData, y * LineSize);
+ SrcData := RowData;
+ aStream.Read(SrcData^, RowSize);
+ for x := 0 to Header.dwWidth-1 do begin
+ Converter.Unmap(SrcData, Pixel, SourceMD);
+ glBitmapConvertPixel(Pixel, Converter, FormatDesc);
+ FormatDesc.Map(Pixel, TmpData, DestMD);
+ end;
+ end;
+ finally
+ Converter.FreeMappingData(SourceMD);
+ FormatDesc.FreeMappingData(DestMD);
+ FreeMem(RowData);
+ end;
+ end else
+
+ // 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
+
+ // 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 EglBitmap.Create('LoadDDS - unsupported Pixelformat found.');
+
+ SetDataPointer(NewImage, ddsFormat, Header.dwWidth, Header.dwHeight); //be careful, Data could be freed by this method
+ result := true;
+ except
+ if Assigned(NewImage) then
+ FreeMem(NewImage);
+ raise;
+ end;
+ finally
+ FreeAndNil(Converter);
+ end;
+end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+procedure TglBitmap.SaveDDS(const aStream: TStream);
+var
+ Header: TDDSHeader;
+ FormatDesc: TFormatDescriptor;
+begin
+ if not (ftDDS in FormatGetSupportedFiles(Format)) then
+ raise EglBitmapUnsupportedFormat.Create(Format);
+
+ FormatDesc := TFormatDescriptor.Get(Format);
+
+ // Generell
+ FillChar(Header{%H-}, SizeOf(Header), 0);
+ Header.dwSize := SizeOf(Header);
+ Header.dwFlags := DDSD_WIDTH or DDSD_HEIGHT or DDSD_CAPS or DDSD_PIXELFORMAT;
+
+ Header.dwWidth := Max(1, Width);
+ Header.dwHeight := Max(1, Height);
+
+ // Caps
+ Header.Caps.dwCaps1 := DDSCAPS_TEXTURE;
+
+ // Pixelformat
+ 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;
+
+ if (FormatDesc.HasAlpha) then
+ Header.PixelFormat.dwFlags := Header.PixelFormat.dwFlags or DDPF_ALPHAPIXELS;
+
+ aStream.Write(DDS_MAGIC, sizeof(DDS_MAGIC));
+ aStream.Write(Header, SizeOf(Header));
+ aStream.Write(Data^, FormatDesc.GetSize(Dimension));
+end;