mirror of
https://github.com/DarthAffe/HPPH.git
synced 2025-12-12 13:28:37 +00:00
Compare commits
6 Commits
dd5f742b48
...
5126980e97
| Author | SHA1 | Date | |
|---|---|---|---|
| 5126980e97 | |||
| 2c401f0eb1 | |||
| 0c7770d42d | |||
| 8fd2ee7091 | |||
| 0a731b5ca4 | |||
| 6f762f7ad6 |
@ -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>
|
||||
|
||||
18
HPPH.SkiaSharp/HPPH.SkiaSharp.csproj
Normal file
18
HPPH.SkiaSharp/HPPH.SkiaSharp.csproj
Normal 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>
|
||||
32
HPPH.SkiaSharp/ImageExtension.cs
Normal file
32
HPPH.SkiaSharp/ImageExtension.cs
Normal 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);
|
||||
}
|
||||
18
HPPH.SkiaSharp/ImageHelper.cs
Normal file
18
HPPH.SkiaSharp/ImageHelper.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
@ -8,7 +8,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Drawing.Common" Version="8.0.6" />
|
||||
<PackageReference Include="System.Drawing.Common" Version="8.0.7" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@ -6,9 +6,62 @@ namespace HPPH.System.Drawing;
|
||||
|
||||
public static class ImageExtension
|
||||
{
|
||||
public static Bitmap ToBitmap(this IImage image)
|
||||
[SupportedOSPlatform("windows")]
|
||||
public static unsafe Bitmap ToBitmap(this IImage image)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
switch (image.ColorFormat.BytesPerPixel)
|
||||
{
|
||||
case 3:
|
||||
{
|
||||
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);
|
||||
|
||||
IImage<ColorBGR> convertedImage = image.ConvertTo<ColorBGR>();
|
||||
|
||||
nint ptr = bmpData.Scan0;
|
||||
foreach (ImageRow<ColorBGR> row in convertedImage.Rows)
|
||||
{
|
||||
row.CopyTo(new Span<byte>((void*)ptr, bmpData.Stride));
|
||||
ptr += bmpData.Stride;
|
||||
}
|
||||
|
||||
bitmap.UnlockBits(bmpData);
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
case 4:
|
||||
{
|
||||
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);
|
||||
|
||||
IImage<ColorBGRA> convertedImage = image.ConvertTo<ColorBGRA>();
|
||||
|
||||
nint ptr = bmpData.Scan0;
|
||||
foreach (ImageRow<ColorBGRA> row in convertedImage.Rows)
|
||||
{
|
||||
row.CopyTo(new Span<byte>((void*)ptr, bmpData.Stride));
|
||||
ptr += bmpData.Stride;
|
||||
}
|
||||
|
||||
bitmap.UnlockBits(bmpData);
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
default:
|
||||
throw new NotSupportedException($"Unsupported color format '{image.ColorFormat}'.");
|
||||
}
|
||||
}
|
||||
|
||||
[SupportedOSPlatform("windows")]
|
||||
public static byte[] ToPng(this IImage image)
|
||||
{
|
||||
using Bitmap bitmap = ToBitmap(image);
|
||||
using MemoryStream ms = new();
|
||||
bitmap.Save(ms, ImageFormat.Png);
|
||||
|
||||
return ms.ToArray();
|
||||
}
|
||||
|
||||
[SupportedOSPlatform("windows")]
|
||||
@ -20,9 +73,9 @@ public static class ImageExtension
|
||||
IImage image;
|
||||
|
||||
if (data.PixelFormat.HasFlag(PixelFormat.Format24bppRgb))
|
||||
image = Image<ColorRGB>.Create(buffer, data.Width, data.Height, data.Stride);
|
||||
image = Image<ColorBGR>.Create(buffer, data.Width, data.Height, data.Stride);
|
||||
else if (data.PixelFormat.HasFlag(PixelFormat.Format32bppArgb))
|
||||
image = Image<ColorARGB>.Create(buffer, data.Width, data.Height, data.Stride);
|
||||
image = Image<ColorBGRA>.Create(buffer, data.Width, data.Height, data.Stride);
|
||||
else throw new NotSupportedException($"Unsupported pixel format '{bitmap.PixelFormat}'.");
|
||||
|
||||
bitmap.UnlockBits(data);
|
||||
|
||||
8
HPPH.sln
8
HPPH.sln
@ -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
|
||||
|
||||
@ -74,6 +74,12 @@ public static unsafe partial class PixelHelper
|
||||
{
|
||||
if (source.Length == 0) return;
|
||||
|
||||
if (sourceFormat == targetFormat)
|
||||
{
|
||||
source.CopyTo(target);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (sourceFormat.BytesPerPixel)
|
||||
{
|
||||
case 3 when (targetFormat.BytesPerPixel == 3):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user