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

? 歡迎來(lái)到蟲(chóng)蟲(chóng)下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲(chóng)蟲(chóng)下載站

?? cqoctree.cpp

?? 《精通 vc++ 圖像編程》的源代碼
?? CPP
字號(hào):
/******************************************************************
	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
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

HPALETTE CreateOctreePalette(HDIB hDIB, UINT nMaxColors, UINT nColorBits)
{
	HANDLE hImage;

	hImage = DIBToDIBSection(hDIB);
	if (! hImage)
		return NULL;
	return BuildOctreePalette(hImage, nMaxColors, nColorBits);
}

HPALETTE CreateOctreePalette(LPBYTE lpDIB, UINT nMaxColors, UINT nColorBits)
{
	HANDLE hImage;

	hImage = DIBToDIBSection(lpDIB);
	if (! hImage)
		return NULL;
	return BuildOctreePalette(hImage, nMaxColors, nColorBits);
}

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;    
	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);            
			}        
		}
        delete pBuffer;        
		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;
    }

    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;
}

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);
    }
}

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;
}

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);
}

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;
}

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);
        }
    }
}

int GetRightShiftCount (DWORD dwVal)
{
    int i;

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

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);
}

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
激情偷乱视频一区二区三区| 91免费看`日韩一区二区| 精品成人一区二区| 国产91高潮流白浆在线麻豆| 久久久91精品国产一区二区三区| 91在线视频免费观看| 91免费精品国自产拍在线不卡| 亚洲欧美另类小说视频| 欧美日韩一区高清| 麻豆91在线看| 国产欧美日韩麻豆91| 91麻豆国产福利在线观看| 亚洲一区二区欧美| 欧美大白屁股肥臀xxxxxx| 豆国产96在线|亚洲| 亚洲自拍偷拍av| 欧美成人三级电影在线| 国产宾馆实践打屁股91| 亚洲欧美福利一区二区| 日韩一区二区三区视频| 国产不卡视频在线播放| 亚洲精品成人悠悠色影视| 正在播放亚洲一区| 国产精品1区二区.| 亚洲精品亚洲人成人网在线播放| 欧美军同video69gay| 国产精品一级片在线观看| 亚洲女人的天堂| 日韩视频一区在线观看| 99视频在线精品| 日本成人在线视频网站| 欧美国产精品v| 欧美在线观看一区| 国产一区二三区好的| 亚洲视频一区二区在线观看| 5566中文字幕一区二区电影 | 一区二区三区波多野结衣在线观看| 久久婷婷综合激情| 日本成人在线电影网| 欧美激情中文字幕| 欧美日韩另类一区| 国产精品中文字幕一区二区三区| 一个色在线综合| 久久女同互慰一区二区三区| 欧美网站一区二区| 国产高清不卡二三区| 偷拍一区二区三区| 国产精品人人做人人爽人人添| 欧美一区三区二区| 99视频在线精品| 加勒比av一区二区| 亚洲永久免费视频| 国产精品区一区二区三区| 欧美一区二区三区在线观看| av欧美精品.com| 韩国三级在线一区| 亚洲va欧美va人人爽| 国产精品国产三级国产三级人妇| 欧美一区二区久久| 日本伦理一区二区| 成人小视频免费观看| 另类专区欧美蜜桃臀第一页| 一区二区三区毛片| 国产精品视频第一区| 精品捆绑美女sm三区| 欧美日产国产精品| 一本到不卡精品视频在线观看| 国产精品1区2区| 青青青伊人色综合久久| 亚洲国产精品尤物yw在线观看| 国产精品久久精品日日| 偷拍日韩校园综合在线| 高清视频一区二区| 国产福利一区在线观看| 日韩成人一区二区三区在线观看| 一区免费观看视频| 久久久久9999亚洲精品| 91精品黄色片免费大全| 色美美综合视频| www.视频一区| 国产成人免费xxxxxxxx| 国产一区二区视频在线| 免费在线欧美视频| 首页亚洲欧美制服丝腿| 亚洲一区二区在线观看视频| 亚洲欧美色综合| 国产精品九色蝌蚪自拍| 国产免费观看久久| 久久久99精品久久| 26uuu精品一区二区 | 色悠悠久久综合| 成人免费视频caoporn| 国产乱人伦精品一区二区在线观看| 老司机免费视频一区二区| 日本三级亚洲精品| 午夜久久久久久| 亚洲成av人片在线观看| 亚洲福利视频三区| 亚洲国产一区在线观看| 亚洲一区二区三区在线看| 亚洲精品ww久久久久久p站| 亚洲手机成人高清视频| 亚洲少妇中出一区| 亚洲免费色视频| 一区二区三区四区在线播放| 亚洲欧美经典视频| 亚洲精品欧美在线| 樱桃国产成人精品视频| 久久众筹精品私拍模特| 国产伦精品一区二区三区免费| 欧美三级中文字幕| 在线一区二区三区做爰视频网站| 91麻豆免费观看| 欧美性猛交一区二区三区精品| 在线精品视频免费播放| 欧美色倩网站大全免费| 欧美日韩国产另类一区| 7777精品伊人久久久大香线蕉经典版下载| 欧美精品在线一区二区三区| 欧美日韩电影在线播放| 在线播放国产精品二区一二区四区| 在线电影一区二区三区| 日韩西西人体444www| 久久久精品影视| 国产精品福利一区二区| 亚洲免费伊人电影| 天天av天天翘天天综合网| 亚洲成a人v欧美综合天堂| 麻豆久久久久久| 成人永久aaa| 色综合色狠狠综合色| 欧美日韩国产另类不卡| 欧美成人激情免费网| 亚洲国产成人在线| 亚洲免费观看高清完整版在线观看熊| 亚洲国产一区二区视频| 男男成人高潮片免费网站| 国产成人亚洲综合a∨猫咪 | 国产综合久久久久久鬼色| 国产成人综合亚洲网站| 色猫猫国产区一区二在线视频| 欧美日韩精品系列| 精品国产免费一区二区三区四区| 国产清纯在线一区二区www| 中文字幕综合网| 日本亚洲欧美天堂免费| 国产91在线看| 欧美在线观看一二区| 精品福利在线导航| 综合激情网...| 视频一区二区三区在线| 国产精品99久| 欧美日韩在线亚洲一区蜜芽| 精品国产成人在线影院 | 国产成人一级电影| 日本伦理一区二区| 亚洲精品在线观看视频| 亚洲另类春色国产| 激情图区综合网| 日本高清视频一区二区| 久久综合色8888| 一区二区三区不卡在线观看| 黄色成人免费在线| 色综合激情五月| 久久亚洲精品小早川怜子| 一区二区三区欧美亚洲| 精彩视频一区二区三区| 91福利视频网站| 2021中文字幕一区亚洲| 亚洲综合激情小说| 国产精品91xxx| 7777精品伊人久久久大香线蕉完整版 | 国产一区二区三区免费播放 | 91麻豆精品在线观看| 欧美大尺度电影在线| 一区二区三区四区中文字幕| 国产中文一区二区三区| 在线免费亚洲电影| 国产亚洲人成网站| 视频在线观看一区| 99麻豆久久久国产精品免费| 欧美一区二区福利视频| 亚洲精选视频免费看| 国产传媒久久文化传媒| 欧美一区二区三区喷汁尤物| 中文字幕日韩一区二区| 韩国一区二区三区| 欧美剧在线免费观看网站 | 99久久精品国产网站| 日韩欧美国产小视频| 一区二区成人在线观看| 国产精品一二三四区| 91精品蜜臀在线一区尤物| 亚洲精品免费在线观看| 成人综合婷婷国产精品久久蜜臀| 日韩欧美一级片| 亚洲va韩国va欧美va| 一本久道中文字幕精品亚洲嫩| 欧美国产禁国产网站cc| 精品一区二区影视|