uGIF.LZWEncoder.Compress C# (CSharp) Method

Compress() private method

private Compress ( int init_bits, Stream outs ) : void
init_bits int
outs Stream
return void
        void Compress(int init_bits, Stream outs)
        {
            int fcode;
            int i /* = 0 */;
            int c;
            int ent;
            int disp;
            int hsize_reg;
            int hshift;

            // Set up the globals:  g_init_bits - initial number of bits
            g_init_bits = init_bits;

            // Set up the necessary values
            clear_flg = false;
            n_bits = g_init_bits;
            maxcode = MaxCode (n_bits);

            ClearCode = 1 << (init_bits - 1);
            EOFCode = ClearCode + 1;
            free_ent = ClearCode + 2;

            a_count = 0; // clear packet

            ent = NextPixel ();

            hshift = 0;
            for (fcode = hsize; fcode < 65536; fcode *= 2)
                ++hshift;
            hshift = 8 - hshift; // set hash code range bound

            hsize_reg = hsize;
            ResetCodeTable (hsize_reg); // clear hash table

            Output (ClearCode, outs);

            outer_loop :
            while ((c = NextPixel()) != EOF) {
                fcode = (c << maxbits) + ent;
                i = (c << hshift) ^ ent; // xor hashing

                if (htab [i] == fcode) {
                    ent = codetab [i];
                    continue;
                } else if (htab [i] >= 0) { // non-empty slot
                    disp = hsize_reg - i; // secondary hash (after G. Knott)
                    if (i == 0)
                        disp = 1;
                    do {
                        if ((i -= disp) < 0)
                            i += hsize_reg;

                        if (htab [i] == fcode) {
                            ent = codetab [i];
                            goto outer_loop;
                        }
                    } while (htab[i] >= 0);
                }
                Output (ent, outs);
                ent = c;
                if (free_ent < maxmaxcode) {
                    codetab [i] = free_ent++; // code -> hashtable
                    htab [i] = fcode;
                } else
                    ClearTable (outs);
            }
            // Put out the final code.
            Output (ent, outs);
            Output (EOFCode, outs);
        }