亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? cqoctree.cpp

?? 《精通 vc++ 圖像編程》的源代碼
?? 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* pTree;
    UINT nLeafCount, nIndex;
    NODE* pReducibleNodes[9];
    HDC hdc;    
	BYTE* pBuffer = NULL;    
	BITMAPINFO bmi;

    // Initialize octree variables
    pTree = NULL;
    nLeafCount = 0;
    if (nColorBits > 8) // Just in case
        return NULL;
    for (i=0; i<=(int) nColorBits; i++)
        pReducibleNodes[i] = NULL;

    // Scan the DIB and build the octree
    GetObject (hImage, sizeof (ds), &ds);
    nPad = ds.dsBm.bmWidthBytes - (((ds.dsBmih.biWidth *
        ds.dsBmih.biBitCount) + 7) / 8);

    switch (ds.dsBmih.biBitCount) {

    case 1: // 1-bit DIB    
	case 4: // 4-bit DIB    
	case 8: // 8-bit DIB
        //        
		// The strategy here is to use ::GetDIBits to convert the
        // image into a 24-bit DIB one scan line at a time. A pleasant
        // side effect of using ::GetDIBits in this manner is that RLE-
        // encoded 4-bit and 8-bit DIBs will be uncompressed.        //
        hdc = GetDC (NULL);        
		pBuffer = new BYTE[ds.dsBmih.biWidth * 3];
        ZeroMemory (&bmi, sizeof (bmi));
        bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
        bmi.bmiHeader.biWidth = ds.dsBmih.biWidth;
        bmi.bmiHeader.biHeight = ds.dsBmih.biHeight;
        bmi.bmiHeader.biPlanes = 1;        
		bmi.bmiHeader.biBitCount = 24;
        bmi.bmiHeader.biCompression = BI_RGB;
        for (i=0; i<ds.dsBmih.biHeight; i++) 
		{
            GetDIBits (hdc, (HBITMAP) hImage, i, 1, pBuffer, &bmi,
                DIB_RGB_COLORS);            
			pbBits = pBuffer;
            for (j=0; j<ds.dsBmih.biWidth; j++) 
			{                
				b = *pbBits++;
                g = *pbBits++;                
				r = *pbBits++;
                AddColor (&pTree, r, g, b, nColorBits, 0, &nLeafCount,
                          pReducibleNodes);
                while (nLeafCount > nMaxColors)
                    ReduceTree (nColorBits, &nLeafCount, pReducibleNodes);            
			}        
		}
		ReleaseDC (NULL, hdc);        
		break;    
	
	case 16: // One case for 16-bit DIBs
        if (ds.dsBmih.biCompression == BI_BITFIELDS) {
            rmask = ds.dsBitfields[0];
            gmask = ds.dsBitfields[1];
            bmask = ds.dsBitfields[2];
        }
        else {
            rmask = 0x7C00;
            gmask = 0x03E0;
            bmask = 0x001F;
        }

        rright = GetRightShiftCount (rmask);
        gright = GetRightShiftCount (gmask);
        bright = GetRightShiftCount (bmask);

        rleft = GetLeftShiftCount (rmask);
        gleft = GetLeftShiftCount (gmask);
        bleft = GetLeftShiftCount (bmask);

        pwBits = (WORD*) ds.dsBm.bmBits;
        for (i=0; i<ds.dsBmih.biHeight; i++) {
            for (j=0; j<ds.dsBmih.biWidth; j++) {
                wColor = *pwBits++;
                b = (BYTE) (((wColor & (WORD) bmask) >> bright) << bleft);
                g = (BYTE) (((wColor & (WORD) gmask) >> gright) << gleft);
                r = (BYTE) (((wColor & (WORD) rmask) >> rright) << rleft);
                AddColor (&pTree, r, g, b, nColorBits, 0, &nLeafCount,
                          pReducibleNodes);
                while (nLeafCount > nMaxColors)
                    ReduceTree (nColorBits, &nLeafCount, pReducibleNodes);
            }
            pwBits = (WORD*) (((BYTE*) pwBits) + nPad);
        }
        break;

    case 24: // Another for 24-bit DIBs
        pbBits = (BYTE*) ds.dsBm.bmBits;
        for (i=0; i<ds.dsBmih.biHeight; i++) {
            for (j=0; j<ds.dsBmih.biWidth; j++) {
                b = *pbBits++;
                g = *pbBits++;
                r = *pbBits++;
                AddColor (&pTree, r, g, b, nColorBits, 0, &nLeafCount,
                          pReducibleNodes);
                while (nLeafCount > nMaxColors)
                    ReduceTree (nColorBits, &nLeafCount, pReducibleNodes);
            }
            pbBits += nPad;
        }
        break;

    case 32: // And another for 32-bit DIBs
        if (ds.dsBmih.biCompression == BI_BITFIELDS) {
            rmask = ds.dsBitfields[0];
            gmask = ds.dsBitfields[1];
            bmask = ds.dsBitfields[2];
        }
        else {
            rmask = 0x00FF0000;
            gmask = 0x0000FF00;
            bmask = 0x000000FF;
        }

        rright = GetRightShiftCount (rmask);
        gright = GetRightShiftCount (gmask);
        bright = GetRightShiftCount (bmask);

        pdwBits = (DWORD*) ds.dsBm.bmBits;
        for (i=0; i<ds.dsBmih.biHeight; i++) {
            for (j=0; j<ds.dsBmih.biWidth; j++) {
                dwColor = *pdwBits++;
                b = (BYTE) ((dwColor & bmask) >> bright);
                g = (BYTE) ((dwColor & gmask) >> gright);
                r = (BYTE) ((dwColor & rmask) >> rright);
                AddColor (&pTree, r, g, b, nColorBits, 0, &nLeafCount,
                          pReducibleNodes);
                while (nLeafCount > nMaxColors)
                    ReduceTree (nColorBits, &nLeafCount, pReducibleNodes);
            }
            pdwBits = (DWORD*) (((BYTE*) pdwBits) + nPad);
        }
        break;

    default: // DIB must be 16, 24, or 32-bit!
        return NULL;
    }

	// cleanup
	if (pBuffer)
		delete[] pBuffer;        

   if (nLeafCount > nMaxColors) 
   { // Sanity check
        DeleteTree (&pTree);
        return NULL;
    }

    // Create a logical palette from the colors in the octree
    dwSize = sizeof (LOGPALETTE) + ((nLeafCount - 1) * sizeof (PALETTEENTRY));
    if ((plp = (LOGPALETTE*) HeapAlloc (GetProcessHeap (), 0,
        dwSize)) == NULL) {
        DeleteTree (&pTree);
        return NULL;
    }

    plp->palVersion = 0x300;
    plp->palNumEntries = (WORD) nLeafCount;
    nIndex = 0;
    GetPaletteColors (pTree, plp->palPalEntry, &nIndex);
    hPalette = CreatePalette (plp);

    HeapFree (GetProcessHeap (), 0, plp);
    DeleteTree (&pTree);
    return hPalette;
}

// local function to add a color to octree
void AddColor (NODE** ppNode, BYTE r, BYTE g, BYTE b, UINT nColorBits,
    UINT nLevel, UINT* pLeafCount, NODE** pReducibleNodes)
{
    int nIndex, shift;
    static BYTE mask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };

    // If the node doesn't exist, create it
    if (*ppNode == NULL)
        *ppNode = CreateNode (nLevel, nColorBits, pLeafCount,
                              pReducibleNodes);

    // Update color information if it's a leaf node
    if ((*ppNode)->bIsLeaf) {
        (*ppNode)->nPixelCount++;
        (*ppNode)->nRedSum += r;
        (*ppNode)->nGreenSum += g;
        (*ppNode)->nBlueSum += b;
    }

    // Recurse a level deeper if the node is not a leaf
    else {
        shift = 7 - nLevel;
        nIndex = (((r & mask[nLevel]) >> shift) << 2) |
            (((g & mask[nLevel]) >> shift) << 1) |
            ((b & mask[nLevel]) >> shift);
        AddColor (&((*ppNode)->pChild[nIndex]), r, g, b, nColorBits,
                  nLevel + 1, pLeafCount, pReducibleNodes);
    }
}

// local function to create a new node in octree
NODE* CreateNode (UINT nLevel, UINT nColorBits, UINT* pLeafCount,
                  NODE** pReducibleNodes)
{
    NODE* pNode;

    if ((pNode = (NODE*) HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY,
        sizeof (NODE))) == NULL)
        return NULL;

    pNode->bIsLeaf = (nLevel == nColorBits) ? TRUE : FALSE;
    if (pNode->bIsLeaf)
        (*pLeafCount)++;
    else { // Add the node to the reducible list for this level
        pNode->pNext = pReducibleNodes[nLevel];
        pReducibleNodes[nLevel] = pNode;
    }
    return pNode;
}

// local function to reduce the nodes of octree
void ReduceTree (UINT nColorBits, UINT* pLeafCount, NODE** pReducibleNodes)
{
    int i;
    NODE* pNode;
    UINT nRedSum, nGreenSum, nBlueSum, nChildren;

    // Find the deepest level containing at least one reducible node
    for (i=nColorBits - 1; (i>0) && (pReducibleNodes[i] == NULL); i--);

    // Reduce the node most recently added to the list at level i
    pNode = pReducibleNodes[i];
    pReducibleNodes[i] = pNode->pNext;

    nRedSum = nGreenSum = nBlueSum = nChildren = 0;
    for (i=0; i<8; i++) {
        if (pNode->pChild[i] != NULL) {
            nRedSum += pNode->pChild[i]->nRedSum;
            nGreenSum += pNode->pChild[i]->nGreenSum;
            nBlueSum += pNode->pChild[i]->nBlueSum;
            pNode->nPixelCount += pNode->pChild[i]->nPixelCount;
            HeapFree (GetProcessHeap (), 0, pNode->pChild[i]);
            pNode->pChild[i] = NULL;
            nChildren++;
        }
    }

    pNode->bIsLeaf = TRUE;
    pNode->nRedSum = nRedSum;
    pNode->nGreenSum = nGreenSum;
    pNode->nBlueSum = nBlueSum;
    *pLeafCount -= (nChildren - 1);
}

// local function to delete a node of octree
void DeleteTree (NODE** ppNode)
{
    int i;

    for (i=0; i<8; i++) {
        if ((*ppNode)->pChild[i] != NULL)
            DeleteTree (&((*ppNode)->pChild[i]));
    }
    HeapFree (GetProcessHeap (), 0, *ppNode);
    *ppNode = NULL;
}

// local function to get color entry from a palette
void GetPaletteColors (NODE* pTree, PALETTEENTRY* pPalEntries, UINT* pIndex)
{
    int i;

    if (pTree->bIsLeaf) {
        pPalEntries[*pIndex].peRed =
            (BYTE) ((pTree->nRedSum) / (pTree->nPixelCount));
        pPalEntries[*pIndex].peGreen =
            (BYTE) ((pTree->nGreenSum) / (pTree->nPixelCount));
        pPalEntries[*pIndex].peBlue =
            (BYTE) ((pTree->nBlueSum) / (pTree->nPixelCount));
        (*pIndex)++;
    }
    else {
        for (i=0; i<8; i++) {
            if (pTree->pChild[i] != NULL)
                GetPaletteColors (pTree->pChild[i], pPalEntries, pIndex);
        }
    }
}

// local function to get pixel count
int GetRightShiftCount (DWORD dwVal)
{
    int i;

    for (i=0; i<sizeof (DWORD) * 8; i++) {
        if (dwVal & 1)
            return i;
        dwVal >>= 1;
    }
    return -1;
}

// local function to get pixel count
int GetLeftShiftCount (DWORD dwVal)
{
    int nCount, i;

    nCount = 0;
    for (i=0; i<sizeof (DWORD) * 8; i++) {
        if (dwVal & 1)
            nCount++;
        dwVal >>= 1;
    }
    return (8 - nCount);
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产 日韩 欧美大片| 国产精品毛片大码女人| 久久日韩粉嫩一区二区三区| 一区二区三区高清| 成人精品电影在线观看| 久久午夜色播影院免费高清| 日韩精品成人一区二区在线| 91麻豆自制传媒国产之光| 欧美成人精品高清在线播放| 日韩精品乱码免费| 亚洲国产经典视频| 麻豆久久久久久| 2019国产精品| 国内成人精品2018免费看| 精品成人佐山爱一区二区| 久久99久久久久久久久久久| 久久亚洲精精品中文字幕早川悠里 | 色悠悠亚洲一区二区| 亚洲欧美色图小说| 欧美性受xxxx黑人xyx性爽| 亚洲一区在线观看视频| 日韩亚洲欧美中文三级| 狠狠色狠狠色综合日日91app| 国产亚洲精品超碰| 欧美日韩综合色| 极品少妇xxxx精品少妇偷拍| 亚洲婷婷在线视频| 欧美一卡2卡3卡4卡| 粉嫩aⅴ一区二区三区四区| 亚洲同性gay激情无套| 91精品福利在线一区二区三区| 美女视频网站久久| 亚洲欧美经典视频| 精品91自产拍在线观看一区| 成人av免费网站| 日本中文字幕一区| 自拍av一区二区三区| 日韩一区二区三区免费看| av福利精品导航| 久久91精品久久久久久秒播| 中文字幕亚洲一区二区av在线| 欧美日韩国产首页在线观看| 成人h动漫精品| 亚洲精品日产精品乱码不卡| 日本二三区不卡| 久久99国产精品久久99果冻传媒| 亚洲激情在线激情| 中文字幕在线播放不卡一区| 国产精品麻豆一区二区| 色综合网色综合| 一区二区三区中文在线观看| 精品久久人人做人人爰| 4438x亚洲最大成人网| 一本色道**综合亚洲精品蜜桃冫| 韩国av一区二区三区四区| 天天色图综合网| 五月天一区二区三区| 一区二区三区四区国产精品| 中文字幕亚洲区| 久久蜜桃av一区二区天堂| 日韩午夜电影在线观看| 91九色02白丝porn| 欧美性欧美巨大黑白大战| 97se亚洲国产综合自在线不卡| 波多野结衣欧美| 成人午夜碰碰视频| 一本大道av伊人久久综合| 欧美午夜精品一区二区三区| 欧美三级中文字幕在线观看| 欧美福利电影网| 日本高清不卡视频| 欧美视频三区在线播放| 欧美高清激情brazzers| 欧美大片顶级少妇| 久久精品人人做人人爽人人 | 亚洲三级小视频| 一区二区三区日韩欧美| 日韩精品一级二级| 久久精品国产秦先生| 精彩视频一区二区| 成人爽a毛片一区二区免费| 精品污污网站免费看| 欧美另类一区二区三区| 2020国产成人综合网| 成人免费在线播放视频| 亚洲私人黄色宅男| 亚洲电影视频在线| 国产成人av电影免费在线观看| 一本大道久久a久久综合婷婷| 5566中文字幕一区二区电影 | 免费在线看成人av| 99久久婷婷国产| 国产亚洲精品aa| 麻豆成人在线观看| 欧美精品电影在线播放| 国产精品国产三级国产a| 精品一区免费av| 91麻豆精品国产自产在线| 亚洲一区二区三区三| 99久久精品国产精品久久| 久久久久国产精品麻豆ai换脸| 婷婷亚洲久悠悠色悠在线播放| www.日韩av| 国产欧美精品日韩区二区麻豆天美| 九九国产精品视频| 精品少妇一区二区三区日产乱码| 一区二区三区久久| 色素色在线综合| 亚洲一区二三区| 欧美色综合久久| 亚洲国产精品久久久久婷婷884 | 理论片日本一区| 91亚洲午夜精品久久久久久| 欧美电影免费观看高清完整版在| 亚洲美女在线一区| 成人免费毛片aaaaa**| 国产欧美日韩精品a在线观看| 国产成人综合网站| 亚洲欧洲精品一区二区三区不卡| 国产乱子伦视频一区二区三区| 51精品久久久久久久蜜臀| 日韩国产欧美在线观看| 日韩欧美久久久| 高清av一区二区| 亚洲精品国产无天堂网2021| 欧美亚洲自拍偷拍| 蜜臀91精品一区二区三区| 26uuu成人网一区二区三区| 成人美女视频在线看| 亚洲综合一二三区| 欧美成人国产一区二区| 粉嫩高潮美女一区二区三区| 亚洲视频一区二区在线| 99久精品国产| 日韩电影在线免费看| 欧美韩国日本不卡| 欧美日韩综合不卡| 国产在线精品一区二区三区不卡| 国产精品久久三区| 欧美日韩精品是欧美日韩精品| 国产在线播放一区二区三区| 亚洲国产美女搞黄色| 中文字幕一区二区在线观看| 日韩三级视频在线看| 欧美日韩亚洲另类| 日本精品一区二区三区高清| 狠狠色丁香婷婷综合久久片| 亚洲午夜视频在线观看| 亚洲人成伊人成综合网小说| 国产清纯美女被跳蛋高潮一区二区久久w| 色素色在线综合| 不卡的av网站| 成人网页在线观看| 一区二区成人在线| 日韩毛片精品高清免费| 中文无字幕一区二区三区| 日韩欧美国产一区二区三区 | 亚洲免费成人av| 亚洲精选一二三| 亚洲一区二区在线视频| 亚洲一区二区视频在线观看| 欧美一区二区三区四区视频| 在线播放91灌醉迷j高跟美女| 国产精品1区2区3区在线观看| 精品亚洲国产成人av制服丝袜| 蜜臀va亚洲va欧美va天堂| 日韩va亚洲va欧美va久久| 轻轻草成人在线| 国产精品亚洲а∨天堂免在线| 久久精品理论片| 国产91精品露脸国语对白| 9人人澡人人爽人人精品| 色欧美88888久久久久久影院| 日本精品一区二区三区高清 | 自拍偷拍国产亚洲| 精品久久国产字幕高潮| 国产精品欧美久久久久无广告| 亚洲欧洲成人自拍| 日韩精品每日更新| 成人av在线资源网站| 韩国视频一区二区| 91麻豆精品在线观看| 日韩欧美高清dvd碟片| 欧美国产激情一区二区三区蜜月| 亚洲国产精品影院| 国产·精品毛片| 91精品久久久久久久91蜜桃| 国产精品日产欧美久久久久| 喷水一区二区三区| 99re这里都是精品| 九色|91porny| www.欧美日韩国产在线| 欧美一级日韩不卡播放免费| 欧美不卡激情三级在线观看| 樱花影视一区二区| av不卡在线播放| 欧美激情在线一区二区三区| 老司机精品视频线观看86| 欧美图片一区二区三区| xnxx国产精品|