ImgFrame.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using FFMpegCore.Pipes;
  2. using PixiEditor.DrawingApi.Core.Surfaces;
  3. using PixiEditor.DrawingApi.Core.Surfaces.ImageData;
  4. namespace PixiEditor.AnimationRenderer.FFmpeg;
  5. public class ImgFrame : IVideoFrame, IDisposable
  6. {
  7. public Image Image { get; }
  8. public int Width => Image.Width;
  9. public int Height => Image.Height;
  10. public string Format => ToStreamFormat();
  11. private Bitmap encoded;
  12. public ImgFrame(Image image)
  13. {
  14. Image = image;
  15. encoded = Bitmap.FromImage(image);
  16. }
  17. public void Serialize(Stream pipe)
  18. {
  19. var bytes = encoded.Bytes;
  20. pipe.Write(bytes, 0, bytes.Length);
  21. }
  22. public async Task SerializeAsync(Stream pipe, CancellationToken token)
  23. {
  24. await pipe.WriteAsync(encoded.Bytes, 0, encoded.Bytes.Length, token).ConfigureAwait(false);
  25. }
  26. private string ToStreamFormat()
  27. {
  28. switch (encoded.Info.ColorType)
  29. {
  30. case ColorType.Gray8:
  31. return "gray8";
  32. case ColorType.Bgra8888:
  33. return "bgra";
  34. case ColorType.Rgba8888:
  35. return "rgba";
  36. case ColorType.Rgb565:
  37. return "rgb565";
  38. default:
  39. throw new NotSupportedException($"Color type {Image.Info.ColorType} is not supported.");
  40. }
  41. }
  42. public void Dispose()
  43. {
  44. encoded.Dispose();
  45. }
  46. }