diff --git a/StableDiffusion.NET/Extensions/ImageExtensions.cs b/StableDiffusion.NET/Extensions/ImageExtension.cs similarity index 100% rename from StableDiffusion.NET/Extensions/ImageExtensions.cs rename to StableDiffusion.NET/Extensions/ImageExtension.cs diff --git a/StableDiffusion.NET/Extensions/ParameterExtension.cs b/StableDiffusion.NET/Extensions/ParameterExtension.cs new file mode 100644 index 0000000..13fd6d8 --- /dev/null +++ b/StableDiffusion.NET/Extensions/ParameterExtension.cs @@ -0,0 +1,70 @@ +#pragma warning disable CA2208 + +using System; + +namespace StableDiffusion.NET; + +public static class ParameterExtension +{ + public static void Validate(this StableDiffusionParameter parameter) + { + ArgumentNullException.ThrowIfNull(parameter, nameof(parameter)); + ArgumentNullException.ThrowIfNull(parameter.ControlNet, nameof(StableDiffusionParameter.ControlNet)); + ArgumentNullException.ThrowIfNull(parameter.PhotoMaker, nameof(StableDiffusionParameter.PhotoMaker)); + ArgumentNullException.ThrowIfNull(parameter.NegativePrompt, nameof(StableDiffusionParameter.NegativePrompt)); + + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(parameter.Width, nameof(StableDiffusionParameter.Width)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(parameter.Height, nameof(StableDiffusionParameter.Height)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(parameter.SampleSteps, nameof(StableDiffusionParameter.SampleSteps)); + + ArgumentOutOfRangeException.ThrowIfNegative(parameter.CfgScale, nameof(StableDiffusionParameter.CfgScale)); + ArgumentOutOfRangeException.ThrowIfNegative(parameter.Strength, nameof(StableDiffusionParameter.Strength)); + + if (!Enum.IsDefined(parameter.SampleMethod)) throw new ArgumentOutOfRangeException(nameof(StableDiffusionParameter.SampleMethod)); + + parameter.ControlNet.Validate(); + parameter.PhotoMaker.Validate(); + } + + public static void Validate(this StableDiffusionControlNetParameter parameter) + { + ArgumentNullException.ThrowIfNull(parameter, nameof(StableDiffusionParameter.ControlNet)); + + ArgumentOutOfRangeException.ThrowIfNegative(parameter.Strength, nameof(StableDiffusionControlNetParameter.Strength)); + ArgumentOutOfRangeException.ThrowIfNegative(parameter.CannyHighThreshold, nameof(StableDiffusionControlNetParameter.CannyHighThreshold)); + ArgumentOutOfRangeException.ThrowIfNegative(parameter.CannyLowThreshold, nameof(StableDiffusionControlNetParameter.CannyLowThreshold)); + ArgumentOutOfRangeException.ThrowIfNegative(parameter.CannyWeak, nameof(StableDiffusionControlNetParameter.CannyWeak)); + ArgumentOutOfRangeException.ThrowIfNegative(parameter.CannyStrong, nameof(StableDiffusionControlNetParameter.CannyStrong)); + } + + public static void Validate(this PhotoMakerParameter parameter) + { + ArgumentNullException.ThrowIfNull(parameter, nameof(StableDiffusionParameter.PhotoMaker)); + ArgumentNullException.ThrowIfNull(parameter.InputIdImageDirectory, nameof(PhotoMakerParameter.InputIdImageDirectory)); + + ArgumentOutOfRangeException.ThrowIfNegative(parameter.StyleRatio, nameof(PhotoMakerParameter.StyleRatio)); + } + + public static void Validate(this ModelParameter parameter) + { + ArgumentNullException.ThrowIfNull(parameter, nameof(parameter)); + ArgumentNullException.ThrowIfNull(parameter.TaesdPath, nameof(ModelParameter.TaesdPath)); + ArgumentNullException.ThrowIfNull(parameter.LoraModelDir, nameof(ModelParameter.LoraModelDir)); + ArgumentNullException.ThrowIfNull(parameter.VaePath, nameof(ModelParameter.VaePath)); + ArgumentNullException.ThrowIfNull(parameter.ControlNetPath, nameof(ModelParameter.ControlNetPath)); + ArgumentNullException.ThrowIfNull(parameter.EmbeddingsDirectory, nameof(ModelParameter.EmbeddingsDirectory)); + ArgumentNullException.ThrowIfNull(parameter.StackedIdEmbeddingsDirectory, nameof(ModelParameter.StackedIdEmbeddingsDirectory)); + + if (!Enum.IsDefined(parameter.RngType)) throw new ArgumentOutOfRangeException(nameof(ModelParameter.RngType)); + if (!Enum.IsDefined(parameter.Quantization)) throw new ArgumentOutOfRangeException(nameof(ModelParameter.Quantization)); + if (!Enum.IsDefined(parameter.Schedule)) throw new ArgumentOutOfRangeException(nameof(ModelParameter.Schedule)); + } + + public static void Validate(this UpscalerModelParameter parameter) + { + ArgumentNullException.ThrowIfNull(parameter, nameof(parameter)); + ArgumentNullException.ThrowIfNull(parameter.ESRGANPath, nameof(UpscalerModelParameter.ESRGANPath)); + + if (!Enum.IsDefined(parameter.Quantization)) throw new ArgumentOutOfRangeException(nameof(ModelParameter.Quantization)); + } +} \ No newline at end of file diff --git a/StableDiffusion.NET/StableDiffusionModel.cs b/StableDiffusion.NET/StableDiffusionModel.cs index a0f0bdb..5d2f579 100644 --- a/StableDiffusion.NET/StableDiffusionModel.cs +++ b/StableDiffusion.NET/StableDiffusionModel.cs @@ -42,6 +42,11 @@ public sealed unsafe class StableDiffusionModel : IDisposable public StableDiffusionModel(string modelPath, ModelParameter parameter, UpscalerModelParameter? upscalerParameter = null) { + ArgumentException.ThrowIfNullOrWhiteSpace(modelPath, nameof(modelPath)); + + parameter.Validate(); + upscalerParameter?.Validate(); + this._modelPath = modelPath; this._parameter = parameter; this._upscalerParameter = upscalerParameter; @@ -88,6 +93,9 @@ public sealed unsafe class StableDiffusionModel : IDisposable public StableDiffusionImage TextToImage(string prompt, StableDiffusionParameter parameter) { ObjectDisposedException.ThrowIf(_disposed, this); + ArgumentNullException.ThrowIfNull(prompt); + + parameter.Validate(); Native.sd_image_t* result; if (parameter.ControlNet.IsEnabled) @@ -186,6 +194,9 @@ public sealed unsafe class StableDiffusionModel : IDisposable public StableDiffusionImage ImageToImage(string prompt, in ReadOnlySpan image, StableDiffusionParameter parameter) { ObjectDisposedException.ThrowIf(_disposed, this); + ArgumentNullException.ThrowIfNull(prompt); + + parameter.Validate(); fixed (byte* imagePtr = image) { @@ -207,6 +218,9 @@ public sealed unsafe class StableDiffusionModel : IDisposable private StableDiffusionImage ImageToImage(string prompt, Native.sd_image_t image, StableDiffusionParameter parameter) { ObjectDisposedException.ThrowIf(_disposed, this); + ArgumentNullException.ThrowIfNull(prompt); + + parameter.Validate(); Native.sd_image_t* result; if (parameter.ControlNet.IsEnabled) @@ -361,7 +375,14 @@ public sealed unsafe class StableDiffusionModel : IDisposable } public static void Convert(string modelPath, string vaePath, Quantization quantization, string outputPath) - => Native.convert(modelPath, vaePath, outputPath, quantization); + { + ArgumentException.ThrowIfNullOrWhiteSpace(nameof(modelPath)); + ArgumentException.ThrowIfNullOrWhiteSpace(nameof(outputPath)); + ArgumentNullException.ThrowIfNull(vaePath); + if (!Enum.IsDefined(quantization)) throw new ArgumentOutOfRangeException(nameof(quantization)); + + Native.convert(modelPath, vaePath, outputPath, quantization); + } public static string GetSystemInfo() {