Skip to content

Instantly share code, notes, and snippets.

@viniciusfbb
Created June 23, 2022 12:36
Show Gist options
  • Save viniciusfbb/c244a47164a398cc1a21c00cca20654b to your computer and use it in GitHub Desktop.
Save viniciusfbb/c244a47164a398cc1a21c00cca20654b to your computer and use it in GitHub Desktop.
SkImage resize with Skia4Delphi
function ImageResize(AImage: ISkImage; const ANewWidth, ANewHeight: Integer): ISkImage;
var
LPixels: Pointer;
LImageInfo: TSkImageInfo;
begin
Assert(Assigned(AImage));
if (ANewWidth <= 0) or (ANewHeight <= 0) then
Exit(nil);
if (AImage.Width = ANewWidth) and (AImage.Height = ANewHeight) then
Exit(AImage);
LImageInfo := TSkImageInfo.Create(ANewWidth, ANewHeight, AImage.ColorType, AImage.AlphaType, AImage.ColorSpace);
GetMem(LPixels, NativeInt(LImageInfo.MinRowBytes) * LImageInfo.Height);
try
if not AImage.ScalePixels(LImageInfo, LPixels, LImageInfo.MinRowBytes, TSkSamplingOptions.High) then
raise Exception.Create('Image scale failed');
Result := TSkImage.MakeFromRaster(LImageInfo, LPixels, LImageInfo.MinRowBytes,
procedure (const APixels: Pointer)
begin
FreeMem(LPixels);
end);
except
FreeMem(LPixels);
raise;
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment