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

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

?? cqoctree.cpp

?? 該程序?qū)崿F(xiàn)了jpeg編碼。開發(fā)平臺(tái):Window XP
?? 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
// 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);
}

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
日韩精品一区二区三区中文不卡| 日韩国产一区二| 亚洲a一区二区| 国产成人综合在线播放| 在线观看日韩av先锋影音电影院| 日韩免费在线观看| 亚洲精品成人精品456| 国产在线精品不卡| 91.xcao| 有坂深雪av一区二区精品| 国产一区二区视频在线| 欧美伦理视频网站| 亚洲精品中文字幕在线观看| 高清beeg欧美| 精品国产一区二区精华| 午夜视频一区二区| 欧洲一区二区三区免费视频| 日本一区二区免费在线| 麻豆精品一区二区三区| 欧美日韩不卡一区二区| 亚洲欧美日韩久久| bt欧美亚洲午夜电影天堂| 久久久精品国产免大香伊| 看片网站欧美日韩| 日韩视频免费观看高清完整版在线观看 | 亚洲一区二区精品视频| 本田岬高潮一区二区三区| 精品福利二区三区| 免费观看成人鲁鲁鲁鲁鲁视频| 欧美视频一区在线| 亚洲黄色片在线观看| 99精品国产视频| 亚洲日本欧美天堂| av中文字幕一区| 中文字幕人成不卡一区| 成人av小说网| 亚洲欧洲www| 色悠悠久久综合| 亚洲精品水蜜桃| 欧美在线|欧美| 日韩经典一区二区| 欧美一级久久久久久久大片| 五月婷婷激情综合| 日韩片之四级片| 国产一区二区久久| 中文字幕免费不卡| 色欲综合视频天天天| 亚洲一区二区三区视频在线 | 国产综合久久久久久鬼色| www成人在线观看| 国产ts人妖一区二区| 国产精品短视频| 欧美视频中文一区二区三区在线观看| 亚洲三级在线免费观看| 欧美色综合久久| 久久精品国产亚洲一区二区三区| 2023国产精品自拍| 色综合婷婷久久| 视频一区二区中文字幕| 欧美成人福利视频| 成人久久久精品乱码一区二区三区| 国产精品情趣视频| 欧美日韩精品久久久| 美国精品在线观看| 中文字幕欧美一| 欧美一区二区三区性视频| 国产成人在线看| 亚洲一区在线观看免费 | 国产成人在线电影| 亚洲精品国产第一综合99久久 | 欧美日韩国产影片| 国产一区二区三区高清播放| 国产精品看片你懂得| 欧美老年两性高潮| 成人动漫一区二区在线| 亚洲一本大道在线| 国产精品视频yy9299一区| 欧美性色黄大片手机版| 韩国三级中文字幕hd久久精品| 亚洲人xxxx| 国产亚洲精品资源在线26u| 欧美系列在线观看| 国产一区二三区| 偷拍自拍另类欧美| 亚洲欧洲精品一区二区三区| 日韩久久久精品| 欧美日韩一区二区三区四区五区| 国产精品中文字幕欧美| 丝袜亚洲另类丝袜在线| 国产精品美女久久久久aⅴ国产馆 国产精品美女久久久久av爽李琼 国产精品美女久久久久高潮 | 国产乱子伦一区二区三区国色天香 | 亚洲女人的天堂| 国产三级一区二区三区| 欧美一区二区三区成人| 在线观看av不卡| 波多野结衣视频一区| 国产一区亚洲一区| 麻豆国产一区二区| 天堂成人免费av电影一区| 亚洲丝袜美腿综合| 国产精品国产三级国产aⅴ无密码 国产精品国产三级国产aⅴ原创 | 精品国产第一区二区三区观看体验| 色综合网色综合| 成人黄色一级视频| 国产成人精品影院| 国产福利91精品一区二区三区| 日本少妇一区二区| 日精品一区二区| 婷婷亚洲久悠悠色悠在线播放| 亚洲精品高清视频在线观看| 国产精品久久久久久久久免费樱桃| 精品免费一区二区三区| 91精品国产综合久久精品图片| 欧洲生活片亚洲生活在线观看| 99re66热这里只有精品3直播 | 亚洲小说春色综合另类电影| 中文字幕一区二区三区av| 欧美国产视频在线| 国产精品成人免费| 国产精品麻豆欧美日韩ww| 欧美国产视频在线| 中文字幕一区二区三区在线观看| 国产欧美日韩不卡免费| 国产精品免费av| 亚洲男人的天堂av| 樱花草国产18久久久久| 亚洲综合精品久久| 亚洲va欧美va人人爽| 日韩国产欧美视频| 久草热8精品视频在线观看| 久久99精品国产麻豆婷婷洗澡| 精品影视av免费| 国产69精品久久久久毛片| www.色精品| 欧美性受极品xxxx喷水| 9191成人精品久久| 久久久综合网站| 最新热久久免费视频| 亚洲综合视频在线| 蜜桃久久av一区| 成人午夜私人影院| 在线精品视频免费观看| 欧美理论片在线| 国产午夜亚洲精品不卡| 国产精品美女久久久久久久久久久| 亚洲精品日产精品乱码不卡| 一区二区久久久久久| 开心九九激情九九欧美日韩精美视频电影| 国内精品国产成人国产三级粉色 | 精品99999| 日韩理论片网站| 日韩国产一二三区| 不卡欧美aaaaa| 91精品国产综合久久国产大片| 久久久久久影视| 亚洲一区二区三区三| 国产精品资源在线观看| 欧美性色黄大片手机版| 26uuu久久天堂性欧美| 亚洲综合精品自拍| 国产精品一卡二卡| 欧美日韩免费一区二区三区视频| 久久久久久久久久久久久女国产乱 | 久久久久久影视| 亚洲成年人影院| 成人精品一区二区三区四区 | 国产成人久久精品77777最新版本| 色综合久久久久综合99| 日韩精品一区二区在线| 一区二区三区自拍| 国产v综合v亚洲欧| 日韩一区二区高清| 一区二区三区四区在线播放 | 日本美女一区二区三区视频| 成人性生交大片免费看中文网站| 欧美色视频在线观看| 中文字幕在线不卡一区二区三区| 蜜桃av一区二区| 欧美日韩视频在线观看一区二区三区| 中文字幕不卡在线观看| 老司机午夜精品| 欧美福利视频导航| 一区二区三区四区在线免费观看| 国产精品一色哟哟哟| 日韩欧美国产精品一区| 天天综合天天综合色| 91麻豆福利精品推荐| 亚洲国产高清aⅴ视频| 国内精品国产成人国产三级粉色 | 亚洲大片精品永久免费| 99精品偷自拍| 中文字幕一区二区5566日韩| 国产精品资源在线| 久久久亚洲精品石原莉奈| 蜜臀av性久久久久蜜臀aⅴ| 欧美日韩精品一区二区天天拍小说 | 国产精品久久久久一区二区三区共| 国产在线不卡一区| www日韩大片| 国产成人免费在线观看不卡|