Skip to content

Commit 8cf9fc3

Browse files
committed
Clean up tests and improve comments
1 parent cfd288c commit 8cf9fc3

3 files changed

Lines changed: 52 additions & 174 deletions

File tree

‎tests/ImageSharp.Textures.Tests/Formats/Astc/EndpointCodecLdrTests.cs‎

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ private static (Rgba32 Low, Rgba32 High) Decode(ColorEndpointMode mode, params i
1919
return (pair.LdrLow, pair.LdrHigh);
2020
}
2121

22-
// ---- Mode 0: LdrLumaDirect (spec §C.2.14 — "Direct luminance") ----
23-
22+
// Mode 0: LdrLumaDirect (spec §C.2.14 — "Direct luminance")
2423
[Fact]
2524
public void Decode_LdrLumaDirect_ProducesGrayscaleWithFullAlpha()
2625
{
@@ -31,12 +30,13 @@ public void Decode_LdrLumaDirect_ProducesGrayscaleWithFullAlpha()
3130
Assert.Equal(new Rgba32(0xE0, 0xE0, 0xE0, 255), high);
3231
}
3332

34-
// ---- Mode 1: LdrLumaBaseOffset (spec §C.2.14 — "Luminance, base+offset") ----
35-
33+
// Mode 1: LdrLumaBaseOffset (spec §C.2.14 — "Luminance, base+offset")
3634
[Fact]
3735
public void Decode_LdrLumaBaseOffset_DecodesBaseAndOffset()
3836
{
39-
// L0 = (v0 >> 2) | (v1 & 0xC0); L1 = L0 + (v1 & 0x3F), saturated at 0xFF.
37+
// Mode 1 (spec §C.2.14): the base luma is packed across v0 and v1's high bits, and v1's
38+
// low bits are a saturating offset producing the high endpoint. The expected values below
39+
// mirror that derivation.
4040
int v0 = 0x80;
4141
int v1 = 0x6F;
4242
int l0 = (v0 >> 2) | (v1 & 0xC0);
@@ -51,14 +51,14 @@ public void Decode_LdrLumaBaseOffset_DecodesBaseAndOffset()
5151
[Fact]
5252
public void Decode_LdrLumaBaseOffset_SaturatesOffsetAtFF()
5353
{
54-
// Choose v1 so L0 + offset > 0xFF.
54+
// Mode 1 (spec §C.2.14): max inputs push the base+offset high endpoint past 0xFF, forcing
55+
// the saturation clamp.
5556
(Rgba32 _, Rgba32 high) = Decode(ColorEndpointMode.LdrLumaBaseOffset, 0xFF, 0xFF);
5657

5758
Assert.Equal(255, high.R);
5859
}
5960

60-
// ---- Mode 4: LdrLumaAlphaDirect (spec §C.2.14 — "Luminance+alpha, direct") ----
61-
61+
// Mode 4: LdrLumaAlphaDirect (spec §C.2.14 — "Luminance+alpha, direct")
6262
[Fact]
6363
public void Decode_LdrLumaAlphaDirect_DecodesLumaAndAlphaIndependently()
6464
{
@@ -69,12 +69,12 @@ public void Decode_LdrLumaAlphaDirect_DecodesLumaAndAlphaIndependently()
6969
Assert.Equal(new Rgba32(0xF0, 0xF0, 0xF0, 0xC0), high);
7070
}
7171

72-
// ---- Mode 5: LdrLumaAlphaBaseOffset (spec §C.2.14) ----
73-
72+
// Mode 5: LdrLumaAlphaBaseOffset (spec §C.2.14)
7473
[Fact]
7574
public void Decode_LdrLumaAlphaBaseOffset_DecodesTransferPrecisionPairs()
7675
{
77-
// TransferPrecision unpacks each (high, low) pair into (offset, base).
76+
// Mode 5 (spec §C.2.14): luma and alpha each arrive as a (base, offset) pair recovered by
77+
// the spec's bit_transfer_signed step (BitOperations.TransferPrecision).
7878
(int b0, int a0) = BitOperations.TransferPrecision(0x30, 0x80);
7979
(int b2, int a2) = BitOperations.TransferPrecision(0x10, 0x40);
8080

@@ -86,47 +86,48 @@ public void Decode_LdrLumaAlphaBaseOffset_DecodesTransferPrecisionPairs()
8686
Assert.Equal(new Rgba32((byte)highLuma, (byte)highLuma, (byte)highLuma, (byte)highAlpha), high);
8787
}
8888

89-
// ---- Mode 6: LdrRgbBaseScale (spec §C.2.14 — "RGB, base+scale") ----
90-
89+
// Mode 6: LdrRgbBaseScale (spec §C.2.14 — "RGB, base+scale")
9190
[Fact]
9291
public void Decode_LdrRgbBaseScale_LowIsScaledHigh()
9392
{
94-
// low = (v0,v1,v2) * v3 >> 8 ; high = (v0,v1,v2). Alpha = 255 on both.
93+
// Mode 6 (spec §C.2.14): the high endpoint is the base RGB (v0,v1,v2), and the low
94+
// endpoint is that base scaled down by v3. Alpha is opaque on both.
9595
(Rgba32 low, Rgba32 high) = Decode(ColorEndpointMode.LdrRgbBaseScale, 0xFF, 0x80, 0x40, 0x80);
9696

97-
Assert.Equal(new Rgba32((byte)((0xFF * 0x80) >> 8), (byte)((0x80 * 0x80) >> 8), (byte)((0x40 * 0x80) >> 8), 255), low);
97+
Assert.Equal(new Rgba32((0xFF * 0x80) >> 8, (0x80 * 0x80) >> 8, (0x40 * 0x80) >> 8, 255), low);
9898
Assert.Equal(new Rgba32(0xFF, 0x80, 0x40, 255), high);
9999
}
100100

101-
// ---- Mode 8: LdrRgbDirect (spec §C.2.14) with blue-contract swap ----
102-
101+
// Mode 8: LdrRgbDirect (spec §C.2.14) with blue-contract swap
103102
[Fact]
104103
public void Decode_LdrRgbDirect_WhenHighIsDimmer_SwapsEndpointsAndAveragesBlue()
105104
{
106-
// sum1 (high) < sum0 (low) triggers blue-contract swap.
105+
// Mode 8 (spec §C.2.14): when the high triple is dimmer than the low triple the endpoints
106+
// are swapped and the blue-contract correction is applied. These inputs select that path.
107107
(Rgba32 low, Rgba32 high) = Decode(ColorEndpointMode.LdrRgbDirect, 0xC0, 0x20, 0xC0, 0x20, 0xC0, 0x20);
108108

109-
// Swapped: low uses odd-indexed (high-side) values with blue-contract averaging.
110109
Assert.Equal(new Rgba32((byte)((0x20 + 0x20) >> 1), (byte)((0x20 + 0x20) >> 1), 0x20, 255), low);
111110
Assert.Equal(new Rgba32((byte)((0xC0 + 0xC0) >> 1), (byte)((0xC0 + 0xC0) >> 1), 0xC0, 255), high);
112111
}
113112

114113
[Fact]
115114
public void Decode_LdrRgbDirect_WhenHighIsBrighter_KeepsDirectValues()
116115
{
117-
// sum1 (high) >= sum0 (low) → no swap.
116+
// Mode 8 (spec §C.2.14): when the high triple is brighter, no swap or blue-contract is
117+
// applied and the endpoints pass through directly.
118118
(Rgba32 low, Rgba32 high) = Decode(ColorEndpointMode.LdrRgbDirect, 0x20, 0xC0, 0x20, 0xC0, 0x20, 0xC0);
119119

120120
Assert.Equal(new Rgba32(0x20, 0x20, 0x20, 255), low);
121121
Assert.Equal(new Rgba32(0xC0, 0xC0, 0xC0, 255), high);
122122
}
123123

124-
// ---- Mode 9: LdrRgbBaseOffset (spec §C.2.14 — with blue-contract) ----
125-
124+
// Mode 9: LdrRgbBaseOffset (spec §C.2.14 — with blue-contract)
126125
[Fact]
127126
public void Decode_LdrRgbBaseOffset_NonNegativeSum_ProducesBasePlusOffset()
128127
{
129-
// b0+b1+b2 >= 0 → low = base, high = base + offset.
128+
// Mode 9 (spec §C.2.14): each channel is a (base, offset) pair via bit_transfer_signed.
129+
// With a non-negative offset sum, the blue-contract branch is skipped: low = base,
130+
// high = base + offset.
130131
(int b0, int a0) = BitOperations.TransferPrecision(0x10, 0x80);
131132
(int b1, int a1) = BitOperations.TransferPrecision(0x08, 0x40);
132133
(int b2, int a2) = BitOperations.TransferPrecision(0x04, 0x20);
@@ -140,19 +141,17 @@ public void Decode_LdrRgbBaseOffset_NonNegativeSum_ProducesBasePlusOffset()
140141
Assert.Equal(new Rgba32((byte)hr, (byte)hg, (byte)hb, 255), high);
141142
}
142143

143-
// ---- Mode 10: LdrRgbBaseScaleTwoA (spec §C.2.14 — base+scale with separate alpha) ----
144-
144+
// Mode 10: LdrRgbBaseScaleTwoA (spec §C.2.14 — base+scale with separate alpha)
145145
[Fact]
146146
public void Decode_LdrRgbBaseScaleTwoA_AppliesScaleAndSeparateAlphaChannels()
147147
{
148148
(Rgba32 low, Rgba32 high) = Decode(ColorEndpointMode.LdrRgbBaseScaleTwoA, 0xFF, 0x80, 0x40, 0x80, 0x20, 0xE0);
149149

150-
Assert.Equal(new Rgba32((byte)((0xFF * 0x80) >> 8), (byte)((0x80 * 0x80) >> 8), (byte)((0x40 * 0x80) >> 8), 0x20), low);
150+
Assert.Equal(new Rgba32((0xFF * 0x80) >> 8, (0x80 * 0x80) >> 8, (0x40 * 0x80) >> 8, 0x20), low);
151151
Assert.Equal(new Rgba32(0xFF, 0x80, 0x40, 0xE0), high);
152152
}
153153

154-
// ---- Mode 12: LdrRgbaDirect (spec §C.2.14) ----
155-
154+
// Mode 12: LdrRgbaDirect (spec §C.2.14)
156155
[Fact]
157156
public void Decode_LdrRgbaDirect_WhenHighIsBrighter_KeepsDirectValues()
158157
{
@@ -167,13 +166,13 @@ public void Decode_LdrRgbaDirect_WhenHighIsDimmer_AppliesBlueContractAndSwaps()
167166
{
168167
(Rgba32 low, Rgba32 high) = Decode(ColorEndpointMode.LdrRgbaDirect, 0xC0, 0x20, 0xC0, 0x20, 0xC0, 0x20, 0x30, 0xB0);
169168

170-
// Blue-contract swap: alpha indexes (6,7) swap too — low gets v7, high gets v6.
171-
Assert.Equal(new Rgba32((byte)((0x20 + 0x20) >> 1), (byte)((0x20 + 0x20) >> 1), 0x20, 0xB0), low);
172-
Assert.Equal(new Rgba32((byte)((0xC0 + 0xC0) >> 1), (byte)((0xC0 + 0xC0) >> 1), 0xC0, 0x30), high);
169+
// Mode 12 (spec §C.2.14): the dimmer-high case swaps endpoints (RGB via blue-contract,
170+
// alpha by index swap), so low takes the high-side alpha and vice versa.
171+
Assert.Equal(new Rgba32((0x20 + 0x20) >> 1, (0x20 + 0x20) >> 1, 0x20, 0xB0), low);
172+
Assert.Equal(new Rgba32((0xC0 + 0xC0) >> 1, (0xC0 + 0xC0) >> 1, 0xC0, 0x30), high);
173173
}
174174

175-
// ---- Mode 13: LdrRgbaBaseOffset (spec §C.2.14) ----
176-
175+
// Mode 13: LdrRgbaBaseOffset (spec §C.2.14)
177176
[Fact]
178177
public void Decode_LdrRgbaBaseOffset_DecodesAllFourChannelsWithTransferPrecision()
179178
{

‎tests/ImageSharp.Textures.Tests/Formats/Astc/HDR/HdrEndpointDecoderTests.cs‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public class HdrEndpointDecoderTests
1515
[Fact]
1616
public void Decode_HdrLumaLargeRange_WithAscendingValues_ReturnsExpected()
1717
{
18-
// v1 >= v0 branch: y0 = v0 << 4, y1 = v1 << 4
18+
// HDR luminance large range (CEM 2, spec §C.2.14): when v0 <= v1 the endpoints take the
19+
// direct branch — no swap and no rounding offset.
1920
ReadOnlySpan<int> values = [0x10, 0x80];
2021
(Rgba64 low, Rgba64 high) = HdrEndpointDecoder.DecodeHdrModeUnquantized(values, ColorEndpointMode.HdrLumaLargeRange);
2122

@@ -26,7 +27,8 @@ public void Decode_HdrLumaLargeRange_WithAscendingValues_ReturnsExpected()
2627
[Fact]
2728
public void Decode_HdrLumaLargeRange_WithDescendingValues_ReturnsSwappedAndOffset()
2829
{
29-
// v1 < v0 branch: y0 = (v1 << 4) + 8, y1 = (v0 << 4) - 8
30+
// HDR luminance large range (CEM 2, spec §C.2.14): when v0 > v1 the endpoints are
31+
// swapped and each nudged by a half-step rounding offset.
3032
ReadOnlySpan<int> values = [0x80, 0x10];
3133
(Rgba64 low, Rgba64 high) = HdrEndpointDecoder.DecodeHdrModeUnquantized(values, ColorEndpointMode.HdrLumaLargeRange);
3234

@@ -39,7 +41,8 @@ public void Decode_HdrLumaLargeRange_WithDescendingValues_ReturnsSwappedAndOffse
3941
[Fact]
4042
public void Decode_HdrLumaSmallRange_V0LowBitClear_UsesLowBitBranch()
4143
{
42-
// (v0 & 0x80) == 0 -> y0 uses v1 & 0xF0, shift << 1
44+
// HDR luminance small range (CEM 3, spec §C.2.14): the top bit of v0 selects between two
45+
// bit layouts. With it clear, the low-precision layout applies.
4346
ReadOnlySpan<int> values = [0x40, 0x55];
4447
(Rgba64 low, Rgba64 high) = HdrEndpointDecoder.DecodeHdrModeUnquantized(values, ColorEndpointMode.HdrLumaSmallRange);
4548

@@ -53,7 +56,8 @@ public void Decode_HdrLumaSmallRange_V0LowBitClear_UsesLowBitBranch()
5356
[Fact]
5457
public void Decode_HdrLumaSmallRange_V0HighBitSet_UsesHighBitBranch()
5558
{
56-
// (v0 & 0x80) != 0 -> y0 uses v1 & 0xE0, shift << 2
59+
// HDR luminance small range (CEM 3, spec §C.2.14): with the top bit of v0 set, the
60+
// high-precision layout applies.
5761
ReadOnlySpan<int> values = [0xC0, 0xB5];
5862
(Rgba64 low, Rgba64 high) = HdrEndpointDecoder.DecodeHdrModeUnquantized(values, ColorEndpointMode.HdrLumaSmallRange);
5963

@@ -67,7 +71,8 @@ public void Decode_HdrLumaSmallRange_V0HighBitSet_UsesHighBitBranch()
6771
[Fact]
6872
public void Decode_HdrLumaSmallRange_ClampsY1ToMax()
6973
{
70-
// Values chosen so y1 overflows 0xFFF and clamps.
74+
// CEM 3 (spec §C.2.14): the high luma is clamped to the 12-bit intermediate maximum
75+
// (0xFFF) before the << 4 pack to FP16. Max inputs force that clamp.
7176
ReadOnlySpan<int> values = [0xFF, 0xFF];
7277
(_, Rgba64 high) = HdrEndpointDecoder.DecodeHdrModeUnquantized(values, ColorEndpointMode.HdrLumaSmallRange);
7378

@@ -88,7 +93,8 @@ public void Decode_HdrRgbBaseScale_DoesNotReturnGarbage(int v0, int v1, int v2,
8893
ReadOnlySpan<int> values = [v0, v1, v2, v3];
8994
(Rgba64 low, Rgba64 high) = HdrEndpointDecoder.DecodeHdrModeUnquantized(values, ColorEndpointMode.HdrRgbBaseScale);
9095

91-
// Alpha always HdrOne. Other channels must be clamped to [0, 0xFFF0].
96+
// CEM 7 (spec §C.2.14): alpha is always opaque; RGB channels are the 12-bit intermediate
97+
// maximum (0xFFF) packed to 16-bit (0xFFF0) at the upper bound.
9298
Assert.Equal(HdrOne, low.A);
9399
Assert.Equal(HdrOne, high.A);
94100
Assert.True(low.R <= 0xFFF0);
@@ -125,8 +131,9 @@ public void Decode_HdrRgbBaseScale_Mode0_NonZeroInputs_ProducesPinnedOutput()
125131
[Fact]
126132
public void Decode_HdrRgbDirect_MajorComponent3_UsesPassthroughBranch()
127133
{
128-
// majorComponent = ((v4 & 0x80) >> 7) | (((v5 & 0x80) >> 7) << 1) = 0x80 | 0x80 = 3.
129-
// Produces direct shifts rather than running the bit-placement tree.
134+
// CEM 11 (spec §C.2.14): a major component of 3 selects the passthrough layout — the
135+
// decoder emits direct shifts rather than running the bit-placement tables. The inputs
136+
// set the top bit of both v4 and v5 to reach that case.
130137
ReadOnlySpan<int> values = [0x10, 0x80, 0x20, 0x90, 0xA5, 0xC5];
131138
(Rgba64 low, Rgba64 high) = HdrEndpointDecoder.DecodeHdrModeUnquantized(values, ColorEndpointMode.HdrRgbDirect);
132139

@@ -176,7 +183,8 @@ public void Decode_HdrRgbDirect_EachMode_AlphaIsHdrOne(int modeValue)
176183
[Fact]
177184
public void Decode_HdrRgbDirectLdrAlpha_AlphaIsUnorm16()
178185
{
179-
// RGB decoded via UnpackHdrRgbDirect; alpha is v6,v7 * 257 (UNORM8 → UNORM16).
186+
// CEM 14 (spec §C.2.14): RGB is HDR (decoded as CEM 11) but alpha is LDR — the two 8-bit
187+
// alpha values widen to UNORM16 by the standard bit-replication (× 257).
180188
ReadOnlySpan<int> values = [0x10, 0x80, 0x20, 0x90, 0xA5, 0xC5, 0x40, 0xC0];
181189
(Rgba64 low, Rgba64 high) = HdrEndpointDecoder.DecodeHdrModeUnquantized(values, ColorEndpointMode.HdrRgbDirectLdrAlpha);
182190

@@ -187,11 +195,12 @@ public void Decode_HdrRgbDirectLdrAlpha_AlphaIsUnorm16()
187195
[Fact]
188196
public void Decode_HdrRgbDirectHdrAlpha_AlphaDecodedAsHdr()
189197
{
190-
// Selector derived from high bits of v6,v7. Here selector = 3 (simple passthrough branch).
198+
// CEM 15 (spec §C.2.14): both RGB and alpha are HDR. The alpha sub-mode selector comes
199+
// from the high bits of v6/v7; these inputs select selector 3, the direct passthrough
200+
// branch, so both alpha endpoints decode to the same value.
191201
ReadOnlySpan<int> values = [0x10, 0x80, 0x20, 0x90, 0xA5, 0xC5, 0xC0, 0xC0];
192202
(Rgba64 low, Rgba64 high) = HdrEndpointDecoder.DecodeHdrModeUnquantized(values, ColorEndpointMode.HdrRgbDirectHdrAlpha);
193203

194-
// Selector == 3: a0 = (v6 & 0x7F) << 5, a1 = (v7 & 0x7F) << 5.
195204
Assert.Equal((ushort)(((0xC0 & 0x7F) << 5) << 4), low.A);
196205
Assert.Equal((ushort)(((0xC0 & 0x7F) << 5) << 4), high.A);
197206
}

‎tests/ImageSharp.Textures.Tests/Formats/Ktx/Ktx1AstcTestImageGenerator.cs‎

Lines changed: 0 additions & 130 deletions
This file was deleted.

0 commit comments

Comments
 (0)