-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCompressionTools.cs
38 lines (36 loc) · 1.44 KB
/
CompressionTools.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
using System;
using System.Linq;
using System.IO;
using BCnEncoder.Decoder;
using BCnEncoder.Encoder;
using BCnEncoder.Shared;
using BCnEncoder.ImageSharp;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using System.Runtime.CompilerServices;
namespace BurnoutImage
{
public static class CompressionTools
{
public static byte[] CompressTexture(string source, CompressionFormat compression)
{
BcEncoder encoder = new BcEncoder();
encoder.OutputOptions.Format = compression;
encoder.OutputOptions.FileFormat = OutputFileFormat.Dds;
encoder.OutputOptions.GenerateMipMaps = false;
encoder.OutputOptions.Quality = CompressionQuality.BestQuality;
Image<Rgba32> image = Image.Load<Rgba32>(source);
MemoryStream stream = new MemoryStream();
encoder.EncodeToStream(image, stream);
return new ArraySegment<byte>(stream.ToArray(), 0x80, (int)stream.Length - 0x80).ToArray<byte>();
}
public static byte[] DecompressTexture(byte[] source, int width, int height, CompressionFormat compression)
{
BcDecoder decoder = new BcDecoder();
Image<Rgba32> image = decoder.DecodeRawToImageRgba32(source, width, height, compression);
byte[] data = new byte[width * height * Unsafe.SizeOf<Rgba32>()];
image.CopyPixelDataTo(data);
return data;
}
}
}