예제 #1
0
        /*public void Decode(bool[] values)
         * {
         *  BinaryStats bs = new BinaryStats();
         *  coder.initDecoding();
         *  for (int i = 0; i < values.Length; i++)
         *  {
         *      values[i] = coder.decodeBit(bs.P0, bs.P1);
         *      bs.Update(values[i]);
         *  }
         * }*/

        public int[] Decode()
        {
            BinaryReader br            = new BinaryReader(this.target);
            int          cnt           = br.ReadInt32();
            int          encodedLength = br.ReadInt32();
            long         origPos       = target.Position;

            int[] result = new int[cnt];

            Coder coder = new Coder(this.target);

            coder.initDecoding();
            IBinaryStats[] stats = new IBinaryStats[contextCount];
            for (int i = 0; i < stats.Length; i++)
            {
                stats[i] = new ContextBinaryStats(contextDepth);
            }
            IBinaryStats rest = new ContextBinaryStats(contextDepth);
            IBinaryStats sgn  = new ContextBinaryStats(contextDepth);

            for (int i = 0; i < cnt; i++)
            {
                List <bool> code = new List <bool>();
                int         bin  = 0;
                while (!isCode(code, binarizerCutoff, binarizerOrder))
                {
                    IBinaryStats bs = bin < stats.Length ? stats[bin] : rest;
                    if (isSgn(code, binarizerCutoff, binarizerOrder))
                    {
                        bs = sgn;
                    }
                    bool bit = coder.decodeBit(bs.P0, bs.P1);
                    bs.Update(bit);
                    code.Add(bit);
                    bin++;
                }
                result[i] = deBinarize(code.ToArray(), binarizerCutoff, binarizerOrder);
            }

            target.Position = origPos + encodedLength;
            return(result);
        }
예제 #2
0
        public void Encode(int[] values)
        {
            MemoryStream ms    = new MemoryStream();
            Coder        coder = new Coder(ms);

            bool[][]       bins  = binarize(values, binarizerCutoff, binarizerOrder);
            IBinaryStats[] stats = new IBinaryStats[contextCount];
            for (int i = 0; i < stats.Length; i++)
            {
                stats[i] = new ContextBinaryStats(contextDepth);
            }
            IBinaryStats rest = new ContextBinaryStats(contextDepth);
            IBinaryStats sgn  = new ContextBinaryStats(contextDepth);

            for (int i = 0; i < bins.Length; i++)
            {
                for (int j = 0; j < bins[i].Length; j++)
                {
                    IBinaryStats bs = j < stats.Length ? stats[j] : rest;
                    if ((j > 0) && (j == bins[i].Length - 1))
                    {
                        bs = sgn;
                    }
                    coder.encodeBit(bs.P0, bs.P1, bins[i][j]);
                    bs.Update(bins[i][j]);
                }
            }
            coder.finishEncode();

            int          encodedLength = (int)ms.Length;
            BinaryWriter bw            = new BinaryWriter(target);

            bw.Write(values.Length);
            bw.Write(encodedLength);
            ms.Position = 0;
            byte[] buffer = new byte[encodedLength];
            ms.Read(buffer, 0, encodedLength);
            target.Write(buffer, 0, encodedLength);
        }