Cupola/Program.cs

140 lines
4.2 KiB
C#
Raw Normal View History

2023-02-03 09:03:47 +00:00
using ComputeSharp;
using static Cupola.Program;
2022-11-15 11:50:42 +00:00
namespace Cupola
{
2023-02-03 09:03:47 +00:00
internal partial class Program
2022-11-15 11:50:42 +00:00
{
2023-02-03 09:03:47 +00:00
static void Main(string[] args)
2022-11-15 11:50:42 +00:00
{
Console.WriteLine("Image Dir?");
2023-02-03 09:03:47 +00:00
string? fileLoc = Console.ReadLine();
2022-11-15 11:50:42 +00:00
if (fileLoc == null)
throw new ArgumentException("input should not be NULL");
string[] files = Directory.GetFiles(fileLoc);
2023-02-03 09:03:47 +00:00
ReadWriteTexture2D<Bgra32, float4>[] images = new ReadWriteTexture2D<Bgra32, Float4>[files.Length];
2022-11-15 11:50:42 +00:00
for (int i = 0; i < files.Length; i++)
{
Console.WriteLine(files[i]);
2023-02-03 09:03:47 +00:00
images[i] = GraphicsDevice.GetDefault().LoadReadWriteTexture2D<Bgra32, float4>(files[i]);
2022-11-15 11:50:42 +00:00
}
Console.WriteLine("Output: ");
2023-02-03 09:03:47 +00:00
string? name = Console.ReadLine();
if (name == null)
throw new Exception("UwU");
2022-11-15 11:50:42 +00:00
Console.WriteLine("mode?");
2023-01-13 09:53:06 +00:00
Console.WriteLine();
2023-02-03 09:03:47 +00:00
ConsoleKey key = Console.ReadKey().Key;
2022-11-15 11:50:42 +00:00
2023-02-03 09:03:47 +00:00
if (key == ConsoleKey.P)
{
2023-02-03 09:03:47 +00:00
ReadWriteTexture2D<Bgra32, Float4> brightest = images[0];
ReadWriteTexture2D<Bgra32, Float4> output = images[0];
2023-02-03 09:03:47 +00:00
for (int i = 1; i < images.Length; i++)
{
2023-02-03 09:03:47 +00:00
Console.WriteLine(i.ToString());
ReadWriteTexture2D<Bgra32, Float4> temp = images[i];
GraphicsDevice.GetDefault().For
(
temp.Width,
temp.Height,
new Brightest(brightest, temp)
);
GraphicsDevice.GetDefault().For
(
temp.Width,
temp.Height,
new Average(temp, output)
);
GraphicsDevice.GetDefault().For
(
temp.Width,
temp.Height,
new Average(temp, brightest)
);
output = temp;
}
2023-02-03 09:03:47 +00:00
output.Save(name + ".jpg");
}
2023-02-03 09:03:47 +00:00
else
2023-01-11 12:13:26 +00:00
{
2023-02-03 09:03:47 +00:00
ReadWriteTexture2D<Bgra32, Float4> i1 = images[0];
ReadWriteTexture2D<Bgra32, Float4> i2 = images[1];
2023-01-11 12:13:26 +00:00
2023-02-03 09:03:47 +00:00
GraphicsDevice.GetDefault().For
(
i1.Width,
i1.Height,
new Average(i1, i2)
);
2023-01-13 09:35:09 +00:00
2023-02-03 09:03:47 +00:00
i1.Save(name + ".jpg");
2023-01-11 12:13:26 +00:00
}
2022-11-15 11:50:42 +00:00
}
2023-02-03 09:03:47 +00:00
[AutoConstructor]
public readonly partial struct Brightest : IComputeShader
2023-01-10 09:38:37 +00:00
{
2023-02-03 09:03:47 +00:00
public readonly IReadWriteNormalizedTexture2D<float4> input1;
public readonly IReadWriteNormalizedTexture2D<float4> input2;
2023-01-10 09:38:37 +00:00
2023-02-03 09:03:47 +00:00
// Other captured resources or values here...
2023-01-10 09:38:37 +00:00
2023-02-03 09:03:47 +00:00
public void Execute()
2023-01-10 09:38:37 +00:00
{
2023-02-03 09:03:47 +00:00
float3 i1 = input1[ThreadIds.XY].RGB;
float3 i2 = input2[ThreadIds.XY].RGB;
2023-01-10 09:38:37 +00:00
2023-02-03 09:03:47 +00:00
float i1Intensity = i1.X * i1.Y * i1.Z;
float i2Intensity = i2.X * i2.Y * i2.Z;
2023-01-10 09:38:37 +00:00
2023-02-03 09:03:47 +00:00
if (i1Intensity > i2Intensity)
2023-01-10 09:38:37 +00:00
{
2023-02-03 09:03:47 +00:00
input1[ThreadIds.XY].RGB = i1;
2023-01-10 09:38:37 +00:00
}
2023-02-03 09:03:47 +00:00
else
2023-01-10 09:38:37 +00:00
{
2023-02-03 09:03:47 +00:00
input1[ThreadIds.XY].RGB = i2;
2023-01-10 09:38:37 +00:00
}
}
2023-02-03 09:03:47 +00:00
}
2023-01-10 09:38:37 +00:00
2023-02-03 09:03:47 +00:00
[AutoConstructor]
public readonly partial struct Average : IComputeShader
{
public readonly IReadWriteNormalizedTexture2D<float4> input1;
public readonly IReadWriteNormalizedTexture2D<float4> input2;
2023-01-13 09:34:43 +00:00
2023-02-03 09:03:47 +00:00
// Other captured resources or values here...
2023-01-11 11:40:46 +00:00
2023-02-03 09:03:47 +00:00
public void Execute()
2023-01-11 11:40:46 +00:00
{
2023-02-03 09:03:47 +00:00
float3 i1 = input1[ThreadIds.XY].RGB;
float3 i2 = input2[ThreadIds.XY].RGB;
2023-01-11 11:40:46 +00:00
2023-02-03 09:03:47 +00:00
float3 o1 = 0;
2023-01-11 11:40:46 +00:00
2023-02-03 09:03:47 +00:00
o1.X = (i1.X + i2.X) / 2;
o1.Y = (i1.Y + i2.Y) / 2;
o1.Z = (i1.Z + i2.Z) / 2;
2023-01-13 09:34:43 +00:00
2023-02-03 09:03:47 +00:00
input1[ThreadIds.XY].RGB = o1;
2023-01-11 11:40:46 +00:00
}
2023-01-10 09:38:37 +00:00
}
2022-11-15 11:50:42 +00:00
}
}