Skip to content

Commit c919852

Browse files
committed
Generic ColorARGB and ColorBGRA
1 parent 9bca952 commit c919852

File tree

29 files changed

+659
-888
lines changed

29 files changed

+659
-888
lines changed

AssetRipper.TextureDecoder.Benchmarks/Argb32Decoding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ public Argb32Decoding()
2121
public int OriginalMethod() => RgbConverter.ARGB32ToBGRA32(inputData, Width, Height, outputData);
2222

2323
[Benchmark]
24-
public int GenericMethod() => RgbConverter.Convert<ColorARGB32, byte, ColorBGRA32, byte>(inputData, Width, Height, outputData);
24+
public int GenericMethod() => RgbConverter.Convert<ColorARGB<byte>, byte, ColorBGRA<byte>, byte>(inputData, Width, Height, outputData);
2525
}

AssetRipper.TextureDecoder.Benchmarks/Rgb9e5Decoding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ public Rgb9e5Decoding()
2121
public int OriginalMethod() => RgbConverter.RGB9e5FloatToBGRA32(inputData, Width, Height, outputData);
2222

2323
[Benchmark]
24-
public int GenericMethod() => RgbConverter.Convert<ColorRGB9e5, double, ColorBGRA32, byte>(inputData, Width, Height, outputData);
24+
public int GenericMethod() => RgbConverter.Convert<ColorRGB9e5, double, ColorBGRA<byte>, byte>(inputData, Width, Height, outputData);
2525
}

AssetRipper.TextureDecoder.ColorGenerator/Program.cs

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using AssetRipper.Text.SourceGeneration;
22
using AssetRipper.TextureDecoder.SourceGeneration.Common;
33
using System.CodeDom.Compiler;
4+
using System.Diagnostics;
45
using System.Text;
56

67
namespace AssetRipper.TextureDecoder.ColorGenerator;
@@ -18,32 +19,30 @@ internal static partial class Program
1819
private static readonly List<(string, Type, bool, bool, bool, bool, bool)> CustomColors = new()
1920
{
2021
( "ColorARGB16", typeof(byte), true, true, true, true, false ),
21-
( "ColorARGB32", typeof(byte), true, true, true, true, true ),
22-
( "ColorBGRA32", typeof(byte), true, true, true, true, true ),
2322
( "ColorRGB16", typeof(byte), true, true, true, false, false ),
2423
( "ColorRGB9e5", typeof(double), true, true, true, false, false ),
2524
( "ColorRGBA16", typeof(byte), true, true, true, true, false ),
2625
};
2726

28-
/// <summary>
29-
/// Name, Red, Blue, Green, Alpha
30-
/// </summary>
31-
private static readonly List<(string, bool, bool, bool, bool)> GenericColors = new()
32-
{
33-
( "ColorR", true, false, false, false ),
34-
( "ColorRG", true, true, false, false ),
35-
( "ColorRGB", true, true, true, false ),
36-
( "ColorRGBA", true, true, true, true ),
37-
( "ColorA", false, false, false, true ),
38-
};
27+
private static readonly List<string> GenericColors =
28+
[
29+
"ColorR",
30+
"ColorRG",
31+
"ColorRGB",
32+
"ColorRGBA",
33+
"ColorA",
34+
"ColorARGB",
35+
"ColorBGRA",
36+
"ColorBGR",
37+
];
3938

4039
static void Main()
4140
{
4241
foreach (var customColor in CustomColors)
4342
{
4443
WriteCustomColor(customColor);
4544
}
46-
foreach (var genericColor in GenericColors)
45+
foreach (string genericColor in GenericColors)
4746
{
4847
WriteGenericColor(genericColor);
4948
}
@@ -63,12 +62,11 @@ private static void WriteCustomColor((string, Type, bool, bool, bool, bool, bool
6362
WriteCustomColor(writer, name, type, hasRed, hasGreen, hasBlue, hasAlpha, fullyUtilized);
6463
}
6564

66-
private static void WriteGenericColor((string, bool, bool, bool, bool) details)
65+
private static void WriteGenericColor(string name)
6766
{
68-
(string name, bool hasRed, bool hasGreen, bool hasBlue, bool hasAlpha) = details;
6967
Console.WriteLine(name);
7068
using IndentedTextWriter writer = IndentedTextWriterFactory.Create(FormatsFolder, name);
71-
WriteGenericColor(writer, name, hasRed, hasGreen, hasBlue, hasAlpha);
69+
WriteGenericColor(writer, name);
7270
}
7371

7472
private static void WriteSuperGenericColor(int channelCount)
@@ -214,7 +212,7 @@ static int CountRGBA(bool hasRed, bool hasGreen, bool hasBlue, bool hasAlpha)
214212
}
215213
}
216214

217-
private static void WriteGenericColor(IndentedTextWriter writer, string name, bool hasRed, bool hasGreen, bool hasBlue, bool hasAlpha)
215+
private static void WriteGenericColor(IndentedTextWriter writer, string name)
218216
{
219217
const string typeName = "T";
220218
const string minValue = "NumericConversion.GetMinimumValueSafe<T>()";
@@ -229,14 +227,39 @@ private static void WriteGenericColor(IndentedTextWriter writer, string name, bo
229227
writer.WriteLine($"public partial struct {name}<T> : IColor<{name}<T>, T> where T : unmanaged, INumberBase<T>, IMinMaxValue<T>");
230228
using (new CurlyBrackets(writer))
231229
{
232-
WriteProperty(writer, hasRed, typeName, 'R', minValue);
233-
writer.WriteLineNoTabs();
234-
WriteProperty(writer, hasGreen, typeName, 'G', minValue);
235-
writer.WriteLineNoTabs();
236-
WriteProperty(writer, hasBlue, typeName, 'B', minValue);
237-
writer.WriteLineNoTabs();
238-
WriteProperty(writer, hasAlpha, typeName, 'A', maxValue);
239-
writer.WriteLineNoTabs();
230+
Debug.Assert(name.StartsWith("Color", StringComparison.Ordinal));
231+
ReadOnlySpan<char> channels = name.AsSpan()[5..];
232+
foreach (char channel in channels)
233+
{
234+
WriteProperty(writer, true, typeName, channel, minValue);
235+
writer.WriteLineNoTabs();
236+
}
237+
238+
bool hasRed = channels.Contains('R');
239+
bool hasGreen = channels.Contains('G');
240+
bool hasBlue = channels.Contains('B');
241+
bool hasAlpha = channels.Contains('A');
242+
243+
if (!hasRed)
244+
{
245+
WriteProperty(writer, hasRed, typeName, 'R', minValue);
246+
writer.WriteLineNoTabs();
247+
}
248+
if (!hasGreen)
249+
{
250+
WriteProperty(writer, hasGreen, typeName, 'G', minValue);
251+
writer.WriteLineNoTabs();
252+
}
253+
if (!hasBlue)
254+
{
255+
WriteProperty(writer, hasBlue, typeName, 'B', minValue);
256+
writer.WriteLineNoTabs();
257+
}
258+
if (!hasAlpha)
259+
{
260+
WriteProperty(writer, hasAlpha, typeName, 'A', maxValue);
261+
writer.WriteLineNoTabs();
262+
}
240263

241264
WriteConstructor(writer, name, hasRed, hasGreen, hasBlue, hasAlpha, typeName);
242265
writer.WriteLineNoTabs();

AssetRipper.TextureDecoder.ConsoleApp/Program.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static void Main(string[] args)
2727
string args6 = args.GetArgument(6);
2828
byte[] data = File.ReadAllBytes(path);
2929

30-
DirectBitmap<ColorBGRA32, byte> bitmap = new DirectBitmap<ColorBGRA32, byte>(width, height);
30+
DirectBitmap<ColorBGRA<byte>, byte> bitmap = new DirectBitmap<ColorBGRA<byte>, byte>(width, height);
3131
switch (inputType)
3232
{
3333
case "astc":
@@ -52,7 +52,7 @@ static void Main(string[] args)
5252
DecodeRgb(data, width, height, args5, bitmap.Bits);
5353
break;
5454
case "yuy2":
55-
Yuy2Decoder.DecompressYUY2<ColorBGRA32, byte>(data, width, height, bitmap.Bits);
55+
Yuy2Decoder.DecompressYUY2<ColorBGRA<byte>, byte>(data, width, height, bitmap.Bits);
5656
break;
5757
default:
5858
throw new NotSupportedException(inputType);
@@ -105,7 +105,7 @@ private static void DecodeAstc(ReadOnlySpan<byte> input, int width, int height,
105105
Console.WriteLine("Arg at index 6 : blockYSize");
106106
int blockXSize = int.Parse(blockXSizeString);
107107
int blockYSize = int.Parse(blockYSizeString);
108-
AstcDecoder.DecodeASTC<ColorBGRA32, byte>(input, width, height, blockXSize, blockYSize, output);
108+
AstcDecoder.DecodeASTC<ColorBGRA<byte>, byte>(input, width, height, blockXSize, blockYSize, output);
109109
}
110110

111111
private static void DecodeAtc(ReadOnlySpan<byte> input, int width, int height, string modeString, Span<byte> output)
@@ -117,10 +117,10 @@ private static void DecodeAtc(ReadOnlySpan<byte> input, int width, int height, s
117117
switch (mode)
118118
{
119119
case 0:
120-
AtcDecoder.DecompressAtcRgb4<ColorBGRA32, byte>(input, width, height, output);
120+
AtcDecoder.DecompressAtcRgb4<ColorBGRA<byte>, byte>(input, width, height, output);
121121
break;
122122
case 1:
123-
AtcDecoder.DecompressAtcRgba8<ColorBGRA32, byte>(input, width, height, output);
123+
AtcDecoder.DecompressAtcRgba8<ColorBGRA<byte>, byte>(input, width, height, output);
124124
break;
125125

126126
default:
@@ -143,25 +143,25 @@ private static void DecodeBc(ReadOnlySpan<byte> input, int width, int height, st
143143
switch (mode)
144144
{
145145
case 1:
146-
Bc1.Decompress<ColorBGRA32, byte>(input, width, height, output);
146+
Bc1.Decompress<ColorBGRA<byte>, byte>(input, width, height, output);
147147
break;
148148
case 2:
149-
Bc2.Decompress<ColorBGRA32, byte>(input, width, height, output);
149+
Bc2.Decompress<ColorBGRA<byte>, byte>(input, width, height, output);
150150
break;
151151
case 3:
152-
Bc3.Decompress<ColorBGRA32, byte>(input, width, height, output);
152+
Bc3.Decompress<ColorBGRA<byte>, byte>(input, width, height, output);
153153
break;
154154
case 4:
155-
Bc4.Decompress<ColorBGRA32, byte>(input, width, height, output);
155+
Bc4.Decompress<ColorBGRA<byte>, byte>(input, width, height, output);
156156
break;
157157
case 5:
158-
Bc5.Decompress<ColorBGRA32, byte>(input, width, height, output);
158+
Bc5.Decompress<ColorBGRA<byte>, byte>(input, width, height, output);
159159
break;
160160
case 6:
161-
Bc6h.Decompress<ColorBGRA32, byte>(input, width, height, bool.Parse(isSignedString), output);
161+
Bc6h.Decompress<ColorBGRA<byte>, byte>(input, width, height, bool.Parse(isSignedString), output);
162162
break;
163163
case 7:
164-
Bc7.Decompress<ColorBGRA32, byte>(input, width, height, output);
164+
Bc7.Decompress<ColorBGRA<byte>, byte>(input, width, height, output);
165165
break;
166166

167167
default:
@@ -179,13 +179,13 @@ private static void DecodeDxt(ReadOnlySpan<byte> input, int width, int height, s
179179
switch (mode)
180180
{
181181
case 0:
182-
DxtDecoder.DecompressDXT1<ColorBGRA32, byte>(input, width, height, output);
182+
DxtDecoder.DecompressDXT1<ColorBGRA<byte>, byte>(input, width, height, output);
183183
break;
184184
case 1:
185-
DxtDecoder.DecompressDXT3<ColorBGRA32, byte>(input, width, height, output);
185+
DxtDecoder.DecompressDXT3<ColorBGRA<byte>, byte>(input, width, height, output);
186186
break;
187187
case 2:
188-
DxtDecoder.DecompressDXT5<ColorBGRA32, byte>(input, width, height, output);
188+
DxtDecoder.DecompressDXT5<ColorBGRA<byte>, byte>(input, width, height, output);
189189
break;
190190

191191
default:
@@ -208,28 +208,28 @@ private static void DecodeEtc(ReadOnlySpan<byte> input, int width, int height, s
208208
switch (mode)
209209
{
210210
case 0:
211-
EtcDecoder.DecompressETC<ColorBGRA32, byte>(input, width, height, output);
211+
EtcDecoder.DecompressETC<ColorBGRA<byte>, byte>(input, width, height, output);
212212
break;
213213
case 1:
214-
EtcDecoder.DecompressETC2<ColorBGRA32, byte>(input, width, height, output);
214+
EtcDecoder.DecompressETC2<ColorBGRA<byte>, byte>(input, width, height, output);
215215
break;
216216
case 2:
217-
EtcDecoder.DecompressETC2A1<ColorBGRA32, byte>(input, width, height, output);
217+
EtcDecoder.DecompressETC2A1<ColorBGRA<byte>, byte>(input, width, height, output);
218218
break;
219219
case 3:
220-
EtcDecoder.DecompressETC2A8<ColorBGRA32, byte>(input, width, height, output);
220+
EtcDecoder.DecompressETC2A8<ColorBGRA<byte>, byte>(input, width, height, output);
221221
break;
222222
case 4:
223-
EtcDecoder.DecompressEACRUnsigned<ColorBGRA32, byte>(input, width, height, output);
223+
EtcDecoder.DecompressEACRUnsigned<ColorBGRA<byte>, byte>(input, width, height, output);
224224
break;
225225
case 5:
226-
EtcDecoder.DecompressEACRSigned<ColorBGRA32, byte>(input, width, height, output);
226+
EtcDecoder.DecompressEACRSigned<ColorBGRA<byte>, byte>(input, width, height, output);
227227
break;
228228
case 6:
229-
EtcDecoder.DecompressEACRGUnsigned<ColorBGRA32, byte>(input, width, height, output);
229+
EtcDecoder.DecompressEACRGUnsigned<ColorBGRA<byte>, byte>(input, width, height, output);
230230
break;
231231
case 7:
232-
EtcDecoder.DecompressEACRGSigned<ColorBGRA32, byte>(input, width, height, output);
232+
EtcDecoder.DecompressEACRGSigned<ColorBGRA<byte>, byte>(input, width, height, output);
233233
break;
234234

235235
default:
@@ -241,6 +241,6 @@ private static void DecodePvrtc(ReadOnlySpan<byte> input, int width, int height,
241241
{
242242
Console.WriteLine("Arg at index 5 : 2bitMode");
243243
bool do2bit = bool.Parse(do2bitModeString);
244-
PvrtcDecoder.DecompressPVRTC<ColorBGRA32, byte>(input, width, height, do2bit, output);
244+
PvrtcDecoder.DecompressPVRTC<ColorBGRA<byte>, byte>(input, width, height, do2bit, output);
245245
}
246246
}

0 commit comments

Comments
 (0)