private void DecodeBT_Click(object sender, RoutedEventArgs e)
        {
            DecodeLB.Text = "";
            string[] decode = DecodeTB.Text.Split(' ');

            LZ78 lz = new LZ78();

            List <Tuple <byte, byte> > resultd = new List <Tuple <byte, byte> >();

            for (int i = 0; i < decode.Length - 1; i += 2)
            {
                resultd.Add(new Tuple <byte, byte>(Convert.ToByte(decode[i]), Convert.ToByte(decode[i + 1])));
            }

            List <string> decoderesult = lz.Decode(resultd);

            List <string> r = new List <string>();

            for (int i = 0; i < decoderesult.Count; i++)
            {
                string[] val = decoderesult[i].Split(' ');
                foreach (var item in val)
                {
                    r.Add(item);
                }
            }
            for (int i = 0; i < r.Count; i++)
            {
                if (r[i] != "")
                {
                    int n = Convert.ToInt32(r[i]);
                    DecodeLB.Text += (char)n;
                }
            }
        }
Esempio n. 2
0
        public void Decompress_Decompresses_Decompressed()
        {
            var lz78   = new LZ78();
            var input  = new MemoryStream(new byte[] { 1, 0, 160, 56, 1, 1, 4, 20, 128, 48, 8, 3, 40, 72, 128, 128 });
            var output = new MemoryStream();

            byte[] expected = { 2, 5, 7, 1, 8, 2, 5, 2, 0, 2, 5, 1, 6, 8, 9, 2, 1 };
            lz78.Decompress(input, output);
            byte[] result = output.ToArray();
            CollectionAssert.AreEqual(expected, result);
        }
        private void FileBT_Click(object sender, RoutedEventArgs e)
        {
            byte[]         file;
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = @"K:\PROJEKTY VS\GolombCoding\GolombCoding\bin";
            openFileDialog.DefaultExt       = ".txt";
            openFileDialog.Filter           = "Text documents (.txt)|*.txt";
            openFileDialog.Multiselect      = false;

            if (openFileDialog.ShowDialog() == true)
            {
                file = File.ReadAllBytes(openFileDialog.FileName);
            }
            else
            {
                file = new byte[] { }
            };

            LZ78 lz = new LZ78();

            List <List <Tuple <byte, byte> > > result = lz.Encode(file);

            TextWriter tw = new StreamWriter(@"..\LZ78Encode.txt");

            foreach (var list in result)
            {
                if (list.Count != 0)
                {
                    byte[] b = new byte[list.Count * 2];

                    for (int i = 0; i < list.Count; i++)
                    {
                        tw.Write(list[i].Item1 + " ");
                        tw.Write(list[i].Item2 + " ");

                        b[2 * i]     = list[i].Item1;
                        b[2 * i + 1] = list[i].Item2;
                    }

                    using (var stream = new FileStream(@"..\LZ78Encode.bin", FileMode.Append))
                    {
                        stream.Write(b, 0, b.Length);
                    }
                }
                else
                {
                    break;
                }
            }
            tw.Close();
        }
        private void EncodeBT_Click(object sender, RoutedEventArgs e)
        {
            string encode = EncodeTB.Text;

            LZ78 lz = new LZ78();

            byte[] bytes = Encoding.ASCII.GetBytes(encode);

            List <List <Tuple <byte, byte> > > list = lz.Encode(bytes);

            EncodeLB.Text = "";

            foreach (var item in list)
            {
                for (int i = 0; i < item.Count; i++)
                {
                    EncodeLB.Text += string.Format("({0}, {1}) ", item[i].Item1, item[i].Item2);
                }
            }
        }
        private void DecodeFileBT_Click(object sender, RoutedEventArgs e)
        {
            byte[] byt = File.ReadAllBytes(@"..\LZ78Encode.bin");

            List <Tuple <byte, byte> > resultbin = new List <Tuple <byte, byte> >();



            for (int i = 0; i < byt.Length / 2; i++)
            {
                resultbin.Add(new Tuple <byte, byte>(byt[2 * i], byt[2 * i + 1]));
            }


            for (int j = 0; j < resultbin.Count; j += 256)
            {
                List <Tuple <byte, byte> > resultdec = resultbin.Skip(j).Take(256).ToList();

                LZ78          lz           = new LZ78();
                List <string> decoderesult = lz.Decode(resultdec);
                List <string> r            = new List <string>();
                for (int i = 0; i < decoderesult.Count; i++)
                {
                    string[] val = decoderesult[i].Split(' ');
                    foreach (var item in val)
                    {
                        r.Add(item);
                    }
                }
                TextWriter ws = new StreamWriter(@"..\LZ78DecodeBin.txt", true);
                for (int i = 0; i < r.Count; i++)
                {
                    if (r[i] != "")
                    {
                        int n = Convert.ToInt32(r[i]);
                        ws.Write((char)n);
                    }
                }
                ws.Close();
            }
        }
Esempio n. 6
0
        public ActionResult LZ78Encode(string toEncode)
        {
            if (toEncode.Length == 0)
            {
                ViewData["ArgumentException"] = "Пустая строка, пожалуйста введите что-нибудь.";
                return(View());
            }

            toEncode = toEncode.Replace("\r\n", "\n");
            try
            {
                ViewData["Encoded"]           = new LZ78().Encode(toEncode, out double compressionRatio);
                ViewData["CompressionDegree"] = string.Format("{0:0.##}", compressionRatio);
            }
            catch (Exception e)
            {
                ViewData["Error"] = "Пожалуйста, проверьте соответствие введенных данных нужному формату.";
            }

            ViewData["UploadedText"] = toEncode;

            return(View());
        }
Esempio n. 7
0
        public static List <LZ78> SetLZ78(string text)
        {
            var lz78 = new List <LZ78>();
            int i    = 0;

            while (i < text.Count())
            {
                var lz = new LZ78();
                if (i == 0)
                {
                    lz.Code       = $"0, {text[i]}";
                    lz.LengthCode = 9;
                    lz.FutureDictionary.Add(text[i].ToString());
                    lz.Buffer       = text;
                    lz.FutureBuffer = text.Remove(0, 1);
                }
                else
                {
                    foreach (var item in lz78.LastOrDefault().FutureDictionary)
                    {
                        lz.Dictionary.Add(item);
                    }
                    lz.Buffer = lz78.LastOrDefault().FutureBuffer;
                    bool   flag    = false;
                    string newWord = "";
                    int    j       = 0;
                    int    b       = i;
                    int    length  = lz.Buffer.Length;
                    while (j <= lz.Buffer.Length - 1)
                    {
                        string SubText = lz.Buffer.Substring(0, length);
                        foreach (var item in lz.Dictionary)
                        {
                            if (SubText == item)
                            {
                                flag = true;
                                if (lz.Buffer.Count() != 1)
                                {
                                    newWord = item + lz.Buffer[item.Count()];
                                }
                                else
                                {
                                    newWord = item;
                                }
                                lz.Code         = $"1, {lz.Dictionary.IndexOf(item)}, {newWord[newWord.Length - 1]}";
                                lz.FutureBuffer = lz.Buffer.Remove(0, newWord.Length);
                                i = i + (newWord.Length - 1);
                                break;
                            }
                        }
                        if (flag == true)
                        {
                            break;
                        }
                        j++;
                        length--;
                    }
                    if (flag == false)
                    {
                        newWord         = text[i].ToString();
                        lz.Code         = $"0, {text[i]}";
                        lz.FutureBuffer = lz.Buffer.Remove(0, 1);
                    }

                    if (lz.Dictionary.Count == 16)
                    {
                        foreach (var item in lz.Dictionary)
                        {
                            lz.FutureDictionary.Add(item);
                        }
                        lz.FutureDictionary.RemoveAt(0);
                        lz.FutureDictionary.Add(newWord);
                    }
                    else
                    {
                        foreach (var item in lz.Dictionary)
                        {
                            lz.FutureDictionary.Add(item);
                        }
                        lz.FutureDictionary.Add(newWord);
                    }
                }
                i++;
                lz78.Add(lz);
            }

            foreach (var lz in lz78)
            {
                foreach (var item in lz.Dictionary)
                {
                    lz.DictionatyString += "'" + item + "',";
                }
            }
            return(lz78);
        }