From 8720d831ac6aa500e975453db997bbda3f46ba06 Mon Sep 17 00:00:00 2001 From: Anna Date: Thu, 22 Feb 2024 00:22:33 -0500 Subject: [PATCH] refactor: try decoding into managed memory --- Util/WebPHelper.cs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Util/WebPHelper.cs b/Util/WebPHelper.cs index faa73d1..4e0dcf8 100644 --- a/Util/WebPHelper.cs +++ b/Util/WebPHelper.cs @@ -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 - ); } }