2023-02-13 11:25:45 +01:00
|
|
|
|
using FFMpegCore.Pipes;
|
|
|
|
|
using SkiaSharp;
|
2020-04-27 18:23:31 +02:00
|
|
|
|
|
2022-03-24 20:19:37 +01:00
|
|
|
|
namespace FFMpegCore.Extensions.System.Drawing.Common
|
2020-04-27 18:23:31 +02:00
|
|
|
|
{
|
|
|
|
|
public class BitmapVideoFrameWrapper : IVideoFrame, IDisposable
|
|
|
|
|
{
|
|
|
|
|
public int Width => Source.Width;
|
|
|
|
|
|
|
|
|
|
public int Height => Source.Height;
|
|
|
|
|
|
|
|
|
|
public string Format { get; private set; }
|
|
|
|
|
|
2023-02-13 11:25:45 +01:00
|
|
|
|
public SKBitmap Source { get; private set; }
|
2020-04-27 18:23:31 +02:00
|
|
|
|
|
2023-02-13 11:25:45 +01:00
|
|
|
|
public BitmapVideoFrameWrapper(SKBitmap bitmap)
|
2020-04-27 18:23:31 +02:00
|
|
|
|
{
|
|
|
|
|
Source = bitmap ?? throw new ArgumentNullException(nameof(bitmap));
|
2023-02-13 11:25:45 +01:00
|
|
|
|
Format = ConvertStreamFormat(bitmap.ColorType);
|
2020-04-27 18:23:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 13:48:55 +02:00
|
|
|
|
public void Serialize(Stream stream)
|
2020-04-27 18:23:31 +02:00
|
|
|
|
{
|
2023-02-13 11:25:45 +01:00
|
|
|
|
var data = Source.Bytes;
|
|
|
|
|
stream.Write(data, 0, data.Length);
|
2020-04-27 18:23:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 13:48:55 +02:00
|
|
|
|
public async Task SerializeAsync(Stream stream, CancellationToken token)
|
2020-04-27 18:23:31 +02:00
|
|
|
|
{
|
2023-02-13 11:25:45 +01:00
|
|
|
|
var data = Source.Bytes;
|
|
|
|
|
await stream.WriteAsync(data, 0, data.Length, token).ConfigureAwait(false);
|
2020-04-27 18:23:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Source.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 11:25:45 +01:00
|
|
|
|
private static string ConvertStreamFormat(SKColorType fmt)
|
2020-04-27 18:23:31 +02:00
|
|
|
|
{
|
|
|
|
|
switch (fmt)
|
|
|
|
|
{
|
2023-02-13 11:25:45 +01:00
|
|
|
|
case SKColorType.Gray8:
|
|
|
|
|
return "gray8";
|
|
|
|
|
case SKColorType.Bgra8888:
|
2020-05-12 17:26:52 +02:00
|
|
|
|
return "bgra";
|
2023-02-13 11:25:45 +01:00
|
|
|
|
case SKColorType.Rgb888x:
|
|
|
|
|
return "rgb";
|
|
|
|
|
case SKColorType.Rgba8888:
|
2020-04-27 18:23:31 +02:00
|
|
|
|
return "rgba";
|
2023-02-13 11:25:45 +01:00
|
|
|
|
case SKColorType.Rgb565:
|
|
|
|
|
return "rgb565";
|
2020-04-27 18:23:31 +02:00
|
|
|
|
default:
|
|
|
|
|
throw new NotSupportedException($"Not supported pixel format {fmt}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|