Bmp图像的哈夫曼编码代码.doc_第1页
Bmp图像的哈夫曼编码代码.doc_第2页
Bmp图像的哈夫曼编码代码.doc_第3页
Bmp图像的哈夫曼编码代码.doc_第4页
Bmp图像的哈夫曼编码代码.doc_第5页
免费预览已结束,剩余2页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Bmp图像的哈夫曼编码代码#include #include #include #include #include #include #include typedef structunsigned int weight;unsigned int parent,lchild,rchild;HTNode, *huffmantree;/ 动态分配数组存储赫夫曼树typedef char *huffmancode;/ 动态分配数组存储赫夫曼编码表int power(int i)int j,result=1;for(j=0;j0;i-)if(temp8-i!=0)result+=power(i-1);strcpy(temp,temp+8);void select(huffmantree HT, unsigned int i, unsigned int *s1, unsigned int *s2)/ s1为最小的两个值中序号小的那个unsigned int j,temp;for(j=1;j=i;j+)if(HTj.parent=0)*s2=j;break;for(;j=i;j+)if(HTj.parent=0 & j!=*s2)*s1=j;break;for(j=1;j=i;j+)if(HTj.parent=0 & HTj.weight HT*s1.weight & *s2!=j)*s1=j;for(j=1;j=i;j+)if(HTj.parent=0 & HTj.weight *s2)temp=*s1;*s1=*s2;*s2=temp;void huffmancoding(huffmantree &HT, huffmancode &HC,unsigned long int *w, unsigned int n)/ w存放n个字符的权值(均0),构造赫夫曼树HT,并求出n个字符的赫夫曼编码HCunsigned int s1,s2,start,c,f,i,m;/ 0号单元未用huffmantree p;char *cd;if (n=1) return;m=2*n-1;HT=(huffmantree)malloc(m+1)*sizeof(HTNode);/malloc 向系统申请分配指定size个字节的内存空间。for(p=HT+1,i=1; iweight=*w;p-parent=0;p-lchild=0;p-rchild=0;for(; iweight=0;p-parent=0;p-lchild=0;p-rchild=0;for(i=n+1; i=m; +i)/ 建赫夫曼树 / 在HT1i-1中选择parent为0且weight最小的两个结点,其序号分别为s1和s2select(HT,i-1,&s1,&s2);HTs1.parent=i; HTs2.parent=i;HTi.lchild=s1; HTi.rchild=s2;HTi.weight=HTs1.weight+HTs2.weight; / 从叶子到根逆向求每个字符的赫夫曼编码HC=(huffmancode)malloc(n+1)*sizeof(char *);/ 分配n个字符编码的头指针向量(0不用cd=(char *)malloc(n*sizeof(char); cdn-1=0;/ 编码结束符for(i=1; i=n; +i)/ 逐个字符求赫夫曼编码start=n-1;/ 编码结束符位置for(c=i,f=HTi.parent; f!=0; c=f,f=HTf.parent)/ 从叶子到根逆向求编码if(HTf.lchild=c)cd-start=0;else cd-start=1;HCi=(char *)malloc(n-start)*sizeof(char); / 为第i个字符编码分配空间strcpy(HCi,&cdstart);/ 从cd复制编码(串)到HCfree(cd);/ 释放工作空间void main()char w256=0;char temp40=0;unsigned char len;unsigned long int count256=0,weight256=0,n=0,i=0;unsigned int q=0;unsigned long int *p;char filename260=0,filename2260=0,houzui8=0;char *point;char colorplat1024;FILE *fp,*fp2;huffmantree HT;huffmancode HC;BITMAPFILEHEADER bf;/BMP文件头结构体 BITMAPINFOHEADER bi;/BMP信息头结构体 /打开文件 printf(input the file name :);gets(filename);/从stdin流中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放在str指针所指向的字符数组中。strcpy(filename2,filename);for(point=filename2+strlen(filename2); *point!=.; point-);/文件名修改strcpy(houzui,point);strcpy(point,.shq);if(fp=fopen(filename,rb)=NULL)/fopen函数用来打开一个文件printf(errorn);exit(1);/读取信息头、文件头 fread( &bf,sizeof(BITMAPFILEHEADER), 1, fp ); /把指针fp所指向的文件的头信息写入bf(地址) fread( &bi,sizeof(BITMAPINFOHEADER), 1, fp ); if(bf.bfType != 0x4d42) /校验图片类型printf(文件非bmp格式 n);exit(1);if(bi.biBitCount24)int tm;(int)tm=pow(2.,bi.biBitCount);fread(colorplat,4*tm,1,fp);while(fread(&q,1,1,fp)countq+;fclose(fp);for(p=count;i256;p+,i+)weightn+=*p;huffmancoding(HT,HC,weight,n);for(i=0,n=1;i256;i+)if(counti!=0)printf(%d: ,i);printf(%sn,HCn+);if(fp=fopen(filename,rb)=NULL) /只读打开或建立一个二进制文件,只允许读数据printf(errorn);exit(1);if(fp2=fopen(filename2,wb)=NULL) /只写打开或建立一个二进制文件,只允许写数据printf(errorn);exit(1);fwrite( &bf,sizeof(BITMAPFILEHEADER), 1 ,fp2 );fwrite( &bi,sizeof(BITMAPINFOHEADER), 1, fp2 );if(bi.biBitCount24)/调用套色板fwrite(colorplat,4*(int)pow(2.,bi.biBitCount),1,fp2);fwrite(h

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论