Exemplo n.º 1
0
        /// <summary>
        /// ReadByteStuffedByte is like ReadByte but is for byte-stuffed Huffman data.
        /// </summary>
        /// <param name="inputStream">Input stream</param>
        /// <param name="errorCode">Error code</param>
        /// <returns>The <see cref="byte"/></returns>
        internal byte ReadByteStuffedByte(Stream inputStream, out JpegDecoderCore.ErrorCodes errorCode)
        {
            byte x;

            errorCode = JpegDecoderCore.ErrorCodes.NoError;

            // Take the fast path if bytes.buf contains at least two bytes.
            if (this.I + 2 <= this.J)
            {
                x = this.Buffer[this.I];
                this.I++;
                this.UnreadableBytes = 1;
                if (x != JpegConstants.Markers.XFF)
                {
                    return(x);
                }

                if (this.Buffer[this.I] != 0x00)
                {
                    errorCode = JpegDecoderCore.ErrorCodes.MissingFF00;
                    return(0);

                    // throw new MissingFF00Exception();
                }

                this.I++;
                this.UnreadableBytes = 2;
                return(JpegConstants.Markers.XFF);
            }

            this.UnreadableBytes = 0;

            x = this.ReadByte(inputStream);
            this.UnreadableBytes = 1;
            if (x != JpegConstants.Markers.XFF)
            {
                return(x);
            }

            x = this.ReadByte(inputStream);
            this.UnreadableBytes = 2;
            if (x != 0x00)
            {
                errorCode = JpegDecoderCore.ErrorCodes.MissingFF00;
                return(0);

                // throw new MissingFF00Exception();
            }

            return(JpegConstants.Markers.XFF);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Receive extend
        /// </summary>
        /// <param name="t">Byte</param>
        /// <param name="decoder">Jpeg decoder</param>
        /// <returns>Read bits value</returns>
        internal int ReceiveExtend(byte t, JpegDecoderCore decoder)
        {
            if (this.UnreadBits < t)
            {
                JpegDecoderCore.ErrorCodes errorCode = this.EnsureNBits(t, decoder);
                if (errorCode != JpegDecoderCore.ErrorCodes.NoError)
                {
                    throw new JpegDecoderCore.MissingFF00Exception();
                }
            }

            this.UnreadBits -= t;
            this.Mask      >>= t;
            int s = 1 << t;
            int x = (int)((this.Accumulator >> this.UnreadBits) & (s - 1));

            if (x < (s >> 1))
            {
                x += ((-1) << t) + 1;
            }

            return(x);
        }