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
35 changes: 32 additions & 3 deletions StableDiffusion.NET/Helper/ImageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,46 @@ public static unsafe Image<ColorRGB> ToImage(Native.sd_image_t sdImage)
return image;
}

public static unsafe void Dispose(Native.sd_image_t image)
public static unsafe void Dispose(Native.sd_image_t image) => Marshal.FreeHGlobal((nint)image.data);

public static unsafe Native.sd_image_t ToSdImage(this IImage image, out nint dataPtr)
{
Marshal.FreeHGlobal((nint)image.data);
int sizeInBytes = image.SizeInBytes;

dataPtr = Marshal.AllocHGlobal(sizeInBytes);
image.CopyTo(new Span<byte>((void*)dataPtr, sizeInBytes));

return image.ToSdImage((byte*)dataPtr);
}

public static unsafe Native.sd_image_t ToSdImage(this IImage<ColorRGB> image, byte* pinnedReference)
public static unsafe Native.sd_image_t ToSdImage(this IImage image, byte* pinnedReference)
=> new()
{
width = (uint)image.Width,
height = (uint)image.Height,
channel = (uint)image.ColorFormat.BytesPerPixel,
data = pinnedReference
};

public static unsafe Native.sd_image_t* ToSdImagePtr(this IImage image, out nint dataPtr)
{
int sizeInBytes = image.SizeInBytes;

dataPtr = Marshal.AllocHGlobal(sizeInBytes);
image.CopyTo(new Span<byte>((void*)dataPtr, sizeInBytes));

return image.ToSdImagePtr((byte*)dataPtr);
}

public static unsafe Native.sd_image_t* ToSdImagePtr(this IImage image, byte* pinnedReference)
{
Native.sd_image_t* nativeImage = (Native.sd_image_t*)Marshal.AllocHGlobal(sizeof(Native.sd_image_t));

nativeImage->width = (uint)image.Width;
nativeImage->height = (uint)image.Height;
nativeImage->channel = (uint)image.ColorFormat.BytesPerPixel;
nativeImage->data = pinnedReference;

return nativeImage;
}
}
Loading