forked from arduano/ImageToMidi
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSkiaSharpExtensions.cs
More file actions
122 lines (109 loc) · 4.14 KB
/
SkiaSharpExtensions.cs
File metadata and controls
122 lines (109 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using SkiaSharp;
using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace ImageToMidi
{
public static class SkiaSharpExtensions
{
public static async Task<SKBitmap> ToSKBitmapAsync(this BitmapSource bitmapSource, CancellationToken cancellationToken = default)
{
if (bitmapSource == null) return null;
return await Task.Run(() =>
{
cancellationToken.ThrowIfCancellationRequested();
try
{
return ConvertDirectPixelCopyParallel(bitmapSource, cancellationToken);
}
catch
{
return ConvertViaEncoding(bitmapSource);
}
}, cancellationToken);
}
public static SKBitmap ToSKBitmap(this BitmapSource bitmapSource)
{
if (bitmapSource == null) return null;
try
{
return ConvertDirectPixelCopyParallel(bitmapSource, CancellationToken.None);
}
catch
{
return ConvertViaEncoding(bitmapSource);
}
}
private static SKBitmap ConvertDirectPixelCopyParallel(BitmapSource bitmapSource, CancellationToken cancellationToken)
{
var width = bitmapSource.PixelWidth;
var height = bitmapSource.PixelHeight;
var pixelSize = width * height * 4;
var convertedSource = bitmapSource.Format != System.Windows.Media.PixelFormats.Bgra32
? new FormatConvertedBitmap(bitmapSource, System.Windows.Media.PixelFormats.Bgra32, null, 0)
: bitmapSource;
SKBitmap skBitmap = new SKBitmap(width, height, SKColorType.Bgra8888, SKAlphaType.Premul);
byte[] pixels = pixelSize >= 85000
? new byte[pixelSize]
: ArrayPool<byte>.Shared.Rent(pixelSize);
try
{
int stride = width * 4;
convertedSource.CopyPixels(pixels, stride, 0);
IntPtr dstPtr = skBitmap.GetPixels();
unsafe
{
byte* dst = (byte*)dstPtr.ToPointer();
Parallel.For(0, height, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, y =>
{
cancellationToken.ThrowIfCancellationRequested();
Buffer.MemoryCopy(
source: Unsafe.AsPointer(ref pixels[y * stride]),
destination: dst + y * stride,
destinationSizeInBytes: stride,
sourceBytesToCopy: stride
);
});
}
}
finally
{
if (pixelSize < 85000)
ArrayPool<byte>.Shared.Return(pixels);
}
return skBitmap;
}
private static SKBitmap ConvertViaEncoding(BitmapSource bitmapSource)
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
using (var stream = new MemoryStream())
{
encoder.Save(stream);
stream.Position = 0;
return SKBitmap.Decode(stream);
}
}
public static async Task<(SKBitmap bitmap, SKImage image)> LoadImageAsync(string filePath)
{
return await Task.Run(() =>
{
try
{
var bitmap = SKBitmap.Decode(filePath);
var image = SKImage.FromBitmap(bitmap);
return (bitmap, image);
}
catch
{
return (null, null);
}
});
}
}
}