📄 coscode.cpp
字号:
}
}
*/
int InputCode(BIT_FILE *input_file)
{
int bit_count;
int result;
if(InputRunLength>0)
{
InputRunLength--;
return(0);
}
bit_count=(int)InputBits(input_file,2);
if(bit_count==0)
{
InputRunLength=(int)InputBits(input_file,4);
return(0);
}
if(bit_count==1)
bit_count=(int)InputBits(input_file,1)+1;
else
bit_count=(int)InputBits(input_file,2)+(bit_count<<2)-5;
result=(int)InputBits(input_file,bit_count);
if(result&(1<<(bit_count-1)))
return(result);
return(result-(1<<bit_count)+1);
}
//This routine reads in a block of encoded DCT data from a compressed file.
//The rountine recorders it in row major format and dequantizes it using
//the quantization matrix.
void ReadDCTData(BIT_FILE *input_file,int input_data[N][N])
{
int i;
int row;
int col;
int p;
for(i=0;i<(N*N);i++)
{
row=ZigZag[i].row;
col=ZigZag[i].col;
p=InputCode(input_file);
input_data[row][col]=p*Quantum[row][col];
}
}
//This routine outputs a code to the compressed DCT file.For specs on the
//exact format,see the comments that go with InputCode,shown earlier in
//this file.
void OutputCode(BIT_FILE *output_file,int code)
{
int top_of_range;
int abs_code;
int bit_count;
if(code==0)
{
OutputRunLength++;
return;
}
if(OutputRunLength!=0)
{ while(OutputRunLength>0)
{OutputBits(output_file,0L,2);
if(OutputRunLength<=16)
{ OutputBits(output_file,(unsigned long)(OutputRunLength-1),4);
OutputRunLength=0;
}
else
{OutputBits(output_file,15L,4);
OutputRunLength-=16;
}
}
}
if(code==30000)
return;
if(code<0)
abs_code=-code;
else
abs_code=code;
top_of_range=1;
bit_count=1;
while(abs_code>top_of_range)
{bit_count++;
top_of_range=((top_of_range+1)*2)-1;
}
if(bit_count<3)
OutputBits(output_file,(unsigned long)(bit_count+1),3);
else
OutputBits(output_file,(unsigned long)(bit_count+5),4);
if(code>0)
OutputBits(output_file,(unsigned long)code,bit_count);
else
OutputBits(output_file,(unsigned long)(code+top_of_range),bit_count);
}
//This routine takes DCT data,puts it in zigzag order,quantizes it,and
//outputs the code.
void WriteDCTData(BIT_FILE *output_file,int output_data[N][N])
{
int i;
int row;
int col;
double result;
for(i=0;i<(N*N);i++)
{
row=ZigZag[i].row;
col=ZigZag[i].col;
result=output_data[row][col]/Quantum[row][col];
OutputCode(output_file,ROUND(result));
}
}
void NewWriteDCTData(BIT_FILE *output_file,int output_data[N][N])
{
int i;
int row;
int col;
double result;
for(i=0;i<(N*N);i++)
{
row=ZigZag[i].row;
col=ZigZag[i].col;
result=output_data[row][col]/Quantum[row][col];
OutputCode(output_file,ROUND(result));
}
OutputCode(output_file,30000);
}
/*
//This rountine writes out a strip of pixel data to GS format file.
//void WritePixelStrip(FILE *output,unsigned char strip[N][COLS])
void WritePixelStrip(FILE *output,unsigned char **strip,int N,int COLS)
{
int row;
int col;
for(row=0;row<N;row++)
for(col=0;col<COLS;col++)
putc(strip[row][col],output);
}
*/
//The Forward DCT routine implements the matrix function:
//
// DCT=C*pixel*Ct
void ForwardDCT(unsigned char input[N][N],int output[N][N])
{
double temp[N][N];
double temp1;
int i;
int j;
int k;
//MatrixMultiply(temp,input,Ct)
for(i=0;i<N;i++)
{for(j=0;j<N;j++)
{temp[i][j]=0.0;
for(k=0;k<N;k++)
temp[i][j]+=((int)input[i][k]-128)*Ct[k][j];
}
}
//MatrixMultiply(output,C,temp);
for(i=0;i<N;i++)
{for(j=0;j<N;j++)
{temp1=0.0;
for(k=0;k<N;k++)
temp1+=C[i][k]*temp[k][j];
output[i][j]=ROUND(temp1);
}
}
}
//The Inverse DCT routine implements the matrix function:
//
// pixel=C*DCT*Ct
void InverseDCT(int input[N][N],unsigned char output[N][N])
{
double temp[N][N];
double temp1;
int i;
int j;
int k;
//MatrixMultiply(temp,input,C);
for(i=0;i<N;i++)
{for(j=0;j<N;j++)
{temp[i][j]=0.0;
for(k=0;k<N;k++)
temp[i][j]+=input[i][k]*C[k][j];
}
}
//MatrixMultiply(output,Ct,temp);
for(i=0;i<N;i++)
{for(j=0;j<N;j++)
{temp1=0.0;
for(k=0;k<N;k++)
temp1+=Ct[i][k]*temp[k][j];
temp1+=128.0;
if(temp1<0)
output[i][j]=0;
else if(temp1>255)
output[i][j]=255;
else output[i][j]=(unsigned char)ROUND(temp1);
}
}
}
//This is the driver program used when testing compression algorithms.In
//order to cut back on repetitive code,this version of main is used with
//all of the compression routines.It in order to turn into a real program,
//it needs to have another module that supplies one routine and two strings,
//namely:
//
//
//void CompressFile(FILE *input,BIT_FILE *output,
//int argc,char *argv);
//char *Usage;
//char *CompressionName;
//
//The main() routine supplied here has the job of checking for valid input
//and output files,opening them,and then calling the compression routine.If
//the files are not present,or no arguments are supplied,it prints out an
//error message,which includes the Usage string supplied by the compression
//module.All of the routines and strings needed by this routine are defined
//in the main.h header file.
//After this is built into a compression program of any sort,the program
//can be called like this:
//main-c infile outfile[options]
void CompressFile1(unsigned char **Image,BIT_FILE *output,int quality,int rw,int cl)
{
int row;
int col;
int i;
int j;
unsigned char input_array[N][N];
int output_array[N][N];
Initialize(quality);
OutputBits(output,(unsigned long)quality,8);
for(row=0;row<rw;row+=N)
for(col=0;col<cl;col+=N)
{
for(i=0;i<N;i++)
for(j=0;j<N;j++)
input_array[i][j]=Image[i+row][j+col];
ForwardDCT(input_array,output_array);
WriteDCTData(output,output_array);
}
OutputCode(output,1);
}
void dctcc( unsigned char **Image,char *outputfile,
int rw,int cl,int quality)
{
BIT_FILE *output;
int RealCol,RealRow;
output=OpenOutputBitFile(outputfile);
CompressFile1(Image,output,quality,rw,cl);
CloseOutputBitFile(output);
}
//This driver program tests compression algorithms.To cut back on repetitive
//code,this version of main is used with all the expansion routine.The
//main() routine supplied here checks for valid input and output files,
//opens them,then calls the compression routine.
void ExpandFile1( BIT_FILE *input,
unsigned char **Image,
int rw,int cl)
{
int row;
int col;
int i,j;
int input_array[N][N];
unsigned char output_array[N][N];
int quality;
quality=(int)InputBits(input,8);
Initialize(quality);
for(row=0;row<rw;row+=N)
for(col=0;col<cl;col+=N)
{
ReadDCTData(input,input_array);
InverseDCT(input_array,output_array);
for(i=0;i<N;i++)
for(j=0;j<N;j++)
Image[row+i][col+j]=output_array[i][j];
}
}
void dct_ce(char *inputfile,unsigned char **output,int rw,int cl)
{
BIT_FILE *input;
input=OpenInputBitFile(inputfile);
ExpandFile1(input,output,rw,cl);
CloseInputBitFile(input);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -