diff --git a/src/Tests/Aardvark.Base.Tests/Images/PixImageTests.cs b/src/Tests/Aardvark.Base.Tests/Images/PixImageTests.cs index 1624182b..c26dce65 100644 --- a/src/Tests/Aardvark.Base.Tests/Images/PixImageTests.cs +++ b/src/Tests/Aardvark.Base.Tests/Images/PixImageTests.cs @@ -1,6 +1,8 @@ using NUnit.Framework; using System; +using System.Collections.Generic; using Aardvark.Base; +using float16 = Aardvark.Base.Half; namespace Aardvark.Tests.Images { @@ -91,29 +93,41 @@ public void MipMapCreate57x11() #region Format Conversions - private static byte[] GetArray(PixImage pi, V2l coord) + internal static readonly Dictionary> s_randomValues = new () { - var arr = new byte[pi.ChannelCount]; + { typeof(byte), rnd => (byte)rnd.UniformInt(256) }, + { typeof(ushort), rnd => (ushort)rnd.UniformInt(Col.Info.MaxValue + 1) }, + { typeof(uint), rnd => rnd.UniformUInt() }, + { typeof(float16), rnd => (float16)rnd.UniformFloat() }, + { typeof(float), rnd => rnd.UniformFloat() }, + { typeof(double), rnd => rnd.UniformDouble() }, + }; + + private static T[] GetArray(PixImage pi, V2l coord) + { + var arr = new T[pi.ChannelCount]; arr.SetByIndex(i => pi.Volume[new V3l(coord, i)]); return arr; } - private static T GetColor(PixImage pi, V2l coord) - => pi.GetMatrix()[coord]; + private static Tc GetColor(PixImage pi, V2l coord) + => pi.GetMatrix()[coord]; - private static void FormatConversion(Col.Format sourceFormat, Col.Format targetFormat, - Func, V2l, T1> getInput, - Func, V2l, T2> getActual, - Func expectedConversion, - bool subImageWindow) + private static void FormatConversion(Col.Format sourceFormat, Col.Format targetFormat, + Func, V2l, Tc1> getInput, + Func, V2l, Tc2> getActual, + Func expectedConversion, + bool subImageWindow) { - var src = new PixImage(sourceFormat, 43, 81); - src.Volume.Data.SetByIndex((_) => (byte)rnd.UniformInt(256)); + + var getRandom = s_randomValues[typeof(T1)]; + var src = new PixImage(sourceFormat, 43, 81); + src.Volume.Data.SetByIndex((_) => (T1)getRandom(rnd)); if (subImageWindow) - src = new PixImage(sourceFormat, src.Volume.SubImageWindow(2, 3, 33, 67)); + src = new PixImage(sourceFormat, src.Volume.SubImageWindow(2, 3, 33, 67)); - var dst = src.ToFormat(targetFormat); + var dst = new PixImage(targetFormat, src); src.GetChannel(0L).ForeachCoord((coord) => { @@ -123,47 +137,47 @@ private static void FormatConversion(Col.Format sourceFormat, Col.Format }); } - private static void FormatConversionArrays(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) - => FormatConversion(sourceFormat, targetFormat, GetArray, GetArray, expectedConversion, subImageWindow); + private static void FormatConversionArrays(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) + => FormatConversion(sourceFormat, targetFormat, GetArray, GetArray, expectedConversion, subImageWindow); - private static void FormatConversionFromArray(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) - => FormatConversion(sourceFormat, targetFormat, GetArray, GetColor, expectedConversion, subImageWindow); + private static void FormatConversionFromArray(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) + => FormatConversion(sourceFormat, targetFormat, GetArray, GetColor, expectedConversion, subImageWindow); - private static void FormatConversionToArray(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) - => FormatConversion(sourceFormat, targetFormat, GetColor, GetArray, expectedConversion, subImageWindow); + private static void FormatConversionToArray(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) + => FormatConversion(sourceFormat, targetFormat, GetColor, GetArray, expectedConversion, subImageWindow); - private static void FormatConversion(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) - => FormatConversion(sourceFormat, targetFormat, GetColor, GetColor, expectedConversion, subImageWindow); + private static void FormatConversion(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) + => FormatConversion(sourceFormat, targetFormat, GetColor, GetColor, expectedConversion, subImageWindow); #region From Gray [Test] public void FormatConversionGrayToGrayAlpha() - => FormatConversionToArray(Col.Format.Gray, Col.Format.GrayAlpha, gray => new byte[] { gray, 255 }); + => FormatConversionToArray(Col.Format.Gray, Col.Format.GrayAlpha, gray => new byte[] { gray, 255 }); [Test] public void FormatConversionGrayToAlpha() - => FormatConversion(Col.Format.Gray, Col.Format.Alpha, gray => 255); + => FormatConversion(Col.Format.Gray, Col.Format.Alpha, gray => 255); [Test] public void FormatConversionGrayToRGB() - => FormatConversion(Col.Format.Gray, Col.Format.RGB, gray => new C3b(gray)); + => FormatConversion(Col.Format.Gray, Col.Format.RGB, gray => new C3b(gray)); [Test] public void FormatConversionGrayToBGR() - => FormatConversion(Col.Format.Gray, Col.Format.BGR, gray => new C3b(gray)); + => FormatConversion(Col.Format.Gray, Col.Format.BGR, gray => new C3b(gray)); [Test] public void FormatConversionGrayToRGBA() - => FormatConversion(Col.Format.Gray, Col.Format.RGBA, gray => new C4b(gray)); + => FormatConversion(Col.Format.Gray, Col.Format.RGBA, gray => new C4b(gray)); [Test] public void FormatConversionGrayToBGRA() - => FormatConversion(Col.Format.Gray, Col.Format.BGRA, gray => new C4b(gray)); + => FormatConversion(Col.Format.Gray, Col.Format.BGRA, gray => new C4b(gray)); [Test] public void FormatConversionGrayToRG() - => FormatConversionToArray(Col.Format.Gray, Col.Format.RG, gray => new byte[] { gray, gray }); + => FormatConversionToArray(Col.Format.Gray, Col.Format.RG, gray => new byte[] { gray, gray }); #endregion @@ -171,31 +185,31 @@ public void FormatConversionGrayToRG() [Test] public void FormatConversionGrayAlphaToGray() - => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.Gray, gray => gray[0]); + => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.Gray, gray => gray[0]); [Test] public void FormatConversionGrayAlphaToAlpha() - => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.Alpha, gray => gray[1]); + => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.Alpha, gray => gray[1]); [Test] public void FormatConversionGrayAlphaToRGB() - => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.RGB, gray => new C3b(gray[0])); + => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.RGB, gray => new C3b(gray[0])); [Test] public void FormatConversionGrayAlphaToBGR() - => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.BGR, gray => new C3b(gray[0])); + => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.BGR, gray => new C3b(gray[0])); [Test] public void FormatConversionGrayAlphaToRGBA() - => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.RGBA, gray => new C4b(gray[0], gray[0], gray[0], gray[1])); + => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.RGBA, gray => new C4b(gray[0], gray[0], gray[0], gray[1])); [Test] public void FormatConversionGrayAlphaToBGRA() - => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.BGRA, gray => new C4b(gray[0], gray[0], gray[0], gray[1])); + => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.BGRA, gray => new C4b(gray[0], gray[0], gray[0], gray[1])); [Test] public void FormatConversionGrayAlphaToRG() - => FormatConversionArrays(Col.Format.GrayAlpha, Col.Format.RG, gray => new byte[] { gray[0], gray[0] }); + => FormatConversionArrays(Col.Format.GrayAlpha, Col.Format.RG, gray => new byte[] { gray[0], gray[0] }); #endregion @@ -203,39 +217,204 @@ public void FormatConversionGrayAlphaToRG() [Test] public void FormatConversionRGBAToRGBA() - => FormatConversion(Col.Format.RGBA, Col.Format.RGBA, (color => color)); + => FormatConversion(Col.Format.RGBA, Col.Format.RGBA, (c => c)); [Test] public void FormatConversionRGBAToRGBAWindow() - => FormatConversion(Col.Format.RGBA, Col.Format.RGBA, (color => color), true); + => FormatConversion(Col.Format.RGBA, Col.Format.RGBA, (c => c), true); [Test] public void FormatConversionRGBAToBGRA() - => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, color => color); + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c); [Test] public void FormatConversionRGBAToBGRAWindow() - => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, (color => color), true); + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, (c => c), true); [Test] public void FormatConversionRGBAToRGB() - => FormatConversion(Col.Format.RGBA, Col.Format.RGB, color => color.RGB); + => FormatConversion(Col.Format.RGBA, Col.Format.RGB, c => c.RGB); [Test] public void FormatConversionRGBAToBGR() - => FormatConversion(Col.Format.RGBA, Col.Format.BGR, color => color.RGB); + => FormatConversion(Col.Format.RGBA, Col.Format.BGR, c => c.RGB); [Test] public void FormatConversionRGBAToGray() - => FormatConversion(Col.Format.RGBA, Col.Format.Gray, color => color.ToGrayByte()); + => FormatConversion(Col.Format.RGBA, Col.Format.Gray, c => c.ToGrayByte()); [Test] public void FormatConversionRGBAToGrayAlpha() - => FormatConversionToArray(Col.Format.RGBA, Col.Format.GrayAlpha, color => new byte[] { color.ToGrayByte(), color.A }); + => FormatConversionToArray(Col.Format.RGBA, Col.Format.GrayAlpha, c => new byte[] { c.ToGrayByte(), c.A }); [Test] public void FormatConversionRGBAToRG() - => FormatConversionToArray(Col.Format.RGBA, Col.Format.RG, color => new byte[] { color.R, color.G }); + => FormatConversionToArray(Col.Format.RGBA, Col.Format.RG, c => new byte[] { c.R, c.G }); + + #region Byte to BGRA (other types) + + [Test] + public void TypeConversionByteToUShort() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4us()); + + [Test] + public void TypeConversionByteToUInt() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4ui()); + + [Test] + public void TypeConversionByteToHalf() + => FormatConversionToArray(Col.Format.RGBA, Col.Format.BGRA, + c => new float16[] { c.B.ByteToHalf(), c.G.ByteToHalf(), c.R.ByteToHalf(), c.A.ByteToHalf() } + ); + + [Test] + public void TypeConversionByteToFloat() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4f()); + + [Test] + public void TypeConversionByteToDouble() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4d()); + + #endregion + + #region UShort to BGRA (other types) + + [Test] + public void TypeConversionUShortToByte() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4b()); + + [Test] + public void TypeConversionUShortToUInt() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4ui()); + + [Test] + public void TypeConversionUShortToHalf() + => FormatConversionToArray(Col.Format.RGBA, Col.Format.BGRA, + c => new float16[] { c.B.UShortToHalf(), c.G.UShortToHalf(), c.R.UShortToHalf(), c.A.UShortToHalf() } + ); + + [Test] + public void TypeConversionUShortToFloat() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4f()); + + [Test] + public void TypeConversionUShortToDouble() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4d()); + + #endregion + + #region UInt to BGRA (other types) + + [Test] + public void TypeConversionUIntToByte() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4b()); + + [Test] + public void TypeConversionUIntToUShort() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4us()); + + [Test] + public void TypeConversionUIntToHalf() + => FormatConversionToArray(Col.Format.RGBA, Col.Format.BGRA, + c => new float16[] { c.B.UIntToHalf(), c.G.UIntToHalf(), c.R.UIntToHalf(), c.A.UIntToHalf() } + ); + + [Test] + public void TypeConversionUIntToFloat() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4f()); + + [Test] + public void TypeConversionUIntToDouble() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4d()); + + #endregion + + #region Half to BGRA (other types) + + [Test] + public void TypeConversionHalfToByte() + => FormatConversionFromArray(Col.Format.RGBA, Col.Format.BGRA, c => + new C4b(c[0].HalfToByte(), c[1].HalfToByte(), c[2].HalfToByte(), c[3].HalfToByte()) + ); + + [Test] + public void TypeConversionHalfToUShort() + => FormatConversionFromArray(Col.Format.RGBA, Col.Format.BGRA, c => + new C4us(c[0].HalfToUShort(), c[1].HalfToUShort(), c[2].HalfToUShort(), c[3].HalfToUShort()) + ); + + [Test] + public void TypeConversionHalfToUInt() + => FormatConversionFromArray(Col.Format.RGBA, Col.Format.BGRA, c => + new C4ui(c[0].HalfToUInt(), c[1].HalfToUInt(), c[2].HalfToUInt(), c[3].HalfToUInt()) + ); + + [Test] + public void TypeConversionHalfToFloat() + => FormatConversionFromArray(Col.Format.RGBA, Col.Format.BGRA, c => + new C4f(c[0], c[1], c[2], c[3]) + ); + + [Test] + public void TypeConversionHalfToDouble() + => FormatConversionFromArray(Col.Format.RGBA, Col.Format.BGRA, c => + new C4d(c[0], c[1], c[2], c[3]) + ); + + #endregion + + #region Float to BGRA (other types) + + [Test] + public void TypeConversionFloatToByte() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4b()); + + [Test] + public void TypeConversionFloatToUShort() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4us()); + + [Test] + public void TypeConversionFloatToUInt() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4ui()); + + [Test] + public void TypeConversionFloatToHalf() + => FormatConversionToArray(Col.Format.RGBA, Col.Format.BGRA, + c => new float16[] { c.B.FloatToHalf(), c.G.FloatToHalf(), c.R.FloatToHalf(), c.A.FloatToHalf() } + ); + + + [Test] + public void TypeConversionFloatToDouble() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4d()); + + #endregion + + #region Double to BGRA (other types) + + [Test] + public void TypeConversionDoubleToByte() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4b()); + + [Test] + public void TypeConversionDoubleToUShort() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4us()); + + [Test] + public void TypeConversionDoubleToUInt() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4ui()); + + [Test] + public void TypeConversionDoubleToHalf() + => FormatConversionToArray(Col.Format.RGBA, Col.Format.BGRA, + c => new float16[] { c.B.DoubleToHalf(), c.G.DoubleToHalf(), c.R.DoubleToHalf(), c.A.DoubleToHalf() } + ); + + [Test] + public void TypeConversionDoubleToFloat() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4d()); + + #endregion #endregion @@ -243,27 +422,27 @@ public void FormatConversionRGBAToRG() [Test] public void FormatConversionBGRAToRGBA() - => FormatConversion(Col.Format.BGRA, Col.Format.RGBA, color => color); + => FormatConversion(Col.Format.BGRA, Col.Format.RGBA, c => c); [Test] public void FormatConversionBGRAToRGB() - => FormatConversion(Col.Format.BGRA, Col.Format.RGB, color => color.RGB); + => FormatConversion(Col.Format.BGRA, Col.Format.RGB, c => c.RGB); [Test] public void FormatConversionBGRAToBGR() - => FormatConversion(Col.Format.BGRA, Col.Format.BGR, color => color.RGB); + => FormatConversion(Col.Format.BGRA, Col.Format.BGR, c => c.RGB); [Test] public void FormatConversionBGRAToGray() - => FormatConversion(Col.Format.BGRA, Col.Format.Gray, color => color.ToGrayByte()); + => FormatConversion(Col.Format.BGRA, Col.Format.Gray, c => c.ToGrayByte()); [Test] public void FormatConversionBGRAToGrayAlpha() - => FormatConversionToArray(Col.Format.BGRA, Col.Format.GrayAlpha, color => new byte[] { color.ToGrayByte(), color.A }); + => FormatConversionToArray(Col.Format.BGRA, Col.Format.GrayAlpha, c => new byte[] { c.ToGrayByte(), c.A }); [Test] public void FormatConversionBGRAToRG() - => FormatConversionToArray(Col.Format.BGRA, Col.Format.RG, color => new byte[] { color.R, color.G }); + => FormatConversionToArray(Col.Format.BGRA, Col.Format.RG, c => new byte[] { c.R, c.G }); #endregion @@ -271,27 +450,27 @@ public void FormatConversionBGRAToRG() [Test] public void FormatConversionRGBToBGRA() - => FormatConversion(Col.Format.RGB, Col.Format.BGRA, color => color.ToC4b()); + => FormatConversion(Col.Format.RGB, Col.Format.BGRA, c => c.ToC4b()); [Test] public void FormatConversionRGBToRGBA() - => FormatConversion(Col.Format.RGB, Col.Format.RGBA, color => color.ToC4b()); + => FormatConversion(Col.Format.RGB, Col.Format.RGBA, c => c.ToC4b()); [Test] public void FormatConversionRGBToBGR() - => FormatConversion(Col.Format.RGB, Col.Format.BGR, color => color); + => FormatConversion(Col.Format.RGB, Col.Format.BGR, c => c); [Test] public void FormatConversionRGBToGray() - => FormatConversion(Col.Format.RGB, Col.Format.Gray, color => color.ToGrayByte()); + => FormatConversion(Col.Format.RGB, Col.Format.Gray, c => c.ToGrayByte()); [Test] public void FormatConversionRGBToGrayAlpha() - => FormatConversionToArray(Col.Format.RGB, Col.Format.GrayAlpha, color => new byte[] { color.ToGrayByte(), 255 }); + => FormatConversionToArray(Col.Format.RGB, Col.Format.GrayAlpha, c => new byte[] { c.ToGrayByte(), 255 }); [Test] public void FormatConversionRGBToRG() - => FormatConversionToArray(Col.Format.RGB, Col.Format.RG, color => new byte[] { color.R, color.G }); + => FormatConversionToArray(Col.Format.RGB, Col.Format.RG, c => new byte[] { c.R, c.G }); #endregion @@ -299,27 +478,27 @@ public void FormatConversionRGBToRG() [Test] public void FormatConversionBGRToBGRA() - => FormatConversion(Col.Format.BGR, Col.Format.BGRA, color => color.ToC4b()); + => FormatConversion(Col.Format.BGR, Col.Format.BGRA, c => c.ToC4b()); [Test] public void FormatConversionBGRToRGBA() - => FormatConversion(Col.Format.BGR, Col.Format.RGBA, color => color.ToC4b()); + => FormatConversion(Col.Format.BGR, Col.Format.RGBA, c => c.ToC4b()); [Test] public void FormatConversionBGRToRGB() - => FormatConversion(Col.Format.BGR, Col.Format.RGB, color => color); + => FormatConversion(Col.Format.BGR, Col.Format.RGB, c => c); [Test] public void FormatConversionBGRToGray() - => FormatConversion(Col.Format.BGR, Col.Format.Gray, color => color.ToGrayByte()); + => FormatConversion(Col.Format.BGR, Col.Format.Gray, c => c.ToGrayByte()); [Test] public void FormatConversionBGRToGrayAlpha() - => FormatConversionToArray(Col.Format.BGR, Col.Format.GrayAlpha, color => new byte[] { color.ToGrayByte(), 255 }); + => FormatConversionToArray(Col.Format.BGR, Col.Format.GrayAlpha, c => new byte[] { c.ToGrayByte(), 255 }); [Test] public void FormatConversionBGRToRG() - => FormatConversionToArray(Col.Format.BGR, Col.Format.RG, color => new byte[] { color.R, color.G }); + => FormatConversionToArray(Col.Format.BGR, Col.Format.RG, c => new byte[] { c.R, c.G }); #endregion @@ -327,19 +506,19 @@ public void FormatConversionBGRToRG() [Test] public void FormatConversionRGToBGRA() - => FormatConversionFromArray(Col.Format.RG, Col.Format.BGRA, color => new C4b(color[0], color[1], (byte)0, (byte)255)); + => FormatConversionFromArray(Col.Format.RG, Col.Format.BGRA, c => new C4b(c[0], c[1], (byte)0, (byte)255)); [Test] public void FormatConversionRGToRGBA() - => FormatConversionFromArray(Col.Format.RG, Col.Format.RGBA, color => new C4b(color[0], color[1], (byte)0, (byte)255)); + => FormatConversionFromArray(Col.Format.RG, Col.Format.RGBA, c => new C4b(c[0], c[1], (byte)0, (byte)255)); [Test] public void FormatConversionRGToRGB() - => FormatConversionFromArray(Col.Format.RG, Col.Format.RGB, color => new C3b(color[0], color[1], (byte)0)); + => FormatConversionFromArray(Col.Format.RG, Col.Format.RGB, c => new C3b(c[0], c[1], (byte)0)); [Test] public void FormatConversionRGToBGR() - => FormatConversionFromArray(Col.Format.RG, Col.Format.BGR, color => new C3b(color[0], color[1], (byte)0)); + => FormatConversionFromArray(Col.Format.RG, Col.Format.BGR, c => new C3b(c[0], c[1], (byte)0)); #endregion diff --git a/src/Tests/Aardvark.Base.Tests/Images/PixVolumeTests.cs b/src/Tests/Aardvark.Base.Tests/Images/PixVolumeTests.cs index 3bfcc260..cb4f924c 100644 --- a/src/Tests/Aardvark.Base.Tests/Images/PixVolumeTests.cs +++ b/src/Tests/Aardvark.Base.Tests/Images/PixVolumeTests.cs @@ -1,6 +1,7 @@ using NUnit.Framework; using System; using Aardvark.Base; +using float16 = Aardvark.Base.Half; namespace Aardvark.Tests.Images { @@ -11,29 +12,30 @@ class PixVolumeTests #region Format Conversions - private static byte[] GetArray(PixVolume pi, V3l coord) + private static T[] GetArray(PixVolume pi, V3l coord) { - var arr = new byte[pi.ChannelCount]; + var arr = new T[pi.ChannelCount]; arr.SetByIndex(i => pi.Tensor4[new V4l(coord, i)]); return arr; } - private static T GetColor(PixVolume pi, V3l coord) - => pi.GetVolume()[coord]; + private static Tc GetColor(PixVolume pi, V3l coord) + => pi.GetVolume()[coord]; - private static void FormatConversion(Col.Format sourceFormat, Col.Format targetFormat, - Func, V3l, T1> getInput, - Func, V3l, T2> getActual, - Func expectedConversion, - bool subImageWindow) + private static void FormatConversion(Col.Format sourceFormat, Col.Format targetFormat, + Func, V3l, Tc1> getInput, + Func, V3l, Tc2> getActual, + Func expectedConversion, + bool subImageWindow) { - var src = new PixVolume(sourceFormat, 43, 31, 23); - src.Tensor4.Data.SetByIndex((_) => (byte)rnd.UniformInt(256)); + var getRandom = PixImageTests.s_randomValues[typeof(T1)]; + var src = new PixVolume(sourceFormat, 43, 31, 23); + src.Tensor4.Data.SetByIndex((_) => (T1)getRandom(rnd)); if (subImageWindow) - src = new PixVolume(sourceFormat, src.Tensor4.SubImageWindow(11, 7, 3, 27, 13, 8)); + src = new PixVolume(sourceFormat, src.Tensor4.SubImageWindow(11, 7, 3, 27, 13, 8)); - var dst = src.ToFormat(targetFormat); + var dst = new PixVolume(targetFormat, src); src.GetChannel(0L).ForeachCoord((coord) => { @@ -43,47 +45,47 @@ private static void FormatConversion(Col.Format sourceFormat, Col.Format }); } - private static void FormatConversionArrays(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) - => FormatConversion(sourceFormat, targetFormat, GetArray, GetArray, expectedConversion, subImageWindow); + private static void FormatConversionArrays(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) + => FormatConversion(sourceFormat, targetFormat, GetArray, GetArray, expectedConversion, subImageWindow); - private static void FormatConversionFromArray(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) - => FormatConversion(sourceFormat, targetFormat, GetArray, GetColor, expectedConversion, subImageWindow); + private static void FormatConversionFromArray(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) + => FormatConversion(sourceFormat, targetFormat, GetArray, GetColor, expectedConversion, subImageWindow); - private static void FormatConversionToArray(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) - => FormatConversion(sourceFormat, targetFormat, GetColor, GetArray, expectedConversion, subImageWindow); + private static void FormatConversionToArray(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) + => FormatConversion(sourceFormat, targetFormat, GetColor, GetArray, expectedConversion, subImageWindow); - private static void FormatConversion(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) - => FormatConversion(sourceFormat, targetFormat, GetColor, GetColor, expectedConversion, subImageWindow); + private static void FormatConversion(Col.Format sourceFormat, Col.Format targetFormat, Func expectedConversion, bool subImageWindow = false) + => FormatConversion(sourceFormat, targetFormat, GetColor, GetColor, expectedConversion, subImageWindow); #region From Gray [Test] public void FormatConversionGrayToGrayAlpha() - => FormatConversionToArray(Col.Format.Gray, Col.Format.GrayAlpha, gray => new byte[] { gray, 255 }); + => FormatConversionToArray(Col.Format.Gray, Col.Format.GrayAlpha, gray => new byte[] { gray, 255 }); [Test] public void FormatConversionGrayToAlpha() - => FormatConversion(Col.Format.Gray, Col.Format.Alpha, gray => 255); + => FormatConversion(Col.Format.Gray, Col.Format.Alpha, gray => 255); [Test] public void FormatConversionGrayToRGB() - => FormatConversion(Col.Format.Gray, Col.Format.RGB, gray => new C3b(gray)); + => FormatConversion(Col.Format.Gray, Col.Format.RGB, gray => new C3b(gray)); [Test] public void FormatConversionGrayToBGR() - => FormatConversion(Col.Format.Gray, Col.Format.BGR, gray => new C3b(gray)); + => FormatConversion(Col.Format.Gray, Col.Format.BGR, gray => new C3b(gray)); [Test] public void FormatConversionGrayToRGBA() - => FormatConversion(Col.Format.Gray, Col.Format.RGBA, gray => new C4b(gray)); + => FormatConversion(Col.Format.Gray, Col.Format.RGBA, gray => new C4b(gray)); [Test] public void FormatConversionGrayToBGRA() - => FormatConversion(Col.Format.Gray, Col.Format.BGRA, gray => new C4b(gray)); + => FormatConversion(Col.Format.Gray, Col.Format.BGRA, gray => new C4b(gray)); [Test] public void FormatConversionGrayToRG() - => FormatConversionToArray(Col.Format.Gray, Col.Format.RG, gray => new byte[] { gray, gray }); + => FormatConversionToArray(Col.Format.Gray, Col.Format.RG, gray => new byte[] { gray, gray }); #endregion @@ -91,31 +93,31 @@ public void FormatConversionGrayToRG() [Test] public void FormatConversionGrayAlphaToGray() - => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.Gray, gray => gray[0]); + => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.Gray, gray => gray[0]); [Test] public void FormatConversionGrayAlphaToAlpha() - => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.Alpha, gray => gray[1]); + => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.Alpha, gray => gray[1]); [Test] public void FormatConversionGrayAlphaToRGB() - => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.RGB, gray => new C3b(gray[0])); + => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.RGB, gray => new C3b(gray[0])); [Test] public void FormatConversionGrayAlphaToBGR() - => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.BGR, gray => new C3b(gray[0])); + => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.BGR, gray => new C3b(gray[0])); [Test] public void FormatConversionGrayAlphaToRGBA() - => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.RGBA, gray => new C4b(gray[0], gray[0], gray[0], gray[1])); + => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.RGBA, gray => new C4b(gray[0], gray[0], gray[0], gray[1])); [Test] public void FormatConversionGrayAlphaToBGRA() - => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.BGRA, gray => new C4b(gray[0], gray[0], gray[0], gray[1])); + => FormatConversionFromArray(Col.Format.GrayAlpha, Col.Format.BGRA, gray => new C4b(gray[0], gray[0], gray[0], gray[1])); [Test] public void FormatConversionGrayAlphaToRG() - => FormatConversionArrays(Col.Format.GrayAlpha, Col.Format.RG, gray => new byte[] { gray[0], gray[0] }); + => FormatConversionArrays(Col.Format.GrayAlpha, Col.Format.RG, gray => new byte[] { gray[0], gray[0] }); #endregion @@ -123,39 +125,39 @@ public void FormatConversionGrayAlphaToRG() [Test] public void FormatConversionRGBAToRGBA() - => FormatConversion(Col.Format.RGBA, Col.Format.RGBA, color => color); + => FormatConversion(Col.Format.RGBA, Col.Format.RGBA, c => c); [Test] public void FormatConversionRGBAToRGBAWindow() - => FormatConversion(Col.Format.RGBA, Col.Format.RGBA, color => color, true); + => FormatConversion(Col.Format.RGBA, Col.Format.RGBA, c => c, true); [Test] public void FormatConversionRGBAToBGRA() - => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, color => color); + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c); [Test] public void FormatConversionRGBAToBGRAWindow() - => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, color => color, true); + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c, true); [Test] public void FormatConversionRGBAToRGB() - => FormatConversion(Col.Format.RGBA, Col.Format.RGB, color => color.RGB); + => FormatConversion(Col.Format.RGBA, Col.Format.RGB, c => c.RGB); [Test] public void FormatConversionRGBAToBGR() - => FormatConversion(Col.Format.RGBA, Col.Format.BGR, color => color.RGB); + => FormatConversion(Col.Format.RGBA, Col.Format.BGR, c => c.RGB); [Test] public void FormatConversionRGBAToGray() - => FormatConversion(Col.Format.RGBA, Col.Format.Gray, color => color.ToGrayByte()); + => FormatConversion(Col.Format.RGBA, Col.Format.Gray, c => c.ToGrayByte()); [Test] public void FormatConversionRGBAToGrayAlpha() - => FormatConversionToArray(Col.Format.RGBA, Col.Format.GrayAlpha, color => new byte[] { color.ToGrayByte(), color.A }); + => FormatConversionToArray(Col.Format.RGBA, Col.Format.GrayAlpha, c => new byte[] { c.ToGrayByte(), c.A }); [Test] public void FormatConversionRGBAToRG() - => FormatConversionToArray(Col.Format.RGBA, Col.Format.RG, color => new byte[] { color.R, color.G }); + => FormatConversionToArray(Col.Format.RGBA, Col.Format.RG, c => new byte[] { c.R, c.G }); #endregion @@ -163,27 +165,192 @@ public void FormatConversionRGBAToRG() [Test] public void FormatConversionBGRAToRGBA() - => FormatConversion(Col.Format.BGRA, Col.Format.RGBA, color => color); + => FormatConversion(Col.Format.BGRA, Col.Format.RGBA, c => c); [Test] public void FormatConversionBGRAToRGB() - => FormatConversion(Col.Format.BGRA, Col.Format.RGB, color => color.RGB); + => FormatConversion(Col.Format.BGRA, Col.Format.RGB, c => c.RGB); [Test] public void FormatConversionBGRAToBGR() - => FormatConversion(Col.Format.BGRA, Col.Format.BGR, color => color.RGB); + => FormatConversion(Col.Format.BGRA, Col.Format.BGR, c => c.RGB); [Test] public void FormatConversionBGRAToGray() - => FormatConversion(Col.Format.BGRA, Col.Format.Gray, color => color.ToGrayByte()); + => FormatConversion(Col.Format.BGRA, Col.Format.Gray, c => c.ToGrayByte()); [Test] public void FormatConversionBGRAToGrayAlpha() - => FormatConversionToArray(Col.Format.BGRA, Col.Format.GrayAlpha, color => new byte[] { color.ToGrayByte(), color.A }); + => FormatConversionToArray(Col.Format.BGRA, Col.Format.GrayAlpha, c => new byte[] { c.ToGrayByte(), c.A }); [Test] public void FormatConversionBGRAToRG() - => FormatConversionToArray(Col.Format.BGRA, Col.Format.RG, color => new byte[] { color.R, color.G }); + => FormatConversionToArray(Col.Format.BGRA, Col.Format.RG, c => new byte[] { c.R, c.G }); + + #region Byte to BGRA (other types) + + [Test] + public void TypeConversionByteToUShort() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4us()); + + [Test] + public void TypeConversionByteToUInt() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4ui()); + + [Test] + public void TypeConversionByteToHalf() + => FormatConversionToArray(Col.Format.RGBA, Col.Format.BGRA, + c => new float16[] { c.B.ByteToHalf(), c.G.ByteToHalf(), c.R.ByteToHalf(), c.A.ByteToHalf() } + ); + + [Test] + public void TypeConversionByteToFloat() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4f()); + + [Test] + public void TypeConversionByteToDouble() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4d()); + + #endregion + + #region UShort to BGRA (other types) + + [Test] + public void TypeConversionUShortToByte() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4b()); + + [Test] + public void TypeConversionUShortToUInt() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4ui()); + + [Test] + public void TypeConversionUShortToHalf() + => FormatConversionToArray(Col.Format.RGBA, Col.Format.BGRA, + c => new float16[] { c.B.UShortToHalf(), c.G.UShortToHalf(), c.R.UShortToHalf(), c.A.UShortToHalf() } + ); + + [Test] + public void TypeConversionUShortToFloat() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4f()); + + [Test] + public void TypeConversionUShortToDouble() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4d()); + + #endregion + + #region UInt to BGRA (other types) + + [Test] + public void TypeConversionUIntToByte() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4b()); + + [Test] + public void TypeConversionUIntToUShort() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4us()); + + [Test] + public void TypeConversionUIntToHalf() + => FormatConversionToArray(Col.Format.RGBA, Col.Format.BGRA, + c => new float16[] { c.B.UIntToHalf(), c.G.UIntToHalf(), c.R.UIntToHalf(), c.A.UIntToHalf() } + ); + + [Test] + public void TypeConversionUIntToFloat() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4f()); + + [Test] + public void TypeConversionUIntToDouble() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4d()); + + #endregion + + #region Half to BGRA (other types) + + [Test] + public void TypeConversionHalfToByte() + => FormatConversionFromArray(Col.Format.RGBA, Col.Format.BGRA, c => + new C4b(c[0].HalfToByte(), c[1].HalfToByte(), c[2].HalfToByte(), c[3].HalfToByte()) + ); + + [Test] + public void TypeConversionHalfToUShort() + => FormatConversionFromArray(Col.Format.RGBA, Col.Format.BGRA, c => + new C4us(c[0].HalfToUShort(), c[1].HalfToUShort(), c[2].HalfToUShort(), c[3].HalfToUShort()) + ); + + [Test] + public void TypeConversionHalfToUInt() + => FormatConversionFromArray(Col.Format.RGBA, Col.Format.BGRA, c => + new C4ui(c[0].HalfToUInt(), c[1].HalfToUInt(), c[2].HalfToUInt(), c[3].HalfToUInt()) + ); + + [Test] + public void TypeConversionHalfToFloat() + => FormatConversionFromArray(Col.Format.RGBA, Col.Format.BGRA, c => + new C4f(c[0], c[1], c[2], c[3]) + ); + + [Test] + public void TypeConversionHalfToDouble() + => FormatConversionFromArray(Col.Format.RGBA, Col.Format.BGRA, c => + new C4d(c[0], c[1], c[2], c[3]) + ); + + #endregion + + #region Float to BGRA (other types) + + [Test] + public void TypeConversionFloatToByte() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4b()); + + [Test] + public void TypeConversionFloatToUShort() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4us()); + + [Test] + public void TypeConversionFloatToUInt() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4ui()); + + [Test] + public void TypeConversionFloatToHalf() + => FormatConversionToArray(Col.Format.RGBA, Col.Format.BGRA, + c => new float16[] { c.B.FloatToHalf(), c.G.FloatToHalf(), c.R.FloatToHalf(), c.A.FloatToHalf() } + ); + + + [Test] + public void TypeConversionFloatToDouble() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4d()); + + #endregion + + #region Double to BGRA (other types) + + [Test] + public void TypeConversionDoubleToByte() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4b()); + + [Test] + public void TypeConversionDoubleToUShort() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4us()); + + [Test] + public void TypeConversionDoubleToUInt() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4ui()); + + [Test] + public void TypeConversionDoubleToHalf() + => FormatConversionToArray(Col.Format.RGBA, Col.Format.BGRA, + c => new float16[] { c.B.DoubleToHalf(), c.G.DoubleToHalf(), c.R.DoubleToHalf(), c.A.DoubleToHalf() } + ); + + [Test] + public void TypeConversionDoubleToFloat() + => FormatConversion(Col.Format.RGBA, Col.Format.BGRA, c => c.ToC4d()); + + #endregion #endregion @@ -191,27 +358,27 @@ public void FormatConversionBGRAToRG() [Test] public void FormatConversionRGBToBGRA() - => FormatConversion(Col.Format.RGB, Col.Format.BGRA, color => color.ToC4b()); + => FormatConversion(Col.Format.RGB, Col.Format.BGRA, c => c.ToC4b()); [Test] public void FormatConversionRGBToRGBA() - => FormatConversion(Col.Format.RGB, Col.Format.RGBA, color => color.ToC4b()); + => FormatConversion(Col.Format.RGB, Col.Format.RGBA, c => c.ToC4b()); [Test] public void FormatConversionRGBToBGR() - => FormatConversion(Col.Format.RGB, Col.Format.BGR, color => color); + => FormatConversion(Col.Format.RGB, Col.Format.BGR, c => c); [Test] public void FormatConversionRGBToGray() - => FormatConversion(Col.Format.RGB, Col.Format.Gray, color => color.ToGrayByte()); + => FormatConversion(Col.Format.RGB, Col.Format.Gray, c => c.ToGrayByte()); [Test] public void FormatConversionRGBToGrayAlpha() - => FormatConversionToArray(Col.Format.RGB, Col.Format.GrayAlpha, color => new byte[] { color.ToGrayByte(), 255 }); + => FormatConversionToArray(Col.Format.RGB, Col.Format.GrayAlpha, c => new byte[] { c.ToGrayByte(), 255 }); [Test] public void FormatConversionRGBToRG() - => FormatConversionToArray(Col.Format.RGB, Col.Format.RG, color => new byte[] { color.R, color.G }); + => FormatConversionToArray(Col.Format.RGB, Col.Format.RG, c => new byte[] { c.R, c.G }); #endregion @@ -219,27 +386,27 @@ public void FormatConversionRGBToRG() [Test] public void FormatConversionBGRToBGRA() - => FormatConversion(Col.Format.BGR, Col.Format.BGRA, color => color.ToC4b()); + => FormatConversion(Col.Format.BGR, Col.Format.BGRA, c => c.ToC4b()); [Test] public void FormatConversionBGRToRGBA() - => FormatConversion(Col.Format.BGR, Col.Format.RGBA, color => color.ToC4b()); + => FormatConversion(Col.Format.BGR, Col.Format.RGBA, c => c.ToC4b()); [Test] public void FormatConversionBGRToRGB() - => FormatConversion(Col.Format.BGR, Col.Format.RGB, color => color); + => FormatConversion(Col.Format.BGR, Col.Format.RGB, c => c); [Test] public void FormatConversionBGRToGray() - => FormatConversion(Col.Format.BGR, Col.Format.Gray, color => color.ToGrayByte()); + => FormatConversion(Col.Format.BGR, Col.Format.Gray, c => c.ToGrayByte()); [Test] public void FormatConversionBGRToGrayAlpha() - => FormatConversionToArray(Col.Format.BGR, Col.Format.GrayAlpha, color => new byte[] { color.ToGrayByte(), 255 }); + => FormatConversionToArray(Col.Format.BGR, Col.Format.GrayAlpha, c => new byte[] { c.ToGrayByte(), 255 }); [Test] public void FormatConversionBGRToRG() - => FormatConversionToArray(Col.Format.BGR, Col.Format.RG, color => new byte[] { color.R, color.G }); + => FormatConversionToArray(Col.Format.BGR, Col.Format.RG, c => new byte[] { c.R, c.G }); #endregion @@ -247,19 +414,19 @@ public void FormatConversionBGRToRG() [Test] public void FormatConversionRGToBGRA() - => FormatConversionFromArray(Col.Format.RG, Col.Format.BGRA, color => new C4b(color[0], color[1], (byte)0, (byte)255)); + => FormatConversionFromArray(Col.Format.RG, Col.Format.BGRA, c => new C4b(c[0], c[1], (byte)0, (byte)255)); [Test] public void FormatConversionRGToRGBA() - => FormatConversionFromArray(Col.Format.RG, Col.Format.RGBA, color => new C4b(color[0], color[1], (byte)0, (byte)255)); + => FormatConversionFromArray(Col.Format.RG, Col.Format.RGBA, c => new C4b(c[0], c[1], (byte)0, (byte)255)); [Test] public void FormatConversionRGToRGB() - => FormatConversionFromArray(Col.Format.RG, Col.Format.RGB, color => new C3b(color[0], color[1], (byte)0)); + => FormatConversionFromArray(Col.Format.RG, Col.Format.RGB, c => new C3b(c[0], c[1], (byte)0)); [Test] public void FormatConversionRGToBGR() - => FormatConversionFromArray(Col.Format.RG, Col.Format.BGR, color => new C3b(color[0], color[1], (byte)0)); + => FormatConversionFromArray(Col.Format.RG, Col.Format.BGR, c => new C3b(c[0], c[1], (byte)0)); #endregion