Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Bmp.cs
浏览该文件的文档.
1using System.Runtime.InteropServices;
2using SixLabors.ImageSharp;
3using SixLabors.ImageSharp.Formats.Bmp;
4
5namespace Engine.Media {
6 public static class Bmp {
16
17 public struct BmpInfo {
18 public int Width;
19
20 public int Height;
21
22 public Format Format;
23 }
24
25 [StructLayout(LayoutKind.Sequential, Pack = 1)]
26 public struct BitmapHeader {
27 public byte Type1;
28
29 public byte Type2;
30
31 public int Size;
32
33 public short Reserved1;
34
35 public short Reserved2;
36
37 public int OffBits;
38
39 public int Size2;
40
41 public int Width;
42
43 public int Height;
44
45 public short Planes;
46
47 public short BitCount;
48
49 public int Compression;
50
51 public int SizeImage;
52
53 public int XPelsPerMeter;
54
55 public int YPelsPerMeter;
56
57 public int ClrUsed;
58
59 public int ClrImportant;
60 }
61
62 public static bool IsBmpStream(Stream stream) {
63 ArgumentNullException.ThrowIfNull(stream);
64 return SixLabors.ImageSharp.Image.DetectFormat(stream).Name == "BMP";
65 }
66
67 public static BmpInfo GetInfo(Stream stream) {
68 ArgumentNullException.ThrowIfNull(stream);
69 ImageInfo info = SixLabors.ImageSharp.Image.Identify(stream);
70 if (info.Metadata.DecodedImageFormat.Name != "BMP") {
71 throw new FormatException($"Image format({info.Metadata.DecodedImageFormat.Name}) is not Bmp");
72 }
73 BmpInfo result = default;
74 result.Width = info.Width;
75 result.Height = info.Height;
76 return !ImageSharpBitsPerPixel2EngineBmpFormat.TryGetValue(info.Metadata.GetBmpMetadata().BitsPerPixel, out result.Format)
77 ? throw new InvalidOperationException("Unsupported BMP pixel format.")
78 : result;
79 }
80
81 public static Image Load(Stream stream) {
82 ArgumentNullException.ThrowIfNull(stream);
83 string formatName = SixLabors.ImageSharp.Image.DetectFormat(stream).Name;
84 return formatName != "BMP" ? throw new FormatException($"Image format({formatName}) is not BMP") : Image.Load(stream);
85 }
86
87 public static void Save(Image image, Stream stream, Format format, bool sync = false) {
88 ArgumentNullException.ThrowIfNull(image);
89 ArgumentNullException.ThrowIfNull(stream);
90 if (!EngineBmpFormat2ImageSharpBitsPerPixel.TryGetValue(format, out BmpBitsPerPixel bitsPerPixel)) {
91 throw new InvalidOperationException("Unsupported BMP pixel format.");
92 }
93 BmpEncoder encoder = new() { BitsPerPixel = bitsPerPixel };
94 if (sync) {
95 image.m_trueImage.SaveAsBmp(stream, encoder);
96 }
97 else {
98 image.m_trueImage.SaveAsBmpAsync(stream, encoder);
99 }
100 }
101
102 public static BitmapHeader ReadHeader(Stream stream) {
103 ArgumentNullException.ThrowIfNull(stream);
104 if (!BitConverter.IsLittleEndian) {
105 throw new InvalidOperationException("Unsupported system endianness.");
106 }
107 byte[] array = new byte[54];
108 if (stream.Read(array, 0, array.Length) != array.Length) {
109 throw new InvalidOperationException("Invalid BMP header.");
110 }
111 BitmapHeader result = Utilities.ArrayToStructure<BitmapHeader>(array);
112 return result.Type1 != 66 || result.Type2 != 77 ? throw new InvalidOperationException("Invalid BMP header.") :
113 result.Compression != 0 ? throw new InvalidOperationException("Unsupported BMP compression.") : result;
114 }
115
116 public static readonly Dictionary<Format, BmpBitsPerPixel> EngineBmpFormat2ImageSharpBitsPerPixel = new() {
117 { Format.RGBA8, BmpBitsPerPixel.Pixel32 },
118 { Format.RGB8, BmpBitsPerPixel.Pixel24 },
119 { Format.Pixel1, BmpBitsPerPixel.Pixel1 },
120 { Format.Pixel16, BmpBitsPerPixel.Pixel16 },
121 { Format.Pixel2, BmpBitsPerPixel.Pixel2 },
122 { Format.Pixel4, BmpBitsPerPixel.Pixel4 },
123 { Format.Pixel8, BmpBitsPerPixel.Pixel8 }
124 };
125
126 public static readonly Dictionary<BmpBitsPerPixel, Format> ImageSharpBitsPerPixel2EngineBmpFormat = new() {
127 { BmpBitsPerPixel.Pixel32, Format.RGBA8 },
128 { BmpBitsPerPixel.Pixel24, Format.RGB8 },
129 { BmpBitsPerPixel.Pixel1, Format.Pixel1 },
130 { BmpBitsPerPixel.Pixel16, Format.Pixel16 },
131 { BmpBitsPerPixel.Pixel2, Format.Pixel2 },
132 { BmpBitsPerPixel.Pixel4, Format.Pixel4 },
133 { BmpBitsPerPixel.Pixel8, Format.Pixel8 }
134 };
135 }
136}
static readonly Dictionary< Format, BmpBitsPerPixel > EngineBmpFormat2ImageSharpBitsPerPixel
定义 Bmp.cs:116
static Image Load(Stream stream)
定义 Bmp.cs:81
static readonly Dictionary< BmpBitsPerPixel, Format > ImageSharpBitsPerPixel2EngineBmpFormat
定义 Bmp.cs:126
static BitmapHeader ReadHeader(Stream stream)
定义 Bmp.cs:102
static bool IsBmpStream(Stream stream)
定义 Bmp.cs:62
static void Save(Image image, Stream stream, Format format, bool sync=false)
定义 Bmp.cs:87
static BmpInfo GetInfo(Stream stream)
定义 Bmp.cs:67
static Image Load(Stream stream, ImageFileFormat format)
readonly Image< Rgba32 > m_trueImage