function TglBitmapData.LoadPNG(const aStream: TStream): Boolean;
var
StreamPos: Int64;
Png: TPNGObject;
Header: String[8];
Row, Col, PixSize, LineSize: Integer;
NewImage, pSource, pDest, pAlpha: pByte;
PngFormat: TglBitmapFormat;
FormatDesc: TFormatDescriptor;
const
PngHeader: String[8] = #137#80#78#71#13#10#26#10;
begin
result := false;
StreamPos := aStream.Position;
aStream.Read(Header[0], SizeOf(Header));
aStream.Position := StreamPos;
{Test if the header matches}
if Header = PngHeader then begin
Png := TPNGObject.Create;
try
Png.LoadFromStream(aStream);
case Png.Header.ColorType of
COLOR_GRAYSCALE:
PngFormat := tfLuminance8ub1;
// COLOR_GRAYSCALEALPHA:
// PngFormat := tfLuminance8Alpha8us1; <= Das Format gibt es nicht
COLOR_RGB:
PngFormat := tfBGR8ub3;
COLOR_RGBALPHA:
PngFormat := tfBGRA8ub4;
// else
// raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.'); <= das gibt es nicht
end;
FormatDesc := TFormatDescriptor.Get(PngFormat);
// PixSize := Round(FormatDesc.PixelSize); <= PixelSize gibt es nicht
LineSize := FormatDesc.GetSize(Png.Header.Width, 1);
GetMem(NewImage, LineSize * Integer(Png.Header.Height));
try
pDest := NewImage;
case Png.Header.ColorType of
COLOR_RGB, COLOR_GRAYSCALE:
begin
for Row := 0 to Png.Height -1 do begin
Move (Png.Scanline[Row]^, pDest^, LineSize);
Inc(pDest, LineSize);
end;
end;
COLOR_RGBALPHA, COLOR_GRAYSCALEALPHA:
begin
PixSize := PixSize -1;
for Row := 0 to Png.Height -1 do begin
pSource := Png.Scanline[Row];
pAlpha := pByte(Png.AlphaScanline[Row]);
for Col := 0 to Png.Width -1 do begin
Move (pSource^, pDest^, PixSize);
Inc(pSource, PixSize);
Inc(pDest, PixSize);
pDest^ := pAlpha^;
inc(pAlpha);
Inc(pDest);
end;
end;
end;
else
// raise EglBitmapException.Create ('LoadPng - Unsupported Colortype found.'); <= das gibt es auch nicht
end;
SetData(NewImage, PngFormat, Png.Header.Width, Png.Header.Height);
result := true;
except
if Assigned(NewImage) then
FreeMem(NewImage);
raise;
end;
finally
Png.Free;
end;
end;
end;