【VC开源代码栏目提醒】:网学会员为广大网友收集整理了,CqOctree.cpp,希望对大家有所帮助!
/******************************************************************
CqOctree.CPP
Performing Color Quantization using Octree algorithm
The 2 functions for global use is
HPALETTE CreateOctreePalette (HBITMAP hImage, UINT nMaxColors, UINT nColorBits)
HPALETTE CreateOctreePalette (LPSTR lpDIB, UINT nMaxColors, UINT nColorBits)
For using convenience, define it in DIBAPI.H
******************************************************************/
#include "stdafx.h"
#include "dibapi.h"
// structure use internally
// store the necessary info of a node in octree
typedef struct _NODE
{
BOOL bIsLeaf; // TRUE if node has no children
UINT nPixelCount; // Number of pixels represented by this leaf
UINT nRedSum; // Sum of red components
UINT nGreenSum; // Sum of green components
UINT nBlueSum; // Sum of blue components
struct _NODE* pChild[8]; // Pointers to child nodes
struct _NODE* pNext; // Pointer to next reducible node
} NODE;
// Function prototypes
//Global use, define it in dibapi.h
//HPALETTE CreateOctreePalette (HDIB hDIB, UINT nMaxColors, UINT nColorBits)
//HPALETTE CreateOctreePalette (LPSTR lpDIB, UINT nMaxColors, UINT nColorBits)
//Local use only
HPALETTE BuildOctreePalette(HANDLE hImage, UINT nMaxColors, UINT nColorBits);
void AddColor (NODE**, BYTE, BYTE, BYTE, UINT, UINT, UINT*, NODE**);
NODE* CreateNode (UINT, UINT, UINT*, NODE**);
void ReduceTree (UINT, UINT*, NODE**);
void DeleteTree (NODE**);
void GetPaletteColors (NODE*, PALETTEENTRY*, UINT*);
int GetRightShiftCount (DWORD);
int GetLeftShiftCount (DWORD);
// Function body
/*************************************************************************
*
* CreateOctreePalette()
*
* Parameters:
*
* HDIB hDIB - Handle to source DIB
* UINT nMaxColors - destination color number
* UINT nColorBits - destination color bits
*
* Return Value:
*
* HPALETTE - Handle to the result palette
*
* Description:
*
* This function use Octree color quantization algorithm to get
* optimal m kinds of color to represent total n kinds of color
* in a DIB, and use the m kinds of color to build a palette.
* With the palette, we can display the DIB on reasonable accuracy.
*
************************************************************************/
HPALETTE CreateOctreePalette(HDIB hDIB, UINT nMaxColors, UINT nColorBits)
{
HANDLE hImage;
hImage = DIBToDIBSection(hDIB);
if (! hImage)
return NULL;
return BuildOctreePalette(hImage, nMaxColors, nColorBits);
}
/*************************************************************************
*
* CreateOctreePalette()
*
* Parameters:
*
* LPBYTE lpDIB - Pointer to DIB data buffer
* UINT nMaxColors - destination color number
* UINT nColorBits - destination color bits
*
* Return Value:
*
* HPALETTE - Handle to the result palette
*
* Description:
*
* This function use Octree color quantization algorithm to get
* optimal m kinds of color to represent total n kinds of color
* in a DIB, and use the m kinds of color to build a palette.
* With the palette, we can display the DIB on reasonable accuracy.
*
************************************************************************/
HPALETTE CreateOctreePalette(LPBYTE lpDIB, UINT nMaxColors, UINT nColorBits)
{
HANDLE hImage;
hImage = DIBToDIBSection(lpDIB);
if (! hImage)
return NULL;
return BuildOctreePalette(hImage, nMaxColors, nColorBits);
}
// local function to build optimal palette from DIBSection
HPALETTE BuildOctreePalette(HANDLE hImage, UINT nMaxColors, UINT nColorBits)
{
DIBSECTION ds;
int i, j, nPad;
BYTE* pbBits;
WORD* pwBits;
DWORD* pdwBits;
DWORD rmask, gmask, bmask;
int rright, gright, bright;
int rleft, gleft, bleft;
BYTE r, g, b;
WORD wColor;
DWORD dwColor, dwSize;
LOGPALETTE* plp;
HPALETTE hPalette;
NODE* pTre