Created
June 23, 2022 12:36
-
-
Save viniciusfbb/c244a47164a398cc1a21c00cca20654b to your computer and use it in GitHub Desktop.
SkImage resize with Skia4Delphi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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