예제 #1
0
        private 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 Exception("Unsupported color endpoint mode (is it HDR?)");
            }
        }
예제 #2
0
        private 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 Exception("Invalid block mode");
            }

            if (TexelParams.VoidExtentLDR)
            {
                FillVoidExtentLDR(BitStream, OutputBuffer, BlockWidth, BlockHeight);
                return(true);
            }

            if (TexelParams.VoidExtentHDR)
            {
                throw new Exception("HDR void extent blocks are unsupported!");
            }

            if (TexelParams.Width > BlockWidth)
            {
                throw new Exception("Texel weight grid width should be smaller than block width");
            }

            if (TexelParams.Height > BlockHeight)
            {
                throw new Exception("Texel weight grid height should be smaller than block height");
            }

            // Read num partitions
            int NumberPartitions = BitStream.ReadBits(2) + 1;

            if (NumberPartitions == 4 && TexelParams.DualPlane)
            {
                throw new Exception("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 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: 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
            int 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;
                }

                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;
                }
            }

            // 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));

                    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);
        }