+ 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
+
+ // 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 EglBitmapException.Create('LoadDDS - unsupported Pixelformat found.');
+
+ SetDataPointer(NewImage, ddsFormat, Header.dwWidth, Header.dwHeight);
+ result := true;
+ except
+ FreeMem(NewImage);
+ raise;
+ end;
+ finally
+ FreeAndNil(Converter);