Пример #1
0
        public void NormalizedByte2()
        {
            Assert.AreEqual(0x0, new NormalizedByte2(Vector2.Zero).PackedValue);
            Assert.AreEqual(0x7F7F, new NormalizedByte2(Vector2.One).PackedValue);
            Assert.AreEqual(0x8181, new NormalizedByte2(-Vector2.One).PackedValue);

            Assert.AreEqual(Vector2.One, new NormalizedByte2(Vector2.One).ToVector2());
            Assert.AreEqual(Vector2.Zero, new NormalizedByte2(Vector2.Zero).ToVector2());
            Assert.AreEqual(-Vector2.One, new NormalizedByte2(-Vector2.One).ToVector2());
            Assert.AreEqual(Vector2.One, new NormalizedByte2(Vector2.One * 1234.0f).ToVector2());
            Assert.AreEqual(-Vector2.One, new NormalizedByte2(Vector2.One * -1234.0f).ToVector2());

            Assert.AreEqual(new Vector4(1, 1, 0, 1), ((IPackedVector) new NormalizedByte2(Vector2.One)).ToVector4());
            Assert.AreEqual(new Vector4(0, 0, 0, 1), ((IPackedVector) new NormalizedByte2(Vector2.Zero)).ToVector4());

            //Test Ordering
            float x = 0.1f;
            float y = -0.3f;

            Assert.AreEqual(0xda0d, new NormalizedByte2(x, y).PackedValue);

            var packed   = new NormalizedByte2(x, y).PackedValue;
            var unpacked = new NormalizedByte2()
            {
                PackedValue = packed
            }.ToVector2();

            Assert.AreEqual(x, unpacked.X, 0.01f);
            Assert.AreEqual(y, unpacked.Y, 0.01f);
        }
Пример #2
0
        public void NormalizedByte2()
        {
            // Test PackedValue
            Assert.Equal(0x0, new NormalizedByte2(Vector2.Zero).PackedValue);
            Assert.Equal(0x7F7F, new NormalizedByte2(Vector2.One).PackedValue);
            Assert.Equal(0x8181, new NormalizedByte2(-Vector2.One).PackedValue);

            // Test ToVector2
            Assert.True(Equal(Vector2.One, new NormalizedByte2(Vector2.One).ToVector2()));
            Assert.True(Equal(Vector2.Zero, new NormalizedByte2(Vector2.Zero).ToVector2()));
            Assert.True(Equal(-Vector2.One, new NormalizedByte2(-Vector2.One).ToVector2()));
            Assert.True(Equal(Vector2.One, new NormalizedByte2(Vector2.One * 1234.0f).ToVector2()));
            Assert.True(Equal(-Vector2.One, new NormalizedByte2(Vector2.One * -1234.0f).ToVector2()));

            // Test ToVector4
            Assert.True(Equal(new Vector4(1, 1, 0, 1), new NormalizedByte2(Vector2.One).ToVector4()));
            Assert.True(Equal(new Vector4(0, 0, 0, 1), new NormalizedByte2(Vector2.Zero).ToVector4()));

            // Test ToScaledVector4.
            Vector4 scaled = new NormalizedByte2(-Vector2.One).ToScaledVector4();

            Assert.Equal(0, scaled.X);
            Assert.Equal(0, scaled.Y);
            Assert.Equal(0, scaled.Z);
            Assert.Equal(1F, scaled.W);

            // Test PackFromScaledVector4.
            var pixel = default(NormalizedByte2);

            pixel.PackFromScaledVector4(scaled);
            Assert.Equal(0x8181, pixel.PackedValue);

            // Test Ordering
            float x = 0.1f;
            float y = -0.3f;

            Assert.Equal(0xda0d, new NormalizedByte2(x, y).PackedValue);
            var n = new NormalizedByte2();

            n.PackFromRgba32(new Rgba32(141, 90, 0, 0));
            Assert.Equal(0xda0d, n.PackedValue);

            var rgb  = default(Rgb24);
            var rgba = default(Rgba32);
            var bgr  = default(Bgr24);
            var bgra = default(Bgra32);

            new NormalizedByte2(x, y).ToRgb24(ref rgb);
            Assert.Equal(rgb, new Rgb24(141, 90, 0));

            new NormalizedByte2(x, y).ToRgba32(ref rgba);
            Assert.Equal(rgba, new Rgba32(141, 90, 0, 255));

            new NormalizedByte2(x, y).ToBgr24(ref bgr);
            Assert.Equal(bgr, new Bgr24(141, 90, 0));

            new NormalizedByte2(x, y).ToBgra32(ref bgra);
            Assert.Equal(bgra, new Bgra32(141, 90, 0, 255));
        }
Пример #3
0
        public void NormalizedByte2_ToRgb24()
        {
            // arrange
            var short4   = new NormalizedByte2(0.1f, -0.3f);
            var actual   = default(Rgb24);
            var expected = new Rgb24(141, 90, 0);

            // act
            short4.ToRgb24(ref actual);

            // assert
            Assert.Equal(expected, actual);
        }
Пример #4
0
        public void NormalizedByte2_ToArgb32()
        {
            // arrange
            var short4   = new NormalizedByte2(0.1f, -0.3f);
            var actual   = default(Argb32);
            var expected = new Argb32(141, 90, 0, 255);

            // act
            short4.ToArgb32(ref actual);

            // assert
            Assert.Equal(expected, actual);
        }
Пример #5
0
        public void NormalizedByte2_FromScaledVector4()
        {
            // arrange
            Vector4 scaled   = new NormalizedByte2(-Vector2.One).ToScaledVector4();
            var     byte2    = default(NormalizedByte2);
            uint    expected = 0x8181;

            // act
            byte2.FromScaledVector4(scaled);
            uint actual = byte2.PackedValue;

            // assert
            Assert.Equal(expected, actual);
        }
Пример #6
0
        public void NormalizedByte2_ToScaledVector4()
        {
            // arrange
            var byte2 = new NormalizedByte2(-Vector2.One);

            // act
            Vector4 actual = byte2.ToScaledVector4();

            // assert
            Assert.Equal(0, actual.X);
            Assert.Equal(0, actual.Y);
            Assert.Equal(0, actual.Z);
            Assert.Equal(1F, actual.W);
        }
Пример #7
0
        public void NormalizedByte2_PackFromRgba32()
        {
            // arrange
            var byte2    = new NormalizedByte2();
            var rgba     = new Rgba32(141, 90, 0, 0);
            int expected = 0xda0d;

            // act
            byte2.PackFromRgba32(rgba);
            ushort actual = byte2.PackedValue;

            // assert
            Assert.Equal(expected, actual);
        }
        public void NormalizedByte2()
        {
            // Test PackedValue
            Assert.Equal(0x0, new NormalizedByte2(Vector2.Zero).PackedValue);
            Assert.Equal(0x7F7F, new NormalizedByte2(Vector2.One).PackedValue);
            Assert.Equal(0x8181, new NormalizedByte2(-Vector2.One).PackedValue);

            // Test ToVector2
            Assert.True(Equal(Vector2.One, new NormalizedByte2(Vector2.One).ToVector2()));
            Assert.True(Equal(Vector2.Zero, new NormalizedByte2(Vector2.Zero).ToVector2()));
            Assert.True(Equal(-Vector2.One, new NormalizedByte2(-Vector2.One).ToVector2()));
            Assert.True(Equal(Vector2.One, new NormalizedByte2(Vector2.One * 1234.0f).ToVector2()));
            Assert.True(Equal(-Vector2.One, new NormalizedByte2(Vector2.One * -1234.0f).ToVector2()));

            // Test ToVector4
            Assert.True(Equal(new Vector4(1, 1, 0, 1), new NormalizedByte2(Vector2.One).ToVector4()));
            Assert.True(Equal(new Vector4(0, 0, 0, 1), new NormalizedByte2(Vector2.Zero).ToVector4()));

            // Test Ordering
            float x = 0.1f;
            float y = -0.3f;

            Assert.Equal(0xda0d, new NormalizedByte2(x, y).PackedValue);
            NormalizedByte2 n = new NormalizedByte2();

            n.PackFromBytes(141, 90, 0, 0);
            Assert.Equal(0xda0d, n.PackedValue);

            byte[] rgb  = new byte[3];
            byte[] rgba = new byte[4];
            byte[] bgr  = new byte[3];
            byte[] bgra = new byte[4];

            new NormalizedByte2(x, y).ToXyzBytes(rgb, 0);
            Assert.Equal(rgb, new byte[] { 141, 90, 0 });

            new NormalizedByte2(x, y).ToXyzwBytes(rgba, 0);
            Assert.Equal(rgba, new byte[] { 141, 90, 0, 255 });

            new NormalizedByte2(x, y).ToZyxBytes(bgr, 0);
            Assert.Equal(bgr, new byte[] { 0, 90, 141 });

            new NormalizedByte2(x, y).ToZyxwBytes(bgra, 0);
            Assert.Equal(bgra, new byte[] { 0, 90, 141, 255 });
        }
Пример #9
0
        private void GetColorData(Texture2D texture2D)
        {
            int colorDataLength = texture2D.Width * texture2D.Height;
            colorData = new Color[colorDataLength];

            switch (texture2D.Format)
            {
                case SurfaceFormat.Color:
                    texture2D.GetData<Color>(colorData);
                    break;

                case SurfaceFormat.Alpha8:
                    var alpha8Data = new Alpha8[colorDataLength];
                    texture2D.GetData<Alpha8>(alpha8Data);

                    for (int i = 0; i < colorDataLength; i++)
                    {
                        colorData[i] = new Color(((IPackedVector)alpha8Data[i]).ToVector4());
                    }

                    break;
                
                case SurfaceFormat.Bgr565:
                    var bgr565Data = new Bgr565[colorDataLength];
                    texture2D.GetData<Bgr565>(bgr565Data);

                    for (int i = 0; i < colorDataLength; i++)
                    {
                        colorData[i] = new Color(((IPackedVector)bgr565Data[i]).ToVector4());
                    }

                    break;

                case SurfaceFormat.Bgra4444:
                    var bgra4444Data = new Bgra4444[colorDataLength];
                    texture2D.GetData<Bgra4444>(bgra4444Data);

                    for (int i = 0; i < colorDataLength; i++)
                    {
                        colorData[i] = new Color(((IPackedVector)bgra4444Data[i]).ToVector4());
                    }

                    break;

                case SurfaceFormat.Bgra5551:
                    var bgra5551Data = new Bgra5551[colorDataLength];
                    texture2D.GetData<Bgra5551>(bgra5551Data);

                    for (int i = 0; i < colorDataLength; i++)
                    {
                        colorData[i] = new Color(((IPackedVector)bgra5551Data[i]).ToVector4());
                    }
                    break;

                case SurfaceFormat.HalfSingle:
                    var halfSingleData = new HalfSingle[colorDataLength];
                    texture2D.GetData<HalfSingle>(halfSingleData);

                    for (int i = 0; i < colorDataLength; i++)
                    {
                        colorData[i] = new Color(((IPackedVector)halfSingleData[i]).ToVector4());
                    }

                    break;

                case SurfaceFormat.HalfVector2:
                    var halfVector2Data = new HalfVector2[colorDataLength];
                    texture2D.GetData<HalfVector2>(halfVector2Data);

                    for (int i = 0; i < colorDataLength; i++)
                    {
                        colorData[i] = new Color(((IPackedVector)halfVector2Data[i]).ToVector4());
                    }

                    break;

                case SurfaceFormat.HalfVector4:
                    var halfVector4Data = new HalfVector4[colorDataLength];
                    texture2D.GetData<HalfVector4>(halfVector4Data);

                    for (int i = 0; i < colorDataLength; i++)
                    {
                        colorData[i] = new Color(((IPackedVector)halfVector4Data[i]).ToVector4());
                    }

                    break;

                case SurfaceFormat.NormalizedByte2:
                    var normalizedByte2Data = new NormalizedByte2[colorDataLength];
                    texture2D.GetData<NormalizedByte2>(normalizedByte2Data);

                    for (int i = 0; i < colorDataLength; i++)
                    {
                        colorData[i] = new Color(((IPackedVector)normalizedByte2Data[i]).ToVector4());
                    }

                    break;

                case SurfaceFormat.NormalizedByte4:
                    var normalizedByte4Data = new NormalizedByte4[colorDataLength];
                    texture2D.GetData<NormalizedByte4>(normalizedByte4Data);

                    for (int i = 0; i < colorDataLength; i++)
                    {
                        colorData[i] = new Color(((IPackedVector)normalizedByte4Data[i]).ToVector4());
                    }

                    break;

                case SurfaceFormat.Rg32:
                    var rg32Data = new Rg32[colorDataLength];
                    texture2D.GetData<Rg32>(rg32Data);

                    for (int i = 0; i < colorDataLength; i++)
                    {
                        colorData[i] = new Color(((IPackedVector)rg32Data[i]).ToVector4());
                    }

                    break;

                case SurfaceFormat.Rgba64:
                    var rgba64Data = new Rgba64[colorDataLength];
                    texture2D.GetData<Rgba64>(rgba64Data);

                    for (int i = 0; i < colorDataLength; i++)
                    {
                        colorData[i] = new Color(((IPackedVector)rgba64Data[i]).ToVector4());
                    }

                    break;

                case SurfaceFormat.Rgba1010102:
                    var rgba1010102Data = new Rgba1010102[colorDataLength];
                    texture2D.GetData<Rgba1010102>(rgba1010102Data);

                    for (int i = 0; i < colorDataLength; i++)
                    {
                        colorData[i] = new Color(((IPackedVector)rgba1010102Data[i]).ToVector4());
                    }

                    break;

                default:
                    throw new Exception("Texture surface format not supported");
            }
        }
Пример #10
0
        private void GetColorData(Texture2D texture2D)
        {
            int colorDataLength = texture2D.Width * texture2D.Height;

            colorData = new Color[colorDataLength];

            switch (texture2D.Format)
            {
            case SurfaceFormat.Single:
                var floatData = new float[colorDataLength];
                texture2D.GetData <float>(floatData);

                for (int i = 0; i < colorDataLength; i++)
                {
                    float brightness = floatData[i];
                    // Export as a greyscale image.
                    colorData[i] = new Color(brightness, brightness, brightness);
                }
                break;

            case SurfaceFormat.Color:
                texture2D.GetData <Color>(colorData);
                break;

            case SurfaceFormat.Alpha8:
                var alpha8Data = new Alpha8[colorDataLength];
                texture2D.GetData <Alpha8>(alpha8Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(((IPackedVector)alpha8Data[i]).ToVector4());
                }

                break;

            case SurfaceFormat.Bgr565:
                var bgr565Data = new Bgr565[colorDataLength];
                texture2D.GetData <Bgr565>(bgr565Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(((IPackedVector)bgr565Data[i]).ToVector4());
                }

                break;

            case SurfaceFormat.Bgra4444:
                var bgra4444Data = new Bgra4444[colorDataLength];
                texture2D.GetData <Bgra4444>(bgra4444Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(((IPackedVector)bgra4444Data[i]).ToVector4());
                }

                break;

            case SurfaceFormat.Bgra5551:
                var bgra5551Data = new Bgra5551[colorDataLength];
                texture2D.GetData <Bgra5551>(bgra5551Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(((IPackedVector)bgra5551Data[i]).ToVector4());
                }
                break;

            case SurfaceFormat.HalfSingle:
                var halfSingleData = new HalfSingle[colorDataLength];
                texture2D.GetData <HalfSingle>(halfSingleData);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(((IPackedVector)halfSingleData[i]).ToVector4());
                }

                break;

            case SurfaceFormat.HalfVector2:
                var halfVector2Data = new HalfVector2[colorDataLength];
                texture2D.GetData <HalfVector2>(halfVector2Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(((IPackedVector)halfVector2Data[i]).ToVector4());
                }

                break;

            case SurfaceFormat.HalfVector4:
                var halfVector4Data = new HalfVector4[colorDataLength];
                texture2D.GetData <HalfVector4>(halfVector4Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(((IPackedVector)halfVector4Data[i]).ToVector4());
                }

                break;

            case SurfaceFormat.NormalizedByte2:
                var normalizedByte2Data = new NormalizedByte2[colorDataLength];
                texture2D.GetData <NormalizedByte2>(normalizedByte2Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(((IPackedVector)normalizedByte2Data[i]).ToVector4());
                }

                break;

            case SurfaceFormat.NormalizedByte4:
                var normalizedByte4Data = new NormalizedByte4[colorDataLength];
                texture2D.GetData <NormalizedByte4>(normalizedByte4Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(((IPackedVector)normalizedByte4Data[i]).ToVector4());
                }

                break;

            case SurfaceFormat.Rg32:
                var rg32Data = new Rg32[colorDataLength];
                texture2D.GetData <Rg32>(rg32Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(((IPackedVector)rg32Data[i]).ToVector4());
                }

                break;

            case SurfaceFormat.Rgba64:
                var rgba64Data = new Rgba64[colorDataLength];
                texture2D.GetData <Rgba64>(rgba64Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(((IPackedVector)rgba64Data[i]).ToVector4());
                }

                break;

            case SurfaceFormat.Rgba1010102:
                var rgba1010102Data = new Rgba1010102[colorDataLength];
                texture2D.GetData <Rgba1010102>(rgba1010102Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(((IPackedVector)rgba1010102Data[i]).ToVector4());
                }

                break;

            default:
                throw new Exception("Texture surface format not supported");
            }
        }
Пример #11
0
        internal Color[] GetColorData()
        {
            int colorDataLength = Width * Height;
            var colorData       = new Color[colorDataLength];

            switch (Format)
            {
            case SurfaceFormat.Single:
                var floatData = new float[colorDataLength];
                GetData(floatData);

                for (int i = 0; i < colorDataLength; i++)
                {
                    float brightness = floatData[i];
                    // Export as a greyscale image.
                    colorData[i] = new Color(brightness, brightness, brightness);
                }
                break;

            case SurfaceFormat.Color:
                GetData(colorData);
                break;

            case SurfaceFormat.Alpha8:
                var alpha8Data = new Alpha8[colorDataLength];
                GetData(alpha8Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(alpha8Data[i].ToVector4());
                }

                break;

            case SurfaceFormat.Bgr565:
                var bgr565Data = new Bgr565[colorDataLength];
                GetData(bgr565Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(bgr565Data[i].ToVector4());
                }

                break;

            case SurfaceFormat.Bgra4444:
                var bgra4444Data = new Bgra4444[colorDataLength];
                GetData(bgra4444Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(bgra4444Data[i].ToVector4());
                }

                break;

            case SurfaceFormat.Bgra5551:
                var bgra5551Data = new Bgra5551[colorDataLength];
                GetData(bgra5551Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(bgra5551Data[i].ToVector4());
                }
                break;

            case SurfaceFormat.HalfSingle:
                var halfSingleData = new HalfSingle[colorDataLength];
                GetData(halfSingleData);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(halfSingleData[i].ToVector4());
                }

                break;

            case SurfaceFormat.HalfVector2:
                var halfVector2Data = new HalfVector2[colorDataLength];
                GetData(halfVector2Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(halfVector2Data[i].ToVector4());
                }

                break;

            case SurfaceFormat.HalfVector4:
                var halfVector4Data = new HalfVector4[colorDataLength];
                GetData(halfVector4Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(halfVector4Data[i].ToVector4());
                }

                break;

            case SurfaceFormat.NormalizedByte2:
                var normalizedByte2Data = new NormalizedByte2[colorDataLength];
                GetData(normalizedByte2Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(normalizedByte2Data[i].ToVector4());
                }

                break;

            case SurfaceFormat.NormalizedByte4:
                var normalizedByte4Data = new NormalizedByte4[colorDataLength];
                GetData(normalizedByte4Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(normalizedByte4Data[i].ToVector4());
                }

                break;

            case SurfaceFormat.Rg32:
                var rg32Data = new Rg32[colorDataLength];
                GetData(rg32Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(rg32Data[i].ToVector4());
                }

                break;

            case SurfaceFormat.Rgba64:
                var rgba64Data = new Rgba64[colorDataLength];
                GetData(rgba64Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(rgba64Data[i].ToVector4());
                }

                break;

            case SurfaceFormat.Rgba1010102:
                var rgba1010102Data = new Rgba1010102[colorDataLength];
                GetData(rgba1010102Data);

                for (int i = 0; i < colorDataLength; i++)
                {
                    colorData[i] = new Color(rgba1010102Data[i].ToVector4());
                }

                break;

            default:
                throw new Exception("Texture surface format not supported");
            }

            return(colorData);
        }