Fixed wrong free of upscaled images

This commit is contained in:
Darth Affe 2024-10-17 21:19:38 +02:00
parent eea9a9afe8
commit 46bba39c4a
2 changed files with 17 additions and 9 deletions

View File

@ -8,21 +8,29 @@ internal static class ImageHelper
{
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 = ToImage(*sdImage);
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);
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);
Marshal.FreeHGlobal((nint)image.data);
}
public static unsafe Native.sd_image_t ToSdImage(this IImage<ColorRGB> image, byte* pinnedReference)

View File

@ -58,14 +58,14 @@ public sealed unsafe class UpscaleModel : IDisposable
fixed (byte* imagePtr = sourceImage.AsRefImage())
{
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)
{
Native.sd_image_t result = Native.upscale(_ctx, image, upscaleFactor);
return ImageHelper.ToImage(&result);
return ImageHelper.ToImage(result);
}
public void Dispose()