Skip to content

Commit

Permalink
Merge pull request #48 from DarthAffe/Inpainting
Browse files Browse the repository at this point in the history
Fixed Image2Image and added Inpainting support
  • Loading branch information
DarthAffe authored Jan 5, 2025
2 parents 59133b2 + 489c233 commit d428835
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
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

0 comments on commit d428835

Please sign in to comment.