コード例 #1
0
        public void Bgra5551()
        {
            // Test the limits.
            Assert.Equal(0x0, new Bgra5551(Vector4.Zero).PackedValue);
            Assert.Equal(0xFFFF, new Bgra5551(Vector4.One).PackedValue);

            // Test ToVector4
            Assert.True(Equal(Vector4.Zero, new Bgra5551(Vector4.Zero).ToVector4()));
            Assert.True(Equal(Vector4.One, new Bgra5551(Vector4.One).ToVector4()));

            // Test ToScaledVector4.
            Vector4 scaled = new Bgra5551(Vector4.One).ToScaledVector4();

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

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

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

            // Test clamping.
            Assert.Equal(Vector4.Zero, new Bgra5551(Vector4.One * -1234.0f).ToVector4());
            Assert.Equal(Vector4.One, new Bgra5551(Vector4.One * 1234.0f).ToVector4());

            // Test Ordering
            float x = 0x1a;
            float y = 0x16;
            float z = 0xd;
            float w = 0x1;

            Assert.Equal(0xeacd, new Bgra5551(x / 0x1f, y / 0x1f, z / 0x1f, w).PackedValue);
            x = 0.1f;
            y = -0.3f;
            z = 0.5f;
            w = -0.7f;
            Assert.Equal(3088, new Bgra5551(x, y, z, w).PackedValue);

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

            new Bgra5551(x, y, z, w).ToRgb24(ref rgb);
            Assert.Equal(rgb, new Rgb24(24, 0, 131));

            new Bgra5551(x, y, z, w).ToRgba32(ref rgba);
            Assert.Equal(rgba, new Rgba32(24, 0, 131, 0));

            new Bgra5551(x, y, z, w).ToBgr24(ref bgr);
            Assert.Equal(bgr, new Bgr24(24, 0, 131));

            new Bgra5551(x, y, z, w).ToBgra32(ref bgra);
            Assert.Equal(bgra, new Bgra32(24, 0, 131, 0));
        }
コード例 #2
0
        /// <summary>
        /// Reads a uncompressed TGA image with a palette.
        /// </summary>
        /// <typeparam name="TPixel">The pixel type.</typeparam>
        /// <param name="width">The width of the image.</param>
        /// <param name="height">The height of the image.</param>
        /// <param name="pixels">The <see cref="Buffer2D{TPixel}"/> to assign the palette to.</param>
        /// <param name="palette">The color palette.</param>
        /// <param name="colorMapPixelSizeInBytes">Color map size of one entry in bytes.</param>
        /// <param name="inverted">Indicates, if the origin of the image is top left rather the bottom left (the default).</param>
        private void ReadPaletted <TPixel>(int width, int height, Buffer2D <TPixel> pixels, byte[] palette, int colorMapPixelSizeInBytes, bool inverted)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            using (IManagedByteBuffer row = this.memoryAllocator.AllocateManagedByteBuffer(width, AllocationOptions.Clean))
            {
                TPixel      color   = default;
                Span <byte> rowSpan = row.GetSpan();

                for (int y = 0; y < height; y++)
                {
                    this.currentStream.Read(row);
                    int           newY     = Invert(y, height, inverted);
                    Span <TPixel> pixelRow = pixels.GetRowSpan(newY);
                    switch (colorMapPixelSizeInBytes)
                    {
                    case 2:
                        for (int x = 0; x < width; x++)
                        {
                            int colorIndex = rowSpan[x];

                            Bgra5551 bgra = Unsafe.As <byte, Bgra5551>(ref palette[colorIndex * colorMapPixelSizeInBytes]);
                            if (!this.hasAlpha)
                            {
                                // Set alpha value to 1, to treat it as opaque for Bgra5551.
                                bgra.PackedValue = (ushort)(bgra.PackedValue | 0x8000);
                            }

                            color.FromBgra5551(bgra);
                            pixelRow[x] = color;
                        }

                        break;

                    case 3:
                        for (int x = 0; x < width; x++)
                        {
                            int colorIndex = rowSpan[x];
                            color.FromBgr24(Unsafe.As <byte, Bgr24>(ref palette[colorIndex * colorMapPixelSizeInBytes]));
                            pixelRow[x] = color;
                        }

                        break;

                    case 4:
                        for (int x = 0; x < width; x++)
                        {
                            int colorIndex = rowSpan[x];
                            color.FromBgra32(Unsafe.As <byte, Bgra32>(ref palette[colorIndex * colorMapPixelSizeInBytes]));
                            pixelRow[x] = color;
                        }

                        break;
                    }
                }
            }
        }
コード例 #3
0
ファイル: Bgra5551Tests.cs プロジェクト: wilka/ImageSharp
        public void AreNotEqual()
        {
            var color1 = new Bgra5551(0.0f, 0.0f, 0.0f, 0.0f);
            var color2 = new Bgra5551(new Vector4(1.0f));
            var color3 = new Bgra5551(new Vector4(1.0f, 0.0f, 0.0f, 1.0f));
            var color4 = new Bgra5551(1.0f, 1.0f, 0.0f, 1.0f);

            Assert.NotEqual(color1, color2);
            Assert.NotEqual(color3, color4);
        }
コード例 #4
0
ファイル: Bgra5551Tests.cs プロジェクト: wilka/ImageSharp
        public void Bgra5551_ToRgba32()
        {
            // arrange
            var bgra     = new Bgra5551(Vector4.One);
            var expected = new Rgba32(Vector4.One);
            var actual   = default(Rgba32);

            // act
            bgra.ToRgba32(ref actual);

            Assert.Equal(expected, actual);
        }
コード例 #5
0
        public void Bgra5551_ToBgr24()
        {
            // arrange
            var bgra     = new Bgra5551(0.1f, -0.3f, 0.5f, -0.7f);
            var actual   = default(Bgr24);
            var expected = new Bgr24(24, 0, 131);

            // act
            bgra.ToBgr24(ref actual);

            // assert
            Assert.Equal(expected, actual);
        }
コード例 #6
0
        public void Bgra5551_Rgba32()
        {
            // arrange
            var bgra     = new Bgra5551(0.1f, -0.3f, 0.5f, -0.7f);
            var actual   = default(Rgba32);
            var expected = new Rgba32(24, 0, 131, 0);

            // act
            bgra.ToRgba32(ref actual);

            // assert
            Assert.Equal(expected, actual);
        }
コード例 #7
0
ファイル: Bgra5551Tests.cs プロジェクト: wilka/ImageSharp
        public void Bgra5551_FromBgra5551()
        {
            // arrange
            var bgra     = default(Bgra5551);
            var actual   = default(Bgra5551);
            var expected = new Bgra5551(1.0f, 0.0f, 1.0f, 1.0f);

            // act
            bgra.FromBgra5551(expected);
            actual.FromBgra5551(bgra);

            // assert
            Assert.Equal(expected, actual);
        }
コード例 #8
0
        /// <summary>
        /// Writes a run length encoded tga image to the stream.
        /// </summary>
        /// <typeparam name="TPixel">The pixel type.</typeparam>
        /// <param name="stream">The stream to write the image to.</param>
        /// <param name="image">The image to encode.</param>
        private void WriteRunLengthEncodedImage <TPixel>(Stream stream, ImageFrame <TPixel> image)
            where TPixel : struct, IPixel <TPixel>
        {
            Rgba32            color  = default;
            Buffer2D <TPixel> pixels = image.PixelBuffer;
            int totalPixels          = image.Width * image.Height;
            int encodedPixels        = 0;

            while (encodedPixels < totalPixels)
            {
                int    x            = encodedPixels % pixels.Width;
                int    y            = encodedPixels / pixels.Width;
                TPixel currentPixel = pixels[x, y];
                currentPixel.ToRgba32(ref color);
                byte equalPixelCount = this.FindEqualPixels(pixels, x, y);

                // Write the number of equal pixels, with the high bit set, indicating ist a compressed pixel run.
                stream.WriteByte((byte)(equalPixelCount | 128));
                switch (this.bitsPerPixel)
                {
                case TgaBitsPerPixel.Pixel8:
                    int luminance = GetLuminance(currentPixel);
                    stream.WriteByte((byte)luminance);
                    break;

                case TgaBitsPerPixel.Pixel16:
                    var bgra5551 = new Bgra5551(color.ToVector4());
                    BinaryPrimitives.TryWriteInt16LittleEndian(this.buffer, (short)bgra5551.PackedValue);
                    stream.WriteByte(this.buffer[0]);
                    stream.WriteByte(this.buffer[1]);

                    break;

                case TgaBitsPerPixel.Pixel24:
                    stream.WriteByte(color.B);
                    stream.WriteByte(color.G);
                    stream.WriteByte(color.R);
                    break;

                case TgaBitsPerPixel.Pixel32:
                    stream.WriteByte(color.B);
                    stream.WriteByte(color.G);
                    stream.WriteByte(color.R);
                    stream.WriteByte(color.A);
                    break;
                }

                encodedPixels += equalPixelCount + 1;
            }
        }
コード例 #9
0
ファイル: Bgra5551Tests.cs プロジェクト: wilka/ImageSharp
        public void Bgra5551_ToScaledVector4()
        {
            // arrange
            var bgra = new Bgra5551(Vector4.One);

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

            // assert
            Assert.Equal(1, actual.X);
            Assert.Equal(1, actual.Y);
            Assert.Equal(1, actual.Z);
            Assert.Equal(1, actual.W);
        }
コード例 #10
0
ファイル: Bgra5551Tests.cs プロジェクト: wilka/ImageSharp
        public void Bgra5551_FromScaledVector4()
        {
            // arrange
            Vector4 scaled   = new Bgra5551(Vector4.One).ToScaledVector4();
            int     expected = 0xFFFF;
            var     pixel    = default(Bgra5551);

            // act
            pixel.FromScaledVector4(scaled);
            ushort actual = pixel.PackedValue;

            // assert
            Assert.Equal(expected, actual);
        }
コード例 #11
0
        private void ReadPalettedBgra16Pixel <TPixel>(byte[] palette, int index, int colorMapPixelSizeInBytes, ref TPixel color)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            Bgra5551 bgra = default;

            bgra.FromBgra5551(Unsafe.As <byte, Bgra5551>(ref palette[index * colorMapPixelSizeInBytes]));

            if (!this.hasAlpha)
            {
                // Set alpha value to 1, to treat it as opaque.
                bgra.PackedValue = (ushort)(bgra.PackedValue | 0x8000);
            }

            color.FromBgra5551(bgra);
        }
コード例 #12
0
        private void ReadPalettedBgr16Pixel <TPixel>(byte[] palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span <TPixel> pixelRow)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            int      colorIndex = this.currentStream.ReadByte();
            Bgra5551 bgra       = default;

            bgra.FromBgra5551(Unsafe.As <byte, Bgra5551>(ref palette[colorIndex * colorMapPixelSizeInBytes]));
            if (!this.hasAlpha)
            {
                // Set alpha value to 1, to treat it as opaque for Bgra5551.
                bgra.PackedValue = (ushort)(bgra.PackedValue | 0x8000);
            }

            color.FromBgra5551(bgra);
            pixelRow[x] = color;
        }
コード例 #13
0
ファイル: TgaDecoderCore.cs プロジェクト: zhoubass/ImageSharp
        /// <summary>
        /// Reads a run length encoded TGA image with a palette.
        /// </summary>
        /// <typeparam name="TPixel">The pixel type.</typeparam>
        /// <param name="width">The width of the image.</param>
        /// <param name="height">The height of the image.</param>
        /// <param name="pixels">The <see cref="Buffer2D{TPixel}"/> to assign the palette to.</param>
        /// <param name="palette">The color palette.</param>
        /// <param name="colorMapPixelSizeInBytes">Color map size of one entry in bytes.</param>
        /// <param name="inverted">Indicates, if the origin of the image is top left rather the bottom left (the default).</param>
        private void ReadPalettedRle <TPixel>(int width, int height, Buffer2D <TPixel> pixels, byte[] palette, int colorMapPixelSizeInBytes, bool inverted)
            where TPixel : struct, IPixel <TPixel>
        {
            int bytesPerPixel = 1;

            using (IMemoryOwner <byte> buffer = this.memoryAllocator.Allocate <byte>(width * height * bytesPerPixel, AllocationOptions.Clean))
            {
                TPixel      color      = default;
                Span <byte> bufferSpan = buffer.GetSpan();
                this.UncompressRle(width, height, bufferSpan, bytesPerPixel: 1);

                for (int y = 0; y < height; y++)
                {
                    int           newY        = Invert(y, height, inverted);
                    Span <TPixel> pixelRow    = pixels.GetRowSpan(newY);
                    int           rowStartIdx = y * width * bytesPerPixel;
                    for (int x = 0; x < width; x++)
                    {
                        int idx = rowStartIdx + x;
                        switch (colorMapPixelSizeInBytes)
                        {
                        case 1:
                            color.FromL8(Unsafe.As <byte, L8>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
                            break;

                        case 2:
                            // Set alpha value to 1, to treat it as opaque for Bgra5551.
                            Bgra5551 bgra = Unsafe.As <byte, Bgra5551>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]);
                            bgra.PackedValue = (ushort)(bgra.PackedValue | 0x8000);
                            color.FromBgra5551(bgra);
                            break;

                        case 3:
                            color.FromBgr24(Unsafe.As <byte, Bgr24>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
                            break;

                        case 4:
                            color.FromBgra32(Unsafe.As <byte, Bgra32>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
                            break;
                        }

                        pixelRow[x] = color;
                    }
                }
            }
        }
コード例 #14
0
        public void Bgra5551()
        {
            // Test the limits.
            Assert.AreEqual(0x0, new Bgra5551(Vector4.Zero).PackedValue);
            Assert.AreEqual(0xFFFF, new Bgra5551(Vector4.One).PackedValue);

            // Test ToVector4
            Assert.AreEqual(Vector4.Zero, new Bgra5551(Vector4.Zero).ToVector4());
            Assert.AreEqual(Vector4.One, new Bgra5551(Vector4.One).ToVector4());

            // Test clamping.
            Assert.AreEqual(Vector4.Zero, new Bgra5551(Vector4.One * -1234.0f).ToVector4());
            Assert.AreEqual(Vector4.One, new Bgra5551(Vector4.One * 1234.0f).ToVector4());

            //Test Ordering
            float x = 0x1a;
            float y = 0x16;
            float z = 0xd;
            float w = 0x1;

            Assert.AreEqual(0xeacd, new Bgra5551(x / 0x1f, y / 0x1f, z / 0x1f, w).PackedValue);
            x = 0.1f;
            y = -0.3f;
            z = 0.5f;
            w = -0.7f;
            Assert.AreEqual(3088, new Bgra5551(x, y, z, w).PackedValue);

            var packed   = new Bgra5551(x, y, z, w).PackedValue;
            var unpacked = new Bgra5551()
            {
                PackedValue = packed
            }.ToVector4();

            Assert.AreEqual(x, unpacked.X, 0.1f);
            Assert.AreEqual(0f, unpacked.Y);
            Assert.AreEqual(z, unpacked.Z, 0.1f);
            Assert.AreEqual(0f, unpacked.W);
        }
コード例 #15
0
 public void FromBgra5551(Bgra5551 source) => this.FromScaledVector4(source.ToScaledVector4());
コード例 #16
0
 public void FromBgra5551(Bgra5551 source)
 {
 }
コード例 #17
0
        /// <summary>
        /// Reads a run length encoded TGA image with a palette.
        /// </summary>
        /// <typeparam name="TPixel">The pixel type.</typeparam>
        /// <param name="width">The width of the image.</param>
        /// <param name="height">The height of the image.</param>
        /// <param name="pixels">The <see cref="Buffer2D{TPixel}"/> to assign the palette to.</param>
        /// <param name="palette">The color palette.</param>
        /// <param name="colorMapPixelSizeInBytes">Color map size of one entry in bytes.</param>
        /// <param name="origin">The image origin.</param>
        private void ReadPalettedRle <TPixel>(int width, int height, Buffer2D <TPixel> pixels, byte[] palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            int bytesPerPixel = 1;

            using (IMemoryOwner <byte> buffer = this.memoryAllocator.Allocate <byte>(width * height * bytesPerPixel, AllocationOptions.Clean))
            {
                TPixel      color      = default;
                var         alphaBits  = this.tgaMetadata.AlphaChannelBits;
                Span <byte> bufferSpan = buffer.GetSpan();
                this.UncompressRle(width, height, bufferSpan, bytesPerPixel: 1);

                for (int y = 0; y < height; y++)
                {
                    int           newY        = InvertY(y, height, origin);
                    Span <TPixel> pixelRow    = pixels.GetRowSpan(newY);
                    int           rowStartIdx = y * width * bytesPerPixel;
                    for (int x = 0; x < width; x++)
                    {
                        int idx = rowStartIdx + x;
                        switch (colorMapPixelSizeInBytes)
                        {
                        case 1:
                            color.FromL8(Unsafe.As <byte, L8>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
                            break;

                        case 2:

                            Bgra5551 bgra = Unsafe.As <byte, Bgra5551>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]);
                            if (!this.hasAlpha)
                            {
                                // Set alpha value to 1, to treat it as opaque for Bgra5551.
                                bgra.PackedValue = (ushort)(bgra.PackedValue | 0x8000);
                            }

                            color.FromBgra5551(bgra);
                            break;

                        case 3:
                            color.FromBgr24(Unsafe.As <byte, Bgr24>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
                            break;

                        case 4:
                            if (this.hasAlpha)
                            {
                                color.FromBgra32(Unsafe.As <byte, Bgra32>(ref palette[bufferSpan[idx] * colorMapPixelSizeInBytes]));
                            }
                            else
                            {
                                var alpha = alphaBits == 0 ? byte.MaxValue : bufferSpan[idx + 3];
                                color.FromBgra32(new Bgra32(bufferSpan[idx + 2], bufferSpan[idx + 1], bufferSpan[idx], (byte)alpha));
                            }

                            break;
                        }

                        int newX = InvertX(x, width, origin);
                        pixelRow[newX] = color;
                    }
                }
            }
        }
コード例 #18
0
 /// <inheritdoc />
 public void FromBgra5551(Bgra5551 source)
 {
     throw new NotImplementedException();
 }
コード例 #19
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);
        }
コード例 #20
0
ファイル: PngWriter.cs プロジェクト: Cardanis/MonoGame
        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");
            }
        }
コード例 #21
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");
            }
        }