Exemple #1
0
        public int Pack()
        {
            AstcPixel newPixel = new AstcPixel(A, R, G, B);

            byte[] eightBitDepth = { 8, 8, 8, 8 };

            newPixel.ChangeBitDepth(eightBitDepth);

            return((byte)newPixel.A << 24 |
                   (byte)newPixel.B << 16 |
                   (byte)newPixel.G << 8 |
                   (byte)newPixel.R << 0);
        }
Exemple #2
0
        static void ComputeEndpoints(
            AstcPixel[] endPoints,
            int[]       colorValues,
            uint colorEndpointMode,
            ref int colorValuesPosition)
        {
            switch (colorEndpointMode)
            {
            case 0:
            {
                uint[] val = ReadUintColorValues(2, colorValues, ref colorValuesPosition);

                endPoints[0] = new AstcPixel(0xFF, (short)val[0], (short)val[0], (short)val[0]);
                endPoints[1] = new AstcPixel(0xFF, (short)val[1], (short)val[1], (short)val[1]);

                break;
            }


            case 1:
            {
                uint[] val = ReadUintColorValues(2, colorValues, ref colorValuesPosition);
                int    l0  = (int)((val[0] >> 2) | (val[1] & 0xC0));
                int    l1  = (int)Math.Max(l0 + (val[1] & 0x3F), 0xFFU);

                endPoints[0] = new AstcPixel(0xFF, (short)l0, (short)l0, (short)l0);
                endPoints[1] = new AstcPixel(0xFF, (short)l1, (short)l1, (short)l1);

                break;
            }

            case 4:
            {
                uint[] val = ReadUintColorValues(4, colorValues, ref colorValuesPosition);

                endPoints[0] = new AstcPixel((short)val[2], (short)val[0], (short)val[0], (short)val[0]);
                endPoints[1] = new AstcPixel((short)val[3], (short)val[1], (short)val[1], (short)val[1]);

                break;
            }

            case 5:
            {
                int[] val = ReadIntColorValues(4, colorValues, ref colorValuesPosition);

                BitArrayStream.BitTransferSigned(ref val[1], ref val[0]);
                BitArrayStream.BitTransferSigned(ref val[3], ref val[2]);

                endPoints[0] = new AstcPixel((short)val[2], (short)val[0], (short)val[0], (short)val[0]);
                endPoints[1] = new AstcPixel((short)(val[2] + val[3]), (short)(val[0] + val[1]), (short)(val[0] + val[1]), (short)(val[0] + val[1]));

                endPoints[0].ClampByte();
                endPoints[1].ClampByte();

                break;
            }

            case 6:
            {
                uint[] val = ReadUintColorValues(4, colorValues, ref colorValuesPosition);

                endPoints[0] = new AstcPixel(0xFF, (short)(val[0] * val[3] >> 8), (short)(val[1] * val[3] >> 8), (short)(val[2] * val[3] >> 8));
                endPoints[1] = new AstcPixel(0xFF, (short)val[0], (short)val[1], (short)val[2]);

                break;
            }

            case 8:
            {
                uint[] val = ReadUintColorValues(6, colorValues, ref colorValuesPosition);

                if (val[1] + val[3] + val[5] >= val[0] + val[2] + val[4])
                {
                    endPoints[0] = new AstcPixel(0xFF, (short)val[0], (short)val[2], (short)val[4]);
                    endPoints[1] = new AstcPixel(0xFF, (short)val[1], (short)val[3], (short)val[5]);
                }
                else
                {
                    endPoints[0] = AstcPixel.BlueContract(0xFF, (short)val[1], (short)val[3], (short)val[5]);
                    endPoints[1] = AstcPixel.BlueContract(0xFF, (short)val[0], (short)val[2], (short)val[4]);
                }

                break;
            }

            case 9:
            {
                int[] val = ReadIntColorValues(6, colorValues, ref colorValuesPosition);

                BitArrayStream.BitTransferSigned(ref val[1], ref val[0]);
                BitArrayStream.BitTransferSigned(ref val[3], ref val[2]);
                BitArrayStream.BitTransferSigned(ref val[5], ref val[4]);

                if (val[1] + val[3] + val[5] >= 0)
                {
                    endPoints[0] = new AstcPixel(0xFF, (short)val[0], (short)val[2], (short)val[4]);
                    endPoints[1] = new AstcPixel(0xFF, (short)(val[0] + val[1]), (short)(val[2] + val[3]), (short)(val[4] + val[5]));
                }
                else
                {
                    endPoints[0] = AstcPixel.BlueContract(0xFF, val[0] + val[1], val[2] + val[3], val[4] + val[5]);
                    endPoints[1] = AstcPixel.BlueContract(0xFF, val[0], val[2], val[4]);
                }

                endPoints[0].ClampByte();
                endPoints[1].ClampByte();

                break;
            }

            case 10:
            {
                uint[] val = ReadUintColorValues(6, colorValues, ref colorValuesPosition);

                endPoints[0] = new AstcPixel((short)val[4], (short)(val[0] * val[3] >> 8), (short)(val[1] * val[3] >> 8), (short)(val[2] * val[3] >> 8));
                endPoints[1] = new AstcPixel((short)val[5], (short)val[0], (short)val[1], (short)val[2]);

                break;
            }

            case 12:
            {
                uint[] val = ReadUintColorValues(8, colorValues, ref colorValuesPosition);

                if (val[1] + val[3] + val[5] >= val[0] + val[2] + val[4])
                {
                    endPoints[0] = new AstcPixel((short)val[6], (short)val[0], (short)val[2], (short)val[4]);
                    endPoints[1] = new AstcPixel((short)val[7], (short)val[1], (short)val[3], (short)val[5]);
                }
                else
                {
                    endPoints[0] = AstcPixel.BlueContract((short)val[7], (short)val[1], (short)val[3], (short)val[5]);
                    endPoints[1] = AstcPixel.BlueContract((short)val[6], (short)val[0], (short)val[2], (short)val[4]);
                }

                break;
            }

            case 13:
            {
                int[] val = ReadIntColorValues(8, colorValues, ref colorValuesPosition);

                BitArrayStream.BitTransferSigned(ref val[1], ref val[0]);
                BitArrayStream.BitTransferSigned(ref val[3], ref val[2]);
                BitArrayStream.BitTransferSigned(ref val[5], ref val[4]);
                BitArrayStream.BitTransferSigned(ref val[7], ref val[6]);

                if (val[1] + val[3] + val[5] >= 0)
                {
                    endPoints[0] = new AstcPixel((short)val[6], (short)val[0], (short)val[2], (short)val[4]);
                    endPoints[1] = new AstcPixel((short)(val[7] + val[6]), (short)(val[0] + val[1]), (short)(val[2] + val[3]), (short)(val[4] + val[5]));
                }
                else
                {
                    endPoints[0] = AstcPixel.BlueContract(val[6] + val[7], val[0] + val[1], val[2] + val[3], val[4] + val[5]);
                    endPoints[1] = AstcPixel.BlueContract(val[6], val[0], val[2], val[4]);
                }

                endPoints[0].ClampByte();
                endPoints[1].ClampByte();

                break;
            }

            default:
                throw new AstcDecoderException("Unsupported color endpoint mode (is it HDR?)");
            }
        }
Exemple #3
0
        public static bool DecompressBlock(
            byte[] inputBuffer,
            int[]  outputBuffer,
            int blockWidth,
            int blockHeight)
        {
            BitArrayStream    bitStream   = new BitArrayStream(new BitArray(inputBuffer));
            TexelWeightParams texelParams = DecodeBlockInfo(bitStream);

            if (texelParams.Error)
            {
                throw new AstcDecoderException("Invalid block mode");
            }

            if (texelParams.VoidExtentLdr)
            {
                FillVoidExtentLdr(bitStream, outputBuffer, blockWidth, blockHeight);

                return(true);
            }

            if (texelParams.VoidExtentHdr)
            {
                throw new AstcDecoderException("HDR void extent blocks are unsupported!");
            }

            if (texelParams.Width > blockWidth)
            {
                throw new AstcDecoderException("Texel weight grid width should be smaller than block width");
            }

            if (texelParams.Height > blockHeight)
            {
                throw new AstcDecoderException("Texel weight grid height should be smaller than block height");
            }

            // Read num partitions
            int numberPartitions = bitStream.ReadBits(2) + 1;

            Debug.Assert(numberPartitions <= 4);

            if (numberPartitions == 4 && texelParams.DualPlane)
            {
                throw new AstcDecoderException("Dual plane mode is incompatible with four partition blocks");
            }

            // Based on the number of partitions, read the color endpoint mode for
            // each partition.

            // Determine partitions, partition index, and color endpoint modes
            int planeIndices = -1;
            int partitionIndex;

            uint[] colorEndpointMode = { 0, 0, 0, 0 };

            BitArrayStream colorEndpointStream = new BitArrayStream(new BitArray(16 * 8));

            // Read extra config data...
            uint baseColorEndpointMode = 0;

            if (numberPartitions == 1)
            {
                colorEndpointMode[0] = (uint)bitStream.ReadBits(4);
                partitionIndex       = 0;
            }
            else
            {
                partitionIndex        = bitStream.ReadBits(10);
                baseColorEndpointMode = (uint)bitStream.ReadBits(6);
            }

            uint baseMode = (baseColorEndpointMode & 3);

            // Remaining bits are color endpoint data...
            int numberWeightBits = texelParams.GetPackedBitSize();
            int remainingBits    = 128 - numberWeightBits - bitStream.Position;

            // Consider extra bits prior to texel data...
            uint extraColorEndpointModeBits = 0;

            if (baseMode != 0)
            {
                switch (numberPartitions)
                {
                case 2:  extraColorEndpointModeBits += 2; break;

                case 3:  extraColorEndpointModeBits += 5; break;

                case 4:  extraColorEndpointModeBits += 8; break;

                default: Debug.Assert(false); break;
                }
            }

            remainingBits -= (int)extraColorEndpointModeBits;

            // Do we have a dual plane situation?
            int planeSelectorBits = 0;

            if (texelParams.DualPlane)
            {
                planeSelectorBits = 2;
            }

            remainingBits -= planeSelectorBits;

            // Read color data...
            int colorDataBits = remainingBits;

            while (remainingBits > 0)
            {
                int numberBits = Math.Min(remainingBits, 8);
                int bits       = bitStream.ReadBits(numberBits);
                colorEndpointStream.WriteBits(bits, numberBits);
                remainingBits -= 8;
            }

            // Read the plane selection bits
            planeIndices = bitStream.ReadBits(planeSelectorBits);

            // Read the rest of the CEM
            if (baseMode != 0)
            {
                uint extraColorEndpointMode = (uint)bitStream.ReadBits((int)extraColorEndpointModeBits);
                uint tempColorEndpointMode  = (extraColorEndpointMode << 6) | baseColorEndpointMode;
                tempColorEndpointMode >>= 2;

                bool[] c = new bool[4];

                for (int i = 0; i < numberPartitions; i++)
                {
                    c[i] = (tempColorEndpointMode & 1) != 0;
                    tempColorEndpointMode >>= 1;
                }

                byte[] m = new byte[4];

                for (int i = 0; i < numberPartitions; i++)
                {
                    m[i] = (byte)(tempColorEndpointMode & 3);
                    tempColorEndpointMode >>= 2;
                    Debug.Assert(m[i] <= 3);
                }

                for (int i = 0; i < numberPartitions; i++)
                {
                    colorEndpointMode[i] = baseMode;
                    if (!(c[i]))
                    {
                        colorEndpointMode[i] -= 1;
                    }
                    colorEndpointMode[i] <<= 2;
                    colorEndpointMode[i]  |= m[i];
                }
            }
            else if (numberPartitions > 1)
            {
                uint tempColorEndpointMode = baseColorEndpointMode >> 2;

                for (uint i = 0; i < numberPartitions; i++)
                {
                    colorEndpointMode[i] = tempColorEndpointMode;
                }
            }

            // Make sure everything up till here is sane.
            for (int i = 0; i < numberPartitions; i++)
            {
                Debug.Assert(colorEndpointMode[i] < 16);
            }
            Debug.Assert(bitStream.Position + texelParams.GetPackedBitSize() == 128);

            // Decode both color data and texel weight data
            int[] colorValues = new int[32]; // Four values * two endpoints * four maximum partitions
            DecodeColorValues(colorValues, colorEndpointStream.ToByteArray(), colorEndpointMode, numberPartitions, colorDataBits);

            AstcPixel[][] endPoints = new AstcPixel[4][];
            endPoints[0] = new AstcPixel[2];
            endPoints[1] = new AstcPixel[2];
            endPoints[2] = new AstcPixel[2];
            endPoints[3] = new AstcPixel[2];

            int colorValuesPosition = 0;

            for (int i = 0; i < numberPartitions; i++)
            {
                ComputeEndpoints(endPoints[i], colorValues, colorEndpointMode[i], ref colorValuesPosition);
            }

            // Read the texel weight data.
            byte[] texelWeightData = (byte[])inputBuffer.Clone();

            // Reverse everything
            for (int i = 0; i < 8; i++)
            {
                byte a = ReverseByte(texelWeightData[i]);
                byte b = ReverseByte(texelWeightData[15 - i]);

                texelWeightData[i]      = b;
                texelWeightData[15 - i] = a;
            }

            // Make sure that higher non-texel bits are set to zero
            int clearByteStart = (texelParams.GetPackedBitSize() >> 3) + 1;

            texelWeightData[clearByteStart - 1] &= (byte)((1 << (texelParams.GetPackedBitSize() % 8)) - 1);

            int cLen = 16 - clearByteStart;

            for (int i = clearByteStart; i < clearByteStart + cLen; i++)
            {
                texelWeightData[i] = 0;
            }

            List <IntegerEncoded> texelWeightValues = new List <IntegerEncoded>();
            BitArrayStream        weightBitStream   = new BitArrayStream(new BitArray(texelWeightData));

            IntegerEncoded.DecodeIntegerSequence(texelWeightValues, weightBitStream, texelParams.MaxWeight, texelParams.GetNumWeightValues());

            // Blocks can be at most 12x12, so we can have as many as 144 weights
            int[][] weights = new int[2][];
            weights[0] = new int[144];
            weights[1] = new int[144];

            UnquantizeTexelWeights(weights, texelWeightValues, texelParams, blockWidth, blockHeight);

            // Now that we have endpoints and weights, we can interpolate and generate
            // the proper decoding...
            for (int j = 0; j < blockHeight; j++)
            {
                for (int i = 0; i < blockWidth; i++)
                {
                    int partition = Select2dPartition(partitionIndex, i, j, numberPartitions, ((blockHeight * blockWidth) < 32));
                    Debug.Assert(partition < numberPartitions);

                    AstcPixel pixel = new AstcPixel(0, 0, 0, 0);
                    for (int component = 0; component < 4; component++)
                    {
                        int component0 = endPoints[partition][0].GetComponent(component);
                        component0 = BitArrayStream.Replicate(component0, 8, 16);
                        int component1 = endPoints[partition][1].GetComponent(component);
                        component1 = BitArrayStream.Replicate(component1, 8, 16);

                        int plane = 0;

                        if (texelParams.DualPlane && (((planeIndices + 1) & 3) == component))
                        {
                            plane = 1;
                        }

                        int weight         = weights[plane][j * blockWidth + i];
                        int finalComponent = (component0 * (64 - weight) + component1 * weight + 32) / 64;

                        if (finalComponent == 65535)
                        {
                            pixel.SetComponent(component, 255);
                        }
                        else
                        {
                            double finalComponentFloat = finalComponent;
                            pixel.SetComponent(component, (int)(255.0 * (finalComponentFloat / 65536.0) + 0.5));
                        }
                    }

                    outputBuffer[j * blockWidth + i] = pixel.Pack();
                }
            }

            return(true);
        }