Я мало знаю об алгоритмах сжатия. Я ищу простой алгоритм сжатия (или фрагмент кода), который может уменьшить размер байта [,,] или байт []. Я не могу использовать System.IO.Compression. Кроме того, данные имеют много повторений.C# сжать байтовый массив
Я пробовал реализовать алгоритм RLE (размещен ниже для вашего осмотра). Тем не менее, он увеличивает размер массива в 1,2-1,8 раза.
public static class RLE
{
public static byte[] Encode(byte[] source)
{
List<byte> dest = new List<byte>();
byte runLength;
for (int i = 0; i < source.Length; i++)
{
runLength = 1;
while (runLength < byte.MaxValue
&& i + 1 < source.Length
&& source[i] == source[i + 1])
{
runLength++;
i++;
}
dest.Add(runLength);
dest.Add(source[i]);
}
return dest.ToArray();
}
public static byte[] Decode(byte[] source)
{
List<byte> dest = new List<byte>();
byte runLength;
for (int i = 1; i < source.Length; i+=2)
{
runLength = source[i - 1];
while (runLength > 0)
{
dest.Add(source[i]);
runLength--;
}
}
return dest.ToArray();
}
}
Я также нашел реализацию java, string и integer, LZW. Я преобразовал его в C#, и результаты выглядят хорошо (код указан ниже). Однако я не уверен, как это работает, и как заставить его работать с байтами вместо строк и целых чисел.
public class LZW
{
/* Compress a string to a list of output symbols. */
public static int[] compress(string uncompressed)
{
// Build the dictionary.
int dictSize = 256;
Dictionary<string, int> dictionary = new Dictionary<string, int>();
for (int i = 0; i < dictSize; i++)
dictionary.Add("" + (char)i, i);
string w = "";
List<int> result = new List<int>();
for (int i = 0; i < uncompressed.Length; i++)
{
char c = uncompressed[i];
string wc = w + c;
if (dictionary.ContainsKey(wc))
w = wc;
else
{
result.Add(dictionary[w]);
// Add wc to the dictionary.
dictionary.Add(wc, dictSize++);
w = "" + c;
}
}
// Output the code for w.
if (w != "")
result.Add(dictionary[w]);
return result.ToArray();
}
/* Decompress a list of output ks to a string. */
public static string decompress(int[] compressed)
{
int dictSize = 256;
Dictionary<int, string> dictionary = new Dictionary<int, string>();
for (int i = 0; i < dictSize; i++)
dictionary.Add(i, "" + (char)i);
string w = "" + (char)compressed[0];
string result = w;
for (int i = 1; i < compressed.Length; i++)
{
int k = compressed[i];
string entry = "";
if (dictionary.ContainsKey(k))
entry = dictionary[k];
else if (k == dictSize)
entry = w + w[0];
result += entry;
// Add w+entry[0] to the dictionary.
dictionary.Add(dictSize++, w + entry[0]);
w = entry;
}
return result;
}
}
«Я не могу использовать System.IO.Compression» - почему? –
, чтобы немного рассказать о том, что сказал Митч, есть другие библиотеки (например [SharpZipLib] (http: //www.icsharpcode.net/opensource/sharpziplib /)), поэтому понимание того, почему вы не можете использовать существующие материалы в рамках, поможет выяснить, какие другие варианты могут работать или нет. –
Ну, его недоступно на моей платформе (xbox 360). – zfedoran