Esempio n. 1
0
        /// <summary>
        /// Instantiates a <see cref="BC1BlockData"/> from compressed BC1 block data.
        /// </summary>
        /// <param name="bytes">The data of a BC1 compressed block.</param>
        public static BC1BlockData FromBytes(byte[] bytes)
        {
            Debug.Assert(bytes.Length == BlockFormat.BC1ByteSize,
                         "Mismatching number of bytes for format.");

            byte c0Low = bytes[0];
            byte c0Hi  = bytes[1];
            byte c1Low = bytes[2];
            byte c1Hi  = bytes[3];

            byte[] indexes = new byte[4];
            indexes[0] = bytes[4];
            indexes[1] = bytes[5];
            indexes[2] = bytes[6];
            indexes[3] = bytes[7];

            var block = new BC1BlockData();

            block.Color0 = Color565.FromValue((ushort)((c0Hi << 8) | c0Low));
            block.Color1 = Color565.FromValue((ushort)((c1Hi << 8) | c1Low));

            for (int p = 0, row = 0; p < BlockFormat.TexelCount; p += BlockFormat.Dimension, ++row)
            {
                block.ColorIndexes[p]     = indexes[row] & 0x03;
                block.ColorIndexes[p + 1] = (indexes[row] >> 2) & 0x03;
                block.ColorIndexes[p + 2] = (indexes[row] >> 4) & 0x03;
                block.ColorIndexes[p + 3] = (indexes[row] >> 6) & 0x03;
            }

            return(block);
        }
Esempio n. 2
0
        public void ConstructFrom16BitUnsignedInteger()
        {
            var color = Color565.FromValue(22135);

            Assert.AreEqual(10, color.R);
            Assert.AreEqual(51, color.G);
            Assert.AreEqual(23, color.B);
            Assert.AreEqual(22135, color.Value);
        }
Esempio n. 3
0
        /// <summary>
        /// Instantiates a <see cref="BC2BlockData"/> from compressed BC2 block data.
        /// </summary>
        /// <param name="bytes">The data of a BC2 compressed block.</param>
        public static BC2BlockData FromBytes(byte[] bytes)
        {
            Debug.Assert(bytes.Length == BlockFormat.BC2ByteSize,
                         "Mismatching number of bytes for format.");

            byte[,] alphas = new byte[4, 2];
            alphas[0, 0]   = bytes[0];
            alphas[0, 1]   = bytes[1];
            alphas[1, 0]   = bytes[2];
            alphas[1, 1]   = bytes[3];
            alphas[2, 0]   = bytes[4];
            alphas[2, 1]   = bytes[5];
            alphas[3, 0]   = bytes[6];
            alphas[3, 1]   = bytes[7];
            byte c0Low = bytes[8];
            byte c0Hi  = bytes[9];
            byte c1Low = bytes[10];
            byte c1Hi  = bytes[11];

            byte[] indexes = new byte[4];
            indexes[0] = bytes[12];
            indexes[1] = bytes[13];
            indexes[2] = bytes[14];
            indexes[3] = bytes[15];

            var block = new BC2BlockData();

            block.Color0 = Color565.FromValue((ushort)((c0Hi << 8) | c0Low));
            block.Color1 = Color565.FromValue((ushort)((c1Hi << 8) | c1Low));

            for (int p = 0, row = 0; p < BlockFormat.TexelCount; p += BlockFormat.Dimension, ++row)
            {
                int a = p;
                int b = p + 1;
                int c = p + 2;
                int d = p + 3;

                block.ColorIndexes[a] = indexes[row] & 0x03;
                block.ColorIndexes[b] = (indexes[row] >> 2) & 0x03;
                block.ColorIndexes[c] = (indexes[row] >> 4) & 0x03;
                block.ColorIndexes[d] = (indexes[row] >> 6) & 0x03;

                block.ColorAlphas[a] = (byte)(alphas[row, 1] & 0x0F);
                block.ColorAlphas[b] = (byte)((alphas[row, 1] & 0xF0) >> 4);
                block.ColorAlphas[c] = (byte)(alphas[row, 0] & 0x0F);
                block.ColorAlphas[d] = (byte)((alphas[row, 0] & 0xF0) >> 4);
            }

            return(block);
        }
Esempio n. 4
0
        public void CompressionOrdersReferenceColors()
        {
            var expectedMin    = Color.FromArgb(10, 10, 10);
            var expectedMax    = Color.FromArgb(250, 250, 250);
            var expectedColor0 = ColorUtility.To16Bit(expectedMax);
            var expectedColor1 = ColorUtility.To16Bit(expectedMin);
            var colors         = ColorHelper.CreateRandomColorsBetween(expectedMin, expectedMax);

            // Place expected min and max color in test input
            colors[4]  = expectedMin;
            colors[10] = expectedMax;

            var data = new BC2Format().Compress(colors);

            var color0 = Color565.FromValue((ushort)((data[9] << 8) | data[8]));
            var color1 = Color565.FromValue((ushort)((data[11] << 8) | data[10]));

            Assert.Greater(color0.Value, color1.Value);
            Assert.AreEqual(expectedColor0.Value, color0.Value);
            Assert.AreEqual(expectedColor1.Value, color1.Value);
        }
        public void CompressionSwitchesReferenceColorOrderWhenAlpha()
        {
            var expectedMin    = Color.FromArgb(10, 10, 10);
            var expectedMax    = Color.FromArgb(250, 250, 250);
            var expectedColor0 = ColorUtility.To16Bit(expectedMin);
            var expectedColor1 = ColorUtility.To16Bit(expectedMax);
            var colors         = ColorHelper.CreateRandomColorsBetween(expectedMin, expectedMax);

            // Place expected min and max color in test input
            colors[4]  = expectedMin;
            colors[10] = expectedMax;

            // Add an arbitrary alpha value to some color
            ColorHelper.AddAlpha(ref colors[5]);

            var data = new BC1Format().Compress(colors);

            var color0 = Color565.FromValue((ushort)((data[1] << 8) | data[0]));
            var color1 = Color565.FromValue((ushort)((data[3] << 8) | data[2]));

            Assert.LessOrEqual(color0.Value, color1.Value);
            Assert.AreEqual(expectedColor0.Value, color0.Value);
            Assert.AreEqual(expectedColor1.Value, color1.Value);
        }
Esempio n. 6
0
        /// <summary>
        /// Instantiates a <see cref="BC3BlockData"/> from compressed BC3 block data.
        /// </summary>
        /// <param name="bytes">The data of a BC3 compressed block.</param>
        public static BC3BlockData FromBytes(byte[] bytes)
        {
            Debug.Assert(bytes.Length == BlockFormat.BC3ByteSize,
                         "Mismatching number of bytes for format.");

            byte alpha0 = bytes[0];
            byte alpha1 = bytes[1];

            byte[] alphas = new byte[6];
            alphas[2] = bytes[2];
            alphas[1] = bytes[3];
            alphas[0] = bytes[4];
            alphas[5] = bytes[5];
            alphas[4] = bytes[6];
            alphas[3] = bytes[7];
            byte c0Low = bytes[8];
            byte c0Hi  = bytes[9];
            byte c1Low = bytes[10];
            byte c1Hi  = bytes[11];

            byte[] indexes = new byte[4];
            indexes[0] = bytes[12];
            indexes[1] = bytes[13];
            indexes[2] = bytes[14];
            indexes[3] = bytes[15];

            var block = new BC3BlockData();

            block.Alpha0 = alpha0;
            block.Alpha1 = alpha1;
            block.Color0 = Color565.FromValue((ushort)((c0Hi << 8) | c0Low));
            block.Color1 = Color565.FromValue((ushort)((c1Hi << 8) | c1Low));

            block.AlphaIndexes[0] = alphas[0] & 0x07;               // xxxx x111
            block.AlphaIndexes[1] = (alphas[0] >> 3) & 0x07;        // xx11 1xxx
            block.AlphaIndexes[2] = ((alphas[0] >> 6) & 0x03) |     // 11xx xxxx LSB
                                    ((alphas[1] & 0x01) << 2);      // xxxx xxx1 MSB
            block.AlphaIndexes[3] = (alphas[1] >> 1) & 0x07;        // xxxx 111x
            block.AlphaIndexes[4] = (alphas[1] >> 4) & 0x07;        // x111 xxxx
            block.AlphaIndexes[5] = ((alphas[1] >> 7) & 0x01) |     // 1xxx xxxx LSB
                                    ((alphas[2] & 0x03) << 1);      // xxxx xx11 MSB
            block.AlphaIndexes[6]  = (alphas[2] >> 2) & 0x07;       // xxx1 11xx
            block.AlphaIndexes[7]  = (alphas[2] >> 5) & 0x07;       // 111x xxxx
            block.AlphaIndexes[8]  = alphas[3] & 0x07;              // xxxx x111
            block.AlphaIndexes[9]  = (alphas[3] >> 3) & 0x07;       // xx11 1xxx
            block.AlphaIndexes[10] = ((alphas[3] >> 6) & 0x03) |    // 11xx xxxx LSB
                                     ((alphas[4] & 0x01) << 2);     // xxxx xxx1 MSB
            block.AlphaIndexes[11] = (alphas[4] >> 1) & 0x07;       // xxxx 111x
            block.AlphaIndexes[12] = (alphas[4] >> 4) & 0x07;       // x111 xxxx
            block.AlphaIndexes[13] = ((alphas[4] >> 7) & 0x01) |    // 1xxx xxxx LSB
                                     ((alphas[5] & 0x03) << 1);     // xxxx xx11 MSB
            block.AlphaIndexes[14] = (alphas[5] >> 2) & 0x07;       // xxx1 11xx
            block.AlphaIndexes[15] = (alphas[5] >> 5) & 0x07;       // 111x xxxx

            for (int p = 0, row = 0; p < BlockFormat.TexelCount; p += BlockFormat.Dimension, ++row)
            {
                int a = p;
                int b = p + 1;
                int c = p + 2;
                int d = p + 3;

                block.ColorIndexes[a] = indexes[row] & 0x03;
                block.ColorIndexes[b] = (indexes[row] >> 2) & 0x03;
                block.ColorIndexes[c] = (indexes[row] >> 4) & 0x03;
                block.ColorIndexes[d] = (indexes[row] >> 6) & 0x03;
            }

            return(block);
        }
Esempio n. 7
0
 public Color565Helper(ushort value)
 {
     Color    = Color565.FromValue(value);
     LowByte  = (byte)(Color.Value & 0x00FF);
     HighByte = (byte)(Color.Value >> 8);
 }