📄 file.cpp
字号:
#include "stdafx.h"
#include "file.h"
unsigned long TransAndSave(int mode, char *filename, codeListStruct *acCodeHead,symbolStreamStruct *acSymbolHead,
dcCodeList *dcCodeHead,short dcDiff[],blstype blockSize,unsigned long acsl,
cltype acCodeListLength,cltype dcCodeListLength, unsigned int width, unsigned int height, btype QT[8][8])
{
unsigned long filesize=0;
codeListStruct *pACCode=acCodeHead; //交流码表工作指针
symbolStreamStruct *pACSymbol=acSymbolHead; //交流符号流链表工作指针
dcCodeList *pDCCode=dcCodeHead; //直流码表工作指针
//ofstream distfile(filename,ios::out|ios::binary);
//if(!distfile){
// cout<<"无法创建文件"<<endl;
// return 0;
//}
FILE *distfile;
if( fopen_s( &distfile, filename, "w+b" ) ) {
printf( "The file %s was not opened\n",filename);
return 1;
}
else
printf( "The file %s was opened\n" ,filename);
//distfile.write((char*)&mode,sizeof(unsigned));
fwrite(&mode,sizeof(unsigned),1,distfile);
for (int i=0; i<8; i++)
for (int j=0; j<8; j++)
//distfile.write((char*)&QT[i][j],sizeof(unsigned));
fwrite(&QT[i][j],sizeof(unsigned),1,distfile);
//distfile.write((char*)&width,sizeof(unsigned));//<<'\n';
//distfile.write((char*)&height,sizeof(unsigned));//<<'\n';
//distfile.write((char*)&dcCodeListLength,sizeof(cltype));//<<'\n'; //先保存直流码表
fwrite(&width,sizeof(unsigned),1,distfile);
fwrite(&height,sizeof(unsigned),1,distfile);
fwrite(&dcCodeListLength,sizeof(cltype),1,distfile);
filesize+=sizeof(unsigned)*2+sizeof(cltype);
unsigned dbyte=0;
unsigned short length=0;
//unsigned short maxlen=0;
for(cltype i=0;i<dcCodeListLength;i++){
dbyte=0;
length=unsigned short (strlen(pDCCode->code));
//if(length>maxlen)maxlen=length;
for(int i=length-1;i>=0;i--){
if(pDCCode->code[length-i-1]=='1')
dbyte|=(unsigned)(1<<i);
}
//distfile.write((char*)&pDCCode->diffValue,sizeof(short));
//distfile.write((char*)&length,sizeof(unsigned short));
//distfile.write((char*)&dbyte,sizeof(unsigned));
fwrite(&pDCCode->diffValue,sizeof(short),1,distfile);
fwrite(&length,sizeof(unsigned short),1,distfile);
fwrite(&dbyte,sizeof(unsigned),1,distfile);
pDCCode=pDCCode->next;
}
filesize+=(sizeof(short)+sizeof(unsigned short)+sizeof(unsigned))*(dcCodeListLength-1);
//再保存交流码表
//distfile.write((char*)&acCodeListLength,sizeof(cltype));
fwrite(&acCodeListLength,sizeof(cltype),1,distfile);
for(cltype i=0;i<acCodeListLength;i++){
dbyte=0;
length=unsigned short (strlen(pACCode->code));
//if(length>maxlen)maxlen=length;
for(int i=length-1;i>=0;i--){
if(pACCode->code[length-i-1]=='1')
dbyte|=(unsigned)(1<<i);
}
//distfile.write((char*)&pACCode->ep->run,sizeof(unsigned short));
//distfile.write((char*)&pACCode->ep->level,sizeof(btype));
//distfile.write((char*)&length,sizeof(unsigned short));
//distfile.write((char*)&dbyte,sizeof(unsigned));
fwrite(&pACCode->ep->run,sizeof(unsigned short),1,distfile);
fwrite(&pACCode->ep->level,sizeof(btype),1,distfile);
fwrite(&length,sizeof(unsigned short),1,distfile);
fwrite(&dbyte,sizeof(unsigned),1,distfile);
pACCode=pACCode->next;
}
filesize+=(sizeof(btype)+2*sizeof(unsigned short)+sizeof(unsigned))*(acCodeListLength-1);
//cout<<"ac code max len:"<<maxlen;
pDCCode=dcCodeHead;
pACCode=acCodeHead; //将指向直流和交流码表的工作指针复位
unsigned short currentLength; //当前符号码字长度
//unsigned short currentCode; //当前码字
unsigned char theByte=0; //缓冲字节
short curbit=7; //写入缓冲字节中的位数
//直流符号流长度
//distfile.write((char*)&blockSize,sizeof(blstype));
fwrite(&blockSize,sizeof(blstype),1,distfile);
for(cltype i=0;i<blockSize;i++){ //blocksize
while(dcDiff[i]!=pDCCode->diffValue){ //在直流码表中找到差分值对应的节点
pDCCode=pDCCode->next;
if(pDCCode==NULL){
cout<<"在直流码表中找不到差分值!"<<endl;
exit(1);
}
}
currentLength=unsigned short (strlen(pDCCode->code));
for (int j = 0; j<currentLength; j++)
{
if (pDCCode->code[j]=='1')
theByte |= (1 << curbit);
if (--curbit < 0)
{
//distfile.write((char*)&theByte,sizeof(unsigned char));
fwrite(&theByte,sizeof(unsigned char),1,distfile);
filesize+=sizeof(unsigned char);
theByte = 0;
//curbyte++;
curbit = 7;
}
}
pDCCode=dcCodeHead; //处理完一个差分值,将码表工作指针复位,用于下个差分值扫描
}
//distfile.write((char*)&theByte,sizeof(unsigned char));
fwrite(&theByte,sizeof(unsigned char),1,distfile);
//distfile.write((char*)&acsl,sizeof(unsigned long));//<<endl; //交流符号流长度
fwrite(&acsl,sizeof(unsigned long),1,distfile);
filesize=filesize+sizeof(unsigned long)+sizeof(unsigned char);
currentLength=0; //保存交流编码结果之前先将工作变量复位
theByte=0;
curbit=7;
while(pACSymbol!=NULL){ //->next
while((pACSymbol->e.level!=pACCode->ep->level) || (pACSymbol->e.run!=pACCode->ep->run)){
pACCode=pACCode->next;
if(pACCode==NULL){
cout<<"在交流码表中找不到符号!"<<endl;
exit(1);
}
}
currentLength=unsigned short (strlen(pACCode->code));
for (int j = 0; j<currentLength; j ++ )
{
if ((pACCode->code[j])=='1')
theByte |= (1 << curbit);
if (--curbit < 0)
{
//distfile.write((char*)&theByte,sizeof(unsigned char));
fwrite(&theByte,sizeof(unsigned char),1,distfile);
filesize+=sizeof(unsigned char);
theByte = 0;
//curbyte++;
curbit = 7;
}
}
pACCode=acCodeHead; //处理完一个符号,将码表工作指针复位,用于下次扫描
pACSymbol=pACSymbol->next;
}
//distfile.write((char*)&theByte,sizeof(unsigned char));
fwrite(&theByte,sizeof(unsigned char),1,distfile);
//distfile.close();
fclose(distfile);
return filesize;
}
blstype ReadFile(char* inFileName,int &mode, unsigned &width,unsigned &height, btype QT[8][8], symbolStreamStruct* &acSymbolHead,short* &dcDiff)
{
//ifstream srcfile(inFileName,ios::in|ios::binary);
//if(!srcfile){
// cout<<"无法打开文件"<<endl;
// exit(1);
//}
FILE *srcfile;
if( fopen_s( &srcfile, inFileName, "rb" ) ) {
printf( "The file %s was not opened\n",inFileName);
return 1;
}
else
printf( "The file %s was opened\n" ,inFileName);
cltype acCodeListlength,dcCodeListlength;
// short int diffValue;
// char* code;
//srcfile.read((char*)&mode,sizeof(unsigned));
fread(&mode,sizeof(unsigned),1,srcfile);
for (int i=0; i<8; i++)
for (int j=0; j<8; j++)
//srcfile.read((char*)&QT[i][j],sizeof(unsigned));
fread(&QT[i][j],sizeof(unsigned),1,srcfile);
//srcfile.read((char*)&width,sizeof(unsigned));
// srcfile.read((char*)&height,sizeof(unsigned));
fread(&width,sizeof(unsigned),1,srcfile);
fread(&height,sizeof(unsigned),1,srcfile);
unsigned dbyte=0;
unsigned short length=0;
/*-----------------------------------建立DC码表----------------------------------------*/
//srcfile.read((char*)&dcCodeListlength,sizeof(cltype));
fread(&dcCodeListlength,sizeof(cltype),1,srcfile);
dcCodeList* dcCodeHead=new dcCodeList; //表头指针
if(dcCodeHead==NULL){
cout<<"空间不足"<<endl;
exit(1);
}
//srcfile.read((char*)&dcCodeHead->diffValue,sizeof(short));
//srcfile.read((char*)&length,sizeof(unsigned short));
//srcfile.read((char*)&dbyte,sizeof(unsigned));
fread(&dcCodeHead->diffValue,sizeof(short),1,srcfile);
fread(&length,sizeof(unsigned short),1,srcfile);
fread(&dbyte,sizeof(unsigned),1,srcfile);
dcCodeHead->code=new char[length+1];
if(dcCodeHead->code==NULL){
cout<<"空间不足"<<endl;
exit(1);
}
for (int i=length-1;i>=0;i--){
if((dbyte>>i)&1){
dcCodeHead->code[length-i-1]='1';
}
else {dcCodeHead->code[length-i-1]='0';}
}
dcCodeHead->code[length]='\0';
dcCodeHead->count=0;
dcCodeHead->next=NULL;
dcCodeList* pdcCode=dcCodeHead; //工作指针
for(cltype i=0;i<dcCodeListlength-1;i++)
{
dbyte=0;
length=0;
dcCodeList* newdcCode=new dcCodeList; //新生成的节点
if(newdcCode==NULL){
cout<<"空间不足"<<endl;
exit(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -