-
06-05-2015, 09:13 AM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Thảo luận về thuật toán huffman trong nén ảnh
mình đang ngồi code giải thuật huffman dùng cho việc nén ảnh nhưng tốc độ rất chậm
chạy suýt đơ mất con máy cùi bắp của mình bạn nàoc có ý tưởng hay hơn hoặc cải thiện được nó không ????[IMG]images/smilies/2.gif[/IMG]
giúp mình với . đang bận đi học cải thiện môn giải tích hàm nhiều biến .. [IMG]images/smilies/21.gif[/IMG]
<code>
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
namespace Xulyanh
{
public static class huffman
{
public static unsafe Bitmap huff_decoding(string path)
{
Func<KeyValuePair<string, string>, bool> predicate = null;
string word;
StreamReader reader = File.OpenText(path);
reader.ReadLine();
string[] strArray = reader.ReadLine().Split(new char[] { '-' });
int height = int.Parse(strArray[0]);
int width = int.Parse(strArray[1]);
int[,] numArray = new int[height, width];
int num3 = 0;
while (num3 < 3)
{
if (reader.ReadLine() == "******")
{
num3++;
}
}
reader.ReadLine();
Dictionary<string, string> source = new Dictionary<string, string>();
while (true)
{
//bool fl = true;
string str = reader.ReadLine();
if (str == "******")
{
int num5;
reader.ReadLine();
string str2 = string.Empty;
word = string.Empty;
int num4 = 0;
for (num5 = 0; num5 < height; num5++)
{
str2 = reader.ReadLine();
for (int j = 0; j < str2.Length; j++)
{
word = str2[j].ToString();
goto Label_01C6;
Label_0135:
if (predicate == null)
{
predicate = delegate (KeyValuePair<string, string> x) {
return x.Value == word;
};
}
IEnumerable<string> enumerable = source.Where<KeyValuePair<string, string>>(predicate).Select<KeyValuePair<string, string>, string>(delegate (KeyValuePair<string, string> x) {
return x.Key;
});
if (enumerable.Count<string>() == 0)
{
word = word + str2[++j];
}
else
{
word = enumerable.Single<string>();
goto Label_01CE;
}
Label_01C6:
//fl = true;
goto Label_0135;
Label_01CE:
if (num4 == width)
{
num4 = 0;
}
numArray[num5, num4++] = int.Parse(word);
}
}
reader.Dispose();
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
for (num5 = 0; num5 < height; num5++)
{
byte* numPtr = (byte*) ((void*) bitmapdata.Scan0) + (num5 * bitmapdata.Stride);
for (int k = 0; k < width; k++)
{
numPtr[k] = (byte) numArray[num5, k];
}
}
ColorPalette palette = bitmap.Palette;
for (int i = 0; i < 0x100; i++)
{
palette.Entries[i] = Color.FromArgb(i, i, i);
}
bitmap.Palette = palette;
bitmap.UnlockBits(bitmapdata);
return bitmap;
}
string[] strArray2 = str.Split(new char[] { '-' });
source.Add(strArray2[0], strArray2[1]);
}
}
public static unsafe void huff_encoding(string path, string destPath)
{
int num3;
//bool flag;
Bitmap bitmap = (Bitmap) Image.FromFile(path);
int[] numArray = new int[0x100];
int height = bitmap.Height;
int width = bitmap.Width;
int[,] m = new int[height, width];
StreamWriter writer = File.CreateText(destPath);
writer.WriteLine("K\x00edch thước:");
writer.WriteLine(height + "-" + width);
writer.WriteLine("******");
writer.WriteLine("G.Trị-Số lần");
BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
for (num3 = 0; num3 < bitmap.Height; num3++)
{
byte* numPtr = (byte*) ((void*) bitmapdata.Scan0) + (num3 * bitmapdata.Stride);
for (int i = 0; i < bitmap.Width; i++)
{
m[num3, i] = numPtr[i];
numArray[numPtr[i]]++;
}
}
bitmap.UnlockBits(bitmapdata);
bitmap.Dispose();
Dictionary<string, int> source = new Dictionary<string, int>();
for (num3 = 0; num3 < 0x100; num3++)
{
if (numArray[num3] != 0)
{
source.Add(num3.ToString(), numArray[num3]);
}
}
int count = source.Count;
IOrderedEnumerable<KeyValuePair<string, int>> enumerable = source.OrderBy<KeyValuePair<string, int>, int>(delegate (KeyValuePair<string, int> t) {
return t.Value;
});
foreach (KeyValuePair<string, int> pair in enumerable.Reverse<KeyValuePair<string, int>>())
{
writer.WriteLine(pair.Key + "-" + pair.Value);
}
while (enumerable.Count<KeyValuePair<string, int>>() > 2)
{
string key = "(" + enumerable.ElementAt<KeyValuePair<string, int>>(1).Key + "," + enumerable.ElementAt<KeyValuePair<string, int>>(0).Key + ")";
int num6 = enumerable.ElementAt<KeyValuePair<string, int>>(1).Value + enumerable.ElementAt<KeyValuePair<string, int>>(0).Value;
source.Remove(enumerable.ElementAt<KeyValuePair<st ring, int>>(0).Key);
source.Remove(enumerable.ElementAt<KeyValuePair<st ring, int>>(0).Key);
enumerable = source.OrderBy<KeyValuePair<string, int>, int>(delegate (KeyValuePair<string, int> t) {
return t.Value;
});
Dictionary<string, int> dictionary2 = new Dictionary<string, int>();
int num7 = 0;
while ((num7 < enumerable.Count<KeyValuePair<string, int>>()) && (enumerable.ElementAt<KeyValuePair<string, int>>(num7).Value < num6))
{
dictionary2.Add(enumerable.ElementAt<KeyValuePair< string, int>>(num7).Key, enumerable.ElementAt<KeyValuePair<string, int>>(num7).Value);
num7++;
}
dictionary2.Add(key, num6);
while (num7 < source.Count)
{
dictionary2.Add(enumerable.ElementAt<KeyValuePair< string, int>>(num7).Key, enumerable.ElementAt<KeyValuePair<string, int>>(num7).Value);
num7++;
}
source = dictionary2;
enumerable = source.OrderBy<KeyValuePair<string, int>, int>(delegate (KeyValuePair<string, int> t) {
return t.Value;
});
}
Dictionary<string, string> dictionary3 = new Dictionary<string, string>();
if (source.ElementAt<KeyValuePair<string, int>>(0).Value >= source.ElementAt<KeyValuePair<string, int>>(1).Value)
{
dictionary3.Add(source.ElementAt<KeyValuePair<stri ng, int>>(0).Key, "0");
dictionary3.Add(source.ElementAt<KeyValuePair<stri ng, int>>(1).Key, "1");
}
else
{
dictionary3.Add(source.ElementAt<KeyValuePair<stri ng, int>>(1).Key, "0");
dictionary3.Add(source.ElementAt<KeyValuePair<stri ng, int>>(0).Key, "1");
}
IOrderedEnumerable<KeyValuePair<string, string>> enumerable2 = dictionary3.OrderBy<KeyValuePair<string, string>, string>(delegate (KeyValuePair<string, string> t) {
return t.Value;
});
writer.WriteLine("******");
writer.WriteLine("Kết quả thủ tục tiến");
writer.WriteLine(dictionary3.ElementAt<KeyValuePai r<string, string>>(0).Key + "-" + dictionary3.ElementAt<KeyValuePair<string, string>>(0).Value);
writer.WriteLine(dictionary3.ElementAt<KeyValuePai r<string, string>>(1).Key + "-" + dictionary3.ElementAt<KeyValuePair<string, string>>(1).Value);
count--;
int index = 0;
Label_07E0:
//flag = true;
if (index == count)
{
writer.WriteLine("******");
writer.WriteLine("Kết quả thủ tục l\x00f9i");
foreach (KeyValuePair<string, string> pair2 in enumerable2.OrderBy<KeyValuePair<string, string>, int>(delegate (KeyValuePair<string, string> p) {
return p.Value.Length;
}))
{
writer.WriteLine(pair2.Key + "-" + pair2.Value);
}
writer.WriteLine("******");
writer.WriteLine("M\x00e3 h\x00f3a");
string str6 = string.Empty;
for (int y = 0; y < height; y++)
{
str6 = string.Empty;
Func<KeyValuePair<string, string>, bool> predicate = null;
for (int l = 0; l < width; l++)
{
if (predicate == null)
{
predicate = delegate (KeyValuePair<string, string> p) {
return p.Key == m[y, l].ToString();
};
}
IEnumerable<string> enumerable3 = enumerable2.Where<KeyValuePair<string, string>>(predicate).Select<KeyValuePair<string, string>, string>(delegate (KeyValuePair<string, string> p) {
return p.Value;
});
str6 = str6 + enumerable3.Single<string>();
}
writer.WriteLine(str6);
}
writer.Dispose();
}
else
{
string str2;
string str3;
string str4;
string str5;
if ((enumerable2.ElementAt<KeyValuePair<string, string>>(index).Key[0] != '(') || (enumerable2.ElementAt<KeyValuePair<string, string>>(index).Key[1] != '('))
{
if ((enumerable2.ElementAt<KeyValuePair<string, string>>(index).Key[0] == '(') && (enumerable2.ElementAt<KeyValuePair<string, string>>(index).Key[1] != '('))
{
str2 = enumerable2.ElementAt<KeyValuePair<string, string>>(index).Key;
str3 = enumerable2.ElementAt<KeyValuePair<string, string>>(index).Value;
int num10 = str2.IndexOf(',', 0);
str4 = str2.Substring(1, num10 - 1);
str5 = str2.Substring(num10 + 1, (str2.Length - num10) - 2);
dictionary3.Add(str4, str3 + "0");
dictionary3.Add(str5, str3 + "1");
dictionary3.Remove(str2);
enumerable2 = enumerable2.OrderBy<KeyValuePair<string, string>, string>(delegate (KeyValuePair<string, string> a) {
return a.Value;
});
index = 0;
}
else
{
index++;
}
}
else
{
str2 = enumerable2.ElementAt<KeyValuePair<string, string>>(index).Key;
str3 = enumerable2.ElementAt<KeyValuePair<string, string>>(index).Value;
int num9 = 0;
num3 = 1;
for (num3 = 1; num3 < str2.Length; num3++)
{
if (str2[num3] == '(')
{
num9++;
}
if (str2[num3] == ')')
{
num9--;
}
if (num9 == 0)
{
break;
}
}
str4 = str2.Substring(1, num3);
str5 = str2.Substring(num3 + 2, (str2.Length - num3) - 3);
dictionary3.Add(str4, str3 + "0");
dictionary3.Add(str5, str3 + "1");
dictionary3.Remove(str2);
enumerable2 = enumerable2.OrderBy<KeyValuePair<string, string>, string>(delegate (KeyValuePair<string, string> a) {
return a.Value;
});
index = 0;
}
goto Label_07E0;
}
}
}
}
</code>View more random threads:
- Tốc độ của HttpWebRequest thua xa Js trên Chrome
- Làm sao để chuyển đổi từ kiểu dữ liệu String sang SecureString?
- XIN CƠ SỞ DỮ LIỆU MẪU "AdventureWorks" SQL 2008
- Chương trình winform có sử dụng nhiều ảnh, lưu đường dẫn hay lưu thẳng file nhị phân
- Truyền para giá trị null từ C# xuống Sql Server 2005
- random tọa độ x,y trong from
- Cách gõ font UTF-8 trong lập trình C#?
- Chuyễn chuổi thành byte và ngược lại
- Bắt sự kiện khi nhấn nút Print trong lập trình C#?
- Làm sao để viết lại thuộc tính Find của SearchLookUp Devexpress
Rocket 1h là thực phẩm chức năng giúp tăng cường sinh lý nam có nguồn gốc hoàn toàn từ dược liệu tự nhiên. Rất nhiều nam giới đã tuyển lựa Rocket 1h như một phương thuốc hữu hiệu để cải thiện tình...
Nguyên nhân của cặp đôi nam nữ mua...