PixelFormatTranscoderTests.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using Avalonia.Media.Imaging;
  2. using Avalonia.Platform;
  3. using Xunit;
  4. namespace Avalonia.Base.UnitTests.Media.Imaging
  5. {
  6. public class PixelFormatTranscoderTests
  7. {
  8. [Fact]
  9. public void Should_Transcode()
  10. {
  11. var sourceMemory = CreateBitmapMemory();
  12. var destMemory = new BitmapMemory(PixelFormat.Bgra8888, AlphaFormat.Opaque, sourceMemory.Size);
  13. PixelFormatTranscoder.Transcode(
  14. sourceMemory.Address,
  15. sourceMemory.Size,
  16. sourceMemory.RowBytes,
  17. sourceMemory.Format,
  18. sourceMemory.AlphaFormat,
  19. destMemory.Address,
  20. destMemory.RowBytes,
  21. destMemory.Format,
  22. destMemory.AlphaFormat);
  23. var reader = new PixelFormatReader.Bgra8888PixelFormatReader();
  24. reader.Reset(destMemory.Address);
  25. Assert.Equal(new Rgba8888Pixel(255, 0, 0, 0), reader.ReadNext());
  26. Assert.Equal(new Rgba8888Pixel(0, 255, 0, 0), reader.ReadNext());
  27. Assert.Equal(new Rgba8888Pixel(0, 0, 255, 0), reader.ReadNext());
  28. }
  29. private BitmapMemory CreateBitmapMemory()
  30. {
  31. var bitmapMemory = new BitmapMemory(PixelFormat.Rgba8888, AlphaFormat.Opaque, new PixelSize(3, 1));
  32. var sourceWriter = new PixelFormatWriter.Rgba8888PixelFormatWriter();
  33. sourceWriter.Reset(bitmapMemory.Address);
  34. sourceWriter.WriteNext(new Rgba8888Pixel { R = 255 });
  35. sourceWriter.WriteNext(new Rgba8888Pixel { G = 255 });
  36. sourceWriter.WriteNext(new Rgba8888Pixel { B = 255 });
  37. return bitmapMemory;
  38. }
  39. }
  40. }