| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 488 人关注过本帖
标题:帮忙解释个程序
只看楼主 加入收藏
danzengduoji52
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-4-22
收藏
 问题点数:0 回复次数:0 
帮忙解释个程序
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"  
#include "string.h"
#include "memory.h"         
#include "conio.h"
#include "lzwcode.h"
#include "lzwtable.h"

/*
class CLZWEncodeTable
*/
CLZWDecodeTable::CLZWDecodeTable(BOOL fInit)
{
    m_pbContain=NULL;
    m_dwTableEntryNumber=0;
    if(fInit)
        InitLZWTable();
}
CLZWDecodeTable::~CLZWDecodeTable()
{
    ClearDecodeTable();
}
void CLZWDecodeTable::ClearDecodeTable(void)
{   
    if(m_pbContain==NULL)
        return;
    for(int i=0;i<4096;i++)
    {
        if(m_pbContain[i])
            delete (m_pbContain[i]);
    }
    delete m_pbContain;
    m_pbContain=NULL;
    m_dwTableEntryNumber=0;
}
void CLZWDecodeTable::InitLZWTable(void)
{
    ClearDecodeTable();
    m_pbContain=new BYTE*[4096];
    int iSize=sizeof(m_pbContain);
    //if(NULL==m_pbContain)
    //    AfxMessageBox("error new m_pbContain=BYTE*[4096]");
    for(int i=0;i<4096;i++)
    {
        m_pbContain[i]=NULL;
    }
    for(i=0;i<=255;i++)
    {
        m_pbContain[i]=new BYTE[1+2];
        *((WORD*)m_pbContain[i])=1;
        m_pbContain[i][2]=(BYTE)i;
    }
    m_dwTableEntryNumber=LZW_BEGIN_ENTRY;
}
BYTE* CLZWDecodeTable::GetMatchData(WORD wCode)
{
    return m_pbContain[wCode];
}
void CLZWDecodeTable::AddToChild(WORD wCode,BYTE *pbContain,int iLength)
{
    ASSERT(wCode<4096);
    if(m_pbContain[wCode])
        delete m_pbContain[wCode];
    m_pbContain[wCode]=new BYTE[iLength+2];
    *((WORD*)m_pbContain[wCode])=(WORD)iLength;
    memcpy(m_pbContain[wCode]+2,pbContain,iLength);
}

/*
class CLZWEncodeTable
*/
CLZWEncodeTable::CLZWEncodeTable(BOOL fInit)
{
    if(fInit)
        InitLZWTable();
    else
    {
        m_dwTableEntryNumber=0;
        m_EntryHead.pRightBrother=NULL;
        m_EntryHead.pChild=NULL;
    }
}
CLZWEncodeTable::~CLZWEncodeTable()
{
    ClearLZWTable();
}
void CLZWEncodeTable::ClearLZWTable(void)
{
    if(m_EntryHead.pChild==NULL)
        return;
    m_dwTableEntryNumber=0;
    int iRe=0;
    while(m_EntryHead.pChild)
    {
        RemoveFirstChild();
        iRe++;
        //printf("remove %d\n",++iRe);;
    }
}
void CLZWEncodeTable::RemoveFirstChild(void)
{
    PLZWENCODEENTRY pFirstChild=m_EntryHead.pChild;// this child will be removed
    if(pFirstChild->pChild)
    {
        m_EntryHead.pChild=pFirstChild->pChild;
        if(m_EntryHead.pChild->pRightBrother)
        {
            PLZWENCODEENTRY pRightBrother=FindRightBrother(m_EntryHead.pChild);
            pRightBrother->pRightBrother=pFirstChild->pRightBrother;
        }
        else
            (m_EntryHead.pChild)->pRightBrother=pFirstChild->pRightBrother;
        //delete pFirstChild;
    }
    else
        m_EntryHead.pChild=pFirstChild->pRightBrother;
    delete pFirstChild;
}
PLZWENCODEENTRY CLZWEncodeTable::FindRightBrother(PLZWENCODEENTRY pCurrent)
{
    PLZWENCODEENTRY pFind;
    pFind=pCurrent;
    while(pFind->pRightBrother)
    {
        pFind=pFind->pRightBrother;
    }
    return pFind;
}
void CLZWEncodeTable::InitLZWTable(void)
{// init the table ,it has 256 items code from 0 to 255
    ClearLZWTable();
    PLZWENCODEENTRY pEntryFirst=new LZWENCODEENTRY;
    pEntryFirst->wCode=(WORD)0;
    pEntryFirst->bLast=(BYTE)0;
    pEntryFirst->pRightBrother=pEntryFirst->pChild=NULL;
    m_EntryHead.pChild=pEntryFirst;
    m_EntryHead.pRightBrother=NULL;
    PLZWENCODEENTRY pPrev=pEntryFirst;
    for(int i=1;i<=255;i++)
    {// set the brother nodes
        PLZWENCODEENTRY pEntry=new LZWENCODEENTRY;
        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;
}
PLZWENCODEENTRY CLZWEncodeTable::FindMatchChild(BYTE bChildLast,PLZWENCODEENTRY pCurrent)
{// return the find child entry
    if(pCurrent->pChild==NULL)
        return NULL;
    PLZWENCODEENTRY pChild=pCurrent->pChild;
    // pChild is the current's child
    // and all pChild's brother is the current's child
    while(pChild!=NULL)
    {
        if(pChild->bLast==bChildLast)
            return pChild;
        pChild=pChild->pRightBrother;
    }
    return NULL;
}
PLZWENCODEENTRY CLZWEncodeTable::AddToChild(BYTE bLast,PLZWENCODEENTRY pCurrent)
{
    ASSERT(pCurrent);
    PLZWENCODEENTRY pChild=new LZWENCODEENTRY;
    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;
    return pChild;
}
搜索更多相关主题的帖子: 解释 
2008-04-22 16:08
快速回复:帮忙解释个程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023557 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved