📄 _octree.cpp
字号:
// _OCTree.cpp: implementation of the C_OCTree class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "_OCTree.h"
#include "_ReduceList.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
C_OCTree::C_OCTree()
{
m_wTotalLeafs = 0 ;
m_wTotalLevels = 0 ;
m_wLeafCount = 0 ;
m_psRoot = NULL ;
for(int i=0;i<MAX_LEVELS;i++)
{
m_apIReduceList[i] = NULL ;
}
}
C_OCTree::~C_OCTree()
{
//Release() ;
}
HRESULT C_OCTree::Initialize(WORD wTotalLeafs,WORD wTotalLevels)
{
if(m_psRoot != NULL)
{
return ERROR_OCTREE_INITIALIZED ;
}
if(wTotalLevels > MAX_LEVELS)
{
return ERROR_OCTREE_DEPTH_OVER ;
}
m_wTotalLeafs = wTotalLeafs ;
m_wTotalLevels = wTotalLevels ;
m_wLeafCount = 0 ;
m_psRoot = new S_Node ;
if(m_psRoot == NULL)
{
Release();
return ERROR_MEMORY_FAIL ;
}
for(int i=0;i<wTotalLevels-1;i++)
{
m_apIReduceList[i] = new C_ReduceList ;
if(m_apIReduceList[i] == NULL)
{
Release();
return ERROR_MEMORY_FAIL ;
}
}
return 0 ;
}
HRESULT C_OCTree::AddPixel(S_Color * psClr)
{
if(psClr == NULL)
{
return ERROR_INVALID_PARAM ;
}
S_Node * psNodeFather = m_psRoot ;
if(psNodeFather == NULL)
{
return ERROR_OCTREE_NOT_INITIALIZE ;
}
int i32Index = 0 ;
for(WORD i=0;i<m_wTotalLevels;i++)
{
i32Index = (((psClr->byRed) >> (7 - i)) << 2) & 0x04 ;
i32Index += (((psClr->byGreen) >> (7 - i)) << 1) & 0x02 ;
i32Index += (((psClr->byBlue) >> (7 - i)) ) & 0x01 ;
if(psNodeFather->psChild[i32Index] == NULL)
{
psNodeFather->psChild[i32Index] = new S_Node ;
if(psNodeFather->psChild[i32Index] == NULL)
{
return ERROR_MEMORY_FAIL ;
}
//== Add to Reduce list
if(i < m_wTotalLevels -1)
{
m_apIReduceList[i]->AddNode(psNodeFather->psChild[i32Index]);
}
}
psNodeFather->dwCounter ++ ;
psNodeFather->psChild[i32Index]->dwCounter ++ ;
psNodeFather = psNodeFather->psChild[i32Index] ;
if(psNodeFather->bLeafNode == TRUE)
{
break ;
}
}
psNodeFather->dwRedSum += psClr->byRed ;
psNodeFather->dwGreenSum += psClr->byGreen ;
psNodeFather->dwBlueSum += psClr->byBlue ;
if(psNodeFather->bLeafNode == FALSE)
{
psNodeFather->bLeafNode = TRUE ;
m_wLeafCount ++ ;
if(m_wLeafCount > m_wTotalLeafs)
{
_ReduceLeaf() ;
}
}
return 0 ;
}
HRESULT C_OCTree::FetchPalette(I_Palette * pIPalette)
{
if(pIPalette == NULL)
{
return ERROR_INVALID_PARAM ;
}
HRESULT hRet = pIPalette->Initialize(m_wTotalLeafs) ;
if(hRet != 0)
{
return hRet ;
}
return _FetchPalette(m_psRoot,pIPalette);
}
void C_OCTree::Release()
{
_ReleaseNode(m_psRoot);
m_psRoot = NULL ;
for(int i=0;i<MAX_LEVELS;i++)
{
if(m_apIReduceList[i] != NULL)
{
delete m_apIReduceList[i] ;
m_apIReduceList[i] = NULL ;
}
}
delete this ;
}
void C_OCTree::_ReleaseNode(S_Node * psNode)
{
if(psNode == NULL) return ;
for(int i=0;i<8;i++)
{
if(psNode->psChild[i])
{
_ReleaseNode(psNode->psChild[i]) ;
psNode->psChild[i] = NULL ;
}
}
delete psNode ;
}
HRESULT C_OCTree::_ReduceLeaf()
{
WORD wReduceCount = 0 ;
for(WORD i=m_wTotalLevels-2;i>=0;i--)
{
m_apIReduceList[i]->Reduce(&wReduceCount);
if(wReduceCount > 0)
{
m_wLeafCount -= wReduceCount ;
return 0 ;
}
}
return 0;
}
HRESULT C_OCTree::_FetchPalette(S_Node *psNode, I_Palette *pIPalette)
{
if(psNode == NULL || pIPalette == NULL)
{
return ERROR_INVALID_PARAM ;
}
HRESULT hRet ;
if(psNode->bLeafNode == TRUE)
{
S_Color sClr ;
sClr.byRed = (BYTE)(psNode->dwRedSum / psNode->dwCounter) ;
sClr.byGreen = (BYTE)(psNode->dwGreenSum / psNode->dwCounter) ;
sClr.byBlue = (BYTE)(psNode->dwBlueSum / psNode->dwCounter) ;
hRet = pIPalette->AddColor(&sClr);
if(hRet != 0)
{
return hRet ;
}
}
else
{
for(WORD i=0;i<8;i++)
{
if(psNode->psChild[i] != NULL)
{
hRet = _FetchPalette(psNode->psChild[i], pIPalette);
if(hRet != 0)
{
return hRet ;
}
}
}
}
return 0 ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -