Ejemplo n.º 1
0
        public Color[] Decompress(byte[] blockData)
        {
            Debug.Assert(blockData.Length == BlockFormat.BC2ByteSize);

            var block = BC2BlockData.FromBytes(blockData);

            var colorTable = BC1ColorTable.Create(block.Color0, block.Color1);

            var colors = new Color[BlockFormat.TexelCount];

            for (int i = 0; i < colors.Length; ++i)
            {
                int index = block.ColorIndexes[i];
                int alpha = block.ColorAlphas[i];

                // Convert 4-bit alpha to 8-bit by shifting the bits left and ORing
                // with the most significant bits to calculate the remaining values
                alpha = (alpha << 4) | (alpha & 0x0F);

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

                colors[i] = color32;
            }

            return(colors);
        }
Ejemplo n.º 2
0
        public byte[] Compress(Color[] colors)
        {
            Debug.Assert(colors.Length == BlockFormat.TexelCount);

            var colorSpace = new ColorSpace(colors);
            var colorTable = BC1ColorTable.Create(colorSpace.MaxColor, colorSpace.MinColor);

            // Interpolate 6 alpha values by passing max alpha as alpha0
            var alphaTable = CreateAlphaTable(colorSpace.MaxAlpha, colorSpace.MinAlpha);

            var block = new BC3BlockData();

            block.Color0 = colorTable[0];
            block.Color1 = colorTable[1];
            block.Alpha0 = alphaTable[0];
            block.Alpha1 = alphaTable[1];

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

                block.ColorIndexes[i] = ColorUtility.GetIndexOfClosestColor(colorTable, color16);
                block.AlphaIndexes[i] = ColorUtility.GetIndexOfClosestAlpha(alphaTable, color32.A);
            }

            return(block.ToBytes());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a BC1 color table from the 32-bit colors of a block. If the color space
        /// contains alpha values, the color table is built according to BC1 1-bit alpha
        /// specification.
        /// </summary>
        private static Color565[] CreateColorTable(Color[] colors)
        {
            var colorSpace = new ColorSpace(colors);

            return(colorSpace.HasAlpha?
                   BC1ColorTable.CreateFor1BitAlpha(colorSpace.MinColor, colorSpace.MaxColor) :
                   BC1ColorTable.Create(colorSpace.MaxColor, colorSpace.MinColor));
        }
Ejemplo n.º 4
0
        public byte[] Compress(Color[] colors)
        {
            Debug.Assert(colors.Length == BlockFormat.TexelCount);

            var colorSpace = new ColorSpace(colors);
            var colorTable = BC1ColorTable.Create(colorSpace.MaxColor, colorSpace.MinColor);

            var block = new BC2BlockData();

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

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

                block.ColorIndexes[i] = ColorUtility.GetIndexOfClosestColor(colorTable, color16);
                block.ColorAlphas[i]  = color32.A >> 4; // Convert 8-bit alpha to 4-bit
            }

            return(block.ToBytes());
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Creates a BC1 color table from two compressed 16-bit reference colors.
 /// If the integer value of <paramref name="color0"/> is lower or equal to
 /// that of <paramref name="color1"/>, the color table is built according
 /// to the 1-bit alpha specification.
 /// </summary>
 private static Color565[] CreateColorTable(Color565 color0, Color565 color1)
 {
     return(color0.Value <= color1.Value ?
            BC1ColorTable.CreateFor1BitAlpha(color0, color1) :
            BC1ColorTable.Create(color0, color1));
 }