示例#1
0
        /// <summary>
        ///     Decode the length determinant
        ///     ITU-T X.691. 10.9. General rules for encoding a length determinant
        /// </summary>
        protected virtual int decodeLengthDeterminant(BitArrayInputStream stream)
        {
            skipAlignedBits(stream);
            int result = stream.ReadByte();

            if ((result & 0x80) == 0)
            {
                // NOTE 2. a) ("n" less than 128)
                // a single octet containing "n" with bit 8 set to zero;
                return(result);
            }
            else
            {
                // NOTE 2. b) ("n" less than 16K) two octets
                // containing "n" with bit 8 of the first octet
                // set to 1 and bit 7 set to zero;
                result  = (result & 0x3f) << 8;
                result |= stream.ReadByte();
            }
            // WARNING! Large N doesn't supported NOW!
            // NOTE 2. b) (large "n") a single octet containing a count "m"
            // with bit 8 set to 1 and bit 7 set to 1.
            // The count "m" is one to four, and the length indicates that
            // a fragment of the material follows (a multiple "m" of 16K items).
            // For all values of "m", the fragment is then followed
            // by another length encoding for the remainder of the material.
            return(result);
        }
示例#2
0
        /// <summary>
        ///     Decode of the constrained whole number
        ///     ITU-T X.691. 10.5.
        ///     NOTE – (Tutorial) This subclause is referenced by other clauses,
        ///     and itself references earlier clauses for the production of
        ///     a nonnegative-binary-integer or a 2's-complement-binary-integer encoding.
        /// </summary>
        protected virtual long decodeConstraintNumber(long min, long max, BitArrayInputStream stream)
        {
            long result     = 0;
            long valueRange = max - min;
            //!!!! int narrowedVal = value - min; !!!
            int maxBitLen = PERCoderUtils.getMaxBitLength(valueRange);

            if (valueRange == 0)
            {
                return(max);
            }

            // The rest of this Note addresses the ALIGNED variant.
            if (valueRange > 0 && valueRange < 256)
            {
                /*
                 * 1. Where the range is less than or equal to 255, the value encodes
                 * into a bit-field of the minimum size for the range.
                 * 2. Where the range is exactly 256, the value encodes
                 * into a single octet octet-aligned bit-field.
                 */
                skipAlignedBits(stream);
                result  = stream.readBits(maxBitLen);
                result += min;
            }
            else if (valueRange > 0 && valueRange < 65536)
            {
                /*
                 * 3. Where the range is 257 to 64K, the value encodes into
                 * a two octet octet-aligned bit-field.
                 */
                skipAlignedBits(stream);
                result  = stream.ReadByte() << 8;
                result  = (int)result | stream.ReadByte();
                result += min;
            }
            else
            {
                /*
                 * 4. Where the range is greater than 64K, the range is ignored
                 * and the value encodes into an  octet-aligned bit-field
                 * which is the minimum number of octets for the value.
                 * In this latter case, later procedures (see 10.9)
                 * also encode a length field (usually a single octet) to indicate
                 * the length of the encoding. For the other cases, the length
                 * of the encoding is independent of the value being encoded,
                 * and is not explicitly encoded.
                 */
                int intLen = decodeConstraintLengthDeterminant(1, CoderUtils.getPositiveIntegerLength(valueRange), stream);
                skipAlignedBits(stream);
                result  = (int)decodeIntegerValueAsBytes(intLen, stream);
                result += min;
            }

            return(result);
        }
示例#3
0
        protected override long decodeConstraintNumber(long min, long max, BitArrayInputStream stream)
        {
            long result     = 0;
            long valueRange = max - min;
            // !!! int narrowedVal = value - min; !!!
            int maxBitLen = PERCoderUtils.getMaxBitLength(valueRange);

            if (valueRange == 0)
            {
                return(max);
            }
            // For the UNALIGNED variant the value is always encoded in the minimum
            // number of bits necessary to represent the range (defined in 10.5.3).
            int currentBit = maxBitLen;

            while (currentBit > 7)
            {
                currentBit -= 8;
                result     |= (uint)(stream.ReadByte() << currentBit);
            }
            if (currentBit > 0)
            {
                result |= (uint)stream.readBits(currentBit);
            }
            result += min;
            return(result);
        }
示例#4
0
        protected override long decodeConstraintNumber(long min, long max, BitArrayInputStream stream)
        {
            long result = 0;
            long valueRange = max - min;
            // !!! int narrowedVal = value - min; !!!
            int maxBitLen = PERCoderUtils.getMaxBitLength(valueRange);

            if (valueRange == 0)
            {
                return max;
            }
            //For the UNALIGNED variant the value is always encoded in the minimum 
            // number of bits necessary to represent the range (defined in 10.5.3). 
            int currentBit = maxBitLen;
            while (currentBit > 7)
            {
                currentBit -= 8;
                result |= (uint)(stream.ReadByte() << currentBit);
            }
            if (currentBit > 0)
            {
                result |= (uint)(stream.readBits(currentBit));
            }
            result += min;
            return result;
        }
        /// <seealso cref="BitArrayInputStream.read()">
        /// </seealso>
        public virtual void  testRead()
        {
            BitArrayInputStream stream = newStream();
            int bt = stream.ReadByte();

            Assert.Equals(0xAB, bt);
            bt = stream.ReadByte();
            Assert.Equals(0xCD, bt);
            bt = stream.readBit();
            Assert.Equals(1, bt);
            bt = stream.readBit();
            Assert.Equals(1, bt);
            bt = stream.ReadByte();
            Assert.Equals(0xBC, bt);
            bt = stream.ReadByte();
            Assert.Equals(0xCF, bt);
            stream.skipUnreadedBits();
            bt = stream.ReadByte();
            Assert.Equals(0xDC, bt);
            bt = stream.readBits(4);
            Assert.Equals(0x0B, bt);
            bt = stream.readBits(4);
            Assert.Equals(0x0A, bt);
        }