- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在C++中实现LZW算法。
字典的大小是用户输入的,但最小值为256,因此它应适用于二进制文件。如果到达字典的末尾,它将绕到索引0,然后从那里开始覆盖它。
例如,如果我放入alice in wonderland script并使用字典大小512对其进行压缩,则会得到this dictionary。
但是我对解压缩和从解压缩压缩文件looks like this的输出字典有问题。
我的解压缩代码如下所示
struct dictionary
{
vector<unsigned char> entry;
vector<bool> bits;
};
void decompress(dictionary dict[], vector<bool> file, int dictionarySize, int numberOfBits)
{
//in this example
//dictionarySize = 512, tells the max size of the dictionary, and goes back to 0 if it reaches 513
//numberOfBits = log2(512) = 9
//dictionary dict[] contains bits and strings (strings can be empty)
// dict[0] =
// entry = (unsigned char)0
// bits = (if numberOfBits = 9) 000000001
// dict[255] =
// entry = (unsigned char)255
// bits = (if numberOfBits = 9) 011111111
// so the next entry will be dict[next] (next is currently 256)
// dict[256] =
// entry = what gets added in the code below
// bits = 100000000
// all the bits are already set previously (dictionary size is int dictionarySize) so in this case all the bits from 0 to 511 are already set, entries are set from 0 to 255, so extended ASCII
vector<bool> currentCode;
vector<unsigned char> currentString;
vector<unsigned char> temp;
int next=256;
bool found=false;
for(int i=0;i<file.size();i+=numberOfBits)
{
for(int j=0;j<numberOfBits;j++)
{
currentCode.push_back(file[i+j]);
}
for(int j=0;j<dictionarySize;j++)
{
// when the currentCode (size numberOfBits) gets found in the dictionary
if(currentCode==dict[j].bits)
{
currentString = dict[j].entry;
// if the current string isnt empty, then it means it found the characted in the dictionary
if(!currentString.empty())
{
found = true;
}
}
}
//if the currentCode in the dictionary has a string value attached to it
if(found)
{
for(int j=0;j<currentString.size();j++)
{
cout<<currentString[j];
}
temp.push_back(currentString[0]);
// so it doesnt just push 1 character into the dictionary
// example, if first read character is 'r', it is already in the dictionary so it doesnt get added
if(temp.size()>1)
{
// if next is more than 511, writing to that index would cause an error, so it resets back to 0 and goes back up
if(next>dictionarySize-1) //next > 512-1
{
next = 0;
}
dict[next].entry.clear();
dict[next].entry = temp;
next++;
}
//temp = currentString;
}
else
{
currentString = temp;
currentString.push_back(temp[0]);
for(int j=0;j<currentString.size();j++)
{
cout<<currentString[j];
}
// if next is more than 511, writing to that index would cause an error, so it resets back to 0 and goes back up
if(next>dictionarySize-1)
{
next = 0;
}
dict[next].entry.clear();
dict[next].entry = currentString;
next++;
//break;
}
temp = currentString;
// currentCode gets cleared, and written into in the next iteration
currentCode.clear();
//cout<<endl;
found = false;
}
}
最佳答案
Input: "abacdacacadaad"
step input match output new_entry new_index
a 0
b 1
c 2
d 3
1 abacdacacadaad a 0 ab 4
2 bacdacacadaad b 1 ba 5
3 acdacacadaad a 0 ac 6
4 cdacacadaad c 2 cd 7
5 dacacadaad d 3 da 8
6 acacadaad ac 6 aca 9
7 acadaad aca 9 acad 10
8 daad da 8 daa 11
9 ad a 0 ad 12
10 d d 3
Output: "0102369803"
Input: "0102369803"
step input output new_entry new_index
a 0
b 1
c 2
d 3
1 0 a
2 1 b ab 4
3 0 a ba 5
4 2 c ac 6
5 3 d cd 7
6 6 ac da 8
7 9 aca aca 9
8 8 da acad 10
9 0 a daa 11
10 3 d ad 12
Output: "abacdacacadaad"
//---------------------------------------------------------------------------
// LZW
const int LZW_bits=12; // encoded bitstream size
const int LZW_size=1<<LZW_bits; // dictinary size
// bitstream R/W
DWORD bitstream_tmp=0;
//---------------------------------------------------------------------------
// return LZW_bits from dat[adr,bit] and increment position (adr,bit)
DWORD bitstream_read(BYTE *dat,int siz,int &adr,int &bit,int bits)
{
DWORD a=0,m=(1<<bits)-1;
// save tmp if enough bits
if (bit>=bits){ a=(bitstream_tmp>>(bit-bits))&m; bit-=bits; return a; }
for (;;)
{
// insert byte
bitstream_tmp<<=8;
bitstream_tmp&=0xFFFFFF00;
bitstream_tmp|=dat[adr]&255;
adr++; bit+=8;
// save tmp if enough bits
if (bit>=bits){ a=(bitstream_tmp>>(bit-bits))&m; bit-=bits; return a; }
// end of data
if (adr>=siz) return 0;
}
}
//---------------------------------------------------------------------------
// write LZW_bits from a to dat[adr,bit] and increment position (adr,bit)
// return true if buffer is full
bool bitstream_write(BYTE *dat,int siz,int &adr,int &bit,int bits,DWORD a)
{
a<<=32-bits; // align to MSB
// save tmp if aligned
if ((adr<siz)&&(bit==32)){ dat[adr]=(bitstream_tmp>>24)&255; adr++; bit-=8; }
if ((adr<siz)&&(bit==24)){ dat[adr]=(bitstream_tmp>>16)&255; adr++; bit-=8; }
if ((adr<siz)&&(bit==16)){ dat[adr]=(bitstream_tmp>> 8)&255; adr++; bit-=8; }
if ((adr<siz)&&(bit== 8)){ dat[adr]=(bitstream_tmp )&255; adr++; bit-=8; }
// process all bits of a
for (;bits;bits--)
{
// insert bit
bitstream_tmp<<=1;
bitstream_tmp&=0xFFFFFFFE;
bitstream_tmp|=(a>>31)&1;
a<<=1; bit++;
// save tmp if aligned
if ((adr<siz)&&(bit==32)){ dat[adr]=(bitstream_tmp>>24)&255; adr++; bit-=8; }
if ((adr<siz)&&(bit==24)){ dat[adr]=(bitstream_tmp>>16)&255; adr++; bit-=8; }
if ((adr<siz)&&(bit==16)){ dat[adr]=(bitstream_tmp>> 8)&255; adr++; bit-=8; }
if ((adr<siz)&&(bit== 8)){ dat[adr]=(bitstream_tmp )&255; adr++; bit-=8; }
}
return (adr>=siz);
}
//---------------------------------------------------------------------------
bool str_compare(char *s0,int l0,char *s1,int l1)
{
if (l1<l0) return false;
for (;l0;l0--,s0++,s1++)
if (*s0!=*s1) return false;
return true;
}
//---------------------------------------------------------------------------
AnsiString LZW_encode(AnsiString raw)
{
AnsiString lzw="";
int i,j,k,l;
int adr,bit;
DWORD a;
const int siz=32; // bitstream buffer
BYTE buf[siz];
AnsiString dict[LZW_size]; // dictionary
int dicts=0; // actual size of dictionary
// init dictionary
for (dicts=0;dicts<256;dicts++) dict[dicts]=char(dicts); // full 8bit binary alphabet
// for (dicts=0;dicts<4;dicts++) dict[dicts]=char('a'+dicts); // test alphabet "a,b,c,d"
l=raw.Length();
adr=0; bit=0;
for (i=0;i<l;)
{
i&=i;
// find match in dictionary
for (j=dicts-1;j>=0;j--)
if (str_compare(dict[j].c_str(),dict[j].Length(),raw.c_str()+i,l-i))
{
i+=dict[j].Length();
if (i<l) // add new entry in dictionary (if not end of input)
{
// clear dictionary if full
if (dicts>=LZW_size) dicts=256; // full 8bit binary alphabet
// if (dicts>=LZW_size) dicts=4; // test alphabet "a,b,c,d"
else{
dict[dicts]=dict[j]+AnsiString(raw[i+1]); // AnsiString index starts from 1 hence the +1
dicts++;
}
}
a=j; j=-1; break; // full binary output
// a='0'+j; j=-1; break; // test ASCII output
}
// store result to bitstream
if (bitstream_write(buf,siz,adr,bit,LZW_bits,a))
{
// append buf to lzw
k=lzw.Length();
lzw.SetLength(k+adr);
for (j=0;j<adr;j++) lzw[j+k+1]=buf[j];
// reset buf
adr=0;
}
}
if (bit)
{
// store the remainding bits with zeropad
bitstream_write(buf,siz,adr,bit,LZW_bits-bit,0);
}
if (adr)
{
// append buf to lzw
k=lzw.Length();
lzw.SetLength(k+adr);
for (j=0;j<adr;j++) lzw[j+k+1]=buf[j];
}
return lzw;
}
//---------------------------------------------------------------------------
AnsiString LZW_decode(AnsiString lzw)
{
AnsiString raw="";
int adr,bit,siz,ix;
DWORD a;
AnsiString dict[LZW_size]; // dictionary
int dicts=0; // actual size of dictionary
// init dictionary
for (dicts=0;dicts<256;dicts++) dict[dicts]=char(dicts); // full 8bit binary alphabet
// for (dicts=0;dicts<4;dicts++) dict[dicts]=char('a'+dicts); // test alphabet "a,b,c,d"
siz=lzw.Length();
adr=0; bit=0; ix=-1;
for (adr=0;(adr<siz)||(bit>=LZW_bits);)
{
a=bitstream_read(lzw.c_str(),siz,adr,bit,LZW_bits);
// a-='0'; // test ASCII input
// clear dictionary if full
if (dicts>=LZW_size){ dicts=4; ix=-1; }
// new dictionary entry
if (ix>=0)
{
if (a>=dicts){ dict[dicts]=dict[ix]+AnsiString(dict[ix][1]); dicts++; }
else { dict[dicts]=dict[ix]+AnsiString(dict[a ][1]); dicts++; }
} ix=a;
// update decoded output
raw+=dict[a];
}
return raw;
}
//---------------------------------------------------------------------------
// test ASCII input
行输出:
txt="abacdacacadaad"
enc="0102369803"
dec="abacdacacadaad"
AnsiString
是我使用的唯一VCL东西,它只是自我分配的字符串变量,请注意其索引始于
1
。
AnsiString s;
s[5] // character access (1 is first character)
s.Length() // returns size
s.c_str() // returns char*
s.SetLength(size) // resize
BYTE,DWORD
,请改用
unsigned char
和
unsigned int
...
"a,b,c,d"
字母,也可以使用完整的8it字母。当前设置为8bit。如果要更改它,只需取消rem的
// test ASCII input
行并取消rem的代码中的
// full 8bit binary alphabet
行。
const int LZW_bits=12; // encoded bitstream size
const int LZW_size=1<<LZW_bits; // dictinary size
const int siz=32; // bitstream buffer
bitstream_write
没有经过优化,可以大大提高速度...
AnsiString txt="abacdacacadaadddddddaaaaaaaabcccddaaaaaaaaa",enc,dec,hex;
enc=LZW_encode(txt);
dec=LZW_decode(enc);
// convert to hex
hex=""; for (int i=1,l=enc.Length();i<=l;i++) hex+=AnsiString().sprintf("%02X",enc[i]);
mm_log->Lines->Add("\""+txt+"\"");
mm_log->Lines->Add("\""+hex+"\"");
mm_log->Lines->Add("\""+dec+"\"");
mm_log->Lines->Add(AnsiString().sprintf("ratio: %i%",(100*enc.Length()/dec.Length())));
"abacdacacadaadddddddaaaaaaaabcccddaaaaaaaaa"
"06106206106306410210510406106410FFFFFF910A10706110FFFFFFD10E06206311110910FFFFFFE11410FFFFFFD0"
"abacdacacadaadddddddaaaaaaaabcccddaaaaaaaaa"
ratio: 81%
关于c++ - LZW减压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62086402/
LZW 压缩算法在压缩后增加了位大小: 这是压缩函数的代码: // compression void compress(FILE *inputFile, FILE *outputFile) {
我正在寻找可以压缩和解压缩word文档的c#中的LZW压缩算法。我在谷歌上搜索过它,但它没有给我我需要的答案。任何人都可以帮助我获得它的代码并让我了解如何在我的项目中真正实现 LZW。 最佳答案 有一
我试图让这段代码正常工作,但当我尝试编码时,它似乎无法正常工作。我有一个 60 字节的文本文件。我对其进行编码,输出文件为 100 字节。当我解码该文件时,它变成了 65 字节。它解码正确,但文件大小
我很难理解 LZW 算法。我正在检查维基百科 (http://en.wikipedia.org/w/index.php?title=Lempel-Ziv-Welch&oldid=245292660)
好吧,我必须使用 LZW 算法制作一个 PPM 图像压缩器,我理解该算法和代码并实现了一个字符串版本(在 Java 中,用于测试)。 最大的问题是压缩,因为如果我有: 输入:ABCDABCDABCDA
我目前正在将 LZW 压缩和解压缩方法从 FFmpeg 源代码实现到我的项目。我偶然发现的是输出缓冲区的大小(将存储压缩数据的地方)需要大于我们要压缩的输入缓冲区的大小。这与压缩本身不矛盾吗? 下一部
我找到了 LZW 算法的实现,我想知道如何将其输出(一个 int 列表)表示为字节数组。 我曾尝试使用一个字节,但在长输入的情况下,字典有超过 256 个条目,因此我无法转换。 然后我尝试添加一个额外
我正在编写一个 C 库来将 SDL_Surfaces 导出为各种格式作为练习,到目前为止,我已经搞定了 BMP、TGA 和 PCX 格式。现在我正在制作 GIF格式,我觉得我已经非常接近让它发挥作用了
我正在尝试实现LZW压缩和解压缩技术。我的程序将任何文件作为输入流并将其读入字节数组。然后,它对其应用压缩算法,并在字符串变量中返回编码字节。 然后我应用解压缩算法返回原始数字。 现在为了检索原始文件
我有一个 LZW 算法 - private void start(int maxNumBits) throws IOException{ System.out.println("Beginni
谁能告诉我如何用 c 语言构建 LZW 压缩树?是不是像 结构树{ 短下一个[255]; } 最佳答案 LZW 的树更像是一本字典。每个条目由一个代码(索引)和一个字符组成。树在逻辑上用所有字符初始化
我希望更改 LZW 压缩器,使其能够在 LZW 编码文件中搜索单词并查找该搜索词的匹配数。例如,如果我的文件用作 Prompt:>lzw "searchterm" encoded_file.lzw 3
我做了一些研究,但没有什么真正与我的问题有关...... 我实际上正在尝试为学校编写 LZW 压缩代码,并且我需要一个函数来检查某个元素是否在我的字典中。 但是,当我调用这个函数时,它尝试访问我的字典
这里是函数本身。我在那里遇到了段错误,因为显然我无法将字符串分配给数组中的该值。 clang/gcc 都给我一个警告。 Clang 的好一点,它是“期望 char 分配 char *”。我不知道还有什
我有以下形式的字符串(大约 1-5Kb): FF,A3V,X7Y,aA4,.... lzw 非常好地压缩这些,但包含土耳其字符。然后将它们提交到 MySQL 数据库。 有时 MySQL 可能会“播放”
我正在编写一个通用的 LZW 解码器 c++ 程序,但我无法找到有关所用代码字长度(以位为单位)的文档。我发现一些文章说代码字长 12 位,而其他人说 16 位,还有一些文章说使用可变位长度。那是哪一
所以我正在尝试用 C++ 制作一个简单的文件压缩器/解压缩器(我的目标不是尽可能获得最好的结果)但我真的被卡住了,因为我充满了疑问所以他们在这里: 我应该使用哪种字典(我正在使用 map )?我应该存
CGPDFScanner 会解压缩它遇到的任何 LZW 压缩吗?还是我要为此负责? [我假设它确实如此,因为我正在使用大量的 Tj 运算符等] 最佳答案 是的,CGPDFScanner 将解压缩它找到
当我使用长度为 256 的符号表(字典)时,我的 LZW 压缩工作正常,编码器和解码器都使用 256 并且一切正常但是当我将这个数字增加到例如 512、1024、4096 时,解码文件输出是与第一个输
注意:这不是 LZW 压缩的正确用法。我只是在玩弄它。 问题 在一次传递中,是否可以同时更新字典中元素的频率计数? 我的实现 import sys from collections import de
我是一名优秀的程序员,十分优秀!