Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 64 additions & 3 deletions StableDiffusion.NET/Models/DiffusionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,75 @@ public IImage<ColorRGB> ImageToImage(string prompt, IImage image, DiffusionParam

ObjectDisposedException.ThrowIf(_disposed, this);
ArgumentNullException.ThrowIfNull(prompt);
ArgumentNullException.ThrowIfNull(image);

parameter.Validate();

if (image is not IImage<ColorRGB> refImage)
refImage = image.ConvertTo<ColorRGB>();

fixed (byte* imagePtr = refImage.AsRefImage())
return ImageToImage(prompt, refImage.ToSdImage(imagePtr), parameter);
// DarthAffe 10.08.2024: Mask needs to be a 1 channel all max value image when it's not used - I really don't like this concept as it adds unnecessary allocations, but that's how it is :(
Span<byte> maskBuffer = new byte[image.Width * image.Height];
maskBuffer.Fill(byte.MaxValue);

fixed (byte* maskPtr = maskBuffer)
{
Native.sd_image_t maskImage = new()
{
width = (uint)image.Width,
height = (uint)image.Height,
channel = 1,
data = maskPtr
};

fixed (byte* imagePtr = refImage.AsRefImage())
return ImageToImage(prompt, refImage.ToSdImage(imagePtr), maskImage, parameter);
}
}

public IImage<ColorRGB> Inpaint(string prompt, IImage image, IImage mask, DiffusionParameter? parameter = null)
{
parameter ??= GetDefaultParameter();

ObjectDisposedException.ThrowIf(_disposed, this);
ArgumentNullException.ThrowIfNull(prompt);
ArgumentNullException.ThrowIfNull(image);
ArgumentNullException.ThrowIfNull(mask);

if (image.Width != mask.Width) throw new ArgumentException("The mask needs to have the same with as the image.", nameof(mask));
if (image.Height != mask.Height) throw new ArgumentException("The mask needs to have the same height as the image.", nameof(mask));

parameter.Validate();

if (image is not IImage<ColorRGB> refImage)
refImage = image.ConvertTo<ColorRGB>();

// DarthAffe 10.08.2024: HPPH does currently not support monochrome images, that's why we need to convert it here. We're going for the simple conversion as the source image is supposed to be monochrome anyway.
Span<byte> maskBuffer = new byte[image.Width * image.Height];
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
{
IColor color = mask[x, y];
maskBuffer[(image.Width * y) + x] = (byte)Math.Round((color.R + color.G + color.B) / 3.0);
}

fixed (byte* maskPtr = maskBuffer)
{
Native.sd_image_t maskImage = new()
{
width = (uint)image.Width,
height = (uint)image.Height,
channel = 1,
data = maskPtr
};

fixed (byte* imagePtr = refImage.AsRefImage())
return ImageToImage(prompt, refImage.ToSdImage(imagePtr), maskImage, parameter);
}

}

private IImage<ColorRGB> ImageToImage(string prompt, Native.sd_image_t image, DiffusionParameter parameter)
private IImage<ColorRGB> ImageToImage(string prompt, Native.sd_image_t image, Native.sd_image_t mask, DiffusionParameter parameter)
{
ObjectDisposedException.ThrowIf(_disposed, this);
ArgumentNullException.ThrowIfNull(prompt);
Expand Down Expand Up @@ -246,6 +304,7 @@ private IImage<ColorRGB> ImageToImage(string prompt, Native.sd_image_t image, Di

result = Native.img2img(_ctx,
image,
mask,
prompt,
parameter.NegativePrompt,
parameter.ClipSkip,
Expand Down Expand Up @@ -283,6 +342,7 @@ private IImage<ColorRGB> ImageToImage(string prompt, Native.sd_image_t image, Di

result = Native.img2img(_ctx,
image,
mask,
prompt,
parameter.NegativePrompt,
parameter.ClipSkip,
Expand Down Expand Up @@ -312,6 +372,7 @@ private IImage<ColorRGB> ImageToImage(string prompt, Native.sd_image_t image, Di
{
result = Native.img2img(_ctx,
image,
mask,
prompt,
parameter.NegativePrompt,
parameter.ClipSkip,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ public static DiffusionParameter WithNegativePrompt(this DiffusionParameter para
return parameter;
}

public static DiffusionParameter WithStrength(this DiffusionParameter parameter, float strength)
{
parameter.Strength = strength;

return parameter;
}

public static DiffusionParameter WithSlgScale(this DiffusionParameter parameter, float slgScale)
{
parameter.SlgScale = slgScale;
Expand Down
1 change: 1 addition & 0 deletions StableDiffusion.NET/Native/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ internal struct sd_image_t
[LibraryImport(LIB_NAME, EntryPoint = "img2img")]
internal static partial sd_image_t* img2img(sd_ctx_t* sd_ctx,
sd_image_t init_image,
sd_image_t mask_image,
[MarshalAs(UnmanagedType.LPStr)] string prompt,
[MarshalAs(UnmanagedType.LPStr)] string negative_prompt,
int clip_skip,
Expand Down