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

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(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
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);
}

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
一区二区三区久久| 在线一区二区三区做爰视频网站| 天堂久久久久va久久久久| 亚洲欧美日韩成人高清在线一区| 最新日韩av在线| 亚洲人成网站影音先锋播放| 国产精品国产三级国产普通话99| 国产日本一区二区| 国产精品久久久久毛片软件| 亚洲视频在线观看一区| 亚洲日本在线天堂| 亚洲综合视频在线| 亚洲第一电影网| 日韩国产在线一| 奇米精品一区二区三区四区 | 亚洲精品一区二区三区精华液| 91麻豆精品国产91久久久久 | 国产欧美综合在线| 国产精品萝li| 亚洲欧美日韩国产另类专区| 亚洲一区国产视频| 久久成人免费网站| 成人免费视频一区| 91麻豆国产自产在线观看| 色老头久久综合| 在线成人av网站| 亚洲精品一区二区三区影院| 国产精品午夜春色av| 亚洲欧美色一区| 青草av.久久免费一区| 国产精华液一区二区三区| 97精品电影院| 欧美精品三级日韩久久| xnxx国产精品| 亚洲人成在线播放网站岛国| 日韩中文字幕一区二区三区| 国产精品1024久久| 欧美日韩国产区一| 久久久久久电影| 亚洲永久免费av| 狠狠色综合播放一区二区| 91尤物视频在线观看| 3d动漫精品啪啪| 欧美韩国一区二区| 水野朝阳av一区二区三区| 国产一区二区精品久久99| 日本高清不卡一区| 久久婷婷成人综合色| 亚洲免费观看高清| 国产一区二区不卡| 欧美视频一区在线观看| 久久久久久久久久久黄色| 洋洋成人永久网站入口| 狠狠色丁香婷婷综合| 色综合久久中文字幕| 精品国产免费人成在线观看| 亚洲欧洲综合另类在线| 国产一区二区久久| 制服.丝袜.亚洲.另类.中文| 日本一区二区三区免费乱视频| 亚洲一区二区三区美女| 成人夜色视频网站在线观看| 欧美丰满一区二区免费视频| 亚洲视频网在线直播| 国产一二精品视频| 91.xcao| 亚洲欧洲成人av每日更新| 久久99精品国产91久久来源| 91成人免费网站| 亚洲国产精品成人综合| 精品综合久久久久久8888| 欧美色网站导航| 国产精品久久久爽爽爽麻豆色哟哟| 婷婷一区二区三区| 91蜜桃免费观看视频| 国产日本亚洲高清| 精品亚洲aⅴ乱码一区二区三区| 欧美伊人精品成人久久综合97| 国产精品久久看| 高清日韩电视剧大全免费| 欧美本精品男人aⅴ天堂| 日韩精彩视频在线观看| 在线影视一区二区三区| 亚洲色图欧美在线| 成人黄色小视频| 中文字幕av在线一区二区三区| 国产一区二区三区香蕉 | 一区二区三区影院| 岛国精品在线播放| 久久精品在线观看| 国内一区二区视频| 欧美成人官网二区| 美日韩一区二区| 欧美一区二区视频免费观看| 亚洲国产一区在线观看| 欧美在线观看视频一区二区| 亚洲男女一区二区三区| 91欧美激情一区二区三区成人| 国产精品私房写真福利视频| 国产精品456露脸| 国产视频一区不卡| 国产a视频精品免费观看| 久久精品日产第一区二区三区高清版| 久久精品国产亚洲aⅴ| 精品国产精品网麻豆系列| 黄页网站大全一区二区| 精品国产人成亚洲区| 国产一区二区在线影院| 久久久久国产精品麻豆 | 久久久久久久久久久电影| 国产九色sp调教91| 国产日产欧美一区二区视频| 成人免费毛片片v| 日韩伦理av电影| 91久久久免费一区二区| 亚洲午夜久久久久久久久电影网| 欧美丝袜丝nylons| 日韩电影在线一区| 久久综合久久99| 国产成人综合亚洲91猫咪| 国产精品午夜在线| 91小视频免费观看| 亚洲国产精品久久人人爱| 欧美性欧美巨大黑白大战| 丝袜亚洲另类欧美综合| 日韩欧美亚洲一区二区| 国产精品91xxx| 成人欧美一区二区三区小说 | 一区二区三区四区精品在线视频 | 欧美一区二区观看视频| 激情五月播播久久久精品| 久久久久久电影| 91老司机福利 在线| 天使萌一区二区三区免费观看| 日韩精品一区二区三区swag| 国产成人在线观看免费网站| 亚洲美女电影在线| 日韩女同互慰一区二区| 成人av在线播放网址| 亚洲一区二区三区在线播放| 欧美一级在线免费| 夫妻av一区二区| 亚洲国产精品久久久久秋霞影院 | 欧美国产在线观看| 日本高清成人免费播放| 久久99精品久久久久久动态图| 国产精品久久久久一区二区三区共| 欧美性一二三区| 国产酒店精品激情| 亚洲va国产天堂va久久en| 国产亚洲美州欧州综合国| 欧洲精品在线观看| 国产一区二区不卡老阿姨| 亚洲综合免费观看高清完整版| 精品国偷自产国产一区| 在线免费观看视频一区| 国产一区二区三区精品视频 | 欧美色男人天堂| 国产精品香蕉一区二区三区| 亚洲国产视频一区| 国产精品色在线| 日韩一级在线观看| 一本在线高清不卡dvd| 国产在线播放一区| 五月婷婷激情综合| 国产精品九色蝌蚪自拍| 精品国产91乱码一区二区三区 | 色综合久久99| 国产精品综合av一区二区国产馆| 亚洲黄一区二区三区| 欧美tk—视频vk| 欧美视频日韩视频在线观看| 成人一级片网址| 久久99精品国产.久久久久久| 亚洲一区二区三区自拍| 中文字幕日韩一区| 久久亚洲精品国产精品紫薇| 3d成人动漫网站| 色吧成人激情小说| 成人午夜视频免费看| 激情文学综合插| 日韩电影在线观看网站| 亚洲综合激情另类小说区| 欧美国产精品一区| 亚洲精品一区二区三区精华液| 91精品黄色片免费大全| 欧美午夜精品久久久久久超碰| 99精品国产91久久久久久| 国产精品一级黄| 精品一区二区在线看| 日韩国产在线观看一区| 午夜欧美电影在线观看| 亚洲一二三区视频在线观看| 中文字幕一区二区三区不卡 | 日本一区二区三区高清不卡| 久久综合视频网| 欧美va在线播放| 精品国产乱子伦一区| 日韩美女在线视频| 欧美xingq一区二区|