Beispiel #1
0
        public static IntegerEncoded CreateEncoding(int maxVal)
        {
            while (maxVal > 0)
            {
                int check = maxVal + 1;

                // Is maxVal a power of two?
                if ((check & (check - 1)) == 0)
                {
                    return(new IntegerEncoded(EIntegerEncoding.JustBits, BitArrayStream.PopCnt(maxVal)));
                }

                // Is maxVal of the type 3*2^n - 1?
                if ((check % 3 == 0) && ((check / 3) & ((check / 3) - 1)) == 0)
                {
                    return(new IntegerEncoded(EIntegerEncoding.Trit, BitArrayStream.PopCnt(check / 3 - 1)));
                }

                // Is maxVal of the type 5*2^n - 1?
                if ((check % 5 == 0) && ((check / 5) & ((check / 5) - 1)) == 0)
                {
                    return(new IntegerEncoded(EIntegerEncoding.Quint, BitArrayStream.PopCnt(check / 5 - 1)));
                }

                // Apparently it can't be represented with a bounded integer sequence...
                // just iterate.
                maxVal--;
            }

            return(new IntegerEncoded(EIntegerEncoding.JustBits, 0));
        }
Beispiel #2
0
        public static void DecodeIntegerSequence(
            List <IntegerEncoded> decodeIntegerSequence,
            BitArrayStream bitStream,
            int maxRange,
            int numberValues)
        {
            // Determine encoding parameters
            IntegerEncoded intEncoded = CreateEncoding(maxRange);

            // Start decoding
            int numberValuesDecoded = 0;

            while (numberValuesDecoded < numberValues)
            {
                switch (intEncoded.GetEncoding())
                {
                case EIntegerEncoding.Quint:
                {
                    DecodeQuintBlock(bitStream, decodeIntegerSequence, intEncoded.NumberBits);
                    numberValuesDecoded += 3;

                    break;
                }

                case EIntegerEncoding.Trit:
                {
                    DecodeTritBlock(bitStream, decodeIntegerSequence, intEncoded.NumberBits);
                    numberValuesDecoded += 5;

                    break;
                }

                case EIntegerEncoding.JustBits:
                {
                    intEncoded.BitValue = bitStream.ReadBits(intEncoded.NumberBits);
                    decodeIntegerSequence.Add(intEncoded);
                    numberValuesDecoded++;

                    break;
                }
                }
            }
        }
Beispiel #3
0
        public static void DecodeIntegerSequence(
            List <IntegerEncoded> DecodeIntegerSequence,
            BitArrayStream BitStream,
            int MaxRange,
            int NumberValues)
        {
            // Determine encoding parameters
            IntegerEncoded IntEncoded = CreateEncoding(MaxRange);

            // Start decoding
            int NumberValuesDecoded = 0;

            while (NumberValuesDecoded < NumberValues)
            {
                switch (IntEncoded.GetEncoding())
                {
                case EIntegerEncoding.Quint:
                {
                    DecodeQuintBlock(BitStream, DecodeIntegerSequence, IntEncoded.NumberBits);
                    NumberValuesDecoded += 3;

                    break;
                }

                case EIntegerEncoding.Trit:
                {
                    DecodeTritBlock(BitStream, DecodeIntegerSequence, IntEncoded.NumberBits);
                    NumberValuesDecoded += 5;

                    break;
                }

                case EIntegerEncoding.JustBits:
                {
                    IntEncoded.BitValue = BitStream.ReadBits(IntEncoded.NumberBits);
                    DecodeIntegerSequence.Add(IntEncoded);
                    NumberValuesDecoded++;

                    break;
                }
                }
            }
        }
Beispiel #4
0
        short ChangeBitDepth(short value, byte oldDepth, byte newDepth)
        {
            Debug.Assert(newDepth <= 8);
            Debug.Assert(oldDepth <= 8);

            if (oldDepth == newDepth)
            {
                // Do nothing
                return(value);
            }
            else if (oldDepth == 0 && newDepth != 0)
            {
                return((short)((1 << newDepth) - 1));
            }
            else if (newDepth > oldDepth)
            {
                return((short)BitArrayStream.Replicate(value, oldDepth, newDepth));
            }
            else
            {
                // oldDepth > newDepth
                if (newDepth == 0)
                {
                    return(0xFF);
                }
                else
                {
                    byte  bitsWasted = (byte)(oldDepth - newDepth);
                    short tempValue  = value;

                    tempValue = (short)((tempValue + (1 << (bitsWasted - 1))) >> bitsWasted);
                    tempValue = Math.Min(Math.Max((short)0, tempValue), (short)((1 << newDepth) - 1));

                    return((byte)(tempValue));
                }
            }
        }
Beispiel #5
0
        short ChangeBitDepth(short Value, byte OldDepth, byte NewDepth)
        {
            Debug.Assert(NewDepth <= 8);
            Debug.Assert(OldDepth <= 8);

            if (OldDepth == NewDepth)
            {
                // Do nothing
                return(Value);
            }
            else if (OldDepth == 0 && NewDepth != 0)
            {
                return((short)((1 << NewDepth) - 1));
            }
            else if (NewDepth > OldDepth)
            {
                return((short)BitArrayStream.Replicate(Value, OldDepth, NewDepth));
            }
            else
            {
                // oldDepth > newDepth
                if (NewDepth == 0)
                {
                    return(0xFF);
                }
                else
                {
                    byte  BitsWasted = (byte)(OldDepth - NewDepth);
                    short TempValue  = Value;

                    TempValue = (short)((TempValue + (1 << (BitsWasted - 1))) >> BitsWasted);
                    TempValue = Math.Min(Math.Max((short)0, TempValue), (short)((1 << NewDepth) - 1));

                    return((byte)(TempValue));
                }
            }
        }
Beispiel #6
0
        static void FillVoidExtentLdr(BitArrayStream bitStream, int[] outputBuffer, int blockWidth, int blockHeight)
        {
            // Don't actually care about the void extent, just read the bits...
            for (int i = 0; i < 4; ++i)
            {
                bitStream.ReadBits(13);
            }

            // Decode the RGBA components and renormalize them to the range [0, 255]
            ushort r = (ushort)bitStream.ReadBits(16);
            ushort g = (ushort)bitStream.ReadBits(16);
            ushort b = (ushort)bitStream.ReadBits(16);
            ushort a = (ushort)bitStream.ReadBits(16);

            int rgba = (r >> 8) | (g & 0xFF00) | ((b) & 0xFF00) << 8 | ((a) & 0xFF00) << 16;

            for (int j = 0; j < blockHeight; j++)
            {
                for (int i = 0; i < blockWidth; i++)
                {
                    outputBuffer[j * blockWidth + i] = rgba;
                }
            }
        }
Beispiel #7
0
        static void FillVoidExtentLDR(BitArrayStream BitStream, int[] OutputBuffer, int BlockWidth, int BlockHeight)
        {
            // Don't actually care about the void extent, just read the bits...
            for (int i = 0; i < 4; ++i)
            {
                BitStream.ReadBits(13);
            }

            // Decode the RGBA components and renormalize them to the range [0, 255]
            ushort R = (ushort)BitStream.ReadBits(16);
            ushort G = (ushort)BitStream.ReadBits(16);
            ushort B = (ushort)BitStream.ReadBits(16);
            ushort A = (ushort)BitStream.ReadBits(16);

            int RGBA = (R >> 8) | (G & 0xFF00) | ((B) & 0xFF00) << 8 | ((A) & 0xFF00) << 16;

            for (int j = 0; j < BlockHeight; j++)
            {
                for (int i = 0; i < BlockWidth; i++)
                {
                    OutputBuffer[j * BlockWidth + i] = RGBA;
                }
            }
        }
Beispiel #8
0
        public static void DecodeTritBlock(
            BitArrayStream BitStream,
            List <IntegerEncoded> ListIntegerEncoded,
            int NumberBitsPerValue)
        {
            // Implement the algorithm in section C.2.12
            int[] m = new int[5];
            int[] t = new int[5];
            int   T;

            // Read the trit encoded block according to
            // table C.2.14
            m[0] = BitStream.ReadBits(NumberBitsPerValue);
            T    = BitStream.ReadBits(2);
            m[1] = BitStream.ReadBits(NumberBitsPerValue);
            T   |= BitStream.ReadBits(2) << 2;
            m[2] = BitStream.ReadBits(NumberBitsPerValue);
            T   |= BitStream.ReadBits(1) << 4;
            m[3] = BitStream.ReadBits(NumberBitsPerValue);
            T   |= BitStream.ReadBits(2) << 5;
            m[4] = BitStream.ReadBits(NumberBitsPerValue);
            T   |= BitStream.ReadBits(1) << 7;

            int C = 0;

            BitArrayStream Tb = new BitArrayStream(new BitArray(new int[] { T }));

            if (Tb.ReadBits(2, 4) == 7)
            {
                C    = (Tb.ReadBits(5, 7) << 2) | Tb.ReadBits(0, 1);
                t[4] = t[3] = 2;
            }
            else
            {
                C = Tb.ReadBits(0, 4);
                if (Tb.ReadBits(5, 6) == 3)
                {
                    t[4] = 2;
                    t[3] = Tb.ReadBit(7);
                }
                else
                {
                    t[4] = Tb.ReadBit(7);
                    t[3] = Tb.ReadBits(5, 6);
                }
            }

            BitArrayStream Cb = new BitArrayStream(new BitArray(new int[] { C }));

            if (Cb.ReadBits(0, 1) == 3)
            {
                t[2] = 2;
                t[1] = Cb.ReadBit(4);
                t[0] = (Cb.ReadBit(3) << 1) | (Cb.ReadBit(2) & ~Cb.ReadBit(3));
            }
            else if (Cb.ReadBits(2, 3) == 3)
            {
                t[2] = 2;
                t[1] = 2;
                t[0] = Cb.ReadBits(0, 1);
            }
            else
            {
                t[2] = Cb.ReadBit(4);
                t[1] = Cb.ReadBits(2, 3);
                t[0] = (Cb.ReadBit(1) << 1) | (Cb.ReadBit(0) & ~Cb.ReadBit(1));
            }

            for (int i = 0; i < 5; i++)
            {
                IntegerEncoded IntEncoded = new IntegerEncoded(EIntegerEncoding.Trit, NumberBitsPerValue)
                {
                    BitValue  = m[i],
                    TritValue = t[i]
                };
                ListIntegerEncoded.Add(IntEncoded);
            }
        }
Beispiel #9
0
        public static void DecodeTritBlock(
            BitArrayStream bitStream,
            List <IntegerEncoded> listIntegerEncoded,
            int numberBitsPerValue)
        {
            // Implement the algorithm in section C.2.12
            int[] m = new int[5];
            int[] t = new int[5];
            int   T;

            // Read the trit encoded block according to
            // table C.2.14
            m[0] = bitStream.ReadBits(numberBitsPerValue);
            T    = bitStream.ReadBits(2);
            m[1] = bitStream.ReadBits(numberBitsPerValue);
            T   |= bitStream.ReadBits(2) << 2;
            m[2] = bitStream.ReadBits(numberBitsPerValue);
            T   |= bitStream.ReadBits(1) << 4;
            m[3] = bitStream.ReadBits(numberBitsPerValue);
            T   |= bitStream.ReadBits(2) << 5;
            m[4] = bitStream.ReadBits(numberBitsPerValue);
            T   |= bitStream.ReadBits(1) << 7;

            int c = 0;

            BitArrayStream tb = new BitArrayStream(new BitArray(new int[] { T }));

            if (tb.ReadBits(2, 4) == 7)
            {
                c    = (tb.ReadBits(5, 7) << 2) | tb.ReadBits(0, 1);
                t[4] = t[3] = 2;
            }
            else
            {
                c = tb.ReadBits(0, 4);
                if (tb.ReadBits(5, 6) == 3)
                {
                    t[4] = 2;
                    t[3] = tb.ReadBit(7);
                }
                else
                {
                    t[4] = tb.ReadBit(7);
                    t[3] = tb.ReadBits(5, 6);
                }
            }

            BitArrayStream cb = new BitArrayStream(new BitArray(new int[] { c }));

            if (cb.ReadBits(0, 1) == 3)
            {
                t[2] = 2;
                t[1] = cb.ReadBit(4);
                t[0] = (cb.ReadBit(3) << 1) | (cb.ReadBit(2) & ~cb.ReadBit(3));
            }
            else if (cb.ReadBits(2, 3) == 3)
            {
                t[2] = 2;
                t[1] = 2;
                t[0] = cb.ReadBits(0, 1);
            }
            else
            {
                t[2] = cb.ReadBit(4);
                t[1] = cb.ReadBits(2, 3);
                t[0] = (cb.ReadBit(1) << 1) | (cb.ReadBit(0) & ~cb.ReadBit(1));
            }

            for (int i = 0; i < 5; i++)
            {
                IntegerEncoded intEncoded = new IntegerEncoded(EIntegerEncoding.Trit, numberBitsPerValue)
                {
                    BitValue  = m[i],
                    TritValue = t[i]
                };
                listIntegerEncoded.Add(intEncoded);
            }
        }
Beispiel #10
0
        public static void DecodeQuintBlock(
            BitArrayStream bitStream,
            List <IntegerEncoded> listIntegerEncoded,
            int numberBitsPerValue)
        {
            // Implement the algorithm in section C.2.12
            int[] m  = new int[3];
            int[] qa = new int[3];
            int   q;

            // Read the trit encoded block according to
            // table C.2.15
            m[0] = bitStream.ReadBits(numberBitsPerValue);
            q    = bitStream.ReadBits(3);
            m[1] = bitStream.ReadBits(numberBitsPerValue);
            q   |= bitStream.ReadBits(2) << 3;
            m[2] = bitStream.ReadBits(numberBitsPerValue);
            q   |= bitStream.ReadBits(2) << 5;

            BitArrayStream qb = new BitArrayStream(new BitArray(new int[] { q }));

            if (qb.ReadBits(1, 2) == 3 && qb.ReadBits(5, 6) == 0)
            {
                qa[0] = qa[1] = 4;
                qa[2] = (qb.ReadBit(0) << 2) | ((qb.ReadBit(4) & ~qb.ReadBit(0)) << 1) | (qb.ReadBit(3) & ~qb.ReadBit(0));
            }
            else
            {
                int c = 0;
                if (qb.ReadBits(1, 2) == 3)
                {
                    qa[2] = 4;
                    c     = (qb.ReadBits(3, 4) << 3) | ((~qb.ReadBits(5, 6) & 3) << 1) | qb.ReadBit(0);
                }
                else
                {
                    qa[2] = qb.ReadBits(5, 6);
                    c     = qb.ReadBits(0, 4);
                }

                BitArrayStream cb = new BitArrayStream(new BitArray(new int[] { c }));
                if (cb.ReadBits(0, 2) == 5)
                {
                    qa[1] = 4;
                    qa[0] = cb.ReadBits(3, 4);
                }
                else
                {
                    qa[1] = cb.ReadBits(3, 4);
                    qa[0] = cb.ReadBits(0, 2);
                }
            }

            for (int i = 0; i < 3; i++)
            {
                IntegerEncoded intEncoded = new IntegerEncoded(EIntegerEncoding.Quint, numberBitsPerValue)
                {
                    BitValue   = m[i],
                    QuintValue = qa[i]
                };
                listIntegerEncoded.Add(intEncoded);
            }
        }
Beispiel #11
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);
        }
Beispiel #12
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?)");
            }
        }
Beispiel #13
0
        static void DecodeColorValues(
            int[]  OutputValues,
            byte[] InputData,
            uint[] Modes,
            int NumberPartitions,
            int NumberBitsForColorData)
        {
            // First figure out how many color values we have
            int NumberValues = 0;

            for (int i = 0; i < NumberPartitions; i++)
            {
                NumberValues += (int)((Modes[i] >> 2) + 1) << 1;
            }

            // Then based on the number of values and the remaining number of bits,
            // figure out the max value for each of them...
            int Range = 256;

            while (--Range > 0)
            {
                IntegerEncoded IntEncoded = IntegerEncoded.CreateEncoding(Range);
                int            BitLength  = IntEncoded.GetBitLength(NumberValues);

                if (BitLength <= NumberBitsForColorData)
                {
                    // Find the smallest possible range that matches the given encoding
                    while (--Range > 0)
                    {
                        IntegerEncoded NewIntEncoded = IntegerEncoded.CreateEncoding(Range);
                        if (!NewIntEncoded.MatchesEncoding(IntEncoded))
                        {
                            break;
                        }
                    }

                    // Return to last matching range.
                    Range++;
                    break;
                }
            }

            // We now have enough to decode our integer sequence.
            List <IntegerEncoded> IntegerEncodedSequence = new List <IntegerEncoded>();
            BitArrayStream        ColorBitStream         = new BitArrayStream(new BitArray(InputData));

            IntegerEncoded.DecodeIntegerSequence(IntegerEncodedSequence, ColorBitStream, Range, NumberValues);

            // Once we have the decoded values, we need to dequantize them to the 0-255 range
            // This procedure is outlined in ASTC spec C.2.13
            int OutputIndices = 0;

            foreach (IntegerEncoded IntEncoded in IntegerEncodedSequence)
            {
                int BitLength = IntEncoded.NumberBits;
                int BitValue  = IntEncoded.BitValue;

                Debug.Assert(BitLength >= 1);

                int A = 0, B = 0, C = 0, D = 0;
                // A is just the lsb replicated 9 times.
                A = BitArrayStream.Replicate(BitValue & 1, 1, 9);

                switch (IntEncoded.GetEncoding())
                {
                case IntegerEncoded.EIntegerEncoding.JustBits:
                {
                    OutputValues[OutputIndices++] = BitArrayStream.Replicate(BitValue, BitLength, 8);

                    break;
                }

                case IntegerEncoded.EIntegerEncoding.Trit:
                {
                    D = IntEncoded.TritValue;

                    switch (BitLength)
                    {
                    case 1:
                    {
                        C = 204;

                        break;
                    }

                    case 2:
                    {
                        C = 93;
                        // B = b000b0bb0
                        int b = (BitValue >> 1) & 1;
                        B = (b << 8) | (b << 4) | (b << 2) | (b << 1);

                        break;
                    }

                    case 3:
                    {
                        C = 44;
                        // B = cb000cbcb
                        int cb = (BitValue >> 1) & 3;
                        B = (cb << 7) | (cb << 2) | cb;

                        break;
                    }


                    case 4:
                    {
                        C = 22;
                        // B = dcb000dcb
                        int dcb = (BitValue >> 1) & 7;
                        B = (dcb << 6) | dcb;

                        break;
                    }

                    case 5:
                    {
                        C = 11;
                        // B = edcb000ed
                        int edcb = (BitValue >> 1) & 0xF;
                        B = (edcb << 5) | (edcb >> 2);

                        break;
                    }

                    case 6:
                    {
                        C = 5;
                        // B = fedcb000f
                        int fedcb = (BitValue >> 1) & 0x1F;
                        B = (fedcb << 4) | (fedcb >> 4);

                        break;
                    }

                    default:
                        throw new ASTCDecoderException("Unsupported trit encoding for color values!");
                    }

                    break;
                }

                case IntegerEncoded.EIntegerEncoding.Quint:
                {
                    D = IntEncoded.QuintValue;

                    switch (BitLength)
                    {
                    case 1:
                    {
                        C = 113;

                        break;
                    }

                    case 2:
                    {
                        C = 54;
                        // B = b0000bb00
                        int b = (BitValue >> 1) & 1;
                        B = (b << 8) | (b << 3) | (b << 2);

                        break;
                    }

                    case 3:
                    {
                        C = 26;
                        // B = cb0000cbc
                        int cb = (BitValue >> 1) & 3;
                        B = (cb << 7) | (cb << 1) | (cb >> 1);

                        break;
                    }

                    case 4:
                    {
                        C = 13;
                        // B = dcb0000dc
                        int dcb = (BitValue >> 1) & 7;
                        B = (dcb << 6) | (dcb >> 1);

                        break;
                    }

                    case 5:
                    {
                        C = 6;
                        // B = edcb0000e
                        int edcb = (BitValue >> 1) & 0xF;
                        B = (edcb << 5) | (edcb >> 3);

                        break;
                    }

                    default:
                        throw new ASTCDecoderException("Unsupported quint encoding for color values!");
                    }
                    break;
                }
                }

                if (IntEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits)
                {
                    int T = D * C + B;
                    T ^= A;
                    T  = (A & 0x80) | (T >> 2);

                    OutputValues[OutputIndices++] = T;
                }
            }

            // Make sure that each of our values is in the proper range...
            for (int i = 0; i < NumberValues; i++)
            {
                Debug.Assert(OutputValues[i] <= 255);
            }
        }
Beispiel #14
0
        static TexelWeightParams DecodeBlockInfo(BitArrayStream BitStream)
        {
            TexelWeightParams TexelParams = new TexelWeightParams();

            // Read the entire block mode all at once
            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 = 0;

            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)
                    {
                        // layout is in [7-8]
                        Debug.Assert((ModeBits & 0x40) == 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;
                    }
                }
            }

            Debug.Assert(Layout < 10);

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

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

            Debug.Assert(2 <= R && R <= 7);

            // 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:
                //Don't know this layout...
                TexelParams.Error = true;
                break;
            }

            // Determine whether or not we're using dual planes
            // and/or high precision layouts.
            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);
        }
Beispiel #15
0
        static int UnquantizeTexelWeight(IntegerEncoded IntEncoded)
        {
            int BitValue  = IntEncoded.BitValue;
            int BitLength = IntEncoded.NumberBits;

            int A = BitArrayStream.Replicate(BitValue & 1, 1, 7);
            int B = 0, C = 0, D = 0;

            int Result = 0;

            switch (IntEncoded.GetEncoding())
            {
            case IntegerEncoded.EIntegerEncoding.JustBits:
                Result = BitArrayStream.Replicate(BitValue, BitLength, 6);
                break;

            case IntegerEncoded.EIntegerEncoding.Trit:
            {
                D = IntEncoded.TritValue;
                Debug.Assert(D < 3);

                switch (BitLength)
                {
                case 0:
                {
                    int[] Results = { 0, 32, 63 };
                    Result = Results[D];

                    break;
                }

                case 1:
                {
                    C = 50;
                    break;
                }

                case 2:
                {
                    C = 23;
                    int b = (BitValue >> 1) & 1;
                    B = (b << 6) | (b << 2) | b;

                    break;
                }

                case 3:
                {
                    C = 11;
                    int cb = (BitValue >> 1) & 3;
                    B = (cb << 5) | cb;

                    break;
                }

                default:
                    throw new ASTCDecoderException("Invalid trit encoding for texel weight");
                }

                break;
            }

            case IntegerEncoded.EIntegerEncoding.Quint:
            {
                D = IntEncoded.QuintValue;
                Debug.Assert(D < 5);

                switch (BitLength)
                {
                case 0:
                {
                    int[] Results = { 0, 16, 32, 47, 63 };
                    Result = Results[D];

                    break;
                }

                case 1:
                {
                    C = 28;

                    break;
                }

                case 2:
                {
                    C = 13;
                    int b = (BitValue >> 1) & 1;
                    B = (b << 6) | (b << 1);

                    break;
                }

                default:
                    throw new ASTCDecoderException("Invalid quint encoding for texel weight");
                }

                break;
            }
            }

            if (IntEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits && BitLength > 0)
            {
                // Decode the value...
                Result  = D * C + B;
                Result ^= A;
                Result  = (A & 0x20) | (Result >> 2);
            }

            Debug.Assert(Result < 64);

            // Change from [0,63] to [0,64]
            if (Result > 32)
            {
                Result += 1;
            }

            return(Result);
        }
Beispiel #16
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);
        }
Beispiel #17
0
        public static void DecodeQuintBlock(
            BitArrayStream BitStream,
            List <IntegerEncoded> ListIntegerEncoded,
            int NumberBitsPerValue)
        {
            // Implement the algorithm in section C.2.12
            int[] m = new int[3];
            int[] q = new int[3];
            int   Q;

            // Read the trit encoded block according to
            // table C.2.15
            m[0] = BitStream.ReadBits(NumberBitsPerValue);
            Q    = BitStream.ReadBits(3);
            m[1] = BitStream.ReadBits(NumberBitsPerValue);
            Q   |= BitStream.ReadBits(2) << 3;
            m[2] = BitStream.ReadBits(NumberBitsPerValue);
            Q   |= BitStream.ReadBits(2) << 5;

            BitArrayStream Qb = new BitArrayStream(new BitArray(new int[] { Q }));

            if (Qb.ReadBits(1, 2) == 3 && Qb.ReadBits(5, 6) == 0)
            {
                q[0] = q[1] = 4;
                q[2] = (Qb.ReadBit(0) << 2) | ((Qb.ReadBit(4) & ~Qb.ReadBit(0)) << 1) | (Qb.ReadBit(3) & ~Qb.ReadBit(0));
            }
            else
            {
                int C = 0;
                if (Qb.ReadBits(1, 2) == 3)
                {
                    q[2] = 4;
                    C    = (Qb.ReadBits(3, 4) << 3) | ((~Qb.ReadBits(5, 6) & 3) << 1) | Qb.ReadBit(0);
                }
                else
                {
                    q[2] = Qb.ReadBits(5, 6);
                    C    = Qb.ReadBits(0, 4);
                }

                BitArrayStream Cb = new BitArrayStream(new BitArray(new int[] { C }));
                if (Cb.ReadBits(0, 2) == 5)
                {
                    q[1] = 4;
                    q[0] = Cb.ReadBits(3, 4);
                }
                else
                {
                    q[1] = Cb.ReadBits(3, 4);
                    q[0] = Cb.ReadBits(0, 2);
                }
            }

            for (int i = 0; i < 3; i++)
            {
                IntegerEncoded IntEncoded = new IntegerEncoded(EIntegerEncoding.Quint, NumberBitsPerValue)
                {
                    BitValue   = m[i],
                    QuintValue = q[i]
                };
                ListIntegerEncoded.Add(IntEncoded);
            }
        }
Beispiel #18
0
        static int UnquantizeTexelWeight(IntegerEncoded intEncoded)
        {
            int bitValue  = intEncoded.BitValue;
            int bitLength = intEncoded.NumberBits;

            int a = BitArrayStream.Replicate(bitValue & 1, 1, 7);
            int b = 0, c = 0, d = 0;

            int result = 0;

            switch (intEncoded.GetEncoding())
            {
            case IntegerEncoded.EIntegerEncoding.JustBits:
                result = BitArrayStream.Replicate(bitValue, bitLength, 6);
                break;

            case IntegerEncoded.EIntegerEncoding.Trit:
            {
                d = intEncoded.TritValue;
                Debug.Assert(d < 3);

                switch (bitLength)
                {
                case 0:
                {
                    int[] results = { 0, 32, 63 };
                    result = results[d];

                    break;
                }

                case 1:
                {
                    c = 50;
                    break;
                }

                case 2:
                {
                    c = 23;
                    int b2 = (bitValue >> 1) & 1;
                    b = (b2 << 6) | (b2 << 2) | b2;

                    break;
                }

                case 3:
                {
                    c = 11;
                    int cb = (bitValue >> 1) & 3;
                    b = (cb << 5) | cb;

                    break;
                }

                default:
                    throw new AstcDecoderException("Invalid trit encoding for texel weight");
                }

                break;
            }

            case IntegerEncoded.EIntegerEncoding.Quint:
            {
                d = intEncoded.QuintValue;
                Debug.Assert(d < 5);

                switch (bitLength)
                {
                case 0:
                {
                    int[] results = { 0, 16, 32, 47, 63 };
                    result = results[d];

                    break;
                }

                case 1:
                {
                    c = 28;

                    break;
                }

                case 2:
                {
                    c = 13;
                    int b2 = (bitValue >> 1) & 1;
                    b = (b2 << 6) | (b2 << 1);

                    break;
                }

                default:
                    throw new AstcDecoderException("Invalid quint encoding for texel weight");
                }

                break;
            }
            }

            if (intEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits && bitLength > 0)
            {
                // Decode the value...
                result  = d * c + b;
                result ^= a;
                result  = (a & 0x20) | (result >> 2);
            }

            Debug.Assert(result < 64);

            // Change from [0,63] to [0,64]
            if (result > 32)
            {
                result += 1;
            }

            return(result);
        }
Beispiel #19
0
        static void DecodeColorValues(
            int[]  outputValues,
            byte[] inputData,
            uint[] modes,
            int numberPartitions,
            int numberBitsForColorData)
        {
            // First figure out how many color values we have
            int numberValues = 0;

            for (int i = 0; i < numberPartitions; i++)
            {
                numberValues += (int)((modes[i] >> 2) + 1) << 1;
            }

            // Then based on the number of values and the remaining number of bits,
            // figure out the max value for each of them...
            int range = 256;

            while (--range > 0)
            {
                IntegerEncoded intEncoded = IntegerEncoded.CreateEncoding(range);
                int            bitLength  = intEncoded.GetBitLength(numberValues);

                if (bitLength <= numberBitsForColorData)
                {
                    // Find the smallest possible range that matches the given encoding
                    while (--range > 0)
                    {
                        IntegerEncoded newIntEncoded = IntegerEncoded.CreateEncoding(range);
                        if (!newIntEncoded.MatchesEncoding(intEncoded))
                        {
                            break;
                        }
                    }

                    // Return to last matching range.
                    range++;
                    break;
                }
            }

            // We now have enough to decode our integer sequence.
            List <IntegerEncoded> integerEncodedSequence = new List <IntegerEncoded>();
            BitArrayStream        colorBitStream         = new BitArrayStream(new BitArray(inputData));

            IntegerEncoded.DecodeIntegerSequence(integerEncodedSequence, colorBitStream, range, numberValues);

            // Once we have the decoded values, we need to dequantize them to the 0-255 range
            // This procedure is outlined in ASTC spec C.2.13
            int outputIndices = 0;

            foreach (IntegerEncoded intEncoded in integerEncodedSequence)
            {
                int bitLength = intEncoded.NumberBits;
                int bitValue  = intEncoded.BitValue;

                Debug.Assert(bitLength >= 1);

                int a = 0, b = 0, c = 0, d = 0;
                // A is just the lsb replicated 9 times.
                a = BitArrayStream.Replicate(bitValue & 1, 1, 9);

                switch (intEncoded.GetEncoding())
                {
                case IntegerEncoded.EIntegerEncoding.JustBits:
                {
                    outputValues[outputIndices++] = BitArrayStream.Replicate(bitValue, bitLength, 8);

                    break;
                }

                case IntegerEncoded.EIntegerEncoding.Trit:
                {
                    d = intEncoded.TritValue;

                    switch (bitLength)
                    {
                    case 1:
                    {
                        c = 204;

                        break;
                    }

                    case 2:
                    {
                        c = 93;
                        // B = b000b0bb0
                        int b2 = (bitValue >> 1) & 1;
                        b = (b2 << 8) | (b2 << 4) | (b2 << 2) | (b2 << 1);

                        break;
                    }

                    case 3:
                    {
                        c = 44;
                        // B = cb000cbcb
                        int cb = (bitValue >> 1) & 3;
                        b = (cb << 7) | (cb << 2) | cb;

                        break;
                    }


                    case 4:
                    {
                        c = 22;
                        // B = dcb000dcb
                        int dcb = (bitValue >> 1) & 7;
                        b = (dcb << 6) | dcb;

                        break;
                    }

                    case 5:
                    {
                        c = 11;
                        // B = edcb000ed
                        int edcb = (bitValue >> 1) & 0xF;
                        b = (edcb << 5) | (edcb >> 2);

                        break;
                    }

                    case 6:
                    {
                        c = 5;
                        // B = fedcb000f
                        int fedcb = (bitValue >> 1) & 0x1F;
                        b = (fedcb << 4) | (fedcb >> 4);

                        break;
                    }

                    default:
                        throw new AstcDecoderException("Unsupported trit encoding for color values!");
                    }

                    break;
                }

                case IntegerEncoded.EIntegerEncoding.Quint:
                {
                    d = intEncoded.QuintValue;

                    switch (bitLength)
                    {
                    case 1:
                    {
                        c = 113;

                        break;
                    }

                    case 2:
                    {
                        c = 54;
                        // B = b0000bb00
                        int b2 = (bitValue >> 1) & 1;
                        b = (b2 << 8) | (b2 << 3) | (b2 << 2);

                        break;
                    }

                    case 3:
                    {
                        c = 26;
                        // B = cb0000cbc
                        int cb = (bitValue >> 1) & 3;
                        b = (cb << 7) | (cb << 1) | (cb >> 1);

                        break;
                    }

                    case 4:
                    {
                        c = 13;
                        // B = dcb0000dc
                        int dcb = (bitValue >> 1) & 7;
                        b = (dcb << 6) | (dcb >> 1);

                        break;
                    }

                    case 5:
                    {
                        c = 6;
                        // B = edcb0000e
                        int edcb = (bitValue >> 1) & 0xF;
                        b = (edcb << 5) | (edcb >> 3);

                        break;
                    }

                    default:
                        throw new AstcDecoderException("Unsupported quint encoding for color values!");
                    }
                    break;
                }
                }

                if (intEncoded.GetEncoding() != IntegerEncoded.EIntegerEncoding.JustBits)
                {
                    int T = d * c + b;
                    T ^= a;
                    T  = (a & 0x80) | (T >> 2);

                    outputValues[outputIndices++] = T;
                }
            }

            // Make sure that each of our values is in the proper range...
            for (int i = 0; i < numberValues; i++)
            {
                Debug.Assert(outputValues[i] <= 255);
            }
        }
Beispiel #20
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?)");
            }
        }