mirror of
https://github.com/AssetRipper/AssetRipper.TextureDecoder.git
synced 2026-11-13 12:41:27 -10:00
Lossless conversion tests
This commit is contained in:
1 parent
b992959c24
commit
ce581c0ea6
34 files changed
+2041
-10
No files matched your search
@@ -1,18 +1,125 @@
|
||||
using AssetRipper.TextureDecoder.Rgb;
|
||||
using AssetRipper.TextureDecoder.Attributes;
|
||||
using AssetRipper.TextureDecoder.Rgb;
|
||||
using System.Reflection;
|
||||
|
||||
namespace AssetRipper.TextureDecoder.TestGenerator
|
||||
namespace AssetRipper.TextureDecoder.TestGenerator
|
||||
{
|
||||
internal struct GenerationData
|
||||
internal readonly struct GenerationData
|
||||
{
|
||||
public Type ColorType;
|
||||
public Type ChannelType;
|
||||
public int ColorSize;
|
||||
public Type ColorType { get; }
|
||||
public Type ChannelType { get; }
|
||||
public int ColorSize { get; }
|
||||
|
||||
public bool RedChannel { get; }
|
||||
public bool GreenChannel { get; }
|
||||
public bool BlueChannel { get; }
|
||||
public bool AlphaChannel { get; }
|
||||
public bool FullyUtilizedChannels { get; }
|
||||
|
||||
public bool IsFloatingPoint
|
||||
{
|
||||
get
|
||||
{
|
||||
return ChannelType == typeof(Half) || ChannelType == typeof(float) || ChannelType == typeof(double);
|
||||
}
|
||||
}
|
||||
|
||||
public int BitSizePerChannel
|
||||
{
|
||||
get
|
||||
{
|
||||
if (ChannelType == typeof(byte) || ChannelType == typeof(sbyte))
|
||||
{
|
||||
return 8;
|
||||
}
|
||||
else if (ChannelType == typeof(ushort) || ChannelType == typeof(short) || ChannelType == typeof(Half))
|
||||
{
|
||||
return 16;
|
||||
}
|
||||
else if (ChannelType == typeof(float))
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
else if (ChannelType == typeof(double))
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException(ChannelType.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string ChannelTypeName
|
||||
{
|
||||
get
|
||||
{
|
||||
return ChannelType.Name switch
|
||||
{
|
||||
"Byte" => "byte",
|
||||
"SByte" => "sbyte",
|
||||
"UInt16" => "ushort",
|
||||
"Int16" => "short",
|
||||
"Single" => "float",
|
||||
"Double" => "double",
|
||||
_ => ChannelType.Name,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public GenerationData(Type colorType, int colorSize)
|
||||
{
|
||||
ColorType = colorType;
|
||||
ChannelType = ColorType.GetInterfaces().Single(t => t.Name == $"{nameof(IColor<byte>)}`1").GenericTypeArguments[0];
|
||||
ColorSize = colorSize;
|
||||
|
||||
RgbaAttribute attribute = colorType.GetCustomAttribute<RgbaAttribute>() ?? throw new NullReferenceException(colorType.Name);
|
||||
RedChannel = attribute.RedChannel;
|
||||
GreenChannel = attribute.GreenChannel;
|
||||
BlueChannel = attribute.BlueChannel;
|
||||
AlphaChannel = attribute.AlphaChannel;
|
||||
FullyUtilizedChannels = attribute.FullyUtilizedChannels;
|
||||
}
|
||||
|
||||
public bool Contains(GenerationData other)
|
||||
{
|
||||
if (!FullyUtilizedChannels)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!IsFloatingPoint && other.IsFloatingPoint)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsFloatingPoint && !other.IsFloatingPoint)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (other.RedChannel && !RedChannel)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (other.GreenChannel && !GreenChannel)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (other.BlueChannel && !BlueChannel)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (other.AlphaChannel && !AlphaChannel)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return BitSizePerChannel >= other.BitSizePerChannel;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using AssetRipper.TextureDecoder.Rgb.Formats;
|
||||
using AssetRipper.TextureDecoder.Rgb;
|
||||
using AssetRipper.TextureDecoder.Rgb.Formats;
|
||||
using System.CodeDom.Compiler;
|
||||
|
||||
namespace AssetRipper.TextureDecoder.TestGenerator
|
||||
@@ -59,7 +60,7 @@ namespace AssetRipper.TextureDecoder.TestGenerator
|
||||
static void Main()
|
||||
{
|
||||
Directory.CreateDirectory(OutputFolder);
|
||||
|
||||
|
||||
foreach (GenerationData data in dataList)
|
||||
{
|
||||
using MemoryStream memoryStream = new();
|
||||
@@ -83,6 +84,15 @@ namespace AssetRipper.TextureDecoder.TestGenerator
|
||||
textWriter.WriteLine();
|
||||
textWriter.WriteMakeRandomColor(data);
|
||||
|
||||
foreach (GenerationData otherData in dataList)
|
||||
{
|
||||
if (otherData.Contains(data))
|
||||
{
|
||||
textWriter.WriteLine();
|
||||
textWriter.WriteLosslessConversionTest(data, otherData);
|
||||
}
|
||||
}
|
||||
|
||||
textWriter.Indent -= 1;
|
||||
textWriter.WriteClassDefinitionClose();
|
||||
|
||||
@@ -99,6 +109,7 @@ namespace AssetRipper.TextureDecoder.TestGenerator
|
||||
|
||||
private static void WriteNamespaceUsings(this IndentedTextWriter textWriter, GenerationData data)
|
||||
{
|
||||
textWriter.WriteLine($"using {typeof(ColorExtensions).Namespace};");
|
||||
textWriter.WriteLine($"using {data.ColorType.Namespace};");
|
||||
textWriter.WriteLine($"using {typeof(System.Runtime.CompilerServices.Unsafe).Namespace};");
|
||||
textWriter.WriteLine();//An empty line after the usings
|
||||
@@ -274,5 +285,24 @@ namespace AssetRipper.TextureDecoder.TestGenerator
|
||||
textWriter.Indent -= 1;
|
||||
textWriter.WriteLine("}");
|
||||
}
|
||||
|
||||
private static void WriteLosslessConversionTest(this IndentedTextWriter textWriter, GenerationData currentData, GenerationData containingData)
|
||||
{
|
||||
string currentName = currentData.ColorType.Name;
|
||||
string containingName = containingData.ColorType.Name;
|
||||
|
||||
textWriter.WriteLine("[Test]");
|
||||
textWriter.WriteLine($"public void ConversionTo{containingName}IsLossless()");
|
||||
textWriter.WriteLine("{");
|
||||
textWriter.Indent += 1;
|
||||
|
||||
textWriter.WriteLine($"{currentName} original = MakeRandomColor();");
|
||||
textWriter.WriteLine($"{containingName} converted = original.{nameof(ColorExtensions.Convert)}<{currentName}, {currentData.ChannelTypeName}, {containingName}, {containingData.ChannelTypeName}>();");
|
||||
textWriter.WriteLine($"{currentName} convertedBack = converted.{nameof(ColorExtensions.Convert)}<{containingName}, {containingData.ChannelTypeName}, {currentName}, {currentData.ChannelTypeName}>();");
|
||||
textWriter.WriteLine("Assert.That(convertedBack, Is.EqualTo(original));");
|
||||
|
||||
textWriter.Indent -= 1;
|
||||
textWriter.WriteLine("}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user