mirror of
https://github.com/rosenbjerg/FFMpegCore.git
synced 2024-11-14 02:04:14 +01:00
693acabac4
* Move PosterWithAudio to FFMpegCore
* Reduce windows only tests
* Update Directory.Build.props
* Create .editorconfig
* More cleanup
* Enable implicit usings
* Remove unused method
* Apply dotnet format
* Fix unused variable in AudioGateArgument
* Fix boolean conditions in AudioGateArgument
* Merge boolean conditions into pattern
* Use target-typed new
* Add linting to CI
* Add CUDA to HardwareAccelerationDevice enum
* Increase timeout for Video_Join_Image_Sequence
* Adjust Video_Join_Image_Sequence timeout
* Fix expected seconds in Video_Join_Image_Sequence
* Increase timeout for Video_TranscodeToMemory due to macos agents
Former-commit-id: f9f7161686
87 lines
2.8 KiB
C#
87 lines
2.8 KiB
C#
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.Runtime.InteropServices;
|
|
using FFMpegCore.Pipes;
|
|
|
|
namespace FFMpegCore.Extensions.System.Drawing.Common
|
|
{
|
|
public class BitmapVideoFrameWrapper : IVideoFrame, IDisposable
|
|
{
|
|
public int Width => Source.Width;
|
|
|
|
public int Height => Source.Height;
|
|
|
|
public string Format { get; private set; }
|
|
|
|
public Bitmap Source { get; private set; }
|
|
|
|
public BitmapVideoFrameWrapper(Bitmap bitmap)
|
|
{
|
|
Source = bitmap ?? throw new ArgumentNullException(nameof(bitmap));
|
|
Format = ConvertStreamFormat(bitmap.PixelFormat);
|
|
}
|
|
|
|
public void Serialize(Stream stream)
|
|
{
|
|
var data = Source.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, Source.PixelFormat);
|
|
|
|
try
|
|
{
|
|
var buffer = new byte[data.Stride * data.Height];
|
|
Marshal.Copy(data.Scan0, buffer, 0, buffer.Length);
|
|
stream.Write(buffer, 0, buffer.Length);
|
|
}
|
|
finally
|
|
{
|
|
Source.UnlockBits(data);
|
|
}
|
|
}
|
|
|
|
public async Task SerializeAsync(Stream stream, CancellationToken token)
|
|
{
|
|
var data = Source.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, Source.PixelFormat);
|
|
|
|
try
|
|
{
|
|
var buffer = new byte[data.Stride * data.Height];
|
|
Marshal.Copy(data.Scan0, buffer, 0, buffer.Length);
|
|
await stream.WriteAsync(buffer, 0, buffer.Length, token).ConfigureAwait(false);
|
|
}
|
|
finally
|
|
{
|
|
Source.UnlockBits(data);
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Source.Dispose();
|
|
}
|
|
|
|
private static string ConvertStreamFormat(PixelFormat fmt)
|
|
{
|
|
switch (fmt)
|
|
{
|
|
case PixelFormat.Format16bppGrayScale:
|
|
return "gray16le";
|
|
case PixelFormat.Format16bppRgb555:
|
|
return "bgr555le";
|
|
case PixelFormat.Format16bppRgb565:
|
|
return "bgr565le";
|
|
case PixelFormat.Format24bppRgb:
|
|
return "bgr24";
|
|
case PixelFormat.Format32bppArgb:
|
|
return "bgra";
|
|
case PixelFormat.Format32bppPArgb:
|
|
//This is not really same as argb32
|
|
return "argb";
|
|
case PixelFormat.Format32bppRgb:
|
|
return "rgba";
|
|
case PixelFormat.Format48bppRgb:
|
|
return "rgb48le";
|
|
default:
|
|
throw new NotSupportedException($"Not supported pixel format {fmt}");
|
|
}
|
|
}
|
|
}
|
|
}
|