ImmutableBitmap.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.IO;
  3. using Avalonia.Media.Imaging;
  4. using Avalonia.Platform;
  5. using Avalonia.Skia.Helpers;
  6. using SkiaSharp;
  7. namespace Avalonia.Skia
  8. {
  9. /// <summary>
  10. /// Immutable Skia bitmap.
  11. /// </summary>
  12. internal class ImmutableBitmap : IDrawableBitmapImpl
  13. {
  14. private readonly SKImage _image;
  15. /// <summary>
  16. /// Create immutable bitmap from given stream.
  17. /// </summary>
  18. /// <param name="stream">Stream containing encoded data.</param>
  19. public ImmutableBitmap(Stream stream)
  20. {
  21. using (var skiaStream = new SKManagedStream(stream))
  22. {
  23. using (var data = SKData.Create(skiaStream))
  24. _image = SKImage.FromEncodedData(data);
  25. if (_image == null)
  26. {
  27. throw new ArgumentException("Unable to load bitmap from provided data");
  28. }
  29. PixelSize = new PixelSize(_image.Width, _image.Height);
  30. // TODO: Skia doesn't have an API for DPI.
  31. Dpi = new Vector(96, 96);
  32. }
  33. }
  34. public ImmutableBitmap(SKImage image)
  35. {
  36. _image = image;
  37. PixelSize = new PixelSize(image.Width, image.Height);
  38. Dpi = new Vector(96, 96);
  39. }
  40. public ImmutableBitmap(ImmutableBitmap src, PixelSize destinationSize, BitmapInterpolationMode interpolationMode)
  41. {
  42. SKImageInfo info = new SKImageInfo(destinationSize.Width, destinationSize.Height, SKColorType.Bgra8888);
  43. SKImage output = SKImage.Create(info);
  44. src._image.ScalePixels(output.PeekPixels(), interpolationMode.ToSKFilterQuality());
  45. _image = output;
  46. PixelSize = new PixelSize(_image.Width, _image.Height);
  47. // TODO: Skia doesn't have an API for DPI.
  48. Dpi = new Vector(96, 96);
  49. }
  50. public ImmutableBitmap(Stream stream, int decodeSize, bool horizontal, BitmapInterpolationMode interpolationMode)
  51. {
  52. using (var skStream = new SKManagedStream(stream))
  53. using (var skData = SKData.Create(skStream))
  54. using (var codec = SKCodec.Create(skData))
  55. {
  56. var info = codec.Info;
  57. // get the scale that is nearest to what we want (eg: jpg returned 512)
  58. var supportedScale = codec.GetScaledDimensions(horizontal ? ((float)decodeSize / info.Width) : ((float)decodeSize / info.Height));
  59. // decode the bitmap at the nearest size
  60. var nearest = new SKImageInfo(supportedScale.Width, supportedScale.Height);
  61. var bmp = SKBitmap.Decode(codec, nearest);
  62. // now scale that to the size that we want
  63. var realScale = horizontal ? ((double)info.Height / info.Width) : ((double)info.Width / info.Height);
  64. SKImageInfo desired;
  65. if (horizontal)
  66. {
  67. desired = new SKImageInfo(decodeSize, (int)(realScale * decodeSize));
  68. }
  69. else
  70. {
  71. desired = new SKImageInfo((int)(realScale * decodeSize), decodeSize);
  72. }
  73. if (bmp.Width != desired.Width || bmp.Height != desired.Height)
  74. {
  75. var scaledBmp = bmp.Resize(desired, interpolationMode.ToSKFilterQuality());
  76. bmp.Dispose();
  77. bmp = scaledBmp;
  78. }
  79. _image = SKImage.FromBitmap(bmp);
  80. bmp.Dispose();
  81. if (_image == null)
  82. {
  83. throw new ArgumentException("Unable to load bitmap from provided data");
  84. }
  85. PixelSize = new PixelSize(_image.Width, _image.Height);
  86. // TODO: Skia doesn't have an API for DPI.
  87. Dpi = new Vector(96, 96);
  88. }
  89. }
  90. /// <summary>
  91. /// Create immutable bitmap from given pixel data copy.
  92. /// </summary>
  93. /// <param name="size">Size of the bitmap.</param>
  94. /// <param name="dpi">DPI of the bitmap.</param>
  95. /// <param name="stride">Stride of data pixels.</param>
  96. /// <param name="format">Format of data pixels.</param>
  97. /// <param name="alphaFormat">Alpha format of data pixels.</param>
  98. /// <param name="data">Data pixels.</param>
  99. public ImmutableBitmap(PixelSize size, Vector dpi, int stride, PixelFormat format, AlphaFormat alphaFormat, IntPtr data)
  100. {
  101. var imageInfo = new SKImageInfo(size.Width, size.Height, format.ToSkColorType(), alphaFormat.ToSkAlphaType());
  102. _image = SKImage.FromPixelCopy(imageInfo, data, stride);
  103. if (_image == null)
  104. {
  105. throw new ArgumentException("Unable to create bitmap from provided data");
  106. }
  107. PixelSize = size;
  108. Dpi = dpi;
  109. }
  110. public Vector Dpi { get; }
  111. public PixelSize PixelSize { get; }
  112. public int Version { get; } = 1;
  113. /// <inheritdoc />
  114. public void Dispose()
  115. {
  116. _image.Dispose();
  117. }
  118. /// <inheritdoc />
  119. public void Save(string fileName, int? quality = null)
  120. {
  121. ImageSavingHelper.SaveImage(_image, fileName);
  122. }
  123. /// <inheritdoc />
  124. public void Save(Stream stream, int? quality = null)
  125. {
  126. ImageSavingHelper.SaveImage(_image, stream);
  127. }
  128. /// <inheritdoc />
  129. public void Draw(DrawingContextImpl context, SKRect sourceRect, SKRect destRect, SKPaint paint)
  130. {
  131. context.Canvas.DrawImage(_image, sourceRect, destRect, paint);
  132. }
  133. }
  134. }