mirror of
https://github.com/DarthAffe/StableDiffusion.NET.git
synced 2025-12-12 13:28:35 +00:00
Fixed wrong free of upscaled images
This commit is contained in:
parent
eea9a9afe8
commit
46bba39c4a
@ -8,21 +8,29 @@ internal static class ImageHelper
|
|||||||
{
|
{
|
||||||
public static unsafe Image<ColorRGB> ToImage(Native.sd_image_t* sdImage)
|
public static unsafe Image<ColorRGB> ToImage(Native.sd_image_t* sdImage)
|
||||||
{
|
{
|
||||||
int width = (int)sdImage->width;
|
Image<ColorRGB> image = ToImage(*sdImage);
|
||||||
int height = (int)sdImage->height;
|
|
||||||
int bpp = (int)sdImage->channel;
|
|
||||||
|
|
||||||
Image<ColorRGB> image = Image<ColorRGB>.Create(new ReadOnlySpan<byte>(sdImage->data, width * height * bpp), width, height, width * bpp);
|
Marshal.FreeHGlobal((nint)sdImage);
|
||||||
|
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static unsafe Image<ColorRGB> ToImage(Native.sd_image_t sdImage)
|
||||||
|
{
|
||||||
|
int width = (int)sdImage.width;
|
||||||
|
int height = (int)sdImage.height;
|
||||||
|
int bpp = (int)sdImage.channel;
|
||||||
|
|
||||||
|
Image<ColorRGB> image = Image<ColorRGB>.Create(new ReadOnlySpan<byte>(sdImage.data, width * height * bpp), width, height, width * bpp);
|
||||||
|
|
||||||
Dispose(sdImage);
|
Dispose(sdImage);
|
||||||
|
|
||||||
return image;
|
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);
|
Marshal.FreeHGlobal((nint)image.data);
|
||||||
Marshal.FreeHGlobal((nint)image);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static unsafe Native.sd_image_t ToSdImage(this IImage<ColorRGB> image, byte* pinnedReference)
|
public static unsafe Native.sd_image_t ToSdImage(this IImage<ColorRGB> image, byte* pinnedReference)
|
||||||
|
|||||||
@ -58,14 +58,14 @@ public sealed unsafe class UpscaleModel : IDisposable
|
|||||||
fixed (byte* imagePtr = sourceImage.AsRefImage())
|
fixed (byte* imagePtr = sourceImage.AsRefImage())
|
||||||
{
|
{
|
||||||
Native.sd_image_t result = Native.upscale(_ctx, sourceImage.ToSdImage(imagePtr), upscaleFactor);
|
Native.sd_image_t result = Native.upscale(_ctx, sourceImage.ToSdImage(imagePtr), upscaleFactor);
|
||||||
return ImageHelper.ToImage(&result);
|
return ImageHelper.ToImage(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private IImage<ColorRGB> Upscale(Native.sd_image_t image, int upscaleFactor)
|
private IImage<ColorRGB> Upscale(Native.sd_image_t image, int upscaleFactor)
|
||||||
{
|
{
|
||||||
Native.sd_image_t result = Native.upscale(_ctx, image, upscaleFactor);
|
Native.sd_image_t result = Native.upscale(_ctx, image, upscaleFactor);
|
||||||
return ImageHelper.ToImage(&result);
|
return ImageHelper.ToImage(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user