refactor: try decoding into managed memory

This commit is contained in:
Anna 2024-02-22 00:22:33 -05:00
parent b1beba7c33
commit 8720d831ac
Signed by: anna
GPG Key ID: D0943384CD9F87D1
1 changed files with 14 additions and 14 deletions

View File

@ -23,24 +23,24 @@ internal static class WebPHelper {
return null;
}
width = features.Width;
height = features.Height;
rgba = new byte[width * height * 4];
var decoderConfig = default(WebPDecoderConfig);
Native.WebPInitDecoderConfig(ref decoderConfig);
using var free = new OnDispose(() => Native.WebPFreeDecBuffer(ref decoderConfig.output));
decoderConfig.input = features;
decoderConfig.output.colorSpace = WebpCspMode.ModeRgba;
var code = Native.WebPDecode((nint) imagePtr, imageBytes.Length, ref decoderConfig);
if (code != Vp8StatusCode.Ok) {
Plugin.Log.Warning($"could not decode webp: {Enum.GetName(code)}");
return null;
decoderConfig.output.isExternalMemory = 1;
decoderConfig.output.u.Rgba.size = (nuint) rgba.Length;
decoderConfig.output.u.Rgba.stride = height * 4;
fixed (byte* rgbaPtr = rgba) {
decoderConfig.output.u.Rgba.rgba = (nint) rgbaPtr;
var code = Native.WebPDecode((nint) imagePtr, imageBytes.Length, ref decoderConfig);
if (code != Vp8StatusCode.Ok) {
Plugin.Log.Warning($"could not decode webp: {Enum.GetName(code)}");
return null;
}
}
height = decoderConfig.output.height;
width = decoderConfig.output.width;
rgba = MemoryHelper.ReadRaw(
decoderConfig.output.private_memory,
height * width * 4
);
}
}