Initial commit (WIP)

This commit is contained in:
DarthAffe committed 2024-07-06 00:04:07 +02:00
1 parent 5474273e70
commit 696f3b955f
121 files changed
+5723 -1

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\HPPH\HPPH.csproj" />
</ItemGroup>
</Project>
+14
View File
@@ -0,0 +1,14 @@
namespace HPPH.Reference;
internal static class HPPHExtensions
{
public static byte GetByteValueFromPercentage(this float percentage)
{
if (float.IsNaN(percentage) || (percentage < 0)) return 0;
return (byte)(percentage >= 1.0f ? 255 : percentage * 256.0f);
}
public static float GetPercentageFromByteValue(this byte value)
=> value == 255 ? 1.0f : (value / 256.0f);
}
+43
View File
@@ -0,0 +1,43 @@
namespace HPPH.Reference;
public static partial class ReferencePixelHelper
{
#region Methods
public static IColor Average(IImage image)
{
float count = image.Width * image.Height;
ISum sum = Sum(image);
return new ColorRGBA((sum.R / count).GetByteValueFromPercentage(),
(sum.G / count).GetByteValueFromPercentage(),
(sum.B / count).GetByteValueFromPercentage(),
(sum.A / count).GetByteValueFromPercentage());
}
public static T Average<T>(RefImage<T> image)
where T : struct, IColor
{
float count = image.Width * image.Height;
ISum sum = Sum(image);
return (T)T.Create((sum.R / count).GetByteValueFromPercentage(),
(sum.G / count).GetByteValueFromPercentage(),
(sum.B / count).GetByteValueFromPercentage(),
(sum.A / count).GetByteValueFromPercentage());
}
public static T Average<T>(ReadOnlySpan<T> colors)
where T : struct, IColor
{
float count = colors.Length;
ISum sum = Sum(colors);
return (T)T.Create((sum.R / count).GetByteValueFromPercentage(),
(sum.G / count).GetByteValueFromPercentage(),
(sum.B / count).GetByteValueFromPercentage(),
(sum.A / count).GetByteValueFromPercentage());
}
#endregion
}
+73
View File
@@ -0,0 +1,73 @@
namespace HPPH.Reference;
public static partial class ReferencePixelHelper
{
#region Methods
public static IMinMax MinMax(IImage image)
{
byte minR = byte.MaxValue, minG = byte.MaxValue, minB = byte.MaxValue, minA = byte.MaxValue;
byte maxR = byte.MinValue, maxG = byte.MinValue, maxB = byte.MinValue, maxA = byte.MinValue;
foreach (IColor color in image)
{
minR = Math.Min(minR, color.R);
minG = Math.Min(minG, color.G);
minB = Math.Min(minB, color.B);
minA = Math.Min(minA, color.A);
maxR = Math.Max(maxR, color.R);
maxG = Math.Max(maxG, color.G);
maxB = Math.Max(maxB, color.B);
maxA = Math.Max(maxA, color.A);
}
return new MinMaxRGBA(minR, maxR, minG, maxG, minB, maxB, minA, maxA);
}
public static IMinMax MinMax<T>(RefImage<T> image)
where T : struct, IColor
{
byte minR = byte.MaxValue, minG = byte.MaxValue, minB = byte.MaxValue, minA = byte.MaxValue;
byte maxR = byte.MinValue, maxG = byte.MinValue, maxB = byte.MinValue, maxA = byte.MinValue;
foreach (T color in image)
{
minR = Math.Min(minR, color.R);
minG = Math.Min(minG, color.G);
minB = Math.Min(minB, color.B);
minA = Math.Min(minA, color.A);
maxR = Math.Max(maxR, color.R);
maxG = Math.Max(maxG, color.G);
maxB = Math.Max(maxB, color.B);
maxA = Math.Max(maxA, color.A);
}
return new MinMaxRGBA(minR, maxR, minG, maxG, minB, maxB, minA, maxA);
}
public static IMinMax MinMax<T>(ReadOnlySpan<T> colors)
where T : struct, IColor
{
byte minR = byte.MaxValue, minG = byte.MaxValue, minB = byte.MaxValue, minA = byte.MaxValue;
byte maxR = byte.MinValue, maxG = byte.MinValue, maxB = byte.MinValue, maxA = byte.MinValue;
foreach (T color in colors)
{
minR = Math.Min(minR, color.R);
minG = Math.Min(minG, color.G);
minB = Math.Min(minB, color.B);
minA = Math.Min(minA, color.A);
maxR = Math.Max(maxR, color.R);
maxG = Math.Max(maxG, color.G);
maxB = Math.Max(maxB, color.B);
maxA = Math.Max(maxA, color.A);
}
return new MinMaxRGBA(minR, maxR, minG, maxG, minB, maxB, minA, maxA);
}
#endregion
}
+8
View File
@@ -0,0 +1,8 @@
namespace HPPH.Reference;
public static partial class ReferencePixelHelper
{
#region Methods
#endregion
}
+26
View File
@@ -0,0 +1,26 @@
namespace HPPH.Reference;
public static partial class ReferencePixelHelper
{
#region Methods
// DarthAffe 05.07.2024: LINQ OrderBy uses a stable sorting algorithm -> it's a good reference as the optimized sort is supposed to be stable.
public static void SortByRed<T>(Span<T> colors)
where T : unmanaged, IColor
=> colors.ToArray().OrderBy(x => x.R).ToArray().AsSpan().CopyTo(colors);
public static void SortByGreen<T>(Span<T> colors)
where T : unmanaged, IColor
=> colors.ToArray().OrderBy(x => x.G).ToArray().AsSpan().CopyTo(colors);
public static void SortByBlue<T>(Span<T> colors)
where T : unmanaged, IColor
=> colors.ToArray().OrderBy(x => x.B).ToArray().AsSpan().CopyTo(colors);
public static void SortByAlpha<T>(Span<T> colors)
where T : unmanaged, IColor
=> colors.ToArray().OrderBy(x => x.A).ToArray().AsSpan().CopyTo(colors);
#endregion
}
+55
View File
@@ -0,0 +1,55 @@
namespace HPPH.Reference;
public static partial class ReferencePixelHelper
{
#region Methods
public static ISum Sum(IImage image)
{
long sumR = 0, sumG = 0, sumB = 0, sumA = 0;
foreach (IColor color in image)
{
sumR += color.R;
sumG += color.G;
sumB += color.B;
sumA += color.A;
}
return new SumRGBA(sumR, sumG, sumB, sumA);
}
public static ISum Sum<T>(RefImage<T> image)
where T : struct, IColor
{
long sumR = 0, sumG = 0, sumB = 0, sumA = 0;
foreach (T color in image)
{
sumR += color.R;
sumG += color.G;
sumB += color.B;
sumA += color.A;
}
return new SumRGBA(sumR, sumG, sumB, sumA);
}
public static ISum Sum<T>(ReadOnlySpan<T> colors)
where T : struct, IColor
{
long sumR = 0, sumG = 0, sumB = 0, sumA = 0;
foreach (T color in colors)
{
sumR += color.R;
sumG += color.G;
sumB += color.B;
sumA += color.A;
}
return new SumRGBA(sumR, sumG, sumB, sumA);
}
#endregion
}
+3
View File
@@ -0,0 +1,3 @@
namespace HPPH.Reference;
public static partial class ReferencePixelHelper;