数据结构LZW的压缩与解压实验报告.doc

数据结构LZW的压缩与解压实验报告.doc

ID:55773592

大小:320.50 KB

页数:34页

时间:2020-06-07

上传者:U-5097
数据结构LZW的压缩与解压实验报告.doc_第1页
数据结构LZW的压缩与解压实验报告.doc_第2页
数据结构LZW的压缩与解压实验报告.doc_第3页
数据结构LZW的压缩与解压实验报告.doc_第4页
数据结构LZW的压缩与解压实验报告.doc_第5页
资源描述:

《数据结构LZW的压缩与解压实验报告.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

数据结构课程设计报告-------LZW压缩与解压学院:计算机学院专业:计算机科学与技术班级:2013级1班姓名:傅娴雅学号:4 目录一、问题描述二、设计三、测试结果四、分析与探究五、总结六、附录 一、问题描述(1)系统功能1.在一个文本文件上实现LZW压缩和解压缩,其中每个字符就是该文本的8位ASCII码2.在实现LZW过程中需要仔细考虑在编译表中找到匹配或找不到匹配,需要注意匹配算法的时间、空间开销3.*应用LZW算法实现256色灰度BMP图像文件的压缩和解压缩(2)输入要求用MFC做出了图形化界面,不再需要命令行输入,故输入只需在编辑框输入正确的地址或者直接选择。(3)输出要求消息框提示操作成功,能生成压缩或者解压缩之后的文件。(4)界面设计用MFC生成相应界面,能对压缩或者解压缩操作进行选择,减少界面的冗余度。(5)测试设计找一篇3000字的文章放于txt文件中,测试程序能否压缩成功及压缩的速率。同时利用压缩的文件进行解压缩。照一张256色灰度BMP图像,进行相应的压缩与解压缩过程二、设计(1)系统设计LZW压缩算法是一种新颖的压缩方法,由Lemple-Ziv-Welch 三人共同创造,用他们的名字命名。它采用了一种先进的串表压缩,将每个第一次出现的串放在一个串表中,用一个数字来表示串,压缩文件只存贮数字,则不存贮串,从而使图象文件的压缩效率得到较大的提高。奇妙的是,不管是在压缩还是在解压缩的过程中都能正确的建立这个串表,压缩或解压缩完成后,这个串表又被丢弃。词典编码主要利用数据本身包含许多重复的字符串的特性,用一些简单的代号代替那些重复出现的字符串,从而实现压缩,实际上就是利用了信源符号之间的相关性。字符串与代号的对应表就是词典。实用的词典编码算法的核心就是如何动态地形成词典,以及如何选择输出格式以减小冗余。在本次实验中所用到的LZW(Lempel-ZivWalch)压缩编码方法。LZW的核心思想是用短的编码代替字符串。它不对输入的字符串做任何分析,只是将收到的每一个新字符串添加到一张字符串表中。当已经出现的字符串再次出现,即用一个短的编码代替该字符串,这就实现了压缩。(2)数据结构预算法设计此次实验中采用链表散列的存储方式,牺牲空间以换取时间。采用二进制形式输入,除数设为4096,对前256位初始化,空间用完时丢弃,在后面进行解压过程中重建。A.压缩算法:•1.从输入流中读入一个字符,作为当前串的后缀。•2.如果当前串在字典中,就用当前串的编码作前缀,转到第1步。•3.如果当前串不在字典中,就把当前串放到字典中,并把前缀放到输出流,后缀变前缀,转到第1步。 •4.当输入流读完了后,串中应该还剩一个前缀,把它放到输出流,结束。解压缩算法与压缩算法类似一、测试结果(1)测试过程 (2)测试问题能够实现文本的压缩与解压缩,且压缩过程速度较快,bmp图像的压缩过程也能实现,但是解压缩过程并不能实现。四.分析与探究(1)测试结果分析借助MFC使得程序可视化程度较高,程序运行速度较快,然而最终没能完成所要求的图形的压缩与解压缩过程,程序面向的对象相对来说较为狭窄。(2)思考在网上有查到可以利用树结构来存储,可能会进一步减小程序的时间复杂度。然而没有能够很明白这种结构,最终还是采用了课本上提供的算法思想五.总结在课本上找得到这个问题的解决方法,然而按照课本上的程序,在进行压缩和解压缩的步骤分开,使得整个方法变得特别的冗余,利用MFC进行第一次修改之后在一定程度上有所改善,在最后一次进行修改之后终于达到了想要的效果。 总而言之,这是一次特别好的开发经历,其中各种波折让我对于数据结构的选择,等等一些列问题有了新的认知。五.附录以下是MFC中自己建的类1.对字符进行操作#include"stdafx.h"#include"stdio.h"#include"memory.h"#include"assert.h"#include"lzwtable.h"#include"lzwcode.h"/*CDecodeBitArray*/CDecodeBitArray::CDecodeBitArray(DWORDdwInitWidth)//widthinbit{m_pbBits=NULL;m_dwWidthInByte=0;m_dwTail=m_dwHead=0;if(dwInitWidth)InitBits(dwInitWidth);}CDecodeBitArray::~CDecodeBitArray(){ClearBits();}voidCDecodeBitArray::ClearBits(void){if(m_pbBits)deletem_pbBits;m_dwTail=m_dwHead=0;m_dwWidthInByte=0;}voidCDecodeBitArray::InitBits(DWORDdwInitWidth){//newClearBits();DWORDdwLength=dwInitWidth/8;dwLength+=(dwInitWidth%8)?1:0; m_pbBits=newBYTE[dwLength];m_dwHead=m_dwTail=0;m_dwWidthInByte=dwLength;}voidCDecodeBitArray::InitBytes(DWORDdwInitWidth){//newInitBits(dwInitWidth*8);}DWORDCDecodeBitArray::GetLeftBytes(void){DWORDdwLeftBytes;DWORDdwLeftBits=GetLeftBits();dwLeftBytes=dwLeftBits/8;dwLeftBytes+=(dwLeftBits%8)?1:0;returndwLeftBytes;}WORDCDecodeBitArray::RemoveBits(intiWidth){WORDwGet=0;for(inti=iWidth-1;i>=0;i--){if(RemoveFirstBit())SET_BIT_1(wGet,i);}returnwGet;}WORDCDecodeBitArray::RemoveFirstBit(void){BYTE*pbFirstByte=m_pbBits+m_dwHead/8;BYTEbFirstByte=*pbFirstByte;WORDwGet=(CHECK_BIT_1(bFirstByte,7-m_dwHead%8))?1:0;m_dwHead++;returnwGet;}BOOLCDecodeBitArray::AddBytes(BYTE*pbAdd,intiLength){if(m_pbBits==NULL)returnFALSE;Resort();memcpy(m_pbBits+m_dwTail/8,pbAdd,iLength);m_dwTail+=8*(DWORD)iLength;returnTRUE;}voidCDecodeBitArray::Resort(void) {//becausem_dwTail%8alwaysequal0if(m_dwHead<8)return;if(m_dwTail==m_dwHead){m_dwTail=m_dwHead=0;return;}DWORDdwLength=GetLeftBytes();DWORDdwHead=m_dwHead%8;DWORDdwMove=m_dwHead-dwHead;memcpy(m_pbBits,m_pbBits+(m_dwHead/8),(int)dwLength);m_dwHead=dwHead;m_dwTail-=dwMove;}/*classCEncodeBitArray*/CEncodeBitArray::CEncodeBitArray(DWORDdwInitWidth){if(dwInitWidth==0)m_pbBits=NULL;elseInitBits(dwInitWidth);}CEncodeBitArray::~CEncodeBitArray(){ClearBits();}BOOLCEncodeBitArray::InitBits(DWORDdwInitWidth){ClearBits();assert(dwInitWidth);m_dwWidthInByte=dwInitWidth/8;m_dwWidthInByte+=(dwInitWidth%8)?1:0;m_pbBits=newBYTE[m_dwWidthInByte];m_dwTail=0;returnTRUE;}voidCEncodeBitArray::ClearBits(void){if(m_pbBits)deletem_pbBits; m_pbBits=NULL;}BOOLCEncodeBitArray::AddBits(WORDwAdd,intiWidth){if(m_pbBits==NULL)returnFALSE;for(inti=iWidth-1;i>=0;i--){BYTE*pbByte=m_pbBits+m_dwTail/8;if(CHECK_BIT_1(wAdd,i))SET_BIT_1(*pbByte,7-m_dwTail%8);elseSET_BIT_0(*pbByte,7-m_dwTail%8);m_dwTail++;}returnTRUE;}DWORDCEncodeBitArray::GetBytesWidth(void){DWORDdwBytes=m_dwTail/8;dwBytes+=(m_dwTail%8)?1:0;returndwBytes;}intCEncodeBitArray::RemoveBytes(BYTE*pbGet,intiWant){if(m_pbBits==NULL)return-1;intiTotal=(int)GetBytesWidth();if(iWant>iTotal)iWant=iTotal;if(pbGet!=NULL)memcpy(pbGet,m_pbBits,iWant);memcpy(m_pbBits,m_pbBits+iWant,iTotal-iWant);intiTail=(int)m_dwTail;iTail-=iWant*8;if(iTail<0)iTail=0;m_dwTail=iTail;returniWant;}BOOLCLZWDecode::BeginLZWDecode(constDWORDdwLength,//thecompressedlengthFUN_LZWDECODEGETNEXTBYTESpfunLZWGetNextBytes, FUN_LZWDECODEPUTNEXTBYTEpfunLZWPutNextByte,WORDwBuffer,FUN_LZWDECODEDBYTESpfunLZWDecodedBytes,DWORDdwBytesOnce){m_dwDecodedByte=0;//printf("enterdecodepressakey ");BYTE*pbNewData=newBYTE[4000],*pbOutData=newBYTE[4000];BYTE*pbBuffer=newBYTE[wBuffer];BYTEbFirst;m_LZWTable.InitLZWTable();intiBitWidth=9;m_iTotalEntry=LZW_BEGIN_ENTRY;BYTE*pbDecodedData;WORDwOld,wLastLen;m_baContain.InitBits((wBuffer+20)*8);intiR=0;DWORDdwRead=0;while(1){if(m_baContain.GetLeftBytes()<5){WORDwGetBytes=wBuffer;if((DWORD)wGetBytes+dwRead>dwLength)wGetBytes=(WORD)(dwLength-dwRead);if(wGetBytes!=0){pfunLZWGetNextBytes(pbBuffer,wGetBytes);m_baContain.AddBytes(pbBuffer,wBuffer);dwRead+=wGetBytes;}}intiT=m_iTotalEntry+1;iT>>=9;iBitWidth=9;while(iT>0){iT>>=1;iBitWidth++;}WORDwGet=m_baContain.RemoveBits(iBitWidth);if(wGet==LZW_END_CODE){ break;}if(wGet==LZW_CLEAR_CODE){m_LZWTable.InitLZWTable();wGet=m_baContain.RemoveBits(9);if(wGet==LZW_END_CODE)break;pbDecodedData=m_LZWTable.GetMatchData(wGet);WriteDecode(pbDecodedData,pfunLZWPutNextByte,pfunLZWDecodedBytes,dwBytesOnce);wOld=wGet;m_iTotalEntry=258;}else{//notclearpbDecodedData=m_LZWTable.GetMatchData(wGet);if(NULL!=pbDecodedData){//intablebFirst=pbDecodedData[2];WriteDecode(pbDecodedData,pfunLZWPutNextByte,pfunLZWDecodedBytes,dwBytesOnce);if(wOld!=LZW_CLEAR_CODE){//notthefirstcodebereadinpbDecodedData=m_LZWTable.GetMatchData(wOld);wLastLen=*((WORD*)pbDecodedData);memcpy(pbNewData,pbDecodedData+2,wLastLen);pbNewData[wLastLen]=bFirst;m_LZWTable.AddToChild((WORD)m_iTotalEntry,pbNewData,wLastLen+1);m_iTotalEntry+=1;}wOld=wGet;}else{pbDecodedData=m_LZWTable.GetMatchData(wOld);bFirst=pbDecodedData[2];wLastLen=*((WORD*)pbDecodedData);memcpy(pbOutData+2,pbDecodedData+2,wLastLen); pbOutData[wLastLen+2]=bFirst;*((WORD*)pbOutData)=wLastLen+1;WriteDecode(pbOutData,pfunLZWPutNextByte,pfunLZWDecodedBytes,dwBytesOnce);if(m_iTotalEntry>=4096){int_j=0;}m_LZWTable.AddToChild((WORD)m_iTotalEntry,pbOutData+2,wLastLen+1);m_iTotalEntry+=1;wOld=wGet;}}//pbDecodedData=table.GetMatchData(wGet);//WORDwLen=*((WORD*)pbDecodedData);//pbDecodedData+=2;}deletepbNewData;deletepbOutData;deletepbBuffer;returndwRead==dwLength;}voidCLZWDecode::WriteDecode(BYTE*pbWrite,FUN_LZWDECODEPUTNEXTBYTEpfunLZWPutNextByte,FUN_LZWDECODEDBYTESpfunLZWDecodedBytes,DWORDdwBytesOnce){if(pbWrite==NULL)return;WORDwLength=*((WORD*)pbWrite);pbWrite+=2;for(DWORDi=0;iwCode; m_baContain.AddBits(wW,iBitWidth);//addlastcodetobufferm_LZWTable.AddToChild(bGet,pCurrent);//addlastcodetotableif(m_baContain.GetBytesWidth()>(DWORD)iBufferLen){m_dwCompressedLength+=(DWORD)iBufferLen;pfunLZWPutNextBytes(m_baContain.GetBits(),iBufferLen);m_baContain.RemoveBytes(NULL,iBufferLen);}if(m_LZWTable.GetTableEntryNumber()>=(m_wMaxEntry-3)){iBitWidth=GetBitWidth();m_baContain.AddBits(LZW_CLEAR_CODE,iBitWidth);m_LZWTable.InitLZWTable();}pCurrent=m_LZWTable.FindMatchChild(bGet,m_LZWTable.GetHead());}dwEncoded++;if(pfunLZWEncodedBytes&&dwEncoded==dwBytesOnce){pfunLZWEncodedBytes();dwEncoded=0;}}iBitWidth=GetBitWidth();m_baContain.AddBits(pCurrent->wCode,iBitWidth);//addlastcodetobufferreturnTRUE;}voidCLZWEncode::EndLZWEncode(FUN_LZWENCODEPUTNEXTBYTESpfunLZWPutNextBytes){intiBitWidth=GetBitWidth();m_baContain.AddBits(LZW_END_CODE,iBitWidth);m_dwCompressedLength+=m_baContain.GetBytesWidth();pfunLZWPutNextBytes(m_baContain.GetBits(),(int)m_baContain.GetBytesWidth());m_LZWTable.ClearLZWTable();m_baContain.ClearBits();};intCLZWEncode::GetBitWidth(void){intiTotal=(int)m_LZWTable.GetTableEntryNumber();iTotal>>=9;intiBitWidth=9;while(iTotal>0) {iTotal>>=1;iBitWidth+=1;}returniBitWidth;}2。对文件进行操作#include"stdafx.h"#include"lzwtable.h"#include"lzwcode.h"#include"lzwfile.h"//this.cppisnotconsidertry{}catch{}errorcaseconstintiFileUsedBufferLength=1024*10;CFile*pfileIn,*pfileOut;BYTEbEncodeGet[iFileUsedBufferLength],bDecodePut[iFileUsedBufferLength];DWORDdwEncodeGetTotal;intiEncodeGetPos,iDecodeGetPos;BOOLLZWCheckFile(LPCSTRpszCheck)//isthefileaHGLZ{CFilefileCheck(pszCheck,CFile::modeRead|CFile::typeBinary);BOOLfR=LZWCheckFile(&fileCheck);fileCheck.Close();returnfR;}BOOLLZWCheckFile(CFile*pfileCheck)//isthefileaHGLZ{WORDwRead;DWORDdwRead;BOOLfR=TRUE;if(pfileCheck->GetLength()GetPosition();pfileCheck->SeekToBegin();pfileCheck->Read(&dwRead,4);if(dwRead==LZW_FILE_TAG){pfileCheck->Seek(sizeof(LZWFILEHEAD),CFile::begin);pfileCheck->Read(&wRead,2);if(wRead!=LZW_NODE_TAG)fR=FALSE;} elsefR=FALSE;pfileCheck->Seek(dwPos,CFile::begin);returnfR;}intFindFileName(LPCSTRpszName){intiLen=strlen(pszName);for(inti=iLen-1;i>=0;i--){if(pszName[i]=='\')returni+1;}return0;}BOOLEncodeGetNextByte(BYTE&bGet){//pfileIn->Read(&bGet,1);if(iEncodeGetPos==0){intiRead=(dwEncodeGetTotal>=iFileUsedBufferLength)?iFileUsedBufferLength:(int)dwEncodeGetTotal;dwEncodeGetTotal-=(DWORD)iRead;pfileIn->Read(bEncodeGet,iRead);}bGet=bEncodeGet[iEncodeGetPos++];if(iEncodeGetPos>=iFileUsedBufferLength)iEncodeGetPos=0;returnTRUE;}BOOLEncodePutNextBytes(BYTE*pbPut,intiPut){pfileOut->Write(pbPut,iPut);returnTRUE;}BOOLDecodePutNextByte(BYTEbPut){pfileOut->Write(&bPut,1);returnTRUE;}BOOLDecodeGetNextBytes(BYTE*pbGet,intiGet){pfileIn->Read(pbGet,iGet); returnTRUE;}//forbatchdecompressfilesBOOLLZWDecodeFileOnPosition(LPCSTRpszInFile,//compressedfileLPCSTRpszOutPath,//filebedecompressedDWORDdwPos,//thedecompressedfilepositionFUN_LZWDECODEDBYTESpfunDecodedBytes){charszNewPath[1000]={""};if(pszOutPath!=NULL){strcpy(szNewPath,pszOutPath);if(szNewPath[strlen(szNewPath)-1]!='\'&&szNewPath[strlen(szNewPath)-1]!=':')strcat(szNewPath,"\");}CFilefileIn(pszInFile,CFile::modeRead|CFile::typeBinary);CStringszOutFile;DWORDdwFileLength=fileIn.GetLength();if(dwFileLength<(sizeof(LZWFILEHEAD)+sizeof(LZWNODEHEAD)+sizeof(LZWSINGLEFILEHEAD))){AfxMessageBox("errorhglzfile");fileIn.Close();returnFALSE;}LZWFILEHEADLZWFileHead;fileIn.Read(&LZWFileHead,sizeof(LZWFILEHEAD));if(LZWFileHead.dwFileTag!=LZW_FILE_TAG){AfxMessageBox("nothglzfile");fileIn.Close();returnFALSE;}if(HIBYTE(LZWFileHead.wVersion)!=0||LOBYTE(LZWFileHead.wVersion)>10){AfxMessageBox("nottheversion");fileIn.Close();returnFALSE;}LZWNODEHEADLZWNodeHead;fileIn.Read(&LZWNodeHead,sizeof(LZWNODEHEAD)); if(LZWNodeHead.wNodeTag!=LZW_NODE_TAG){AfxMessageBox("nothglzfilenode");fileIn.Close();returnFALSE;}if(LZWNodeHead.wDirectoryType!=LZW_NODE_BEGIN_COMPRESS){AfxMessageBox("notbegincompress");fileIn.Close();returnFALSE;}LZWSINGLEFILEHEADLZWSingleFileHead;for(DWORDd=0;dBeginLZWDecode(LZWSingleFileHead.dwFileCompressedLength,DecodeGetNextBytes,DecodePutNextByte,1024,(LZWSingleFileHead.dwFileOldLength>1000)?pfunDecodedBytes:NULL,LZWSingleFileHead.dwFileOldLength/100);fileOut.Flush();deletepDecode;}fileIn.Close();fileOut.Close();returnTRUE;}//decompressafileBOOLLZWDecodeFileToFile(LPCSTRpszInFile,LPCSTRpszOutFile,DWORDdwPos,FUN_LZWDECODEDBYTESpfunDecodedBytes){CFilefileIn(pszInFile,CFile::modeRead|CFile::typeBinary);DWORDdwFileLength=fileIn.GetLength();if(dwFileLength<(sizeof(LZWFILEHEAD)+sizeof(LZWNODEHEAD)+sizeof(LZWSINGLEFILEHEAD))){AfxMessageBox("errorhglzfile");fileIn.Close();returnFALSE;}LZWFILEHEADLZWFileHead;fileIn.Read(&LZWFileHead,sizeof(LZWFILEHEAD));if(LZWFileHead.dwFileTag!=LZW_FILE_TAG){AfxMessageBox("nothglzfile");fileIn.Close();returnFALSE;} if(HIBYTE(LZWFileHead.wVersion)!=0||LOBYTE(LZWFileHead.wVersion)>10){AfxMessageBox("nottheversion");fileIn.Close();returnFALSE;}LZWNODEHEADLZWNodeHead;fileIn.Read(&LZWNodeHead,sizeof(LZWNODEHEAD));if(LZWNodeHead.wNodeTag!=LZW_NODE_TAG){AfxMessageBox("nothglzfilenode");fileIn.Close();returnFALSE;}if(LZWNodeHead.wDirectoryType!=LZW_NODE_BEGIN_COMPRESS){AfxMessageBox("notbegincompress");fileIn.Close();returnFALSE;}LZWSINGLEFILEHEADLZWSingleFileHead;for(DWORDd=0;dBeginLZWDecode(LZWSingleFileHead.dwFileCompressedLength,DecodeGetNextBytes,DecodePutNextByte,1024,(LZWSingleFileHead.dwFileCompressedLength>1000)?pfunDecodedBytes:NULL,LZWSingleFileHead.dwFileCompressedLength/100);fileOut.Flush();deletepDecode;}fileIn.Close();fileOut.Close();returnTRUE;}//addanotherfiletothecompressedfileBOOLLZWAddEncodeFile(LPCSTRpszInFile,//filetobeaddedLPCSTRpszOutFile,//compressedfileFUN_LZWENCODEDBYTESpfunEncodedBytes){iEncodeGetPos=0;if(!LZWCheckFile(pszOutFile))returnFALSE;CFilefileIn(pszInFile,CFile::modeRead|CFile::typeBinary);CFilefileOut(pszOutFile,CFile::modeReadWrite|CFile::typeBinary);pfileOut=&fileOut;pfileIn=&fileIn;LZWFILEHEADLZWFileHead;//(LZW_FILE_TAG,(9<<8)|0,1);fileOut.Read(&LZWFileHead,sizeof(LZWFILEHEAD));LZWFileHead.dwFileCount++;fileOut.SeekToBegin(); fileOut.Write(&LZWFileHead,sizeof(LZWFILEHEAD));fileOut.SeekToEnd();LZWSINGLEFILEHEADLZWSingleFileHead;/*(LZW_SINGLE_FILE_TAG,0,dwOldFileLength,0,strlen(pszInFile));*/DWORDdwFileOldLength=fileIn.GetLength();LZWSingleFileHead.wSingleFileTag=LZW_SINGLE_FILE_TAG;LZWSingleFileHead.dwAttribute=0;LZWSingleFileHead.dwFileOldLength=dwFileOldLength;dwEncodeGetTotal=dwFileOldLength;intiFileNamePos=FindFileName(pszInFile);LZWSingleFileHead.wFileNameLength=strlen(pszInFile+iFileNamePos);fileOut.Write(&LZWSingleFileHead,sizeof(LZWSINGLEFILEHEAD));fileOut.Write(pszInFile+iFileNamePos,strlen(pszInFile+iFileNamePos));if(dwFileOldLength!=0){CLZWEncode*pEncode=newCLZWEncode();pEncode->BeginLZWEncode(dwFileOldLength,EncodeGetNextByte,EncodePutNextBytes,1024,(dwFileOldLength>1000)?pfunEncodedBytes:NULL,dwFileOldLength/100);pEncode->EndLZWEncode(EncodePutNextBytes);LZWSingleFileHead.dwFileCompressedLength=(DWORD)pEncode->GetCompressedLength();DWORDdwCurrentPos=fileOut.GetPosition();DWORDdwLastSingleFileHeadPos=dwCurrentPos-LZWSingleFileHead.dwFileCompressedLength-LZWSingleFileHead.wFileNameLength-sizeof(LZWSINGLEFILEHEAD);//fileOut.Flush();/*fileOut.Close();fileOut.Open(pszOutFile,CFile::modeWrite|CFile::typeBinary);*/fileOut.Seek(dwLastSingleFileHeadPos,CFile::begin);fileOut.Write(&LZWSingleFileHead,sizeof(LZWSINGLEFILEHEAD));fileOut.Seek(dwCurrentPos,CFile::begin);fileOut.Flush(); //fileOut.Seek(0,CFile::end);deletepEncode;}fileOut.Close();fileIn.Close();returnTRUE;}BOOLLZWEncodeFile(LPCSTRpszInFile,LPCSTRpszOutFile,FUN_LZWENCODEDBYTESpfunEncodedBytes){iEncodeGetPos=0;CFilefileIn(pszInFile,CFile::modeRead|CFile::typeBinary);CFilefileOut(pszOutFile,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);pfileOut=&fileOut;pfileIn=&fileIn;LZWFILEHEADLZWFileHead;//(LZW_FILE_TAG,(9<<8)|0,1);LZWFileHead.dwFileTag=LZW_FILE_TAG;LZWFileHead.wVersion=(0<<8)|10;LZWFileHead.dwFileCount=1;fileOut.Write(&LZWFileHead,sizeof(LZWFILEHEAD));LZWNODEHEADLZWNodeHead;//(LZW_NODE_TAG,LZW_BEGIN_COMPRESS,0);LZWNodeHead.wNodeTag=LZW_NODE_TAG;LZWNodeHead.wDirectoryType=LZW_NODE_BEGIN_COMPRESS;LZWNodeHead.wDirectoryNameLength=0;fileOut.Write(&LZWNodeHead,sizeof(LZWNODEHEAD));LZWSINGLEFILEHEADLZWSingleFileHead;/*(LZW_SINGLE_FILE_TAG,0,dwOldFileLength,0,strlen(pszInFile));*/DWORDdwFileOldLength=fileIn.GetLength();LZWSingleFileHead.wSingleFileTag=LZW_SINGLE_FILE_TAG;LZWSingleFileHead.dwAttribute=0;LZWSingleFileHead.dwFileOldLength=dwFileOldLength;dwEncodeGetTotal=dwFileOldLength;intiFileNamePos=FindFileName(pszInFile);LZWSingleFileHead.wFileNameLength=strlen(pszInFile+iFileNamePos);fileOut.Write(&LZWSingleFileHead,sizeof(LZWSINGLEFILEHEAD));fileOut.Write(pszInFile+iFileNamePos,strlen(pszInFile+iFileNamePos)); if(dwFileOldLength!=0){CLZWEncode*pEncode=newCLZWEncode();pEncode->BeginLZWEncode(dwFileOldLength,EncodeGetNextByte,EncodePutNextBytes,1024,(dwFileOldLength>1000)?pfunEncodedBytes:NULL,dwFileOldLength/100);pEncode->EndLZWEncode(EncodePutNextBytes);LZWSingleFileHead.dwFileCompressedLength=(DWORD)pEncode->GetCompressedLength();DWORDdwCurrentPos=fileOut.GetPosition();DWORDdwLastSingleFileHeadPos=dwCurrentPos-LZWSingleFileHead.dwFileCompressedLength-LZWSingleFileHead.wFileNameLength-sizeof(LZWSINGLEFILEHEAD);//fileOut.Flush();/*fileOut.Close();fileOut.Open(pszOutFile,CFile::modeWrite|CFile::typeBinary);*/fileOut.Seek(dwLastSingleFileHeadPos,CFile::begin);fileOut.Write(&LZWSingleFileHead,sizeof(LZWSINGLEFILEHEAD));fileOut.Seek(dwCurrentPos,CFile::begin);fileOut.Flush();//fileOut.Seek(0,CFile::end);deletepEncode;}fileOut.Close();fileIn.Close();returnTRUE;}/*BOOLLZWDecodeFile(LPCSTRpszInFile,LPCSTRpszOutFile){returnFALSE;}*/BOOLLZWParseFileHead(LPCSTRpszInFile,CString*pszOut){DWORDdwCount=0;CStringszOut(pszInFile);charszTemp[1000];CFilefileIn(pszInFile,CFile::modeRead|CFile::typeBinary); DWORDdwFileLength=fileIn.GetLength();if(dwFileLength<(sizeof(LZWFILEHEAD)+sizeof(LZWNODEHEAD)+sizeof(LZWSINGLEFILEHEAD))){AfxMessageBox("errorfileformat");returnFALSE;}LZWFILEHEADLZWFileHead;fileIn.Read(&LZWFileHead,sizeof(LZWFILEHEAD));if(LZWFileHead.dwFileTag!=LZW_FILE_TAG){AfxMessageBox("nothglzfile");fileIn.Close();returnFALSE;}szOut+=" ";sprintf(szTemp,"version1.3 filecount1 ",HIBYTE(LZWFileHead.wVersion),LOBYTE(LZWFileHead.wVersion),LZWFileHead.dwFileCount);szOut+=szTemp;dwCount=LZWFileHead.dwFileCount;LZWNODEHEADLZWNodeHead;fileIn.Read(&LZWNodeHead,sizeof(LZWNODEHEAD));if(LZWNodeHead.wNodeTag!=LZW_NODE_TAG){AfxMessageBox("nothglzfilenode");fileIn.Close();returnFALSE;}if(LZWNodeHead.wDirectoryNameLength!=0){ASSERT(0);}for(DWORDd=0;dpChild){m_EntryHead.pChild=pFirstChild->pChild;if(m_EntryHead.pChild->pRightBrother){PLZWENCODEENTRYpRightBrother=FindRightBrother(m_EntryHead.pChild);pRightBrother->pRightBrother=pFirstChild->pRightBrother;}else(m_EntryHead.pChild)->pRightBrother=pFirstChild->pRightBrother;//deletepFirstChild;}else m_EntryHead.pChild=pFirstChild->pRightBrother;deletepFirstChild;}PLZWENCODEENTRYCLZWEncodeTable::FindRightBrother(PLZWENCODEENTRYpCurrent){PLZWENCODEENTRYpFind;pFind=pCurrent;while(pFind->pRightBrother){pFind=pFind->pRightBrother;}returnpFind;}voidCLZWEncodeTable::InitLZWTable(void){//initthetable,ithas256itemscodefrom0to255ClearLZWTable();PLZWENCODEENTRYpEntryFirst=newLZWENCODEENTRY;pEntryFirst->wCode=(WORD)0;pEntryFirst->bLast=(BYTE)0;pEntryFirst->pRightBrother=pEntryFirst->pChild=NULL;m_EntryHead.pChild=pEntryFirst;m_EntryHead.pRightBrother=NULL;PLZWENCODEENTRYpPrev=pEntryFirst;for(inti=1;i<=255;i++){//setthebrothernodesPLZWENCODEENTRYpEntry=newLZWENCODEENTRY;pEntry->wCode=(WORD)i;pEntry->bLast=(BYTE)i;pEntry->pChild=pEntry->pRightBrother=NULL;pPrev->pRightBrother=pEntry;pPrev=pEntry;}m_dwTableEntryNumber=258;m_uNextCodeForUse=LZW_BEGIN_ENTRY;}PLZWENCODEENTRYCLZWEncodeTable::FindMatchChild(BYTEbChildLast,PLZWENCODEENTRYpCurrent){//returnthefindchildentryif(pCurrent->pChild==NULL)returnNULL;PLZWENCODEENTRYpChild=pCurrent->pChild;//pChildisthecurrent'schild//andallpChild'sbrotheristhecurrent'schild while(pChild!=NULL){if(pChild->bLast==bChildLast)returnpChild;pChild=pChild->pRightBrother;}returnNULL;}PLZWENCODEENTRYCLZWEncodeTable::AddToChild(BYTEbLast,PLZWENCODEENTRYpCurrent){ASSERT(pCurrent);PLZWENCODEENTRYpChild=newLZWENCODEENTRY;if(pChild==NULL){int_j=0;}pChild->pChild=pChild->pRightBrother=NULL;pChild->bLast=bLast;pChild->wCode=(WORD)m_uNextCodeForUse;if(pChild->wCode==LZW_CLEAR_CODE){int_i=0;}m_uNextCodeForUse++;m_dwTableEntryNumber++;pChild->pRightBrother=pCurrent->pChild;pCurrent->pChild=pChild;returnpChild;}

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。
关闭