示例#1
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);
        }
示例#2
0
        private static TexelWeightParams DecodeBlockInfo(BitArrayStream BitStream)
        {
            TexelWeightParams TexelParams = new TexelWeightParams();
            ushort            ModeBits    = (ushort)BitStream.ReadBits(11);

            // Does this match the void extent block mode?
            if ((ModeBits & 0x01FF) == 0x1FC)
            {
                if ((ModeBits & 0x200) != 0)
                {
                    TexelParams.VoidExtentHDR = true;
                }
                else
                {
                    TexelParams.VoidExtentLDR = true;
                }

                // Next two bits must be one.
                if ((ModeBits & 0x400) == 0 || BitStream.ReadBits(1) == 0)
                {
                    TexelParams.Error = true;
                }
                return(TexelParams);
            }

            // First check if the last four bits are zero
            if ((ModeBits & 0xF) == 0)
            {
                TexelParams.Error = true;
                return(TexelParams);
            }

            // If the last two bits are zero, then if bits
            // [6-8] are all ones, this is also reserved.
            if ((ModeBits & 0x3) == 0 && (ModeBits & 0x1C0) == 0x1C0)
            {
                TexelParams.Error = true;
                return(TexelParams);
            }

            // Otherwise, there is no error... Figure out the layout
            // of the block mode. Layout is determined by a number
            // between 0 and 9 corresponding to table C.2.8 of the
            // ASTC spec.
            int Layout;

            if ((ModeBits & 0x1) != 0 || (ModeBits & 0x2) != 0)
            {
                // layout is in [0-4]
                if ((ModeBits & 0x8) != 0)
                {
                    // layout is in [2-4]
                    if ((ModeBits & 0x4) != 0)
                    {
                        // layout is in [3-4]
                        if ((ModeBits & 0x100) != 0)
                        {
                            Layout = 4;
                        }
                        else
                        {
                            Layout = 3;
                        }
                    }
                    else
                    {
                        Layout = 2;
                    }
                }
                else
                {
                    // layout is in [0-1]
                    if ((ModeBits & 0x4) != 0)
                    {
                        Layout = 1;
                    }
                    else
                    {
                        Layout = 0;
                    }
                }
            }
            else
            {
                // layout is in [5-9]
                if ((ModeBits & 0x100) != 0)
                {
                    // layout is in [7-9]
                    if ((ModeBits & 0x80) != 0)
                    {
                        if ((ModeBits & 0x20) != 0)
                        {
                            Layout = 8;
                        }
                        else
                        {
                            Layout = 7;
                        }
                    }
                    else
                    {
                        Layout = 9;
                    }
                }
                else
                {
                    // layout is in [5-6]
                    if ((ModeBits & 0x80) != 0)
                    {
                        Layout = 6;
                    }
                    else
                    {
                        Layout = 5;
                    }
                }
            }

            // Determine R
            int R = (ModeBits >> 4) & 1;

            if (Layout < 5)
            {
                R |= (ModeBits & 0x3) << 1;
            }
            else
            {
                R |= (ModeBits & 0xC) >> 1;
            }

            // Determine width & height
            switch (Layout)
            {
            case 0:
            {
                int A = (ModeBits >> 5) & 0x3;
                int B = (ModeBits >> 7) & 0x3;
                TexelParams.Width  = B + 4;
                TexelParams.Height = A + 2;
                break;
            }

            case 1:
            {
                int A = (ModeBits >> 5) & 0x3;
                int B = (ModeBits >> 7) & 0x3;
                TexelParams.Width  = B + 8;
                TexelParams.Height = A + 2;
                break;
            }

            case 2:
            {
                int A = (ModeBits >> 5) & 0x3;
                int B = (ModeBits >> 7) & 0x3;
                TexelParams.Width  = A + 2;
                TexelParams.Height = B + 8;
                break;
            }

            case 3:
            {
                int A = (ModeBits >> 5) & 0x3;
                int B = (ModeBits >> 7) & 0x1;
                TexelParams.Width  = A + 2;
                TexelParams.Height = B + 6;
                break;
            }

            case 4:
            {
                int A = (ModeBits >> 5) & 0x3;
                int B = (ModeBits >> 7) & 0x1;
                TexelParams.Width  = B + 2;
                TexelParams.Height = A + 2;
                break;
            }

            case 5:
            {
                int A = (ModeBits >> 5) & 0x3;
                TexelParams.Width  = 12;
                TexelParams.Height = A + 2;
                break;
            }

            case 6:
            {
                int A = (ModeBits >> 5) & 0x3;
                TexelParams.Width  = A + 2;
                TexelParams.Height = 12;
                break;
            }

            case 7:
            {
                TexelParams.Width  = 6;
                TexelParams.Height = 10;
                break;
            }

            case 8:
            {
                TexelParams.Width  = 10;
                TexelParams.Height = 6;
                break;
            }

            case 9:
            {
                int A = (ModeBits >> 5) & 0x3;
                int B = (ModeBits >> 9) & 0x3;
                TexelParams.Width  = A + 6;
                TexelParams.Height = B + 6;
                break;
            }

            default:
                TexelParams.Error = true;
                break;
            }

            bool D = ((Layout != 9) && ((ModeBits & 0x400) != 0));
            bool H = (Layout != 9) && ((ModeBits & 0x200) != 0);

            if (H)
            {
                int[] MaxWeights = { 9, 11, 15, 19, 23, 31 };
                TexelParams.MaxWeight = MaxWeights[R - 2];
            }
            else
            {
                int[] MaxWeights = { 1, 2, 3, 4, 5, 7 };
                TexelParams.MaxWeight = MaxWeights[R - 2];
            }

            TexelParams.DualPlane = D;
            return(TexelParams);
        }
示例#3
0
        private static void UnquantizeTexelWeights(int[][] OutputBuffer, List <IntegerEncoded> Weights, TexelWeightParams TexelParams, int BlockWidth, int BlockHeight)
        {
            int WeightIndices = 0;

            int[][] Unquantized = new int[2][];
            Unquantized[0] = new int[144];
            Unquantized[1] = new int[144];

            for (int i = 0; i < Weights.Count; i++)
            {
                Unquantized[0][WeightIndices] = UnquantizeTexelWeight(Weights[i]);

                if (TexelParams.DualPlane)
                {
                    i++;
                    Unquantized[1][WeightIndices] = UnquantizeTexelWeight(Weights[i]);

                    if (i == Weights.Count)
                    {
                        break;
                    }
                }

                if (++WeightIndices >= (TexelParams.Width * TexelParams.Height))
                {
                    break;
                }
            }

            // Do infill if necessary (Section C.2.18) ...
            int Ds = (1024 + (BlockWidth / 2)) / (BlockWidth - 1);
            int Dt = (1024 + (BlockHeight / 2)) / (BlockHeight - 1);

            int PlaneScale = TexelParams.DualPlane ? 2 : 1;

            for (int Plane = 0; Plane < PlaneScale; Plane++)
            {
                for (int t = 0; t < BlockHeight; t++)
                {
                    for (int s = 0; s < BlockWidth; s++)
                    {
                        int cs = Ds * s;
                        int ct = Dt * t;

                        int gs = (cs * (TexelParams.Width - 1) + 32) >> 6;
                        int gt = (ct * (TexelParams.Height - 1) + 32) >> 6;

                        int js = gs >> 4;
                        int fs = gs & 0xF;

                        int jt = gt >> 4;
                        int ft = gt & 0x0F;

                        int w11 = (fs * ft + 8) >> 4;
                        int w10 = ft - w11;
                        int w01 = fs - w11;
                        int w00 = 16 - fs - ft + w11;

                        int v0 = js + jt * TexelParams.Width;

                        int p00 = 0;
                        int p01 = 0;
                        int p10 = 0;
                        int p11 = 0;

                        if (v0 < (TexelParams.Width * TexelParams.Height))
                        {
                            p00 = Unquantized[Plane][v0];
                        }

                        if (v0 + 1 < (TexelParams.Width * TexelParams.Height))
                        {
                            p01 = Unquantized[Plane][v0 + 1];
                        }

                        if (v0 + TexelParams.Width < (TexelParams.Width * TexelParams.Height))
                        {
                            p10 = Unquantized[Plane][v0 + TexelParams.Width];
                        }

                        if (v0 + TexelParams.Width + 1 < (TexelParams.Width * TexelParams.Height))
                        {
                            p11 = Unquantized[Plane][v0 + TexelParams.Width + 1];
                        }

                        OutputBuffer[Plane][t * BlockWidth + s] = (p00 * w00 + p01 * w01 + p10 * w10 + p11 * w11 + 8) >> 4;
                    }
                }
            }
        }