LZW compression is a lossless compression technique, in which the quality of compressed data stills as good as the original, and the compressed data is reversable to exact original data. LZW Compression is named after its inventors Abraham Lempel, Jacob Ziv, and Terry Welch.
LZW compression is always used in GIF image files, and offered as an option in TIFF and PostScript.
A particular LZW compression algorithm takes each input sequence of bits of a given length and creates an entry in a table (called a "dictionary" or "codebook") for that particular bit pattern, consisting of the pattern itself and a shorter code. As input is read, any pattern that has been read before results in the substitution of the shorter code, effectively compressing the total amount of input to something smaller.
The following flow chart illistrates the process of compressing data using LZW Compression:
and the following is the C# code of LZW Compression:
LZW compression is always used in GIF image files, and offered as an option in TIFF and PostScript.
A particular LZW compression algorithm takes each input sequence of bits of a given length and creates an entry in a table (called a "dictionary" or "codebook") for that particular bit pattern, consisting of the pattern itself and a shorter code. As input is read, any pattern that has been read before results in the substitution of the shorter code, effectively compressing the total amount of input to something smaller.
The following flow chart illistrates the process of compressing data using LZW Compression:
and the following is the C# code of LZW Compression:
public List<int> LzwCompression()
{
try
{
int dictSize = 256;
var dicList = new List<List<int>>();
for (int i = 0; i < dictSize; i++)
{
var d = new List<int> {i};
dicList.Add(d);
}
List<int> w = new List<int>();
for (int i = 0; i < gifImage.Count; i++)
{
int c = gifImage[i];
List<int> temp = new List<int>();
int m = 0;
foreach (int z in w)
{
temp.Add(w[m]);
m++;
}
temp.Add(c);
if (ListIncluded(dicList, temp))
{
List<int> wc = new List<int>();
int n = 0;
foreach (int z in temp)
{
wc.Add(temp[n]);
n++;
}
w.Clear();
int q = 0;
foreach (int z in wc)
{
w.Add(wc[q]);
q++;
}
}
else
{
result.Add(GetIndex(dicList,w));
List<int> wc = new List<int>();
int n = 0;
foreach (int z in temp)
{
wc.Add(temp[n]);
n++;
}
dicList.Add(wc);
string sum = "";
foreach(int item in wc)
{
sum = sum + " " + item.ToString();
}
w.Clear();
w.Add(c);
}
}
if (w.Count != 0)
{
result.Add(GetIndex(dicList, w));
}
return result;
}
catch (Exception)
{
throw;
}
}
0 komentar:
Post a Comment