⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aencode.c

📁 jpeg压缩程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*************************************************************/
/* FILENAME    : ENCODE.C                                    */
/* FUNCTION    : This program is for encoding image file     */
/* INPUT FORMAT: DECODE <IMAGE_FILENAME> <CODE_FILENAME>     */
/*                 <COLUMNS> <ROWS> <0/1> [Q]                */
/* PARAMETERS  : <IMAGE_FILENAME> <CODE_FILENAME>            */
/* OUTPUT      : <CODE_FILENAME>                             */
/* COMPILING & | TCC CODE.C (PC Computer: Turbo C 2.0)       */
/* AUTHOR      : Ai Wei                                      */
/* ADDRESS     : Room 248, Dormitory 1                       */
/*                Tsing Hua University                       */
/*                Beijing 100084, P.R.China                  */
/* DATE/TIME   : 2-17-1993/5:00pm                            */
/*************************************************************/

#include <stdio.h>
#include "stdlib.h"
#include <time.h>
#include "math.h"
#include "errno.h"
#include "alloc.h"
#include <sys\types.h>
#include <sys\timeb.h>
#include "jpeg.h"

#define PI 3.14159265359

unsigned char
      ac_ehuffsi[256],dc_ehuffsi[16],dc_huffsize[13],ac_huffsize[165];

unsigned char
      buffer[2048];

unsigned int
      ac_ehuffco[256],dc_ehuffco[16],dc_huffcode[13],ac_huffcode[165];

double
	    dct_coe[8][8];

int
      si_data[8][8];

int
      result[8][8],bit_last_left=8,code_stream_length=0;

long
      dc_pre,diff,zz[64];

unsigned char
	  far *code_stream;


/******************************************************/
/*                                                    */
/* Function:   Procedure for loading DCT coefficients */
/*                                                    */
/* Format:     load_dct_coe()                         */
/*                                                    */
/* Parametre:  None.                                  */
/*                                                    */
/* Return value: None.                                */
/*                                                    */
/******************************************************/

load_dct_coe()
   {
   int i,j;
   for(i=0;i<8;i++)
      dct_coe[i][0]=0.5/sqrt((double)2.0);
   for(j=1;j<8;j++)
      for(i=0;i<8;i++)
	 dct_coe[i][j]=0.5*cos((2.0*(double)i+1.0)*(double)j*PI/16.0);
   }



/******************************************************/
/*                                                    */
/* Function:   Procedure for  calculating FDCT        */
/*                                                    */
/* Format:     dct_tran()                             */
/*                                                    */
/* Parametre:  None.                                  */
/*                                                    */
/* Return Value: None.                                */
/*                                                    */
/******************************************************/

dct_tran()
    {
    double re1[8][8],re2[8][8];
    int i,j,k;
    for(i=0;i<8;i++)
       for(j=0;j<8;j++)
	   {
	   re1[i][j]=0.0;
	   re2[i][j]=0.0;
	   for(k=0;k<8;k++)
	      re1[i][j]=re1[i][j]+dct_coe[k][j]*(double)(si_data[i][k]);
	   }
    for(i=0;i<8;i++)
       for(j=0;j<8;j++)
	   for(k=0;k<8;k++)
	      re2[i][j]=re2[i][j]+dct_coe[k][i]*re1[k][j];
    for(i=0;i<8;i++)
       for(j=0;j<8;j++)
	  result[i][j]=re2[i][j];
    }


/*********************************************************/
/*                                                       */
/* Function:   Procedure for getting the real            */
/*              quantization table.                      */
/*                                                       */
/* Format:     get_a_table(int q_value,                  */
/*                         unsigned char *table)         */
/*                                                       */
/* Parametre:  q_value---parametre Q for quantization.   */
/*             table-----pointer of quantization table.  */
/*                                                       */
/* Return Value: None.                                   */
/*                                                       */
/*********************************************************/

void get_q_table(q,table)
    int q;
    unsigned char table[8][8];
    {
    unsigned i,j,tmp;
    for(i=0;i<8;i++)
       for(j=0;j<8;j++)
	    {
	    tmp=table[i][j]*q;
	    table[i][j]=tmp/50.0+0.5;
	    }
    }


/***************************************************/
/*                                                 */
/* Function:   Procedure for Quantize the DCT      */
/*                coefficients                     */
/*                                                 */
/* Format:     quant(unsigned char *table)         */
/*                                                 */     
/* Parametre: table---pointer of quantization      */
/*                      table.                     */
/*                                                 */
/* Return value: None.                             */     
/*                                                 */
/***************************************************/

quant(table)
    unsigned char table[8][8];
    {
    int i,j;
    for(i=0;i<8;i++)
       for(j=0;j<8;j++)
	   {
	   if(result[i][j]>=0)
	       result[i][j] =
		  result[i][j]/(float)(table[i][j])+0.5;
	   else
	       result[i][j] =
		  result[i][j]/(float)(table[i][j])-0.5;
	   }
    }


/***********************************************************/
/*                                                         */
/*  Function:      Procedure for zigzag scan of the        */
/*                 coefficients                            */
/*                                                         */     
/*  Format:        zigzag()                                */
/*                                                         */
/*  Parametre:     None.                                   */     
/*                                                         */     
/*  Return value:  None.                                   */
/*                                                         */
/***********************************************************/

zigzag()
    {
    int i,j;
    for(i=0;i<8;i++)
       for(j=0;j<8;j++)
	  /* 
	  **  reorder the coefficients in zigzag sequence 
	  */
	  zz[zz_index[i*8+j]]=result[i][j];   /* reorder the coefficients in zigzag sequence */
    }


/*****************************************************************/
/*                                                               */
/* Function:      Procedure for generation of the Huffman        */
/*                Codes  &  SIzes.                               */
/*                                                               */
/* Format:        loadhuf(unsigned char *DC_bit,                 */
/*                        unsigned char *DC_huffval,             */
/*                        unsigned char *AC_bit,                 */
/*                        unsigned char *AC_huffval)             */
/*                                                               */
/* Parametre:     DC_bit---------pointer of table used to        */                                                                   
/*                               generate huffman code size      */
/*                               for DC coefficents.             */
/*                DC_huffval-----pointer of table used to        */ 
/*                               generate huffman code           */
/*                               for DC coefficients.            */
/*                DC_bit---------pointer of table used to        */                                                                   
/*                               generate huffman code size      */
/*                               for AC coefficents.             */
/*                DC_huffval-----pointer of table used to        */ 
/*                               generate huffman code           */       
/*                               for AC coefficients.            */
/*                                                               */
/* Return value:  None.                                          */
/*                                                               */
/*****************************************************************/

loadhuf(d_bit,d_huffval,a_bit,a_huffval)
    unsigned char d_bit[16],a_bit[16];
    unsigned char d_huffval[12],a_huffval[162];
    {
    unsigned code;
    int i,j,p,dc_lastp,ac_lastp,si;

    /* 
    **  Generation of table of Huffman code sizes 
    */

    i=0;
    j=1;
    p=0;

    while(i<16)
       {
       if(j>d_bit[i])
	  {
	  i++;
	  j=1;
	  }
       else
	  {
	  /* 
	  **  Generate DC Huffman code sizes 
	  */
	  dc_huffsize[p]=i+1;  
	  p++;
	  j++;
	  }
       }
    dc_huffsize[p]=0;
    dc_lastp=p;

    i=0;
    j=1;
    p=0;

    while(i<16)
       {
       if(j>a_bit[i])
	  {
	  i++;
	  j=1;
	  }
       else
	  {
	  /*
	  ** Generate AC Huffman code sizes
	  */
	  ac_huffsize[p]=i+1;
	  p++;
	  j++;
	  }
       }
    ac_huffsize[p]=0;
    ac_lastp=p;

    /*
    **  Generation of table of Huffman codes
    */

    p=0;
    code=0;
    si=dc_huffsize[0];

y1: do
       {
       /*
       ** Generation of DC Huffman code
       */
       dc_huffcode[p]=code;
       code++;
       p++;
       } while(dc_huffsize[p]==si);
    
    if(dc_huffsize[p]!=0)
       {
       do
	  {
	  code=code<<1;
	  si++;
	  } while(dc_huffsize[p]!=si);
       goto y1;
       }

    p=0;
    code=0;
    si=ac_huffsize[0];

y2: do
       {
	/*
       **  Generation of AC Huffman code
       */
       ac_huffcode[p]=code;
       code++;
       p++;
       } while(ac_huffsize[p]==si);
    
    if(ac_huffsize[p]!=0)
       {
       do
	  {
	  code=code<<1;
	  si++;
	  } while(ac_huffsize[p]!=si);
       goto y2;
       }

    /*
    **   Reorder the values in HUFFCODE and HUFFSIZE according
    **   to the values in HUFFVAL.
    */
    p=0;

    do
       {
       i=d_huffval[p];
       /*
       **  dc code & size reorder
       */
       dc_ehuffco[i]=dc_huffcode[p];
       dc_ehuffsi[i]=dc_huffsize[p];
       p++;
       } while(p<dc_lastp);

    p=0;
    
    do
       {
       i=a_huffval[p];
       /*
       **  ac code & size reorder
       */
       ac_ehuffco[i]=ac_huffcode[p];
       ac_ehuffsi[i]=ac_huffsize[p];
       p++;
       } while(p<ac_lastp);
    }


/**********************************************************/
/*                                                        */
/* Function:     Get the length of a zz[] coefficient     */
/*                                                        */
/* Format:       log_of_int(long value)                   */
/*                                                        */
/* Parametre:    value-----value according to which the   */
/*                         log data to be calculated.     */
/*                                                        */
/* Return value: the log data.                            */
/*                                                        */
/**********************************************************/

int log_of_int(i)
long i;
   {
   int j;
   long k;
   i=labs(i);
   for(j=0,k=1;k<=i;j++,k*=2);
   return (j);
   }


/****************************************************************/
/*                                                              */
/* Function:    Send code of DC coefficient to the code stream  */
/*                                                              */
/* Format:      dc_send(long dc_datacode)                       */
/*                                                              */
/* Parametre:   dc_datacode----DC data to be encoded into code  */
/*                              stream.                         */
/*                                                              */
/* Return value: None.                                          */
/*                                                              */
/****************************************************************/

void dc_send(i)
long i;
    {
    int j,k,num_of_byte,ssss,code_si;
    unsigned int tmp1;
    unsigned char tmp[3];
    ssss=log_of_int(i);
    code_si=dc_ehuffsi[ssss];
    tmp1=dc_ehuffco[ssss];
    num_of_byte=(code_si-bit_last_left+15)/8;
    
    for(j=0;j<num_of_byte;j++)
       {
       if((k=code_si-bit_last_left)>=0)
	  tmp[j]=tmp1>>k;
       else
	  tmp[j]=tmp1<<(-k);
       
       if((code_stream[code_stream_length]|=tmp[j])==0xff)
	   {
	   code_stream_length++;
	   code_stream[code_stream_length]=0;
	   }
       
       if(k>=0)
	  code_stream_length++;
       code_si-=8;
       }

    if(k==0)
       bit_last_left=8;
    else
	/*
	**  encode category
	*/
       bit_last_left= -k;

    if(i==0)
       return;
    
    if(i<0)
       {
       i--;
       tmp1=0xffff;
       tmp1>>=16-ssss;
       tmp1&=i;
       }
    else
       tmp1=i;
    num_of_byte=(ssss-bit_last_left+15)/8;
    
    for(j=0;j<num_of_byte;j++)
       {
       if((k=ssss-bit_last_left)>=0)
	  tmp[j]=tmp1>>k;
       else
	  tmp[j]=tmp1<<(-k);
       
       if((code_stream[code_stream_length]|=tmp[j])==0xff)
	   {
	   code_stream_length++;
	   code_stream[code_stream_length]=0;
	   }
       
       if(k>=0)
	  code_stream_length++;
       ssss-=8;
       }
    
    if(k==0)
       bit_last_left=8;
    else
       bit_last_left= -k;
    }


/****************************************************************/
/*                                                              */
/* Function:    Send code of AC coefficient to the code stream  */
/*                                                              */
/* Format:      ac_send(int number,long ac_data)                */
/*                                                              */
/* Parameter:   number-----length  of 0 sequence                */
/*              ac_data----AC data to be encoded into code      */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -