本篇文章给大家谈谈c语言实现lz78压缩,以及lz77图像压缩算法代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、如何用c语言实现压缩图片内存大小2、如何用c语言压缩解压文件夹3、如何用C语言实现数据压缩4、(20分)用C语言编译的文件压缩解压缩程序5、C语言实现文件压缩6、C语言都有哪些经典的无损压缩算法
如何用c语言实现压缩图片内存大小
是(row,col,value),这样把所有不为零的值组成一个向量。这种存储方式比二维数组节省了不少空间,当然还可以进一步节省,因为三元组里面row或者col重复存储了,一行或者一列存一次就行了,按这种思路走下去就是行压缩存储了。
那具体什么是行压缩存储呢?行压缩存储的思想就是,把所有不为零的值按行访问的顺序组成一个向量,然后再把每一行值不为0的列的下标存下来,这个两个向量的大小和稀疏矩阵中不为0的值得个数相同,当然要实现对行压缩矩阵的访问,还要把每一行的不为0的列的下标在第二个向量中开始的位置存下来,有人把这个叫做指针。有了这三个向量就可以实现对矩阵实现高效的按行访问了。行压缩存储比三元组优秀的不仅是空间的压缩,还有就是行访问时的高效。三元组如果是有序的,可以二分查找来访问一行,但是行压缩存储按行访问时的时间复杂度是常数级的。 大家可以参考下面这个行压缩矩阵示意图:
如何用c语言压缩解压文件夹
压缩是一种有效的减小数据量的方法,目前已经被广泛应用于各种类型的信息系统之中。
一种压缩文本文件的方法如下:
1. 原始文本文件中的非字母的字符,直接拷贝到压缩文件中;
2.
原始文件中的词(全部由字母组成),如果是第一次出现,则将该词加入到一个词的列表中,并拷贝到压缩文件中;否则该词不拷贝到压缩文件中,而是将该词在词的列表中的位置拷贝到压缩文件中。
3. 词的列表的起始位置为 1 。 词的定义为文本中由大小写字母组成的最大序列。大写字母和小写字母认为是不同的字母,即 abc 和 Abc
是不同的词。词的例子如下: * x-ray 包括两个词 x 和 ray * mary’s 包括两个词 mary 和 s * a c-Dec 包括三个词 a 和
c 和 Dec 编写一个程序,输入为一组字符串,输出为压缩后的文本。
输入:
输入为一段文本,你可以假设输入中不会出现数字、每行的长度不会超过 80 个字符,并且输入文本的大小不会超过 10M。
输出:
压缩后的文本。
输入:
Please, please do it–it would please Mary very,
very much.
Thanks
输出:
Please, please do it–4 would 2 Mary very,
7 much.
Thanks
#include stdlib.h
#include stdio.h
#include string.h
#define LEN 120
int isArabic(char c){
return (‘a’=cc=’z’) || (‘A’=cc=’Z’);
}
int main()
{
char dict[LEN];
char *index[100000];
char buf[82];
int nWord=0;
int i,j;
char c;
char *inFile=”G:\\in.txt”,*outFile=”G:\\out.txt”;
FILE *inp,*outp;
if((inp=fopen(inFile,”r”))==NULL){
printf(“cannot open\n”);
exit(1);
}
if((outp=fopen(outFile,”w”))==NULL){
printf(“out fail\n”);
}
index[0]=dict;
do{
/* get a word */
i=0;
do{
c=fgetc(inp);
buf[i++]=c;
}while(isArabic(c));
buf[i-1]=0;
/* put it to dict */
if(i1){
for(j=0;jnWord;j++){
if(strcmp(index[j],buf)==0){
break;
}
}
if(j==nWord){
strcpy(index[nWord],buf);
index[nWord+1]=index[nWord]+strlen(buf)+1;
nWord++;
/* printf(“new: %s\n”,buf);*/
}else{
sprintf(buf,”%d”,j+1);
/* printf(“found: %s\n”,buf);*/
}
}
/* put it to output file */
if(c!=EOF)
fprintf(outp,”%s%c”,buf,c);
else
fprintf(outp,”%s”,buf);
}while(c!=EOF);
fclose(inp);
fclose(outp);
/* system(“PAUSE”);*/
return EXIT_SUCCESS;
}
如何用C语言实现数据压缩
首先选择一个压缩算法
然后按照算法实现压缩代码,调用接口就可以
常见的 可以使用哈夫曼编码压缩,或者使用开源的压缩代码,比如lzo, gzip, lzma等等。
(20分)用C语言编译的文件压缩解压缩程序
是用霍夫曼树做的
#include stdio.h
#include string.h
#include stdlib.h
#include conio.h
struct head
{
unsigned char b; /*the charactor*/
long count; /*the frequency*/
long parent,lch,rch; /*make a tree*/
char bits[256]; /*the haffuman code*/
}
header[512],tmp;
void compress()
{
char filename[255],outputfile[255],buf[512];
unsigned char c;
long i,j,m,n,f;
long min1,pt1,flength;
FILE *ifp,*ofp;
printf(“source filename:”);
gets(filename);
ifp=fopen(filename,”rb”);
if(ifp==NULL)
{
printf(“source file open error!\n”);
return;
}
printf(“destination filename:”);
gets(outputfile);
ofp=fopen(outputfile,”wb”);
if(ofp==NULL)
{
printf(“destination file open error!\n”);
return;
}
flength=0;
while(!feof(ifp))
{
fread(c,1,1,ifp);
header[c].count++;
flength++;
}
flength–;
header[c].count–;
for(i=0;i512;i++)
{
if(header[i].count!=0) header[i].b=(unsigned char)i;
else header[i].b=0;
header[i].parent=-1;
header[i].lch=header[i].rch=-1;
}
for(i=0;i256;i++)
{
for(j=i+1;j256;j++)
{
if(header[i].countheader[j].count)
{
tmp=header[i];
header[i]=header[j];
header[j]=tmp;
}
}
}
for(i=0;i256;i++) if(header[i].count==0) break;
n=i;
m=2*n-1;
for(i=n;im;i++)
{
min1=999999999;
for(j=0;ji;j++)
{
if(header[j].parent!=-1) continue;
if(min1header[j].count)
{
pt1=j;
min1=header[j].count;
continue;
}
}
header[i].count=header[pt1].count;
header[pt1].parent=i;
header[i].lch=pt1;
min1=999999999;
for(j=0;ji;j++)
{
if(header[j].parent!=-1) continue;
if(min1header[j].count)
{
pt1=j;
min1=header[j].count;
continue;
}
}
header[i].count+=header[pt1].count;
header[i].rch=pt1;
header[pt1].parent=i;
}
for(i=0;in;i++)
{
f=i;
header[i].bits[0]=0;
while(header[f].parent!=-1)
{
j=f;
f=header[f].parent;
if(header[f].lch==j)
{
j=strlen(header[i].bits);
memmove(header[i].bits+1,header[i].bits,j+1);
header[i].bits[0]=’0′;
}
else
{
j=strlen(header[i].bits);
memmove(header[i].bits+1,header[i].bits,j+1);
header[i].bits[0]=’1′;
}
}
}
fseek(ifp,0,SEEK_SET);
fwrite(flength,sizeof(int),1,ofp);
fseek(ofp,8,SEEK_SET);
buf[0]=0;
f=0;
pt1=8;
while(!feof(ifp))
{
c=fgetc(ifp);
f++;
for(i=0;in;i++)
{
if(c==header[i].b) break;
}
strcat(buf,header[i].bits);
j=strlen(buf);
c=0;
while(j=8)
{
for(i=0;i8;i++)
{
if(buf[i]==’1′) c=(c1)|1;
else c=c1;
}
fwrite(c,1,1,ofp);
pt1++;
strcpy(buf,buf+8);
j=strlen(buf);
}
if(f==flength) break;
}
if(j0)
{
strcat(buf,”00000000″);
for(i=0;i8;i++)
{
if(buf[i]==’1′) c=(c1)|1;
else c=c1;
}
fwrite(c,1,1,ofp);
pt1++;
}
fseek(ofp,4,SEEK_SET);
fwrite(pt1,sizeof(long),1,ofp);
fseek(ofp,pt1,SEEK_SET);
fwrite(n,sizeof(long),1,ofp);
for(i=0;in;i++)
{
fwrite((header[i].b),1,1,ofp);
c=strlen(header[i].bits);
fwrite(c,1,1,ofp);
j=strlen(header[i].bits);
if(j%8!=0)
{
for(f=j%8;f8;f++)
strcat(header[i].bits,”0″);
}
while(header[i].bits[0]!=0)
{
c=0;
for(j=0;j8;j++)
{
if(header[i].bits[j]==’1′) c=(c1)|1;
else c=c1;
}
strcpy(header[i].bits,header[i].bits+8);
fwrite(c,1,1,ofp);
}
}
fclose(ifp);
fclose(ofp);
printf(“compress successfully!\n”);
return;
}
void uncompress()
{
char filename[255],outputfile[255],buf[255],bx[255];
unsigned char c;
long i,j,m,n,f,p,l;
long flength;
FILE *ifp,*ofp;
printf(“source filename:”);
gets(filename);
ifp=fopen(filename,”rb”);
if(ifp==NULL)
{
printf(“source file open error!\n”);
return;
}
printf(“destination filename:”);
gets(outputfile);
ofp=fopen(outputfile,”wb”);
if(ofp==NULL)
{
printf(“destination file open error!\n”);
return;
}
fread(flength,sizeof(long),1,ifp);
fread(f,sizeof(long),1,ifp);
fseek(ifp,f,SEEK_SET);
fread(n,sizeof(long),1,ifp);
for(i=0;in;i++)
{
fread(header[i].b,1,1,ifp);
fread(c,1,1,ifp);
p=(long)c;
header[i].count=p;
header[i].bits[0]=0;
if(p%80) m=p/8+1;
else m=p/8;
for(j=0;jm;j++)
{
fread(c,1,1,ifp);
f=c;
itoa(f,buf,2);
f=strlen(buf);
for(l=8;lf;l–)
{
strcat(header[i].bits,”0″);
}
strcat(header[i].bits,buf);
}
header[i].bits[p]=0;
}
for(i=0;in;i++)
{
for(j=i+1;jn;j++)
{
if(strlen(header[i].bits)strlen(header[j].bits))
{
tmp=header[i];
header[i]=header[j];
header[j]=tmp;
}
}
}
p=strlen(header[n-1].bits);
fseek(ifp,8,SEEK_SET);
m=0;
bx[0]=0;
while(1)
{
while(strlen(bx)(unsigned int)p)
{
fread(c,1,1,ifp);
f=c;
itoa(f,buf,2);
f=strlen(buf);
for(l=8;lf;l–)
{
strcat(bx,”0″);
}
strcat(bx,buf);
}
for(i=0;in;i++)
{
if(memcmp(header[i].bits,bx,header[i].count)==0) break;
}
strcpy(bx,bx+header[i].count);
c=header[i].b;
fwrite(c,1,1,ofp);
m++;
if(m==flength) break;
}
fclose(ifp);
fclose(ofp);
printf(“Uncompress successfully!\n”);
return;
}
int main()
{
int c;
printf(“1–Compress file\n”);
printf(“2–Uncompress file\n”);
printf(“Select 1 or 2:”);
c=getch();
printf(“%c\n”,c);
if(c==’1′) compress();
else if(c==’2′) uncompress();
return 0;
}
C语言实现文件压缩
typedef int (WINAPI ICEPUB_COMPRESSFILE)(char *strFilename, char *strZipFilename);
ICEPUB_COMPRESSFILE *icePub_compressFile = 0;
HINSTANCE hDLLDrv = LoadLibrary(“icePubDll.dll”);
if(hDLLDrv)
{
icePub_compressFile = (ICEPUB_COMPRESSFILE *)GetProcAddress(hDLLDrv, “icePub_compressFile”);
}
if(icePub_compressFile)
icePub_compressFile(“a.exe”,”a.Z”);
if(hDLLDrv)
FreeLibrary(hDLLDrv);
typedef int (WINAPI ICEPUB_UNCOMPRESSFILE)(char *strZipFilename,char *strFilename);
ICEPUB_UNCOMPRESSFILE *icePub_uncompressFile = 0;
HINSTANCE hDLLDrv = LoadLibrary(“icePubDll.dll”);
if(hDLLDrv)
{
icePub_uncompressFile = (ICEPUB_UNCOMPRESSFILE *)GetProcAddress(hDLLDrv, “icePub_uncompressFile”);
}
if(icePub_uncompressFile)
icePub_uncompressFile(“a.Z”,”a.exe”);
if(hDLLDrv)
FreeLibrary(hDLLDrv);
C语言都有哪些经典的无损压缩算法
C语言经典的无损压缩算法有:哈夫曼算法、LZ。
哈夫曼算法:
哈夫曼编码是David A. Huffman于1952年发明的一种满足对编码算法要求的一种编码算法。
哈夫曼算法是利用频率信息构造一棵二叉树,频率高的离根节点近(编码长度短),频率低的离根节点远(编码长度长),手动构造方法是先将字母按照频率从小到大排序,然后不断选择当前还没有父节点的节点中权值最小的两个,构造新的父节点,父节点的值为这两个节点值的和,直到构造成一棵二叉树。
LZ算法:
LZ算法及其衍生变形算法是压缩算法的一个系列。LZ77和LZ78算法分别在1977年和1978年被创造出来。虽然他们名字差不多,但是算法方法完全不同。这一系列算法主要适用于字母数量有限的信息,比如文字、源码等。流行的GIF和PNG格式的图像,使用颜色数量有限的颜色空间,其压缩就采用了两种算法的灵活变形应用。
c语言实现lz78压缩的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于lz77图像压缩算法代码、c语言实现lz78压缩的信息别忘了在本站进行查找喔。