-
Notifications
You must be signed in to change notification settings - Fork 45
/
Image.cs
60 lines (46 loc) · 1.85 KB
/
Image.cs
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
using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
namespace DirectX12GameEngine.Graphics
{
public sealed class Image : IDisposable
{
public Memory<byte> Data { get; }
public ImageDescription Description { get; }
public int Width => Description.Width;
public int Height => Description.Height;
internal Image()
{
}
internal Image(ImageDescription description, Memory<byte> data)
{
Description = description;
Data = data;
}
public static async Task<Image> LoadAsync(string filePath)
{
using FileStream stream = File.OpenRead(filePath);
return await LoadAsync(stream);
}
public static async Task<Image> LoadAsync(Stream stream)
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream.AsRandomAccessStream());
PixelDataProvider pixelDataProvider = await decoder.GetPixelDataAsync(
decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, new BitmapTransform(),
ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);
byte[] imageBuffer = pixelDataProvider.DetachPixelData();
PixelFormat pixelFormat = decoder.BitmapPixelFormat switch
{
BitmapPixelFormat.Rgba8 => PixelFormat.R8G8B8A8_UNorm,
BitmapPixelFormat.Bgra8 => PixelFormat.B8G8R8A8_UNorm,
_ => throw new NotSupportedException("This format is not supported.")
};
ImageDescription description = ImageDescription.New2D((int)decoder.PixelWidth, (int)decoder.OrientedPixelHeight, pixelFormat);
return new Image(description, imageBuffer);
}
public void Dispose()
{
}
}
}