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; } } } }
private 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; 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 EIntegerEncoding.JustBits: { OutputValues[OutputIndices++] = BitArrayStream.Replicate(BitValue, BitLength, 8); break; } case EIntegerEncoding.Trit: { D = IntEncoded.TritValue; switch (BitLength) { case 1: { C = 204; break; } case 2: { C = 93; int b = (BitValue >> 1) & 1; B = (b << 8) | (b << 4) | (b << 2) | (b << 1); break; } case 3: { C = 44; int cb = (BitValue >> 1) & 3; B = (cb << 7) | (cb << 2) | cb; break; } case 4: { C = 22; int dcb = (BitValue >> 1) & 7; B = (dcb << 6) | dcb; break; } case 5: { C = 11; int edcb = (BitValue >> 1) & 0xF; B = (edcb << 5) | (edcb >> 2); break; } case 6: { C = 5; int fedcb = (BitValue >> 1) & 0x1F; B = (fedcb << 4) | (fedcb >> 4); break; } default: throw new Exception("Unsupported trit encoding for color values!"); } break; } case EIntegerEncoding.Quint: { D = IntEncoded.QuintValue; switch (BitLength) { case 1: { C = 113; break; } case 2: { C = 54; int b = (BitValue >> 1) & 1; B = (b << 8) | (b << 3) | (b << 2); break; } case 3: { C = 26; int cb = (BitValue >> 1) & 3; B = (cb << 7) | (cb << 1) | (cb >> 1); break; } case 4: { C = 13; int dcb = (BitValue >> 1) & 7; B = (dcb << 6) | (dcb >> 1); break; } case 5: { C = 6; int edcb = (BitValue >> 1) & 0xF; B = (edcb << 5) | (edcb >> 3); break; } default: throw new Exception("Unsupported quint encoding for color values!"); } break; } } if (IntEncoded.GetEncoding() != EIntegerEncoding.JustBits) { int T = D * C + B; T ^= A; T = (A & 0x80) | (T >> 2); OutputValues[OutputIndices++] = T; } } }
private 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 EIntegerEncoding.JustBits: Result = BitArrayStream.Replicate(BitValue, BitLength, 6); break; case EIntegerEncoding.Trit: { D = IntEncoded.TritValue; 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 Exception("Invalid trit encoding for texel weight"); } break; } case EIntegerEncoding.Quint: { D = IntEncoded.QuintValue; 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 Exception("Invalid quint encoding for texel weight"); } break; } } if (IntEncoded.GetEncoding() != EIntegerEncoding.JustBits && BitLength > 0) { // Decode the value... Result = D * C + B; Result ^= A; Result = (A & 0x20) | (Result >> 2); } // Change from [0,63] to [0,64] if (Result > 32) { Result += 1; } return(Result); }