public void ConversionTest <TSrcPixel, TDstPixel>() where TSrcPixel : unmanaged, Pixel.IConvertTo, Pixel.IValueSetter <Pixel.BGRA32> where TDstPixel : unmanaged, Pixel.IConvertTo, Pixel.IValueSetter <Pixel.BGRA32> { var srcFmt = PixelFormat.TryIdentifyFormat <TSrcPixel>(); var dstFmt = PixelFormat.TryIdentifyFormat <TDstPixel>(); var src = new TSrcPixel[5]; var dst = new TDstPixel[5]; for (int i = 0; i < 5; ++i) { src[i].SetValue(new Pixel.BGRA32(i * 50, 255 - i * 50, i * 30, 20 + i * 30)); } Pixel.GetPixelCopyConverter <TSrcPixel, TDstPixel>().Invoke(src, dst); for (int i = 0; i < 5; ++i) { var srcP = src[i].To <Pixel.BGRA32>(); var dstP = dst[i].To <Pixel.BGRA32>(); if (!srcFmt.HasUnpremulAlpha || !dstFmt.HasUnpremulAlpha) { srcP = new Pixel.BGRA32(srcP.R, srcP.G, srcP.B, (Byte)255); dstP = new Pixel.BGRA32(dstP.R, dstP.G, dstP.B, (Byte)255); } Assert.AreEqual(srcP, dstP); } }
// 7.508 ns public static Pixel.BGRP32 ToPremulReference(Pixel.BGRA32 src) { uint aa = src.A; return(new Pixel.BGRP32 ( (Byte)((src.R * aa) / 255u), (Byte)((src.G * aa) / 255u), (Byte)((src.B * aa) / 255u), src.A)); }
// 7.163 ns public Pixel.BGRP32 ToPremulFast(Pixel.BGRA32 src) { var aa = (uint)257 * (uint)src.A; return(new Pixel.BGRP32 ( (Byte)((src.R * aa + 255u) >> 16), (Byte)((src.G * aa + 255u) >> 16), (Byte)((src.B * aa + 255u) >> 16), src.A)); }
private static void TestPremultiply <TPixel>(int a, int r) where TPixel : unmanaged , Pixel.IValueSetter <Pixel.BGRA32> , Pixel.IValueSetter <Pixel.BGRP32> , Pixel.IConvertTo { // color var src = new Pixel.BGRA32(r, 1, 255, a); var color = default(TPixel); color.SetValue(src); // references var premulRef0 = color.GetReferenceBGRP32(); var rndtrpRef0 = premulRef0.GetReferenceBGRP32 <TPixel>(); // premul var premul1 = color.To <Pixel.BGRP32>(); var premul2 = Pixel.BGRP32.From(color); var premul3 = default(Pixel.BGRP32); color.CopyTo(ref premul3); Assert.AreEqual(premulRef0, premul1); Assert.AreEqual(premulRef0, premul2); Assert.AreEqual(premulRef0, premul3); // unpremul TPixel color1 = default; color1.SetValue(premul1); var color2 = premul1.To <TPixel>(); Assert.AreEqual(rndtrpRef0, color1); Assert.AreEqual(rndtrpRef0, color2); }