mirror of
https://github.com/DarthAffe/HPPH.git
synced 2025-12-12 13:28:37 +00:00
Reduced allocations on image-conversion if the source iamge is already in the correct color-format
This commit is contained in:
parent
b66fc3c9e9
commit
ca4e7e7af5
@ -21,7 +21,8 @@ public static class ImageExtension
|
|||||||
|
|
||||||
SKBitmap bitmap = new(image.Width, image.Height, SKColorType.Bgra8888, SKAlphaType.Unpremul);
|
SKBitmap bitmap = new(image.Width, image.Height, SKColorType.Bgra8888, SKAlphaType.Unpremul);
|
||||||
nint pixelPtr = bitmap.GetPixels(out nint length);
|
nint pixelPtr = bitmap.GetPixels(out nint length);
|
||||||
image.ConvertTo<ColorBGRA>().CopyTo(new Span<byte>((void*)pixelPtr, (int)length));
|
|
||||||
|
(image as IImage<ColorBGRA> ?? image.ConvertTo<ColorBGRA>()).CopyTo(new Span<byte>((void*)pixelPtr, (int)length));
|
||||||
|
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
@ -32,9 +33,9 @@ public static class ImageExtension
|
|||||||
return skImage.Encode(SKEncodedImageFormat.Png, 100).ToArray();
|
return skImage.Encode(SKEncodedImageFormat.Png, 100).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IImage ToImage(this SKImage skImage) => SKBitmap.FromImage(skImage).ToImage();
|
public static IImage<ColorBGRA> ToImage(this SKImage skImage) => SKBitmap.FromImage(skImage).ToImage();
|
||||||
|
|
||||||
public static IImage ToImage(this SKBitmap bitmap)
|
public static IImage<ColorBGRA> ToImage(this SKBitmap bitmap)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(bitmap, nameof(bitmap));
|
ArgumentNullException.ThrowIfNull(bitmap, nameof(bitmap));
|
||||||
|
|
||||||
|
|||||||
@ -18,10 +18,10 @@ public static class ImageExtension
|
|||||||
Bitmap bitmap = new(image.Width, image.Height, PixelFormat.Format24bppRgb);
|
Bitmap bitmap = new(image.Width, image.Height, PixelFormat.Format24bppRgb);
|
||||||
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
|
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
|
||||||
|
|
||||||
IImage<ColorBGR> convertedImage = image.ConvertTo<ColorBGR>();
|
IImage<ColorBGR> img = image as IImage<ColorBGR> ?? image.ConvertTo<ColorBGR>();
|
||||||
|
|
||||||
nint ptr = bmpData.Scan0;
|
nint ptr = bmpData.Scan0;
|
||||||
foreach (ImageRow<ColorBGR> row in convertedImage.Rows)
|
foreach (ImageRow<ColorBGR> row in img.Rows)
|
||||||
{
|
{
|
||||||
row.CopyTo(new Span<byte>((void*)ptr, bmpData.Stride));
|
row.CopyTo(new Span<byte>((void*)ptr, bmpData.Stride));
|
||||||
ptr += bmpData.Stride;
|
ptr += bmpData.Stride;
|
||||||
@ -37,10 +37,10 @@ public static class ImageExtension
|
|||||||
Bitmap bitmap = new(image.Width, image.Height, PixelFormat.Format32bppArgb);
|
Bitmap bitmap = new(image.Width, image.Height, PixelFormat.Format32bppArgb);
|
||||||
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
|
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
|
||||||
|
|
||||||
IImage<ColorBGRA> convertedImage = image.ConvertTo<ColorBGRA>();
|
IImage<ColorBGRA> img = image as IImage<ColorBGRA> ?? image.ConvertTo<ColorBGRA>();
|
||||||
|
|
||||||
nint ptr = bmpData.Scan0;
|
nint ptr = bmpData.Scan0;
|
||||||
foreach (ImageRow<ColorBGRA> row in convertedImage.Rows)
|
foreach (ImageRow<ColorBGRA> row in img.Rows)
|
||||||
{
|
{
|
||||||
row.CopyTo(new Span<byte>((void*)ptr, bmpData.Stride));
|
row.CopyTo(new Span<byte>((void*)ptr, bmpData.Stride));
|
||||||
ptr += bmpData.Stride;
|
ptr += bmpData.Stride;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user