Improved performance of Average on Images

This commit is contained in:
DarthAffe committed 2024-08-19 22:41:56 +02:00
1 parent f4c1eaddd6
commit e6b77ecfdb
12 files changed
+84 -98

No files matched your search

@@ -19,7 +19,6 @@ public class ColorSourceGenerator : IIncrementalGenerator
private static readonly IGeneratorFeature[] FEATURES =
[
new Colors(),
new Average(),
new MinMax(),
new Sum(),
new Quantize()
-51
View File
@@ -1,51 +0,0 @@
using System.Collections.Generic;
using System.Collections.Immutable;
namespace HPPH.Generators;
internal class Average : IGeneratorFeature
{
public IEnumerable<(string name, string source)> GenerateFor(ColorFormatData colorFormat)
{
yield return ($"ColorFormat{colorFormat.Format}.Average", GenerateColorFormatAverage(colorFormat));
}
public IEnumerable<(string name, string source)> GenerateFor(ImmutableArray<ColorFormatData> colorFormats)
{
yield return ("IColorFormat.Average", GenerateColorFormatInterfaceAverage());
}
private static string GenerateColorFormatAverage(ColorFormatData colorFormat)
{
return $$"""
#nullable enable
using System.Runtime.InteropServices;
namespace HPPH;
public sealed partial class ColorFormat{{colorFormat.Format}}
{
#region Methods
unsafe IColor IColorFormat.Average(ReadOnlySpan<byte> data) => PixelHelper.Average(MemoryMarshal.Cast<byte, Color{{colorFormat.Format}}>(data));
#endregion
}
""";
}
private static string GenerateColorFormatInterfaceAverage()
{
return """
#nullable enable
namespace HPPH;
public partial interface IColorFormat
{
internal IColor Average(ReadOnlySpan<byte> data);
}
""";
}
}
+8
View File
@@ -218,6 +218,12 @@ internal class Colors : IGeneratorFeature
private ColorFormat{{colorFormat.Format}}() {}
#endregion
#region Methods
public IColor CreateColor(byte r, byte g, byte b, byte a) => Color{{colorFormat.Format}}.Create(r, g, b, a);
#endregion
}
""";
}
@@ -243,6 +249,8 @@ internal class Colors : IGeneratorFeature
sb.AppendLine();
sb.AppendLine("""
IColor CreateColor(byte r, byte g, byte b, byte a);
#endregion
}
""");