Beispiel #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);
        }
Beispiel #2
0
        public byte[] Compress(Color[] colors)
        {
            Debug.Assert(colors.Length == BlockFormat.TexelCount);

            var colorTable = CreateColorTable(colors);

            var block = new BC1BlockData();

            block.Color0 = colorTable[0];
            block.Color1 = colorTable[1];

            for (int i = 0; i < colors.Length; ++i)
            {
                var color32 = colors[i];

                // If color has alpha, use a specific index
                // to identify the color when decompressed
                block.ColorIndexes[i] = color32.HasAlpha() ? AlphaColorIndex :
                                        ColorUtility.GetIndexOfClosestColor(colorTable, ColorUtility.To16Bit(color32));
            }

            return(block.ToBytes());
        }
Beispiel #3
0
        public Color[] Decompress(byte[] blockData)
        {
            Debug.Assert(blockData.Length == BlockFormat.BC1ByteSize);

            var block = BC1BlockData.FromBytes(blockData);

            var colorTable  = CreateColorTable(block.Color0, block.Color1);
            var is1BitAlpha = colorTable[0].Value <= colorTable[1].Value;

            var colors = new Color[BlockFormat.TexelCount];

            for (int i = 0; i < colors.Length; ++i)
            {
                int index = block.ColorIndexes[i];
                int alpha = is1BitAlpha && index == AlphaColorIndex ? 0 : 255;

                var color16 = colorTable[index];
                var color32 = Color.FromArgb(alpha, ColorUtility.To32Bit(color16));

                colors[i] = color32;
            }

            return(colors);
        }