iTextSharp.text.pdf.codec.BitFile.WriteBits C# (CSharp) Method

WriteBits() public method

public WriteBits ( int bits, int numbits ) : void
bits int
numbits int
return void
        public void WriteBits(int bits, int numbits)
        {
            int bitsWritten = 0;
            int numBytes = 255;		// gif block count
            do {
                // This handles the GIF block count stuff
                if ((index_ == 254 && bitsLeft_ == 0) || index_ > 254) {
                    if (blocks_)
                        output_.WriteByte((byte)numBytes);

                    output_.Write(buffer_, 0, numBytes);

                    buffer_[0] = 0;
                    index_ = 0;
                    bitsLeft_ = 8;
                }

                if (numbits <= bitsLeft_) { // bits contents fit in current index byte
                    if (blocks_) { // GIF
                        buffer_[index_] |= (byte)((bits & ((1 << numbits) - 1)) << (8 - bitsLeft_));
                        bitsWritten += numbits;
                        bitsLeft_ -= numbits;
                        numbits = 0;
                    }
                    else {
                        buffer_[index_] |= (byte)((bits & ((1 << numbits) - 1)) << (bitsLeft_ - numbits));
                        bitsWritten += numbits;
                        bitsLeft_ -= numbits;
                        numbits = 0;

                    }
                }
                else {	// bits overflow from current byte to next.
                    if (blocks_) { // GIF
                        // if bits  > space left in current byte then the lowest order bits
                        // of code are taken and put in current byte and rest put in next.
                        buffer_[index_] |= (byte)((bits & ((1 << bitsLeft_) - 1)) << (8 - bitsLeft_));
                        bitsWritten += bitsLeft_;
                        bits >>= bitsLeft_;
                        numbits -= bitsLeft_;
                        buffer_[++index_] = 0;
                        bitsLeft_ = 8;
                    }
                    else {
                        // if bits  > space left in current byte then the highest order bits
                        // of code are taken and put in current byte and rest put in next.
                        // at highest order bit location !!
                        int topbits = (byte)(((uint)bits >> (numbits - bitsLeft_)) & ((1 << bitsLeft_) - 1));
                        buffer_[index_] |= (byte)topbits;
                        numbits -= bitsLeft_;	// ok this many bits gone off the top
                        bitsWritten += bitsLeft_;
                        buffer_[++index_] = 0;	// next index
                        bitsLeft_ = 8;
                    }
                }

            } while (numbits != 0);
        }

Usage Example

Ejemplo n.º 1
0
        /**
         * @param buf data to be compressed to output stream
         * @exception IOException if underlying output stream error
         **/
        public void Compress(byte[] buf, int offset, int length)
        {
            int   idx;
            byte  c;
            short index;

            int maxOffset = offset + length;

            for (idx = offset; idx < maxOffset; ++idx)
            {
                c = buf[idx];
                if ((index = lzss_.FindCharString(prefix_, c)) != -1)
                {
                    prefix_ = index;
                }
                else
                {
                    bf_.WriteBits(prefix_, numBits_);
                    if (lzss_.AddCharString(prefix_, c) > limit_)
                    {
                        if (numBits_ == 12)
                        {
                            bf_.WriteBits(clearCode_, numBits_);
                            lzss_.ClearTable(codeSize_);
                            numBits_ = codeSize_ + 1;
                        }
                        else
                        {
                            ++numBits_;
                        }

                        limit_ = (1 << numBits_) - 1;
                        if (tiffFudge_)
                        {
                            --limit_;
                        }
                    }
                    prefix_ = (short)((short)c & 0xFF);
                }
            }
        }
All Usage Examples Of iTextSharp.text.pdf.codec.BitFile::WriteBits