diff --git a/SDFMapCreator/ImageUtil.cs b/SDFMapCreator/ImageUtil.cs index 7588e19..c9c218d 100644 --- a/SDFMapCreator/ImageUtil.cs +++ b/SDFMapCreator/ImageUtil.cs @@ -10,6 +10,107 @@ public static class ImageUtil { using var image16 = image as Image ?? throw new NotSupportedException($"Image format not supported"); int width = image.Width; int height = image.Height; + //result = new T[image.Width, image.Height]; + return image switch { + Image img => img.ProcessPixelsRgba64(), + Image img => img.ProcessPixelsRgb24(), + Image img => img.ProcessPixelsRgba32(), + Image img => img.ProcessPixelsRgb48(), + _ => throw new NotSupportedException($"Image format not supported") + }; + } + + static T[,] ProcessPixelsRgba64(this Image image) where T : struct, IEquatable { + 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(this Image image) where T : struct, IEquatable { + 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(this Image image) where T : struct, IEquatable { + 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(this Image image) where T : struct, IEquatable { + using var image16 = image as Image ?? throw new NotSupportedException($"Image format not supported"); + int width = image.Width; + int height = image.Height; var result = new T[image.Width, image.Height]; image16.ProcessPixelRows(accessor => { //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; break; case Vector2[,] f: - f[x,y] = new (span[x].R, span[x].G); + 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); + 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); + f[x, y] = new(span[x].R, span[x].G, span[x].B, 1f); break; } } } }); - ImageData(image, path); return result; } diff --git a/SDFMapCreator/SDFMapCreator.csproj b/SDFMapCreator/SDFMapCreator.csproj index 38048a6..e0fb3ee 100644 --- a/SDFMapCreator/SDFMapCreator.csproj +++ b/SDFMapCreator/SDFMapCreator.csproj @@ -8,9 +8,7 @@ - -