mirror of
https://github.com/DarthAffe/StableDiffusion.NET.git
synced 2025-12-12 13:28:35 +00:00
commit
50cca14d29
277
.github/workflows/backends.yml
vendored
Normal file
277
.github/workflows/backends.yml
vendored
Normal file
@ -0,0 +1,277 @@
|
||||
name: backends
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
version:
|
||||
description: 'version'
|
||||
required: true
|
||||
type: string
|
||||
commit:
|
||||
description: 'sd.cpp commit'
|
||||
required: true
|
||||
type: string
|
||||
|
||||
jobs:
|
||||
windows:
|
||||
runs-on: windows-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- build: 'noavx'
|
||||
defines: '-DGGML_AVX=OFF -DGGML_AVX2=OFF -DGGML_FMA=OFF -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'avx'
|
||||
defines: '-DGGML_AVX2=OFF -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'avx2'
|
||||
defines: '-DGGML_AVX2=ON -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'avx512'
|
||||
defines: '-DGGML_AVX512=ON -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'cuda11'
|
||||
defines: '-DSD_CUBLAS=ON -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'cuda12'
|
||||
defines: '-DSD_CUBLAS=ON -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'rocm5'
|
||||
defines: '-G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DSD_HIPBLAS=ON -DCMAKE_BUILD_TYPE=Release -DAMDGPU_TARGETS="gfx1100;gfx1102;gfx1030" -DSD_BUILD_SHARED_LIBS=ON'
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
id: checkout
|
||||
uses: actions/checkout@v4.1.2
|
||||
with:
|
||||
repository: 'leejet/stable-diffusion.cpp'
|
||||
ref: '${{ github.event.inputs.commit }}'
|
||||
submodules: recursive
|
||||
|
||||
- name: Install cuda-toolkit
|
||||
id: cuda-toolkit-11
|
||||
if: ${{ matrix.build == 'cuda11' }}
|
||||
uses: Jimver/cuda-toolkit@v0.2.14
|
||||
with:
|
||||
cuda: '11.7.1'
|
||||
method: network
|
||||
sub-packages: '["nvcc", "cudart", "cublas", "cublas_dev", "thrust", "visual_studio_integration"]'
|
||||
|
||||
- name: Install cuda-toolkit
|
||||
id: cuda-toolkit-12
|
||||
if: ${{ matrix.build == 'cuda12' }}
|
||||
uses: Jimver/cuda-toolkit@v0.2.14
|
||||
with:
|
||||
cuda: '12.2.0'
|
||||
method: network
|
||||
sub-packages: '["nvcc", "cudart", "cublas", "cublas_dev", "thrust", "visual_studio_integration"]'
|
||||
|
||||
- name: Install rocm-toolkit
|
||||
id: rocm-toolkit
|
||||
if: ${{ matrix.build == 'rocm5' }}
|
||||
uses: Cyberhan123/rocm-toolkit@v0.1.0
|
||||
with:
|
||||
rocm: '5.5.0'
|
||||
|
||||
- name: Install Ninja
|
||||
id: install-ninja
|
||||
if: ${{ matrix.build == 'rocm5' }}
|
||||
uses: urkle/action-get-ninja@v1
|
||||
with:
|
||||
version: 1.11.1
|
||||
|
||||
- name: Build
|
||||
id: cmake_build
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. ${{ matrix.defines }}
|
||||
cmake --build . --config Release
|
||||
|
||||
- name: Upload artifact
|
||||
id: upload_artifact
|
||||
if: ${{ matrix.build != 'rocm5' }}
|
||||
uses: actions/upload-artifact@v4.3.1
|
||||
with:
|
||||
name: windows-${{ matrix.build }}
|
||||
path: .\build\bin\Release\stable-diffusion.dll
|
||||
|
||||
- name: Upload artifact Rocm
|
||||
id: upload_artifact_rocm
|
||||
if: ${{ matrix.build == 'rocm5' }}
|
||||
uses: actions/upload-artifact@v4.3.1
|
||||
with:
|
||||
name: windows-${{ matrix.build }}
|
||||
path: .\build\bin\stable-diffusion.dll
|
||||
|
||||
linux:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- build: 'noavx'
|
||||
defines: '-DGGML_AVX=OFF -DGGML_AVX2=OFF -DGGML_FMA=OFF -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'avx'
|
||||
defines: '-DGGML_AVX2=OFF -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'avx2'
|
||||
defines: '-DGGML_AVX2=ON -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'avx512'
|
||||
defines: '-DGGML_AVX512=ON -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'cuda11'
|
||||
defines: '-DSD_CUBLAS=ON -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'cuda12'
|
||||
defines: '-DSD_CUBLAS=ON -DSD_BUILD_SHARED_LIBS=ON'
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
id: checkout
|
||||
uses: actions/checkout@v4.1.2
|
||||
with:
|
||||
repository: 'leejet/stable-diffusion.cpp'
|
||||
ref: '${{ github.event.inputs.commit }}'
|
||||
submodules: recursive
|
||||
|
||||
- name: Install cuda-toolkit
|
||||
id: cuda-toolkit-11
|
||||
if: ${{ matrix.build == 'cuda11' }}
|
||||
uses: Jimver/cuda-toolkit@v0.2.14
|
||||
with:
|
||||
cuda: '11.7.1'
|
||||
method: network
|
||||
|
||||
- name: Install cuda-toolkit
|
||||
id: cuda-toolkit-12
|
||||
if: ${{ matrix.build == 'cuda12' }}
|
||||
uses: Jimver/cuda-toolkit@v0.2.14
|
||||
with:
|
||||
cuda: '12.2.0'
|
||||
method: network
|
||||
|
||||
- name: Build
|
||||
id: cmake_build
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. ${{ matrix.defines }}
|
||||
cmake --build . --config Release
|
||||
|
||||
- name: Upload artifact
|
||||
id: upload_artifact
|
||||
uses: actions/upload-artifact@v4.3.1
|
||||
with:
|
||||
name: linux-${{ matrix.build }}
|
||||
path: ./build/bin/libstable-diffusion.so
|
||||
|
||||
linux-hip:
|
||||
runs-on: ubuntu-22.04
|
||||
container: rocm/dev-ubuntu-22.04:6.0.2
|
||||
|
||||
steps:
|
||||
- name: Dependencies
|
||||
id: depends
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y build-essential git cmake rocblas-dev hipblas-dev
|
||||
hipconfig
|
||||
|
||||
- name: Clone
|
||||
id: checkout
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
repository: 'leejet/stable-diffusion.cpp'
|
||||
ref: '${{ github.event.inputs.commit }}'
|
||||
submodules: recursive
|
||||
|
||||
- name: Build
|
||||
id: cmake_build
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DCMAKE_C_COMPILER=hipcc -DCMAKE_CXX_COMPILER=hipcc -DSD_HIPBLAS=ON -DCMAKE_BUILD_TYPE=Release -DAMDGPU_TARGETS="gfx1100;gfx1102;gfx1030" -DSD_BUILD_SHARED_LIBS=ON
|
||||
cmake --build . --config Release
|
||||
|
||||
- name: Upload artifact Rocm
|
||||
id: upload_artifact_rocm
|
||||
uses: actions/upload-artifact@v4.3.1
|
||||
with:
|
||||
name: linux-rocm6
|
||||
path: ./build/bin/libstable-diffusion.so
|
||||
|
||||
osx:
|
||||
runs-on: macos-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- build: 'noavx'
|
||||
defines: '-DCMAKE_OSX_ARCHITECTURES="x86_64" -DGGML_AVX=OFF -DGGML_AVX2=OFF -DGGML_FMA=OFF -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'avx'
|
||||
defines: '-DCMAKE_OSX_ARCHITECTURES="x86_64" -DGGML_AVX2=OFF -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'avx2'
|
||||
defines: '-DCMAKE_OSX_ARCHITECTURES="x86_64" -DGGML_AVX2=ON -DSD_BUILD_SHARED_LIBS=ON'
|
||||
- build: 'avx512'
|
||||
defines: '-DCMAKE_OSX_ARCHITECTURES="x86_64" -DGGML_AVX512=ON -DSD_BUILD_SHARED_LIBS=ON'
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
id: checkout
|
||||
uses: actions/checkout@v4.1.2
|
||||
with:
|
||||
repository: 'leejet/stable-diffusion.cpp'
|
||||
ref: '${{ github.event.inputs.commit }}'
|
||||
submodules: recursive
|
||||
|
||||
- name: Build
|
||||
id: cmake_build
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. ${{ matrix.defines }}
|
||||
cmake --build . --config Release
|
||||
|
||||
- name: Upload artifact
|
||||
id: upload_artifact
|
||||
uses: actions/upload-artifact@v4.3.1
|
||||
with:
|
||||
name: osx-${{ matrix.build }}
|
||||
path: ./build/bin/libstable-diffusion.dylib
|
||||
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
needs:
|
||||
- windows
|
||||
- linux
|
||||
- linux-hip
|
||||
- osx
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
id: checkout
|
||||
uses: actions/checkout@v4.1.2
|
||||
|
||||
- name: Download artifacts
|
||||
id: download_artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
path: Backends
|
||||
|
||||
- name: Display structure of downloaded files
|
||||
run: ls -R
|
||||
|
||||
- name: Setup nuget
|
||||
id: setup_nuget
|
||||
uses: NuGet/setup-nuget@v2.0.0
|
||||
|
||||
- name: Pack
|
||||
id: pack
|
||||
run: |
|
||||
nuget pack ./Backends/StableDiffusion.NET.Backend.Cpu.nuspec -version ${{ github.event.inputs.version }}
|
||||
nuget pack ./Backends/StableDiffusion.NET.Backend.Cuda.nuspec -version ${{ github.event.inputs.version }}
|
||||
nuget pack ./Backends/StableDiffusion.NET.Backend.Rocm.nuspec -version ${{ github.event.inputs.version }}
|
||||
|
||||
- name: Upload artifacts
|
||||
id: upload_artifacts
|
||||
uses: actions/upload-artifact@v4.3.1
|
||||
with:
|
||||
name: StableDiffusion.NET.Backend-Nugets
|
||||
path: ./*.nupkg
|
||||
|
||||
- name: Nuget Push
|
||||
id: nuget_push
|
||||
run: dotnet nuget push **\*.nupkg --skip-duplicate --api-key ${{ secrets.NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json
|
||||
57
.github/workflows/release.yml
vendored
Normal file
57
.github/workflows/release.yml
vendored
Normal file
@ -0,0 +1,57 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
version:
|
||||
description: 'version'
|
||||
required: true
|
||||
type: string
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: windows-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Install .NET Core
|
||||
uses: actions/setup-dotnet@v4
|
||||
with:
|
||||
dotnet-version: 8.0.x
|
||||
|
||||
- name: Restore dependencies
|
||||
run: dotnet restore
|
||||
|
||||
- name: Build
|
||||
run: dotnet build --no-restore --configuration Release /p:Version=${{ github.event.inputs.version }}
|
||||
|
||||
- name: Test
|
||||
run: dotnet test --no-build --verbosity normal --configuration Release
|
||||
|
||||
- name: Upload Nuget Build Artifact
|
||||
uses: actions/upload-artifact@v4.3.1
|
||||
with:
|
||||
name: StableDiffusion.NET-Nuget
|
||||
path: StableDiffusion.NET\bin\Release\*.nupkg
|
||||
if-no-files-found: error
|
||||
|
||||
- name: List files
|
||||
run: tree
|
||||
|
||||
- name: Release
|
||||
uses: softprops/action-gh-release@v2.0.4
|
||||
with:
|
||||
tag_name: ${{ github.event.inputs.version }}
|
||||
generate_release_notes: true
|
||||
files: StableDiffusion.NET/bin/Release/net8.0/StableDiffusion.NET.dll
|
||||
|
||||
- name: Nuget Push
|
||||
id: nuget_push
|
||||
run: |
|
||||
dotnet nuget push **\*.nupkg --skip-duplicate --api-key ${{ secrets.NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json
|
||||
dotnet nuget push **\*.snupkg --skip-duplicate --api-key ${{ secrets.NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json
|
||||
|
||||
42
ImageCreationUI/ActionCommand.cs
Normal file
42
ImageCreationUI/ActionCommand.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace ImageCreationUI;
|
||||
|
||||
public class ActionCommand(Action command, Func<bool>? canExecute = null) : ICommand
|
||||
{
|
||||
#region Events
|
||||
|
||||
public event EventHandler? CanExecuteChanged;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public bool CanExecute(object? parameter) => canExecute?.Invoke() ?? true;
|
||||
|
||||
public void Execute(object? parameter) => command.Invoke();
|
||||
|
||||
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public class ActionCommand<T>(Action<T> command, Func<T, bool>? canExecute = null) : ICommand
|
||||
where T : class
|
||||
{
|
||||
#region Events
|
||||
|
||||
public event EventHandler? CanExecuteChanged;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public bool CanExecute(object? parameter) => canExecute?.Invoke((T)parameter!) ?? true;
|
||||
|
||||
public void Execute(object? parameter) => command.Invoke((T)parameter!);
|
||||
|
||||
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
||||
|
||||
#endregion
|
||||
}
|
||||
4
ImageCreationUI/App.xaml
Normal file
4
ImageCreationUI/App.xaml
Normal file
@ -0,0 +1,4 @@
|
||||
<Application x:Class="ImageCreationUI.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
StartupUri="MainWindow.xaml" />
|
||||
14
ImageCreationUI/App.xaml.cs
Normal file
14
ImageCreationUI/App.xaml.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Windows;
|
||||
|
||||
namespace ImageCreationUI
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
33
ImageCreationUI/Converter/ImageToImageSourceConverter.cs
Normal file
33
ImageCreationUI/Converter/ImageToImageSourceConverter.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using StableDiffusion.NET.Helper.Images;
|
||||
|
||||
namespace ImageCreationUI.Converter;
|
||||
|
||||
[ValueConversion(typeof(IImage), typeof(ImageSource))]
|
||||
public class ImageToImageSourceConverter : IValueConverter
|
||||
{
|
||||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||
{
|
||||
Bitmap? bitmap = (value as IImage)?.ToBitmap();
|
||||
if (bitmap == null) return null;
|
||||
|
||||
using MemoryStream ms = new();
|
||||
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
|
||||
ms.Position = 0;
|
||||
|
||||
BitmapImage bitmapImage = new();
|
||||
bitmapImage.BeginInit();
|
||||
bitmapImage.StreamSource = ms;
|
||||
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
|
||||
bitmapImage.EndInit();
|
||||
|
||||
return bitmapImage;
|
||||
}
|
||||
|
||||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException();
|
||||
}
|
||||
45
ImageCreationUI/Extensions/ImageExtension.cs
Normal file
45
ImageCreationUI/Extensions/ImageExtension.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using StableDiffusion.NET.Helper.Images.Colors;
|
||||
using StableDiffusion.NET.Helper.Images;
|
||||
using System.IO;
|
||||
|
||||
namespace ImageCreationUI;
|
||||
|
||||
public static class ImageExtension
|
||||
{
|
||||
public static Bitmap ToBitmap(this IImage image) => image.AsRefImage<ColorRGB>().ToBitmap();
|
||||
public static Bitmap ToBitmap(this Image<ColorRGB> image) => image.AsRefImage<ColorRGB>().ToBitmap();
|
||||
|
||||
public static unsafe Bitmap ToBitmap(this RefImage<ColorRGB> image)
|
||||
{
|
||||
Bitmap output = new(image.Width, image.Height, PixelFormat.Format24bppRgb);
|
||||
Rectangle rect = new(0, 0, image.Width, image.Height);
|
||||
BitmapData bmpData = output.LockBits(rect, ImageLockMode.ReadWrite, output.PixelFormat);
|
||||
|
||||
nint ptr = bmpData.Scan0;
|
||||
foreach (ReadOnlyRefEnumerable<ColorRGB> row in image.Rows)
|
||||
{
|
||||
Span<ColorBGR> target = new((void*)ptr, bmpData.Stride);
|
||||
for (int i = 0; i < row.Length; i++)
|
||||
{
|
||||
ColorRGB srcColor = row[i];
|
||||
target[i] = new ColorBGR(srcColor.B, srcColor.G, srcColor.R);
|
||||
}
|
||||
|
||||
ptr += bmpData.Stride;
|
||||
}
|
||||
|
||||
output.UnlockBits(bmpData);
|
||||
return output;
|
||||
}
|
||||
|
||||
public static byte[] ToPng(this IImage image)
|
||||
{
|
||||
using Bitmap bitmap = image.ToBitmap();
|
||||
using MemoryStream ms = new();
|
||||
bitmap.Save(ms, ImageFormat.Png);
|
||||
|
||||
return ms.ToArray();
|
||||
}
|
||||
}
|
||||
20
ImageCreationUI/ImageCreationUI.csproj
Normal file
20
ImageCreationUI/ImageCreationUI.csproj
Normal file
@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UseWPF>true</UseWPF>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\StableDiffusion.NET\StableDiffusion.NET.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
2
ImageCreationUI/ImageCreationUI.csproj.DotSettings
Normal file
2
ImageCreationUI/ImageCreationUI.csproj.DotSettings
Normal file
@ -0,0 +1,2 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=extensions/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||
91
ImageCreationUI/MainWindow.xaml
Normal file
91
ImageCreationUI/MainWindow.xaml
Normal file
@ -0,0 +1,91 @@
|
||||
<Window x:Class="ImageCreationUI.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:ImageCreationUI"
|
||||
xmlns:converter="clr-namespace:ImageCreationUI.Converter"
|
||||
mc:Ignorable="d"
|
||||
Title="StableDiffusion.NET" Width="1280" Height="720">
|
||||
<Window.DataContext>
|
||||
<local:MainWindowViewModel />
|
||||
</Window.DataContext>
|
||||
|
||||
<Window.Resources>
|
||||
<converter:ImageToImageSourceConverter x:Key="ImageToImageSourceConverter" />
|
||||
</Window.Resources>
|
||||
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="400" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="400" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<StackPanel Margin="4,0" Orientation="Vertical">
|
||||
<TextBlock Margin="8" Text="This is just an example - inputs are not validated! Make sure everything is correct before pressing 'Create Image'." />
|
||||
|
||||
<Separator />
|
||||
|
||||
<Label Content="Model-Path"/>
|
||||
<DockPanel>
|
||||
<Button DockPanel.Dock="Right" Width="24" Margin="2,0,0,0" Content="..." Command="{Binding SelectModelCommand}" IsEnabled="{Binding IsReady}" />
|
||||
<TextBox Text="{Binding ModelPath}" />
|
||||
</DockPanel>
|
||||
|
||||
<Label Content="Vae-Path (Optional)"/>
|
||||
<DockPanel>
|
||||
<Button DockPanel.Dock="Right" Width="24" Margin="2,0,0,0" Content="..." Command="{Binding SelectVaeCommand}" IsEnabled="{Binding IsReady}" />
|
||||
<TextBox Text="{Binding VaePath}" />
|
||||
</DockPanel>
|
||||
|
||||
<Button Margin="0,8" Content="Load Model" Command="{Binding LoadModelCommand}" IsEnabled="{Binding IsReady}" />
|
||||
|
||||
<Separator />
|
||||
|
||||
<Label Margin="0,8,0,0" Content="Prompt" />
|
||||
<TextBox Height="80" TextWrapping="Wrap" Text="{Binding Prompt}" />
|
||||
|
||||
<Label Content="AntiPrompt" />
|
||||
<TextBox Height="80" TextWrapping="Wrap" Text="{Binding AntiPrompt}" />
|
||||
|
||||
<StackPanel Orientation="Horizontal" Margin="0,8,0,0">
|
||||
<Label Width="50" Content="Width" />
|
||||
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Width}" />
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel Orientation="Horizontal" Margin="0,8,0,0">
|
||||
<Label Width="50" Content="Height" />
|
||||
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Height}" />
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel Orientation="Horizontal" Margin="0,8,0,0">
|
||||
<Label Width="50" Content="Cfg" />
|
||||
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Cfg}" />
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel Orientation="Horizontal" Margin="0,8,0,0">
|
||||
<Label Width="50" Content="Steps" />
|
||||
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Steps}" />
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel Orientation="Horizontal" Margin="0,8,0,0">
|
||||
<Label Width="50" Content="Seed" />
|
||||
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Seed}" />
|
||||
</StackPanel>
|
||||
|
||||
<Button Margin="0,16,0,0" Content="Create Image" Command="{Binding CreateImageCommand}" IsEnabled="{Binding IsReady}" />
|
||||
<Button Margin="0,16,0,0" Content="Save Image" Command="{Binding SaveImageCommand}" IsEnabled="{Binding IsReady}" />
|
||||
</StackPanel>
|
||||
|
||||
<GridSplitter Grid.Column="1" Margin="2,0" Width="2" Background="DimGray" VerticalAlignment="Stretch" HorizontalAlignment="Center" />
|
||||
|
||||
<Image Grid.Column="2" Source="{Binding Image, Converter={StaticResource ImageToImageSourceConverter}}" />
|
||||
|
||||
<GridSplitter Grid.Column="3" Margin="2,0" Width="2" Background="DimGray" VerticalAlignment="Stretch" HorizontalAlignment="Center" />
|
||||
|
||||
<TextBox Grid.Column="4" IsReadOnly="True" BorderThickness="0" AcceptsReturn="True" Text="{Binding Log}" TextChanged="TextBoxBase_OnTextChanged" />
|
||||
</Grid>
|
||||
</Window>
|
||||
11
ImageCreationUI/MainWindow.xaml.cs
Normal file
11
ImageCreationUI/MainWindow.xaml.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace ImageCreationUI;
|
||||
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow() => InitializeComponent();
|
||||
|
||||
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs args) => (sender as TextBox)?.ScrollToEnd();
|
||||
}
|
||||
249
ImageCreationUI/MainWindowViewModel.cs
Normal file
249
ImageCreationUI/MainWindowViewModel.cs
Normal file
@ -0,0 +1,249 @@
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.Win32;
|
||||
using StableDiffusion.NET;
|
||||
using StableDiffusion.NET.Helper.Images;
|
||||
|
||||
namespace ImageCreationUI;
|
||||
|
||||
public class MainWindowViewModel : INotifyPropertyChanged
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private StableDiffusionModel? _model;
|
||||
|
||||
private string _modelPath = string.Empty;
|
||||
public string ModelPath
|
||||
{
|
||||
get => _modelPath;
|
||||
set => SetProperty(ref _modelPath, value);
|
||||
}
|
||||
|
||||
private string _vaePath = string.Empty;
|
||||
public string VaePath
|
||||
{
|
||||
get => _vaePath;
|
||||
set => SetProperty(ref _vaePath, value);
|
||||
}
|
||||
|
||||
private string _prompt = string.Empty;
|
||||
public string Prompt
|
||||
{
|
||||
get => _prompt;
|
||||
set => SetProperty(ref _prompt, value);
|
||||
}
|
||||
|
||||
private string _antiPrompt = string.Empty;
|
||||
public string AntiPrompt
|
||||
{
|
||||
get => _antiPrompt;
|
||||
set => SetProperty(ref _antiPrompt, value);
|
||||
}
|
||||
|
||||
private int _width = 1024;
|
||||
public int Width
|
||||
{
|
||||
get => _width;
|
||||
set => SetProperty(ref _width, value);
|
||||
}
|
||||
|
||||
private int _height = 1024;
|
||||
public int Height
|
||||
{
|
||||
get => _height;
|
||||
set => SetProperty(ref _height, value);
|
||||
}
|
||||
|
||||
private float _cfg = 5f;
|
||||
public float Cfg
|
||||
{
|
||||
get => _cfg;
|
||||
set => SetProperty(ref _cfg, value);
|
||||
}
|
||||
|
||||
private int _steps = 28;
|
||||
public int Steps
|
||||
{
|
||||
get => _steps;
|
||||
set => SetProperty(ref _steps, value);
|
||||
}
|
||||
|
||||
private int _seed = -1;
|
||||
public int Seed
|
||||
{
|
||||
get => _seed;
|
||||
set => SetProperty(ref _seed, value);
|
||||
}
|
||||
|
||||
private IImage? _image;
|
||||
public IImage? Image
|
||||
{
|
||||
get => _image;
|
||||
set => SetProperty(ref _image, value);
|
||||
}
|
||||
|
||||
private string _log = string.Empty;
|
||||
public string Log
|
||||
{
|
||||
get => _log;
|
||||
set => SetProperty(ref _log, value);
|
||||
}
|
||||
|
||||
private bool _isReady = true;
|
||||
public bool IsReady
|
||||
{
|
||||
get => _isReady;
|
||||
set => SetProperty(ref _isReady, value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Commands
|
||||
|
||||
private ActionCommand? _loadModelCommand;
|
||||
public ActionCommand LoadModelCommand => _loadModelCommand ??= new ActionCommand(LoadModel);
|
||||
|
||||
private ActionCommand? _createImageCommand;
|
||||
public ActionCommand CreateImageCommand => _createImageCommand ??= new ActionCommand(CreateImage);
|
||||
|
||||
private ActionCommand? _saveImageCommand;
|
||||
public ActionCommand SaveImageCommand => _saveImageCommand ??= new ActionCommand(SaveImage);
|
||||
|
||||
private ActionCommand? _selectModelCommand;
|
||||
public ActionCommand SelectModelCommand => _selectModelCommand ??= new ActionCommand(SelectModel);
|
||||
|
||||
private ActionCommand? _selectVaeCommand;
|
||||
public ActionCommand SelectVaeCommand => _selectVaeCommand ??= new ActionCommand(SelectVae);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public MainWindowViewModel()
|
||||
{
|
||||
try
|
||||
{
|
||||
StableDiffusionModel.Log += (_, args) => LogLine($"LOG [{args.Level}]: {args.Text}", false);
|
||||
StableDiffusionModel.Progress += (_, args) => LogLine($"PROGRESS {args.Step} / {args.Steps} ({(args.Progress * 100):N2} %) {args.IterationsPerSecond:N2} it/s ({args.Time})");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogLine($"Failed to load stable-diffussion.cpp libraries!{Environment.NewLine}{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
private async void LoadModel()
|
||||
{
|
||||
try
|
||||
{
|
||||
IsReady = false;
|
||||
|
||||
_model?.Dispose();
|
||||
|
||||
LogLine($"Loading model '{ModelPath}'");
|
||||
_model = await Task.Run(() => new StableDiffusionModel(ModelPath, new ModelParameter { VaePath = VaePath }));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogLine($"Failed to load model ...{Environment.NewLine}{ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsReady = true;
|
||||
}
|
||||
}
|
||||
|
||||
private async void CreateImage()
|
||||
{
|
||||
try
|
||||
{
|
||||
IsReady = false;
|
||||
|
||||
LogLine("Creating image ...");
|
||||
using StableDiffusionImage? image = await Task.Run(() => _model?.TextToImage(Prompt, new StableDiffusionParameter
|
||||
{
|
||||
NegativePrompt = AntiPrompt,
|
||||
Width = Width,
|
||||
Height = Height,
|
||||
CfgScale = Cfg,
|
||||
SampleSteps = Steps,
|
||||
Seed = Seed
|
||||
}));
|
||||
|
||||
Image = image?.ToImage();
|
||||
LogLine("done!");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogLine($"Failed to create image ...{Environment.NewLine}{ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsReady = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveImage()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Image == null) return;
|
||||
|
||||
SaveFileDialog saveFileDialog = new() { Filter = "PNG File (*.png)|*.png" };
|
||||
if (saveFileDialog.ShowDialog() == true)
|
||||
{
|
||||
File.WriteAllBytes(saveFileDialog.FileName, Image.ToPng());
|
||||
LogLine($"Image saved to '{saveFileDialog.FileName}'!");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogLine($"Failed to save image ...{Environment.NewLine}{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void SelectModel()
|
||||
{
|
||||
OpenFileDialog openFileDialog = new() { Filter = "Stable Diffusion Model|*.*" };
|
||||
if (openFileDialog.ShowDialog() == true)
|
||||
ModelPath = openFileDialog.FileName;
|
||||
}
|
||||
|
||||
private void SelectVae()
|
||||
{
|
||||
OpenFileDialog openFileDialog = new() { Filter = "Stable Diffusion VAE|*.*" };
|
||||
if (openFileDialog.ShowDialog() == true)
|
||||
VaePath = openFileDialog.FileName;
|
||||
}
|
||||
|
||||
private void LogLine(string line, bool appendNewLine = true)
|
||||
{
|
||||
if (appendNewLine)
|
||||
Log += line + Environment.NewLine;
|
||||
else
|
||||
Log += line;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region NotifyPropertyChanged
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
private void OnPropertyChanged([CallerMemberName] string? propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
|
||||
private bool SetProperty<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
|
||||
{
|
||||
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
|
||||
field = value;
|
||||
OnPropertyChanged(propertyName);
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 leejet
|
||||
Copyright (c) 2024 Darth Affe
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
BIN
Resources/sd_net_icon.xcf
Normal file
BIN
Resources/sd_net_icon.xcf
Normal file
Binary file not shown.
@ -3,7 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.8.34322.80
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StableDiffusion.NET", "StableDiffusion.NET\StableDiffusion.NET.csproj", "{ED9336F9-7C5F-47DD-A120-272F4835E95F}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StableDiffusion.NET", "StableDiffusion.NET\StableDiffusion.NET.csproj", "{ED9336F9-7C5F-47DD-A120-272F4835E95F}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{8961F5D8-3F50-4027-8862-635FCA1E1568}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageCreationUI", "ImageCreationUI\ImageCreationUI.csproj", "{EB73E250-D2F7-469A-80BF-02C5078546A7}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -15,10 +19,17 @@ Global
|
||||
{ED9336F9-7C5F-47DD-A120-272F4835E95F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{ED9336F9-7C5F-47DD-A120-272F4835E95F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{ED9336F9-7C5F-47DD-A120-272F4835E95F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{EB73E250-D2F7-469A-80BF-02C5078546A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{EB73E250-D2F7-469A-80BF-02C5078546A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{EB73E250-D2F7-469A-80BF-02C5078546A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{EB73E250-D2F7-469A-80BF-02C5078546A7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{EB73E250-D2F7-469A-80BF-02C5078546A7} = {8961F5D8-3F50-4027-8862-635FCA1E1568}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {8DD53974-8766-4659-97B6-4AEDF4323F65}
|
||||
EndGlobalSection
|
||||
|
||||
71
StableDiffusion.NET/Backends/Backends.cs
Normal file
71
StableDiffusion.NET/Backends/Backends.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace StableDiffusion.NET;
|
||||
|
||||
public static class Backends
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
public static CpuBackend CpuBackend { get; } = new();
|
||||
public static CudaBackend CudaBackend { get; } = new();
|
||||
public static RocmBackend RocmBackend { get; } = new();
|
||||
|
||||
private static readonly List<IBackend> CUSTOM_BACKENDS = [];
|
||||
public static IReadOnlyList<IBackend> CustomBackends => CUSTOM_BACKENDS.AsReadOnly();
|
||||
|
||||
public static IEnumerable<IBackend> RegisteredBackends => [CpuBackend, CudaBackend, RocmBackend, .. CUSTOM_BACKENDS];
|
||||
public static IEnumerable<IBackend> AvailableBackends => RegisteredBackends.Where(x => x.IsAvailable);
|
||||
public static IEnumerable<IBackend> ActiveBackends => AvailableBackends.Where(x => x.IsEnabled);
|
||||
|
||||
public static List<string> SearchPaths { get; } = [];
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
public static event EventHandler<LibraryPathCreatingEventArgs>? LibraryPathCreating;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public static bool RegisterBackend(IBackend backend)
|
||||
{
|
||||
if (backend is NET.CpuBackend or NET.CudaBackend or NET.RocmBackend)
|
||||
throw new ArgumentException("Default backends can't be registered again.");
|
||||
|
||||
if (CUSTOM_BACKENDS.Contains(backend))
|
||||
return false;
|
||||
|
||||
CUSTOM_BACKENDS.Add(backend);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool UnregisterBackend(IBackend backend)
|
||||
=> CUSTOM_BACKENDS.Remove(backend);
|
||||
|
||||
internal static string GetFullPath(string os, string backend, string libName)
|
||||
{
|
||||
string path = Path.Combine("runtimes", os, "native", backend, libName);
|
||||
return OnLibraryPathCreating(path);
|
||||
}
|
||||
|
||||
private static string OnLibraryPathCreating(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
LibraryPathCreatingEventArgs args = new(path);
|
||||
LibraryPathCreating?.Invoke(null, args);
|
||||
return args.Path;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
101
StableDiffusion.NET/Backends/CpuBackend.cs
Normal file
101
StableDiffusion.NET/Backends/CpuBackend.cs
Normal file
@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StableDiffusion.NET;
|
||||
|
||||
public class CpuBackend : IBackend
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
|
||||
public int Priority => 0;
|
||||
|
||||
public bool IsAvailable => (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
|
||||
|| RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
|
||||
|| RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
&& (RuntimeInformation.OSArchitecture == Architecture.X64);
|
||||
|
||||
public string PathPart => Avx.GetDescription();
|
||||
|
||||
private readonly List<AvxLevel> _availableAvxLevels = [];
|
||||
public IEnumerable<AvxLevel> AvailableAvxLevels => _availableAvxLevels.AsReadOnly();
|
||||
|
||||
private AvxLevel _avx;
|
||||
public AvxLevel Avx
|
||||
{
|
||||
get => _avx;
|
||||
set
|
||||
{
|
||||
if (!_availableAvxLevels.Contains(value)) throw new ArgumentException("The selected AVX-Level is not supported on this system.");
|
||||
_avx = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
internal CpuBackend()
|
||||
{
|
||||
_availableAvxLevels.Add(AvxLevel.None);
|
||||
Avx = AvxLevel.None;
|
||||
|
||||
if (System.Runtime.Intrinsics.X86.Avx.IsSupported)
|
||||
{
|
||||
_availableAvxLevels.Add(AvxLevel.Avx);
|
||||
Avx = AvxLevel.Avx;
|
||||
}
|
||||
|
||||
if (System.Runtime.Intrinsics.X86.Avx2.IsSupported)
|
||||
{
|
||||
_availableAvxLevels.Add(AvxLevel.Avx2);
|
||||
Avx = AvxLevel.Avx2;
|
||||
}
|
||||
|
||||
if (CheckAvx512())
|
||||
{
|
||||
_availableAvxLevels.Add(AvxLevel.Avx512);
|
||||
Avx = AvxLevel.Avx512;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
private static bool CheckAvx512()
|
||||
{
|
||||
if (!System.Runtime.Intrinsics.X86.X86Base.IsSupported)
|
||||
return false;
|
||||
|
||||
(_, int _, int ecx, _) = System.Runtime.Intrinsics.X86.X86Base.CpuId(7, 0);
|
||||
|
||||
bool vnni = (ecx & 0b_1000_0000_0000) != 0;
|
||||
|
||||
bool f = System.Runtime.Intrinsics.X86.Avx512F.IsSupported;
|
||||
bool bw = System.Runtime.Intrinsics.X86.Avx512BW.IsSupported;
|
||||
bool vbmi = System.Runtime.Intrinsics.X86.Avx512Vbmi.IsSupported;
|
||||
|
||||
return vnni && vbmi && bw && f;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public enum AvxLevel
|
||||
{
|
||||
[Description("")]
|
||||
None,
|
||||
|
||||
[Description("avx")]
|
||||
Avx,
|
||||
|
||||
[Description("avx2")]
|
||||
Avx2,
|
||||
|
||||
[Description("avx512")]
|
||||
Avx512,
|
||||
}
|
||||
}
|
||||
135
StableDiffusion.NET/Backends/CudaBackend.cs
Normal file
135
StableDiffusion.NET/Backends/CudaBackend.cs
Normal file
@ -0,0 +1,135 @@
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.Json;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace StableDiffusion.NET;
|
||||
|
||||
public partial class CudaBackend : IBackend
|
||||
{
|
||||
#region Constants
|
||||
|
||||
private const string CUDA_VERSION_FILE = "version.json";
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties & Fields
|
||||
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
|
||||
public int Priority => 10;
|
||||
|
||||
public bool IsAvailable => (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
|
||||
|| RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
&& (RuntimeInformation.OSArchitecture == Architecture.X64)
|
||||
&& CudaVersion is 11 or 12;
|
||||
|
||||
public string PathPart => CudaVersion switch
|
||||
{
|
||||
11 => "cuda11",
|
||||
12 => "cuda12",
|
||||
_ => string.Empty
|
||||
};
|
||||
|
||||
public int CudaVersion { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
internal CudaBackend()
|
||||
{
|
||||
CudaVersion = GetCudaMajorVersion();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
private static int GetCudaMajorVersion()
|
||||
{
|
||||
try
|
||||
{
|
||||
string? cudaPath;
|
||||
string version = "";
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
cudaPath = Environment.GetEnvironmentVariable("CUDA_PATH");
|
||||
|
||||
if (cudaPath == null)
|
||||
{
|
||||
IDictionary environmentVariables = Environment.GetEnvironmentVariables();
|
||||
string? key = environmentVariables.Keys.Cast<string>().Where(x => x.StartsWith("CUDA_PATH_", StringComparison.OrdinalIgnoreCase))
|
||||
.Select(x => (x, CudaPathRegex().Match(x)))
|
||||
.Where(x => x.Item2.Success)
|
||||
.Select(x => (x.x, x.Item2.Groups["majorVersion"].Value))
|
||||
.OrderByDescending(x => int.Parse(x.Value))
|
||||
.FirstOrDefault()
|
||||
.x;
|
||||
if (key != null)
|
||||
cudaPath = Environment.GetEnvironmentVariable(key);
|
||||
}
|
||||
|
||||
if (cudaPath == null) return -1;
|
||||
|
||||
version = GetCudaVersionFromPath(cudaPath);
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
cudaPath = "/usr/local/bin/cuda";
|
||||
version = GetCudaVersionFromPath(cudaPath);
|
||||
if (string.IsNullOrEmpty(version))
|
||||
{
|
||||
cudaPath = Environment.GetEnvironmentVariable("LD_LIBRARY_PATH");
|
||||
if (cudaPath is null)
|
||||
return -1;
|
||||
|
||||
foreach (string path in cudaPath.Split(':'))
|
||||
{
|
||||
version = GetCudaVersionFromPath(Path.Combine(path, ".."));
|
||||
if (string.IsNullOrEmpty(version))
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(version))
|
||||
return -1;
|
||||
|
||||
version = version.Split('.')[0];
|
||||
if (int.TryParse(version, out int majorVersion))
|
||||
return majorVersion;
|
||||
}
|
||||
catch { /* No version or error */ }
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static string GetCudaVersionFromPath(string cudaPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
string json = File.ReadAllText(Path.Combine(cudaPath, CUDA_VERSION_FILE));
|
||||
using JsonDocument document = JsonDocument.Parse(json);
|
||||
JsonElement root = document.RootElement;
|
||||
JsonElement cublasNode = root.GetProperty("libcublas");
|
||||
JsonElement versionNode = cublasNode.GetProperty("version");
|
||||
if (versionNode.ValueKind == JsonValueKind.Undefined)
|
||||
return string.Empty;
|
||||
|
||||
return versionNode.GetString() ?? "";
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
[GeneratedRegex(@"CUDA_PATH_V?(?<majorVersion>\d+)_?\d*", RegexOptions.IgnoreCase)]
|
||||
private static partial Regex CudaPathRegex();
|
||||
|
||||
#endregion
|
||||
}
|
||||
9
StableDiffusion.NET/Backends/IBackend.cs
Normal file
9
StableDiffusion.NET/Backends/IBackend.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace StableDiffusion.NET;
|
||||
|
||||
public interface IBackend
|
||||
{
|
||||
bool IsEnabled { get; set; }
|
||||
public int Priority { get; }
|
||||
bool IsAvailable { get; }
|
||||
string PathPart { get; }
|
||||
}
|
||||
87
StableDiffusion.NET/Backends/RocmBackend.cs
Normal file
87
StableDiffusion.NET/Backends/RocmBackend.cs
Normal file
@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using StableDiffusion.NET.Helper;
|
||||
|
||||
namespace StableDiffusion.NET;
|
||||
|
||||
public partial class RocmBackend : IBackend
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
|
||||
public int Priority => 10;
|
||||
|
||||
public bool IsAvailable => ((RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
|
||||
&& RocmVersion is 5)
|
||||
|| (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
|
||||
&& RocmVersion is 6))
|
||||
&& (RuntimeInformation.OSArchitecture == Architecture.X64);
|
||||
|
||||
public string PathPart => RocmVersion switch
|
||||
{
|
||||
5 => "rocm5",
|
||||
6 => "rocm6",
|
||||
_ => string.Empty
|
||||
};
|
||||
|
||||
public int RocmVersion { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
internal RocmBackend()
|
||||
{
|
||||
RocmVersion = GetRocmMajorVersion();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
private static int GetRocmMajorVersion()
|
||||
{
|
||||
try
|
||||
{
|
||||
string version = "";
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
string? rocmPath = Environment.GetEnvironmentVariable("HIP_PATH");
|
||||
if (rocmPath == null) return -1;
|
||||
|
||||
Match match = GetWindowsVersionRegex().Match(rocmPath);
|
||||
if (match.Success)
|
||||
version = match.Groups["version"].Value;
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
string? hipconfig = ProcessHelper.RunCommand("hipconfig");
|
||||
if (hipconfig == null) return -1;
|
||||
|
||||
Match match = GetLinuxVersionRegex().Match(hipconfig);
|
||||
if (match.Success)
|
||||
version = match.Groups["version"].Value;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(version))
|
||||
return -1;
|
||||
|
||||
version = version.Split('.')[0];
|
||||
if (int.TryParse(version, out int majorVersion))
|
||||
return majorVersion;
|
||||
}
|
||||
catch { /* No version or error */ }
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
[GeneratedRegex(@".*?\\(?<version>\d+.\d*)\\")]
|
||||
private static partial Regex GetWindowsVersionRegex();
|
||||
|
||||
[GeneratedRegex(@"HIP_PATH\s*:\s*[\w\/]+-(?<version>[\d.]+)$")]
|
||||
private static partial Regex GetLinuxVersionRegex();
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace StableDiffusion.NET;
|
||||
|
||||
public class LibraryPathCreatingEventArgs(string path) : EventArgs
|
||||
{
|
||||
public string Path { get; set; } = path;
|
||||
}
|
||||
16
StableDiffusion.NET/Extensions/EnumExtension.cs
Normal file
16
StableDiffusion.NET/Extensions/EnumExtension.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace StableDiffusion.NET;
|
||||
|
||||
internal static class EnumExtension
|
||||
{
|
||||
public static string GetDescription(this Enum value)
|
||||
{
|
||||
DescriptionAttribute[]? attributes = (DescriptionAttribute[]?)value.GetType().GetField(value.ToString())?.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||
|
||||
return attributes?.Length > 0
|
||||
? attributes[0].Description
|
||||
: value.ToString();
|
||||
}
|
||||
}
|
||||
9
StableDiffusion.NET/Extensions/ImageExtensions.cs
Normal file
9
StableDiffusion.NET/Extensions/ImageExtensions.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using StableDiffusion.NET.Helper.Images;
|
||||
using StableDiffusion.NET.Helper.Images.Colors;
|
||||
|
||||
namespace StableDiffusion.NET;
|
||||
|
||||
public static class ImageExtension
|
||||
{
|
||||
public static Image<ColorRGB> ToImage(this StableDiffusionImage image) => new(image.Data.ToArray(), 0, 0, image.Width, image.Height, image.Width);
|
||||
}
|
||||
66
StableDiffusion.NET/Helper/Images/Colors/ColorABGR.cs
Normal file
66
StableDiffusion.NET/Helper/Images/Colors/ColorABGR.cs
Normal file
@ -0,0 +1,66 @@
|
||||
// ReSharper disable ConvertToAutoProperty
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StableDiffusion.NET.Helper.Images.Colors;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color in 32 bit ABGR-format.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct ColorABGR : IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc />
|
||||
public static ColorFormat ColorFormat => ColorFormat.ABGR;
|
||||
|
||||
private readonly byte _a;
|
||||
private readonly byte _b;
|
||||
private readonly byte _g;
|
||||
private readonly byte _r;
|
||||
|
||||
// ReSharper disable ConvertToAutoPropertyWhenPossible
|
||||
/// <inheritdoc />
|
||||
public byte A => _a;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte B => _b;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte G => _g;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte R => _r;
|
||||
// ReSharper restore ConvertToAutoPropertyWhenPossible
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ColorABGR"/> class.
|
||||
/// </summary>
|
||||
/// <param name="a">The alpha-component of the color.</param>
|
||||
/// <param name="b">The blue-component of the color.</param>
|
||||
/// <param name="g">The green-component of the color.</param>
|
||||
/// <param name="r">The red-component of the color.</param>
|
||||
public ColorABGR(byte a, byte b, byte g, byte r)
|
||||
{
|
||||
this._a = a;
|
||||
this._b = b;
|
||||
this._g = g;
|
||||
this._r = r;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"[A: {_a}, R: {_r}, G: {_g}, B: {_b}]";
|
||||
|
||||
#endregion
|
||||
}
|
||||
66
StableDiffusion.NET/Helper/Images/Colors/ColorARGB.cs
Normal file
66
StableDiffusion.NET/Helper/Images/Colors/ColorARGB.cs
Normal file
@ -0,0 +1,66 @@
|
||||
// ReSharper disable ConvertToAutoProperty
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StableDiffusion.NET.Helper.Images.Colors;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color in 32 bit ARGB-format.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct ColorARGB : IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc />
|
||||
public static ColorFormat ColorFormat => ColorFormat.ARGB;
|
||||
|
||||
private readonly byte _a;
|
||||
private readonly byte _r;
|
||||
private readonly byte _g;
|
||||
private readonly byte _b;
|
||||
|
||||
// ReSharper disable ConvertToAutoPropertyWhenPossible
|
||||
/// <inheritdoc />
|
||||
public byte A => _a;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte R => _r;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte G => _g;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte B => _b;
|
||||
// ReSharper restore ConvertToAutoPropertyWhenPossible
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ColorARGB"/> class.
|
||||
/// </summary>
|
||||
/// <param name="a">The alpha-component of the color.</param>
|
||||
/// <param name="r">The red-component of the color.</param>
|
||||
/// <param name="g">The green-component of the color.</param>
|
||||
/// <param name="b">The blue-component of the color.</param>
|
||||
public ColorARGB(byte a, byte r, byte g, byte b)
|
||||
{
|
||||
this._a = a;
|
||||
this._r = r;
|
||||
this._g = g;
|
||||
this._b = b;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"[A: {_a}, R: {_r}, G: {_g}, B: {_b}]";
|
||||
|
||||
#endregion
|
||||
}
|
||||
63
StableDiffusion.NET/Helper/Images/Colors/ColorBGR.cs
Normal file
63
StableDiffusion.NET/Helper/Images/Colors/ColorBGR.cs
Normal file
@ -0,0 +1,63 @@
|
||||
// ReSharper disable ConvertToAutoProperty
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StableDiffusion.NET.Helper.Images.Colors;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color in 24 bit BGR-format.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct ColorBGR : IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc />
|
||||
public static ColorFormat ColorFormat => ColorFormat.BGR;
|
||||
|
||||
private readonly byte _b;
|
||||
private readonly byte _g;
|
||||
private readonly byte _r;
|
||||
|
||||
// ReSharper disable ConvertToAutoPropertyWhenPossible
|
||||
/// <inheritdoc />
|
||||
public byte A => byte.MaxValue;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte B => _b;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte G => _g;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte R => _r;
|
||||
// ReSharper restore ConvertToAutoPropertyWhenPossible
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ColorBGR"/> class.
|
||||
/// </summary>
|
||||
/// <param name="b">The blue-component of the color.</param>
|
||||
/// <param name="g">The green-component of the color.</param>
|
||||
/// <param name="r">The red-component of the color.</param>
|
||||
public ColorBGR(byte b, byte g, byte r)
|
||||
{
|
||||
this._b = b;
|
||||
this._g = g;
|
||||
this._r = r;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"[A: {A}, R: {_r}, G: {_g}, B: {_b}]";
|
||||
|
||||
#endregion
|
||||
}
|
||||
66
StableDiffusion.NET/Helper/Images/Colors/ColorBGRA.cs
Normal file
66
StableDiffusion.NET/Helper/Images/Colors/ColorBGRA.cs
Normal file
@ -0,0 +1,66 @@
|
||||
// ReSharper disable ConvertToAutoProperty
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StableDiffusion.NET.Helper.Images.Colors;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color in 32 bit BGRA-format.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct ColorBGRA : IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc />
|
||||
public static ColorFormat ColorFormat => ColorFormat.BGRA;
|
||||
|
||||
private readonly byte _b;
|
||||
private readonly byte _g;
|
||||
private readonly byte _r;
|
||||
private readonly byte _a;
|
||||
|
||||
// ReSharper disable ConvertToAutoPropertyWhenPossible
|
||||
/// <inheritdoc />
|
||||
public byte B => _b;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte G => _g;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte R => _r;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte A => _a;
|
||||
// ReSharper restore ConvertToAutoPropertyWhenPossible
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ColorBGRA"/> class.
|
||||
/// </summary>
|
||||
/// <param name="b">The blue-component of the color.</param>
|
||||
/// <param name="g">The green-component of the color.</param>
|
||||
/// <param name="r">The red-component of the color.</param>
|
||||
/// <param name="a">The alpha-component of the color.</param>
|
||||
public ColorBGRA(byte b, byte g, byte r, byte a)
|
||||
{
|
||||
this._b = b;
|
||||
this._g = g;
|
||||
this._r = r;
|
||||
this._a = a;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"[A: {_a}, R: {_r}, G: {_g}, B: {_b}]";
|
||||
|
||||
#endregion
|
||||
}
|
||||
58
StableDiffusion.NET/Helper/Images/Colors/ColorFormat.cs
Normal file
58
StableDiffusion.NET/Helper/Images/Colors/ColorFormat.cs
Normal file
@ -0,0 +1,58 @@
|
||||
namespace StableDiffusion.NET.Helper.Images.Colors;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color format.
|
||||
/// </summary>
|
||||
public readonly struct ColorFormat
|
||||
{
|
||||
#region Instances
|
||||
|
||||
public static readonly ColorFormat BGRA = new(1, 4);
|
||||
public static readonly ColorFormat ABGR = new(2, 4);
|
||||
public static readonly ColorFormat RGBA = new(3, 4);
|
||||
public static readonly ColorFormat ARGB = new(4, 4);
|
||||
public static readonly ColorFormat BGR = new(5, 3);
|
||||
public static readonly ColorFormat RGB = new(6, 3);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties & Fields
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Id of the color-format.
|
||||
/// </summary>
|
||||
public readonly int Id;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Bytes per pixel for this color-format.
|
||||
/// </summary>
|
||||
public readonly int BytesPerPixel;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
private ColorFormat(int id, int bytesPerPixel)
|
||||
{
|
||||
this.Id = id;
|
||||
this.BytesPerPixel = bytesPerPixel;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public bool Equals(ColorFormat other) => Id == other.Id;
|
||||
public override bool Equals(object? obj) => obj is ColorFormat other && Equals(other);
|
||||
|
||||
public override int GetHashCode() => Id;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Operators
|
||||
|
||||
public static bool operator ==(ColorFormat left, ColorFormat right) => left.Equals(right);
|
||||
public static bool operator !=(ColorFormat left, ColorFormat right) => !(left == right);
|
||||
|
||||
#endregion
|
||||
}
|
||||
63
StableDiffusion.NET/Helper/Images/Colors/ColorRGB.cs
Normal file
63
StableDiffusion.NET/Helper/Images/Colors/ColorRGB.cs
Normal file
@ -0,0 +1,63 @@
|
||||
// ReSharper disable ConvertToAutoProperty
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StableDiffusion.NET.Helper.Images.Colors;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color in 24 bit RGB-format.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct ColorRGB : IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc />
|
||||
public static ColorFormat ColorFormat => ColorFormat.RGB;
|
||||
|
||||
private readonly byte _r;
|
||||
private readonly byte _g;
|
||||
private readonly byte _b;
|
||||
|
||||
// ReSharper disable ConvertToAutoPropertyWhenPossible
|
||||
/// <inheritdoc />
|
||||
public byte A => byte.MaxValue;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte R => _r;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte G => _g;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte B => _b;
|
||||
// ReSharper restore ConvertToAutoPropertyWhenPossible
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ColorRGB"/> class.
|
||||
/// </summary>
|
||||
/// <param name="r">The red-component of the color.</param>
|
||||
/// <param name="g">The green-component of the color.</param>
|
||||
/// <param name="b">The blue-component of the color.</param>
|
||||
public ColorRGB(byte r, byte g, byte b)
|
||||
{
|
||||
this._r = r;
|
||||
this._g = g;
|
||||
this._b = b;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"[A: {A}, R: {_r}, G: {_g}, B: {_b}]";
|
||||
|
||||
#endregion
|
||||
}
|
||||
66
StableDiffusion.NET/Helper/Images/Colors/ColorRGBA.cs
Normal file
66
StableDiffusion.NET/Helper/Images/Colors/ColorRGBA.cs
Normal file
@ -0,0 +1,66 @@
|
||||
// ReSharper disable ConvertToAutoProperty
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StableDiffusion.NET.Helper.Images.Colors;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color in 32 bit RGBA-format.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct ColorRGBA : IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc />
|
||||
public static ColorFormat ColorFormat => ColorFormat.RGBA;
|
||||
|
||||
private readonly byte _r;
|
||||
private readonly byte _g;
|
||||
private readonly byte _b;
|
||||
private readonly byte _a;
|
||||
|
||||
// ReSharper disable ConvertToAutoPropertyWhenPossible
|
||||
/// <inheritdoc />
|
||||
public byte R => _r;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte G => _g;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte B => _b;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte A => _a;
|
||||
// ReSharper restore ConvertToAutoPropertyWhenPossible
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ColorRGBA"/> class.
|
||||
/// </summary>
|
||||
/// <param name="r">The red-component of the color.</param>
|
||||
/// <param name="g">The green-component of the color.</param>
|
||||
/// <param name="b">The blue-component of the color.</param>
|
||||
/// <param name="a">The alpha-component of the color.</param>
|
||||
public ColorRGBA(byte r, byte g, byte b, byte a)
|
||||
{
|
||||
this._r = r;
|
||||
this._g = g;
|
||||
this._b = b;
|
||||
this._a = a;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"[A: {_a}, R: {_r}, G: {_g}, B: {_b}]";
|
||||
|
||||
#endregion
|
||||
}
|
||||
34
StableDiffusion.NET/Helper/Images/Colors/IColor.cs
Normal file
34
StableDiffusion.NET/Helper/Images/Colors/IColor.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
|
||||
namespace StableDiffusion.NET.Helper.Images.Colors;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a generic color made of 4 bytes (alpha, red, green and blue)
|
||||
/// </summary>
|
||||
public interface IColor
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the red-component of this color.
|
||||
/// </summary>
|
||||
byte R { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the green-component of this color.
|
||||
/// </summary>
|
||||
byte G { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the blue-component of this color.
|
||||
/// </summary>
|
||||
byte B { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the alpha-component of this color.
|
||||
/// </summary>
|
||||
byte A { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the color-format of this color.
|
||||
/// </summary>
|
||||
public static virtual ColorFormat ColorFormat => throw new NotSupportedException();
|
||||
}
|
||||
193
StableDiffusion.NET/Helper/Images/IImage.cs
Normal file
193
StableDiffusion.NET/Helper/Images/IImage.cs
Normal file
@ -0,0 +1,193 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using StableDiffusion.NET.Helper.Images.Colors;
|
||||
|
||||
namespace StableDiffusion.NET.Helper.Images;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a image.
|
||||
/// </summary>
|
||||
public interface IImage : IEnumerable<IColor>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the color format used in this image.
|
||||
/// </summary>
|
||||
ColorFormat ColorFormat { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the width of this image.
|
||||
/// </summary>
|
||||
int Width { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the height of this image.
|
||||
/// </summary>
|
||||
int Height { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of this image.
|
||||
/// </summary>
|
||||
int SizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the color at the specified location.
|
||||
/// </summary>
|
||||
/// <param name="x">The X-location to read.</param>
|
||||
/// <param name="y">The Y-location to read.</param>
|
||||
/// <returns>The color at the specified location.</returns>
|
||||
IColor this[int x, int y] { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets an image representing the specified location.
|
||||
/// </summary>
|
||||
/// <param name="x">The X-location of the image.</param>
|
||||
/// <param name="y">The Y-location of the image.</param>
|
||||
/// <param name="width">The width of the sub-image.</param>
|
||||
/// <param name="height"></param>
|
||||
/// <returns></returns>
|
||||
IImage this[int x, int y, int width, int height] { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of all rows of this image.
|
||||
/// </summary>
|
||||
IImageRows Rows { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of all columns of this image.
|
||||
/// </summary>
|
||||
IImageColumns Columns { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets an <see cref="RefImage{TColor}"/> representing this <see cref="IImage"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TColor">The color-type of the iamge.</typeparam>
|
||||
/// <returns>The <inheritdoc cref="RefImage{TColor}"/>.</returns>
|
||||
RefImage<TColor> AsRefImage<TColor>() where TColor : struct, IColor;
|
||||
|
||||
/// <summary>
|
||||
/// Copies the contents of this <see cref="IImage"/> into a destination <see cref="Span{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImage"/> instance.
|
||||
/// </exception>
|
||||
void CopyTo(in Span<byte> destination);
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new array and copies this <see cref="IImage"/> into it.
|
||||
/// </summary>
|
||||
/// <returns>The new array containing the data of this <see cref="IImage"/>.</returns>
|
||||
byte[] ToArray();
|
||||
|
||||
/// <summary>
|
||||
/// Represents a list of rows of an image.
|
||||
/// </summary>
|
||||
public interface IImageRows : IEnumerable<IImageRow>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the amount of rows in this list.
|
||||
/// </summary>
|
||||
int Count { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a specific <see cref="IImageRow"/>.
|
||||
/// </summary>
|
||||
/// <param name="column">The ´row to get.</param>
|
||||
/// <returns>The requested <see cref="IImageRow"/>.</returns>
|
||||
IImageRow this[int column] { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a list of columns of an image.
|
||||
/// </summary>
|
||||
public interface IImageColumns : IEnumerable<IImageColumn>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the amount of columns in this list.
|
||||
/// </summary>
|
||||
int Count { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a specific <see cref="IImageColumn"/>.
|
||||
/// </summary>
|
||||
/// <param name="column">The column to get.</param>
|
||||
/// <returns>The requested <see cref="IImageColumn"/>.</returns>
|
||||
IImageColumn this[int column] { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a single row of an image.
|
||||
/// </summary>
|
||||
public interface IImageRow : IEnumerable<IColor>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the length of the row.
|
||||
/// </summary>
|
||||
int Length { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of this row.
|
||||
/// </summary>
|
||||
int SizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IColor"/> at the specified location.
|
||||
/// </summary>
|
||||
/// <param name="x">The location to get the color from.</param>
|
||||
/// <returns>The <see cref="IColor"/> at the specified location.</returns>
|
||||
IColor this[int x] { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Copies the contents of this <see cref="IImageRow"/> into a destination <see cref="Span{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImageRow"/> instance.
|
||||
/// </exception>
|
||||
void CopyTo(in Span<byte> destination);
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new array and copies this <see cref="IImageRow"/> into it.
|
||||
/// </summary>
|
||||
/// <returns>The new array containing the data of this <see cref="IImageRow"/>.</returns>
|
||||
byte[] ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a single column of an image.
|
||||
/// </summary>
|
||||
public interface IImageColumn : IEnumerable<IColor>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the length of the column.
|
||||
/// </summary>
|
||||
int Length { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of this column.
|
||||
/// </summary>
|
||||
int SizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IColor"/> at the specified location.
|
||||
/// </summary>
|
||||
/// <param name="y">The location to get the color from.</param>
|
||||
/// <returns>The <see cref="IColor"/> at the specified location.</returns>
|
||||
IColor this[int y] { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Copies the contents of this <see cref="IImageColumn"/> into a destination <see cref="Span{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImageColumn"/> instance.
|
||||
/// </exception>
|
||||
void CopyTo(in Span<byte> destination);
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new array and copies this <see cref="IImageColumn"/> into it.
|
||||
/// </summary>
|
||||
/// <returns>The new array containing the data of this <see cref="IImageColumn"/>.</returns>
|
||||
byte[] ToArray();
|
||||
}
|
||||
}
|
||||
431
StableDiffusion.NET/Helper/Images/Image.cs
Normal file
431
StableDiffusion.NET/Helper/Images/Image.cs
Normal file
@ -0,0 +1,431 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using StableDiffusion.NET.Helper.Images.Colors;
|
||||
|
||||
namespace StableDiffusion.NET.Helper.Images;
|
||||
|
||||
/// <inheritdoc />
|
||||
public sealed class Image<TColor> : IImage
|
||||
where TColor : struct, IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly byte[] _buffer;
|
||||
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _stride;
|
||||
|
||||
/// <inheritdoc />
|
||||
public ColorFormat ColorFormat
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => TColor.ColorFormat;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Width { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Height { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public int SizeInBytes => Width * Height * ColorFormat.BytesPerPixel;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
/// <inheritdoc />
|
||||
public IColor this[int x, int y]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException();
|
||||
|
||||
return MemoryMarshal.Cast<byte, TColor>(_buffer)[((_y + y) * _stride) + (_x + x)];
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IImage this[int x, int y, int width, int height]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new IndexOutOfRangeException();
|
||||
|
||||
return new Image<TColor>(_buffer, _x + x, _y + y, width, height, _stride);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IImage.IImageRows Rows
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new ImageRows(_buffer, _x, _y, Width, Height, _stride);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IImage.IImageColumns Columns
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new ImageColumns(_buffer, _x, _y, Width, Height, _stride);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public Image(byte[] buffer, int x, int y, int width, int height, int stride)
|
||||
{
|
||||
this._buffer = buffer;
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this.Width = width;
|
||||
this.Height = height;
|
||||
this._stride = stride;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public void CopyTo(in Span<byte> destination)
|
||||
{
|
||||
if (destination == null) throw new ArgumentNullException(nameof(destination));
|
||||
if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
|
||||
|
||||
int targetStride = Width * ColorFormat.BytesPerPixel;
|
||||
IImage.IImageRows rows = Rows;
|
||||
Span<byte> target = destination;
|
||||
foreach (IImage.IImageRow row in rows)
|
||||
{
|
||||
row.CopyTo(target);
|
||||
target = target[targetStride..];
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte[] ToArray()
|
||||
{
|
||||
byte[] array = new byte[SizeInBytes];
|
||||
CopyTo(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public RefImage<T> AsRefImage<T>()
|
||||
where T : struct, IColor
|
||||
{
|
||||
if (typeof(T) != typeof(TColor)) throw new ArgumentException("The requested color format does not fit this image.", nameof(T));
|
||||
|
||||
return new RefImage<T>(MemoryMarshal.Cast<byte, T>(_buffer), _x, _y, Width, Height, _stride);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<IColor> GetEnumerator()
|
||||
{
|
||||
for (int y = 0; y < Height; y++)
|
||||
for (int x = 0; x < Width; x++)
|
||||
yield return this[x, y];
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer-Classes
|
||||
|
||||
/// <inheritdoc />
|
||||
private sealed class ImageRows : IImage.IImageRows
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly byte[] _buffer;
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _width;
|
||||
private readonly int _height;
|
||||
private readonly int _stride;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Count => _height;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
/// <inheritdoc />
|
||||
public IImage.IImageRow this[int row]
|
||||
{
|
||||
get
|
||||
{
|
||||
if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException();
|
||||
|
||||
return new ImageRow(_buffer, (((row + _y) * _stride) + _x), _width);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
internal ImageRows(byte[] buffer, int x, int y, int width, int height, int stride)
|
||||
{
|
||||
this._buffer = buffer;
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this._width = width;
|
||||
this._height = height;
|
||||
this._stride = stride;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<IImage.IImageRow> GetEnumerator()
|
||||
{
|
||||
for (int y = 0; y < _height; y++)
|
||||
yield return this[y];
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
private sealed class ImageRow : IImage.IImageRow
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly byte[] _buffer;
|
||||
private readonly int _start;
|
||||
private readonly int _length;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Length => _length;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
/// <inheritdoc />
|
||||
public IColor this[int x]
|
||||
{
|
||||
get
|
||||
{
|
||||
if ((x < 0) || (x >= _length)) throw new IndexOutOfRangeException();
|
||||
|
||||
ReadOnlySpan<TColor> row = MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..];
|
||||
return row[x];
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
internal ImageRow(byte[] buffer, int start, int length)
|
||||
{
|
||||
this._buffer = buffer;
|
||||
this._start = start;
|
||||
this._length = length;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public void CopyTo(in Span<byte> destination)
|
||||
{
|
||||
if (destination == null) throw new ArgumentNullException(nameof(destination));
|
||||
if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
|
||||
|
||||
MemoryMarshal.Cast<byte, TColor>(_buffer).Slice(_start, _length).CopyTo(MemoryMarshal.Cast<byte, TColor>(destination));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte[] ToArray()
|
||||
{
|
||||
byte[] array = new byte[SizeInBytes];
|
||||
CopyTo(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<IColor> GetEnumerator()
|
||||
{
|
||||
for (int x = 0; x < _length; x++)
|
||||
yield return this[x];
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
private sealed class ImageColumns : IImage.IImageColumns
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly byte[] _buffer;
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _width;
|
||||
private readonly int _height;
|
||||
private readonly int _stride;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Count => _width;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
/// <inheritdoc />
|
||||
public IImage.IImageColumn this[int column]
|
||||
{
|
||||
get
|
||||
{
|
||||
if ((column < 0) || (column >= _width)) throw new IndexOutOfRangeException();
|
||||
|
||||
return new ImageColumn(_buffer, (_y * _stride) + _x + column, _height, _stride);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
internal ImageColumns(byte[] buffer, int x, int y, int width, int height, int stride)
|
||||
{
|
||||
this._buffer = buffer;
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this._width = width;
|
||||
this._height = height;
|
||||
this._stride = stride;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<IImage.IImageColumn> GetEnumerator()
|
||||
{
|
||||
for (int y = 0; y < _height; y++)
|
||||
yield return this[y];
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
private sealed class ImageColumn : IImage.IImageColumn
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly byte[] _buffer;
|
||||
private readonly int _start;
|
||||
private readonly int _length;
|
||||
private readonly int _step;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Length => _length;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
/// <inheritdoc />
|
||||
public IColor this[int y]
|
||||
{
|
||||
get
|
||||
{
|
||||
if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException();
|
||||
|
||||
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..];
|
||||
return data[y * _step];
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
internal ImageColumn(byte[] buffer, int start, int length, int step)
|
||||
{
|
||||
this._buffer = buffer;
|
||||
this._start = start;
|
||||
this._length = length;
|
||||
this._step = step;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public void CopyTo(in Span<byte> destination)
|
||||
{
|
||||
if (destination == null) throw new ArgumentNullException(nameof(destination));
|
||||
if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
|
||||
|
||||
if (_step == 1)
|
||||
_buffer.AsSpan(_start, SizeInBytes).CopyTo(destination);
|
||||
else
|
||||
{
|
||||
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..];
|
||||
Span<TColor> target = MemoryMarshal.Cast<byte, TColor>(destination);
|
||||
for (int i = 0; i < Length; i++)
|
||||
target[i] = data[i * _step];
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte[] ToArray()
|
||||
{
|
||||
byte[] array = new byte[SizeInBytes];
|
||||
CopyTo(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<IColor> GetEnumerator()
|
||||
{
|
||||
for (int y = 0; y < _length; y++)
|
||||
yield return this[y];
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
266
StableDiffusion.NET/Helper/Images/ReadOnlyRefEnumerable.cs
Normal file
266
StableDiffusion.NET/Helper/Images/ReadOnlyRefEnumerable.cs
Normal file
@ -0,0 +1,266 @@
|
||||
// DarthAffe 05.09.2023: Based on https://github.com/CommunityToolkit/dotnet/blob/b0d6c4f9c0cfb5d860400abb00b0ca1b3e94dfa4/src/CommunityToolkit.HighPerformance/Enumerables/ReadOnlyRefEnumerable%7BT%7D.cs
|
||||
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StableDiffusion.NET.Helper.Images;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="ref"/> <see langword="struct"/> that iterates readonly items from arbitrary memory locations.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to enumerate.</typeparam>
|
||||
public readonly ref struct ReadOnlyRefEnumerable<T>
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ReadOnlySpan{T}"/> instance pointing to the first item in the target memory area.
|
||||
/// </summary>
|
||||
/// <remarks>The <see cref="ReadOnlySpan{T}.Length"/> field maps to the total available length.</remarks>
|
||||
private readonly ReadOnlySpan<T> _span;
|
||||
|
||||
/// <summary>
|
||||
/// The distance between items in the sequence to enumerate.
|
||||
/// </summary>
|
||||
/// <remarks>The distance refers to <typeparamref name="T"/> items, not byte offset.</remarks>
|
||||
private readonly int _step;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total available length for the sequence.
|
||||
/// </summary>
|
||||
public int Length
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _span.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the element at the specified zero-based index.
|
||||
/// </summary>
|
||||
/// <param name="index">The zero-based index of the element.</param>
|
||||
/// <returns>A reference to the element at the specified index.</returns>
|
||||
/// <exception cref="IndexOutOfRangeException">
|
||||
/// Thrown when <paramref name="index"/> is invalid.
|
||||
/// </exception>
|
||||
public ref readonly T this[int index]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((uint)index >= (uint)Length) throw new IndexOutOfRangeException();
|
||||
|
||||
ref T r0 = ref MemoryMarshal.GetReference(_span);
|
||||
nint offset = (nint)(uint)index * (nint)(uint)_step;
|
||||
ref T ri = ref Unsafe.Add(ref r0, offset);
|
||||
|
||||
return ref ri;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the element at the specified zero-based index.
|
||||
/// </summary>
|
||||
/// <param name="index">The zero-based index of the element.</param>
|
||||
/// <returns>A reference to the element at the specified index.</returns>
|
||||
/// <exception cref="IndexOutOfRangeException">
|
||||
/// Thrown when <paramref name="index"/> is invalid.
|
||||
/// </exception>
|
||||
public ref readonly T this[Index index]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref this[index.GetOffset(Length)];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ReadOnlyRefEnumerable{T}"/> struct.
|
||||
/// </summary>
|
||||
/// <param name="reference">A reference to the first item of the sequence.</param>
|
||||
/// <param name="length">The number of items in the sequence.</param>
|
||||
/// <param name="step">The distance between items in the sequence to enumerate.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ReadOnlyRefEnumerable(in T reference, int length, int step)
|
||||
{
|
||||
this._step = step;
|
||||
|
||||
_span = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in reference), length);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Enumerator GetEnumerator() => new(_span, _step);
|
||||
|
||||
public T[] ToArray()
|
||||
{
|
||||
int length = _span.Length;
|
||||
|
||||
// Empty array if no data is mapped
|
||||
if (length == 0)
|
||||
return Array.Empty<T>();
|
||||
|
||||
T[] array = new T[length];
|
||||
CopyTo(array);
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the contents of this <see cref="ReadOnlyRefEnumerable{T}"/> into a destination <see cref="Span{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="ReadOnlyRefEnumerable{T}"/> instance.
|
||||
/// </exception>
|
||||
public void CopyTo(Span<T> destination)
|
||||
{
|
||||
if (_step == 1)
|
||||
{
|
||||
_span.CopyTo(destination);
|
||||
return;
|
||||
}
|
||||
|
||||
ref T sourceRef = ref MemoryMarshal.GetReference(_span);
|
||||
int length = _span.Length;
|
||||
if ((uint)destination.Length < (uint)length)
|
||||
throw new ArgumentException("The target span is too short to copy all the current items to.");
|
||||
|
||||
ref T destinationRef = ref MemoryMarshal.GetReference(destination);
|
||||
|
||||
CopyTo(ref sourceRef, ref destinationRef, (nint)(uint)length, (nint)(uint)_step);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to copy the current <see cref="ReadOnlyRefEnumerable{T}"/> instance to a destination <see cref="Span{T}"/>.
|
||||
/// </summary>
|
||||
/// <param name="destination">The target <see cref="Span{T}"/> of the copy operation.</param>
|
||||
/// <returns>Whether or not the operation was successful.</returns>
|
||||
public bool TryCopyTo(Span<T> destination)
|
||||
{
|
||||
if (destination.Length >= _span.Length)
|
||||
{
|
||||
CopyTo(destination);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void CopyTo(ref T sourceRef, ref T destinationRef, nint length, nint sourceStep)
|
||||
{
|
||||
nint sourceOffset = 0;
|
||||
nint destinationOffset = 0;
|
||||
|
||||
while (length >= 8)
|
||||
{
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 0) = Unsafe.Add(ref sourceRef, sourceOffset);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 1) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 2) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 3) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 4) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 5) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 6) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 7) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
|
||||
length -= 8;
|
||||
sourceOffset += sourceStep;
|
||||
destinationOffset += 8;
|
||||
}
|
||||
|
||||
if (length >= 4)
|
||||
{
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 0) = Unsafe.Add(ref sourceRef, sourceOffset);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 1) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 2) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 3) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
|
||||
length -= 4;
|
||||
sourceOffset += sourceStep;
|
||||
destinationOffset += 4;
|
||||
}
|
||||
|
||||
while (length > 0)
|
||||
{
|
||||
Unsafe.Add(ref destinationRef, destinationOffset) = Unsafe.Add(ref sourceRef, sourceOffset);
|
||||
|
||||
length -= 1;
|
||||
sourceOffset += sourceStep;
|
||||
destinationOffset += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// A custom enumerator type to traverse items within a <see cref="ReadOnlyRefEnumerable{T}"/> instance.
|
||||
/// </summary>
|
||||
public ref struct Enumerator
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc cref="ReadOnlyRefEnumerable{T}._span"/>
|
||||
private readonly ReadOnlySpan<T> _span;
|
||||
|
||||
/// <inheritdoc cref="ReadOnlyRefEnumerable{T}._step"/>
|
||||
private readonly int _step;
|
||||
|
||||
/// <summary>
|
||||
/// The current position in the sequence.
|
||||
/// </summary>
|
||||
private int _position;
|
||||
|
||||
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
|
||||
public readonly ref readonly T Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ref T r0 = ref MemoryMarshal.GetReference(_span);
|
||||
|
||||
nint offset = (nint)(uint)_position * (nint)(uint)_step;
|
||||
ref T ri = ref Unsafe.Add(ref r0, offset);
|
||||
|
||||
return ref ri;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Enumerator"/> struct.
|
||||
/// </summary>
|
||||
/// <param name="span">The <see cref="ReadOnlySpan{T}"/> instance with the info on the items to traverse.</param>
|
||||
/// <param name="step">The distance between items in the sequence to enumerate.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal Enumerator(ReadOnlySpan<T> span, int step)
|
||||
{
|
||||
this._span = span;
|
||||
this._step = step;
|
||||
|
||||
_position = -1;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext() => ++_position < _span.Length;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
368
StableDiffusion.NET/Helper/Images/RefImage.cs
Normal file
368
StableDiffusion.NET/Helper/Images/RefImage.cs
Normal file
@ -0,0 +1,368 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using StableDiffusion.NET.Helper.Images.Colors;
|
||||
|
||||
namespace StableDiffusion.NET.Helper.Images;
|
||||
|
||||
public readonly ref struct RefImage<TColor>
|
||||
where TColor : struct, IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly ReadOnlySpan<TColor> _pixels;
|
||||
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the width of the image.
|
||||
/// </summary>
|
||||
public int Width { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the height of the image.
|
||||
/// </summary>
|
||||
public int Height { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the stride (entries per row) of the underlying buffer.
|
||||
/// Only useful if you want to work with a pinned buffer.
|
||||
/// </summary>
|
||||
public int RawStride { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
public ref readonly TColor this[int x, int y]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException();
|
||||
|
||||
ref TColor r0 = ref MemoryMarshal.GetReference(_pixels);
|
||||
nint offset = (nint)(uint)((_y + y) * RawStride) + (_x + x);
|
||||
return ref Unsafe.Add(ref r0, offset);
|
||||
}
|
||||
}
|
||||
|
||||
public RefImage<TColor> this[int x, int y, int width, int height]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new IndexOutOfRangeException();
|
||||
|
||||
return new RefImage<TColor>(_pixels, _x + x, _y + y, width, height, RawStride);
|
||||
}
|
||||
}
|
||||
|
||||
public ImageRows Rows
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new(_pixels, _x, _y, Width, Height, RawStride);
|
||||
}
|
||||
public ImageColumns Columns
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new(_pixels, _x, _y, Width, Height, RawStride);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public RefImage(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride)
|
||||
{
|
||||
this._pixels = pixels;
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this.Width = width;
|
||||
this.Height = height;
|
||||
this.RawStride = stride;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Copies the contents of this <see cref="RefImage{TColor}"/> into a destination <see cref="Span{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="RefImage{TColor}"/> instance.
|
||||
/// </exception>
|
||||
public void CopyTo(in Span<TColor> destination)
|
||||
{
|
||||
if (destination == null) throw new ArgumentNullException(nameof(destination));
|
||||
if (destination.Length < (Width * Height)) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
|
||||
|
||||
ImageRows rows = Rows;
|
||||
Span<TColor> target = destination;
|
||||
foreach (ReadOnlyRefEnumerable<TColor> row in rows)
|
||||
{
|
||||
row.CopyTo(target);
|
||||
target = target[Width..];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new array and copies this <see cref="RefImage{TColor}"/> into it.
|
||||
/// </summary>
|
||||
/// <returns>The new array containing the data of this <see cref="RefImage{TColor}"/>.</returns>
|
||||
public TColor[] ToArray()
|
||||
{
|
||||
TColor[] array = new TColor[Width * Height];
|
||||
CopyTo(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a reference to the first element of this image inside the full image buffer.
|
||||
/// </summary>
|
||||
public ref readonly TColor GetPinnableReference()
|
||||
{
|
||||
if (_pixels.Length == 0)
|
||||
return ref Unsafe.NullRef<TColor>();
|
||||
|
||||
int offset = (_y * RawStride) + _x;
|
||||
return ref MemoryMarshal.GetReference(_pixels[offset..]);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ImageEnumerator GetEnumerator() => new(_pixels);
|
||||
|
||||
#endregion
|
||||
|
||||
public ref struct ImageEnumerator
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly ReadOnlySpan<TColor> _pixels;
|
||||
private int _position;
|
||||
|
||||
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
|
||||
public TColor Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _pixels[_position];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ImageEnumerator(ReadOnlySpan<TColor> pixels)
|
||||
{
|
||||
this._pixels = pixels;
|
||||
|
||||
_position = -1;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext() => ++_position < _pixels.Length;
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region Indexer-Structs
|
||||
|
||||
public readonly ref struct ImageRows
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly ReadOnlySpan<TColor> _pixels;
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _width;
|
||||
private readonly int _height;
|
||||
private readonly int _stride;
|
||||
|
||||
public int Count => _height;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
public readonly ReadOnlyRefEnumerable<TColor> this[int row]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((row < 0) || (row > _height)) throw new IndexOutOfRangeException();
|
||||
|
||||
ref TColor r0 = ref MemoryMarshal.GetReference(_pixels);
|
||||
ref TColor rr = ref Unsafe.Add(ref r0, (nint)(uint)(((row + _y) * _stride) + _x));
|
||||
|
||||
return new ReadOnlyRefEnumerable<TColor>(rr, _width, 1);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public ImageRows(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride)
|
||||
{
|
||||
this._pixels = pixels;
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this._width = width;
|
||||
this._height = height;
|
||||
this._stride = stride;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ImageRowsEnumerator GetEnumerator() => new(this);
|
||||
|
||||
#endregion
|
||||
|
||||
public ref struct ImageRowsEnumerator
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly ImageRows _rows;
|
||||
private int _position;
|
||||
|
||||
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
|
||||
public ReadOnlyRefEnumerable<TColor> Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _rows[_position];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ImageRowsEnumerator(ImageRows rows)
|
||||
{
|
||||
this._rows = rows;
|
||||
|
||||
_position = -1;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext() => ++_position < _rows._height;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
public readonly ref struct ImageColumns
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly ReadOnlySpan<TColor> _pixels;
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _width;
|
||||
private readonly int _height;
|
||||
private readonly int _stride;
|
||||
|
||||
public int Count => _width;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
public ReadOnlyRefEnumerable<TColor> this[int column]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((column < 0) || (column > _width)) throw new IndexOutOfRangeException();
|
||||
|
||||
ref TColor r0 = ref MemoryMarshal.GetReference(_pixels);
|
||||
ref TColor rc = ref Unsafe.Add(ref r0, (nint)(uint)((_y * _stride) + (column + _x)));
|
||||
|
||||
return new ReadOnlyRefEnumerable<TColor>(rc, _height, _stride);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public ImageColumns(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride)
|
||||
{
|
||||
this._pixels = pixels;
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this._width = width;
|
||||
this._height = height;
|
||||
this._stride = stride;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ImageColumnsEnumerator GetEnumerator() => new(this);
|
||||
|
||||
#endregion
|
||||
|
||||
public ref struct ImageColumnsEnumerator
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly ImageColumns _columns;
|
||||
private int _position;
|
||||
|
||||
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
|
||||
public ReadOnlyRefEnumerable<TColor> Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _columns[_position];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ImageColumnsEnumerator(ImageColumns columns)
|
||||
{
|
||||
this._columns = columns;
|
||||
this._position = -1;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext() => ++_position < _columns._width;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
27
StableDiffusion.NET/Helper/ProcessHelper.cs
Normal file
27
StableDiffusion.NET/Helper/ProcessHelper.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace StableDiffusion.NET.Helper;
|
||||
|
||||
internal static class ProcessHelper
|
||||
{
|
||||
public static string? RunCommand(string command)
|
||||
{
|
||||
try
|
||||
{
|
||||
using Process process = new();
|
||||
process.StartInfo.UseShellExecute = false;
|
||||
process.StartInfo.RedirectStandardOutput = true;
|
||||
process.StartInfo.FileName = command;
|
||||
process.Start();
|
||||
|
||||
string output = process.StandardOutput.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
|
||||
return output;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
104
StableDiffusion.NET/Native/Native.Load.cs
Normal file
104
StableDiffusion.NET/Native/Native.Load.cs
Normal file
@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StableDiffusion.NET;
|
||||
|
||||
internal static partial class Native
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private static nint _loadedLibraryHandle;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
static Native()
|
||||
{
|
||||
NativeLibrary.SetDllImportResolver(typeof(Native).Assembly, ResolveDllImport);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
private static nint ResolveDllImport(string libraryname, Assembly assembly, DllImportSearchPath? searchpath)
|
||||
{
|
||||
if (libraryname != LIB_NAME) return nint.Zero;
|
||||
if (_loadedLibraryHandle != nint.Zero) return _loadedLibraryHandle;
|
||||
|
||||
_loadedLibraryHandle = TryLoadLibrary();
|
||||
|
||||
return _loadedLibraryHandle;
|
||||
}
|
||||
|
||||
private static nint TryLoadLibrary()
|
||||
{
|
||||
GetPlatformPathParts(out string os, out string fileExtension, out string libPrefix);
|
||||
|
||||
foreach (IBackend backend in Backends.ActiveBackends.OrderByDescending(x => x.Priority))
|
||||
{
|
||||
string path = Backends.GetFullPath(os, backend.PathPart, $"{libPrefix}{LIB_NAME}{fileExtension}");
|
||||
if (string.IsNullOrWhiteSpace(path)) continue;
|
||||
|
||||
string fullPath = TryFindPath(path);
|
||||
nint result = TryLoad(fullPath);
|
||||
|
||||
if (result != nint.Zero)
|
||||
return result;
|
||||
}
|
||||
|
||||
return nint.Zero;
|
||||
|
||||
static nint TryLoad(string path)
|
||||
{
|
||||
if (NativeLibrary.TryLoad(path, out nint handle))
|
||||
return handle;
|
||||
|
||||
return nint.Zero;
|
||||
}
|
||||
|
||||
static string TryFindPath(string filename)
|
||||
{
|
||||
IEnumerable<string> searchPaths = [.. Backends.SearchPaths, AppDomain.CurrentDomain.BaseDirectory, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? ""];
|
||||
foreach (string path in searchPaths)
|
||||
{
|
||||
string candidate = Path.Combine(path, filename);
|
||||
if (File.Exists(candidate))
|
||||
return candidate;
|
||||
}
|
||||
|
||||
return filename;
|
||||
}
|
||||
}
|
||||
|
||||
private static void GetPlatformPathParts(out string os, out string fileExtension, out string libPrefix)
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
os = "win-x64";
|
||||
fileExtension = ".dll";
|
||||
libPrefix = "";
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
os = "linux-x64";
|
||||
fileExtension = ".so";
|
||||
libPrefix = "lib";
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
fileExtension = ".dylib";
|
||||
os = "osx-x64";
|
||||
libPrefix = "lib";
|
||||
}
|
||||
else
|
||||
throw new NotSupportedException("Your operating system is not supported.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -4,6 +4,53 @@
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
|
||||
<Authors>Darth Affe</Authors>
|
||||
<Company>Wyrez</Company>
|
||||
<Language>en-US</Language>
|
||||
<NeutralLanguage>en-US</NeutralLanguage>
|
||||
<Title>StableDiffusion.NET</Title>
|
||||
<AssemblyName>StableDiffusion.NET</AssemblyName>
|
||||
<AssemblyTitle>StableDiffusion.NET</AssemblyTitle>
|
||||
<PackageId>StableDiffusion.NET</PackageId>
|
||||
<RootNamespace>StableDiffusion.NET</RootNamespace>
|
||||
<Description>Stable-Diffusion for .NET based on stable-diffusion.cpp</Description>
|
||||
<Summary>Stable-Diffusion for .NET based on stable-diffusion.cpp</Summary>
|
||||
<Copyright>Copyright © Darth Affe 2024</Copyright>
|
||||
<PackageCopyright>Copyright © Darth Affe 2024</PackageCopyright>
|
||||
<PackageIcon>sd_net.png</PackageIcon>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<PackageProjectUrl>https://github.com/DarthAffe/StableDiffusion.NET</PackageProjectUrl>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<RepositoryType>Github</RepositoryType>
|
||||
<RepositoryUrl>https://github.com/DarthAffe/StableDiffusion.NET</RepositoryUrl>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
|
||||
<Version>0.0.1</Version>
|
||||
<AssemblyVersion>0.0.1</AssemblyVersion>
|
||||
<FileVersion>0.0.1</FileVersion>
|
||||
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<IncludeSource>True</IncludeSource>
|
||||
<IncludeSymbols>True</IncludeSymbols>
|
||||
<DebugType>portable</DebugType>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<DefineConstants>TRACE;DEBUG</DefineConstants>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<Optimize>false</Optimize>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
|
||||
<Optimize>true</Optimize>
|
||||
<NoWarn>$(NoWarn);CS1591;CS1572;CS1573</NoWarn>
|
||||
<DefineConstants>RELEASE</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="sd_net.png" Link="sd_net.png" Pack="true" PackagePath="\" />
|
||||
<None Include="..\README.md" Pack="true" PackagePath="\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=attributes/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=enums/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=eventargs/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=extensions/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=extensions/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=native/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||
BIN
StableDiffusion.NET/sd_net.png
Normal file
BIN
StableDiffusion.NET/sd_net.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 452 KiB |
Loading…
x
Reference in New Issue
Block a user