Added extensions for SkiaSharp

This commit is contained in:
Darth Affe 2024-07-15 00:00:58 +02:00
parent 2c401f0eb1
commit 5126980e97
5 changed files with 76 additions and 1 deletions

View File

@ -13,6 +13,7 @@
<ItemGroup>
<ProjectReference Include="..\HPPH.Reference\HPPH.Reference.csproj" />
<ProjectReference Include="..\HPPH.SkiaSharp\HPPH.SkiaSharp.csproj" />
<ProjectReference Include="..\HPPH.System.Drawing\HPPH.System.Drawing.csproj" />
<ProjectReference Include="..\HPPH\HPPH.csproj" />
</ItemGroup>

View File

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.88.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HPPH\HPPH.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,32 @@
// ReSharper disable InconsistentNaming
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using SkiaSharp;
namespace HPPH.SkiaSharp;
public static class ImageExtension
{
public static unsafe SKImage ToSKImage(this IImage image) => SKImage.FromBitmap(image.ToSKBitmap());
public static unsafe SKBitmap ToSKBitmap(this IImage image)
{
SKBitmap bitmap = new(image.Width, image.Height, SKColorType.Bgra8888, SKAlphaType.Unpremul);
nint pixelPtr = bitmap.GetPixels(out nint length);
image.ConvertTo<ColorBGRA>().CopyTo(new Span<byte>((void*)pixelPtr, (int)length));
return bitmap;
}
public static byte[] ToPng(this IImage image)
{
using SKImage skImage = image.ToSKImage();
return skImage.Encode(SKEncodedImageFormat.Png, 100).ToArray();
}
public static IImage ToImage(this SKImage skImage) => SKBitmap.FromImage(skImage).ToImage();
public static IImage ToImage(this SKBitmap bitmap)
=> Image<ColorBGRA>.Create(MemoryMarshal.Cast<SKColor, ColorBGRA>(bitmap.Pixels), bitmap.Width, bitmap.Height);
}

View File

@ -0,0 +1,18 @@
using SkiaSharp;
namespace HPPH.SkiaSharp;
public static class ImageHelper
{
public static IImage LoadImage(string path)
{
using SKImage image = SKImage.FromEncodedData(path);
return image.ToImage();
}
public static IImage LoadImage(Stream stream)
{
using SKImage image = SKImage.FromEncodedData(stream);
return image.ToImage();
}
}

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HPPH.Reference", "HPPH.Refe
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HPPH.Generators", "HPPH.Generators\HPPH.Generators.csproj", "{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HPPH.System.Drawing", "HPPH.System.Drawing\HPPH.System.Drawing.csproj", "{16EC37E4-3EF1-47AF-B257-465334B36571}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HPPH.System.Drawing", "HPPH.System.Drawing\HPPH.System.Drawing.csproj", "{16EC37E4-3EF1-47AF-B257-465334B36571}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HPPH.SkiaSharp", "HPPH.SkiaSharp\HPPH.SkiaSharp.csproj", "{E70CD72D-1A41-413E-B0C6-24958733A773}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{16EC37E4-3EF1-47AF-B257-465334B36571}.Debug|Any CPU.Build.0 = Debug|Any CPU
{16EC37E4-3EF1-47AF-B257-465334B36571}.Release|Any CPU.ActiveCfg = Release|Any CPU
{16EC37E4-3EF1-47AF-B257-465334B36571}.Release|Any CPU.Build.0 = Release|Any CPU
{E70CD72D-1A41-413E-B0C6-24958733A773}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E70CD72D-1A41-413E-B0C6-24958733A773}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E70CD72D-1A41-413E-B0C6-24958733A773}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E70CD72D-1A41-413E-B0C6-24958733A773}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE