private static int EncodePatchHeader(BitPack output, TerrainPatch.Header header, int[] patch, int RegionSizeX, int RegionSizeY, int wbits) { // better check if (wbits > 17) { wbits = 16; } else if (wbits < 3) { wbits = 3; } header.QuantWBits &= 0xf0; header.QuantWBits |= (wbits - 2); output.PackBits(header.QuantWBits, 8); output.PackFloat(header.DCOffset); output.PackBits(header.Range, 16); if (RegionSizeX > Constants.RegionSize || RegionSizeY > Constants.RegionSize) { output.PackBits(header.PatchIDs, 32); } else { output.PackBits(header.PatchIDs, 10); } return(wbits); }
public void BitPacking() { byte[] packedBytes = new byte[12]; BitPack bitpacker = new BitPack(packedBytes, 0); bitpacker.PackBits(0x0ABBCCDD, 32); bitpacker.PackBits(25, 5); bitpacker.PackFloat(123.321f); bitpacker.PackBits(1000, 16); bitpacker = new BitPack(packedBytes, 0); int b = bitpacker.UnpackBits(32); Assert.IsTrue(b == 0x0ABBCCDD, "Unpacked " + b + " instead of 2864434397"); b = bitpacker.UnpackBits(5); Assert.IsTrue(b == 25, "Unpacked " + b + " instead of 25"); float f = bitpacker.UnpackFloat(); Assert.IsTrue(f == 123.321f, "Unpacked " + f + " instead of 123.321"); b = bitpacker.UnpackBits(16); Assert.IsTrue(b == 1000, "Unpacked " + b + " instead of 1000"); }
private static int EncodePatchHeader(BitPack output, TerrainPatch.Header header, int[] patch, int RegionSizeX, int RegionSizeY, int wbits) { /* * int temp; * int wbits = (header.QuantWBits & 0x0f) + 2; * uint maxWbits = (uint)wbits + 5; * uint minWbits = ((uint)wbits >> 1); * int wbitsMaxValue; */ // goal is to determ minimum number of bits to use so all data fits /* * wbits = (int)minWbits; * wbitsMaxValue = (1 << wbits); * * for (int i = 0; i < patch.Length; i++) * { * temp = patch[i]; * if (temp != 0) * { * // Get the absolute value * if (temp < 0) temp *= -1; * * no coments.. * * for (int j = (int)maxWbits; j > (int)minWbits; j--) * { * if ((temp & (1 << j)) != 0) * { * if (j > wbits) wbits = j; * break; * } * } * * while (temp > wbitsMaxValue) * { * wbits++; * if (wbits == maxWbits) * goto Done; * wbitsMaxValue = 1 << wbits; * } * } * } * * Done: * * // wbits += 1; */ // better check if (wbits > 17) { wbits = 16; } else if (wbits < 3) { wbits = 3; } header.QuantWBits &= 0xf0; header.QuantWBits |= (wbits - 2); output.PackBits(header.QuantWBits, 8); output.PackFloat(header.DCOffset); output.PackBits(header.Range, 16); if (RegionSizeX > Constants.RegionSize || RegionSizeY > Constants.RegionSize) { output.PackBits(header.PatchIDs, 32); } else { output.PackBits(header.PatchIDs, 10); } return(wbits); }