fix: maybe read out the image correctly

This commit is contained in:
Anna 2024-02-22 00:13:51 -05:00
parent 9c8f07e3a7
commit 3edb3f4e6d
Signed by: anna
GPG Key ID: D0943384CD9F87D1
1 changed files with 38 additions and 4 deletions

View File

@ -1,15 +1,49 @@
using Dalamud.Interface;
using Dalamud.Interface.Internal;
using Dalamud.Memory;
using WebP.Net;
using WebP.Net.Natives;
using WebP.Net.Natives.Enums;
using WebP.Net.Natives.Structs;
namespace Screenie.Util;
internal static class WebPHelper {
internal static async Task<IDalamudTextureWrap?> LoadAsync(UiBuilder builder, byte[] imageBytes) {
using var webp = new WebPObject(imageBytes);
var info = webp.GetInfo();
var outputBuffer = webp.GetWebPLossy(80);
int width;
int height;
byte[] rgba;
return await builder.LoadImageRawAsync(outputBuffer, info.Width, info.Height, info.HasAlpha ? 4 : 3);
unsafe {
fixed (byte* imagePtr = imageBytes) {
var features = default(WebPBitstreamFeatures);
var vp8StatusCode = Native.WebPGetFeatures((nint) imagePtr, imageBytes.Length, ref features);
if (vp8StatusCode != Vp8StatusCode.Ok) {
Plugin.Log.Warning($"could not get webp features: {Enum.GetName(vp8StatusCode)}");
return null;
}
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;
}
height = decoderConfig.output.height;
width = decoderConfig.output.width;
rgba = MemoryHelper.ReadRaw(
decoderConfig.output.private_memory,
height * width * 4
);
}
}
return await builder.LoadImageRawAsync(rgba, width, height, 4);
}
}