WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 106a26d

Browse files
committed
Move to using RgbaFloat internally
1 parent 22d5360 commit 106a26d

File tree

80 files changed

+2126
-2931
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+2126
-2931
lines changed

BCnEnc.Net/BCnEncoder.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55

66
<PublishRepositoryUrl>true</PublishRepositoryUrl>
77
<IncludeSymbols>true</IncludeSymbols>

BCnEnc.Net/Decoder/BcBlockDecoder.cs

Lines changed: 59 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,69 +9,26 @@
99

1010
namespace BCnEncoder.Decoder
1111
{
12-
internal interface IBcBlockDecoder<TRawBlock> where TRawBlock : unmanaged
12+
internal interface IBcBlockDecoder : IBcDecoder
1313
{
14-
TRawBlock[] Decode(ReadOnlyMemory<byte> data, OperationContext context);
15-
TRawBlock DecodeBlock(ReadOnlySpan<byte> data);
14+
RawBlock4X4RgbaFloat[] Decode(ReadOnlyMemory<byte> data, OperationContext context);
15+
RawBlock4X4RgbaFloat DecodeBlock(ReadOnlySpan<byte> data);
1616
}
1717

18-
internal interface IBcLdrBlockDecoder : IBcBlockDecoder<RawBlock4X4Rgba32>, IBcLdrDecoder {}
19-
internal interface IBcHdrBlockDecoder : IBcBlockDecoder<RawBlock4X4RgbFloat>, IBcHdrDecoder {}
20-
21-
internal abstract class BaseLdrBlockDecoder<TEncodedBlock> : BaseBcBlockDecoder<TEncodedBlock, RawBlock4X4Rgba32>, IBcLdrBlockDecoder
18+
internal abstract class BaseBcBlockDecoder<TEncodedBlock> : IBcBlockDecoder
2219
where TEncodedBlock : unmanaged
2320
{
24-
25-
/// <inheritdoc />
26-
public byte[] Decode(ReadOnlyMemory<byte> data, int width, int height, OperationContext context)
27-
{
28-
var blocks = Decode(data, context);
29-
return ImageToBlocks.ColorsFromRawBlocks(blocks, width, height).CopyAsBytes();
30-
}
31-
32-
/// <inheritdoc />
33-
public ColorRgba32[] DecodeColor(ReadOnlyMemory<byte> data, int width, int height, OperationContext context)
34-
{
35-
var blocks = Decode(data, context);
36-
return ImageToBlocks.ColorsFromRawBlocks(blocks, width, height);
37-
}
38-
}
39-
40-
internal abstract class BaseHdrBlockDecoder<TEncodedBlock> : BaseBcBlockDecoder<TEncodedBlock, RawBlock4X4RgbFloat>, IBcHdrBlockDecoder
41-
where TEncodedBlock : unmanaged
42-
{
43-
44-
/// <inheritdoc />
45-
public byte[] Decode(ReadOnlyMemory<byte> data, int width, int height, OperationContext context)
46-
{
47-
var blocks = Decode(data, context);
48-
return ImageToBlocks.ColorsFromRawBlocks(blocks, width, height).ConvertTo<ColorRgbFloat, ColorRgbaFloat>().CopyAsBytes();
49-
}
50-
51-
/// <inheritdoc />
52-
public ColorRgbaFloat[] DecodeColor(ReadOnlyMemory<byte> data, int width, int height, OperationContext context)
53-
{
54-
var blocks = Decode(data, context);
55-
return ImageToBlocks.ColorsFromRawBlocks(blocks, width, height).ConvertTo<ColorRgbFloat, ColorRgbaFloat>();
56-
}
57-
}
58-
59-
internal abstract class BaseBcBlockDecoder<TEncodedBlock, TRawBlock> : IBcBlockDecoder<TRawBlock>
60-
where TEncodedBlock : unmanaged
61-
where TRawBlock : unmanaged
62-
{
6321
private static readonly object lockObj = new object();
6422

65-
public TRawBlock[] Decode(ReadOnlyMemory<byte> data, OperationContext context)
23+
public RawBlock4X4RgbaFloat[] Decode(ReadOnlyMemory<byte> data, OperationContext context)
6624
{
67-
6825
if (data.Length % Unsafe.SizeOf<TEncodedBlock>() != 0)
6926
{
7027
throw new InvalidDataException("Given data does not align with the block length.");
7128
}
7229

7330
var blockCount = data.Length / Unsafe.SizeOf<TEncodedBlock>();
74-
var output = new TRawBlock[blockCount];
31+
var output = new RawBlock4X4RgbaFloat[blockCount];
7532

7633
if (context.IsParallel)
7734
{
@@ -84,6 +41,7 @@ public TRawBlock[] Decode(ReadOnlyMemory<byte> data, OperationContext context)
8441
{
8542
var encodedBlocks = MemoryMarshal.Cast<byte, TEncodedBlock>(data.Span);
8643
output[i] = DecodeBlock(encodedBlocks[i]);
44+
output[i].ColorConvert(context.ColorConversionMode);
8745

8846
if (context.Progress != null)
8947
{
@@ -102,56 +60,78 @@ public TRawBlock[] Decode(ReadOnlyMemory<byte> data, OperationContext context)
10260
context.CancellationToken.ThrowIfCancellationRequested();
10361

10462
output[i] = DecodeBlock(encodedBlocks[i]);
63+
output[i].ColorConvert(context.ColorConversionMode);
10564

10665
context.Progress?.Report(1);
10766
}
10867
}
10968

69+
70+
11071
return output;
11172
}
11273

113-
public TRawBlock DecodeBlock(ReadOnlySpan<byte> data)
74+
public RawBlock4X4RgbaFloat DecodeBlock(ReadOnlySpan<byte> data)
11475
{
11576
var encodedBlock = MemoryMarshal.Cast<byte, TEncodedBlock>(data)[0];
11677
return DecodeBlock(encodedBlock);
11778
}
11879

119-
protected abstract TRawBlock DecodeBlock(TEncodedBlock block);
80+
protected abstract RawBlock4X4RgbaFloat DecodeBlock(TEncodedBlock block);
81+
82+
public byte[] Decode(ReadOnlyMemory<byte> data, int width, int height, OperationContext context)
83+
{
84+
byte[] output = new byte[width * height * Unsafe.SizeOf<ColorRgbaFloat>()];
85+
Span<ColorRgbaFloat> colors = MemoryMarshal.Cast<byte, ColorRgbaFloat>(output);
86+
87+
ImageToBlocks.ColorsFromRawBlocks(Decode(data, context), colors, width, height);
88+
89+
return output;
90+
}
91+
92+
public ColorRgbaFloat[] DecodeColor(ReadOnlyMemory<byte> data, int width, int height, OperationContext context)
93+
{
94+
ColorRgbaFloat[] output = new ColorRgbaFloat[width * height];
95+
96+
ImageToBlocks.ColorsFromRawBlocks(Decode(data, context), output, width, height);
97+
98+
return output;
99+
}
120100
}
121101

122-
internal class Bc1NoAlphaDecoder : BaseLdrBlockDecoder<Bc1Block>
102+
internal class Bc1NoAlphaDecoder : BaseBcBlockDecoder<Bc1Block>
123103
{
124-
protected override RawBlock4X4Rgba32 DecodeBlock(Bc1Block block)
104+
protected override RawBlock4X4RgbaFloat DecodeBlock(Bc1Block block)
125105
{
126106
return block.Decode(false);
127107
}
128108
}
129109

130-
internal class Bc1ADecoder : BaseLdrBlockDecoder<Bc1Block>
110+
internal class Bc1ADecoder : BaseBcBlockDecoder<Bc1Block>
131111
{
132-
protected override RawBlock4X4Rgba32 DecodeBlock(Bc1Block block)
112+
protected override RawBlock4X4RgbaFloat DecodeBlock(Bc1Block block)
133113
{
134114
return block.Decode(true);
135115
}
136116
}
137117

138-
internal class Bc2Decoder : BaseLdrBlockDecoder<Bc2Block>
118+
internal class Bc2Decoder : BaseBcBlockDecoder<Bc2Block>
139119
{
140-
protected override RawBlock4X4Rgba32 DecodeBlock(Bc2Block block)
120+
protected override RawBlock4X4RgbaFloat DecodeBlock(Bc2Block block)
141121
{
142122
return block.Decode();
143123
}
144124
}
145125

146-
internal class Bc3Decoder : BaseLdrBlockDecoder<Bc3Block>
126+
internal class Bc3Decoder : BaseBcBlockDecoder<Bc3Block>
147127
{
148-
protected override RawBlock4X4Rgba32 DecodeBlock(Bc3Block block)
128+
protected override RawBlock4X4RgbaFloat DecodeBlock(Bc3Block block)
149129
{
150130
return block.Decode();
151131
}
152132
}
153133

154-
internal class Bc4Decoder : BaseLdrBlockDecoder<Bc4Block>
134+
internal class Bc4Decoder : BaseBcBlockDecoder<Bc4Block>
155135
{
156136
private readonly ColorComponent component;
157137

@@ -160,72 +140,74 @@ public Bc4Decoder(ColorComponent component)
160140
this.component = component;
161141
}
162142

163-
protected override RawBlock4X4Rgba32 DecodeBlock(Bc4Block block)
143+
protected override RawBlock4X4RgbaFloat DecodeBlock(Bc4Block block)
164144
{
165145
return block.Decode(component);
166146
}
167147
}
168148

169-
internal class Bc5Decoder : BaseLdrBlockDecoder<Bc5Block>
149+
internal class Bc5Decoder : BaseBcBlockDecoder<Bc5Block>
170150
{
171151
private readonly ColorComponent component1;
172152
private readonly ColorComponent component2;
153+
private readonly ColorComponent componentCalculated;
173154

174-
public Bc5Decoder(ColorComponent component1, ColorComponent component2)
155+
public Bc5Decoder(ColorComponent component1, ColorComponent component2, ColorComponent componentCalculated)
175156
{
176157
this.component1 = component1;
177158
this.component2 = component2;
159+
this.componentCalculated = componentCalculated;
178160
}
179161

180-
protected override RawBlock4X4Rgba32 DecodeBlock(Bc5Block block)
162+
protected override RawBlock4X4RgbaFloat DecodeBlock(Bc5Block block)
181163
{
182-
return block.Decode(component1, component2);
164+
return block.Decode(component1, component2, componentCalculated);
183165
}
184166
}
185167

186-
internal class Bc6UDecoder : BaseHdrBlockDecoder<Bc6Block>
168+
internal class Bc6UDecoder : BaseBcBlockDecoder<Bc6Block>
187169
{
188-
protected override RawBlock4X4RgbFloat DecodeBlock(Bc6Block block)
170+
protected override RawBlock4X4RgbaFloat DecodeBlock(Bc6Block block)
189171
{
190172
return block.Decode(false);
191173
}
192174
}
193175

194-
internal class Bc6SDecoder : BaseHdrBlockDecoder<Bc6Block>
176+
internal class Bc6SDecoder : BaseBcBlockDecoder<Bc6Block>
195177
{
196-
protected override RawBlock4X4RgbFloat DecodeBlock(Bc6Block block)
178+
protected override RawBlock4X4RgbaFloat DecodeBlock(Bc6Block block)
197179
{
198180
return block.Decode(true);
199181
}
200182
}
201183

202-
internal class Bc7Decoder : BaseLdrBlockDecoder<Bc7Block>
184+
internal class Bc7Decoder : BaseBcBlockDecoder<Bc7Block>
203185
{
204-
protected override RawBlock4X4Rgba32 DecodeBlock(Bc7Block block)
186+
protected override RawBlock4X4RgbaFloat DecodeBlock(Bc7Block block)
205187
{
206188
return block.Decode();
207189
}
208190
}
209191

210-
internal class AtcDecoder : BaseLdrBlockDecoder<AtcBlock>
192+
internal class AtcDecoder : BaseBcBlockDecoder<AtcBlock>
211193
{
212-
protected override RawBlock4X4Rgba32 DecodeBlock(AtcBlock block)
194+
protected override RawBlock4X4RgbaFloat DecodeBlock(AtcBlock block)
213195
{
214196
return block.Decode();
215197
}
216198
}
217199

218-
internal class AtcExplicitAlphaDecoder : BaseLdrBlockDecoder<AtcExplicitAlphaBlock>
200+
internal class AtcExplicitAlphaDecoder : BaseBcBlockDecoder<AtcExplicitAlphaBlock>
219201
{
220-
protected override RawBlock4X4Rgba32 DecodeBlock(AtcExplicitAlphaBlock block)
202+
protected override RawBlock4X4RgbaFloat DecodeBlock(AtcExplicitAlphaBlock block)
221203
{
222204
return block.Decode();
223205
}
224206
}
225207

226-
internal class AtcInterpolatedAlphaDecoder : BaseLdrBlockDecoder<AtcInterpolatedAlphaBlock>
208+
internal class AtcInterpolatedAlphaDecoder : BaseBcBlockDecoder<AtcInterpolatedAlphaBlock>
227209
{
228-
protected override RawBlock4X4Rgba32 DecodeBlock(AtcInterpolatedAlphaBlock block)
210+
protected override RawBlock4X4RgbaFloat DecodeBlock(AtcInterpolatedAlphaBlock block)
229211
{
230212
return block.Decode();
231213
}

0 commit comments

Comments
 (0)