Added multiple image formats support

This commit is contained in:
Samuele Lorefice
2025-04-01 17:48:03 +02:00
parent a77e9b7989
commit 537bcc0305
2 changed files with 104 additions and 6 deletions

View File

@@ -10,6 +10,107 @@ public static class ImageUtil {
using var image16 = image as Image<Rgba64> ?? throw new NotSupportedException($"Image format not supported"); using var image16 = image as Image<Rgba64> ?? throw new NotSupportedException($"Image format not supported");
int width = image.Width; int width = image.Width;
int height = image.Height; int height = image.Height;
//result = new T[image.Width, image.Height];
return image switch {
Image<Rgba64> img => img.ProcessPixelsRgba64<T>(),
Image<Rgb24> img => img.ProcessPixelsRgb24<T>(),
Image<Rgba32> img => img.ProcessPixelsRgba32<T>(),
Image<Rgb48> img => img.ProcessPixelsRgb48<T>(),
_ => throw new NotSupportedException($"Image format not supported")
};
}
static T[,] ProcessPixelsRgba64<T>(this Image<Rgba64> image) where T : struct, IEquatable<T> {
int width = image.Width;
int height = image.Height;
var result = new T[image.Width, image.Height];
image.ProcessPixelRows(accessor => {
//we use Y as the row index and X as the column index
for (int y = 0; y < height; y++) {
var span = accessor.GetRowSpan(y);
for (int x = 0; x < width; x++) {
switch (result) {
case float[,] f:
f[x, y] = span[x].R;
break;
case Vector2[,] f:
f[x, y] = new(span[x].R, span[x].G);
break;
case Vector3[,] f:
f[x, y] = new(span[x].R, span[x].G, span[x].B);
break;
case Vector4[,] f:
f[x, y] = new(span[x].R, span[x].G, span[x].B, 1f);
break;
}
}
}
});
return result;
}
static T[,] ProcessPixelsRgb24<T>(this Image<Rgb24> image) where T : struct, IEquatable<T> {
int width = image.Width;
int height = image.Height;
var result = new T[image.Width, image.Height];
image.ProcessPixelRows(accessor => {
//we use Y as the row index and X as the column index
for (int y = 0; y < height; y++) {
var span = accessor.GetRowSpan(y);
for (int x = 0; x < width; x++) {
switch (result) {
case float[,] f:
f[x, y] = span[x].R;
break;
case Vector2[,] f:
f[x, y] = new(span[x].R, span[x].G);
break;
case Vector3[,] f:
f[x, y] = new(span[x].R, span[x].G, span[x].B);
break;
case Vector4[,] f:
f[x, y] = new(span[x].R, span[x].G, span[x].B, 1f);
break;
}
}
}
});
return result;
}
static T[,] ProcessPixelsRgba32<T>(this Image<Rgba32> image) where T : struct, IEquatable<T> {
int width = image.Width;
int height = image.Height;
var result = new T[image.Width, image.Height];
image.ProcessPixelRows(accessor => {
//we use Y as the row index and X as the column index
for (int y = 0; y < height; y++) {
var span = accessor.GetRowSpan(y);
for (int x = 0; x < width; x++) {
switch (result) {
case float[,] f:
f[x, y] = span[x].R;
break;
case Vector2[,] f:
f[x, y] = new(span[x].R, span[x].G);
break;
case Vector3[,] f:
f[x, y] = new(span[x].R, span[x].G, span[x].B);
break;
case Vector4[,] f:
f[x, y] = new(span[x].R, span[x].G, span[x].B, 1f);
break;
}
}
}
});
return result;
}
static T[,] ProcessPixelsRgb48<T>(this Image<Rgb48> image) where T : struct, IEquatable<T> {
using var image16 = image as Image<Rgb48> ?? throw new NotSupportedException($"Image format not supported");
int width = image.Width;
int height = image.Height;
var result = new T[image.Width, image.Height]; var result = new T[image.Width, image.Height];
image16.ProcessPixelRows(accessor => { image16.ProcessPixelRows(accessor => {
//we use Y as the row index and X as the column index //we use Y as the row index and X as the column index
@@ -21,19 +122,18 @@ public static class ImageUtil {
f[x, y] = span[x].R; f[x, y] = span[x].R;
break; break;
case Vector2[,] f: case Vector2[,] f:
f[x,y] = new (span[x].R, span[x].G); f[x, y] = new(span[x].R, span[x].G);
break; break;
case Vector3[,] f: case Vector3[,] f:
f[x,y] = new (span[x].R, span[x].G, span[x].B); f[x, y] = new(span[x].R, span[x].G, span[x].B);
break; break;
case Vector4[,] f: case Vector4[,] f:
f[x,y] = new (span[x].R, span[x].G, span[x].B, 1f); f[x, y] = new(span[x].R, span[x].G, span[x].B, 1f);
break; break;
} }
} }
} }
}); });
ImageData(image, path);
return result; return result;
} }

View File

@@ -8,9 +8,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Magick.NET-Q16-HDRI-OpenMP-x64" Version="14.5.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.7" /> <PackageReference Include="SixLabors.ImageSharp" Version="3.1.7" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.5" />
<PackageReference Include="System.Numerics.Vectors" Version="4.6.1" /> <PackageReference Include="System.Numerics.Vectors" Version="4.6.1" />
</ItemGroup> </ItemGroup>