Beispiel #1
0
        public Color[] Decompress(byte[] blockData)
        {
            Debug.Assert(blockData.Length == BlockFormat.BC3ByteSize);

            var block = BC3BlockData.FromBytes(blockData);

            var colorTable = BC1ColorTable.Create(block.Color0, block.Color1);
            var alphaTable = CreateAlphaTable(block.Alpha0, block.Alpha1);

            var colors = new Color[BlockFormat.TexelCount];

            for (int i = 0; i < colors.Length; ++i)
            {
                int colorIndex = block.ColorIndexes[i];
                int alphaIndex = block.AlphaIndexes[i];

                var color = colorTable[colorIndex];
                var alpha = alphaTable[alphaIndex];

                colors[i] = Color.FromArgb(alpha, ColorUtility.To32Bit(color));
            }

            return(colors);
        }
        /// <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);
        }