forked from crskycode/BCnEncoder.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBc4BlockEncoder.cs
More file actions
251 lines (227 loc) · 6.32 KB
/
Copy pathBc4BlockEncoder.cs
File metadata and controls
251 lines (227 loc) · 6.32 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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using BCnEncoder.Shared;
namespace BCnEncoder.Encoder
{
internal class Bc4BlockEncoder : IBcBlockEncoder {
private readonly bool luminanceAsRed;
public Bc4BlockEncoder(bool luminanceAsRed) {
this.luminanceAsRed = luminanceAsRed;
}
public unsafe byte[] Encode(RawBlock4X4Rgba32[] blocks, int blockWidth, int blockHeight, CompressionQuality quality,
bool parallel) {
byte[] outputData = new byte[blockWidth * blockHeight * sizeof(Bc4Block)];
fixed (byte* oDataBytes = outputData)
{
Bc4Block* oDataBlocks = (Bc4Block*)oDataBytes;
int oDataBlocksLength = outputData.Length / sizeof(Bc4Block);
for (int i = 0; i < oDataBlocksLength; i++)
{
oDataBlocks[i] = EncodeBlock(blocks[i], quality);
}
}
return outputData;
}
private Bc4Block EncodeBlock(RawBlock4X4Rgba32 block, CompressionQuality quality) {
Bc4Block output = new Bc4Block();
byte[] colors = new byte[16];
var pixels = block.AsArray;
for (int i = 0; i < 16; i++) {
if (luminanceAsRed) {
colors[i] = (byte)(new ColorYCbCr(pixels[i]).y * 255);
}
else {
colors[i] = pixels[i].R;
}
}
switch (quality) {
case CompressionQuality.Fast:
return FindRedValues(output, colors, 3);
case CompressionQuality.Balanced:
return FindRedValues(output, colors, 4);
case CompressionQuality.BestQuality:
return FindRedValues(output, colors, 8);
default:
throw new ArgumentOutOfRangeException(nameof(quality), quality, null);
}
}
public GlInternalFormat GetInternalFormat() {
return GlInternalFormat.GL_COMPRESSED_RED_RGTC1_EXT;
}
public GLFormat GetBaseInternalFormat() {
return GLFormat.GL_RED;
}
public DXGI_FORMAT GetDxgiFormat() {
return DXGI_FORMAT.DXGI_FORMAT_BC4_UNORM;
}
#region Encoding private stuff
private static int SelectIndices(ref Bc4Block block, byte[] pixels)
{
int cumulativeError = 0;
var c0 = block.Red0;
var c1 = block.Red1;
byte[] colors = c0 > c1
? new 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),
}
: new 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.SetRedIndex(i, bestIndex);
cumulativeError += bestError * bestError;
}
return cumulativeError;
}
private static Bc4Block FindRedValues(Bc4Block colorBlock, byte[] pixels, int variations) {
//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;
}
}
//everything is either fully black or fully red
if (hasExtremeValues && min == 255 && max == 0) {
colorBlock.Red0 = 0;
colorBlock.Red1 = 255;
var error = SelectIndices(ref colorBlock, pixels);
Debug.Assert(0 == error);
return colorBlock;
}
var best = colorBlock;
best.Red0 = max;
best.Red1 = min;
int bestError = SelectIndices(ref best, pixels);
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;
int error = SelectIndices(ref block, pixels);
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;
int error = SelectIndices(ref block, pixels);
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;
int error = SelectIndices(ref block, pixels);
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;
int error = SelectIndices(ref block, pixels);
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;
int error = SelectIndices(ref block, pixels);
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;
int error = SelectIndices(ref block, pixels);
if (error < bestError) {
best = block;
bestError = error;
max = c0;
min = c1;
}
}
if (bestError < 5) {
break;
}
}
return best;
}
#endregion
}
}