LZW-декомпрессия в C
-
11-09-2019 - |
Вопрос
У меня есть компрессор/декомпрессор LZW, написанный на C.
Исходная таблица состоит из символов ASCII, а затем каждая строка, сохраняемая в таблице, состоит из префикс и характер оба сохранены в списке как int.
Мое сжатие работает, но при распаковке некоторые символы остаются без внимания.
Вход:
<title>Agile</title><body><h1>Agile</h1></body></html>
Результат, который я получаю (обратите внимание на отсутствующие «e» и «<»):
<title>Agile</title><body><h1>Agil</h1></body>/html>
Это код, который я использую (соответствующая часть):
void expand(int * input, int inputSize) {
// int prevcode, currcode
int previousCode; int currentCode;
int nextCode = 256; // start with the same dictionary of 255 characters
dictionaryInit();
// prevcode = read in a code
previousCode = input[0];
int pointer = 1;
// while (there is still data to read)
while (pointer < inputSize) {
// currcode = read in a code
currentCode = input[pointer++];
if (currentCode >= nextCode) printf("!"); // XXX not yet implemented!
currentCode = decode(currentCode);
// add a new code to the string table
dictionaryAdd(previousCode, currentCode, nextCode++);
// prevcode = currcode
previousCode = currentCode;
}
}
int decode(int code) {
int character; int temp;
if (code > 255) { // decode
character = dictionaryCharacter(code);
temp = decode(dictionaryPrefix(code)); // recursion
} else {
character = code; // ASCII
temp = code;
}
appendCharacter(character); // save to output
return temp;
}
Можете ли вы это заметить?Я был бы благодарен.
Решение
Ваша функция декодирования возвращает первый символ строки.Вам нужен этот символ, чтобы добавить его в словарь, но вы должны нет набор previousCode
к этому.Итак, ваш код должен выглядеть так:
...
firstChar = decode(currentCode);
dictionaryAdd(previousCode, firstChar, nextCode++);
previousCode = currentCode;
...
Не связан с StackOverflow