Esempio n. 1
0
        public static void Huff(Stream sin, Stream sout, UpdateCRC callback)
        {
            Tree t = new Tree ();
            int rd, i, tmp;
            Stack<int> ret;
            BitIO bw = new BitIO (sout, true);
            while ((rd = sin.ReadByte ()) != -1) {
                tmp = rd;
                callback ((byte)tmp);
                if (!t.contains (rd)) {
                    ret = t.GetCode (257);
                    while (ret.Count > 0)
                        bw.WriteBit (ret.Pop ());
                    for (i = 0; i < 8; i++) {
                        bw.WriteBit ((int)(rd & 0x80));
                        rd <<= 1;
                    }
                    t.UpdateTree (tmp);
                }
                else {
                    ret = t.GetCode (tmp);
                    while (ret.Count > 0)
                        bw.WriteBit (ret.Pop ());
                    t.UpdateTree (tmp);
                }
            }

            ret = t.GetCode (256);
            while (ret.Count > 0) {
                bw.WriteBit (ret.Pop ());
            }
        }