forked from Nominom/BCnEncoder.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRgbBoundingBox.cs
More file actions
283 lines (228 loc) · 8.64 KB
/
Copy pathRgbBoundingBox.cs
File metadata and controls
283 lines (228 loc) · 8.64 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
using System;
using System.Runtime.InteropServices;
using BCnEncoder.Shared.Colors;
namespace BCnEncoder.Shared
{
/// <summary>
/// Calculate the bounding box of rgb values as described in
/// "Real-Time DXT Compression by J.M.P. van Waveren, 2006, Id Software, Inc." and
/// "Real-Time YCoCg-DXT Compression J.M.P. van Waveren, Ignacio Castaño id Software, Inc. NVIDIA Corp."
/// </summary>
internal static class RgbBoundingBox
{
public static void Create565(ReadOnlySpan<ColorRgba32> colors, out ColorB5G6R5Packed min, out ColorB5G6R5Packed max)
{
const int colorInsetShift = 4;
const int c5655Mask = 0xF8;
const int c5656Mask = 0xFC;
int minR = 255,
minG = 255,
minB = 255;
int maxR = 0,
maxG = 0,
maxB = 0;
for (var i = 0; i < colors.Length; i++)
{
var c = colors[i];
if (c.r < minR) minR = c.r;
if (c.g < minG) minG = c.g;
if (c.b < minB) minB = c.b;
if (c.r > maxR) maxR = c.r;
if (c.g > maxG) maxG = c.g;
if (c.b > maxB) maxB = c.b;
}
var insetR = (maxR - minR) >> colorInsetShift;
var insetG = (maxG - minG) >> colorInsetShift;
var insetB = (maxB - minB) >> colorInsetShift;
// Inset by 1/16th
minR = ((minR << colorInsetShift) + insetR) >> colorInsetShift;
minG = ((minG << colorInsetShift) + insetG) >> colorInsetShift;
minB = ((minB << colorInsetShift) + insetB) >> colorInsetShift;
maxR = ((maxR << colorInsetShift) - insetR) >> colorInsetShift;
maxG = ((maxG << colorInsetShift) - insetG) >> colorInsetShift;
maxB = ((maxB << colorInsetShift) - insetB) >> colorInsetShift;
minR = minR >= 0 ? minR : 0;
minG = minG >= 0 ? minG : 0;
minB = minB >= 0 ? minB : 0;
maxR = maxR <= 255 ? maxR : 255;
maxG = maxG <= 255 ? maxG : 255;
maxB = maxB <= 255 ? maxB : 255;
// Optimal rounding
minR = (minR & c5655Mask) | (minR >> 5);
minG = (minG & c5656Mask) | (minG >> 6);
minB = (minB & c5655Mask) | (minB >> 5);
maxR = (maxR & c5655Mask) | (maxR >> 5);
maxG = (maxG & c5656Mask) | (maxG >> 6);
maxB = (maxB & c5655Mask) | (maxB >> 5);
min = new ColorB5G6R5Packed((byte)minR, (byte)minG, (byte)minB);
max = new ColorB5G6R5Packed((byte)maxR, (byte)maxG, (byte)maxB);
}
public static void Create565AlphaCutoff(ReadOnlySpan<ColorRgba32> colors, out ColorB5G6R5Packed min, out ColorB5G6R5Packed max, int alphaCutoff = 128)
{
const int colorInsetShift = 4;
const int c5655Mask = 0xF8;
const int c5656Mask = 0xFC;
int minR = 255,
minG = 255,
minB = 255;
int maxR = 0,
maxG = 0,
maxB = 0;
for (var i = 0; i < colors.Length; i++)
{
var c = colors[i];
if (c.a < alphaCutoff) continue;
if (c.r < minR) minR = c.r;
if (c.g < minG) minG = c.g;
if (c.b < minB) minB = c.b;
if (c.r > maxR) maxR = c.r;
if (c.g > maxG) maxG = c.g;
if (c.b > maxB) maxB = c.b;
}
var insetR = (maxR - minR) >> colorInsetShift;
var insetG = (maxG - minG) >> colorInsetShift;
var insetB = (maxB - minB) >> colorInsetShift;
minR = ((minR << colorInsetShift) + insetR) >> colorInsetShift;
minG = ((minG << colorInsetShift) + insetG) >> colorInsetShift;
minB = ((minB << colorInsetShift) + insetB) >> colorInsetShift;
maxR = ((maxR << colorInsetShift) - insetR) >> colorInsetShift;
maxG = ((maxG << colorInsetShift) - insetG) >> colorInsetShift;
maxB = ((maxB << colorInsetShift) - insetB) >> colorInsetShift;
minR = minR >= 0 ? minR : 0;
minG = minG >= 0 ? minG : 0;
minB = minB >= 0 ? minB : 0;
maxR = maxR <= 255 ? maxR : 255;
maxG = maxG <= 255 ? maxG : 255;
maxB = maxB <= 255 ? maxB : 255;
minR = (minR & c5655Mask) | (minR >> 5);
minG = (minG & c5656Mask) | (minG >> 6);
minB = (minB & c5655Mask) | (minB >> 5);
maxR = (maxR & c5655Mask) | (maxR >> 5);
maxG = (maxG & c5656Mask) | (maxG >> 6);
maxB = (maxB & c5655Mask) | (maxB >> 5);
min = new ColorB5G6R5Packed((byte)minR, (byte)minG, (byte)minB);
max = new ColorB5G6R5Packed((byte)maxR, (byte)maxG, (byte)maxB);
}
public static void Create565A(ReadOnlySpan<ColorRgba32> colors, out ColorB5G6R5Packed min, out ColorB5G6R5Packed max, out byte minAlpha, out byte maxAlpha)
{
const int colorInsetShift = 4;
const int alphaInsetShift = 5;
const int c5655Mask = 0xF8;
const int c5656Mask = 0xFC;
int minR = 255,
minG = 255,
minB = 255,
minA = 255;
int maxR = 0,
maxG = 0,
maxB = 0,
maxA = 0;
for (var i = 0; i < colors.Length; i++)
{
var c = colors[i];
if (c.r < minR) minR = c.r;
if (c.g < minG) minG = c.g;
if (c.b < minB) minB = c.b;
if (c.a < minA) minA = c.a;
if (c.r > maxR) maxR = c.r;
if (c.g > maxG) maxG = c.g;
if (c.b > maxB) maxB = c.b;
if (c.a > maxA) maxA = c.a;
}
var insetR = (maxR - minR) >> colorInsetShift;
var insetG = (maxG - minG) >> colorInsetShift;
var insetB = (maxB - minB) >> colorInsetShift;
var insetA = (maxA - minA) >> alphaInsetShift;
minR = ((minR << colorInsetShift) + insetR) >> colorInsetShift;
minG = ((minG << colorInsetShift) + insetG) >> colorInsetShift;
minB = ((minB << colorInsetShift) + insetB) >> colorInsetShift;
minA = ((minA << alphaInsetShift) + insetA) >> alphaInsetShift;
maxR = ((maxR << colorInsetShift) - insetR) >> colorInsetShift;
maxG = ((maxG << colorInsetShift) - insetG) >> colorInsetShift;
maxB = ((maxB << colorInsetShift) - insetB) >> colorInsetShift;
maxA = ((maxA << alphaInsetShift) - insetA) >> alphaInsetShift;
minR = minR >= 0 ? minR : 0;
minG = minG >= 0 ? minG : 0;
minB = minB >= 0 ? minB : 0;
minA = minA >= 0 ? minA : 0;
maxR = maxR <= 255 ? maxR : 255;
maxG = maxG <= 255 ? maxG : 255;
maxB = maxB <= 255 ? maxB : 255;
maxA = maxA <= 255 ? maxA : 255;
minR = (minR & c5655Mask) | (minR >> 5);
minG = (minG & c5656Mask) | (minG >> 6);
minB = (minB & c5655Mask) | (minB >> 5);
maxR = (maxR & c5655Mask) | (maxR >> 5);
maxG = (maxG & c5656Mask) | (maxG >> 6);
maxB = (maxB & c5655Mask) | (maxB >> 5);
min = new ColorB5G6R5Packed((byte)minR, (byte)minG, (byte)minB);
max = new ColorB5G6R5Packed((byte)maxR, (byte)maxG, (byte)maxB);
minAlpha = (byte)minA;
maxAlpha = (byte)maxA;
}
/// <summary>
/// Hdr rgb bounding box inset by Krzysztof Narkowicz. https://github.com/knarkowicz/GPURealTimeBC6H
/// Code is public domain.
/// </summary>
private static void InsetHdrChannel(ReadOnlySpan<ColorRgbaFloat> colors, int channel, ref float blockMax, ref float blockMin)
{
var offset = 0f;
if (blockMin < 0)
{
offset = -blockMin;
blockMin += offset;
blockMax += offset;
}
float Select(ReadOnlySpan<float> span, int i)
{
return span[i * 4 + channel] + offset;
}
var floats = MemoryMarshal.Cast<ColorRgbaFloat, float>(colors);
var refinedBlockMin = blockMax;
var refinedBlockMax = blockMin;
for (var i = 0; i < 16; ++i)
{
refinedBlockMin = MathF.Min(refinedBlockMin, Select(floats, i) == blockMin ? refinedBlockMin : Select(floats, i));
refinedBlockMax = MathF.Max(refinedBlockMax, Select(floats, i) == blockMax ? refinedBlockMax : Select(floats, i));
}
var logRefinedBlockMax = MathF.Log(refinedBlockMax + 1.0f, 2);
var logRefinedBlockMin = MathF.Log(refinedBlockMin + 1.0f, 2);
var logBlockMax = MathF.Log(blockMax + 1.0f, 2);
var logBlockMin = MathF.Log(blockMin + 1.0f, 2);
var logBlockMaxExt = (logBlockMax - logBlockMin) * (1.0f / 32.0f);
logBlockMin += MathF.Min(logRefinedBlockMin - logBlockMin, logBlockMaxExt);
logBlockMax -= MathF.Min(logBlockMax - logRefinedBlockMax, logBlockMaxExt);
blockMin = MathF.Pow(2, logBlockMin) - 1.0f - offset;
blockMax = MathF.Pow(2, logBlockMax) - 1.0f - offset;
}
public static void CreateFloat(ReadOnlySpan<ColorRgbaFloat> colors, out ColorRgbaFloat min, out ColorRgbaFloat max)
{
float minR = float.MaxValue,
minG = float.MaxValue,
minB = float.MaxValue,
minA = float.MaxValue;
float maxR = float.MinValue,
maxG = float.MinValue,
maxB = float.MinValue,
maxA = float.MinValue;
for (var i = 0; i < colors.Length; i++)
{
var c = colors[i];
if (c.r < minR) minR = c.r;
if (c.g < minG) minG = c.g;
if (c.b < minB) minB = c.b;
if (c.a < minA) minA = c.a;
if (c.r > maxR) maxR = c.r;
if (c.g > maxG) maxG = c.g;
if (c.b > maxB) maxB = c.b;
if (c.b > maxA) maxA = c.a;
}
InsetHdrChannel(colors, 0, ref maxR, ref minR);
InsetHdrChannel(colors, 1, ref maxG, ref minG);
InsetHdrChannel(colors, 2, ref maxB, ref minB);
InsetHdrChannel(colors, 3, ref maxA, ref minA);
min = new ColorRgbaFloat(minR, minG, minB, minA);
max = new ColorRgbaFloat(maxR, maxG, maxB, maxA);
}
}
}