Skip to content

Commit d25eb97

Browse files
authored
Merge pull request #33 from DarthAffe/UpscalerFix
Fixed wrong free of upscaled images
2 parents eea9a9a + 46bba39 commit d25eb97

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

StableDiffusion.NET/Helper/ImageHelper.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,29 @@ internal static class ImageHelper
88
{
99
public static unsafe Image<ColorRGB> ToImage(Native.sd_image_t* sdImage)
1010
{
11-
int width = (int)sdImage->width;
12-
int height = (int)sdImage->height;
13-
int bpp = (int)sdImage->channel;
11+
Image<ColorRGB> image = ToImage(*sdImage);
1412

15-
Image<ColorRGB> image = Image<ColorRGB>.Create(new ReadOnlySpan<byte>(sdImage->data, width * height * bpp), width, height, width * bpp);
13+
Marshal.FreeHGlobal((nint)sdImage);
14+
15+
return image;
16+
}
17+
18+
public static unsafe Image<ColorRGB> ToImage(Native.sd_image_t sdImage)
19+
{
20+
int width = (int)sdImage.width;
21+
int height = (int)sdImage.height;
22+
int bpp = (int)sdImage.channel;
23+
24+
Image<ColorRGB> image = Image<ColorRGB>.Create(new ReadOnlySpan<byte>(sdImage.data, width * height * bpp), width, height, width * bpp);
1625

1726
Dispose(sdImage);
1827

1928
return image;
2029
}
2130

22-
public static unsafe void Dispose(Native.sd_image_t* image)
31+
public static unsafe void Dispose(Native.sd_image_t image)
2332
{
24-
Marshal.FreeHGlobal((nint)image->data);
25-
Marshal.FreeHGlobal((nint)image);
33+
Marshal.FreeHGlobal((nint)image.data);
2634
}
2735

2836
public static unsafe Native.sd_image_t ToSdImage(this IImage<ColorRGB> image, byte* pinnedReference)

StableDiffusion.NET/Models/UpscaleModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public IImage<ColorRGB> Upscale(IImage image, int upscaleFactor)
5858
fixed (byte* imagePtr = sourceImage.AsRefImage())
5959
{
6060
Native.sd_image_t result = Native.upscale(_ctx, sourceImage.ToSdImage(imagePtr), upscaleFactor);
61-
return ImageHelper.ToImage(&result);
61+
return ImageHelper.ToImage(result);
6262
}
6363
}
6464

6565
private IImage<ColorRGB> Upscale(Native.sd_image_t image, int upscaleFactor)
6666
{
6767
Native.sd_image_t result = Native.upscale(_ctx, image, upscaleFactor);
68-
return ImageHelper.ToImage(&result);
68+
return ImageHelper.ToImage(result);
6969
}
7070

7171
public void Dispose()

0 commit comments

Comments
 (0)