要求完整的源程序:
输入:图像变换后的数据
输出:图像编码
最好有中文解释!
如果好,马上给分!
谢谢

解决方案 »

  1.   


    /*************************************编码部分********************************************/
    long putbuffer=0;// 编码时位流缓冲中的积聚的位数据
    int  putbits=0;// 编码时位流缓冲中数据的位长度
    //由基本哈夫表产生编码用的哈夫表
    void MakeHufTable( )
    {
    unsigned int hsize[256],hcode[256];
    int i,j,k,n;
    int size,index;
    unsigned int code;

    for(n=0;n<4;n++)  
    {
    //产生每个code所占的位长度
    k=0;
    for(i=1;i<=16;i++)
    for(j=1;j<=codesize[n][i-1];j++)  
    {
    hsize[k]=i;
    k++;
    }
    hsize[k]=0;
    codenum[n]=k;
    //产生每个code本身的值  
    k=0;
    code=0;
    size=hsize[0];
    for(;;)  
    {
    do  
    {
    hcode[k++]=code;
    code++;
    }while(hsize[k]==size);

    if(hsize[k]==0) break;
    do  
    {
    code<<=1;
    size++;
    }while(hsize[k]!=size);
    }

    /* while (hsize[n][k]) 
    {
    while (hsize[n][k] == size) 
    {
    hsize[n][k++] = code;
    code++;
    }
    code <<= 1;
    size++;
    }
    */ //产生整张哈夫曼编码表
    for(k=0;k<codenum[n];k++)  
    {
    index=huffval[n][k];
    hufcode[n][index]=hcode[k];
    hufsize[n][index]=hsize[k];
    } }
    }//处理位流,当put_buffer的位长度大于8就调用EmitByte保存一个字节
    //由于每个code最长为16bit,而state.put_buffer中只要超过8bit就会被处理,
    //所以state.put_buffer存在的一直不会超过8bit,我们就只要用到put_buffer的右24bit
    void EmitBits(unsigned int code, int size)
    {
    register long put_buffer = (long) code;
    register int put_bits = putbits; if (size == 0) 
    return ;
    put_buffer &= (((long) 1)<<size) - 1;
    put_bits += size;
    put_buffer <<= 24 - put_bits; 
    put_buffer |= putbuffer; //merge with old buffer contents 
      while(put_bits >= 8) 
    {
    unsigned char c = (int) ((put_buffer >> 16) & 0xFF);
    bswrite(c);
    if (c == 0xFF)//碰到0xFF,要避免解码时与段标识符混淆,所以在其后加一字节0x00
    { // need to stuff a zero byte? 
      bswrite(0);
    }
    put_buffer <<= 8;
    put_bits -= 8;
    }
    putbuffer = put_buffer;
    putbits = put_bits;
    }//对MCU中的Block哈夫编码
    void HufEncodeBlock(short *block,unsigned char DCHufIndex, unsigned char ACHufIndex)
    {
    register int temp, temp2;
    register int nbits;
    register int k, r, i;
    unsigned int HufTabindex; //对DC系数差值编码
    HufTabindex=DCHufIndex;
    temp = temp2 = block[0]; if (temp < 0) //如果是负数要取绝对值后,再取反相
    {
    temp = -temp; 
    temp2--;
    }
    //算出temp的位数  
    nbits = 0;
    while (temp) 
    {
    nbits++;
    temp >>= 1;
    }

    //先把DiffDC位长度对应的哈夫曼编码按该编码位长度加到put_buffer中
    EmitBits(hufcode[HufTabindex][nbits], hufsize[HufTabindex][nbits]);
    //如果DiffDC为正数,则把DiffDC本身按其位长度加到put_buffer中
    //如果DiffDC为负数,则把DiffDC本身的绝对值取其1的补数按其位长度加到put_buffer中
    //1的补数即反相,比如一个二进制数100101,则其1的补数为011010
    if (nbits) // emit_bits rejects calls with size 0 
    EmitBits((unsigned int) temp2, nbits); //对从第1~63个AC系数编码,先用行程编码,再用哈夫编码
    HufTabindex=ACHufIndex;
        r = 0; // r为0的run length(行程长度)
        for (k = 1; k < DCTSIZE; k++) 
    {
    if ((temp = block[k]) == 0) //这里要改
    r++;
    else 
    {
    // 如果run length > 15, 必须先把0行程(ZRL,即(15,0),表示有连续16个0)处理
    //再把run-length-16。ZRL在哈夫码表中对应的位置为0xF0
    while (r > 15) 
    {
    EmitBits(hufcode[HufTabindex][0xF0], hufsize[HufTabindex][0xF0]);
    r -= 16;
    }
    temp2 = temp;
    if (temp < 0) 
    {
    temp = -temp;
    temp2--;
    }
    nbits = 1; // 对就AC系数至少为1bit 
    while ((temp >>= 1))
    nbits++;
    //先把<R,V_length>的哈夫曼编码按该编码位长度加到put_buffer中
    i = (r << 4) + nbits;
    EmitBits(hufcode[HufTabindex][i], hufsize[HufTabindex][i]);
    //接着同上
    EmitBits((unsigned int) temp2, nbits);
    r = 0;
    }
    }
    //如果后面全为0,则用EOB表示,EOB在哈夫码表中对应的位置为0
    if (r > 0)
    EmitBits(hufcode[HufTabindex][0], hufsize[HufTabindex][0]);
    }
    void EncodeMCU(short *MCUBlock)
    {
    static int ycoef=0,ucoef=0,vcoef=0;
    int i;
    short *buffer=MCUBlock,coef; for(i=0;i<YinMCU;i++)
    {
    QuanBlock(buffer,YQt,128);
    coef=buffer[0];
    buffer[0]=coef-ycoef;
    ycoef=coef;
    HufEncodeBlock(buffer,YDCindex,YACindex);
    buffer+=64;
    }
    for(i=0;i<UinMCU;i++)  
    {
    QuanBlock(buffer,UQt,0);
    coef=buffer[0];
    buffer[0]=coef-ucoef;
    ucoef=coef;
    HufEncodeBlock(buffer,UVDCindex,UVACindex);
    buffer+=64;
    }
    for(i=0;i<VinMCU;i++)  
    {
    QuanBlock(buffer,VQt,0);
    coef=buffer[0];
    buffer[0]=coef-vcoef;
    vcoef=coef;
    HufEncodeBlock(buffer,UVDCindex,UVACindex);
    buffer+=64;
    }
    }
      

  2.   

    unsigned char codesize[4][16]={
    { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
    { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
    { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d},
    { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77}
    };
    unsigned char huffval[4][256]={
    { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
      0x08, 0x09, 0x0a, 0x0b, 0x00, 0x00, 0x00, 0x00},
    { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
      0x08, 0x09, 0x0a, 0x0b, 0x00, 0x00, 0x00, 0x00},
        { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
          0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
          0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
          0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
          0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
          0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
          0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
          0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
          0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
          0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
          0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
          0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
          0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
          0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
          0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
          0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
          0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
          0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
          0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
          0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
          0xf9, 0xfa },   
        { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
          0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
          0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
          0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
          0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
          0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
          0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
          0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
          0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
          0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
          0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
          0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
          0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
          0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
          0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
          0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
          0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
          0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
          0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
          0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
          0xf9, 0xfa }
    };unsigned int hufsize[4][256],hufcode[4][256],codenum[4];
      

  3.   

    #include "stdafx.h"
    //===================================================================// 2 . &frac12;¨&Aacute;&cent;huffman&Ecirc;÷, ·&micro;&raquo;&Oslash;root&micro;&Auml;&Icirc;&raquo;&Ouml;&Atilde;
    DWORD  Huff_Build_Tree (DWORD Count_Array[256], 
    HUFF_NODE Node_Array [512])
    {
    DWORD dwTemp ;
    int min_1, min_2 ; // &sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;&Ograve;&para;&frac12;&Uacute;&micro;&atilde;
    for (dwTemp = 0 ; dwTemp < 256 ; dwTemp++)
    if (Count_Array[dwTemp] != 0)
    {
    Node_Array[dwTemp].active  = 1 ; // &Icirc;&ordf;&raquo;&icirc;&para;&macr;&frac12;&Uacute;&micro;&atilde;
    Node_Array[dwTemp].number  = (BYTE) dwTemp ; // &Ouml;&micro;
    Node_Array[dwTemp].count   = Count_Array[dwTemp] ; // &Iacute;&sup3;&frac14;&AElig;&frac14;&AElig;&Ecirc;&yacute;
    Node_Array[dwTemp].child_0 = 0xFFFF ;
    }
    else
    Node_Array[dwTemp].active = 0 ; // &Icirc;&ordf;&sup2;&raquo;&raquo;&icirc;&para;&macr;&frac12;&Uacute;&micro;&atilde; dwTemp = 256 ;  // &Ouml;&cedil;&Iuml;òNode_Array&micro;&Auml;&iquest;&Otilde;&Icirc;&raquo;&Ouml;&Atilde;
    while (true)
    {
    if ( !__fooSearchMin2 (Node_Array, &min_1, &min_2) )
    break ;
    Node_Array[dwTemp].child_0 = min_1 ;
    Node_Array[dwTemp].child_1 = min_2 ;
    Node_Array[dwTemp].count   = Node_Array[min_1].count + Node_Array[min_2].count ;
    Node_Array[dwTemp].active  = 1 ;  // &raquo;&icirc;&para;&macr;&frac12;&Uacute;&micro;&atilde;
    dwTemp++ ;
    }
    return (--dwTemp) ;
    }//===================================================================// 3 . &frac12;¨&Aacute;&cent;&Oacute;&sup3;&Eacute;&auml;±í
    void  Huff_Create_Table (HUFF_NODE Node_Array[512], HUFF_CODE Code_Array[256],
     DWORD code_walk, // init == 0
     WORD  bit_walk, // init == 0
     DWORD root)
    {
    if (Node_Array[root].child_0 == 0xFFFF) // &micro;&frac12;&acute;&iuml;&Ograve;&para;&frac12;&Uacute;&micro;&atilde;
    {
    Code_Array[root].code = code_walk ;
    Code_Array[root].bit_length = bit_walk ;
    return ;
    } code_walk <<= 1 ;
    bit_walk++ ;
    Huff_Create_Table (Node_Array, Code_Array, code_walk,
       bit_walk, Node_Array[root].child_0) ;
    Huff_Create_Table (Node_Array, Code_Array, code_walk | 1,
       bit_walk, Node_Array[root].child_1) ;
    return ;
    }//===================================================================DWORD  Huff_Encode (BYTE * InBuffer, DWORD dwInSize,
    BYTE * OutBuffer, BYTE * WriteBit)
    {
    HUFF_NODE Node_Array [512] ;
    HUFF_CODE Code_Array[256] ;
    DWORD Count_Array[256] ;
    DWORD dwRoot ; Huff_Count (InBuffer, dwInSize, Count_Array) ;
    dwRoot = Huff_Build_Tree (Count_Array, Node_Array) ;
    Huff_Create_Table (Node_Array, Code_Array, 0, 0, dwRoot) ; BYTE * pOldOut = OutBuffer ;
    BYTE BitPos  = 8 ; // Out Bit Pointer
    BYTE code ;
    DWORD index = 0 ; // In Buffer index
    DWORD dwTmp ;
    register BYTE byLeftBit ;
    while (index < dwInSize)
    {
    code = InBuffer[index++] ; byLeftBit = (BYTE)Code_Array[code].bit_length ;
    while (byLeftBit > BitPos)
    {
    dwTmp = Code_Array[code].code ;
    dwTmp >>= byLeftBit - BitPos ;
    *OutBuffer++ |= (BYTE)dwTmp ;
    byLeftBit -= BitPos ;
    BitPos = 8 ;
    } BYTE byTmp = (BYTE)Code_Array[code].code ;
    byTmp <<= BitPos - byLeftBit ;
    *OutBuffer |= byTmp ;
    BitPos -= byLeftBit ;
    if (BitPos == 0)
    {
    OutBuffer++ ;
    BitPos = 8 ;
    }
    }
    if (WriteBit != NULL)
    *WriteBit = 8 - BitPos ;
    return OutBuffer - pOldOut ;
    }//===================================================================void  Huff_Decode (BYTE * InBuffer, DWORD Count_Array[256],
       BYTE * OutBuffer, DWORD dwOutLength)
    {
    HUFF_NODE Node_Array [512] ;
    HUFF_CODE Code_Array [256] ; // &frac12;¨&Aacute;&cent; &Ecirc;÷ && &Oacute;&sup3;&Iuml;&ntilde;±í
    DWORD dwRoot = Huff_Build_Tree (Count_Array, Node_Array) ;
    Huff_Create_Table (Node_Array, Code_Array, 0, 0, dwRoot) ; HUFF_NODE code ;
    char cBit = 7 ; while (dwOutLength > 0)
    {
    code = Node_Array[dwRoot] ;
    while (true)
    {
    if (fooTestBit (*InBuffer, cBit))
    code = Node_Array[code.child_1] ;
    else
    code = Node_Array[code.child_0] ;
    if (--cBit < 0)
    {
    cBit = 7 ;
    InBuffer++ ;
    }
    if (code.child_0 == 0xFFFF) // &Ograve;&para;&frac12;&Uacute;&micro;&atilde;&Igrave;&Oslash;&Otilde;÷
    {
    *OutBuffer++ = code.number ;
    dwOutLength-- ;
    break ;
    }
    }
    }
    return ;
    }//===================================================================