www.pudn.com > yuzhishuanfa.zip > COLOR.CPP


//Color.cpp  位图API,调色板和色彩 
// 
 
 
#include "stdafx.h" 
#include "dibapi.h" 
 
 
/**************************************************************************** 
 *                                                                          * 
 *  FUNCTION   : CreateBIPalette(LPBITMAPINFOHEADER lpbi)                   * 
 *                                                                          * 
 *  PURPOSE    : Given a Pointer to a BITMAPINFO struct will create a       * 
 *               a GDI palette object from the color table.                 * 
 *                                                                          * 
 *  RETURNS    : A handle to the palette.                                   * 
 *                                                                          * 
 ****************************************************************************/ 
HPALETTE WINAPI CreateBIPalette (LPBITMAPINFOHEADER lpbi) 
{ 
    LOGPALETTE          *pPal; 
    HPALETTE            hpal = NULL; 
    WORD                nNumColors; 
    BYTE                red; 
    BYTE                green; 
    BYTE                blue; 
    WORD                i; 
    RGBQUAD        FAR *pRgb; 
 
    if (!lpbi) 
        return NULL; 
 
    if (lpbi->biSize != sizeof(BITMAPINFOHEADER)) 
        return NULL; 
 
    /* Get a pointer to the color table and the number of colors in it */ 
    pRgb = (RGBQUAD FAR *)((LPSTR)lpbi + (WORD)lpbi->biSize); 
    nNumColors = DIBNumColors((LPSTR)lpbi); 
 
    if (nNumColors){ 
        /* Allocate for the logical palette structure */ 
        pPal = (LOGPALETTE*)LocalAlloc(LPTR,sizeof(LOGPALETTE) + nNumColors * sizeof(PALETTEENTRY)); 
        if (!pPal) 
            return NULL; 
 
        pPal->palNumEntries = nNumColors; 
        pPal->palVersion    = PALVERSION; 
 
        /* Fill in the palette entries from the DIB color table and 
         * create a logical color palette. 
         */ 
        for (i = 0; i < nNumColors; i++){ 
            pPal->palPalEntry[i].peRed   = pRgb[i].rgbRed; 
            pPal->palPalEntry[i].peGreen = pRgb[i].rgbGreen; 
            pPal->palPalEntry[i].peBlue  = pRgb[i].rgbBlue; 
            pPal->palPalEntry[i].peFlags = (BYTE)0; 
        } 
        hpal = CreatePalette(pPal); 
        LocalFree((HANDLE)pPal); 
    } 
    else if (lpbi->biBitCount == 24){ 
        /* A 24 bitcount DIB has no color table entries so, set the number of 
         * to the maximum value (256). 
         */ 
        nNumColors = 256; 
        pPal = (LOGPALETTE*)LocalAlloc(LPTR,sizeof(LOGPALETTE) + nNumColors * sizeof(PALETTEENTRY)); 
        if (!pPal) 
            return NULL; 
 
        pPal->palNumEntries = nNumColors; 
        pPal->palVersion    = PALVERSION; 
 
        red = green = blue = 0; 
 
        /* Generate 256 (= 8*8*4) RGB combinations to fill the palette 
         * entries. 
         */ 
        for (i = 0; i < pPal->palNumEntries; i++){ 
            pPal->palPalEntry[i].peRed   = red; 
            pPal->palPalEntry[i].peGreen = green; 
            pPal->palPalEntry[i].peBlue  = blue; 
            pPal->palPalEntry[i].peFlags = (BYTE)0; 
 
            if (!(red += 32)) 
                if (!(green += 32)) 
                    blue += 64; 
        } 
        hpal = CreatePalette(pPal); 
        LocalFree((HANDLE)pPal); 
    } 
    return hpal; 
}