C 中的 LZW 解压

发布于 2024-08-13 08:45:18 字数 1626 浏览 8 评论 0原文

我有一个用 C 编写的 LZW 压缩器/解压缩器。

初始表由 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;
}

你能发现它吗?我将不胜感激。

I have an LZW compressor/decompressor written in C.

The initial table consists of ASCII characters and then each now string to be saved into the table consists of a prefix and a character both saved in a list as int.

My compression works but my decompression leaves some characters out.

The input:

<title>Agile</title><body><h1>Agile</h1></body></html>

The output I get (notice the missing 'e' and '<'):

<title>Agile</title><body><h1>Agil</h1></body>/html>

This is the code I use (the relevant part):

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;
}

Can you spot it? I'd be grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反目相谮 2024-08-20 08:45:18

您的解码函数返回字符串中的第一个字符。您需要此字符才能将其添加到字典中,但您不应该为其设置 previousCode。所以你的代码应该是这样的:

...
firstChar = decode(currentCode);
dictionaryAdd(previousCode, firstChar, nextCode++);
previousCode = currentCode;
...

Your decode function returns the first character in the string. You need this character in order to add it to the dictionary, but you should not set previousCode to it. So your code should look like:

...
firstChar = decode(currentCode);
dictionaryAdd(previousCode, firstChar, nextCode++);
previousCode = currentCode;
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文