forked from Nominom/BCnEncoder.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBcBlockDecoder.cs
More file actions
156 lines (117 loc) · 4.9 KB
/
Copy pathBcBlockDecoder.cs
File metadata and controls
156 lines (117 loc) · 4.9 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.IO;
using System.Runtime.InteropServices;
using BCnEncoder.Shared;
namespace BCnEncoder.Decoder
{
internal interface IBcBlockDecoder {
RawBlock4X4Rgba32[,] Decode(ReadOnlySpan<byte> data, int pixelWidth, int pixelHeight, out int blockWidth,
out int blockHeight);
}
internal class Bc1NoAlphaDecoder : IBcBlockDecoder {
public RawBlock4X4Rgba32[,] Decode(ReadOnlySpan<byte> data, int pixelWidth, int pixelHeight, out int blockWidth, out int blockHeight) {
blockWidth = (int)MathF.Ceiling(pixelWidth / 4.0f);
blockHeight = (int)MathF.Ceiling(pixelHeight / 4.0f);
if (data.Length != (blockWidth * blockHeight * Marshal.SizeOf<Bc1Block>())) {
throw new InvalidDataException();
}
var encodedBlocks = MemoryMarshal.Cast<byte, Bc1Block>(data);
RawBlock4X4Rgba32[,] output = new RawBlock4X4Rgba32[blockWidth, blockHeight];
for (int x = 0; x < blockWidth; x++) {
for (int y = 0; y < blockHeight; y++) {
output[x, y] = encodedBlocks[x + y * blockWidth].Decode(false);
}
}
return output;
}
}
internal class Bc1ADecoder : IBcBlockDecoder {
public RawBlock4X4Rgba32[,] Decode(ReadOnlySpan<byte> data, int pixelWidth, int pixelHeight, out int blockWidth, out int blockHeight) {
blockWidth = (int)MathF.Ceiling(pixelWidth / 4.0f);
blockHeight = (int)MathF.Ceiling(pixelHeight / 4.0f);
if (data.Length != (blockWidth * blockHeight * Marshal.SizeOf<Bc1Block>())) {
throw new InvalidDataException();
}
var encodedBlocks = MemoryMarshal.Cast<byte, Bc1Block>(data);
RawBlock4X4Rgba32[,] output = new RawBlock4X4Rgba32[blockWidth, blockHeight];
for (int x = 0; x < blockWidth; x++) {
for (int y = 0; y < blockHeight; y++) {
output[x, y] = encodedBlocks[x + y * blockWidth].Decode(true);
}
}
return output;
}
}
internal class Bc2Decoder : IBcBlockDecoder {
public RawBlock4X4Rgba32[,] Decode(ReadOnlySpan<byte> data, int pixelWidth, int pixelHeight, out int blockWidth, out int blockHeight) {
blockWidth = (int)MathF.Ceiling(pixelWidth / 4.0f);
blockHeight = (int)MathF.Ceiling(pixelHeight / 4.0f);
if (data.Length != (blockWidth * blockHeight * Marshal.SizeOf<Bc2Block>())) {
throw new InvalidDataException();
}
var encodedBlocks = MemoryMarshal.Cast<byte, Bc2Block>(data);
RawBlock4X4Rgba32[,] output = new RawBlock4X4Rgba32[blockWidth, blockHeight];
for (int x = 0; x < blockWidth; x++) {
for (int y = 0; y < blockHeight; y++) {
output[x, y] = encodedBlocks[x + y * blockWidth].Decode();
}
}
return output;
}
}
internal class Bc3Decoder : IBcBlockDecoder {
public RawBlock4X4Rgba32[,] Decode(ReadOnlySpan<byte> data, int pixelWidth, int pixelHeight, out int blockWidth, out int blockHeight) {
blockWidth = (int)MathF.Ceiling(pixelWidth / 4.0f);
blockHeight = (int)MathF.Ceiling(pixelHeight / 4.0f);
if (data.Length != (blockWidth * blockHeight * Marshal.SizeOf<Bc3Block>())) {
throw new InvalidDataException();
}
var encodedBlocks = MemoryMarshal.Cast<byte, Bc3Block>(data);
RawBlock4X4Rgba32[,] output = new RawBlock4X4Rgba32[blockWidth, blockHeight];
for (int x = 0; x < blockWidth; x++) {
for (int y = 0; y < blockHeight; y++) {
output[x, y] = encodedBlocks[x + y * blockWidth].Decode();
}
}
return output;
}
}
internal class Bc4Decoder : IBcBlockDecoder {
private readonly bool redAsLuminance;
public Bc4Decoder(bool redAsLuminance) {
this.redAsLuminance = redAsLuminance;
}
public RawBlock4X4Rgba32[,] Decode(ReadOnlySpan<byte> data, int pixelWidth, int pixelHeight, out int blockWidth, out int blockHeight) {
blockWidth = (int)MathF.Ceiling(pixelWidth / 4.0f);
blockHeight = (int)MathF.Ceiling(pixelHeight / 4.0f);
if (data.Length != (blockWidth * blockHeight * Marshal.SizeOf<Bc4Block>())) {
throw new InvalidDataException();
}
var encodedBlocks = MemoryMarshal.Cast<byte, Bc4Block>(data);
RawBlock4X4Rgba32[,] output = new RawBlock4X4Rgba32[blockWidth, blockHeight];
for (int x = 0; x < blockWidth; x++) {
for (int y = 0; y < blockHeight; y++) {
output[x, y] = encodedBlocks[x + y * blockWidth].Decode(redAsLuminance);
}
}
return output;
}
}
internal class Bc5Decoder : IBcBlockDecoder {
public RawBlock4X4Rgba32[,] Decode(ReadOnlySpan<byte> data, int pixelWidth, int pixelHeight, out int blockWidth, out int blockHeight) {
blockWidth = (int)MathF.Ceiling(pixelWidth / 4.0f);
blockHeight = (int)MathF.Ceiling(pixelHeight / 4.0f);
if (data.Length != (blockWidth * blockHeight * Marshal.SizeOf<Bc5Block>())) {
throw new InvalidDataException();
}
var encodedBlocks = MemoryMarshal.Cast<byte, Bc5Block>(data);
RawBlock4X4Rgba32[,] output = new RawBlock4X4Rgba32[blockWidth, blockHeight];
for (int x = 0; x < blockWidth; x++) {
for (int y = 0; y < blockHeight; y++) {
output[x, y] = encodedBlocks[x + y * blockWidth].Decode();
}
}
return output;
}
}
}