forked from Nominom/BCnEncoder.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBc5BlockEncoder.cs
More file actions
317 lines (291 loc) · 8.68 KB
/
Copy pathBc5BlockEncoder.cs
File metadata and controls
317 lines (291 loc) · 8.68 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using BCnEncoder.Shared;
namespace BCnEncoder.Encoder
{
internal class Bc5BlockEncoder : IBcBlockEncoder {
public byte[] Encode(RawBlock4X4Rgba32[] blocks, int blockWidth, int blockHeight, EncodingQuality quality, bool parallel)
{
byte[] outputData = new byte[blockWidth * blockHeight * Marshal.SizeOf<Bc5Block>()];
Span<Bc5Block> outputBlocks = MemoryMarshal.Cast<byte, Bc5Block>(outputData);
if (parallel) {
Parallel.For(0, blocks.Length, i => {
Span<Bc5Block> outputBlocks = MemoryMarshal.Cast<byte, Bc5Block>(outputData);
outputBlocks[i] = EncodeBlock(blocks[i], quality);
});
}
else {
for (int i = 0; i < blocks.Length; i++) {
outputBlocks[i] = EncodeBlock(blocks[i], quality);
}
}
return outputData;
}
private Bc5Block EncodeBlock(RawBlock4X4Rgba32 block, EncodingQuality quality) {
Bc5Block output = new Bc5Block();
byte[] reds = new byte[16];
byte[] greens = new byte[16];
var pixels = block.AsSpan;
for (int i = 0; i < 16; i++) {
reds[i] = pixels[i].R;
greens[i] = pixels[i].G;
}
int variations = 0;
int errorThreshold = 0;
switch (quality) {
case EncodingQuality.Fast:
variations = 3;
errorThreshold = 5;
break;
case EncodingQuality.Balanced:
variations = 5;
errorThreshold = 1;
break;
case EncodingQuality.BestQuality:
variations = 8;
errorThreshold = 0;
break;
default:
throw new ArgumentOutOfRangeException(nameof(quality), quality, null);
}
output = FindValues(output, reds, variations, errorThreshold,
(block, i, idx) => {
block.SetRedIndex(i, idx);
return block;
},
(block, col) => {
block.Red0 = col;
return block;
},
(block, col) => {
block.Red1 = col;
return block;
},
(block) => {
return block.Red0;
},
(block) => {
return block.Red1;
}
);
output = FindValues(output, greens, variations, errorThreshold,
(block, i, idx) => {
block.SetGreenIndex(i, idx);
return block;
},
(block, col) => {
block.Green0 = col;
return block;
},
(block, col) => {
block.Green1 = col;
return block;
},
(block) => {
return block.Green0;
},
(block) => {
return block.Green1;
});
return output;
}
public GlInternalFormat GetInternalFormat() {
return GlInternalFormat.GL_COMPRESSED_RED_GREEN_RGTC2_EXT;
}
public GLFormat GetBaseInternalFormat() {
return GLFormat.GL_RG;
}
public DXGI_FORMAT GetDxgiFormat() {
return DXGI_FORMAT.DXGI_FORMAT_BC5_UNORM;
}
#region Encoding private stuff
private static Bc5Block FindValues(Bc5Block colorBlock, byte[] pixels, int variations, int errorThreshold,
Func<Bc5Block, int, byte, Bc5Block> indexSetter,
Func<Bc5Block, byte, Bc5Block> col0Setter,
Func<Bc5Block, byte, Bc5Block> col1Setter,
Func<Bc5Block, byte> col0Getter,
Func<Bc5Block, byte> col1Getter) {
//Find min and max alpha
byte min = 255;
byte max = 0;
bool hasExtremeValues = false;
for (int i = 0; i < pixels.Length; i++) {
if (pixels[i] < 255 && pixels[i] > 0) {
if (pixels[i] < min) min = pixels[i];
if (pixels[i] > max) max = pixels[i];
}
else {
hasExtremeValues = true;
}
}
int SelectIndices(ref Bc5Block block) {
int cumulativeError = 0;
//var c0 = block.Red0;
//var c1 = block.Red1;
var c0 = col0Getter(block);
var c1 = col1Getter(block);
Span<byte> colors = c0 > c1
? stackalloc byte[] {
c0,
c1,
(byte) (6 / 7.0 * c0 + 1 / 7.0 * c1),
(byte) (5 / 7.0 * c0 + 2 / 7.0 * c1),
(byte) (4 / 7.0 * c0 + 3 / 7.0 * c1),
(byte) (3 / 7.0 * c0 + 4 / 7.0 * c1),
(byte) (2 / 7.0 * c0 + 5 / 7.0 * c1),
(byte) (1 / 7.0 * c0 + 6 / 7.0 * c1),
}
: stackalloc byte[] {
c0,
c1,
(byte) (4 / 5.0 * c0 + 1 / 5.0 * c1),
(byte) (3 / 5.0 * c0 + 2 / 5.0 * c1),
(byte) (2 / 5.0 * c0 + 3 / 5.0 * c1),
(byte) (1 / 5.0 * c0 + 4 / 5.0 * c1),
0,
255
};
for (int i = 0; i < pixels.Length; i++) {
byte bestIndex = 0;
int bestError = Math.Abs(pixels[i] - colors[0]);
for (byte j = 1; j < colors.Length; j++) {
int error = Math.Abs(pixels[i] - colors[j]);
if (error < bestError) {
bestIndex = j;
bestError = error;
}
if (bestError == 0) break;
}
block = indexSetter(block, i, bestIndex);
//block.SetRedIndex(i, bestIndex);
cumulativeError += bestError * bestError;
}
return cumulativeError;
}
//everything is either fully black or fully red
if (hasExtremeValues && min == 255 && max == 0) {
//colorBlock.Red0 = 0;
//colorBlock.Red1 = 255;
colorBlock = col0Setter(colorBlock, 0);
colorBlock = col1Setter(colorBlock, 255);
var error = SelectIndices(ref colorBlock);
Debug.Assert(0 == error);
return colorBlock;
}
var best = colorBlock;
//best.Red0 = max;
//best.Red1 = min;
best = col0Setter(best, max);
best = col1Setter(best, min);
int bestError = SelectIndices(ref best);
if (bestError == 0) {
return best;
}
for (byte i = (byte)variations; i > 0; i--) {
{
byte c0 = ByteHelper.ClampToByte(max - i);
byte c1 = ByteHelper.ClampToByte(min + i);
var block = colorBlock;
//block.Red0 = hasExtremeValues ? c1 : c0;
//block.Red1 = hasExtremeValues ? c0 : c1;
block = col0Setter(block, hasExtremeValues ? c1 : c0);
block = col1Setter(block, hasExtremeValues ? c0 : c1);
int error = SelectIndices(ref block);
if (error < bestError) {
best = block;
bestError = error;
max = c0;
min = c1;
}
}
{
byte c0 = ByteHelper.ClampToByte(max + i);
byte c1 = ByteHelper.ClampToByte(min - i);
var block = colorBlock;
//block.Red0 = hasExtremeValues ? c1 : c0;
//block.Red1 = hasExtremeValues ? c0 : c1;
block = col0Setter(block, hasExtremeValues ? c1 : c0);
block = col1Setter(block, hasExtremeValues ? c0 : c1);
int error = SelectIndices(ref block);
if (error < bestError) {
best = block;
bestError = error;
max = c0;
min = c1;
}
}
{
byte c0 = ByteHelper.ClampToByte(max);
byte c1 = ByteHelper.ClampToByte(min - i);
var block = colorBlock;
//block.Red0 = hasExtremeValues ? c1 : c0;
//block.Red1 = hasExtremeValues ? c0 : c1;
block = col0Setter(block, hasExtremeValues ? c1 : c0);
block = col1Setter(block, hasExtremeValues ? c0 : c1);
int error = SelectIndices(ref block);
if (error < bestError) {
best = block;
bestError = error;
max = c0;
min = c1;
}
}
{
byte c0 = ByteHelper.ClampToByte(max + i);
byte c1 = ByteHelper.ClampToByte(min);
var block = colorBlock;
//block.Red0 = hasExtremeValues ? c1 : c0;
//block.Red1 = hasExtremeValues ? c0 : c1;
block = col0Setter(block, hasExtremeValues ? c1 : c0);
block = col1Setter(block, hasExtremeValues ? c0 : c1);
int error = SelectIndices(ref block);
if (error < bestError) {
best = block;
bestError = error;
max = c0;
min = c1;
}
}
{
byte c0 = ByteHelper.ClampToByte(max);
byte c1 = ByteHelper.ClampToByte(min + i);
var block = colorBlock;
//block.Red0 = hasExtremeValues ? c1 : c0;
//block.Red1 = hasExtremeValues ? c0 : c1;
block = col0Setter(block, hasExtremeValues ? c1 : c0);
block = col1Setter(block, hasExtremeValues ? c0 : c1);
int error = SelectIndices(ref block);
if (error < bestError) {
best = block;
bestError = error;
max = c0;
min = c1;
}
}
{
byte c0 = ByteHelper.ClampToByte(max - i);
byte c1 = ByteHelper.ClampToByte(min);
var block = colorBlock;
//block.Red0 = hasExtremeValues ? c1 : c0;
//block.Red1 = hasExtremeValues ? c0 : c1;
block = col0Setter(block, hasExtremeValues ? c1 : c0);
block = col1Setter(block, hasExtremeValues ? c0 : c1);
int error = SelectIndices(ref block);
if (error < bestError) {
best = block;
bestError = error;
max = c0;
min = c1;
}
}
if (bestError <= errorThreshold) {
break;
}
}
return best;
}
#endregion
}
}