ImmutableBitmap.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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, IReadableBitmapWithAlphaImpl
  13. {
  14. private readonly SKImage _image;
  15. private readonly SKBitmap? _bitmap;
  16. /// <summary>
  17. /// Create immutable bitmap from given stream.
  18. /// </summary>
  19. /// <param name="stream">Stream containing encoded data.</param>
  20. public ImmutableBitmap(Stream stream)
  21. {
  22. using (var skiaStream = new SKManagedStream(stream))
  23. {
  24. using (var data = SKData.Create(skiaStream))
  25. _bitmap = SKBitmap.Decode(data);
  26. if (_bitmap == null)
  27. throw new ArgumentException("Unable to load bitmap from provided data");
  28. _bitmap.SetImmutable();
  29. _image = SKImage.FromBitmap(_bitmap);
  30. PixelSize = new PixelSize(_image.Width, _image.Height);
  31. // TODO: Skia doesn't have an API for DPI.
  32. Dpi = new Vector(96, 96);
  33. }
  34. }
  35. public ImmutableBitmap(SKImage image)
  36. {
  37. _image = image;
  38. PixelSize = new PixelSize(image.Width, image.Height);
  39. Dpi = new Vector(96, 96);
  40. }
  41. public ImmutableBitmap(ImmutableBitmap src, PixelSize destinationSize, BitmapInterpolationMode interpolationMode)
  42. {
  43. SKImageInfo info = new SKImageInfo(destinationSize.Width, destinationSize.Height, SKColorType.Bgra8888);
  44. _bitmap = new SKBitmap(info);
  45. src._image.ScalePixels(_bitmap.PeekPixels(), interpolationMode.ToSKSamplingOptions());
  46. _bitmap.SetImmutable();
  47. _image = SKImage.FromBitmap(_bitmap);
  48. PixelSize = new PixelSize(_image.Width, _image.Height);
  49. // TODO: Skia doesn't have an API for DPI.
  50. Dpi = new Vector(96, 96);
  51. }
  52. public ImmutableBitmap(Stream stream, int decodeSize, bool horizontal, BitmapInterpolationMode interpolationMode)
  53. {
  54. using (var skStream = new SKManagedStream(stream))
  55. using (var skData = SKData.Create(skStream))
  56. using (var codec = SKCodec.Create(skData))
  57. {
  58. var info = codec.Info;
  59. // get the scale that is nearest to what we want (eg: jpg returned 512)
  60. var supportedScale = codec.GetScaledDimensions(horizontal ? ((float)decodeSize / info.Width) : ((float)decodeSize / info.Height));
  61. // decode the bitmap at the nearest size
  62. var nearest = new SKImageInfo(supportedScale.Width, supportedScale.Height);
  63. _bitmap = SKBitmap.Decode(codec, nearest);
  64. if (_bitmap == null)
  65. throw new ArgumentException("Unable to load bitmap from provided data");
  66. // now scale that to the size that we want
  67. var realScale = horizontal ? ((double)info.Height / info.Width) : ((double)info.Width / info.Height);
  68. SKImageInfo desired;
  69. if (horizontal)
  70. {
  71. desired = new SKImageInfo(decodeSize, (int)(realScale * decodeSize));
  72. }
  73. else
  74. {
  75. desired = new SKImageInfo((int)(realScale * decodeSize), decodeSize);
  76. }
  77. if (_bitmap.Width != desired.Width || _bitmap.Height != desired.Height)
  78. {
  79. var scaledBmp = _bitmap.Resize(desired, interpolationMode.ToSKSamplingOptions());
  80. _bitmap.Dispose();
  81. _bitmap = scaledBmp;
  82. }
  83. _bitmap.SetImmutable();
  84. _image = SKImage.FromBitmap(_bitmap);
  85. if (_image == null)
  86. {
  87. throw new ArgumentException("Unable to load bitmap from provided data");
  88. }
  89. PixelSize = new PixelSize(_image.Width, _image.Height);
  90. // TODO: Skia doesn't have an API for DPI.
  91. Dpi = new Vector(96, 96);
  92. }
  93. }
  94. /// <summary>
  95. /// Create immutable bitmap from given pixel data copy.
  96. /// </summary>
  97. /// <param name="size">Size of the bitmap.</param>
  98. /// <param name="dpi">DPI of the bitmap.</param>
  99. /// <param name="stride">Stride of data pixels.</param>
  100. /// <param name="format">Format of data pixels.</param>
  101. /// <param name="alphaFormat">Alpha format of data pixels.</param>
  102. /// <param name="data">Data pixels.</param>
  103. public ImmutableBitmap(PixelSize size, Vector dpi, int stride, PixelFormat format, AlphaFormat alphaFormat, IntPtr data)
  104. {
  105. using (var tmp = new SKBitmap())
  106. {
  107. tmp.InstallPixels(
  108. new SKImageInfo(size.Width, size.Height, format.ToSkColorType(), alphaFormat.ToSkAlphaType()),
  109. data, stride);
  110. _bitmap = tmp.Copy();
  111. }
  112. _bitmap.SetImmutable();
  113. _image = SKImage.FromBitmap(_bitmap);
  114. if (_image == null)
  115. {
  116. throw new ArgumentException("Unable to create bitmap from provided data");
  117. }
  118. PixelSize = size;
  119. Dpi = dpi;
  120. }
  121. public Vector Dpi { get; }
  122. public PixelSize PixelSize { get; }
  123. public int Version { get; } = 1;
  124. /// <inheritdoc />
  125. public void Dispose()
  126. {
  127. _image.Dispose();
  128. _bitmap?.Dispose();
  129. }
  130. /// <inheritdoc />
  131. public void Save(string fileName, int? quality = null)
  132. {
  133. ImageSavingHelper.SaveImage(_image, fileName, quality);
  134. }
  135. /// <inheritdoc />
  136. public void Save(Stream stream, int? quality = null)
  137. {
  138. ImageSavingHelper.SaveImage(_image, stream, quality);
  139. }
  140. /// <inheritdoc />
  141. public void Draw(DrawingContextImpl context, SKRect sourceRect, SKRect destRect, SKSamplingOptions samplingOptions, SKPaint paint)
  142. {
  143. context.Canvas.DrawImage(_image, sourceRect, destRect, samplingOptions, paint);
  144. }
  145. public PixelFormat? Format => _bitmap?.ColorType.ToAvalonia();
  146. public AlphaFormat? AlphaFormat => _bitmap?.AlphaType.ToAlphaFormat();
  147. public ILockedFramebuffer Lock()
  148. {
  149. if (_bitmap is null)
  150. throw new NotSupportedException("A bitmap is needed for locking");
  151. if (_bitmap.ColorType.ToAvalonia() is not { } format)
  152. throw new NotSupportedException($"Unsupported format {_bitmap.ColorType}");
  153. return new LockedFramebuffer(_bitmap.GetPixels(), PixelSize, _bitmap.RowBytes, Dpi, format, null);
  154. }
  155. }
  156. }