using System.Collections.Generic;
using System.Runtime.InteropServices;
using Dalamud.Utility;
using TerraFX.Interop.Windows;
namespace Dalamud.Interface.Textures.Internal;
/// Represents an available bitmap codec.
internal sealed class BitmapCodecInfo : IBitmapCodecInfo
{
/// Initializes a new instance of the class.
/// The source codec info. Ownership is not transferred.
public unsafe BitmapCodecInfo(ComPtr codecInfo)
{
this.Name = ReadStringUsing(
codecInfo,
((IWICBitmapCodecInfo.Vtbl*)codecInfo.Get()->lpVtbl)->GetFriendlyName);
Guid temp;
codecInfo.Get()->GetContainerFormat(&temp).ThrowOnError();
this.ContainerGuid = temp;
this.Extensions = ReadStringUsing(
codecInfo,
((IWICBitmapCodecInfo.Vtbl*)codecInfo.Get()->lpVtbl)->GetFileExtensions)
.Split(',');
this.MimeTypes = ReadStringUsing(
codecInfo,
((IWICBitmapCodecInfo.Vtbl*)codecInfo.Get()->lpVtbl)->GetMimeTypes)
.Split(',');
}
/// Gets the friendly name for the codec.
public string Name { get; }
/// Gets the representing the container.
public Guid ContainerGuid { get; }
/// Gets the suggested file extensions.
public IReadOnlyCollection Extensions { get; }
/// Gets the corresponding mime types.
public IReadOnlyCollection MimeTypes { get; }
private static unsafe string ReadStringUsing(
IWICBitmapCodecInfo* codecInfo,
delegate* unmanaged[MemberFunction] readFuncPtr)
{
var cch = 0u;
_ = readFuncPtr(codecInfo, 0, null, &cch);
var buf = stackalloc char[(int)cch + 1];
Marshal.ThrowExceptionForHR(readFuncPtr(codecInfo, cch + 1, buf, &cch));
return new(buf, 0, (int)cch);
}
}